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 31 2d 32 "2009-11-2
5750: 33 20 31 33 3a 31 37 3a 32 37 20 33 39 32 31 34 3 13:17:27 39214
5760: 61 65 65 36 35 35 33 64 62 37 36 33 30 39 38 35 aee6553db7630985
5770: 31 65 37 61 61 37 34 66 63 63 30 32 64 34 66 35 1e7aa74fcc02d4f5
5780: 39 62 37 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 9b7"../*.** 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 52 65 d back..**.** Re
13360 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
13370 48 31 32 32 32 31 5d 20 5b 48 31 32 32 32 33 5d H12221] [H12223]
13380 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 .**.** If a sepa
13390 72 61 74 65 20 74 68 72 65 61 64 20 70 65 72 66 rate thread perf
133a0 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45 orms a new [INSE
133b0 52 54 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a RT] on the same.
133c0 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
133d0 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65 ection while the
133e0 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 [sqlite3_last_i
133f0 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a nsert_rowid()].*
13400 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 * function is ru
13410 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75 73 20 63 nning and thus c
13420 68 61 6e 67 65 73 20 74 68 65 20 6c 61 73 74 20 hanges the last
13430 69 6e 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c 0a insert [rowid],.
13440 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 ** then the valu
13450 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 e returned by [s
13460 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 qlite3_last_inse
13470 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a rt_rowid()] is.*
13480 2a 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 * unpredictable
13490 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74 20 65 71 and might not eq
134a0 75 61 6c 20 65 69 74 68 65 72 20 74 68 65 20 6f ual either the o
134b0 6c 64 20 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a ld or the new.**
134c0 20 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f last insert [ro
134d0 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f wid]..*/.SQLITE_
134e0 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 API sqlite3_int6
134f0 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 4 sqlite3_last_i
13500 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 nsert_rowid(sqli
13510 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 te3*);../*.** CA
13520 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 68 PI3REF: Count Th
13530 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 e Number Of Rows
13540 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32 32 34 Modified {H1224
13550 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 0} <S10600>.**.*
13560 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
13570 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 returns the numb
13580 65 72 20 6f 66 20 64 61 74 61 62 61 73 65 20 72 er of database r
13590 6f 77 73 20 74 68 61 74 20 77 65 72 65 20 63 68 ows that were ch
135a0 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 anged.** or inse
135b0 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 rted or deleted
135c0 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 by the most rece
135d0 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 53 ntly completed S
135e0 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 QL statement.**
135f0 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 on the [database
13600 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 connection] spe
13610 63 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 cified by the fi
13620 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a rst parameter..*
13630 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74 * Only changes t
13640 68 61 74 20 61 72 65 20 64 69 72 65 63 74 6c 79 hat are directly
13650 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 specified by th
13660 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44 e [INSERT], [UPD
13670 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c ATE],.** or [DEL
13680 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61 ETE] statement a
13690 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78 re counted. Aux
136a0 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73 20 63 iliary changes c
136b0 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67 aused by.** trig
136c0 67 65 72 73 20 6f 72 20 5b 66 6f 72 65 69 67 6e gers or [foreign
136d0 20 6b 65 79 20 61 63 74 69 6f 6e 73 5d 20 61 72 key actions] ar
136e0 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 55 e not counted. U
136f0 73 65 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 se the.** [sqlit
13700 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 e3_total_changes
13710 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 ()] function to
13720 66 69 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 6e find the total n
13730 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 umber of changes
13740 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 .** including ch
13750 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 anges caused by
13760 74 72 69 67 67 65 72 73 20 61 6e 64 20 66 6f 72 triggers and for
13770 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 eign key actions
13780 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 ..**.** Changes
13790 74 6f 20 61 20 76 69 65 77 20 74 68 61 74 20 61 to a view that a
137a0 72 65 20 73 69 6d 75 6c 61 74 65 64 20 62 79 20 re simulated by
137b0 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 an [INSTEAD OF t
137c0 72 69 67 67 65 72 5d 0a 2a 2a 20 61 72 65 20 6e rigger].** are n
137d0 6f 74 20 63 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c ot counted. Onl
137e0 79 20 72 65 61 6c 20 74 61 62 6c 65 20 63 68 61 y real table cha
137f0 6e 67 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 nges are counted
13800 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 ..**.** A "row c
13810 68 61 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e hange" is a chan
13820 67 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 ge to a single r
13830 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 ow of a single t
13840 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 able.** caused b
13850 79 20 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c y an INSERT, DEL
13860 45 54 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 ETE, or UPDATE s
13870 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 tatement. Rows
13880 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e that.** are chan
13890 67 65 64 20 61 73 20 73 69 64 65 20 65 66 66 65 ged as side effe
138a0 63 74 73 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d cts of [REPLACE]
138b0 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f constraint reso
138c0 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 lution,.** rollb
138d0 61 63 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 ack, ABORT proce
138e0 73 73 69 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42 ssing, [DROP TAB
138f0 4c 45 5d 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f LE], or by any o
13900 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 ther.** mechanis
13910 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 ms do not count
13920 61 73 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 as direct row ch
13930 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 anges..**.** A "
13940 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 trigger context"
13950 20 69 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 is a scope of e
13960 78 65 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 xecution that be
13970 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 gins and.** ends
13980 20 77 69 74 68 20 74 68 65 20 73 63 72 69 70 74 with the script
13990 20 6f 66 20 61 20 5b 43 52 45 41 54 45 20 54 52 of a [CREATE TR
139a0 49 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d IGGER | trigger]
139b0 2e 20 0a 2a 2a 20 4d 6f 73 74 20 53 51 4c 20 73 . .** Most SQL s
139c0 74 61 74 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a tatements are.**
139d0 20 65 76 61 6c 75 61 74 65 64 20 6f 75 74 73 69 evaluated outsi
139e0 64 65 20 6f 66 20 61 6e 79 20 74 72 69 67 67 65 de of any trigge
139f0 72 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 r. This is the
13a00 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 "top level".** t
13a10 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 rigger context.
13a20 20 49 66 20 61 20 74 72 69 67 67 65 72 20 66 69 If a trigger fi
13a30 72 65 73 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 res from the top
13a40 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 level, a.** new
13a50 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 trigger context
13a60 20 69 73 20 65 6e 74 65 72 65 64 20 66 6f 72 20 is entered for
13a70 74 68 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 the duration of
13a80 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 that one.** trig
13a90 67 65 72 2e 20 20 53 75 62 74 72 69 67 67 65 72 ger. Subtrigger
13aa0 73 20 63 72 65 61 74 65 20 73 75 62 63 6f 6e 74 s create subcont
13ab0 65 78 74 73 20 66 6f 72 20 74 68 65 69 72 20 64 exts for their d
13ac0 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 uration..**.** C
13ad0 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f alling [sqlite3_
13ae0 65 78 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 exec()] or [sqli
13af0 74 65 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75 te3_step()] recu
13b00 72 73 69 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 rsively does.**
13b10 6e 6f 74 20 63 72 65 61 74 65 20 61 20 6e 65 77 not create a new
13b20 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 trigger context
13b30 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
13b40 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 ction returns th
13b50 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 72 65 e number of dire
13b60 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 20 69 ct row changes i
13b70 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 n the.** most re
13b80 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 cent INSERT, UPD
13b90 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 20 73 ATE, or DELETE s
13ba0 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 tatement within
13bb0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 72 69 67 the same.** trig
13bc0 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a ger context..**.
13bd0 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e 20 63 61 ** Thus, when ca
13be0 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f lled from the to
13bf0 70 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 66 75 p level, this fu
13c00 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 nction returns t
13c10 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
13c20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d changes in the m
13c30 6f 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 ost recent INSER
13c40 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 T, UPDATE, or DE
13c50 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 61 6c 73 LETE.** that als
13c60 6f 20 6f 63 63 75 72 72 65 64 20 61 74 20 74 68 o occurred at th
13c70 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20 20 57 69 e top level. Wi
13c80 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 thin the body of
13c90 20 61 20 74 72 69 67 67 65 72 2c 0a 2a 2a 20 74 a trigger,.** t
13ca0 68 65 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 he sqlite3_chang
13cb0 65 73 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 es() interface c
13cc0 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 an be called to
13cd0 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 find the number
13ce0 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e of.** changes in
13cf0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 the most recent
13d00 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 ly completed INS
13d10 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 ERT, UPDATE, or
13d20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d DELETE.** statem
13d30 65 6e 74 20 77 69 74 68 69 6e 20 74 68 65 20 62 ent within the b
13d40 6f 64 79 20 6f 66 20 74 68 65 20 73 61 6d 65 20 ody of the same
13d50 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 trigger..** Howe
13d60 76 65 72 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 ver, the number
13d70 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f returned does no
13d80 74 20 69 6e 63 6c 75 64 65 20 63 68 61 6e 67 65 t include change
13d90 73 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20 73 s.** caused by s
13da0 75 62 74 72 69 67 67 65 72 73 20 73 69 6e 63 65 ubtriggers since
13db0 20 74 68 6f 73 65 20 68 61 76 65 20 74 68 65 69 those have thei
13dc0 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a r own context..*
13dd0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 *.** See also th
13de0 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c e [sqlite3_total
13df0 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 _changes()] inte
13e00 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a rface and the.**
13e10 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 [count_changes
13e20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 pragma]..**.** R
13e30 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
13e40 5b 48 31 32 32 34 31 5d 20 5b 48 31 32 32 34 33 [H12241] [H12243
13e50 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 ].**.** If a sep
13e60 61 72 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b arate thread mak
13e70 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 es changes on th
13e80 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
13e90 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 connection.** wh
13ea0 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 ile [sqlite3_cha
13eb0 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 nges()] is runni
13ec0 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 ng then the valu
13ed0 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 e returned.** is
13ee0 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 unpredictable a
13ef0 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 nd not meaningfu
13f00 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 l..*/.SQLITE_API
13f10 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 int sqlite3_cha
13f20 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a nges(sqlite3*);.
13f30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
13f40 20 54 6f 74 61 6c 20 4e 75 6d 62 65 72 20 4f 66 Total Number Of
13f50 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b Rows Modified {
13f60 48 31 32 32 36 30 7d 20 3c 53 31 30 36 30 30 3e H12260} <S10600>
13f70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 .**.** This func
13f80 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 tion returns the
13f90 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 20 63 number of row c
13fa0 68 61 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 hanges caused by
13fb0 20 5b 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 [INSERT],.** [U
13fc0 50 44 41 54 45 5d 20 6f 72 20 5b 44 45 4c 45 54 PDATE] or [DELET
13fd0 45 5d 20 73 74 61 74 65 6d 65 6e 74 73 20 73 69 E] statements si
13fe0 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62 61 73 nce the [databas
13ff0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 e connection] wa
14000 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 s opened..** The
14010 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65 73 20 count includes
14020 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72 6f 6d all changes from
14030 20 61 6c 6c 20 5b 43 52 45 41 54 45 20 54 52 49 all [CREATE TRI
14040 47 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 20 GGER | trigger]
14050 0a 2a 2a 20 63 6f 6e 74 65 78 74 73 20 61 6e 64 .** contexts and
14060 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
14070 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 [foreign key ac
14080 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 65 72 2c tions]. However,
14090 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f .** the count do
140a0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 es not include c
140b0 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 hanges used to i
140c0 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 mplement [REPLAC
140d0 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a E] constraints,.
140e0 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 ** do rollbacks
140f0 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 or ABORT process
14100 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 ing, or [DROP TA
14110 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e BLE] processing.
14120 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 The.** count d
14130 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 oes not include
14140 72 6f 77 73 20 6f 66 20 76 69 65 77 73 20 74 68 rows of views th
14150 61 74 20 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 at fire an [INST
14160 45 41 44 20 4f 46 20 74 72 69 67 67 65 72 5d 2c EAD OF trigger],
14170 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66 20 74 68 .** though if th
14180 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 e INSTEAD OF tri
14190 67 67 65 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 gger makes chang
141a0 65 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 es of its own, t
141b0 68 6f 73 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a hose changes .**
141c0 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a are counted..**
141d0 20 54 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 The changes are
141e0 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e counted as soon
141f0 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e as the statemen
14200 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 t that makes the
14210 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 m is.** complete
14220 64 20 28 77 68 65 6e 20 74 68 65 20 73 74 61 74 d (when the stat
14230 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 ement handle is
14240 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 passed to [sqlit
14250 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a e3_reset()] or.*
14260 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c * [sqlite3_final
14270 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 ize()])..**.** S
14280 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c ee also the [sql
14290 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 ite3_changes()]
142a0 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74 68 interface and th
142b0 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e e.** [count_chan
142c0 67 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a ges pragma]..**.
142d0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
142e0 0a 2a 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 .** [H12261] [H1
142f0 32 32 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 2263].**.** If a
14300 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64 separate thread
14310 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f makes changes o
14320 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 n the same datab
14330 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a ase connection.*
14340 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 * while [sqlite3
14350 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 _total_changes()
14360 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 ] is running the
14370 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 n the value.** r
14380 65 74 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65 eturned is unpre
14390 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 dictable and not
143a0 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a meaningful..*/.
143b0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
143c0 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 qlite3_total_cha
143d0 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a nges(sqlite3*);.
143e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
143f0 20 49 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e Interrupt A Lon
14400 67 2d 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 g-Running Query
14410 7b 48 31 32 32 37 30 7d 20 3c 53 33 30 35 30 30 {H12270} <S30500
14420 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e >.**.** This fun
14430 63 74 69 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 ction causes any
14440 20 70 65 6e 64 69 6e 67 20 64 61 74 61 62 61 73 pending databas
14450 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 e operation to a
14460 62 6f 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 bort and.** retu
14470 72 6e 20 61 74 20 69 74 73 20 65 61 72 6c 69 65 rn at its earlie
14480 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 st opportunity.
14490 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
144a0 74 79 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c typically.** cal
144b0 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 led in response
144c0 74 6f 20 61 20 75 73 65 72 20 61 63 74 69 6f 6e to a user action
144d0 20 73 75 63 68 20 61 73 20 70 72 65 73 73 69 6e such as pressin
144e0 67 20 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 g "Cancel".** or
144f0 20 43 74 72 6c 2d 43 20 77 68 65 72 65 20 74 68 Ctrl-C where th
14500 65 20 75 73 65 72 20 77 61 6e 74 73 20 61 20 6c e user wants a l
14510 6f 6e 67 20 71 75 65 72 79 20 6f 70 65 72 61 74 ong query operat
14520 69 6f 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 ion to halt.** i
14530 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a mmediately..**.*
14540 2a 20 49 74 20 69 73 20 73 61 66 65 20 74 6f 20 * It is safe to
14550 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e call this routin
14560 65 20 66 72 6f 6d 20 61 20 74 68 72 65 61 64 20 e from a thread
14570 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 different from t
14580 68 65 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 61 he.** thread tha
14590 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 t is currently r
145a0 75 6e 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62 unning the datab
145b0 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 ase operation.
145c0 42 75 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 But it.** is not
145d0 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 safe to call th
145e0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 is routine with
145f0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
14600 65 63 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 ection] that.**
14610 69 73 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 is closed or mig
14620 68 74 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 ht close before
14630 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
14640 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a t() returns..**.
14650 2a 2a 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 ** If an SQL ope
14660 72 61 74 69 6f 6e 20 69 73 20 76 65 72 79 20 6e ration is very n
14670 65 61 72 6c 79 20 66 69 6e 69 73 68 65 64 20 61 early finished a
14680 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 0a t the time when.
14690 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 ** sqlite3_inter
146a0 72 75 70 74 28 29 20 69 73 20 63 61 6c 6c 65 64 rupt() is called
146b0 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 , then it might
146c0 6e 6f 74 20 68 61 76 65 20 61 6e 20 6f 70 70 6f not have an oppo
146d0 72 74 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 rtunity.** to be
146e0 20 69 6e 74 65 72 72 75 70 74 65 64 20 61 6e 64 interrupted and
146f0 20 6d 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 might continue
14700 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a to completion..*
14710 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 *.** An SQL oper
14720 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e ation that is in
14730 74 65 72 72 75 70 74 65 64 20 77 69 6c 6c 20 72 terrupted will r
14740 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e eturn [SQLITE_IN
14750 54 45 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 TERRUPT]..** If
14760 74 68 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 the interrupted
14770 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 SQL operation is
14780 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 an INSERT, UPDA
14790 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a TE, or DELETE.**
147a0 20 74 68 61 74 20 69 73 20 69 6e 73 69 64 65 20 that is inside
147b0 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e an explicit tran
147c0 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 saction, then th
147d0 65 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 e entire transac
147e0 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 tion.** will be
147f0 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f rolled back auto
14800 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a matically..**.**
14810 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 The sqlite3_int
14820 65 72 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69 errupt(D) call i
14830 73 20 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69 s in effect unti
14840 6c 20 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 l all currently
14850 72 75 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 running.** SQL s
14860 74 61 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 tatements on [da
14870 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
14880 6e 5d 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 n] D complete.
14890 41 6e 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74 Any new SQL stat
148a0 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 ements.** that a
148b0 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 re started after
148c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 the sqlite3_int
148d0 65 72 72 75 70 74 28 29 20 63 61 6c 6c 20 61 6e errupt() call an
148e0 64 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a d before the .**
148f0 20 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 running stateme
14900 6e 74 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f nts reaches zero
14910 20 61 72 65 20 69 6e 74 65 72 72 75 70 74 65 64 are interrupted
14920 20 61 73 20 69 66 20 74 68 65 79 20 68 61 64 20 as if they had
14930 62 65 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 been.** running
14940 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 71 6c prior to the sql
14950 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
14960 20 63 61 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 call. New SQL
14970 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 statements.** th
14980 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20 61 at are started a
14990 66 74 65 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 fter the running
149a0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 statement count
149b0 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 reaches zero ar
149c0 65 0a 2a 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 e.** not effecte
149d0 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 d by the sqlite3
149e0 5f 69 6e 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a _interrupt()..**
149f0 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 A call to sqlit
14a00 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 e3_interrupt(D)
14a10 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 65 6e that occurs when
14a20 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 75 there are no ru
14a30 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 nning.** SQL sta
14a40 74 65 6d 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d tements is a no-
14a50 6f 70 20 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 op and has no ef
14a60 66 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 fect on SQL stat
14a70 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 ements.** that a
14a80 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65 72 re started after
14a90 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 the sqlite3_int
14aa0 65 72 72 75 70 74 28 29 20 63 61 6c 6c 20 72 65 errupt() call re
14ab0 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 turns..**.** Req
14ac0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
14ad0 31 32 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 12271] [H12272].
14ae0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 **.** If the dat
14af0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14b00 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 closes while [s
14b10 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
14b20 28 29 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e ()].** is runnin
14b30 67 20 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 g then bad thing
14b40 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 s will likely ha
14b50 70 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ppen..*/.SQLITE_
14b60 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
14b70 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 _interrupt(sqlit
14b80 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 e3*);../*.** CAP
14b90 49 33 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 I3REF: Determine
14ba0 20 49 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 If An SQL State
14bb0 6d 65 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 ment Is Complete
14bc0 20 7b 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 {H10510} <S7020
14bd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 0>.**.** These r
14be0 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 outines are usef
14bf0 75 6c 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e ul during comman
14c00 64 2d 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 d-line input to
14c10 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 determine if the
14c20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e .** currently en
14c30 74 65 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 tered text seems
14c40 20 74 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c to form a compl
14c50 65 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e ete SQL statemen
14c60 74 20 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 t or.** if addit
14c70 69 6f 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e ional input is n
14c80 65 65 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e eeded before sen
14c90 64 69 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e ding the text in
14ca0 74 6f 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 to.** SQLite for
14cb0 20 70 61 72 73 69 6e 67 2e 20 20 54 68 65 73 65 parsing. These
14cc0 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e routines return
14cd0 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 1 if the input
14ce0 73 74 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 string.** appear
14cf0 73 20 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 s to be a comple
14d00 74 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 te SQL statement
14d10 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 . A statement i
14d20 73 20 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a s judged to be.*
14d30 2a 20 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 * complete if it
14d40 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d ends with a sem
14d50 69 63 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 icolon token and
14d60 20 69 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 is not a prefix
14d70 20 6f 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f of a.** well-fo
14d80 72 6d 65 64 20 43 52 45 41 54 45 20 54 52 49 47 rmed CREATE TRIG
14d90 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 GER statement.
14da0 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 Semicolons that
14db0 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69 74 are embedded wit
14dc0 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 hin.** string li
14dd0 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 terals or quoted
14de0 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 identifier name
14df0 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 s or comments ar
14e00 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e e not.** indepen
14e10 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 dent tokens (the
14e20 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 y are part of th
14e30 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 e token in which
14e40 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 they are.** emb
14e50 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73 20 edded) and thus
14e60 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 do not count as
14e70 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d a statement term
14e80 69 6e 61 74 6f 72 2e 20 20 57 68 69 74 65 73 70 inator. Whitesp
14e90 61 63 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 ace.** and comme
14ea0 6e 74 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 nts that follow
14eb0 74 68 65 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f the final semico
14ec0 6c 6f 6e 20 61 72 65 20 69 67 6e 6f 72 65 64 2e lon are ignored.
14ed0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 .**.** These rou
14ee0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 30 20 69 tines return 0 i
14ef0 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 f the statement
14f00 69 73 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 is incomplete.
14f10 49 66 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 If a.** memory a
14f20 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
14f30 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d then SQLITE_NOM
14f40 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a EM is returned..
14f50 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 **.** These rout
14f60 69 6e 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 ines do not pars
14f70 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d e the SQL statem
14f80 65 6e 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c ents thus.** wil
14f90 6c 20 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e l not detect syn
14fa0 74 61 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 tactically incor
14fb0 72 65 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 rect SQL..**.**
14fc0 49 66 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f If SQLite has no
14fd0 74 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a t been initializ
14fe0 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ed using [sqlite
14ff0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 3_initialize()]
15000 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 prior .** to inv
15010 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f oking sqlite3_co
15020 6d 70 6c 65 74 65 31 36 28 29 20 74 68 65 6e 20 mplete16() then
15030 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
15040 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a ze() is invoked.
15050 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ** automatically
15060 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 by sqlite3_comp
15070 6c 65 74 65 31 36 28 29 2e 20 20 49 66 20 74 68 lete16(). If th
15080 61 74 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f at initializatio
15090 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e n fails,.** then
150a0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 the return valu
150b0 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 e from sqlite3_c
150c0 6f 6d 70 6c 65 74 65 31 36 28 29 20 77 69 6c 6c omplete16() will
150d0 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 be non-zero.**
150e0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 regardless of wh
150f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
15100 20 69 6e 70 75 74 20 53 51 4c 20 69 73 20 63 6f input SQL is co
15110 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 mplete..**.** Re
15120 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 quirements: [H10
15130 35 31 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 511] [H10512].**
15140 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f .** The input to
15150 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 [sqlite3_comple
15160 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 te()] must be a
15170 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a zero-terminated.
15180 2a 2a 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e ** UTF-8 string.
15190 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 .**.** The input
151a0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d to [sqlite3_com
151b0 70 6c 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20 plete16()] must
151c0 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e be a zero-termin
151d0 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 ated.** UTF-16 s
151e0 74 72 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 tring in native
151f0 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 byte order..*/.S
15200 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
15210 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 lite3_complete(c
15220 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b onst char *sql);
15230 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
15240 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 sqlite3_complete
15250 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 16(const void *s
15260 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 ql);../*.** CAPI
15270 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 3REF: Register A
15280 20 43 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e Callback To Han
15290 64 6c 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 dle SQLITE_BUSY
152a0 45 72 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 Errors {H12310}
152b0 3c 53 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S40400>.**.** T
152c0 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 his routine sets
152d0 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 a callback func
152e0 74 69 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20 tion that might
152f0 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 be invoked whene
15300 76 65 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 ver.** an attemp
15310 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 t is made to ope
15320 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 n a database tab
15330 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 le that another
15340 74 68 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f thread.** or pro
15350 63 65 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e cess has locked.
15360 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 .**.** If the bu
15370 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e sy callback is N
15380 55 4c 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 ULL, then [SQLIT
15390 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 E_BUSY] or [SQLI
153a0 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
153b0 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 ].** is returned
153c0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f immediately upo
153d0 6e 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 n encountering t
153e0 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 he lock. If the
153f0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a busy callback.**
15400 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 is not NULL, th
15410 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 en the callback
15420 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 will be invoked
15430 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e with two argumen
15440 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ts..**.** The fi
15450 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 rst argument to
15460 74 68 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61 the handler is a
15470 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 copy of the voi
15480 64 2a 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 d* pointer which
15490 0a 2a 2a 20 69 73 20 74 68 65 20 74 68 69 72 64 .** is the third
154a0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
154b0 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 ite3_busy_handle
154c0 72 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 r(). The second
154d0 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 argument to.**
154e0 74 68 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c the handler call
154f0 62 61 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 back is the numb
15500 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
15510 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
15520 72 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e r has.** been in
15530 76 6f 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c voked for this l
15540 6f 63 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 ocking event. I
15550 66 20 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 f the.** busy ca
15560 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 llback returns 0
15570 2c 20 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 , then no additi
15580 6f 6e 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 onal attempts ar
15590 65 20 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 e made to.** acc
155a0 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 ess the database
155b0 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 and [SQLITE_BUS
155c0 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f Y] or [SQLITE_IO
155d0 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 ERR_BLOCKED] is
155e0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 returned..** If
155f0 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 the callback ret
15600 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 urns non-zero, t
15610 68 65 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65 hen another atte
15620 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 mpt.** is made t
15630 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 o open the datab
15640 61 73 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 ase for reading
15650 61 6e 64 20 74 68 65 20 63 79 63 6c 65 20 72 65 and the cycle re
15660 70 65 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 peats..**.** The
15670 20 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62 presence of a b
15680 75 73 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 usy handler does
15690 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74 not guarantee t
156a0 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69 hat it will be i
156b0 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 nvoked.** when t
156c0 68 65 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e here is lock con
156d0 74 65 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 tention. If SQLi
156e0 74 65 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 te determines th
156f0 61 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 at invoking the
15700 62 75 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 busy.** handler
15710 63 6f 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 could result in
15720 61 20 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 a deadlock, it w
15730 69 6c 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 ill go ahead and
15740 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
15750 42 55 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c BUSY].** or [SQL
15760 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
15770 44 5d 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e D] instead of in
15780 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 voking the busy
15790 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 handler..** Cons
157a0 69 64 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20 ider a scenario
157b0 77 68 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 where one proces
157c0 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 s is holding a r
157d0 65 61 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a ead lock that.**
157e0 20 69 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f it is trying to
157f0 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 promote to a re
15800 73 65 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a served lock and.
15810 2a 2a 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 ** a second proc
15820 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 ess is holding a
15830 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 reserved lock t
15840 68 61 74 20 69 74 20 69 73 20 74 72 79 69 6e 67 hat it is trying
15850 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 .** to promote t
15860 6f 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c o an exclusive l
15870 6f 63 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20 ock. The first
15880 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 process cannot p
15890 72 6f 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 roceed.** becaus
158a0 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 e it is blocked
158b0 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e by the second an
158c0 64 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f d the second pro
158d0 63 65 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 cess cannot.** p
158e0 72 6f 63 65 65 64 20 62 65 63 61 75 73 65 20 69 roceed because i
158f0 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 t is blocked by
15900 74 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 the first. If b
15910 6f 74 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a oth processes.**
15920 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 invoke the busy
15930 20 68 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 handlers, neith
15940 65 72 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 er will make any
15950 20 70 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 progress. Ther
15960 65 66 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 efore,.** SQLite
15970 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
15980 5f 42 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66 _BUSY] for the f
15990 69 72 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f irst process, ho
159a0 70 69 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a ping that this.*
159b0 2a 20 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 * will induce th
159c0 65 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 e first process
159d0 74 6f 20 72 65 6c 65 61 73 65 20 69 74 73 20 72 to release its r
159e0 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c ead lock and all
159f0 6f 77 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 ow.** the second
15a00 20 70 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 process to proc
15a10 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 eed..**.** The d
15a20 65 66 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c efault busy call
15a30 62 61 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a back is NULL..**
15a40 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f .** The [SQLITE_
15a50 42 55 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63 BUSY] error is c
15a60 6f 6e 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c onverted to [SQL
15a70 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 ITE_IOERR_BLOCKE
15a80 44 5d 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 D].** when SQLit
15a90 65 20 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64 e is in the midd
15aa0 6c 65 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72 le of a large tr
15ab0 61 6e 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20 ansaction where
15ac0 61 6c 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 all the.** chang
15ad0 65 73 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 es will not fit
15ae0 69 6e 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f into the in-memo
15af0 72 79 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74 ry cache. SQLit
15b00 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 e will.** alread
15b10 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 y hold a RESERVE
15b20 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 D lock on the da
15b30 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 tabase file, but
15b40 20 69 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 it needs.** to
15b50 70 72 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 promote this loc
15b60 6b 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73 k to EXCLUSIVE s
15b70 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 73 70 o that it can sp
15b80 69 6c 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 ill cache.** pag
15b90 65 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 es into the data
15ba0 62 61 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 base file withou
15bb0 74 20 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 t harm to concur
15bc0 72 65 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e rent.** readers.
15bd0 20 20 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c If it is unabl
15be0 65 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 e to promote the
15bf0 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 lock, then the
15c00 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 in-memory.** cac
15c10 68 65 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 he will be left
15c20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 in an inconsiste
15c30 6e 74 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20 nt state and so
15c40 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 the error.** cod
15c50 65 20 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 e is promoted fr
15c60 6f 6d 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c om the relativel
15c70 79 20 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 y benign [SQLITE
15c80 5f 42 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 _BUSY] to.** the
15c90 20 6d 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51 more severe [SQ
15ca0 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b LITE_IOERR_BLOCK
15cb0 45 44 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72 ED]. This error
15cc0 20 63 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a code promotion.
15cd0 2a 2a 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74 ** forces an aut
15ce0 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 omatic rollback
15cf0 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20 of the changes.
15d00 20 53 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 See the.** <a h
15d10 72 65 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 ref="/cvstrac/wi
15d20 6b 69 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 ki?p=CorruptionF
15d30 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f ollowingBusyErro
15d40 72 22 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f r">.** Corruptio
15d50 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 nFollowingBusyEr
15d60 72 6f 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 ror</a> wiki pag
15d70 65 20 66 6f 72 20 61 20 64 69 73 63 75 73 73 69 e for a discussi
15d80 6f 6e 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 on of why.** thi
15d90 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a s is important..
15da0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 **.** There can
15db0 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 only be a single
15dc0 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65 busy handler de
15dd0 66 69 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a fined for each.*
15de0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e * [database conn
15df0 65 63 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e ection]. Settin
15e00 67 20 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e g a new busy han
15e10 64 6c 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a dler clears any.
15e20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 ** previously se
15e30 74 20 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 t handler. Note
15e40 20 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 that calling [s
15e50 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 qlite3_busy_time
15e60 6f 75 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 out()].** will a
15e70 6c 73 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 lso set or clear
15e80 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 the busy handle
15e90 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 r..**.** The bus
15ea0 79 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c y callback shoul
15eb0 64 20 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 d not take any a
15ec0 63 74 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 ctions which mod
15ed0 69 66 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 ify the.** datab
15ee0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
15ef0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
15f00 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 busy handler. A
15f10 6e 79 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a ny such actions.
15f20 2a 2a 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 ** result in und
15f30 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e efined behavior.
15f40 0a 2a 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d .** .** Requirem
15f50 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 ents:.** [H12311
15f60 5d 20 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33 ] [H12312] [H123
15f70 31 34 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 14] [H12316] [H1
15f80 32 33 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 2318].**.** A bu
15f90 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 sy handler must
15fa0 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 not close the da
15fb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
15fc0 6e 0a 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 n.** or [prepare
15fd0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 d statement] tha
15fe0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 t invoked the bu
15ff0 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 sy handler..*/.S
16000 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
16010 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c lite3_busy_handl
16020 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 er(sqlite3*, int
16030 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 (*)(void*,int),
16040 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void*);../*.** C
16050 41 50 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 API3REF: Set A B
16060 75 73 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 usy Timeout {H12
16070 33 34 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 340} <S40410>.**
16080 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
16090 20 73 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 sets a [sqlite3
160a0 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 _busy_handler |
160b0 62 75 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 busy handler] th
160c0 61 74 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 at sleeps.** for
160d0 20 61 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f a specified amo
160e0 75 6e 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e unt of time when
160f0 20 61 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b a table is lock
16100 65 64 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 ed. The handler
16110 0a 2a 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d .** will sleep m
16120 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e ultiple times un
16130 74 69 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 til at least "ms
16140 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f " milliseconds o
16150 66 20 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 f sleeping.** ha
16160 76 65 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 ve accumulated.
16170 7b 48 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 {H12343} After "
16180 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 ms" milliseconds
16190 20 6f 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a of sleeping,.**
161a0 20 74 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 the handler ret
161b0 75 72 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 urns 0 which cau
161c0 73 65 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ses [sqlite3_ste
161d0 70 28 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a p()] to return.*
161e0 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 * [SQLITE_BUSY]
161f0 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 or [SQLITE_IOERR
16200 5f 42 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a _BLOCKED]..**.**
16210 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f Calling this ro
16220 75 74 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 utine with an ar
16230 67 75 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e gument less than
16240 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 or equal to zer
16250 6f 0a 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 o.** turns off a
16260 6c 6c 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 ll busy handlers
16270 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 ..**.** There ca
16280 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 n only be a sing
16290 6c 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 le busy handler
162a0 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 for a particular
162b0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
162c0 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e nnection] any an
162d0 79 20 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 y given moment.
162e0 20 49 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 If another busy
162f0 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 handler.** was
16300 64 65 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 defined (using
16310 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 [sqlite3_busy_ha
16320 6e 64 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 ndler()]) prior
16330 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 to calling.** th
16340 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 is routine, that
16350 20 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 other busy hand
16360 6c 65 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a ler is cleared..
16370 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
16380 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 ts:.** [H12341]
16390 5b 48 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34 [H12343] [H12344
163a0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ].*/.SQLITE_API
163b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 int sqlite3_busy
163c0 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 _timeout(sqlite3
163d0 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a *, int ms);../*.
163e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e ** CAPI3REF: Con
163f0 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 venience Routine
16400 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 s For Running Qu
16410 65 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c eries {H12370} <
16420 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 S10000>.**.** De
16430 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 finition: A <b>r
16440 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 esult table</b>
16450 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 is memory data s
16460 74 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64 tructure created
16470 20 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 by the.** [sqli
16480 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d te3_get_table()]
16490 20 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 interface. A r
164a0 65 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f esult table reco
164b0 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c rds the.** compl
164c0 65 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 ete query result
164d0 73 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f s from one or mo
164e0 72 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a re queries..**.*
164f0 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 * The table conc
16500 65 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e eptually has a n
16510 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e umber of rows an
16520 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a d columns. But.
16530 2a 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 ** these numbers
16540 20 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 are not part of
16550 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c the result tabl
16560 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 e itself. These
16570 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 .** numbers are
16580 6f 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 obtained separat
16590 65 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 ely. Let N be t
165a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 he number of row
165b0 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 s.** and M be th
165c0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 e number of colu
165d0 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 mns..**.** A res
165e0 75 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 ult table is an
165f0 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 array of pointer
16600 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e s to zero-termin
16610 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e ated UTF-8 strin
16620 67 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 gs..** There are
16630 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 (N+1)*M element
16640 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 s in the array.
16650 20 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 The first M poi
16660 6e 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 nters point.** t
16670 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 o zero-terminate
16680 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 d strings that
16690 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 contain the name
166a0 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 s of the columns
166b0 2e 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 ..** The remaini
166c0 6e 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 ng entries all p
166d0 6f 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 oint to query re
166e0 73 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c sults. NULL val
166f0 75 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e ues result.** in
16700 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 NULL pointers.
16710 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 All other value
16720 73 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 s are in their U
16730 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e TF-8 zero-termin
16740 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 ated.** string r
16750 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 epresentation as
16760 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 returned by [sq
16770 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 lite3_column_tex
16780 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 t()]..**.** A re
16790 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 sult table might
167a0 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 consist of one
167b0 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 or more memory a
167c0 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 llocations..** I
167d0 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f t is not safe to
167e0 20 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 pass a result t
167f0 61 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f able directly to
16800 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 [sqlite3_free()
16810 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 ]..** A result t
16820 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 able should be d
16830 65 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 eallocated using
16840 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 [sqlite3_free_t
16850 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 able()]..**.** A
16860 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 s an example of
16870 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 the result table
16880 20 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 format, suppose
16890 20 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a a query result.
168a0 2a 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 ** is as follows
168b0 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 :.**.** <blockqu
168c0 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 ote><pre>.**
168d0 20 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 Name
168e0 7c 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 | Age.**
168f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
16900 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 -------.**
16910 20 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 Alice |
16920 34 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 43.** Bob
16930 20 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a | 28.**
16940 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 Cindy
16950 20 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 | 21.** </pr
16960 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a e></blockquote>.
16970 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
16980 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 two column (M==2
16990 29 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 ) and three rows
169a0 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 (N==3). Thus t
169b0 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 he.** result tab
169c0 6c 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 le has 8 entries
169d0 2e 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 . Suppose the r
169e0 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 esult table is s
169f0 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 tored.** in an a
16a00 72 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 rray names azRes
16a10 75 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 ult. Then azRes
16a20 75 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 ult holds this c
16a30 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 ontent:.**.** <b
16a40 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a lockquote><pre>.
16a50 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 ** azResu
16a60 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d lt[0] = "Nam
16a70 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a e";.** az
16a80 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 Result[1] =
16a90 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 "Age";.**
16aa0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d azResult[2]
16ab0 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 = "Alice";.**
16ac0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 azResult&#
16ad0 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 91;3] = "43";.**
16ae0 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 azResult
16af0 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b [4] = "Bob";
16b00 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 .** azRes
16b10 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 ult[5] = "28
16b20 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 ";.** azR
16b30 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 esult[6] = "
16b40 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 Cindy";.**
16b50 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 azResult[7
16b60 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 ] = "21";.** </p
16b70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
16b80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
16b90 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 e3_get_table() f
16ba0 75 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 unction evaluate
16bb0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a s one or more.**
16bc0 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 semicolon-separ
16bd0 61 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 ated SQL stateme
16be0 6e 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d nts in the zero-
16bf0 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
16c00 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 .** string of it
16c10 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e s 2nd parameter.
16c20 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 It returns a r
16c30 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 esult table to t
16c40 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 he.** pointer gi
16c50 76 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 ven in its 3rd p
16c60 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 arameter..**.**
16c70 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e After the callin
16c80 67 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 g function has f
16c90 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 inished using th
16ca0 65 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f e result, it sho
16cb0 75 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 uld.** pass the
16cc0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
16cd0 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 esult table to s
16ce0 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c qlite3_free_tabl
16cf0 65 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a e() in order to.
16d00 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d ** release the m
16d10 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d emory that was m
16d20 61 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 alloced. Becaus
16d30 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 e of the way the
16d40 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c .** [sqlite3_mal
16d50 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 loc()] happens w
16d60 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 ithin sqlite3_ge
16d70 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 t_table(), the c
16d80 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 alling.** functi
16d90 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 on must not try
16da0 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 to call [sqlite3
16db0 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c _free()] directl
16dc0 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c y. Only.** [sql
16dd0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 ite3_free_table(
16de0 29 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 )] is able to re
16df0 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 lease the memory
16e00 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 properly and sa
16e10 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 fely..**.** The
16e20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c sqlite3_get_tabl
16e30 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 e() interface is
16e40 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 implemented as
16e50 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 a wrapper around
16e60 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 .** [sqlite3_exe
16e70 63 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 c()]. The sqlit
16e80 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 e3_get_table() r
16e90 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 outine does not
16ea0 68 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 have access.** t
16eb0 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 o any internal d
16ec0 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f ata structures o
16ed0 66 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 f SQLite. It us
16ee0 65 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c es only the publ
16ef0 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ic.** interface
16f00 64 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 defined here. A
16f10 73 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c s a consequence,
16f20 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 errors that occ
16f30 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 ur in the.** wra
16f40 70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 pper layer outsi
16f50 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e de of the intern
16f60 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 al [sqlite3_exec
16f70 28 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 ()] call are not
16f80 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e .** reflected in
16f90 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
16fa0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 s to [sqlite3_er
16fb0 72 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c rcode()] or [sql
16fc0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a ite3_errmsg()]..
16fd0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
16fe0 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 ts:.** [H12371]
16ff0 5b 48 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34 [H12373] [H12374
17000 5d 20 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33 ] [H12376] [H123
17010 37 39 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 79] [H12382].*/.
17020 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
17030 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 qlite3_get_table
17040 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c (. sqlite3 *db,
17050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 /* An
17060 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f open database */
17070 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
17080 53 71 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 Sql, /* SQL
17090 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 to be evaluated
170a0 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a */. char ***paz
170b0 52 65 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 Result, /* Re
170c0 73 75 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 sults of the que
170d0 72 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 ry */. int *pnR
170e0 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ow, /*
170f0 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c Number of resul
17100 74 20 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 t rows written h
17110 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e ere */. int *pn
17120 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f Column, /
17130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 * Number of resu
17140 6c 74 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 lt columns writt
17150 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 en here */. cha
17160 72 20 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 r **pzErrmsg
17170 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 /* Error msg
17180 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a written here */.
17190 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
171a0 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f id sqlite3_free_
171b0 74 61 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 table(char **res
171c0 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ult);../*.** CAP
171d0 49 33 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 I3REF: Formatted
171e0 20 53 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 String Printing
171f0 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 Functions {H174
17200 30 30 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 00} <S70000><S20
17210 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 000>.**.** These
17220 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f routines are wo
17230 72 6b 2d 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 rk-alikes of the
17240 20 22 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 "printf()" fami
17250 6c 79 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a ly of functions.
17260 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e ** from the stan
17270 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a dard C library..
17280 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
17290 33 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 3_mprintf() and
172a0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 sqlite3_vmprintf
172b0 28 29 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 () routines writ
172c0 65 20 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c e their.** resul
172d0 74 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f ts into memory o
172e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 btained from [sq
172f0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e lite3_malloc()].
17300 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 .** The strings
17310 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 returned by thes
17320 65 20 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 e two routines s
17330 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 hould be.** rele
17340 61 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 ased by [sqlite3
17350 5f 66 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 _free()]. Both
17360 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
17370 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 a.** NULL pointe
17380 72 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 r if [sqlite3_ma
17390 6c 6c 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c lloc()] is unabl
173a0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e e to allocate en
173b0 6f 75 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 ough.** memory t
173c0 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c o hold the resul
173d0 74 69 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a ting string..**.
173e0 2a 2a 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e ** In sqlite3_sn
173f0 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 printf() routine
17400 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 is similar to "
17410 73 6e 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d snprintf()" from
17420 0a 2a 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 .** the standard
17430 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 C library. The
17440 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 result is writt
17450 65 6e 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 en into the.** b
17460 75 66 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 uffer supplied a
17470 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 s the second par
17480 61 6d 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a ameter whose siz
17490 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a e is given by.**
174a0 20 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d the first param
174b0 65 74 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 eter. Note that
174c0 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 the order of the
174d0 0a 2a 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 .** first two pa
174e0 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 rameters is reve
174f0 72 73 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e rsed from snprin
17500 74 66 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 tf(). This is a
17510 6e 0a 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 n.** historical
17520 61 63 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 accident that ca
17530 6e 6e 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 nnot be fixed wi
17540 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a thout breaking.*
17550 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 * backwards comp
17560 61 74 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 atibility. Note
17570 20 61 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 also that sqlit
17580 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a e3_snprintf().**
17590 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 returns a point
175a0 65 72 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 er to its buffer
175b0 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 instead of the
175c0 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 number of.** cha
175d0 72 61 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 racters actually
175e0 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
175f0 65 20 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 e buffer. We ad
17600 6d 69 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 mit that.** the
17610 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 number of charac
17620 74 65 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 ters written wou
17630 6c 64 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 ld be a more use
17640 66 75 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 ful return.** va
17650 6c 75 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f lue but we canno
17660 74 20 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 t change the imp
17670 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 lementation of s
17680 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
17690 29 0a 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 ).** now without
176a0 20 62 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 breaking compat
176b0 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 ibility..**.** A
176c0 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 s long as the bu
176d0 66 66 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 ffer size is gre
176e0 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 ater than zero,
176f0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
17700 28 29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 ().** guarantees
17710 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65 72 that the buffer
17720 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d is always zero-
17730 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 terminated. The
17740 20 66 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 first.** parame
17750 74 65 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 ter "n" is the t
17760 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 otal size of the
17770 20 62 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 buffer, includi
17780 6e 67 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 ng space for.**
17790 74 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 the zero termina
177a0 74 6f 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e tor. So the lon
177b0 67 65 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 gest string that
177c0 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 can be complete
177d0 6c 79 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 ly.** written wi
177e0 6c 6c 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 ll be n-1 charac
177f0 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 ters..**.** Thes
17800 65 20 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 e routines all i
17810 6d 70 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 mplement some ad
17820 64 69 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 ditional formatt
17830 69 6e 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 ing.** options t
17840 68 61 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 hat are useful f
17850 6f 72 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 or constructing
17860 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a SQL statements..
17870 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 ** All of the us
17880 75 61 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 ual printf() for
17890 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 matting options
178a0 61 70 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 apply. In addit
178b0 69 6f 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 ion, there.** is
178c0 20 61 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c are "%q", "%Q",
178d0 20 61 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e and "%z" option
178e0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 s..**.** The %q
178f0 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b option works lik
17900 65 20 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 e %s in that it
17910 73 75 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 substitutes a nu
17920 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a ll-terminated.**
17930 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 string from the
17940 20 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 argument list.
17950 20 42 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 But %q also dou
17960 62 6c 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 bles every '\''
17970 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 character..** %q
17980 20 69 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 is designed for
17990 20 75 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 use inside a st
179a0 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 ring literal. B
179b0 79 20 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 y doubling each
179c0 27 5c 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 '\''.** characte
179d0 72 20 69 74 20 65 73 63 61 70 65 73 20 74 68 61 r it escapes tha
179e0 74 20 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 t character and
179f0 61 6c 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 allows it to be
17a00 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a inserted into.**
17a10 20 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a the string..**.
17a20 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 ** For example,
17a30 61 73 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e assume the strin
17a40 67 20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 g variable zText
17a50 20 63 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 contains text a
17a60 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
17a70 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 <blockquote><pr
17a80 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 e>.** char *zTe
17a90 78 74 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 xt = "It's a hap
17aa0 70 79 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 py day!";.** </p
17ab0 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e re></blockquote>
17ac0 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 .**.** One can u
17ad0 73 65 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 se this text in
17ae0 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 an SQL statement
17af0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
17b00 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c ** <blockquote><
17b10 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a pre>.** char *z
17b20 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 SQL = sqlite3_mp
17b30 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e rintf("INSERT IN
17b40 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 TO table VALUES(
17b50 27 25 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a '%q')", zText);.
17b60 2a 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 ** sqlite3_exec
17b70 28 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c (db, zSQL, 0, 0,
17b80 20 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 0);.** sqlite3
17b90 5f 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 _free(zSQL);.**
17ba0 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f </pre></blockquo
17bb0 74 65 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 te>.**.** Becaus
17bc0 65 20 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 e the %q format
17bd0 73 74 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 string is used,
17be0 74 68 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 the '\'' charact
17bf0 65 72 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 er in zText.** i
17c00 73 20 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 s escaped and th
17c10 65 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 e SQL generated
17c20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
17c30 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 *.** <blockquote
17c40 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 ><pre>.** INSER
17c50 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 T INTO table1 VA
17c60 4c 55 45 53 28 27 49 74 27 27 73 20 61 20 68 61 LUES('It''s a ha
17c70 70 70 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f ppy day!').** </
17c80 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 pre></blockquote
17c90 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 >.**.** This is
17ca0 63 6f 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 correct. Had we
17cb0 20 75 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 used %s instead
17cc0 20 6f 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 of %q, the gene
17cd0 72 61 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 rated SQL.** wou
17ce0 6c 64 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c ld have looked l
17cf0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 ike this:.**.**
17d00 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 <blockquote><pre
17d10 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 >.** INSERT INT
17d20 4f 20 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 O table1 VALUES(
17d30 27 49 74 27 73 20 61 20 68 61 70 70 79 20 64 61 'It's a happy da
17d40 79 21 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c y!');.** </pre><
17d50 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a /blockquote>.**.
17d60 2a 2a 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 ** This second e
17d70 78 61 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c xample is an SQL
17d80 20 73 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 syntax error.
17d90 41 73 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c As a general rul
17da0 65 20 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 e you should.**
17db0 61 6c 77 61 79 73 20 75 73 65 20 25 71 20 69 6e always use %q in
17dc0 73 74 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e stead of %s when
17dd0 20 69 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 inserting text
17de0 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 into a string li
17df0 74 65 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 teral..**.** The
17e00 20 25 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 %Q option works
17e10 20 6c 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 like %q except
17e20 69 74 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e it also adds sin
17e30 67 6c 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e gle quotes aroun
17e40 64 0a 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 d.** the outside
17e50 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 of the total st
17e60 72 69 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 ring. Additiona
17e70 6c 6c 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 lly, if the para
17e80 6d 65 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 meter in the.**
17e90 61 72 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 argument list is
17ea0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
17eb0 20 25 51 20 73 75 62 73 74 69 74 75 74 65 73 20 %Q substitutes
17ec0 74 68 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 the text "NULL"
17ed0 28 77 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 (without.** sing
17ee0 6c 65 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c le quotes) in pl
17ef0 61 63 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 ace of the %Q op
17f00 74 69 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 tion. So, for e
17f10 78 61 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c xample, one coul
17f20 64 20 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c d say:.**.** <bl
17f30 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a ockquote><pre>.*
17f40 2a 20 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 * char *zSQL =
17f50 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 sqlite3_mprintf(
17f60 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 "INSERT INTO tab
17f70 6c 65 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 le VALUES(%Q)",
17f80 7a 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 zText);.** sqli
17f90 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 te3_exec(db, zSQ
17fa0 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 L, 0, 0, 0);.**
17fb0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 sqlite3_free(zS
17fc0 51 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f QL);.** </pre></
17fd0 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a blockquote>.**.*
17fe0 2a 20 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 * The code above
17ff0 20 77 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 will render a c
18000 6f 72 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 orrect SQL state
18010 6d 65 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c ment in the zSQL
18020 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 .** variable eve
18030 6e 20 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 n if the zText v
18040 61 72 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c ariable is a NUL
18050 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
18060 20 54 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 The "%z" format
18070 74 69 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b ting option work
18080 73 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 s exactly like "
18090 25 73 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 %s" with the.**
180a0 61 64 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 addition that af
180b0 74 65 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 ter the string h
180c0 61 73 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 as been read and
180d0 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 copied into.**
180e0 74 68 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c the result, [sql
180f0 69 74 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 ite3_free()] is
18100 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e called on the in
18110 70 75 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 put string. {END
18120 7d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d }.**.** Requirem
18130 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 ents:.** [H17403
18140 5d 20 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 ] [H17406] [H174
18150 30 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 07].*/.SQLITE_AP
18160 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
18170 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 mprintf(const ch
18180 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 ar*,...);.SQLITE
18190 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 _API char *sqlit
181a0 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 e3_vmprintf(cons
181b0 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 t char*, va_list
181c0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 );.SQLITE_API ch
181d0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 ar *sqlite3_snpr
181e0 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 intf(int,char*,c
181f0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 onst char*, ...)
18200 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
18210 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 F: Memory Alloca
18220 74 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b tion Subsystem {
18230 48 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e H17300} <S20000>
18240 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 .**.** The SQLit
18250 65 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 e core uses the
18260 73 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 se three routine
18270 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 s for all of its
18280 20 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c own.** internal
18290 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
182a0 6f 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 on needs. "Core"
182b0 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 in the previous
182c0 20 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 sentence.** doe
182d0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 s not include op
182e0 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 erating-system s
182f0 70 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c pecific VFS impl
18300 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 ementation. The
18310 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 .** Windows VFS
18320 75 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c uses native mall
18330 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 oc() and free()
18340 66 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 for some operati
18350 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ons..**.** The s
18360 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
18370 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
18380 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 a pointer to a b
18390 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 lock.** of memor
183a0 79 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 y at least N byt
183b0 65 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 es in length, wh
183c0 65 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 ere N is the par
183d0 61 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 ameter..** If sq
183e0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 lite3_malloc() i
183f0 73 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 s unable to obta
18400 69 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 in sufficient fr
18410 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 ee.** memory, it
18420 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 returns a NULL
18430 70 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 pointer. If the
18440 20 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a parameter N to.
18450 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ** sqlite3_mallo
18460 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e c() is zero or n
18470 65 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c egative then sql
18480 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 ite3_malloc() re
18490 74 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 turns.** a NULL
184a0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 pointer..**.** C
184b0 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 alling sqlite3_f
184c0 72 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 ree() with a poi
184d0 6e 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 nter previously
184e0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 returned.** by s
184f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 qlite3_malloc()
18500 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c or sqlite3_reall
18510 6f 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 oc() releases th
18520 61 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 at memory so.**
18530 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 that it might be
18540 20 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 reused. The sq
18550 6c 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 lite3_free() rou
18560 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d tine is.** a no-
18570 6f 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 op if is called
18580 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e with a NULL poin
18590 74 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 ter. Passing a
185a0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 NULL pointer.**
185b0 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 to sqlite3_free(
185c0 29 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 ) is harmless.
185d0 41 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 After being free
185e0 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f d, memory.** sho
185f0 75 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 uld neither be r
18600 65 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e ead nor written.
18610 20 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 Even reading p
18620 72 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a reviously freed.
18630 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 ** memory might
18640 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d result in a segm
18650 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f entation fault o
18660 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 r other severe e
18670 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 rror..** Memory
18680 63 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 corruption, a se
18690 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 gmentation fault
186a0 2c 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 , or other sever
186b0 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 e error.** might
186c0 20 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 result if sqlit
186d0 65 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c e3_free() is cal
186e0 6c 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e led with a non-N
186f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 ULL pointer that
18700 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 .** was not obta
18710 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 ined from sqlite
18720 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 3_malloc() or sq
18730 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e lite3_realloc().
18740 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
18750 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 e3_realloc() int
18760 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 erface attempts
18770 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 to resize a.** p
18780 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f rior memory allo
18790 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 cation to be at
187a0 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 least N bytes, w
187b0 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a here N is the.**
187c0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
187d0 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 r. The memory a
187e0 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 llocation to be
187f0 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 resized is the f
18800 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 irst.** paramete
18810 72 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74 r. If the first
18820 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 parameter to sq
18830 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a lite3_realloc().
18840 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 ** is a NULL poi
18850 6e 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 nter then its be
18860 68 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 havior is identi
18870 63 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a cal to calling.*
18880 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 * sqlite3_malloc
18890 28 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 (N) where N is t
188a0 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 he second parame
188b0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
188c0 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 ealloc()..** If
188d0 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
188e0 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f eter to sqlite3_
188f0 72 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 realloc() is zer
18900 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 o or.** negative
18910 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 then the behavi
18920 6f 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 or is exactly th
18930 65 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e e same as callin
18940 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 g.** sqlite3_fre
18950 65 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20 e(P) where P is
18960 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 the first parame
18970 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 ter to sqlite3_r
18980 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c ealloc()..** sql
18990 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 ite3_realloc() r
189a0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
189b0 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c to a memory all
189c0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 ocation.** of at
189d0 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 least N bytes i
189e0 6e 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 n size or NULL i
189f0 66 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d f sufficient mem
18a00 6f 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 ory is unavailab
18a10 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 le..** If M is t
18a20 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 he size of the p
18a30 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c rior allocation,
18a40 20 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 then min(N,M) b
18a50 79 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 ytes.** of the p
18a60 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 rior allocation
18a70 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 are copied into
18a80 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 the beginning of
18a90 20 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 buffer returned
18aa0 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 .** by sqlite3_r
18ab0 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 ealloc() and the
18ac0 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f prior allocatio
18ad0 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 n is freed..** I
18ae0 66 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f f sqlite3_reallo
18af0 63 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c c() returns NULL
18b00 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 , then the prior
18b10 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 allocation.** i
18b20 73 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a s not freed..**.
18b30 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 ** The memory re
18b40 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
18b50 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 3_malloc() and s
18b60 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 qlite3_realloc()
18b70 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c .** is always al
18b80 69 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 igned to at leas
18b90 74 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e t an 8 byte boun
18ba0 64 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a dary. {END}.**.*
18bb0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d * The default im
18bc0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
18bd0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
18be0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 ation subsystem
18bf0 75 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c uses.** the mall
18c00 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 oc(), realloc()
18c10 61 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 and free() provi
18c20 64 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 ded by the stand
18c30 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a ard C library..*
18c40 2a 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 * {H17382} Howev
18c50 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 er, if SQLite is
18c60 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
18c70 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d he.** SQLITE_MEM
18c80 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c ORY_SIZE=<i>NNN<
18c90 2f 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 /i> C preprocess
18ca0 6f 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 or macro (where
18cb0 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 <i>NNN</i>.** is
18cc0 20 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 an integer), th
18cd0 65 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 en SQLite create
18ce0 20 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20 a static array
18cf0 6f 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c of at least.** <
18d00 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 i>NNN</i> bytes
18d10 69 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 in size and uses
18d20 20 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 that array for
18d30 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d all of its dynam
18d40 69 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c ic.** memory all
18d50 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b ocation needs. {
18d60 45 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c END} Additional
18d70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
18d80 72 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 r options.** may
18d90 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 be added in fut
18da0 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a ure releases..**
18db0 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 .** In SQLite ve
18dc0 72 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 rsion 3.5.0 and
18dd0 33 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 3.5.1, it was po
18de0 73 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 ssible to define
18df0 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f .** the SQLITE_O
18e00 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 MIT_MEMORY_ALLOC
18e10 41 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c ATION which woul
18e20 64 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c d cause the buil
18e30 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e t-in.** implemen
18e40 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 tation of these
18e50 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f routines to be o
18e60 6d 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 mitted. That ca
18e70 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e pability.** is n
18e80 6f 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 o longer provide
18e90 64 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 d. Only built-i
18ea0 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 n memory allocat
18eb0 6f 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e ors can be used.
18ec0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f .**.** The Windo
18ed0 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 ws OS interface
18ee0 6c 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 layer calls.** t
18ef0 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 he system malloc
18f00 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 () and free() di
18f10 72 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 rectly when conv
18f20 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 erting.** filena
18f30 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 mes between the
18f40 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 UTF-8 encoding u
18f50 73 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a sed by SQLite.**
18f60 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 and whatever fi
18f70 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 lename encoding
18f80 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 is used by the p
18f90 61 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 articular Window
18fa0 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f s.** installatio
18fb0 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 n. Memory alloc
18fc0 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 ation errors are
18fd0 20 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a detected, but.*
18fe0 2a 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 * they are repor
18ff0 74 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c ted back as [SQL
19000 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 ITE_CANTOPEN] or
19010 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 .** [SQLITE_IOER
19020 52 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b R] rather than [
19030 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a SQLITE_NOMEM]..*
19040 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
19050 73 3a 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b s:.** [H17303] [
19060 48 31 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d H17304] [H17305]
19070 20 5b 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 [H17306] [H1731
19080 30 5d 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 0] [H17312] [H17
19090 33 31 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 315] [H17318].**
190a0 20 5b 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 [H17321] [H1732
190b0 32 5d 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2] [H17323].**.*
190c0 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 * The pointer ar
190d0 67 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 guments to [sqli
190e0 74 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 te3_free()] and
190f0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 [sqlite3_realloc
19100 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 ()].** must be e
19110 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c ither NULL or el
19120 73 65 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 se pointers obta
19130 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f ined from a prio
19140 72 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 r.** invocation
19150 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c of [sqlite3_mall
19160 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 oc()] or [sqlite
19170 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 3_realloc()] tha
19180 74 20 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 t have.** not ye
19190 74 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e t been released.
191a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 .**.** The appli
191b0 63 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 cation must not
191c0 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e read or write an
191d0 79 20 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 y part of.** a b
191e0 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 lock of memory a
191f0 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e fter it has been
19200 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a released using.
19210 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 ** [sqlite3_free
19220 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
19230 72 65 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 realloc()]..*/.S
19240 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
19250 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 sqlite3_malloc(i
19260 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 nt);.SQLITE_API
19270 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 void *sqlite3_re
19280 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 alloc(void*, int
19290 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f );.SQLITE_API vo
192a0 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 id sqlite3_free(
192b0 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void*);../*.** C
192c0 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 API3REF: Memory
192d0 41 6c 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 Allocator Statis
192e0 74 69 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 tics {H17370} <S
192f0 33 30 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 30210>.**.** SQL
19300 69 74 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 ite provides the
19310 73 65 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 se two interface
19320 73 20 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 s for reporting
19330 6f 6e 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a on the status.**
19340 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 of the [sqlite3
19350 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c _malloc()], [sql
19360 69 74 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e ite3_free()], an
19370 64 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c d [sqlite3_reall
19380 6f 63 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 oc()].** routine
19390 73 2c 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 s, which form th
193a0 65 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 e built-in memor
193b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
193c0 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 system..**.** Re
193d0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
193e0 48 31 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d H17371] [H17373]
193f0 20 5b 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 [H17374] [H1737
19400 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 5].*/.SQLITE_API
19410 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
19420 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 qlite3_memory_us
19430 65 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 ed(void);.SQLITE
19440 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 _API sqlite3_int
19450 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 64 sqlite3_memor
19460 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 y_highwater(int
19470 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a resetFlag);../*.
19480 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 ** CAPI3REF: Pse
19490 75 64 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 udo-Random Numbe
194a0 72 20 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 r Generator {H17
194b0 33 39 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 390} <S20000>.**
194c0 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 .** SQLite conta
194d0 69 6e 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 ins a high-quali
194e0 74 79 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d ty pseudo-random
194f0 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f number generato
19500 72 20 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f r (PRNG) used to
19510 0a 2a 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f .** select rando
19520 6d 20 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 m [ROWID | ROWID
19530 73 5d 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e s] when insertin
19540 67 20 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e g new records in
19550 74 6f 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a to a table that.
19560 2a 2a 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 ** already uses
19570 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 the largest poss
19580 69 62 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 ible [ROWID]. T
19590 68 65 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 he PRNG is also
195a0 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 used for.** the
195b0 62 75 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 build-in random(
195c0 29 20 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 ) and randomblob
195d0 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 () SQL functions
195e0 2e 20 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 . This interfac
195f0 65 20 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c e allows.** appl
19600 69 63 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 ications to acce
19610 73 73 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 ss the same PRNG
19620 20 66 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f for other purpo
19630 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c ses..**.** A cal
19640 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e l to this routin
19650 65 20 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 e stores N bytes
19660 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 of randomness i
19670 6e 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a nto buffer P..**
19680 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 .** The first ti
19690 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 me this routine
196a0 69 73 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 is invoked (eith
196b0 65 72 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 er internally or
196c0 20 62 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 by.** the appli
196d0 63 61 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 cation) the PRNG
196e0 20 69 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 is seeded using
196f0 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 randomness obta
19700 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 ined.** from the
19710 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 xRandomness met
19720 68 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 hod of the defau
19730 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d lt [sqlite3_vfs]
19740 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 object..** On a
19750 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e ll subsequent in
19760 76 6f 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 vocations, the p
19770 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 seudo-randomness
19780 20 69 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a is generated.**
19790 20 69 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 internally and
197a0 77 69 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 without recourse
197b0 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 to the [sqlite3
197c0 5f 76 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 _vfs] xRandomnes
197d0 73 0a 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a s.** method..**.
197e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
197f0 0a 2a 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a .** [H17392].*/.
19800 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
19810 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 sqlite3_randomne
19820 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a ss(int N, void *
19830 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 P);../*.** CAPI3
19840 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d REF: Compile-Tim
19850 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 e Authorization
19860 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 Callbacks {H1250
19870 30 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 0} <S70100>.**.*
19880 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
19890 65 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f egisters a autho
198a0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 rizer callback w
198b0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 ith a particular
198c0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f .** [database co
198d0 6e 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c nnection], suppl
198e0 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 ied in the first
198f0 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 argument..** Th
19900 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
19910 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
19920 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e as SQL statemen
19930 74 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d ts are being com
19940 70 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c piled.** by [sql
19950 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 ite3_prepare()]
19960 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 or its variants
19970 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 [sqlite3_prepare
19980 5f 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 _v2()],.** [sqli
19990 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d te3_prepare16()]
199a0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 and [sqlite3_pr
199b0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 epare16_v2()].
199c0 41 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f At various.** po
199d0 69 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 ints during the
199e0 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 compilation proc
199f0 65 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 ess, as logic is
19a00 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a being created.*
19a10 2a 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 * to perform var
19a20 69 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 ious actions, th
19a30 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c e authorizer cal
19a40 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
19a50 20 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 to.** see if th
19a60 6f 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 ose actions are
19a70 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 allowed. The au
19a80 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
19a90 6b 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 k should.** retu
19aa0 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 rn [SQLITE_OK] t
19ab0 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 o allow the acti
19ac0 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f on, [SQLITE_IGNO
19ad0 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 RE] to disallow
19ae0 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 the.** specific
19af0 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 action but allow
19b00 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 the SQL stateme
19b10 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 nt to continue t
19b20 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 o be.** compiled
19b30 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e , or [SQLITE_DEN
19b40 59 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 Y] to cause the
19b50 65 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 entire SQL state
19b60 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 ment to be.** re
19b70 6a 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 jected with an e
19b80 72 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 rror. If the au
19b90 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
19ba0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 k returns.** any
19bb0 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 value other tha
19bc0 6e 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 n [SQLITE_IGNORE
19bd0 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 ], [SQLITE_OK],
19be0 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d or [SQLITE_DENY]
19bf0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 .** then the [sq
19c00 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 lite3_prepare_v2
19c10 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e ()] or equivalen
19c20 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 t call that trig
19c30 67 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 gered.** the aut
19c40 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 horizer will fai
19c50 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 l with an error
19c60 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 message..**.** W
19c70 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b hen the callback
19c80 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 returns [SQLITE
19c90 5f 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 _OK], that means
19ca0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a the operation.*
19cb0 2a 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f * requested is o
19cc0 6b 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c k. When the cal
19cd0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 lback returns [S
19ce0 51 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 QLITE_DENY], the
19cf0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
19d00 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 pare_v2()] or eq
19d10 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 uivalent call th
19d20 61 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 at triggered the
19d30 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 .** authorizer w
19d40 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e ill fail with an
19d50 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 error message e
19d60 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a xplaining that.*
19d70 2a 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 * access is deni
19d80 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ed. .**.** The f
19d90 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 irst parameter t
19da0 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 o the authorizer
19db0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 callback is a c
19dc0 6f 70 79 20 6f 66 20 74 68 65 20 74 68 69 72 64 opy of the third
19dd0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f .** parameter to
19de0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 the sqlite3_set
19df0 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e _authorizer() in
19e00 74 65 72 66 61 63 65 2e 20 54 68 65 20 73 65 63 terface. The sec
19e10 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a ond parameter.**
19e20 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b to the callback
19e30 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b is an integer [
19e40 53 51 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 SQLITE_COPY | ac
19e50 74 69 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 tion code] that
19e60 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 specifies.** the
19e70 20 70 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 particular acti
19e80 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 on to be authori
19e90 7a 65 64 2e 20 54 68 65 20 74 68 69 72 64 20 74 zed. The third t
19ea0 68 72 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 hrough sixth par
19eb0 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 ameters.** to th
19ec0 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a e callback are z
19ed0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 ero-terminated s
19ee0 74 72 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 trings that cont
19ef0 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a ain additional.*
19f00 2a 20 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 * details about
19f10 74 68 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 the action to be
19f20 20 61 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a authorized..**.
19f30 2a 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e ** If the action
19f40 20 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 code is [SQLITE
19f50 5f 52 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 _READ].** and th
19f60 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 e callback retur
19f70 6e 73 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 ns [SQLITE_IGNOR
19f80 45 5d 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b E] then the.** [
19f90 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
19fa0 6e 74 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 nt] statement is
19fb0 20 63 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 constructed to
19fc0 73 75 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 substitute.** a
19fd0 4e 55 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c NULL value in pl
19fe0 61 63 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 ace of the table
19ff0 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 column that wou
1a000 6c 64 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 ld have.** been
1a010 72 65 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f read if [SQLITE_
1a020 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74 OK] had been ret
1a030 75 72 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c urned. The [SQL
1a040 49 54 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 ITE_IGNORE].** r
1a050 65 74 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65 eturn can be use
1a060 64 20 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 d to deny an unt
1a070 72 75 73 74 65 64 20 75 73 65 72 20 61 63 63 65 rusted user acce
1a080 73 73 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c ss to individual
1a090 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 .** columns of a
1a0a0 20 74 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 table..** If th
1a0b0 65 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 e action code is
1a0c0 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d [SQLITE_DELETE]
1a0d0 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 and the callbac
1a0e0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 k returns.** [SQ
1a0f0 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 LITE_IGNORE] the
1a100 6e 20 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f n the [DELETE] o
1a110 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 peration proceed
1a120 73 20 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 s but the.** [tr
1a130 75 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 uncate optimizat
1a140 69 6f 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 64 ion] is disabled
1a150 20 61 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 and all rows ar
1a160 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 76 69 e deleted indivi
1a170 64 75 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e dually..**.** An
1a180 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 authorizer is u
1a190 73 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 sed when [sqlite
1a1a0 33 5f 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 3_prepare | prep
1a1b0 61 72 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 aring].** SQL st
1a1c0 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e atements from an
1a1d0 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 untrusted sourc
1a1e0 65 2c 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 e, to ensure tha
1a1f0 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d t the SQL statem
1a200 65 6e 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 ents.** do not t
1a210 72 79 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 ry to access dat
1a220 61 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 a they are not a
1a230 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f llowed to see, o
1a240 72 20 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e r that they do n
1a250 6f 74 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 ot.** try to exe
1a260 63 75 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 cute malicious s
1a270 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 tatements that d
1a280 61 6d 61 67 65 20 74 68 65 20 64 61 74 61 62 61 amage the databa
1a290 73 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d se. For.** exam
1a2a0 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 ple, an applicat
1a2b0 69 6f 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 ion may allow a
1a2c0 75 73 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 user to enter ar
1a2d0 62 69 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 bitrary.** SQL q
1a2e0 75 65 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 ueries for evalu
1a2f0 61 74 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 ation by a datab
1a300 61 73 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 ase. But the ap
1a310 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a plication does.*
1a320 2a 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 * not want the u
1a330 73 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 ser to be able t
1a340 6f 20 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 o make arbitrary
1a350 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a changes to the.
1a360 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e ** database. An
1a370 20 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c authorizer coul
1a380 64 20 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e d then be put in
1a390 20 70 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 place while the
1a3a0 0a 2a 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 .** user-entered
1a3b0 20 53 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 SQL is being [s
1a3c0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c qlite3_prepare |
1a3d0 20 70 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a prepared] that.
1a3e0 2a 2a 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 ** disallows eve
1a3f0 72 79 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b rything except [
1a400 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e SELECT] statemen
1a410 74 73 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 ts..**.** Applic
1a420 61 74 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 ations that need
1a430 20 74 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 to process SQL
1a440 66 72 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 from untrusted s
1a450 6f 75 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 ources.** might
1a460 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f also consider lo
1a470 77 65 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 wering resource
1a480 6c 69 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 limits using [sq
1a490 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a lite3_limit()].*
1a4a0 2a 20 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 * and limiting d
1a4b0 61 74 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 atabase size usi
1a4c0 6e 67 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 ng the [max_page
1a4d0 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d _count] [PRAGMA]
1a4e0 0a 2a 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 .** in addition
1a4f0 74 6f 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 to using an auth
1a500 6f 72 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e orizer..**.** On
1a510 6c 79 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 ly a single auth
1a520 6f 72 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e orizer can be in
1a530 20 70 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 place on a data
1a540 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a base connection.
1a550 2a 2a 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 ** at a time. E
1a560 61 63 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 ach call to sqli
1a570 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a te3_set_authoriz
1a580 65 72 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 er overrides the
1a590 0a 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c .** previous cal
1a5a0 6c 2e 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 l. Disable the
1a5b0 61 75 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e authorizer by in
1a5c0 73 74 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 stalling a NULL
1a5d0 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 callback..** The
1a5e0 20 61 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 authorizer is d
1a5f0 69 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 isabled by defau
1a600 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 lt..**.** The au
1a610 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1a620 6b 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e k must not do an
1a630 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c ything that will
1a640 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 modify.** the d
1a650 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1a660 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 on that invoked
1a670 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 the authorizer c
1a680 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 allback..** Note
1a690 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 that [sqlite3_p
1a6a0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 repare_v2()] and
1a6b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1a6c0 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 ] both modify th
1a6d0 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 eir.** database
1a6e0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 connections for
1a6f0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 the meaning of "
1a700 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 modify" in this
1a710 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a paragraph..**.**
1a720 20 57 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 When [sqlite3_p
1a730 72 65 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 repare_v2()] is
1a740 75 73 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 used to prepare
1a750 61 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 a statement, the
1a760 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 .** statement mi
1a770 67 68 74 20 62 65 20 72 65 2d 70 72 65 70 61 72 ght be re-prepar
1a780 65 64 20 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 ed during [sqlit
1a790 65 33 5f 73 74 65 70 28 29 5d 20 64 75 65 20 74 e3_step()] due t
1a7a0 6f 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 o a .** schema c
1a7b0 68 61 6e 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 hange. Hence, t
1a7c0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 he application s
1a7d0 68 6f 75 6c 64 20 65 6e 73 75 72 65 20 74 68 61 hould ensure tha
1a7e0 74 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 t the.** correct
1a7f0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1a800 62 61 63 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 back remains in
1a810 70 6c 61 63 65 20 64 75 72 69 6e 67 20 74 68 65 place during the
1a820 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 [sqlite3_step()
1a830 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 ]..**.** Note th
1a840 61 74 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 at the authorize
1a850 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e r callback is in
1a860 76 6f 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e voked only durin
1a870 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 g.** [sqlite3_pr
1a880 65 70 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 epare()] or its
1a890 76 61 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f variants. Autho
1a8a0 72 69 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a rization is not.
1a8b0 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72 ** performed dur
1a8c0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76 ing statement ev
1a8d0 61 6c 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c aluation in [sql
1a8e0 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e ite3_step()], un
1a8f0 6c 65 73 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 less.** as state
1a900 64 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 d in the previou
1a910 73 20 70 61 72 61 67 72 61 70 68 2c 20 73 71 6c s paragraph, sql
1a920 69 74 65 33 5f 73 74 65 70 28 29 20 69 6e 76 6f ite3_step() invo
1a930 6b 65 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 kes.** sqlite3_p
1a940 72 65 70 61 72 65 5f 76 32 28 29 20 74 6f 20 72 repare_v2() to r
1a950 65 70 72 65 70 61 72 65 20 61 20 73 74 61 74 65 eprepare a state
1a960 6d 65 6e 74 20 61 66 74 65 72 20 61 20 73 63 68 ment after a sch
1a970 65 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a ema change..**.*
1a980 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
1a990 2a 2a 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 ** [H12501] [H12
1a9a0 35 30 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 502] [H12503] [H
1a9b0 31 32 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 12504] [H12505]
1a9c0 5b 48 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 [H12506] [H12507
1a9d0 5d 20 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 ] [H12510].** [H
1a9e0 31 32 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 12511] [H12512]
1a9f0 5b 48 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 [H12520] [H12521
1aa00 5d 20 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 ] [H12522].*/.SQ
1aa10 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1aa20 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 ite3_set_authori
1aa30 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c zer(. sqlite3*,
1aa40 0a 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 . int (*xAuth)(
1aa50 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 void*,int,const
1aa60 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
1aa70 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f *,const char*,co
1aa80 6e 73 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f nst char*),. vo
1aa90 69 64 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b id *pUserData.);
1aaa0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1aab0 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 : Authorizer Ret
1aac0 75 72 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 urn Codes {H1259
1aad0 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 0} <H12500>.**.*
1aae0 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 * The [sqlite3_s
1aaf0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 et_authorizer |
1ab00 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 authorizer callb
1ab10 61 63 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 ack function] mu
1ab20 73 74 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 st.** return eit
1ab30 68 65 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 her [SQLITE_OK]
1ab40 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 or one of these
1ab50 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e two constants in
1ab60 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 order.** to sig
1ab70 6e 61 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 nal SQLite wheth
1ab80 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 er or not the ac
1ab90 74 69 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 tion is permitte
1aba0 64 2e 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b d. See the.** [
1abb0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 sqlite3_set_auth
1abc0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 orizer | authori
1abd0 7a 65 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f zer documentatio
1abe0 6e 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 n] for additiona
1abf0 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e l.** information
1ac00 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
1ac10 49 54 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f ITE_DENY 1 /
1ac20 2a 20 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 * Abort the SQL
1ac30 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 statement with a
1ac40 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 n error */.#defi
1ac50 6e 65 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ne SQLITE_IGNORE
1ac60 20 32 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 2 /* Don't al
1ac70 6c 6f 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 low access, but
1ac80 64 6f 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 don't generate a
1ac90 6e 20 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a n error */../*.*
1aca0 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 * CAPI3REF: Auth
1acb0 6f 72 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f orizer Action Co
1acc0 64 65 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 des {H12550} <H1
1acd0 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 2500>.**.** The
1ace0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 [sqlite3_set_aut
1acf0 68 6f 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 horizer()] inter
1ad00 66 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 face registers a
1ad10 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 callback functi
1ad20 6f 6e 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e on.** that is in
1ad30 76 6f 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 voked to authori
1ad40 7a 65 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 ze certain SQL s
1ad50 74 61 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 tatement actions
1ad60 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 . The.** second
1ad70 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 parameter to th
1ad80 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e e callback is an
1ad90 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 integer code th
1ada0 61 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 at specifies.**
1adb0 77 68 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 what action is b
1adc0 65 69 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e eing authorized.
1add0 20 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20 These are the
1ade0 69 6e 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 integer action c
1adf0 6f 64 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 odes that.** the
1ae00 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1ae10 62 61 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 back may be pass
1ae20 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ed..**.** These
1ae30 61 63 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 action code valu
1ae40 65 73 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 es signify what
1ae50 6b 69 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f kind of operatio
1ae60 6e 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 n is to be.** au
1ae70 74 68 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 thorized. The 3
1ae80 72 64 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d rd and 4th param
1ae90 65 74 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 eters to the aut
1aea0 68 6f 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 horization.** ca
1aeb0 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
1aec0 77 69 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 will be paramete
1aed0 72 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e rs or NULL depen
1aee0 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 ding on which of
1aef0 20 74 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 these.** codes
1af00 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20 73 is used as the s
1af10 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e econd parameter.
1af20 20 20 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 The 5th parame
1af30 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 ter to the.** au
1af40 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 thorizer callbac
1af50 6b 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 k is the name of
1af60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 22 the database ("
1af70 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a main", "temp",.*
1af80 2a 20 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 * etc.) if appli
1af90 63 61 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 cable. The 6th
1afa0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
1afb0 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c authorizer call
1afc0 62 61 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e back.** is the n
1afd0 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 ame of the inner
1afe0 2d 6d 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 -most trigger or
1aff0 20 76 69 65 77 20 74 68 61 74 20 69 73 20 72 65 view that is re
1b000 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a sponsible for.**
1b010 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74 65 the access atte
1b020 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 mpt or NULL if t
1b030 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65 6d his access attem
1b040 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 pt is directly f
1b050 72 6f 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c rom.** top-level
1b060 20 53 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a SQL code..**.**
1b070 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
1b080 2a 20 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 * [H12551] [H125
1b090 35 32 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 52] [H12553] [H1
1b0a0 32 35 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2554].*/./******
1b0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b0d0 2a 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a ***** 3rd ******
1b0e0 2a 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a ****** 4th *****
1b0f0 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 ******/.#define
1b100 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e SQLITE_CREATE_IN
1b110 44 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 DEX 1
1b120 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 /* Index Name
1b130 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 Table Name
1b140 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b150 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 QLITE_CREATE_TAB
1b160 4c 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 LE 2
1b170 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 /* Table Name
1b180 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1b190 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b1a0 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 LITE_CREATE_TEMP
1b1b0 5f 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f _INDEX 3 /
1b1c0 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 * Index Name
1b1d0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 Table Name
1b1e0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c */.#define SQL
1b1f0 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f ITE_CREATE_TEMP_
1b200 54 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a TABLE 4 /*
1b210 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b220 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 NULL
1b230 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b240 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 TE_CREATE_TEMP_T
1b250 52 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 RIGGER 5 /*
1b260 54 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 Trigger Name
1b270 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 Table Name
1b280 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b290 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 E_CREATE_TEMP_VI
1b2a0 45 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 EW 6 /* V
1b2b0 69 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e iew Name N
1b2c0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b2d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b2e0 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 _CREATE_TRIGGER
1b2f0 20 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 7 /* Tr
1b300 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 igger Name Ta
1b310 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f ble Name */
1b320 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b330 43 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 CREATE_VIEW
1b340 20 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 8 /* Vie
1b350 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c w Name NUL
1b360 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b370 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
1b380 45 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 ELETE
1b390 20 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 9 /* Tabl
1b3a0 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c e Name NULL
1b3b0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b3c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 define SQLITE_DR
1b3d0 4f 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 OP_INDEX
1b3e0 20 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 10 /* Index
1b3f0 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 Name Table
1b400 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 Name */.#d
1b410 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f efine SQLITE_DRO
1b420 50 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 P_TABLE
1b430 20 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 11 /* Table
1b440 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
1b450 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b460 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 fine SQLITE_DROP
1b470 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 _TEMP_INDEX
1b480 20 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 12 /* Index N
1b490 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e ame Table N
1b4a0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 ame */.#def
1b4b0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f ine SQLITE_DROP_
1b4c0 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 TEMP_TABLE
1b4d0 31 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 13 /* Table Na
1b4e0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 me NULL
1b4f0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 */.#defi
1b500 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 ne SQLITE_DROP_T
1b510 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 EMP_TRIGGER 1
1b520 34 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 4 /* Trigger N
1b530 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d ame Table Nam
1b540 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e e */.#defin
1b550 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 e SQLITE_DROP_TE
1b560 4d 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 MP_VIEW 15
1b570 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 /* View Name
1b580 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 NULL
1b590 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 */.#define
1b5a0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 SQLITE_DROP_TRI
1b5b0 47 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 GGER 16
1b5c0 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d /* Trigger Nam
1b5d0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 e Table Name
1b5e0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 */.#define
1b5f0 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 SQLITE_DROP_VIEW
1b600 20 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 17
1b610 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 /* View Name
1b620 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 NULL
1b630 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 */.#define S
1b640 51 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 QLITE_INSERT
1b650 20 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 18
1b660 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 /* Table Name
1b670 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 NULL
1b680 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 */.#define SQ
1b690 4c 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 LITE_PRAGMA
1b6a0 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 19 /
1b6b0 2a 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 * Pragma Name
1b6c0 20 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 1st arg or NUL
1b6d0 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c L */.#define SQL
1b6e0 49 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 ITE_READ
1b6f0 20 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 /*
1b700 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 Table Name
1b710 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 Column Name
1b720 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
1b730 54 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 TE_SELECT
1b740 20 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 21 /*
1b750 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1b760 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 NULL
1b770 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
1b780 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 E_TRANSACTION
1b790 20 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 22 /* O
1b7a0 70 65 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e peration N
1b7b0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a ULL *
1b7c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
1b7d0 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 _UPDATE
1b7e0 20 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 23 /* Ta
1b7f0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f ble Name Co
1b800 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f lumn Name */
1b810 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1b820 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 ATTACH
1b830 20 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 24 /* Fil
1b840 65 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c ename NUL
1b850 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a L */.
1b860 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 #define SQLITE_D
1b870 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 ETACH
1b880 20 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 25 /* Data
1b890 62 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c base Name NULL
1b8a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 */.#
1b8b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c define SQLITE_AL
1b8c0 54 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 TER_TABLE
1b8d0 20 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 26 /* Datab
1b8e0 61 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 ase Name Table
1b8f0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 Name */.#d
1b900 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 efine SQLITE_REI
1b910 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 NDEX
1b920 20 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 27 /* Index
1b930 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 Name NULL
1b940 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 */.#de
1b950 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c fine SQLITE_ANAL
1b960 59 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 YZE
1b970 20 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 28 /* Table N
1b980 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 ame NULL
1b990 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 */.#def
1b9a0 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 ine SQLITE_CREAT
1b9b0 45 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 E_VTABLE
1b9c0 32 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 29 /* Table Na
1b9d0 6d 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e me Module N
1b9e0 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 ame */.#defi
1b9f0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 ne SQLITE_DROP_V
1ba00 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 TABLE 3
1ba10 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 0 /* Table Nam
1ba20 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 e Module Na
1ba30 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e me */.#defin
1ba40 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f e SQLITE_FUNCTIO
1ba50 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 N 31
1ba60 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 /* NULL
1ba70 20 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e Function N
1ba80 61 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 ame */.#define
1ba90 20 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e SQLITE_SAVEPOIN
1baa0 54 20 20 20 20 20 20 20 20 20 20 20 20 33 32 20 T 32
1bab0 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 /* Operation
1bac0 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e Savepoint N
1bad0 61 6d 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ame */.#define
1bae0 53 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 SQLITE_COPY
1baf0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 0
1bb00 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 /* No longer us
1bb10 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 ed */../*.** CAP
1bb20 49 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 I3REF: Tracing A
1bb30 6e 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e nd Profiling Fun
1bb40 63 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 ctions {H12280}
1bb50 3c 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 <S60400>.** EXPE
1bb60 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 RIMENTAL.**.** T
1bb70 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 hese routines re
1bb80 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 gister callback
1bb90 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 functions that c
1bba0 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a an be used for.*
1bbb0 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 * tracing and pr
1bbc0 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 ofiling the exec
1bbd0 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 ution of SQL sta
1bbe0 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 tements..**.** T
1bbf0 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 he callback func
1bc00 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 tion registered
1bc10 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 by sqlite3_trace
1bc20 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 () is invoked at
1bc30 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 .** various time
1bc40 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 s when an SQL st
1bc50 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 atement is being
1bc60 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 run by [sqlite3
1bc70 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 _step()]..** The
1bc80 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e callback return
1bc90 73 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 s a UTF-8 render
1bca0 69 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 ing of the SQL s
1bcb0 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a tatement text.**
1bcc0 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e as the statemen
1bcd0 74 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 t first begins e
1bce0 78 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 xecuting. Addit
1bcf0 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 ional callbacks
1bd00 6f 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 occur.** as each
1bd10 20 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72 triggered subpr
1bd20 6f 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 ogram is entered
1bd30 2e 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 . The callbacks
1bd40 20 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a for triggers.**
1bd50 20 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 contain a UTF-8
1bd60 20 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 SQL comment tha
1bd70 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 t identifies the
1bd80 20 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 trigger..**.**
1bd90 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e The callback fun
1bda0 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
1bdb0 20 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 by sqlite3_prof
1bdc0 69 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 ile() is invoked
1bdd0 0a 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 .** as each SQL
1bde0 73 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 statement finish
1bdf0 65 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 es. The profile
1be00 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 callback contai
1be10 6e 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e ns.** the origin
1be20 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 al statement tex
1be30 74 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 t and an estimat
1be40 65 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 e of wall-clock
1be50 74 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c time.** of how l
1be60 6f 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 ong that stateme
1be70 6e 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a nt took to run..
1be80 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
1be90 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 ts:.** [H12281]
1bea0 5b 48 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 [H12282] [H12283
1beb0 5d 20 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 ] [H12284] [H122
1bec0 38 35 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 85] [H12287] [H1
1bed0 32 32 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2288] [H12289].*
1bee0 2a 20 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 * [H12290].*/.SQ
1bef0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f LITE_API SQLITE_
1bf00 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 EXPERIMENTAL voi
1bf10 64 20 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 d *sqlite3_trace
1bf20 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 (sqlite3*, void(
1bf30 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 *xTrace)(void*,c
1bf40 6f 6e 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 onst char*), voi
1bf50 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 d*);.SQLITE_API
1bf60 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
1bf70 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 TAL void *sqlite
1bf80 33 5f 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 3_profile(sqlite
1bf90 33 2a 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 3*,. void(*xPr
1bfa0 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e ofile)(void*,con
1bfb0 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 st char*,sqlite3
1bfc0 5f 75 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 _uint64), void*)
1bfd0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
1bfe0 46 3a 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 F: Query Progres
1bff0 73 20 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 s Callbacks {H12
1c000 39 31 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 910} <S60400>.**
1c010 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
1c020 20 63 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 configures a ca
1c030 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
1c040 2d 20 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 - the.** progres
1c050 73 20 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 s callback - tha
1c060 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 t is invoked per
1c070 69 6f 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 iodically during
1c080 20 6c 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 long.** running
1c090 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 calls to [sqlit
1c0a0 65 33 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c e3_exec()], [sql
1c0b0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 ite3_step()] and
1c0c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 .** [sqlite3_get
1c0d0 5f 74 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 _table()]. An e
1c0e0 78 61 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 xample use for t
1c0f0 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 his.** interface
1c100 20 69 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 is to keep a GU
1c110 49 20 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 I updated during
1c120 20 61 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a a large query..
1c130 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f **.** If the pro
1c140 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 gress callback r
1c150 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c eturns non-zero,
1c160 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 the operation i
1c170 73 0a 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 s.** interrupted
1c180 2e 20 20 54 68 69 73 20 66 65 61 74 75 72 65 20 . This feature
1c190 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 can be used to i
1c1a0 6d 70 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 mplement a.** "C
1c1b0 61 6e 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e ancel" button on
1c1c0 20 61 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 a GUI progress
1c1d0 64 69 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a dialog box..**.*
1c1e0 2a 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 * The progress h
1c1f0 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 andler must not
1c200 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 do anything that
1c210 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 will modify.**
1c220 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
1c230 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 nection that inv
1c240 6f 6b 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 oked the progres
1c250 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f s handler..** No
1c260 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 te that [sqlite3
1c270 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 _prepare_v2()] a
1c280 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 nd [sqlite3_step
1c290 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 ()] both modify
1c2a0 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 their.** databas
1c2b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f e connections fo
1c2c0 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 r the meaning of
1c2d0 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 "modify" in thi
1c2e0 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a s paragraph..**.
1c2f0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
1c300 0a 2a 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 .** [H12911] [H1
1c310 32 39 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 2912] [H12913] [
1c320 48 31 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d H12914] [H12915]
1c330 20 5b 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 [H12916] [H1291
1c340 37 5d 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 7] [H12918].**.*
1c350 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
1c360 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 d sqlite3_progre
1c370 73 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 ss_handler(sqlit
1c380 65 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 e3*, int, int(*)
1c390 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b (void*), void*);
1c3a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
1c3b0 3a 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 : Opening A New
1c3c0 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 Database Connect
1c3d0 69 6f 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 ion {H12700} <S4
1c3e0 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 0200>.**.** Thes
1c3f0 65 20 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 e routines open
1c400 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 an SQLite databa
1c410 73 65 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 se file whose na
1c420 6d 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 me is given by t
1c430 68 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 he.** filename a
1c440 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c rgument. The fil
1c450 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 ename argument i
1c460 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 s interpreted as
1c470 20 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 UTF-8 for.** sq
1c480 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 lite3_open() and
1c490 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 sqlite3_open_v2
1c4a0 28 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 () and as UTF-16
1c4b0 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 in the native b
1c4c0 79 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 yte.** order for
1c4d0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 sqlite3_open16(
1c4e0 29 2e 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 ). A [database c
1c4f0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c onnection] handl
1c500 65 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 e is usually.**
1c510 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 returned in *ppD
1c520 62 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 b, even if an er
1c530 72 6f 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 ror occurs. The
1c540 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 only exception
1c550 69 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 is that.** if SQ
1c560 4c 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 Lite is unable t
1c570 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 o allocate memor
1c580 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 y to hold the [s
1c590 71 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a qlite3] object,.
1c5a0 2a 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 ** a NULL will b
1c5b0 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a e written into *
1c5c0 70 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 ppDb instead of
1c5d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1c5e0 20 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 [sqlite3].** ob
1c5f0 6a 65 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 ject. If the dat
1c600 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 abase is opened
1c610 28 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 (and/or created)
1c620 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 successfully, t
1c630 68 65 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f hen.** [SQLITE_O
1c640 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 K] is returned.
1c650 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 Otherwise an [e
1c660 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
1c670 74 75 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 turned. The.**
1c680 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
1c690 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 )] or [sqlite3_e
1c6a0 72 72 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 rrmsg16()] routi
1c6b0 6e 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 nes can be used
1c6c0 74 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 to obtain.** an
1c6d0 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 English language
1c6e0 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 description of
1c6f0 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a the error..**.**
1c700 20 54 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 The default enc
1c710 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 oding for the da
1c720 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 tabase will be U
1c730 54 46 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 TF-8 if.** sqlit
1c740 65 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c e3_open() or sql
1c750 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 ite3_open_v2() i
1c760 73 20 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 s called and.**
1c770 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 UTF-16 in the na
1c780 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 tive byte order
1c790 69 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 if sqlite3_open1
1c7a0 36 28 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 6() is used..**.
1c7b0 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f ** Whether or no
1c7c0 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 t an error occur
1c7d0 73 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 s when it is ope
1c7e0 6e 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a ned, resources.*
1c7f0 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 * associated wit
1c800 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 h the [database
1c810 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 connection] hand
1c820 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c le should be rel
1c830 65 61 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 eased by.** pass
1c840 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 ing it to [sqlit
1c850 65 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e e3_close()] when
1c860 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 it is no longer
1c870 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a required..**.**
1c880 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 The sqlite3_ope
1c890 6e 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 n_v2() interface
1c8a0 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 works like sqli
1c8b0 74 65 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 te3_open().** ex
1c8c0 63 65 70 74 20 74 68 61 74 20 69 74 20 61 63 63 cept that it acc
1c8d0 65 70 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f epts two additio
1c8e0 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 nal parameters f
1c8f0 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f or additional co
1c900 6e 74 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 ntrol.** over th
1c910 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 e new database c
1c920 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 onnection. The
1c930 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
1c940 63 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a can take one of.
1c950 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** the following
1c960 20 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f three values, o
1c970 70 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e ptionally combin
1c980 65 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 ed with the .**
1c990 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d [SQLITE_OPEN_NOM
1c9a0 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f UTEX], [SQLITE_O
1c9b0 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 PEN_FULLMUTEX],
1c9c0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 [SQLITE_OPEN_SHA
1c9d0 52 45 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e REDCACHE],.** an
1c9e0 64 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 d/or [SQLITE_OPE
1c9f0 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 5d 20 N_PRIVATECACHE]
1ca00 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c flags:.**.** <dl
1ca10 3e 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 >.** <dt>[SQLITE
1ca20 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c _OPEN_READONLY]<
1ca30 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1ca40 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e database is open
1ca50 65 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 ed in read-only
1ca60 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 mode. If the da
1ca70 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a tabase does not.
1ca80 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ** already exist
1ca90 2c 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 , an error is re
1caa0 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a turned.</dd>.**.
1cab0 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f ** <dt>[SQLITE_O
1cac0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f PEN_READWRITE]</
1cad0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 dt>.** <dd>The d
1cae0 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 atabase is opene
1caf0 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e d for reading an
1cb00 64 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 d writing if pos
1cb10 73 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e sible, or readin
1cb20 67 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 g.** only if the
1cb30 20 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 file is write p
1cb40 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 rotected by the
1cb50 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d operating system
1cb60 2e 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 . In either.**
1cb70 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 case the databas
1cb80 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 e must already e
1cb90 78 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 xist, otherwise
1cba0 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 an error is retu
1cbb0 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a rned.</dd>.**.**
1cbc0 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 <dt>[SQLITE_OPE
1cbd0 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b N_READWRITE] | [
1cbe0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
1cbf0 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e TE]</dt>.** <dd>
1cc00 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 The database is
1cc10 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 opened for readi
1cc20 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 ng and writing,
1cc30 61 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69 and is creates i
1cc40 74 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 t if.** it does
1cc50 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 not already exis
1cc60 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62 t. This is the b
1cc70 65 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 ehavior that is
1cc80 61 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a always used for.
1cc90 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ** sqlite3_open(
1cca0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 ) and sqlite3_op
1ccb0 65 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 en16().</dd>.**
1ccc0 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 </dl>.**.** If t
1ccd0 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 he 3rd parameter
1cce0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e to sqlite3_open
1ccf0 5f 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 _v2() is not one
1cd00 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 of the.** combi
1cd10 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 nations shown ab
1cd20 6f 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 ove or one of th
1cd30 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 e combinations s
1cd40 68 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 hown above combi
1cd50 6e 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 ned.** with the
1cd60 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d [SQLITE_OPEN_NOM
1cd70 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f UTEX], [SQLITE_O
1cd80 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a PEN_FULLMUTEX],.
1cd90 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ** [SQLITE_OPEN_
1cda0 53 48 41 52 45 44 43 41 43 48 45 5d 20 61 6e 64 SHAREDCACHE] and
1cdb0 2f 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e /or [SQLITE_OPEN
1cdc0 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c _SHAREDCACHE] fl
1cdd0 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ags,.** then the
1cde0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
1cdf0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 efined..**.** If
1ce00 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 the [SQLITE_OPE
1ce10 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 N_NOMUTEX] flag
1ce20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
1ce30 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1ce40 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e tion.** opens in
1ce50 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 the multi-threa
1ce60 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 d [threading mod
1ce70 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 e] as long as th
1ce80 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a e single-thread.
1ce90 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 ** mode has not
1cea0 62 65 65 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 been set at comp
1ceb0 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 ile-time or star
1cec0 74 2d 74 69 6d 65 2e 20 20 49 66 20 74 68 65 0a t-time. If the.
1ced0 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ** [SQLITE_OPEN_
1cee0 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 FULLMUTEX] flag
1cef0 69 73 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 is set then the
1cf00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1cf10 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 ion opens.** in
1cf20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b the serialized [
1cf30 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 threading mode]
1cf40 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 unless single-th
1cf50 72 65 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 read was.** prev
1cf60 69 6f 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 iously selected
1cf70 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 at compile-time
1cf80 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a or start-time..*
1cf90 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 * The [SQLITE_OP
1cfa0 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 EN_SHAREDCACHE]
1cfb0 66 6c 61 67 20 63 61 75 73 65 73 20 74 68 65 20 flag causes the
1cfc0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
1cfd0 69 6f 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 ion to be.** eli
1cfe0 67 69 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 gible to use [sh
1cff0 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d ared cache mode]
1d000 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 , regardless of
1d010 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 whether or not s
1d020 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 hared.** cache i
1d030 73 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 s enabled using
1d040 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f [sqlite3_enable_
1d050 73 68 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e shared_cache()].
1d060 20 20 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 The.** [SQLITE
1d070 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 _OPEN_PRIVATECAC
1d080 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20 HE] flag causes
1d090 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
1d0a0 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a nection to not.*
1d0b0 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e * participate in
1d0c0 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 20 6d [shared cache m
1d0d0 6f 64 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20 ode] even if it
1d0e0 69 73 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a is enabled..**.*
1d0f0 2a 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d * If the filenam
1d100 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c e is ":memory:",
1d110 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c then a private,
1d120 20 74 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 temporary in-me
1d130 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a mory database.**
1d140 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 is created for
1d150 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 the connection.
1d160 20 54 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 This in-memory
1d170 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 database will va
1d180 6e 69 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 nish when.** the
1d190 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1d1a0 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 tion is closed.
1d1b0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 Future versions
1d1c0 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 of SQLite might
1d1d0 0a 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 .** make use of
1d1e0 61 64 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 additional speci
1d1f0 61 6c 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 al filenames tha
1d200 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 t begin with the
1d210 20 22 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a ":" character..
1d220 2a 2a 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 ** It is recomme
1d230 6e 64 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 nded that when a
1d240 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 database filena
1d250 6d 65 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 me actually does
1d260 20 62 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 begin with.** a
1d270 20 22 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 ":" character y
1d280 6f 75 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 ou should prefix
1d290 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 the filename wi
1d2a0 74 68 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 th a pathname su
1d2b0 63 68 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f ch as.** "./" to
1d2c0 20 61 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 avoid ambiguity
1d2d0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 ..**.** If the f
1d2e0 69 6c 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d ilename is an em
1d2f0 70 74 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e pty string, then
1d300 20 61 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 a private, temp
1d310 6f 72 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b orary.** on-disk
1d320 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 database will b
1d330 65 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 e created. This
1d340 20 70 72 69 76 61 74 65 20 64 61 74 61 62 61 73 private databas
1d350 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 e will be.** aut
1d360 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 omatically delet
1d370 65 64 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 ed as soon as th
1d380 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1d390 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e ction is closed.
1d3a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 .**.** The fourt
1d3b0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 h parameter to s
1d3c0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
1d3d0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 is the name of
1d3e0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f the.** [sqlite3_
1d3f0 76 66 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 vfs] object that
1d400 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 defines the ope
1d410 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
1d420 74 65 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 terface that.**
1d430 74 68 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 the new database
1d440 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 connection shou
1d450 6c 64 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 ld use. If the
1d460 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 fourth parameter
1d470 20 69 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f is.** a NULL po
1d480 69 6e 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 inter then the d
1d490 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f efault [sqlite3_
1d4a0 76 66 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 vfs] object is u
1d4b0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f sed..**.** <b>No
1d4c0 74 65 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 te to Windows us
1d4d0 65 72 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e ers:</b> The en
1d4e0 63 6f 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 coding used for
1d4f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 the filename arg
1d500 75 6d 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 ument.** of sqli
1d510 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 te3_open() and s
1d520 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 qlite3_open_v2()
1d530 20 6d 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 must be UTF-8,
1d540 6e 6f 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 not whatever.**
1d550 63 6f 64 65 70 61 67 65 20 69 73 20 63 75 72 72 codepage is curr
1d560 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 ently defined.
1d570 46 69 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 Filenames contai
1d580 6e 69 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e ning internation
1d590 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 al.** characters
1d5a0 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 must be convert
1d5b0 65 64 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f ed to UTF-8 prio
1d5c0 72 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 r to passing the
1d5d0 6d 20 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 m into.** sqlite
1d5e0 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 3_open() or sqli
1d5f0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a te3_open_v2()..*
1d600 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
1d610 73 3a 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b s:.** [H12701] [
1d620 48 31 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d H12702] [H12703]
1d630 20 5b 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 [H12704] [H1270
1d640 36 5d 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 6] [H12707] [H12
1d650 37 30 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 709] [H12711].**
1d660 20 5b 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 [H12712] [H1271
1d670 33 5d 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 3] [H12714] [H12
1d680 37 31 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 717] [H12719] [H
1d690 31 32 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 12721] [H12723].
1d6a0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
1d6b0 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a t sqlite3_open(.
1d6c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 const char *fi
1d6d0 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 lename, /* Dat
1d6e0 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 abase filename (
1d6f0 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 UTF-8) */. sqli
1d700 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 te3 **ppDb
1d710 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 /* OUT: SQLi
1d720 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a te db handle */.
1d730 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
1d740 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 t sqlite3_open16
1d750 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a (. const void *
1d760 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 filename, /* D
1d770 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 atabase filename
1d780 20 28 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 (UTF-16) */. s
1d790 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 qlite3 **ppDb
1d7a0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 /* OUT: S
1d7b0 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 QLite db handle
1d7c0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
1d7d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 int sqlite3_ope
1d7e0 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 n_v2(. const ch
1d7f0 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 ar *filename,
1d800 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 /* Database file
1d810 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a name (UTF-8) */.
1d820 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 sqlite3 **ppDb
1d830 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 , /* OUT
1d840 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 : SQLite db hand
1d850 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 le */. int flag
1d860 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s,
1d870 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f /* Flags */. co
1d880 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 nst char *zVfs
1d890 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
1d8a0 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 VFS module to u
1d8b0 73 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 se */.);../*.**
1d8c0 43 41 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 CAPI3REF: Error
1d8d0 43 6f 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 Codes And Messag
1d8e0 65 73 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 es {H12800} <S60
1d8f0 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 200>.**.** The s
1d900 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 qlite3_errcode()
1d910 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 interface retur
1d920 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b ns the numeric [
1d930 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a result code] or.
1d940 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 ** [extended res
1d950 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 ult code] for th
1d960 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 e most recent fa
1d970 69 6c 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 iled sqlite3_* A
1d980 50 49 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 PI call.** assoc
1d990 69 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 iated with a [da
1d9a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
1d9b0 6e 5d 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 n]. If a prior A
1d9c0 50 49 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a PI call failed.*
1d9d0 2a 20 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 * but the most r
1d9e0 65 63 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 ecent API call s
1d9f0 75 63 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 ucceeded, the re
1da00 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a turn value from.
1da10 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f ** sqlite3_errco
1da20 64 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 de() is undefine
1da30 64 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f d. The sqlite3_
1da40 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 extended_errcode
1da50 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 ().** interface
1da60 69 73 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 is the same exce
1da70 70 74 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 pt that it alway
1da80 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a s returns the .*
1da90 2a 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 * [extended resu
1daa0 6c 74 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 lt code] even wh
1dab0 65 6e 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 en extended resu
1dac0 6c 74 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 lt codes are.**
1dad0 64 69 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 disabled..**.**
1dae0 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d The sqlite3_errm
1daf0 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 sg() and sqlite3
1db00 5f 65 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 _errmsg16() retu
1db10 72 6e 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 rn English-langu
1db20 61 67 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 age.** text that
1db30 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 describes the e
1db40 72 72 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 rror, as either
1db50 55 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 UTF-8 or UTF-16
1db60 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a respectively..**
1db70 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 Memory to hold
1db80 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
1db90 65 20 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 e string is mana
1dba0 67 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a ged internally..
1dbb0 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 ** The applicati
1dbc0 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
1dbd0 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 to worry about
1dbe0 66 72 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 freeing the resu
1dbf0 6c 74 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 lt..** However,
1dc00 74 68 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 the error string
1dc10 20 6d 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 might be overwr
1dc20 69 74 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 itten or dealloc
1dc30 61 74 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 ated by.** subse
1dc40 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f quent calls to o
1dc50 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 ther SQLite inte
1dc60 72 66 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e rface functions.
1dc70 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
1dc80 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 serialized [thre
1dc90 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 ading mode] is i
1dca0 6e 20 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 n use, it might
1dcb0 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 be the.** case t
1dcc0 68 61 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 hat a second err
1dcd0 6f 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 or occurs on a s
1dce0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 69 eparate thread i
1dcf0 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 n between.** the
1dd00 20 74 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 time of the fir
1dd10 73 74 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 st error and the
1dd20 20 63 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 call to these i
1dd30 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 nterfaces..** Wh
1dd40 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c en that happens,
1dd50 20 74 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f the second erro
1dd60 72 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 r will be report
1dd70 65 64 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a ed since these.*
1dd80 2a 20 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 * interfaces alw
1dd90 61 79 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d ays report the m
1dda0 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c ost recent resul
1ddb0 74 2e 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 t. To avoid.**
1ddc0 74 68 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 this, each threa
1ddd0 64 20 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 d can obtain exc
1dde0 6c 75 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 lusive use of th
1ddf0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
1de00 65 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 ection] D.** by
1de10 69 6e 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 invoking [sqlite
1de20 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 3_mutex_enter]([
1de30 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 sqlite3_db_mutex
1de40 5d 28 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 ](D)) before beg
1de50 69 6e 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 inning.** to use
1de60 20 44 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 D and invoking
1de70 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c [sqlite3_mutex_l
1de80 65 61 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 eave]([sqlite3_d
1de90 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 b_mutex](D)) aft
1dea0 65 72 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 er.** all calls
1deb0 74 6f 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 to the interface
1dec0 73 20 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 s listed here ar
1ded0 65 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a e completed..**.
1dee0 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 ** If an interfa
1def0 63 65 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 ce fails with SQ
1df00 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 LITE_MISUSE, tha
1df10 74 20 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 t means the inte
1df20 72 66 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 rface.** was inv
1df30 6f 6b 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 oked incorrectly
1df40 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 by the applicat
1df50 69 6f 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 ion. In that ca
1df60 73 65 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 se, the.** error
1df70 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 code and messag
1df80 65 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 e may or may not
1df90 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 be set..**.** R
1dfa0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 equirements:.**
1dfb0 5b 48 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 [H12801] [H12802
1dfc0 5d 20 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 ] [H12803] [H128
1dfd0 30 37 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 07] [H12808] [H1
1dfe0 32 38 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 2809].*/.SQLITE_
1dff0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1e000 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 errcode(sqlite3
1e010 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 *db);.SQLITE_API
1e020 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 int sqlite3_ext
1e030 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 ended_errcode(sq
1e040 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 lite3 *db);.SQLI
1e050 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
1e060 72 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 r *sqlite3_errms
1e070 67 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c g(sqlite3*);.SQL
1e080 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f ITE_API const vo
1e090 69 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d id *sqlite3_errm
1e0a0 73 67 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a sg16(sqlite3*);.
1e0b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
1e0c0 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f SQL Statement O
1e0d0 62 6a 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c bject {H13000} <
1e0e0 48 31 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f H13010>.** KEYWO
1e0f0 52 44 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 RDS: {prepared s
1e100 74 61 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 tatement} {prepa
1e110 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a red statements}.
1e120 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 **.** An instanc
1e130 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 e of this object
1e140 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 represents a si
1e150 6e 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 ngle SQL stateme
1e160 6e 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 nt..** This obje
1e170 63 74 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 ct is variously
1e180 6b 6e 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 known as a "prep
1e190 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 ared statement"
1e1a0 6f 72 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 or a.** "compile
1e1b0 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 d SQL statement"
1e1c0 20 6f 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 or simply as a
1e1d0 22 73 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a "statement"..**.
1e1e0 2a 2a 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 ** The life of a
1e1f0 20 73 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 statement objec
1e200 74 20 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 t goes something
1e210 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
1e220 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 * <ol>.** <li> C
1e230 72 65 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 reate the object
1e240 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f using [sqlite3_
1e250 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 prepare_v2()] or
1e260 20 61 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 a related.**
1e270 20 20 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 function..**
1e280 3c 6c 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 <li> Bind values
1e290 20 74 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 to [host parame
1e2a0 74 65 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 ters] using the
1e2b0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 sqlite3_bind_*()
1e2c0 0a 2a 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 .** interfa
1e2d0 63 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e ces..** <li> Run
1e2e0 20 74 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c the SQL by call
1e2f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 ing [sqlite3_ste
1e300 70 28 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 p()] one or more
1e310 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 times..** <li>
1e320 52 65 73 65 74 20 74 68 65 20 73 74 61 74 65 6d Reset the statem
1e330 65 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ent using [sqlit
1e340 65 33 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e e3_reset()] then
1e350 20 67 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 go back.**
1e360 20 74 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 to step 2. Do
1e370 74 68 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 this zero or mor
1e380 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e e times..** <li>
1e390 20 44 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a Destroy the obj
1e3a0 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ect using [sqlit
1e3b0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a e3_finalize()]..
1e3c0 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 ** </ol>.**.** R
1e3d0 65 66 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 efer to document
1e3e0 61 74 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 ation on individ
1e3f0 75 61 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 ual methods abov
1e400 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c e for additional
1e410 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e .** information.
1e420 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
1e430 63 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ct sqlite3_stmt
1e440 73 71 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f sqlite3_stmt;../
1e450 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 *.** CAPI3REF: R
1e460 75 6e 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b un-time Limits {
1e470 48 31 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e H12760} <S20600>
1e480 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 .**.** This inte
1e490 72 66 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 rface allows the
1e4a0 20 73 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 size of various
1e4b0 20 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 constructs to b
1e4c0 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 e limited.** on
1e4d0 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 a connection by
1e4e0 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 connection basis
1e4f0 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 . The first par
1e500 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a ameter is the.**
1e510 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 [database conne
1e520 63 74 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d ction] whose lim
1e530 69 74 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 it is to be set
1e540 6f 72 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 or queried. The
1e550 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d .** second param
1e560 65 74 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 eter is one of t
1e570 68 65 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f he [limit catego
1e580 72 69 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e ries] that defin
1e590 65 20 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 e a.** class of
1e5a0 63 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 constructs to be
1e5b0 20 73 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 size limited.
1e5c0 54 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 The third parame
1e5d0 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 ter is the.** ne
1e5e0 77 20 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 w limit for that
1e5f0 20 63 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 construct. The
1e600 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1e610 73 20 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e s the old limit.
1e620 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 .**.** If the ne
1e630 77 20 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 w limit is a neg
1e640 61 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 ative number, th
1e650 65 20 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 e limit is uncha
1e660 6e 67 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 nged..** For the
1e670 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 limit category
1e680 6f 66 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f of SQLITE_LIMIT_
1e690 58 59 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a XYZ there is a .
1e6a0 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 ** [limits | har
1e6b0 64 20 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a d upper bound].*
1e6c0 2a 20 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 * set by a compi
1e6d0 6c 65 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f le-time C prepro
1e6e0 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d cessor macro nam
1e6f0 65 64 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c ed .** [limits |
1e700 20 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d SQLITE_MAX_XYZ]
1e710 2e 0a 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 ..** (The "_LIMI
1e720 54 5f 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 T_" in the name
1e730 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f is changed to "_
1e740 4d 41 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d MAX_".).** Attem
1e750 70 74 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 pts to increase
1e760 61 20 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 a limit above it
1e770 73 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 s hard upper bou
1e780 6e 64 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 nd are.** silent
1e790 6c 79 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 ly truncated to
1e7a0 74 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c the hard upper l
1e7b0 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 imit..**.** Run
1e7c0 74 69 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 time limits are
1e7d0 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 intended for use
1e7e0 20 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 in applications
1e7f0 20 74 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 that manage.**
1e800 62 6f 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 both their own i
1e810 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 nternal database
1e820 20 61 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 and also databa
1e830 73 65 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e ses that are con
1e840 74 72 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e trolled.** by un
1e850 74 72 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c trusted external
1e860 20 73 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 sources. An ex
1e870 61 6d 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f ample applicatio
1e880 6e 20 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 n might be a.**
1e890 77 65 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 web browser that
1e8a0 20 68 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 has its own dat
1e8b0 61 62 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 abases for stori
1e8c0 6e 67 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a ng history and.*
1e8d0 2a 20 73 65 70 61 72 61 74 65 20 64 61 74 61 62 * separate datab
1e8e0 61 73 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 ases controlled
1e8f0 62 79 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 by JavaScript ap
1e900 70 6c 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c plications downl
1e910 6f 61 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 oaded.** off the
1e920 20 49 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 Internet. The
1e930 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 internal databas
1e940 65 73 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 es can be given
1e950 74 68 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 the.** large, de
1e960 66 61 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 fault limits. D
1e970 61 74 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 atabases managed
1e980 20 62 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 by external sou
1e990 72 63 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 rces can.** be g
1e9a0 69 76 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 iven much smalle
1e9b0 72 20 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 r limits designe
1e9c0 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 d to prevent a d
1e9d0 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 enial of service
1e9e0 0a 2a 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 .** attack. Dev
1e9f0 65 6c 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c elopers might al
1ea00 73 6f 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 so want to use t
1ea10 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f he [sqlite3_set_
1ea20 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a authorizer()].**
1ea30 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 interface to fu
1ea40 72 74 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e rther control un
1ea50 74 72 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 trusted SQL. Th
1ea60 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
1ea70 74 61 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 tabase.** create
1ea80 64 20 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 d by an untruste
1ea90 64 20 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 d script can be
1eaa0 63 6f 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 contained using
1eab0 74 68 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 the.** [max_page
1eac0 5f 63 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d _count] [PRAGMA]
1ead0 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d ..**.** New run-
1eae0 74 69 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 time limit categ
1eaf0 6f 72 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 ories may be add
1eb00 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c ed in future rel
1eb10 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 eases..**.** Req
1eb20 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
1eb30 31 32 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 12762] [H12766]
1eb40 5b 48 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 [H12769].*/.SQLI
1eb50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1eb60 65 33 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 e3_limit(sqlite3
1eb70 2a 2c 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e *, int id, int n
1eb80 65 77 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 ewVal);../*.** C
1eb90 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d API3REF: Run-Tim
1eba0 65 20 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 e Limit Categori
1ebb0 65 73 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 es {H12790} <H12
1ebc0 37 36 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 760>.** KEYWORDS
1ebd0 3a 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 : {limit categor
1ebe0 79 7d 20 7b 2a 6c 69 6d 69 74 20 63 61 74 65 67 y} {*limit categ
1ebf0 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 ories}.**.** The
1ec00 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 se constants def
1ec10 69 6e 65 20 76 61 72 69 6f 75 73 20 70 65 72 66 ine various perf
1ec20 6f 72 6d 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a ormance limits.*
1ec30 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c 6f * that can be lo
1ec40 77 65 72 65 64 20 61 74 20 72 75 6e 2d 74 69 6d wered at run-tim
1ec50 65 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 e using [sqlite3
1ec60 5f 6c 69 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 _limit()]..** Th
1ec70 65 20 73 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 e synopsis of th
1ec80 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 e meanings of th
1ec90 65 20 76 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 e various limits
1eca0 20 69 73 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e is shown below.
1ecb0 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 .** Additional i
1ecc0 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 nformation is av
1ecd0 61 69 6c 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 ailable at [limi
1ece0 74 73 20 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 ts | Limits in S
1ecf0 51 4c 69 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 QLite]..**.** <d
1ed00 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 l>.** <dt>SQLITE
1ed10 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 _LIMIT_LENGTH</d
1ed20 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1ed30 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e ximum size of an
1ed40 79 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 y string or BLOB
1ed50 20 6f 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 or table row.<d
1ed60 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c d>.**.** <dt>SQL
1ed70 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 ITE_LIMIT_SQL_LE
1ed80 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 NGTH</dt>.** <dd
1ed90 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e >The maximum len
1eda0 67 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 gth of an SQL st
1edb0 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a atement.</dd>.**
1edc0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c .** <dt>SQLITE_L
1edd0 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e IMIT_COLUMN</dt>
1ede0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 .** <dd>The maxi
1edf0 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f mum number of co
1ee00 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 lumns in a table
1ee10 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 definition or i
1ee20 6e 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 n the.** result
1ee30 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 set of a [SELECT
1ee40 5d 20 6f 72 20 74 68 65 20 6d 61 78 69 6d 75 6d ] or the maximum
1ee50 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1ee60 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a ns in an index.*
1ee70 2a 20 6f 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 * or in an ORDER
1ee80 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 BY or GROUP BY
1ee90 63 6c 61 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a clause.</dd>.**.
1eea0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 ** <dt>SQLITE_LI
1eeb0 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f MIT_EXPR_DEPTH</
1eec0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d dt>.** <dd>The m
1eed0 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 aximum depth of
1eee0 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 6f the parse tree o
1eef0 6e 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e n any expression
1ef00 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 .</dd>.**.** <dt
1ef10 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f >SQLITE_LIMIT_CO
1ef20 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 MPOUND_SELECT</d
1ef30 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 t>.** <dd>The ma
1ef40 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 ximum number of
1ef50 74 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f terms in a compo
1ef60 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 und SELECT state
1ef70 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ment.</dd>.**.**
1ef80 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1ef90 54 5f 56 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a T_VDBE_OP</dt>.*
1efa0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 * <dd>The maximu
1efb0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 m number of inst
1efc0 72 75 63 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 ructions in a vi
1efd0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 rtual machine pr
1efe0 6f 67 72 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f ogram.** used to
1eff0 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 implement an SQ
1f000 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 L statement.</dd
1f010 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 >.**.** <dt>SQLI
1f020 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f TE_LIMIT_FUNCTIO
1f030 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 N_ARG</dt>.** <d
1f040 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 d>The maximum nu
1f050 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
1f060 73 20 6f 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e s on a function.
1f070 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e </dd>.**.** <dt>
1f080 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 SQLITE_LIMIT_ATT
1f090 41 43 48 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 ACHED</dt>.** <d
1f0a0 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 d>The maximum nu
1f0b0 6d 62 65 72 20 6f 66 20 5b 41 54 54 41 43 48 20 mber of [ATTACH
1f0c0 7c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 | attached datab
1f0d0 61 73 65 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a ases].</dd>.**.*
1f0e0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d * <dt>SQLITE_LIM
1f0f0 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f IT_LIKE_PATTERN_
1f100 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c LENGTH</dt>.** <
1f110 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c dd>The maximum l
1f120 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70 61 74 ength of the pat
1f130 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f tern argument to
1f140 20 74 68 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a the [LIKE] or.*
1f150 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f * [GLOB] operato
1f160 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c rs.</dd>.**.** <
1f170 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f dt>SQLITE_LIMIT_
1f180 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c VARIABLE_NUMBER<
1f190 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1f1a0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
1f1b0 66 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 f variables in a
1f1c0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 n SQL statement
1f1d0 74 68 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 that can.** be b
1f1e0 6f 75 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a ound.</dd>.**.**
1f1f0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 <dt>SQLITE_LIMI
1f200 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 3c T_TRIGGER_DEPTH<
1f210 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 /dt>.** <dd>The
1f220 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 maximum depth of
1f230 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72 20 74 recursion for t
1f240 72 69 67 67 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a riggers.</dd>.**
1f250 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e </dl>.*/.#defin
1f260 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c e SQLITE_LIMIT_L
1f270 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20 ENGTH
1f280 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 0.#defi
1f290 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f ne SQLITE_LIMIT_
1f2a0 53 51 4c 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 SQL_LENGTH
1f2b0 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
1f2c0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 ine SQLITE_LIMIT
1f2d0 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 _COLUMN
1f2e0 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 2.#de
1f2f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 fine SQLITE_LIMI
1f300 54 5f 45 58 50 52 5f 44 45 50 54 48 20 20 20 20 T_EXPR_DEPTH
1f310 20 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 3.#d
1f320 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d efine SQLITE_LIM
1f330 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 IT_COMPOUND_SELE
1f340 43 54 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 CT 4.#
1f350 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 define SQLITE_LI
1f360 4d 49 54 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 MIT_VDBE_OP
1f370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 5.
1f380 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c #define SQLITE_L
1f390 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 IMIT_FUNCTION_AR
1f3a0 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 G 6
1f3b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
1f3c0 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 20 20 LIMIT_ATTACHED
1f3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f3e0 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 7.#define SQLITE
1f3f0 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 _LIMIT_LIKE_PATT
1f400 45 52 4e 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 ERN_LENGTH
1f410 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 8.#define SQLIT
1f420 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 E_LIMIT_VARIABLE
1f430 5f 4e 55 4d 42 45 52 20 20 20 20 20 20 20 20 20 _NUMBER
1f440 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 9.#define SQLI
1f450 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 TE_LIMIT_TRIGGER
1f460 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20 20 _DEPTH
1f470 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 10../*.** CAPI
1f480 33 52 45 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 3REF: Compiling
1f490 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 An SQL Statement
1f4a0 20 7b 48 31 33 30 31 30 7d 20 3c 53 31 30 30 30 {H13010} <S1000
1f4b0 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 0>.** KEYWORDS:
1f4c0 7b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 {SQL statement c
1f4d0 6f 6d 70 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 ompiler}.**.** T
1f4e0 6f 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c o execute an SQL
1f4f0 20 71 75 65 72 79 2c 20 69 74 20 6d 75 73 74 20 query, it must
1f500 66 69 72 73 74 20 62 65 20 63 6f 6d 70 69 6c 65 first be compile
1f510 64 20 69 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f d into a byte-co
1f520 64 65 0a 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73 de.** program us
1f530 69 6e 67 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 ing one of these
1f540 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a routines..**.**
1f550 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d The first argum
1f560 65 6e 74 2c 20 22 64 62 22 2c 20 69 73 20 61 20 ent, "db", is a
1f570 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
1f580 74 69 6f 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66 tion] obtained f
1f590 72 6f 6d 20 61 0a 2a 2a 20 70 72 69 6f 72 20 73 rom a.** prior s
1f5a0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
1f5b0 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 o [sqlite3_open(
1f5c0 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
1f5d0 6e 5f 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 n_v2()] or.** [s
1f5e0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d qlite3_open16()]
1f5f0 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 . The database
1f600 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 connection must
1f610 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20 63 6c not have been cl
1f620 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 osed..**.** The
1f630 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c second argument,
1f640 20 22 7a 53 71 6c 22 2c 20 69 73 20 74 68 65 20 "zSql", is the
1f650 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 statement to be
1f660 63 6f 6d 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 compiled, encode
1f670 64 0a 2a 2a 20 61 73 20 65 69 74 68 65 72 20 55 d.** as either U
1f680 54 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 TF-8 or UTF-16.
1f690 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 The sqlite3_pre
1f6a0 70 61 72 65 28 29 20 61 6e 64 20 73 71 6c 69 74 pare() and sqlit
1f6b0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 0a e3_prepare_v2().
1f6c0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 75 73 ** interfaces us
1f6d0 65 20 55 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c e UTF-8, and sql
1f6e0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 ite3_prepare16()
1f6f0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 and sqlite3_pre
1f700 70 61 72 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 pare16_v2().** u
1f710 73 65 20 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a se UTF-16..**.**
1f720 20 49 66 20 74 68 65 20 6e 42 79 74 65 20 61 72 If the nByte ar
1f730 67 75 6d 65 6e 74 20 69 73 20 6c 65 73 73 20 74 gument is less t
1f740 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a han zero, then z
1f750 53 71 6c 20 69 73 20 72 65 61 64 20 75 70 20 74 Sql is read up t
1f760 6f 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a o the.** first z
1f770 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 ero terminator.
1f780 49 66 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d If nByte is non-
1f790 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 69 negative, then i
1f7a0 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d t is the maximum
1f7b0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 .** number of b
1f7c0 79 74 65 73 20 72 65 61 64 20 66 72 6f 6d 20 7a ytes read from z
1f7d0 53 71 6c 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 Sql. When nByte
1f7e0 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 is non-negative
1f7f0 2c 20 74 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 , the.** zSql st
1f800 72 69 6e 67 20 65 6e 64 73 20 61 74 20 65 69 74 ring ends at eit
1f810 68 65 72 20 74 68 65 20 66 69 72 73 74 20 27 5c her the first '\
1f820 30 30 30 27 20 6f 72 20 27 5c 75 30 30 30 30 27 000' or '\u0000'
1f830 20 63 68 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a character or.**
1f840 20 74 68 65 20 6e 42 79 74 65 2d 74 68 20 62 79 the nByte-th by
1f850 74 65 2c 20 77 68 69 63 68 65 76 65 72 20 63 6f te, whichever co
1f860 6d 65 73 20 66 69 72 73 74 2e 20 49 66 20 74 68 mes first. If th
1f870 65 20 63 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a e caller knows.*
1f880 2a 20 74 68 61 74 20 74 68 65 20 73 75 70 70 6c * that the suppl
1f890 69 65 64 20 73 74 72 69 6e 67 20 69 73 20 6e 75 ied string is nu
1f8a0 6c 2d 74 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 l-terminated, th
1f8b0 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 73 6d en there is a sm
1f8c0 61 6c 6c 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e all.** performan
1f8d0 63 65 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20 ce advantage to
1f8e0 62 65 20 67 61 69 6e 65 64 20 62 79 20 70 61 73 be gained by pas
1f8f0 73 69 6e 67 20 61 6e 20 6e 42 79 74 65 20 70 61 sing an nByte pa
1f900 72 61 6d 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 rameter that.**
1f910 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 is equal to the
1f920 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1f930 69 6e 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 in the input str
1f940 69 6e 67 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 ing <i>including
1f950 3c 2f 69 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d </i>.** the nul-
1f960 74 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 terminator bytes
1f970 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 7a 54 61 69 ..**.** If pzTai
1f980 6c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 l is not NULL th
1f990 65 6e 20 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 en *pzTail is ma
1f9a0 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 de to point to t
1f9b0 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a he first byte.**
1f9c0 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 past the end of
1f9d0 20 74 68 65 20 66 69 72 73 74 20 53 51 4c 20 73 the first SQL s
1f9e0 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c tatement in zSql
1f9f0 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 . These routine
1fa00 73 20 6f 6e 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c s only.** compil
1fa10 65 20 74 68 65 20 66 69 72 73 74 20 73 74 61 74 e the first stat
1fa20 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 ement in zSql, s
1fa30 6f 20 2a 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 o *pzTail is lef
1fa40 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a t pointing to.**
1fa50 20 77 68 61 74 20 72 65 6d 61 69 6e 73 20 75 6e what remains un
1fa60 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 compiled..**.**
1fa70 2a 70 70 53 74 6d 74 20 69 73 20 6c 65 66 74 20 *ppStmt is left
1fa80 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f pointing to a co
1fa90 6d 70 69 6c 65 64 20 5b 70 72 65 70 61 72 65 64 mpiled [prepared
1faa0 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 statement] that
1fab0 20 63 61 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 can be.** execu
1fac0 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 ted using [sqlit
1fad0 65 33 5f 73 74 65 70 28 29 5d 2e 20 20 49 66 20 e3_step()]. If
1fae0 74 68 65 72 65 20 69 73 20 61 6e 20 65 72 72 6f there is an erro
1faf0 72 2c 20 2a 70 70 53 74 6d 74 20 69 73 20 73 65 r, *ppStmt is se
1fb00 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 t.** to NULL. I
1fb10 66 20 74 68 65 20 69 6e 70 75 74 20 74 65 78 74 f the input text
1fb20 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c contains no SQL
1fb30 20 28 69 66 20 74 68 65 20 69 6e 70 75 74 20 69 (if the input i
1fb40 73 20 61 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 s an empty.** st
1fb50 72 69 6e 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e ring or a commen
1fb60 74 29 20 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 t) then *ppStmt
1fb70 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a is set to NULL..
1fb80 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 ** The calling p
1fb90 72 6f 63 65 64 75 72 65 20 69 73 20 72 65 73 70 rocedure is resp
1fba0 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 6c 65 onsible for dele
1fbb0 74 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 65 ting the compile
1fbc0 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 d.** SQL stateme
1fbd0 6e 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 nt using [sqlite
1fbe0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66 3_finalize()] af
1fbf0 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69 73 ter it has finis
1fc00 68 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2a 20 hed with it..**
1fc10 70 70 53 74 6d 74 20 6d 61 79 20 6e 6f 74 20 62 ppStmt may not b
1fc20 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e e NULL..**.** On
1fc30 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 success, [SQLIT
1fc40 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 E_OK] is returne
1fc50 64 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 d, otherwise an
1fc60 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 [error code] is
1fc70 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1fc80 54 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 The sqlite3_prep
1fc90 61 72 65 5f 76 32 28 29 20 61 6e 64 20 73 71 6c are_v2() and sql
1fca0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 ite3_prepare16_v
1fcb0 32 28 29 20 69 6e 74 65 72 66 61 63 65 73 20 61 2() interfaces a
1fcc0 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 re.** recommende
1fcd0 64 20 66 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72 d for all new pr
1fce0 6f 67 72 61 6d 73 2e 20 54 68 65 20 74 77 6f 20 ograms. The two
1fcf0 6f 6c 64 65 72 20 69 6e 74 65 72 66 61 63 65 73 older interfaces
1fd00 20 61 72 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a are retained.**
1fd10 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63 for backwards c
1fd20 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c 20 62 75 ompatibility, bu
1fd30 74 20 74 68 65 69 72 20 75 73 65 20 69 73 20 64 t their use is d
1fd40 69 73 63 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49 iscouraged..** I
1fd50 6e 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 n the "v2" inter
1fd60 66 61 63 65 73 2c 20 74 68 65 20 70 72 65 70 61 faces, the prepa
1fd70 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a red statement.**
1fd80 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 that is returne
1fd90 64 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f d (the [sqlite3_
1fda0 73 74 6d 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f stmt] object) co
1fdb0 6e 74 61 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 ntains a copy of
1fdc0 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c the.** original
1fdd0 20 53 51 4c 20 74 65 78 74 2e 20 54 68 69 73 20 SQL text. This
1fde0 63 61 75 73 65 73 20 74 68 65 20 5b 73 71 6c 69 causes the [sqli
1fdf0 74 65 33 5f 73 74 65 70 28 29 5d 20 69 6e 74 65 te3_step()] inte
1fe00 72 66 61 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61 rface to.** beha
1fe10 76 65 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 ve differently i
1fe20 6e 20 74 68 72 65 65 20 77 61 79 73 3a 0a 2a 2a n three ways:.**
1fe30 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ol>.** <li>
1fe40 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 .** If the datab
1fe50 61 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 ase schema chang
1fe60 65 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 es, instead of r
1fe70 65 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 eturning [SQLITE
1fe80 5f 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a _SCHEMA] as it.*
1fe90 2a 20 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f * always used to
1fea0 20 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 do, [sqlite3_st
1feb0 65 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d ep()] will autom
1fec0 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 atically recompi
1fed0 6c 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 le the SQL.** st
1fee0 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 atement and try
1fef0 74 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e to run it again.
1ff00 20 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 If the schema
1ff10 68 61 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a has changed in.*
1ff20 2a 20 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b * a way that mak
1ff30 65 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 es the statement
1ff40 20 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 no longer valid
1ff50 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 , [sqlite3_step(
1ff60 29 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a )] will still.**
1ff70 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f return [SQLITE_
1ff80 53 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e SCHEMA]. But un
1ff90 6c 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 like the legacy
1ffa0 62 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 behavior, [SQLIT
1ffb0 45 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 E_SCHEMA] is.**
1ffc0 6e 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f now a fatal erro
1ffd0 72 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c r. Calling [sql
1ffe0 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
1fff0 29 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f )] again will no
20000 74 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 t make the.** er
20010 72 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f ror go away. No
20020 74 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 te: use [sqlite3
20030 5f 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 _errmsg()] to fi
20040 6e 64 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f nd the text.** o
20050 66 20 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 f the parsing er
20060 72 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 ror that results
20070 20 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 in an [SQLITE_S
20080 43 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a CHEMA] return..*
20090 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c * </li>.**.** <l
200a0 69 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 i>.** When an er
200b0 72 6f 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c ror occurs, [sql
200c0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c ite3_step()] wil
200d0 6c 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 l return one of
200e0 74 68 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 the detailed.**
200f0 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 [error codes] or
20100 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 [extended error
20110 20 63 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 codes]. The le
20120 67 61 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 gacy behavior wa
20130 73 20 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 s that.** [sqlit
20140 65 33 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 e3_step()] would
20150 20 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 only return a g
20160 65 6e 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 eneric [SQLITE_E
20170 52 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 RROR] result cod
20180 65 0a 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 e.** and you wou
20190 6c 64 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 ld have to make
201a0 61 20 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f a second call to
201b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 [sqlite3_reset(
201c0 29 5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 )] in order.** t
201d0 6f 20 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 o find the under
201e0 6c 79 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 lying cause of t
201f0 68 65 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 he problem. With
20200 20 74 68 65 20 22 76 32 22 20 70 72 65 70 61 72 the "v2" prepar
20210 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c e.** interfaces,
20220 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
20230 72 65 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 reason for the e
20240 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
20250 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a immediately..**
20260 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 </li>.**.** <li
20270 3e 0a 2a 2a 20 5e 49 66 20 74 68 65 20 76 61 6c >.** ^If the val
20280 75 65 20 6f 66 20 61 20 5b 70 61 72 61 6d 65 74 ue of a [paramet
20290 65 72 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 er | host parame
202a0 74 65 72 5d 20 69 6e 20 74 68 65 20 57 48 45 52 ter] in the WHER
202b0 45 20 63 6c 61 75 73 65 20 6d 69 67 68 74 0a 2a E clause might.*
202c0 2a 20 63 68 61 6e 67 65 20 74 68 65 20 71 75 65 * change the que
202d0 72 79 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 ry plan for a st
202e0 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 atement, then th
202f0 65 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 e statement may
20300 62 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 be.** automatica
20310 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 28 lly recompiled (
20320 61 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20 as if there had
20330 62 65 65 6e 20 61 20 73 63 68 65 6d 61 20 63 68 been a schema ch
20340 61 6e 67 65 29 20 6f 6e 20 74 68 65 20 66 69 72 ange) on the fir
20350 73 74 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f st .** [sqlite3_
20360 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c step()] call fol
20370 6c 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 lowing any chang
20380 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 e to the .** [sq
20390 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 lite3_bind_text
203a0 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 | bindings] of t
203b0 68 65 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20 he [parameter].
203c0 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f .** </li>.** </o
203d0 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 l>.**.** Require
203e0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 ments:.** [H1301
203f0 31 5d 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 1] [H13012] [H13
20400 30 31 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 013] [H13014] [H
20410 31 33 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 13015] [H13016]
20420 5b 48 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 [H13019] [H13021
20430 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ].**.*/.SQLITE_A
20440 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
20450 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 repare(. sqlite
20460 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 3 *db,
20470 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 /* Database ha
20480 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ndle */. const
20490 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 char *zSql,
204a0 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 /* SQL stateme
204b0 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 nt, UTF-8 encode
204c0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 d */. int nByte
204d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
204e0 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 * Maximum length
204f0 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 of zSql in byte
20500 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f s. */. sqlite3_
20510 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 stmt **ppStmt,
20520 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e /* OUT: Statemen
20530 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f t handle */. co
20540 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 nst char **pzTai
20550 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f l /* OUT: Po
20560 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 inter to unused
20570 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 portion of zSql
20580 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 */.);.SQLITE_API
20590 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 int sqlite3_pre
205a0 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 pare_v2(. sqlit
205b0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
205c0 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 /* Database h
205d0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
205e0 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 char *zSql,
205f0 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d /* SQL statem
20600 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 ent, UTF-8 encod
20610 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 ed */. int nByt
20620 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e,
20630 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 /* Maximum lengt
20640 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 h of zSql in byt
20650 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 es. */. sqlite3
20660 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 _stmt **ppStmt,
20670 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 /* OUT: Stateme
20680 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 nt handle */. c
20690 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 onst char **pzTa
206a0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 il /* OUT: P
206b0 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 ointer to unused
206c0 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c portion of zSql
206d0 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 */.);.SQLITE_AP
206e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 I int sqlite3_pr
206f0 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 epare16(. sqlit
20700 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
20710 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 /* Database h
20720 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 andle */. const
20730 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 void *zSql,
20740 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d /* SQL statem
20750 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f ent, UTF-16 enco
20760 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 ded */. int nBy
20770 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
20780 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 /* Maximum leng
20790 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 th of zSql in by
207a0 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 tes. */. sqlite
207b0 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 3_stmt **ppStmt,
207c0 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d /* OUT: Statem
207d0 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 ent handle */.
207e0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 const void **pzT
207f0 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 ail /* OUT:
20800 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 Pointer to unuse
20810 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 d portion of zSq
20820 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 l */.);.SQLITE_A
20830 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 PI int sqlite3_p
20840 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 repare16_v2(. s
20850 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 qlite3 *db,
20860 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 /* Databa
20870 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 se handle */. c
20880 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c onst void *zSql,
20890 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 /* SQL st
208a0 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 atement, UTF-16
208b0 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 encoded */. int
208c0 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 nByte,
208d0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 /* Maximum
208e0 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 length of zSql i
208f0 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 n bytes. */. sq
20900 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 lite3_stmt **ppS
20910 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 tmt, /* OUT: St
20920 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a atement handle *
20930 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
20940 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f *pzTail /* O
20950 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 UT: Pointer to u
20960 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 nused portion of
20970 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a zSql */.);../*.
20980 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 ** CAPI3REF: Ret
20990 72 69 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e rieving Statemen
209a0 74 20 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c t SQL {H13100} <
209b0 48 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 H13000>.**.** Th
209c0 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e is interface can
209d0 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 be used to retr
209e0 69 65 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 ieve a saved cop
209f0 79 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 y of the origina
20a00 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 l.** SQL text us
20a10 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b ed to create a [
20a20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
20a30 6e 74 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 nt] if that stat
20a40 65 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d ement was.** com
20a50 70 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 piled using eith
20a60 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 er [sqlite3_prep
20a70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 are_v2()] or [sq
20a80 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f lite3_prepare16_
20a90 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 v2()]..**.** Req
20aa0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
20ab0 31 33 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 13101] [H13102]
20ac0 5b 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 [H13103].*/.SQLI
20ad0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
20ae0 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 r *sqlite3_sql(s
20af0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
20b00 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 mt);../*.** CAPI
20b10 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 3REF: Dynamicall
20b20 79 20 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 y Typed Value Ob
20b30 6a 65 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 ject {H15000} <S
20b40 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 20200>.** KEYWOR
20b50 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 DS: {protected s
20b60 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 qlite3_value} {u
20b70 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
20b80 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 e3_value}.**.**
20b90 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 SQLite uses the
20ba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
20bb0 6a 65 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e ject to represen
20bc0 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 t all values.**
20bd0 74 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 that can be stor
20be0 65 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 ed in a database
20bf0 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 table. SQLite u
20c00 73 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 ses dynamic typi
20c10 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 ng.** for the va
20c20 6c 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 lues it stores.
20c30 56 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e Values stored in
20c40 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
20c50 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 bjects.** can be
20c60 20 69 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 integers, float
20c70 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 ing point values
20c80 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 , strings, BLOBs
20c90 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a , or NULL..**.**
20ca0 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 An sqlite3_valu
20cb0 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 e object may be
20cc0 65 69 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 either "protecte
20cd0 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 d" or "unprotect
20ce0 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 ed"..** Some int
20cf0 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20 erfaces require
20d00 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 a protected sqli
20d10 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 te3_value. Othe
20d20 72 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 r interfaces.**
20d30 77 69 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 will accept eith
20d40 65 72 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f er a protected o
20d50 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 r an unprotected
20d60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a sqlite3_value..
20d70 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 ** Every interfa
20d80 63 65 20 74 68 61 74 20 61 63 63 65 70 74 73 20 ce that accepts
20d90 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 sqlite3_value ar
20da0 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 guments specifie
20db0 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 s.** whether or
20dc0 6e 6f 74 20 69 74 20 72 65 71 75 69 72 65 73 20 not it requires
20dd0 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 a protected sqli
20de0 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a te3_value..**.**
20df0 20 54 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 The terms "prot
20e00 65 63 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 ected" and "unpr
20e10 6f 74 65 63 74 65 64 22 20 72 65 66 65 72 20 74 otected" refer t
20e20 6f 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 o whether or not
20e30 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 .** a mutex is h
20e40 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c eld. A internal
20e50 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 mutex is held f
20e60 6f 72 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a or a protected.*
20e70 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 * sqlite3_value
20e80 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 object but no mu
20e90 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 tex is held for
20ea0 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a an unprotected.*
20eb0 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 * sqlite3_value
20ec0 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 object. If SQLi
20ed0 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 te is compiled t
20ee0 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 o be single-thre
20ef0 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 aded.** (with [S
20f00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
20f10 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 =0] and with [sq
20f20 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 lite3_threadsafe
20f30 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 ()] returning 0)
20f40 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 .** or if SQLite
20f50 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f is run in one o
20f60 66 20 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 f reduced mutex
20f70 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 modes .** [SQLIT
20f80 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 E_CONFIG_SINGLET
20f90 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 HREAD] or [SQLIT
20fa0 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 E_CONFIG_MULTITH
20fb0 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 READ].** then th
20fc0 65 72 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e ere is no distin
20fd0 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 ction between pr
20fe0 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 otected and unpr
20ff0 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 otected.** sqlit
21000 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 e3_value objects
21010 20 61 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 and they can be
21020 20 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 used interchang
21030 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c eably. However,
21040 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 .** for maximum
21050 63 6f 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 code portability
21060 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 it is recommend
21070 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 ed that applicat
21080 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 ions.** still ma
21090 6b 65 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 ke the distincti
210a0 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 on between betwe
210b0 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 en protected and
210c0 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 unprotected.**
210d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 sqlite3_value ob
210e0 6a 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 jects even when
210f0 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 not strictly req
21100 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 uired..**.** The
21110 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
21120 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 bjects that are
21130 70 61 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 passed as parame
21140 74 65 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a ters into the.**
21150 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
21160 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d of [application-
21170 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 defined SQL func
21180 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 tions] are prote
21190 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c cted..** The sql
211a0 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 ite3_value objec
211b0 74 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a t returned by.**
211c0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
211d0 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 _value()] is unp
211e0 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 rotected..** Unp
211f0 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 rotected sqlite3
21200 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d _value objects m
21210 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 ay only be used
21220 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 with.** [sqlite3
21230 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d _result_value()]
21240 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 and [sqlite3_bi
21250 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 nd_value()]..**
21260 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c The [sqlite3_val
21270 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 ue_blob | sqlite
21280 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 3_value_type()]
21290 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 family of.** int
212a0 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20 erfaces require
212b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
212c0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 3_value objects.
212d0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
212e0 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 ct Mem sqlite3_v
212f0 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 alue;../*.** CAP
21300 49 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 I3REF: SQL Funct
21310 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 ion Context Obje
21320 63 74 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 ct {H16001} <S20
21330 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 200>.**.** The c
21340 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 ontext in which
21350 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 an SQL function
21360 65 78 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 executes is stor
21370 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 ed in an.** sqli
21380 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 te3_context obje
21390 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 ct. A pointer t
213a0 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e o an sqlite3_con
213b0 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 text object.** i
213c0 73 20 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 s always first p
213d0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 arameter to [app
213e0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
213f0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e SQL functions].
21400 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 .** The applicat
21410 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 ion-defined SQL
21420 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 function impleme
21430 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 ntation will pas
21440 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 s this.** pointe
21450 72 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 r through into c
21460 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 alls to [sqlite3
21470 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 _result_int | sq
21480 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c lite3_result()],
21490 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 .** [sqlite3_agg
214a0 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 regate_context()
214b0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 ], [sqlite3_user
214c0 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 _data()],.** [sq
214d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 lite3_context_db
214e0 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c _handle()], [sql
214f0 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 ite3_get_auxdata
21500 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b ()],.** and/or [
21510 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
21520 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 ata()]..*/.typed
21530 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 ef struct sqlite
21540 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 3_context sqlite
21550 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 3_context;../*.*
21560 2a 20 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 * CAPI3REF: Bind
21570 69 6e 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 ing Values To Pr
21580 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
21590 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 s {H13500} <S703
215a0 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 00>.** KEYWORDS:
215b0 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 {host parameter
215c0 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 } {host paramete
215d0 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 rs} {host parame
215e0 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 ter name}.** KEY
215f0 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 WORDS: {SQL para
21600 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 meter} {SQL para
21610 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 meters} {paramet
21620 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a er binding}.**.*
21630 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 * In the SQL str
21640 69 6e 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 ings input to [s
21650 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 qlite3_prepare_v
21660 32 28 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 2()] and its var
21670 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 iants,.** litera
21680 6c 73 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 ls may be replac
21690 65 64 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 ed by a [paramet
216a0 65 72 5d 20 74 68 61 74 20 6d 61 74 63 68 65 73 er] that matches
216b0 20 6f 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 6e one of followin
216c0 67 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a g.** templates:.
216d0 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
216e0 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f i> ?.** <li> ?
216f0 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 NNN.** <li> :VV
21700 56 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a V.** <li> @VVV.
21710 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a ** <li> $VVV.**
21720 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 </ul>.**.** In
21730 74 68 65 20 74 65 6d 70 6c 61 74 65 73 20 61 62 the templates ab
21740 6f 76 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 65 ove, NNN represe
21750 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c nts an integer l
21760 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 iteral,.** and V
21770 56 56 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e VV represents an
21780 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 64 alphanumeric id
21790 65 6e 74 69 66 65 72 2e 20 20 54 68 65 20 76 61 entifer. The va
217a0 6c 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a lues of these.**
217b0 20 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 parameters (als
217c0 6f 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 o called "host p
217d0 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 arameter names"
217e0 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 or "SQL paramete
217f0 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 rs").** can be s
21800 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c et using the sql
21810 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f ite3_bind_*() ro
21820 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 utines defined h
21830 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 ere..**.** The f
21840 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
21850 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e the sqlite3_bin
21860 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 d_*() routines i
21870 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f s always.** a po
21880 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 inter to the [sq
21890 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 lite3_stmt] obje
218a0 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d ct returned from
218b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 .** [sqlite3_pre
218c0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 pare_v2()] or it
218d0 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a s variants..**.*
218e0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 * The second arg
218f0 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 ument is the ind
21900 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 ex of the SQL pa
21910 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 rameter to be se
21920 74 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f t..** The leftmo
21930 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 st SQL parameter
21940 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
21950 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 1. When the sa
21960 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 me named.** SQL
21970 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 parameter is use
21980 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 d more than once
21990 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 , second and sub
219a0 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 sequent.** occur
219b0 72 65 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 rences have the
219c0 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 same index as th
219d0 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e e first occurren
219e0 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 ce..** The index
219f0 20 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d for named param
21a00 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f eters can be loo
21a10 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 ked up using the
21a20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
21a30 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 d_parameter_inde
21a40 78 28 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 x()] API if desi
21a50 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a red. The index.
21a60 2a 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 ** for "?NNN" pa
21a70 72 61 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 rameters is the
21a80 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a value of NNN..**
21a90 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d The NNN value m
21aa0 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 ust be between 1
21ab0 20 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 and the [sqlite
21ac0 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 3_limit()].** pa
21ad0 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f rameter [SQLITE_
21ae0 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e LIMIT_VARIABLE_N
21af0 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 UMBER] (default
21b00 76 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a value: 999)..**.
21b10 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 ** The third arg
21b20 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c ument is the val
21b30 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 ue to bind to th
21b40 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a e parameter..**.
21b50 2a 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 ** In those rout
21b60 69 6e 65 73 20 74 68 61 74 20 68 61 76 65 20 61 ines that have a
21b70 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 fourth argument
21b80 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 , its value is t
21b90 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
21ba0 62 79 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 bytes in the par
21bb0 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 ameter. To be c
21bc0 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 lear: the value
21bd0 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 is the.** number
21be0 20 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e of <u>bytes</u>
21bf0 20 69 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e in the value, n
21c00 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ot the number of
21c10 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 characters..**
21c20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 If the fourth pa
21c30 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 rameter is negat
21c40 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 ive, the length
21c50 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 of the string is
21c60 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
21c70 66 20 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 f bytes up to th
21c80 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 e first zero ter
21c90 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 minator..**.** T
21ca0 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e he fifth argumen
21cb0 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e t to sqlite3_bin
21cc0 64 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 d_blob(), sqlite
21cd0 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 3_bind_text(), a
21ce0 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 nd.** sqlite3_bi
21cf0 6e 64 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 nd_text16() is a
21d00 20 64 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 destructor used
21d10 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 to dispose of t
21d20 68 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 he BLOB or.** st
21d30 72 69 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 ring after SQLit
21d40 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 e has finished w
21d50 69 74 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 ith it. If the f
21d60 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 ifth argument is
21d70 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 .** the special
21d80 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 value [SQLITE_ST
21d90 41 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 ATIC], then SQLi
21da0 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 te assumes that
21db0 74 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 the.** informati
21dc0 6f 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c on is in static,
21dd0 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 unmanaged space
21de0 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 and does not ne
21df0 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a ed to be freed..
21e00 2a 2a 20 49 66 20 74 68 65 20 66 69 66 74 68 20 ** If the fifth
21e10 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 argument has the
21e20 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 value [SQLITE_T
21e30 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a RANSIENT], then.
21e40 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 ** SQLite makes
21e50 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 its own private
21e60 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 copy of the data
21e70 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 immediately, be
21e80 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 fore.** the sqli
21e90 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 te3_bind_*() rou
21ea0 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a tine returns..**
21eb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
21ec0 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 bind_zeroblob()
21ed0 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 routine binds a
21ee0 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e BLOB of length N
21ef0 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c that.** is fill
21f00 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 ed with zeroes.
21f10 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 A zeroblob uses
21f20 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 a fixed amount
21f30 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 of memory.** (ju
21f40 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f st an integer to
21f50 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 hold its size)
21f60 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e while it is bein
21f70 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 g processed..**
21f80 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e Zeroblobs are in
21f90 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 tended to serve
21fa0 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 as placeholders
21fb0 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a for BLOBs whose.
21fc0 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 ** content is la
21fd0 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e ter written usin
21fe0 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c g.** [sqlite3_bl
21ff0 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d ob_open | increm
22000 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 ental BLOB I/O]
22010 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e routines..** A n
22020 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f egative value fo
22030 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 r the zeroblob r
22040 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f esults in a zero
22050 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a -length BLOB..**
22060 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
22070 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 bind_*() routine
22080 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 s must be called
22090 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 after.** [sqlit
220a0 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d e3_prepare_v2()]
220b0 20 28 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e (and its varian
220c0 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ts) or [sqlite3_
220d0 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 reset()] and.**
220e0 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f before [sqlite3_
220f0 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 step()]..** Bind
22100 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 ings are not cle
22110 61 72 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c ared by the [sql
22120 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f ite3_reset()] ro
22130 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e utine..** Unboun
22140 64 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 d parameters are
22150 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 interpreted as
22160 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 NULL..**.** Thes
22170 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 e routines retur
22180 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e n [SQLITE_OK] on
22190 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 success or an e
221a0 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 rror code if.**
221b0 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 anything goes wr
221c0 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 ong. [SQLITE_RA
221d0 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 NGE] is returned
221e0 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 if the paramete
221f0 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 r.** index is ou
22200 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 t of range. [SQ
22210 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 LITE_NOMEM] is r
22220 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f eturned if mallo
22230 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 c() fails..** [S
22240 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 QLITE_MISUSE] mi
22250 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 ght be returned
22260 69 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 if these routine
22270 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 s are called on
22280 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 a.** virtual mac
22290 68 69 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 hine that is the
222a0 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 wrong state or
222b0 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 which has alread
222c0 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 y been finalized
222d0 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f ..** Detection o
222e0 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 f misuse is unre
222f0 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 liable. Applica
22300 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 tions should not
22310 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 depend.** on SQ
22320 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 LITE_MISUSE retu
22330 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 rns. SQLITE_MIS
22340 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 USE is intended
22350 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a to indicate a.**
22360 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 a logic error i
22370 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f n the applicatio
22380 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 n. Future versi
22390 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 ons of SQLite mi
223a0 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 ght.** panic rat
223b0 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 her than return
223c0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a SQLITE_MISUSE..*
223d0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
223e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
223f0 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c ameter_count()],
22400 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e .** [sqlite3_bin
22410 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 d_parameter_name
22420 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 ()], and [sqlite
22430 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
22440 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a _index()]..**.**
22450 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a Requirements:.*
22460 2a 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 * [H13506] [H135
22470 30 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 09] [H13512] [H1
22480 33 35 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 3515] [H13518] [
22490 48 31 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d H13521] [H13524]
224a0 20 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 [H13527].** [H1
224b0 33 35 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 3530] [H13533] [
224c0 48 31 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d H13536] [H13539]
224d0 20 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 [H13542] [H1354
224e0 35 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 5] [H13548] [H13
224f0 35 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 551].**.*/.SQLIT
22500 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
22510 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 3_bind_blob(sqli
22520 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
22530 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 const void*, int
22540 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 n, void(*)(void
22550 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
22560 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
22570 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f _double(sqlite3_
22580 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 stmt*, int, doub
22590 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 le);.SQLITE_API
225a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
225b0 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d _int(sqlite3_stm
225c0 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 t*, int, int);.S
225d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
225e0 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 lite3_bind_int64
225f0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
22600 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 int, sqlite3_int
22610 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64);.SQLITE_API
22620 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
22630 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 _null(sqlite3_st
22640 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 mt*, int);.SQLIT
22650 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
22660 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 3_bind_text(sqli
22670 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 te3_stmt*, int,
22680 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
22690 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 n, void(*)(void
226a0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 *));.SQLITE_API
226b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
226c0 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f _text16(sqlite3_
226d0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 stmt*, int, cons
226e0 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f t void*, int, vo
226f0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
22700 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
22710 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 lite3_bind_value
22720 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
22730 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 int, const sqlit
22740 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.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 7a 65 72 6f 62 6c 6f 62 e3_bind_zeroblob
22770 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 (sqlite3_stmt*,
22780 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a int, int n);../*
22790 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 .** CAPI3REF: Nu
227a0 6d 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 mber Of SQL Para
227b0 6d 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 meters {H13600}
227c0 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S70300>.**.** T
227d0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 his routine can
227e0 62 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 be used to find
227f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 the number of [S
22800 51 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a QL parameters].*
22810 2a 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 * in a [prepared
22820 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 statement]. SQ
22830 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 L parameters are
22840 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a tokens of the.*
22850 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e * form "?", "?NN
22860 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 N", ":AAA", "$AA
22870 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 A", or "@AAA" th
22880 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 at serve as.** p
22890 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 laceholders for
228a0 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 values that are
228b0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c [sqlite3_bind_bl
228c0 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 ob | bound].** t
228d0 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 o the parameters
228e0 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 at a later time
228f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
22900 74 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 tine actually re
22910 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 turns the index
22920 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 of the largest (
22930 72 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 rightmost).** pa
22940 72 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c rameter. For all
22950 20 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e forms except ?N
22960 4e 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f NN, this will co
22970 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a rrespond to the.
22980 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 ** number of uni
22990 71 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 que parameters.
229a0 20 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f If parameters o
229b0 66 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 f the ?NNN are u
229c0 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 sed,.** there ma
229d0 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 y be gaps in the
229e0 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 list..**.** See
229f0 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f also: [sqlite3_
22a00 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 bind_blob|sqlite
22a10 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 3_bind()],.** [s
22a20 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
22a30 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 meter_name()], a
22a40 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 nd.** [sqlite3_b
22a50 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e ind_parameter_in
22a60 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 dex()]..**.** Re
22a70 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
22a80 48 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 H13601].*/.SQLIT
22a90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
22aa0 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
22ab0 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 _count(sqlite3_s
22ac0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tmt*);../*.** CA
22ad0 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 PI3REF: Name Of
22ae0 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 A Host Parameter
22af0 20 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 {H13620} <S7030
22b00 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 0>.**.** This ro
22b10 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 utine returns a
22b20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e pointer to the n
22b30 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a ame of the n-th.
22b40 2a 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 ** [SQL paramete
22b50 72 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 r] in a [prepare
22b60 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a d statement]..**
22b70 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 SQL parameters
22b80 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e of the form "?NN
22b90 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 N" or ":AAA" or
22ba0 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 "@AAA" or "$AAA"
22bb0 0a 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 .** have a name
22bc0 77 68 69 63 68 20 69 73 20 74 68 65 20 73 74 72 which is the str
22bd0 69 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a ing "?NNN" or ":
22be0 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f AAA" or "@AAA" o
22bf0 72 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 r "$AAA".** resp
22c00 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 ectively..** In
22c10 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 other words, the
22c20 20 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 initial ":" or
22c30 22 24 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f "$" or "@" or "?
22c40 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 ".** is included
22c50 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
22c60 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 name..** Paramet
22c70 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ers of the form
22c80 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f "?" without a fo
22c90 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 llowing integer
22ca0 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 have no name.**
22cb0 61 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 and are also ref
22cc0 65 72 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f erred to as "ano
22cd0 6e 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 nymous parameter
22ce0 73 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 s"..**.** The fi
22cf0 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 rst host paramet
22d00 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 er has an index
22d10 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a of 1, not 0..**.
22d20 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 ** If the value
22d30 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 n is out of rang
22d40 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 e or if the n-th
22d50 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a parameter is.**
22d60 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 nameless, then
22d70 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
22d80 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 . The returned
22d90 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 string is.** alw
22da0 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 ays in UTF-8 enc
22db0 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 oding even if th
22dc0 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 e named paramete
22dd0 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 r was.** origina
22de0 6c 6c 79 20 73 70 65 63 69 66 69 65 64 20 61 73 lly specified as
22df0 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 UTF-16 in [sqli
22e00 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d te3_prepare16()]
22e10 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f or.** [sqlite3_
22e20 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e prepare16_v2()].
22e30 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
22e40 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
22e50 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 lob|sqlite3_bind
22e60 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ()],.** [sqlite3
22e70 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f _bind_parameter_
22e80 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a count()], and.**
22e90 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 [sqlite3_bind_p
22ea0 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 arameter_index()
22eb0 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 ]..**.** Require
22ec0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 ments:.** [H1362
22ed0 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 1].*/.SQLITE_API
22ee0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
22ef0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 ite3_bind_parame
22f00 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 ter_name(sqlite3
22f10 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f _stmt*, int);../
22f20 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 *.** CAPI3REF: I
22f30 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 ndex Of A Parame
22f40 74 65 72 20 57 69 74 68 20 41 20 47 69 76 65 6e ter With A Given
22f50 20 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c Name {H13640} <
22f60 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 S70300>.**.** Re
22f70 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f turn the index o
22f80 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 f an SQL paramet
22f90 65 72 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d er given its nam
22fa0 65 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 e. The.** index
22fb0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 value returned
22fc0 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 is suitable for
22fd0 75 73 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e use as the secon
22fe0 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 d.** parameter t
22ff0 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f o [sqlite3_bind_
23000 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e blob|sqlite3_bin
23010 64 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a d()]. A zero.**
23020 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
23030 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 no matching para
23040 6d 65 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 meter is found.
23050 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a The parameter.*
23060 2a 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 * name must be g
23070 69 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 iven in UTF-8 ev
23080 65 6e 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e en if the origin
23090 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 al statement.**
230a0 77 61 73 20 70 72 65 70 61 72 65 64 20 66 72 6f was prepared fro
230b0 6d 20 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 m UTF-16 text us
230c0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 ing [sqlite3_pre
230d0 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a pare16_v2()]..**
230e0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 .** See also: [s
230f0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 qlite3_bind_blob
23100 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d |sqlite3_bind()]
23110 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 ,.** [sqlite3_bi
23120 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 nd_parameter_cou
23130 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 nt()], and.** [s
23140 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 qlite3_bind_para
23150 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a meter_index()]..
23160 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
23170 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a ts:.** [H13641].
23180 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
23190 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 t sqlite3_bind_p
231a0 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 arameter_index(s
231b0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f qlite3_stmt*, co
231c0 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 nst char *zName)
231d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
231e0 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e F: Reset All Bin
231f0 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 dings On A Prepa
23200 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 red Statement {H
23210 31 33 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 13660} <S70300>.
23220 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 **.** Contrary t
23230 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 o the intuition
23240 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 of many, [sqlite
23250 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 3_reset()] does
23260 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 not reset.** the
23270 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 [sqlite3_bind_b
23280 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 lob | bindings]
23290 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 on a [prepared s
232a0 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 tatement]..** Us
232b0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 e this routine t
232c0 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 o reset all host
232d0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e parameters to N
232e0 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ULL..**.** Requi
232f0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
23300 36 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 661].*/.SQLITE_A
23310 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
23320 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 lear_bindings(sq
23330 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f lite3_stmt*);../
23340 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e *.** CAPI3REF: N
23350 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 umber Of Columns
23360 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 In A Result Set
23370 20 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 {H13710} <S1070
23380 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 0>.**.** Return
23390 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f the number of co
233a0 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 lumns in the res
233b0 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 ult set returned
233c0 20 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 by the.** [prep
233d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e ared statement].
233e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 This routine re
233f0 74 75 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 turns 0 if pStmt
23400 20 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 is an SQL.** st
23410 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 atement that doe
23420 73 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 s not return dat
23430 61 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 a (for example a
23440 6e 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a n [UPDATE])..**.
23450 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a ** Requirements:
23460 0a 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a .** [H13711].*/.
23470 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
23480 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f qlite3_column_co
23490 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
234a0 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a *pStmt);../*.**
234b0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d CAPI3REF: Colum
234c0 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 n Names In A Res
234d0 75 6c 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d ult Set {H13720}
234e0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
234f0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 These routines r
23500 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 eturn the name a
23510 73 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 ssigned to a par
23520 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a ticular column.*
23530 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 * in the result
23540 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 set of a [SELECT
23550 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 ] statement. Th
23560 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e e sqlite3_column
23570 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 _name().** inter
23580 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 face returns a p
23590 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f ointer to a zero
235a0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d -terminated UTF-
235b0 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 8 string.** and
235c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e sqlite3_column_n
235d0 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 ame16() returns
235e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a a pointer to a z
235f0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a ero-terminated.*
23600 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e * UTF-16 string.
23610 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 The first para
23620 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 meter is the [pr
23630 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
23640 5d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d ].** that implem
23650 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 ents the [SELECT
23660 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 ] statement. The
23670 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 second paramete
23680 72 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 r is the.** colu
23690 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 mn number. The
236a0 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 leftmost column
236b0 69 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a is number 0..**.
236c0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 ** The returned
236d0 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 string pointer i
236e0 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 s valid until ei
236f0 74 68 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 ther the [prepar
23700 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a ed statement].**
23710 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 is destroyed by
23720 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 [sqlite3_finali
23730 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 ze()] or until t
23740 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a he next call to.
23750 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
23760 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 n_name() or sqli
23770 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 te3_column_name1
23780 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 6() on the same
23790 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 column..**.** If
237a0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
237b0 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 ) fails during t
237c0 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 he processing of
237d0 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a either routine.
237e0 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 ** (for example
237f0 64 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 during a convers
23800 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 ion from UTF-8 t
23810 6f 20 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 o UTF-16) then a
23820 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 .** NULL pointer
23830 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
23840 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 .** The name of
23850 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 a result column
23860 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 is the value of
23870 74 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 the "AS" clause
23880 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 for.** that colu
23890 6d 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 mn, if there is
238a0 61 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 an AS clause. I
238b0 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 f there is no AS
238c0 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 clause.** then
238d0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
238e0 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 column is unspec
238f0 69 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 ified and may ch
23900 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 ange from.** one
23910 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 release of SQLi
23920 74 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a te to the next..
23930 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
23940 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 ts:.** [H13721]
23950 5b 48 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 [H13723] [H13724
23960 5d 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 ] [H13725] [H137
23970 32 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 26] [H13727].*/.
23980 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
23990 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 char *sqlite3_c
239a0 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 olumn_name(sqlit
239b0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 e3_stmt*, int N)
239c0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
239d0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 st void *sqlite3
239e0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 _column_name16(s
239f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e qlite3_stmt*, in
23a00 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 t N);../*.** CAP
23a10 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 I3REF: Source Of
23a20 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 Data In A Query
23a30 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d Result {H13740}
23a40 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S10700>.**.**
23a50 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 These routines p
23a60 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 rovide a means t
23a70 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 o determine what
23a80 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a column of what.
23a90 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 ** table in whic
23aa0 68 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 h database a res
23ab0 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 ult of a [SELECT
23ac0 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 ] statement come
23ad0 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e s from..** The n
23ae0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 ame of the datab
23af0 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 ase or table or
23b00 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 column can be re
23b10 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 turned as.** eit
23b20 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 her a UTF-8 or U
23b30 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 TF-16 string. T
23b40 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f he _database_ ro
23b50 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a utines return.**
23b60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 the database na
23b70 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 me, the _table_
23b80 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 routines return
23b90 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 the table name,
23ba0 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 and.** the origi
23bb0 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 n_ routines retu
23bc0 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 rn the column na
23bd0 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 me..** The retur
23be0 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 ned string is va
23bf0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 lid until the [p
23c00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
23c10 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a t] is destroyed.
23c20 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 ** using [sqlite
23c30 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 3_finalize()] or
23c40 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 until the same
23c50 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 information is r
23c60 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 equested.** agai
23c70 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 n in a different
23c80 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a encoding..**.**
23c90 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 The names retur
23ca0 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 ned are the orig
23cb0 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 inal un-aliased
23cc0 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 names of the.**
23cd0 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c database, table,
23ce0 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a and column..**.
23cf0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 ** The first arg
23d00 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c ument to the fol
23d10 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 lowing calls is
23d20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 a [prepared stat
23d30 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 ement]..** These
23d40 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 functions retur
23d50 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 n information ab
23d60 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 out the Nth colu
23d70 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a mn returned by.*
23d80 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c * the statement,
23d90 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
23da0 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 second function
23db0 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 argument..**.**
23dc0 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d If the Nth colum
23dd0 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 n returned by th
23de0 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 e statement is a
23df0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a n expression or.
23e00 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 ** subquery and
23e10 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 is not a column
23e20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 value, then all
23e30 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f of these functio
23e40 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c ns return.** NUL
23e50 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e L. These routin
23e60 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 e might also ret
23e70 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 urn NULL if a me
23e80 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
23e90 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e error.** occurs.
23ea0 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 Otherwise, the
23eb0 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d y return the nam
23ec0 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 e of the attache
23ed0 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c d database, tabl
23ee0 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 e.** and column
23ef0 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c that query resul
23f00 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 t column was ext
23f10 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a racted from..**.
23f20 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f ** As with all o
23f30 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 ther SQLite APIs
23f40 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 , those postfixe
23f50 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 d with "16" retu
23f60 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 rn.** UTF-16 enc
23f70 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 oded strings, th
23f80 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e e other function
23f90 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 s return UTF-8.
23fa0 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 {END}.**.** Thes
23fb0 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 e APIs are only
23fc0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 available if the
23fd0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d library was com
23fe0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a piled with the.*
23ff0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 * [SQLITE_ENABLE
24000 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
24010 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 ] C-preprocessor
24020 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e symbol defined.
24030 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a .**.** {A13751}.
24040 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 ** If two or mor
24050 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f e threads call o
24060 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
24070 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 ese routines aga
24080 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a inst the same.**
24090 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
240a0 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 ent and column a
240b0 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 t the same time
240c0 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 then the results
240d0 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 are.** undefine
240e0 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 d..**.** Require
240f0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 ments:.** [H1374
24100 31 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 1] [H13742] [H13
24110 37 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 743] [H13744] [H
24120 31 33 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 13745] [H13746]
24130 5b 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 [H13748].**.** I
24140 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 f two or more th
24150 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f reads call one o
24160 72 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 r more.** [sqlit
24170 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 e3_column_databa
24180 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e se_name | column
24190 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 metadata interf
241a0 61 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 aces].** for the
241b0 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 same [prepared
241c0 73 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 statement] and r
241d0 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 esult column.**
241e0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
241f0 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 then the result
24200 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e s are undefined.
24210 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 .*/.SQLITE_API c
24220 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 onst char *sqlit
24230 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 e3_column_databa
24240 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f se_name(sqlite3_
24250 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 stmt*,int);.SQLI
24260 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 TE_API const voi
24270 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d d *sqlite3_colum
24280 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 n_database_name1
24290 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 6(sqlite3_stmt*,
242a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 int);.SQLITE_API
242b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
242c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c ite3_column_tabl
242d0 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 e_name(sqlite3_s
242e0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 tmt*,int);.SQLIT
242f0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 E_API const void
24300 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e *sqlite3_column
24310 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 _table_name16(sq
24320 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 lite3_stmt*,int)
24330 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e ;.SQLITE_API con
24340 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
24350 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e _column_origin_n
24360 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 ame(sqlite3_stmt
24370 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 *,int);.SQLITE_A
24380 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
24390 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 qlite3_column_or
243a0 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 igin_name16(sqli
243b0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a te3_stmt*,int);.
243c0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
243d0 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 Declared Dataty
243e0 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 pe Of A Query Re
243f0 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 sult {H13760} <S
24400 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 10700>.**.** The
24410 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 first parameter
24420 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 is a [prepared
24430 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 statement]..** I
24440 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 f this statement
24450 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 is a [SELECT] s
24460 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 tatement and the
24470 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 Nth column of t
24480 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 he.** returned r
24490 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 esult set of tha
244a0 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 t [SELECT] is a
244b0 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f table column (no
244c0 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 t an.** expressi
244d0 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 on or subquery)
244e0 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 then the declare
244f0 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 d type of the ta
24500 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 ble.** column is
24510 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 returned. If t
24520 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 he Nth column of
24530 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
24540 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 is an.** express
24550 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c ion or subquery,
24560 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 then a NULL poi
24570 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
24580 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 ..** The returne
24590 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 d string is alwa
245a0 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 ys UTF-8 encoded
245b0 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f . {END}.**.** Fo
245c0 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e r example, given
245d0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
245e0 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 hema:.**.** CREA
245f0 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 TE TABLE t1(c1 V
24600 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 ARIANT);.**.** a
24610 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 nd the following
24620 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 statement to be
24630 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a compiled:.**.**
24640 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 SELECT c1 + 1,
24650 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a c1 FROM t1;.**.*
24660 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * this routine w
24670 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 ould return the
24680 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 string "VARIANT"
24690 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 for the second
246a0 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e result.** column
246b0 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e (i==1), and a N
246c0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 ULL pointer for
246d0 74 68 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 the first result
246e0 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a column (i==0)..
246f0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 **.** SQLite use
24700 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 s dynamic run-ti
24710 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a me typing. So j
24720 75 73 74 20 62 65 63 61 75 73 65 20 61 20 63 6f ust because a co
24730 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 lumn.** is decla
24740 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 red to contain a
24750 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 particular type
24760 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 does not mean t
24770 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 hat the.** data
24780 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 stored in that c
24790 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 olumn is of the
247a0 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 declared type.
247b0 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 SQLite is.** str
247c0 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 ongly typed, but
247d0 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 the typing is d
247e0 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 ynamic not stati
247f0 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 c. Type.** is a
24800 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 ssociated with i
24810 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 ndividual values
24820 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 , not with the c
24830 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 ontainers.** use
24840 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 d to hold those
24850 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 values..**.** Re
24860 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
24870 48 31 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d H13761] [H13762]
24880 20 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c [H13763].*/.SQL
24890 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 ITE_API const ch
248a0 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ar *sqlite3_colu
248b0 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 mn_decltype(sqli
248c0 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a te3_stmt*,int);.
248d0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
248e0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 void *sqlite3_c
248f0 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 olumn_decltype16
24900 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 (sqlite3_stmt*,i
24910 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 nt);../*.** CAPI
24920 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 3REF: Evaluate A
24930 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 n SQL Statement
24940 7b 48 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 {H13200} <S10000
24950 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 >.**.** After a
24960 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
24970 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 ent] has been pr
24980 65 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 epared using eit
24990 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f her.** [sqlite3_
249a0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 prepare_v2()] or
249b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
249c0 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 e16_v2()] or one
249d0 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a of the legacy.*
249e0 2a 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 * interfaces [sq
249f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d lite3_prepare()]
24a00 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 or [sqlite3_pre
24a10 70 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 pare16()], this
24a20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 function.** must
24a30 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f be called one o
24a40 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 r more times to
24a50 65 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 evaluate the sta
24a60 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 tement..**.** Th
24a70 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 e details of the
24a80 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 behavior of the
24a90 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
24aa0 69 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 interface depend
24ab0 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 .** on whether t
24ac0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 he statement was
24ad0 20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 prepared using
24ae0 74 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 the newer "v2" i
24af0 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c nterface.** [sql
24b00 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 ite3_prepare_v2(
24b10 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f )] and [sqlite3_
24b20 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 prepare16_v2()]
24b30 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 or the older leg
24b40 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 acy.** interface
24b50 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 [sqlite3_prepar
24b60 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 e()] and [sqlite
24b70 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 3_prepare16()].
24b80 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a The use of the.
24b90 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 ** new "v2" inte
24ba0 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 rface is recomme
24bb0 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 nded for new app
24bc0 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 lications but th
24bd0 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 e legacy.** inte
24be0 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 rface will conti
24bf0 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 nue to be suppor
24c00 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 ted..**.** In th
24c10 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 e legacy interfa
24c20 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 ce, the return v
24c30 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 alue will be eit
24c40 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 her [SQLITE_BUSY
24c50 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f ],.** [SQLITE_DO
24c60 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 NE], [SQLITE_ROW
24c70 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ], [SQLITE_ERROR
24c80 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 ], or [SQLITE_MI
24c90 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 SUSE]..** With t
24ca0 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 he "v2" interfac
24cb0 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 e, any of the ot
24cc0 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 her [result code
24cd0 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 s] or.** [extend
24ce0 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d ed result codes]
24cf0 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e might be return
24d00 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a ed as well..**.*
24d10 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 * [SQLITE_BUSY]
24d20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 means that the d
24d30 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 atabase engine w
24d40 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 as unable to acq
24d50 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 uire the.** data
24d60 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 base locks it ne
24d70 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f eds to do its jo
24d80 62 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 b. If the state
24d90 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 ment is a [COMMI
24da0 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 T].** or occurs
24db0 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 outside of an ex
24dc0 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 plicit transacti
24dd0 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e on, then you can
24de0 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 retry the.** st
24df0 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 atement. If the
24e00 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f statement is no
24e10 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 t a [COMMIT] and
24e20 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 occurs within a
24e30 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 .** explicit tra
24e40 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f nsaction then yo
24e50 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 u should rollbac
24e60 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f k the transactio
24e70 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 n before.** cont
24e80 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 inuing..**.** [S
24e90 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e QLITE_DONE] mean
24ea0 73 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 s that the state
24eb0 6d 65 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 ment has finishe
24ec0 64 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 d executing.** s
24ed0 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 uccessfully. sq
24ee0 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f lite3_step() sho
24ef0 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 uld not be calle
24f00 64 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 d again on this
24f10 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 virtual.** machi
24f20 6e 65 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 ne without first
24f30 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 calling [sqlite
24f40 33 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 3_reset()] to re
24f50 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a set the virtual.
24f60 2a 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 ** machine back
24f70 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 to its initial s
24f80 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 tate..**.** If t
24f90 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 he SQL statement
24fa0 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 being executed
24fb0 72 65 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 returns any data
24fc0 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 , then [SQLITE_R
24fd0 4f 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e OW].** is return
24fe0 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e ed each time a n
24ff0 65 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 ew row of data i
25000 73 20 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 s ready for proc
25010 65 73 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a essing by the.**
25020 20 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c caller. The val
25030 75 65 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 ues may be acces
25040 73 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 sed using the [c
25050 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e olumn access fun
25060 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 ctions]..** sqli
25070 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 te3_step() is ca
25080 6c 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 lled again to re
25090 74 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 trieve the next
250a0 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a row of data..**.
250b0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 ** [SQLITE_ERROR
250c0 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 ] means that a r
250d0 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 un-time error (s
250e0 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 uch as a constra
250f0 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e int.** violation
25100 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 ) has occurred.
25110 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 sqlite3_step()
25120 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 should not be ca
25130 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a lled again on.**
25140 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e the VM. More in
25150 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 formation may be
25160 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e found by callin
25170 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 g [sqlite3_errms
25180 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 g()]..** With th
25190 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 e legacy interfa
251a0 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 ce, a more speci
251b0 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 fic error code (
251c0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 for example,.**
251d0 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 [SQLITE_INTERRUP
251e0 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 T], [SQLITE_SCHE
251f0 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 MA], [SQLITE_COR
25200 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f RUPT], and so fo
25210 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f rth).** can be o
25220 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 btained by calli
25230 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 ng [sqlite3_rese
25240 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b t()] on the.** [
25250 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
25260 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 nt]. In the "v2
25270 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 " interface,.**
25280 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 the more specifi
25290 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 c error code is
252a0 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c returned directl
252b0 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 y by sqlite3_ste
252c0 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 p()..**.** [SQLI
252d0 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 TE_MISUSE] means
252e0 20 74 68 61 74 20 74 68 65 20 74 68 69 73 20 72 that the this r
252f0 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 outine was calle
25300 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c d inappropriatel
25310 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 y..** Perhaps it
25320 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 was called on a
25330 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 [prepared state
25340 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a ment] that has.*
25350 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b * already been [
25360 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 sqlite3_finalize
25370 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 | finalized] or
25380 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 on one that had
25390 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 .** previously r
253a0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f eturned [SQLITE_
253b0 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 ERROR] or [SQLIT
253c0 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 E_DONE]. Or it
253d0 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 could.** be the
253e0 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 61 case that the sa
253f0 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e me database conn
25400 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 ection is being
25410 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a used by two or.*
25420 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 * more threads a
25430 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e t the same momen
25440 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a t in time..**.**
25450 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 <b>Goofy Interf
25460 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 ace Alert:</b> I
25470 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 n the legacy int
25480 65 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 erface, the sqli
25490 74 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 te3_step().** AP
254a0 49 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 I always returns
254b0 20 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 a generic error
254c0 20 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 code, [SQLITE_E
254d0 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 RROR], following
254e0 20 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 any.** error ot
254f0 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 her than [SQLITE
25500 5f 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 _BUSY] and [SQLI
25510 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 TE_MISUSE]. You
25520 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 must call.** [s
25530 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 qlite3_reset()]
25540 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 or [sqlite3_fina
25550 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 lize()] in order
25560 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 to find one of
25570 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 the.** specific
25580 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 [error codes] th
25590 61 74 20 62 65 74 74 65 72 20 64 65 73 63 72 69 at better descri
255a0 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a bes the error..*
255b0 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 * We admit that
255c0 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 this is a goofy
255d0 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f design. The pro
255e0 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 blem has been fi
255f0 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 xed.** with the
25600 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 "v2" interface.
25610 20 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 If you prepare
25620 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 all of your SQL
25630 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 statements.** us
25640 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 ing either [sqli
25650 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 te3_prepare_v2()
25660 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 ] or [sqlite3_pr
25670 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e epare16_v2()] in
25680 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 stead.** of the
25690 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f legacy [sqlite3_
256a0 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b prepare()] and [
256b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 sqlite3_prepare1
256c0 36 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 6()] interfaces,
256d0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 .** then the mor
256e0 65 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f e specific [erro
256f0 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 r codes] are ret
25700 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a urned directly.*
25710 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 * by sqlite3_ste
25720 70 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 p(). The use of
25730 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 the "v2" interf
25740 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 ace is recommend
25750 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ed..**.** Requir
25760 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 ements:.** [H132
25770 30 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 02] [H15304] [H1
25780 35 33 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 5306] [H15308] [
25790 48 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 H15310].*/.SQLIT
257a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
257b0 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 3_step(sqlite3_s
257c0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tmt*);../*.** CA
257d0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f PI3REF: Number o
257e0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 f columns in a r
257f0 65 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 esult set {H1377
25800 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 0} <S10700>.**.*
25810 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 * Returns the nu
25820 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 mber of values i
25830 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f n the current ro
25840 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 w of the result
25850 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 set..**.** Requi
25860 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 rements:.** [H13
25870 37 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 771] [H13772].*/
25880 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
25890 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 sqlite3_data_cou
258a0 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 nt(sqlite3_stmt
258b0 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 *pStmt);../*.**
258c0 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d CAPI3REF: Fundam
258d0 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 ental Datatypes
258e0 7b 48 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 {H10265} <S10110
258f0 3e 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 ><S10120>.** KEY
25900 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 WORDS: SQLITE_TE
25910 58 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 XT.**.** {H10266
25920 7d 20 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e } Every value in
25930 20 53 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 SQLite has one
25940 6f 66 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e of five fundamen
25950 74 61 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a tal datatypes:.*
25960 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 *.** <ul>.** <li
25970 3e 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 > 64-bit signed
25980 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 integer.** <li>
25990 36 34 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 64-bit IEEE floa
259a0 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 ting point numbe
259b0 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 r.** <li> string
259c0 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a .** <li> BLOB.**
259d0 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f <li> NULL.** </
259e0 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 ul> {END}.**.**
259f0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 These constants
25a00 61 72 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 are codes for ea
25a10 63 68 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 ch of those type
25a20 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 s..**.** Note th
25a30 61 74 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 at the SQLITE_TE
25a40 58 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 XT constant was
25a50 61 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c also used in SQL
25a60 69 74 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a ite version 2.**
25a70 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c for a completel
25a80 79 20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e y different mean
25a90 69 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 ing. Software t
25aa0 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 hat links agains
25ab0 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 t both.** SQLite
25ac0 20 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 version 2 and S
25ad0 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 QLite version 3
25ae0 73 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 should use SQLIT
25af0 45 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 E3_TEXT, not.**
25b00 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a SQLITE_TEXT..*/.
25b10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 #define SQLITE_I
25b20 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e NTEGER 1.#defin
25b30 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 e SQLITE_FLOAT
25b40 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
25b50 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 TE_BLOB 4.#d
25b60 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c efine SQLITE_NUL
25b70 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 L 5.#ifdef S
25b80 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 QLITE_TEXT.# und
25b90 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 ef SQLITE_TEXT.#
25ba0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
25bb0 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a LITE_TEXT 3.
25bc0 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 #endif.#define S
25bd0 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 QLITE3_TEXT
25be0 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 3../*.** CAPI3RE
25bf0 46 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 F: Result Values
25c00 20 46 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 From A Query {H
25c10 31 33 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 13800} <S10700>.
25c20 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f ** KEYWORDS: {co
25c30 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 lumn access func
25c40 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 tions}.**.** The
25c50 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d se routines form
25c60 20 74 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 the "result set
25c70 20 71 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 query" interfac
25c80 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 e..**.** These r
25c90 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 outines return i
25ca0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
25cb0 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e a single column
25cc0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a of the current.
25cd0 2a 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 ** result row of
25ce0 20 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 a query. In ev
25cf0 65 72 79 20 63 61 73 65 20 74 68 65 20 66 69 72 ery case the fir
25d00 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 st argument is a
25d10 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 pointer.** to t
25d20 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 he [prepared sta
25d30 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 tement] that is
25d40 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 being evaluated
25d50 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 (the [sqlite3_st
25d60 6d 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 mt*].** that was
25d70 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b returned from [
25d80 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
25d90 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 v2()] or one of
25da0 69 74 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a its variants).**
25db0 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 and the second
25dc0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
25dd0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c index of the col
25de0 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e umn for which in
25df0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f formation.** sho
25e00 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e uld be returned.
25e10 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 The leftmost c
25e20 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 olumn of the res
25e30 75 6c 74 20 73 65 74 20 68 61 73 20 74 68 65 20 ult set has the
25e40 69 6e 64 65 78 20 30 2e 0a 2a 2a 20 54 68 65 20 index 0..** The
25e50 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e number of column
25e60 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
25e70 63 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 can be determine
25e80 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 d using.** [sqli
25e90 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 te3_column_count
25ea0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ()]..**.** If th
25eb0 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
25ec0 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 does not current
25ed0 6c 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 ly point to a va
25ee0 6c 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 lid row, or if t
25ef0 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 he.** column ind
25f00 65 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e ex is out of ran
25f10 67 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 ge, the result i
25f20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 s undefined..**
25f30 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d These routines m
25f40 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 ay only be calle
25f50 64 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 d when the most
25f60 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a recent call to.*
25f70 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 * [sqlite3_step(
25f80 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 )] has returned
25f90 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 [SQLITE_ROW] and
25fa0 20 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c neither.** [sql
25fb0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f ite3_reset()] no
25fc0 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c r [sqlite3_final
25fd0 69 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e ize()] have been
25fe0 20 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 called subseque
25ff0 6e 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 ntly..** If any
26000 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 of these routine
26010 73 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 s are called aft
26020 65 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 er [sqlite3_rese
26030 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 t()] or.** [sqli
26040 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 te3_finalize()]
26050 6f 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 or after [sqlite
26060 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 3_step()] has re
26070 74 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 turned.** someth
26080 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b ing other than [
26090 53 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 SQLITE_ROW], the
260a0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 results are und
260b0 65 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 efined..** If [s
260c0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f qlite3_step()] o
260d0 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 r [sqlite3_reset
260e0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f ()] or [sqlite3_
260f0 66 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 finalize()].** a
26100 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 re called from a
26110 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 different threa
26120 64 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 d while any of t
26130 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a hese routines.**
26140 20 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 are pending, th
26150 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 en the results a
26160 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a re undefined..**
26170 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
26180 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f column_type() ro
26190 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 utine returns th
261a0 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 e.** [SQLITE_INT
261b0 45 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 EGER | datatype
261c0 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e code] for the in
261d0 69 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a itial data type.
261e0 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 ** of the result
261f0 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 column. The re
26200 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 turned value is
26210 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 one of [SQLITE_I
26220 4e 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c NTEGER],.** [SQL
26230 49 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c ITE_FLOAT], [SQL
26240 49 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 ITE_TEXT], [SQLI
26250 54 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 TE_BLOB], or [SQ
26260 4c 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 LITE_NULL]. The
26270 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e value.** return
26280 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
26290 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f lumn_type() is o
262a0 6e 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 nly meaningful i
262b0 66 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e f no type.** con
262c0 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 versions have oc
262d0 63 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69 curred as descri
262e0 62 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 bed below. Afte
262f0 72 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 r a type convers
26300 69 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ion,.** the valu
26310 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
26320 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 lite3_column_typ
26330 65 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 e() is undefined
26340 2e 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 . Future.** ver
26350 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 sions of SQLite
26360 6d 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 may change the b
26370 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 ehavior of sqlit
26380 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 e3_column_type()
26390 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 .** following a
263a0 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e type conversion.
263b0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 .**.** If the re
263c0 73 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f sult is a BLOB o
263d0 72 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 r UTF-8 string t
263e0 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f hen the sqlite3_
263f0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a column_bytes().*
26400 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e * routine return
26410 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
26420 62 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c bytes in that BL
26430 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a OB or string..**
26440 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 If the result i
26450 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e s a UTF-16 strin
26460 67 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f g, then sqlite3_
26470 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 column_bytes() c
26480 6f 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 onverts.** the s
26490 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 tring to UTF-8 a
264a0 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 nd then returns
264b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
264c0 74 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 tes..** If the r
264d0 65 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 esult is a numer
264e0 69 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 ic value then sq
264f0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 lite3_column_byt
26500 65 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 es() uses.** [sq
26510 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
26520 5d 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 ] to convert tha
26530 74 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 t value to a UTF
26540 2d 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 -8 string and re
26550 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d turns.** the num
26560 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 ber of bytes in
26570 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 that string..**
26580 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e The value return
26590 65 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c ed does not incl
265a0 75 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 ude the zero ter
265b0 6d 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 minator at the e
265c0 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 nd.** of the str
265d0 69 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 ing. For clarit
265e0 79 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 y: the value ret
265f0 75 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d urned is the num
26600 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 ber of.** bytes
26610 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e in the string, n
26620 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ot the number of
26630 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a characters..**.
26640 2a 2a 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 ** Strings retur
26650 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 ned by sqlite3_c
26660 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 olumn_text() and
26670 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
26680 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 text16(),.** eve
26690 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c n empty strings,
266a0 20 61 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f are always zero
266b0 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 terminated. Th
266c0 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 e return.** valu
266d0 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 e from sqlite3_c
266e0 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 olumn_blob() for
266f0 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 a zero-length B
26700 4c 4f 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 LOB is an arbitr
26710 61 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 ary.** pointer,
26720 70 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 possibly even a
26730 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
26740 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
26750 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 column_bytes16()
26760 20 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 routine is simi
26770 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 lar to sqlite3_c
26780 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a olumn_bytes().**
26790 20 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 but leaves the
267a0 72 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 result in UTF-16
267b0 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 in native byte
267c0 6f 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 order instead of
267d0 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a UTF-8..** The z
267e0 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 ero terminator i
267f0 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 s not included i
26800 6e 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a n this count..**
26810 0a 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 .** The object r
26820 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 eturned by [sqli
26830 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 te3_column_value
26840 28 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e ()] is an.** [un
26850 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 protected sqlite
26860 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 3_value] object.
26870 20 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 An unprotected
26880 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f sqlite3_value o
26890 62 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c bject.** may onl
268a0 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20 5b y be used with [
268b0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c sqlite3_bind_val
268c0 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 ue()] and [sqlit
268d0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 e3_result_value(
268e0 29 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 )]..** If the [u
268f0 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 nprotected sqlit
26900 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 e3_value] object
26910 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 returned by.**
26920 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f [sqlite3_column_
26930 76 61 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 value()] is used
26940 20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 in any other wa
26950 79 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c y, including cal
26960 6c 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 ls.** to routine
26970 73 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f s like [sqlite3_
26980 76 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 value_int()], [s
26990 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 qlite3_value_tex
269a0 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c t()],.** or [sql
269b0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
269c0 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 ()], then the be
269d0 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 havior is undefi
269e0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 ned..**.** These
269f0 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 routines attemp
26a00 74 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 t to convert the
26a10 20 76 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 value where app
26a20 72 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a ropriate. For.*
26a30 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 * example, if th
26a40 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 e internal repre
26a50 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f sentation is FLO
26a60 41 54 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 AT and a text re
26a70 73 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 sult.** is reque
26a80 73 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 sted, [sqlite3_s
26a90 6e 70 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 nprintf()] is us
26aa0 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f ed internally to
26ab0 20 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 perform the.**
26ac0 63 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d conversion autom
26ad0 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 atically. The f
26ae0 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 ollowing table d
26af0 65 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 etails the conve
26b00 72 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 rsions.** that a
26b10 72 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a re applied:.**.*
26b20 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a * <blockquote>.*
26b30 2a 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d * <table border=
26b40 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e "1">.** <tr><th>
26b50 20 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 Internal<br>Typ
26b60 65 20 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 e <th> Requested
26b70 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 <br>Type <th> C
26b80 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 onversion.**.**
26b90 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 <tr><td> NULL
26ba0 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 <td> INTEGER
26bb0 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 <td> Result is
26bc0 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 0.** <tr><td> N
26bd0 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f ULL <td> FLO
26be0 41 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c AT <td> Resul
26bf0 74 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e t is 0.0.** <tr>
26c00 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 <td> NULL <t
26c10 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 d> TEXT <td
26c20 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c > Result is NULL
26c30 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e pointer.** <tr>
26c40 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 <td> NULL <t
26c50 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 d> BLOB <td
26c60 3e 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c > Result is NULL
26c70 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e pointer.** <tr>
26c80 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 <td> INTEGER <t
26c90 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 d> FLOAT <td
26ca0 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 > Convert from i
26cb0 6e 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a nteger to float.
26cc0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 ** <tr><td> INTE
26cd0 47 45 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 GER <td> TEXT
26ce0 20 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 <td> ASCII r
26cf0 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 endering of the
26d00 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c integer.** <tr><
26d10 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 td> INTEGER <td
26d20 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e > BLOB <td>
26d30 20 53 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 Same as INTEGER
26d40 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 ->TEXT.** <tr><t
26d50 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e d> FLOAT <td>
26d60 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 INTEGER <td>
26d70 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f Convert from flo
26d80 61 74 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a at to integer.**
26d90 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 <tr><td> FLOAT
26da0 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 <td> TEXT
26db0 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e <td> ASCII ren
26dc0 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c dering of the fl
26dd0 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 oat.** <tr><td>
26de0 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 FLOAT <td>
26df0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d BLOB <td> Sam
26e00 65 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 e as FLOAT->TEXT
26e10 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 .** <tr><td> TE
26e20 58 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 XT <td> INTEG
26e30 45 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 ER <td> Use at
26e40 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e oi().** <tr><td>
26e50 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 TEXT <td>
26e60 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 FLOAT <td> Us
26e70 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e e atof().** <tr>
26e80 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 <td> TEXT <t
26e90 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 d> BLOB <td
26ea0 3e 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c > No change.** <
26eb0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 tr><td> BLOB
26ec0 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 <td> INTEGER
26ed0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 <td> Convert to
26ee0 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 TEXT then use at
26ef0 6f 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e oi().** <tr><td>
26f00 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 BLOB <td>
26f10 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f FLOAT <td> Co
26f20 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 nvert to TEXT th
26f30 65 6e 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a en use atof().**
26f40 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 <tr><td> BLOB
26f50 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 <td> TEXT
26f60 20 20 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 <td> Add a zer
26f70 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 o terminator if
26f80 6e 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c needed.** </tabl
26f90 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f e>.** </blockquo
26fa0 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 te>.**.** The ta
26fb0 62 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 ble above makes
26fc0 72 65 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 reference to sta
26fd0 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 ndard C library
26fe0 66 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 functions atoi()
26ff0 0a 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 .** and atof().
27000 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 SQLite does not
27010 20 72 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 really use thes
27020 65 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 e functions. It
27030 20 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 has its.** own
27040 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 equivalent inter
27050 6e 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 nal routines. T
27060 68 65 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 he atoi() and at
27070 6f 66 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a of() names are.*
27080 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 * used in the ta
27090 62 6c 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 ble for brevity
270a0 61 6e 64 20 62 65 63 61 75 73 65 20 74 68 65 79 and because they
270b0 20 61 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f are familiar to
270c0 20 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 most.** C progr
270d0 61 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f ammers..**.** No
270e0 74 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 te that when typ
270f0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 e conversions oc
27100 63 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 cur, pointers re
27110 74 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a turned by prior.
27120 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 ** calls to sqli
27130 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 te3_column_blob(
27140 29 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ), sqlite3_colum
27150 6e 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 n_text(), and/or
27160 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 .** sqlite3_colu
27170 6d 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 mn_text16() may
27180 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a be invalidated..
27190 2a 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 ** Type conversi
271a0 6f 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 ons and pointer
271b0 69 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 invalidations mi
271c0 67 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 ght occur.** in
271d0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 the following ca
271e0 73 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a ses:.**.** <ul>.
271f0 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 ** <li> The init
27200 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 ial content is a
27210 20 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 BLOB and sqlite
27220 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
27230 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 or.** sqlit
27240 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
27250 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 () is called. A
27260 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 zero-terminator
27270 20 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e might.** n
27280 65 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 eed to be added
27290 74 6f 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f to the string.</
272a0 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 li>.** <li> The
272b0 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 initial content
272c0 69 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e is UTF-8 text an
272d0 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e d sqlite3_column
272e0 5f 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a _bytes16() or.**
272f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f sqlite3_co
27300 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 lumn_text16() is
27310 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f called. The co
27320 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f ntent must be co
27330 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 nverted.**
27340 74 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a to UTF-16.</li>.
27350 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 ** <li> The init
27360 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 ial content is U
27370 54 46 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 TF-16 text and s
27380 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
27390 74 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 tes() or.**
273a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
273b0 74 65 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 text() is called
273c0 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d . The content m
273d0 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 ust be converted
273e0 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d .** to UTF-
273f0 38 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 8.</li>.** </ul>
27400 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f .**.** Conversio
27410 6e 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 ns between UTF-1
27420 36 62 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 6be and UTF-16le
27430 20 61 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 are always done
27440 20 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f in place and do
27450 0a 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 .** not invalida
27460 74 65 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 te a prior point
27470 65 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f er, though of co
27480 75 72 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 urse the content
27490 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a of the buffer.*
274a0 2a 20 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 * that the prior
274b0 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 pointer points
274c0 74 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 to will have bee
274d0 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 n modified. Oth
274e0 65 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 er kinds.** of c
274f0 6f 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f onversion are do
27500 6e 65 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e ne in place when
27510 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c it is possible,
27520 20 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 but sometimes t
27530 68 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 hey.** are not p
27540 6f 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 ossible and in t
27550 68 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 hose cases prior
27560 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e pointers are in
27570 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a validated..**.**
27580 20 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 The safest and
27590 65 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d easiest to remem
275a0 62 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f ber policy is to
275b0 20 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f invoke these ro
275c0 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 utines.** in one
275d0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
275e0 67 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 g ways:.**.** <u
275f0 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 l>.** <li>sqlit
27600 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
27610 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c followed by sql
27620 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 ite3_column_byte
27630 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 s()</li>.** <li
27640 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f >sqlite3_column_
27650 62 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 blob() followed
27660 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d by sqlite3_colum
27670 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a n_bytes()</li>.*
27680 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 * <li>sqlite3_c
27690 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 olumn_text16() f
276a0 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 ollowed by sqlit
276b0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 e3_column_bytes1
276c0 36 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 6()</li>.** </ul
276d0 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 >.**.** In other
276e0 20 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 words, you shou
276f0 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f ld call sqlite3_
27700 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a column_text(),.*
27710 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e * sqlite3_column
27720 5f 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 _blob(), or sqli
27730 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 te3_column_text1
27740 36 28 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 6() first to for
27750 63 65 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a ce the result.**
27760 20 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 into the desire
27770 64 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 d format, then i
27780 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f nvoke sqlite3_co
27790 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a lumn_bytes() or.
277a0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d ** sqlite3_colum
277b0 6e 5f 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 n_bytes16() to f
277c0 69 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 ind the size of
277d0 74 68 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 the result. Do
277e0 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a not mix calls.**
277f0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
27800 6d 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c mn_text() or sql
27810 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 ite3_column_blob
27820 28 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f () with calls to
27830 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 .** sqlite3_colu
27840 6d 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e mn_bytes16(), an
27850 64 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c d do not mix cal
27860 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ls to sqlite3_co
27870 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a lumn_text16().**
27880 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 with calls to s
27890 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 qlite3_column_by
278a0 74 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 tes()..**.** The
278b0 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e pointers return
278c0 65 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 ed are valid unt
278d0 69 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 il a type conver
278e0 73 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a sion occurs as.*
278f0 2a 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 * described abov
27900 65 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c e, or until [sql
27910 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 ite3_step()] or
27920 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 [sqlite3_reset()
27930 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 ] or.** [sqlite3
27940 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 _finalize()] is
27950 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d called. The mem
27960 6f 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 ory space used t
27970 6f 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a o hold strings.*
27980 2a 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 * and BLOBs is f
27990 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c reed automatical
279a0 6c 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f ly. Do <b>not</
279b0 62 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e b> pass the poin
279c0 74 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a ters returned.**
279d0 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e [sqlite3_column
279e0 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 _blob()], [sqlit
279f0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 e3_column_text()
27a00 5d 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 ], etc. into.**
27a10 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d [sqlite3_free()]
27a20 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d ..**.** If a mem
27a30 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 ory allocation e
27a40 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 rror occurs duri
27a50 6e 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f ng the evaluatio
27a60 6e 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 n of any.** of t
27a70 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 hese routines, a
27a80 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 default value i
27a90 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 s returned. The
27aa0 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a default value.*
27ab0 2a 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 * is either the
27ac0 69 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 integer 0, the f
27ad0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 loating point nu
27ae0 6d 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e mber 0.0, or a N
27af0 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 ULL.** pointer.
27b00 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c Subsequent call
27b10 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 s to [sqlite3_er
27b20 72 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 rcode()] will re
27b30 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f turn.** [SQLITE_
27b40 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 NOMEM]..**.** Re
27b50 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
27b60 48 31 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d H13803] [H13806]
27b70 20 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 [H13809] [H1381
27b80 32 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 2] [H13815] [H13
27b90 38 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 818] [H13821] [H
27ba0 31 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 13824].** [H1382
27bb0 37 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 7] [H13830].*/.S
27bc0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 QLITE_API const
27bd0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f void *sqlite3_co
27be0 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 lumn_blob(sqlite
27bf0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
27c00 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
27c10 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
27c20 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f n_bytes(sqlite3_
27c30 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27c40 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
27c50 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f sqlite3_column_
27c60 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f bytes16(sqlite3_
27c70 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 stmt*, int iCol)
27c80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 ;.SQLITE_API dou
27c90 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ble sqlite3_colu
27ca0 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 mn_double(sqlite
27cb0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 3_stmt*, int iCo
27cc0 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 l);.SQLITE_API i
27cd0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d nt sqlite3_colum
27ce0 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 n_int(sqlite3_st
27cf0 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a mt*, int iCol);.
27d00 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
27d10 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e3_int64 sqlite3
27d20 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 _column_int64(sq
27d30 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
27d40 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 iCol);.SQLITE_A
27d50 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 PI const unsigne
27d60 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f d char *sqlite3_
27d70 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 column_text(sqli
27d80 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 te3_stmt*, int i
27d90 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 Col);.SQLITE_API
27da0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c const void *sql
27db0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
27dc0 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 16(sqlite3_stmt*
27dd0 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c , int iCol);.SQL
27de0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
27df0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 te3_column_type(
27e00 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 sqlite3_stmt*, i
27e10 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 nt iCol);.SQLITE
27e20 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c _API sqlite3_val
27e30 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ue *sqlite3_colu
27e40 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 mn_value(sqlite3
27e50 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c _stmt*, int iCol
27e60 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
27e70 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 EF: Destroy A Pr
27e80 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 epared Statement
27e90 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d Object {H13300}
27ea0 20 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 <S70300><S30100
27eb0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
27ec0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 te3_finalize() f
27ed0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
27ee0 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 d to delete a [p
27ef0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
27f00 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 t]..** If the st
27f10 61 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 atement was exec
27f20 75 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c uted successfull
27f30 79 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 y or not execute
27f40 64 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a d at all, then.*
27f50 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
27f60 65 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 eturned. If exec
27f70 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 ution of the sta
27f80 74 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 tement failed th
27f90 65 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 en an.** [error
27fa0 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 code] or [extend
27fb0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 ed error code] i
27fc0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a s returned..**.*
27fd0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 * This routine c
27fe0 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 an be called at
27ff0 61 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 any point during
28000 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f the execution o
28010 66 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 f the.** [prepar
28020 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 ed statement].
28030 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d If the virtual m
28040 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a achine has not.*
28050 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 * completed exec
28060 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 ution when this
28070 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
28080 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a d, that is like.
28090 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 ** encountering
280a0 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b an error or an [
280b0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 sqlite3_interrup
280c0 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a t | interrupt]..
280d0 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 ** Incomplete up
280e0 64 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c dates may be rol
280f0 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 led back and tra
28100 6e 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c nsactions cancel
28110 65 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 ed,.** depending
28120 20 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 on the circumst
28130 61 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a ances, and the.*
28140 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 * [error code] r
28150 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 eturned will be
28160 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a [SQLITE_ABORT]..
28170 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
28180 74 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 ts:.** [H11302]
28190 5b 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 [H11304].*/.SQLI
281a0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
281b0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 e3_finalize(sqli
281c0 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 te3_stmt *pStmt)
281d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
281e0 46 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 F: Reset A Prepa
281f0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 red Statement Ob
28200 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 ject {H13330} <S
28210 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 70300>.**.** The
28220 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 sqlite3_reset()
28230 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
28240 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b led to reset a [
28250 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
28260 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 nt].** object ba
28270 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 ck to its initia
28280 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 l state, ready t
28290 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 o be re-executed
282a0 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 ..** Any SQL sta
282b0 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 tement variables
282c0 20 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 that had values
282d0 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 bound to them u
282e0 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c sing.** the [sql
282f0 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c ite3_bind_blob |
28300 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 sqlite3_bind_*(
28310 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 ) API] retain th
28320 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 eir values..** U
28330 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 se [sqlite3_clea
28340 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f r_bindings()] to
28350 20 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 reset the bindi
28360 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 ngs..**.** {H113
28370 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 32} The [sqlite3
28380 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 _reset(S)] inter
28390 66 61 63 65 20 72 65 73 65 74 73 20 74 68 65 20 face resets the
283a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
283b0 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 ent] S.**
283c0 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 back to the b
283d0 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 eginning of its
283e0 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b program..**.** {
283f0 48 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d H11334} If the m
28400 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 ost recent call
28410 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 to [sqlite3_step
28420 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 (S)] for the.**
28430 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 [prepar
28440 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 ed statement] S
28450 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 returned [SQLITE
28460 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 _ROW] or [SQLITE
28470 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 _DONE],.**
28480 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 or if [sqlit
28490 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 e3_step(S)] has
284a0 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 never before bee
284b0 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a n called on S,.*
284c0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 * then
284d0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 [sqlite3_reset(S
284e0 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 )] returns [SQLI
284f0 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 TE_OK]..**.** {H
28500 31 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 11336} If the mo
28510 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 st recent call t
28520 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 o [sqlite3_step(
28530 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 S)] for the.**
28540 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 [prepare
28550 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 d statement] S i
28560 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f ndicated an erro
28570 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 r, then.**
28580 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 [sqlite3_res
28590 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 et(S)] returns a
285a0 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 n appropriate [e
285b0 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a rror code]..**.*
285c0 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b * {H11338} The [
285d0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 sqlite3_reset(S)
285e0 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 ] interface does
285f0 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 not change the
28600 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 values.**
28610 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 of any [sqlit
28620 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e e3_bind_blob|bin
28630 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 dings] on the [p
28640 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
28650 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f t] S..*/.SQLITE_
28660 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
28670 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 reset(sqlite3_st
28680 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a mt *pStmt);../*.
28690 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 ** CAPI3REF: Cre
286a0 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 ate Or Redefine
286b0 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 SQL Functions {H
286c0 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 16100} <S20200>.
286d0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 ** KEYWORDS: {fu
286e0 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 nction creation
286f0 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 routines}.** KEY
28700 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 WORDS: {applicat
28710 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 ion-defined SQL
28720 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 function}.** KEY
28730 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 WORDS: {applicat
28740 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 ion-defined SQL
28750 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a functions}.**.**
28760 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 These two funct
28770 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 ions (collective
28780 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e ly known as "fun
28790 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 ction creation r
287a0 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 outines").** are
287b0 20 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c used to add SQL
287c0 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 functions or ag
287d0 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 gregates or to r
287e0 65 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 edefine the beha
287f0 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 vior.** of exist
28800 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e ing SQL function
28810 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e s or aggregates.
28820 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 The only diffe
28830 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 rence between th
28840 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 e.** two is that
28850 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
28860 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 meter, the name
28870 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 of the (scalar)
28880 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 function or.** a
28890 67 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 ggregate, is enc
288a0 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f oded in UTF-8 fo
288b0 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 r sqlite3_create
288c0 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 _function() and
288d0 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 UTF-16.** for sq
288e0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
288f0 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a ction16()..**.**
28900 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d The first param
28910 65 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 eter is the [dat
28920 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
28930 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 ] to which the S
28940 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 QL.** function i
28950 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 s to be added.
28960 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 If a single prog
28970 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 ram uses more th
28980 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a an one database.
28990 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e ** connection in
289a0 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 ternally, then S
289b0 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 QL functions mus
289c0 74 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 t be added indiv
289d0 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 idually to.** ea
289e0 63 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ch database conn
289f0 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 ection..**.** Th
28a00 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
28a10 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f er is the name o
28a20 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 f the SQL functi
28a30 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 on to be created
28a40 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 or.** redefined
28a50 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 . The length of
28a60 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d the name is lim
28a70 69 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 ited to 255 byte
28a80 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a s, exclusive of.
28a90 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d ** the zero-term
28aa0 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 inator. Note th
28ab0 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 at the name leng
28ac0 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 th limit is in b
28ad0 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 ytes, not.** cha
28ae0 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 racters. Any at
28af0 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 tempt to create
28b00 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 a function with
28b10 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a a longer name.**
28b20 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 will result in
28b30 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 [SQLITE_ERROR] b
28b40 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a eing returned..*
28b50 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 *.** The third p
28b60 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a arameter (nArg).
28b70 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 ** is the number
28b80 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 of arguments th
28b90 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 at the SQL funct
28ba0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 ion or.** aggreg
28bb0 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 ate takes. If th
28bc0 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 is parameter is
28bd0 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c -1, then the SQL
28be0 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 function or.**
28bf0 61 67 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 aggregate may ta
28c00 6b 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 ke any number of
28c10 20 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 arguments betwe
28c20 65 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d en 0 and the lim
28c30 69 74 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 it.** set by [sq
28c40 6c 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 lite3_limit]([SQ
28c50 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 LITE_LIMIT_FUNCT
28c60 49 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 ION_ARG]). If t
28c70 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 he third.** para
28c80 6d 65 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 meter is less th
28c90 61 6e 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 an -1 or greater
28ca0 20 74 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 than 127 then t
28cb0 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a he behavior is.*
28cc0 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a * undefined..**.
28cd0 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 ** The fourth pa
28ce0 72 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 rameter, eTextRe
28cf0 70 2c 20 73 70 65 63 69 66 69 65 73 20 77 68 61 p, specifies wha
28d00 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 t.** [SQLITE_UTF
28d10 38 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 8 | text encodin
28d20 67 5d 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 g] this SQL func
28d30 74 69 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 tion prefers for
28d40 0a 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 .** its paramete
28d50 72 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e rs. Any SQL fun
28d60 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ction implementa
28d70 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 tion should be a
28d80 62 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 ble to work.** w
28d90 6f 72 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 ork with UTF-8,
28da0 55 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 UTF-16le, or UTF
28db0 2d 31 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 -16be. But some
28dc0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
28dd0 20 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 may be.** more
28de0 65 66 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f efficient with o
28df0 6e 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e ne encoding than
28e00 20 61 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 another. An ap
28e10 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a plication may.**
28e20 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f invoke sqlite3_
28e30 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 create_function(
28e40 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 ) or sqlite3_cre
28e50 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 ate_function16()
28e60 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d multiple.** tim
28e70 65 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 es with the same
28e80 20 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 function but wi
28e90 74 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c th different val
28ea0 75 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e ues of eTextRep.
28eb0 0a 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c .** When multipl
28ec0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
28ed0 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 s of the same fu
28ee0 6e 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c nction are avail
28ef0 61 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 able, SQLite.**
28f00 77 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e will pick the on
28f10 65 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 e that involves
28f20 74 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 the least amount
28f30 20 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 of data convers
28f40 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 ion..** If there
28f50 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c is only a singl
28f60 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
28f70 20 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 which does not
28f80 63 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a care what text.*
28f90 2a 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 * encoding is us
28fa0 65 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 ed, then the fou
28fb0 72 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f rth argument sho
28fc0 75 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 uld be [SQLITE_A
28fd0 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 NY]..**.** The f
28fe0 69 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 ifth parameter i
28ff0 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 s an arbitrary p
29000 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 ointer. The imp
29010 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
29020 68 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 he.** function c
29030 61 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 an gain access t
29040 6f 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 o this pointer u
29050 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 sing [sqlite3_us
29060 65 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a er_data()]..**.*
29070 2a 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 * The seventh, e
29080 69 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 ighth and ninth
29090 70 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e parameters, xFun
290a0 63 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 c, xStep and xFi
290b0 6e 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e nal, are.** poin
290c0 74 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 ters to C-langua
290d0 67 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 ge functions tha
290e0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 t implement the
290f0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a SQL function or.
29100 2a 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 ** aggregate. A
29110 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 scalar SQL funct
29120 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 ion requires an
29130 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
29140 66 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 f the xFunc.** c
29150 61 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 allback only, NU
29160 4c 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 LL pointers shou
29170 6c 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20 ld be passed as
29180 74 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 the xStep and xF
29190 69 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 inal.** paramete
291a0 72 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 rs. An aggregate
291b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 SQL function re
291c0 71 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d quires an implem
291d0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 entation of xSte
291e0 70 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 p.** and xFinal
291f0 61 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 and NULL should
29200 62 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 be passed for xF
29210 75 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 unc. To delete a
29220 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 n existing.** SQ
29230 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 L function or ag
29240 67 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 gregate, pass NU
29250 4c 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 LL for all three
29260 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 function callba
29270 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 cks..**.** It is
29280 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 permitted to re
29290 67 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 gister multiple
292a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
292b0 6f 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 of the same.** f
292c0 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 unctions with th
292d0 65 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 e same name but
292e0 77 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66 with either diff
292f0 65 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 ering numbers of
29300 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 .** arguments or
29310 20 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 differing prefe
29320 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 rred text encodi
29330 6e 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c ngs. SQLite wil
29340 6c 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 l use.** the imp
29350 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 lementation that
29360 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 most closely ma
29370 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e tches the way in
29380 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 which the.** SQ
29390 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 L function is us
293a0 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 ed. A function
293b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 implementation w
293c0 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 ith a non-negati
293d0 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d ve.** nArg param
293e0 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 eter is a better
293f0 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 match than a fu
29400 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 nction implement
29410 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 ation with.** a
29420 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 negative nArg.
29430 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 A function where
29440 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 the preferred t
29450 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 ext encoding.**
29460 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 matches the data
29470 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 base encoding is
29480 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 a better.** mat
29490 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 ch than a functi
294a0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 on where the enc
294b0 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 oding is differe
294c0 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 nt. .** A funct
294d0 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e ion where the en
294e0 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 coding differenc
294f0 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 e is between UTF
29500 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 16le and UTF16be
29510 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 .** is a closer
29520 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e match than a fun
29530 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 ction where the
29540 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 encoding differe
29550 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 nce is.** betwee
29560 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 n UTF8 and UTF16
29570 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e ..**.** Built-in
29580 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 functions may b
29590 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 e overloaded by
295a0 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d new application-
295b0 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e defined function
295c0 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 s..** The first
295d0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 application-defi
295e0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 ned function wit
295f0 68 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f h a given name o
29600 76 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 verrides all.**
29610 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f built-in functio
29620 6e 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b ns in the same [
29630 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
29640 69 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 ion] with the sa
29650 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 me name..** Subs
29660 65 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 equent applicati
29670 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 on-defined funct
29680 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 ions of the same
29690 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 name only overr
296a0 69 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 ide .** prior ap
296b0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
296c0 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 d functions that
296d0 20 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 are an exact ma
296e0 74 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e tch for the.** n
296f0 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 umber of paramet
29700 65 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 ers and preferre
29710 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a d encoding..**.*
29720 2a 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e * An application
29730 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
29740 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 n is permitted t
29750 6f 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 o call other.**
29760 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 SQLite interface
29770 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 s. However, suc
29780 68 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 h calls must not
29790 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 .** close the da
297a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
297b0 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f n nor finalize o
297c0 72 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 r reset the prep
297d0 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e ared.** statemen
297e0 74 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 t in which the f
297f0 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 unction is runni
29800 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 ng..**.** Requir
29810 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 ements:.** [H161
29820 30 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 03] [H16106] [H1
29830 36 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 6109] [H16112] [
29840 48 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d H16118] [H16121]
29850 20 5b 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 [H16127].** [H1
29860 36 31 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 6130] [H16133] [
29870 48 31 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d H16136] [H16139]
29880 20 5b 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c [H16142].*/.SQL
29890 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
298a0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 te3_create_funct
298b0 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a ion(. sqlite3 *
298c0 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 db,. const char
298d0 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c *zFunctionName,
298e0 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 . int nArg,. i
298f0 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 nt eTextRep,. v
29900 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 oid *pApp,. voi
29910 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 d (*xFunc)(sqlit
29920 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c e3_context*,int,
29930 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 sqlite3_value**)
29940 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 ,. void (*xStep
29950 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
29960 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
29970 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 alue**),. void
29980 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 (*xFinal)(sqlite
29990 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 3_context*).);.S
299a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
299b0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
299c0 63 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 ction16(. sqlit
299d0 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 e3 *db,. const
299e0 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e void *zFunctionN
299f0 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c ame,. int nArg,
29a00 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c . int eTextRep,
29a10 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 . void *pApp,.
29a20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 void (*xFunc)(s
29a30 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
29a40 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 int,sqlite3_valu
29a50 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 e**),. void (*x
29a60 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f Step)(sqlite3_co
29a70 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 ntext*,int,sqlit
29a80 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 e3_value**),. v
29a90 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 oid (*xFinal)(sq
29aa0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a lite3_context*).
29ab0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
29ac0 45 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e EF: Text Encodin
29ad0 67 73 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 gs {H10267} <S50
29ae0 32 30 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 200> <H16100>.**
29af0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 .** These consta
29b00 6e 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 nt define intege
29b10 72 20 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 r codes that rep
29b20 72 65 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f resent the vario
29b30 75 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 us.** text encod
29b40 69 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 ings supported b
29b50 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 y SQLite..*/.#de
29b60 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 fine SQLITE_UTF8
29b70 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 1.#de
29b80 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 fine SQLITE_UTF1
29b90 36 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 6LE 2.#de
29ba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 fine SQLITE_UTF1
29bb0 36 42 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 6BE 3.#de
29bc0 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 fine SQLITE_UTF1
29bd0 36 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 6 4
29be0 2f 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 /* Use native by
29bf0 74 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 te order */.#def
29c00 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 ine SQLITE_ANY
29c10 20 20 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 5 /
29c20 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 * sqlite3_create
29c30 5f 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a _function only *
29c40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
29c50 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 _UTF16_ALIGNED
29c60 38 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 8 /* sqlite3_
29c70 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
29c80 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 only */../*.**
29c90 43 41 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 CAPI3REF: Deprec
29ca0 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a ated Functions.*
29cb0 2a 20 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a * DEPRECATED.**.
29cc0 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f ** These functio
29cd0 6e 73 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 ns are [deprecat
29ce0 65 64 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 ed]. In order t
29cf0 6f 20 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 o maintain.** ba
29d00 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 ckwards compatib
29d10 69 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 ility with older
29d20 20 63 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e code, these fun
29d30 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 ctions continue
29d40 0a 2a 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 .** to be suppor
29d50 74 65 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e ted. However, n
29d60 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 ew applications
29d70 73 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 should avoid.**
29d80 74 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 the use of these
29d90 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 functions. To
29da0 68 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 help encourage p
29db0 65 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a eople to avoid.*
29dc0 2a 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 * using these fu
29dd0 6e 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 nctions, we are
29de0 6e 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c not going to tel
29df0 6c 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 l you what they
29e00 64 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 do..*/.#ifndef S
29e10 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 QLITE_OMIT_DEPRE
29e20 43 41 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 CATED.SQLITE_API
29e30 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 SQLITE_DEPRECAT
29e40 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 ED int sqlite3_a
29e50 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 ggregate_count(s
29e60 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 qlite3_context*)
29e70 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c ;.SQLITE_API SQL
29e80 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 ITE_DEPRECATED i
29e90 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 nt sqlite3_expir
29ea0 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a ed(sqlite3_stmt*
29eb0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 );.SQLITE_API SQ
29ec0 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 LITE_DEPRECATED
29ed0 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e int sqlite3_tran
29ee0 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 sfer_bindings(sq
29ef0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c lite3_stmt*, sql
29f00 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c ite3_stmt*);.SQL
29f10 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 ITE_API SQLITE_D
29f20 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 EPRECATED int sq
29f30 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 lite3_global_rec
29f40 6f 76 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 over(void);.SQLI
29f50 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
29f60 50 52 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 PRECATED void sq
29f70 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 lite3_thread_cle
29f80 61 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 anup(void);.SQLI
29f90 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 TE_API SQLITE_DE
29fa0 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c PRECATED int sql
29fb0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 ite3_memory_alar
29fc0 6d 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c m(void(*)(void*,
29fd0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e sqlite3_int64,in
29fe0 74 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 t),void*,sqlite3
29ff0 5f 69 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a _int64);.#endif.
2a000 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2a010 20 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 Obtaining SQL F
2a020 75 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 unction Paramete
2a030 72 20 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 r Values {H15100
2a040 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a } <S20200>.**.**
2a050 20 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 The C-language
2a060 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
2a070 66 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 f SQL functions
2a080 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 and aggregates u
2a090 73 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 ses.** this set
2a0a0 6f 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 of interface rou
2a0b0 74 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 tines to access
2a0c0 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 the parameter va
2a0d0 6c 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 lues on.** the f
2a0e0 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 unction or aggre
2a0f0 67 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 gate..**.** The
2a100 78 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 xFunc (for scala
2a110 72 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 r functions) or
2a120 78 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 xStep (for aggre
2a130 67 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 gates) parameter
2a140 73 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 s.** to [sqlite3
2a150 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
2a160 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 ()] and [sqlite3
2a170 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e _create_function
2a180 31 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 16()].** define
2a190 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 callbacks that i
2a1a0 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c mplement the SQL
2a1b0 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 functions and a
2a1c0 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 ggregates..** Th
2a1d0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 e 4th parameter
2a1e0 74 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 to these callbac
2a1f0 6b 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f ks is an array o
2a200 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a f pointers to.**
2a210 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 [protected sqli
2a220 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
2a230 74 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e ts. There is on
2a240 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 e [sqlite3_value
2a250 5d 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 ] object for.**
2a260 65 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 each parameter t
2a270 6f 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 o the SQL functi
2a280 6f 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 on. These routi
2a290 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a nes are used to.
2a2a0 2a 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 ** extract value
2a2b0 73 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 s from the [sqli
2a2c0 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 te3_value] objec
2a2d0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 ts..**.** These
2a2e0 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e routines work on
2a2f0 6c 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 ly with [protect
2a300 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2a310 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e ] objects..** An
2a320 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 y attempt to use
2a330 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
2a340 6f 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 on an [unprotect
2a350 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2a360 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 ].** object resu
2a370 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 lts in undefined
2a380 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a behavior..**.**
2a390 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
2a3a0 77 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 work just like t
2a3b0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 he corresponding
2a3c0 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 [column access
2a3d0 66 75 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 functions].** ex
2a3e0 63 65 70 74 20 74 68 61 74 20 20 74 68 65 73 65 cept that these
2a3f0 20 72 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 routines take a
2a400 20 73 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 single [protect
2a410 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 ed sqlite3_value
2a420 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e ] object.** poin
2a430 74 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 ter instead of a
2a440 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d [sqlite3_stmt*]
2a450 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 pointer and an
2a460 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e integer column n
2a470 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 umber..**.** The
2a480 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
2a490 65 78 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 ext16() interfac
2a4a0 65 20 65 78 74 72 61 63 74 73 20 61 20 55 54 46 e extracts a UTF
2a4b0 2d 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e -16 string.** in
2a4c0 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 the native byte
2a4d0 2d 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f -order of the ho
2a4e0 73 74 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 st machine. The
2a4f0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
2a500 65 5f 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 e_text16be() and
2a510 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
2a520 65 78 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 ext16le() interf
2a530 61 63 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 aces.** extract
2a540 55 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 UTF-16 strings a
2a550 73 20 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 s big-endian and
2a560 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 little-endian r
2a570 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a espectively..**.
2a580 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 ** The sqlite3_v
2a590 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 alue_numeric_typ
2a5a0 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 e() interface at
2a5b0 74 65 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a tempts to apply.
2a5c0 2a 2a 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e ** numeric affin
2a5d0 69 74 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 ity to the value
2a5e0 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 . This means th
2a5f0 61 74 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 at an attempt is
2a600 0a 2a 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 .** made to conv
2a610 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f ert the value to
2a620 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 an integer or f
2a630 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 loating point.
2a640 49 66 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e If.** such a con
2a650 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 version is possi
2a660 62 6c 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 ble without loss
2a670 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 of information
2a680 28 69 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 (in other.** wor
2a690 64 73 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 ds, if the value
2a6a0 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 is a string tha
2a6b0 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e t looks like a n
2a6c0 75 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 umber).** then t
2a6d0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 he conversion is
2a6e0 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 performed. Oth
2a6f0 65 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 erwise no conver
2a700 73 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 sion occurs..**
2a710 54 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 The [SQLITE_INTE
2a720 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 GER | datatype]
2a730 61 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e after conversion
2a740 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
2a750 0a 2a 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 .** Please pay p
2a760 61 72 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 articular attent
2a770 69 6f 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 ion to the fact
2a780 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 that the pointer
2a790 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f returned.** fro
2a7a0 6d 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 m [sqlite3_value
2a7b0 5f 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 _blob()], [sqlit
2a7c0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d e3_value_text()]
2a7d0 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 , or.** [sqlite3
2a7e0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d _value_text16()]
2a7f0 20 63 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 can be invalida
2a800 74 65 64 20 62 79 20 61 20 73 75 62 73 65 71 75 ted by a subsequ
2a810 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b ent call to.** [
2a820 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
2a830 74 65 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 tes()], [sqlite3
2a840 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 _value_bytes16()
2a850 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 ], [sqlite3_valu
2a860 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 e_text()],.** or
2a870 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f [sqlite3_value_
2a880 74 65 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a text16()]..**.**
2a890 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
2a8a0 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 must be called f
2a8b0 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 rom the same thr
2a8c0 65 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 ead as.** the SQ
2a8d0 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 L function that
2a8e0 73 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 supplied the [sq
2a8f0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 lite3_value*] pa
2a900 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 rameters..**.**
2a910 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
2a920 20 5b 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 [H15103] [H1510
2a930 36 5d 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 6] [H15109] [H15
2a940 31 31 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 112] [H15115] [H
2a950 31 35 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 15118] [H15121]
2a960 5b 48 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 [H15124].** [H15
2a970 31 32 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 127] [H15130] [H
2a980 31 35 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 15133] [H15136].
2a990 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
2a9a0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
2a9b0 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 3_value_blob(sql
2a9c0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 ite3_value*);.SQ
2a9d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
2a9e0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 ite3_value_bytes
2a9f0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
2aa00 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
2aa10 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 sqlite3_value_b
2aa20 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 ytes16(sqlite3_v
2aa30 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 alue*);.SQLITE_A
2aa40 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 PI double sqlite
2aa50 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 3_value_double(s
2aa60 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2aa70 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
2aa80 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 qlite3_value_int
2aa90 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
2aaa0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c ;.SQLITE_API sql
2aab0 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 ite3_int64 sqlit
2aac0 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 e3_value_int64(s
2aad0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2aae0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2aaf0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
2ab00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
2ab10 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 xt(sqlite3_value
2ab20 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 *);.SQLITE_API c
2ab30 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
2ab40 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 e3_value_text16(
2ab50 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b sqlite3_value*);
2ab60 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 .SQLITE_API cons
2ab70 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f t void *sqlite3_
2ab80 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 value_text16le(s
2ab90 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
2aba0 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
2abb0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
2abc0 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 alue_text16be(sq
2abd0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 lite3_value*);.S
2abe0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
2abf0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 lite3_value_type
2ac00 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 (sqlite3_value*)
2ac10 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
2ac20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e sqlite3_value_n
2ac30 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 umeric_type(sqli
2ac40 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a te3_value*);../*
2ac50 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 .** CAPI3REF: Ob
2ac60 74 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 tain Aggregate F
2ac70 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 unction Context
2ac80 7b 48 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 {H16210} <S20200
2ac90 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c >.**.** The impl
2aca0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 ementation of ag
2acb0 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 gregate SQL func
2acc0 74 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 tions use this r
2acd0 6f 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 outine to alloca
2ace0 74 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 te.** a structur
2acf0 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 e for storing th
2ad00 65 69 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a eir state..**.**
2ad10 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 The first time
2ad20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 the sqlite3_aggr
2ad30 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 egate_context()
2ad40 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
2ad50 64 20 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 d for a.** parti
2ad60 63 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c cular aggregate,
2ad70 20 53 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 SQLite allocate
2ad80 73 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f s nBytes of memo
2ad90 72 79 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 ry, zeroes out t
2ada0 68 61 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 hat.** memory, a
2adb0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 nd returns a poi
2adc0 6e 74 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 nter to it. On s
2add0 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 econd and subseq
2ade0 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a uent calls to.**
2adf0 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 sqlite3_aggrega
2ae00 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 te_context() for
2ae10 20 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 the same aggreg
2ae20 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 ate function ind
2ae30 65 78 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 ex,.** the same
2ae40 62 75 66 66 65 72 20 69 73 20 72 65 74 75 72 6e buffer is return
2ae50 65 64 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e ed. The implemen
2ae60 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 tation of the ag
2ae70 67 72 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a gregate can use.
2ae80 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 ** the returned
2ae90 62 75 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 buffer to accumu
2aea0 6c 61 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a late data..**.**
2aeb0 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 SQLite automati
2aec0 63 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 cally frees the
2aed0 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 allocated buffer
2aee0 20 77 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 when the aggreg
2aef0 61 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e ate.** query con
2af00 63 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 cludes..**.** Th
2af10 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
2af20 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f r should be a co
2af30 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 py of the.** [sq
2af40 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 lite3_context |
2af50 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e SQL function con
2af60 74 65 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 text] that is th
2af70 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 e first paramete
2af80 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c r.** to the call
2af90 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 back routine tha
2afa0 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 t implements the
2afb0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 aggregate funct
2afc0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ion..**.** This
2afd0 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 routine must be
2afe0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 called from the
2aff0 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 same thread in w
2b000 68 69 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 hich.** the aggr
2b010 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 egate SQL functi
2b020 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a on is running..*
2b030 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2b040 73 3a 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b s:.** [H16211] [
2b050 48 31 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d H16213] [H16215]
2b060 20 5b 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c [H16217].*/.SQL
2b070 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
2b080 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f lite3_aggregate_
2b090 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f context(sqlite3_
2b0a0 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 context*, int nB
2b0b0 79 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 ytes);../*.** CA
2b0c0 50 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 PI3REF: User Dat
2b0d0 61 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 a For Functions
2b0e0 7b 48 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 {H16240} <S20200
2b0f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
2b100 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 te3_user_data()
2b110 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e interface return
2b120 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 s a copy of.** t
2b130 68 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 he pointer that
2b140 77 61 73 20 74 68 65 20 70 55 73 65 72 44 61 74 was the pUserDat
2b150 61 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 a parameter (the
2b160 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 5th parameter).
2b170 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 ** of the [sqlit
2b180 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 e3_create_functi
2b190 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 on()].** and [sq
2b1a0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e lite3_create_fun
2b1b0 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 ction16()] routi
2b1c0 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 nes that origina
2b1d0 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 lly.** registere
2b1e0 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f d the applicatio
2b1f0 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 n defined functi
2b200 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 on. {END}.**.**
2b210 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 This routine mus
2b220 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d t be called from
2b230 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
2b240 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 in which.** the
2b250 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
2b260 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 ined function is
2b270 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 running..**.**
2b280 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a Requirements:.**
2b290 20 5b 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c [H16243].*/.SQL
2b2a0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
2b2b0 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 lite3_user_data(
2b2c0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2b2d0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
2b2e0 45 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e EF: Database Con
2b2f0 6e 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 nection For Func
2b300 74 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c tions {H16250} <
2b310 53 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a S60600><S20200>.
2b320 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2b330 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
2b340 64 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 dle() interface
2b350 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f returns a copy o
2b360 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 f.** the pointer
2b370 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 to the [databas
2b380 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 e connection] (t
2b390 68 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 he 1st parameter
2b3a0 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c ).** of the [sql
2b3b0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 ite3_create_func
2b3c0 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b tion()].** and [
2b3d0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 sqlite3_create_f
2b3e0 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 unction16()] rou
2b3f0 74 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 tines that origi
2b400 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 nally.** registe
2b410 72 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 red the applicat
2b420 69 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 ion defined func
2b430 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 tion..**.** Requ
2b440 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
2b450 36 32 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 6253].*/.SQLITE_
2b460 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c API sqlite3 *sql
2b470 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f ite3_context_db_
2b480 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 handle(sqlite3_c
2b490 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a ontext*);../*.**
2b4a0 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 CAPI3REF: Funct
2b4b0 69 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 ion Auxiliary Da
2b4c0 74 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 ta {H16270} <S20
2b4d0 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 200>.**.** The f
2b4e0 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e ollowing two fun
2b4f0 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 ctions may be us
2b500 65 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c ed by scalar SQL
2b510 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a functions to.**
2b520 20 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 associate metad
2b530 61 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e ata with argumen
2b540 74 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 t values. If the
2b550 20 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 same value is p
2b560 61 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 assed to.** mult
2b570 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 iple invocations
2b580 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c of the same SQL
2b590 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 function during
2b5a0 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e query execution
2b5b0 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 , under.** some
2b5c0 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 circumstances th
2b5d0 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 e associated met
2b5e0 61 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 adata may be pre
2b5f0 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 served. This may
2b600 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 .** be used, for
2b610 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 example, to add
2b620 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 a regular-expre
2b630 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 ssion matching s
2b640 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f calar.** functio
2b650 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 n. The compiled
2b660 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 version of the r
2b670 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f egular expressio
2b680 6e 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a n is stored as.*
2b690 2a 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 * metadata assoc
2b6a0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 iated with the S
2b6b0 51 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 QL value passed
2b6c0 61 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 as the regular e
2b6d0 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 xpression.** pat
2b6e0 74 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 tern. The compi
2b6f0 6c 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 led regular expr
2b700 65 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 ession can be re
2b710 75 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 used on multiple
2b720 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 .** invocations
2b730 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 of the same func
2b740 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 tion so that the
2b750 20 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 original patter
2b760 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 n string.** does
2b770 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 not need to be
2b780 72 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 recompiled on ea
2b790 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a ch invocation..*
2b7a0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2b7b0 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 _get_auxdata() i
2b7c0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
2b7d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
2b7e0 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 e metadata.** as
2b7f0 73 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 sociated by the
2b800 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 sqlite3_set_auxd
2b810 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 ata() function w
2b820 69 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 ith the Nth argu
2b830 6d 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f ment.** value to
2b840 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2b850 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2b860 6e 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 n. If no metadat
2b870 61 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a a has been ever.
2b880 2a 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 ** been set for
2b890 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 the Nth argument
2b8a0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
2b8b0 2c 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 , or if the corr
2b8c0 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e esponding.** fun
2b8d0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 ction parameter
2b8e0 68 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 has changed sinc
2b8f0 65 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 e the meta-data
2b900 77 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e was set,.** then
2b910 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 sqlite3_get_aux
2b920 64 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 data() returns a
2b930 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
2b940 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
2b950 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 _set_auxdata() i
2b960 6e 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 nterface saves t
2b970 68 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 he metadata.** p
2b980 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 ointed to by its
2b990 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 3rd parameter a
2b9a0 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 s the metadata f
2b9b0 6f 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 or the N-th.** a
2b9c0 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 rgument of the a
2b9d0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2b9e0 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 ed function. Su
2b9f0 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c bsequent.** call
2ba00 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 s to sqlite3_get
2ba10 5f 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 _auxdata() might
2ba20 20 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 return this dat
2ba30 61 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 a, if it has.**
2ba40 6e 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 not been destroy
2ba50 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 ed..** If it is
2ba60 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 not NULL, SQLite
2ba70 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 will invoke the
2ba80 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 destructor.** f
2ba90 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 unction given by
2baa0 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 the 4th paramet
2bab0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 er to sqlite3_se
2bac0 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a t_auxdata() on.*
2bad0 2a 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 * the metadata w
2bae0 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f hen the correspo
2baf0 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 nding function p
2bb00 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 arameter changes
2bb10 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 .** or when the
2bb20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f SQL statement co
2bb30 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 mpletes, whichev
2bb40 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a er comes first..
2bb50 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 **.** SQLite is
2bb60 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 free to call the
2bb70 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 destructor and
2bb80 64 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e drop metadata on
2bb90 20 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 any.** paramete
2bba0 72 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f r of any functio
2bbb0 6e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 n at any time.
2bbc0 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 The only guarant
2bbd0 65 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 ee is that.** th
2bbe0 65 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c e destructor wil
2bbf0 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f l be called befo
2bc00 72 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 re the metadata
2bc10 69 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a is dropped..**.*
2bc20 2a 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d * In practice, m
2bc30 65 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 etadata is prese
2bc40 72 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e rved between fun
2bc50 63 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a ction calls for.
2bc60 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 ** expressions t
2bc70 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 hat are constant
2bc80 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
2bc90 2e 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 . This includes
2bca0 6c 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 literal.** value
2bcb0 73 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 s and SQL variab
2bcc0 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 les..**.** These
2bcd0 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 routines must b
2bce0 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 e called from th
2bcf0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e e same thread in
2bd00 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 which.** the SQ
2bd10 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 L function is ru
2bd20 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 nning..**.** Req
2bd30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 uirements:.** [H
2bd40 31 36 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 16272] [H16274]
2bd50 5b 48 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 [H16276] [H16277
2bd60 5d 20 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 ] [H16278] [H162
2bd70 37 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 79].*/.SQLITE_AP
2bd80 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f I void *sqlite3_
2bd90 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 get_auxdata(sqli
2bda0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e te3_context*, in
2bdb0 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 t N);.SQLITE_API
2bdc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 void sqlite3_se
2bdd0 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 t_auxdata(sqlite
2bde0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 3_context*, int
2bdf0 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 N, void*, void (
2be00 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a *)(void*));.../*
2be10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f .** CAPI3REF: Co
2be20 6e 73 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 nstants Defining
2be30 20 53 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 Special Destruc
2be40 74 6f 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 tor Behavior {H1
2be50 30 32 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 0280} <S30100>.*
2be60 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 *.** These are s
2be70 70 65 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f pecial values fo
2be80 72 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 r the destructor
2be90 20 74 68 61 74 20 69 73 20 70 61 73 73 65 64 20 that is passed
2bea0 69 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e in as the.** fin
2beb0 61 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 al argument to r
2bec0 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 outines like [sq
2bed0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f lite3_result_blo
2bee0 62 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 b()]. If the de
2bef0 73 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 structor.** argu
2bf00 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 ment is SQLITE_S
2bf10 54 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 TATIC, it means
2bf20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 that the content
2bf30 20 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 pointer is cons
2bf40 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c tant.** and will
2bf50 20 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 never change.
2bf60 49 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 It does not need
2bf70 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 to be destroyed
2bf80 2e 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 . The.** SQLITE
2bf90 5f 54 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 _TRANSIENT value
2bfa0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 means that the
2bfb0 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b content will lik
2bfc0 65 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a ely change in.**
2bfd0 20 74 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 the near future
2bfe0 20 61 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 and that SQLite
2bff0 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 should make its
2c000 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 own private cop
2c010 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 y of.** the cont
2c020 65 6e 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 ent before retur
2c030 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ning..**.** The
2c040 74 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 typedef is neces
2c050 73 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f sary to work aro
2c060 75 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 und problems in
2c070 63 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 certain.** C++ c
2c080 6f 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 ompilers. See t
2c090 69 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a icket #2191..*/.
2c0a0 74 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 typedef void (*s
2c0b0 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f qlite3_destructo
2c0c0 72 5f 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a r_type)(void*);.
2c0d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
2c0e0 54 41 54 49 43 20 20 20 20 20 20 28 28 73 71 6c TATIC ((sql
2c0f0 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f ite3_destructor_
2c100 74 79 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 type)0).#define
2c110 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
2c120 20 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 ((sqlite3_des
2c130 74 72 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 tructor_type)-1)
2c140 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
2c150 3a 20 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 : Setting The Re
2c160 73 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 sult Of An SQL F
2c170 75 6e 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d unction {H16400}
2c180 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 <S20200>.**.**
2c190 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 These routines a
2c1a0 72 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78 re used by the x
2c1b0 46 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 Func or xFinal c
2c1c0 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a allbacks that.**
2c1d0 20 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 implement SQL f
2c1e0 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 unctions and agg
2c1f0 72 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a regates. See.**
2c200 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
2c210 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 _function()] and
2c220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
2c230 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a _function16()].*
2c240 2a 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c * for additional
2c250 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
2c260 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 .** These functi
2c270 6f 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 ons work very mu
2c280 63 68 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 ch like the [par
2c290 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 ameter binding]
2c2a0 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e family of.** fun
2c2b0 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 ctions used to b
2c2c0 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f ind values to ho
2c2d0 73 74 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e st parameters in
2c2e0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
2c2f0 65 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 ents..** Refer t
2c300 6f 20 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d o the [SQL param
2c310 65 74 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 eter] documentat
2c320 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e ion for addition
2c330 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a al information..
2c340 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2c350 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 3_result_blob()
2c360 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 interface sets t
2c370 68 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a he result from.*
2c380 2a 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e * an application
2c390 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2c3a0 6e 20 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 n to be the BLOB
2c3b0 20 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 whose content i
2c3c0 73 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 s pointed.** to
2c3d0 62 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 by the second pa
2c3e0 72 61 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 rameter and whic
2c3f0 68 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e h is N bytes lon
2c400 67 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 g where N is the
2c410 0a 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 .** third parame
2c420 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ter..**.** The s
2c430 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 qlite3_result_ze
2c440 72 6f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 roblob() interfa
2c450 63 65 73 20 73 65 74 20 74 68 65 20 72 65 73 75 ces set the resu
2c460 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 lt of.** the app
2c470 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 lication-defined
2c480 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 function to be
2c490 61 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e a BLOB containin
2c4a0 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 g all zero.** by
2c4b0 74 65 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 tes and N bytes
2c4c0 69 6e 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e in size, where N
2c4d0 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 is the value of
2c4e0 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
2c4f0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 er..**.** The sq
2c500 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 lite3_result_dou
2c510 62 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 ble() interface
2c520 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 sets the result
2c530 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 from.** an appli
2c540 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
2c550 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 unction to be a
2c560 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
2c570 61 6c 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a alue specified.*
2c580 2a 20 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 * by its 2nd arg
2c590 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ument..**.** The
2c5a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c5b0 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 error() and sqli
2c5c0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2c5d0 31 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 16() functions.*
2c5e0 2a 20 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c * cause the impl
2c5f0 65 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 emented SQL func
2c600 74 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e tion to throw an
2c610 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 exception..** S
2c620 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 QLite uses the s
2c630 74 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f tring pointed to
2c640 20 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 by the.** 2nd p
2c650 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 arameter of sqli
2c660 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 te3_result_error
2c670 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 () or sqlite3_re
2c680 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a sult_error16().*
2c690 2a 20 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 * as the text of
2c6a0 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
2c6b0 65 2e 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 e. SQLite inter
2c6c0 70 72 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a prets the error.
2c6d0 2a 2a 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e ** message strin
2c6e0 67 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 g from sqlite3_r
2c6f0 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 esult_error() as
2c700 20 55 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a UTF-8. SQLite.*
2c710 2a 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 * interprets the
2c720 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c string from sql
2c730 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2c740 72 31 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 r16() as UTF-16
2c750 69 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 in native.** byt
2c760 65 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 e order. If the
2c770 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 third parameter
2c780 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 to sqlite3_resu
2c790 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 lt_error().** or
2c7a0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2c7b0 65 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 error16() is neg
2c7c0 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 ative then SQLit
2c7d0 65 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 e takes as the e
2c7e0 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 rror.** message
2c7f0 61 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f all text up thro
2c800 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 ugh the first ze
2c810 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a ro character..**
2c820 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 If the third pa
2c830 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
2c840 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2c850 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f ) or.** sqlite3_
2c860 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 result_error16()
2c870 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 is non-negative
2c880 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b then SQLite tak
2c890 65 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 es that many.**
2c8a0 62 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 bytes (not chara
2c8b0 63 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 cters) from the
2c8c0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 2nd parameter as
2c8d0 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 the error messa
2c8e0 67 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ge..** The sqlit
2c8f0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
2c900 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 ) and sqlite3_re
2c910 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a sult_error16().*
2c920 2a 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 * routines make
2c930 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f a private copy o
2c940 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 f the error mess
2c950 61 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a age text before.
2c960 2a 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 ** they return.
2c970 20 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c Hence, the call
2c980 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e ing function can
2c990 20 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a deallocate or.*
2c9a0 2a 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 * modify the tex
2c9b0 74 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74 t after they ret
2c9c0 75 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d urn without harm
2c9d0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 ..** The sqlite3
2c9e0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f _result_error_co
2c9f0 64 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 de() function ch
2ca00 61 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 anges the error
2ca10 63 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 code.** returned
2ca20 20 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 by SQLite as a
2ca30 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 result of an err
2ca40 6f 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e or in a function
2ca50 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a . By default,.*
2ca60 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 * the error code
2ca70 20 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 is SQLITE_ERROR
2ca80 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 . A subsequent
2ca90 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f call to sqlite3_
2caa0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a result_error().*
2cab0 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 * or sqlite3_res
2cac0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 ult_error16() re
2cad0 73 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 sets the error c
2cae0 6f 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 ode to SQLITE_ER
2caf0 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ROR..**.** The s
2cb00 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f qlite3_result_to
2cb10 6f 62 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 obig() interface
2cb20 20 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 causes SQLite t
2cb30 6f 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 o throw an error
2cb40 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 .** indicating t
2cb50 68 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 hat a string or
2cb60 42 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 BLOB is to long
2cb70 74 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a to represent..**
2cb80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2cb90 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 result_nomem() i
2cba0 6e 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 nterface causes
2cbb0 53 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 SQLite to throw
2cbc0 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 an error.** indi
2cbd0 63 61 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 cating that a me
2cbe0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
2cbf0 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 failed..**.** Th
2cc00 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 e sqlite3_result
2cc10 5f 69 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 _int() interface
2cc20 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e sets the return
2cc30 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 value.** of the
2cc40 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 application-def
2cc50 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f ined function to
2cc60 20 62 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 be the 32-bit s
2cc70 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
2cc80 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 value given in
2cc90 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 the 2nd argument
2cca0 2e 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 36 34 28 29 20 _result_int64()
2ccc0 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 interface sets t
2ccd0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
2cce0 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 ** of the applic
2ccf0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 ation-defined fu
2cd00 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 nction to be the
2cd10 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
2cd20 6e 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 nteger.** value
2cd30 67 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 given in the 2nd
2cd40 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a argument..**.**
2cd50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 The sqlite3_res
2cd60 75 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 ult_null() inter
2cd70 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 face sets the re
2cd80 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 turn value.** of
2cd90 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
2cda0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f -defined functio
2cdb0 6e 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a n to be NULL..**
2cdc0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f .** The sqlite3_
2cdd0 72 65 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 result_text(), s
2cde0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2cdf0 78 74 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 xt16(),.** sqlit
2ce00 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 e3_result_text16
2ce10 6c 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 le(), and sqlite
2ce20 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 3_result_text16b
2ce30 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a e() interfaces.*
2ce40 2a 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e * set the return
2ce50 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 value of the ap
2ce60 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 plication-define
2ce70 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 d function to be
2ce80 0a 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e .** a text strin
2ce90 67 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65 g which is repre
2cea0 73 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c sented as UTF-8,
2ceb0 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 UTF-16 native b
2cec0 79 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 yte order,.** UT
2ced0 46 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 F-16 little endi
2cee0 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 an, or UTF-16 bi
2cef0 67 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 g endian, respec
2cf00 74 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 tively..** SQLit
2cf10 65 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 e takes the text
2cf20 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 result from the
2cf30 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f application fro
2cf40 6d 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 m.** the 2nd par
2cf50 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 ameter of the sq
2cf60 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2cf70 74 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a t* interfaces..*
2cf80 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 * If the 3rd par
2cf90 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 ameter to the sq
2cfa0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
2cfb0 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a t* interfaces.**
2cfc0 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 is negative, th
2cfd0 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 en SQLite takes
2cfe0 72 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d result text from
2cff0 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 the 2nd paramet
2d000 65 72 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 er.** through th
2d010 65 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 e first zero cha
2d020 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 racter..** If th
2d030 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 e 3rd parameter
2d040 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 to the sqlite3_r
2d050 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 esult_text* inte
2d060 72 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e rfaces.** is non
2d070 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 -negative, then
2d080 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e as many bytes (n
2d090 6f 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f ot characters) o
2d0a0 66 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f f the text.** po
2d0b0 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 inted to by the
2d0c0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 2nd parameter ar
2d0d0 65 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 e taken as the a
2d0e0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e pplication-defin
2d0f0 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 ed.** function r
2d100 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 esult..** If the
2d110 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 4th parameter t
2d120 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 o the sqlite3_re
2d130 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 sult_text* inter
2d140 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 faces.** or sqli
2d150 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 te3_result_blob
2d160 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f is a non-NULL po
2d170 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 inter, then SQLi
2d180 74 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a te calls that.**
2d190 20 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 function as the
2d1a0 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 destructor on t
2d1b0 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 he text or BLOB
2d1c0 72 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 result when it h
2d1d0 61 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 as.** finished u
2d1e0 73 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 sing that result
2d1f0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 ..** If the 4th
2d200 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 parameter to the
2d210 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2d220 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 text* interfaces
2d230 20 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 or to.** sqlite
2d240 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 3_result_blob is
2d250 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e the special con
2d260 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 stant SQLITE_STA
2d270 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 TIC, then SQLite
2d280 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 .** assumes that
2d290 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f the text or BLO
2d2a0 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 B result is in c
2d2b0 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e onstant space an
2d2c0 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f d does not.** co
2d2d0 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f py the content o
2d2e0 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 f the parameter
2d2f0 6e 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 nor call a destr
2d300 75 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e uctor on the con
2d310 74 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 tent.** when it
2d320 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 has finished usi
2d330 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a ng that result..
2d340 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 ** If the 4th pa
2d350 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 rameter to the s
2d360 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
2d370 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a xt* interfaces.*
2d380 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 * or sqlite3_res
2d390 75 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 ult_blob is the
2d3a0 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 special constant
2d3b0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
2d3c0 54 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 T.** then SQLite
2d3d0 20 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 makes a copy of
2d3e0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f the result into
2d3f0 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 space obtained
2d400 66 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 from.** from [sq
2d410 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 lite3_malloc()]
2d420 62 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e before it return
2d430 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c s..**.** The sql
2d440 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 ite3_result_valu
2d450 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 e() interface se
2d460 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 ts the result of
2d470 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 .** the applicat
2d480 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 ion-defined func
2d490 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 tion to be a cop
2d4a0 79 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 y the.** [unprot
2d4b0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
2d4c0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 lue] object spec
2d4d0 69 66 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 ified by the 2nd
2d4e0 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 parameter. The
2d4f0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 .** sqlite3_resu
2d500 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 lt_value() inter
2d510 66 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 face makes a cop
2d520 79 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 y of the [sqlite
2d530 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 3_value].** so t
2d540 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 hat the [sqlite3
2d550 5f 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 _value] specifie
2d560 64 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 d in the paramet
2d570 65 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 er may change or
2d580 0a 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 .** be deallocat
2d590 65 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 ed after sqlite3
2d5a0 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 _result_value()
2d5b0 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 returns without
2d5c0 68 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 harm..** A [prot
2d5d0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 ected sqlite3_va
2d5e0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 lue] object may
2d5f0 61 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 77 always be used w
2d600 68 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 here an.** [unpr
2d610 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f otected sqlite3_
2d620 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 value] object is
2d630 20 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 required, so ei
2d640 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 ther.** kind of
2d650 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 [sqlite3_value]
2d660 6f 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 object can be us
2d670 65 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 ed with this int
2d680 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 erface..**.** If
2d690 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 these routines
2d6a0 61 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 are called from
2d6b0 77 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 within the diffe
2d6c0 72 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 rent thread.** t
2d6d0 68 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 han the one cont
2d6e0 61 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 aining the appli
2d6f0 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 cation-defined f
2d700 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 unction that rec
2d710 65 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 eived.** the [sq
2d720 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 lite3_context] p
2d730 6f 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 ointer, the resu
2d740 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 lts are undefine
2d750 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 d..**.** Require
2d760 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 ments:.** [H1640
2d770 33 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 3] [H16406] [H16
2d780 34 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 409] [H16412] [H
2d790 31 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 16415] [H16418]
2d7a0 5b 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 [H16421] [H16424
2d7b0 5d 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 ].** [H16427] [H
2d7c0 31 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 16430] [H16433]
2d7d0 5b 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 [H16436] [H16439
2d7e0 5d 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 ] [H16442] [H164
2d7f0 34 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 45] [H16448].**
2d800 5b 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 [H16451] [H16454
2d810 5d 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 ] [H16457] [H164
2d820 36 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 60] [H16463].*/.
2d830 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2d840 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 sqlite3_result_b
2d850 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 lob(sqlite3_cont
2d860 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ext*, const void
2d870 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 *, int, void(*)(
2d880 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f void*));.SQLITE_
2d890 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
2d8a0 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 _result_double(s
2d8b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2d8c0 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 double);.SQLITE
2d8d0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2d8e0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 3_result_error(s
2d8f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2d900 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e const char*, in
2d910 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 t);.SQLITE_API v
2d920 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2d930 6c 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 lt_error16(sqlit
2d940 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e e3_context*, con
2d950 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a st void*, int);.
2d960 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
2d970 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 sqlite3_result_e
2d980 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 rror_toobig(sqli
2d990 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 te3_context*);.S
2d9a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2d9b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 qlite3_result_er
2d9c0 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 ror_nomem(sqlite
2d9d0 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 3_context*);.SQL
2d9e0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
2d9f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f ite3_result_erro
2da00 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 r_code(sqlite3_c
2da10 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 ontext*, int);.S
2da20 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
2da30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e qlite3_result_in
2da40 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 t(sqlite3_contex
2da50 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 t*, int);.SQLITE
2da60 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
2da70 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 3_result_int64(s
2da80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c qlite3_context*,
2da90 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b sqlite3_int64);
2daa0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2dab0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2dac0 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e null(sqlite3_con
2dad0 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 text*);.SQLITE_A
2dae0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2daf0 72 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 result_text(sqli
2db00 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f te3_context*, co
2db10 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 nst char*, int,
2db20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b void(*)(void*));
2db30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
2db40 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
2db50 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 text16(sqlite3_c
2db60 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 ontext*, const v
2db70 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 oid*, int, void(
2db80 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
2db90 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2dba0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 te3_result_text1
2dbb0 36 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 6le(sqlite3_cont
2dbc0 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 ext*, const void
2dbd0 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 *, int,void(*)(v
2dbe0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 oid*));.SQLITE_A
2dbf0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
2dc00 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 result_text16be(
2dc10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a sqlite3_context*
2dc20 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 , const void*, i
2dc30 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a nt,void(*)(void*
2dc40 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 ));.SQLITE_API v
2dc50 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
2dc60 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 lt_value(sqlite3
2dc70 5f 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 _context*, sqlit
2dc80 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 e3_value*);.SQLI
2dc90 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
2dca0 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 te3_result_zerob
2dcb0 6c 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 lob(sqlite3_cont
2dcc0 65 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f ext*, int n);../
2dcd0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 *.** CAPI3REF: D
2dce0 65 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 efine New Collat
2dcf0 69 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 ing Sequences {H
2dd00 31 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 16600} <S20300>.
2dd10 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 **.** These func
2dd20 74 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 tions are used t
2dd30 6f 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 o add new collat
2dd40 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f ion sequences to
2dd50 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 the.** [databas
2dd60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 e connection] sp
2dd70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20 66 ecified as the f
2dd80 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a irst argument..*
2dd90 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 *.** The name of
2dda0 20 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 the new collati
2ddb0 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 on sequence is s
2ddc0 70 65 63 69 66 69 65 64 20 61 73 20 61 20 55 54 pecified as a UT
2ddd0 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f F-8 string.** fo
2dde0 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 r sqlite3_create
2ddf0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 _collation() and
2de00 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2de10 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a collation_v2().*
2de20 2a 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 * and a UTF-16 s
2de30 74 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 tring for sqlite
2de40 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 3_create_collati
2de50 6f 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 on16(). In all c
2de60 61 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 ases.** the name
2de70 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 is passed as th
2de80 65 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f e second functio
2de90 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a n argument..**.*
2dea0 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 * The third argu
2deb0 6d 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 ment may be one
2dec0 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 of the constants
2ded0 20 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a [SQLITE_UTF8],.
2dee0 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 ** [SQLITE_UTF16
2def0 4c 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f LE], or [SQLITE_
2df00 55 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 UTF16BE], indica
2df10 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 ting that the us
2df20 65 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 er-supplied.** r
2df30 6f 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 outine expects t
2df40 6f 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e o be passed poin
2df50 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 ters to strings
2df60 65 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 encoded using UT
2df70 46 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c F-8,.** UTF-16 l
2df80 69 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 ittle-endian, or
2df90 20 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 UTF-16 big-endi
2dfa0 61 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 an, respectively
2dfb0 2e 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 . The.** third a
2dfc0 72 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c rgument might al
2dfd0 73 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 so be [SQLITE_UT
2dfe0 46 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 F16] to indicate
2dff0 20 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e that the routin
2e000 65 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 e.** expects poi
2e010 6e 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d nters to be UTF-
2e020 31 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 16 strings in th
2e030 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 e native byte or
2e040 64 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 der, or the.** a
2e050 72 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b rgument can be [
2e060 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 SQLITE_UTF16_ALI
2e070 47 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 GNED] if the.**
2e080 74 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 the routine expe
2e090 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 cts pointers to
2e0a0 31 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 16-bit word alig
2e0b0 6e 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f ned strings.** o
2e0c0 66 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 f UTF-16 in the
2e0d0 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 native byte orde
2e0e0 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 r..**.** A point
2e0f0 65 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 er to the user s
2e100 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 upplied routine
2e110 6d 75 73 74 20 62 65 20 70 61 73 73 65 64 20 61 must be passed a
2e120 73 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 s the fifth.** a
2e130 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 rgument. If it
2e140 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 is NULL, this is
2e150 20 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c the same as del
2e160 65 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 eting the collat
2e170 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 ion.** sequence
2e180 28 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 (so that SQLite
2e190 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 cannot call it a
2e1a0 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 nymore)..** Each
2e1b0 20 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 time the applic
2e1c0 61 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 ation supplied f
2e1d0 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b unction is invok
2e1e0 65 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 ed, it is passed
2e1f0 0a 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 .** as its first
2e200 20 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 parameter a cop
2e210 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 y of the void* p
2e220 61 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 assed as the fou
2e230 72 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 rth argument.**
2e240 74 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 to sqlite3_creat
2e250 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 e_collation() or
2e260 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f sqlite3_create_
2e270 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a collation16()..*
2e280 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 *.** The remaini
2e290 6e 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 ng arguments to
2e2a0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d the application-
2e2b0 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 supplied routine
2e2c0 20 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 are two strings
2e2d0 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 ,.** each repres
2e2e0 65 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 ented by a (leng
2e2f0 74 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 th, data) pair a
2e300 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 nd encoded in th
2e310 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 e encoding.** th
2e320 61 74 20 77 61 73 20 70 61 73 73 65 64 20 61 73 at was passed as
2e330 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d the third argum
2e340 65 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c ent when the col
2e350 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
2e360 77 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 was.** registere
2e370 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 d. {END} The ap
2e380 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 plication define
2e390 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 d collation rout
2e3a0 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 ine should.** re
2e3b0 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a turn negative, z
2e3c0 65 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 ero or positive
2e3d0 69 66 20 74 68 65 20 66 69 72 73 74 20 73 74 72 if the first str
2e3e0 69 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e ing is less than
2e3f0 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f ,.** equal to, o
2e400 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 r greater than t
2e410 68 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 he second string
2e420 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 . i.e. (STRING1
2e430 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a - STRING2)..**.*
2e440 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 * The sqlite3_cr
2e450 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 eate_collation_v
2e460 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 2() works like s
2e470 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f qlite3_create_co
2e480 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 llation().** exc
2e490 65 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 ept that it take
2e4a0 73 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d s an extra argum
2e4b0 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64 ent which is a d
2e4c0 65 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a estructor for.**
2e4d0 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 the collation.
2e4e0 20 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 The destructor
2e4f0 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 is called when t
2e500 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a he collation is.
2e510 2a 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 ** destroyed and
2e520 20 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 is passed a cop
2e530 79 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 y of the fourth
2e540 70 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 parameter void*
2e550 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 pointer.** of th
2e560 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 e sqlite3_create
2e570 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e _collation_v2().
2e580 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 .** Collations a
2e590 72 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 re destroyed whe
2e5a0 6e 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 n they are overr
2e5b0 69 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 idden by later c
2e5c0 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 alls to the.** c
2e5d0 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f ollation creatio
2e5e0 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 n functions or w
2e5f0 68 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 hen the [databas
2e600 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 e connection] is
2e610 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 closed.** using
2e620 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 [sqlite3_close(
2e630 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c )]..**.** See al
2e640 73 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f so: [sqlite3_co
2e650 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 llation_needed()
2e660 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 ] and [sqlite3_c
2e670 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 ollation_needed1
2e680 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 6()]..**.** Requ
2e690 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
2e6a0 36 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 6603] [H16604] [
2e6b0 48 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d H16606] [H16609]
2e6c0 20 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 [H16612] [H1661
2e6d0 35 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 5] [H16618] [H16
2e6e0 36 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 621].** [H16624]
2e6f0 20 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 [H16627] [H1663
2e700 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 0].*/.SQLITE_API
2e710 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 int sqlite3_cre
2e720 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 ate_collation(.
2e730 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f sqlite3*, . co
2e740 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
2e750 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 . int eTextRep
2e760 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e , . void*,. in
2e770 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 t(*xCompare)(voi
2e780 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 d*,int,const voi
2e790 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 d*,int,const voi
2e7a0 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 d*).);.SQLITE_AP
2e7b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 I int sqlite3_cr
2e7c0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 eate_collation_v
2e7d0 32 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 2(. sqlite3*, .
2e7e0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
2e7f0 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 ame, . int eTex
2e800 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a tRep, . void*,.
2e810 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 int(*xCompare)
2e820 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 (void*,int,const
2e830 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 void*,int,const
2e840 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 void*),. void(
2e850 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a *xDestroy)(void*
2e860 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 ).);.SQLITE_API
2e870 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 int sqlite3_crea
2e880 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a te_collation16(.
2e890 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 sqlite3*, . c
2e8a0 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 onst void *zName
2e8b0 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 ,. int eTextRep
2e8c0 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e , . void*,. in
2e8d0 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 t(*xCompare)(voi
2e8e0 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 d*,int,const voi
2e8f0 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 d*,int,const voi
2e900 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 d*).);../*.** CA
2e910 50 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f PI3REF: Collatio
2e920 6e 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 n Needed Callbac
2e930 6b 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 ks {H16700} <S20
2e940 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 300>.**.** To av
2e950 6f 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 oid having to re
2e960 67 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 gister all colla
2e970 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 tion sequences b
2e980 65 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 efore a database
2e990 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c .** can be used,
2e9a0 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 a single callba
2e9b0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 ck function may
2e9c0 62 65 20 72 65 67 69 73 74 65 72 65 64 20 77 69 be registered wi
2e9d0 74 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 th the.** [datab
2e9e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 ase connection]
2e9f0 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 to be called whe
2ea00 6e 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e never an undefin
2ea10 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 ed collation.**
2ea20 73 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 sequence is requ
2ea30 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ired..**.** If t
2ea40 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 he function is r
2ea50 65 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 egistered using
2ea60 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c the sqlite3_coll
2ea70 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 ation_needed() A
2ea80 50 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 PI,.** then it i
2ea90 73 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d s passed the nam
2eaa0 65 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 es of undefined
2eab0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
2eac0 63 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a ces as strings.*
2ead0 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 * encoded in UTF
2eae0 2d 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 -8. {H16703} If
2eaf0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f sqlite3_collatio
2eb00 6e 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 n_needed16() is
2eb10 75 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d used,.** the nam
2eb20 65 73 20 61 72 65 20 70 61 73 73 65 64 20 61 73 es are passed as
2eb30 20 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 UTF-16 in machi
2eb40 6e 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f ne native byte o
2eb50 72 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 rder..** A call
2eb60 74 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 to either functi
2eb70 6f 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 on replaces any
2eb80 65 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 existing callbac
2eb90 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 k..**.** When th
2eba0 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e e callback is in
2ebb0 76 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 voked, the first
2ebc0 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 argument passed
2ebd0 20 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 is a copy.** of
2ebe0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
2ebf0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f ment to sqlite3_
2ec00 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
2ec10 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 () or.** sqlite3
2ec20 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 _collation_neede
2ec30 64 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f d16(). The seco
2ec40 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 nd argument is t
2ec50 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 he database.** c
2ec60 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 onnection. The
2ec70 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 third argument i
2ec80 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 s one of [SQLITE
2ec90 5f 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f _UTF8], [SQLITE_
2eca0 55 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 UTF16BE],.** or
2ecb0 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d [SQLITE_UTF16LE]
2ecc0 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 , indicating the
2ecd0 20 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 most desirable
2ece0 66 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c form of the coll
2ecf0 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 ation.** sequenc
2ed00 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 e function requi
2ed10 72 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 red. The fourth
2ed20 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 parameter is th
2ed30 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a e name of the.**
2ed40 20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 required collat
2ed50 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a ion sequence..**
2ed60 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b .** The callback
2ed70 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
2ed80 20 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65 register the de
2ed90 73 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 sired collation
2eda0 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 using.** [sqlite
2edb0 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 3_create_collati
2edc0 6f 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f on()], [sqlite3_
2edd0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e create_collation
2ede0 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 16()], or.** [sq
2edf0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c lite3_create_col
2ee00 6c 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a lation_v2()]..**
2ee10 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
2ee20 3a 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 :.** [H16702] [H
2ee30 31 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 16704] [H16706].
2ee40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
2ee50 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 t sqlite3_collat
2ee60 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 ion_needed(. sq
2ee70 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a lite3*, . void*
2ee80 2c 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 , . void(*)(voi
2ee90 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 d*,sqlite3*,int
2eea0 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 eTextRep,const c
2eeb0 68 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f har*).);.SQLITE_
2eec0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
2eed0 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 collation_needed
2eee0 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 16(. sqlite3*,
2eef0 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 . void*,. void
2ef00 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 (*)(void*,sqlite
2ef10 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 3*,int eTextRep,
2ef20 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a const void*).);.
2ef30 0a 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 ./*.** Specify t
2ef40 68 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e he key for an en
2ef50 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65 crypted database
2ef60 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
2ef70 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c should be.** cal
2ef80 6c 65 64 20 72 69 67 68 74 20 61 66 74 65 72 20 led right after
2ef90 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a sqlite3_open()..
2efa0 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 **.** The code t
2efb0 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 o implement this
2efc0 20 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 API is not avai
2efd0 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 lable in the pub
2efe0 6c 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f lic release.** o
2eff0 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c f SQLite..*/.SQL
2f000 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
2f010 74 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 te3_key(. sqlit
2f020 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
2f030 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 /* Dat
2f040 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 abase to be reke
2f050 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 yed */. const v
2f060 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e oid *pKey, int n
2f070 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b Key /* The k
2f080 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 ey */.);../*.**
2f090 43 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f Change the key o
2f0a0 6e 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 n an open databa
2f0b0 73 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 se. If the curr
2f0c0 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 20 ent database is
2f0d0 6e 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 not.** encrypted
2f0e0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 , this routine w
2f0f0 69 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 ill encrypt it.
2f100 20 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e If pNew==0 or n
2f110 4e 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 New==0, the.** d
2f120 61 74 61 62 61 73 65 20 69 73 20 64 65 63 72 79 atabase is decry
2f130 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 pted..**.** The
2f140 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e code to implemen
2f150 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f t this API is no
2f160 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 t available in t
2f170 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 he public releas
2f180 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a e.** of SQLite..
2f190 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
2f1a0 74 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 t sqlite3_rekey(
2f1b0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
2f1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2f1d0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f /* Database to
2f1e0 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 be rekeyed */.
2f1f0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
2f200 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 y, int nKey
2f210 2f 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a /* The new key *
2f220 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 /.);../*.** CAPI
2f230 33 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 3REF: Suspend Ex
2f240 65 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 ecution For A Sh
2f250 6f 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 ort Time {H10530
2f260 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a } <S40410>.**.**
2f270 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 The sqlite3_sle
2f280 65 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 ep() function ca
2f290 75 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 uses the current
2f2a0 20 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 thread to suspe
2f2b0 6e 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 nd execution.**
2f2c0 66 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e for at least a n
2f2d0 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 umber of millise
2f2e0 63 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 conds specified
2f2f0 69 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 in its parameter
2f300 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f ..**.** If the o
2f310 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
2f320 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 does not support
2f330 20 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 sleep requests
2f340 77 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 with.** millisec
2f350 6f 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 ond time resolut
2f360 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 ion, then the ti
2f370 6d 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 me will be round
2f380 65 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 ed up to.** the
2f390 6e 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 nearest second.
2f3a0 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 The number of mi
2f3b0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c lliseconds of sl
2f3c0 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 eep actually.**
2f3d0 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 requested from t
2f3e0 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 he operating sys
2f3f0 74 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e tem is returned.
2f400 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d .**.** SQLite im
2f410 70 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e plements this in
2f420 74 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 terface by calli
2f430 6e 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a ng the xSleep().
2f440 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 ** method of the
2f450 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 default [sqlite
2f460 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 3_vfs] object..*
2f470 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
2f480 73 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 s: [H10533] [H10
2f490 35 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 536].*/.SQLITE_A
2f4a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 PI int sqlite3_s
2f4b0 6c 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a leep(int);../*.*
2f4c0 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 * CAPI3REF: Name
2f4d0 20 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 Of The Folder H
2f4e0 6f 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 olding Temporary
2f4f0 20 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 Files {H10310}
2f500 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 <S20000>.**.** I
2f510 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 f this global va
2f520 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 riable is made t
2f530 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 o point to a str
2f540 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 ing which is.**
2f550 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f the name of a fo
2f560 6c 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 lder (a.k.a. dir
2f570 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c ectory), then al
2f580 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 l temporary file
2f590 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 s.** created by
2f5a0 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 SQLite will be p
2f5b0 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 laced in that di
2f5c0 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 rectory. If thi
2f5d0 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 s variable.** is
2f5e0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c a NULL pointer,
2f5f0 20 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 then SQLite per
2f600 66 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 forms a search f
2f610 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 or an appropriat
2f620 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 e.** temporary f
2f630 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a ile directory..*
2f640 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 *.** It is not s
2f650 61 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d afe to read or m
2f660 6f 64 69 66 79 20 74 68 69 73 20 76 61 72 69 61 odify this varia
2f670 62 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e ble in more than
2f680 20 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 one.** thread a
2f690 74 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 t a time. It is
2f6a0 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 not safe to rea
2f6b0 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 d or modify this
2f6c0 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 variable.** if
2f6d0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e a [database conn
2f6e0 65 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 ection] is being
2f6f0 20 75 73 65 64 20 61 74 20 74 68 65 20 73 61 6d used at the sam
2f700 65 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 e time in a sepa
2f710 72 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a rate.** thread..
2f720 2a 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 ** It is intende
2f730 64 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 d that this vari
2f740 61 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 able be set once
2f750 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 .** as part of p
2f760 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a rocess initializ
2f770 61 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 ation and before
2f780 20 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 any SQLite inte
2f790 72 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 rface.** routine
2f7a0 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c s have been call
2f7b0 65 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 ed and that this
2f7c0 20 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e variable remain
2f7d0 20 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 unchanged.** th
2f7e0 65 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 ereafter..**.**
2f7f0 54 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f The [temp_store_
2f800 64 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 directory pragma
2f810 5d 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 ] may modify thi
2f820 73 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 s variable and c
2f830 61 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f ause.** it to po
2f840 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 int to memory ob
2f850 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c tained from [sql
2f860 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 ite3_malloc]. F
2f870 75 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 urthermore,.** t
2f880 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 he [temp_store_d
2f890 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d irectory pragma]
2f8a0 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 always assumes
2f8b0 74 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a that any string.
2f8c0 2a 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72 ** that this var
2f8d0 69 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 iable points to
2f8e0 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 is held in memor
2f8f0 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 y obtained from
2f900 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c .** [sqlite3_mal
2f910 6c 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 loc] and the pra
2f920 67 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 gma may attempt
2f930 74 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d to free that mem
2f940 6f 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 ory.** using [sq
2f950 6c 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 lite3_free]..**
2f960 48 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 Hence, if this v
2f970 61 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 ariable is modif
2f980 69 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 ied directly, ei
2f990 74 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 ther it should b
2f9a0 65 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f e.** made NULL o
2f9b0 72 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 r made to point
2f9c0 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
2f9d0 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 ed from [sqlite3
2f9e0 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 _malloc].** or e
2f9f0 6c 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 lse the use of t
2fa00 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 he [temp_store_d
2fa10 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d irectory pragma]
2fa20 20 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 should be avoid
2fa30 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ed..*/.SQLITE_AP
2fa40 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f I char *sqlite3_
2fa50 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a temp_directory;.
2fa60 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
2fa70 20 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 Test For Auto-C
2fa80 6f 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 ommit Mode {H129
2fa90 33 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 30} <S60200>.**
2faa0 4b 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 KEYWORDS: {autoc
2fab0 6f 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a ommit mode}.**.*
2fac0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 * The sqlite3_ge
2fad0 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 t_autocommit() i
2fae0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
2faf0 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 non-zero or.**
2fb00 7a 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 zero if the give
2fb10 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 n database conne
2fb20 63 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e ction is or is n
2fb30 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 ot in autocommit
2fb40 20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 mode,.** respec
2fb50 74 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d tively. Autocom
2fb60 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 mit mode is on b
2fb70 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 y default..** Au
2fb80 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 tocommit mode is
2fb90 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b disabled by a [
2fba0 42 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 BEGIN] statement
2fbb0 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 ..** Autocommit
2fbc0 6d 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c mode is re-enabl
2fbd0 65 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d ed by a [COMMIT]
2fbe0 20 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a or [ROLLBACK]..
2fbf0 2a 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e **.** If certain
2fc00 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 kinds of errors
2fc10 20 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 occur on a stat
2fc20 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d ement within a m
2fc30 75 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a ulti-statement.*
2fc40 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 * transaction (e
2fc50 72 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 rrors including
2fc60 5b 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b [SQLITE_FULL], [
2fc70 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a SQLITE_IOERR],.*
2fc80 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d * [SQLITE_NOMEM]
2fc90 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c , [SQLITE_BUSY],
2fca0 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 and [SQLITE_INT
2fcb0 45 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 ERRUPT]) then th
2fcc0 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
2fcd0 20 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 might be rolled
2fce0 20 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 back automatica
2fcf0 6c 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 lly. The only w
2fd00 61 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 ay to.** find ou
2fd10 74 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 t whether SQLite
2fd20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 automatically r
2fd30 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 olled back the t
2fd40 72 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 ransaction after
2fd50 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 .** an error is
2fd60 74 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 to use this func
2fd70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 tion..**.** If a
2fd80 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 68 nother thread ch
2fd90 61 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f anges the autoco
2fda0 6d 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 mmit status of t
2fdb0 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 he database.** c
2fdc0 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 onnection while
2fdd0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 this routine is
2fde0 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 running, then th
2fdf0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a e return value.*
2fe00 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a * is undefined..
2fe10 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e **.** Requiremen
2fe20 74 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 ts: [H12931] [H1
2fe30 32 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 2932] [H12933] [
2fe40 48 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 H12934].*/.SQLIT
2fe50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
2fe60 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 3_get_autocommit
2fe70 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a (sqlite3*);../*.
2fe80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e ** CAPI3REF: Fin
2fe90 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 d The Database H
2fea0 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 andle Of A Prepa
2feb0 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 red Statement {H
2fec0 31 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 13120} <S60600>.
2fed0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
2fee0 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 3_db_handle inte
2fef0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 rface returns th
2ff00 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e e [database conn
2ff10 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a ection] handle.*
2ff20 2a 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 * to which a [pr
2ff30 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
2ff40 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 ] belongs. The
2ff50 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
2ff60 74 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 tion].** returne
2ff70 64 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f d by sqlite3_db_
2ff80 68 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 handle is the sa
2ff90 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e me [database con
2ffa0 6e 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 nection] that wa
2ffb0 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 s the first argu
2ffc0 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b ment.** to the [
2ffd0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f sqlite3_prepare_
2ffe0 76 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 v2()] call (or i
2fff0 74 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 ts variants) tha
30000 74 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a t was used to.**
30010 20 63 72 65 61 74 65 20 74 68 65 20 73 74 61 74 create the stat
30020 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 ement in the fir
30030 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 st place..**.**
30040 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 Requirements: [H
30050 31 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 13123].*/.SQLITE
30060 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 _API sqlite3 *sq
30070 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 lite3_db_handle(
30080 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a sqlite3_stmt*);.
30090 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
300a0 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 Find the next p
300b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
300c0 74 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 t {H13140} <S606
300d0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 00>.**.** This i
300e0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
300f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
30100 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 e next [prepared
30110 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 statement] afte
30120 72 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 r.** pStmt assoc
30130 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b iated with the [
30140 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
30150 69 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 ion] pDb. If pS
30160 74 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 tmt is NULL.** t
30170 68 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 hen this interfa
30180 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 ce returns a poi
30190 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 nter to the firs
301a0 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 t prepared state
301b0 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 ment.** associat
301c0 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 ed with the data
301d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
301e0 70 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 pDb. If no prep
301f0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ared statement.*
30200 2a 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 * satisfies the
30210 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 conditions of th
30220 69 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 is routine, it r
30230 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a eturns NULL..**.
30240 2a 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 ** The [database
30250 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 connection] poi
30260 6e 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c nter D in a call
30270 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f to.** [sqlite3_
30280 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 next_stmt(D,S)]
30290 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e must refer to an
302a0 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a open database.*
302b0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 * connection and
302c0 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d in particular m
302d0 75 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c ust not be a NUL
302e0 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a L pointer..**.**
302f0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b Requirements: [
30300 48 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d H13143] [H13146]
30310 20 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 [H13149] [H1315
30320 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 2].*/.SQLITE_API
30330 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 sqlite3_stmt *s
30340 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 qlite3_next_stmt
30350 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 (sqlite3 *pDb, s
30360 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
30370 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 mt);../*.** CAPI
30380 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 3REF: Commit And
30390 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 Rollback Notifi
303a0 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 cation Callbacks
303b0 20 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 {H12950} <S6040
303c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 0>.**.** The sql
303d0 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
303e0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 () interface reg
303f0 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 isters a callbac
30400 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f k.** function to
30410 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e be invoked when
30420 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 ever a transacti
30430 6f 6e 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 on is [COMMIT |
30440 63 6f 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 committed]..** A
30450 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 ny callback set
30460 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 by a previous ca
30470 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f ll to sqlite3_co
30480 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 mmit_hook().** f
30490 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 or the same data
304a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
304b0 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a is overridden..*
304c0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f * The sqlite3_ro
304d0 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e llback_hook() in
304e0 74 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 terface register
304f0 73 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 s a callback.**
30500 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 function to be i
30510 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 nvoked whenever
30520 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 a transaction is
30530 20 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c [ROLLBACK | rol
30540 6c 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e led back]..** An
30550 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 y callback set b
30560 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c y a previous cal
30570 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d l to sqlite3_com
30580 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f mit_hook().** fo
30590 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 r the same datab
305a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 ase connection i
305b0 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a s overridden..**
305c0 20 54 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 The pArg argume
305d0 6e 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72 nt is passed thr
305e0 6f 75 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c ough to the call
305f0 62 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 back..** If the
30600 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f callback on a co
30610 6d 6d 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 mmit hook functi
30620 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a on returns non-z
30630 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ero,.** then the
30640 20 63 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 commit is conve
30650 72 74 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c rted into a roll
30660 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 back..**.** If a
30670 6e 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 nother function
30680 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 was previously r
30690 65 67 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a egistered, its.*
306a0 2a 20 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 * pArg value is
306b0 72 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 returned. Other
306c0 77 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 wise NULL is ret
306d0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 urned..**.** The
306e0 20 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d callback implem
306f0 65 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f entation must no
30700 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 t do anything th
30710 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a at will modify.*
30720 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 * the database c
30730 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 onnection that i
30740 6e 76 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 nvoked the callb
30750 61 63 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e ack. Any action
30760 73 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 s.** to modify t
30770 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
30780 65 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 ection must be d
30790 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 eferred until af
307a0 74 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c ter the.** compl
307b0 65 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 etion of the [sq
307c0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 lite3_step()] ca
307d0 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 ll that triggere
307e0 64 20 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 d the commit.**
307f0 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b or rollback hook
30800 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c in the first pl
30810 61 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ace..** Note tha
30820 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 t [sqlite3_prepa
30830 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 re_v2()] and [sq
30840 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f lite3_step()] bo
30850 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a th modify their.
30860 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e ** database conn
30870 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 ections for the
30880 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 meaning of "modi
30890 66 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 fy" in this para
308a0 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 graph..**.** Reg
308b0 69 73 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 istering a NULL
308c0 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 function disable
308d0 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a s the callback..
308e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 **.** When the c
308f0 6f 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 ommit hook callb
30900 61 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 ack routine retu
30910 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 rns zero, the [C
30920 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 OMMIT].** operat
30930 69 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 ion is allowed t
30940 6f 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 o continue norma
30950 6c 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d lly. If the com
30960 6d 69 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 mit hook.** retu
30970 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 rns non-zero, th
30980 65 6e 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 en the [COMMIT]
30990 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 is converted int
309a0 6f 20 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a o a [ROLLBACK]..
309b0 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** The rollback
309c0 68 6f 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 hook is invoked
309d0 6f 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 on a rollback th
309e0 61 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 at results from
309f0 61 20 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b a commit.** hook
30a00 20 72 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a returning non-z
30a10 65 72 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 ero, just as it
30a20 77 6f 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e would be with an
30a30 79 20 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b y other rollback
30a40 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 ..**.** For the
30a50 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 purposes of this
30a60 20 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 API, a transact
30a70 69 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 ion is said to h
30a80 61 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c ave been.** roll
30a90 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 ed back if an ex
30aa0 70 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b plicit "ROLLBACK
30ab0 22 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 " statement is e
30ac0 78 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 xecuted, or.** a
30ad0 6e 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 n error or const
30ae0 72 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 raint causes an
30af0 69 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 implicit rollbac
30b00 6b 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 k to occur..** T
30b10 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c he rollback call
30b20 62 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f back is not invo
30b30 6b 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 ked if a transac
30b40 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d tion is.** autom
30b50 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 atically rolled
30b60 62 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 back because the
30b70 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
30b80 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a tion is closed..
30b90 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** The rollback
30ba0 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 callback is not
30bb0 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 invoked if a tra
30bc0 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 nsaction is.** r
30bd0 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 olled back becau
30be0 73 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c se a commit call
30bf0 62 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f back returned no
30c00 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f n-zero..** <todo
30c10 3e 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 > Check on this
30c20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 </todo>.**.** Se
30c30 65 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 e also the [sqli
30c40 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 te3_update_hook(
30c50 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a )] interface..**
30c60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 .** Requirements
30c70 3a 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 :.** [H12951] [H
30c80 31 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 12952] [H12953]
30c90 5b 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 [H12954] [H12955
30ca0 5d 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 ].** [H12961] [H
30cb0 31 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 12962] [H12963]
30cc0 5b 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 [H12964].*/.SQLI
30cd0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c TE_API void *sql
30ce0 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b ite3_commit_hook
30cf0 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a (sqlite3*, int(*
30d00 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 )(void*), void*)
30d10 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 ;.SQLITE_API voi
30d20 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 d *sqlite3_rollb
30d30 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 ack_hook(sqlite3
30d40 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 *, void(*)(void
30d50 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a *), void*);../*.
30d60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 ** CAPI3REF: Dat
30d70 61 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 a Change Notific
30d80 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 ation Callbacks
30d90 7b 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 {H12970} <S60400
30da0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 >.**.** The sqli
30db0 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 te3_update_hook(
30dc0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 ) interface regi
30dd0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b sters a callback
30de0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 function.** wit
30df0 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 h the [database
30e00 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e connection] iden
30e10 74 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 tified by the fi
30e20 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 rst argument.**
30e30 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 to be invoked wh
30e40 65 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 enever a row is
30e50 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 updated, inserte
30e60 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a d or deleted..**
30e70 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 Any callback se
30e80 74 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 t by a previous
30e90 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e call to this fun
30ea0 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 ction.** for the
30eb0 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 same database c
30ec0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 onnection is ove
30ed0 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 rridden..**.** T
30ee0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 he second argume
30ef0 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 nt is a pointer
30f00 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 to the function
30f10 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 to invoke when a
30f20 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 .** row is updat
30f30 65 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 ed, inserted or
30f40 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 deleted..** The
30f50 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 first argument t
30f60 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 o the callback i
30f70 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 s a copy of the
30f80 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a third argument.*
30f90 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 * to sqlite3_upd
30fa0 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 ate_hook()..** T
30fb0 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 he second callba
30fc0 63 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f ck argument is o
30fd0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e ne of [SQLITE_IN
30fe0 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 SERT], [SQLITE_D
30ff0 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 ELETE],.** or [S
31000 51 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 QLITE_UPDATE], d
31010 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
31020 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 operation that c
31030 61 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 aused the callba
31040 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f ck.** to be invo
31050 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 ked..** The thir
31060 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 d and fourth arg
31070 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 uments to the ca
31080 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 llback contain p
31090 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a ointers to the.*
310a0 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 * database and t
310b0 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 able name contai
310c0 6e 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 ning the affecte
310d0 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 d row..** The fi
310e0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 nal callback par
310f0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 ameter is the [r
31100 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 owid] of the row
31110 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 ..** In the case
31120 20 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 of an update, t
31130 68 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 his is the [rowi
31140 64 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 d] after the upd
31150 61 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e ate takes place.
31160 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 .**.** The updat
31170 65 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e e hook is not in
31180 76 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 voked when inter
31190 6e 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 nal system table
311a0 73 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 s are.** modifie
311b0 64 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d d (i.e. sqlite_m
311c0 61 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 aster and sqlite
311d0 5f 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a _sequence)..**.*
311e0 2a 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 * In the current
311f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c implementation,
31200 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b the update hook
31210 0a 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b .** is not invok
31220 65 64 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 ed when duplicat
31230 69 6f 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c ion rows are del
31240 65 74 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 eted because of
31250 61 6e 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 an.** [ON CONFLI
31260 43 54 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 CT | ON CONFLICT
31270 20 52 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 REPLACE] clause
31280 2e 20 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 . Nor is the up
31290 64 61 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 date hook.** inv
312a0 6f 6b 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 oked when rows a
312b0 72 65 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 re deleted using
312c0 20 74 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f the [truncate o
312d0 70 74 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a ptimization]..**
312e0 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 The exceptions
312f0 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 defined in this
31300 70 61 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 paragraph might
31310 63 68 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 change in a futu
31320 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 re.** release of
31330 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 SQLite..**.** T
31340 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 he update hook i
31350 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 mplementation mu
31360 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 st not do anythi
31370 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 ng that will mod
31380 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 ify.** the datab
31390 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ase connection t
313a0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 hat invoked the
313b0 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e update hook. An
313c0 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 y actions.** to
313d0 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 modify the datab
313e0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d ase connection m
313f0 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 ust be deferred
31400 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a until after the.
31410 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 ** completion of
31420 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
31430 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 ep()] call that
31440 74 72 69 67 67 65 72 65 64 20 74 68 65 20 75 70 triggered the up
31450 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f date hook..** No
31460 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 te that [sqlite3
31470 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 _prepare_v2()] a
31480 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 nd [sqlite3_step
31490 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 ()] both modify
314a0 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 their.** databas
314b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f e connections fo
314c0 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 r the meaning of
314d0 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 "modify" in thi
314e0 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a s paragraph..**.
314f0 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 ** If another fu
31500 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 nction was previ
31510 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 ously registered
31520 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 , its pArg value
31530 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e .** is returned.
31540 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
31550 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a is returned..**
31560 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 .** See also the
31570 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 [sqlite3_commit
31580 5f 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 _hook()] and [sq
31590 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 lite3_rollback_h
315a0 6f 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 ook()].** interf
315b0 61 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 aces..**.** Requ
315c0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 irements:.** [H1
315d0 32 39 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 2971] [H12973] [
315e0 48 31 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d H12975] [H12977]
315f0 20 5b 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 [H12979] [H1298
31600 31 5d 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 1] [H12983] [H12
31610 39 38 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 986].*/.SQLITE_A
31620 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 PI void *sqlite3
31630 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 _update_hook(.
31640 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 sqlite3*, . voi
31650 64 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 d(*)(void *,int
31660 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 ,char const *,ch
31670 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 ar const *,sqlit
31680 65 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 e3_int64),. voi
31690 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 d*.);../*.** CAP
316a0 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 I3REF: Enable Or
316b0 20 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 Disable Shared
316c0 50 61 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 Pager Cache {H10
316d0 33 33 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 330} <S30900>.**
316e0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 KEYWORDS: {shar
316f0 65 64 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 ed cache}.**.**
31700 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 This routine ena
31710 62 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 bles or disables
31720 20 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 the sharing of
31730 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63 the database cac
31740 68 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 he.** and schema
31750 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 data structures
31760 20 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 between [databa
31770 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 se connection |
31780 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 connections].**
31790 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 to the same data
317a0 62 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 base. Sharing is
317b0 20 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 enabled if the
317c0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 argument is true
317d0 0a 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 .** and disabled
317e0 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 if the argument
317f0 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a is false..**.**
31800 20 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 Cache sharing i
31810 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 s enabled and di
31820 73 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e sabled for an en
31830 74 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a tire process..**
31840 20 54 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 This is a chang
31850 65 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 e as of SQLite v
31860 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e ersion 3.5.0. In
31870 20 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 prior versions
31880 6f 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 of SQLite,.** sh
31890 61 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 aring was enable
318a0 64 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f d or disabled fo
318b0 72 20 65 61 63 68 20 74 68 72 65 61 64 20 73 65 r each thread se
318c0 70 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 parately..**.**
318d0 54 68 65 20 63 61 63 68 65 20 73 68 61 72 69 6e The cache sharin
318e0 67 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 g mode set by th
318f0 69 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 is interface eff
31900 65 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 ects all subsequ
31910 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 ent.** calls to
31920 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d [sqlite3_open()]
31930 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f , [sqlite3_open_
31940 76 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 v2()], and [sqli
31950 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a te3_open16()]..*
31960 2a 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 * Existing datab
31970 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ase connections
31980 63 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 continue use the
31990 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a sharing mode.**
319a0 20 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 that was in eff
319b0 65 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 ect at the time
319c0 74 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 they were opened
319d0 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 ..**.** Virtual
319e0 74 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 tables cannot be
319f0 20 75 73 65 64 20 77 69 74 68 20 61 20 73 68 61 used with a sha
31a00 72 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e red cache. When
31a10 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 shared.** cache
31a20 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 is enabled, the
31a30 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 [sqlite3_create
31a40 5f 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 _module()] API u
31a50 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a sed to register.
31a60 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ** virtual table
31a70 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 s will always re
31a80 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a turn an error..*
31a90 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
31aa0 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 e returns [SQLIT
31ab0 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 E_OK] if shared
31ac0 63 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 cache was enable
31ad0 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a d or disabled.**
31ae0 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 successfully.
31af0 41 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 An [error code]
31b00 69 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 is returned othe
31b10 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 rwise..**.** Sha
31b20 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 red cache is dis
31b30 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 abled by default
31b40 2e 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 . But this might
31b50 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 change in.** fu
31b60 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 ture releases of
31b70 20 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 SQLite. Applic
31b80 61 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 ations that care
31b90 20 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a about shared.**
31ba0 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 cache setting s
31bb0 68 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 hould set it exp
31bc0 6c 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 licitly..**.** S
31bd0 65 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 ee Also: [SQLit
31be0 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d e Shared-Cache M
31bf0 6f 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ode].**.** Requi
31c00 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 rements: [H10331
31c10 5d 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 ] [H10336] [H103
31c20 33 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 37] [H10339].*/.
31c30 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
31c40 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 qlite3_enable_sh
31c50 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b ared_cache(int);
31c60 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
31c70 3a 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 : Attempt To Fre
31c80 65 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 e Heap Memory {H
31c90 31 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 17340} <S30220>.
31ca0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 **.** The sqlite
31cb0 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 3_release_memory
31cc0 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 () interface att
31cd0 65 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 empts to free N
31ce0 62 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 bytes.** of heap
31cf0 20 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c memory by deall
31d00 6f 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 ocating non-esse
31d10 6e 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c ntial memory all
31d20 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 ocations.** held
31d30 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 by the database
31d40 20 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 library. {END}
31d50 20 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 Memory used to
31d60 63 61 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a cache database.*
31d70 2a 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f * pages to impro
31d80 76 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 ve performance i
31d90 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 s an example of
31da0 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 non-essential me
31db0 6d 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 mory..** sqlite3
31dc0 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 _release_memory(
31dd0 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 ) returns the nu
31de0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 mber of bytes ac
31df0 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a tually freed,.**
31e00 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 which might be
31e10 6d 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 more or less tha
31e20 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 n the amount req
31e30 75 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 uested..**.** Re
31e40 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 quirements: [H17
31e50 33 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 341] [H17342].*/
31e60 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
31e70 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f sqlite3_release_
31e80 6d 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a memory(int);../*
31e90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d .** CAPI3REF: Im
31ea0 70 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 pose A Limit On
31eb0 48 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 Heap Size {H1735
31ec0 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 0} <S30220>.**.*
31ed0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f * The sqlite3_so
31ee0 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 ft_heap_limit()
31ef0 69 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 interface places
31f00 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a a "soft" limit.
31f10 2a 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 ** on the amount
31f20 20 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 of heap memory
31f30 74 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f that may be allo
31f40 63 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e cated by SQLite.
31f50 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e .** If an intern
31f60 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 al allocation is
31f70 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 20 requested that
31f80 77 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 would exceed the
31f90 0a 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 .** soft heap li
31fa0 6d 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 mit, [sqlite3_re
31fb0 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 lease_memory()]
31fc0 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f is invoked one o
31fd0 72 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 r.** more times
31fe0 74 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 to free up some
31ff0 73 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 space before the
32000 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 allocation is p
32010 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 erformed..**.**
32020 54 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c The limit is cal
32030 6c 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 led "soft", beca
32040 75 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f use if [sqlite3_
32050 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 release_memory()
32060 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 ].** cannot free
32070 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f sufficient memo
32080 72 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 ry to prevent th
32090 65 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 e limit from bei
320a0 6e 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 ng exceeded,.**
320b0 74 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c the memory is al
320c0 6c 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 located anyway a
320d0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f nd the current o
320e0 70 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 peration proceed
320f0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 s..**.** A negat
32100 69 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 ive or zero valu
32110 65 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 e for N means th
32120 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 at there is no s
32130 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 oft heap limit a
32140 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 nd.** [sqlite3_r
32150 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d elease_memory()]
32160 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 will only be ca
32170 6c 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 lled when memory
32180 20 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a is exhausted..*
32190 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 * The default va
321a0 6c 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 lue for the soft
321b0 20 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a heap limit is z
321c0 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ero..**.** SQLit
321d0 65 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 e makes a best e
321e0 66 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 ffort to honor t
321f0 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d he soft heap lim
32200 69 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 it..** But if th
32210 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 e soft heap limi
32220 74 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f t cannot be hono
32230 72 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 red, execution w
32240 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 ill.** continue
32250 77 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 without error or
32260 20 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 notification.
32270 54 68 69 73 20 69 73 20 77 68 79 20 74 68 65 20 This is why the
32280 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c limit is.** call
32290 65 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 ed a "soft" limi
322a0 74 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 6f t. It is adviso
322b0 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 ry only..**.** P
322c0 72 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 rior to SQLite v
322d0 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 ersion 3.5.0, th
322e0 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
322f0 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 constrained the
32300 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 memory.** alloca
32310 74 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 ted by a single
32320 74 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d thread - the sam
32330 65 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 e thread in whic
32340 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a h this routine.*
32350 2a 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 * runs. Beginni
32360 6e 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 ng with SQLite v
32370 65 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 ersion 3.5.0, th
32380 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 e soft heap limi
32390 74 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 t is.** applied
323a0 74 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 to all threads.
323b0 54 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 The value specif
323c0 69 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 ied for the soft
323d0 20 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 heap limit.** i
323e0 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 s an upper bound
323f0 20 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 on the total me
32400 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
32410 66 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e for all threads.
32420 20 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 In.** version 3
32430 2e 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f .5.0 there is no
32440 20 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c mechanism for l
32450 69 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 imiting the heap
32460 20 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e usage for.** in
32470 64 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 dividual threads
32480 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d ..**.** Requirem
32490 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 ents:.** [H16351
324a0 5d 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 ] [H16352] [H163
324b0 35 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 53] [H16354] [H1
324c0 36 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 6355] [H16358].*
324d0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 /.SQLITE_API voi
324e0 64 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 d sqlite3_soft_h
324f0 65 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a eap_limit(int);.
32500 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
32510 20 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 Extract Metadat
32520 61 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e a About A Column
32530 20 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 Of A Table {H12
32540 38 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 850} <S60300>.**
32550 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
32560 20 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 returns metadat
32570 61 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 a about a specif
32580 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 ic column of a s
32590 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 pecific.** datab
325a0 61 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 ase table access
325b0 69 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b ible using the [
325c0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
325d0 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 ion] handle.** p
325e0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 assed as the fir
325f0 73 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 st function argu
32600 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ment..**.** The
32610 63 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 column is identi
32620 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f fied by the seco
32630 6e 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f nd, third and fo
32640 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 urth parameters
32650 74 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 to.** this funct
32660 69 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 ion. The second
32670 70 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 parameter is eit
32680 68 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 her the name of
32690 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
326a0 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 (i.e. "main", "t
326b0 65 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 emp" or an attac
326c0 68 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f hed database) co
326d0 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 ntaining the spe
326e0 63 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 cified.** table
326f0 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 or NULL. If it i
32700 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c s NULL, then all
32710 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 attached databa
32720 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 ses are searched
32730 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c .** for the tabl
32740 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 e using the same
32750 20 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 algorithm used
32760 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 by the database
32770 65 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 engine to.** res
32780 6f 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 olve unqualified
32790 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 table reference
327a0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 s..**.** The thi
327b0 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 rd and fourth pa
327c0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 rameters to this
327d0 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 function are th
327e0 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 e table and colu
327f0 6d 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 mn.** name of th
32800 65 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e e desired column
32810 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 , respectively.
32820 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 Neither of these
32830 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d parameters.** m
32840 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a ay be NULL..**.*
32850 2a 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 * Metadata is re
32860 74 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e turned by writin
32870 67 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 g to the memory
32880 6c 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 locations passed
32890 20 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 as the 5th.** a
328a0 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 nd subsequent pa
328b0 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 rameters to this
328c0 20 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f function. Any o
328d0 66 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 f these argument
328e0 73 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c s may be.** NULL
328f0 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 , in which case
32900 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e the correspondin
32910 67 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 g element of met
32920 61 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 adata is omitted
32930 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 ..**.** <blockqu
32940 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 ote>.** <table b
32950 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 order="1">.** <t
32960 72 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 r><th> Parameter
32970 20 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e <th> Output<br>
32980 54 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 Type <th> Descr
32990 69 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 iption.**.** <tr
329a0 3e 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 ><td> 5th <td> c
329b0 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 onst char* <td>
329c0 44 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 Data type.** <tr
329d0 3e 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 ><td> 6th <td> c
329e0 6f 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 onst char* <td>
329f0 4e 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 Name of default
32a00 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e collation sequen
32a10 63 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 ce.** <tr><td> 7
32a20 74 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 th <td> int
32a30 20 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 <td> True if
32a40 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f column has a NO
32a50 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e T NULL constrain
32a60 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 t.** <tr><td> 8t
32a70 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 h <td> int
32a80 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 <td> True if
32a90 63 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f column is part o
32aa0 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 f the PRIMARY KE
32ab0 59 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 Y.** <tr><td> 9t
32ac0 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 h <td> int
32ad0 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 <td> True if
32ae0 63 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 column is [AUTOI
32af0 4e 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 NCREMENT].** </t
32b00 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b able>.** </block
32b10 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 quote>.**.** The
32b20 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 memory pointed
32b30 74 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63 to by the charac
32b40 74 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 ter pointers ret
32b50 75 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a urned for the.**
32b60 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 declaration typ
32b70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 e and collation
32b80 73 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 sequence is vali
32b90 64 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 d only until the
32ba0 20 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f next.** call to
32bb0 20 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 any SQLite API
32bc0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
32bd0 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 If the specified
32be0 20 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c table is actual
32bf0 6c 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 ly a view, an [e
32c00 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
32c10 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
32c20 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 the specified c
32c30 6f 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 olumn is "rowid"
32c40 2c 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 , "oid" or "_row
32c50 69 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b id_" and an.** [
32c60 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 INTEGER PRIMARY
32c70 4b 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 KEY] column has
32c80 62 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 been explicitly
32c90 64 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 declared, then t
32ca0 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 he output.** par
32cb0 61 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 ameters are set
32cc0 66 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 for the explicit
32cd0 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 ly declared colu
32ce0 6d 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 mn. If there is
32cf0 6e 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 no.** explicitly
32d00 20 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 declared [INTEG
32d10 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 ER PRIMARY KEY]
32d20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 column, then the
32d30 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d output.** param
32d40 65 74 65 72 73 20 61 72 65 20 73 65 74 20 61 73 eters are set as
32d50 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 follows:.**.**
32d60 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 <pre>.** dat
32d70 61 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 a type: "INTEGER
32d80 22 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 ".** collati
32d90 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 on sequence: "BI
32da0 4e 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 NARY".** not
32db0 20 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 null: 0.**
32dc0 70 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a primary key: 1.*
32dd0 2a 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 * auto incre
32de0 6d 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 ment: 0.** </pre
32df0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e >.**.** This fun
32e00 63 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f ction may load o
32e10 6e 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d ne or more schem
32e20 61 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 as from database
32e30 20 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a files. If an.**
32e40 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 error occurs du
32e50 72 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 ring this proces
32e60 73 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 s, or if the req
32e70 75 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 uested table or
32e80 63 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 column.** cannot
32e90 20 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 be found, an [e
32ea0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 rror code] is re
32eb0 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 turned and an er
32ec0 72 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 ror message left
32ed0 0a 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 .** in the [data
32ee0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d base connection]
32ef0 20 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 (to be retrieve
32f00 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f d using sqlite3_
32f10 65 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a errmsg())..**.**
32f20 20 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c This API is onl
32f30 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 y available if t
32f40 68 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 he library was c
32f50 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 ompiled with the
32f60 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 .** [SQLITE_ENAB
32f70 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
32f80 54 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 TA] C-preprocess
32f90 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 or symbol define
32fa0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 d..*/.SQLITE_API
32fb0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 int sqlite3_tab
32fc0 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 le_column_metada
32fd0 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ta(. sqlite3 *d
32fe0 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b,
32ff0 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 /* Connection
33000 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
33010 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c t char *zDbName,
33020 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 /* Datab
33030 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c ase name or NULL
33040 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
33050 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 *zTableName,
33060 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 /* Table name
33070 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
33080 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 *zColumnName,
33090 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 /* Column name
330a0 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 */. char const
330b0 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 **pzDataType,
330c0 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c /* OUTPUT: Decl
330d0 61 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a ared data type *
330e0 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a /. char const *
330f0 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 *pzCollSeq,
33100 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 /* OUTPUT: Colla
33110 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 tion sequence na
33120 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f me */. int *pNo
33130 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 tNull,
33140 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 /* OUTPUT: T
33150 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 rue if NOT NULL
33160 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 constraint exist
33170 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 s */. int *pPri
33180 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 maryKey,
33190 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 /* OUTPUT: Tr
331a0 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 ue if column par
331b0 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 t of PK */. int
331c0 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 *pAutoinc
331d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 /* OUTP
331e0 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 UT: True if colu
331f0 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 mn is auto-incre
33200 6d 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a ment */.);../*.*
33210 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 * CAPI3REF: Load
33220 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 An Extension {H
33230 31 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 12600} <S20500>.
33240 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
33250 66 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 face loads an SQ
33260 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c Lite extension l
33270 69 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 ibrary from the
33280 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a named file..**.*
33290 2a 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 * {H12601} The s
332a0 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 qlite3_load_exte
332b0 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 nsion() interfac
332c0 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f e attempts to lo
332d0 61 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 ad an.**
332e0 20 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 SQLite extensi
332f0 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 on library conta
33300 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 ined in the file
33310 20 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 zFile..**.** {H
33320 31 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 12602} The entry
33330 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e point is zProc.
33340 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 .**.** {H12603}
33350 7a 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 zProc may be 0,
33360 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
33370 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e e name of the en
33380 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 try point.**
33390 20 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 defaults t
333a0 6f 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e o "sqlite3_exten
333b0 73 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a sion_init"..**.*
333c0 2a 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 * {H12604} The s
333d0 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 qlite3_load_exte
333e0 6e 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 nsion() interfac
333f0 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a e shall return.*
33400 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 * [SQLI
33410 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 TE_OK] on succes
33420 73 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 s and [SQLITE_ER
33430 52 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e ROR] if somethin
33440 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a g goes wrong..**
33450 0a 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 .** {H12605} If
33460 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
33470 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 and pzErrMsg is
33480 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a not 0, then the.
33490 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c ** [sql
334a0 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 ite3_load_extens
334b0 69 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 ion()] interface
334c0 20 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 shall attempt t
334d0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 o.** fi
334e0 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 ll *pzErrMsg wit
334f0 68 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 h error message
33500 74 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d text stored in m
33510 65 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 emory.**
33520 20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 obtained from
33530 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
33540 29 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 )]. {END} The c
33550 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a alling function.
33560 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 ** shou
33570 6c 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d ld free this mem
33580 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b ory by calling [
33590 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e sqlite3_free()].
335a0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 .**.** {H12606}
335b0 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e Extension loadin
335c0 67 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 g must be enable
335d0 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 d using.**
335e0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 [sqlite3_ena
335f0 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 ble_load_extensi
33600 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 on()] prior to c
33610 61 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c alling this API,
33620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 .** oth
33630 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 erwise an error
33640 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 will be returned
33650 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
33660 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 int sqlite3_load
33670 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 _extension(. sq
33680 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
33690 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 /* Load the
336a0 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 extension into t
336b0 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e his database con
336c0 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e nection */. con
336d0 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 st char *zFile,
336e0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
336f0 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 e shared library
33700 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 containing exte
33710 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 nsion */. const
33720 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 char *zProc,
33730 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e /* Entry point.
33740 20 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a Derived from z
33750 46 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 File if 0 */. c
33760 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 har **pzErrMsg
33770 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f /* Put erro
33780 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 r message here i
33790 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f f not 0 */.);../
337a0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 *.** CAPI3REF: E
337b0 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 nable Or Disable
337c0 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 Extension Loadi
337d0 6e 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 ng {H12620} <S20
337e0 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 500>.**.** So as
337f0 20 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 not to open sec
33800 75 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f urity holes in o
33810 6c 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e lder application
33820 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e s that are.** un
33830 70 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c prepared to deal
33840 20 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 with extension
33850 6c 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 loading, and as
33860 61 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 a means of disab
33870 6c 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f ling.** extensio
33880 6e 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 n loading while
33890 65 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d evaluating user-
338a0 65 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 entered SQL, the
338b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a following API.*
338c0 2a 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f * is provided to
338d0 20 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 turn the [sqlit
338e0 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f e3_load_extensio
338f0 6e 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f n()] mechanism o
33900 6e 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a n and off..**.**
33910 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 Extension loadi
33920 6e 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 ng is off by def
33930 61 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 ault. See ticket
33940 20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 #1863..**.** {H
33950 31 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 12621} Call the
33960 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c sqlite3_enable_l
33970 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 oad_extension()
33980 72 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f routine with ono
33990 66 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 ff==1.**
339a0 20 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 to turn extens
339b0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 ion loading on a
339c0 6e 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 nd call it with
339d0 6f 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e onoff==0 to turn
339e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 .** it
339f0 62 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a back off again..
33a00 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 **.** {H12622} E
33a10 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 xtension loading
33a20 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 is off by defau
33a30 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 lt..*/.SQLITE_AP
33a40 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e I int sqlite3_en
33a50 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 able_load_extens
33a60 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ion(sqlite3 *db,
33a70 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a int onoff);../*
33a80 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 .** CAPI3REF: Au
33a90 74 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 tomatically Load
33aa0 20 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b An Extensions {
33ab0 48 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e H12640} <S20500>
33ac0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 .**.** This API
33ad0 63 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 can be invoked a
33ae0 74 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 t program startu
33af0 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 p in order to re
33b00 67 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 gister.** one or
33b10 20 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 more statically
33b20 20 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f linked extensio
33b30 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ns that will be
33b40 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 available.** to
33b50 61 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 all new [databas
33b60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 e connections].
33b70 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 {END}.**.** This
33b80 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 routine stores
33b90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
33ba0 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e extension in an
33bb0 20 61 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a array that is.*
33bc0 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 * obtained from
33bd0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 [sqlite3_malloc(
33be0 29 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 )]. If you run
33bf0 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 a memory leak ch
33c00 65 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 ecker.** on your
33c10 20 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 program and it
33c20 72 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 reports a leak b
33c30 65 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 ecause of this a
33c40 72 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 rray, invoke.**
33c50 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 [sqlite3_reset_a
33c60 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d uto_extension()]
33c70 20 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f prior to shutdo
33c80 77 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d wn to free the m
33c90 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 emory..**.** {H1
33ca0 32 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 2641} This funct
33cb0 69 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e ion registers an
33cc0 20 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 extension entry
33cd0 20 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a point that is.*
33ce0 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d * autom
33cf0 61 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 atically invoked
33d00 20 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 whenever a new
33d10 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
33d20 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 tion].**
33d30 20 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e is opened usin
33d40 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 g [sqlite3_open(
33d50 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 )], [sqlite3_ope
33d60 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 n16()],.**
33d70 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f or [sqlite3_
33d80 6f 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a open_v2()]..**.*
33d90 2a 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 * {H12642} Dupli
33da0 63 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 cate extensions
33db0 61 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 are detected so
33dc0 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 calling this rou
33dd0 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 tine.**
33de0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 multiple times
33df0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 with the same ex
33e00 74 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c tension is harml
33e10 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ess..**.** {H126
33e20 34 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 43} This routine
33e30 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 stores a pointe
33e40 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 r to the extensi
33e50 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a on in an array.*
33e60 2a 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 * that
33e70 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d is obtained from
33e80 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 [sqlite3_malloc
33e90 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 ()]..**.** {H126
33ea0 34 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 44} Automatic ex
33eb0 74 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 tensions apply a
33ec0 63 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 cross all thread
33ed0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
33ee0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 int sqlite3_aut
33ef0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 o_extension(void
33f00 20 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 (*xEntryPoint)(
33f10 76 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 void));../*.** C
33f20 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 API3REF: Reset A
33f30 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 utomatic Extensi
33f40 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 on Loading {H126
33f50 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 60} <S20500>.**.
33f60 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
33f70 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 disables all pr
33f80 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 eviously registe
33f90 72 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a red automatic.**
33fa0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e extensions. {EN
33fb0 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 D} It undoes th
33fc0 65 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 e effect of all
33fd0 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 prior.** [sqlite
33fe0 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 3_auto_extension
33ff0 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a ()] calls..**.**
34000 20 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 {H12661} This f
34010 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 unction disables
34020 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 all previously
34030 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 registered.**
34040 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 automatic
34050 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a extensions..**.
34060 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 ** {H12662} This
34070 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c function disabl
34080 65 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 es automatic ext
34090 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 ensions in all t
340a0 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 hreads..*/.SQLIT
340b0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
340c0 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 e3_reset_auto_ex
340d0 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a tension(void);..
340e0 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 /*.****** EXPERI
340f0 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 MENTAL - subject
34100 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f to change witho
34110 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a ut notice ******
34120 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
34130 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 he interface to
34140 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c the virtual-tabl
34150 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 e mechanism is c
34160 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 urrently conside
34170 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 red.** to be exp
34180 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 erimental. The
34190 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 interface might
341a0 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 change in incomp
341b0 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 atible ways..**
341c0 49 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f If this is a pro
341d0 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f blem for you, do
341e0 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 not use the int
341f0 65 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 erface at this t
34200 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 ime..**.** When
34210 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c the virtual-tabl
34220 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 e mechanism stab
34230 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 ilizes, we will
34240 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 declare the.** i
34250 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 nterface fixed,
34260 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 support it indef
34270 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d initely, and rem
34280 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 ove this comment
34290 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 ..*/../*.** Stru
342a0 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 ctures used by t
342b0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
342c0 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 interface.*/.ty
342d0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
342e0 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 ite3_vtab sqlite
342f0 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 3_vtab;.typedef
34300 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
34310 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 ndex_info sqlite
34320 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 3_index_info;.ty
34330 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
34340 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 ite3_vtab_cursor
34350 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 sqlite3_vtab_cu
34360 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 rsor;.typedef st
34370 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 ruct sqlite3_mod
34380 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 ule sqlite3_modu
34390 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 le;../*.** CAPI3
343a0 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 REF: Virtual Tab
343b0 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 le Object {H1800
343c0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 0} <S20400>.** K
343d0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 EYWORDS: sqlite3
343e0 5f 6d 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c _module {virtual
343f0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a table module}.*
34400 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a * EXPERIMENTAL.*
34410 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 *.** This struct
34420 75 72 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 ure, sometimes c
34430 61 6c 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 alled a a "virtu
34440 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 al table module"
34450 2c 20 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 , .** defines th
34460 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
34470 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 of a [virtual t
34480 61 62 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 ables]. .** Thi
34490 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 s structure cons
344a0 69 73 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d ists mostly of m
344b0 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d ethods for the m
344c0 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 odule..**.** A v
344d0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 irtual table mod
344e0 75 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 62 ule is created b
344f0 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 y filling in a p
34500 65 72 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 ersistent.** ins
34510 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
34520 72 75 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 ructure and pass
34530 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f ing a pointer to
34540 20 74 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a that instance.*
34550 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 * to [sqlite3_cr
34560 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f eate_module()] o
34570 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 r [sqlite3_creat
34580 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a e_module_v2()]..
34590 2a 2a 20 54 68 65 20 72 65 67 69 73 74 72 61 74 ** The registrat
345a0 69 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 ion remains vali
345b0 64 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 d until it is re
345c0 70 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66 placed by a diff
345d0 65 72 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 erent.** module
345e0 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 or until the [da
345f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
34600 6e 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 n] closes. The
34610 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 content.** of th
34620 69 73 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 is structure mus
34630 74 20 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 t not change whi
34640 6c 65 20 69 74 20 69 73 20 72 65 67 69 73 74 65 le it is registe
34650 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 red with.** any
34660 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
34670 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 ion..*/.struct s
34680 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a qlite3_module {.
34690 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a int iVersion;.
346a0 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 int (*xCreate)
346b0 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 (sqlite3*, void
346c0 2a 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 *pAux,.
346d0 20 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 int argc,
346e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 const char *cons
346f0 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 t*argv,.
34700 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 sqlite3_v
34710 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 tab **ppVTab, ch
34720 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 ar**);. int (*x
34730 43 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 Connect)(sqlite3
34740 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 *, void *pAux,.
34750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
34760 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 t argc, const ch
34770 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a ar *const*argv,.
34780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 s
34790 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 qlite3_vtab **pp
347a0 56 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 VTab, char**);.
347b0 20 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 int (*xBestInde
347c0 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 x)(sqlite3_vtab
347d0 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f *pVTab, sqlite3_
347e0 69 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 index_info*);.
347f0 69 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 int (*xDisconnec
34800 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 t)(sqlite3_vtab
34810 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 *pVTab);. int (
34820 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 *xDestroy)(sqlit
34830 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b e3_vtab *pVTab);
34840 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 . int (*xOpen)(
34850 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
34860 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 Tab, sqlite3_vta
34870 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 b_cursor **ppCur
34880 73 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 sor);. int (*xC
34890 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 lose)(sqlite3_vt
348a0 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 ab_cursor*);. i
348b0 6e 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 nt (*xFilter)(sq
348c0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
348d0 72 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 r*, int idxNum,
348e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 const char *idxS
348f0 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 tr,.
34900 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 int argc, sq
34910 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 lite3_value **ar
34920 67 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 gv);. int (*xNe
34930 78 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 xt)(sqlite3_vtab
34940 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 _cursor*);. int
34950 20 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 (*xEof)(sqlite3
34960 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a _vtab_cursor*);.
34970 20 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 int (*xColumn)
34980 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 (sqlite3_vtab_cu
34990 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 rsor*, sqlite3_c
349a0 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 ontext*, int);.
349b0 20 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 int (*xRowid)(s
349c0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 qlite3_vtab_curs
349d0 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 or*, sqlite3_int
349e0 36 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 64 *pRowid);. i
349f0 6e 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 nt (*xUpdate)(sq
34a00 6c 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e lite3_vtab *, in
34a10 74 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 t, sqlite3_value
34a20 20 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 **, sqlite3_int
34a30 36 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 64 *);. int (*x
34a40 42 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 Begin)(sqlite3_v
34a50 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 tab *pVTab);. i
34a60 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 nt (*xSync)(sqli
34a70 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 te3_vtab *pVTab)
34a80 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 ;. int (*xCommi
34a90 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 t)(sqlite3_vtab
34aa0 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 *pVTab);. int (
34ab0 2a 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 *xRollback)(sqli
34ac0 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 te3_vtab *pVTab)
34ad0 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 ;. int (*xFindF
34ae0 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 unction)(sqlite3
34af0 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e _vtab *pVtab, in
34b00 74 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 t nArg, const ch
34b10 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 ar *zName,.
34b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34b30 20 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 void (**pxFunc
34b40 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 )(sqlite3_contex
34b50 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 t*,int,sqlite3_v
34b60 61 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 alue**),.
34b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34b80 76 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 void **ppArg);.
34b90 20 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 int (*xRename)(
34ba0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
34bb0 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 tab, const char
34bc0 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a *zNew);.};../*.*
34bd0 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 * CAPI3REF: Virt
34be0 75 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 ual Table Indexi
34bf0 6e 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b ng Information {
34c00 48 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e H18100} <S20400>
34c10 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 .** KEYWORDS: sq
34c20 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
34c30 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c .** EXPERIMENTAL
34c40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 .**.** The sqlit
34c50 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 e3_index_info st
34c60 72 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 ructure and its
34c70 73 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 substructures is
34c80 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 used to.** pass
34c90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 information int
34ca0 6f 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 o and receive th
34cb0 65 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 e reply from the
34cc0 20 5b 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a [xBestIndex].**
34cd0 20 6d 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 method of a [vi
34ce0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 rtual table modu
34cf0 6c 65 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 le]. The fields
34d00 20 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a under **Inputs*
34d10 2a 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 * are the.** inp
34d20 75 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 uts to xBestInde
34d30 78 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f x and are read-o
34d40 6e 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 nly. xBestIndex
34d50 20 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 inserts its.**
34d60 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 results into the
34d70 20 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 **Outputs** fie
34d80 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 lds..**.** The a
34d90 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 Constraint[] arr
34da0 61 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 ay records WHERE
34db0 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 clause constrai
34dc0 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a nts of the form:
34dd0 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 .**.** <pre>colu
34de0 6d 6e 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e mn OP expr</pre>
34df0 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 .**.** where OP
34e00 69 73 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b is =, <, <
34e10 3d 2c 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b =, >, or >
34e20 3d 2e 20 20 54 68 65 20 70 61 72 74 69 63 75 6c =. The particul
34e30 61 72 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a ar operator is.*
34e40 2a 20 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e * stored in aCon
34e50 73 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 straint[].op. T
34e60 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 he index of the
34e70 63 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 column is stored
34e80 20 69 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 in.** aConstrai
34e90 6e 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 nt[].iColumn. a
34ea0 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 Constraint[].usa
34eb0 62 6c 65 20 69 73 20 54 52 55 45 20 69 66 20 74 ble is TRUE if t
34ec0 68 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 he.** expr on th
34ed0 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
34ee0 65 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 e can be evaluat
34ef0 65 64 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 ed (and thus the
34f00 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 constraint.** i
34f10 73 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 s usable) and fa
34f20 6c 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 lse if it cannot
34f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 ..**.** The opti
34f40 6d 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 mizer automatica
34f50 6c 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d lly inverts term
34f60 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 s of the form "e
34f70 78 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a xpr OP column".*
34f80 2a 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 * and makes othe
34f90 72 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e r simplification
34fa0 73 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 s to the WHERE c
34fb0 6c 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 lause in an atte
34fc0 6d 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 mpt to.** get as
34fd0 20 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 many WHERE clau
34fe0 73 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 se terms into th
34ff0 65 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f e form shown abo
35000 76 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a ve as possible..
35010 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 ** The aConstrai
35020 6e 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 nt[] array only
35030 72 65 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c reports WHERE cl
35040 61 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 ause terms in th
35050 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 e correct.** for
35060 6d 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 m that refer to
35070 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 the particular v
35080 69 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 irtual table bei
35090 6e 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a ng queried..**.*
350a0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 * Information ab
350b0 6f 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 out the ORDER BY
350c0 20 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 clause is store
350d0 64 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e d in aOrderBy[].
350e0 0a 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 .** Each term of
350f0 20 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 aOrderBy record
35100 73 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 s a column of th
35110 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
35120 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 e..**.** The [xB
35130 65 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 estIndex] method
35140 20 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 must fill aCons
35150 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 traintUsage[] wi
35160 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a th information.*
35170 2a 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 * about what par
35180 61 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 ameters to pass
35190 74 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 to xFilter. If
351a0 61 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e argvIndex>0 then
351b0 0a 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 .** the right-ha
351c0 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 nd side of the c
351d0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f orresponding aCo
351e0 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 nstraint[] is ev
351f0 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 aluated.** and b
35200 65 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 ecomes the argvI
35210 6e 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e ndex-th entry in
35220 20 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 argv. If aCons
35230 74 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d traintUsage[].om
35240 69 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 it.** is true, t
35250 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 hen the constrai
35260 6e 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f nt is assumed to
35270 20 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 be fully handle
35280 64 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 d by the.** virt
35290 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 ual table and is
352a0 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 not checked aga
352b0 69 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a in by SQLite..**
352c0 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 .** The idxNum a
352d0 6e 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 nd idxPtr values
352e0 20 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e are recorded an
352f0 64 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 d passed into th
35300 65 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d e.** [xFilter] m
35310 65 74 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 ethod..** [sqlit
35320 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 e3_free()] is us
35330 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 ed to free idxPt
35340 72 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 r if and only if
35350 66 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 f.** needToFreeI
35360 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a dxPtr is true..*
35370 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 *.** The orderBy
35380 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 Consumed means t
35390 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 hat output from
353a0 5b 78 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 [xFilter]/[xNext
353b0 5d 20 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a ] will occur in.
353c0 2a 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f ** the correct o
353d0 72 64 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 rder to satisfy
353e0 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
353f0 75 73 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 use so that no s
35400 65 70 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 eparate.** sorti
35410 6e 67 20 73 74 65 70 20 69 73 20 72 65 71 75 69 ng step is requi
35420 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 red..**.** The e
35430 73 74 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c stimatedCost val
35440 75 65 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 ue is an estimat
35450 65 20 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 e of the cost of
35460 20 64 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 doing the.** pa
35470 72 74 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e rticular lookup.
35480 20 20 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 A full scan of
35490 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 a table with N
354a0 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 entries should h
354b0 61 76 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 ave.** a cost of
354c0 20 4e 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 N. A binary se
354d0 61 72 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 arch of a table
354e0 6f 66 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f of N entries sho
354f0 75 6c 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f uld have a.** co
35500 73 74 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 st of approximat
35510 65 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 ely log(N)..*/.s
35520 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
35530 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 dex_info {. /*
35540 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 Inputs */. int
35550 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 nConstraint;
35560 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
35570 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 of entries in a
35580 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 Constraint */.
35590 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
355a0 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 ndex_constraint
355b0 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 {. int iColu
355c0 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 mn;
355d0 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 /* Column on le
355e0 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 ft-hand side of
355f0 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 constraint */.
35600 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 unsigned char
35610 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 op; /*
35620 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 Constraint opera
35630 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 tor */. unsi
35640 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 gned char usable
35650 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 ; /* True if
35660 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 this constraint
35670 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 is usable */.
35680 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 int iTermOffs
35690 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et; /*
356a0 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 Used internally
356b0 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f - xBestIndex sho
356c0 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 uld ignore */.
356d0 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 } *aConstraint;
356e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 /* Ta
356f0 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 ble of WHERE cla
35700 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 use constraints
35710 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 */. int nOrderB
35720 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
35730 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 /* Number of ter
35740 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 ms in the ORDER
35750 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 BY clause */. s
35760 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
35770 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 dex_orderby {.
35780 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 int iColumn;
35790 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
357a0 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f Column number */
357b0 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 . unsigned c
357c0 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 har desc;
357d0 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 /* True for DESC
357e0 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 . False for ASC
357f0 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 . */. } *aOrder
35800 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 By;
35810 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 /* The ORDER B
35820 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a Y clause */. /*
35830 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 Outputs */. st
35840 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 ruct sqlite3_ind
35850 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 ex_constraint_us
35860 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 age {. int ar
35870 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 gvIndex;
35880 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e /* if >0, con
35890 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 straint is part
358a0 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 of argv to xFilt
358b0 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e er */. unsign
358c0 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 ed char omit;
358d0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 /* Do not cod
358e0 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 e a test for thi
358f0 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a s constraint */.
35900 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 } *aConstraint
35910 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 Usage;. int idx
35920 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Num;
35930 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 /* Number us
35940 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 ed to identify t
35950 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 he index */. ch
35960 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 ar *idxStr;
35970 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 /* Stri
35980 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 ng, possibly obt
35990 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
359a0 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 e3_malloc */. i
359b0 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 nt needToFreeIdx
359c0 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 Str; /* Fre
359d0 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 e idxStr using s
359e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 qlite3_free() if
359f0 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f true */. int o
35a00 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 rderByConsumed;
35a10 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
35a20 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 output is alrea
35a30 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 dy ordered */.
35a40 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 double estimated
35a50 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 Cost; /* Es
35a60 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 timated cost of
35a70 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 using this index
35a80 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 */.};.#define S
35a90 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
35aa0 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 TRAINT_EQ 2.#
35ab0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
35ac0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 DEX_CONSTRAINT_G
35ad0 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 T 4.#define S
35ae0 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
35af0 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 TRAINT_LE 8.#
35b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e define SQLITE_IN
35b10 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c DEX_CONSTRAINT_L
35b20 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 T 16.#define
35b30 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e SQLITE_INDEX_CON
35b40 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 STRAINT_GE 32
35b50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
35b60 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 INDEX_CONSTRAINT
35b70 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a _MATCH 64../*.**
35b80 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 CAPI3REF: Regis
35b90 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 ter A Virtual Ta
35ba0 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 ble Implementati
35bb0 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 on {H18200} <S20
35bc0 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 400>.** EXPERIME
35bd0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 NTAL.**.** This
35be0 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
35bf0 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 to register a ne
35c00 77 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 w [virtual table
35c10 20 6d 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a module] name..*
35c20 2a 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d * Module names m
35c30 75 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 ust be registere
35c40 64 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 d before.** crea
35c50 74 69 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 ting a new [virt
35c60 75 61 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 ual table] using
35c70 20 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 the module, or
35c80 62 65 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a before using a.*
35c90 2a 20 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 * preexisting [v
35ca0 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f irtual table] fo
35cb0 72 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a r the module..**
35cc0 0a 2a 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e .** The module n
35cd0 61 6d 65 20 69 73 20 72 65 67 69 73 74 65 72 65 ame is registere
35ce0 64 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 d on the [databa
35cf0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 se connection] s
35d00 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 pecified.** by t
35d10 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
35d20 65 72 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 er. The name of
35d30 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 the module is g
35d40 69 76 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 iven by the .**
35d50 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 second parameter
35d60 2e 20 20 54 68 65 20 74 68 69 72 64 20 70 61 72 . The third par
35d70 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e ameter is a poin
35d80 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d ter to.** the im
35d90 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 plementation of
35da0 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 the [virtual tab
35db0 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 le module]. Th
35dc0 65 20 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 e fourth.** para
35dd0 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 meter is an arbi
35de0 74 72 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 trary client dat
35df0 61 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 a pointer that i
35e00 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 s passed through
35e10 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 .** into the [xC
35e20 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e reate] and [xCon
35e30 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 nect] methods of
35e40 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
35e50 6c 65 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 le module.** whe
35e60 6e 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 n a new virtual
35e70 74 61 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e table is be bein
35e80 67 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 g created or rei
35e90 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a nitialized..**.*
35ea0 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
35eb0 20 68 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 has exactly the
35ec0 20 73 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 same effect as
35ed0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 calling.** [sqli
35ee0 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c te3_create_modul
35ef0 65 5f 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e e_v2()] with a N
35f00 55 4c 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 ULL client data
35f10 64 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 destructor..*/.S
35f20 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 QLITE_API SQLITE
35f30 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e _EXPERIMENTAL in
35f40 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 t sqlite3_create
35f50 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 _module(. sqlit
35f60 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
35f70 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 /* SQLite
35f80 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 connection to re
35f90 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 gister module wi
35fa0 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 th */. const ch
35fb0 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 ar *zName,
35fc0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
35fd0 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f e module */. co
35fe0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
35ff0 6c 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 le *p, /* Meth
36000 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 ods for the modu
36010 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 le */. void *pC
36020 6c 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 lientData
36030 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 /* Client dat
36040 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 a for xCreate/xC
36050 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a onnect */.);../*
36060 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 .** CAPI3REF: Re
36070 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c gister A Virtual
36080 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 Table Implement
36090 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c ation {H18210} <
360a0 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 S20400>.** EXPER
360b0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
360c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 is routine is id
360d0 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b entical to the [
360e0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d sqlite3_create_m
360f0 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c odule()] method,
36100 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 .** except that
36110 69 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 it has an extra
36120 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 parameter to spe
36130 63 69 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 cify .** a destr
36140 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 uctor function f
36150 6f 72 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 or the client da
36160 74 61 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c ta pointer. SQL
36170 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f ite will.** invo
36180 6b 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f ke the destructo
36190 72 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 r function (if i
361a0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 t is not NULL) w
361b0 68 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f hen SQLite.** no
361c0 20 6c 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 longer needs th
361d0 65 20 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f e pClientData po
361e0 69 6e 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 inter. .*/.SQLI
361f0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 TE_API SQLITE_EX
36200 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 PERIMENTAL int s
36210 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f qlite3_create_mo
36220 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 dule_v2(. sqlit
36230 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 e3 *db,
36240 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 /* SQLite
36250 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 connection to re
36260 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 gister module wi
36270 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 th */. const ch
36280 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 ar *zName,
36290 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
362a0 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f e module */. co
362b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
362c0 6c 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 le *p, /* Meth
362d0 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 ods for the modu
362e0 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 le */. void *pC
362f0 6c 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 lientData,
36300 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 /* Client dat
36310 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 a for xCreate/xC
36320 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 onnect */. void
36330 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 (*xDestroy)(void
36340 2a 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 *) /* Module
36350 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 destructor func
36360 74 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a tion */.);../*.*
36370 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 * CAPI3REF: Virt
36380 75 61 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e ual Table Instan
36390 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 ce Object {H1801
363a0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 0} <S20400>.** K
363b0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 EYWORDS: sqlite3
363c0 5f 76 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d _vtab.** EXPERIM
363d0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 ENTAL.**.** Ever
363e0 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 y [virtual table
363f0 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 module] impleme
36400 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 ntation uses a s
36410 75 62 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 ubclass.** of th
36420 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
36430 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 cture to describ
36440 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 e a particular i
36450 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
36460 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 e [virtual table
36470 5d 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 ]. Each subclas
36480 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 s will.** be tai
36490 6c 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 lored to the spe
364a0 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 cific needs of t
364b0 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d he module implem
364c0 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 entation..** The
364d0 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 purpose of this
364e0 20 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 superclass is t
364f0 6f 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e o define certain
36500 20 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 fields that are
36510 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c .** common to al
36520 6c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 l module impleme
36530 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 ntations..**.**
36540 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d Virtual tables m
36550 65 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 ethods can set a
36560 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
36570 62 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a by assigning a.*
36580 2a 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 * string obtaine
36590 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f d from [sqlite3_
365a0 6d 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 mprintf()] to zE
365b0 72 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 rrMsg. The meth
365c0 6f 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b od should.** tak
365d0 65 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 e care that any
365e0 70 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 prior string is
365f0 66 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 freed by a call
36600 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 to [sqlite3_free
36610 28 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 ()].** prior to
36620 61 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 assigning a new
36630 73 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 string to zErrMs
36640 67 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 g. After the er
36650 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 ror message.** i
36660 73 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 s delivered up t
36670 6f 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 o the client app
36680 6c 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 lication, the st
36690 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 ring will be aut
366a0 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 omatically.** fr
366b0 65 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 eed by sqlite3_f
366c0 72 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 ree() and the zE
366d0 72 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c rrMsg field will
366e0 20 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 be zeroed..*/.s
366f0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 truct sqlite3_vt
36700 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c ab {. const sql
36710 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
36720 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f dule; /* The mo
36730 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 dule for this vi
36740 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 rtual table */.
36750 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
36760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
36770 20 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 /* NO LONGER US
36780 45 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 ED */. char *zE
36790 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 rrMsg;
367a0 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 /* Error
367b0 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 message from sq
367c0 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 lite3_mprintf()
367d0 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 */. /* Virtual
367e0 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 table implementa
367f0 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 tions will typic
36800 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f ally add additio
36810 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b nal fields */.};
36820 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
36830 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 : Virtual Table
36840 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b Cursor Object {
36850 48 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e H18020} <S20400>
36860 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 .** KEYWORDS: sq
36870 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
36880 72 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 r {virtual table
36890 20 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 cursor}.** EXPE
368a0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 RIMENTAL.**.** E
368b0 76 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 very [virtual ta
368c0 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c ble module] impl
368d0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 ementation uses
368e0 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 a subclass of th
368f0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 e.** following s
36900 74 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 tructure to desc
36910 72 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 ribe cursors tha
36920 74 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 t point into the
36930 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 .** [virtual tab
36940 6c 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 le] and are used
36950 0a 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f .** to loop thro
36960 75 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 ugh the virtual
36970 74 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 table. Cursors
36980 61 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e are created usin
36990 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 g the.** [sqlite
369a0 33 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 3_module.xOpen |
369b0 20 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f xOpen] method o
369c0 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 f the module and
369d0 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a are destroyed.*
369e0 2a 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 * by the [sqlite
369f0 33 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 3_module.xClose
36a00 7c 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 | xClose] method
36a10 2e 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 . Cussors are u
36a20 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 sed.** by the [x
36a30 46 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d Filter], [xNext]
36a40 2c 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 , [xEof], [xColu
36a50 6d 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 mn], and [xRowid
36a60 5d 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 ] methods.** of
36a70 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 the module. Eac
36a80 68 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 h module impleme
36a90 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 ntation will def
36aa0 69 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 ine.** the conte
36ab0 6e 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 nt of a cursor s
36ac0 74 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 tructure to suit
36ad0 20 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a its own needs..
36ae0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 **.** This super
36af0 63 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 class exists in
36b00 6f 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 order to define
36b10 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
36b20 72 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 rsor that.** are
36b30 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 common to all i
36b40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a mplementations..
36b50 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
36b60 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 3_vtab_cursor {.
36b70 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
36b80 70 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 pVtab; /* V
36b90 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 irtual table of
36ba0 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 this cursor */.
36bb0 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c /* Virtual tabl
36bc0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
36bd0 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 s will typically
36be0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
36bf0 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a fields */.};../*
36c00 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 .** CAPI3REF: De
36c10 63 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 clare The Schema
36c20 20 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 Of A Virtual Ta
36c30 62 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 ble {H18280} <S2
36c40 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 0400>.** EXPERIM
36c50 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ENTAL.**.** The
36c60 5b 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 [xCreate] and [x
36c70 43 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 Connect] methods
36c80 20 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 of a.** [virtua
36c90 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 l table module]
36ca0 63 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 call this interf
36cb0 61 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 ace.** to declar
36cc0 65 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 e the format (th
36cd0 65 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 e names and data
36ce0 74 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c types of the col
36cf0 75 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 umns) of.** the
36d00 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 virtual tables t
36d10 68 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a hey implement..*
36d20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c /.SQLITE_API SQL
36d30 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c ITE_EXPERIMENTAL
36d40 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 int sqlite3_dec
36d50 6c 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 lare_vtab(sqlite
36d60 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 3*, const char *
36d70 7a 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 zSQL);../*.** CA
36d80 50 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 PI3REF: Overload
36d90 20 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 A Function For
36da0 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 A Virtual Table
36db0 7b 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 {H18300} <S20400
36dc0 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 >.** EXPERIMENTA
36dd0 4c 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 L.**.** Virtual
36de0 74 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 tables can provi
36df0 64 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 de alternative i
36e00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f mplementations o
36e10 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 f functions.** u
36e20 73 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 sing the [xFindF
36e30 75 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 unction] method
36e40 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 of the [virtual
36e50 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 table module].
36e60 0a 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 .** But global v
36e70 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 ersions of those
36e80 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 functions.** mu
36e90 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 st exist in orde
36ea0 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 r to be overload
36eb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 ed..**.** This A
36ec0 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 PI makes sure a
36ed0 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f global version o
36ee0 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 f a function wit
36ef0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a h a particular.*
36f00 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 * name and numbe
36f10 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 r of parameters
36f20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 exists. If no s
36f30 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 uch function exi
36f40 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 sts.** before th
36f50 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 is API is called
36f60 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e , a new function
36f70 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 is created. Th
36f80 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
36f90 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 .** of the new f
36fa0 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 unction always c
36fb0 61 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 auses an excepti
36fc0 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e on to be thrown.
36fd0 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 So.** the new
36fe0 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 function is not
36ff0 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e good for anythin
37000 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 g by itself. It
37010 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 s only.** purpos
37020 65 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 e is to be a pla
37030 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f ceholder functio
37040 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 n that can be ov
37050 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 erloaded.** by a
37060 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d [virtual table]
37070 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
37080 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e SQLITE_EXPERIMEN
37090 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f TAL int sqlite3_
370a0 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f overload_functio
370b0 6e 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 n(sqlite3*, cons
370c0 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d t char *zFuncNam
370d0 65 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f e, int nArg);../
370e0 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 *.** The interfa
370f0 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 ce to the virtua
37100 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 l-table mechanis
37110 6d 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 m defined above
37120 28 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 (back up.** to a
37130 20 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 comment remarka
37140 62 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 bly similar to t
37150 68 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 his one) is curr
37160 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 ently considered
37170 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 .** to be experi
37180 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 mental. The int
37190 65 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 erface might cha
371a0 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 nge in incompati
371b0 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 ble ways..** If
371c0 74 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 this is a proble
371d0 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f m for you, do no
371e0 74 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 t use the interf
371f0 61 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 ace at this time
37200 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 ..**.** When the
37210 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d virtual-table m
37220 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 echanism stabili
37230 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 zes, we will dec
37240 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 lare the.** inte
37250 72 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 rface fixed, sup
37260 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 port it indefini
37270 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 tely, and remove
37280 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a this comment..*
37290 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d *.****** EXPERIM
372a0 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 ENTAL - subject
372b0 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 to change withou
372c0 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a t notice *******
372d0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a *******.*/../*.*
372e0 2a 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 * CAPI3REF: A Ha
372f0 6e 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 ndle To An Open
37300 42 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 BLOB {H17800} <S
37310 33 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 30230>.** KEYWOR
37320 44 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 DS: {BLOB handle
37330 7d 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d } {BLOB handles}
37340 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e .**.** An instan
37350 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
37360 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 t represents an
37370 6f 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 open BLOB on whi
37380 63 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 ch.** [sqlite3_b
37390 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 lob_open | incre
373a0 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d mental BLOB I/O]
373b0 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 can be performe
373c0 64 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 d..** Objects of
373d0 20 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 this type are c
373e0 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 reated by [sqlit
373f0 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a e3_blob_open()].
37400 2a 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 ** and destroyed
37410 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f by [sqlite3_blo
37420 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 b_close()]..** T
37430 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 he [sqlite3_blob
37440 5f 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 _read()] and [sq
37450 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 lite3_blob_write
37460 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a ()] interfaces.*
37470 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f * can be used to
37480 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 read or write s
37490 6d 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 mall subsections
374a0 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a of the BLOB..**
374b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c The [sqlite3_bl
374c0 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 ob_bytes()] inte
374d0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 rface returns th
374e0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c e size of the BL
374f0 4f 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a OB in bytes..*/.
37500 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 typedef struct s
37510 71 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 qlite3_blob sqli
37520 74 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a te3_blob;../*.**
37530 20 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 CAPI3REF: Open
37540 41 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 A BLOB For Incre
37550 6d 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 mental I/O {H178
37560 31 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 10} <S30230>.**.
37570 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 ** This interfac
37580 65 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 es opens a [BLOB
37590 20 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 handle | handle
375a0 5d 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f ] to the BLOB lo
375b0 63 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 cated.** in row
375c0 69 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f iRow, column zCo
375d0 6c 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 lumn, table zTab
375e0 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a le in database z
375f0 44 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 Db;.** in other
37600 77 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 words, the same
37610 42 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 BLOB that would
37620 62 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a be selected by:.
37630 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 **.** <pre>.**
37640 20 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d SELECT zColum
37650 6e 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c n FROM zDb.zTabl
37660 65 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 e WHERE [rowid]
37670 3d 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 = iRow;.** </pre
37680 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 > {END}.**.** If
37690 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d the flags param
376a0 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f eter is non-zero
376b0 2c 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 , then the BLOB
376c0 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 is opened for re
376d0 61 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 ad.** and write
376e0 61 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 access. If it is
376f0 20 7a 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 zero, the BLOB
37700 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 is opened for re
37710 61 64 20 61 63 63 65 73 73 2e 0a 2a 2a 20 49 74 ad access..** It
37720 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
37730 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d to open a colum
37740 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f n that is part o
37750 66 20 61 6e 20 69 6e 64 65 78 20 6f 72 20 70 72 f an index or pr
37760 69 6d 61 72 79 20 0a 2a 2a 20 6b 65 79 20 66 6f imary .** key fo
37770 72 20 77 72 69 74 69 6e 67 2e 20 5e 49 66 20 5b r writing. ^If [
37780 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
37790 74 72 61 69 6e 74 73 5d 20 61 72 65 20 65 6e 61 traints] are ena
377a0 62 6c 65 64 2c 20 69 74 20 69 73 20 0a 2a 2a 20 bled, it is .**
377b0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
377c0 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 open a column th
377d0 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 at is part of a
377e0 5b 63 68 69 6c 64 20 6b 65 79 5d 20 66 6f 72 20 [child key] for
377f0 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e writing..**.** N
37800 6f 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74 ote that the dat
37810 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f abase name is no
37820 74 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 t the filename t
37830 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 hat contains.**
37840 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 the database but
37850 20 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 rather the symb
37860 6f 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 olic name of the
37870 20 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a database that.*
37880 2a 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 * is assigned wh
37890 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 en the database
378a0 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 is connected usi
378b0 6e 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 ng [ATTACH]..**
378c0 46 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 For the main dat
378d0 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 abase file, the
378e0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 database name is
378f0 20 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 "main"..** For
37900 54 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 TEMP tables, the
37910 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 database name i
37920 73 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 s "temp"..**.**
37930 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c On success, [SQL
37940 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 ITE_OK] is retur
37950 6e 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 ned and the new
37960 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 [BLOB handle] is
37970 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a written.** to *
37980 70 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 ppBlob. Otherwis
37990 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 e an [error code
379a0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e ] is returned an
379b0 64 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 d *ppBlob is set
379c0 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c .** to be a null
379d0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 pointer..** Thi
379e0 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 s function sets
379f0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f the [database co
37a00 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 nnection] error
37a10 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 code and message
37a20 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 .** accessible v
37a30 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 ia [sqlite3_errc
37a40 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 ode()] and [sqli
37a50 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e te3_errmsg()] an
37a60 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e d related.** fun
37a70 63 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 ctions. Note th
37a80 61 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 at the *ppBlob v
37a90 61 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 ariable is alway
37aa0 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e s initialized in
37ab0 20 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d a.** way that m
37ac0 61 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 akes it safe to
37ad0 69 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f invoke [sqlite3_
37ae0 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e blob_close()] on
37af0 20 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 *ppBlob.** rega
37b00 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 rdless of the su
37b10 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 ccess or failure
37b20 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
37b30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 ..**.** If the r
37b40 6f 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 ow that a BLOB h
37b50 61 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 andle points to
37b60 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 is modified by a
37b70 6e 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b n.** [UPDATE], [
37b80 44 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b DELETE], or by [
37b90 4f 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 ON CONFLICT] sid
37ba0 65 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 e-effects.** the
37bb0 6e 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c n the BLOB handl
37bc0 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 e is marked as "
37bd0 65 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 expired"..** Thi
37be0 73 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 s is true if any
37bf0 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 column of the r
37c00 6f 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 ow is changed, e
37c10 76 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 ven a column.**
37c20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f other than the o
37c30 6e 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 ne the BLOB hand
37c40 6c 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a le is open on..*
37c50 2a 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 * Calls to [sqli
37c60 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d te3_blob_read()]
37c70 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c and [sqlite3_bl
37c80 6f 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a ob_write()] for.
37c90 2a 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f ** a expired BLO
37ca0 42 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 B handle fail wi
37cb0 74 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 th an return cod
37cc0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f e of [SQLITE_ABO
37cd0 52 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 RT]..** Changes
37ce0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 written into a B
37cf0 4c 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 LOB prior to the
37d00 20 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 BLOB expiring a
37d10 72 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 re not.** rollba
37d20 63 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 61 ck by the expira
37d30 74 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 tion of the BLOB
37d40 2e 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 . Such changes
37d50 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a will eventually.
37d60 2a 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 ** commit if the
37d70 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e transaction con
37d80 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 tinues to comple
37d90 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 tion..**.** Use
37da0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f the [sqlite3_blo
37db0 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 b_bytes()] inter
37dc0 66 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e face to determin
37dd0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a e the size of.**
37de0 20 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 the opened blob
37df0 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 . The size of a
37e00 20 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 blob may not be
37e10 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 changed by this
37e20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 .** interface.
37e30 55 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d Use the [UPDATE]
37e40 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 SQL command to
37e50 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 change the size
37e60 6f 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a of a.** blob..**
37e70 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
37e80 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 _bind_zeroblob()
37e90 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 ] and [sqlite3_r
37ea0 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 esult_zeroblob()
37eb0 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 ] interfaces.**
37ec0 61 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e and the built-in
37ed0 20 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 [zeroblob] SQL
37ee0 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 function can be
37ef0 75 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 used, if desired
37f00 2c 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 ,.** to create a
37f10 6e 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 n empty, zero-fi
37f20 6c 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 lled blob in whi
37f30 63 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 ch to read or wr
37f40 69 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 ite using.** thi
37f50 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a s interface..**.
37f60 2a 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 ** To avoid a re
37f70 73 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 source leak, eve
37f80 72 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 ry open [BLOB ha
37f90 6e 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 ndle] should eve
37fa0 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 ntually.** be re
37fb0 6c 65 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c leased by a call
37fc0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f to [sqlite3_blo
37fd0 62 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a b_close()]..**.*
37fe0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a * Requirements:.
37ff0 2a 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 ** [H17813] [H17
38000 38 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 814] [H17816] [H
38010 31 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 17819] [H17821]
38020 5b 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 [H17824].*/.SQLI
38030 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
38040 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 e3_blob_open(.
38050 73 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 sqlite3*,. cons
38060 74 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 t char *zDb,. c
38070 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c onst char *zTabl
38080 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 e,. const char
38090 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 *zColumn,. sqli
380a0 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a te3_int64 iRow,.
380b0 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 int flags,. s
380c0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 qlite3_blob **pp
380d0 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 Blob.);../*.** C
380e0 41 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 API3REF: Close A
380f0 20 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 BLOB Handle {H1
38100 37 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 7830} <S30230>.*
38110 2a 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f *.** Closes an o
38120 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 pen [BLOB handle
38130 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 ]..**.** Closing
38140 20 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 a BLOB shall ca
38150 75 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 use the current
38160 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 transaction to c
38170 6f 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 ommit.** if ther
38180 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 e are no other B
38190 4c 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 LOBs, no pending
381a0 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
381b0 65 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a ents, and the.**
381c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
381d0 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f tion is in [auto
381e0 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a commit mode]..**
381f0 20 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 If any writes w
38200 65 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 ere made to the
38210 42 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 BLOB, they might
38220 20 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 be held in cach
38230 65 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 e.** until the c
38240 6c 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 lose operation i
38250 66 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e f they will fit.
38260 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 .**.** Closing t
38270 68 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f he BLOB often fo
38280 72 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 rces the changes
38290 0a 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 .** out to disk
382a0 61 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f and so if any I/
382b0 4f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 O errors occur,
382c0 74 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 they will likely
382d0 20 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 occur.** at the
382e0 20 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 time when the B
382f0 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 LOB is closed.
38300 41 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 Any errors that
38310 6f 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 occur during.**
38320 63 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f closing are repo
38330 72 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 rted as a non-ze
38340 72 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e ro return value.
38350 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 .**.** The BLOB
38360 69 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 is closed uncond
38370 69 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e itionally. Even
38380 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 if this routine
38390 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 returns.** an e
383a0 72 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 rror code, the B
383b0 4c 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f LOB is still clo
383c0 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 sed..**.** Calli
383d0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ng this routine
383e0 77 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e with a null poin
383f0 74 65 72 20 28 77 68 69 63 68 20 61 73 20 77 6f ter (which as wo
38400 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a uld be returned.
38410 2a 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c ** by failed cal
38420 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c l to [sqlite3_bl
38430 6f 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 ob_open()]) is a
38440 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e harmless no-op.
38450 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
38460 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d nts:.** [H17833]
38470 20 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 [H17836] [H1783
38480 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 9].*/.SQLITE_API
38490 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
384a0 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f b_close(sqlite3_
384b0 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 blob *);../*.**
384c0 43 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e CAPI3REF: Return
384d0 20 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 The Size Of An
384e0 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 Open BLOB {H1784
384f0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
38500 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 * Returns the si
38510 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
38520 68 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 he BLOB accessib
38530 6c 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 le via the .** s
38540 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e uccessfully open
38550 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d ed [BLOB handle]
38560 20 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 in its only arg
38570 75 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 ument. The.** i
38580 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 ncremental blob
38590 49 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e I/O routines can
385a0 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 only read or ov
385b0 65 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 erwriting existi
385c0 6e 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 ng.** blob conte
385d0 6e 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 nt; they cannot
385e0 63 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 change the size
385f0 6f 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a of a blob..**.**
38600 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e This routine on
38610 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 ly works on a [B
38620 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 LOB handle] whic
38630 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 h has been creat
38640 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 ed.** by a prior
38650 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c successful call
38660 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f to [sqlite3_blo
38670 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 b_open()] and wh
38680 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 ich has not.** b
38690 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 een closed by [s
386a0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 qlite3_blob_clos
386b0 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 e()]. Passing a
386c0 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 ny other pointer
386d0 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 in.** to this r
386e0 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 outine results i
386f0 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 n undefined and
38700 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 probably undesir
38710 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a able behavior..*
38720 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 *.** Requirement
38730 73 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a s:.** [H17843].*
38740 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
38750 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 sqlite3_blob_by
38760 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 tes(sqlite3_blob
38770 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 *);../*.** CAPI
38780 33 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 3REF: Read Data
38790 46 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 From A BLOB Incr
387a0 65 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 ementally {H1785
387b0 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 0} <S30230>.**.*
387c0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
387d0 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 is used to read
387e0 64 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 data from an ope
387f0 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 n [BLOB handle]
38800 69 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 into a.** caller
38810 2d 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 -supplied buffer
38820 2e 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 . N bytes of dat
38830 61 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 a are copied int
38840 6f 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 o buffer Z.** fr
38850 6f 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 om the open BLOB
38860 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 , starting at of
38870 66 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a fset iOffset..**
38880 0a 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f .** If offset iO
38890 66 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 ffset is less th
388a0 61 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 an N bytes from
388b0 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 the end of the B
388c0 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f LOB,.** [SQLITE_
388d0 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e ERROR] is return
388e0 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 ed and no data i
388f0 73 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 s read. If N or
38900 20 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c iOffset is.** l
38910 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b ess than zero, [
38920 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 SQLITE_ERROR] is
38930 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f returned and no
38940 20 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a data is read..*
38950 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 * The size of th
38960 65 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 e blob (and henc
38970 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 e the maximum va
38980 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 lue of N+iOffset
38990 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 ).** can be dete
389a0 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 rmined using the
389b0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 [sqlite3_blob_b
389c0 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 ytes()] interfac
389d0 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 e..**.** An atte
389e0 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d mpt to read from
389f0 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f an expired [BLO
38a00 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 B handle] fails
38a10 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 with an.** error
38a20 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 code of [SQLITE
38a30 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f _ABORT]..**.** O
38a40 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 n success, SQLIT
38a50 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 E_OK is returned
38a60 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ..** Otherwise,
38a70 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 an [error code]
38a80 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 or an [extended
38a90 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 error code] is r
38aa0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 eturned..**.** T
38ab0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 his routine only
38ac0 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f works on a [BLO
38ad0 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 B handle] which
38ae0 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 has been created
38af0 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 .** by a prior s
38b00 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 uccessful call t
38b10 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f o [sqlite3_blob_
38b20 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 open()] and whic
38b30 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 h has not.** bee
38b40 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c n closed by [sql
38b50 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 ite3_blob_close(
38b60 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 )]. Passing any
38b70 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 other pointer i
38b80 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 n.** to this rou
38b90 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 tine results in
38ba0 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 undefined and pr
38bb0 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 obably undesirab
38bc0 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a le behavior..**.
38bd0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 ** See also: [sq
38be0 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 lite3_blob_write
38bf0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 ()]..**.** Requi
38c00 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 rements:.** [H17
38c10 38 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 853] [H17856] [H
38c20 31 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 17859] [H17862]
38c30 5b 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 [H17863] [H17865
38c40 5d 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 ] [H17868].*/.SQ
38c50 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
38c60 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 ite3_blob_read(s
38c70 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 qlite3_blob *, v
38c80 6f 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 oid *Z, int N, i
38c90 6e 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a nt iOffset);../*
38ca0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 .** CAPI3REF: Wr
38cb0 69 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 ite Data Into A
38cc0 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c BLOB Incremental
38cd0 6c 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 ly {H17870} <S30
38ce0 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 230>.**.** This
38cf0 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 function is used
38d00 20 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 to write data i
38d10 6e 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f nto an open [BLO
38d20 42 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 B handle] from a
38d30 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c .** caller-suppl
38d40 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 ied buffer. N by
38d50 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 tes of data are
38d60 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 copied from the
38d70 62 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f buffer Z.** into
38d80 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 the open BLOB,
38d90 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 starting at offs
38da0 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a et iOffset..**.*
38db0 2a 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 * If the [BLOB h
38dc0 61 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 andle] passed as
38dd0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d the first argum
38de0 65 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e ent was not open
38df0 65 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e ed for.** writin
38e00 67 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 g (the flags par
38e10 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 ameter to [sqlit
38e20 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 e3_blob_open()]
38e30 77 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 was zero),.** th
38e40 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
38e50 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 rns [SQLITE_READ
38e60 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ONLY]..**.** Thi
38e70 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f s function may o
38e80 6e 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 nly modify the c
38e90 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 ontents of the B
38ea0 4c 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f LOB; it is.** no
38eb0 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e t possible to in
38ec0 63 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 crease the size
38ed0 6f 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 of a BLOB using
38ee0 74 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 this API..** If
38ef0 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 offset iOffset i
38f00 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 s less than N by
38f10 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 tes from the end
38f20 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a of the BLOB,.**
38f30 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 [SQLITE_ERROR]
38f40 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 is returned and
38f50 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 no data is writt
38f60 65 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 en. If N is.**
38f70 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b less than zero [
38f80 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 SQLITE_ERROR] is
38f90 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f returned and no
38fa0 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e data is written
38fb0 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 ..** The size of
38fc0 20 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 the BLOB (and h
38fd0 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d ence the maximum
38fe0 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 value of N+iOff
38ff0 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 set).** can be d
39000 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 etermined using
39010 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f the [sqlite3_blo
39020 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 b_bytes()] inter
39030 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 face..**.** An a
39040 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 ttempt to write
39050 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 to an expired [B
39060 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c LOB handle] fail
39070 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 s with an.** err
39080 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 or code of [SQLI
39090 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 TE_ABORT]. Writ
390a0 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 es to the BLOB t
390b0 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 hat occurred.**
390c0 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 before the [BLOB
390d0 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 handle] expired
390e0 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 are not rolled
390f0 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 back by the.** e
39100 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 xpiration of the
39110 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 handle, though
39120 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 of course those
39130 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a changes might.**
39140 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 have been overw
39150 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 ritten by the st
39160 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 atement that exp
39170 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 ired the BLOB ha
39180 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 ndle.** or by ot
39190 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 her independent
391a0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a statements..**.*
391b0 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 * On success, SQ
391c0 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
391d0 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 ned..** Otherwis
391e0 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f e, an [error co
391f0 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e de] or an [exten
39200 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 ded error code]
39210 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
39220 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
39230 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 only works on a
39240 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 [BLOB handle] wh
39250 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 ich has been cre
39260 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 ated.** by a pri
39270 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 or successful ca
39280 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 ll to [sqlite3_b
39290 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 lob_open()] and
392a0 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a which has not.**
392b0 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 been closed by
392c0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c [sqlite3_blob_cl
392d0 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 ose()]. Passing
392e0 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 any other point
392f0 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 er in.** to this
39300 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 routine results
39310 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e in undefined an
39320 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 d probably undes
39330 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e irable behavior.
39340 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a .**.** See also:
39350 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 [sqlite3_blob_r
39360 65 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 ead()]..**.** Re
39370 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b quirements:.** [
39380 48 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d H17873] [H17874]
39390 20 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 [H17875] [H1787
393a0 36 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 6] [H17877] [H17
393b0 38 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 879] [H17882] [H
393c0 31 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 17885].** [H1788
393d0 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 8].*/.SQLITE_API
393e0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
393f0 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f b_write(sqlite3_
39400 62 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f blob *, const vo
39410 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e id *z, int n, in
39420 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a t iOffset);../*.
39430 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 ** CAPI3REF: Vir
39440 74 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d tual File System
39450 20 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 Objects {H11200
39460 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a } <S20100>.**.**
39470 20 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 A virtual files
39480 79 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 ystem (VFS) is a
39490 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 n [sqlite3_vfs]
394a0 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 object.** that S
394b0 51 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e QLite uses to in
394c0 74 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 teract.** with t
394d0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 he underlying op
394e0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 erating system.
394f0 20 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 Most SQLite bui
39500 6c 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a lds come with a.
39510 2a 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c ** single defaul
39520 74 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70 t VFS that is ap
39530 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 propriate for th
39540 65 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e e host computer.
39550 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 .** New VFSes ca
39560 6e 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 n be registered
39570 61 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 and existing VFS
39580 65 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 es can be unregi
39590 73 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 stered..** The f
395a0 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 ollowing interfa
395b0 63 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 ces are provided
395c0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
395d0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 te3_vfs_find() i
395e0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 nterface returns
395f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
39600 56 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 VFS given its na
39610 6d 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 me..** Names are
39620 20 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e case sensitive.
39630 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 .** Names are ze
39640 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 ro-terminated UT
39650 46 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 F-8 strings..**
39660 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d If there is no m
39670 61 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 atch, a NULL poi
39680 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
39690 2e 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 ..** If zVfsName
396a0 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 is NULL then th
396b0 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 e default VFS is
396c0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
396d0 20 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72 New VFSes are r
396e0 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73 egistered with s
396f0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
39700 74 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e ter()..** Each n
39710 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 ew VFS becomes t
39720 68 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 he default VFS i
39730 66 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 f the makeDflt f
39740 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 lag is set..** T
39750 68 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 he same VFS can
39760 62 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 be registered mu
39770 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 ltiple times wit
39780 68 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 hout injury..**
39790 54 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 To make an exist
397a0 69 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 ing VFS into the
397b0 20 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 default VFS, re
397c0 67 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a gister it again.
397d0 2a 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 ** with the make
397e0 44 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 Dflt flag set.
397f0 49 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 If two different
39800 20 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a VFSes with the.
39810 2a 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 ** same name are
39820 20 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 registered, the
39830 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 behavior is und
39840 65 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a efined. If a.**
39850 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 VFS is register
39860 65 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 ed with a name t
39870 68 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 hat is NULL or a
39880 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a n empty string,.
39890 2a 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 ** then the beha
398a0 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 vior is undefine
398b0 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 d..**.** Unregis
398c0 74 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74 ter a VFS with t
398d0 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 he sqlite3_vfs_u
398e0 6e 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 nregister() inte
398f0 72 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 rface..** If the
39900 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 default VFS is
39910 75 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e unregistered, an
39920 6f 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f other VFS is cho
39930 73 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 sen as.** the de
39940 66 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 fault. The choi
39950 63 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 ce for the new V
39960 46 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e FS is arbitrary.
39970 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 .**.** Requireme
39980 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d nts:.** [H11203]
39990 20 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 [H11206] [H1120
399a0 39 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 9] [H11212] [H11
399b0 32 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 215] [H11218].*/
399c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 .SQLITE_API sqli
399d0 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 te3_vfs *sqlite3
399e0 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 _vfs_find(const
399f0 63 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b char *zVfsName);
39a00 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
39a10 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
39a20 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 ster(sqlite3_vfs
39a30 2a 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 *, int makeDflt)
39a40 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
39a50 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 sqlite3_vfs_unr
39a60 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f egister(sqlite3_
39a70 76 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 vfs*);../*.** CA
39a80 50 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 PI3REF: Mutexes
39a90 7b 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 {H17000} <S20000
39aa0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 >.**.** The SQLi
39ab0 74 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 te core uses the
39ac0 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 se routines for
39ad0 74 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 thread.** synchr
39ae0 6f 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 onization. Thoug
39af0 68 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e h they are inten
39b00 64 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c ded for internal
39b10 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
39b20 65 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e e, code that lin
39b30 6b 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 ks against SQLit
39b40 65 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 e is.** permitte
39b50 64 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 d to use any of
39b60 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a these routines..
39b70 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 **.** The SQLite
39b80 20 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e source code con
39b90 74 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 tains multiple i
39ba0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a mplementations.*
39bb0 2a 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 * of these mutex
39bc0 20 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 routines. An a
39bd0 70 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 ppropriate imple
39be0 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 mentation.** is
39bf0 73 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 selected automat
39c00 69 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c ically at compil
39c10 65 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c e-time. The fol
39c20 6c 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d lowing.** implem
39c30 65 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 entations are av
39c40 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 ailable in the S
39c50 51 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a QLite core:.**.*
39c60 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
39c70 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 SQLITE_MUTEX_OS
39c80 32 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 2.** <li> SQLI
39c90 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 TE_MUTEX_PTHREAD
39ca0 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 .** <li> SQLIT
39cb0 45 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c E_MUTEX_W32.** <
39cc0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 li> SQLITE_MUT
39cd0 45 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e EX_NOOP.** </ul>
39ce0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 .**.** The SQLIT
39cf0 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 E_MUTEX_NOOP imp
39d00 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 lementation is a
39d10 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 set of routines
39d20 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f .** that does no
39d30 20 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e real locking an
39d40 64 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 d is appropriate
39d50 20 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 for use in.** a
39d60 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 single-threaded
39d70 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 application. T
39d80 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f he SQLITE_MUTEX_
39d90 4f 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d OS2,.** SQLITE_M
39da0 55 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e UTEX_PTHREAD, an
39db0 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 d SQLITE_MUTEX_W
39dc0 33 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 32 implementatio
39dd0 6e 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 ns.** are approp
39de0 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e riate for use on
39df0 20 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 OS/2, Unix, and
39e00 20 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 Windows..**.**
39e10 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d If SQLite is com
39e20 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 piled with the S
39e30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 QLITE_MUTEX_APPD
39e40 45 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a EF preprocessor.
39e50 2a 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 ** macro defined
39e60 20 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 (with "-DSQLITE
39e70 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 _MUTEX_APPDEF=1"
39e80 29 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 ), then no mutex
39e90 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
39ea0 6f 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 on is included w
39eb0 69 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e ith the library.
39ec0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 In this case th
39ed0 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e e.** application
39ee0 20 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 must supply a c
39ef0 75 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c ustom mutex impl
39f00 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 ementation using
39f10 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f the.** [SQLITE_
39f20 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 CONFIG_MUTEX] op
39f30 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
39f40 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e te3_config() fun
39f50 63 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 ction.** before
39f60 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f calling sqlite3_
39f70 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 initialize() or
39f80 61 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 any other public
39f90 20 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e sqlite3_.** fun
39fa0 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 ction that calls
39fb0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
39fc0 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 ize()..**.** {H1
39fd0 37 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 7011} The sqlite
39fe0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 3_mutex_alloc()
39ff0 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 routine allocate
3a000 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 s a new.** mutex
3a010 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 and returns a p
3a020 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 ointer to it. {H
3a030 31 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 17012} If it ret
3a040 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 urns NULL.** tha
3a050 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d t means that a m
3a060 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 utex could not b
3a070 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 e allocated. {H1
3a080 37 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 7013} SQLite.**
3a090 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 will unwind its
3a0a0 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e stack and return
3a0b0 20 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 an error. {H170
3a0c0 31 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 14} The argument
3a0d0 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d .** to sqlite3_m
3a0e0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 utex_alloc() is
3a0f0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 one of these int
3a100 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a eger constants:.
3a110 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c **.** <ul>.** <l
3a120 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
3a130 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _FAST.** <li> S
3a140 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
3a150 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 RSIVE.** <li> S
3a160 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
3a170 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 IC_MASTER.** <li
3a180 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
3a190 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c STATIC_MEM.** <l
3a1a0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
3a1b0 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 _STATIC_MEM2.**
3a1c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 <li> SQLITE_MUT
3a1d0 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a EX_STATIC_PRNG.*
3a1e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
3a1f0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a UTEX_STATIC_LRU.
3a200 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f ** <li> SQLITE_
3a210 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 MUTEX_STATIC_LRU
3a220 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 2.** </ul>.**.**
3a230 20 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 {H17015} The fi
3a240 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 rst two constant
3a250 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f s cause sqlite3_
3a260 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f mutex_alloc() to
3a270 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 create.** a new
3a280 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 mutex. The new
3a290 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 mutex is recurs
3a2a0 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f ive when SQLITE_
3a2b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a MUTEX_RECURSIVE.
3a2c0 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e ** is used but n
3a2d0 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 ot necessarily s
3a2e0 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 o when SQLITE_MU
3a2f0 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 TEX_FAST is used
3a300 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d . {END}.** The m
3a310 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 utex implementat
3a320 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 ion does not nee
3a330 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 d to make a dist
3a340 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 inction.** betwe
3a350 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f en SQLITE_MUTEX_
3a360 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 RECURSIVE and SQ
3a370 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 LITE_MUTEX_FAST
3a380 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f if it does.** no
3a390 74 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 t want to. {H17
3a3a0 30 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 016} But SQLite
3a3b0 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 will only reques
3a3c0 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 t a recursive mu
3a3d0 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 tex in.** cases
3a3e0 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 where it really
3a3f0 6e 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 needs one. {END
3a400 7d 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f } If a faster no
3a410 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n-recursive mute
3a420 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
3a430 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
3a440 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 on the host pla
3a450 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 tform, the mutex
3a460 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 subsystem.** mi
3a470 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 ght return such
3a480 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f a mutex in respo
3a490 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 nse to SQLITE_MU
3a4a0 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 TEX_FAST..**.**
3a4b0 7b 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 {H17017} The oth
3a4c0 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d er allowed param
3a4d0 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 eters to sqlite3
3a4e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 _mutex_alloc() e
3a4f0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 ach return.** a
3a500 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 pointer to a sta
3a510 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 tic preexisting
3a520 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 mutex. {END} Si
3a530 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 x static mutexes
3a540 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 are.** used by
3a550 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 the current vers
3a560 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 ion of SQLite.
3a570 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 Future versions
3a580 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 of SQLite.** may
3a590 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 add additional
3a5a0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 static mutexes.
3a5b0 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 Static mutexes
3a5c0 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c are for internal
3a5d0 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 .** use by SQLit
3a5e0 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 e only. Applica
3a5f0 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 tions that use S
3a600 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 QLite mutexes sh
3a610 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 ould.** use only
3a620 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 the dynamic mut
3a630 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 exes returned by
3a640 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 SQLITE_MUTEX_FA
3a650 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ST or.** SQLITE_
3a660 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e MUTEX_RECURSIVE.
3a670 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 .**.** {H17018}
3a680 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 Note that if one
3a690 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 of the dynamic
3a6a0 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 mutex parameters
3a6b0 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 (SQLITE_MUTEX_F
3a6c0 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 AST.** or SQLITE
3a6d0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
3a6e0 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 ) is used then s
3a6f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
3a700 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 oc().** returns
3a710 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 a different mute
3a720 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e x on every call.
3a730 20 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 {H17034} But f
3a740 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a or the static.**
3a750 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 mutex types, th
3a760 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 e same mutex is
3a770 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 returned on ever
3a780 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a y call that has.
3a790 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 ** the same type
3a7a0 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b number..**.** {
3a7b0 48 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 H17019} The sqli
3a7c0 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 te3_mutex_free()
3a7d0 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 routine dealloc
3a7e0 61 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c ates a previousl
3a7f0 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 y.** allocated d
3a800 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 ynamic mutex. {H
3a810 31 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 17020} SQLite is
3a820 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c careful to deal
3a830 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 locate every.**
3a840 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 dynamic mutex th
3a850 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e at it allocates.
3a860 20 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 {A17021} The dy
3a870 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 namic mutexes mu
3a880 73 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 st not be in.**
3a890 75 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 use when they ar
3a8a0 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b e deallocated. {
3a8b0 41 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 A17022} Attempti
3a8c0 6e 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 ng to deallocate
3a8d0 20 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 a static.** mut
3a8e0 65 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e ex results in un
3a8f0 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 defined behavior
3a900 2e 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 . {H17023} SQLit
3a910 65 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 e never dealloca
3a920 74 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 tes.** a static
3a930 6d 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a mutex. {END}.**.
3a940 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
3a950 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 utex_enter() and
3a960 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
3a970 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 ry() routines at
3a980 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 tempt.** to ente
3a990 72 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 r a mutex. {H170
3a9a0 32 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 24} If another t
3a9b0 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 hread is already
3a9c0 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 within the mute
3a9d0 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 x,.** sqlite3_mu
3a9e0 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c tex_enter() will
3a9f0 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 block and sqlit
3aa00 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 e3_mutex_try() w
3aa10 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 ill return.** SQ
3aa20 4c 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 LITE_BUSY. {H170
3aa30 32 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 25} The sqlite3
3aa40 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 _mutex_try() int
3aa50 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b erface returns [
3aa60 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 SQLITE_OK].** up
3aa70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e on successful en
3aa80 74 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d try. {H17026} M
3aa90 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 utexes created u
3aaa0 73 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d sing.** SQLITE_M
3aab0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 UTEX_RECURSIVE c
3aac0 61 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 an be entered mu
3aad0 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 ltiple times by
3aae0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e the same thread.
3aaf0 0a 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 .** {H17027} In
3ab00 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a such cases the,.
3ab10 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 ** mutex must be
3ab20 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c exited an equal
3ab30 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 number of times
3ab40 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 before another
3ab50 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e thread.** can en
3ab60 74 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 ter. {A17028} I
3ab70 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 f the same threa
3ab80 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 d tries to enter
3ab90 20 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 any other.** ki
3aba0 6e 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 nd of mutex more
3abb0 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 than once, the
3abc0 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 behavior is unde
3abd0 66 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 fined..** {H1702
3abe0 39 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 9} SQLite will n
3abf0 65 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 ever exhibit.**
3ac00 73 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e such behavior in
3ac10 20 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 its own use of
3ac20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 mutexes..**.** S
3ac30 6f 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 ome systems (for
3ac40 20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 example, Window
3ac50 73 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 s 95) do not sup
3ac60 70 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 port the operati
3ac70 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 on.** implemente
3ac80 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 d by sqlite3_mut
3ac90 65 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 ex_try(). On th
3aca0 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c ose systems, sql
3acb0 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 ite3_mutex_try()
3acc0 0a 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 .** will always
3acd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 return SQLITE_BU
3ace0 53 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 SY. {H17030} Th
3acf0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e e SQLite core on
3ad00 6c 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 ly ever uses.**
3ad10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
3ad20 79 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 y() as an optimi
3ad30 7a 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 zation so this i
3ad40 73 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 s acceptable beh
3ad50 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 avior..**.** {H1
3ad60 37 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 7031} The sqlite
3ad70 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 3_mutex_leave()
3ad80 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 routine exits a
3ad90 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a mutex that was.*
3ada0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 * previously ent
3adb0 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 ered by the same
3adc0 20 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 thread. {A1703
3add0 32 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2} The behavior.
3ade0 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 ** is undefined
3adf0 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 if the mutex is
3ae00 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e not currently en
3ae10 74 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 tered by the.**
3ae20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f calling thread o
3ae30 72 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 r is not current
3ae40 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b ly allocated. {
3ae50 48 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 H17033} SQLite w
3ae60 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 ill.** never do
3ae70 65 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a either. {END}.**
3ae80 0a 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d .** If the argum
3ae90 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d ent to sqlite3_m
3aea0 75 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 utex_enter(), sq
3aeb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
3aec0 29 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 ), or.** sqlite3
3aed0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 _mutex_leave() i
3aee0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 s a NULL pointer
3aef0 2c 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 , then all three
3af00 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 routines.** beh
3af10 61 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a ave as no-ops..*
3af20 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b *.** See also: [
3af30 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
3af40 6c 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 ld()] and [sqlit
3af50 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
3af60 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ()]..*/.SQLITE_A
3af70 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 PI sqlite3_mutex
3af80 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f *sqlite3_mutex_
3af90 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 alloc(int);.SQLI
3afa0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 TE_API void sqli
3afb0 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 te3_mutex_free(s
3afc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a qlite3_mutex*);.
3afd0 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
3afe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
3aff0 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ter(sqlite3_mute
3b000 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 x*);.SQLITE_API
3b010 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 int sqlite3_mute
3b020 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 x_try(sqlite3_mu
3b030 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 tex*);.SQLITE_AP
3b040 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d I void sqlite3_m
3b050 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 utex_leave(sqlit
3b060 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a e3_mutex*);../*.
3b070 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 ** CAPI3REF: Mut
3b080 65 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 ex Methods Objec
3b090 74 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 t {H17120} <S201
3b0a0 33 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 30>.** EXPERIMEN
3b0b0 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 TAL.**.** An ins
3b0c0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
3b0d0 72 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 ructure defines
3b0e0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f the low-level ro
3b0f0 75 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 utines.** used t
3b100 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 o allocate and u
3b110 73 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a se mutexes..**.*
3b120 2a 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 * Usually, the d
3b130 65 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 efault mutex imp
3b140 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f lementations pro
3b150 76 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 vided by SQLite
3b160 61 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e are.** sufficien
3b170 74 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 t, however the u
3b180 73 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 ser has the opti
3b190 6f 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 on of substituti
3b1a0 6e 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 ng a custom.** i
3b1b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f mplementation fo
3b1c0 72 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 r specialized de
3b1d0 70 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 ployments or sys
3b1e0 74 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 tems for which S
3b1f0 51 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f QLite.** does no
3b200 74 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 t provide a suit
3b210 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 able implementat
3b220 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ion. In this cas
3b230 65 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 e, the user.** c
3b240 72 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c reates and popul
3b250 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 ates an instance
3b260 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 of this structu
3b270 72 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f re to pass.** to
3b280 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
3b290 29 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 ) along with the
3b2a0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f [SQLITE_CONFIG_
3b2b0 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a MUTEX] option..*
3b2c0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 * Additionally,
3b2d0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 an instance of t
3b2e0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 his structure ca
3b2f0 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a n be used as an.
3b300 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 ** output variab
3b310 6c 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 le when querying
3b320 20 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 the system for
3b330 74 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 the current mute
3b340 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
3b350 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b ion, using the [
3b360 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 SQLITE_CONFIG_GE
3b370 54 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a TMUTEX] option..
3b380 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 **.** The xMutex
3b390 49 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 Init method defi
3b3a0 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 ned by this stru
3b3b0 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 cture is invoked
3b3c0 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 as.** part of s
3b3d0 79 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 ystem initializa
3b3e0 74 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 tion by the sqli
3b3f0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
3b400 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 function..** {H
3b410 31 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 17001} The xMute
3b420 78 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 xInit routine sh
3b430 61 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 all be called by
3b440 20 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 SQLite once for
3b450 20 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 each.** effecti
3b460 76 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 ve call to [sqli
3b470 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 te3_initialize()
3b480 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 ]..**.** The xMu
3b490 74 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 texEnd method de
3b4a0 66 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 fined by this st
3b4b0 72 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b ructure is invok
3b4c0 65 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 ed as.** part of
3b4d0 20 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e system shutdown
3b4e0 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f by the sqlite3_
3b4f0 73 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 shutdown() funct
3b500 69 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c ion. The.** impl
3b510 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
3b520 69 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 is method is exp
3b530 65 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 ected to release
3b540 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 all outstanding
3b550 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 .** resources ob
3b560 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 tained by the mu
3b570 74 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c tex methods impl
3b580 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 ementation, espe
3b590 63 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 cially.** those
3b5a0 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 obtained by the
3b5b0 78 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f xMutexInit metho
3b5c0 64 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 d. {H17003} The
3b5d0 78 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 xMutexEnd().** i
3b5e0 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 nterface shall b
3b5f0 65 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 e invoked once f
3b600 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 or each call to
3b610 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 [sqlite3_shutdow
3b620 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 n()]..**.** The
3b630 72 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 remaining seven
3b640 6d 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 methods defined
3b650 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 by this structur
3b660 65 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a e (xMutexAlloc,.
3b670 2a 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 ** xMutexFree, x
3b680 4d 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 MutexEnter, xMut
3b690 65 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 exTry, xMutexLea
3b6a0 76 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 ve, xMutexHeld a
3b6b0 6e 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 nd.** xMutexNoth
3b6c0 65 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 eld) implement t
3b6d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 he following int
3b6e0 65 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 erfaces (respect
3b6f0 69 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 ively):.**.** <u
3b700 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 l>.** <li> [s
3b710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c qlite3_mutex_all
3b720 6f 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 oc()] </li>.**
3b730 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f <li> [sqlite3_
3b740 6d 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f mutex_free()] </
3b750 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b li>.** <li> [
3b760 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
3b770 74 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 ter()] </li>.**
3b780 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
3b790 5f 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f _mutex_try()] </
3b7a0 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b li>.** <li> [
3b7b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
3b7c0 61 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 ave()] </li>.**
3b7d0 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 <li> [sqlite3
3b7e0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c _mutex_held()] <
3b7f0 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 /li>.** <li>
3b800 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e [sqlite3_mutex_n
3b810 6f 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a otheld()] </li>.
3b820 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
3b830 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e he only differen
3b840 63 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 ce is that the p
3b850 75 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 ublic sqlite3_XX
3b860 58 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d X functions enum
3b870 65 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 erated.** above
3b880 73 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 silently ignore
3b890 61 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 any invocations
3b8a0 74 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c that pass a NULL
3b8b0 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 pointer instead
3b8c0 0a 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d .** of a valid m
3b8d0 75 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 utex handle. The
3b8e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
3b8f0 20 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 of the methods
3b900 64 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 defined.** by th
3b910 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 is structure are
3b920 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f not required to
3b930 20 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 handle this cas
3b940 65 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a e, the results.*
3b950 2a 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e * of passing a N
3b960 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 ULL pointer inst
3b970 65 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d ead of a valid m
3b980 75 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 utex handle are
3b990 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e undefined.** (i.
3b9a0 65 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 e. it is accepta
3b9b0 62 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 ble to provide a
3b9c0 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e n implementation
3b9d0 20 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 that segfaults
3b9e0 69 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 if.** it is pass
3b9f0 65 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ed a NULL pointe
3ba00 72 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d r)..**.** The xM
3ba10 75 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f utexInit() metho
3ba20 64 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 d must be thread
3ba30 73 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 safe. It must b
3ba40 65 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a e harmless to.**
3ba50 20 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e invoke xMutexIn
3ba60 69 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d it() mutiple tim
3ba70 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 es within the sa
3ba80 6d 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 me process and w
3ba90 69 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 ithout.** interv
3baa0 65 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 ening calls to x
3bab0 4d 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 MutexEnd(). Sec
3bac0 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 ond and subseque
3bad0 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 nt calls to.** x
3bae0 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 MutexInit() must
3baf0 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a be no-ops..**.*
3bb00 2a 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d * xMutexInit() m
3bb10 75 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 ust not use SQLi
3bb20 74 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 te memory alloca
3bb30 74 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d tion ([sqlite3_m
3bb40 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 alloc()].** and
3bb50 69 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e its associates).
3bb60 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 Similarly, xMu
3bb70 74 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 texAlloc() must
3bb80 6e 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d not use SQLite m
3bb90 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 emory.** allocat
3bba0 69 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 ion for a static
3bbb0 20 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 mutex. However
3bbc0 20 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d xMutexAlloc() m
3bbd0 61 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a ay use SQLite.**
3bbe0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
3bbf0 6f 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 on for a fast or
3bc00 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
3bc10 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 ..**.** SQLite w
3bc20 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 ill invoke the x
3bc30 4d 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f MutexEnd() metho
3bc40 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f d when [sqlite3_
3bc50 73 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a shutdown()] is.*
3bc60 2a 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e * called, but on
3bc70 6c 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 ly if the prior
3bc80 63 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e call to xMutexIn
3bc90 69 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 it returned SQLI
3bca0 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 TE_OK..** If xMu
3bcb0 74 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e texInit fails in
3bcc0 20 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 any way, it is
3bcd0 65 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 expected to clea
3bce0 6e 20 75 70 20 61 66 74 65 72 20 69 74 73 65 6c n up after itsel
3bcf0 66 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 f.** prior to re
3bd00 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 turning..*/.type
3bd10 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 def struct sqlit
3bd20 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 e3_mutex_methods
3bd30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
3bd40 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 ethods;.struct s
3bd50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 qlite3_mutex_met
3bd60 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 hods {. int (*x
3bd70 4d 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 MutexInit)(void)
3bd80 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 ;. int (*xMutex
3bd90 45 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 End)(void);. sq
3bda0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 lite3_mutex *(*x
3bdb0 4d 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 MutexAlloc)(int)
3bdc0 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 ;. void (*xMute
3bdd0 78 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d xFree)(sqlite3_m
3bde0 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 utex *);. void
3bdf0 28 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 (*xMutexEnter)(s
3be00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b qlite3_mutex *);
3be10 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 . int (*xMutexT
3be20 72 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ry)(sqlite3_mute
3be30 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 x *);. void (*x
3be40 4d 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 MutexLeave)(sqli
3be50 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 te3_mutex *);.
3be60 69 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 int (*xMutexHeld
3be70 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 )(sqlite3_mutex
3be80 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 *);. int (*xMut
3be90 65 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 exNotheld)(sqlit
3bea0 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a e3_mutex *);.};.
3beb0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a ./*.** CAPI3REF:
3bec0 20 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 Mutex Verificat
3bed0 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 ion Routines {H1
3bee0 37 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 7080} <S20130> <
3bef0 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 S30800>.**.** Th
3bf00 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
3bf10 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
3bf20 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
3bf30 28 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 () routines.** a
3bf40 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 re intended for
3bf50 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 use inside asser
3bf60 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 t() statements.
3bf70 7b 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c {H17081} The SQL
3bf80 69 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 ite core.** neve
3bf90 72 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 r uses these rou
3bfa0 74 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 tines except ins
3bfb0 69 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 ide an assert()
3bfc0 61 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 and applications
3bfd0 0a 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 .** are advised
3bfe0 74 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 to follow the le
3bff0 61 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 ad of the core.
3c000 20 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f {H17082} The co
3c010 72 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 re only.** provi
3c020 64 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 des implementati
3c030 6f 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f ons for these ro
3c040 75 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 utines when it i
3c050 73 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 s compiled.** wi
3c060 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 th the SQLITE_DE
3c070 42 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 BUG flag. {A170
3c080 38 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 87} External mut
3c090 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ex implementatio
3c0a0 6e 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 ns.** are only r
3c0b0 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 equired to provi
3c0c0 64 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 de these routine
3c0d0 73 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 s if SQLITE_DEBU
3c0e0 47 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 G is.** defined
3c0f0 61 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 and if NDEBUG is
3c100 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a not defined..**
3c110 0a 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 .** {H17083} The
3c120 73 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 se routines shou
3c130 6c 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 ld return true i
3c140 66 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 f the mutex in t
3c150 68 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a heir argument.**
3c160 20 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 is held or not
3c170 68 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 held, respective
3c180 6c 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 ly, by the calli
3c190 6e 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a ng thread..**.**
3c1a0 20 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d {X17084} The im
3c1b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
3c1c0 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 not required to
3c1d0 70 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e provided version
3c1e0 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f s of these.** ro
3c1f0 75 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 utines that actu
3c200 61 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 ally work. If th
3c210 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
3c220 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 does not provid
3c230 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 e working.** ver
3c240 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 sions of these r
3c250 6f 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 outines, it shou
3c260 6c 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 ld at least prov
3c270 69 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 ide stubs that a
3c280 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 lways.** return
3c290 74 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 true so that one
3c2a0 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 does not get sp
3c2b0 75 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e urious assertion
3c2c0 20 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a failures..**.**
3c2d0 20 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 {H17085} If the
3c2e0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
3c2f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
3c300 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e ) is a NULL poin
3c310 74 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 ter then.** the
3c320 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 routine should r
3c330 65 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 eturn 1. {END}
3c340 54 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 This seems count
3c350 65 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e er-intuitive sin
3c360 63 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 ce.** clearly th
3c370 65 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 e mutex cannot b
3c380 65 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 e held if it doe
3c390 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 s not exist. Bu
3c3a0 74 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 t the.** the rea
3c3b0 73 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f son the mutex do
3c3c0 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 es not exist is
3c3d0 62 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c because the buil
3c3e0 64 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e d is not.** usin
3c3f0 67 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 g mutexes. And
3c400 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 we do not want t
3c410 68 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 he assert() cont
3c420 61 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 aining the.** ca
3c430 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 ll to sqlite3_mu
3c440 74 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 tex_held() to fa
3c450 69 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 il, so a non-zer
3c460 6f 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 o return is.** t
3c470 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 he appropriate t
3c480 68 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 hing to do. {H1
3c490 37 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 7086} The sqlite
3c4a0 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
3c4b0 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 ).** interface s
3c4c0 68 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 hould also retur
3c4d0 6e 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 n 1 when given a
3c4e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a NULL pointer..*
3c4f0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
3c500 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
3c510 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 eld(sqlite3_mute
3c520 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 x*);.SQLITE_API
3c530 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 int sqlite3_mute
3c540 78 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 x_notheld(sqlite
3c550 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 3_mutex*);../*.*
3c560 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 * CAPI3REF: Mute
3c570 78 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d x Types {H17001}
3c580 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 <H17000>.**.**
3c590 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 The [sqlite3_mut
3c5a0 65 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 ex_alloc()] inte
3c5b0 72 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 rface takes a si
3c5c0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a ngle argument.**
3c5d0 20 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 which is one of
3c5e0 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 these integer c
3c5f0 6f 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 onstants..**.**
3c600 54 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 The set of stati
3c610 63 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 c mutexes may ch
3c620 61 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 ange from one SQ
3c630 4c 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 Lite release to
3c640 74 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 the.** next. Ap
3c650 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 plications that
3c660 6f 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 override the bui
3c670 6c 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 lt-in mutex logi
3c680 63 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 c must be.** pre
3c690 70 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f pared to accommo
3c6a0 64 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 date additional
3c6b0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a static mutexes..
3c6c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3c6d0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 E_MUTEX_FAST
3c6e0 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 0.#defi
3c6f0 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
3c700 52 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 RECURSIVE
3c710 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 1.#define SQLIT
3c720 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
3c730 41 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 ASTER 2.#defi
3c740 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
3c750 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 STATIC_MEM
3c760 20 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 3 /* sqlite3_m
3c770 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 alloc() */.#defi
3c780 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ne SQLITE_MUTEX_
3c790 53 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 STATIC_MEM2
3c7a0 20 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 4 /* NOT USED
3c7b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3c7c0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f E_MUTEX_STATIC_O
3c7d0 50 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 PEN 4 /* s
3c7e0 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 qlite3BtreeOpen(
3c7f0 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ) */.#define SQL
3c800 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
3c810 5f 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a _PRNG 5 /*
3c820 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 sqlite3_random(
3c830 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ) */.#define SQL
3c840 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
3c850 5f 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a _LRU 6 /*
3c860 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a lru page list *
3c870 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3c880 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 _MUTEX_STATIC_LR
3c890 55 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 U2 7 /* lr
3c8a0 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a u page list */..
3c8b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3c8c0 52 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 Retrieve the mut
3c8d0 65 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 ex for a databas
3c8e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 e connection {H1
3c8f0 37 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 7002} <H17000>.*
3c900 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 *.** This interf
3c910 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f ace returns a po
3c920 69 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 inter the [sqlit
3c930 65 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 e3_mutex] object
3c940 20 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c that .** serial
3c950 69 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 izes access to t
3c960 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e he [database con
3c970 6e 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 nection] given i
3c980 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a n the argument.*
3c990 2a 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 * when the [thre
3c9a0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 ading mode] is S
3c9b0 65 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 erialized..** If
3c9c0 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 the [threading
3c9d0 6d 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d mode] is Single-
3c9e0 74 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d thread or Multi-
3c9f0 74 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 thread then this
3ca00 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 .** routine retu
3ca10 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 rns a NULL point
3ca20 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 er..*/.SQLITE_AP
3ca30 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
3ca40 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 *sqlite3_db_mute
3ca50 78 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a x(sqlite3*);../*
3ca60 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f .** CAPI3REF: Lo
3ca70 77 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 w-Level Control
3ca80 4f 66 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 Of Database File
3ca90 73 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 s {H11300} <S308
3caa0 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 00>.**.** {H1130
3cab0 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 1} The [sqlite3_
3cac0 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 file_control()]
3cad0 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 interface makes
3cae0 61 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f a direct call to
3caf0 20 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e the.** xFileCon
3cb00 74 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 trol method for
3cb10 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f the [sqlite3_io_
3cb20 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 methods] object
3cb30 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 associated.** wi
3cb40 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 th a particular
3cb50 64 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 database identif
3cb60 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e ied by the secon
3cb70 64 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 d argument. {H11
3cb80 33 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 302} The.** name
3cb90 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
3cba0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 is the name ass
3cbb0 69 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 igned to the dat
3cbc0 61 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 abase by the.**
3cbd0 3c 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 <a href="lang_at
3cbe0 74 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 tach.html">ATTAC
3cbf0 48 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e H</a> SQL comman
3cc00 64 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 d that opened th
3cc10 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b e.** database. {
3cc20 48 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 H11303} To contr
3cc30 6f 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 ol the main data
3cc40 62 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 base file, use t
3cc50 68 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a he name "main".*
3cc60 2a 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e * or a NULL poin
3cc70 74 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 ter. {H11304} Th
3cc80 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 e third and four
3cc90 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f th parameters to
3cca0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a this routine.**
3ccb0 20 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65 are passed dire
3ccc0 63 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 ctly through to
3ccd0 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 the second and t
3cce0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 hird parameters
3ccf0 6f 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 of.** the xFileC
3cd00 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 ontrol method.
3cd10 7b 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 {H11305} The ret
3cd20 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 urn value of the
3cd30 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a xFileControl.**
3cd40 20 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 method becomes
3cd50 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 the return value
3cd60 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
3cd70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d ..**.** {H11306}
3cd80 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 If the second p
3cd90 61 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d arameter (zDbNam
3cda0 65 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 e) does not matc
3cdb0 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e h the name of an
3cdc0 79 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 y.** open databa
3cdd0 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 se file, then SQ
3cde0 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 LITE_ERROR is re
3cdf0 74 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d turned. {H11307}
3ce00 20 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 This error.** c
3ce10 6f 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d ode is not remem
3ce20 62 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e bered and will n
3ce30 6f 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 ot be recalled b
3ce40 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f y [sqlite3_errco
3ce50 64 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c de()].** or [sql
3ce60 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 ite3_errmsg()].
3ce70 7b 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 {A11308} The und
3ce80 65 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e erlying xFileCon
3ce90 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 trol method migh
3cea0 74 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e t.** also return
3ceb0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 SQLITE_ERROR.
3cec0 7b 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 {A11309} There i
3ced0 73 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 s no way to dist
3cee0 69 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a inguish between.
3cef0 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 ** an incorrect
3cf00 7a 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 zDbName and an S
3cf10 51 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 QLITE_ERROR retu
3cf20 72 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 rn from the unde
3cf30 72 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 rlying.** xFileC
3cf40 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b ontrol method. {
3cf50 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 END}.**.** See a
3cf60 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e lso: [SQLITE_FCN
3cf70 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f TL_LOCKSTATE].*/
3cf80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
3cf90 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e sqlite3_file_con
3cfa0 74 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 trol(sqlite3*, c
3cfb0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 onst char *zDbNa
3cfc0 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 me, int op, void
3cfd0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 *);../*.** CAPI3
3cfe0 52 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 REF: Testing Int
3cff0 65 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 erface {H11400}
3d000 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 <S30800>.**.** T
3d010 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f he sqlite3_test_
3d020 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 control() interf
3d030 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 ace is used to r
3d040 65 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c ead out internal
3d050 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c .** state of SQL
3d060 69 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 ite and to injec
3d070 74 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 t faults into SQ
3d080 4c 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 Lite for testing
3d090 0a 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 .** purposes. T
3d0a0 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 he first paramet
3d0b0 65 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 er is an operati
3d0c0 6f 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 on code that det
3d0d0 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e ermines.** the n
3d0e0 75 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 umber, meaning,
3d0f0 61 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 and operation of
3d100 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 all subsequent
3d110 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a parameters..**.*
3d120 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 * This interface
3d130 20 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 is not for use
3d140 62 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e by applications.
3d150 20 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 It exists sole
3d160 6c 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 ly.** for verify
3d170 69 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 ing the correct
3d180 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
3d190 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e SQLite library.
3d1a0 20 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f Depending.** o
3d1b0 6e 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 n how the SQLite
3d1c0 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 library is comp
3d1d0 69 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 iled, this inter
3d1e0 66 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 face might not e
3d1f0 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 xist..**.** The
3d200 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f details of the o
3d210 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 peration codes,
3d220 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 their meanings,
3d230 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a the parameters.*
3d240 2a 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 * they take, and
3d250 20 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 what they do ar
3d260 65 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f e all subject to
3d270 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 change without
3d280 6e 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b notice..** Unlik
3d290 65 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 e most of the SQ
3d2a0 4c 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 Lite API, this f
3d2b0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 unction is not g
3d2c0 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 uaranteed to.**
3d2d0 6f 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 operate consiste
3d2e0 6e 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 ntly from one re
3d2f0 6c 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 lease to the nex
3d300 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
3d310 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 int sqlite3_tes
3d320 74 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 t_control(int op
3d330 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 , ...);../*.** C
3d340 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 API3REF: Testing
3d350 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 Interface Opera
3d360 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 tion Codes {H114
3d370 31 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 10} <H11400>.**.
3d380 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e ** These constan
3d390 74 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 ts are the valid
3d3a0 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 operation code
3d3b0 70 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a parameters used.
3d3c0 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 ** as the first
3d3d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c argument to [sql
3d3e0 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f ite3_test_contro
3d3f0 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 l()]..**.** Thes
3d400 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 e parameters and
3d410 20 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 their meanings
3d420 61 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 are subject to c
3d430 68 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 hange.** without
3d440 20 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 notice. These
3d450 76 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 values are for t
3d460 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 esting purposes
3d470 6f 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 only..** Applica
3d480 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 tions should not
3d490 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 use any of thes
3d4a0 65 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 e parameters or
3d4b0 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f the.** [sqlite3_
3d4c0 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 test_control()]
3d4d0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 interface..*/.#d
3d4e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 efine SQLITE_TES
3d4f0 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 TCTRL_PRNG_SAVE
3d500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
3d510 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3d520 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 TESTCTRL_PRNG_RE
3d530 53 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 STORE
3d540 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 6.#define SQLI
3d550 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 TE_TESTCTRL_PRNG
3d560 5f 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 _RESET
3d570 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 7.#define S
3d580 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 QLITE_TESTCTRL_B
3d590 49 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 ITVEC_TEST
3d5a0 20 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 8.#defin
3d5b0 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 e SQLITE_TESTCTR
3d5c0 4c 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 L_FAULT_INSTALL
3d5d0 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 9.#de
3d5e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 fine SQLITE_TEST
3d5f0 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c CTRL_BENIGN_MALL
3d600 4f 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a OC_HOOKS 10.
3d610 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 #define SQLITE_T
3d620 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f ESTCTRL_PENDING_
3d630 42 59 54 45 20 20 20 20 20 20 20 20 20 20 20 20 BYTE
3d640 31 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 11.#define SQLIT
3d650 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 E_TESTCTRL_ASSER
3d660 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
3d670 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 12.#define SQ
3d680 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c LITE_TESTCTRL_AL
3d690 57 41 59 53 20 20 20 20 20 20 20 20 20 20 20 20 WAYS
3d6a0 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 13.#define
3d6b0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c SQLITE_TESTCTRL
3d6c0 5f 52 45 53 45 52 56 45 20 20 20 20 20 20 20 20 _RESERVE
3d6d0 20 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 14../*.
3d6e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c ** CAPI3REF: SQL
3d6f0 69 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 ite Runtime Stat
3d700 75 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 us {H17200} <S60
3d710 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 200>.** EXPERIME
3d720 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 NTAL.**.** This
3d730 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 interface is use
3d740 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 d to retrieve ru
3d750 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 ntime status inf
3d760 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 ormation.** abou
3d770 74 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 t the preformanc
3d780 65 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 e of SQLite, and
3d790 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 optionally to r
3d7a0 65 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 eset various.**
3d7b0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e highwater marks.
3d7c0 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 The first argu
3d7d0 6d 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 ment is an integ
3d7e0 65 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 er code for.** t
3d7f0 68 65 20 73 70 65 63 69 66 69 63 20 70 61 72 61 he specific para
3d800 6d 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 meter to measure
3d810 2e 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e . Recognized in
3d820 74 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 teger codes.** a
3d830 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b re of the form [
3d840 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
3d850 4d 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 MORY_USED | SQLI
3d860 54 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a TE_STATUS_...]..
3d870 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 ** The current v
3d880 61 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 alue of the para
3d890 6d 65 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 meter is returne
3d8a0 64 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 d into *pCurrent
3d8b0 2e 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 ..** The highest
3d8c0 20 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 recorded value
3d8d0 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a is returned in *
3d8e0 70 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 pHighwater. If
3d8f0 74 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 the.** resetFlag
3d900 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 is true, then t
3d910 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 he highest recor
3d920 64 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 d value is reset
3d930 20 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 after.** *pHigh
3d940 77 61 74 65 72 20 69 73 20 77 72 69 74 74 65 6e water is written
3d950 2e 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 . Some parameter
3d960 73 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 s do not record
3d970 74 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 the highest.** v
3d980 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 alue. For those
3d990 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e parameters.** n
3d9a0 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 othing is writte
3d9b0 6e 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 n into *pHighwat
3d9c0 65 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 er and the reset
3d9d0 46 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e Flag is ignored.
3d9e0 0a 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 .** Other parame
3d9f0 74 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 ters record only
3da00 20 74 68 65 20 68 69 67 68 77 61 74 65 72 20 6d the highwater m
3da10 61 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 ark and not the
3da20 63 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 current.** value
3da30 2e 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 . For these lat
3da40 74 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e ter parameters n
3da50 6f 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 othing is writte
3da60 6e 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 n into *pCurrent
3da70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
3da80 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c tine returns SQL
3da90 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
3daa0 73 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f s and a non-zero
3dab0 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d .** [error code]
3dac0 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a on failure..**.
3dad0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
3dae0 69 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 is threadsafe bu
3daf0 74 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e t is not atomic.
3db00 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 This routine c
3db10 61 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 an be.** called
3db20 77 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 while other thre
3db30 61 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 ads are running
3db40 74 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 the same or diff
3db50 65 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 erent SQLite.**
3db60 69 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 interfaces. How
3db70 65 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 ever the values
3db80 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 returned in *pCu
3db90 72 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 rrent and.** *pH
3dba0 69 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 ighwater reflect
3dbb0 20 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 the status of S
3dbc0 51 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 QLite at differe
3dbd0 6e 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d nt points in tim
3dbe0 65 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 e.** and it is p
3dbf0 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f ossible that ano
3dc00 74 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 ther thread migh
3dc10 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 t change the par
3dc20 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 ameter.** in bet
3dc30 77 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 ween the times w
3dc40 68 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e hen *pCurrent an
3dc50 64 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 d *pHighwater ar
3dc60 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a e written..**.**
3dc70 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 See also: [sqli
3dc80 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d te3_db_status()]
3dc90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 .*/.SQLITE_API S
3dca0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 QLITE_EXPERIMENT
3dcb0 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 AL int sqlite3_s
3dcc0 74 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e tatus(int op, in
3dcd0 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 t *pCurrent, int
3dce0 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e *pHighwater, in
3dcf0 74 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a t resetFlag);...
3dd00 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
3dd10 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 Status Parameter
3dd20 73 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 s {H17250} <H172
3dd30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 00>.** EXPERIMEN
3dd40 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 TAL.**.** These
3dd50 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
3dd60 73 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 s designate vari
3dd70 6f 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 ous run-time sta
3dd80 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a tus parameters.*
3dd90 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 * that can be re
3dda0 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 turned by [sqlit
3ddb0 65 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a e3_status()]..**
3ddc0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e .** <dl>.** <dt>
3ddd0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 SQLITE_STATUS_ME
3dde0 4d 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a MORY_USED</dt>.*
3ddf0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
3de00 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72 72 eter is the curr
3de10 65 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 ent amount of me
3de20 6d 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 mory checked out
3de30 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 .** using [sqlit
3de40 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 e3_malloc()], ei
3de50 74 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 ther directly or
3de60 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 indirectly. Th
3de70 65 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c e.** figure incl
3de80 75 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 udes calls made
3de90 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c to [sqlite3_mall
3dea0 6f 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 oc()] by the app
3deb0 6c 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 lication.** and
3dec0 69 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 internal memory
3ded0 75 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c usage by the SQL
3dee0 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 ite library. Sc
3def0 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 ratch memory.**
3df00 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 controlled by [S
3df10 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 QLITE_CONFIG_SCR
3df20 41 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 ATCH] and auxili
3df30 61 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a ary page-cache.*
3df40 2a 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c * memory control
3df50 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 led by [SQLITE_C
3df60 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d ONFIG_PAGECACHE]
3df70 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 is not included
3df80 20 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 in.** this para
3df90 6d 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 meter. The amou
3dfa0 6e 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 nt returned is t
3dfb0 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c he sum of the al
3dfc0 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 location.** size
3dfd0 73 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 s as reported by
3dfe0 20 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f the xSize metho
3dff0 64 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 d in [sqlite3_me
3e000 6d 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e m_methods].</dd>
3e010 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
3e020 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f E_STATUS_MALLOC_
3e030 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 SIZE</dt>.** <dd
3e040 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 >This parameter
3e050 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 records the larg
3e060 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 est memory alloc
3e070 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a ation request.**
3e080 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 handed to [sqli
3e090 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 te3_malloc()] or
3e0a0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f [sqlite3_reallo
3e0b0 63 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a c()] (or their.*
3e0c0 2a 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 * internal equiv
3e0d0 61 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 alents). Only t
3e0e0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 he value returne
3e0f0 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 d in the.** *pHi
3e100 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 ghwater paramete
3e110 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 r to [sqlite3_st
3e120 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e atus()] is of in
3e130 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 terest. .** The
3e140 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 value written i
3e150 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e nto the *pCurren
3e160 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 t parameter is u
3e170 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a ndefined.</dd>.*
3e180 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3e190 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 STATUS_PAGECACHE
3e1a0 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 _USED</dt>.** <d
3e1b0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3e1c0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d returns the num
3e1d0 62 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 ber of pages use
3e1e0 64 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 d out of the.**
3e1f0 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 [pagecache memor
3e200 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 y allocator] tha
3e210 74 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 t was configured
3e220 20 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 using .** [SQLI
3e230 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 TE_CONFIG_PAGECA
3e240 43 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 CHE]. The.** va
3e250 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 lue returned is
3e260 69 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e in pages, not in
3e270 20 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a bytes.</dd>.**.
3e280 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 ** <dt>SQLITE_ST
3e290 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
3e2a0 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 VERFLOW</dt>.**
3e2b0 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3e2c0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
3e2d0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
3e2e0 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 f page cache.**
3e2f0 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 allocation which
3e300 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 could not be st
3e310 61 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 atisfied by the
3e320 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 [SQLITE_CONFIG_P
3e330 41 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 AGECACHE].** buf
3e340 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f fer and where fo
3e350 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 rced to overflow
3e360 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c to [sqlite3_mal
3e370 6c 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 loc()]. The.**
3e380 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 returned value i
3e390 6e 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 ncludes allocati
3e3a0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f ons that overflo
3e3b0 77 65 64 20 62 65 63 61 75 73 65 20 74 68 65 79 wed because they
3e3c0 0a 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 .** where too la
3e3d0 72 67 65 20 28 74 68 65 79 20 77 65 72 65 20 6c rge (they were l
3e3e0 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 arger than the "
3e3f0 73 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f sz" parameter to
3e400 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 .** [SQLITE_CONF
3e410 49 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 IG_PAGECACHE]) a
3e420 6e 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 nd allocations t
3e430 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 hat overflowed b
3e440 65 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 ecause.** no spa
3e450 63 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 ce was left in t
3e460 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f he page cache.</
3e470 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3e480 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 LITE_STATUS_PAGE
3e490 43 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a CACHE_SIZE</dt>.
3e4a0 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 ** <dd>This para
3e4b0 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 meter records th
3e4c0 65 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 e largest memory
3e4d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 allocation requ
3e4e0 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f est.** handed to
3e4f0 20 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f [pagecache memo
3e500 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 ry allocator].
3e510 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 Only the value r
3e520 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a eturned in the.*
3e530 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 * *pHighwater pa
3e540 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 rameter to [sqli
3e550 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 te3_status()] is
3e560 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a of interest. .
3e570 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 ** The value wri
3e580 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 tten into the *p
3e590 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 Current paramete
3e5a0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c r is undefined.<
3e5b0 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 /dd>.**.** <dt>S
3e5c0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 QLITE_STATUS_SCR
3e5d0 41 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a ATCH_USED</dt>.*
3e5e0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d * <dd>This param
3e5f0 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 eter returns the
3e600 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 number of alloc
3e610 61 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 ations used out
3e620 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 of the.** [scrat
3e630 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 ch memory alloca
3e640 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 tor] configured
3e650 75 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 using.** [SQLITE
3e660 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d _CONFIG_SCRATCH]
3e670 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 . The value ret
3e680 75 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f urned is in allo
3e690 63 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 cations, not.**
3e6a0 69 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 in bytes. Since
3e6b0 20 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 a single thread
3e6c0 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f may only have o
3e6d0 6e 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 ne scratch alloc
3e6e0 61 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e ation.** outstan
3e6f0 64 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 ding at time, th
3e700 69 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 is parameter als
3e710 6f 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 o reports the nu
3e720 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a mber of threads.
3e730 2a 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 ** using scratch
3e740 20 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 memory at the s
3e750 61 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a ame time.</dd>.*
3e760 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f *.** <dt>SQLITE_
3e770 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f STATUS_SCRATCH_O
3e780 56 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 VERFLOW</dt>.**
3e790 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 <dd>This paramet
3e7a0 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e er returns the n
3e7b0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
3e7c0 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 f scratch memory
3e7d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 .** allocation w
3e7e0 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 hich could not b
3e7f0 65 20 73 74 61 74 69 73 66 69 65 64 20 62 79 20 e statisfied by
3e800 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 the [SQLITE_CONF
3e810 49 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 IG_SCRATCH].** b
3e820 75 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 uffer and where
3e830 66 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c forced to overfl
3e840 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d ow to [sqlite3_m
3e850 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 alloc()]. The v
3e860 61 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 alues.** returne
3e870 64 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c d include overfl
3e880 6f 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 ows because the
3e890 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 requested alloca
3e8a0 74 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 tion was too.**
3e8b0 6c 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c larger (that is,
3e8c0 20 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 because the req
3e8d0 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f uested allocatio
3e8e0 6e 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 n was larger tha
3e8f0 6e 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 n the.** "sz" pa
3e900 72 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 rameter to [SQLI
3e910 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 TE_CONFIG_SCRATC
3e920 48 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 H]) and because
3e930 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 no scratch buffe
3e940 72 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 r.** slots were
3e950 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f available..** </
3e960 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 dd>.**.** <dt>SQ
3e970 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 LITE_STATUS_SCRA
3e980 54 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a TCH_SIZE</dt>.**
3e990 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 <dd>This parame
3e9a0 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 ter records the
3e9b0 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 largest memory a
3e9c0 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 llocation reques
3e9d0 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b t.** handed to [
3e9e0 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 scratch memory a
3e9f0 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 llocator]. Only
3ea00 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
3ea10 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 ned in the.** *p
3ea20 48 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 Highwater parame
3ea30 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f ter to [sqlite3_
3ea40 73 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 status()] is of
3ea50 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 interest. .** T
3ea60 68 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e he value written
3ea70 20 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 into the *pCurr
3ea80 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 ent parameter is
3ea90 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e undefined.</dd>
3eaa0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 .**.** <dt>SQLIT
3eab0 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f E_STATUS_PARSER_
3eac0 53 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 STACK</dt>.** <d
3ead0 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 d>This parameter
3eae0 20 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 records the dee
3eaf0 70 65 73 74 20 70 61 72 73 65 72 20 73 74 61 63 pest parser stac
3eb00 6b 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a k. It is only.*
3eb10 2a 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 * meaningful if
3eb20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
3eb30 65 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b ed with [YYTRACK
3eb40 4d 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c MAXSTACKDEPTH].<
3eb50 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a /dd>.** </dl>.**
3eb60 0a 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 .** New status p
3eb70 61 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 arameters may be
3eb80 20 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 added from time
3eb90 20 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 to time..*/.#de
3eba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 fine SQLITE_STAT
3ebb0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 US_MEMORY_USED
3ebc0 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 0.#defin
3ebd0 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f e SQLITE_STATUS_
3ebe0 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 PAGECACHE_USED
3ebf0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 1.#define S
3ec00 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 QLITE_STATUS_PAG
3ec10 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 ECACHE_OVERFLOW
3ec20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 2.#define SQLI
3ec30 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
3ec40 48 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 33 H_USED 3
3ec50 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
3ec60 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f STATUS_SCRATCH_O
3ec70 56 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 VERFLOW 4.#d
3ec80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 efine SQLITE_STA
3ec90 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 TUS_MALLOC_SIZE
3eca0 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 5.#defi
3ecb0 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ne SQLITE_STATUS
3ecc0 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 _PARSER_STACK
3ecd0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
3ece0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 SQLITE_STATUS_PA
3ecf0 47 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 GECACHE_SIZE
3ed00 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 7.#define SQL
3ed10 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
3ed20 43 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 CH_SIZE
3ed30 38 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 8../*.** CAPI3RE
3ed40 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e F: Database Conn
3ed50 65 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 ection Status {H
3ed60 31 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 17500} <S60200>.
3ed70 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
3ed80 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
3ed90 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 face is used to
3eda0 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 retrieve runtime
3edb0 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 status informat
3edc0 69 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 ion .** about a
3edd0 73 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 single [database
3ede0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 connection]. T
3edf0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
3ee00 74 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 t is the.** data
3ee10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
3ee20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 object to be int
3ee30 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 errogated. The
3ee40 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a second argument.
3ee50 2a 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 ** is the parame
3ee60 74 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 ter to interroga
3ee70 74 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 te. Currently,
3ee80 74 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 the only allowed
3ee90 20 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 value.** for th
3eea0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
3eeb0 65 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 er is [SQLITE_DB
3eec0 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 STATUS_LOOKASIDE
3eed0 5f 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 _USED]..** Addit
3eee0 69 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 ional options wi
3eef0 6c 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 ll likely appear
3ef00 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 in future relea
3ef10 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a ses of SQLite..*
3ef20 2a 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 *.** The current
3ef30 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 value of the re
3ef40 71 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 quested paramete
3ef50 72 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 r is written int
3ef60 6f 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 o *pCur.** and t
3ef70 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 he highest insta
3ef80 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 ntaneous value i
3ef90 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a s written into *
3efa0 70 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 pHiwtr. If.** t
3efb0 68 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 he resetFlg is t
3efc0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 rue, then the hi
3efd0 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 ghest instantane
3efe0 6f 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 ous value is.**
3eff0 72 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 reset back down
3f000 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 to the current v
3f010 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 alue..**.** See
3f020 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 also: [sqlite3_s
3f030 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 tatus()] and [sq
3f040 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 lite3_stmt_statu
3f050 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f s()]..*/.SQLITE_
3f060 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 API SQLITE_EXPER
3f070 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 IMENTAL int sqli
3f080 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 te3_db_status(sq
3f090 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 lite3*, int op,
3f0a0 69 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a int *pCur, int *
3f0b0 70 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 pHiwtr, int rese
3f0c0 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 tFlg);../*.** CA
3f0d0 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 PI3REF: Status P
3f0e0 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 arameters for da
3f0f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
3f100 6e 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 ns {H17520} <H17
3f110 35 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 500>.** EXPERIME
3f120 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 NTAL.**.** These
3f130 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 constants are t
3f140 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 he available int
3f150 65 67 65 72 20 22 76 65 72 62 73 22 20 74 68 61 eger "verbs" tha
3f160 74 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 t can be passed
3f170 61 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 as.** the second
3f180 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 argument to the
3f190 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 [sqlite3_db_sta
3f1a0 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 tus()] interface
3f1b0 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 ..**.** New verb
3f1c0 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 s may be added i
3f1d0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 n future release
3f1e0 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 s of SQLite. Exi
3f1f0 73 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d sting verbs.** m
3f200 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 ight be disconti
3f210 6e 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f nued. Applicatio
3f220 6e 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 ns should check
3f230 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 the return code
3f240 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 from.** [sqlite3
3f250 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f _db_status()] to
3f260 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 make sure that
3f270 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e the call worked.
3f280 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 .** The [sqlite3
3f290 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e _db_status()] in
3f2a0 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 terface will ret
3f2b0 75 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 urn a non-zero e
3f2c0 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 rror code.** if
3f2d0 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f a discontinued o
3f2e0 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 r unsupported ve
3f2f0 72 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a rb is invoked..*
3f300 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 *.** <dl>.** <dt
3f310 3e 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 >SQLITE_DBSTATUS
3f320 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c _LOOKASIDE_USED<
3f330 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 /dt>.** <dd>This
3f340 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 parameter retur
3f350 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
3f360 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 lookaside memor
3f370 79 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c y slots currentl
3f380 79 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 y.** checked out
3f390 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a .</dd>.** </dl>.
3f3a0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 */.#define SQLIT
3f3b0 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 E_DBSTATUS_LOOKA
3f3c0 53 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a SIDE_USED 0.
3f3d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
3f3e0 3a 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 : Prepared State
3f3f0 6d 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 ment Status {H17
3f400 35 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 550} <S60200>.**
3f410 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a EXPERIMENTAL.**
3f420 0a 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 .** Each prepare
3f430 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e d statement main
3f440 74 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a tains various.**
3f450 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 [SQLITE_STMTSTA
3f460 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 TUS_SORT | count
3f470 65 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 ers] that measur
3f480 65 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 e the number.**
3f490 6f 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 of times it has
3f4a0 70 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 performed specif
3f4b0 69 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 ic operations.
3f4c0 54 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 These counters c
3f4d0 61 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f an.** be used to
3f4e0 20 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 monitor the per
3f4f0 66 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 formance charact
3f500 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 eristics of the
3f510 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 prepared.** stat
3f520 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 ements. For exa
3f530 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d mple, if the num
3f540 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 ber of table ste
3f550 70 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 ps greatly excee
3f560 64 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 ds.** the number
3f570 20 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 of table search
3f580 65 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 es or result row
3f590 73 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 s, that would te
3f5a0 6e 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a nd to indicate.*
3f5b0 2a 20 74 68 61 74 20 74 68 65 20 70 72 65 70 61 * that the prepa
3f5c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 red statement is
3f5d0 20 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 using a full ta
3f5e0 62 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 ble scan rather
3f5f0 74 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 than.** an index
3f600 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 . .**.** This i
3f610 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 nterface is used
3f620 20 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 to retrieve and
3f630 20 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 reset counter v
3f640 61 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 alues from.** a
3f650 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d [prepared statem
3f660 65 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 ent]. The first
3f670 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
3f680 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
3f690 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f ent.** object to
3f6a0 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 be interrogated
3f6b0 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 . The second ar
3f6c0 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 gument.** is an
3f6d0 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 integer code for
3f6e0 20 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c a specific [SQL
3f6f0 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 ITE_STMTSTATUS_S
3f700 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a ORT | counter].*
3f710 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 * to be interrog
3f720 61 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 ated. .** The cu
3f730 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 rrent value of t
3f740 68 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 he requested cou
3f750 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 nter is returned
3f760 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 ..** If the rese
3f770 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 tFlg is true, th
3f780 65 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 en the counter i
3f790 73 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 s reset to zero
3f7a0 61 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e after this.** in
3f7b0 74 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 terface call ret
3f7c0 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 urns..**.** See
3f7d0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 also: [sqlite3_s
3f7e0 74 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 tatus()] and [sq
3f7f0 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
3f800 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 )]..*/.SQLITE_AP
3f810 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d I SQLITE_EXPERIM
3f820 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 ENTAL int sqlite
3f830 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 3_stmt_status(sq
3f840 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 lite3_stmt*, int
3f850 20 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 op,int resetFlg
3f860 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 );../*.** CAPI3R
3f870 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d EF: Status Param
3f880 65 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 eters for prepar
3f890 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 ed statements {H
3f8a0 31 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 17570} <H17550>.
3f8b0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a ** EXPERIMENTAL.
3f8c0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 **.** These prep
3f8d0 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 rocessor macros
3f8e0 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 define integer c
3f8f0 6f 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 odes that name c
3f900 6f 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 ounter.** values
3f910 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
3f920 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 the [sqlite3_st
3f930 6d 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 mt_status()] int
3f940 65 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d erface..** The m
3f950 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 eanings of the v
3f960 61 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 arious counters
3f970 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a are as follows:.
3f980 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 **.** <dl>.** <d
3f990 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 t>SQLITE_STMTSTA
3f9a0 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 TUS_FULLSCAN_STE
3f9b0 50 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 P</dt>.** <dd>Th
3f9c0 69 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 is is the number
3f9d0 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 of times that S
3f9e0 51 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 65 QLite has steppe
3f9f0 64 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 d forward in.**
3fa00 61 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 a table as part
3fa10 6f 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 of a full table
3fa20 73 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d scan. Large num
3fa30 62 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f bers for this co
3fa40 75 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 unter.** may ind
3fa50 69 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 icate opportunit
3fa60 69 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 ies for performa
3fa70 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 nce improvement
3fa80 74 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 through .** care
3fa90 66 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 ful use of indic
3faa0 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c es.</dd>.**.** <
3fab0 64 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 dt>SQLITE_STMTST
3fac0 41 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a ATUS_SORT</dt>.*
3fad0 2a 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 * <dd>This is th
3fae0 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 e number of sort
3faf0 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 operations that
3fb00 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a have occurred..
3fb10 2a 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 ** A non-zero va
3fb20 6c 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e lue in this coun
3fb30 74 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 ter may indicate
3fb40 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 an opportunity
3fb50 74 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e to.** improvemen
3fb60 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 t performance th
3fb70 72 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 rough careful us
3fb80 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 e of indices.</d
3fb90 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a d>.**.** </dl>.*
3fba0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
3fbb0 5f 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c _STMTSTATUS_FULL
3fbc0 53 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a SCAN_STEP 1.
3fbd0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 #define SQLITE_S
3fbe0 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 TMTSTATUS_SORT
3fbf0 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2../
3fc00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 *.** CAPI3REF: C
3fc10 75 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 ustom Page Cache
3fc20 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 Object.** EXPER
3fc30 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
3fc40 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
3fc50 20 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e type is opaque.
3fc60 20 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e It is implemen
3fc70 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c ted by.** the pl
3fc80 75 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 uggable module.
3fc90 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 The SQLite core
3fca0 20 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 has no knowledg
3fcb0 65 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 e of.** its size
3fcc0 20 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 or internal str
3fcd0 75 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 ucture and never
3fce0 20 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a deals with the.
3fcf0 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ** sqlite3_pcach
3fd00 65 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 e object except
3fd10 62 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 by holding and p
3fd20 61 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a assing pointers.
3fd30 2a 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 ** to the object
3fd40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c ..**.** See [sql
3fd50 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
3fd60 6f 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f ods] for additio
3fd70 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
3fd80 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
3fd90 63 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 ct sqlite3_pcach
3fda0 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
3fdb0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
3fdc0 46 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 F: Application D
3fdd0 65 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 efined Page Cach
3fde0 65 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 e..** KEYWORDS:
3fdf0 7b 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 {page cache}.**
3fe00 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a EXPERIMENTAL.**.
3fe10 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f ** The [sqlite3_
3fe20 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f config]([SQLITE_
3fe30 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 CONFIG_PCACHE],
3fe40 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 ...) interface c
3fe50 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 an.** register a
3fe60 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 n alternative pa
3fe70 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 ge cache impleme
3fe80 6e 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 ntation by passi
3fe90 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 ng in an .** ins
3fea0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c tance of the sql
3feb0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
3fec0 6f 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 ods structure. T
3fed0 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 he majority of t
3fee0 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f he .** heap memo
3fef0 72 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 ry used by SQLit
3ff00 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 e is used by the
3ff10 20 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 page cache to c
3ff20 61 63 68 65 20 64 61 74 61 20 72 65 61 64 20 0a ache data read .
3ff30 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 ** from, or read
3ff40 79 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 y to be written
3ff50 74 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 to, the database
3ff60 20 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d file. By implem
3ff70 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 enting a .** cus
3ff80 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 tom page cache u
3ff90 73 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 sing this API, a
3ffa0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 n application ca
3ffb0 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a n control more .
3ffc0 2a 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 ** precisely the
3ffd0 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 amount of memor
3ffe0 79 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 y consumed by SQ
3fff0 4c 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e Lite, the way in
40000 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 which .** that
40010 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 memory is alloca
40020 74 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 ted and released
40030 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 , and the polici
40040 65 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 es used to .** d
40050 65 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 etermine exactly
40060 20 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 which parts of
40070 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 a database file
40080 61 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 are cached and f
40090 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e or .** how long.
400a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 .**.** The conte
400b0 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 nts of the sqlit
400c0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 e3_pcache_method
400d0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 s structure are
400e0 63 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 copied to an.**
400f0 69 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 internal buffer
40100 62 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e by SQLite within
40110 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 the call to [sq
40120 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 lite3_config].
40130 48 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 Hence.** the app
40140 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 lication may dis
40150 63 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 card the paramet
40160 65 72 20 61 66 74 65 72 20 74 68 65 20 63 61 6c er after the cal
40170 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 l to.** [sqlite3
40180 5f 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 _config()] retur
40190 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 ns..**.** The xI
401a0 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 nit() method is
401b0 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 called once for
401c0 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 each call to [sq
401d0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
401e0 28 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 ()].** (usually
401f0 6f 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 only once during
40200 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 the lifetime of
40210 20 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 the process). I
40220 74 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 t is passed.** a
40230 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c copy of the sql
40240 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
40250 6f 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 ods.pArg value.
40260 49 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 It can be used t
40270 6f 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 o set.** up glob
40280 61 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 6e al structures an
40290 64 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 d mutexes requir
402a0 65 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d ed by the custom
402b0 20 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 page cache .**
402c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 implementation.
402d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 .**.** The xShut
402e0 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 down() method is
402f0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 called from wit
40300 68 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 hin [sqlite3_shu
40310 74 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 tdown()], .** if
40320 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e the application
40330 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 invokes this AP
40340 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 I. It can be use
40350 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a d to clean up .*
40360 2a 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e * any outstandin
40370 67 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f g resources befo
40380 72 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 re process shutd
40390 6f 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 own, if required
403a0 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 ..**.** SQLite h
403b0 6f 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d olds a [SQLITE_M
403c0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 UTEX_RECURSIVE]
403d0 6d 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e mutex when it in
403e0 76 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e vokes.** the xIn
403f0 69 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 it method, so th
40400 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e e xInit method n
40410 65 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 eed not be threa
40420 64 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 dsafe. The.** x
40430 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 Shutdown method
40440 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 is only called f
40450 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 rom [sqlite3_shu
40460 74 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 tdown()] so it d
40470 6f 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 oes.** not need
40480 74 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 65 to be threadsafe
40490 20 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 either. All ot
404a0 68 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 her methods must
404b0 20 62 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a be threadsafe.*
404c0 2a 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 * in multithread
404d0 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e ed applications.
404e0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 .**.** SQLite wi
404f0 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 ll never invoke
40500 78 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 xInit() more tha
40510 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 n once without a
40520 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a n intervening.**
40530 20 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f call to xShutdo
40540 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 wn()..**.** The
40550 78 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 xCreate() method
40560 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 is used to cons
40570 74 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 truct a new cach
40580 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c e instance. SQL
40590 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 ite.** will typi
405a0 63 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 cally create one
405b0 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 cache instance
405c0 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 for each open da
405d0 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 tabase file,.**
405e0 74 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e though this is n
405f0 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 ot guaranteed. T
40600 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 he.** first para
40610 6d 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 meter, szPage, i
40620 73 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 s the size in by
40630 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 tes of the pages
40640 20 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 that must.** be
40650 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 allocated by th
40660 65 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 e cache. szPage
40670 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 will not be a p
40680 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a ower of two. sz
40690 50 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 Page.** will the
406a0 20 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 page size of th
406b0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
406c0 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 that is to be ca
406d0 63 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 ched plus an.**
406e0 69 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 increment (here
406f0 63 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 called "R") of a
40700 62 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e bout 100 or 200.
40710 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 SQLite will us
40720 65 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 e the.** extra R
40730 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 bytes on each p
40740 61 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 age to store met
40750 61 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 adata about the
40760 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 underlying.** da
40770 74 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 tabase page on d
40780 69 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 isk. The value
40790 6f 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 of R depends.**
407a0 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 on the SQLite ve
407b0 72 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 rsion, the targe
407c0 74 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 t platform, and
407d0 68 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 how SQLite was c
407e0 6f 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 ompiled..** R is
407f0 20 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 constant for a
40800 70 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 particular build
40810 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 of SQLite. The
40820 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
40830 20 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 to.** xCreate()
40840 2c 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 , bPurgeable, is
40850 20 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 true if the cac
40860 68 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 he being created
40870 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 will.** be used
40880 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 to cache databa
40890 73 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 se pages of a fi
408a0 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 le stored on dis
408b0 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 k, or.** false i
408c0 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 f it is used for
408d0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
408e0 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 tabase. The cach
408f0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
40900 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 .** does not hav
40910 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 e to do anything
40920 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 special based w
40930 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ith the value of
40940 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 bPurgeable;.**
40950 69 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 it is purely adv
40960 69 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 isory. On a cac
40970 68 65 20 77 68 65 72 65 20 62 50 75 72 67 65 61 he where bPurgea
40980 62 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 ble is false, SQ
40990 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 Lite will.** nev
409a0 65 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e er invoke xUnpin
409b0 28 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c () except to del
409c0 69 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 iberately delete
409d0 20 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f a page..** In o
409e0 74 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 ther words, a ca
409f0 63 68 65 20 63 72 65 61 74 65 64 20 77 69 74 68 che created with
40a00 20 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 bPurgeable set
40a10 74 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a to false will.**
40a20 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 never contain a
40a30 6e 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 ny unpinned page
40a40 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 s..**.** The xCa
40a50 63 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 chesize() method
40a60 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 may be called a
40a70 74 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 t any time by SQ
40a80 4c 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a Lite to set the.
40a90 2a 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 ** suggested max
40aa0 69 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 imum cache-size
40ab0 28 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 (number of pages
40ac0 20 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 stored by) the
40ad0 63 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 cache.** instanc
40ae0 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 e passed as the
40af0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 first argument.
40b00 54 68 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 This is the valu
40b10 65 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 e configured usi
40b20 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 ng.** the SQLite
40b30 20 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f "[PRAGMA cache_
40b40 73 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 size]" command.
40b50 41 73 20 77 69 74 68 20 74 68 65 20 62 50 75 72 As with the bPur
40b60 67 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 geable parameter
40b70 2c 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 ,.** the impleme
40b80 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 ntation is not r
40b90 65 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e equired to do an
40ba0 79 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 ything with this
40bb0 0a 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 .** value; it is
40bc0 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a advisory only..
40bd0 2a 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 **.** The xPagec
40be0 6f 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 ount() method sh
40bf0 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 ould return the
40c00 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
40c10 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f currently.** sto
40c20 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 red in the cache
40c30 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 ..** .** The xFe
40c40 74 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 tch() method is
40c50 75 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 used to fetch a
40c60 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 page and return
40c70 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e a pointer to it.
40c80 20 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 .** A 'page', i
40c90 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 n this context,
40ca0 69 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 is a buffer of s
40cb0 7a 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 zPage bytes alig
40cc0 6e 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 ned at an.** 8-b
40cd0 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 yte boundary. Th
40ce0 65 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 e page to be fet
40cf0 63 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e ched is determin
40d00 65 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 ed by the key. T
40d10 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 he.** mimimum ke
40d20 79 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 y value is 1. Af
40d30 74 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 ter it has been
40d40 72 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 retrieved using
40d50 78 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 xFetch, the page
40d60 20 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 .** is consider
40d70 65 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 ed to be "pinned
40d80 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 "..**.** If the
40d90 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
40da0 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 s already in the
40db0 20 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 page cache, the
40dc0 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 n the page cache
40dd0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
40de0 6f 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 on must return a
40df0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
40e00 70 61 67 65 20 62 75 66 66 65 72 20 77 69 74 68 page buffer with
40e10 20 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 its content.**
40e20 69 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 intact. If the
40e30 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 requested page i
40e40 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e s not already in
40e50 20 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e the cache, then
40e60 20 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 the.** behavior
40e70 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 6d of the cache im
40e80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 plementation is
40e90 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 determined by th
40ea0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a e value of the.*
40eb0 2a 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 * createFlag par
40ec0 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f ameter passed to
40ed0 20 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 xFetch, accordi
40ee0 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 ng to the follow
40ef0 69 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a ing table:.**.**
40f00 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 <table border=1
40f10 20 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e width=85% align
40f20 3d 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e =center>.** <tr>
40f30 3c 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 <th> createFlag
40f40 3c 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 <th> Behaviour w
40f50 68 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 hen page is not
40f60 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 already in cache
40f70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c .** <tr><td> 0 <
40f80 74 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 td> Do not alloc
40f90 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 ate a new page.
40fa0 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a Return NULL..**
40fb0 20 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e <tr><td> 1 <td>
40fc0 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 Allocate a new
40fd0 70 61 67 65 20 69 66 20 69 74 20 65 61 73 79 20 page if it easy
40fe0 61 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 and convenient t
40ff0 6f 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 o do so..**
41000 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 Othe
41010 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c rwise return NUL
41020 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 L..** <tr><td> 2
41030 20 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 <td> Make every
41040 20 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 effort to alloc
41050 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 ate a new page.
41060 20 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 Only return.**
41070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41080 4e 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 NULL if allocati
41090 6e 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 ng a new page is
410a0 20 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 effectively imp
410b0 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 ossible..** </ta
410c0 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 ble>.**.** SQLit
410d0 65 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 e will normally
410e0 69 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 invoke xFetch()
410f0 77 69 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 with a createFla
41100 67 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 g of 0 or 1. If
41110 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 .** a call to xF
41120 65 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 etch() with crea
41130 74 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e teFlag==1 return
41140 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c s NULL, then SQL
41150 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 ite will.** atte
41160 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 mpt to unpin one
41170 20 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 or more cache p
41180 61 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 ages by spilling
41190 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a the content of.
411a0 2a 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 ** pinned pages
411b0 74 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 to disk and sync
411c0 68 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 hing the operati
411d0 6e 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 ng system disk c
411e0 61 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 ache. After.** a
411f0 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 ttempting to unp
41200 69 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 in pages, the xF
41210 65 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 etch() method wi
41220 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 ll be invoked ag
41230 61 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 ain with.** a cr
41240 65 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a eateFlag of 2..*
41250 2a 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 *.** xUnpin() is
41260 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 called by SQLit
41270 65 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 e with a pointer
41280 20 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 to a currently
41290 70 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 pinned page.** a
412a0 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 s its second arg
412b0 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 ument. If the th
412c0 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 ird parameter, d
412d0 69 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a iscard, is non-z
412e0 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 ero,.** then the
412f0 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 page should be
41300 65 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 evicted from the
41310 20 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 cache. In this
41320 63 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 case SQLite .**
41330 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 assumes that the
41340 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 next time the p
41350 61 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 age is retrieved
41360 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 from the cache
41370 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 using.** the xFe
41380 74 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 tch() method, it
41390 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e will be zeroed.
413a0 20 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 If the discard
413b0 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 parameter is.**
413c0 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 zero, then the p
413d0 61 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 age is considere
413e0 64 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 d to be unpinned
413f0 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c . The cache impl
41400 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 ementation.** ma
41410 79 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 y choose to evic
41420 74 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 t unpinned pages
41430 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a at any time..**
41440 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 .** The cache is
41450 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f not required to
41460 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 perform any ref
41470 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e erence counting.
41480 20 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 A single .** ca
41490 6c 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 ll to xUnpin() u
414a0 6e 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 npins the page r
414b0 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 egardless of the
414c0 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 number of prior
414d0 20 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 calls .** to xF
414e0 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 etch()..**.** Th
414f0 65 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f e xRekey() metho
41500 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 d is used to cha
41510 6e 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 nge the key valu
41520 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
41530 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 h the.** page pa
41540 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f ssed as the seco
41550 6e 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d nd argument from
41560 20 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 oldKey to newKe
41570 79 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 0a y. If the cache.
41580 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f ** previously co
41590 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 ntains an entry
415a0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
415b0 6e 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c newKey, it shoul
415c0 64 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 d be.** discarde
415d0 64 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 d. Any prior cac
415e0 68 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 he entry associa
415f0 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 ted with newKey
41600 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f is guaranteed no
41610 74 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 t.** to be pinne
41620 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 d..**.** When SQ
41630 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 Lite calls the x
41640 54 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f Truncate() metho
41650 64 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 d, the cache mus
41660 74 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a t discard all.**
41670 20 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 existing cache
41680 65 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 67 entries with pag
41690 65 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 e numbers (keys)
416a0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 greater than or
416b0 20 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 equal.** to the
416c0 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c value of the iL
416d0 69 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 imit parameter p
416e0 61 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 assed to xTrunca
416f0 74 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 te(). If any.**
41700 6f 66 20 74 68 65 73 65 20 70 61 67 65 73 20 61 of these pages a
41710 72 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 re pinned, they
41720 61 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 are implicitly u
41730 6e 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 npinned, meaning
41740 20 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 that.** they ca
41750 6e 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 n be safely disc
41760 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 arded..**.** The
41770 20 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 xDestroy() meth
41780 6f 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 od is used to de
41790 6c 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c lete a cache all
417a0 6f 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74 ocated by xCreat
417b0 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f e()..** All reso
417c0 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 urces associated
417d0 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 with the specif
417e0 69 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 ied cache should
417f0 20 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 be freed. After
41800 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 .** calling the
41810 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f xDestroy() metho
41820 64 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 d, SQLite consid
41830 65 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 ers the [sqlite3
41840 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e _pcache*].** han
41850 64 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 dle invalid, and
41860 20 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 will not use it
41870 20 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 with any other
41880 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d sqlite3_pcache_m
41890 65 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 ethods.** functi
418a0 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ons..*/.typedef
418b0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 struct sqlite3_p
418c0 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 cache_methods sq
418d0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
418e0 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c hods;.struct sql
418f0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 ite3_pcache_meth
41900 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 ods {. void *pA
41910 72 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 rg;. int (*xIni
41920 74 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 t)(void*);. voi
41930 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 d (*xShutdown)(v
41940 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 oid*);. sqlite3
41950 5f 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 _pcache *(*xCrea
41960 74 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 te)(int szPage,
41970 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b int bPurgeable);
41980 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 . void (*xCache
41990 73 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 size)(sqlite3_pc
419a0 61 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 ache*, int nCach
419b0 65 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a esize);. int (*
419c0 78 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 xPagecount)(sqli
419d0 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 te3_pcache*);.
419e0 76 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 void *(*xFetch)(
419f0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c sqlite3_pcache*,
41a00 20 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 unsigned key, i
41a10 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a nt createFlag);.
41a20 20 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 void (*xUnpin)
41a30 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a (sqlite3_pcache*
41a40 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 , void*, int dis
41a50 63 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a card);. void (*
41a60 78 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f xRekey)(sqlite3_
41a70 70 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 pcache*, void*,
41a80 75 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c unsigned oldKey,
41a90 20 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 unsigned newKey
41aa0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 );. void (*xTru
41ab0 6e 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 ncate)(sqlite3_p
41ac0 63 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 cache*, unsigned
41ad0 20 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 iLimit);. void
41ae0 20 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c (*xDestroy)(sql
41af0 69 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d ite3_pcache*);.}
41b00 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 ;../*.** CAPI3RE
41b10 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 F: Online Backup
41b20 20 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 Object.** EXPER
41b30 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 IMENTAL.**.** Th
41b40 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 e sqlite3_backup
41b50 20 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 object records
41b60 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f state informatio
41b70 6e 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 n about an ongoi
41b80 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 ng.** online bac
41b90 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 kup operation.
41ba0 54 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b The sqlite3_back
41bb0 75 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 up object is cre
41bc0 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c ated by.** a cal
41bd0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 l to [sqlite3_ba
41be0 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 ckup_init()] and
41bf0 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 is destroyed by
41c00 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 a call to.** [s
41c10 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
41c20 6e 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 nish()]..**.** S
41c30 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 ee Also: [Using
41c40 74 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e the SQLite Onlin
41c50 65 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f e Backup API].*/
41c60 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
41c70 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 sqlite3_backup s
41c80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a qlite3_backup;..
41c90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 /*.** CAPI3REF:
41ca0 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 Online Backup AP
41cb0 49 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 I..** EXPERIMENT
41cc0 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 AL.**.** This AP
41cd0 49 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 I is used to ove
41ce0 72 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 rwrite the conte
41cf0 6e 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 nts of one datab
41d00 61 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a ase with that.**
41d10 20 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 of another. It
41d20 69 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 is useful either
41d30 20 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 for creating ba
41d40 63 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 ckups of databas
41d50 65 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 es or.** for cop
41d60 79 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 ying in-memory d
41d70 61 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 atabases to or f
41d80 72 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 rom persistent f
41d90 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 iles. .**.** See
41da0 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 Also: [Using th
41db0 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 e SQLite Online
41dc0 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a Backup API].**.*
41dd0 2a 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 * Exclusive acce
41de0 73 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 ss is required t
41df0 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
41e00 6e 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 n database for t
41e10 68 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 he .** duration
41e20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e of the operation
41e30 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f . However the so
41e40 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 urce database is
41e50 20 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f only.** read-lo
41e60 63 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 cked while it is
41e70 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 actually being
41e80 72 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 read, it is not
41e90 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e locked.** contin
41ea0 75 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 uously for the e
41eb0 6e 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e ntire operation.
41ec0 20 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 Thus, the backu
41ed0 70 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 p may be.** perf
41ee0 6f 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 ormed on a live
41ef0 64 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 database without
41f00 20 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 preventing othe
41f10 72 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 r users from.**
41f20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 writing to the d
41f30 61 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 atabase for an e
41f40 78 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f xtended period o
41f50 66 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 f time..** .** T
41f60 6f 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b o perform a back
41f70 75 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a up operation: .*
41f80 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 * <ol>.**
41f90 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 <li><b>sqlite3_b
41fa0 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e ackup_init()</b>
41fb0 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 is called once
41fc0 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 to initialize th
41fd0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 e.** bac
41fe0 6b 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 kup, .** <li
41ff0 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b ><b>sqlite3_back
42000 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 up_step()</b> is
42010 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d called one or m
42020 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 ore times to tra
42030 6e 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 nsfer .**
42040 20 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65 the data betwe
42050 65 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 en the two datab
42060 61 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c ases, and finall
42070 79 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e y.** <li><b>
42080 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
42090 69 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 inish()</b> is c
420a0 61 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 alled to release
420b0 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a all resources .
420c0 2a 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 ** assoc
420d0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 iated with the b
420e0 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e ackup operation.
420f0 20 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 .** </ol>.**
42100 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 There should be
42110 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c exactly one call
42120 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
42130 75 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 up_finish() for
42140 65 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 each.** successf
42150 75 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 ul call to sqlit
42160 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
42170 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 ..**.** <b>sqlit
42180 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
42190 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 </b>.**.** The f
421a0 69 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e irst two argumen
421b0 74 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 ts passed to [sq
421c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 lite3_backup_ini
421d0 74 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 t()] are the dat
421e0 61 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 abase.** handle
421f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
42200 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
42210 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 database and the
42220 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a database name .
42230 2a 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 ** used to attac
42240 68 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f h the destinatio
42250 6e 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 n database to th
42260 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 e handle. The da
42270 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 tabase name.** i
42280 73 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 s "main" for the
42290 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 main database,
422a0 22 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 "temp" for the t
422b0 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 emporary databas
422c0 65 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d e, or.** the nam
422d0 65 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 e specified as p
422e0 61 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 art of the [ATTA
422f0 43 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 CH] statement if
42300 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
42310 20 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 is.** an attach
42320 65 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 ed database. The
42330 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 third and fourt
42340 68 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 h arguments pass
42350 65 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 ed to .** sqlite
42360 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 3_backup_init()
42370 69 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 identify the [da
42380 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
42390 6e 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 n].** and databa
423a0 73 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 se name used.**
423b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 6f to access the so
423c0 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 urce database. T
423d0 68 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 he values passed
423e0 20 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 for the source
423f0 61 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 and .** destinat
42400 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f ion [database co
42410 6e 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 nnection] parame
42420 74 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 ters must not be
42430 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a the same..**.**
42440 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
42450 75 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 urs within sqlit
42460 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 e3_backup_init()
42470 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 , then NULL is r
42480 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 eturned.** and a
42490 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 n error code and
424a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 error message w
424b0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
424c0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 [database connec
424d0 74 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 tion] .** passed
424e0 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
424f0 67 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 gument. They may
42500 20 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 be retrieved us
42510 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 ing the.** [sqli
42520 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 te3_errcode()],
42530 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 [sqlite3_errmsg(
42540 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 )], and [sqlite3
42550 5f 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e _errmsg16()] fun
42560 63 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 ctions..** Other
42570 77 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 wise, if success
42580 66 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 ful, a pointer t
42590 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 o an [sqlite3_ba
425a0 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a ckup] object is.
425b0 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 ** returned. Thi
425c0 73 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 s pointer may be
425d0 20 75 73 65 64 20 77 69 74 68 20 74 68 65 20 73 used with the s
425e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
425f0 65 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 ep() and.** sqli
42600 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 te3_backup_finis
42610 68 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f h() functions to
42620 20 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 perform the spe
42630 63 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a cified backup .*
42640 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a * operation..**.
42650 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 ** <b>sqlite3_ba
42660 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a ckup_step()</b>.
42670 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b **.** Function [
42680 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
42690 74 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 tep()] is used t
426a0 6f 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 o copy up to nPa
426b0 67 65 20 70 61 67 65 73 20 62 65 74 77 65 65 6e ge pages between
426c0 20 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 .** the source
426d0 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 and destination
426e0 64 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 databases, where
426f0 20 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 61 nPage is the va
42700 6c 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 lue of the .** s
42710 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 econd parameter
42720 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
42730 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 3_backup_step().
42740 20 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e If nPage is a n
42750 65 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 egative.** value
42760 2c 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 , all remaining
42770 73 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 65 source pages are
42780 20 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 copied. If the
42790 72 65 71 75 69 72 65 64 20 70 61 67 65 73 20 61 required pages a
427a0 72 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c re .** succesful
427b0 6c 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 ly copied, but t
427c0 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d here are still m
427d0 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 ore pages to cop
427e0 79 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a y before the .**
427f0 20 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c backup is compl
42800 65 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 ete, it returns
42810 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 [SQLITE_OK]. If
42820 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 no error occured
42830 20 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 and there .** a
42840 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 re no more pages
42850 20 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b to copy, then [
42860 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 SQLITE_DONE] is
42870 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 returned. If an
42880 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 error .** occurs
42890 2c 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 , then an SQLite
428a0 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
428b0 65 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c eturned. As well
428c0 20 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 as [SQLITE_OK]
428d0 61 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 and.** [SQLITE_D
428e0 4f 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 ONE], a call to
428f0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
42900 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e tep() may return
42910 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c [SQLITE_READONL
42920 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e Y],.** [SQLITE_N
42930 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 OMEM], [SQLITE_B
42940 55 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f USY], [SQLITE_LO
42950 43 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 CKED], or an.**
42960 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 [SQLITE_IOERR_AC
42970 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f CESS | SQLITE_IO
42980 45 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 ERR_XXX] extende
42990 64 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a d error code..**
429a0 0a 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 .** As well as t
429b0 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 he case where th
429c0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 e destination da
429d0 74 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 tabase file was
429e0 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 opened for.** re
429f0 61 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 ad-only access,
42a00 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
42a10 74 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e tep() may return
42a20 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c [SQLITE_READONL
42a30 59 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 Y] if.** the des
42a40 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 tination is an i
42a50 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 n-memory databas
42a60 65 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 e with a differe
42a70 6e 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 nt page size.**
42a80 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 from the source
42a90 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
42aa0 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 If sqlite3_backu
42ab0 70 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 p_step() cannot
42ac0 6f 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 obtain a require
42ad0 64 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f d file-system lo
42ae0 63 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 ck, then.** the
42af0 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 [sqlite3_busy_ha
42b00 6e 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e ndler | busy-han
42b10 64 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a dler function].*
42b20 2a 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 * is invoked (if
42b30 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 one is specifie
42b40 64 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 d). If the .** b
42b50 75 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 usy-handler retu
42b60 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 rns non-zero bef
42b70 6f 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 ore the lock is
42b80 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 available, then
42b90 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 .** [SQLITE_BUSY
42ba0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f ] is returned to
42bb0 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 the caller. In
42bc0 74 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 this case the ca
42bd0 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 ll to.** sqlite3
42be0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 _backup_step() c
42bf0 61 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 an be retried la
42c00 74 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 ter. If the sour
42c10 63 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 ce.** [database
42c20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 connection].** i
42c30 73 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 s being used to
42c40 77 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 write to the sou
42c50 72 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65 rce database whe
42c60 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 n sqlite3_backup
42c70 5f 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 _step().** is ca
42c80 6c 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 lled, then [SQLI
42c90 54 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 TE_LOCKED] is re
42ca0 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 turned immediate
42cb0 6c 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 ly. Again, in th
42cc0 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 is.** case the c
42cd0 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 all to sqlite3_b
42ce0 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e ackup_step() can
42cf0 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 be retried late
42d00 72 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c r on. If.** [SQL
42d10 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 ITE_IOERR_ACCESS
42d20 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f | SQLITE_IOERR_
42d30 58 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f XXX], [SQLITE_NO
42d40 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c MEM], or.** [SQL
42d50 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 ITE_READONLY] is
42d60 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 returned, then
42d70 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 .** there is no
42d80 70 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e point in retryin
42d90 67 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 g the call to sq
42da0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
42db0 70 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 p(). These .** e
42dc0 72 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 rrors are consid
42dd0 65 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 ered fatal. At t
42de0 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 his point the ap
42df0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 plication must a
42e00 63 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 ccept .** that t
42e10 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 he backup operat
42e20 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 ion has failed a
42e30 6e 64 20 70 61 73 73 20 74 68 65 20 62 61 63 6b nd pass the back
42e40 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e up operation han
42e50 64 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 dle .** to the s
42e60 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
42e70 6e 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 nish() to releas
42e80 65 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 e associated res
42e90 6f 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f ources..**.** Fo
42ea0 6c 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 llowing the firs
42eb0 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 t call to sqlite
42ec0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 3_backup_step(),
42ed0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
42ee0 63 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 ck is.** obtaine
42ef0 64 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 d on the destina
42f00 74 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 tion file. It is
42f10 20 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e not released un
42f20 74 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 til either .** s
42f30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
42f40 6e 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 nish() is called
42f50 20 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f or the backup o
42f60 70 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 peration is comp
42f70 6c 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c lete .** and sql
42f80 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 ite3_backup_step
42f90 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 () returns [SQLI
42fa0 54 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 TE_DONE]. Additi
42fb0 6f 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d onally, each tim
42fc0 65 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 e .** a call to
42fd0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
42fe0 74 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 tep() is made a
42ff0 5b 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 [shared lock] is
43000 20 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 obtained on.**
43010 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
43020 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c ase file. This l
43030 6f 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 ock is released
43040 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 before the.** sq
43050 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
43060 70 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 p() call returns
43070 2e 20 42 65 63 61 75 73 65 20 74 68 65 20 73 6f . Because the so
43080 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 urce database is
43090 20 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 not.** locked b
430a0 65 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 etween calls to
430b0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
430c0 74 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 tep(), it may be
430d0 20 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 modified mid-wa
430e0 79 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 y.** through the
430f0 20 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 backup procedur
43100 65 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 e. If the source
43110 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 database is mod
43120 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 ified by an.** e
43130 78 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 xternal process
43140 6f 72 20 76 69 61 20 61 20 64 61 74 61 62 61 73 or via a databas
43150 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 e connection oth
43160 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 er than the one
43170 62 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 being.** used by
43180 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 the backup oper
43190 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 ation, then the
431a0 62 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 backup will be t
431b0 72 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 ransparently.**
431c0 72 65 73 74 61 72 74 65 64 20 62 79 20 74 68 65 restarted by the
431d0 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 next call to sq
431e0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
431f0 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 p(). If the sour
43200 63 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 ce .** database
43210 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 is modified by t
43220 68 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d he using the sam
43230 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
43240 63 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 ction as is used
43250 0a 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 .** by the backu
43260 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 p operation, the
43270 6e 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 n the backup dat
43280 61 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 abase is transpa
43290 72 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 rently .** updat
432a0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 ed at the same t
432b0 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 ime..**.** <b>sq
432c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
432d0 69 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 ish()</b>.**.**
432e0 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 Once sqlite3_bac
432f0 6b 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 kup_step() has r
43300 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f eturned [SQLITE_
43310 44 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 DONE], or when t
43320 68 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 he .** applicati
43330 6f 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 on wishes to aba
43340 6e 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 ndon the backup
43350 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b operation, the [
43360 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a sqlite3_backup].
43370 2a 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 ** object should
43380 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 be passed to sq
43390 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
433a0 69 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 ish(). This rele
433b0 61 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f ases all.** reso
433c0 75 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 urces associated
433d0 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 with the backup
433e0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 operation. If s
433f0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
43400 65 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 ep().** has not
43410 79 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 yet returned [SQ
43420 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e LITE_DONE], then
43430 20 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 any active writ
43440 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e e-transaction on
43450 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 the.** destinat
43460 69 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 ion database is
43470 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 rolled back. The
43480 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 [sqlite3_backup
43490 5d 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 ] object is inva
434a0 6c 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e lid.** and may n
434b0 6f 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f ot be used follo
434c0 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 wing a call to s
434d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 qlite3_backup_fi
434e0 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 nish()..**.** Th
434f0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
43500 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b by sqlite3_back
43510 75 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 up_finish is [SQ
43520 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 LITE_OK] if no e
43530 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 rror.** occurred
43540 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 , regardless or
43550 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 whether or not s
43560 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
43570 65 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a ep() was called.
43580 2a 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 ** a sufficient
43590 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
435a0 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 to complete the
435b0 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
435c0 2e 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f . Or, if.** an o
435d0 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e ut-of-memory con
435e0 64 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 dition or IO err
435f0 6f 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e or occured durin
43600 67 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 g a call to.** s
43610 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
43620 65 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 ep() then [SQLIT
43630 45 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a E_NOMEM] or an.*
43640 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f * [SQLITE_IOERR_
43650 41 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f ACCESS | SQLITE_
43660 49 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 IOERR_XXX] error
43670 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 code.** is retu
43680 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 rned. In this ca
43690 73 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 se the error cod
436a0 65 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d e and an error m
436b0 65 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 essage are.** wr
436c0 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 itten to the des
436d0 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 tination [databa
436e0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a se connection]..
436f0 2a 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f **.** A return o
43700 66 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 f [SQLITE_BUSY]
43710 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 or [SQLITE_LOCKE
43720 44 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f D] from sqlite3_
43730 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 backup_step() is
43740 0a 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e .** not a perman
43750 65 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f ent error and do
43760 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 es not affect th
43770 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f e return value o
43780 66 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 f.** sqlite3_bac
43790 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a kup_finish()..**
437a0 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 .** <b>sqlite3_b
437b0 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
437c0 29 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ), sqlite3_backu
437d0 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 p_pagecount()</b
437e0 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c >.**.** Each cal
437f0 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 l to sqlite3_bac
43800 6b 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 kup_step() sets
43810 74 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 two values store
43820 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 d internally.**
43830 62 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 by an [sqlite3_b
43840 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 ackup] object. T
43850 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
43860 65 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 es still to be b
43870 61 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 acked.** up, whi
43880 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 ch may be querie
43890 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 d by sqlite3_bac
438a0 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c kup_remaining(),
438b0 20 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a and the total.*
438c0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * number of page
438d0 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 s in the source
438e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 database file, w
438f0 68 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 hich may be quer
43900 69 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 ied by.** sqlite
43910 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 3_backup_pagecou
43920 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 nt()..**.** The
43930 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 values returned
43940 62 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f by these functio
43950 6e 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 ns are only upda
43960 74 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 ted by.** sqlite
43970 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 3_backup_step().
43980 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 If the source d
43990 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 atabase is modif
439a0 69 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 ied during a bac
439b0 6b 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e kup.** operation
439c0 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 , then the value
439d0 73 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 s are not update
439e0 64 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 d to account for
439f0 20 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 any extra.** pa
43a00 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ges that need to
43a10 20 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 be updated or t
43a20 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 he size of the s
43a30 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 ource database f
43a40 69 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e ile.** changing.
43a50 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 .**.** <b>Concur
43a60 72 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 rent Usage of Da
43a70 74 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f tabase Handles</
43a80 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 b>.**.** The sou
43a90 72 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f rce [database co
43aa0 6e 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 nnection] may be
43ab0 20 75 73 65 64 20 62 79 20 74 68 65 20 61 70 70 used by the app
43ac0 6c 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 lication for oth
43ad0 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 er.** purposes w
43ae0 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 hile a backup op
43af0 65 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 eration is under
43b00 77 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 way or being ini
43b10 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 tialized..** If
43b20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c SQLite is compil
43b30 65 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 ed and configure
43b40 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 d to support thr
43b50 65 61 64 73 61 66 65 20 64 61 74 61 62 61 73 65 eadsafe database
43b60 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c .** connections,
43b70 20 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 then the source
43b80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
43b90 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 tion may be used
43ba0 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a concurrently.**
43bb0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 from within oth
43bc0 65 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a er threads..**.*
43bd0 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 * However, the a
43be0 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 pplication must
43bf0 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 guarantee that t
43c00 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
43c10 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
43c20 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 ction handle is
43c30 6e 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e not passed to an
43c40 79 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 y other API (by
43c50 61 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 65 any thread) afte
43c60 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 r .** sqlite3_ba
43c70 63 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 ckup_init() is c
43c80 61 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 alled and before
43c90 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
43ca0 6e 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 ng call to.** sq
43cb0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e lite3_backup_fin
43cc0 69 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 ish(). Unfortuna
43cd0 74 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 tely SQLite does
43ce0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 not currently c
43cf0 68 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 heck.** for this
43d00 2c 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 , if the applica
43d10 74 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 tion does use th
43d20 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 e destination [d
43d30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
43d40 6f 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 on].** for some
43d50 6f 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 other purpose du
43d60 72 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 ring a backup op
43d70 65 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 eration, things
43d80 6d 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a may appear to.**
43d90 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 work correctly
43da0 62 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 but in fact be s
43db0 75 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f ubtly malfunctio
43dc0 6e 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 ning. Use of th
43dd0 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e e.** destination
43de0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
43df0 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 tion while a bac
43e00 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 kup is in progre
43e10 73 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f ss might.** also
43e20 20 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 cause a mutex d
43e30 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 eadlock..**.** F
43e40 75 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 urthermore, if r
43e50 75 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 unning in [share
43e60 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 d cache mode], t
43e70 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d he application m
43e80 75 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 ust.** guarantee
43e90 20 74 68 61 74 20 74 68 65 20 73 68 61 72 65 64 that the shared
43ea0 20 63 61 63 68 65 20 75 73 65 64 20 62 79 20 74 cache used by t
43eb0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 he destination d
43ec0 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f atabase.** is no
43ed0 74 20 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 t accessed while
43ee0 20 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 the backup is r
43ef0 75 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 unning. In pract
43f00 69 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a ice this means.*
43f10 2a 20 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 * that the appli
43f20 63 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 cation must guar
43f30 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 66 antee that the f
43f40 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 ile-system file
43f50 62 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 being .** backed
43f60 20 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 up to is not ac
43f70 63 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f cessed by any co
43f80 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 nnection within
43f90 74 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 the process,.**
43fa0 6e 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 not just the spe
43fb0 63 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e cific connection
43fc0 20 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 that was passed
43fd0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b to sqlite3_back
43fe0 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a up_init()..**.**
43ff0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 The [sqlite3_ba
44000 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 ckup] object its
44010 65 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 elf is partially
44020 20 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c threadsafe. Mul
44030 74 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 tiple .** thread
44040 73 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b s may safely mak
44050 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 e multiple concu
44060 72 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 rrent calls to s
44070 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
44080 65 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 ep()..** However
44090 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 , the sqlite3_ba
440a0 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 ckup_remaining()
440b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 and sqlite3_bac
440c0 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a kup_pagecount().
440d0 2a 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 ** APIs are not
440e0 73 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e strictly speakin
440f0 67 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 g threadsafe. If
44100 20 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 they are invoke
44110 64 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 d at the.** same
44120 20 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 time as another
44130 20 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b thread is invok
44140 69 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b ing sqlite3_back
44150 75 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a up_step() it is.
44160 2a 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 ** possible that
44170 20 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 they return inv
44180 61 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a alid values..*/.
44190 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 SQLITE_API sqlit
441a0 65 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 e3_backup *sqlit
441b0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a e3_backup_init(.
441c0 20 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 sqlite3 *pDest
441d0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
441e0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 /* Dest
441f0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
44200 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e handle */. con
44210 73 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 st char *zDestNa
44220 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 me,
44230 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 /* Destinati
44240 6f 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 on database name
44250 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 */. sqlite3 *p
44260 53 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 Source,
44270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
44280 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 Source database
44290 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 handle */. cons
442a0 74 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e t char *zSourceN
442b0 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ame
442c0 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 /* Source dat
442d0 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b abase name */.);
442e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
442f0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 sqlite3_backup_s
44300 74 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b tep(sqlite3_back
44310 75 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 up *p, int nPage
44320 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e );.SQLITE_API in
44330 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 t sqlite3_backup
44340 5f 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f _finish(sqlite3_
44350 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 backup *p);.SQLI
44360 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
44370 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e e3_backup_remain
44380 69 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b ing(sqlite3_back
44390 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 up *p);.SQLITE_A
443a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
443b0 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 ackup_pagecount(
443c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
443d0 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 p);../*.** CAPI3
443e0 52 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 REF: Unlock Noti
443f0 66 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 fication.** EXPE
44400 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 RIMENTAL.**.** W
44410 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 hen running in s
44420 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 hared-cache mode
44430 2c 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 , a database ope
44440 72 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 ration may fail
44450 77 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 with.** an [SQLI
44460 54 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 TE_LOCKED] error
44470 20 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 if the required
44480 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 locks on the sh
44490 61 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a ared-cache or.**
444a0 20 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c individual tabl
444b0 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 es within the sh
444c0 61 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f ared-cache canno
444d0 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 t be obtained. S
444e0 65 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 ee.** [SQLite Sh
444f0 61 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d ared-Cache Mode]
44500 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 for a descripti
44510 6f 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 on of shared-cac
44520 68 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 he locking. .**
44530 54 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 This API may be
44540 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 used to register
44550 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 a callback that
44560 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 SQLite will inv
44570 6f 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 oke .** when the
44580 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 connection curr
44590 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 ently holding th
445a0 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 e required lock
445b0 72 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e relinquishes it.
445c0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 .** This API is
445d0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 only available i
445e0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 f the library wa
445f0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 s compiled with
44600 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 the.** [SQLITE_E
44610 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 NABLE_UNLOCK_NOT
44620 49 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 IFY] C-preproces
44630 73 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e sor symbol defin
44640 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c ed..**.** See Al
44650 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 so: [Using the S
44660 51 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 QLite Unlock Not
44670 69 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 ification Featur
44680 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 e]..**.** Shared
44690 2d 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 -cache locks are
446a0 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 released when a
446b0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
446c0 74 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a tion concludes.*
446d0 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 * its current tr
446e0 61 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 ansaction, eithe
446f0 72 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 r by committing
44700 69 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 it or rolling it
44710 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 back. .**.** Wh
44720 65 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 en a connection
44730 28 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c (known as the bl
44740 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e ocked connection
44750 29 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 ) fails to obtai
44760 6e 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 n a.** shared-ca
44770 63 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c che lock and SQL
44780 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 ITE_LOCKED is re
44790 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 turned to the ca
447a0 6c 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 ller, the.** ide
447b0 6e 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 ntity of the dat
447c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
447d0 20 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 (the blocking c
447e0 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a onnection) that.
447f0 2a 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 ** has locked th
44800 65 20 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 e required resou
44810 72 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e rce is stored in
44820 74 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 ternally. After
44830 61 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 an .** applicati
44840 6f 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 on receives an S
44850 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 QLITE_LOCKED err
44860 6f 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 or, it may call
44870 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 the.** sqlite3_u
44880 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d nlock_notify() m
44890 65 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 ethod with the b
448a0 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f locked connectio
448b0 6e 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 n handle as .**
448c0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
448d0 6e 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 nt to register f
448e0 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 or a callback th
448f0 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b at will be invok
44900 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 ed.** when the b
44910 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 locking connecti
44920 6f 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e ons current tran
44930 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c saction is concl
44940 75 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c uded. The.** cal
44950 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
44960 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 from within the
44970 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 [sqlite3_step]
44980 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 or [sqlite3_clos
44990 65 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 e].** call that
449a0 63 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c concludes the bl
449b0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
449c0 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a ns transaction..
449d0 2a 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 **.** If sqlite3
449e0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
449f0 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 is called in a
44a00 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 multi-threaded a
44a10 70 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 pplication,.** t
44a20 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 here is a chance
44a30 20 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 that the blocki
44a40 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 ng connection wi
44a50 6c 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a ll have already.
44a60 2a 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 ** concluded its
44a70 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 transaction by
44a80 74 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 the time sqlite3
44a90 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 _unlock_notify()
44aa0 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 is invoked..**
44ab0 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c If this happens,
44ac0 20 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 then the specif
44ad0 69 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 ied callback is
44ae0 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 invoked immediat
44af0 65 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 ely,.** from wit
44b00 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 hin the call to
44b10 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
44b20 6f 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 otify()..**.** I
44b30 66 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f f the blocked co
44b40 6e 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 nnection is atte
44b50 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e mpting to obtain
44b60 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e a write-lock on
44b70 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 a.** shared-cac
44b80 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f he table, and mo
44b90 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 re than one othe
44ba0 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 r connection cur
44bb0 72 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 rently holds.**
44bc0 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 a read-lock on t
44bd0 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 he same table, t
44be0 68 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 hen SQLite arbit
44bf0 72 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f rarily selects o
44c00 6e 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 ne of .** the ot
44c10 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 her connections
44c20 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 6c to use as the bl
44c30 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f ocking connectio
44c40 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d n..**.** There m
44c50 61 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e ay be at most on
44c60 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 e unlock-notify
44c70 63 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 callback registe
44c80 72 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f red by a .** blo
44c90 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e cked connection.
44ca0 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f If sqlite3_unlo
44cb0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 ck_notify() is c
44cc0 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a alled when the.*
44cd0 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 * blocked connec
44ce0 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 tion already has
44cf0 20 61 20 72 65 67 69 73 74 65 72 65 64 20 75 6e a registered un
44d00 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c lock-notify call
44d10 62 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 back,.** then th
44d20 65 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 e new callback r
44d30 65 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e eplaces the old.
44d40 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f If sqlite3_unlo
44d50 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a ck_notify() is.*
44d60 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 * called with a
44d70 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 NULL pointer as
44d80 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d its second argum
44d90 65 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 ent, then any ex
44da0 69 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b isting.** unlock
44db0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b -notify callback
44dc0 20 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 is cancelled. T
44dd0 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 he blocked conne
44de0 63 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 ctions .** unloc
44df0 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
44e00 6b 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 k may also be ca
44e10 6e 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e nceled by closin
44e20 67 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a g the blocked.**
44e30 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e connection usin
44e40 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 g [sqlite3_close
44e50 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 ()]..**.** The u
44e60 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
44e70 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 lback is not ree
44e80 6e 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 ntrant. If an ap
44e90 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 plication invoke
44ea0 73 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 s.** any sqlite3
44eb0 5f 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f _xxx API functio
44ec0 6e 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 ns from within a
44ed0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 n unlock-notify
44ee0 63 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 callback, a.** c
44ef0 72 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b rash or deadlock
44f00 20 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75 may be the resu
44f10 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 lt..**.** Unless
44f20 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 deadlock is det
44f30 65 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 ected (see below
44f40 29 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 ), sqlite3_unloc
44f50 6b 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 k_notify() alway
44f60 73 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c s.** returns SQL
44f70 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 ITE_OK..**.** <b
44f80 3e 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 >Callback Invoca
44f90 74 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e tion Details</b>
44fa0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 .**.** When an u
44fb0 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
44fc0 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 lback is registe
44fd0 72 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 red, the applica
44fe0 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 tion provides a
44ff0 0a 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a .** single void*
45000 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 pointer that is
45010 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 passed to the c
45020 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 allback when it
45030 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 is invoked..** H
45040 6f 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e owever, the sign
45050 61 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c ature of the cal
45060 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 lback function a
45070 6c 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 llows SQLite to
45080 70 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 pass.** it an ar
45090 72 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e ray of void* con
450a0 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 text pointers. T
450b0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
450c0 74 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 t passed to.** a
450d0 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 n unlock-notify
450e0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f callback is a po
450f0 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 inter to an arra
45100 79 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 y of void* point
45110 65 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ers,.** and the
45120 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 second is the nu
45130 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 mber of entries
45140 69 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a in the array..**
45150 0a 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b .** When a block
45160 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 ing connections
45170 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 transaction is c
45180 6f 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 oncluded, there
45190 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 may be.** more t
451a0 68 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 han one blocked
451b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 connection that
451c0 68 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66 has registered f
451d0 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 or an unlock-not
451e0 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e ify.** callback.
451f0 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 If two or more
45200 73 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e such blocked con
45210 6e 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 nections have sp
45220 65 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 ecified the.** s
45230 61 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e ame callback fun
45240 63 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 ction, then inst
45250 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 ead of invoking
45260 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e the callback fun
45270 63 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c ction.** multipl
45280 65 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 e times, it is i
45290 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 nvoked once with
452a0 20 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 the set of void
452b0 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 * context pointe
452c0 72 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 rs.** specified
452d0 62 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 by the blocked c
452e0 6f 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c onnections bundl
452f0 65 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f ed together into
45300 20 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 an array..** Th
45310 69 73 20 67 69 76 65 73 20 74 68 65 20 61 70 70 is gives the app
45320 6c 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f lication an oppo
45330 72 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 rtunity to prior
45340 69 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e itize any action
45350 73 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f s .** related to
45360 20 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c the set of unbl
45370 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 ocked database c
45380 6f 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a onnections..**.*
45390 2a 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 * <b>Deadlock De
453a0 74 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a tection</b>.**.*
453b0 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 * Assuming that
453c0 61 66 74 65 72 20 72 65 67 69 73 74 65 72 69 6e after registerin
453d0 67 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d g for an unlock-
453e0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 notify callback
453f0 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 a .** database w
45400 61 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c aits for the cal
45410 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 lback to be issu
45420 65 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 ed before taking
45430 20 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 any further.**
45440 61 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e action (a reason
45450 61 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 able assumption)
45460 2c 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 , then using thi
45470 73 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 s API may cause
45480 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 the.** applicati
45490 6f 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 on to deadlock.
454a0 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 For example, if
454b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 connection X is
454c0 77 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 waiting for.** c
454d0 6f 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 onnection Y's tr
454e0 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 ansaction to be
454f0 63 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 concluded, and s
45500 69 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 imilarly connect
45510 69 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 ion.** Y is wait
45520 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f ing on connectio
45530 6e 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f n X's transactio
45540 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 n, then neither
45550 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 connection.** wi
45560 6c 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 ll proceed and t
45570 68 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 he system may re
45580 6d 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 main deadlocked
45590 69 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a indefinitely..**
455a0 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 .** To avoid thi
455b0 73 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 s scenario, the
455c0 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e sqlite3_unlock_n
455d0 6f 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 otify() performs
455e0 20 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 deadlock.** det
455f0 65 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 ection. If a giv
45600 65 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 en call to sqlit
45610 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 e3_unlock_notify
45620 28 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 () would put the
45630 0a 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 .** system in a
45640 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 deadlocked state
45650 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f , then SQLITE_LO
45660 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 CKED is returned
45670 20 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 and no.** unloc
45680 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 k-notify callbac
45690 6b 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e k is registered.
456a0 20 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 The system is s
456b0 61 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 aid to be in.**
456c0 61 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 a deadlocked sta
456d0 74 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e te if connection
456e0 20 41 20 68 61 73 20 72 65 67 69 73 74 65 72 65 A has registere
456f0 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d d for an unlock-
45700 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 notify.** callba
45710 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 ck on the conclu
45720 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 sion of connecti
45730 6f 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 on B's transacti
45740 6f 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 on, and connecti
45750 6f 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 on.** B has itse
45760 6c 66 20 72 65 67 69 73 74 65 72 65 64 20 66 6f lf registered fo
45770 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 r an unlock-noti
45780 66 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e fy callback when
45790 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 connection.** A
457a0 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 's transaction i
457b0 73 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 s concluded. Ind
457c0 69 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 irect deadlock i
457d0 73 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c s also detected,
457e0 20 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 so.** the syste
457f0 6d 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 m is also consid
45800 65 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c ered to be deadl
45810 6f 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 ocked if connect
45820 69 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 ion B has.** reg
45830 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 istered for an u
45840 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
45850 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e lback on the con
45860 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 clusion of conne
45870 63 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 ction.** C's tra
45880 6e 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 nsaction, where
45890 63 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 connection C is
458a0 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 waiting on conne
458b0 63 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 ction A. Any.**
458c0 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 number of levels
458d0 20 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 of indirection
458e0 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a are allowed..**.
458f0 2a 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 ** <b>The "DROP
45900 54 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e TABLE" Exception
45910 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 </b>.**.** When
45920 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 a call to [sqlit
45930 65 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 e3_step()] retur
45940 6e 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 ns SQLITE_LOCKED
45950 2c 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a , it is almost .
45960 2a 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 ** always approp
45970 72 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 riate to call sq
45980 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
45990 69 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 ify(). There is
459a0 68 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 however,.** one
459b0 65 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 exception. When
459c0 65 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f executing a "DRO
459d0 50 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f P TABLE" or "DRO
459e0 50 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 P INDEX" stateme
459f0 6e 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 nt,.** SQLite ch
45a00 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 ecks if there ar
45a10 65 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 e any currently
45a20 65 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 executing SELECT
45a30 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 statements.** t
45a40 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 hat belong to th
45a50 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f e same connectio
45a60 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 2c n. If there are,
45a70 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 SQLITE_LOCKED i
45a80 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 s.** returned. I
45a90 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 n this case ther
45aa0 65 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e e is no "blockin
45ab0 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 g connection", s
45ac0 6f 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 o invoking.** sq
45ad0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 lite3_unlock_not
45ae0 69 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e ify() results in
45af0 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 the unlock-noti
45b00 66 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e fy callback bein
45b10 67 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d g.** invoked imm
45b20 65 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 ediately. If the
45b30 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 application the
45b40 6e 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 n re-attempts th
45b50 65 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a e "DROP TABLE".*
45b60 2a 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 * or "DROP INDEX
45b70 22 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 " query, an infi
45b80 6e 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 nite loop might
45b90 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a be the result..*
45ba0 2a 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f *.** One way aro
45bb0 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d und this problem
45bc0 20 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 is to check the
45bd0 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 extended error
45be0 63 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a code returned.**
45bf0 20 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 by an sqlite3_s
45c00 74 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 tep() call. If t
45c10 68 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 here is a blocki
45c20 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 ng connection, t
45c30 68 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e hen the.** exten
45c40 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ded error code i
45c50 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f s set to SQLITE_
45c60 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 LOCKED_SHAREDCAC
45c70 48 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 HE. Otherwise, i
45c80 6e 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c n.** the special
45c90 20 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 "DROP TABLE/IND
45ca0 45 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 EX" case, the ex
45cb0 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 tended error cod
45cc0 65 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 e is just .** SQ
45cd0 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a LITE_LOCKED..*/.
45ce0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
45cf0 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f qlite3_unlock_no
45d00 74 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 tify(. sqlite3
45d10 2a 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 *pBlocked,
45d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45d30 20 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 /* Waiting c
45d40 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 onnection */. v
45d50 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 oid (*xNotify)(v
45d60 6f 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 oid **apArg, int
45d70 20 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 nArg), /* Ca
45d80 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 llback function
45d90 74 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 to invoke */. v
45da0 6f 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 oid *pNotifyArg
45db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
45dc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
45dd0 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 gument to pass t
45de0 6f 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a o xNotify */.);.
45df0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 ../*.** CAPI3REF
45e00 3a 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 : String Compari
45e10 73 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e son.** EXPERIMEN
45e20 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 TAL.**.** The [s
45e30 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 qlite3_strnicmp(
45e40 29 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 )] API allows ap
45e50 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 plications and e
45e60 78 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 xtensions to.**
45e70 63 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 compare the cont
45e80 65 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 ents of two buff
45e90 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 ers containing U
45ea0 54 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 TF-8 strings in
45eb0 61 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 a.** case-indend
45ec0 65 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 ent fashion, usi
45ed0 6e 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 ng the same defi
45ee0 6e 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 nition of case i
45ef0 6e 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 ndependence .**
45f00 74 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 that SQLite uses
45f10 20 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e internally when
45f20 20 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 comparing ident
45f30 69 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ifiers..*/.SQLIT
45f40 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
45f50 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 3_strnicmp(const
45f60 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 char *, const c
45f70 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a har *, int);../*
45f80 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 .** Undo the hac
45f90 6b 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 k that converts
45fa0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 floating point t
45fb0 79 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 ypes to integer
45fc0 66 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e for.** builds on
45fd0 20 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 processors with
45fe0 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 out floating poi
45ff0 6e 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 nt support..*/.#
46000 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
46010 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 T_FLOATING_POINT
46020 0a 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a .# undef double.
46030 23 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 #endif..#if 0.}
46040 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 /* End of the '
46050 65 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 extern "C"' bloc
46060 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 k */.#endif.#end
46070 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if.../**********
46080 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 **** End of sqli
46090 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a te3.h **********
460a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
460b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
460c0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
460d0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
460e0 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
460f0 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 f in sqliteInt.h
46100 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
46110 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
46120 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 **** Include has
46130 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c h.h in the middl
46140 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
46150 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
46160 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
46170 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
46180 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a hash.h *********
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
461a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
461b0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 ***/./*.** 2001
461c0 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a September 22.**.
461d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
461e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
461f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
46200 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
46210 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
46220 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
46230 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
46240 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
46250 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
46260 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
46270 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
46280 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
46290 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
462a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
462b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
462c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
462d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
462e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
462f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
46330 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 his is the heade
46340 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 r file for the g
46350 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c eneric hash-tabl
46360 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a e implemenation.
46370 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 ** used in SQLit
46380 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 e..*/.#ifndef _S
46390 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 QLITE_HASH_H_.#d
463a0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 efine _SQLITE_HA
463b0 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 SH_H_../* Forwar
463c0 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f d declarations o
463d0 66 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f f structures. */
463e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
463f0 48 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 Hash Hash;.typed
46400 65 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c ef struct HashEl
46410 65 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a em HashElem;../*
46420 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 A complete hash
46430 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 table is an ins
46440 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
46450 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
46460 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 ..** The interna
46470 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ls of this struc
46480 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 ture are intende
46490 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d d to be opaque -
464a0 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 - client.** code
464b0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 should not atte
464c0 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 mpt to access or
464d0 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c modify the fiel
464e0 64 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 ds of this struc
464f0 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 ture.** directly
46500 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 . Change this s
46510 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 tructure only by
46520 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 using the routi
46530 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f nes below..** Ho
46540 77 65 76 65 72 2c 20 73 6f 6d 65 20 6f 66 20 74 wever, some of t
46550 68 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 he "procedures"
46560 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 and "functions"
46570 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e for modifying an
46580 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 d.** accessing t
46590 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 his structure ar
465a0 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c e really macros,
465b0 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 so we can't rea
465c0 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 lly make.** this
465d0 20 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 structure opaqu
465e0 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65 e..**.** All ele
465f0 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 68 61 73 ments of the has
46600 68 20 74 61 62 6c 65 20 61 72 65 20 6f 6e 20 61 h table are on a
46610 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c single doubly-l
46620 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 20 48 inked list..** H
46630 61 73 68 2e 66 69 72 73 74 20 70 6f 69 6e 74 73 ash.first points
46640 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 to the head of
46650 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a this list..**.**
46660 20 54 68 65 72 65 20 61 72 65 20 48 61 73 68 2e There are Hash.
46670 68 74 73 69 7a 65 20 62 75 63 6b 65 74 73 2e 20 htsize buckets.
46680 20 45 61 63 68 20 62 75 63 6b 65 74 20 70 6f 69 Each bucket poi
46690 6e 74 73 20 74 6f 20 61 20 73 70 6f 74 20 69 6e nts to a spot in
466a0 0a 2a 2a 20 74 68 65 20 67 6c 6f 62 61 6c 20 64 .** the global d
466b0 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 oubly-linked lis
466c0 74 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 t. The contents
466d0 20 6f 66 20 74 68 65 20 62 75 63 6b 65 74 20 61 of the bucket a
466e0 72 65 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e re the.** elemen
466f0 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 70 6c 75 t pointed to plu
46700 73 20 74 68 65 20 6e 65 78 74 20 5f 68 74 2e 63 s the next _ht.c
46710 6f 75 6e 74 2d 31 20 65 6c 65 6d 65 6e 74 73 20 ount-1 elements
46720 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a in the list..**.
46730 2a 2a 20 48 61 73 68 2e 68 74 73 69 7a 65 20 61 ** Hash.htsize a
46740 6e 64 20 48 61 73 68 2e 68 74 20 6d 61 79 20 62 nd Hash.ht may b
46750 65 20 7a 65 72 6f 2e 20 20 49 6e 20 74 68 61 74 e zero. In that
46760 20 63 61 73 65 20 6c 6f 6f 6b 75 70 20 69 73 20 case lookup is
46770 64 6f 6e 65 0a 2a 2a 20 62 79 20 61 20 6c 69 6e done.** by a lin
46780 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 74 68 ear search of th
46790 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 2e 20 20 e global list.
467a0 46 6f 72 20 73 6d 61 6c 6c 20 74 61 62 6c 65 73 For small tables
467b0 2c 20 74 68 65 20 0a 2a 2a 20 48 61 73 68 2e 68 , the .** Hash.h
467c0 74 20 74 61 62 6c 65 20 69 73 20 6e 65 76 65 72 t table is never
467d0 20 61 6c 6c 6f 63 61 74 65 64 20 62 65 63 61 75 allocated becau
467e0 73 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 se if there are
467f0 66 65 77 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 few elements.**
46800 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 69 74 in the table, it
46810 20 69 73 20 66 61 73 74 65 72 20 74 6f 20 64 6f is faster to do
46820 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68 a linear search
46830 20 74 68 61 6e 20 74 6f 20 6d 61 6e 61 67 65 0a than to manage.
46840 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c ** the hash tabl
46850 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 e..*/.struct Has
46860 68 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 h {. unsigned i
46870 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20 20 nt htsize;
46880 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63 /* Number of buc
46890 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 kets in the hash
468a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 table */. unsi
468b0 67 6e 65 64 20 69 6e 74 20 63 6f 75 6e 74 3b 20 gned int count;
468c0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
468d0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 of entries in th
468e0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 is table */. Ha
468f0 73 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20 shElem *first;
46900 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
46910 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
46920 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 the array */. s
46930 74 72 75 63 74 20 5f 68 74 20 7b 20 20 20 20 20 truct _ht {
46940 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 /* the
46950 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 hash table */.
46960 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 int count;
46970 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
46980 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
46990 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 s with this hash
469a0 20 2a 2f 0a 20 20 20 20 48 61 73 68 45 6c 65 6d */. HashElem
469b0 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 20 20 20 *chain;
469c0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
469d0 20 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 first entry wit
469e0 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 h this hash */.
469f0 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 } *ht;.};../* E
46a00 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 ach element in t
46a10 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 he hash table is
46a20 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
46a30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a the following .*
46a40 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c * structure. Al
46a50 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 l elements are s
46a60 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c tored on a singl
46a70 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 e doubly-linked
46a80 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 list..**.** Agai
46a90 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 n, this structur
46aa0 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f e is intended to
46ab0 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 be opaque, but
46ac0 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a it can't really.
46ad0 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 ** be opaque bec
46ae0 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64 20 ause it is used
46af0 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 by macros..*/.st
46b00 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 7b 0a ruct HashElem {.
46b10 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 HashElem *next
46b20 2c 20 2a 70 72 65 76 3b 20 20 20 20 20 20 20 2f , *prev; /
46b30 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69 * Next and previ
46b40 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 ous elements in
46b50 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 the table */. v
46b60 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20 oid *data;
46b70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
46b80 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 ata associated w
46b90 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 ith this element
46ba0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
46bb0 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65 79 *pKey; int nKey
46bc0 3b 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 ; /* Key associ
46bd0 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65 ated with this e
46be0 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a lement */.};../*
46bf0 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74 69 .** Access routi
46c00 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c nes. To delete,
46c10 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 insert a NULL p
46c20 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 ointer..*/.SQLIT
46c30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
46c40 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 48 qlite3HashInit(H
46c50 61 73 68 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ash*);.SQLITE_PR
46c60 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
46c70 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 te3HashInsert(Ha
46c80 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 sh*, const char
46c90 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c *pKey, int nKey,
46ca0 20 76 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 void *pData);.S
46cb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
46cc0 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 id *sqlite3HashF
46cd0 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 2a 2c ind(const Hash*,
46ce0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 const char *pKe
46cf0 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 y, int nKey);.SQ
46d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
46d10 64 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 d sqlite3HashCle
46d20 61 72 28 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a ar(Hash*);../*.*
46d30 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c 6f 6f * Macros for loo
46d40 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c ping over all el
46d50 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61 73 68 ements of a hash
46d60 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69 64 69 table. The idi
46d70 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 om is.** like th
46d80 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 is:.**.** Hash
46d90 20 68 3b 0a 2a 2a 20 20 20 48 61 73 68 45 6c 65 h;.** HashEle
46da0 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a m *p;.** ....*
46db0 2a 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 * for(p=sqlite
46dc0 48 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 HashFirst(&h); p
46dd0 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 ; p=sqliteHashNe
46de0 78 74 28 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 xt(p)){.** S
46df0 6f 6d 65 53 74 72 75 63 74 75 72 65 20 2a 70 44 omeStructure *pD
46e00 61 74 61 20 3d 20 73 71 6c 69 74 65 48 61 73 68 ata = sqliteHash
46e10 44 61 74 61 28 70 29 3b 0a 2a 2a 20 20 20 20 20 Data(p);.**
46e20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 // do something
46e30 77 69 74 68 20 70 44 61 74 61 0a 2a 2a 20 20 20 with pData.**
46e40 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c }.*/.#define sql
46e50 69 74 65 48 61 73 68 46 69 72 73 74 28 48 29 20 iteHashFirst(H)
46e60 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a 23 64 ((H)->first).#d
46e70 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 efine sqliteHash
46e80 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e Next(E) ((E)->
46e90 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 73 71 next).#define sq
46ea0 6c 69 74 65 48 61 73 68 44 61 74 61 28 45 29 20 liteHashData(E)
46eb0 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a 2f 2a ((E)->data)./*
46ec0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 #define sqliteH
46ed0 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45 ashKey(E) ((E
46ee0 29 2d 3e 70 4b 65 79 29 20 2f 2f 20 4e 4f 54 20 )->pKey) // NOT
46ef0 55 53 45 44 20 2a 2f 0a 2f 2a 20 23 64 65 66 69 USED */./* #defi
46f00 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 ne sqliteHashKey
46f10 73 69 7a 65 28 45 29 20 28 28 45 29 2d 3e 6e 4b size(E) ((E)->nK
46f20 65 79 29 20 20 2f 2f 20 4e 4f 54 20 55 53 45 44 ey) // NOT USED
46f30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 */../*.** Numbe
46f40 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 r of entries in
46f50 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f 0a a hash table.*/.
46f60 2f 2a 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 /* #define sqlit
46f70 65 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28 eHashCount(H) (
46f80 28 48 29 2d 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e (H)->count) // N
46f90 4f 54 20 55 53 45 44 20 2a 2f 0a 0a 23 65 6e 64 OT USED */..#end
46fa0 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 48 41 if /* _SQLITE_HA
46fb0 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a SH_H_ */../*****
46fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
46fd0 20 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a hash.h ********
46fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
46ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
47000 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
47010 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e ********* Contin
47020 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 uing where we le
47030 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 ft off in sqlite
47040 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
47050 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
47060 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
47070 65 20 70 61 72 73 65 2e 68 20 69 6e 20 74 68 65 e parse.h in the
47080 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 middle of sqlit
47090 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
470a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
470b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
470c0 66 69 6c 65 20 70 61 72 73 65 2e 68 20 2a 2a 2a file parse.h ***
470d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
470e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
470f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e ********/.#defin
47100 65 20 54 4b 5f 53 45 4d 49 20 20 20 20 20 20 20 e TK_SEMI
47110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47120 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 54 1.#define T
47130 4b 5f 45 58 50 4c 41 49 4e 20 20 20 20 20 20 20 K_EXPLAIN
47140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47150 20 20 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 51 2.#define TK_Q
47160 55 45 52 59 20 20 20 20 20 20 20 20 20 20 20 20 UERY
47170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
47180 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 41 4e .#define TK_PLAN
47190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471a0 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 4.#d
471b0 65 66 69 6e 65 20 54 4b 5f 42 45 47 49 4e 20 20 efine TK_BEGIN
471c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
471d0 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 5.#defi
471e0 6e 65 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f ne TK_TRANSACTIO
471f0 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
47200 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
47210 54 4b 5f 44 45 46 45 52 52 45 44 20 20 20 20 20 TK_DEFERRED
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47230 20 20 20 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 7.#define TK_
47240 49 4d 4d 45 44 49 41 54 45 20 20 20 20 20 20 20 IMMEDIATE
47250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47260 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43 8.#define TK_EXC
47270 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 20 20 LUSIVE
47280 20 20 20 20 20 20 20 20 20 20 20 20 20 39 0a 23 9.#
47290 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 define TK_COMMIT
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472b0 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 10.#def
472c0 69 6e 65 20 54 4b 5f 45 4e 44 20 20 20 20 20 20 ine TK_END
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
472e0 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 11.#define
472f0 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 20 20 20 20 TK_ROLLBACK
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47310 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 12.#define TK
47320 5f 53 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 _SAVEPOINT
47330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47340 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 13.#define TK_RE
47350 4c 45 41 53 45 20 20 20 20 20 20 20 20 20 20 20 LEASE
47360 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 0a 14.
47370 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20 #define TK_TO
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47390 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64 65 15.#de
473a0 66 69 6e 65 20 54 4b 5f 54 41 42 4c 45 20 20 20 fine TK_TABLE
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473c0 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 16.#defin
473d0 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 20 20 e TK_CREATE
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
473f0 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65 20 54 17.#define T
47400 4b 5f 49 46 20 20 20 20 20 20 20 20 20 20 20 20 K_IF
47410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47420 20 31 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 18.#define TK_N
47430 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OT
47440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39 19
47450 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53 .#define TK_EXIS
47460 54 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 TS
47470 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 23 64 20.#d
47480 65 66 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20 efine TK_TEMP
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474a0 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 66 69 21.#defi
474b0 6e 65 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20 ne TK_LP
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
474d0 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20 22.#define
474e0 54 4b 5f 52 50 20 20 20 20 20 20 20 20 20 20 20 TK_RP
474f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47500 20 20 32 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 23.#define TK_
47510 41 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AS
47520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
47530 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4.#define TK_COM
47540 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 MA
47550 20 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 25.#
47560 64 65 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 define TK_ID
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47580 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 26.#def
47590 69 6e 65 20 54 4b 5f 49 4e 44 45 58 45 44 20 20 ine TK_INDEXED
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475b0 20 20 20 20 20 20 32 37 0a 23 64 65 66 69 6e 65 27.#define
475c0 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20 20 TK_ABORT
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
475e0 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 28.#define TK
475f0 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 _ACTION
47600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47610 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 46 29.#define TK_AF
47620 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 TER
47630 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 30.
47640 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 41 4c 59 #define TK_ANALY
47650 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ZE
47660 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65 31.#de
47670 66 69 6e 65 20 54 4b 5f 41 53 43 20 20 20 20 20 fine TK_ASC
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47690 20 20 20 20 20 20 20 33 32 0a 23 64 65 66 69 6e 32.#defin
476a0 65 20 54 4b 5f 41 54 54 41 43 48 20 20 20 20 20 e TK_ATTACH
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476c0 20 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 54 33.#define T
476d0 4b 5f 42 45 46 4f 52 45 20 20 20 20 20 20 20 20 K_BEFORE
476e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
476f0 20 33 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 34.#define TK_B
47700 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 Y
47710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 35 35
47720 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41 53 43 .#define TK_CASC
47730 41 44 45 20 20 20 20 20 20 20 20 20 20 20 20 20 ADE
47740 20 20 20 20 20 20 20 20 20 20 20 33 36 0a 23 64 36.#d
47750 65 66 69 6e 65 20 54 4b 5f 43 41 53 54 20 20 20 efine TK_CAST
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47770 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 37.#defi
47780 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 ne TK_COLUMNKW
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477a0 20 20 20 20 20 33 38 0a 23 64 65 66 69 6e 65 20 38.#define
477b0 54 4b 5f 43 4f 4e 46 4c 49 43 54 20 20 20 20 20 TK_CONFLICT
477c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
477d0 20 20 33 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 39.#define TK_
477e0 44 41 54 41 42 41 53 45 20 20 20 20 20 20 20 20 DATABASE
477f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
47800 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 53 0.#define TK_DES
47810 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 C
47820 20 20 20 20 20 20 20 20 20 20 20 20 34 31 0a 23 41.#
47830 64 65 66 69 6e 65 20 54 4b 5f 44 45 54 41 43 48 define TK_DETACH
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47850 20 20 20 20 20 20 20 20 20 34 32 0a 23 64 65 66 42.#def
47860 69 6e 65 20 54 4b 5f 45 41 43 48 20 20 20 20 20 ine TK_EACH
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47880 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 43.#define
47890 20 54 4b 5f 46 41 49 4c 20 20 20 20 20 20 20 20 TK_FAIL
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478b0 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 44.#define TK
478c0 5f 46 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 _FOR
478d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
478e0 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 47 45.#define TK_IG
478f0 4e 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 NORE
47900 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 46.
47910 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 49 54 49 #define TK_INITI
47920 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20 20 ALLY
47930 20 20 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 47.#de
47940 66 69 6e 65 20 54 4b 5f 49 4e 53 54 45 41 44 20 fine TK_INSTEAD
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47960 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69 6e 48.#defin
47970 65 20 54 4b 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 e TK_LIKE_KW
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47990 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65 20 54 49.#define T
479a0 4b 5f 4d 41 54 43 48 20 20 20 20 20 20 20 20 20 K_MATCH
479b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
479c0 20 35 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 50.#define TK_N
479d0 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 O
479e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31 51
479f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20 .#define TK_KEY
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a10 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 52.#d
47a20 65 66 69 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20 efine TK_OF
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a40 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 66 69 53.#defi
47a50 6e 65 20 54 4b 5f 4f 46 46 53 45 54 20 20 20 20 ne TK_OFFSET
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47a70 20 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 54.#define
47a80 54 4b 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20 TK_PRAGMA
47a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47aa0 20 20 35 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55.#define TK_
47ab0 52 41 49 53 45 20 20 20 20 20 20 20 20 20 20 20 RAISE
47ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
47ad0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 6.#define TK_REP
47ae0 4c 41 43 45 20 20 20 20 20 20 20 20 20 20 20 20 LACE
47af0 20 20 20 20 20 20 20 20 20 20 20 20 35 37 0a 23 57.#
47b00 64 65 66 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 define TK_RESTRI
47b10 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 CT
47b20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65 66 58.#def
47b30 69 6e 65 20 54 4b 5f 52 4f 57 20 20 20 20 20 20 ine TK_ROW
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b50 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 59.#define
47b60 20 54 4b 5f 54 52 49 47 47 45 52 20 20 20 20 20 TK_TRIGGER
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47b80 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 54 4b 60.#define TK
47b90 5f 56 41 43 55 55 4d 20 20 20 20 20 20 20 20 20 _VACUUM
47ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47bb0 36 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 61.#define TK_VI
47bc0 45 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 EW
47bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 62.
47be0 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 52 54 55 #define TK_VIRTU
47bf0 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AL
47c00 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 63.#de
47c10 66 69 6e 65 20 54 4b 5f 52 45 49 4e 44 45 58 20 fine TK_REINDEX
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c30 20 20 20 20 20 20 20 36 34 0a 23 64 65 66 69 6e 64.#defin
47c40 65 20 54 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20 e TK_RENAME
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c60 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65 20 54 65.#define T
47c70 4b 5f 43 54 49 4d 45 5f 4b 57 20 20 20 20 20 20 K_CTIME_KW
47c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47c90 20 36 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 66.#define TK_A
47ca0 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NY
47cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 37 67
47cc0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20 .#define TK_OR
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ce0 20 20 20 20 20 20 20 20 20 20 20 36 38 0a 23 64 68.#d
47cf0 65 66 69 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20 efine TK_AND
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d10 20 20 20 20 20 20 20 20 36 39 0a 23 64 65 66 69 69.#defi
47d20 6e 65 20 54 4b 5f 49 53 20 20 20 20 20 20 20 20 ne TK_IS
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d40 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 70.#define
47d50 54 4b 5f 42 45 54 57 45 45 4e 20 20 20 20 20 20 TK_BETWEEN
47d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47d70 20 20 37 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 71.#define TK_
47d80 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 IN
47d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
47da0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 2.#define TK_ISN
47db0 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 ULL
47dc0 20 20 20 20 20 20 20 20 20 20 20 20 37 33 0a 23 73.#
47dd0 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c define TK_NOTNUL
47de0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 L
47df0 20 20 20 20 20 20 20 20 20 37 34 0a 23 64 65 66 74.#def
47e00 69 6e 65 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 ine TK_NE
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e20 20 20 20 20 20 20 37 35 0a 23 64 65 66 69 6e 65 75.#define
47e30 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20 20 20 TK_EQ
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e50 20 20 20 37 36 0a 23 64 65 66 69 6e 65 20 54 4b 76.#define TK
47e60 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20 20 _GT
47e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47e80 37 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45 77.#define TK_LE
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 20 20 20 20 20 20 37 38 0a 78.
47eb0 23 64 65 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20 #define TK_LT
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47ed0 20 20 20 20 20 20 20 20 20 20 37 39 0a 23 64 65 79.#de
47ee0 66 69 6e 65 20 54 4b 5f 47 45 20 20 20 20 20 20 fine TK_GE
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f00 20 20 20 20 20 20 20 38 30 0a 23 64 65 66 69 6e 80.#defin
47f10 65 20 54 4b 5f 45 53 43 41 50 45 20 20 20 20 20 e TK_ESCAPE
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f30 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 54 81.#define T
47f40 4b 5f 42 49 54 41 4e 44 20 20 20 20 20 20 20 20 K_BITAND
47f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47f60 20 38 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 82.#define TK_B
47f70 49 54 4f 52 20 20 20 20 20 20 20 20 20 20 20 20 ITOR
47f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33 83
47f90 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49 .#define TK_LSHI
47fa0 46 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 FT
47fb0 20 20 20 20 20 20 20 20 20 20 20 38 34 0a 23 64 84.#d
47fc0 65 66 69 6e 65 20 54 4b 5f 52 53 48 49 46 54 20 efine TK_RSHIFT
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
47fe0 20 20 20 20 20 20 20 20 38 35 0a 23 64 65 66 69 85.#defi
47ff0 6e 65 20 54 4b 5f 50 4c 55 53 20 20 20 20 20 20 ne TK_PLUS
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48010 20 20 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 86.#define
48020 54 4b 5f 4d 49 4e 55 53 20 20 20 20 20 20 20 20 TK_MINUS
48030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48040 20 20 38 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 87.#define TK_
48050 53 54 41 52 20 20 20 20 20 20 20 20 20 20 20 20 STAR
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 8
48070 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 8.#define TK_SLA
48080 53 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SH
48090 20 20 20 20 20 20 20 20 20 20 20 20 38 39 0a 23 89.#
480a0 64 65 66 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 define TK_REM
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480c0 20 20 20 20 20 20 20 20 20 39 30 0a 23 64 65 66 90.#def
480d0 69 6e 65 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 ine TK_CONCAT
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
480f0 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 6e 65 91.#define
48100 20 54 4b 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20 TK_COLLATE
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48120 20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 54 4b 92.#define TK
48130 5f 42 49 54 4e 4f 54 20 20 20 20 20 20 20 20 20 _BITNOT
48140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48150 39 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 93.#define TK_ST
48160 52 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 RING
48170 20 20 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 94.
48180 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f #define TK_JOIN_
48190 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 KW
481a0 20 20 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 95.#de
481b0 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 52 41 49 fine TK_CONSTRAI
481c0 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 NT
481d0 20 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 96.#defin
481e0 65 20 54 4b 5f 44 45 46 41 55 4c 54 20 20 20 20 e TK_DEFAULT
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48200 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20 54 97.#define T
48210 4b 5f 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 K_NULL
48220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48230 20 39 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 98.#define TK_P
48240 52 49 4d 41 52 59 20 20 20 20 20 20 20 20 20 20 RIMARY
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39 99
48260 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 51 .#define TK_UNIQ
48270 55 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 UE
48280 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 100.#
48290 64 65 66 69 6e 65 20 54 4b 5f 43 48 45 43 4b 20 define TK_CHECK
482a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
482b0 20 20 20 20 20 20 20 20 20 31 30 31 0a 23 64 65 101.#de
482c0 66 69 6e 65 20 54 4b 5f 52 45 46 45 52 45 4e 43 fine TK_REFERENC
482d0 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ES
482e0 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69 102.#defi
482f0 6e 65 20 54 4b 5f 41 55 54 4f 49 4e 43 52 20 20 ne TK_AUTOINCR
48300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48310 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 103.#define
48320 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20 20 20 20 TK_ON
48330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48340 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 54 104.#define T
48350 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 K_DELETE
48360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48370 20 31 30 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 105.#define TK_
48380 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 UPDATE
48390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
483a0 30 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 06.#define TK_SE
483b0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37 107
483d0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 .#define TK_DEFE
483e0 52 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 RRABLE
483f0 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 108.#
48400 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 define TK_FOREIG
48410 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 N
48420 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 64 65 109.#de
48430 66 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 fine TK_DROP
48440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48450 20 20 20 20 20 20 20 31 31 30 0a 23 64 65 66 69 110.#defi
48460 6e 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 ne TK_UNION
48470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48480 20 20 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 111.#define
48490 20 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 TK_ALL
484a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484b0 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 54 112.#define T
484c0 4b 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20 K_EXCEPT
484d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
484e0 20 31 31 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 113.#define TK_
484f0 49 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20 INTERSECT
48500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48510 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 14.#define TK_SE
48520 4c 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20 LECT
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 35 115
48540 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 .#define TK_DIST
48550 49 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20 INCT
48560 20 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 116.#
48570 64 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 define TK_DOT
48580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48590 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 64 65 117.#de
485a0 66 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 fine TK_FROM
485b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485c0 20 20 20 20 20 20 20 31 31 38 0a 23 64 65 66 69 118.#defi
485d0 6e 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 ne TK_JOIN
485e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
485f0 20 20 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 119.#define
48600 20 54 4b 5f 55 53 49 4e 47 20 20 20 20 20 20 20 TK_USING
48610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48620 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 54 120.#define T
48630 4b 5f 4f 52 44 45 52 20 20 20 20 20 20 20 20 20 K_ORDER
48640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48650 20 31 32 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 121.#define TK_
48660 47 52 4f 55 50 20 20 20 20 20 20 20 20 20 20 20 GROUP
48670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48680 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 48 41 22.#define TK_HA
48690 56 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 VING
486a0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33 123
486b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 .#define TK_LIMI
486c0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
486d0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 124.#
486e0 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 52 45 20 define TK_WHERE
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48700 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65 125.#de
48710 66 69 6e 65 20 54 4b 5f 49 4e 54 4f 20 20 20 20 fine TK_INTO
48720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48730 20 20 20 20 20 20 20 31 32 36 0a 23 64 65 66 69 126.#defi
48740 6e 65 20 54 4b 5f 56 41 4c 55 45 53 20 20 20 20 ne TK_VALUES
48750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48760 20 20 20 20 20 31 32 37 0a 23 64 65 66 69 6e 65 127.#define
48770 20 54 4b 5f 49 4e 53 45 52 54 20 20 20 20 20 20 TK_INSERT
48780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48790 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 54 128.#define T
487a0 4b 5f 49 4e 54 45 47 45 52 20 20 20 20 20 20 20 K_INTEGER
487b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
487c0 20 31 32 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 129.#define TK_
487d0 46 4c 4f 41 54 20 20 20 20 20 20 20 20 20 20 20 FLOAT
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
487f0 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c 30.#define TK_BL
48800 4f 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 OB
48810 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 31 131
48820 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 47 49 .#define TK_REGI
48830 53 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 STER
48840 20 20 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 132.#
48850 64 65 66 69 6e 65 20 54 4b 5f 56 41 52 49 41 42 define TK_VARIAB
48860 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 LE
48870 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64 65 133.#de
48880 66 69 6e 65 20 54 4b 5f 43 41 53 45 20 20 20 20 fine TK_CASE
48890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488a0 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66 69 134.#defi
488b0 6e 65 20 54 4b 5f 57 48 45 4e 20 20 20 20 20 20 ne TK_WHEN
488c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
488d0 20 20 20 20 20 31 33 35 0a 23 64 65 66 69 6e 65 135.#define
488e0 20 54 4b 5f 54 48 45 4e 20 20 20 20 20 20 20 20 TK_THEN
488f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48900 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 54 136.#define T
48910 4b 5f 45 4c 53 45 20 20 20 20 20 20 20 20 20 20 K_ELSE
48920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48930 20 31 33 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 137.#define TK_
48940 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20 INDEX
48950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48960 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 38.#define TK_AL
48970 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 TER
48980 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 39 139
48990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 44 44 20 .#define TK_ADD
489a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
489b0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 23 140.#
489c0 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 define TK_TO_TEX
489d0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 T
489e0 20 20 20 20 20 20 20 20 20 31 34 31 0a 23 64 65 141.#de
489f0 66 69 6e 65 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 fine TK_TO_BLOB
48a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a10 20 20 20 20 20 20 20 31 34 32 0a 23 64 65 66 69 142.#defi
48a20 6e 65 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 ne TK_TO_NUMERIC
48a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a40 20 20 20 20 20 31 34 33 0a 23 64 65 66 69 6e 65 143.#define
48a50 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 20 20 20 TK_TO_INT
48a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48a70 20 20 20 31 34 34 0a 23 64 65 66 69 6e 65 20 54 144.#define T
48a80 4b 5f 54 4f 5f 52 45 41 4c 20 20 20 20 20 20 20 K_TO_REAL
48a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48aa0 20 31 34 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 145.#define TK_
48ab0 49 53 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 ISNOT
48ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48ad0 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 46.#define TK_EN
48ae0 44 5f 4f 46 5f 46 49 4c 45 20 20 20 20 20 20 20 D_OF_FILE
48af0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 37 147
48b00 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 .#define TK_ILLE
48b10 47 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 GAL
48b20 20 20 20 20 20 20 20 20 20 20 20 31 34 38 0a 23 148.#
48b30 64 65 66 69 6e 65 20 54 4b 5f 53 50 41 43 45 20 define TK_SPACE
48b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48b50 20 20 20 20 20 20 20 20 20 31 34 39 0a 23 64 65 149.#de
48b60 66 69 6e 65 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 fine TK_UNCLOSED
48b70 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 20 _STRING
48b80 20 20 20 20 20 20 20 31 35 30 0a 23 64 65 66 69 150.#defi
48b90 6e 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 ne TK_FUNCTION
48ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48bb0 20 20 20 20 20 31 35 31 0a 23 64 65 66 69 6e 65 151.#define
48bc0 20 54 4b 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 TK_COLUMN
48bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48be0 20 20 20 31 35 32 0a 23 64 65 66 69 6e 65 20 54 152.#define T
48bf0 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 20 K_AGG_FUNCTION
48c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48c10 20 31 35 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 153.#define TK_
48c20 41 47 47 5f 43 4f 4c 55 4d 4e 20 20 20 20 20 20 AGG_COLUMN
48c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 1
48c40 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 54.#define TK_CO
48c50 4e 53 54 5f 46 55 4e 43 20 20 20 20 20 20 20 20 NST_FUNC
48c60 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 35 155
48c70 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d 49 4e .#define TK_UMIN
48c80 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 US
48c90 20 20 20 20 20 20 20 20 20 20 20 31 35 36 0a 23 156.#
48ca0 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53 20 define TK_UPLUS
48cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
48cc0 20 20 20 20 20 20 20 20 20 31 35 37 0a 0a 2f 2a 157../*
48cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
48ce0 64 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a d of parse.h ***
48cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
48d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
48d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f ************* Co
48d30 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 ntinuing where w
48d40 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 e left off in sq
48d50 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a liteInt.h ******
48d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 ************/.#i
48d70 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e nclude <stdio.h>
48d80 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 .#include <stdli
48d90 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 b.h>.#include <s
48da0 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 tring.h>.#includ
48db0 65 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e e <assert.h>.#in
48dc0 63 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e clude <stddef.h>
48dd0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 ../*.** If compi
48de0 6c 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 ling for a proce
48df0 73 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 ssor that lacks
48e00 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 floating point s
48e10 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 upport,.** subst
48e20 69 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f itute integer fo
48e30 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 r floating-point
48e40 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
48e50 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
48e60 50 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 POINT.# define d
48e70 6f 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 ouble sqlite_int
48e80 36 34 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 64.# define LONG
48e90 44 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 DOUBLE_TYPE sqli
48ea0 74 65 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65 te_int64.# ifnde
48eb0 66 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c f SQLITE_BIG_DBL
48ec0 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 .# define SQLI
48ed0 54 45 5f 42 49 47 5f 44 42 4c 20 28 28 28 73 71 TE_BIG_DBL (((sq
48ee0 6c 69 74 65 33 5f 69 6e 74 36 34 29 31 29 3c 3c lite3_int64)1)<<
48ef0 35 30 29 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 50).# endif.# de
48f00 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fine SQLITE_OMIT
48f10 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 20 _DATETIME_FUNCS
48f20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 1.# define SQLIT
48f30 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 31 0a 23 E_OMIT_TRACE 1.#
48f40 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 undef SQLITE_MI
48f50 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 XED_ENDIAN_64BIT
48f60 5f 46 4c 4f 41 54 0a 23 20 75 6e 64 65 66 20 53 _FLOAT.# undef S
48f70 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e QLITE_HAVE_ISNAN
48f80 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
48f90 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 SQLITE_BIG_DBL.#
48fa0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 define SQLITE_B
48fb0 49 47 5f 44 42 4c 20 28 31 65 39 39 29 0a 23 65 IG_DBL (1e99).#e
48fc0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 ndif../*.** OMIT
48fd0 5f 54 45 4d 50 44 42 20 69 73 20 73 65 74 20 74 _TEMPDB is set t
48fe0 6f 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d o 1 if SQLITE_OM
48ff0 49 54 5f 54 45 4d 50 44 42 20 69 73 20 64 65 66 IT_TEMPDB is def
49000 69 6e 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 ined, or 0.** af
49010 74 65 72 77 61 72 64 2e 20 48 61 76 69 6e 67 20 terward. Having
49020 74 68 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 this macro allow
49030 73 20 75 73 20 74 6f 20 63 61 75 73 65 20 74 68 s us to cause th
49040 65 20 43 20 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a e C compiler .**
49050 20 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75 73 to omit code us
49060 65 64 20 62 79 20 54 45 4d 50 20 74 61 62 6c 65 ed by TEMP table
49070 73 20 77 69 74 68 6f 75 74 20 6d 65 73 73 79 20 s without messy
49080 23 69 66 6e 64 65 66 20 73 74 61 74 65 6d 65 6e #ifndef statemen
49090 74 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ts..*/.#ifdef SQ
490a0 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 LITE_OMIT_TEMPDB
490b0 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 .#define OMIT_TE
490c0 4d 50 44 42 20 31 0a 23 65 6c 73 65 0a 23 64 65 MPDB 1.#else.#de
490d0 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 fine OMIT_TEMPDB
490e0 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
490f0 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e If the followin
49100 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20 74 g macro is set t
49110 6f 20 31 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 76 o 1, then NULL v
49120 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 alues are consid
49130 65 72 65 64 0a 2a 2a 20 64 69 73 74 69 6e 63 74 ered.** distinct
49140 20 77 68 65 6e 20 64 65 74 65 72 6d 69 6e 69 6e when determinin
49150 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 g whether or not
49160 20 74 77 6f 20 65 6e 74 72 69 65 73 20 61 72 65 two entries are
49170 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 the same.** in
49180 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 a UNIQUE index.
49190 20 54 68 69 73 20 69 73 20 74 68 65 20 77 61 79 This is the way
491a0 20 50 6f 73 74 67 72 65 53 51 4c 2c 20 4f 72 61 PostgreSQL, Ora
491b0 63 6c 65 2c 20 44 42 32 2c 20 4d 79 53 51 4c 2c cle, DB2, MySQL,
491c0 0a 2a 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 .** OCELOT, and
491d0 46 69 72 65 62 69 72 64 20 61 6c 6c 20 77 6f 72 Firebird all wor
491e0 6b 2e 20 20 54 68 65 20 53 51 4c 39 32 20 73 70 k. The SQL92 sp
491f0 65 63 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 ec explicitly sa
49200 79 73 20 74 68 69 73 0a 2a 2a 20 69 73 20 74 68 ys this.** is th
49210 65 20 77 61 79 20 74 68 69 6e 67 73 20 61 72 65 e way things are
49220 20 73 75 70 70 6f 73 65 20 74 6f 20 77 6f 72 6b suppose to work
49230 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 ..**.** If the f
49240 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 ollowing macro i
49250 73 20 73 65 74 20 74 6f 20 30 2c 20 74 68 65 20 s set to 0, the
49260 4e 55 4c 4c 73 20 61 72 65 20 69 6e 64 69 73 74 NULLs are indist
49270 69 6e 63 74 20 66 6f 72 0a 2a 2a 20 61 20 55 4e inct for.** a UN
49280 49 51 55 45 20 69 6e 64 65 78 2e 20 20 49 6e 20 IQUE index. In
49290 74 68 69 73 20 6d 6f 64 65 2c 20 79 6f 75 20 63 this mode, you c
492a0 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 61 20 73 an only have a s
492b0 69 6e 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 72 79 ingle NULL entry
492c0 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6c 75 6d 6e .** for a column
492d0 20 64 65 63 6c 61 72 65 64 20 55 4e 49 51 55 45 declared UNIQUE
492e0 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 77 . This is the w
492f0 61 79 20 49 6e 66 6f 72 6d 69 78 20 61 6e 64 20 ay Informix and
49300 53 51 4c 20 53 65 72 76 65 72 0a 2a 2a 20 77 6f SQL Server.** wo
49310 72 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e rk..*/.#define N
49320 55 4c 4c 5f 44 49 53 54 49 4e 43 54 5f 46 4f 52 ULL_DISTINCT_FOR
49330 5f 55 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a _UNIQUE 1../*.**
49340 20 54 68 65 20 22 66 69 6c 65 20 66 6f 72 6d 61 The "file forma
49350 74 22 20 6e 75 6d 62 65 72 20 69 73 20 61 6e 20 t" number is an
49360 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 integer that is
49370 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e incremented when
49380 65 76 65 72 0a 2a 2a 20 74 68 65 20 56 44 42 45 ever.** the VDBE
49390 2d 6c 65 76 65 6c 20 66 69 6c 65 20 66 6f 72 6d -level file form
493a0 61 74 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65 at changes. The
493b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f following macro
493c0 73 20 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a 20 s define the.**
493d0 74 68 65 20 64 65 66 61 75 6c 74 20 66 69 6c 65 the default file
493e0 20 66 6f 72 6d 61 74 20 66 6f 72 20 6e 65 77 20 format for new
493f0 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 74 68 databases and th
49400 65 20 6d 61 78 69 6d 75 6d 20 66 69 6c 65 20 66 e maximum file f
49410 6f 72 6d 61 74 0a 2a 2a 20 74 68 61 74 20 74 68 ormat.** that th
49420 65 20 6c 69 62 72 61 72 79 20 63 61 6e 20 72 65 e library can re
49430 61 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ad..*/.#define S
49440 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 QLITE_MAX_FILE_F
49450 4f 52 4d 41 54 20 34 0a 23 69 66 6e 64 65 66 20 ORMAT 4.#ifndef
49460 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
49470 49 4c 45 5f 46 4f 52 4d 41 54 0a 23 20 64 65 66 ILE_FORMAT.# def
49480 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 ine SQLITE_DEFAU
49490 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 31 LT_FILE_FORMAT 1
494a0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 .#endif..#ifndef
494b0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
494c0 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 45 RECURSIVE_TRIGGE
494d0 52 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 RS.# define SQLI
494e0 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52 TE_DEFAULT_RECUR
494f0 53 49 56 45 5f 54 52 49 47 47 45 52 53 20 30 0a SIVE_TRIGGERS 0.
49500 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 #endif../*.** Pr
49510 6f 76 69 64 65 20 61 20 64 65 66 61 75 6c 74 20 ovide a default
49520 76 61 6c 75 65 20 66 6f 72 20 53 51 4c 49 54 45 value for SQLITE
49530 5f 54 45 4d 50 5f 53 54 4f 52 45 20 69 6e 20 63 _TEMP_STORE in c
49540 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20 73 70 ase it is not sp
49550 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 ecified.** on th
49560 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a e command-line.*
49570 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 /.#ifndef SQLITE
49580 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 23 20 64 65 _TEMP_STORE.# de
49590 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 fine SQLITE_TEMP
495a0 5f 53 54 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a _STORE 1.#endif.
495b0 0a 2f 2a 0a 2a 2a 20 47 43 43 20 64 6f 65 73 20 ./*.** GCC does
495c0 6e 6f 74 20 64 65 66 69 6e 65 20 74 68 65 20 6f not define the o
495d0 66 66 73 65 74 6f 66 28 29 20 6d 61 63 72 6f 20 ffsetof() macro
495e0 73 6f 20 77 65 27 6c 6c 20 68 61 76 65 20 74 6f so we'll have to
495f0 20 64 6f 20 69 74 0a 2a 2a 20 6f 75 72 73 65 6c do it.** oursel
49600 76 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ves..*/.#ifndef
49610 6f 66 66 73 65 74 6f 66 0a 23 64 65 66 69 6e 65 offsetof.#define
49620 20 6f 66 66 73 65 74 6f 66 28 53 54 52 55 43 54 offsetof(STRUCT
49630 55 52 45 2c 46 49 45 4c 44 29 20 28 28 69 6e 74 URE,FIELD) ((int
49640 29 28 28 63 68 61 72 2a 29 26 28 28 53 54 52 55 )((char*)&((STRU
49650 43 54 55 52 45 2a 29 30 29 2d 3e 46 49 45 4c 44 CTURE*)0)->FIELD
49660 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a )).#endif../*.**
49670 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
49680 20 74 68 69 73 20 6d 61 63 68 69 6e 65 20 75 73 this machine us
49690 65 73 20 45 42 43 44 49 43 2e 20 20 28 59 65 73 es EBCDIC. (Yes
496a0 2c 20 62 65 6c 69 65 76 65 20 69 74 20 6f 72 0a , believe it or.
496b0 2a 2a 20 6e 6f 74 2c 20 74 68 65 72 65 20 61 72 ** not, there ar
496c0 65 20 73 74 69 6c 6c 20 6d 61 63 68 69 6e 65 73 e still machines
496d0 20 6f 75 74 20 74 68 65 72 65 20 74 68 61 74 20 out there that
496e0 75 73 65 20 45 42 43 44 49 43 2e 29 0a 2a 2f 0a use EBCDIC.).*/.
496f0 23 69 66 20 27 41 27 20 3d 3d 20 27 5c 33 30 31 #if 'A' == '\301
49700 27 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 '.# define SQLIT
49710 45 5f 45 42 43 44 49 43 20 31 0a 23 65 6c 73 65 E_EBCDIC 1.#else
49720 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
49730 5f 41 53 43 49 49 20 31 0a 23 65 6e 64 69 66 0a _ASCII 1.#endif.
49740 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 67 65 72 73 20 ./*.** Integers
49750 6f 66 20 6b 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 of known sizes.
49760 20 54 68 65 73 65 20 74 79 70 65 64 65 66 73 20 These typedefs
49770 6d 69 67 68 74 20 63 68 61 6e 67 65 20 66 6f 72 might change for
49780 20 61 72 63 68 69 74 65 63 74 75 72 65 73 0a 2a architectures.*
49790 2a 20 77 68 65 72 65 20 74 68 65 20 73 69 7a 65 * where the size
497a0 73 20 76 65 72 79 2e 20 20 50 72 65 70 72 6f 63 s very. Preproc
497b0 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 61 72 65 essor macros are
497c0 20 61 76 61 69 6c 61 62 6c 65 20 73 6f 20 74 68 available so th
497d0 61 74 20 74 68 65 0a 2a 2a 20 74 79 70 65 73 20 at the.** types
497e0 63 61 6e 20 62 65 20 63 6f 6e 76 65 6e 69 65 6e can be convenien
497f0 74 6c 79 20 72 65 64 65 66 69 6e 65 64 20 61 74 tly redefined at
49800 20 63 6f 6d 70 69 6c 65 2d 74 79 70 65 2e 20 20 compile-type.
49810 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a Like this:.**.**
49820 20 20 20 20 20 20 20 20 20 63 63 20 27 2d 44 55 cc '-DU
49830 49 4e 54 50 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 INTPTR_TYPE=long
49840 20 6c 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a long int' ....*
49850 2f 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 33 32 /.#ifndef UINT32
49860 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 _TYPE.# ifdef HA
49870 56 45 5f 55 49 4e 54 33 32 5f 54 0a 23 20 20 64 VE_UINT32_T.# d
49880 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59 50 efine UINT32_TYP
49890 45 20 75 69 6e 74 33 32 5f 74 0a 23 20 65 6c 73 E uint32_t.# els
498a0 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 e.# define UINT
498b0 33 32 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 32_TYPE unsigned
498c0 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e int.# endif.#en
498d0 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 dif.#ifndef UINT
498e0 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 16_TYPE.# ifdef
498f0 48 41 56 45 5f 55 49 4e 54 31 36 5f 54 0a 23 20 HAVE_UINT16_T.#
49900 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f 54 define UINT16_T
49910 59 50 45 20 75 69 6e 74 31 36 5f 74 0a 23 20 65 YPE uint16_t.# e
49920 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 lse.# define UI
49930 4e 54 31 36 5f 54 59 50 45 20 75 6e 73 69 67 6e NT16_TYPE unsign
49940 65 64 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65 ed short int.# e
49950 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e ndif.#endif.#ifn
49960 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 0a 23 def INT16_TYPE.#
49970 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e 54 31 ifdef HAVE_INT1
49980 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 6_T.# define IN
49990 54 31 36 5f 54 59 50 45 20 69 6e 74 31 36 5f 74 T16_TYPE int16_t
499a0 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e .# else.# defin
499b0 65 20 49 4e 54 31 36 5f 54 59 50 45 20 73 68 6f e INT16_TYPE sho
499c0 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23 rt int.# endif.#
499d0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49 endif.#ifndef UI
499e0 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 NT8_TYPE.# ifdef
499f0 20 48 41 56 45 5f 55 49 4e 54 38 5f 54 0a 23 20 HAVE_UINT8_T.#
49a00 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 define UINT8_TY
49a10 50 45 20 75 69 6e 74 38 5f 74 0a 23 20 65 6c 73 PE uint8_t.# els
49a20 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 e.# define UINT
49a30 38 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 8_TYPE unsigned
49a40 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e char.# endif.#en
49a50 64 69 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 38 dif.#ifndef INT8
49a60 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 _TYPE.# ifdef HA
49a70 56 45 5f 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 VE_INT8_T.# def
49a80 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 69 6e ine INT8_TYPE in
49a90 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 t8_t.# else.# d
49aa0 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20 efine INT8_TYPE
49ab0 73 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e signed char.# en
49ac0 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 dif.#endif.#ifnd
49ad0 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 ef LONGDOUBLE_TY
49ae0 50 45 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 PE.# define LONG
49af0 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 DOUBLE_TYPE long
49b00 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a 74 double.#endif.t
49b10 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e ypedef sqlite_in
49b20 74 36 34 20 69 36 34 3b 20 20 20 20 20 20 20 20 t64 i64;
49b30 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67 6e /* 8-byte sign
49b40 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 ed integer */.ty
49b50 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e pedef sqlite_uin
49b60 74 36 34 20 75 36 34 3b 20 20 20 20 20 20 20 20 t64 u64;
49b70 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e 73 69 67 /* 8-byte unsig
49b80 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 ned integer */.t
49b90 79 70 65 64 65 66 20 55 49 4e 54 33 32 5f 54 59 ypedef UINT32_TY
49ba0 50 45 20 75 33 32 3b 20 20 20 20 20 20 20 20 20 PE u32;
49bb0 20 20 2f 2a 20 34 2d 62 79 74 65 20 75 6e 73 69 /* 4-byte unsi
49bc0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
49bd0 74 79 70 65 64 65 66 20 55 49 4e 54 31 36 5f 54 typedef UINT16_T
49be0 59 50 45 20 75 31 36 3b 20 20 20 20 20 20 20 20 YPE u16;
49bf0 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 75 6e 73 /* 2-byte uns
49c00 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
49c10 0a 74 79 70 65 64 65 66 20 49 4e 54 31 36 5f 54 .typedef INT16_T
49c20 59 50 45 20 69 31 36 3b 20 20 20 20 20 20 20 20 YPE i16;
49c30 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 /* 2-byte si
49c40 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
49c50 74 79 70 65 64 65 66 20 55 49 4e 54 38 5f 54 59 typedef UINT8_TY
49c60 50 45 20 75 38 3b 20 20 20 20 20 20 20 20 20 20 PE u8;
49c70 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 75 6e 73 /* 1-byte uns
49c80 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f igned integer */
49c90 0a 74 79 70 65 64 65 66 20 49 4e 54 38 5f 54 59 .typedef INT8_TY
49ca0 50 45 20 69 38 3b 20 20 20 20 20 20 20 20 20 20 PE i8;
49cb0 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 /* 1-byte si
49cc0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
49cd0 0a 2f 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 ./*.** SQLITE_MA
49ce0 58 5f 55 33 32 20 69 73 20 61 20 75 36 34 20 63 X_U32 is a u64 c
49cf0 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 69 73 20 onstant that is
49d00 74 68 65 20 6d 61 78 69 6d 75 6d 20 75 36 34 20 the maximum u64
49d10 76 61 6c 75 65 0a 2a 2a 20 74 68 61 74 20 63 61 value.** that ca
49d20 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 n be stored in a
49d30 20 75 33 32 20 77 69 74 68 6f 75 74 20 6c 6f 73 u32 without los
49d40 73 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20 s of data. The
49d50 76 61 6c 75 65 0a 2a 2a 20 69 73 20 30 78 30 30 value.** is 0x00
49d60 30 30 30 30 30 30 66 66 66 66 66 66 66 66 2e 20 000000ffffffff.
49d70 20 42 75 74 20 62 65 63 61 75 73 65 20 6f 66 20 But because of
49d80 71 75 69 72 6b 73 20 6f 66 20 73 6f 6d 65 20 63 quirks of some c
49d90 6f 6d 70 69 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 ompilers, we.**
49da0 68 61 76 65 20 74 6f 20 73 70 65 63 69 66 79 20 have to specify
49db0 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 the value in the
49dc0 20 6c 65 73 73 20 69 6e 74 75 69 74 69 76 65 20 less intuitive
49dd0 6d 61 6e 6e 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f manner shown:.*/
49de0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
49df0 4d 41 58 5f 55 33 32 20 20 28 28 28 28 75 36 34 MAX_U32 ((((u64
49e00 29 31 29 3c 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a )1)<<32)-1)../*.
49e10 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 ** Macros to det
49e20 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 74 ermine whether t
49e30 68 65 20 6d 61 63 68 69 6e 65 20 69 73 20 62 69 he machine is bi
49e40 67 20 6f 72 20 6c 69 74 74 6c 65 20 65 6e 64 69 g or little endi
49e50 61 6e 2c 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 an,.** evaluated
49e60 20 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a at runtime..*/.
49e70 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 4d #ifdef SQLITE_AM
49e80 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 ALGAMATION.SQLIT
49e90 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
49ea0 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d int sqlite3one =
49eb0 20 31 3b 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 1;.#else.SQLITE
49ec0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 _PRIVATE const i
49ed0 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 nt sqlite3one;.#
49ee0 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 endif.#if define
49ef0 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e d(i386) || defin
49f00 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 ed(__i386__) ||
49f10 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 defined(_M_IX86)
49f20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
49f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
49f40 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f | defined(__x86_
49f50 36 34 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 64) || defined(_
49f60 5f 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64 65 66 _x86_64__).# def
49f70 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e ine SQLITE_BIGEN
49f80 44 49 41 4e 20 20 20 20 30 0a 23 20 64 65 66 69 DIAN 0.# defi
49f90 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 ne SQLITE_LITTLE
49fa0 45 4e 44 49 41 4e 20 31 0a 23 20 64 65 66 69 6e ENDIAN 1.# defin
49fb0 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 e SQLITE_UTF16NA
49fc0 54 49 56 45 20 20 53 51 4c 49 54 45 5f 55 54 46 TIVE SQLITE_UTF
49fd0 31 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64 65 66 16LE.#else.# def
49fe0 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 45 4e ine SQLITE_BIGEN
49ff0 44 49 41 4e 20 20 20 20 28 2a 28 63 68 61 72 20 DIAN (*(char
4a000 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d *)(&sqlite3one)=
4a010 3d 30 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c =0).# define SQL
4a020 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41 4e ITE_LITTLEENDIAN
4a030 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73 71 6c (*(char *)(&sql
4a040 69 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 ite3one)==1).# d
4a050 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 efine SQLITE_UTF
4a060 31 36 4e 41 54 49 56 45 20 28 53 51 4c 49 54 45 16NATIVE (SQLITE
4a070 5f 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c 49 54 _BIGENDIAN?SQLIT
4a080 45 5f 55 54 46 31 36 42 45 3a 53 51 4c 49 54 45 E_UTF16BE:SQLITE
4a090 5f 55 54 46 31 36 4c 45 29 0a 23 65 6e 64 69 66 _UTF16LE).#endif
4a0a0 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 ../*.** Constant
4a0b0 73 20 66 6f 72 20 74 68 65 20 6c 61 72 67 65 73 s for the larges
4a0c0 74 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74 20 70 t and smallest p
4a0d0 6f 73 73 69 62 6c 65 20 36 34 2d 62 69 74 20 73 ossible 64-bit s
4a0e0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a igned integers..
4a0f0 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 ** These macros
4a100 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20 are designed to
4a110 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f work correctly o
4a120 6e 20 62 6f 74 68 20 33 32 2d 62 69 74 20 61 6e n both 32-bit an
4a130 64 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 d 64-bit.** comp
4a140 69 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e ilers..*/.#defin
4a150 65 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 20 e LARGEST_INT64
4a160 20 28 30 78 66 66 66 66 66 66 66 66 7c 28 28 28 (0xffffffff|(((
4a170 69 36 34 29 30 78 37 66 66 66 66 66 66 66 29 3c i64)0x7fffffff)<
4a180 3c 33 32 29 29 0a 23 64 65 66 69 6e 65 20 53 4d <32)).#define SM
4a190 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28 28 28 ALLEST_INT64 (((
4a1a0 69 36 34 29 2d 31 29 20 2d 20 4c 41 52 47 45 53 i64)-1) - LARGES
4a1b0 54 5f 49 4e 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a T_INT64)../* .**
4a1c0 20 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62 Round up a numb
4a1d0 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c er to the next l
4a1e0 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f arger multiple o
4a1f0 66 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73 f 8. This is us
4a200 65 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38 ed.** to force 8
4a210 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 -byte alignment
4a220 6f 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74 on 64-bit archit
4a230 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 ectures..*/.#def
4a240 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20 ine ROUND8(x)
4a250 20 20 28 28 28 78 29 2b 37 29 26 7e 37 29 0a 0a (((x)+7)&~7)..
4a260 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 64 6f 77 6e /*.** Round down
4a270 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20 to the nearest
4a280 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 0a 2a 2f multiple of 8.*/
4a290 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 44 4f .#define ROUNDDO
4a2a0 57 4e 38 28 78 29 20 28 28 78 29 26 7e 37 29 0a WN8(x) ((x)&~7).
4a2b0 0a 2f 2a 0a 2a 2a 20 41 73 73 65 72 74 20 74 68 ./*.** Assert th
4a2c0 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 58 at the pointer X
4a2d0 20 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 is aligned to a
4a2e0 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 n 8-byte boundar
4a2f0 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 49 y..*/.#define EI
4a300 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
4a310 4e 54 28 58 29 20 20 20 28 28 28 28 63 68 61 72 NT(X) ((((char
4a320 2a 29 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30 *)(X) - (char*)0
4a330 29 26 37 29 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a )&7)==0).../*.**
4a340 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
4a350 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
4a360 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 ructure is used
4a370 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 75 73 to store the bus
4a380 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c y-handler.** cal
4a390 6c 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76 65 lback for a give
4a3a0 6e 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 2e n sqlite handle.
4a3b0 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 .**.** The sqli
4a3c0 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20 6d te.busyHandler m
4a3d0 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71 6c ember of the sql
4a3e0 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74 61 ite struct conta
4a3f0 69 6e 73 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 ins the busy.**
4a400 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 callback for the
4a410 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
4a420 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f 70 65 . Each pager ope
4a430 6e 65 64 20 76 69 61 20 74 68 65 20 73 71 6c 69 ned via the sqli
4a440 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73 20 te.** handle is
4a450 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65 72 passed a pointer
4a460 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73 79 48 to sqlite.busyH
4a470 61 6e 64 6c 65 72 2e 20 54 68 65 20 62 75 73 79 andler. The busy
4a480 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c -handler.** call
4a490 62 61 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c back is currentl
4a4a0 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 y invoked only f
4a4b0 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 rom within pager
4a4c0 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 .c..*/.typedef s
4a4d0 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 truct BusyHandle
4a4e0 72 20 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 73 r BusyHandler;.s
4a4f0 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c 65 truct BusyHandle
4a500 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e r {. int (*xFun
4a510 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 c)(void *,int);
4a520 20 2f 2a 20 54 68 65 20 62 75 73 79 20 63 61 6c /* The busy cal
4a530 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 lback */. void
4a540 2a 70 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 *pArg;
4a550 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 /* First a
4a560 72 67 20 74 6f 20 62 75 73 79 20 63 61 6c 6c 62 rg to busy callb
4a570 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 ack */. int nBu
4a580 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sy;
4a590 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 /* Increment
4a5a0 65 64 20 77 69 74 68 20 65 61 63 68 20 62 75 73 ed with each bus
4a5b0 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a y call */.};../*
4a5c0 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 .** Name of the
4a5d0 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65 20 master database
4a5e0 74 61 62 6c 65 2e 20 20 54 68 65 20 6d 61 73 74 table. The mast
4a5f0 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c er database tabl
4a600 65 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69 61 e.** is a specia
4a610 6c 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f 6c l table that hol
4a620 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 ds the names and
4a630 20 61 74 74 72 69 62 75 74 65 73 20 6f 66 20 61 attributes of a
4a640 6c 6c 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c 65 ll.** user table
4a650 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a 2a s and indices..*
4a660 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 /.#define MASTER
4a670 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73 71 6c _NAME "sql
4a680 69 74 65 5f 6d 61 73 74 65 72 22 0a 23 64 65 66 ite_master".#def
4a690 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54 45 52 5f ine TEMP_MASTER_
4a6a0 4e 41 4d 45 20 20 22 73 71 6c 69 74 65 5f 74 65 NAME "sqlite_te
4a6b0 6d 70 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a mp_master"../*.*
4a6c0 2a 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 * The root-page
4a6d0 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 of the master da
4a6e0 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f tabase table..*/
4a6f0 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52 5f .#define MASTER_
4a700 52 4f 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f 2a ROOT 1../*
4a710 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 .** The name of
4a720 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 the schema table
4a730 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43 48 ..*/.#define SCH
4a740 45 4d 41 5f 54 41 42 4c 45 28 78 29 20 20 28 28 EMA_TABLE(x) ((
4a750 21 4f 4d 49 54 5f 54 45 4d 50 44 42 29 26 26 28 !OMIT_TEMPDB)&&(
4a760 78 3d 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54 45 x==1)?TEMP_MASTE
4a770 52 5f 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 R_NAME:MASTER_NA
4a780 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e ME)../*.** A con
4a790 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20 74 venience macro t
4a7a0 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 hat returns the
4a7b0 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e number of elemen
4a7c0 74 73 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 ts in.** an arra
4a7d0 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41 72 y..*/.#define Ar
4a7e0 72 61 79 53 69 7a 65 28 58 29 20 20 20 20 28 28 raySize(X) ((
4a7f0 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f 73 int)(sizeof(X)/s
4a800 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f izeof(X[0])))../
4a810 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
4a820 6e 67 20 76 61 6c 75 65 20 61 73 20 61 20 64 65 ng value as a de
4a830 73 74 72 75 63 74 6f 72 20 6d 65 61 6e 73 20 74 structor means t
4a840 6f 20 75 73 65 20 73 71 6c 69 74 65 33 44 62 46 o use sqlite3DbF
4a850 72 65 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 69 ree()..** This i
4a860 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 78 s an internal ex
4a870 74 65 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49 54 tension to SQLIT
4a880 45 5f 53 54 41 54 49 43 20 61 6e 64 20 53 51 4c E_STATIC and SQL
4a890 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a 2a ITE_TRANSIENT..*
4a8a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
4a8b0 5f 44 59 4e 41 4d 49 43 20 20 20 28 28 73 71 6c _DYNAMIC ((sql
4a8c0 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f ite3_destructor_
4a8d0 74 79 70 65 29 73 71 6c 69 74 65 33 44 62 46 72 type)sqlite3DbFr
4a8e0 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 ee)../*.** When
4a8f0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 SQLITE_OMIT_WSD
4a900 69 73 20 64 65 66 69 6e 65 64 2c 20 69 74 20 6d is defined, it m
4a910 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61 eans that the ta
4a920 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 20 64 6f rget platform do
4a930 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f 72 es.** not suppor
4a940 74 20 57 72 69 74 61 62 6c 65 20 53 74 61 74 69 t Writable Stati
4a950 63 20 44 61 74 61 20 28 57 53 44 29 20 73 75 63 c Data (WSD) suc
4a960 68 20 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64 20 h as global and
4a970 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 static variables
4a980 2e 0a 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62 6c ..** All variabl
4a990 65 73 20 6d 75 73 74 20 65 69 74 68 65 72 20 62 es must either b
4a9a0 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f e on the stack o
4a9b0 72 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c r dynamically al
4a9c0 6c 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 located from.**
4a9d0 74 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e 20 the heap. When
4a9e0 57 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72 74 WSD is unsupport
4a9f0 65 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c 65 ed, the variable
4aa00 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73 63 declarations sc
4aa10 61 74 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f 75 attered.** throu
4aa20 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74 65 ghout the SQLite
4aa30 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f 6d code must becom
4aa40 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73 74 e constants inst
4aa50 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 ead. The SQLITE
4aa60 5f 57 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 _WSD.** macro is
4aa70 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 70 used for this p
4aa80 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e 73 urpose. And ins
4aa90 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e 63 tead of referenc
4aaa0 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c 65 ing the variable
4aab0 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 77 65 .** directly, we
4aac0 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74 61 6e use its constan
4aad0 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 6c 6f t as a key to lo
4aae0 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74 69 6d okup the run-tim
4aaf0 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 e allocated.** b
4ab00 75 66 66 65 72 20 74 68 61 74 20 68 6f 6c 64 73 uffer that holds
4ab10 20 72 65 61 6c 20 76 61 72 69 61 62 6c 65 2e 20 real variable.
4ab20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74 20 69 73 The constant is
4ab30 20 61 6c 73 6f 20 74 68 65 20 69 6e 69 74 69 61 also the initia
4ab40 6c 69 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68 65 lizer.** for the
4ab50 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 run-time alloca
4ab60 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a ted buffer..**.*
4ab70 2a 20 49 6e 20 74 68 65 20 75 73 75 61 6c 20 63 * In the usual c
4ab80 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69 73 ase where WSD is
4ab90 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 supported, the
4aba0 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20 47 SQLITE_WSD and G
4abb0 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 LOBAL.** macros
4abc0 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e become no-ops an
4abd0 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72 66 d have zero perf
4abe0 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e 0a ormance impact..
4abf0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
4ac00 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65 66 _OMIT_WSD. #def
4ac10 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20 63 ine SQLITE_WSD c
4ac20 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20 47 onst. #define G
4ac30 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74 2a LOBAL(t,v) (*(t*
4ac40 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e )sqlite3_wsd_fin
4ac50 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20 73 d((void*)&(v), s
4ac60 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64 65 izeof(v))). #de
4ac70 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62 fine sqlite3Glob
4ac80 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c 28 alConfig GLOBAL(
4ac90 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f struct Sqlite3Co
4aca0 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f 6e nfig, sqlite3Con
4acb0 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49 20 fig).SQLITE_API
4acc0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77 73 int sqlite3_ws
4acd0 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69 6e d_init(int N, in
4ace0 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 t J);.SQLITE_API
4acf0 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 void *sqlite3
4ad00 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20 2a _wsd_find(void *
4ad10 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 K, int L);.#else
4ad20 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 . #define SQLIT
4ad30 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e 65 E_WSD . #define
4ad40 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a 20 GLOBAL(t,v) v.
4ad50 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
4ad60 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71 6c GlobalConfig sql
4ad70 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64 69 ite3Config.#endi
4ad80 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c f../*.** The fol
4ad90 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 61 72 lowing macros ar
4ada0 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 e used to suppre
4adb0 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e ss compiler warn
4adc0 69 6e 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d ings and to.** m
4add0 61 6b 65 20 69 74 20 63 6c 65 61 72 20 74 6f 20 ake it clear to
4ade0 68 75 6d 61 6e 20 72 65 61 64 65 72 73 20 77 68 human readers wh
4adf0 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 en a function pa
4ae00 72 61 6d 65 74 65 72 20 69 73 20 64 65 6c 69 62 rameter is delib
4ae10 65 72 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 erately .** left
4ae20 20 75 6e 75 73 65 64 20 77 69 74 68 69 6e 20 74 unused within t
4ae30 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e he body of a fun
4ae40 63 74 69 6f 6e 2e 20 54 68 69 73 20 75 73 75 61 ction. This usua
4ae50 6c 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e lly happens when
4ae60 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 .** a function i
4ae70 73 20 63 61 6c 6c 65 64 20 76 69 61 20 61 20 66 s called via a f
4ae80 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e unction pointer.
4ae90 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68 65 For example the
4aea0 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 .** implementat
4aeb0 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61 67 ion of an SQL ag
4aec0 67 72 65 67 61 74 65 20 73 74 65 70 20 63 61 6c gregate step cal
4aed0 6c 62 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75 73 lback may not us
4aee0 65 20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65 74 e the.** paramet
4aef0 65 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 er indicating th
4af00 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 e number of argu
4af10 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f 20 ments passed to
4af20 74 68 65 20 61 67 67 72 65 67 61 74 65 2c 0a 2a the aggregate,.*
4af30 2a 20 69 66 20 69 74 20 6b 6e 6f 77 73 20 74 68 * if it knows th
4af40 61 74 20 74 68 69 73 20 69 73 20 65 6e 66 6f 72 at this is enfor
4af50 63 65 64 20 65 6c 73 65 77 68 65 72 65 2e 0a 2a ced elsewhere..*
4af60 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e 63 *.** When a func
4af70 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 tion parameter i
4af80 73 20 6e 6f 74 20 75 73 65 64 20 61 74 20 61 6c s not used at al
4af90 6c 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64 l within the bod
4afa0 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 2c y of a function,
4afb0 0a 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72 61 .** it is genera
4afc0 6c 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55 73 lly named "NotUs
4afd0 65 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64 32 ed" or "NotUsed2
4afe0 22 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67 73 " to make things
4aff0 20 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a 2a even clearer..*
4b000 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73 65 * However, these
4b010 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73 6f macros may also
4b020 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 be used to supp
4b030 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72 65 ress warnings re
4b040 6c 61 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72 61 lated to.** para
4b050 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79 20 meters that may
4b060 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 or may not be us
4b070 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 ed depending on
4b080 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74 69 compilation opti
4b090 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d ons..** For exam
4b0a0 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61 6d 65 ple those parame
4b0b0 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 ters only used i
4b0c0 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 n assert() state
4b0d0 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73 65 0a ments. In these.
4b0e0 2a 2a 20 63 61 73 65 73 20 74 68 65 20 70 61 72 ** cases the par
4b0f0 61 6d 65 74 65 72 73 20 61 72 65 20 6e 61 6d 65 ameters are name
4b100 64 20 61 73 20 70 65 72 20 74 68 65 20 75 73 75 d as per the usu
4b110 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a al conventions..
4b120 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 55 53 45 */.#define UNUSE
4b130 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 20 28 D_PARAMETER(x) (
4b140 76 6f 69 64 29 28 78 29 0a 23 64 65 66 69 6e 65 void)(x).#define
4b150 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
4b160 52 32 28 78 2c 79 29 20 55 4e 55 53 45 44 5f 50 R2(x,y) UNUSED_P
4b170 41 52 41 4d 45 54 45 52 28 78 29 2c 55 4e 55 53 ARAMETER(x),UNUS
4b180 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 29 0a ED_PARAMETER(y).
4b190 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 72 ./*.** Forward r
4b1a0 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74 72 eferences to str
4b1b0 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65 64 uctures.*/.typed
4b1c0 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 ef struct AggInf
4b1d0 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65 64 o AggInfo;.typed
4b1e0 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43 6f ef struct AuthCo
4b1f0 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65 78 ntext AuthContex
4b200 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4b210 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 41 75 t AutoincInfo Au
4b220 74 6f 69 6e 63 49 6e 66 6f 3b 0a 74 79 70 65 64 toincInfo;.typed
4b230 65 66 20 73 74 72 75 63 74 20 42 69 74 76 65 63 ef struct Bitvec
4b240 20 42 69 74 76 65 63 3b 0a 74 79 70 65 64 65 66 Bitvec;.typedef
4b250 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 20 52 struct RowSet R
4b260 6f 77 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 owSet;.typedef s
4b270 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 43 6f truct CollSeq Co
4b280 6c 6c 53 65 71 3b 0a 74 79 70 65 64 65 66 20 73 llSeq;.typedef s
4b290 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c truct Column Col
4b2a0 75 6d 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 umn;.typedef str
4b2b0 75 63 74 20 44 62 20 44 62 3b 0a 74 79 70 65 64 uct Db Db;.typed
4b2c0 65 66 20 73 74 72 75 63 74 20 53 63 68 65 6d 61 ef struct Schema
4b2d0 20 53 63 68 65 6d 61 3b 0a 74 79 70 65 64 65 66 Schema;.typedef
4b2e0 20 73 74 72 75 63 74 20 45 78 70 72 20 45 78 70 struct Expr Exp
4b2f0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 r;.typedef struc
4b300 74 20 45 78 70 72 4c 69 73 74 20 45 78 70 72 4c t ExprList ExprL
4b310 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 ist;.typedef str
4b320 75 63 74 20 45 78 70 72 53 70 61 6e 20 45 78 70 uct ExprSpan Exp
4b330 72 53 70 61 6e 3b 0a 74 79 70 65 64 65 66 20 73 rSpan;.typedef s
4b340 74 72 75 63 74 20 46 4b 65 79 20 46 4b 65 79 3b truct FKey FKey;
4b350 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b360 46 75 6e 63 44 65 66 20 46 75 6e 63 44 65 66 3b FuncDef FuncDef;
4b370 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b380 46 75 6e 63 44 65 66 48 61 73 68 20 46 75 6e 63 FuncDefHash Func
4b390 44 65 66 48 61 73 68 3b 0a 74 79 70 65 64 65 66 DefHash;.typedef
4b3a0 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 49 struct IdList I
4b3b0 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 dList;.typedef s
4b3c0 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64 65 truct Index Inde
4b3d0 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 x;.typedef struc
4b3e0 74 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 49 6e t IndexSample In
4b3f0 64 65 78 53 61 6d 70 6c 65 3b 0a 74 79 70 65 64 dexSample;.typed
4b400 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 6c 61 ef struct KeyCla
4b410 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79 70 ss KeyClass;.typ
4b420 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 49 edef struct KeyI
4b430 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 nfo KeyInfo;.typ
4b440 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b edef struct Look
4b450 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65 3b aside Lookaside;
4b460 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b470 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c 6f LookasideSlot Lo
4b480 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79 70 okasideSlot;.typ
4b490 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64 75 edef struct Modu
4b4a0 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65 64 le Module;.typed
4b4b0 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 43 6f ef struct NameCo
4b4c0 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65 78 ntext NameContex
4b4d0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4b4e0 74 20 50 61 72 73 65 20 50 61 72 73 65 3b 0a 74 t Parse Parse;.t
4b4f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 61 ypedef struct Sa
4b500 76 65 70 6f 69 6e 74 20 53 61 76 65 70 6f 69 6e vepoint Savepoin
4b510 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 t;.typedef struc
4b520 74 20 53 65 6c 65 63 74 20 53 65 6c 65 63 74 3b t Select Select;
4b530 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b540 53 72 63 4c 69 73 74 20 53 72 63 4c 69 73 74 3b SrcList SrcList;
4b550 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4b560 53 74 72 41 63 63 75 6d 20 53 74 72 41 63 63 75 StrAccum StrAccu
4b570 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 m;.typedef struc
4b580 74 20 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 t Table Table;.t
4b590 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 ypedef struct Ta
4b5a0 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 bleLock TableLoc
4b5b0 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 k;.typedef struc
4b5c0 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 t Token Token;.t
4b5d0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 ypedef struct Tr
4b5e0 69 67 67 65 72 50 72 67 20 54 72 69 67 67 65 72 iggerPrg Trigger
4b5f0 50 72 67 3b 0a 74 79 70 65 64 65 66 20 73 74 72 Prg;.typedef str
4b600 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20 uct TriggerStep
4b610 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79 70 TriggerStep;.typ
4b620 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 edef struct Trig
4b630 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79 70 ger Trigger;.typ
4b640 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70 61 edef struct Unpa
4b650 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61 63 ckedRecord Unpac
4b660 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65 64 kedRecord;.typed
4b670 65 66 20 73 74 72 75 63 74 20 56 54 61 62 6c 65 ef struct VTable
4b680 20 56 54 61 62 6c 65 3b 0a 74 79 70 65 64 65 66 VTable;.typedef
4b690 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20 57 struct Walker W
4b6a0 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 66 20 73 alker;.typedef s
4b6b0 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 truct WherePlan
4b6c0 57 68 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65 64 WherePlan;.typed
4b6d0 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 49 ef struct WhereI
4b6e0 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 nfo WhereInfo;.t
4b6f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 ypedef struct Wh
4b700 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 ereLevel WhereLe
4b710 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 vel;../*.** Defe
4b720 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e r sourcing vdbe.
4b730 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e h and btree.h un
4b740 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 til after the "u
4b750 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 8" and .** "Busy
4b760 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 Handler" typedef
4b770 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 s. vdbe.h also r
4b780 65 71 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 equires a few of
4b790 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 the opaque.** p
4b7a0 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e ointer types (i.
4b7b0 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 e. FuncDef) defi
4b7c0 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a ned above..*/./*
4b7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e ************* In
4b7e0 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e clude btree.h in
4b7f0 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 the middle of s
4b800 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
4b810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
4b820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
4b830 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 gin file btree.h
4b840 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4b850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
4b870 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 .** 2001 Septemb
4b880 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 er 15.**.** The
4b890 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
4b8a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
4b8b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
4b8c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
4b8d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
4b8e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
4b8f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
4b900 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
4b910 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
4b920 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
4b930 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
4b940 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
4b950 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
4b960 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
4b970 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
4b980 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
4b990 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
4b9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4b9e0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 ****.** This hea
4b9f0 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 der file defines
4ba00 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 the interface t
4ba10 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 hat the sqlite B
4ba20 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 -Tree file.** su
4ba30 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f bsystem. See co
4ba40 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f mments in the so
4ba50 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 urce code for a
4ba60 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 detailed descrip
4ba70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 tion.** of what
4ba80 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 each interface r
4ba90 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2f 0a outine does..*/.
4baa0 23 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f 48 #ifndef _BTREE_H
4bab0 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45 45 _.#define _BTREE
4bac0 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54 68 _H_../* TODO: Th
4bad0 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 is definition is
4bae0 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20 73 just included s
4baf0 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 o other modules
4bb00 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20 6e compile. It.** n
4bb10 65 65 64 73 20 74 6f 20 62 65 20 72 65 76 69 73 eeds to be revis
4bb20 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ited..*/.#define
4bb30 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f SQLITE_N_BTREE_
4bb40 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 49 META 10../*.** I
4bb50 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e f defined as non
4bb60 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75 -zero, auto-vacu
4bb70 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79 um is enabled by
4bb80 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77 default. Otherw
4bb90 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62 ise.** it must b
4bba0 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20 e turned on for
4bbb0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73 each database us
4bbc0 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f ing "PRAGMA auto
4bbd0 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f _vacuum = 1"..*/
4bbe0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
4bbf0 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 DEFAULT_AUTOVACU
4bc00 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c UM. #define SQL
4bc10 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f ITE_DEFAULT_AUTO
4bc20 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a VACUUM 0.#endif.
4bc30 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 .#define BTREE_A
4bc40 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20 30 UTOVACUUM_NONE 0
4bc50 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f /* Do no
4bc60 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75 6d t do auto-vacuum
4bc70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 */.#define BTRE
4bc80 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c E_AUTOVACUUM_FUL
4bc90 4c 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44 6f L 1 /* Do
4bca0 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75 75 full auto-vacuu
4bcb0 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 m */.#define BTR
4bcc0 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e EE_AUTOVACUUM_IN
4bcd0 43 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20 49 CR 2 /* I
4bce0 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 ncremental vacuu
4bcf0 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 m */../*.** Forw
4bd00 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 ard declarations
4bd10 20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a 2f of structure.*/
4bd20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4bd30 42 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79 70 Btree Btree;.typ
4bd40 65 64 65 66 20 73 74 72 75 63 74 20 42 74 43 75 edef struct BtCu
4bd50 72 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a 74 rsor BtCursor;.t
4bd60 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 ypedef struct Bt
4bd70 53 68 61 72 65 64 20 42 74 53 68 61 72 65 64 3b Shared BtShared;
4bd80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4bd90 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 BtreeMutexArray
4bda0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 3b BtreeMutexArray;
4bdb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ../*.** This str
4bdc0 75 63 74 75 72 65 20 72 65 63 6f 72 64 73 20 61 ucture records a
4bdd0 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65 73 ll of the Btrees
4bde0 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68 6f that need to ho
4bdf0 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62 65 ld.** a mutex be
4be00 66 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73 71 fore we enter sq
4be10 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e lite3VdbeExec().
4be20 20 20 54 68 65 20 42 74 72 65 65 73 20 61 72 65 The Btrees are
4be30 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20 69 .** are placed i
4be40 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f 72 n aBtree[] in or
4be50 64 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d 2d der of aBtree[]-
4be60 3e 70 42 74 2e 20 20 54 68 61 74 20 77 61 79 2c >pBt. That way,
4be70 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61 79 .** we can alway
4be80 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f 63 s lock and unloc
4be90 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63 6b k them all quick
4bea0 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 ly..*/.struct Bt
4beb0 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b 0a reeMutexArray {.
4bec0 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20 20 int nMutex;.
4bed0 42 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53 51 Btree *aBtree[SQ
4bee0 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 LITE_MAX_ATTACHE
4bef0 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49 54 D+1];.};...SQLIT
4bf00 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4bf10 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a lite3BtreeOpen(.
4bf20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
4bf30 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 ilename, /* Na
4bf40 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 me of database f
4bf50 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 ile to open */.
4bf60 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
4bf70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 /* Ass
4bf80 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 ociated database
4bf90 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
4bfa0 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 Btree **ppBtree
4bfb0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 , /* Ret
4bfc0 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a 20 urn open Btree*
4bfd0 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c here */. int fl
4bfe0 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 ags,
4bff0 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 /* Flags */.
4c000 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20 int vfsFlags
4c010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
4c020 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 gs passed throug
4c030 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a 2f h to VFS open */
4c040 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 .);../* The flag
4c050 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 s parameter to s
4c060 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 20 qlite3BtreeOpen
4c070 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 can be the bitwi
4c080 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a 20 se or of the.**
4c090 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 following values
4c0a0 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 54 ..**.** NOTE: T
4c0b0 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74 hese values must
4c0c0 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65 match the corre
4c0d0 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f 20 sponding PAGER_
4c0e0 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61 67 values in.** pag
4c0f0 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 er.h..*/.#define
4c100 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 BTREE_OMIT_JOUR
4c110 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e 6f NAL 1 /* Do no
4c120 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 t use journal.
4c130 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 No argument */.#
4c140 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f 5f define BTREE_NO_
4c150 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f 2a READLOCK 2 /*
4c160 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 Omit readlocks
4c170 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 on readonly file
4c180 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 s */.#define BTR
4c190 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 20 EE_MEMORY
4c1a0 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 4 /* In-memory
4c1b0 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e DB. No argumen
4c1c0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 t */.#define BTR
4c1d0 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 EE_READONLY
4c1e0 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 8 /* Open the
4c1f0 64 61 74 61 62 61 73 65 20 69 6e 20 72 65 61 64 database in read
4c200 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23 64 -only mode */.#d
4c210 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41 44 efine BTREE_READ
4c220 57 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a 20 WRITE 16 /*
4c230 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72 65 Open for both re
4c240 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e ading and writin
4c250 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 g */.#define BTR
4c260 45 45 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 EE_CREATE
4c270 33 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 32 /* Create th
4c280 65 20 64 61 74 61 62 61 73 65 20 69 66 20 69 74 e database if it
4c290 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 does not exist
4c2a0 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 */..SQLITE_PRIVA
4c2b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4c2c0 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a 29 reeClose(Btree*)
4c2d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c2e0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c2f0 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 eSetCacheSize(Bt
4c300 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ree*,int);.SQLIT
4c310 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4c320 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 lite3BtreeSetSaf
4c330 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a 2c etyLevel(Btree*,
4c340 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
4c350 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4c360 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 ite3BtreeSyncDis
4c370 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a 53 abled(Btree*);.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 53 65 t sqlite3BtreeSe
4c3a0 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 tPageSize(Btree
4c3b0 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 73 69 7a *p, int nPagesiz
4c3c0 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c e, int nReserve,
4c3d0 20 69 6e 74 20 65 46 69 78 29 3b 0a 53 51 4c 49 int eFix);.SQLI
4c3e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c3f0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 qlite3BtreeGetPa
4c400 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b 0a geSize(Btree*);.
4c410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4c420 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
4c430 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 axPageCount(Btre
4c440 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f e*,int);.SQLITE_
4c450 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c460 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65 72 te3BtreeGetReser
4c470 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 ve(Btree*);.SQLI
4c480 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c490 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 qlite3BtreeSetAu
4c4a0 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a toVacuum(Btree *
4c4b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
4c4c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c4d0 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 e3BtreeGetAutoVa
4c4e0 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a 53 cuum(Btree *);.S
4c4f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c500 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 t sqlite3BtreeBe
4c510 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 2c ginTrans(Btree*,
4c520 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
4c530 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c540 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
4c550 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e 73 One(Btree*, cons
4c560 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 t char *zMaster)
4c570 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4c580 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4c590 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 eCommitPhaseTwo(
4c5a0 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c5b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c5c0 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 te3BtreeCommit(B
4c5d0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 tree*);.SQLITE_P
4c5e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
4c5f0 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 e3BtreeRollback(
4c600 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Btree*);.SQLITE_
4c610 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4c620 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d te3BtreeBeginStm
4c630 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 t(Btree*,int);.S
4c640 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4c650 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 t sqlite3BtreeCr
4c660 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 2a eateTable(Btree*
4c670 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 6c 61 67 , int*, int flag
4c680 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 s);.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 49 73 49 6e 54 72 61 6e 73 28 42 74 72 reeIsInTrans(Btr
4c6b0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ee*);.SQLITE_PRI
4c6c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c6d0 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 BtreeIsInReadTra
4c6e0 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 ns(Btree*);.SQLI
4c6f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4c700 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 qlite3BtreeIsInB
4c710 61 63 6b 75 70 28 42 74 72 65 65 2a 29 3b 0a 53 ackup(Btree*);.S
4c720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
4c730 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 id *sqlite3Btree
4c740 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c 20 Schema(Btree *,
4c750 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 int, void(*)(voi
4c760 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 d *));.SQLITE_PR
4c770 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
4c780 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 3BtreeSchemaLock
4c790 65 64 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 ed(Btree *pBtree
4c7a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4c7b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4c7c0 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 eeLockTable(Btre
4c7d0 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 e *pBtree, int i
4c7e0 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c Tab, u8 isWriteL
4c7f0 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ock);.SQLITE_PRI
4c800 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4c810 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28 42 BtreeSavepoint(B
4c820 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 tree *, int, int
4c830 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
4c840 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
4c850 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 qlite3BtreeGetFi
4c860 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b lename(Btree *);
4c870 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c880 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
4c890 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e te3BtreeGetJourn
4c8a0 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29 3b alname(Btree *);
4c8b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
4c8c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
4c8d0 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20 2a CopyFile(Btree *
4c8e0 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51 4c , Btree *);..SQL
4c8f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4c900 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 sqlite3BtreeIncr
4c910 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b Vacuum(Btree *);
4c920 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 70 ../* The flags p
4c930 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 arameter to sqli
4c940 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 te3BtreeCreateTa
4c950 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20 62 ble can be the b
4c960 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66 20 itwise OR.** of
4c970 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c the following fl
4c980 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ags:.*/.#define
4c990 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20 20 BTREE_INTKEY
4c9a0 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 1 /* Table h
4c9b0 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20 73 as only 64-bit s
4c9c0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b 65 igned integer ke
4c9d0 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 ys */.#define BT
4c9e0 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20 32 REE_ZERODATA 2
4c9f0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61 73 /* Table has
4ca00 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 keys only - no
4ca10 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65 20 data */.#define
4ca20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20 20 BTREE_LEAFDATA
4ca30 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73 74 4 /* Data st
4ca40 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20 6f ored in leaves o
4ca50 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49 4e nly. Implies IN
4ca60 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f TKEY */..SQLITE_
4ca70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
4ca80 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c te3BtreeDropTabl
4ca90 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 e(Btree*, int, i
4caa0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 nt*);.SQLITE_PRI
4cab0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4cac0 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65 28 BtreeClearTable(
4cad0 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Btree*, int, int
4cae0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4caf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
4cb00 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
4cb10 72 73 28 42 74 72 65 65 2a 2c 20 69 6e 74 29 3b rs(Btree*, int);
4cb20 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
4cb30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
4cb40 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 20 eeGetMeta(Btree
4cb50 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 64 78 *pBtree, int idx
4cb60 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 3b 0a , u32 *pValue);.
4cb70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
4cb80 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 nt sqlite3BtreeU
4cb90 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 2a pdateMeta(Btree*
4cba0 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20 76 , int idx, u32 v
4cbb0 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 alue);../*.** Th
4cbc0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 e second paramet
4cbd0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 er to sqlite3Btr
4cbe0 65 65 47 65 74 4d 65 74 61 20 6f 72 20 73 71 6c eeGetMeta or sql
4cbf0 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d ite3BtreeUpdateM
4cc00 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 eta.** should be
4cc10 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c one of the foll
4cc20 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20 54 68 owing values. Th
4cc30 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 e integer values
4cc40 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 0a 2a are assigned .*
4cc50 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73 20 73 * to constants s
4cc60 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66 73 65 o that the offse
4cc70 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 t of the corresp
4cc80 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69 6e 20 onding field in
4cc90 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 an.** SQLite dat
4cca0 61 62 61 73 65 20 68 65 61 64 65 72 20 6d 61 79 abase header may
4ccb0 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e 67 20 be found using
4ccc0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f the following fo
4ccd0 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 6f rmula:.**.** o
4cce0 66 66 73 65 74 20 3d 20 33 36 20 2b 20 28 69 64 ffset = 36 + (id
4ccf0 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 46 6f 72 x * 4).**.** For
4cd00 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 72 example, the fr
4cd10 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 20 66 69 ee-page-count fi
4cd20 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 20 61 eld is located a
4cd30 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 33 36 t byte offset 36
4cd40 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 of.** the datab
4cd50 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 2e ase file header.
4cd60 20 54 68 65 20 69 6e 63 72 2d 76 61 63 75 75 6d The incr-vacuum
4cd70 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 73 20 6c -flag field is l
4cd80 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 62 79 74 ocated at.** byt
4cd90 65 20 6f 66 66 73 65 74 20 36 34 20 28 3d 3d 20 e offset 64 (==
4cda0 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 64 65 66 36+4*7)..*/.#def
4cdb0 69 6e 65 20 42 54 52 45 45 5f 46 52 45 45 5f 50 ine BTREE_FREE_P
4cdc0 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 20 30 0a AGE_COUNT 0.
4cdd0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 53 43 #define BTREE_SC
4cde0 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 20 20 20 HEMA_VERSION
4cdf0 20 20 31 0a 23 64 65 66 69 6e 65 20 42 54 52 45 1.#define BTRE
4ce00 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 20 20 E_FILE_FORMAT
4ce10 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 2.#define
4ce20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 BTREE_DEFAULT_CA
4ce30 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 64 65 66 CHE_SIZE 3.#def
4ce40 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 47 45 53 ine BTREE_LARGES
4ce50 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 20 34 0a T_ROOT_PAGE 4.
4ce60 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 54 45 #define BTREE_TE
4ce70 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20 20 20 XT_ENCODING
4ce80 20 20 35 0a 23 64 65 66 69 6e 65 20 42 54 52 45 5.#define BTRE
4ce90 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 20 20 E_USER_VERSION
4cea0 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 6.#define
4ceb0 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55 BTREE_INCR_VACUU
4cec0 4d 20 20 20 20 20 20 20 20 20 37 0a 0a 53 51 4c M 7..SQL
4ced0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4cee0 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
4cef0 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20 20 or(. Btree*,
4cf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 54 /* BT
4cf20 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ree containing t
4cf30 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a able to open */.
4cf40 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 int iTable,
4cf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cf60 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
4cf70 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f 0a of root page */.
4cf80 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 int wrFlag,
4cf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cfa0 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 /* 1 for
4cfb0 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72 20 writing. 0 for
4cfc0 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 read-only */. s
4cfd0 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c 20 truct KeyInfo*,
4cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4cff0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 /* First arg
4d000 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72 65 ument to compare
4d010 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 function */. B
4d020 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 tCursor *pCursor
4d030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d040 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 /* Space to
4d050 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 74 72 write cursor str
4d060 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c ucture */.);.SQL
4d070 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d080 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
4d090 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 orSize(void);.SQ
4d0a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
4d0b0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 d sqlite3BtreeCu
4d0c0 72 73 6f 72 5a 65 72 6f 28 42 74 43 75 72 73 6f rsorZero(BtCurso
4d0d0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 r*);..SQLITE_PRI
4d0e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
4d0f0 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 BtreeCloseCursor
4d100 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c (BtCursor*);.SQL
4d110 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
4d120 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
4d130 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 toUnpacked(. Bt
4d140 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e 70 61 63 Cursor*,. Unpac
4d150 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 4b 65 kedRecord *pUnKe
4d160 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c y,. i64 intKey,
4d170 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a 20 20 69 . int bias,. i
4d180 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 51 4c 49 nt *pRes.);.SQLI
4d190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4d1a0 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
4d1b0 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 rHasMoved(BtCurs
4d1c0 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 or*, int*);.SQLI
4d1d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4d1e0 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
4d1f0 65 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 e(BtCursor*);.SQ
4d200 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d210 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 sqlite3BtreeIns
4d220 65 72 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 63 ert(BtCursor*, c
4d230 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c onst void *pKey,
4d240 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 20 20 20 i64 nKey,.
4d250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d260 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e con
4d270 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 st void *pData,
4d280 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 int nData,.
4d290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4d2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
4d2b0 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 69 61 73 nZero, int bias
4d2c0 2c 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 , int seekResult
4d2d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d2e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4d2f0 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 eeFirst(BtCursor
4d300 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 *, int *pRes);.S
4d310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
4d320 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 t sqlite3BtreeLa
4d330 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e st(BtCursor*, in
4d340 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 t *pRes);.SQLITE
4d350 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
4d360 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 ite3BtreeNext(Bt
4d370 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 Cursor*, int *pR
4d380 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 es);.SQLITE_PRIV
4d390 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
4d3a0 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f 72 treeEof(BtCursor
4d3b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
4d3c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
4d3d0 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 reePrevious(BtCu
4d3e0 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 rsor*, int *pRes
4d3f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d400 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
4d410 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 eeKeySize(BtCurs
4d420 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 or*, i64 *pSize)
4d430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d440 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4d450 65 4b 65 79 28 42 74 43 75 72 73 6f 72 2a 2c 20 eKey(BtCursor*,
4d460 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
4d470 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c amt, void*);.SQL
4d480 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
4d490 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 t void *sqlite3B
4d4a0 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 treeKeyFetch(BtC
4d4b0 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d ursor*, int *pAm
4d4c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
4d4d0 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 TE const void *s
4d4e0 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46 qlite3BtreeDataF
4d4f0 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 etch(BtCursor*,
4d500 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 int *pAmt);.SQLI
4d510 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4d520 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 qlite3BtreeDataS
4d530 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 ize(BtCursor*, u
4d540 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 32 *pSize);.SQLI
4d550 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
4d560 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 qlite3BtreeData(
4d570 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f BtCursor*, u32 o
4d580 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 ffset, u32 amt,
4d590 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 void*);.SQLITE_P
4d5a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
4d5b0 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 te3BtreeSetCache
4d5c0 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a dRowid(BtCursor*
4d5d0 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 , sqlite3_int64)
4d5e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d5f0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
4d600 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61 qlite3BtreeGetCa
4d610 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 chedRowid(BtCurs
4d620 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 or*);..SQLITE_PR
4d630 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
4d640 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 te3BtreeIntegrit
4d650 79 43 68 65 63 6b 28 42 74 72 65 65 2a 2c 20 69 yCheck(Btree*, i
4d660 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e 74 20 6e nt *aRoot, int n
4d670 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 Root, int, int*)
4d680 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4d690 20 73 74 72 75 63 74 20 50 61 67 65 72 20 2a 73 struct Pager *s
4d6a0 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
4d6b0 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 (Btree*);..SQLIT
4d6c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
4d6d0 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74 lite3BtreePutDat
4d6e0 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 a(BtCursor*, u32
4d6f0 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 offset, u32 amt
4d700 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 , void*);.SQLITE
4d710 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
4d720 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65 4f lite3BtreeCacheO
4d730 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 verflow(BtCursor
4d740 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
4d750 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
4d760 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 BtreeClearCursor
4d770 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 (BtCursor *);..#
4d780 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 ifndef NDEBUG.SQ
4d790 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d7a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
4d7b0 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75 72 sorIsValid(BtCur
4d7c0 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 sor*);.#endif..#
4d7d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
4d7e0 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 53 51 IT_BTREECOUNT.SQ
4d7f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
4d800 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 sqlite3BtreeCou
4d810 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 nt(BtCursor *, i
4d820 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 64 *);.#endif..#
4d830 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
4d840 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
4d850 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
4d860 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43 75 eCursorInfo(BtCu
4d870 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 rsor*, int*, int
4d880 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
4d890 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
4d8a0 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42 74 reeCursorList(Bt
4d8b0 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f ree*);.#endif../
4d8c0 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 6e *.** If we are n
4d8d0 6f 74 20 75 73 69 6e 67 20 73 68 61 72 65 64 20 ot using shared
4d8e0 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 72 cache, then ther
4d8f0 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 0a e is no need to.
4d900 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 73 20 74 ** use mutexes t
4d910 6f 20 61 63 63 65 73 73 20 74 68 65 20 42 74 53 o access the BtS
4d920 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 hared structures
4d930 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 65 0a 2a . So make the.*
4d940 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c 65 61 76 * Enter and Leav
4d950 65 20 70 72 6f 63 65 64 75 72 65 73 20 6e 6f 2d e procedures no-
4d960 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ops..*/.#ifndef
4d970 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
4d980 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f ED_CACHE.SQLITE_
4d990 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
4d9a0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
4d9b0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 (Btree*);.SQLITE
4d9c0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4d9d0 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
4d9e0 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a rAll(sqlite3*);.
4d9f0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 #else.# define s
4da00 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
4da10 28 58 29 20 0a 23 20 64 65 66 69 6e 65 20 73 71 (X) .# define sq
4da20 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
4da30 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 ll(X).#endif..#i
4da40 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 f !defined(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 29 20 26 26 20 53 51 4c 49 54 45 5f 54 CHE) && SQLITE_T
4da70 48 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45 HREADSAFE.SQLITE
4da80 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4da90 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
4daa0 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 e(Btree*);.SQLIT
4dab0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 E_PRIVATE void
4dac0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
4dad0 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f erCursor(BtCurso
4dae0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
4daf0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
4db00 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 e3BtreeLeaveCurs
4db10 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 or(BtCursor*);.S
4db20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4db30 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4db40 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 eLeaveAll(sqlite
4db50 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 3*);.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 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
4db80 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 yEnter(BtreeMute
4db90 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 xArray*);.SQLITE
4dba0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
4dbb0 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 sqlite3BtreeMute
4dbc0 78 41 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 xArrayLeave(Btre
4dbd0 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 eMutexArray*);.S
4dbe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
4dbf0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
4dc00 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 eMutexArrayInser
4dc10 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 t(BtreeMutexArra
4dc20 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a 23 69 66 y*, Btree*);.#if
4dc30 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a ndef NDEBUG. /*
4dc40 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 These routines
4dc50 61 72 65 20 75 73 65 64 20 69 6e 73 69 64 65 20 are used inside
4dc60 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
4dc70 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 4c nts only. */.SQL
4dc80 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
4dc90 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f t sqlite3BtreeHo
4dca0 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a 29 ldsMutex(Btree*)
4dcb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
4dcc0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 int sqlite3Bt
4dcd0 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 reeHoldsAllMutex
4dce0 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 es(sqlite3*);.#e
4dcf0 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23 20 64 65 ndif.#else..# de
4dd00 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 fine sqlite3Btre
4dd10 65 4c 65 61 76 65 28 58 29 0a 23 20 64 65 66 69 eLeave(X).# defi
4dd20 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 ne sqlite3BtreeE
4dd30 6e 74 65 72 43 75 72 73 6f 72 28 58 29 0a 23 20 nterCursor(X).#
4dd40 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 define sqlite3Bt
4dd50 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 58 reeLeaveCursor(X
4dd60 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
4dd70 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 e3BtreeLeaveAll(
4dd80 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 X).# define sqli
4dd90 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
4dda0 61 79 45 6e 74 65 72 28 58 29 0a 23 20 64 65 66 ayEnter(X).# def
4ddb0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 ine sqlite3Btree
4ddc0 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 MutexArrayLeave(
4ddd0 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 X).# define sqli
4dde0 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
4ddf0 61 79 49 6e 73 65 72 74 28 58 2c 59 29 0a 0a 23 ayInsert(X,Y)..#
4de00 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 define sqlite3B
4de10 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 58 treeHoldsMutex(X
4de20 29 20 31 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ) 1.# define sql
4de30 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c ite3BtreeHoldsAl
4de40 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23 65 lMutexes(X) 1.#e
4de50 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a ndif...#endif /*
4de60 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f _BTREE_H_ */../
4de70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
4de80 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 20 2a 2a nd of btree.h **
4de90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4deb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
4dec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 ************** C
4ded0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 ontinuing where
4dee0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 we left off in s
4def0 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
4df00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
4df10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
4df20 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20 69 6e nclude vdbe.h in
4df30 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 the middle of s
4df40 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a qliteInt.h *****
4df50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
4df60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
4df70 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e 68 egin file vdbe.h
4df80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
4dfb0 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d *.** 2001 Septem
4dfc0 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 15.**.** The
4dfd0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
4dfe0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
4dff0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
4e000 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
4e010 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
4e020 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
4e030 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
4e040 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
4e050 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
4e060 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
4e070 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
4e080 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
4e090 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
4e0a0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
4e0b0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
4e0c0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
4e0d0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
4e0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4e120 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65 72 20 *****.** Header
4e130 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56 69 72 file for the Vir
4e140 74 75 61 6c 20 44 61 74 61 42 61 73 65 20 45 6e tual DataBase En
4e150 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a 0a 2a gine (VDBE).**.*
4e160 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 64 65 * This header de
4e170 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 fines the interf
4e180 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 ace to the virtu
4e190 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 al database engi
4e1a0 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e 20 20 ne.** or VDBE.
4e1b0 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 The VDBE impleme
4e1c0 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63 74 20 nts an abstract
4e1d0 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72 75 6e machine that run
4e1e0 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20 70 72 s a.** simple pr
4e1f0 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73 73 20 ogram to access
4e200 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20 75 and modify the u
4e210 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61 nderlying databa
4e220 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f se..*/.#ifndef _
4e230 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a 23 SQLITE_VDBE_H_.#
4e240 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56 define _SQLITE_V
4e250 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 DBE_H_../*.** A
4e260 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 20 61 single VDBE is a
4e270 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 n opaque structu
4e280 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 22 2e re named "Vdbe".
4e290 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 0a Only routines.
4e2a0 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 ** in the source
4e2b0 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 62 65 file sqliteVdbe
4e2c0 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 .c are allowed t
4e2d0 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 64 65 o see the inside
4e2e0 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 s.** of this str
4e2f0 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 ucture..*/.typed
4e300 65 66 20 73 74 72 75 63 74 20 56 64 62 65 20 56 ef struct Vdbe V
4e310 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 dbe;../*.** The
4e320 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 6c names of the fol
4e330 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 65 63 lowing types dec
4e340 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 6e 74 lared in vdbeInt
4e350 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 64 0a .h are required.
4e360 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65 4f ** for the VdbeO
4e370 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f p definition..*/
4e380 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
4e390 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 75 6e VdbeFunc VdbeFun
4e3a0 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 c;.typedef struc
4e3b0 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79 70 65 64 t Mem Mem;.typed
4e3c0 65 66 20 73 74 72 75 63 74 20 53 75 62 50 72 6f ef struct SubPro
4e3d0 67 72 61 6d 20 53 75 62 50 72 6f 67 72 61 6d 3b gram SubProgram;
4e3e0 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 ../*.** A single
4e3f0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 instruction of
4e400 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 the virtual mach
4e410 69 6e 65 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 ine has an opcod
4e420 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 e.** and as many
4e430 20 61 73 20 74 68 72 65 65 20 6f 70 65 72 61 6e as three operan
4e440 64 73 2e 20 20 54 68 65 20 69 6e 73 74 72 75 63 ds. The instruc
4e450 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 64 65 64 tion is recorded
4e460 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73 74 61 6e .** as an instan
4e470 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
4e480 69 6e 67 20 73 74 72 75 63 74 75 72 65 3a 0a 2a ing structure:.*
4e490 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 /.struct VdbeOp
4e4a0 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 {. u8 opcode;
4e4b0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 /* What
4e4c0 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 operation to per
4e4d0 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 form */. signed
4e4e0 20 63 68 61 72 20 70 34 74 79 70 65 3b 20 2f 2a char p4type; /*
4e4f0 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 34 5f 78 One of the P4_x
4e500 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 xx constants for
4e510 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c p4 */. u8 opfl
4e520 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ags; /*
4e530 4d 61 73 6b 20 6f 66 20 74 68 65 20 4f 50 46 4c Mask of the OPFL
4e540 47 5f 2a 20 66 6c 61 67 73 20 69 6e 20 6f 70 63 G_* flags in opc
4e550 6f 64 65 73 2e 68 20 2a 2f 0a 20 20 75 38 20 70 odes.h */. u8 p
4e560 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5;
4e570 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 74 /* Fifth paramet
4e580 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 er is an unsigne
4e590 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 d character */.
4e5a0 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 int p1;
4e5b0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 /* First op
4e5c0 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 erand */. int p
4e5d0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2; /
4e5e0 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 * Second paramet
4e5f0 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 er (often the ju
4e600 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 mp destination)
4e610 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 20 */. int p3;
4e620 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
4e630 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 third parameter
4e640 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 */. union {
4e650 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 72 /* four
4e660 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a th parameter */.
4e670 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 int i;
4e680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
4e690 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70 teger value if p
4e6a0 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 4type==P4_INT32
4e6b0 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20 */. void *p;
4e6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4e6d0 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 Generic pointer
4e6e0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b */. char *z;
4e6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4e700 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 * Pointer to dat
4e710 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68 a for string (ch
4e720 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20 ar array) types
4e730 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34 */. i64 *pI64
4e740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
4e750 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 Used when p4typ
4e760 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f e is P4_INT64 */
4e770 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65 . double *pRe
4e780 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 al; /* U
4e790 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 sed when p4type
4e7a0 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20 is P4_REAL */.
4e7b0 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 FuncDef *pFunc
4e7c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 ; /* Used
4e7d0 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e7e0 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20 P4_FUNCDEF */.
4e7f0 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 VdbeFunc *pVdb
4e800 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 eFunc; /* Used
4e810 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 when p4type is
4e820 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20 P4_VDBEFUNC */.
4e830 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c CollSeq *pCol
4e840 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 l; /* Use
4e850 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4e860 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20 P4_COLLSEQ */.
4e870 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 Mem *pMem;
4e880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 /* Use
4e890 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 d when p4type is
4e8a0 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 56 P4_MEM */. V
4e8b0 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20 Table *pVtab;
4e8c0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 /* Used wh
4e8d0 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f en p4type is P4_
4e8e0 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49 VTAB */. KeyI
4e8f0 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 nfo *pKeyInfo;
4e900 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
4e910 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59 p4type is P4_KEY
4e920 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 INFO */. int
4e930 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 *ai;
4e940 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 /* Used when
4e950 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 p4type is P4_INT
4e960 41 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 62 ARRAY */. Sub
4e970 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 Program *pProgra
4e980 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e m; /* Used when
4e990 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 55 p4type is P4_SU
4e9a0 42 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d 20 BPROGRAM */. }
4e9b0 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 p4;.#ifdef SQLIT
4e9c0 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a E_DEBUG. char *
4e9d0 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 zComment;
4e9e0 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f /* Comment to
4e9f0 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 improve readabi
4ea00 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 lity */.#endif.#
4ea10 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 ifdef VDBE_PROFI
4ea20 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 LE. int cnt;
4ea30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4ea40 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 Number of times
4ea50 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f this instructio
4ea60 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 20 2a n was executed *
4ea70 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 /. u64 cycles;
4ea80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
4ea90 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 Total time spent
4eaa0 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 executing this
4eab0 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 instruction */.#
4eac0 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 endif.};.typedef
4ead0 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 struct VdbeOp V
4eae0 64 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 dbeOp;.../*.** A
4eaf0 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 sub-routine use
4eb00 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 d to implement a
4eb10 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d trigger program
4eb20 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 50 ..*/.struct SubP
4eb30 72 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f rogram {. VdbeO
4eb40 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 p *aOp;
4eb50 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 /* Arra
4eb60 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 y of opcodes for
4eb70 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a sub-program */.
4eb80 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 int nOp;
4eb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4eba0 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 /* Elements in a
4ebb0 4f 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d Op[] */. int nM
4ebc0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 em;
4ebd0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
4ebe0 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c r of memory cell
4ebf0 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 s required */.
4ec00 69 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 20 int nCsr;
4ec10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
4ec20 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f Number of curso
4ec30 72 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 rs required */.
4ec40 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
4ec50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
4ec60 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e * Number of poin
4ec70 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 ters to this str
4ec80 75 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 64 ucture */. void
4ec90 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 *token;
4eca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20 /* id
4ecb0 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 that may be used
4ecc0 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72 to recursive tr
4ecd0 69 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a iggers */.};../*
4ece0 0a 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 .** A smaller ve
4ecf0 72 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 rsion of VdbeOp
4ed00 75 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 used for the Vdb
4ed10 65 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e eAddOpList() fun
4ed20 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a ction because.**
4ed30 20 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 it takes up les
4ed40 73 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 s space..*/.stru
4ed50 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a ct VdbeOpList {.
4ed60 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 u8 opcode;
4ed70 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 /* What op
4ed80 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f eration to perfo
4ed90 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 rm */. signed c
4eda0 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 har p1; /* F
4edb0 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a irst operand */.
4edc0 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 signed char p2
4edd0 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 ; /* Second
4ede0 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e parameter (often
4edf0 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e the jump destin
4ee00 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e ation) */. sign
4ee10 65 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 ed char p3;
4ee20 2f 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 /* Third paramet
4ee30 65 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 er */.};.typedef
4ee40 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 struct VdbeOpLi
4ee50 73 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a st VdbeOpList;..
4ee60 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
4ee70 6c 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 lues of VdbeOp.p
4ee80 34 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 4type.*/.#define
4ee90 20 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 P4_NOTUSED 0
4eea0 20 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 /* The P4 par
4eeb0 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 ameter is not us
4eec0 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 ed */.#define P4
4eed0 5f 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 _DYNAMIC (-1)
4eee0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 /* Pointer to a
4eef0 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 string obtained
4ef00 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
4ef10 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 c() */.#define P
4ef20 34 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 4_STATIC (-2)
4ef30 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 /* Pointer to a
4ef40 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a static string *
4ef50 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c /.#define P4_COL
4ef60 4c 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 LSEQ (-4) /* P
4ef70 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 4 is a pointer t
4ef80 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 o a CollSeq stru
4ef90 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 cture */.#define
4efa0 20 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 P4_FUNCDEF (-5
4efb0 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f ) /* P4 is a po
4efc0 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 inter to a FuncD
4efd0 65 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a ef structure */.
4efe0 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e #define P4_KEYIN
4eff0 46 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 FO (-6) /* P4
4f000 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
4f010 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 a KeyInfo struct
4f020 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ure */.#define P
4f030 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 4_VDBEFUNC (-7)
4f040 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e /* P4 is a poin
4f050 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e ter to a VdbeFun
4f060 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 c structure */.#
4f070 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 define P4_MEM
4f080 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 (-8) /* P4 i
4f090 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
4f0a0 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 Mem* structu
4f0b0 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 re */.#define P4
4f0c0 5f 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 _TRANSIENT (-9)
4f0d0 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 /* P4 is a point
4f0e0 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e er to a transien
4f0f0 74 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 t string */.#def
4f100 69 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 ine P4_VTAB
4f110 28 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 (-10) /* P4 is a
4f120 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 pointer to an s
4f130 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 qlite3_vtab stru
4f140 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 cture */.#define
4f150 20 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 P4_MPRINTF (-1
4f160 31 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 1) /* P4 is a st
4f170 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 ring obtained fr
4f180 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e om sqlite3_mprin
4f190 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 tf() */.#define
4f1a0 50 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 P4_REAL (-12
4f1b0 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d ) /* P4 is a 64-
4f1c0 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 bit floating poi
4f1d0 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 nt value */.#def
4f1e0 69 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 ine P4_INT64
4f1f0 28 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 (-13) /* P4 is a
4f200 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
4f210 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e nteger */.#defin
4f220 65 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d e P4_INT32 (-
4f230 31 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 14) /* P4 is a 3
4f240 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 2-bit signed int
4f250 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 eger */.#define
4f260 50 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 P4_INTARRAY (-15
4f270 29 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 ) /* P4 is a vec
4f280 74 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e tor of 32-bit in
4f290 74 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e tegers */.#defin
4f2a0 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 e P4_SUBPROGRAM
4f2b0 20 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 (-18) /* P4 is
4f2c0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 a pointer to a S
4f2d0 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 ubProgram struct
4f2e0 75 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 ure */../* When
4f2f0 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 adding a P4 argu
4f300 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 ment using P4_KE
4f310 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 YINFO, a copy of
4f320 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 the KeyInfo str
4f330 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 ucture.** is mad
4f340 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 e. That copy is
4f350 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 freed when the
4f360 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 Vdbe is finalize
4f370 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a d. But if the.*
4f380 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 * argument is P4
4f390 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
4f3a0 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 , the passed in
4f3b0 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e pointer is used.
4f3c0 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 It still.** ge
4f3d0 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 ts freed when th
4f3e0 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 e Vdbe is finali
4f3f0 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 zed so it still
4f400 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e should be obtain
4f410 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e ed.** from a sin
4f420 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 gle sqliteMalloc
4f430 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 (). But no copy
4f440 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 is made and the
4f450 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 calling.** func
4f460 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 tion should *not
4f470 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 * try to free th
4f480 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 e KeyInfo..*/.#d
4f490 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f efine P4_KEYINFO
4f4a0 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 _HANDOFF (-16).#
4f4b0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 define P4_KEYINF
4f4c0 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a O_STATIC (-17).
4f4d0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e ./*.** The Vdbe.
4f4e0 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 aColName array c
4f4f0 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 ontains 5n Mem s
4f500 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 tructures, where
4f510 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 n is the .** nu
4f520 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
4f530 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 of data returned
4f540 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
4f550 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f t..*/.#define CO
4f560 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 LNAME_NAME 0
4f570 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 .#define COLNAME
4f580 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 _DECLTYPE 1.#def
4f590 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 ine COLNAME_DATA
4f5a0 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 BASE 2.#define C
4f5b0 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 OLNAME_TABLE
4f5c0 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 3.#define COLNAM
4f5d0 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 E_COLUMN 4.#if
4f5e0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
4f5f0 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
4f600 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 A.# define COLNA
4f610 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 ME_N 5
4f620 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
4f630 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 COLNAME_xxx symb
4f640 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 ols */.#else.# i
4f650 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
4f660 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 _DECLTYPE.# de
4f670 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 fine COLNAME_N
4f680 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 1 /* St
4f690 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d ore only the nam
4f6a0 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 e */.# else.#
4f6b0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e define COLNAME_N
4f6c0 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 2 /*
4f6d0 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 Store the name a
4f6e0 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 nd decltype */.#
4f6f0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f endif.#endif../
4f700 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
4f710 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 ng macro convert
4f720 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 s a relative add
4f730 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 ress in the p2 f
4f740 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 ield.** of a Vdb
4f750 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e eOp structure in
4f760 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 to a negative nu
4f770 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a mber so that .**
4f780 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
4f790 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 pList() knows th
4f7a0 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 at the address i
4f7b0 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c s relative. Cal
4f7c0 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 ling.** the macr
4f7d0 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 o again restores
4f7e0 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f the address..*/
4f7f0 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 .#define ADDR(X)
4f800 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a (-1-(X))../*.*
4f810 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 * The makefile s
4f820 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 cans the vdbe.c
4f830 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 source file and
4f840 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 creates the "opc
4f850 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 odes.h".** heade
4f860 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 r file that defi
4f870 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 nes a number for
4f880 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 each opcode use
4f890 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a d by the VDBE..*
4f8a0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4f8b0 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 * Include opcode
4f8c0 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c s.h in the middl
4f8d0 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a e of vdbe.h ****
4f8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f8f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
4f900 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 * Begin file opc
4f910 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a odes.h *********
4f920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
4f940 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c /./* Automatical
4f950 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 ly generated. D
4f960 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a o not edit */./*
4f970 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 See the mkopcod
4f980 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f eh.awk script fo
4f990 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 r details */.#de
4f9a0 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 fine OP_Goto
4f9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 1.#
4f9d0 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62 20 define OP_Gosub
4f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4f9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2
4fa00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 74 75 .#define OP_Retu
4fa10 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 rn
4fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa30 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59 69 3.#define OP_Yi
4fa40 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 20 20 eld
4fa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa60 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4.#define OP_
4fa70 48 61 6c 74 49 66 4e 75 6c 6c 20 20 20 20 20 20 HaltIfNull
4fa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fa90 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 5.#define O
4faa0 50 5f 48 61 6c 74 20 20 20 20 20 20 20 20 20 20 P_Halt
4fab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fac0 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 6.#define
4fad0 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 20 OP_Integer
4fae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4faf0 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 7.#defi
4fb00 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 20 20 20 ne OP_Int64
4fb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 8.#de
4fb30 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 20 20 fine OP_Real
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fb50 20 20 20 20 20 20 20 20 20 20 20 31 33 30 20 20 130
4fb60 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 /* same as TK_F
4fb70 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65 66 69 LOAT */.#defi
4fb80 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 ne OP_String8
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fba0 20 20 20 20 20 20 20 20 20 20 39 34 20 20 20 2f 94 /
4fbb0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 * same as TK_STR
4fbc0 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 ING */.#define
4fbd0 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20 20 OP_String
4fbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fbf0 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 9.#defi
4fc00 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20 ne OP_Null
4fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64 65 10.#de
4fc30 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20 20 fine OP_Blob
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 11.#
4fc60 64 65 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 define OP_Variab
4fc70 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 le
4fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
4fc90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 .#define OP_Move
4fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcc0 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 13.#define OP_Co
4fcd0 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 py
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fcf0 20 20 31 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 14.#define OP_
4fd00 53 43 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 SCopy
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20 4f 15.#define O
4fd30 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20 P_ResultRow
4fd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd50 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 16.#define
4fd60 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 OP_Concat
4fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fd80 20 20 20 20 20 20 20 20 39 31 20 20 20 2f 2a 20 91 /*
4fd90 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 same as TK_CONCA
4fda0 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f T */.#define O
4fdb0 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20 20 P_Add
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fdd0 20 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73 61 86 /* sa
4fde0 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20 20 me as TK_PLUS
4fdf0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f */.#define OP_
4fe00 53 75 62 74 72 61 63 74 20 20 20 20 20 20 20 20 Subtract
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d 65 87 /* same
4fe30 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 as TK_MINUS
4fe40 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 */.#define OP_Mu
4fe50 6c 74 69 70 6c 79 20 20 20 20 20 20 20 20 20 20 ltiply
4fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fe70 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 88 /* same a
4fe80 73 20 54 4b 5f 53 54 41 52 20 20 20 20 20 2a 2f s TK_STAR */
4fe90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76 69 .#define OP_Divi
4fea0 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 de
4feb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4fec0 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 89 /* same as
4fed0 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a 23 TK_SLASH */.#
4fee0 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e define OP_Remain
4fef0 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 der
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30 90
4ff10 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
4ff20 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 _REM */.#de
4ff30 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20 fine OP_CollSeq
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ff50 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 17.#
4ff60 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 74 69 define OP_Functi
4ff70 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 on
4ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38 18
4ff90 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 41 .#define OP_BitA
4ffa0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nd
4ffb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4ffc0 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 82 /* same as
4ffd0 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23 TK_BITAND */.#
4ffe0 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 define OP_BitOr
4fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 33 83
50010 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
50020 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 _BITOR */.#de
50030 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 fine OP_ShiftLef
50040 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t
50050 20 20 20 20 20 20 20 20 20 20 20 20 38 34 20 20 84
50060 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
50070 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 SHIFT */.#defi
50080 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 ne OP_ShiftRight
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500a0 20 20 20 20 20 20 20 20 20 20 38 35 20 20 20 2f 85 /
500b0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 * same as TK_RSH
500c0 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 IFT */.#define
500d0 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 OP_AddImm
500e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
500f0 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 69 20.#defi
50100 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 ne OP_MustBeInt
50110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50120 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64 65 21.#de
50130 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 fine OP_RealAffi
50140 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20 20 nity
50150 20 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 22.#
50160 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74 define OP_ToText
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50180 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 31 141
50190 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
501a0 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65 _TO_TEXT */.#de
501b0 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20 20 fine OP_ToBlob
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
501d0 20 20 20 20 20 20 20 20 20 20 20 31 34 32 20 20 142
501e0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
501f0 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66 69 O_BLOB */.#defi
50200 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 ne OP_ToNumeric
50210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50220 20 20 20 20 20 20 20 20 20 31 34 33 20 20 20 2f 143 /
50230 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f * same as TK_TO_
50240 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 6e NUMERIC*/.#defin
50250 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 e OP_ToInt
50260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50270 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a 144 /*
50280 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 same as TK_TO_I
50290 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 NT */.#define
502a0 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20 20 OP_ToReal
502b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
502c0 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20 73 145 /* s
502d0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45 41 ame as TK_TO_REA
502e0 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 L */.#define OP
502f0 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20 20 _Eq
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50310 20 20 20 20 20 37 36 20 20 20 2f 2a 20 73 61 6d 76 /* sam
50320 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20 20 e as TK_EQ
50330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e */.#define OP_N
50340 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50360 20 20 20 37 35 20 20 20 2f 2a 20 73 61 6d 65 20 75 /* same
50370 61 73 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 2a as TK_NE *
50380 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 /.#define OP_Lt
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503b0 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 79 /* same as
503c0 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a TK_LT */.
503d0 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 #define OP_Le
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
503f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 7
50400 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 8 /* same as T
50410 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 K_LE */.#d
50420 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 efine OP_Gt
50430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50440 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 20 77
50450 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
50460 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 GT */.#def
50470 69 6e 65 20 4f 50 5f 47 65 20 20 20 20 20 20 20 ine OP_Ge
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50490 20 20 20 20 20 20 20 20 20 20 20 38 30 20 20 20 80
504a0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 /* same as TK_GE
504b0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e */.#defin
504c0 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e e OP_Permutation
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
504e0 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 23.#def
504f0 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20 ine OP_Compare
50500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50510 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 24.#d
50520 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 20 efine OP_Jump
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 25.
50550 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20 20 #define OP_And
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
50580 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 9 /* same as T
50590 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23 64 K_AND */.#d
505a0 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20 efine OP_Or
505b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
505c0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38 20 68
505d0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
505e0 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 OR */.#def
505f0 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20 20 ine OP_Not
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50610 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20 20 19
50620 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f /* same as TK_NO
50630 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e T */.#defin
50640 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 20 e OP_BitNot
50650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50660 20 20 20 20 20 20 20 20 20 39 33 20 20 20 2f 2a 93 /*
50670 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e same as TK_BITN
50680 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 OT */.#define
50690 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20 OP_If
506a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506b0 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e 26.#defin
506c0 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20 20 e OP_IfNot
506d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
506e0 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65 66 27.#def
506f0 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 ine OP_IsNull
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50710 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 73
50720 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 /* same as TK_IS
50730 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e NULL */.#defin
50740 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20 e OP_NotNull
50750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50760 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a 74 /*
50770 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e same as TK_NOTN
50780 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ULL */.#define
50790 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 OP_Column
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507b0 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69 6e 28.#defin
507c0 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 e OP_Affinity
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
507e0 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 29.#def
507f0 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 ine OP_MakeRecor
50800 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
50810 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 30.#d
50820 65 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 efine OP_Count
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50840 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 31.
50850 23 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 70 #define OP_Savep
50860 6f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 oint
50870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 3
50880 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 75 74 2.#define OP_Aut
50890 6f 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 oCommit
508a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508b0 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 33.#define OP_T
508c0 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20 ransaction
508d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
508e0 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 34.#define OP
508f0 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20 _ReadCookie
50900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50910 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 35.#define
50920 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20 20 OP_SetCookie
50930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50940 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 36.#defin
50950 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 e OP_VerifyCooki
50960 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50970 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65 66 37.#def
50980 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 ine OP_OpenRead
50990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
509a0 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 38.#d
509b0 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 efine OP_OpenWri
509c0 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 te
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a 39.
509e0 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 45 #define OP_OpenE
509f0 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20 20 phemeral
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
50a10 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 0.#define OP_Ope
50a20 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 nPseudo
50a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a40 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 41.#define OP_C
50a50 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20 lose
50a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50a70 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 42.#define OP
50a80 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 20 20 20 _SeekLt
50a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50aa0 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65 20 43.#define
50ab0 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20 20 OP_SeekLe
50ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ad0 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69 6e 44.#defin
50ae0 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 20 20 20 e OP_SeekGe
50af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b00 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65 66 45.#def
50b10 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 ine OP_SeekGt
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b30 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 46.#d
50b40 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 20 20 efine OP_Seek
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a 47.
50b70 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f #define OP_NotFo
50b80 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 und
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 4
50ba0 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 75 8.#define OP_Fou
50bb0 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nd
50bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50bd0 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 49.#define OP_I
50be0 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20 sUnique
50bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c00 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 50.#define OP
50c10 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20 20 _NotExists
50c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c30 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 51.#define
50c40 4f 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20 20 OP_Sequence
50c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c60 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 52.#defin
50c70 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20 e OP_NewRowid
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50c90 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65 66 53.#def
50ca0 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 20 ine OP_Insert
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50cc0 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 54.#d
50cd0 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 49 efine OP_InsertI
50ce0 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 nt
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a 55.
50d00 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65 74 #define OP_Delet
50d10 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 5
50d30 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 6.#define OP_Res
50d40 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20 20 20 etCount
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d60 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 57.#define OP_R
50d70 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 owKey
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50d90 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 50 58.#define OP
50da0 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20 _RowData
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50dc0 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20 59.#define
50dd0 4f 50 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 OP_Rowid
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50df0 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69 6e 60.#defin
50e00 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 e OP_NullRow
50e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65 66 61.#def
50e30 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 ine OP_Last
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e50 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23 64 62.#d
50e60 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 efine OP_Sort
50e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33 0a 63.
50e90 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e #define OP_Rewin
50ea0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
50eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 6
50ec0 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 4.#define OP_Pre
50ed0 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50ef0 20 36 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65.#define OP_N
50f00 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 ext
50f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f20 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 4f 50 66.#define OP
50f30 5f 49 64 78 49 6e 73 65 72 74 20 20 20 20 20 20 _IdxInsert
50f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f50 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 67.#define
50f60 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20 OP_IdxDelete
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50f80 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69 6e 70.#defin
50f90 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20 20 e OP_IdxRowid
50fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fb0 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 71.#def
50fc0 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 20 20 20 ine OP_IdxLT
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
50fe0 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 72.#d
50ff0 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20 20 efine OP_IdxGE
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51010 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31 0a 81.
51020 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74 72 #define OP_Destr
51030 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 oy
51040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 9
51050 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 65 2.#define OP_Cle
51060 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ar
51070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51080 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 95.#define OP_C
51090 72 65 61 74 65 49 6e 64 65 78 20 20 20 20 20 20 reateIndex
510a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510b0 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f 50 96.#define OP
510c0 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 _CreateTable
510d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
510e0 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65 20 97.#define
510f0 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 20 OP_ParseSchema
51100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51110 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e 98.#defin
51120 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 e OP_LoadAnalysi
51130 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s
51140 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66 99.#def
51150 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 ine OP_DropTable
51160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51170 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 100.#d
51180 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e 64 efine OP_DropInd
51190 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ex
511a0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 101.
511b0 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 #define OP_DropT
511c0 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 rigger
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 10
511e0 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 2.#define OP_Int
511f0 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20 20 egrityCk
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51210 31 30 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 103.#define OP_R
51220 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 20 20 owSetAdd
51230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51240 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 4f 50 104.#define OP
51250 5f 52 6f 77 53 65 74 52 65 61 64 20 20 20 20 20 _RowSetRead
51260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51270 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65 20 105.#define
51280 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20 20 OP_RowSetTest
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512a0 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 106.#defin
512b0 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 e OP_Program
512c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
512d0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66 107.#def
512e0 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20 20 ine OP_Param
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51300 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 108.#d
51310 65 66 69 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 efine OP_FkCount
51320 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 er
51330 20 20 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 109.
51340 23 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 5a #define OP_FkIfZ
51350 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 ero
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
51370 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 0.#define OP_Mem
51380 4d 61 78 20 20 20 20 20 20 20 20 20 20 20 20 20 Max
51390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513a0 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 111.#define OP_I
513b0 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20 20 fPos
513c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
513d0 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 112.#define OP
513e0 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 20 20 _IfNeg
513f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51400 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 113.#define
51410 4f 50 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 20 OP_IfZero
51420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51430 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 114.#defin
51440 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20 20 e OP_AggStep
51450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51460 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 115.#def
51470 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 ine OP_AggFinal
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51490 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 116.#d
514a0 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20 efine OP_Vacuum
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
514c0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 117.
514d0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 #define OP_IncrV
514e0 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20 20 acuum
514f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 11
51500 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 8.#define OP_Exp
51510 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ire
51520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51530 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 119.#define OP_T
51540 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20 20 ableLock
51550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51560 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 120.#define OP
51570 5f 56 42 65 67 69 6e 20 20 20 20 20 20 20 20 20 _VBegin
51580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51590 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 121.#define
515a0 4f 50 5f 56 43 72 65 61 74 65 20 20 20 20 20 20 OP_VCreate
515b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515c0 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 122.#defin
515d0 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20 20 e OP_VDestroy
515e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
515f0 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66 123.#def
51600 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20 ine OP_VOpen
51610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51620 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23 64 124.#d
51630 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 efine OP_VFilter
51640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51650 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35 0a 125.
51660 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 #define OP_VColu
51670 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 mn
51680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 12
51690 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4e 65 6.#define OP_VNe
516a0 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xt
516b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516c0 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 127.#define OP_V
516d0 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 Rename
516e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
516f0 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 128.#define OP
51700 5f 56 55 70 64 61 74 65 20 20 20 20 20 20 20 20 _VUpdate
51710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51720 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 129.#define
51730 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20 OP_Pagecount
51740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51750 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 131.#defin
51760 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 e OP_Trace
51770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51780 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 132.#def
51790 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20 20 ine OP_Noop
517a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517b0 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23 64 133.#d
517c0 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e efine OP_Explain
517d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
517e0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a 134.
517f0 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ./* The followin
51800 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73 20 g opcode values
51810 61 72 65 20 6e 65 76 65 72 20 75 73 65 64 20 2a are never used *
51820 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 /.#define OP_Not
51830 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 Used_135
51840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51850 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 135.#define OP_N
51860 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 otUsed_136
51870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51880 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 136.#define OP
51890 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 _NotUsed_137
518a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518b0 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 137.#define
518c0 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 OP_NotUsed_138
518d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
518e0 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 138.#defin
518f0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 e OP_NotUsed_139
51900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51910 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 139.#def
51920 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 ine OP_NotUsed_1
51930 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 40
51940 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 140...
51950 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 /* Properties su
51960 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 ch as "out2" or
51970 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 "jump" that are
51980 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 specified in.**
51990 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 comments followi
519a0 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f ng the "case" fo
519b0 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e r each opcode in
519c0 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 the vdbe.c.** a
519d0 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 re encoded into
519e0 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f bitvectors as fo
519f0 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e llows:.*/.#defin
51a00 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 e OPFLG_JUMP
51a10 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 0x0001
51a20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c /* jump: P2 hol
51a30 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f ds jmp target */
51a40 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f .#define OPFLG_O
51a50 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 UT2_PRERELEASE 0
51a60 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 x0002 /* out2-p
51a70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 rerelease: */.#d
51a80 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 efine OPFLG_IN1
51a90 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 0x00
51aa0 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 04 /* in1: P1
51ab0 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a is an input */.
51ac0 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e #define OPFLG_IN
51ad0 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 2 0x
51ae0 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 0008 /* in2:
51af0 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a P2 is an input *
51b00 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f /.#define OPFLG_
51b10 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 IN3
51b20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 0x0010 /* in3:
51b30 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 P3 is an input
51b40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c */.#define OPFL
51b50 47 5f 4f 55 54 32 20 20 20 20 20 20 20 20 20 20 G_OUT2
51b60 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 0x0020 /* out
51b70 32 3a 20 20 50 32 20 69 73 20 61 6e 20 6f 75 74 2: P2 is an out
51b80 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f put */.#define O
51b90 50 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20 20 PFLG_OUT3
51ba0 20 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a 20 0x0040 /*
51bb0 6f 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e 20 out3: P3 is an
51bc0 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e output */.#defin
51bd0 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 e OPFLG_INITIALI
51be0 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f ZER {\./* 0 */
51bf0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
51c00 35 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 5, 0x04, 0x04, 0
51c10 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 32 2c x10, 0x00, 0x02,
51c20 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30 32 \./* 8 */ 0x02
51c30 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
51c40 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 02, 0x00, 0x00,
51c50 30 78 32 34 2c 20 30 78 32 34 2c 5c 0a 2f 2a 20 0x24, 0x24,\./*
51c60 20 31 36 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 16 */ 0x00, 0x0
51c70 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c 20 30 0, 0x00, 0x24, 0
51c80 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 34 2c x04, 0x05, 0x04,
51c90 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20 2a 0x00,\./* 24 *
51ca0 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 / 0x00, 0x01, 0x
51cb0 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 20 05, 0x05, 0x00,
51cc0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32 0x00, 0x00, 0x02
51cd0 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78 30 ,\./* 32 */ 0x0
51ce0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
51cf0 78 30 32 2c 20 30 78 31 30 2c 20 30 78 30 30 2c x02, 0x10, 0x00,
51d00 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a 0x00, 0x00,\./*
51d10 20 20 34 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 40 */ 0x00, 0x
51d20 30 30 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 00, 0x00, 0x11,
51d30 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31 31 0x11, 0x11, 0x11
51d40 2c 20 30 78 30 38 2c 5c 0a 2f 2a 20 20 34 38 20 , 0x08,\./* 48
51d50 2a 2f 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 */ 0x11, 0x11, 0
51d60 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30 32 2c x11, 0x11, 0x02,
51d70 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 0x02, 0x00, 0x0
51d80 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30 78 0,\./* 56 */ 0x
51d90 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
51da0 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30 0x00, 0x02, 0x00
51db0 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 5c 0a 2f , 0x01, 0x01,\./
51dc0 2a 20 20 36 34 20 2a 2f 20 30 78 30 31 2c 20 30 * 64 */ 0x01, 0
51dd0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 38 2c x01, 0x01, 0x08,
51de0 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 30 0x4c, 0x4c, 0x0
51df0 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 37 32 0, 0x02,\./* 72
51e00 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 35 2c 20 */ 0x01, 0x05,
51e10 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31 35 0x05, 0x15, 0x15
51e20 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78 , 0x15, 0x15, 0x
51e30 31 35 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 15,\./* 80 */ 0
51e40 78 31 35 2c 20 30 78 30 31 2c 20 30 78 34 63 2c x15, 0x01, 0x4c,
51e50 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 0x4c, 0x4c, 0x4
51e60 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 5c 0a c, 0x4c, 0x4c,\.
51e70 2f 2a 20 20 38 38 20 2a 2f 20 30 78 34 63 2c 20 /* 88 */ 0x4c,
51e80 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 0x4c, 0x4c, 0x4c
51e90 2c 20 30 78 30 32 2c 20 30 78 32 34 2c 20 30 78 , 0x02, 0x24, 0x
51ea0 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 02, 0x00,\./* 9
51eb0 36 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 32 2c 6 */ 0x02, 0x02,
51ec0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51ed0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
51ee0 78 30 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 x00,\./* 104 */
51ef0 30 78 30 63 2c 20 30 78 34 35 2c 20 30 78 31 35 0x0c, 0x45, 0x15
51f00 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 , 0x01, 0x02, 0x
51f10 30 30 2c 20 30 78 30 31 2c 20 30 78 30 38 2c 5c 00, 0x01, 0x08,\
51f20 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 35 2c ./* 112 */ 0x05,
51f30 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 0x05, 0x05, 0x0
51f40 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
51f50 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 x01, 0x00,\./* 1
51f60 32 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 20 */ 0x00, 0x00
51f70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51f80 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 00, 0x01, 0x00,
51f90 30 78 30 31 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 0x01,\./* 128 */
51fa0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
51fb0 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 2, 0x02, 0x00, 0
51fc0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
51fd0 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30 30 \./* 136 */ 0x00
51fe0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 , 0x00, 0x00, 0x
51ff0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c 20 00, 0x00, 0x04,
52000 30 78 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 0x04, 0x04,\./*
52010 31 34 34 20 2a 2f 20 30 78 30 34 2c 20 30 78 30 144 */ 0x04, 0x0
52020 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 4,}../**********
52030 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f **** End of opco
52040 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a des.h **********
52050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52070 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
52080 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 **** Continuing
52090 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 where we left of
520a0 66 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a f in vdbe.h ****
520b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
520c0 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 ***/../*.** Prot
520d0 6f 74 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 otypes for the V
520e0 44 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 DBE interface.
520f0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 See comments on
52100 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
52110 6f 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 on.** for a desc
52120 72 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 ription of what
52130 65 61 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f each of these ro
52140 75 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a utines does..*/.
52150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 SQLITE_PRIVATE V
52160 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 dbe *sqlite3Vdbe
52170 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 Create(sqlite3*)
52180 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52190 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
521a0 41 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 AddOp0(Vdbe*,int
521b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
521c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
521d0 65 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e eAddOp1(Vdbe*,in
521e0 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 t,int);.SQLITE_P
521f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
52200 65 33 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 e3VdbeAddOp2(Vdb
52210 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b e*,int,int,int);
52220 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52230 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
52240 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c ddOp3(Vdbe*,int,
52250 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 int,int,int);.SQ
52260 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
52270 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
52280 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 p4(Vdbe*,int,int
52290 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 ,int,int,const c
522a0 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 har *zP4,int);.S
522b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
522c0 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 t sqlite3VdbeAdd
522d0 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69 6e 74 Op4Int(Vdbe*,int
522e0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
522f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52300 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52310 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65 2a eAddOpList(Vdbe*
52320 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f , int nOp, VdbeO
52330 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 pList const *aOp
52340 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
52360 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65 2a beChangeP1(Vdbe*
52370 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 , int addr, int
52380 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 P1);.SQLITE_PRIV
52390 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
523a0 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64 62 VdbeChangeP2(Vdb
523b0 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e e*, int addr, in
523c0 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 t P2);.SQLITE_PR
523d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
523e0 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 e3VdbeChangeP3(V
523f0 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 dbe*, int addr,
52400 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 45 5f int P3);.SQLITE_
52410 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
52420 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 ite3VdbeChangeP5
52430 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 3b 0a (Vdbe*, u8 P5);.
52440 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
52450 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a oid sqlite3VdbeJ
52460 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20 69 umpHere(Vdbe*, i
52470 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54 45 nt addr);.SQLITE
52480 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
52490 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 lite3VdbeChangeT
524a0 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e 74 oNoop(Vdbe*, int
524b0 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a 53 addr, int N);.S
524c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
524d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
524e0 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 69 6e angeP4(Vdbe*, in
524f0 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68 t addr, const ch
52500 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e 29 3b ar *zP4, int N);
52510 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52520 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
52530 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a 2c UsesBtree(Vdbe*,
52540 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
52550 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 IVATE VdbeOp *sq
52560 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 lite3VdbeGetOp(V
52570 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 dbe*, int);.SQLI
52580 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
52590 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
525a0 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 bel(Vdbe*);.SQLI
525b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
525c0 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
525d0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 e(Vdbe*);.SQLITE
525e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
525f0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 lite3VdbeMakeRea
52600 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 dy(Vdbe*,int,int
52610 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 ,int,int,int,int
52620 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
52630 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52640 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a 29 eFinalize(Vdbe*)
52650 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
52660 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
52670 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56 64 eResolveLabel(Vd
52680 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 be*, int);.SQLIT
52690 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
526a0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 lite3VdbeCurrent
526b0 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69 66 Addr(Vdbe*);.#if
526c0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
526d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
526e0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 int sqlite3Vdb
526f0 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 eAssertMayAbort(
52700 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 Vdbe *, int);.SQ
52710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
52720 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54 oid sqlite3VdbeT
52730 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45 2a race(Vdbe*,FILE*
52740 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 );.#endif.SQLITE
52750 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
52760 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 lite3VdbeResetSt
52770 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29 3b epResult(Vdbe*);
52780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
52790 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 int sqlite3VdbeR
527a0 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51 4c eset(Vdbe*);.SQL
527b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
527c0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e sqlite3VdbeSetN
527d0 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e 74 umCols(Vdbe*,int
527e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
527f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
52800 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62 65 eSetColName(Vdbe
52810 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f 6e *, int, int, con
52820 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64 28 st char *, void(
52830 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 *)(void*));.SQLI
52840 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
52850 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 sqlite3VdbeCount
52860 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b 0a Changes(Vdbe*);.
52870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
52880 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 qlite3 *sqlite3V
52890 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53 51 dbeDb(Vdbe*);.SQ
528a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
528b0 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 d sqlite3VdbeSet
528c0 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 Sql(Vdbe*, const
528d0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c char *z, int n,
528e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
528f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
52900 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a e3VdbeSwap(Vdbe*
52910 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f ,Vdbe*);.SQLITE_
52920 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a PRIVATE VdbeOp *
52930 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f sqlite3VdbeTakeO
52940 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 69 6e pArray(Vdbe*, in
52950 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 t*, int*);.SQLIT
52960 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
52970 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72 61 qlite3VdbeProgra
52980 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 mDelete(sqlite3
52990 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 2c *, SubProgram *,
529a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
529b0 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 IVATE sqlite3_va
529c0 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 lue *sqlite3Vdbe
529d0 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c 20 GetValue(Vdbe*,
529e0 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 int, u8);.SQLITE
529f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
52a00 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72 6d lite3VdbeSetVarm
52a10 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b ask(Vdbe*, int);
52a20 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
52a30 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
52a40 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f *sqlite3VdbeReco
52a50 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f rdUnpack(KeyInfo
52a60 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 *,int,const void
52a70 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 *,char*,int);.SQ
52a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
52a90 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c d sqlite3VdbeDel
52aa0 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 eteUnpackedRecor
52ab0 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 d(UnpackedRecord
52ac0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
52ad0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
52ae0 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
52af0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c int,const void*,
52b00 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 UnpackedRecord*)
52b10 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 ;...#ifndef NDEB
52b20 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
52b30 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
52b40 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 VdbeComment(Vdbe
52b50 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 *, const char*,
52b60 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 ...);.# define V
52b70 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 dbeComment(X) s
52b80 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e qlite3VdbeCommen
52b90 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t X.SQLITE_PRIVA
52ba0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 TE void sqlite
52bb0 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 3VdbeNoopComment
52bc0 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 (Vdbe*, const ch
52bd0 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 ar*, ...);.# def
52be0 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d ine VdbeNoopComm
52bf0 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 ent(X) sqlite3V
52c00 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 dbeNoopComment X
52c10 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
52c20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 VdbeComment(X).#
52c30 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 define VdbeNoop
52c40 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 Comment(X).#endi
52c50 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a f..#endif../****
52c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
52c70 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a f vdbe.h *******
52c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
52cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
52cc0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
52cd0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 eft off in sqlit
52ce0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eInt.h *********
52cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
52d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
52d10 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68 de pager.h in th
52d20 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 e middle of sqli
52d30 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a teInt.h ********
52d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
52d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
52d60 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a file pager.h **
52d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
52da0 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 2001 September
52db0 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 15.**.** The aut
52dc0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
52dd0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
52de0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
52df0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
52e00 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
52e10 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
52e20 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
52e30 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
52e40 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
52e50 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
52e60 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
52e70 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
52e80 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
52e90 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
52ea0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
52eb0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
52ec0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
52ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
52f10 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
52f20 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 file defines th
52f30 65 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 e interface that
52f40 20 74 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 the sqlite page
52f50 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 cache.** subsys
52f60 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63 tem. The page c
52f70 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 72 ache subsystem r
52f80 65 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 eads and writes
52f90 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a a file a page.**
52fa0 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70 at a time and p
52fb0 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 rovides a journa
52fc0 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a l for rollback..
52fd0 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47 */..#ifndef _PAG
52fe0 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50 ER_H_.#define _P
52ff0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 AGER_H_../*.** D
53000 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73 efault maximum s
53010 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65 ize for persiste
53020 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 nt journal files
53030 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a . A negative .**
53040 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20 value means no
53050 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75 limit. This valu
53060 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64 e may be overrid
53070 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a den using the .*
53080 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f * sqlite3PagerJo
53090 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29 urnalSizeLimit()
530a0 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22 API. See also "
530b0 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 PRAGMA journal_s
530c0 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 ize_limit"..*/.#
530d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
530e0 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 FAULT_JOURNAL_SI
530f0 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69 ZE_LIMIT. #defi
53100 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c ne SQLITE_DEFAUL
53110 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c T_JOURNAL_SIZE_L
53120 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a IMIT -1.#endif..
53130 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75 /*.** The type u
53140 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74 sed to represent
53150 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 a page number.
53160 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 The first page
53170 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20 in a file.** is
53180 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20 called page 1.
53190 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70 0 is used to rep
531a0 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61 resent "not a pa
531b0 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ge"..*/.typedef
531c0 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a u32 Pgno;../*.**
531d0 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20 Each open file
531e0 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20 is managed by a
531f0 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63 separate instanc
53200 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22 e of the "Pager"
53210 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 structure..*/.t
53220 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61 ypedef struct Pa
53230 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a ger Pager;../*.*
53240 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f * Handle type fo
53250 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 r pages..*/.type
53260 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 def struct PgHdr
53270 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 DbPage;../*.**
53280 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45 Page number PAGE
53290 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76 R_MJ_PGNO is nev
532a0 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51 er used in an SQ
532b0 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69 Lite database (i
532c0 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64 t is.** reserved
532d0 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f for working aro
532e0 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f und a windows/po
532f0 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c six incompatibil
53300 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75 ity). It is.** u
53310 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e sed in the journ
53320 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68 al to signify th
53330 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 at the remainder
53340 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
53350 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f file .** is devo
53360 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61 ted to storing a
53370 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
53380 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65 name - there are
53390 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74 no more pages t
533a0 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 o.** roll back.
533b0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 See comments for
533c0 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d function writeM
533d0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69 asterJournal() i
533e0 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f n pager.c .** fo
533f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 r details..*/.#d
53400 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50 efine PAGER_MJ_P
53410 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28 GNO(x) ((Pgno)((
53420 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78 PENDING_BYTE/((x
53430 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29 )->pageSize))+1)
53440 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 )../*.** Allowed
53450 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 values for the
53460 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 flags parameter
53470 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f to sqlite3PagerO
53480 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 pen()..**.** NOT
53490 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 E: These values
534a0 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63 must match the c
534b0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52 orresponding BTR
534c0 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74 EE_ values in bt
534d0 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e ree.h..*/.#defin
534e0 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 e PAGER_OMIT_JOU
534f0 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20 RNAL 0x0001
53500 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 /* Do not use a
53510 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
53520 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 */.#define PAGE
53530 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 R_NO_READLOCK
53540 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69 0x0002 /* Omi
53550 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 t readlocks on r
53560 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f eadonly files */
53570 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 ../*.** Valid va
53580 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63 lues for the sec
53590 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 ond argument to
535a0 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b sqlite3PagerLock
535b0 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 ingMode()..*/.#d
535c0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
535d0 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 INGMODE_QUERY
535e0 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 -1.#define PA
535f0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
53600 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64 NORMAL 0.#d
53610 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b efine PAGER_LOCK
53620 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
53630 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c E 1../*.** Val
53640 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 id values for th
53650 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
53660 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 t to sqlite3Page
53670 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a rJournalMode()..
53680 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
53690 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
536a0 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 RY -1.#defi
536b0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
536c0 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20 MODE_DELETE
536d0 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 0 /* Commit b
536e0 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e y deleting journ
536f0 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 al file */.#defi
53700 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
53710 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20 MODE_PERSIST
53720 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 1 /* Commit b
53730 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 y zeroing journa
53740 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66 l header */.#def
53750 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 ine PAGER_JOURNA
53760 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20 LMODE_OFF
53770 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 2 /* Journal
53780 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64 omitted. */.#d
53790 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 efine PAGER_JOUR
537a0 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 NALMODE_TRUNCATE
537b0 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 3 /* Commi
537c0 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20 t by truncating
537d0 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 journal */.#defi
537e0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c ne PAGER_JOURNAL
537f0 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 MODE_MEMORY
53800 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 4 /* In-memor
53810 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a y journal file *
53820 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d /../*.** The rem
53830 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66 ainder of this f
53840 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 ile contains the
53850 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 declarations of
53860 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a the functions.*
53870 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 * that make up t
53880 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73 he Pager sub-sys
53890 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75 tem API. See sou
538a0 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 rce code comment
538b0 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61 s for .** a deta
538c0 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e iled description
538d0 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65 of each routine
538e0 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e ..*/../* Open an
538f0 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20 d close a Pager
53900 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a connection. */ .
53910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53920 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f nt sqlite3PagerO
53930 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 pen(. sqlite3_v
53940 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70 fs*,. Pager **p
53950 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20 pPager,. const
53960 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 char*,. int,.
53970 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f int,. int,. vo
53980 69 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29 id(*)(DbPage*).)
53990 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
539a0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
539b0 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 rClose(Pager *pP
539c0 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ager);.SQLITE_PR
539d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
539e0 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 3PagerReadFilehe
539f0 61 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 ader(Pager*, int
53a00 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a , unsigned char*
53a10 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 );../* Functions
53a20 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75 used to configu
53a30 72 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 re a Pager objec
53a40 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t. */.SQLITE_PRI
53a50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
53a60 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 3PagerSetBusyhan
53a70 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 dler(Pager*, int
53a80 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 (*)(void *), voi
53a90 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 d *);.SQLITE_PRI
53aa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
53ab0 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
53ac0 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69 (Pager*, u16*, i
53ad0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
53ae0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
53af0 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 agerMaxPageCount
53b00 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 (Pager*, int);.S
53b10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
53b20 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 id sqlite3PagerS
53b30 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 etCachesize(Page
53b40 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 r*, int);.SQLITE
53b50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
53b60 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 lite3PagerSetSaf
53b70 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c etyLevel(Pager*,
53b80 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 int,int);.SQLITE
53b90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53ba0 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 ite3PagerLocking
53bb0 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e Mode(Pager *, in
53bc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53bd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53be0 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
53bf0 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 ager *, int);.SQ
53c00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 LITE_PRIVATE i64
53c10 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 sqlite3PagerJou
53c20 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 rnalSizeLimit(Pa
53c30 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c ger *, i64);.SQL
53c40 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 ITE_PRIVATE sqli
53c50 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c te3_backup **sql
53c60 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50 ite3PagerBackupP
53c70 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 tr(Pager*);../*
53c80 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 Functions used t
53c90 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c o obtain and rel
53ca0 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65 ease page refere
53cb0 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 nces. */ .SQLITE
53cc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
53cd0 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
53ce0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
53cf0 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67 Pgno pgno, DbPag
53d00 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20 e **ppPage, int
53d10 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e clrFlag);.#defin
53d20 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 e sqlite3PagerGe
53d30 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33 t(A,B,C) sqlite3
53d40 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42 PagerAcquire(A,B
53d50 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49 ,C,0).SQLITE_PRI
53d60 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c VATE DbPage *sql
53d70 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 ite3PagerLookup(
53d80 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
53d90 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 gno pgno);.SQLIT
53da0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
53db0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44 qlite3PagerRef(D
53dc0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f bPage*);.SQLITE_
53dd0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
53de0 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44 ite3PagerUnref(D
53df0 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 bPage*);../* Ope
53e00 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20 rations on page
53e10 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 references. */.S
53e20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
53e30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 t sqlite3PagerWr
53e40 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 ite(DbPage*);.SQ
53e50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
53e60 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f d sqlite3PagerDo
53e70 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 ntWrite(DbPage*)
53e80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
53e90 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
53ea0 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a rMovepage(Pager*
53eb0 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e ,DbPage*,Pgno,in
53ec0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
53ed0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
53ee0 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 gerPageRefcount(
53ef0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 DbPage*);.SQLITE
53f00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
53f10 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
53f20 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53 ta(DbPage *); .S
53f30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
53f40 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 id *sqlite3Pager
53f50 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20 GetExtra(DbPage
53f60 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f *); ../* Functio
53f70 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67 ns used to manag
53f80 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74 e pager transact
53f90 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69 ions and savepoi
53fa0 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts. */.SQLITE_P
53fb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
53fc0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
53fd0 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a (Pager*, int*);.
53fe0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
53ff0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42 nt sqlite3PagerB
54000 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74 egin(Pager*, int
54010 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 exFlag, int);.S
54020 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54030 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f t sqlite3PagerCo
54040 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67 mmitPhaseOne(Pag
54050 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a er*,const char *
54060 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53 zMaster, int);.S
54070 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54080 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 t sqlite3PagerSy
54090 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 nc(Pager *pPager
540a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
540b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
540c0 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f erCommitPhaseTwo
540d0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
540e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
540f0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 ite3PagerRollbac
54100 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 k(Pager*);.SQLIT
54110 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
54120 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61 lite3PagerOpenSa
54130 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
54140 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 Pager, int n);.S
54150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
54160 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61 t sqlite3PagerSa
54170 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 vepoint(Pager *p
54180 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69 Pager, int op, i
54190 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a nt iSavepoint);.
541a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
541b0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 nt sqlite3PagerS
541c0 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 haredLock(Pager
541d0 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 *pPager);../* Fu
541e0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 nctions used to
541f0 71 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74 query pager stat
54200 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74 e and configurat
54210 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion. */.SQLITE_P
54220 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65 RIVATE u8 sqlite
54230 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 3PagerIsreadonly
54240 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 (Pager*);.SQLITE
54250 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
54260 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e ite3PagerRefcoun
54270 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 t(Pager*);.SQLIT
54280 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 E_PRIVATE const
54290 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 char *sqlite3Pag
542a0 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 erFilename(Pager
542b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
542c0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 TE const sqlite3
542d0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 _vfs *sqlite3Pag
542e0 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53 erVfs(Pager*);.S
542f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 QLITE_PRIVATE sq
54300 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 lite3_file *sqli
54310 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 te3PagerFile(Pag
54320 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 er*);.SQLITE_PRI
54330 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
54340 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 *sqlite3PagerJou
54350 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29 rnalname(Pager*)
54360 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
54370 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
54380 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b rNosync(Pager*);
54390 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
543a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 void *sqlite3Pag
543b0 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 erTempSpace(Page
543c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
543d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
543e0 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65 agerIsMemdb(Page
543f0 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f r*);../* Functio
54400 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63 ns used to trunc
54410 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ate the database
54420 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 file. */.SQLITE
54430 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
54440 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 lite3PagerTrunca
54450 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50 teImage(Pager*,P
54460 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 gno);../* Functi
54470 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74 ons to support t
54480 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 esting and debug
54490 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65 ging. */.#if !de
544a0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c fined(NDEBUG) ||
544b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
544c0 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 TEST).SQLITE_PRI
544d0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69 VATE Pgno sqli
544e0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
544f0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c er(DbPage*);.SQL
54500 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e ITE_PRIVATE in
54510 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 t sqlite3PagerIs
54520 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65 writeable(DbPage
54530 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 *);.#endif.#ifde
54540 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
54550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 LITE_PRIVATE i
54560 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 nt *sqlite3Pager
54570 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53 Stats(Pager*);.S
54580 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
54590 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
545a0 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29 rRefdump(Pager*)
545b0 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65 ;. void disable
545c0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
545d0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f rors(void);. vo
545e0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 id enable_simula
545f0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f ted_io_errors(vo
54600 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 id);.#else.# def
54610 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 ine disable_simu
54620 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
54630 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c ).# define enabl
54640 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 e_simulated_io_e
54650 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a rrors().#endif..
54660 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 #endif /* _PAGER
54670 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a _H_ */../*******
54680 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 ******* End of p
54690 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a ager.h *********
546a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
546b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
546c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
546d0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 ******* Continui
546e0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 ng where we left
546f0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e off in sqliteIn
54700 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a t.h ************
54710 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54720 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 ******* Include
54730 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 pcache.h in the
54740 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 middle of sqlite
54750 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a Int.h **********
54760 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
54770 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
54780 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a le pcache.h ****
54790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
547a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
547b0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
547c0 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 08 August 05.**.
547d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
547e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
547f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
54800 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
54810 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
54820 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
54830 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
54840 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
54850 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
54860 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
54870 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
54880 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
54890 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
548a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
548b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
548c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
548d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
548e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
548f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
54930 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 his header file
54940 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 defines the inte
54950 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 rface that the s
54960 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65 qlite page cache
54970 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a .** subsystem. .
54980 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 43 41 */..#ifndef _PCA
54990 43 48 45 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 CHE_H_..typedef
549a0 73 74 72 75 63 74 20 50 67 48 64 72 20 50 67 48 struct PgHdr PgH
549b0 64 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 dr;.typedef stru
549c0 63 74 20 50 43 61 63 68 65 20 50 43 61 63 68 65 ct PCache PCache
549d0 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79 20 70 ;../*.** Every p
549e0 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 age in the cache
549f0 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 is controlled b
54a00 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 y an instance of
54a10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a the following.*
54a20 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a * structure..*/.
54a30 73 74 72 75 63 74 20 50 67 48 64 72 20 7b 0a 20 struct PgHdr {.
54a40 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 20 20 20 void *pData;
54a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54a60 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 /* Content of th
54a70 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 76 6f 69 is page */. voi
54a80 64 20 2a 70 45 78 74 72 61 3b 20 20 20 20 20 20 d *pExtra;
54a90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
54aa0 78 74 72 61 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a xtra content */.
54ab0 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 3b PgHdr *pDirty;
54ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54ad0 20 2f 2a 20 54 72 61 6e 73 69 65 6e 74 20 6c 69 /* Transient li
54ae0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 st of dirty page
54af0 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f s */. Pgno pgno
54b00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
54b10 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
54b20 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61 mber for this pa
54b30 67 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 70 ge */. Pager *p
54b40 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 Pager;
54b50 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 /* The pa
54b60 67 65 72 20 74 68 69 73 20 70 61 67 65 20 69 73 ger this page is
54b70 20 70 61 72 74 20 6f 66 20 2a 2f 0a 23 69 66 64 part of */.#ifd
54b80 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f ef SQLITE_CHECK_
54b90 50 41 47 45 53 0a 20 20 75 33 32 20 70 61 67 65 PAGES. u32 page
54ba0 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 Hash;
54bb0 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 6f /* Hash o
54bc0 66 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 2a f page content *
54bd0 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 66 /.#endif. u16 f
54be0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
54bf0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 47 48 /* PGH
54c00 44 52 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 DR flags defined
54c10 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 2f 2a 2a below */.. /**
54c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
54c60 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 6d 65 6e ****. ** Elemen
54c70 74 73 20 61 62 6f 76 65 20 61 72 65 20 70 75 62 ts above are pub
54c80 6c 69 63 2e 20 20 41 6c 6c 20 74 68 61 74 20 66 lic. All that f
54c90 6f 6c 6c 6f 77 73 20 69 73 20 70 72 69 76 61 74 ollows is privat
54ca0 65 20 74 6f 20 70 63 61 63 68 65 2e 63 0a 20 20 e to pcache.c.
54cb0 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f ** and should no
54cc0 74 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79 t be accessed by
54cd0 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a other modules..
54ce0 20 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65 66 3b */. i16 nRef;
54cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54d00 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
54d10 6f 66 20 75 73 65 72 73 20 6f 66 20 74 68 69 73 of users of this
54d20 20 70 61 67 65 20 2a 2f 0a 20 20 50 43 61 63 68 page */. PCach
54d30 65 20 2a 70 43 61 63 68 65 3b 20 20 20 20 20 20 e *pCache;
54d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 /* Cac
54d50 68 65 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 he that owns thi
54d60 73 20 70 61 67 65 20 2a 2f 0a 0a 20 20 50 67 48 s page */.. PgH
54d70 64 72 20 2a 70 44 69 72 74 79 4e 65 78 74 3b 20 dr *pDirtyNext;
54d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
54d90 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c ext element in l
54da0 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 ist of dirty pag
54db0 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 es */. PgHdr *p
54dc0 44 69 72 74 79 50 72 65 76 3b 20 20 20 20 20 20 DirtyPrev;
54dd0 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f /* Previo
54de0 75 73 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 us element in li
54df0 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 st of dirty page
54e00 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69 74 20 s */.};../* Bit
54e10 76 61 6c 75 65 73 20 66 6f 72 20 50 67 48 64 72 values for PgHdr
54e20 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 65 66 69 6e .flags */.#defin
54e30 65 20 50 47 48 44 52 5f 44 49 52 54 59 20 20 20 e PGHDR_DIRTY
54e40 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32 20 0x002
54e50 20 2f 2a 20 50 61 67 65 20 68 61 73 20 63 68 61 /* Page has cha
54e60 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 nged */.#define
54e70 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
54e80 20 20 20 20 20 20 20 20 30 78 30 30 34 20 20 2f 0x004 /
54e90 2a 20 46 73 79 6e 63 20 74 68 65 20 72 6f 6c 6c * Fsync the roll
54ea0 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 62 65 66 back journal bef
54eb0 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 ore.
54ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
54ed0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 72 ** wr
54ee0 69 74 69 6e 67 20 74 68 69 73 20 70 61 67 65 20 iting this page
54ef0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
54f00 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 */.#define PGHDR
54f10 5f 4e 45 45 44 5f 52 45 41 44 20 20 20 20 20 20 _NEED_READ
54f20 20 20 20 30 78 30 30 38 20 20 2f 2a 20 43 6f 6e 0x008 /* Con
54f30 74 65 6e 74 20 69 73 20 75 6e 72 65 61 64 20 2a tent is unread *
54f40 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f /.#define PGHDR_
54f50 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59 20 20 REUSE_UNLIKELY
54f60 20 20 30 78 30 31 30 20 20 2f 2a 20 41 20 68 69 0x010 /* A hi
54f70 6e 74 20 74 68 61 74 20 72 65 75 73 65 20 69 73 nt that reuse is
54f80 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a 23 64 65 unlikely */.#de
54f90 66 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e 54 5f fine PGHDR_DONT_
54fa0 57 52 49 54 45 20 20 20 20 20 20 20 20 30 78 30 WRITE 0x0
54fb0 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 20 /* Do not wr
54fc0 69 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 64 ite content to d
54fd0 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e 69 74 69 isk */../* Initi
54fe0 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f alize and shutdo
54ff0 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 wn the page cach
55000 65 20 73 75 62 73 79 73 74 65 6d 20 2a 2f 0a 53 e subsystem */.S
55010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
55020 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 t sqlite3PcacheI
55030 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b nitialize(void);
55040 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55050 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
55060 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 heShutdown(void)
55070 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 61 63 68 65 ;../* Page cache
55080 20 62 75 66 66 65 72 20 6d 61 6e 61 67 65 6d 65 buffer manageme
55090 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 nt:.** These rou
550a0 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 tines implement
550b0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
550c0 47 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 51 4c 49 GECACHE..*/.SQLI
550d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
550e0 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 sqlite3PCacheBuf
550f0 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a 2c ferSetup(void *,
55100 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 3b int sz, int n);
55110 0a 0a 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 ../* Create a ne
55120 77 20 70 61 67 65 72 20 63 61 63 68 65 2e 0a 2a w pager cache..*
55130 2a 20 55 6e 64 65 72 20 6d 65 6d 6f 72 79 20 73 * Under memory s
55140 74 72 65 73 73 2c 20 69 6e 76 6f 6b 65 20 78 53 tress, invoke xS
55150 74 72 65 73 73 20 74 6f 20 74 72 79 20 74 6f 20 tress to try to
55160 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61 6e make pages clean
55170 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 61 6e 20 ..** Only clean
55180 61 6e 64 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 and unpinned pag
55190 65 73 20 63 61 6e 20 62 65 20 72 65 63 6c 61 69 es can be reclai
551a0 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 med..*/.SQLITE_P
551b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
551c0 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 te3PcacheOpen(.
551d0 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 int szPage,
551e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
551f0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 /* Size of every
55200 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 page */. int s
55210 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 zExtra,
55220 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 /* Ext
55230 72 61 20 73 70 61 63 65 20 61 73 73 6f 63 69 61 ra space associa
55240 74 65 64 20 77 69 74 68 20 65 61 63 68 20 70 61 ted with each pa
55250 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 ge */. int bPur
55260 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 geable,
55270 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
55280 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 f pages are on b
55290 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a acking store */.
552a0 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 int (*xStress)
552b0 28 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a 29 2c (void*, PgHdr*),
552c0 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 /* Call to try
552d0 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c to make pages cl
552e0 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ean */. void *p
552f0 53 74 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 Stress,
55300 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d /* Argum
55310 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73 20 2a ent to xStress *
55320 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 54 6f 49 /. PCache *pToI
55330 6e 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 nit
55340 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 74 /* Preallocat
55350 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 ed space for the
55360 20 50 43 61 63 68 65 20 2a 2f 0a 29 3b 0a 0a 2f PCache */.);../
55370 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 70 61 67 * Modify the pag
55380 65 2d 73 69 7a 65 20 61 66 74 65 72 20 74 68 65 e-size after the
55390 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 cache has been
553a0 63 72 65 61 74 65 64 2e 20 2a 2f 0a 53 51 4c 49 created. */.SQLI
553b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
553c0 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 sqlite3PcacheSet
553d0 50 61 67 65 53 69 7a 65 28 50 43 61 63 68 65 20 PageSize(PCache
553e0 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 52 65 74 *, int);../* Ret
553f0 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69 6e 20 urn the size in
55400 62 79 74 65 73 20 6f 66 20 61 20 50 43 61 63 68 bytes of a PCach
55410 65 20 6f 62 6a 65 63 74 2e 20 20 55 73 65 64 20 e object. Used
55420 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65 0a 2a to preallocate.*
55430 2a 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e * storage space.
55440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
55450 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
55460 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 3b 0a acheSize(void);.
55470 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73 65 20 ./* One release
55480 70 65 72 20 73 75 63 63 65 73 73 66 75 6c 20 66 per successful f
55490 65 74 63 68 2e 20 20 50 61 67 65 20 69 73 20 70 etch. Page is p
554a0 69 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65 6c 65 inned until rele
554b0 61 73 65 64 2e 0a 2a 2a 20 52 65 66 65 72 65 6e ased..** Referen
554c0 63 65 20 63 6f 75 6e 74 65 64 2e 20 0a 2a 2f 0a ce counted. .*/.
554d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
554e0 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
554f0 46 65 74 63 68 28 50 43 61 63 68 65 2a 2c 20 50 Fetch(PCache*, P
55500 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74 65 46 gno, int createF
55510 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b 0a 53 lag, PgHdr**);.S
55520 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
55530 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
55540 52 65 6c 65 61 73 65 28 50 67 48 64 72 2a 29 3b Release(PgHdr*);
55550 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
55560 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55570 63 68 65 44 72 6f 70 28 50 67 48 64 72 2a 29 3b cheDrop(PgHdr*);
55580 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f /* Remo
55590 76 65 20 70 61 67 65 20 66 72 6f 6d 20 63 61 63 ve page from cac
555a0 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 he */.SQLITE_PRI
555b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
555c0 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 3PcacheMakeDirty
555d0 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 (PgHdr*); /*
555e0 4d 61 6b 65 20 73 75 72 65 20 70 61 67 65 20 69 Make sure page i
555f0 73 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20 2a s marked dirty *
55600 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
55610 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
55620 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 cheMakeClean(PgH
55630 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b dr*); /* Mark
55640 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 61 a single page a
55650 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 4c 49 54 s clean */.SQLIT
55660 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
55670 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 qlite3PcacheClea
55680 6e 41 6c 6c 28 50 43 61 63 68 65 2a 29 3b 20 20 nAll(PCache*);
55690 20 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c 20 64 69 /* Mark all di
556a0 72 74 79 20 6c 69 73 74 20 70 61 67 65 73 20 61 rty list pages a
556b0 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a 20 43 s clean */../* C
556c0 68 61 6e 67 65 20 61 20 70 61 67 65 20 6e 75 6d hange a page num
556d0 62 65 72 2e 20 20 55 73 65 64 20 62 79 20 69 6e ber. Used by in
556e0 63 72 2d 76 61 63 75 75 6d 2e 20 2a 2f 0a 53 51 cr-vacuum. */.SQ
556f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
55700 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d d sqlite3PcacheM
55710 6f 76 65 28 50 67 48 64 72 2a 2c 20 50 67 6e 6f ove(PgHdr*, Pgno
55720 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c );../* Remove al
55730 6c 20 70 61 67 65 73 20 77 69 74 68 20 70 67 6e l pages with pgn
55740 6f 3e 78 2e 20 20 52 65 73 65 74 20 74 68 65 20 o>x. Reset the
55750 63 61 63 68 65 20 69 66 20 78 3d 3d 30 20 2a 2f cache if x==0 */
55760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55770 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
55780 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 heTruncate(PCach
55790 65 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a 0a 2f 2a e*, Pgno x);../*
557a0 20 47 65 74 20 61 20 6c 69 73 74 20 6f 66 20 61 Get a list of a
557b0 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69 ll dirty pages i
557c0 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 n the cache, sor
557d0 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 ted by page numb
557e0 65 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 er */.SQLITE_PRI
557f0 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 VATE PgHdr *sqli
55800 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 te3PcacheDirtyLi
55810 73 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a st(PCache*);../*
55820 20 52 65 73 65 74 20 61 6e 64 20 63 6c 6f 73 65 Reset and close
55830 20 74 68 65 20 63 61 63 68 65 20 6f 62 6a 65 63 the cache objec
55840 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 t */.SQLITE_PRIV
55850 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55860 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61 63 PcacheClose(PCac
55870 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 61 72 20 he*);../* Clear
55880 66 6c 61 67 73 20 66 72 6f 6d 20 70 61 67 65 73 flags from pages
55890 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 of the page cac
558a0 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 he */.SQLITE_PRI
558b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
558c0 33 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 3PcacheClearSync
558d0 46 6c 61 67 73 28 50 43 61 63 68 65 20 2a 29 3b Flags(PCache *);
558e0 0a 0a 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 ../* Discard the
558f0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
55900 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 cache */.SQLITE
55910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
55920 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 lite3PcacheClear
55930 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 52 (PCache*);../* R
55940 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 eturn the total
55950 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73 74 61 number of outsta
55960 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 nding page refer
55970 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49 54 45 5f ences */.SQLITE_
55980 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
55990 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e te3PcacheRefCoun
559a0 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 t(PCache*);../*
559b0 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 Increment the re
559c0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 ference count of
559d0 20 61 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67 an existing pag
559e0 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 e */.SQLITE_PRIV
559f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55a00 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 2a PcacheRef(PgHdr*
55a10 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 );..SQLITE_PRIVA
55a20 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 TE int sqlite3Pc
55a30 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e 74 achePageRefcount
55a40 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a 20 52 65 (PgHdr*);../* Re
55a50 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e turn the total n
55a60 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
55a70 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 tored in the cac
55a80 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 he */.SQLITE_PRI
55a90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
55aa0 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 PcachePagecount(
55ab0 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69 66 20 64 PCache*);..#if d
55ac0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 48 efined(SQLITE_CH
55ad0 45 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20 64 65 ECK_PAGES) || de
55ae0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
55af0 55 47 29 0a 2f 2a 20 49 74 65 72 61 74 65 20 74 UG)./* Iterate t
55b00 68 72 6f 75 67 68 20 61 6c 6c 20 64 69 72 74 79 hrough all dirty
55b10 20 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 pages currently
55b20 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 stored in the c
55b30 61 63 68 65 2e 20 54 68 69 73 0a 2a 2a 20 69 6e ache. This.** in
55b40 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 terface is only
55b50 61 76 61 69 6c 61 62 6c 65 20 69 66 20 53 51 4c available if SQL
55b60 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 ITE_CHECK_PAGES
55b70 69 73 20 64 65 66 69 6e 65 64 20 77 68 65 6e 20 is defined when
55b80 74 68 65 20 0a 2a 2a 20 6c 69 62 72 61 72 79 20 the .** library
55b90 69 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53 51 4c is built..*/.SQL
55ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
55bb0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 sqlite3PcacheIt
55bc0 65 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68 erateDirty(PCach
55bd0 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20 e *pCache, void
55be0 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a (*xIter)(PgHdr *
55bf0 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 53 ));.#endif../* S
55c00 65 74 20 61 6e 64 20 67 65 74 20 74 68 65 20 73 et and get the s
55c10 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 uggested cache-s
55c20 69 7a 65 20 66 6f 72 20 74 68 65 20 73 70 65 63 ize for the spec
55c30 69 66 69 65 64 20 70 61 67 65 72 2d 63 61 63 68 ified pager-cach
55c40 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 67 e..**.** If no g
55c50 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 69 73 lobal maximum is
55c60 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 74 68 65 configured, the
55c70 6e 20 74 68 65 20 73 79 73 74 65 6d 20 61 74 74 n the system att
55c80 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74 0a 2a empts to limit.*
55c90 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 * the total numb
55ca0 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 63 68 er of pages cach
55cb0 65 64 20 62 79 20 70 75 72 67 65 61 62 6c 65 20 ed by purgeable
55cc0 70 61 67 65 72 2d 63 61 63 68 65 73 20 74 6f 20 pager-caches to
55cd0 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 the sum.** of th
55ce0 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 e suggested cach
55cf0 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 51 4c 49 e-sizes..*/.SQLI
55d00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
55d10 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 sqlite3PcacheSet
55d20 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 Cachesize(PCache
55d30 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66 *, int);.#ifdef
55d40 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
55d50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
55d60 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 74 sqlite3PcacheGet
55d70 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 Cachesize(PCache
55d80 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 *);.#endif..#if
55d90 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
55da0 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d E_MEMORY_MANAGEM
55db0 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20 72 65 ENT./* Try to re
55dc0 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73 65 64 turn memory used
55dd0 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20 6d by the pcache m
55de0 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 6d 61 69 odule to the mai
55df0 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 20 2a 2f n memory heap */
55e00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
55e10 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 int sqlite3Pcach
55e20 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 eReleaseMemory(i
55e30 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 nt);.#endif..#if
55e40 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
55e50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
55e60 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
55e70 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e 74 2a eStats(int*,int*
55e80 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23 65 6e ,int*,int*);.#en
55e90 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 dif..SQLITE_PRIV
55ea0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
55eb0 50 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 PCacheSetDefault
55ec0 28 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 69 66 20 (void);..#endif
55ed0 2f 2a 20 5f 50 43 41 43 48 45 5f 48 5f 20 2a 2f /* _PCACHE_H_ */
55ee0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
55ef0 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 2e * End of pcache.
55f00 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
55f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55f30 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
55f40 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 * Continuing whe
55f50 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 re we left off i
55f60 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a n sqliteInt.h **
55f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55f80 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
55f90 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e 68 20 ** Include os.h
55fa0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
55fb0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
55fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
55fd0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
55fe0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 ** Begin file os
55ff0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
56000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56020 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
56030 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a 2a 2a ptember 16.**.**
56040 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
56050 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
56060 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
56070 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
56080 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
56090 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
560a0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
560b0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
560c0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
560d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
560e0 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
560f0 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
56100 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
56110 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
56120 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
56130 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
56140 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
56150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
56190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
561a0 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
561b0 20 66 69 6c 65 20 28 74 6f 67 65 74 68 65 72 20 file (together
561c0 77 69 74 68 20 69 73 20 63 6f 6d 70 61 6e 69 6f with is companio
561d0 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f 64 65 20 n C source-code
561e0 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22 29 20 file.** "os.c")
561f0 61 74 74 65 6d 70 74 20 74 6f 20 61 62 73 74 72 attempt to abstr
56200 61 63 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 act the underlyi
56210 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 ng operating sys
56220 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 74 tem so that.** t
56230 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 he SQLite librar
56240 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e 20 62 y will work on b
56250 6f 74 68 20 50 4f 53 49 58 20 61 6e 64 20 77 69 oth POSIX and wi
56260 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e 0a 2a ndows systems..*
56270 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 *.** This header
56280 20 66 69 6c 65 20 69 73 20 23 69 6e 63 6c 75 64 file is #includ
56290 65 2d 65 64 20 62 79 20 73 71 6c 69 74 65 49 6e e-ed by sqliteIn
562a0 74 2e 68 20 61 6e 64 20 74 68 75 73 20 65 6e 64 t.h and thus end
562b0 73 20 75 70 0a 2a 2a 20 62 65 69 6e 67 20 69 6e s up.** being in
562c0 63 6c 75 64 65 64 20 62 79 20 65 76 65 72 79 20 cluded by every
562d0 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2f 0a source file..*/.
562e0 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f #ifndef _SQLITE_
562f0 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 OS_H_.#define _S
56300 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a QLITE_OS_H_../*.
56310 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 ** Figure out if
56320 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 we are dealing
56330 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e 64 6f with Unix, Windo
56340 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 ws, or some othe
56350 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 r.** operating s
56360 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20 74 68 ystem. After th
56370 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
56380 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73 73 20 k of preprocess
56390 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f macros,.** all o
563a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 f SQLITE_OS_UNIX
563b0 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c , SQLITE_OS_WIN,
563c0 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 SQLITE_OS_OS2,
563d0 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 and SQLITE_OS_OT
563e0 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 HER .** will def
563f0 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72 20 31 ined to either 1
56400 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66 20 74 or 0. One of t
56410 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62 65 20 he four will be
56420 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20 0a 2a 1. The other .*
56430 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62 65 20 * three will be
56440 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 0..*/.#if define
56450 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 d(SQLITE_OS_OTHE
56460 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45 5f 4f R).# if SQLITE_O
56470 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20 20 75 S_OTHER==1.# u
56480 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 ndef SQLITE_OS_U
56490 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65 20 53 NIX.# define S
564a0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a QLITE_OS_UNIX 0.
564b0 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 # undef SQLITE
564c0 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65 66 69 _OS_WIN.# defi
564d0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e ne SQLITE_OS_WIN
564e0 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 0.# undef SQL
564f0 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 64 ITE_OS_OS2.# d
56500 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56510 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23 20 20 OS2 0.# else.#
56520 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 undef SQLITE_OS
56530 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66 0a 23 _OTHER.# endif.#
56540 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66 69 6e endif.#if !defin
56550 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 ed(SQLITE_OS_UNI
56560 58 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 X) && !defined(S
56570 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a QLITE_OS_OTHER).
56580 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f # define SQLITE_
56590 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69 66 6e OS_OTHER 0.# ifn
565a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 def SQLITE_OS_WI
565b0 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e 65 64 N.# if defined
565c0 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 (_WIN32) || defi
565d0 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20 64 65 ned(WIN32) || de
565e0 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e 5f 5f fined(__CYGWIN__
565f0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4d ) || defined(__M
56600 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64 65 66 INGW32__) || def
56610 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f ined(__BORLANDC_
56620 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 _).# define
56630 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 31 0a SQLITE_OS_WIN 1.
56640 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c # define SQL
56650 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 ITE_OS_UNIX 0.#
56660 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 define SQLIT
56670 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20 20 65 E_OS_OS2 0.# e
56680 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 45 4d lif defined(__EM
56690 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 X__) || defined(
566a0 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 _OS2) || defined
566b0 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 (OS2) || defined
566c0 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66 69 6e (_OS2_) || defin
566d0 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 ed(__OS2__).#
566e0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
566f0 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 64 OS_WIN 0.# d
56700 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f efine SQLITE_OS_
56710 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 66 UNIX 0.# def
56720 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 ine SQLITE_OS_OS
56730 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23 20 20 2 1.# else.#
56740 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 define SQLITE
56750 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20 20 _OS_WIN 0.#
56760 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 define SQLITE_OS
56770 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20 64 65 _UNIX 1.# de
56780 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f fine SQLITE_OS_O
56790 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 20 S2 0.# endif.#
567a0 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 53 else.# define S
567b0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a QLITE_OS_UNIX 0.
567c0 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 # define SQLITE
567d0 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e 64 69 _OS_OS2 0.# endi
567e0 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64 65 66 f.#else.# ifndef
567f0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 SQLITE_OS_WIN.#
56800 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
56810 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64 69 66 OS_WIN 0.# endif
56820 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
56830 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 etermine if we a
56840 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 re dealing with
56850 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 WindowsCE - whic
56860 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 h has a much.**
56870 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a reduced API..*/.
56880 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49 4e #if defined(_WIN
56890 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69 6e 65 32_WCE).# define
568a0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
568b0 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 1.#else.# defin
568c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 e SQLITE_OS_WINC
568d0 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a E 0.#endif.../*.
568e0 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 6d 61 ** Define the ma
568f0 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 20 ximum size of a
56900 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 6e 61 temporary filena
56910 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 me.*/.#if SQLITE
56920 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c 75 64 _OS_WIN.# includ
56930 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 e <windows.h>.#
56940 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 define SQLITE_TE
56950 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d 41 58 MPNAME_SIZE (MAX
56960 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69 66 20 _PATH+50).#elif
56970 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 SQLITE_OS_OS2.#
56980 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 if (__GNUC__ > 3
56990 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 || __GNUC__ ==
569a0 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 3 && __GNUC_MINO
569b0 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64 65 66 R__ >= 3) && def
569c0 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f 4d 45 ined(OS2_HIGH_ME
569d0 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75 64 65 MORY).# include
569e0 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f 2a 20 <os2safe.h> /*
569f0 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 has to be includ
56a00 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e 68 20 ed before os2.h
56a10 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 for linking to w
56a20 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 ork */.# endif.#
56a30 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 define INCL_DOS
56a40 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66 69 6e DATETIME.# defin
56a50 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 e INCL_DOSFILEMG
56a60 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f R.# define INCL_
56a70 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65 66 69 DOSERRORS.# defi
56a80 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a ne INCL_DOSMISC.
56a90 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f # define INCL_DO
56aa0 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66 69 6e SPROCESS.# defin
56ab0 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 e INCL_DOSMODULE
56ac0 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 MGR.# define INC
56ad0 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45 53 0a L_DOSSEMAPHORES.
56ae0 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 2e 68 # include <os2.h
56af0 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75 63 6f >.# include <uco
56b00 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 53 nv.h>.# define S
56b10 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 QLITE_TEMPNAME_S
56b20 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54 48 43 IZE (CCHMAXPATHC
56b30 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 OMP).#else.# def
56b40 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e ine SQLITE_TEMPN
56b50 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23 65 6e AME_SIZE 200.#en
56b60 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65 20 53 dif../* If the S
56b70 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61 63 72 ET_FULLSYNC macr
56b80 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 o is not defined
56b90 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d 61 6b above, then mak
56ba0 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a e it.** a no-op.
56bb0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54 5f 46 */.#ifndef SET_F
56bc0 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 ULLSYNC.# define
56bd0 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28 78 2c SET_FULLSYNC(x,
56be0 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a y).#endif../*.**
56bf0 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 69 7a The default siz
56c00 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65 63 74 e of a disk sect
56c10 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 or.*/.#ifndef SQ
56c20 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 LITE_DEFAULT_SEC
56c30 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e TOR_SIZE.# defin
56c40 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 e SQLITE_DEFAULT
56c50 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35 31 32 _SECTOR_SIZE 512
56c60 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
56c70 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61 emporary files a
56c80 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74 69 6e re named startin
56c90 67 20 77 69 74 68 20 74 68 69 73 20 70 72 65 66 g with this pref
56ca0 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 31 ix followed by 1
56cb0 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 6 random.** alph
56cc0 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61 63 74 anumeric charact
56cd0 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69 6c 65 ers, and no file
56ce0 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68 65 79 extension. They
56cf0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 are stored in t
56d00 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61 6e 64 he.** OS's stand
56d10 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 ard temporary fi
56d20 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20 61 6e le directory, an
56d30 64 20 61 72 65 20 64 65 6c 65 74 65 64 20 70 72 d are deleted pr
56d40 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 ior to exit..**
56d50 49 66 20 73 71 6c 69 74 65 20 69 73 20 62 65 69 If sqlite is bei
56d60 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e 20 61 ng embedded in a
56d70 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d 2c 20 nother program,
56d80 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74 6f 20 you may wish to
56d90 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 70 72 change the.** pr
56da0 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63 74 20 efix to reflect
56db0 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73 20 6e your program's n
56dc0 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69 66 20 ame, so that if
56dd0 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65 78 69 your program exi
56de0 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72 65 6c ts.** prematurel
56df0 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 y, old temporary
56e00 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20 65 61 files can be ea
56e10 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65 64 2e sily identified.
56e20 20 54 68 69 73 20 63 61 6e 20 62 65 20 64 6f 6e This can be don
56e30 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53 51 4c e.** using -DSQL
56e40 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 ITE_TEMP_FILE_PR
56e50 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f 20 6f EFIX=myprefix_ o
56e60 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 n the compiler c
56e70 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a ommand line..**.
56e80 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a 20 20 ** 2006-10-31:
56e90 54 68 65 20 64 65 66 61 75 6c 74 20 70 72 65 66 The default pref
56ea0 69 78 20 75 73 65 64 20 74 6f 20 62 65 20 22 73 ix used to be "s
56eb0 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20 74 68 qlite_". But th
56ec0 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73 74 61 en.** Mcafee sta
56ed0 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c 69 74 rted using SQLit
56ee0 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74 69 2d e in their anti-
56ef0 76 69 72 75 73 20 70 72 6f 64 75 63 74 20 61 6e virus product an
56f00 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65 64 20 d it.** started
56f10 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20 77 69 putting files wi
56f20 74 68 20 74 68 65 20 22 73 71 6c 69 74 65 22 20 th the "sqlite"
56f30 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a 2f 74 name in the c:/t
56f40 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 emp folder..** T
56f50 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 his annoyed many
56f60 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73 2e 20 windows users.
56f70 20 54 68 6f 73 65 20 75 73 65 72 73 20 77 6f 75 Those users wou
56f80 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a 2a 2a ld then do a .**
56f90 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68 20 66 Google search f
56fa0 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66 69 6e or "sqlite", fin
56fb0 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e 65 20 d the telephone
56fc0 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65 0a 2a numbers of the.*
56fd0 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61 6e 64 * developers and
56fe0 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20 74 68 call to wake th
56ff0 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74 20 61 em up at night a
57000 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 nd complain..**
57010 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c For this reason,
57020 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 6d the default nam
57030 65 20 70 72 65 66 69 78 20 69 73 20 63 68 61 6e e prefix is chan
57040 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74 ged to be "sqlit
57050 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64 20 62 e" .** spelled b
57060 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20 74 68 ackwards. So th
57070 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61 72 65 e temp files are
57080 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66 69 65 still identifie
57090 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 d, but.** anybod
570a0 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68 20 74 y smart enough t
570b0 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 65 o figure out the
570c0 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20 6c 69 code is also li
570d0 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20 65 6e kely smart.** en
570e0 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74 68 61 ough to know tha
570f0 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 t calling the de
57100 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e 6f 74 veloper will not
57110 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a 2a 2a help get rid.**
57120 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f of the file..*/
57130 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
57140 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 TEMP_FILE_PREFIX
57150 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
57160 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 _TEMP_FILE_PREFI
57170 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65 6e 64 X "etilqs_".#end
57180 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f if../*.** The fo
57190 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 20 6d llowing values m
571a0 61 79 20 62 65 20 70 61 73 73 65 64 20 61 73 20 ay be passed as
571b0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
571c0 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 ent to.** sqlite
571d0 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65 20 76 3OsLock(). The v
571e0 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65 78 68 arious locks exh
571f0 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ibit the followi
57200 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a ng semantics:.**
57210 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20 20 41 .** SHARED: A
57220 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 6f ny number of pro
57230 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20 cesses may hold
57240 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 73 69 a SHARED lock si
57250 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a multaneously..**
57260 20 52 45 53 45 52 56 45 44 3a 20 20 41 20 73 69 RESERVED: A si
57270 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 ngle process may
57280 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 hold a RESERVED
57290 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 lock on a file
572a0 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
572b0 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68 65 72 any time. Other
572c0 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 processes may h
572d0 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e 20 6e old and obtain n
572e0 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 2e ew SHARED locks.
572f0 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20 20 41 .** PENDING: A
57300 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 single process
57310 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e 44 49 may hold a PENDI
57320 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c NG lock on a fil
57330 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 e at.**
57340 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d 65 2e any one time.
57350 20 45 78 69 73 74 69 6e 67 20 53 48 41 52 45 44 Existing SHARED
57360 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72 73 69 locks may persi
57370 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77 0a 2a st, but no new.*
57380 2a 20 20 20 20 20 20 20 20 20 20 20 20 53 48 41 * SHA
57390 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 62 65 RED locks may be
573a0 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f 74 68 obtained by oth
573b0 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a er processes..**
573c0 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e 20 45 EXCLUSIVE: An E
573d0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 70 72 XCLUSIVE lock pr
573e0 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74 68 65 ecludes all othe
573f0 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 r locks..**.** P
57400 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 ENDING_LOCK may
57410 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20 64 69 not be passed di
57420 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69 74 65 rectly to sqlite
57430 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73 74 65 3OsLock(). Inste
57440 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65 73 73 ad, a.** process
57450 20 74 68 61 74 20 72 65 71 75 65 73 74 73 20 61 that requests a
57460 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b n EXCLUSIVE lock
57470 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 6f 62 may actually ob
57480 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 0a 2a tain a PENDING.*
57490 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63 61 6e * lock. This can
574a0 20 62 65 20 75 70 67 72 61 64 65 64 20 74 6f 20 be upgraded to
574b0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
574c0 6b 20 62 79 20 61 20 73 75 62 73 65 71 75 65 6e k by a subsequen
574d0 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c t call to.** sql
574e0 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f ite3OsLock()..*/
574f0 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b .#define NO_LOCK
57500 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 0.#defi
57510 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 ne SHARED_LOCK
57520 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52 45 53 1.#define RES
57530 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 ERVED_LOCK 2.#
57540 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 4c define PENDING_L
57550 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69 6e 65 OCK 3.#define
57560 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 EXCLUSIVE_LOCK
57570 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 4../*.** File L
57580 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20 20 28 ocking Notes: (
57590 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77 69 6e Mostly about win
575a0 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20 73 6f dows but also so
575b0 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e 69 78 me info for Unix
575c0 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f ).**.** We canno
575d0 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65 45 78 t use LockFileEx
575e0 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 () or UnlockFile
575f0 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f 39 38 Ex() on Win95/98
57600 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 /ME because.** t
57610 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 hose functions a
57620 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 re not available
57630 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f 6e 6c . So we use onl
57640 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61 6e 64 y LockFile() and
57650 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 .** UnlockFile()
57660 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 ..**.** LockFile
57670 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f 74 20 () prevents not
57680 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62 75 74 just writing but
57690 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20 62 79 also reading by
576a0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 other processes
576b0 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f 4c 4f ..** A SHARED_LO
576c0 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 CK is obtained b
576d0 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69 6e 67 y locking a sing
576e0 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 le randomly-chos
576f0 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75 74 20 en .** byte out
57700 6f 66 20 61 20 73 70 65 63 69 66 69 63 20 72 61 of a specific ra
57710 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20 54 68 nge of bytes. Th
57720 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73 20 6f e lock byte is o
57730 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a 20 72 btained at .** r
57740 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73 65 70 andom so two sep
57750 61 72 61 74 65 20 72 65 61 64 65 72 73 20 63 61 arate readers ca
57760 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63 65 73 n probably acces
57770 73 20 74 68 65 20 66 69 6c 65 20 61 74 20 74 68 s the file at th
57780 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65 2c e .** same time,
57790 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61 72 65 unless they are
577a0 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63 68 6f unlucky and cho
577b0 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 ose the same loc
577c0 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 k byte..** An EX
577d0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69 73 20 CLUSIVE_LOCK is
577e0 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b obtained by lock
577f0 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20 69 6e ing all bytes in
57800 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 the range..** T
57810 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 here can only be
57820 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20 41 20 one writer. A
57830 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 69 73 RESERVED_LOCK is
57840 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 obtained by loc
57850 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 king.** a single
57860 20 62 79 74 65 20 6f 66 20 74 68 65 20 66 69 6c byte of the fil
57870 65 20 74 68 61 74 20 69 73 20 64 65 73 69 67 6e e that is design
57880 61 74 65 64 20 61 73 20 74 68 65 20 72 65 73 65 ated as the rese
57890 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65 2e 0a rved lock byte..
578a0 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 ** A PENDING_LOC
578b0 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 K is obtained by
578c0 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73 69 67 locking a desig
578d0 6e 61 74 65 64 20 62 79 74 65 20 64 69 66 66 65 nated byte diffe
578e0 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 rent from.** the
578f0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 62 RESERVED_LOCK b
57900 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 yte..**.** On Wi
57910 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74 65 6d nNT/2K/XP system
57920 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 s, LockFileEx()
57930 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 and UnlockFileEx
57940 28 29 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 () are available
57950 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 ,.** which means
57960 20 77 65 20 63 61 6e 20 75 73 65 20 72 65 61 64 we can use read
57970 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 2e er/writer locks.
57980 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f 77 72 When reader/wr
57990 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 iter locks.** ar
579a0 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f 63 6b e used, the lock
579b0 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20 74 68 is placed on th
579c0 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f 66 20 e same range of
579d0 62 79 74 65 73 20 74 68 61 74 20 69 73 20 75 73 bytes that is us
579e0 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62 61 62 ed.** for probab
579f0 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e 67 20 ilistic locking
57a00 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 in Win95/98/ME.
57a10 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f 63 6b Hence, the lock
57a20 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20 77 69 ing scheme.** wi
57a30 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f 20 6f ll support two o
57a40 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72 65 61 r more Win95 rea
57a50 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72 20 6d ders or two or m
57a60 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64 65 72 ore WinNT reader
57a70 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69 6e 67 s..** But a sing
57a80 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 20 le Win95 reader
57a90 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c will lock out al
57aa0 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72 73 20 l WinNT readers
57ab0 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 and a single.**
57ac0 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77 69 6c WinNT reader wil
57ad0 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 6f l lock out all o
57ae0 74 68 65 72 20 57 69 6e 39 35 20 72 65 61 64 65 ther Win95 reade
57af0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f rs..**.** The fo
57b00 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e 65 73 llowing #defines
57b10 20 73 70 65 63 69 66 79 20 74 68 65 20 72 61 6e specify the ran
57b20 67 65 20 6f 66 20 62 79 74 65 73 20 75 73 65 64 ge of bytes used
57b30 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a for locking..**
57b40 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20 SHARED_SIZE is
57b50 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 the number of by
57b60 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e tes available in
57b70 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 the pool from w
57b80 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d hich.** a random
57b90 20 62 79 74 65 20 69 73 20 73 65 6c 65 63 74 65 byte is selecte
57ba0 64 20 66 6f 72 20 61 20 73 68 61 72 65 64 20 6c d for a shared l
57bb0 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c 20 6f ock. The pool o
57bc0 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a 20 73 f bytes for.** s
57bd0 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65 67 69 hared locks begi
57be0 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46 49 52 ns at SHARED_FIR
57bf0 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 ST. .**.** The s
57c00 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 ame locking stra
57c10 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74 65 tegy and.** byte
57c20 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65 64 ranges are used
57c30 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69 73 for Unix. This
57c40 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68 65 leaves open the
57c50 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20 68 possiblity of h
57c60 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 aving.** clients
57c70 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e 54 on win95, winNT
57c80 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20 74 , and unix all t
57c90 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 61 alking to the sa
57ca0 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a 2a me shared file.*
57cb0 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e * and all lockin
57cc0 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54 6f g correctly. To
57cd0 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65 71 do so would req
57ce0 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61 20 uire that samba
57cf0 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 (or whatever.**
57d00 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75 73 tool is being us
57d10 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61 72 ed for file shar
57d20 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73 20 ing) implements
57d30 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79 20 locks correctly
57d40 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f between.** windo
57d50 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49 27 ws and unix. I'
57d60 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74 20 m guessing that
57d70 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f 20 isn't likely to
57d80 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a 2a happen, but by.*
57d90 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 * using the same
57da0 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 77 locking range w
57db0 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 6f e are at least o
57dc0 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73 69 pen to the possi
57dd0 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f bility..**.** Lo
57de0 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77 73 cking in windows
57df0 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20 20 is manditory.
57e00 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e 2c For this reason,
57e10 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72 65 we cannot store
57e20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61 20 .** actual data
57e30 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73 65 in the bytes use
57e40 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 d for locking.
57e50 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72 20 The pager never
57e60 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68 65 allocates.** the
57e70 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 pages involved
57e80 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72 65 in locking there
57e90 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53 49 fore. SHARED_SI
57ea0 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20 73 ZE is selected s
57eb0 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c 6f o.** that all lo
57ec0 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 cks will fit on
57ed0 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65 76 a single page ev
57ee0 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d 75 en at the minimu
57ef0 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 m page size..**
57f00 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65 66 PENDING_BYTE def
57f10 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e 69 ines the beginni
57f20 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73 2e ng of the locks.
57f30 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45 4e By default PEN
57f40 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73 20 DING_BYTE.** is
57f50 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61 74 set high so that
57f60 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20 74 we don't have t
57f70 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75 6e o allocate an un
57f80 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70 74 used page except
57f90 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61 72 .** for very lar
57fa0 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20 42 ge databases. B
57fb0 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74 65 ut one should te
57fc0 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69 70 st the page skip
57fd0 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 ping logic .** b
57fe0 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49 4e y setting PENDIN
57ff0 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20 72 G_BYTE low and r
58000 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69 72 unning the entir
58010 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75 69 e regression sui
58020 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 te..**.** Changi
58030 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ng the value of
58040 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65 73 PENDING_BYTE res
58050 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c 79 ults in a subtly
58060 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a incompatible.**
58070 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 44 file format. D
58080 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 epending on how
58090 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20 79 it is changed, y
580a0 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f 74 ou might not not
580b0 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f 6d ice.** the incom
580c0 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68 74 patibility right
580d0 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e 6e away, even runn
580e0 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72 65 ing a full regre
580f0 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 ssion test..** T
58100 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61 74 he default locat
58110 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f 42 ion of PENDING_B
58120 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73 74 YTE is the first
58130 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a 2a byte past the.*
58140 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e 0a * 1GB boundary..
58150 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 45 **.*/.#define PE
58160 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 NDING_BYTE
58170 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 sqlite3PendingBy
58180 74 65 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 te.#define RESER
58190 56 45 44 5f 42 59 54 45 20 20 20 20 20 28 50 45 VED_BYTE (PE
581a0 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a 23 64 NDING_BYTE+1).#d
581b0 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46 49 52 efine SHARED_FIR
581c0 53 54 20 20 20 20 20 20 28 50 45 4e 44 49 4e 47 ST (PENDING
581d0 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69 6e 65 _BYTE+2).#define
581e0 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20 20 20 SHARED_SIZE
581f0 20 20 20 35 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 510../*.** Wr
58200 61 70 70 65 72 20 61 72 6f 75 6e 64 20 4f 53 20 apper around OS
58210 73 70 65 63 69 66 69 63 20 73 71 6c 69 74 65 33 specific sqlite3
58220 5f 6f 73 5f 69 6e 69 74 28 29 20 66 75 6e 63 74 _os_init() funct
58230 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
58240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58250 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 3b 0a e3OsInit(void);.
58260 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e ./* .** Function
58270 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 s for accessing
58280 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 sqlite3_file met
58290 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f hods .*/.SQLITE_
582a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
582b0 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74 te3OsClose(sqlit
582c0 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 e3_file*);.SQLIT
582d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
582e0 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69 lite3OsRead(sqli
582f0 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f 69 64 2a te3_file*, void*
58300 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f , int amt, i64 o
58310 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 ffset);.SQLITE_P
58320 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58330 65 33 4f 73 57 72 69 74 65 28 73 71 6c 69 74 65 e3OsWrite(sqlite
58340 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 3_file*, const v
58350 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 oid*, int amt, i
58360 36 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 64 offset);.SQLI
58370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
58380 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
58390 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
583a0 69 36 34 20 73 69 7a 65 29 3b 0a 53 51 4c 49 54 i64 size);.SQLIT
583b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
583c0 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 lite3OsSync(sqli
583d0 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b te3_file*, int);
583e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
583f0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c int sqlite3OsFil
58400 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 eSize(sqlite3_fi
58410 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 le*, i64 *pSize)
58420 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
58430 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c 6f int sqlite3OsLo
58440 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a ck(sqlite3_file*
58450 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
58460 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
58470 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 e3OsUnlock(sqlit
58480 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a e3_file*, int);.
58490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
584a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 nt sqlite3OsChec
584b0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
584c0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
584d0 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 int *pResOut);.S
584e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
584f0 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 t sqlite3OsFileC
58500 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 ontrol(sqlite3_f
58510 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b ile*,int,void*);
58520 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
58530 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 FCNTL_DB_UNCHANG
58540 45 44 20 30 78 63 61 30 39 33 66 61 30 0a 53 51 ED 0xca093fa0.SQ
58550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
58560 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 sqlite3OsSector
58570 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c Size(sqlite3_fil
58580 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 e *id);.SQLITE_P
58590 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
585a0 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
585b0 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
585c0 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 3_file *id);../*
585d0 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 .** Functions f
585e0 6f 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c or accessing sql
585f0 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 ite3_vfs methods
58600 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
58610 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
58620 73 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 sOpen(sqlite3_vf
58630 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 s *, const char
58640 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a *, sqlite3_file*
58650 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 , int, int *);.S
58660 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
58670 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 t sqlite3OsDelet
58680 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c e(sqlite3_vfs *,
58690 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
586a0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
586b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
586c0 73 41 63 63 65 73 73 28 73 71 6c 69 74 65 33 5f sAccess(sqlite3_
586d0 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 vfs *, const cha
586e0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 70 r *, int, int *p
586f0 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f ResOut);.SQLITE_
58700 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
58710 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d te3OsFullPathnam
58720 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c e(sqlite3_vfs *,
58730 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 const char *, i
58740 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 23 69 66 nt, char *);.#if
58750 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
58760 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a _LOAD_EXTENSION.
58770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
58780 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c oid *sqlite3OsDl
58790 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 Open(sqlite3_vfs
587a0 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *, const char *
587b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
587c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 E void sqlite3Os
587d0 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
587e0 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 vfs *, int, char
587f0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
58800 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 ATE void (*sqlit
58810 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 e3OsDlSym(sqlite
58820 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 3_vfs *, void *,
58830 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 29 28 const char *))(
58840 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
58850 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
58860 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 e3OsDlClose(sqli
58870 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64 20 te3_vfs *, void
58880 2a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 *);.#endif /* SQ
58890 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
588a0 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 XTENSION */.SQLI
588b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
588c0 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 qlite3OsRandomne
588d0 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a ss(sqlite3_vfs *
588e0 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a , int, char *);.
588f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58900 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 nt sqlite3OsSlee
58910 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c p(sqlite3_vfs *,
58920 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
58930 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
58940 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73 3OsCurrentTime(s
58950 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 64 6f qlite3_vfs *, do
58960 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 uble*);../*.** C
58970 6f 6e 76 65 6e 69 65 6e 63 65 20 66 75 6e 63 74 onvenience funct
58980 69 6f 6e 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 ions for opening
58990 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 66 69 6c and closing fil
589a0 65 73 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c es using .** sql
589b0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f ite3_malloc() to
589c0 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 6f obtain space fo
589d0 72 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c r the file-handl
589e0 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a e structure..*/.
589f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58a00 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e nt sqlite3OsOpen
58a10 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 76 Malloc(sqlite3_v
58a20 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 fs *, const char
58a30 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 *, sqlite3_file
58a40 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a **, int,int*);.
58a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
58a60 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 nt sqlite3OsClos
58a70 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66 69 eFree(sqlite3_fi
58a80 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64 69 66 20 2f le *);..#endif /
58a90 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 * _SQLITE_OS_H_
58aa0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../***********
58ab0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 68 20 *** End of os.h
58ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58af0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
58b00 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 *** Continuing w
58b10 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 here we left off
58b20 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 in sqliteInt.h
58b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58b40 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
58b50 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6d 75 74 65 *** Include mute
58b60 78 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c x.h in the middl
58b70 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 e of sqliteInt.h
58b80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
58b90 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
58ba0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d *** Begin file m
58bb0 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utex.h *********
58bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58be0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 **/./*.** 2007 A
58bf0 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 ugust 28.**.** T
58c00 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
58c10 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
58c20 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
58c30 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
58c40 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
58c50 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
58c60 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
58c70 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
58c80 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
58c90 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
58ca0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
58cb0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
58cc0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
58cd0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
58ce0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
58cf0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
58d00 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
58d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
58d50 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 *******.**.** Th
58d60 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
58d70 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 the common head
58d80 65 72 20 66 6f 72 20 61 6c 6c 20 6d 75 74 65 78 er for all mutex
58d90 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
58da0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 49 ..** The sqliteI
58db0 6e 74 2e 68 20 68 65 61 64 65 72 20 23 69 6e 63 nt.h header #inc
58dc0 6c 75 64 65 73 20 74 68 69 73 20 66 69 6c 65 20 ludes this file
58dd0 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 76 so that it is av
58de0 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c ailable.** to al
58df0 6c 20 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 20 l source files.
58e00 20 57 65 20 62 72 65 61 6b 20 69 74 20 6f 75 74 We break it out
58e10 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f in an effort to
58e20 20 6b 65 65 70 20 74 68 65 20 63 6f 64 65 0a 2a keep the code.*
58e30 2a 20 62 65 74 74 65 72 20 6f 72 67 61 6e 69 7a * better organiz
58e40 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 ed..**.** NOTE:
58e50 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20 73 68 source files sh
58e60 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c ould *not* #incl
58e70 75 64 65 20 74 68 69 73 20 68 65 61 64 65 72 20 ude this header
58e80 66 69 6c 65 20 64 69 72 65 63 74 6c 79 2e 0a 2a file directly..*
58e90 2a 20 53 6f 75 72 63 65 20 66 69 6c 65 73 20 73 * Source files s
58ea0 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64 65 20 74 hould #include t
58eb0 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 66 he sqliteInt.h f
58ec0 69 6c 65 20 61 6e 64 20 6c 65 74 20 74 68 61 74 ile and let that
58ed0 20 66 69 6c 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 file.** include
58ee0 20 74 68 69 73 20 6f 6e 65 20 69 6e 64 69 72 65 this one indire
58ef0 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a ctly..*/.../*.**
58f00 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 Figure out what
58f10 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 version of the
58f20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 code to use. Th
58f30 65 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a e choices are.**
58f40 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 .** SQLITE_MUT
58f50 45 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 EX_OMIT
58f60 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 No mutex logic.
58f70 20 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e Not even stubs.
58f80 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 The.**
58f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
58fa0 20 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 mutexes imp
58fb0 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 lemention cannot
58fc0 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a be overridden.*
58fd0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
58fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 at
58ff0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a start-time..**.
59000 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 ** SQLITE_MUTE
59010 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 X_NOOP F
59020 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 or single-thread
59030 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e ed applications.
59040 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 No.**
59050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
59060 20 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 mutual exclu
59070 73 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 sion is provided
59080 2e 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 . But this.**
59090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
590a0 20 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 imple
590b0 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 mentation can be
590c0 20 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a overridden at.*
590d0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
590e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 st
590f0 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 art-time..**.**
59100 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 SQLITE_MUTEX_P
59110 54 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 THREADS For
59120 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 multi-threaded a
59130 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 pplications on U
59140 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c nix..**.** SQL
59150 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 ITE_MUTEX_W32
59160 20 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 For multi
59170 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 -threaded applic
59180 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e ations on Win32.
59190 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f .**.** SQLITE_
591a0 4d 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 MUTEX_OS2
591b0 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 For multi-thr
591c0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f eaded applicatio
591d0 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 ns on OS/2..*/.#
591e0 69 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 if !SQLITE_THREA
591f0 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 DSAFE.# define S
59200 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 QLITE_MUTEX_OMIT
59210 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
59220 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 TE_THREADSAFE &&
59230 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
59240 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 _MUTEX_NOOP).#
59250 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 if SQLITE_OS_UNI
59260 58 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 X.# define SQ
59270 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 LITE_MUTEX_PTHRE
59280 41 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 ADS.# elif SQLI
59290 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 TE_OS_WIN.# d
592a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 efine SQLITE_MUT
592b0 45 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 EX_W32.# elif S
592c0 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 QLITE_OS_OS2.#
592d0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
592e0 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 MUTEX_OS2.# els
592f0 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 e.# define SQ
59300 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a LITE_MUTEX_NOOP.
59310 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a # endif.#endif.
59320 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d .#ifdef SQLITE_M
59330 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 UTEX_OMIT./*.**
59340 49 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d If this is a no-
59350 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f op implementatio
59360 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 n, implement eve
59370 72 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f rything as macro
59380 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 s..*/.#define sq
59390 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
593a0 63 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 c(X) ((sqlite
593b0 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 3_mutex*)8).#def
593c0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ine sqlite3_mute
593d0 78 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e x_free(X).#defin
593e0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
593f0 65 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 enter(X).#define
59400 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
59410 72 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 ry(X) SQLIT
59420 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c E_OK.#define sql
59430 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
59440 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 (X).#define sqli
59450 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 te3_mutex_held(X
59460 29 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 ) 1.#define
59470 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f sqlite3_mutex_no
59480 74 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 theld(X) 1.#def
59490 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 ine sqlite3Mutex
594a0 41 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 Alloc(X) ((
594b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 sqlite3_mutex*)8
594c0 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 ).#define sqlite
594d0 33 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 3MutexInit()
594e0 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 SQLITE_OK.#d
594f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 efine sqlite3Mut
59500 65 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f exEnd().#endif /
59510 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 * defined(SQLITE
59520 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a _MUTEX_OMIT) */.
59530 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
59540 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 End of mutex.h
59550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
59570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
59580 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
59590 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
595a0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
595b0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a sqliteInt.h ***
595c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
595d0 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 .../*.** Each da
595e0 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 tabase file to b
595f0 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 e accessed by th
59600 65 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 e system is an i
59610 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
59620 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 e following stru
59630 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 cture. There ar
59640 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f e normally two o
59650 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
59660 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c es.** in the sql
59670 69 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e ite.aDb[] array.
59680 20 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 aDb[0] is the
59690 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
596a0 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d le and.** aDb[1]
596b0 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 is the database
596c0 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f file used to ho
596d0 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 ld temporary tab
596e0 6c 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c les. Additional
596f0 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 .** databases ma
59700 79 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a y be attached..*
59710 2f 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 /.struct Db {.
59720 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
59730 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
59740 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f this database */
59750 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 . Btree *pBt;
59760 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 /* The B
59770 2a 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 *Tree structure
59780 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 for this databas
59790 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 e file */. u8 i
597a0 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 nTrans;
597b0 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 /* 0: not writa
597c0 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 ble. 1: Transac
597d0 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 tion. 2: Checkp
597e0 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 oint */. u8 saf
597f0 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f ety_level; /
59800 2a 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 * How aggressive
59810 20 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 at syncing data
59820 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 53 63 to disk */. Sc
59830 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 hema *pSchema;
59840 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
59850 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
59860 20 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72 65 (possibly share
59870 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 d) */.};../*.**
59880 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
59890 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
598a0 75 63 74 75 72 65 20 73 74 6f 72 65 73 20 61 20 ucture stores a
598b0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e database schema.
598c0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
598d0 61 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 are no virtual t
598e0 61 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65 64 ables configured
598f0 20 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61 2c in this schema,
59900 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 the.** Schema.d
59910 62 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 b variable is se
59920 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 t to NULL. After
59930 20 74 68 65 20 66 69 72 73 74 20 76 69 72 74 75 the first virtu
59940 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 al table.** has
59950 62 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20 69 been added, it i
59960 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 s set to point t
59970 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 o the database c
59980 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 onnection .** us
59990 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 ed to create the
599a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 connection. Onc
599b0 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c e a virtual tabl
599c0 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 e has been.** ad
599d0 64 65 64 20 74 6f 20 74 68 65 20 53 63 68 65 6d ded to the Schem
599e0 61 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 a structure and
599f0 74 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76 61 the Schema.db va
59a00 72 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 riable populated
59a10 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 , .** only that
59a20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
59a30 69 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65 20 ion may use the
59a40 53 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61 72 Schema to prepar
59a50 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 e .** statements
59a60 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68 65 ..*/.struct Sche
59a70 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d ma {. int schem
59a80 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 a_cookie; /* D
59a90 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 76 atabase schema v
59aa0 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f ersion number fo
59ab0 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 r this file */.
59ac0 20 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20 20 Hash tblHash;
59ad0 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 /* All tab
59ae0 6c 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e les indexed by n
59af0 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 ame */. Hash id
59b00 78 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a xHash; /*
59b10 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 All (named) ind
59b20 69 63 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 ices indexed by
59b30 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 name */. Hash t
59b40 72 69 67 48 61 73 68 3b 20 20 20 20 20 20 20 2f rigHash; /
59b50 2a 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 69 * All triggers i
59b60 6e 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a ndexed by name *
59b70 2f 0a 20 20 48 61 73 68 20 66 6b 65 79 48 61 73 /. Hash fkeyHas
59b80 68 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 h; /* All
59b90 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 62 79 20 foreign keys by
59ba0 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 referenced table
59bb0 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 name */. Table
59bc0 20 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20 *pSeqTab;
59bd0 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65 /* The sqlite_se
59be0 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 quence table use
59bf0 64 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 d by AUTOINCREME
59c00 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f NT */. u8 file_
59c10 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 format; /*
59c20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 Schema format ve
59c30 72 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 rsion for this f
59c40 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b ile */. u8 enc;
59c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
59c60 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 Text encoding u
59c70 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 sed by this data
59c80 62 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c base */. u16 fl
59c90 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f ags; /
59ca0 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 * Flags associat
59cb0 65 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 ed with this sch
59cc0 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 ema */. int cac
59cd0 68 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a he_size; /*
59ce0 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
59cf0 20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63 to use in the c
59d00 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 ache */.#ifndef
59d10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
59d20 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 UALTABLE. sqlit
59d30 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 e3 *db;
59d40 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 /* "Owner" conne
59d50 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 ction. See comme
59d60 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 nt above */.#end
59d70 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 if.};../*.** The
59d80 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 se macros can be
59d90 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 used to test, s
59da0 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 et, or clear bit
59db0 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e s in the .** Db.
59dc0 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a flags field..*/.
59dd0 23 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f #define DbHasPro
59de0 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 perty(D,I,P)
59df0 20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 (((D)->aDb[I].p
59e00 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 Schema->flags&(P
59e10 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 ))==(P)).#define
59e20 20 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74 DbHasAnyPropert
59e30 79 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d y(D,I,P) (((D)-
59e40 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d >aDb[I].pSchema-
59e50 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a >flags&(P))!=0).
59e60 23 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f #define DbSetPro
59e70 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 perty(D,I,P)
59e80 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 (D)->aDb[I].pSc
59e90 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 hema->flags|=(P)
59ea0 0a 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72 .#define DbClear
59eb0 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 Property(D,I,P)
59ec0 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 (D)->aDb[I].pS
59ed0 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 chema->flags&=~(
59ee0 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 P)../*.** Allowe
59ef0 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 d values for the
59f00 20 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e DB.flags field.
59f10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 .**.** The DB_Sc
59f20 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 hemaLoaded flag
59f30 69 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65 is set after the
59f40 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 database schema
59f50 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 has been.** rea
59f60 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 d into internal
59f70 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a hash tables..**.
59f80 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 ** DB_UnresetVie
59f90 77 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e ws means that on
59fa0 65 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 e or more views
59fb0 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 have column name
59fc0 73 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 s that.** have b
59fd0 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 een filled out.
59fe0 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63 If the schema c
59ff0 68 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f hanges, these co
5a000 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 lumn names might
5a010 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20 .** changes and
5a020 73 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c so the view will
5a030 20 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65 need to be rese
5a040 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 t..*/.#define DB
5a050 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 _SchemaLoaded
5a060 20 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20 0x0001 /* The
5a070 73 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 schema has been
5a080 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e loaded */.#defin
5a090 65 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 e DB_UnresetView
5a0a0 73 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 s 0x0002 /*
5a0b0 53 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 Some views have
5a0c0 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e defined column n
5a0d0 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 ames */.#define
5a0e0 44 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 DB_Empty
5a0f0 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 0x0004 /* Th
5a100 65 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 e file is empty
5a110 28 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 (length 0 bytes)
5a120 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e */../*.** The n
5a130 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 umber of differe
5a140 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e nt kinds of thin
5a150 67 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c gs that can be l
5a160 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 imited.** using
5a170 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 the sqlite3_limi
5a180 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a t() interface..*
5a190 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5a1a0 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 _N_LIMIT (SQLITE
5a1b0 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 _LIMIT_TRIGGER_D
5a1c0 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c EPTH+1)../*.** L
5a1d0 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 ookaside malloc
5a1e0 69 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 65 is a set of fixe
5a1f0 64 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 74 d-size buffers t
5a200 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 0a hat can be used.
5a210 2a 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 6d ** to satisfy sm
5a220 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 all transient me
5a230 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
5a240 72 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 6a requests for obj
5a250 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 ects.** associat
5a260 65 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 ed with a partic
5a270 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f ular database co
5a280 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 nnection. The u
5a290 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 se of.** lookasi
5a2a0 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 de malloc provid
5a2b0 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74 es a significant
5a2c0 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 performance enh
5a2d0 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 ancement.** (app
5a2e0 72 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f 69 rox 10%) by avoi
5a2f0 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 ding numerous ma
5a300 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 73 lloc/free reques
5a310 74 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 ts while parsing
5a320 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e .** SQL statemen
5a330 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f ts..**.** The Lo
5a340 6f 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72 okaside structur
5a350 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 e holds configur
5a360 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f ation informatio
5a370 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c n about the.** l
5a380 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 ookaside malloc
5a390 73 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68 subsystem. Each
5a3a0 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 available memor
5a3b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a y allocation in.
5a3c0 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 ** the lookaside
5a3d0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 74 subsystem is st
5a3e0 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 ored on a linked
5a3f0 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 list of Lookasi
5a400 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 deSlot.** object
5a410 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 s..**.** Lookasi
5a420 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 de allocations a
5a430 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 re only allowed
5a440 66 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 74 for objects that
5a450 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a are associated.
5a460 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 ** with a partic
5a470 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f ular database co
5a480 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 nnection. Hence
5a490 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 , schema informa
5a4a0 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 tion cannot.** b
5a4b0 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b e stored in look
5a4c0 61 73 69 64 65 20 62 65 63 61 75 73 65 20 69 6e aside because in
5a4d0 20 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f shared cache mo
5a4e0 64 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e de the schema in
5a4f0 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 formation.** is
5a500 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70 shared by multip
5a510 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e le database conn
5a520 65 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 ections. Theref
5a530 6f 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 69 ore, while parsi
5a540 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 ng.** schema inf
5a550 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f ormation, the Lo
5a560 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 okaside.bEnabled
5a570 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 flag is cleared
5a580 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b so that.** look
5a590 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e aside allocation
5a5a0 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 74 s are not used t
5a5b0 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 o construct the
5a5c0 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a schema objects..
5a5d0 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 */.struct Lookas
5a5e0 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 ide {. u16 sz;
5a5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5a600 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 /* Size of each
5a610 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 buffer in bytes
5a620 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64 */. u8 bEnabled
5a630 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
5a640 46 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c 65 False to disable
5a650 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 new lookaside a
5a660 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 llocations */.
5a670 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 u8 bMalloced;
5a680 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
5a690 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e if pStart obtain
5a6a0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
5a6b0 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e malloc() */. in
5a6c0 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 t nOut;
5a6d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
5a6e0 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 of buffers curre
5a6f0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 ntly checked out
5a700 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b */. int mxOut;
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5a720 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 Highwater mark
5a730 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f for nOut */. Lo
5a740 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 okasideSlot *pFr
5a750 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 ee; /* List of
5a760 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65 available buffe
5a770 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 rs */. void *pS
5a780 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 tart;
5a790 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 /* First byte of
5a7a0 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 available memor
5a7b0 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 y space */. voi
5a7c0 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 d *pEnd;
5a7d0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 /* First by
5a7e0 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 te past end of a
5a7f0 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a vailable space *
5a800 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b /.};.struct Look
5a810 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f asideSlot {. Lo
5a820 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 okasideSlot *pNe
5a830 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 xt; /* Next b
5a840 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73 uffer in the lis
5a850 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72 t of free buffer
5a860 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 s */.};../*.** A
5a870 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 hash table for
5a880 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 function definit
5a890 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 ions..**.** Hash
5a8a0 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74 each FuncDef st
5a8b0 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 ructure into one
5a8c0 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48 of the FuncDefH
5a8d0 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a ash.a[] slots..*
5a8e0 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 * Collisions are
5a8f0 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e on the FuncDef.
5a900 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a pHash chain..*/.
5a910 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 struct FuncDefHa
5a920 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a sh {. FuncDef *
5a930 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 a[23]; /*
5a940 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 Hash table for f
5a950 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a unctions */.};..
5a960 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 /*.** Each datab
5a970 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e ase is an instan
5a980 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ce of the follow
5a990 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a ing structure..*
5a9a0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e *.** The sqlite.
5a9b0 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 lastRowid record
5a9c0 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 s the last inser
5a9d0 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 t rowid generate
5a9e0 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 d by an.** inser
5a9f0 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e t statement. In
5aa00 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 serts on views d
5aa10 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 o not affect its
5aa20 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a value. Each.**
5aa30 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73 trigger has its
5aa40 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f own context, so
5aa50 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 that lastRowid
5aa60 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 can be updated i
5aa70 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 nside.** trigger
5aa80 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 s as usual. The
5aa90 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
5aaa0 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 will be restored
5aab0 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 once the trigge
5aac0 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f r.** exits. Upo
5aad0 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 n entering a bef
5aae0 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f ore or instead o
5aaf0 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 f trigger, lastR
5ab00 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f owid is no.** lo
5ab10 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 nger (since afte
5ab20 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 r version 2.8.12
5ab30 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a ) reset to -1..*
5ab40 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e *.** The sqlite.
5ab50 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 nChange does not
5ab60 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 count changes w
5ab70 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 ithin triggers a
5ab80 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 nd keeps no.** c
5ab90 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 ontext. It is r
5aba0 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 eset at start of
5abb0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a sqlite3_exec..*
5abc0 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 * The sqlite.lsC
5abd0 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 hange represents
5abe0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
5abf0 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 hanges made by t
5ac00 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 he last.** inser
5ac10 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 t, update, or de
5ac20 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 lete statement.
5ac30 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 It remains cons
5ac40 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 tant throughout
5ac50 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 the.** length of
5ac60 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 a statement and
5ac70 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 is then updated
5ac80 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 by OP_SetCounts
5ac90 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a . It keeps a.**
5aca0 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a context stack j
5acb0 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 ust like lastRow
5acc0 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 id so that the c
5acd0 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a ount of changes.
5ace0 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 ** within a trig
5acf0 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 ger is not seen
5ad00 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67 outside the trig
5ad10 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f ger. Changes to
5ad20 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a views do not.**
5ad30 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 affect the valu
5ad40 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a e of lsChange..*
5ad50 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 * The sqlite.csC
5ad60 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 hange keeps trac
5ad70 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 k of the number
5ad80 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 of current chang
5ad90 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 es (since.** the
5ada0 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 last statement)
5adb0 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 and is used to
5adc0 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 update sqlite_ls
5add0 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 Change..**.** Th
5ade0 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c e member variabl
5adf0 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 es sqlite.errCod
5ae00 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 e, sqlite.zErrMs
5ae10 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 g and sqlite.zEr
5ae20 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 rMsg16.** store
5ae30 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 the most recent
5ae40 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 error code and,
5ae50 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 if applicable, s
5ae60 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e tring. The.** in
5ae70 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 ternal function
5ae80 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 sqlite3Error() i
5ae90 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 s used to set th
5aea0 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a ese variables.**
5aeb0 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a consistently..*
5aec0 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 /.struct sqlite3
5aed0 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 {. sqlite3_vfs
5aee0 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 *pVfs;
5aef0 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 /* OS Interfa
5af00 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b ce */. int nDb;
5af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
5af30 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 of backends curr
5af40 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a ently in use */.
5af50 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 Db *aDb;
5af60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5af70 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 /* All backends
5af80 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 */. int flags;
5af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5afa0 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 /* Miscellane
5afb0 6f 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 ous flags. See b
5afc0 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 elow */. int op
5afd0 65 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 enFlags;
5afe0 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 /* Flags
5aff0 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 passed to sqlit
5b000 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a e3_vfs.xOpen() *
5b010 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b /. int errCode;
5b020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b030 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
5b040 20 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c error code (SQL
5b050 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 ITE_*) */. int
5b060 65 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 errMask;
5b070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 /* & r
5b080 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 esult codes with
5b090 20 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 this before ret
5b0a0 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 urning */. u8 a
5b0b0 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 utoCommit;
5b0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
5b0d0 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 auto-commit fla
5b0e0 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f g. */. u8 temp_
5b0f0 73 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 store;
5b100 20 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 /* 1: file
5b110 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 2: memory 0: de
5b120 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 fault */. u8 ma
5b130 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 llocFailed;
5b140 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
5b150 20 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e if we have seen
5b160 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 a malloc failur
5b170 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f e */. u8 dfltLo
5b180 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 ckMode;
5b190 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 /* Default
5b1a0 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 locking-mode for
5b1b0 20 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f attached dbs */
5b1c0 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 . u8 dfltJourna
5b1d0 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 lMode;
5b1e0 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 /* Default jour
5b1f0 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 nal mode for att
5b200 61 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 ached dbs */. s
5b210 69 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 igned char nextA
5b220 75 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 utovac; /*
5b230 41 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 Autovac setting
5b240 61 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 after VACUUM if
5b250 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 >=0 */. int nex
5b260 74 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 tPagesize;
5b270 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 /* Pagesi
5b280 7a 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 ze after VACUUM
5b290 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e if >0 */. int n
5b2a0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Table;
5b2b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
5b2c0 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 er of tables in
5b2d0 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
5b2e0 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 CollSeq *pDflt
5b2f0 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Coll;
5b300 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 /* The default c
5b310 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5b320 65 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 e (BINARY) */.
5b330 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 i64 lastRowid;
5b340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b350 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 ROWID of most r
5b360 65 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 ecent insert (se
5b370 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 75 33 e above) */. u3
5b380 32 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 2 magic;
5b390 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
5b3a0 61 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 agic number for
5b3b0 64 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 6d detect library m
5b3c0 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e isuse */. int n
5b3d0 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 Change;
5b3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
5b3f0 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 e returned by sq
5b400 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 lite3_changes()
5b410 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 */. int nTotalC
5b420 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 hange;
5b430 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 /* Value retu
5b440 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
5b450 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 total_changes()
5b460 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 */. sqlite3_mut
5b470 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
5b480 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e /* Connection
5b490 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 mutex */. int
5b4a0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f aLimit[SQLITE_N_
5b4b0 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d LIMIT]; /* Lim
5b4c0 69 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 its */. struct
5b4d0 73 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 sqlite3InitInfo
5b4e0 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d { /* Inform
5b4f0 61 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69 6e ation used durin
5b500 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e g initialization
5b510 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b */. int iDb;
5b520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b530 20 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b /* When back
5b540 20 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69 61 is being initia
5b550 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 lized */. int
5b560 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 newTnum;
5b570 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 /* Root
5b580 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62 65 page of table be
5b590 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 ing initialized
5b5a0 2a 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b 20 */. u8 busy;
5b5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b5c0 20 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63 75 /* TRUE if cu
5b5d0 72 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 rrently initiali
5b5e0 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f zing */. u8 o
5b5f0 72 70 68 61 6e 54 72 69 67 67 65 72 3b 20 20 20 rphanTrigger;
5b600 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 /* Last
5b610 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 statement is orp
5b620 68 61 6e 65 64 20 54 45 4d 50 20 74 72 69 67 67 haned TEMP trigg
5b630 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a er */. } init;.
5b640 20 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e int nExtension
5b650 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b660 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 /* Number of loa
5b670 64 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a ded extensions *
5b680 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 /. void **aExte
5b690 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 nsion;
5b6a0 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68 /* Array of sh
5b6b0 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e ared library han
5b6c0 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 dles */. struct
5b6d0 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 Vdbe *pVdbe;
5b6e0 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
5b6f0 6f 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61 of active virtua
5b700 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 l machines */.
5b710 69 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e int activeVdbeCn
5b720 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
5b730 20 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 45 73 Number of VDBEs
5b740 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 currently execu
5b750 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 ting */. int wr
5b760 69 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 iteVdbeCnt;
5b770 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5b780 72 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45 r of active VDBE
5b790 73 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69 s that are writi
5b7a0 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 ng */. void (*x
5b7b0 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e Trace)(void*,con
5b7c0 73 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20 st char*);
5b7d0 20 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74 /* Trace funct
5b7e0 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 ion */. void *p
5b7f0 54 72 61 63 65 41 72 67 3b 20 20 20 20 20 20 20 TraceArg;
5b800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b810 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 /* Argument t
5b820 6f 20 74 68 65 20 74 72 61 63 65 20 66 75 6e 63 o the trace func
5b830 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 tion */. void (
5b840 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a *xProfile)(void*
5b850 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 ,const char*,u64
5b860 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 ); /* Profiling
5b870 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 function */. v
5b880 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 oid *pProfileArg
5b890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5b8a0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
5b8b0 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 ment to profile
5b8c0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f function */. vo
5b8d0 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 id *pCommitArg;
5b8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5b8f0 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 /* Argument to x
5b900 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 CommitCallback()
5b910 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 */ . int (*x
5b920 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 CommitCallback)(
5b930 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e void*); /* In
5b940 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 voked at every c
5b950 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 ommit. */. void
5b960 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 *pRollbackArg;
5b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5b980 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f Argument to xRo
5b990 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 llbackCallback()
5b9a0 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 28 2a */ . void (*
5b9b0 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 xRollbackCallbac
5b9c0 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e k)(void*); /* In
5b9d0 76 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 voked at every c
5b9e0 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 ommit. */. void
5b9f0 20 2a 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20 *pUpdateArg;.
5ba00 76 6f 69 64 20 28 2a 78 55 70 64 61 74 65 43 61 void (*xUpdateCa
5ba10 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e llback)(void*,in
5ba20 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 t, const char*,c
5ba30 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 onst char*,sqlit
5ba40 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 e_int64);. void
5ba50 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 (*xCollNeeded)(v
5ba60 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e oid*,sqlite3*,in
5ba70 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 t eTextRep,const
5ba80 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 char*);. void(
5ba90 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 *xCollNeeded16)(
5baa0 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 void*,sqlite3*,i
5bab0 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 nt eTextRep,cons
5bac0 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 t void*);. void
5bad0 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 *pCollNeededArg
5bae0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 ;. sqlite3_valu
5baf0 65 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 e *pErr;
5bb00 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
5bb10 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a error message *
5bb20 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 /. char *zErrMs
5bb30 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
5bb40 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
5bb50 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 error message (
5bb60 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a UTF-8 encoded) *
5bb70 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 /. char *zErrMs
5bb80 67 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 g16;
5bb90 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 /* Most recent
5bba0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 error message (
5bbb0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 UTF-16 encoded)
5bbc0 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 */. union {.
5bbd0 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 volatile int is
5bbe0 49 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 Interrupted; /*
5bbf0 54 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f True if sqlite3_
5bc00 69 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65 interrupt has be
5bc10 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 en called */.
5bc20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 double notUsed1
5bc30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
5bc40 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 Spacer */. } u1
5bc50 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f ;. Lookaside lo
5bc60 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 okaside;
5bc70 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d /* Lookaside m
5bc80 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 alloc configurat
5bc90 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 ion */.#ifndef S
5bca0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f QLITE_OMIT_AUTHO
5bcb0 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 RIZATION. int (
5bcc0 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e *xAuth)(void*,in
5bcd0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f t,const char*,co
5bce0 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 nst char*,const
5bcf0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 char*,const char
5bd00 2a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 *);.
5bd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bd20 20 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75 /* Access au
5bd30 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 thorization func
5bd40 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a tion */. void *
5bd50 70 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20 pAuthArg;
5bd60 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 /* 1st a
5bd70 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 rgument to the a
5bd80 63 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74 ccess auth funct
5bd90 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 ion */.#endif.#i
5bda0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
5bdb0 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 T_PROGRESS_CALLB
5bdc0 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f ACK. int (*xPro
5bdd0 67 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 gress)(void *);
5bde0 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 /* The progr
5bdf0 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a ess callback */.
5be00 20 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 void *pProgres
5be10 73 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 sArg;
5be20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 /* Argument to t
5be30 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c he progress call
5be40 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 back */. int nP
5be50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 rogressOps;
5be60 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
5be70 72 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 r of opcodes for
5be80 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 progress callba
5be90 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 ck */.#endif.#if
5bea0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
5beb0 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
5bec0 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 Hash aModule;
5bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
5bee0 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71 populated by sq
5bef0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 lite3_create_mod
5bf00 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 ule() */. Table
5bf10 20 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20 *pVTab;
5bf20 20 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62 /* vtab
5bf30 20 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e with active Con
5bf40 6e 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68 nect/Create meth
5bf50 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a od */. VTable *
5bf60 2a 61 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 *aVTrans;
5bf70 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c /* Virtual
5bf80 20 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65 tables with ope
5bf90 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a n transactions *
5bfa0 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b /. int nVTrans;
5bfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5bfc0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 /* Allocated s
5bfd0 69 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a ize of aVTrans *
5bfe0 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 69 73 /. VTable *pDis
5bff0 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 connect; /* D
5c000 69 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 isconnect these
5c010 69 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 33 5f in next sqlite3_
5c020 70 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e prepare() */.#en
5c030 64 69 66 0a 20 20 46 75 6e 63 44 65 66 48 61 73 dif. FuncDefHas
5c040 68 20 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20 h aFunc;
5c050 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c /* Hash tabl
5c060 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 e of connection
5c070 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 functions */. H
5c080 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 ash aCollSeq;
5c090 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
5c0a0 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 All collating se
5c0b0 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 quences */. Bus
5c0c0 79 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e yHandler busyHan
5c0d0 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 dler; /* Bu
5c0e0 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 sy callback */.
5c0f0 20 69 6e 74 20 62 75 73 79 54 69 6d 65 6f 75 74 int busyTimeout
5c100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
5c110 2a 20 42 75 73 79 20 68 61 6e 64 6c 65 72 20 74 * Busy handler t
5c120 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 imeout, in msec
5c130 2a 2f 0a 20 20 44 62 20 61 44 62 53 74 61 74 69 */. Db aDbStati
5c140 63 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 c[2];
5c150 20 20 20 2f 2a 20 53 74 61 74 69 63 20 73 70 61 /* Static spa
5c160 63 65 20 66 6f 72 20 74 68 65 20 32 20 64 65 66 ce for the 2 def
5c170 61 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f ault backends */
5c180 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 . Savepoint *pS
5c190 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 avepoint;
5c1a0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 /* List of acti
5c1b0 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f ve savepoints */
5c1c0 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e . int nSavepoin
5c1d0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
5c1e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f /* Number of no
5c1f0 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 n-transaction sa
5c200 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e vepoints */. in
5c210 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 t nStatement;
5c220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
5c230 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 umber of nested
5c240 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 statement-transa
5c250 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 ctions */. u8
5c260 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 isTransactionSav
5c270 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 epoint; /* Tr
5c280 75 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d ue if the outerm
5c290 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 ost savepoint is
5c2a0 20 61 20 54 53 20 2a 2f 0a 20 20 69 36 34 20 6e a TS */. i64 n
5c2b0 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20 DeferredCons;
5c2c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 20 /* Net
5c2d0 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 deferred constra
5c2e0 69 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73 61 ints this transa
5c2f0 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 64 65 ction. */..#ifde
5c300 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
5c310 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 UNLOCK_NOTIFY.
5c320 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
5c330 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 variables are a
5c340 6c 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 ll protected by
5c350 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 the STATIC_MASTE
5c360 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e R . ** mutex, n
5c370 6f 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 ot by sqlite3.mu
5c380 74 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 tex. They are us
5c390 65 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f ed by code in no
5c3a0 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 tify.c. . **.
5c3b0 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 ** When X.pUnloc
5c3c0 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 kConnection==Y,
5c3d0 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 that means that
5c3e0 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 X is waiting for
5c3f0 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 Y to. ** unloc
5c400 6b 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e k so that it can
5c410 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 proceed.. **.
5c420 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 ** When X.pBloc
5c430 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d kingConnection==
5c440 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 Y, that means th
5c450 61 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 at something tha
5c460 74 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 t X tried. ** t
5c470 72 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 6e ried to do recen
5c480 74 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 20 tly failed with
5c490 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 an SQLITE_LOCKED
5c4a0 20 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f error due to lo
5c4b0 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 cks. ** held by
5c4c0 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 Y.. */. sqlit
5c4d0 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e e3 *pBlockingCon
5c4e0 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e nection; /* Conn
5c4f0 65 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 ection that caus
5c500 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 ed SQLITE_LOCKED
5c510 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 */. sqlite3 *p
5c520 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e UnlockConnection
5c530 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 ; /* C
5c540 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 onnection to wat
5c550 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f ch for unlock */
5c560 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b . void *pUnlock
5c570 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 Arg;
5c580 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 /* Argu
5c590 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e ment to xUnlockN
5c5a0 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 otify */. void
5c5b0 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 (*xUnlockNotify)
5c5c0 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 (void **, int);
5c5d0 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 /* Unlock notif
5c5e0 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 y callback */.
5c5f0 73 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c sqlite3 *pNextBl
5c600 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a ocked; /*
5c610 20 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 Next in list of
5c620 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e all blocked con
5c630 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 nections */.#end
5c640 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d if.};../*.** A m
5c650 61 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 acro to discover
5c660 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 the encoding of
5c670 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a a database..*/.
5c680 23 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 #define ENC(db)
5c690 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 ((db)->aDb[0].pS
5c6a0 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a chema->enc)../*.
5c6b0 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 ** Possible valu
5c6c0 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 es for the sqlit
5c6d0 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 e.flags and or D
5c6e0 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a b.flags fields..
5c6f0 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e **.** On sqlite.
5c700 66 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 flags, the SQLIT
5c710 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 E_InTrans value
5c720 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 means that we ha
5c730 76 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 ve.** executed a
5c740 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 BEGIN. On Db.f
5c750 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 lags, SQLITE_InT
5c760 72 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 rans means a sta
5c770 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 tement.** transa
5c780 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 ction is active
5c790 6f 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c on that particul
5c7a0 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ar database file
5c7b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5c7c0 49 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 ITE_VdbeTrace
5c7d0 20 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 0x00000001 /
5c7e0 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 * True to trace
5c7f0 56 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a VDBE execution *
5c800 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5c810 5f 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 _InTrans
5c820 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 0x00000008 /* T
5c830 72 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e rue if in a tran
5c840 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 saction */.#defi
5c850 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e ne SQLITE_Intern
5c860 43 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 Changes 0x00000
5c870 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 010 /* Uncommit
5c880 74 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 ted Hash table c
5c890 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e hanges */.#defin
5c8a0 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c e SQLITE_FullCol
5c8b0 4e 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 Names 0x000000
5c8c0 32 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20 /* Show full
5c8d0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e column names on
5c8e0 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 SELECT */.#defi
5c8f0 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 ne SQLITE_ShortC
5c900 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 olNames 0x00000
5c910 30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 040 /* Show sho
5c920 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 rt columns names
5c930 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5c940 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 TE_CountRows
5c950 20 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 0x00000080 /*
5c960 20 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e Count rows chan
5c970 67 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a ged by INSERT, *
5c980 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
5c990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c9a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 /*
5c9b0 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 DELETE, or UPDA
5c9c0 54 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f TE and return */
5c9d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
5c9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5c9f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 /*
5ca00 74 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 the count using
5ca10 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 a callback. */.#
5ca20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 define SQLITE_Nu
5ca30 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 llCallback 0x0
5ca40 30 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 0000100 /* Invo
5ca50 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 ke the callback
5ca60 6f 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 once if the */.
5ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ca80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5ca90 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 /* re
5caa0 73 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 sult set is empt
5cab0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c y */.#define SQL
5cac0 49 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 ITE_SqlTrace
5cad0 20 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 0x00000200 /
5cae0 2a 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 * Debug print SQ
5caf0 4c 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 L as it executes
5cb00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5cb10 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 TE_VdbeListing
5cb20 20 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 0x00000400 /*
5cb30 20 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 Debug listings
5cb40 6f 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 of VDBE programs
5cb50 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 */.#define SQLI
5cb60 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 TE_WriteSchema
5cb70 20 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 0x00000800 /*
5cb80 20 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 OK to update SQ
5cb90 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 LITE_MASTER */.#
5cba0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f define SQLITE_No
5cbb0 52 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 Readlock 0x0
5cbc0 30 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 0001000 /* Read
5cbd0 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 locks are omitte
5cbe0 64 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 d when .
5cbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5cc10 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 ** accessing r
5cc20 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 ead-only databas
5cc30 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 es */.#define SQ
5cc40 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b LITE_IgnoreCheck
5cc50 73 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 s 0x00002000
5cc60 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 /* Do not enforc
5cc70 65 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 e check constrai
5cc80 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 nts */.#define S
5cc90 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d QLITE_ReadUncomm
5cca0 69 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 itted 0x00004000
5ccb0 20 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 /* For shared-c
5ccc0 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 ache mode */.#de
5ccd0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 fine SQLITE_Lega
5cce0 63 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 cyFileFmt 0x000
5ccf0 30 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 08000 /* Create
5cd00 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 new databases i
5cd10 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 n format 1 */.#d
5cd20 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c efine SQLITE_Ful
5cd30 6c 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 lFSync 0x00
5cd40 30 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 010000 /* Use f
5cd50 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 ull fsync on the
5cd60 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 backend */.#def
5cd70 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 ine SQLITE_LoadE
5cd80 78 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 xtension 0x0002
5cd90 30 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 0000 /* Enable
5cda0 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a load_extension *
5cdb0 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 /..#define SQLIT
5cdc0 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 E_RecoveryMode
5cdd0 20 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 0x00040000 /*
5cde0 49 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 Ignore schema er
5cdf0 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 rors */.#define
5ce00 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 SQLITE_ReverseOr
5ce10 64 65 72 20 20 20 30 78 30 30 31 30 30 30 30 30 der 0x00100000
5ce20 20 20 2f 2a 20 52 65 76 65 72 73 65 20 75 6e 6f /* Reverse uno
5ce30 72 64 65 72 65 64 20 53 45 4c 45 43 54 73 20 2a rdered SELECTs *
5ce40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5ce50 5f 52 65 63 54 72 69 67 67 65 72 73 20 20 20 20 _RecTriggers
5ce60 30 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 45 0x00200000 /* E
5ce70 6e 61 62 6c 65 20 72 65 63 75 72 73 69 76 65 20 nable recursive
5ce80 74 72 69 67 67 65 72 73 20 2a 2f 0a 23 64 65 66 triggers */.#def
5ce90 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 ine SQLITE_Forei
5cea0 67 6e 4b 65 79 73 20 20 20 20 30 78 30 30 34 30 gnKeys 0x0040
5ceb0 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72 63 65 0000 /* Enforce
5cec0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e foreign key con
5ced0 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a 0a 2f 2a straints */../*
5cee0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c .** Possible val
5cef0 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 ues for the sqli
5cf00 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a te.magic field..
5cf10 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 20 61 ** The numbers a
5cf20 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74 20 72 re obtained at r
5cf30 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e andom and have n
5cf40 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e o special meanin
5cf50 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e g, other.** than
5cf60 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63 74 20 being distinct
5cf70 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 from one another
5cf80 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c ..*/.#define SQL
5cf90 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 ITE_MAGIC_OPEN
5cfa0 20 20 20 30 78 61 30 32 39 61 36 39 37 20 20 2f 0xa029a697 /
5cfb0 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 6f 70 * Database is op
5cfc0 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 en */.#define SQ
5cfd0 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 LITE_MAGIC_CLOSE
5cfe0 44 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20 D 0x9f3c2d33
5cff0 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63 /* Database is c
5d000 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 losed */.#define
5d010 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 SQLITE_MAGIC_SI
5d020 43 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39 CK 0x4b77129
5d030 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 0 /* Error and
5d040 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a awaiting close *
5d050 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5d060 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20 20 _MAGIC_BUSY
5d070 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20 44 0xf03b7906 /* D
5d080 61 74 61 62 61 73 65 20 63 75 72 72 65 6e 74 6c atabase currentl
5d090 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 y in use */.#def
5d0a0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 ine SQLITE_MAGIC
5d0b0 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35 33 35 _ERROR 0xb535
5d0c0 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 7930 /* An SQLI
5d0d0 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 20 TE_MISUSE error
5d0e0 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a occurred */../*.
5d0f0 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e 63 ** Each SQL func
5d100 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20 tion is defined
5d110 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f by an instance o
5d120 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a f the following.
5d130 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 ** structure. A
5d140 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 pointer to this
5d150 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 74 structure is st
5d160 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 ored in the sqli
5d170 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 te.aFunc.** hash
5d180 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 table. When mu
5d190 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 ltiple functions
5d1a0 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e have the same n
5d1b0 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74 61 ame, the hash ta
5d1c0 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f ble.** points to
5d1d0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
5d1e0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 f these structur
5d1f0 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 es..*/.struct Fu
5d200 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 ncDef {. i16 nA
5d210 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f rg; /
5d220 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 * Number of argu
5d230 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 ments. -1 means
5d240 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 unlimited */.
5d250 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20 20 u8 iPrefEnc;
5d260 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65 /* Preferre
5d270 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 d text encoding
5d280 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 (SQLITE_UTF8, 16
5d290 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 LE, 16BE) */. u
5d2a0 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 8 flags;
5d2b0 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 /* Some comb
5d2c0 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 ination of SQLIT
5d2d0 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f E_FUNC_* */. vo
5d2e0 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 id *pUserData;
5d2f0 20 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 /* User data
5d300 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 parameter */. F
5d310 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 uncDef *pNext;
5d320 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 /* Next func
5d330 74 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e tion with same n
5d340 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a ame */. void (*
5d350 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 xFunc)(sqlite3_c
5d360 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 ontext*,int,sqli
5d370 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a te3_value**); /*
5d380 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f Regular functio
5d390 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 n */. void (*xS
5d3a0 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e tep)(sqlite3_con
5d3b0 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 text*,int,sqlite
5d3c0 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 3_value**); /* A
5d3d0 67 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f ggregate step */
5d3e0 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c . void (*xFinal
5d3f0 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e ize)(sqlite3_con
5d400 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 text*);
5d410 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 /* Aggreg
5d420 61 74 65 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f ate finalizer */
5d430 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 . char *zName;
5d440 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e /* SQL n
5d450 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ame of the funct
5d460 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 ion. */. FuncDe
5d470 66 20 2a 70 48 61 73 68 3b 20 20 20 20 20 20 2f f *pHash; /
5d480 2a 20 4e 65 78 74 20 77 69 74 68 20 61 20 64 69 * Next with a di
5d490 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 fferent name but
5d4a0 20 74 68 65 20 73 61 6d 65 20 68 61 73 68 20 2a the same hash *
5d4b0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 /.};../*.** Poss
5d4c0 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 ible values for
5d4d0 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f FuncDef.flags.*/
5d4e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f .#define SQLITE_
5d4f0 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 FUNC_LIKE 0x
5d500 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 01 /* Candidate
5d510 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 for the LIKE opt
5d520 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 imization */.#de
5d530 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 fine SQLITE_FUNC
5d540 5f 43 41 53 45 20 20 20 20 20 30 78 30 32 20 2f _CASE 0x02 /
5d550 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76 65 * Case-sensitive
5d560 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 LIKE-type funct
5d570 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ion */.#define S
5d580 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d QLITE_FUNC_EPHEM
5d590 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70 68 65 0x04 /* Ephe
5d5a0 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 meral. Delete w
5d5b0 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 ith VDBE */.#def
5d5c0 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f ine SQLITE_FUNC_
5d5d0 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a NEEDCOLL 0x08 /*
5d5e0 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 sqlite3GetFuncC
5d5f0 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62 ollSeq() might b
5d600 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 e called */.#def
5d610 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f ine SQLITE_FUNC_
5d620 50 52 49 56 41 54 45 20 20 30 78 31 30 20 2f 2a PRIVATE 0x10 /*
5d630 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 Allowed for int
5d640 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a ernal use only *
5d650 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5d660 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20 20 20 30 _FUNC_COUNT 0
5d670 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 x20 /* Built-in
5d680 63 6f 75 6e 74 28 2a 29 20 61 67 67 72 65 67 61 count(*) aggrega
5d690 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 te */.#define SQ
5d6a0 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 LITE_FUNC_COALES
5d6b0 43 45 20 30 78 34 30 20 2f 2a 20 42 75 69 6c 74 CE 0x40 /* Built
5d6c0 2d 69 6e 20 63 6f 61 6c 65 73 63 65 28 29 20 6f -in coalesce() o
5d6d0 72 20 69 66 6e 75 6c 6c 28 29 20 66 75 6e 63 74 r ifnull() funct
5d6e0 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 ion */../*.** Th
5d6f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 e following thre
5d700 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54 49 e macros, FUNCTI
5d710 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28 29 ON(), LIKEFUNC()
5d720 20 61 6e 64 20 41 47 47 52 45 47 41 54 45 28 29 and AGGREGATE()
5d730 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 are.** used to
5d740 63 72 65 61 74 65 20 74 68 65 20 69 6e 69 74 69 create the initi
5d750 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68 65 20 alizers for the
5d760 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 FuncDef structur
5d770 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e 43 es..**.** FUNC
5d780 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 TION(zName, nArg
5d790 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 , iArg, bNC, xFu
5d7a0 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 nc).** Used
5d7b0 74 6f 20 63 72 65 61 74 65 20 61 20 73 63 61 6c to create a scal
5d7c0 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 ar function defi
5d7d0 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 nition of a func
5d7e0 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 tion zName .**
5d7f0 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 implemented b
5d800 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78 46 75 y C function xFu
5d810 6e 63 20 74 68 61 74 20 61 63 63 65 70 74 73 20 nc that accepts
5d820 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 2e 20 nArg arguments.
5d830 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65 The.** value
5d840 20 70 61 73 73 65 64 20 61 73 20 69 41 72 67 20 passed as iArg
5d850 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f is cast to a (vo
5d860 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20 61 76 id*) and made av
5d870 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20 61 ailable.** a
5d880 73 20 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 s the user-data
5d890 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 (sqlite3_user_da
5d8a0 74 61 28 29 29 20 66 6f 72 20 74 68 65 20 66 75 ta()) for the fu
5d8b0 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 20 nction. If .**
5d8c0 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e 43 20 argument bNC
5d8d0 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 is true, then th
5d8e0 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 e SQLITE_FUNC_NE
5d8f0 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73 20 73 EDCOLL flag is s
5d900 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52 et..**.** AGGR
5d910 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 EGATE(zName, nAr
5d920 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 53 g, iArg, bNC, xS
5d930 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20 tep, xFinal).**
5d940 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 Used to crea
5d950 74 65 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 te an aggregate
5d960 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 function definit
5d970 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 ion implemented
5d980 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 20 by.** the C
5d990 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 20 functions xStep
5d9a0 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 20 and xFinal. The
5d9b0 66 69 72 73 74 20 66 6f 75 72 20 70 61 72 61 6d first four param
5d9c0 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 65 eters.** are
5d9d0 20 69 6e 74 65 72 70 72 65 74 65 64 20 69 6e 20 interpreted in
5d9e0 74 68 65 20 73 61 6d 65 20 77 61 79 20 61 73 20 the same way as
5d9f0 74 68 65 20 66 69 72 73 74 20 34 20 70 61 72 61 the first 4 para
5da00 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 meters to.**
5da10 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a FUNCTION()..**.
5da20 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e ** LIKEFUNC(zN
5da30 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c ame, nArg, pArg,
5da40 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 55 flags).** U
5da50 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 sed to create a
5da60 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 scalar function
5da70 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 definition of a
5da80 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a function zName .
5da90 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63 63 65 ** that acce
5daa0 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e pts nArg argumen
5dab0 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d ts and is implem
5dac0 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20 ented by a call
5dad0 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e to C .** fun
5dae0 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 ction likeFunc.
5daf0 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73 Argument pArg is
5db00 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 cast to a (void
5db10 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20 *) and made.**
5db20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73 available as
5db30 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 the function us
5db40 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 er-data (sqlite3
5db50 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54 _user_data()). T
5db60 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 65 he.** FuncDe
5db70 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65 f.flags variable
5db80 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 is set to the v
5db90 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 alue passed as t
5dba0 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20 he flags.**
5dbb0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64 parameter..*/.#d
5dbc0 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a efine FUNCTION(z
5dbd0 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 Name, nArg, iArg
5dbe0 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a , bNC, xFunc) \.
5dbf0 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f {nArg, SQLITE_
5dc00 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 UTF8, bNC*SQLITE
5dc10 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 _FUNC_NEEDCOLL,
5dc20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f \. SQLITE_INT_
5dc30 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c TO_PTR(iArg), 0,
5dc40 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a xFunc, 0, 0, #z
5dc50 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 Name, 0}.#define
5dc60 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e STR_FUNCTION(zN
5dc70 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c ame, nArg, pArg,
5dc80 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 bNC, xFunc) \.
5dc90 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 {nArg, SQLITE_U
5dca0 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f TF8, bNC*SQLITE_
5dcb0 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c FUNC_NEEDCOLL, \
5dcc0 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 78 46 75 . pArg, 0, xFu
5dcd0 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 nc, 0, 0, #zName
5dce0 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c 49 4b , 0}.#define LIK
5dcf0 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 EFUNC(zName, nAr
5dd00 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29 20 5c g, arg, flags) \
5dd10 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 . {nArg, SQLITE
5dd20 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20 28 76 _UTF8, flags, (v
5dd30 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c 69 oid *)arg, 0, li
5dd40 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a keFunc, 0, 0, #z
5dd50 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 Name, 0}.#define
5dd60 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 AGGREGATE(zName
5dd70 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 6e 63 2c , nArg, arg, nc,
5dd80 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 20 xStep, xFinal)
5dd90 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 \. {nArg, SQLIT
5dda0 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 4c 49 54 E_UTF8, nc*SQLIT
5ddb0 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c E_FUNC_NEEDCOLL,
5ddc0 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 \. SQLITE_INT
5ddd0 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c _TO_PTR(arg), 0,
5dde0 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 0, xStep,xFinal
5ddf0 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a ,#zName,0}../*.*
5de00 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 * All current sa
5de10 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f vepoints are sto
5de20 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 red in a linked
5de30 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 list starting at
5de40 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 .** sqlite3.pSav
5de50 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 epoint. The firs
5de60 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 t element in the
5de70 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 list is the mos
5de80 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 t recently.** op
5de90 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 ened savepoint.
5dea0 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 Savepoints are a
5deb0 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 dded to the list
5dec0 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 by the vdbe.**
5ded0 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 OP_Savepoint ins
5dee0 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 truction..*/.str
5def0 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a uct Savepoint {.
5df00 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
5df10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5df20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 /* Savepoi
5df30 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 nt name (nul-ter
5df40 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20 69 36 minated) */. i6
5df50 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 4 nDeferredCons;
5df60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5df70 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 /* Number of d
5df80 65 66 65 72 72 65 64 20 66 6b 20 76 69 6f 6c 61 eferred fk viola
5df90 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 65 70 tions */. Savep
5dfa0 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 oint *pNext;
5dfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5dfc0 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70 6f 69 * Parent savepoi
5dfd0 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a 7d nt (if any) */.}
5dfe0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
5dff0 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65 64 20 lowing are used
5e000 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 as the second pa
5e010 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 rameter to sqlit
5e020 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a 2a e3Savepoint(),.*
5e030 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50 31 20 * and as the P1
5e040 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
5e050 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 OP_Savepoint ins
5e060 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 truction..*/.#de
5e070 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 42 fine SAVEPOINT_B
5e080 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64 65 66 EGIN 0.#def
5e090 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 ine SAVEPOINT_RE
5e0a0 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65 66 69 LEASE 1.#defi
5e0b0 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c ne SAVEPOINT_ROL
5e0c0 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a LBACK 2.../*.*
5e0d0 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f * Each SQLite mo
5e0e0 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 dule (virtual ta
5e0f0 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 ble definition)
5e100 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e is defined by an
5e110 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 .** instance of
5e120 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 the following st
5e130 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 ructure, stored
5e140 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 in the sqlite3.a
5e150 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 Module.** hash t
5e160 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 able..*/.struct
5e170 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 Module {. const
5e180 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
5e190 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 *pModule;
5e1a0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e /* Callback poin
5e1b0 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ters */. const
5e1c0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
5e1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5e1e0 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f * Name passed to
5e1f0 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 create_module()
5e200 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 */. void *pAux
5e210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
5e220 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 /* pA
5e230 75 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 ux passed to cre
5e240 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a ate_module() */.
5e250 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f void (*xDestro
5e260 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 y)(void *);
5e270 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 /* Module
5e280 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 destructor func
5e290 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a tion */.};../*.*
5e2a0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 * information ab
5e2b0 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 out each column
5e2c0 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 of an SQL table
5e2d0 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e is held in an in
5e2e0 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 stance.** of thi
5e2f0 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a s structure..*/.
5e300 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a struct Column {.
5e310 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
5e320 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
5e330 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 is column */. E
5e340 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 xpr *pDflt;
5e350 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 /* Default value
5e360 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 of this column
5e370 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 */. char *zDflt
5e380 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 ; /* Origina
5e390 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 64 65 l text of the de
5e3a0 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 20 fault value */.
5e3b0 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 char *zType;
5e3c0 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20 66 /* Data type f
5e3d0 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a or this column *
5e3e0 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b /. char *zColl;
5e3f0 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e /* Collatin
5e400 67 20 73 65 71 75 65 6e 63 65 2e 20 20 49 66 20 g sequence. If
5e410 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 20 64 65 NULL, use the de
5e420 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6e 6f fault */. u8 no
5e430 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 54 tNull; /* T
5e440 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20 rue if there is
5e450 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 a NOT NULL const
5e460 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 20 69 73 raint */. u8 is
5e470 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 54 PrimKey; /* T
5e480 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75 rue if this colu
5e490 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 mn is part of th
5e4a0 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f e PRIMARY KEY */
5e4b0 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 . char affinity
5e4c0 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 ; /* One of th
5e4d0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e e SQLITE_AFF_...
5e4e0 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 66 6e 64 values */.#ifnd
5e4f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
5e500 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 38 IRTUALTABLE. u8
5e510 20 69 73 48 69 64 64 65 6e 3b 20 20 20 20 20 2f isHidden; /
5e520 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 * True if this c
5e530 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 64 65 6e olumn is 'hidden
5e540 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a ' */.#endif.};..
5e550 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69 /*.** A "Collati
5e560 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69 73 20 ng Sequence" is
5e570 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e defined by an in
5e580 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
5e590 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 llowing.** struc
5e5a0 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c ture. Conceptual
5e5b0 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 ly, a collating
5e5c0 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 74 sequence consist
5e5d0 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 0a s of a name and.
5e5e0 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 ** a comparison
5e5f0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 65 66 routine that def
5e600 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72 20 6f ines the order o
5e610 66 20 74 68 61 74 20 73 65 71 75 65 6e 63 65 2e f that sequence.
5e620 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 .**.** There may
5e630 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 69 6d two separate im
5e640 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 plementations of
5e650 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 the collation f
5e660 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 unction, one.**
5e670 74 68 61 74 20 70 72 6f 63 65 73 73 65 73 20 74 that processes t
5e680 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 ext in UTF-8 enc
5e690 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e 78 oding (CollSeq.x
5e6a0 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68 65 72 Cmp) and another
5e6b0 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73 73 that.** process
5e6c0 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65 64 20 es text encoded
5e6d0 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c 53 in UTF-16 (CollS
5e6e0 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73 69 6e eq.xCmp16), usin
5e6f0 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a 2a g the machine.**
5e700 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 native byte ord
5e710 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c 61 er. When a colla
5e720 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 tion sequence is
5e730 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74 65 invoked, SQLite
5e740 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65 20 selects.** the
5e750 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77 69 6c version that wil
5e760 6c 20 72 65 71 75 69 72 65 20 74 68 65 20 6c 65 l require the le
5e770 61 73 74 20 65 78 70 65 6e 73 69 76 65 20 65 6e ast expensive en
5e780 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c coding.** transl
5e790 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e 0a ations, if any..
5e7a0 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 65 **.** The CollSe
5e7b0 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72 20 76 q.pUser member v
5e7c0 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20 65 78 ariable is an ex
5e7d0 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74 68 tra parameter th
5e7e0 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20 at passed in.**
5e7f0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 as the first arg
5e800 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55 54 46 ument to the UTF
5e810 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 -8 comparison fu
5e820 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a nction, xCmp..**
5e830 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31 36 CollSeq.pUser16
5e840 20 69 73 20 74 68 65 20 65 71 75 69 76 61 6c 65 is the equivale
5e850 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46 2d 31 nt for the UTF-1
5e860 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 6 comparison fun
5e870 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 ction,.** xCmp16
5e880 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 ..**.** If both
5e890 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64 CollSeq.xCmp and
5e8a0 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20 CollSeq.xCmp16
5e8b0 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 are NULL, it mea
5e8c0 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 ns that the.** c
5e8d0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
5e8e0 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 e is undefined.
5e8f0 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74 20 6f Indices built o
5e900 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a n an undefined.*
5e910 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 * collating sequ
5e920 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 ence may not be
5e930 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e read or written.
5e940 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c 53 .*/.struct CollS
5e950 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 eq {. char *zNa
5e960 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 me; /*
5e970 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c Name of the coll
5e980 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c 20 ating sequence,
5e990 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f UTF-8 encoded */
5e9a0 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20 . u8 enc;
5e9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 /* Text
5e9c0 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c 65 encoding handle
5e9d0 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a 20 d by xCmp() */.
5e9e0 20 75 38 20 74 79 70 65 3b 20 20 20 20 20 20 20 u8 type;
5e9f0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
5ea00 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c the SQLITE_COLL
5ea10 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 65 6c 6f _... values belo
5ea20 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 w */. void *pUs
5ea30 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 er; /*
5ea40 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 First argument t
5ea50 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 69 6e o xCmp() */. in
5ea60 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c t (*xCmp)(void*,
5ea70 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a int, const void*
5ea80 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 , int, const voi
5ea90 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 d*);. void (*xD
5eaa0 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 el)(void*); /*
5eab0 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 70 Destructor for p
5eac0 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a User */.};../*.*
5ead0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 * Allowed values
5eae0 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 79 70 65 of CollSeq.type
5eaf0 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c :.*/.#define SQL
5eb00 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 ITE_COLL_BINARY
5eb10 20 31 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 1 /* The defau
5eb20 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 6f 6c 6c lt memcmp() coll
5eb30 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a ating sequence *
5eb40 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5eb50 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 20 _COLL_NOCASE 2
5eb60 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e /* The built-in
5eb70 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e NOCASE collatin
5eb80 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 g sequence */.#d
5eb90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c efine SQLITE_COL
5eba0 4c 5f 52 45 56 45 52 53 45 20 33 20 20 2f 2a 20 L_REVERSE 3 /*
5ebb0 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 52 45 56 The built-in REV
5ebc0 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 ERSE collating s
5ebd0 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 equence */.#defi
5ebe0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 ne SQLITE_COLL_U
5ebf0 53 45 52 20 20 20 20 30 20 20 2f 2a 20 41 6e 79 SER 0 /* Any
5ec00 20 6f 74 68 65 72 20 75 73 65 72 2d 64 65 66 69 other user-defi
5ec10 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 ned collating se
5ec20 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a quence */../*.**
5ec30 20 41 20 73 6f 72 74 20 6f 72 64 65 72 20 63 61 A sort order ca
5ec40 6e 20 62 65 20 65 69 74 68 65 72 20 41 53 43 20 n be either ASC
5ec50 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 or DESC..*/.#def
5ec60 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 ine SQLITE_SO_AS
5ec70 43 20 20 20 20 20 20 20 30 20 20 2f 2a 20 53 6f C 0 /* So
5ec80 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 rt in ascending
5ec90 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 order */.#define
5eca0 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 SQLITE_SO_DESC
5ecb0 20 20 20 20 20 31 20 20 2f 2a 20 53 6f 72 74 20 1 /* Sort
5ecc0 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 in ascending ord
5ecd0 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c er */../*.** Col
5ece0 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 79 70 umn affinity typ
5ecf0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 es..**.** These
5ed00 75 73 65 64 20 74 6f 20 68 61 76 65 20 6d 6e 65 used to have mne
5ed10 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20 monic name like
5ed20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 'i' for SQLITE_A
5ed30 46 46 5f 49 4e 54 45 47 45 52 20 61 6e 64 0a 2a FF_INTEGER and.*
5ed40 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49 54 45 * 't' for SQLITE
5ed50 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75 74 20 _AFF_TEXT. But
5ed60 77 65 20 63 61 6e 20 73 61 76 65 20 61 20 6c 69 we can save a li
5ed70 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64 20 69 ttle space and i
5ed80 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 20 73 70 mprove.** the sp
5ed90 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62 79 20 eed a little by
5eda0 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20 76 61 numbering the va
5edb0 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69 76 65 lues consecutive
5edc0 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 ly. .**.** But
5edd0 72 61 74 68 65 72 20 74 68 61 6e 20 73 74 61 72 rather than star
5ede0 74 20 77 69 74 68 20 30 20 6f 72 20 31 2c 20 77 t with 0 or 1, w
5edf0 65 20 62 65 67 69 6e 20 77 69 74 68 20 27 61 27 e begin with 'a'
5ee00 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 . That way,.**
5ee10 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20 61 66 when multiple af
5ee20 66 69 6e 69 74 79 20 74 79 70 65 73 20 61 72 65 finity types are
5ee30 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 69 6e concatenated in
5ee40 74 6f 20 61 20 73 74 72 69 6e 67 20 61 6e 64 0a to a string and.
5ee50 2a 2a 20 75 73 65 64 20 61 73 20 74 68 65 20 50 ** used as the P
5ee60 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 65 79 20 4 operand, they
5ee70 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 72 65 61 will be more rea
5ee80 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 dable..**.** Not
5ee90 65 20 61 6c 73 6f 20 74 68 61 74 20 74 68 65 20 e also that the
5eea0 6e 75 6d 65 72 69 63 20 74 79 70 65 73 20 61 72 numeric types ar
5eeb0 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 e grouped togeth
5eec0 65 72 20 73 6f 20 74 68 61 74 20 74 65 73 74 69 er so that testi
5eed0 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 6d 65 ng.** for a nume
5eee0 72 69 63 20 74 79 70 65 20 69 73 20 61 20 73 69 ric type is a si
5eef0 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e ngle comparison.
5ef00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 .*/.#define SQLI
5ef10 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20 20 TE_AFF_TEXT
5ef20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 'a'.#define SQLI
5ef30 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 20 TE_AFF_NONE
5ef40 27 62 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 'b'.#define SQLI
5ef50 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 20 TE_AFF_NUMERIC
5ef60 27 63 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 'c'.#define SQLI
5ef70 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 20 TE_AFF_INTEGER
5ef80 27 64 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 'd'.#define SQLI
5ef90 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 20 TE_AFF_REAL
5efa0 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 'e'..#define sql
5efb0 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 ite3IsNumericAff
5efc0 69 6e 69 74 79 28 58 29 20 20 28 28 58 29 3e 3d inity(X) ((X)>=
5efd0 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 SQLITE_AFF_NUMER
5efe0 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 IC)../*.** The S
5eff0 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 76 QLITE_AFF_MASK v
5f000 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f 66 66 20 alues masks off
5f010 74 68 65 20 73 69 67 6e 69 66 69 63 61 6e 74 20 the significant
5f020 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 bits of an.** af
5f030 66 69 6e 69 74 79 20 76 61 6c 75 65 2e 20 0a 2a finity value. .*
5f040 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 /.#define SQLITE
5f050 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 20 30 78 _AFF_MASK 0x
5f060 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 67../*.** Additi
5f070 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 65 73 20 onal bit values
5f080 74 68 61 74 20 63 61 6e 20 62 65 20 4f 52 65 64 that can be ORed
5f090 20 77 69 74 68 20 61 6e 20 61 66 66 69 6e 69 74 with an affinit
5f0a0 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 63 68 61 y without.** cha
5f0b0 6e 67 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 nging the affini
5f0c0 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ty..*/.#define S
5f0d0 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
5f0e0 20 20 20 30 78 30 38 20 20 2f 2a 20 6a 75 6d 70 0x08 /* jump
5f0f0 73 20 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 s if either oper
5f100 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 and is NULL */.#
5f110 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 define SQLITE_ST
5f120 4f 52 45 50 32 20 20 20 20 20 20 30 78 31 30 20 OREP2 0x10
5f130 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 /* Store result
5f140 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61 74 68 in reg[P2] rath
5f150 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a er than jump */.
5f160 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e #define SQLITE_N
5f170 55 4c 4c 45 51 20 20 20 20 20 20 20 30 78 38 30 ULLEQ 0x80
5f180 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a /* NULL=NULL *
5f190 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 /../*.** An obje
5f1a0 63 74 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 ct of this type
5f1b0 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 65 is created for e
5f1c0 61 63 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c ach virtual tabl
5f1d0 65 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 e present in.**
5f1e0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 the database sch
5f1f0 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ema. .**.** If t
5f200 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 he database sche
5f210 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 74 68 ma is shared, th
5f220 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 en there is one
5f230 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
5f240 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 66 6f .** structure fo
5f250 72 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 r each database
5f260 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 71 6c 69 connection (sqli
5f270 74 65 33 2a 29 20 74 68 61 74 20 75 73 65 73 20 te3*) that uses
5f280 74 68 65 20 73 68 61 72 65 64 0a 2a 2a 20 73 63 the shared.** sc
5f290 68 65 6d 61 2e 20 54 68 69 73 20 69 73 20 62 65 hema. This is be
5f2a0 63 61 75 73 65 20 65 61 63 68 20 64 61 74 61 62 cause each datab
5f2b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 ase connection r
5f2c0 65 71 75 69 72 65 73 20 69 74 73 20 6f 77 6e 20 equires its own
5f2d0 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 74 61 6e unique.** instan
5f2e0 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 ce of the sqlite
5f2f0 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 75 3_vtab* handle u
5f300 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 sed to access th
5f310 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 e virtual table
5f320 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 .** implementati
5f330 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 on. sqlite3_vtab
5f340 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e 20 6e 6f * handles can no
5f350 74 20 62 65 20 73 68 61 72 65 64 20 62 65 74 77 t be shared betw
5f360 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 een .** database
5f370 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 76 connections, ev
5f380 65 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 74 en when the rest
5f390 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 of the in-memor
5f3a0 79 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 73 y database .** s
5f3b0 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c chema is shared,
5f3c0 20 61 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e as the implemen
5f3d0 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 73 74 6f tation often sto
5f3e0 72 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 res the database
5f3f0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 .** connection h
5f400 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 6f 20 andle passed to
5f410 69 74 20 76 69 61 20 74 68 65 20 78 43 6f 6e 6e it via the xConn
5f420 65 63 74 28 29 20 6f 72 20 78 43 72 65 61 74 65 ect() or xCreate
5f430 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 72 () method.** dur
5f440 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ing initializati
5f450 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 on internally. T
5f460 68 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e his database con
5f470 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 6d nection handle m
5f480 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 65 64 20 ay.** then used
5f490 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 by the virtual t
5f4a0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 able implementat
5f4b0 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65 ion to access re
5f4c0 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 al tables .** wi
5f4d0 74 68 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 thin the databas
5f4e0 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65 79 20 e. So that they
5f4f0 61 70 70 65 61 72 20 61 73 20 70 61 72 74 20 6f appear as part o
5f500 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a f the callers .*
5f510 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 * transaction, t
5f520 68 65 73 65 20 61 63 63 65 73 73 65 73 20 6e 65 hese accesses ne
5f530 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20 76 69 ed to be made vi
5f540 61 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 a the same datab
5f550 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 ase .** connecti
5f560 6f 6e 20 61 73 20 74 68 61 74 20 75 73 65 64 20 on as that used
5f570 74 6f 20 65 78 65 63 75 74 65 20 53 51 4c 20 6f to execute SQL o
5f580 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 perations on the
5f590 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a virtual table..
5f5a0 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 **.** All VTable
5f5b0 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 63 6f objects that co
5f5c0 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 rrespond to a si
5f5d0 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 20 ngle table in a
5f5e0 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 shared.** databa
5f5f0 73 65 20 73 63 68 65 6d 61 20 61 72 65 20 69 6e se schema are in
5f600 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 itially stored i
5f610 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 n a linked-list
5f620 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a pointed to by.**
5f630 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 the Table.pVTab
5f640 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 le member variab
5f650 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 le of the corres
5f660 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 ponding Table ob
5f670 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e ject..** When an
5f680 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 sqlite3_prepare
5f690 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 () operation is
5f6a0 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 required to acce
5f6b0 73 73 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a ss the virtual.*
5f6c0 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 65 61 72 * table, it sear
5f6d0 63 68 65 73 20 74 68 65 20 6c 69 73 74 20 66 6f ches the list fo
5f6e0 72 20 74 68 65 20 56 54 61 62 6c 65 20 74 68 61 r the VTable tha
5f6f0 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f t corresponds to
5f700 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
5f710 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e connection doin
5f720 67 20 74 68 65 20 70 72 65 70 61 72 69 6e 67 20 g the preparing
5f730 73 6f 20 61 73 20 74 6f 20 75 73 65 20 74 68 65 so as to use the
5f740 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 correct.** sqli
5f750 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 te3_vtab* handle
5f760 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 in the compiled
5f770 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 query..**.** Wh
5f780 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 en an in-memory
5f790 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 Table object is
5f7a0 64 65 6c 65 74 65 64 20 28 66 6f 72 20 65 78 61 deleted (for exa
5f7b0 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a mple when the.**
5f7c0 20 73 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67 schema is being
5f7d0 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f reloaded for so
5f7e0 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 me reason), the
5f7f0 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 VTable objects a
5f800 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 re not .** delet
5f810 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 ed and the sqlit
5f820 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 e3_vtab* handles
5f830 20 61 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e are not xDiscon
5f840 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d nect()ed .** imm
5f850 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 ediately. Instea
5f860 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65 d, they are move
5f870 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 d from the Table
5f880 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f .pVTable list to
5f890 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b .** another link
5f8a0 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 ed list headed b
5f8b0 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 y the sqlite3.pD
5f8c0 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 isconnect member
5f8d0 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 of the.** corre
5f8e0 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 sponding sqlite3
5f8f0 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 79 structure. They
5f900 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65 74 65 are then delete
5f910 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 d/xDisconnected
5f920 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 20 .** next time a
5f930 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70 72 65 statement is pre
5f940 70 61 72 65 64 20 75 73 69 6e 67 20 73 61 69 64 pared using said
5f950 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 sqlite3*. This
5f960 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 is done.** to av
5f970 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 oid deadlock iss
5f980 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 ues involving mu
5f990 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d ltiple sqlite3.m
5f9a0 75 74 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a utex mutexes..**
5f9b0 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e Refer to commen
5f9c0 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f ts above functio
5f9d0 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c n sqlite3VtabUnl
5f9e0 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e ockList() for an
5f9f0 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 .** explanation
5fa00 61 73 20 74 6f 20 77 68 79 20 69 74 20 69 73 20 as to why it is
5fa10 73 61 66 65 20 74 6f 20 61 64 64 20 61 6e 20 65 safe to add an e
5fa20 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 ntry to an sqlit
5fa30 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a e3.pDisconnect.*
5fa40 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 74 20 68 * list without h
5fa50 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f 72 72 65 olding the corre
5fa60 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 sponding sqlite3
5fa70 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a .mutex mutex..**
5fa80 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 .** The memory f
5fa90 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 or objects of th
5faa0 69 73 20 74 79 70 65 20 69 73 20 61 6c 77 61 79 is type is alway
5fab0 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a s allocated by .
5fac0 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c ** sqlite3DbMall
5fad0 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 68 65 20 oc(), using the
5fae0 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
5faf0 65 20 73 74 6f 72 65 64 20 69 6e 20 56 54 61 62 e stored in VTab
5fb00 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 le.db as .** the
5fb10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e first argument.
5fb20 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 61 62 6c .*/.struct VTabl
5fb30 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 e {. sqlite3 *d
5fb40 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
5fb50 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e /* Database conn
5fb60 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 ection associate
5fb70 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c d with this tabl
5fb80 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 e */. Module *p
5fb90 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mod;
5fba0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d /* Pointer to m
5fbb0 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 odule implementa
5fbc0 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 tion */. sqlite
5fbd0 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 3_vtab *pVtab;
5fbe0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
5fbf0 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20 o vtab instance
5fc00 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 */. int nRef;
5fc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
5fc20 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e * Number of poin
5fc30 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 ters to this str
5fc40 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 54 61 62 ucture */. VTab
5fc50 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 le *pNext;
5fc60 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e /* Next in
5fc70 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 73 65 linked list (se
5fc80 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a 0a e above) */.};..
5fc90 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 74 /*.** Each SQL t
5fca0 61 62 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e able is represen
5fcb0 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 ted in memory by
5fcc0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
5fcd0 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 the.** following
5fce0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
5fcf0 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73 * Table.zName is
5fd00 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
5fd10 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63 61 73 table. The cas
5fd20 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 e of the origina
5fd30 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c l.** CREATE TABL
5fd40 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73 E statement is s
5fd50 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73 65 20 tored, but case
5fd60 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69 63 61 is not significa
5fd70 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 72 nt for.** compar
5fd80 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 isons..**.** Tab
5fd90 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e le.nCol is the n
5fda0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
5fdb0 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 in this table.
5fdc0 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73 20 61 Table.aCol is a
5fdd0 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 .** pointer to a
5fde0 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c 75 6d n array of Colum
5fdf0 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20 6f 6e n structures, on
5fe00 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d e for each colum
5fe10 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 n..**.** If the
5fe20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 table has an INT
5fe30 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
5fe40 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69 50 4b , then Table.iPK
5fe50 65 79 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 ey is the index
5fe60 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e of.** the column
5fe70 20 74 68 61 74 20 69 73 20 74 68 61 74 20 6b 65 that is that ke
5fe80 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65 20 54 y. Otherwise T
5fe90 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20 6e 65 able.iPKey is ne
5fea0 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a 2a 2a gative. Note.**
5feb0 20 74 68 61 74 20 74 68 65 20 64 61 74 61 74 79 that the dataty
5fec0 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 pe of the PRIMAR
5fed0 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 49 4e Y KEY must be IN
5fee0 54 45 47 45 52 20 66 6f 72 20 74 68 69 73 20 66 TEGER for this f
5fef0 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 73 65 ield to.** be se
5ff00 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52 20 50 t. An INTEGER P
5ff10 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 75 73 RIMARY KEY is us
5ff20 65 64 20 61 73 20 74 68 65 20 72 6f 77 69 64 20 ed as the rowid
5ff30 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66 0a for each row of.
5ff40 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 ** the table. I
5ff50 66 20 61 20 74 61 62 6c 65 20 68 61 73 20 6e 6f f a table has no
5ff60 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
5ff70 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72 61 6e KEY, then a ran
5ff80 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 73 20 dom rowid.** is
5ff90 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 65 61 generated for ea
5ffa0 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20 74 61 ch row of the ta
5ffb0 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72 69 6d ble. TF_HasPrim
5ffc0 61 72 79 4b 65 79 20 69 73 20 73 65 74 20 69 66 aryKey is set if
5ffd0 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 .** the table ha
5ffe0 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20 4b 45 s any PRIMARY KE
5fff0 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20 6f 74 Y, INTEGER or ot
60000 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 herwise..**.** T
60010 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74 68 65 able.tnum is the
60020 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 page number for
60030 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65 65 20 the root BTree
60040 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c page of the tabl
60050 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 e in the.** data
60060 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 54 base file. If T
60070 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68 65 20 able.iDb is the
60080 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 index of the dat
60090 61 62 61 73 65 20 74 61 62 6c 65 20 62 61 63 6b abase table back
600a0 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 end.** in sqlite
600b0 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f .aDb[]. 0 is fo
600c0 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
600d0 61 73 65 20 61 6e 64 20 31 20 69 73 20 66 6f 72 ase and 1 is for
600e0 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 0a 2a the file that.*
600f0 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72 * holds temporar
60100 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 y tables and ind
60110 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45 70 68 ices. If TF_Eph
60120 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a 2a 2a emeral is set.**
60130 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 then the table
60140 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 66 is stored in a f
60150 69 6c 65 20 74 68 61 74 20 69 73 20 61 75 74 6f ile that is auto
60160 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 matically delete
60170 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 56 44 d.** when the VD
60180 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 BE cursor to the
60190 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 table is closed
601a0 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 . In this case
601b0 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 Table.tnum .** r
601c0 65 66 65 72 73 20 56 44 42 45 20 63 75 72 73 6f efers VDBE curso
601d0 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20 68 6f r number that ho
601e0 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20 6f 70 lds the table op
601f0 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 en, not to the r
60200 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 oot.** page numb
60210 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74 20 74 er. Transient t
60220 61 62 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 ables are used t
60230 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c o hold the resul
60240 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 ts of a.** sub-q
60250 75 65 72 79 20 74 68 61 74 20 61 70 70 65 61 72 uery that appear
60260 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 72 s instead of a r
60270 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 eal table name i
60280 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
60290 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 e .** of a SELEC
602a0 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a T statement..*/.
602b0 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 struct Table {.
602c0 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d 3b sqlite3 *dbMem;
602d0 20 20 20 20 20 20 2f 2a 20 44 42 20 63 6f 6e 6e /* DB conn
602e0 65 63 74 69 6f 6e 20 75 73 65 64 20 66 6f 72 20 ection used for
602f0 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 lookaside alloca
60300 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 72 tions. */. char
60310 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 *zName;
60320 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
60330 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f table or view */
60340 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 . int iPKey;
60350 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f /* If no
60360 74 20 6e 65 67 61 74 69 76 65 2c 20 75 73 65 20 t negative, use
60370 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 aCol[iPKey] as t
60380 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a he primary key *
60390 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 /. int nCol;
603a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
603b0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
603c0 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 this table */.
603d0 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 20 Column *aCol;
603e0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
603f0 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 tion about each
60400 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 column */. Inde
60410 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 x *pIndex;
60420 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c 20 /* List of SQL
60430 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69 73 20 indexes on this
60440 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 table. */. int
60450 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 tnum;
60460 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e /* Root BTree n
60470 6f 64 65 20 66 6f 72 20 74 68 69 73 20 74 61 62 ode for this tab
60480 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f le (see note abo
60490 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ve) */. Select
604a0 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a *pSelect; /*
604b0 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65 73 NULL for tables
604c0 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64 65 66 . Points to def
604d0 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76 69 65 inition if a vie
604e0 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 w. */. u16 nRef
604f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
60500 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 Number of pointe
60510 72 73 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65 rs to this Table
60520 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 67 */. u8 tabFlag
60530 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 s; /* Ma
60540 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 sk of TF_* value
60550 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e s */. u8 keyCon
60560 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 f; /* W
60570 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 hat to do in cas
60580 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20 e of uniqueness
60590 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 conflict on iPKe
605a0 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b y */. FKey *pFK
605b0 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c ey; /* L
605c0 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c inked list of al
605d0 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 l foreign keys i
605e0 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a n this table */.
605f0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b char *zColAff;
60600 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 /* String
60610 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 defining the af
60620 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 finity of each c
60630 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 olumn */.#ifndef
60640 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 SQLITE_OMIT_CHE
60650 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63 CK. Expr *pChec
60660 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 k; /* The
60670 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45 43 AND of all CHEC
60680 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f K constraints */
60690 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 .#endif.#ifndef
606a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 SQLITE_OMIT_ALTE
606b0 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 61 64 64 RTABLE. int add
606c0 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a ColOffset; /*
606d0 20 4f 66 66 73 65 74 20 69 6e 20 43 52 45 41 54 Offset in CREAT
606e0 45 20 54 41 42 4c 45 20 73 74 6d 74 20 74 6f 20 E TABLE stmt to
606f0 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e add a new column
60700 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 */.#endif.#ifnd
60710 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
60720 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 IRTUALTABLE. VT
60730 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20 20 able *pVTable;
60740 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56 54 /* List of VT
60750 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a 2f able objects. */
60760 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72 . int nModuleAr
60770 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 g; /* Numbe
60780 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
60790 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a o the module */.
607a0 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c char **azModul
607b0 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 74 20 6f eArg; /* Text o
607c0 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67 f all module arg
607d0 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65 s. [0] is module
607e0 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a name */.#endif.
607f0 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 Trigger *pTrig
60800 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f ger; /* List o
60810 66 20 74 72 69 67 67 65 72 73 20 73 74 6f 72 65 f triggers store
60820 64 20 69 6e 20 70 53 63 68 65 6d 61 20 2a 2f 0a d in pSchema */.
60830 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d Schema *pSchem
60840 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 a; /* Schema
60850 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
60860 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 his table */. T
60870 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 69 able *pNextZombi
60880 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 74 e; /* Next on t
60890 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d 62 69 65 he Parse.pZombie
608a0 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a Tab list */.};..
608b0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
608c0 6c 75 65 73 20 66 6f 72 20 54 61 62 65 2e 74 61 lues for Tabe.ta
608d0 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 bFlags..*/.#defi
608e0 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20 ne TF_Readonly
608f0 20 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 0x01 /*
60900 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65 Read-only syste
60910 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 m table */.#defi
60920 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 ne TF_Ephemeral
60930 20 20 20 20 20 20 30 78 30 32 20 20 20 20 2f 2a 0x02 /*
60940 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 An ephemeral ta
60950 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 ble */.#define T
60960 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 F_HasPrimaryKey
60970 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54 61 62 0x04 /* Tab
60980 6c 65 20 68 61 73 20 61 20 70 72 69 6d 61 72 79 le has a primary
60990 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 key */.#define
609a0 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 TF_Autoincrement
609b0 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20 49 6e 0x08 /* In
609c0 74 65 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 teger primary ke
609d0 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 y is autoincreme
609e0 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 nt */.#define TF
609f0 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20 20 20 _Virtual
60a00 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73 20 61 0x10 /* Is a
60a10 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a virtual table *
60a20 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e 65 65 /.#define TF_Nee
60a30 64 4d 65 74 61 64 61 74 61 20 20 20 20 30 78 32 dMetadata 0x2
60a40 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 7a 0 /* aCol[].z
60a50 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b 5d 2e Type and aCol[].
60a60 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 2a 2f pColl missing */
60a70 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 ..../*.** Test t
60a80 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f 72 o see whether or
60a90 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 69 73 20 not a table is
60aa0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e a virtual table.
60ab0 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 64 6f 6e This is.** don
60ac0 65 20 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 e as a macro so
60ad0 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 that it will be
60ae0 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 20 77 68 optimized out wh
60af0 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 en virtual.** ta
60b00 62 6c 65 20 73 75 70 70 6f 72 74 20 69 73 20 6f ble support is o
60b10 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 mitted from the
60b20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 build..*/.#ifnde
60b30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
60b40 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 RTUALTABLE.# de
60b50 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 fine IsVirtual(X
60b60 29 20 20 20 20 20 20 28 28 28 58 29 2d 3e 74 61 ) (((X)->ta
60b70 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 bFlags & TF_Virt
60b80 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 65 66 69 ual)!=0).# defi
60b90 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d ne IsHiddenColum
60ba0 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48 69 64 n(X) ((X)->isHid
60bb0 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 den).#else.# de
60bc0 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 fine IsVirtual(X
60bd0 29 20 20 20 20 20 20 30 0a 23 20 20 64 65 66 69 ) 0.# defi
60be0 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d ne IsHiddenColum
60bf0 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f n(X) 0.#endif../
60c00 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65 69 67 *.** Each foreig
60c10 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 n key constraint
60c20 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 is an instance
60c30 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
60c40 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a structure..**.*
60c50 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 * A foreign key
60c60 69 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 is associated wi
60c70 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e 20 20 th two tables.
60c80 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62 6c 65 The "from" table
60c90 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 is.** the table
60ca0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
60cb0 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c he REFERENCES cl
60cc0 61 75 73 65 20 74 68 61 74 20 63 72 65 61 74 65 ause that create
60cd0 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a s the foreign.**
60ce0 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f 22 20 key. The "to"
60cf0 74 61 62 6c 65 20 69 73 20 74 68 65 20 74 61 62 table is the tab
60d00 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65 64 le that is named
60d10 20 69 6e 20 74 68 65 20 52 45 46 45 52 45 4e 43 in the REFERENC
60d20 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 6f ES clause..** Co
60d30 6e 73 69 64 65 72 20 74 68 69 73 20 65 78 61 6d nsider this exam
60d40 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 ple:.**.** C
60d50 52 45 41 54 45 20 54 41 42 4c 45 20 65 78 31 28 REATE TABLE ex1(
60d60 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e 54 45 .** a INTE
60d70 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c GER PRIMARY KEY,
60d80 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e 54 45 .** b INTE
60d90 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54 20 66 GER CONSTRAINT f
60da0 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20 65 78 k1 REFERENCES ex
60db0 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a 2a 2(x).** );.*
60dc0 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e *.** For foreign
60dd0 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68 65 20 key "fk1", the
60de0 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 65 from-table is "e
60df0 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f 2d 74 x1" and the to-t
60e00 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e 0a 2a able is "ex2"..*
60e10 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45 52 45 *.** Each REFERE
60e20 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65 6e 65 NCES clause gene
60e30 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 rates an instanc
60e40 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
60e50 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 ng structure.**
60e60 77 68 69 63 68 20 69 73 20 61 74 74 61 63 68 65 which is attache
60e70 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61 d to the from-ta
60e80 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74 61 62 ble. The to-tab
60e90 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 le need not exis
60ea0 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66 72 t when.** the fr
60eb0 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72 65 61 om-table is crea
60ec0 74 65 64 2e 20 20 54 68 65 20 65 78 69 73 74 65 ted. The existe
60ed0 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d 74 61 nce of the to-ta
60ee0 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65 63 6b ble is not check
60ef0 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b ed..*/.struct FK
60f00 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46 ey {. Table *pF
60f10 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c rom; /* Tabl
60f20 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
60f30 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 REFERENCES clau
60f40 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64 29 20 se (aka: Child)
60f50 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 */. FKey *pNext
60f60 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 From; /* Next f
60f70 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 oreign key in pF
60f80 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a rom */. char *z
60f90 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 To; /* Na
60fa0 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 me of table that
60fb0 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 the key points
60fc0 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e 74 29 to (aka: Parent)
60fd0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 */. FKey *pNex
60fe0 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 tTo; /* Next
60ff0 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 foreign key on t
61000 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a able named zTo *
61010 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65 76 54 /. FKey *pPrevT
61020 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 o; /* Previou
61030 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e s foreign key on
61040 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f table named zTo
61050 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 */. int nCol;
61060 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
61070 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
61080 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f 2a this key */. /*
61090 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 EV: R-30323-219
610a0 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 17 */. u8 isDef
610b0 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 erred; /* Tru
610c0 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 e if constraint
610d0 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65 66 65 checking is defe
610e0 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 rred till COMMIT
610f0 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69 6f 6e */. u8 aAction
61100 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a [2]; /*
61110 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f ON DELETE and O
61120 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 N UPDATE actions
61130 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 20 2a , respectively *
61140 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61 70 54 /. Trigger *apT
61150 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54 rigger[2]; /* T
61160 72 69 67 67 65 72 73 20 66 6f 72 20 61 41 63 74 riggers for aAct
61170 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f ion[] actions */
61180 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 . struct sColMa
61190 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 p { /* Mapping
611a0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 of columns in pF
611b0 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 rom to columns i
611c0 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 n zTo */. int
611d0 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 iFrom;
611e0 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 /* Index of colu
611f0 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 mn in pFrom */.
61200 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 char *zCol;
61210 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
61220 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 column in zTo.
61230 20 49 66 20 30 20 75 73 65 20 50 52 49 4d 41 52 If 0 use PRIMAR
61240 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43 6f Y KEY */. } aCo
61250 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 l[1]; /*
61260 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 One entry for ea
61270 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d ch of nCol colum
61280 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a n s */.};../*.**
61290 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 SQLite supports
612a0 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 many different
612b0 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 ways to resolve
612c0 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 a constraint.**
612d0 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b error. ROLLBACK
612e0 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e processing mean
612f0 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 s that a constra
61300 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a int violation.**
61310 20 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 causes the oper
61320 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 ation in process
61330 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 to fail and for
61340 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
61350 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 nsaction.** to b
61360 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 e rolled back.
61370 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 ABORT processing
61380 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 means the opera
61390 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a tion in process.
613a0 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 ** fails and any
613b0 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 prior changes f
613c0 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 rom that one ope
613d0 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 ration are backe
613e0 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 d out,.** but th
613f0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 e transaction is
61400 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b not rolled back
61410 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 . FAIL processi
61420 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a ng means that.**
61430 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 the operation i
61440 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 n progress stops
61450 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 and returns an
61460 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 error code. But
61470 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 prior.** change
61480 73 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d s due to the sam
61490 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 e operation are
614a0 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 not backed out a
614b0 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a nd no rollback.*
614c0 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 * occurs. IGNOR
614d0 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 E means that the
614e0 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 particular row
614f0 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 that caused the
61500 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 constraint.** er
61510 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 ror is not inser
61520 74 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 ted or updated.
61530 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 Processing cont
61540 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 inues and no err
61550 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 or.** is returne
61560 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e d. REPLACE mean
61570 73 20 74 68 61 74 20 70 72 65 65 78 69 73 74 69 s that preexisti
61580 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 ng database rows
61590 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 that caused.**
615a0 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 a UNIQUE constra
615b0 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 int violation ar
615c0 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 e removed so tha
615d0 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 t the new insert
615e0 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 or.** update ca
615f0 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 n proceed. Proc
61600 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 essing continues
61610 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 and no error is
61620 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a reported..**.**
61630 20 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 RESTRICT, SETNU
61640 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 LL, and CASCADE
61650 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e actions apply on
61660 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 ly to foreign ke
61670 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 ys..** RESTRICT
61680 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 is the same as A
61690 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 BORT for IMMEDIA
616a0 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 TE foreign keys
616b0 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 and the.** same
616c0 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 as ROLLBACK for
616d0 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 DEFERRED keys.
616e0 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 SETNULL means th
616f0 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a at the foreign.*
61700 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 * key is set to
61710 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d NULL. CASCADE m
61720 65 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 eans that a DELE
61730 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 TE or UPDATE of
61740 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 the.** reference
61750 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 d table row is p
61760 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 ropagated into t
61770 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 he row that hold
61780 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e s the.** foreign
61790 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 key..** .** The
617a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f following symbo
617b0 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 lic values are u
617c0 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 sed to record wh
617d0 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 ich type.** of a
617e0 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a ction to take..*
617f0 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e /.#define OE_Non
61800 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 e 0 /* The
61810 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 re is no constra
61820 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a int to check */.
61830 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 #define OE_Rollb
61840 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 ack 1 /* Fail
61850 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e the operation an
61860 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 d rollback the t
61870 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 ransaction */.#d
61880 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 efine OE_Abort
61890 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 2 /* Back ou
618a0 74 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f t changes but do
618b0 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 no rollback tra
618c0 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 nsaction */.#def
618d0 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 ine OE_Fail
618e0 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 3 /* Stop the
618f0 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 operation but le
61900 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 ave all prior ch
61910 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 anges */.#define
61920 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 OE_Ignore 4
61930 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 /* Ignore the e
61940 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 rror. Do not do
61950 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 the INSERT or UP
61960 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 DATE */.#define
61970 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 OE_Replace 5
61980 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 /* Delete existi
61990 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 ng record, then
619a0 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 do INSERT or UPD
619b0 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 ATE */..#define
619c0 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 OE_Restrict 6
619d0 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 /* OE_Abort for
619e0 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f IMMEDIATE, OE_Ro
619f0 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 llback for DEFER
61a00 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f RED */.#define O
61a10 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f E_SetNull 7 /
61a20 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 * Set the foreig
61a30 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e n key value to N
61a40 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f ULL */.#define O
61a50 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f E_SetDflt 8 /
61a60 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 * Set the foreig
61a70 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 n key value to i
61a80 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 ts default */.#d
61a90 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 efine OE_Cascade
61aa0 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 9 /* Cascade
61ab0 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a the changes */.
61ac0 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 .#define OE_Defa
61ad0 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 ult 99 /* Do w
61ae0 68 61 74 65 76 65 72 20 74 68 65 20 64 65 66 61 hatever the defa
61af0 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f ult action is */
61b00 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 .../*.** An inst
61b10 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
61b20 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
61b30 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 is passed as the
61b40 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 first.** argume
61b50 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 nt to sqlite3Vdb
61b60 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 eKeyCompare and
61b70 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 is used to contr
61b80 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 ol the .** compa
61b90 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f rison of the two
61ba0 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a index keys..*/.
61bb0 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b struct KeyInfo {
61bc0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
61bd0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 /* The da
61be0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
61bf0 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 n */. u8 enc;
61c00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 /* Te
61c10 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e xt encoding - on
61c20 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 e of the TEXT_Ut
61c30 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 f* values */. u
61c40 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 16 nField;
61c50 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
61c60 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c entries in aColl
61c70 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 [] */. u8 *aSor
61c80 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 tOrder; /* I
61c90 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f f defined an aSo
61ca0 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 rtOrder[i] is tr
61cb0 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f ue, sort DESC */
61cc0 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c . CollSeq *aCol
61cd0 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 l[1]; /* Collat
61ce0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 ing sequence for
61cf0 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 each term of th
61d00 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a e key */.};../*.
61d10 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f ** An instance o
61d20 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
61d30 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 structure holds
61d40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
61d50 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e t a.** single in
61d60 64 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 dex record that
61d70 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
61d80 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f parsed out into
61d90 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 individual.** v
61da0 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 alues..**.** A r
61db0 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 ecord is an obje
61dc0 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 ct that contains
61dd0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 one or more fie
61de0 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 lds of data..**
61df0 52 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 Records are used
61e00 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f to store the co
61e10 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 ntent of a table
61e20 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 row and to stor
61e30 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 e.** the key of
61e40 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f an index. A blo
61e50 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 b encoding of a
61e60 72 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 record is create
61e70 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d d by.** the OP_M
61e80 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 akeRecord opcode
61e90 20 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 of the VDBE and
61ea0 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c 65 64 is disassembled
61eb0 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f by the.** OP_Co
61ec0 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a lumn opcode..**.
61ed0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
61ee0 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 e holds a record
61ef0 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 that has alread
61f00 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62 y been disassemb
61f10 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 led.** into its
61f20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69 65 6c constituent fiel
61f30 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e ds..*/.struct Un
61f40 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 packedRecord {.
61f50 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
61f60 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f fo; /* Collatio
61f70 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 n and sort-order
61f80 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a information */.
61f90 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 u16 nField;
61fa0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
61fb0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 of entries in ap
61fc0 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 66 Mem[] */. u16 f
61fd0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f lags; /
61fe0 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e * Boolean settin
61ff0 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e gs. UNPACKED_..
62000 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 . below */. i64
62010 20 72 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 rowid;
62020 20 2f 2a 20 55 73 65 64 20 62 79 20 55 4e 50 41 /* Used by UNPA
62030 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 CKED_PREFIX_SEAR
62040 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 CH */. Mem *aMe
62050 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 m; /* V
62060 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a alues */.};../*.
62070 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
62080 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 52 65 63 s of UnpackedRec
62090 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 ord.flags.*/.#de
620a0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 fine UNPACKED_NE
620b0 45 44 5f 46 52 45 45 20 20 20 20 20 30 78 30 30 ED_FREE 0x00
620c0 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 01 /* Memory is
620d0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c from sqlite3Mal
620e0 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 loc() */.#define
620f0 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 UNPACKED_NEED_D
62100 45 53 54 52 4f 59 20 20 30 78 30 30 30 32 20 20 ESTROY 0x0002
62110 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 68 6f 75 /* apMem[]s shou
62120 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 74 72 6f ld all be destro
62130 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 yed */.#define U
62140 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 NPACKED_IGNORE_R
62150 4f 57 49 44 20 20 30 78 30 30 30 34 20 20 2f 2a OWID 0x0004 /*
62160 20 49 67 6e 6f 72 65 20 74 72 61 69 6c 69 6e 67 Ignore trailing
62170 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 31 20 2a rowid on key1 *
62180 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b /.#define UNPACK
62190 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 20 20 20 ED_INCRKEY
621a0 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 61 6b 65 0x0008 /* Make
621b0 20 74 68 69 73 20 6b 65 79 20 61 6e 20 65 70 73 this key an eps
621c0 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a 2f 0a 23 ilon larger */.#
621d0 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f define UNPACKED_
621e0 50 52 45 46 49 58 5f 4d 41 54 43 48 20 20 30 78 PREFIX_MATCH 0x
621f0 30 30 31 30 20 20 2f 2a 20 41 20 70 72 65 66 69 0010 /* A prefi
62200 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 x match is consi
62210 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 66 dered OK */.#def
62220 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 ine UNPACKED_PRE
62230 46 49 58 5f 53 45 41 52 43 48 20 30 78 30 30 32 FIX_SEARCH 0x002
62240 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20 6d 0 /* A prefix m
62250 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65 72 atch is consider
62260 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 ed OK */../*.**
62270 45 61 63 68 20 53 51 4c 20 69 6e 64 65 78 20 69 Each SQL index i
62280 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e s represented in
62290 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a memory by an.**
622a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 instance of the
622b0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 following struc
622c0 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ture..**.** The
622d0 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 columns of the t
622e0 61 62 6c 65 20 74 68 61 74 20 61 72 65 20 74 6f able that are to
622f0 20 62 65 20 69 6e 64 65 78 65 64 20 61 72 65 20 be indexed are
62300 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 described.** by
62310 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 the aiColumn[] f
62320 69 65 6c 64 20 6f 66 20 74 68 69 73 20 73 74 72 ield of this str
62330 75 63 74 75 72 65 2e 20 20 46 6f 72 20 65 78 61 ucture. For exa
62340 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a mple, suppose.**
62350 20 77 65 20 68 61 76 65 20 74 68 65 20 66 6f 6c we have the fol
62360 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 lowing table and
62370 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 index:.**.**
62380 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 45 CREATE TABLE E
62390 78 31 28 63 31 20 69 6e 74 2c 20 63 32 20 69 6e x1(c1 int, c2 in
623a0 74 2c 20 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 t, c3 text);.**
623b0 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 CREATE INDEX
623c0 20 45 78 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 Ex2 ON Ex1(c3,c
623d0 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 1);.**.** In the
623e0 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 Table structure
623f0 20 64 65 73 63 72 69 62 69 6e 67 20 45 78 31 2c describing Ex1,
62400 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 nCol==3 because
62410 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 there are.** th
62420 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 ree columns in t
62430 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 he table. In th
62440 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 e Index structur
62450 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 e describing.**
62460 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 Ex2, nColumn==2
62470 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65 20 33 since 2 of the 3
62480 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 columns of Ex1
62490 61 72 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 are indexed..**
624a0 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61 69 43 The value of aiC
624b0 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e olumn is {2, 0}.
624c0 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 aiColumn[0]==2
624d0 20 62 65 63 61 75 73 65 20 74 68 65 20 0a 2a 2a because the .**
624e0 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f first column to
624f0 20 62 65 20 69 6e 64 65 78 65 64 20 28 63 33 29 be indexed (c3)
62500 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 has an index of
62510 20 32 20 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2 in Ex1.aCol[]
62520 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ..** The second
62530 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 column to be ind
62540 65 78 65 64 20 28 63 31 29 20 68 61 73 20 61 6e exed (c1) has an
62550 20 69 6e 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a index of 0 in.*
62560 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 * Ex1.aCol[], he
62570 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e nce Ex2.aiColumn
62580 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 [1]==0..**.** Th
62590 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 e Index.onError
625a0 66 69 65 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 field determines
625b0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
625c0 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 the indexed colu
625d0 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 mns.** must be u
625e0 6e 69 71 75 65 20 61 6e 64 20 77 68 61 74 20 74 nique and what t
625f0 6f 20 64 6f 20 69 66 20 74 68 65 79 20 61 72 65 o do if they are
62600 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 not. When Inde
62610 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e x.onError=OE_Non
62620 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 e,.** it means t
62630 68 69 73 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 his is not a uni
62640 71 75 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 que index. Othe
62650 72 77 69 73 65 20 69 74 20 69 73 20 61 20 75 6e rwise it is a un
62660 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e ique index.** an
62670 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 49 d the value of I
62680 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 ndex.onError ind
62690 69 63 61 74 65 20 74 68 65 20 77 68 69 63 68 20 icate the which
626a0 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 conflict resolut
626b0 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 ion .** algorith
626c0 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e m to employ when
626d0 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70 74 20 ever an attempt
626e0 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 is made to inser
626f0 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a t a non-unique.*
62700 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 * element..*/.st
62710 72 75 63 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 ruct Index {. c
62720 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 har *zName;
62730 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 /* Name of this
62740 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e index */. int n
62750 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e Column; /* N
62760 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 umber of columns
62770 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 75 73 in the table us
62780 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 ed by this index
62790 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c */. int *aiCol
627a0 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63 68 20 umn; /* Which
627b0 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 columns are used
627c0 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 2e 20 by this index.
627d0 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20 20 75 1st is 0 */. u
627e0 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f 77 45 73 nsigned *aiRowEs
627f0 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 t; /* Result of
62800 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72 6f ANALYZE: Est. ro
62810 77 73 20 73 65 6c 65 63 74 65 64 20 62 79 20 65 ws selected by e
62820 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 ach column */.
62830 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 Table *pTable;
62840 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61 62 6c /* The SQL tabl
62850 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65 64 20 e being indexed
62860 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 */. int tnum;
62870 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f /* Page co
62880 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 ntaining root of
62890 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e 20 64 this index in d
628a0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a atabase file */.
628b0 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20 u8 onError;
628c0 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 /* OE_Abort,
628d0 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 OE_Ignore, OE_Re
628e0 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e place, or OE_Non
628f0 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e e */. u8 autoIn
62900 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 dex; /* True
62910 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 if is automatica
62920 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65 78 3a lly created (ex:
62930 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20 by UNIQUE) */.
62940 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 char *zColAff;
62950 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 /* String defi
62960 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 ning the affinit
62970 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e y of each column
62980 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 4e 65 */. Index *pNe
62990 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 xt; /* The ne
629a0 78 74 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 xt index associa
629b0 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61 6d ted with the sam
629c0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 63 68 e table */. Sch
629d0 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 2f 2a ema *pSchema; /*
629e0 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 Schema containi
629f0 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f ng this index */
62a00 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 . u8 *aSortOrde
62a10 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 r; /* Array of
62a20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 size Index.nColu
62a30 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 43 2c 20 mn. True==DESC,
62a40 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 20 False==ASC */.
62a50 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 char **azColl;
62a60 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c /* Array of col
62a70 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
62a80 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 65 78 20 names for index
62a90 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 */. IndexSample
62aa0 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a *aSample; /*
62ab0 20 41 72 72 61 79 20 6f 66 20 53 51 4c 49 54 45 Array of SQLITE
62ac0 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 73 _INDEX_SAMPLES s
62ad0 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a amples */.};../*
62ae0 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c 65 20 .** Each sample
62af0 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 stored in the sq
62b00 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 lite_stat2 table
62b10 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 is represented
62b20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73 in memory .** us
62b30 69 6e 67 20 61 20 73 74 72 75 63 74 75 72 65 20 ing a structure
62b40 6f 66 20 74 68 69 73 20 74 79 70 65 2e 0a 2a 2f of this type..*/
62b50 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d .struct IndexSam
62b60 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a ple {. union {.
62b70 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 char *z;
62b80 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 /* Value if
62b90 65 54 79 70 65 20 69 73 20 53 51 4c 49 54 45 5f eType is SQLITE_
62ba0 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45 5f 42 TEXT or SQLITE_B
62bb0 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c LOB */. doubl
62bc0 65 20 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 e r; /* Va
62bd0 6c 75 65 20 69 66 20 65 54 79 70 65 20 69 73 20 lue if eType is
62be0 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 6f 72 20 SQLITE_FLOAT or
62bf0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2a SQLITE_INTEGER *
62c00 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 20 65 54 /. } u;. u8 eT
62c10 79 70 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ype; /*
62c20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c SQLITE_NULL, SQL
62c30 49 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20 ITE_INTEGER ...
62c40 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 79 etc. */. u8 nBy
62c50 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 te; /* S
62c60 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66 20 74 ize in byte of t
62c70 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a ext or blob. */.
62c80 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 };../*.** Each t
62c90 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 oken coming out
62ca0 6f 66 20 74 68 65 20 6c 65 78 65 72 20 69 73 20 of the lexer is
62cb0 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a an instance of.*
62cc0 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 * this structure
62cd0 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c . Tokens are al
62ce0 73 6f 20 75 73 65 64 20 61 73 20 70 61 72 74 20 so used as part
62cf0 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e of an expression
62d00 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 ..**.** Note if
62d10 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 Token.z==0 then
62d20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f Token.dyn and To
62d30 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 ken.n are undefi
62d40 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 ned and.** may c
62d50 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 ontain random va
62d60 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 lues. Do not ma
62d70 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74 69 6f ke any assumptio
62d80 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 ns about Token.d
62d90 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e yn.** and Token.
62da0 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d n when Token.z==
62db0 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 0..*/.struct Tok
62dc0 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 en {. const cha
62dd0 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 65 78 r *z; /* Tex
62de0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 t of the token.
62df0 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e Not NULL-termin
62e00 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 ated! */. unsig
62e10 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a ned int n; /*
62e20 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 Number of chara
62e30 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f cters in this to
62e40 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ken */.};../*.**
62e50 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 An instance of
62e60 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63 this structure c
62e70 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 ontains informat
62e80 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 ion needed to ge
62e90 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
62ea0 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 or a SELECT that
62eb0 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 contains aggreg
62ec0 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a ate functions..*
62ed0 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d *.** If Expr.op=
62ee0 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f =TK_AGG_COLUMN o
62ef0 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f r TK_AGG_FUNCTIO
62f00 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 N then Expr.pAgg
62f10 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 Info is a.** poi
62f20 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 nter to this str
62f30 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 ucture. The Exp
62f40 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 r.iColumn field
62f50 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a is the index in.
62f60 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b ** AggInfo.aCol[
62f70 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 ] or AggInfo.aFu
62f80 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 nc[] of informat
62f90 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 ion needed to ge
62fa0 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 nerate.** code f
62fb0 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a or that node..**
62fc0 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f .** AggInfo.pGro
62fd0 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f upBy and AggInfo
62fe0 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 .aFunc.pExpr poi
62ff0 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 nt to fields wit
63000 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 hin the.** origi
63010 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 nal Select struc
63020 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 ture that descri
63030 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 bes the SELECT s
63040 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 tatement. These
63050 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f .** fields do no
63060 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 t need to be fre
63070 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 ed when dealloca
63080 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f ting the AggInfo
63090 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 structure..*/.s
630a0 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a truct AggInfo {.
630b0 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b u8 directMode;
630c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 /* Dir
630d0 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f ect rendering mo
630e0 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 de means take da
630f0 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 ta directly.
63100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
63110 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f ** from so
63120 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 urce tables rath
63130 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 er than from acc
63140 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 umulators */. u
63150 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 8 useSortingIdx;
63160 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 /* In dir
63170 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 ect mode, refere
63180 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 nce the sorting
63190 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 index rather.
631a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
631b0 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 ** than t
631c0 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 he source table
631d0 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 */. int sorting
631e0 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Idx; /*
631f0 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 Cursor number of
63200 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 the sorting ind
63210 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 ex */. ExprList
63220 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 *pGroupBy;
63230 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 /* The group by
63240 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 clause */. int
63250 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 nSortingColumn;
63260 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
63270 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
63280 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f sorting index */
63290 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 . struct AggInf
632a0 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f o_col { /* Fo
632b0 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 r each column us
632c0 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 ed in source tab
632d0 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 les */. Table
632e0 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 *pTab;
632f0 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 /* Source ta
63300 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ble */. int i
63310 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 Table;
63320 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 /* Cursor nu
63330 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 mber of the sour
63340 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 ce table */.
63350 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 int iColumn;
63360 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 /* Colu
63370 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e mn number within
63380 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c the source tabl
63390 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f e */. int iSo
633a0 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 rterColumn;
633b0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 /* Column numb
633c0 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e er in the sortin
633d0 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 g index */. i
633e0 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 nt iMem;
633f0 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 /* Memor
63400 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 y location that
63410 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 acts as accumula
63420 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 tor */. Expr
63430 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
63440 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 /* The origi
63450 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a nal expression *
63460 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 /. } *aCol;. i
63470 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 nt nColumn;
63480 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
63490 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 of used entries
634a0 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 in aCol[] */.
634b0 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 int nColumnAlloc
634c0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 ; /* Numbe
634d0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
634e0 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 ated for aCol[]
634f0 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 */. int nAccumu
63500 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 lator; /*
63510 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e Number of column
63520 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f s that show thro
63530 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ugh to the outpu
63540 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t..
63550 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 **
63560 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d Additional colum
63570 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 ns are used only
63580 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 as parameters t
63590 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o.
635a0 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 ** a
635b0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
635c0 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 ns */. struct A
635d0 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 ggInfo_func {
635e0 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 /* For each aggr
635f0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a egate function *
63600 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 /. Expr *pExp
63610 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f r; /
63620 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 * Expression enc
63630 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 oding the functi
63640 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 on */. FuncDe
63650 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 f *pFunc;
63660 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 /* The aggreg
63670 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 ate function imp
63680 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 lementation */.
63690 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 int iMem;
636a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
636b0 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 emory location t
636c0 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75 hat acts as accu
636d0 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 mulator */. i
636e0 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 nt iDistinct;
636f0 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 6d /* Ephem
63700 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64 20 eral table used
63710 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54 49 to enforce DISTI
63720 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e NCT */. } *aFun
63730 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 c;. int nFunc;
63740 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
63750 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
63760 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a s in aFunc[] */.
63770 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 int nFuncAlloc
63780 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d ; /* Num
63790 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c ber of slots all
637a0 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e 63 ocated for aFunc
637b0 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 [] */.};../*.**
637c0 54 68 65 20 64 61 74 61 74 79 70 65 20 79 6e 56 The datatype ynV
637d0 61 72 20 69 73 20 61 20 73 69 67 6e 65 64 20 69 ar is a signed i
637e0 6e 74 65 67 65 72 2c 20 65 69 74 68 65 72 20 31 nteger, either 1
637f0 36 2d 62 69 74 20 6f 72 20 33 32 2d 62 69 74 2e 6-bit or 32-bit.
63800 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 69 74 20 69 .** Usually it i
63810 73 20 31 36 2d 62 69 74 73 2e 20 20 42 75 74 20 s 16-bits. But
63820 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 if SQLITE_MAX_VA
63830 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 69 73 RIABLE_NUMBER is
63840 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e greater.** than
63850 20 33 32 37 36 37 20 77 65 20 68 61 76 65 20 74 32767 we have t
63860 6f 20 6d 61 6b 65 20 69 74 20 33 32 2d 62 69 74 o make it 32-bit
63870 2e 20 20 31 36 2d 62 69 74 20 69 73 20 70 72 65 . 16-bit is pre
63880 66 65 72 72 65 64 20 62 65 63 61 75 73 65 0a 2a ferred because.*
63890 2a 20 69 74 20 75 73 65 73 20 6c 65 73 73 20 6d * it uses less m
638a0 65 6d 6f 72 79 20 69 6e 20 74 68 65 20 45 78 70 emory in the Exp
638b0 72 20 6f 62 6a 65 63 74 2c 20 77 68 69 63 68 20 r object, which
638c0 69 73 20 61 20 62 69 67 20 6d 65 6d 6f 72 79 20 is a big memory
638d0 75 73 65 72 0a 2a 2a 20 69 6e 20 73 79 73 74 65 user.** in syste
638e0 6d 73 20 77 69 74 68 20 6c 6f 74 73 20 6f 66 20 ms with lots of
638f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
63900 6e 74 73 2e 20 20 41 6e 64 20 66 65 77 20 61 70 nts. And few ap
63910 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6e 65 plications.** ne
63920 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 61 62 6f ed more than abo
63930 75 74 20 31 30 20 6f 72 20 32 30 20 76 61 72 69 ut 10 or 20 vari
63940 61 62 6c 65 73 2e 20 20 42 75 74 20 73 6f 6d 65 ables. But some
63950 20 65 78 74 72 65 6d 65 20 75 73 65 72 73 20 77 extreme users w
63960 61 6e 74 0a 2a 2a 20 74 6f 20 68 61 76 65 20 70 ant.** to have p
63970 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e repared statemen
63980 74 73 20 77 69 74 68 20 6f 76 65 72 20 33 32 37 ts with over 327
63990 36 37 20 76 61 72 69 61 62 6c 65 73 2c 20 61 6e 67 variables, an
639a0 64 20 66 6f 72 20 74 68 65 6d 0a 2a 2a 20 74 68 d for them.** th
639b0 65 20 6f 70 74 69 6f 6e 20 69 73 20 61 76 61 69 e option is avai
639c0 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d 70 69 6c lable (at compil
639d0 65 2d 74 69 6d 65 29 2e 0a 2a 2f 0a 23 69 66 20 e-time)..*/.#if
639e0 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 SQLITE_MAX_VARIA
639f0 42 4c 45 5f 4e 55 4d 42 45 52 3c 3d 33 32 37 36 BLE_NUMBER<=3276
63a00 37 0a 74 79 70 65 64 65 66 20 69 31 36 20 79 6e 7.typedef i16 yn
63a10 56 61 72 3b 0a 23 65 6c 73 65 0a 74 79 70 65 64 Var;.#else.typed
63a20 65 66 20 69 6e 74 20 79 6e 56 61 72 3b 0a 23 65 ef int ynVar;.#e
63a30 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 ndif../*.** Each
63a40 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78 70 72 node of an expr
63a50 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 70 61 ession in the pa
63a60 72 73 65 20 74 72 65 65 20 69 73 20 61 6e 20 69 rse tree is an i
63a70 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 nstance.** of th
63a80 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a is structure..**
63a90 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73 20 74 .** Expr.op is t
63aa0 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 69 he opcode. The i
63ab0 6e 74 65 67 65 72 20 70 61 72 73 65 72 20 74 6f nteger parser to
63ac0 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20 72 65 ken codes are re
63ad0 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63 6f 64 used.** as opcod
63ae0 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65 78 61 es here. For exa
63af0 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73 65 72 mple, the parser
63b00 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45 20 74 defines TK_GE t
63b10 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 0a o be an integer.
63b20 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73 65 6e ** code represen
63b30 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20 6f 70 ting the ">=" op
63b40 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 61 6d erator. This sam
63b50 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20 69 e integer code i
63b60 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f 20 72 s reused.** to r
63b70 65 70 72 65 73 65 6e 74 20 74 68 65 20 67 72 65 epresent the gre
63b80 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65 71 75 ater-than-or-equ
63b90 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72 20 69 al-to operator i
63ba0 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e n the expression
63bb0 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 .** tree..**.**
63bc0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f If the expressio
63bd0 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69 74 65 n is an SQL lite
63be0 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45 52 2c ral (TK_INTEGER,
63bf0 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f 42 4c TK_FLOAT, TK_BL
63c00 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f 53 54 OB, .** or TK_ST
63c10 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78 70 72 RING), then Expr
63c20 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 .token contains
63c30 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 the text of the
63c40 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49 66 0a SQL literal. If.
63c50 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73 69 6f ** the expressio
63c60 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 20 n is a variable
63c70 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c 20 74 (TK_VARIABLE), t
63c80 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 hen Expr.token c
63c90 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a 2a 20 ontains the .**
63ca0 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e 20 46 variable name. F
63cb0 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65 20 65 inally, if the e
63cc0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 xpression is an
63cd0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28 54 4b SQL function (TK
63ce0 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a 20 74 _FUNCTION),.** t
63cf0 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 hen Expr.token c
63d00 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 ontains the name
63d10 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
63d20 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69 ..**.** Expr.pRi
63d30 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65 ght and Expr.pLe
63d40 66 74 20 61 72 65 20 74 68 65 20 6c 65 66 74 20 ft are the left
63d50 61 6e 64 20 72 69 67 68 74 20 73 75 62 65 78 70 and right subexp
63d60 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a 2a 2a ressions of a.**
63d70 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 binary operator
63d80 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f 74 68 . Either or both
63d90 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a may be NULL..**
63da0 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69 73 74 .** Expr.x.pList
63db0 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 61 72 is a list of ar
63dc0 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65 20 65 guments if the e
63dd0 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 xpression is an
63de0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a SQL function,.**
63df0 20 61 20 43 41 53 45 20 65 78 70 72 65 73 73 69 a CASE expressi
63e00 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 on or an IN expr
63e10 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f ession of the fo
63e20 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28 3c 79 rm "<lhs> IN (<y
63e30 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a 2a 20 >, <z>...)"..**
63e40 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 69 Expr.x.pSelect i
63e50 73 20 75 73 65 64 20 69 66 20 74 68 65 20 65 78 s used if the ex
63e60 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75 pression is a su
63e70 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e 20 65 b-select or an e
63e80 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a 2a 20 xpression of.**
63e90 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 the form "<lhs>
63ea0 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 IN (SELECT ...)"
63eb0 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49 73 53 . If the EP_xIsS
63ec0 65 6c 65 63 74 20 62 69 74 20 69 73 20 73 65 74 elect bit is set
63ed0 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70 72 2e in the.** Expr.
63ee0 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e flags mask, then
63ef0 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74 20 Expr.x.pSelect
63f00 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65 72 77 is valid. Otherw
63f10 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c 69 73 ise, Expr.x.pLis
63f20 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64 2e 0a t is .** valid..
63f30 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73 **.** An express
63f40 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 ion of the form
63f50 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65 ID or ID.ID refe
63f60 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 rs to a column i
63f70 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f n a table..** Fo
63f80 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f r such expressio
63f90 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73 ns, Expr.op is s
63fa0 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 et to TK_COLUMN
63fb0 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20 and Expr.iTable
63fc0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 is.** the intege
63fd0 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 r cursor number
63fe0 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72 of a VDBE cursor
63ff0 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61 pointing to tha
64000 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45 t table and.** E
64010 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74 xpr.iColumn is t
64020 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 he column number
64030 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 for the specifi
64040 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 c column. If th
64050 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 e.** expression
64060 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73 is used as a res
64070 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67 ult in an aggreg
64080 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e ate SELECT, then
64090 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73 the.** value is
640a0 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20 also stored in
640b0 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f the Expr.iAgg co
640c0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72 lumn in the aggr
640d0 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a egate so that.**
640e0 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73 it can be acces
640f0 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67 sed after all ag
64100 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d gregates are com
64110 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 puted..**.** If
64120 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
64130 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76 61 72 s an unbound var
64140 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28 61 20 iable marker (a
64150 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20 0a 2a question mark .*
64160 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f 27 20 * character '?'
64170 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 in the original
64180 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20 45 78 SQL) then the Ex
64190 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64 73 20 pr.iTable holds
641a0 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6e 75 the index .** nu
641b0 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20 76 61 mber for that va
641c0 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 riable..**.** If
641d0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
641e0 69 73 20 61 20 73 75 62 71 75 65 72 79 20 74 68 is a subquery th
641f0 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 en Expr.iColumn
64200 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 holds an integer
64210 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e 75 6d .** register num
64220 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ber containing t
64230 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 he result of the
64240 20 73 75 62 71 75 65 72 79 2e 20 20 49 66 20 74 subquery. If t
64250 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 67 he.** subquery g
64260 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 ives a constant
64270 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69 54 61 result, then iTa
64280 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66 20 74 ble is -1. If t
64290 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 67 he subquery.** g
642a0 69 76 65 73 20 61 20 64 69 66 66 65 72 65 6e 74 ives a different
642b0 20 61 6e 73 77 65 72 20 61 74 20 64 69 66 66 65 answer at diffe
642c0 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72 69 6e rent times durin
642d0 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72 6f 63 g statement proc
642e0 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e 20 69 essing.** then i
642f0 54 61 62 6c 65 20 69 73 20 74 68 65 20 61 64 64 Table is the add
64300 72 65 73 73 20 6f 66 20 61 20 73 75 62 72 6f 75 ress of a subrou
64310 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70 75 74 tine that comput
64320 65 73 20 74 68 65 20 73 75 62 71 75 65 72 79 2e es the subquery.
64330 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 .**.** If the Ex
64340 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50 pr is of type OP
64350 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65 _Column, and the
64360 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c table it is sel
64370 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69 ecting from.** i
64380 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f s a disk table o
64390 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73 r the "old.*" ps
643a0 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e eudo-table, then
643b0 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20 pTab points to
643c0 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e the.** correspon
643d0 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e ding table defin
643e0 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 4c 4c ition..**.** ALL
643f0 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a 0a 2a OCATION NOTES:.*
64400 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65 63 74 *.** Expr object
64410 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f 74 20 s can use a lot
64420 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 of memory space
64430 69 6e 20 64 61 74 61 62 61 73 65 20 73 63 68 65 in database sche
64440 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c 70 20 ma. To.** help
64450 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20 72 65 reduce memory re
64460 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f 6d 65 quirements, some
64470 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20 6f 62 times an Expr ob
64480 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 ject will be.**
64490 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e 64 20 truncated. And
644a0 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6e 75 to reduce the nu
644b0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 61 mber of memory a
644c0 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f 6d 65 llocations, some
644d0 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f 72 20 times.** two or
644e0 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65 63 74 more Expr object
644f0 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 s will be stored
64500 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d 65 6d in a single mem
64510 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 0a ory allocation,.
64520 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69 74 68 ** together with
64530 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73 74 72 Expr.zToken str
64540 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ings..**.** If t
64550 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 61 6e he EP_Reduced an
64560 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 d EP_TokenOnly f
64570 6c 61 67 73 20 61 72 65 20 73 65 74 20 77 68 65 lags are set whe
64580 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f 62 6a n.** an Expr obj
64590 65 63 74 20 69 73 20 74 72 75 6e 63 61 74 65 64 ect is truncated
645a0 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64 75 63 . When EP_Reduc
645b0 65 64 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ed is set, then
645c0 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69 6c 64 all.** the child
645d0 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20 69 6e Expr objects in
645e0 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66 74 20 the Expr.pLeft
645f0 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 and Expr.pRight
64600 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72 65 20 subtrees.** are
64610 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e contained within
64620 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f 72 79 the same memory
64630 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4e 6f allocation. No
64640 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 te, however, tha
64650 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72 65 65 t.** the subtree
64660 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c 69 73 s in Expr.x.pLis
64670 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65 6c t or Expr.x.pSel
64680 65 63 74 20 61 72 65 20 61 6c 77 61 79 73 20 73 ect are always s
64690 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61 6c 6c eparately.** all
646a0 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64 6c 65 ocated, regardle
646b0 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 ss of whether or
646c0 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65 64 20 not EP_Reduced
646d0 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72 75 63 is set..*/.struc
646e0 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 t Expr {. u8 op
646f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
64700 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 /* Operation p
64710 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 erformed by this
64720 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 node */. char
64730 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 affinity;
64740 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 /* The affinit
64750 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 y of the column
64760 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f or 0 if not a co
64770 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c lumn */. u16 fl
64780 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ags;
64790 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 /* Various flag
647a0 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 62 65 6c s. EP_* See bel
647b0 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a ow */. union {.
647c0 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e char *zToken
647d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f ; /* To
647e0 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72 6f 20 ken value. Zero
647f0 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 64 terminated and d
64800 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 20 20 69 equoted */. i
64810 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20 20 20 nt iValue;
64820 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 /* Integer
64830 20 76 61 6c 75 65 20 69 66 20 45 50 5f 49 6e 74 value if EP_Int
64840 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a Value */. } u;.
64850 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 50 5f . /* If the EP_
64860 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 69 TokenOnly flag i
64870 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 s set in the Exp
64880 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 r.flags mask, th
64890 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 en no. ** space
648a0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
648b0 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c r the fields bel
648c0 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 ow this point. A
648d0 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a n attempt to. *
648e0 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 * access them wi
648f0 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 ll result in a s
64900 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 egfault or malfu
64910 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a 2a 2a nction. . *****
64920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64960 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 /.. Expr *pLeft
64970 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c ; /* L
64980 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 eft subnode */.
64990 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20 Expr *pRight;
649a0 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 /* Right
649b0 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20 75 6e subnode */. un
649c0 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72 4c 69 ion {. ExprLi
649d0 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 2f st *pList; /
649e0 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d * Function argum
649f0 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65 78 70 ents or in "<exp
64a00 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73 r> IN (<expr-lis
64a10 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 t)" */. Selec
64a20 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 t *pSelect;
64a30 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75 62 2d /* Used for sub-
64a40 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c 65 78 selects and "<ex
64a50 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63 74 3e pr> IN (<select>
64a60 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20 20 43 )" */. } x;. C
64a70 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 ollSeq *pColl;
64a80 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c /* The col
64a90 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66 20 74 lation type of t
64aa0 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30 20 2a he column or 0 *
64ab0 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45 /.. /* If the E
64ac0 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 69 P_Reduced flag i
64ad0 73 20 73 65 74 20 69 6e 20 74 68 65 20 45 78 70 s set in the Exp
64ae0 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 r.flags mask, th
64af0 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61 63 65 en no. ** space
64b00 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
64b10 72 20 74 68 65 20 66 69 65 6c 64 73 20 62 65 6c r the fields bel
64b20 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 41 ow this point. A
64b30 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 2a n attempt to. *
64b40 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20 77 69 * access them wi
64b50 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 20 73 ll result in a s
64b60 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c 66 75 egfault or malfu
64b70 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a 2a 2a nction.. ******
64b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
64bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
64bc0 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 3b 20 .. int iTable;
64bd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b /* TK
64be0 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f 72 20 _COLUMN: cursor
64bf0 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 number of table
64c00 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e 0a 20 holding column.
64c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64c20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 52 45 ** TK_RE
64c30 47 49 53 54 45 52 3a 20 72 65 67 69 73 74 65 72 GISTER: register
64c40 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 20 20 number.
64c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64c60 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 ** TK_TRIGGER:
64c70 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e 20 6f 1 -> new, 0 -> o
64c80 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 69 43 ld */. ynVar iC
64c90 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f olumn; /
64ca0 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 6f 6c * TK_COLUMN: col
64cb0 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31 20 66 umn index. -1 f
64cc0 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20 20 20 20 or rowid..
64cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
64ce0 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49 41 42 4c ** TK_VARIABL
64cf0 45 3a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 E: variable numb
64d00 65 72 20 28 61 6c 77 61 79 73 20 3e 3d 20 31 29 er (always >= 1)
64d10 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41 67 67 3b . */. i16 iAgg;
64d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
64d30 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 Which entry in
64d40 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d pAggInfo->aCol[]
64d50 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f or ->aFunc[] */
64d60 0a 20 20 69 31 36 20 69 52 69 67 68 74 4a 6f 69 . i16 iRightJoi
64d70 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20 nTable; /* If
64d80 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65 EP_FromJoin, the
64d90 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 right table of
64da0 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 75 38 the join */. u8
64db0 20 66 6c 61 67 73 32 3b 20 20 20 20 20 20 20 20 flags2;
64dc0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 73 /* Second s
64dd0 65 74 20 6f 66 20 66 6c 61 67 73 2e 20 20 45 50 et of flags. EP
64de0 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38 20 6f 70 2_... */. u8 op
64df0 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2;
64e00 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f 52 45 47 /* If a TK_REG
64e10 49 53 54 45 52 2c 20 74 68 65 20 6f 72 69 67 69 ISTER, the origi
64e20 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 45 78 70 nal value of Exp
64e30 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 r.op */. AggInf
64e40 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 20 o *pAggInfo;
64e50 20 2f 2a 20 55 73 65 64 20 62 79 20 54 4b 5f 41 /* Used by TK_A
64e60 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64 20 54 4b GG_COLUMN and TK
64e70 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 2a 2f _AGG_FUNCTION */
64e80 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 . Table *pTab;
64e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 /* Tab
64ea0 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c 55 4d 4e le for TK_COLUMN
64eb0 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 2a 2f expressions. */
64ec0 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f .#if SQLITE_MAX_
64ed0 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 69 EXPR_DEPTH>0. i
64ee0 6e 74 20 6e 48 65 69 67 68 74 3b 20 20 20 20 20 nt nHeight;
64ef0 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 /* Height
64f00 6f 66 20 74 68 65 20 74 72 65 65 20 68 65 61 64 of the tree head
64f10 65 64 20 62 79 20 74 68 69 73 20 6e 6f 64 65 20 ed by this node
64f20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
64f30 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
64f40 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e g are the meanin
64f50 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 gs of bits in th
64f60 65 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 e Expr.flags fie
64f70 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 ld..*/.#define E
64f80 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20 30 78 30 P_FromJoin 0x0
64f90 30 30 31 20 20 2f 2a 20 4f 72 69 67 69 6e 61 74 001 /* Originat
64fa0 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55 53 49 4e ed in ON or USIN
64fb0 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f G clause of a jo
64fc0 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 in */.#define EP
64fd0 5f 41 67 67 20 20 20 20 20 20 20 20 30 78 30 30 _Agg 0x00
64fe0 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 73 20 02 /* Contains
64ff0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61 67 67 72 one or more aggr
65000 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
65010 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 52 65 */.#define EP_Re
65020 73 6f 6c 76 65 64 20 20 20 30 78 30 30 30 34 20 solved 0x0004
65030 20 2f 2a 20 49 44 73 20 68 61 76 65 20 62 65 65 /* IDs have bee
65040 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 43 4f n resolved to CO
65050 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 LUMNs */.#define
65060 20 45 50 5f 45 72 72 6f 72 20 20 20 20 20 20 30 EP_Error 0
65070 78 30 30 30 38 20 20 2f 2a 20 45 78 70 72 65 73 x0008 /* Expres
65080 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 6f 6e sion contains on
65090 65 20 6f 72 20 6d 6f 72 65 20 65 72 72 6f 72 73 e or more errors
650a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 */.#define EP_D
650b0 69 73 74 69 6e 63 74 20 20 20 30 78 30 30 31 30 istinct 0x0010
650c0 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66 /* Aggregate f
650d0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 44 49 53 unction with DIS
650e0 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 2a 2f TINCT keyword */
650f0 0a 23 64 65 66 69 6e 65 20 45 50 5f 56 61 72 53 .#define EP_VarS
65100 65 6c 65 63 74 20 20 30 78 30 30 32 30 20 20 2f elect 0x0020 /
65110 2a 20 70 53 65 6c 65 63 74 20 69 73 20 63 6f 72 * pSelect is cor
65120 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20 63 6f 6e related, not con
65130 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 stant */.#define
65140 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 20 30 EP_DblQuoted 0
65150 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b 65 6e 2e x0040 /* token.
65160 7a 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 z was originally
65170 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 23 64 65 in "..." */.#de
65180 66 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e fine EP_InfixFun
65190 63 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 c 0x0080 /* Tr
651a0 75 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 ue for an infix
651b0 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 function: LIKE,
651c0 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 GLOB, etc */.#de
651d0 66 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 fine EP_ExpColla
651e0 74 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f te 0x0100 /* Co
651f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
65200 20 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 specified expli
65210 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 citly */.#define
65220 20 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 EP_AnyAff 0
65230 78 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 x0200 /* Can ta
65240 6b 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 ke a cached colu
65250 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 mn of any affini
65260 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ty */.#define EP
65270 5f 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 _FixedDest 0x04
65280 30 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 00 /* Result ne
65290 65 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 eded in a specif
652a0 69 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 ic register */.#
652b0 64 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c define EP_IntVal
652c0 75 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 ue 0x0800 /*
652d0 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f Integer value co
652e0 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e 69 56 61 ntained in u.iVa
652f0 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 lue */.#define E
65300 50 5f 78 49 73 53 65 6c 65 63 74 20 20 30 78 31 P_xIsSelect 0x1
65310 30 30 30 20 20 2f 2a 20 78 2e 70 53 65 6c 65 63 000 /* x.pSelec
65320 74 20 69 73 20 76 61 6c 69 64 20 28 6f 74 68 65 t is valid (othe
65330 72 77 69 73 65 20 78 2e 70 4c 69 73 74 20 69 73 rwise x.pList is
65340 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 45 50 ) */..#define EP
65350 5f 52 65 64 75 63 65 64 20 20 20 20 30 78 32 30 _Reduced 0x20
65360 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 00 /* Expr stru
65370 63 74 20 69 73 20 45 58 50 52 5f 52 45 44 55 43 ct is EXPR_REDUC
65380 45 44 53 49 5a 45 20 62 79 74 65 73 20 6f 6e 6c EDSIZE bytes onl
65390 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f y */.#define EP_
653a0 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78 34 30 30 TokenOnly 0x400
653b0 30 20 20 2f 2a 20 45 78 70 72 20 73 74 72 75 63 0 /* Expr struc
653c0 74 20 69 73 20 45 58 50 52 5f 54 4f 4b 45 4e 4f t is EXPR_TOKENO
653d0 4e 4c 59 53 49 5a 45 20 62 79 74 65 73 20 6f 6e NLYSIZE bytes on
653e0 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 ly */.#define EP
653f0 5f 53 74 61 74 69 63 20 20 20 20 20 30 78 38 30 _Static 0x80
65400 30 30 20 20 2f 2a 20 48 65 6c 64 20 69 6e 20 6d 00 /* Held in m
65410 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74 61 69 6e emory not obtain
65420 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
65430 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 */../*.** The f
65440 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 ollowing are the
65450 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 meanings of bit
65460 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c s in the Expr.fl
65470 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 ags2 field..*/.#
65480 64 65 66 69 6e 65 20 45 50 32 5f 4d 61 6c 6c 6f define EP2_Mallo
65490 63 65 64 54 6f 6b 65 6e 20 20 30 78 30 30 30 31 cedToken 0x0001
654a0 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 73 71 6c /* Need to sql
654b0 69 74 65 33 44 62 46 72 65 65 28 29 20 45 78 70 ite3DbFree() Exp
654c0 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23 64 65 66 r.zToken */.#def
654d0 69 6e 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 ine EP2_Irreduci
654e0 62 6c 65 20 20 20 20 30 78 30 30 30 32 20 20 2f ble 0x0002 /
654f0 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52 44 55 50 * Cannot EXPRDUP
65500 5f 52 45 44 55 43 45 20 74 68 69 73 20 45 78 70 _REDUCE this Exp
65510 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 r */../*.** The
65520 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e 65 20 73 pseudo-routine s
65530 71 6c 69 74 65 33 45 78 70 72 53 65 74 49 72 72 qlite3ExprSetIrr
65540 65 64 75 63 69 62 6c 65 20 73 65 74 73 20 74 68 educible sets th
65550 65 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c e EP2_Irreducibl
65560 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 61 6e 20 e.** flag on an
65570 65 78 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 expression struc
65580 74 75 72 65 2e 20 20 54 68 69 73 20 66 6c 61 67 ture. This flag
65590 20 69 73 20 75 73 65 64 20 66 6f 72 20 56 56 26 is used for VV&
655a0 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 A only. The.**
655b0 72 6f 75 74 69 6e 65 20 69 73 20 69 6d 70 6c 65 routine is imple
655c0 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61 63 72 mented as a macr
655d0 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77 6f 72 6b o that only work
655e0 73 20 77 68 65 6e 20 69 6e 20 64 65 62 75 67 67 s when in debugg
655f0 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20 73 6f 20 ing mode,.** so
65600 61 73 20 6e 6f 74 20 74 6f 20 62 75 72 64 65 6e as not to burden
65610 20 70 72 6f 64 75 63 74 69 6f 6e 20 63 6f 64 65 production code
65620 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
65630 54 45 5f 44 45 42 55 47 0a 23 20 64 65 66 69 6e TE_DEBUG.# defin
65640 65 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 e ExprSetIrreduc
65650 69 62 6c 65 28 58 29 20 20 28 58 29 2d 3e 66 6c ible(X) (X)->fl
65660 61 67 73 32 20 7c 3d 20 45 50 32 5f 49 72 72 65 ags2 |= EP2_Irre
65670 64 75 63 69 62 6c 65 0a 23 65 6c 73 65 0a 23 20 ducible.#else.#
65680 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49 72 define ExprSetIr
65690 72 65 64 75 63 69 62 6c 65 28 58 29 0a 23 65 6e reducible(X).#en
656a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 dif../*.** These
656b0 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 macros can be u
656c0 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 sed to test, set
656d0 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 , or clear bits
656e0 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e in the .** Expr.
656f0 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a flags field..*/.
65700 23 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 #define ExprHasP
65710 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 roperty(E,P)
65720 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 (((E)->flags&(P
65730 29 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 ))==(P)).#define
65740 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 ExprHasAnyPrope
65750 72 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d rty(E,P) (((E)-
65760 3e 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a >flags&(P))!=0).
65770 23 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 #define ExprSetP
65780 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 roperty(E,P)
65790 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 (E)->flags|=(P)
657a0 0a 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 .#define ExprCle
657b0 61 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 arProperty(E,P)
657c0 20 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 (E)->flags&=~(
657d0 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 P)../*.** Macros
657e0 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 to determine th
657f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 e number of byte
65800 73 20 72 65 71 75 69 72 65 64 20 62 79 20 61 20 s required by a
65810 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a 2a 2a 20 normal Expr .**
65820 73 74 72 75 63 74 2c 20 61 6e 20 45 78 70 72 20 struct, an Expr
65830 73 74 72 75 63 74 20 77 69 74 68 20 74 68 65 20 struct with the
65840 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67 20 EP_Reduced flag
65850 73 65 74 20 69 6e 20 45 78 70 72 2e 66 6c 61 67 set in Expr.flag
65860 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20 45 78 70 s .** and an Exp
65870 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74 68 r struct with th
65880 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 e EP_TokenOnly f
65890 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 lag set..*/.#def
658a0 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a ine EXPR_FULLSIZ
658b0 45 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 E size
658c0 6f 66 28 45 78 70 72 29 20 20 20 20 20 20 20 20 of(Expr)
658d0 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69 7a 65 20 /* Full size
658e0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f */.#define EXPR_
658f0 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 20 20 REDUCEDSIZE
65900 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72 offsetof(Expr
65910 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20 43 6f 6d ,iTable) /* Com
65920 6d 6f 6e 20 66 65 61 74 75 72 65 73 20 2a 2f 0a mon features */.
65930 23 64 65 66 69 6e 65 20 45 58 50 52 5f 54 4f 4b #define EXPR_TOK
65940 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20 20 20 20 ENONLYSIZE
65950 6f 66 66 73 65 74 6f 66 28 45 78 70 72 2c 70 4c offsetof(Expr,pL
65960 65 66 74 29 20 20 20 2f 2a 20 46 65 77 65 72 20 eft) /* Fewer
65970 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a 2f 2a 0a features */../*.
65980 2a 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 ** Flags passed
65990 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 45 78 to the sqlite3Ex
659a0 70 72 44 75 70 28 29 20 66 75 6e 63 74 69 6f 6e prDup() function
659b0 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 . See the header
659c0 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20 61 62 6f comment .** abo
659d0 76 65 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 ve sqlite3ExprDu
659e0 70 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e p() for details.
659f0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 .*/.#define EXPR
65a00 44 55 50 5f 52 45 44 55 43 45 20 20 20 20 20 20 DUP_REDUCE
65a10 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 55 73 0x0001 /* Us
65a20 65 64 20 72 65 64 75 63 65 64 2d 73 69 7a 65 20 ed reduced-size
65a30 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f 0a 0a 2f Expr nodes */../
65a40 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f 66 20 65 *.** A list of e
65a50 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 45 61 63 xpressions. Eac
65a60 68 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 h expression may
65a70 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68 61 76 65 optionally have
65a80 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20 41 6e 20 a.** name. An
65a90 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d 62 69 6e expr/name combin
65aa0 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 ation can be use
65ab0 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 d in several way
65ac0 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73 20 74 68 s, such.** as th
65ad0 65 20 6c 69 73 74 20 6f 66 20 22 65 78 70 72 20 e list of "expr
65ae0 41 53 20 49 44 22 20 66 69 65 6c 64 73 20 66 6f AS ID" fields fo
65af0 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45 4c 45 43 llowing a "SELEC
65b00 54 22 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 T" or in the.**
65b10 6c 69 73 74 20 6f 66 20 22 49 44 20 3d 20 65 78 list of "ID = ex
65b20 70 72 22 20 69 74 65 6d 73 20 69 6e 20 61 6e 20 pr" items in an
65b30 55 50 44 41 54 45 2e 20 20 41 20 6c 69 73 74 20 UPDATE. A list
65b40 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 of expressions c
65b50 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 an.** also be us
65b60 65 64 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 ed as the argume
65b70 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e nt to a function
65b80 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 , in which case
65b90 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a 2a 20 66 the a.zName.** f
65ba0 69 65 6c 64 20 69 73 20 6e 6f 74 20 75 73 65 64 ield is not used
65bb0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 70 72 ..*/.struct Expr
65bc0 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 List {. int nEx
65bd0 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 pr;
65be0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 70 /* Number of exp
65bf0 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74 68 65 20 ressions on the
65c00 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 list */. int nA
65c10 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 lloc;
65c20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
65c30 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 tries allocated
65c40 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 69 below */. int i
65c50 45 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 ECursor;
65c60 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 /* VDBE Cursor
65c70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
65c80 20 74 68 69 73 20 45 78 70 72 4c 69 73 74 20 2a this ExprList *
65c90 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c /. struct ExprL
65ca0 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 45 ist_item {. E
65cb0 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 xpr *pExpr;
65cc0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 /* The lis
65cd0 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 t of expressions
65ce0 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e */. char *zN
65cf0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f ame; /
65d00 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63 69 61 74 * Token associat
65d10 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78 70 ed with this exp
65d20 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 ression */. c
65d30 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20 20 20 20 har *zSpan;
65d40 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 /* Origina
65d50 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20 65 78 l text of the ex
65d60 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 pression */.
65d70 75 38 20 73 6f 72 74 4f 72 64 65 72 3b 20 20 20 u8 sortOrder;
65d80 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 /* 1 for
65d90 44 45 53 43 20 6f 72 20 30 20 66 6f 72 20 41 53 DESC or 0 for AS
65da0 43 20 2a 2f 0a 20 20 20 20 75 38 20 64 6f 6e 65 C */. u8 done
65db0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
65dc0 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20 69 6e 64 /* A flag to ind
65dd0 69 63 61 74 65 20 77 68 65 6e 20 70 72 6f 63 65 icate when proce
65de0 73 73 69 6e 67 20 69 73 20 66 69 6e 69 73 68 65 ssing is finishe
65df0 64 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 43 6f d */. u16 iCo
65e00 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l;
65e10 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20 42 59 2c /* For ORDER BY,
65e20 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 column number i
65e30 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a n result set */.
65e40 20 20 20 20 75 31 36 20 69 41 6c 69 61 73 3b 20 u16 iAlias;
65e50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
65e60 64 65 78 20 69 6e 74 6f 20 50 61 72 73 65 2e 61 dex into Parse.a
65e70 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a 4e 61 6d Alias[] for zNam
65e80 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20 20 20 20 e */. } *a;
65e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
65ea0 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 One entry for e
65eb0 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a ach expression *
65ec0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 /.};../*.** An i
65ed0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 nstance of this
65ee0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 structure is use
65ef0 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 d by the parser
65f00 74 6f 20 72 65 63 6f 72 64 20 62 6f 74 68 0a 2a to record both.*
65f10 2a 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 * the parse tree
65f20 20 66 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 for an expressi
65f30 6f 6e 20 61 6e 64 20 74 68 65 20 73 70 61 6e 20 on and the span
65f40 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20 66 6f of input text fo
65f50 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 r an.** expressi
65f60 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45 78 on..*/.struct Ex
65f70 70 72 53 70 61 6e 20 7b 0a 20 20 45 78 70 72 20 prSpan {. Expr
65f80 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
65f90 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 /* The expressi
65fa0 6f 6e 20 70 61 72 73 65 20 74 72 65 65 20 2a 2f on parse tree */
65fb0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
65fc0 53 74 61 72 74 3b 20 20 20 2f 2a 20 46 69 72 73 Start; /* Firs
65fd0 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 69 t character of i
65fe0 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20 63 nput text */. c
65ff0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 3b onst char *zEnd;
66000 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63 68 61 72 /* One char
66010 61 63 74 65 72 20 70 61 73 74 20 74 68 65 20 65 acter past the e
66020 6e 64 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 nd of input text
66030 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e */.};../*.** An
66040 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
66050 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 s structure can
66060 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c 69 hold a simple li
66070 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72 st of identifier
66080 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74 68 s,.** such as th
66090 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20 69 e list "a,b,c" i
660a0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 n the following
660b0 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a statements:.**.*
660c0 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e * INSERT IN
660d0 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c 55 TO t(a,b,c) VALU
660e0 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20 20 ES ...;.**
660f0 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64 78 CREATE INDEX idx
66100 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a 2a ON t(a,b,c);.**
66110 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52 49 CREATE TRI
66120 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52 45 GGER trig BEFORE
66130 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c 62 UPDATE ON t(a,b
66140 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 ,c) ...;.**.** T
66150 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78 20 he IdList.a.idx
66160 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77 68 field is used wh
66170 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72 65 en the IdList re
66180 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69 73 presents the lis
66190 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e t of.** column n
661a0 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61 62 ames after a tab
661b0 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49 4e le name in an IN
661c0 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 SERT statement.
661d0 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e In the statemen
661e0 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 t.**.** INSE
661f0 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 RT INTO t(a,b,c)
66200 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22 61 ....**.** If "a
66210 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63 6f " is the k-th co
66220 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22 74 lumn of table "t
66230 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e 61 ", then IdList.a
66240 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a 73 [0].idx==k..*/.s
66250 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a 20 truct IdList {.
66260 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f 69 struct IdList_i
66270 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a tem {. char *
66280 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e zName; /* N
66290 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e 74 ame of the ident
662a0 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 ifier */. int
662b0 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f idx; /
662c0 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65 20 * Index in some
662d0 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66 20 Table.aCol[] of
662e0 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 7a a column named z
662f0 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 0a Name */. } *a;.
66300 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20 20 int nId;
66310 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
66320 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20 74 identifiers on t
66330 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 he list */. int
66340 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a nAlloc; /*
66350 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
66360 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 es allocated for
66370 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d 3b a[] below */.};
66380 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74 6d ../*.** The bitm
66390 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65 66 ask datatype def
663a0 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75 73 ined below is us
663b0 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20 6f ed for various o
663c0 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a 2a ptimizations..**
663d0 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 69 .** Changing thi
663e0 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74 20 s from a 64-bit
663f0 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70 65 to a 32-bit type
66400 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d 62 limits the numb
66410 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 er of.** tables
66420 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32 20 in a join to 32
66430 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20 20 instead of 64.
66440 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64 75 But it also redu
66450 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 ces the size.**
66460 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 62 of the library b
66470 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20 69 y 738 bytes on i
66480 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 x86..*/.typedef
66490 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f 2a u64 Bitmask;../*
664a0 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f .** The number o
664b0 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74 6d f bits in a Bitm
664c0 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61 6e ask. "BMS" mean
664d0 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65 22 s "BitMask Size"
664e0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d 53 ..*/.#define BMS
664f0 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 ((int)(sizeof(
66500 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f 2a Bitmask)*8))../*
66510 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
66520 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 g structure desc
66530 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20 63 ribes the FROM c
66540 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 lause of a SELEC
66550 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 T statement..**
66560 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73 75 Each table or su
66570 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 bquery in the FR
66580 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20 73 OM clause is a s
66590 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74 20 eparate element
665a0 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69 73 of.** the SrcLis
665b0 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a 0a t.a[] array..**.
665c0 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64 69 ** With the addi
665d0 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c 65 tion of multiple
665e0 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
665f0 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 t, the following
66600 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63 61 structure.** ca
66610 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74 n also be used t
66620 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72 o describe a par
66630 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73 75 ticular table su
66640 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 ch as the table
66650 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69 66 that.** is modif
66660 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 ied by an INSERT
66670 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 , DELETE, or UPD
66680 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 ATE statement.
66690 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c 2c In standard SQL,
666a0 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c 65 .** such a table
666b0 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70 6c must be a simpl
666c0 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75 74 e name: ID. But
666d0 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65 20 in SQLite, the
666e0 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f 77 table can.** now
666f0 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 be identified b
66700 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61 6d y a database nam
66710 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20 74 e, a dot, then t
66720 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20 49 he table name: I
66730 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 D.ID..**.** The
66740 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73 20 jointype starts
66750 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65 20 out showing the
66760 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65 65 join type betwee
66770 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 n the current ta
66780 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e ble.** and the n
66790 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 ext table on the
667a0 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72 73 list. The pars
667b0 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c 69 er builds the li
667c0 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a 20 st this way..**
667d0 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c 69 But sqlite3SrcLi
667e0 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 stShiftJoinType(
667f0 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20 74 ) later shifts t
66800 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f 20 he jointypes so
66810 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f 69 that each.** joi
66820 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73 20 ntype expresses
66830 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e the join between
66840 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 the table and t
66850 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62 6c he previous tabl
66860 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72 63 e..*/.struct Src
66870 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53 72 List {. i16 nSr
66880 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d c; /* Num
66890 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f 72 ber of tables or
668a0 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 subqueries in t
668b0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a he FROM clause *
668c0 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b 20 /. i16 nAlloc;
668d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
668e0 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 f entries alloca
668f0 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f 77 ted in a[] below
66900 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 */. struct Src
66910 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 List_item {.
66920 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b char *zDatabase;
66930 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 /* Name of dat
66940 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 abase holding th
66950 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 is table */.
66960 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 char *zName;
66970 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 /* Name of the
66980 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63 68 table */. ch
66990 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20 20 ar *zAlias;
669a0 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74 20 /* The "B" part
669b0 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70 68 of a "A AS B" ph
669c0 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73 20 rase. zName is
669d0 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20 54 the "A" */. T
669e0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 able *pTab;
669f0 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c 65 /* An SQL table
66a00 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
66a10 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 53 o zName */. S
66a20 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 elect *pSelect;
66a30 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 /* A SELECT sta
66a40 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20 70 tement used in p
66a50 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20 lace of a table
66a60 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20 69 name */. u8 i
66a70 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f 2a sPopulated; /*
66a80 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 Temporary table
66a90 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
66aa0 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75 6c SELECT is popul
66ab0 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6a ated */. u8 j
66ac0 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f 2a ointype; /*
66ad0 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62 65 Type of join be
66ae0 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65 20 tween this able
66af0 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 and the previous
66b00 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74 49 6e */. u8 notIn
66b10 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 dexed; /* Tru
66b20 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 e if there is a
66b30 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 NOT INDEXED clau
66b40 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 se */. int iC
66b50 75 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 ursor; /* T
66b60 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e he VDBE cursor n
66b70 75 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 umber used to ac
66b80 63 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 cess this table
66b90 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e */. Expr *pOn
66ba0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
66bb0 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a ON clause of a j
66bc0 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 oin */. IdLis
66bd0 74 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 t *pUsing; /*
66be0 54 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 The USING clause
66bf0 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 of a join */.
66c00 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 Bitmask colUse
66c10 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c d; /* Bit N (1<
66c20 3c 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d <N) set if colum
66c30 6e 20 4e 20 6f 66 20 70 54 61 62 20 69 73 20 75 n N of pTab is u
66c40 73 65 64 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 sed */. char
66c50 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a 20 *zIndex; /*
66c60 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d 20 Identifier from
66c70 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49 6e "INDEXED BY <zIn
66c80 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f 0a dex>" clause */.
66c90 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 Index *pInde
66ca0 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 x; /* Index s
66cb0 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73 70 tructure corresp
66cc0 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65 78 onding to zIndex
66cd0 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d 20 , if any */. }
66ce0 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 a[1];
66cf0 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 /* One entry f
66d00 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66 69 or each identifi
66d10 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a er on the list *
66d20 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 6d /.};../*.** Perm
66d30 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66 20 itted values of
66d40 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a 6f the SrcList.a.jo
66d50 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f 0a intype field.*/.
66d60 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45 52 #define JT_INNER
66d70 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20 2f 0x0001 /
66d80 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69 6e * Any kind of in
66d90 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f 69 ner or cross joi
66da0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f n */.#define JT_
66db0 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30 32 CROSS 0x0002
66dc0 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74 20 /* Explicit
66dd0 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53 53 use of the CROSS
66de0 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 keyword */.#def
66df0 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 ine JT_NATURAL
66e00 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54 72 0x0004 /* Tr
66e10 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72 61 ue for a "natura
66e20 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 l" join */.#defi
66e30 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20 20 ne JT_LEFT
66e40 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65 66 0x0008 /* Lef
66e50 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a t outer join */.
66e60 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48 54 #define JT_RIGHT
66e70 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20 2f 0x0010 /
66e80 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a 6f * Right outer jo
66e90 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 in */.#define JT
66ea0 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30 32 _OUTER 0x002
66eb0 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55 54 0 /* The "OUT
66ec0 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20 70 ER" keyword is p
66ed0 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e resent */.#defin
66ee0 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20 30 e JT_ERROR 0
66ef0 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b 6e x0040 /* unkn
66f00 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 own or unsupport
66f10 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f 0a ed join type */.
66f20 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 50 ../*.** A WhereP
66f30 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 lan object holds
66f40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 information tha
66f50 74 20 64 65 73 63 72 69 62 65 73 20 61 20 6c 6f t describes a lo
66f60 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74 65 67 79 okup.** strategy
66f70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a ..**.** This obj
66f80 65 63 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 ect is intended
66f90 74 6f 20 62 65 20 6f 70 61 71 75 65 20 6f 75 74 to be opaque out
66fa0 73 69 64 65 20 6f 66 20 74 68 65 20 77 68 65 72 side of the wher
66fb0 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 20 49 e.c module..** I
66fc0 74 20 69 73 20 69 6e 63 6c 75 64 65 64 20 68 65 t is included he
66fd0 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68 61 74 20 re only so that
66fe0 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 20 77 69 that compiler wi
66ff0 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62 69 67 20 ll know how big
67000 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f 6e 65 20 it.** is. None
67010 6f 66 20 74 68 65 20 66 69 65 6c 64 73 20 69 6e of the fields in
67020 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 68 6f this object sho
67030 75 6c 64 20 62 65 20 75 73 65 64 20 6f 75 74 73 uld be used outs
67040 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65 20 77 68 ide of.** the wh
67050 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a 2a ere.c module..**
67060 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 65 20 75 .** Within the u
67070 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73 20 6f 6e nion, pIdx is on
67080 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46 ly used when wsF
67090 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 44 45 58 lags&WHERE_INDEX
670a0 45 44 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 70 ED is true..** p
670b0 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20 75 73 65 Term is only use
670c0 64 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57 d when wsFlags&W
670d0 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 69 73 HERE_MULTI_OR is
670e0 20 74 72 75 65 2e 20 20 41 6e 64 20 70 56 74 61 true. And pVta
670f0 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 bIdx.** is only
67100 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67 used when wsFlag
67110 73 26 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 s&WHERE_VIRTUALT
67120 41 42 4c 45 20 69 73 20 74 72 75 65 2e 20 20 49 ABLE is true. I
67130 74 20 69 73 20 6e 65 76 65 72 20 74 68 65 0a 2a t is never the.*
67140 2a 20 63 61 73 65 20 74 68 61 74 20 6d 6f 72 65 * case that more
67150 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 than one of the
67160 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 se conditions is
67170 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 true..*/.struct
67180 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a 20 20 75 WherePlan {. u
67190 33 32 20 77 73 46 6c 61 67 73 3b 20 20 20 20 20 32 wsFlags;
671a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
671b0 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 74 WHERE_* flags t
671c0 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 hat describe the
671d0 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 75 strategy */. u
671e0 33 32 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 32 nEq;
671f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
67200 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 63 6f Number of == co
67210 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 75 nstraints */. u
67220 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e 64 65 78 nion {. Index
67230 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 *pIdx;
67240 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
67250 65 78 20 77 68 65 6e 20 57 48 45 52 45 5f 49 4e ex when WHERE_IN
67260 44 45 58 45 44 20 69 73 20 74 72 75 65 20 2a 2f DEXED is true */
67270 0a 20 20 20 20 73 74 72 75 63 74 20 57 68 65 72 . struct Wher
67280 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 eTerm *pTerm;
67290 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 /* WHERE cla
672a0 75 73 65 20 74 65 72 6d 20 66 6f 72 20 4f 52 2d use term for OR-
672b0 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 73 71 search */. sq
672c0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
672d0 20 2a 70 56 74 61 62 49 64 78 3b 20 20 2f 2a 20 *pVtabIdx; /*
672e0 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e Virtual table in
672f0 64 65 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 dex to use */.
67300 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 } u;.};../*.** F
67310 6f 72 20 65 61 63 68 20 6e 65 73 74 65 64 20 6c or each nested l
67320 6f 6f 70 20 69 6e 20 61 20 57 48 45 52 45 20 63 oop in a WHERE c
67330 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65 6e 74 61 lause implementa
67340 74 69 6f 6e 2c 20 74 68 65 20 57 68 65 72 65 49 tion, the WhereI
67350 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 nfo.** structure
67360 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 contains a sing
67370 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 le instance of t
67380 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 his structure.
67390 54 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a This structure.*
673a0 2a 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f * is intended to
673b0 20 62 65 20 70 72 69 76 61 74 65 20 74 68 65 20 be private the
673c0 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 the where.c modu
673d0 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f le and should no
673e0 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 20 6f t be.** access o
673f0 72 20 6d 6f 64 69 66 69 65 64 20 62 79 20 6f 74 r modified by ot
67400 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a 2a 2a 0a her modules..**.
67410 2a 2a 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 ** The pIdxInfo
67420 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 74 6f field is used to
67430 20 68 65 6c 70 20 70 69 63 6b 20 74 68 65 20 62 help pick the b
67440 65 73 74 20 69 6e 64 65 78 20 6f 6e 20 61 0a 2a est index on a.*
67450 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e * virtual table.
67460 20 20 54 68 65 20 70 49 64 78 49 6e 66 6f 20 70 The pIdxInfo p
67470 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 ointer contains
67480 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69 6e 66 6f indexing.** info
67490 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 rmation for the
674a0 69 2d 74 68 20 74 61 62 6c 65 20 69 6e 20 74 68 i-th table in th
674b0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 62 65 e FROM clause be
674c0 66 6f 72 65 20 72 65 6f 72 64 65 72 69 6e 67 2e fore reordering.
674d0 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70 49 64 78 .** All the pIdx
674e0 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73 20 61 72 Info pointers ar
674f0 65 20 66 72 65 65 64 20 62 79 20 77 68 65 72 65 e freed by where
67500 49 6e 66 6f 46 72 65 65 28 29 20 69 6e 20 77 68 InfoFree() in wh
67510 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 ere.c..** All ot
67520 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 her information
67530 69 6e 20 74 68 65 20 69 2d 74 68 20 57 68 65 72 in the i-th Wher
67540 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 66 6f eLevel object fo
67550 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 r the i-th table
67560 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f 4d 20 63 .** after FROM c
67570 6c 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 0a lause ordering..
67580 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 4c */.struct WhereL
67590 65 76 65 6c 20 7b 0a 20 20 57 68 65 72 65 50 6c evel {. WherePl
675a0 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20 20 20 2f an plan; /
675b0 2a 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 * query plan for
675c0 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 6f 66 this element of
675d0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
675e0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 4a */. int iLeftJ
675f0 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d oin; /* M
67600 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 emory cell used
67610 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 4c 45 46 to implement LEF
67620 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 2a 2f 0a T OUTER JOIN */.
67630 20 20 69 6e 74 20 69 54 61 62 43 75 72 3b 20 20 int iTabCur;
67640 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
67650 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 DBE cursor used
67660 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 74 61 to access the ta
67670 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 ble */. int iId
67680 78 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f xCur; /
67690 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
676a0 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 r used to access
676b0 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 61 pIdx */. int a
676c0 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20 ddrBrk;
676d0 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f /* Jump here to
676e0 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 break out of th
676f0 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 e loop */. int
67700 61 64 64 72 4e 78 74 3b 20 20 20 20 20 20 20 20 addrNxt;
67710 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
67720 6f 20 73 74 61 72 74 20 74 68 65 20 6e 65 78 74 o start the next
67730 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 IN combination
67740 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e */. int addrCon
67750 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 t; /* Ju
67760 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 mp here to conti
67770 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 nue with the nex
67780 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20 2a 2f 0a t loop cycle */.
67790 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 3b int addrFirst;
677a0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
677b0 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 instruction of
677c0 69 6e 74 65 72 69 6f 72 20 6f 66 20 74 68 65 20 interior of the
677d0 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20 69 46 72 loop */. u8 iFr
677e0 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 om;
677f0 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 /* Which entry i
67800 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 n the FROM claus
67810 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 70 35 e */. u8 op, p5
67820 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
67830 4f 70 63 6f 64 65 20 61 6e 64 20 50 35 20 6f 66 Opcode and P5 of
67840 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68 61 74 the opcode that
67850 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a ends the loop *
67860 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70 32 3b 20 /. int p1, p2;
67870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 /* Ope
67880 72 61 6e 64 73 20 6f 66 20 74 68 65 20 6f 70 63 rands of the opc
67890 6f 64 65 20 75 73 65 64 20 74 6f 20 65 6e 64 73 ode used to ends
678a0 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 the loop */. u
678b0 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 nion {
678c0 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
678d0 69 6f 6e 20 74 68 61 74 20 64 65 70 65 6e 64 73 ion that depends
678e0 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 on plan.wsFlags
678f0 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b */. struct {
67900 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20 . int nIn;
67910 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
67920 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 Number of entrie
67930 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a s in aInLoop[] *
67940 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49 /. struct I
67950 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20 nLoop {.
67960 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 int iCur;
67970 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 /* The VD
67980 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62 BE cursor used b
67990 79 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74 y this IN operat
679a0 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e or */. in
679b0 74 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20 t addrInTop;
679c0 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 /* Top of t
679d0 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 he IN loop */.
679e0 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f 70 3b 20 } *aInLoop;
679f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 /* Inf
67a00 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 ormation about e
67a10 61 63 68 20 6e 65 73 74 65 64 20 49 4e 20 6f 70 ach nested IN op
67a20 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 7d 20 erator */. }
67a30 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 in;
67a40 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e /* Used when
67a50 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 plan.wsFlags&WH
67a60 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20 ERE_IN_ABLE */.
67a70 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 } u;.. /* The
67a80 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 following field
67a90 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 is really not pa
67aa0 72 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e rt of the curren
67ab0 74 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 t level. But.
67ac0 2a 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 ** we need a pla
67ad0 63 65 20 74 6f 20 63 61 63 68 65 20 76 69 72 74 ce to cache virt
67ae0 75 61 6c 20 74 61 62 6c 65 20 69 6e 64 65 78 20 ual table index
67af0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
67b00 65 61 63 68 0a 20 20 2a 2a 20 76 69 72 74 75 61 each. ** virtua
67b10 6c 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 l table in the F
67b20 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e 64 20 74 ROM clause and t
67b30 68 65 20 57 68 65 72 65 4c 65 76 65 6c 20 73 74 he WhereLevel st
67b40 72 75 63 74 75 72 65 20 69 73 0a 20 20 2a 2a 20 ructure is. **
67b50 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20 70 6c 61 a convenient pla
67b60 63 65 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 ce since there i
67b70 73 20 6f 6e 65 20 57 68 65 72 65 4c 65 76 65 6c s one WhereLevel
67b80 20 66 6f 72 20 65 61 63 68 20 46 52 4f 4d 20 63 for each FROM c
67b90 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c 65 6d 65 lause. ** eleme
67ba0 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 nt.. */. sqlit
67bb0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
67bc0 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20 49 6e 64 IdxInfo; /* Ind
67bd0 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e 2d 74 68 ex info for n-th
67be0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f source table */
67bf0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 .};../*.** Flags
67c00 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 appropriate for
67c10 20 74 68 65 20 77 63 74 72 6c 46 6c 61 67 73 20 the wctrlFlags
67c20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c parameter of sql
67c30 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
67c40 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 .** and the Wher
67c50 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c 61 67 73 eInfo.wctrlFlags
67c60 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 member..*/.#def
67c70 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 ine WHERE_ORDERB
67c80 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78 30 30 30 Y_NORMAL 0x000
67c90 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 23 64 0 /* No-op */.#d
67ca0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 efine WHERE_ORDE
67cb0 52 42 59 5f 4d 49 4e 20 20 20 20 20 20 30 78 30 RBY_MIN 0x0
67cc0 30 30 31 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 001 /* ORDER BY
67cd0 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d processing for m
67ce0 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 in() func */.#de
67cf0 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 fine WHERE_ORDER
67d00 42 59 5f 4d 41 58 20 20 20 20 20 20 30 78 30 30 BY_MAX 0x00
67d10 30 32 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 70 02 /* ORDER BY p
67d20 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 6d 61 rocessing for ma
67d30 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23 64 65 66 x() func */.#def
67d40 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 ine WHERE_ONEPAS
67d50 53 5f 44 45 53 49 52 45 44 20 20 30 78 30 30 30 S_DESIRED 0x000
67d60 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20 64 6f 20 4 /* Want to do
67d70 6f 6e 65 2d 70 61 73 73 20 55 50 44 41 54 45 2f one-pass UPDATE/
67d80 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65 66 69 6e DELETE */.#defin
67d90 65 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 e WHERE_DUPLICAT
67da0 45 53 5f 4f 4b 20 20 20 20 30 78 30 30 30 38 20 ES_OK 0x0008
67db0 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75 72 6e 20 /* Ok to return
67dc0 61 20 72 6f 77 20 6d 6f 72 65 20 74 68 61 6e 20 a row more than
67dd0 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 once */.#define
67de0 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 WHERE_OMIT_OPEN
67df0 20 20 20 20 20 20 20 30 78 30 30 31 30 20 2f 2a 0x0010 /*
67e00 20 54 61 62 6c 65 20 63 75 72 73 6f 72 20 61 72 Table cursor ar
67e10 65 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 2a e already open *
67e20 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
67e30 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20 20 20 20 OMIT_CLOSE
67e40 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d 69 74 20 0x0020 /* Omit
67e50 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c 65 20 26 close of table &
67e60 20 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a index cursors *
67e70 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
67e80 46 4f 52 43 45 5f 54 41 42 4c 45 20 20 20 20 20 FORCE_TABLE
67e90 20 30 78 30 30 34 30 20 2f 2a 20 44 6f 20 6e 6f 0x0040 /* Do no
67ea0 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 2d 6f t use an index-o
67eb0 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f 0a 0a 2f nly search */../
67ec0 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 63 *.** The WHERE c
67ed0 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 lause processing
67ee0 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 6f routine has two
67ef0 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a 2a halves. The.**
67f00 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 73 first part does
67f10 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
67f20 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e 64 e WHERE loop and
67f30 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 68 the second.** h
67f40 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 69 alf does the tai
67f50 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c l of the WHERE l
67f60 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e 63 oop. An instanc
67f70 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 e of.** this str
67f80 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 6e ucture is return
67f90 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 20 ed by the first
67fa0 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 0a half and passed.
67fb0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 6f ** into the seco
67fc0 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 20 nd half to give
67fd0 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 2e some continuity.
67fe0 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 .*/.struct Where
67ff0 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20 2a Info {. Parse *
68000 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a pParse; /*
68010 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 Parsing and cod
68020 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
68030 74 65 78 74 20 2a 2f 0a 20 20 75 31 36 20 77 63 text */. u16 wc
68040 74 72 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 2f trlFlags; /
68050 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 6c * Flags original
68060 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c ly passed to sql
68070 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 ite3WhereBegin()
68080 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 61 */. u8 okOnePa
68090 73 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6b ss; /* Ok
680a0 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70 61 73 73 to use one-pass
680b0 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 55 algorithm for U
680c0 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 PDATE or DELETE
680d0 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 */. SrcList *pT
680e0 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 abList;
680f0 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 /* List of t
68100 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f 69 ables in the joi
68110 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 3b n */. int iTop;
68120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
68130 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65 72 /* The ver
68140 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 y beginning of t
68150 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a 2f he WHERE loop */
68160 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 . int iContinue
68170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
68180 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
68190 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 o continue with
681a0 6e 65 78 74 20 72 65 63 6f 72 64 20 2a 2f 0a 20 next record */.
681b0 20 69 6e 74 20 69 42 72 65 61 6b 3b 20 20 20 20 int iBreak;
681c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
681d0 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 /* Jump here to
681e0 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 break out of the
681f0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e loop */. int n
68200 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20 20 20 Level;
68210 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
68220 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f ber of nested lo
68230 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 57 op */. struct W
68240 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b hereClause *pWC;
68250 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 /* Decomp
68260 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 osition of the W
68270 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
68280 20 57 68 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d WhereLevel a[1]
68290 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
682a0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
682b0 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20 6c bout each nest l
682c0 6f 6f 70 20 69 6e 20 57 48 45 52 45 20 2a 2f 0a oop in WHERE */.
682d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 61 6d 65 };../*.** A Name
682e0 43 6f 6e 74 65 78 74 20 64 65 66 69 6e 65 73 20 Context defines
682f0 61 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 a context in whi
68300 63 68 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 61 ch to resolve ta
68310 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 2a ble and column.*
68320 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 63 6f * names. The co
68330 6e 74 65 78 74 20 63 6f 6e 73 69 73 74 73 20 6f ntext consists o
68340 66 20 61 20 6c 69 73 74 20 6f 66 20 74 61 62 6c f a list of tabl
68350 65 73 20 28 74 68 65 20 70 53 72 63 4c 69 73 74 es (the pSrcList
68360 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a 2a 20 61 ) field and.** a
68370 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 list of named e
68380 78 70 72 65 73 73 69 6f 6e 20 28 70 45 4c 69 73 xpression (pELis
68390 74 29 2e 20 20 54 68 65 20 6e 61 6d 65 64 20 65 t). The named e
683a0 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 6d xpression list m
683b0 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c 2e 20 20 ay.** be NULL.
683c0 54 68 65 20 70 53 72 63 20 63 6f 72 72 65 73 70 The pSrc corresp
683d0 6f 6e 64 73 20 74 6f 20 74 68 65 20 46 52 4f 4d onds to the FROM
683e0 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c clause of a SEL
683f0 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20 74 68 65 ECT or.** to the
68400 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6f 70 65 table being ope
68410 72 61 74 65 64 20 6f 6e 20 62 79 20 49 4e 53 45 rated on by INSE
68420 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 RT, UPDATE, or D
68430 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a 2a 20 70 ELETE. The.** p
68440 45 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 EList correspond
68450 73 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20 s to the result
68460 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 set of a SELECT
68470 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66 6f 72 0a and is NULL for.
68480 2a 2a 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 ** other stateme
68490 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 43 nts..**.** NameC
684a0 6f 6e 74 65 78 74 73 20 63 61 6e 20 62 65 20 6e ontexts can be n
684b0 65 73 74 65 64 2e 20 20 57 68 65 6e 20 72 65 73 ested. When res
684c0 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c 20 74 68 olving names, th
684d0 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 0a 2a 2a e inner-most .**
684e0 20 63 6f 6e 74 65 78 74 20 69 73 20 73 65 61 72 context is sear
684f0 63 68 65 64 20 66 69 72 73 74 2e 20 20 49 66 20 ched first. If
68500 6e 6f 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e no match is foun
68510 64 2c 20 74 68 65 20 6e 65 78 74 20 6f 75 74 65 d, the next oute
68520 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 r.** context is
68530 63 68 65 63 6b 65 64 2e 20 20 49 66 20 74 68 65 checked. If the
68540 72 65 20 69 73 20 73 74 69 6c 6c 20 6e 6f 20 6d re is still no m
68550 61 74 63 68 2c 20 74 68 65 20 6e 65 78 74 20 63 atch, the next c
68560 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20 63 68 65 ontext.** is che
68570 63 6b 65 64 2e 20 20 54 68 69 73 20 70 72 6f 63 cked. This proc
68580 65 73 73 20 63 6f 6e 74 69 6e 75 65 73 20 75 6e ess continues un
68590 74 69 6c 20 65 69 74 68 65 72 20 61 20 6d 61 74 til either a mat
685a0 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a 2a 20 6f ch is found.** o
685b0 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74 73 20 61 r all contexts a
685c0 72 65 20 63 68 65 63 6b 2e 20 20 57 68 65 6e 20 re check. When
685d0 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 a match is found
685e0 2c 20 74 68 65 20 6e 52 65 66 20 6d 65 6d 62 65 , the nRef membe
685f0 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 r of.** the cont
68600 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ext containing t
68610 68 65 20 6d 61 74 63 68 20 69 73 20 69 6e 63 72 he match is incr
68620 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 emented. .**.**
68630 45 61 63 68 20 73 75 62 71 75 65 72 79 20 67 65 Each subquery ge
68640 74 73 20 61 20 6e 65 77 20 4e 61 6d 65 43 6f 6e ts a new NameCon
68650 74 65 78 74 2e 20 20 54 68 65 20 70 4e 65 78 74 text. The pNext
68660 20 66 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f field points to
68670 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 the.** NameCont
68680 65 78 74 20 69 6e 20 74 68 65 20 70 61 72 65 6e ext in the paren
68690 74 20 71 75 65 72 79 2e 20 20 54 68 75 73 20 74 t query. Thus t
686a0 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 73 63 he process of sc
686b0 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 4e 61 anning the.** Na
686c0 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73 74 20 63 meContext list c
686d0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 73 65 orresponds to se
686e0 61 72 63 68 69 6e 67 20 74 68 72 6f 75 67 68 20 arching through
686f0 73 75 63 63 65 73 73 69 76 65 6c 79 20 6f 75 74 successively out
68700 65 72 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 er.** subqueries
68710 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 6d looking for a m
68720 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 atch..*/.struct
68730 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b 0a 20 20 NameContext {.
68740 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 Parse *pParse;
68750 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 /* The pars
68760 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 er */. SrcList
68770 2a 70 53 72 63 4c 69 73 74 3b 20 20 20 2f 2a 20 *pSrcList; /*
68780 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 61 62 6c One or more tabl
68790 65 73 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c es used to resol
687a0 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 45 78 ve names */. Ex
687b0 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 prList *pEList;
687c0 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 6c /* Optional l
687d0 69 73 74 20 6f 66 20 6e 61 6d 65 64 20 65 78 70 ist of named exp
687e0 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e ressions */. in
687f0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 t nRef;
68800 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
68810 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65 64 20 62 names resolved b
68820 79 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 2a y this context *
68830 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 /. int nErr;
68840 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
68850 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 er of errors enc
68860 6f 75 6e 74 65 72 65 64 20 77 68 69 6c 65 20 72 ountered while r
68870 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 2a esolving names *
68880 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41 67 67 3b /. u8 allowAgg;
68890 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 /* Aggr
688a0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 egate functions
688b0 61 6c 6c 6f 77 65 64 20 68 65 72 65 20 2a 2f 0a allowed here */.
688c0 20 20 75 38 20 68 61 73 41 67 67 3b 20 20 20 20 u8 hasAgg;
688d0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
688e0 66 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 f aggregates are
688f0 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 69 73 seen */. u8 is
68900 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20 20 20 Check;
68910 2f 2a 20 54 72 75 65 20 69 66 20 72 65 73 6f 6c /* True if resol
68920 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20 61 20 ving names in a
68930 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 CHECK constraint
68940 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65 70 74 68 */. int nDepth
68950 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 ; /* De
68960 70 74 68 20 6f 66 20 73 75 62 71 75 65 72 79 20 pth of subquery
68970 72 65 63 75 72 73 69 6f 6e 2e 20 31 20 66 6f 72 recursion. 1 for
68980 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f no recursion */
68990 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 . AggInfo *pAgg
689a0 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e 66 6f 72 Info; /* Infor
689b0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 67 67 mation about agg
689c0 72 65 67 61 74 65 73 20 61 74 20 74 68 69 73 20 regates at this
689d0 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61 6d 65 43 level */. NameC
689e0 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74 3b 20 20 ontext *pNext;
689f0 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72 20 6e 61 /* Next outer na
68a00 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 4e 55 4c me context. NUL
68a10 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f 73 74 20 L for outermost
68a20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
68a30 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
68a40 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
68a50 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c ure contains all
68a60 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
68a70 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65 72 61 needed to genera
68a80 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 te code for a si
68a90 6e 67 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 ngle SELECT stat
68aa0 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 6e 4c 69 ement..**.** nLi
68ab0 6d 69 74 20 69 73 20 73 65 74 20 74 6f 20 2d 31 mit is set to -1
68ac0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
68ad0 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 6e LIMIT clause. n
68ae0 4f 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f Offset is set to
68af0 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 0..** If there
68b00 69 73 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 is a LIMIT claus
68b10 65 2c 20 74 68 65 20 70 61 72 73 65 72 20 73 65 e, the parser se
68b20 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20 74 68 65 ts nLimit to the
68b30 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a value of the.**
68b40 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 limit and nOffs
68b50 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 et to the value
68b60 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 28 6f of the offset (o
68b70 72 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 r 0 if there is
68b80 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74 29 2e 20 not.** offset).
68b90 20 42 75 74 20 6c 61 74 65 72 20 6f 6e 2c 20 6e But later on, n
68ba0 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66 66 73 65 Limit and nOffse
68bb0 74 20 62 65 63 6f 6d 65 20 74 68 65 20 6d 65 6d t become the mem
68bc0 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a ory locations.**
68bd0 20 69 6e 20 74 68 65 20 56 44 42 45 20 74 68 61 in the VDBE tha
68be0 74 20 72 65 63 6f 72 64 20 74 68 65 20 6c 69 6d t record the lim
68bf0 69 74 20 61 6e 64 20 6f 66 66 73 65 74 20 63 6f it and offset co
68c00 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 61 64 unters..**.** ad
68c10 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20 65 6e 74 drOpenEphm[] ent
68c20 72 69 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 ries contain the
68c30 20 61 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f address of OP_O
68c40 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 penEphemeral opc
68c50 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73 65 20 61 odes..** These a
68c60 64 64 72 65 73 73 65 73 20 6d 75 73 74 20 62 65 ddresses must be
68c70 20 73 74 6f 72 65 64 20 73 6f 20 74 68 61 74 20 stored so that
68c80 77 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 61 we can go back a
68c90 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a 20 74 68 nd fill in.** th
68ca0 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20 61 6e 64 e P4_KEYINFO and
68cb0 20 50 32 20 70 61 72 61 6d 65 74 65 72 73 20 6c P2 parameters l
68cc0 61 74 65 72 2e 20 20 4e 65 69 74 68 65 72 20 74 ater. Neither t
68cd0 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f 72 0a 2a he KeyInfo nor.*
68ce0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
68cf0 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32 20 63 61 columns in P2 ca
68d00 6e 20 62 65 20 63 6f 6d 70 75 74 65 64 20 61 74 n be computed at
68d10 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 0a 2a the same time.*
68d20 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f 70 65 6e * as the OP_Open
68d30 45 70 68 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e Ephm instruction
68d40 20 69 73 20 63 6f 64 65 64 20 62 65 63 61 75 73 is coded becaus
68d50 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 e not.** enough
68d60 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 information abou
68d70 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 t the compound q
68d80 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e 20 61 74 uery is known at
68d90 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2a 20 that point..**
68da0 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 The KeyInfo for
68db0 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 30 5d 20 addrOpenTran[0]
68dc0 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61 69 6e 73 and [1] contains
68dd0 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
68de0 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74 68 65 20 nces.** for the
68df0 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 65 result set. The
68e00 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 61 64 64 KeyInfo for add
68e10 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20 63 6f 6e rOpenTran[2] con
68e20 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 0a tains collating.
68e30 2a 2a 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72 ** sequences for
68e40 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
68e50 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 ause..*/.struct
68e60 53 65 6c 65 63 74 20 7b 0a 20 20 45 78 70 72 4c Select {. ExprL
68e70 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 ist *pEList;
68e80 20 20 2f 2a 20 54 68 65 20 66 69 65 6c 64 73 20 /* The fields
68e90 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f of the result */
68ea0 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 . u8 op;
68eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 /* One
68ec0 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e 20 54 4b of: TK_UNION TK
68ed0 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52 53 45 43 _ALL TK_INTERSEC
68ee0 54 20 54 4b 5f 45 58 43 45 50 54 20 2a 2f 0a 20 T TK_EXCEPT */.
68ef0 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 char affinity;
68f00 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 52 /* MakeR
68f10 65 63 6f 72 64 20 77 69 74 68 20 74 68 69 73 20 ecord with this
68f20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 53 52 54 affinity for SRT
68f30 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36 20 73 65 _Set */. u16 se
68f40 6c 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 lFlags;
68f50 20 2f 2a 20 56 61 72 69 6f 75 73 20 53 46 5f 2a /* Various SF_*
68f60 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 53 72 63 values */. Src
68f70 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 List *pSrc;
68f80 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 /* The FROM
68f90 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
68fa0 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 *pWhere;
68fb0 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 /* The WHERE
68fc0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 clause */. Expr
68fd0 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 List *pGroupBy;
68fe0 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 /* The GROUP
68ff0 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 BY clause */. E
69000 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 xpr *pHaving;
69010 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 /* The HAV
69020 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ING clause */.
69030 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
69040 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 By; /* The OR
69050 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f DER BY clause */
69060 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f . Select *pPrio
69070 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 r; /* Pri
69080 6f 72 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 or select in a c
69090 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 ompound select s
690a0 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 tatement */. Se
690b0 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 lect *pNext;
690c0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 73 65 6c /* Next sel
690d0 65 63 74 20 74 6f 20 74 68 65 20 6c 65 66 74 20 ect to the left
690e0 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f in a compound */
690f0 0a 20 20 53 65 6c 65 63 74 20 2a 70 52 69 67 68 . Select *pRigh
69100 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20 52 69 67 tmost; /* Rig
69110 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 ht-most select i
69120 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c n a compound sel
69130 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ect statement */
69140 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b . Expr *pLimit;
69150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d /* LIM
69160 49 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e IT expression. N
69170 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 ULL means not us
69180 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ed. */. Expr *p
69190 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 Offset;
691a0 2f 2a 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 /* OFFSET expres
691b0 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 sion. NULL means
691c0 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 not used. */.
691d0 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 int iLimit, iOff
691e0 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 set; /* Memory
691f0 20 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 registers holdi
69200 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46 46 53 45 ng LIMIT & OFFSE
69210 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 T counters */.
69220 69 6e 74 20 61 64 64 72 4f 70 65 6e 45 70 68 6d int addrOpenEphm
69230 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 [3]; /* OP_Ope
69240 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65 73 20 72 nEphem opcodes r
69250 65 6c 61 74 65 64 20 74 6f 20 74 68 69 73 20 73 elated to this s
69260 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a elect */.};../*.
69270 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 ** Allowed value
69280 73 20 66 6f 72 20 53 65 6c 65 63 74 2e 73 65 6c s for Select.sel
69290 46 6c 61 67 73 2e 20 20 54 68 65 20 22 53 46 22 Flags. The "SF"
692a0 20 70 72 65 66 69 78 20 73 74 61 6e 64 73 20 66 prefix stands f
692b0 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74 20 46 6c or.** "Select Fl
692c0 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ag"..*/.#define
692d0 53 46 5f 44 69 73 74 69 6e 63 74 20 20 20 20 20 SF_Distinct
692e0 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 75 0x0001 /* Ou
692f0 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 44 tput should be D
69300 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64 65 66 69 ISTINCT */.#defi
69310 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 20 ne SF_Resolved
69320 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 0x0002 /*
69330 20 49 64 65 6e 74 69 66 69 65 72 73 20 68 61 76 Identifiers hav
69340 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 e been resolved
69350 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 41 67 */.#define SF_Ag
69360 67 72 65 67 61 74 65 20 20 20 20 20 20 20 30 78 gregate 0x
69370 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74 61 69 6e 0004 /* Contain
69380 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 s aggregate func
69390 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 tions */.#define
693a0 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 SF_UsesEphemera
693b0 6c 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 55 l 0x0008 /* U
693c0 73 65 73 20 74 68 65 20 4f 70 65 6e 45 70 68 65 ses the OpenEphe
693d0 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 2a 2f 0a meral opcode */.
693e0 23 64 65 66 69 6e 65 20 53 46 5f 45 78 70 61 6e #define SF_Expan
693f0 64 65 64 20 20 20 20 20 20 20 20 30 78 30 30 31 ded 0x001
69400 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65 6c 0 /* sqlite3Sel
69410 65 63 74 45 78 70 61 6e 64 28 29 20 63 61 6c 6c ectExpand() call
69420 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f 0a 23 64 ed on this */.#d
69430 65 66 69 6e 65 20 53 46 5f 48 61 73 54 79 70 65 efine SF_HasType
69440 49 6e 66 6f 20 20 20 20 20 30 78 30 30 32 30 20 Info 0x0020
69450 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71 75 65 72 /* FROM subquer
69460 69 65 73 20 68 61 76 65 20 54 61 62 6c 65 20 6d ies have Table m
69470 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a 2f 2a 0a etadata */.../*.
69480 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f ** The results o
69490 66 20 61 20 73 65 6c 65 63 74 20 63 61 6e 20 62 f a select can b
694a0 65 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e e distributed in
694b0 20 73 65 76 65 72 61 6c 20 77 61 79 73 2e 20 20 several ways.
694c0 54 68 65 0a 2a 2a 20 22 53 52 54 22 20 70 72 65 The.** "SRT" pre
694d0 66 69 78 20 6d 65 61 6e 73 20 22 53 45 4c 45 43 fix means "SELEC
694e0 54 20 52 65 73 75 6c 74 20 54 79 70 65 22 2e 0a T Result Type"..
694f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 55 */.#define SRT_U
69500 6e 69 6f 6e 20 20 20 20 20 20 20 20 31 20 20 2f nion 1 /
69510 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 * Store result a
69520 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 s keys in an ind
69530 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 ex */.#define SR
69540 54 5f 45 78 63 65 70 74 20 20 20 20 20 20 20 32 T_Except 2
69550 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72 65 73 75 /* Remove resu
69560 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49 4f 4e 20 lt from a UNION
69570 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 index */.#define
69580 20 53 52 54 5f 45 78 69 73 74 73 20 20 20 20 20 SRT_Exists
69590 20 20 33 20 20 2f 2a 20 53 74 6f 72 65 20 31 20 3 /* Store 1
695a0 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 if the result is
695b0 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f 0a 23 64 not empty */.#d
695c0 65 66 69 6e 65 20 53 52 54 5f 44 69 73 63 61 72 efine SRT_Discar
695d0 64 20 20 20 20 20 20 34 20 20 2f 2a 20 44 6f 20 d 4 /* Do
695e0 6e 6f 74 20 73 61 76 65 20 74 68 65 20 72 65 73 not save the res
695f0 75 6c 74 73 20 61 6e 79 77 68 65 72 65 20 2a 2f ults anywhere */
69600 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 ../* The ORDER B
69610 59 20 63 6c 61 75 73 65 20 69 73 20 69 67 6e 6f Y clause is igno
69620 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 red for all of t
69630 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23 64 65 66 he above */.#def
69640 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 ine IgnorableOrd
69650 65 72 62 79 28 58 29 20 28 28 58 2d 3e 65 44 65 erby(X) ((X->eDe
69660 73 74 29 3c 3d 53 52 54 5f 44 69 73 63 61 72 64 st)<=SRT_Discard
69670 29 0a 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4f )..#define SRT_O
69680 75 74 70 75 74 20 20 20 20 20 20 20 35 20 20 2f utput 5 /
69690 2a 20 4f 75 74 70 75 74 20 65 61 63 68 20 72 6f * Output each ro
696a0 77 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 23 w of result */.#
696b0 64 65 66 69 6e 65 20 53 52 54 5f 4d 65 6d 20 20 define SRT_Mem
696c0 20 20 20 20 20 20 20 20 36 20 20 2f 2a 20 53 74 6 /* St
696d0 6f 72 65 20 72 65 73 75 6c 74 20 69 6e 20 61 20 ore result in a
696e0 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a 2f 0a 23 memory cell */.#
696f0 64 65 66 69 6e 65 20 53 52 54 5f 53 65 74 20 20 define SRT_Set
69700 20 20 20 20 20 20 20 20 37 20 20 2f 2a 20 53 74 7 /* St
69710 6f 72 65 20 72 65 73 75 6c 74 73 20 61 73 20 6b ore results as k
69720 65 79 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 20 eys in an index
69730 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 54 */.#define SRT_T
69740 61 62 6c 65 20 20 20 20 20 20 20 20 38 20 20 2f able 8 /
69750 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 61 * Store result a
69760 73 20 64 61 74 61 20 77 69 74 68 20 61 6e 20 61 s data with an a
69770 75 74 6f 6d 61 74 69 63 20 72 6f 77 69 64 20 2a utomatic rowid *
69780 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 70 /.#define SRT_Ep
69790 68 65 6d 54 61 62 20 20 20 20 20 39 20 20 2f 2a hemTab 9 /*
697a0 20 43 72 65 61 74 65 20 74 72 61 6e 73 69 65 6e Create transien
697b0 74 20 74 61 62 20 61 6e 64 20 73 74 6f 72 65 20 t tab and store
697c0 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 2a like SRT_Table *
697d0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 43 6f /.#define SRT_Co
697e0 72 6f 75 74 69 6e 65 20 20 20 31 30 20 20 2f 2a routine 10 /*
697f0 20 47 65 6e 65 72 61 74 65 20 61 20 73 69 6e 67 Generate a sing
69800 6c 65 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 le row of result
69810 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 */../*.** A str
69820 75 63 74 75 72 65 20 75 73 65 64 20 74 6f 20 63 ucture used to c
69830 75 73 74 6f 6d 69 7a 65 20 74 68 65 20 62 65 68 ustomize the beh
69840 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 avior of sqlite3
69850 53 65 6c 65 63 74 28 29 2e 20 53 65 65 0a 2a 2a Select(). See.**
69860 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 comments above
69870 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 sqlite3Select()
69880 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a for details..*/.
69890 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 typedef struct S
698a0 65 6c 65 63 74 44 65 73 74 20 53 65 6c 65 63 74 electDest Select
698b0 44 65 73 74 3b 0a 73 74 72 75 63 74 20 53 65 6c Dest;.struct Sel
698c0 65 63 74 44 65 73 74 20 7b 0a 20 20 75 38 20 65 ectDest {. u8 e
698d0 44 65 73 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Dest; /*
698e0 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 How to dispose
698f0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a of the results *
69900 2f 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 3b /. u8 affinity;
69910 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 /* Affinit
69920 79 20 75 73 65 64 20 77 68 65 6e 20 65 44 65 73 y used when eDes
69930 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 t==SRT_Set */.
69940 69 6e 74 20 69 50 61 72 6d 3b 20 20 20 20 20 20 int iParm;
69950 20 20 2f 2a 20 41 20 70 61 72 61 6d 65 74 65 72 /* A parameter
69960 20 75 73 65 64 20 62 79 20 74 68 65 20 65 44 65 used by the eDe
69970 73 74 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 st disposal meth
69980 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 6d od */. int iMem
69990 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 ; /* Bas
699a0 65 20 72 65 67 69 73 74 65 72 20 77 68 65 72 65 e register where
699b0 20 72 65 73 75 6c 74 73 20 61 72 65 20 77 72 69 results are wri
699c0 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d tten */. int nM
699d0 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e em; /* N
699e0 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 umber of registe
699f0 72 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a rs allocated */.
69a00 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 };../*.** During
69a10 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e code generation
69a20 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20 74 of statements t
69a30 68 61 74 20 64 6f 20 69 6e 73 65 72 74 73 20 69 hat do inserts i
69a40 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e nto AUTOINCREMEN
69a50 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c 20 74 68 T .** tables, th
69a60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f e following info
69a70 72 6d 61 74 69 6f 6e 20 69 73 20 61 74 74 61 63 rmation is attac
69a80 68 65 64 20 74 6f 20 74 68 65 20 54 61 62 6c 65 hed to the Table
69a90 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a 2a 2a 20 .u.autoInc.p.**
69aa0 70 6f 69 6e 74 65 72 20 6f 66 20 65 61 63 68 20 pointer of each
69ab0 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 autoincrement ta
69ac0 62 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 73 6f ble to record so
69ad0 6d 65 20 73 69 64 65 20 69 6e 66 6f 72 6d 61 74 me side informat
69ae0 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ion that.** the
69af0 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 6e code generator n
69b00 65 65 64 73 2e 20 20 57 65 20 68 61 76 65 20 74 eeds. We have t
69b10 6f 20 6b 65 65 70 20 70 65 72 2d 74 61 62 6c 65 o keep per-table
69b20 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a autoincrement.*
69b30 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e * information in
69b40 20 63 61 73 65 20 69 6e 73 65 72 74 73 20 61 72 case inserts ar
69b50 65 20 64 6f 77 6e 20 77 69 74 68 69 6e 20 74 72 e down within tr
69b60 69 67 67 65 72 73 2e 20 20 54 72 69 67 67 65 72 iggers. Trigger
69b70 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d s do not.** norm
69b80 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61 74 65 20 ally coordinate
69b90 74 68 65 69 72 20 61 63 74 69 76 69 74 69 65 73 their activities
69ba0 2c 20 62 75 74 20 77 65 20 64 6f 20 6e 65 65 64 , but we do need
69bb0 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 74 to coordinate t
69bc0 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 61 6e he.** loading an
69bd0 64 20 73 61 76 69 6e 67 20 6f 66 20 61 75 74 6f d saving of auto
69be0 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66 6f 72 6d increment inform
69bf0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 ation..*/.struct
69c00 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 7b 0a 20 AutoincInfo {.
69c10 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 4e AutoincInfo *pN
69c20 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 69 ext; /* Next i
69c30 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20 61 20 6c nfo block in a l
69c40 69 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 ist of them all
69c50 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 */. Table *pTab
69c60 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 ; /* Ta
69c70 62 6c 65 20 74 68 69 73 20 69 6e 66 6f 20 62 6c ble this info bl
69c80 6f 63 6b 20 72 65 66 65 72 73 20 74 6f 20 2a 2f ock refers to */
69c90 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 . int iDb;
69ca0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
69cb0 78 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 x in sqlite3.aDb
69cc0 5b 5d 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 [] of database h
69cd0 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 olding pTab */.
69ce0 20 69 6e 74 20 72 65 67 43 74 72 3b 20 20 20 20 int regCtr;
69cf0 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
69d00 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e register holdin
69d10 67 20 74 68 65 20 72 6f 77 69 64 20 63 6f 75 6e g the rowid coun
69d20 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a ter */.};../*.**
69d30 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 6c Size of the col
69d40 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a 23 69 66 umn cache.*/.#if
69d50 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e 5f 43 4f ndef SQLITE_N_CO
69d60 4c 43 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20 LCACHE.# define
69d70 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
69d80 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a E 10.#endif../*.
69d90 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f 6e 65 20 ** At least one
69da0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
69db0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
69dc0 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 66 ure is created f
69dd0 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74 72 69 67 or each .** trig
69de0 67 65 72 20 74 68 61 74 20 6d 61 79 20 62 65 20 ger that may be
69df0 66 69 72 65 64 20 77 68 69 6c 65 20 70 61 72 73 fired while pars
69e00 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 ing an INSERT, U
69e10 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 0a PDATE or DELETE.
69e20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 41 6c ** statement. Al
69e30 6c 20 73 75 63 68 20 6f 62 6a 65 63 74 73 20 61 l such objects a
69e40 72 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 re stored in the
69e50 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 linked list hea
69e60 64 65 64 20 61 74 0a 2a 2a 20 50 61 72 73 65 2e ded at.** Parse.
69e70 70 54 72 69 67 67 65 72 50 72 67 20 61 6e 64 20 pTriggerPrg and
69e80 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 73 74 61 deleted once sta
69e90 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 61 74 69 tement compilati
69ea0 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 63 on has been.** c
69eb0 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 ompleted..**.**
69ec0 41 20 56 64 62 65 20 73 75 62 2d 70 72 6f 67 72 A Vdbe sub-progr
69ed0 61 6d 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e am that implemen
69ee0 74 73 20 74 68 65 20 62 6f 64 79 20 61 6e 64 20 ts the body and
69ef0 57 48 45 4e 20 63 6c 61 75 73 65 20 6f 66 20 74 WHEN clause of t
69f00 72 69 67 67 65 72 0a 2a 2a 20 54 72 69 67 67 65 rigger.** Trigge
69f10 72 50 72 67 2e 70 54 72 69 67 67 65 72 2c 20 61 rPrg.pTrigger, a
69f20 73 73 75 6d 69 6e 67 20 61 20 64 65 66 61 75 6c ssuming a defaul
69f30 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c t ON CONFLICT cl
69f40 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72 69 67 67 ause of.** Trigg
69f50 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c 20 69 73 erPrg.orconf, is
69f60 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 54 stored in the T
69f70 72 69 67 67 65 72 50 72 67 2e 70 50 72 6f 67 72 riggerPrg.pProgr
69f80 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 20 am variable..**
69f90 54 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67 The Parse.pTrigg
69fa0 65 72 50 72 67 20 6c 69 73 74 20 6e 65 76 65 72 erPrg list never
69fb0 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e contains two en
69fc0 74 72 69 65 73 20 77 69 74 68 20 74 68 65 20 73 tries with the s
69fd0 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73 20 66 6f ame.** values fo
69fe0 72 20 62 6f 74 68 20 70 54 72 69 67 67 65 72 20 r both pTrigger
69ff0 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a 2a 0a 2a and orconf..**.*
6a000 2a 20 54 68 65 20 54 72 69 67 67 65 72 50 72 67 * The TriggerPrg
6a010 2e 6f 6c 64 6d 61 73 6b 20 76 61 72 69 61 62 6c .oldmask variabl
6a020 65 20 69 73 20 73 65 74 20 74 6f 20 61 20 6d 61 e is set to a ma
6a030 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75 sk of old.* colu
6a040 6d 6e 73 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 mns.** accessed
6a050 28 6f 72 20 73 65 74 20 74 6f 20 30 20 66 6f 72 (or set to 0 for
6a060 20 74 72 69 67 67 65 72 73 20 66 69 72 65 64 20 triggers fired
6a070 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 49 as a result of I
6a080 4e 53 45 52 54 20 0a 2a 2a 20 73 74 61 74 65 6d NSERT .** statem
6a090 65 6e 74 73 29 2e 0a 2a 2f 0a 73 74 72 75 63 74 ents)..*/.struct
6a0a0 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20 TriggerPrg {.
6a0b0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 Trigger *pTrigge
6a0c0 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67 r; /* Trigg
6a0d0 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20 er this program
6a0e0 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a was coded from *
6a0f0 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 /. int orconf;
6a100 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 /* D
6a110 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 efault ON CONFLI
6a120 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 53 CT policy */. S
6a130 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 ubProgram *pProg
6a140 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 61 ram; /* Progra
6a150 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 70 m implementing p
6a160 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 2a Trigger/orconf *
6a170 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b 3b /. u32 oldmask;
6a180 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
6a190 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c ask of old.* col
6a1a0 75 6d 6e 73 20 61 63 63 65 73 73 65 64 20 2a 2f umns accessed */
6a1b0 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 . TriggerPrg *p
6a1c0 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 Next; /* Ne
6a1d0 78 74 20 65 6e 74 72 79 20 69 6e 20 50 61 72 73 xt entry in Pars
6a1e0 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c 69 e.pTriggerPrg li
6a1f0 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 st */.};../*.**
6a200 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f An SQL parser co
6a210 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f ntext. A copy o
6a220 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
6a230 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 is passed throu
6a240 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72 gh.** the parser
6a250 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61 and down into a
6a260 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63 ll the parser ac
6a270 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20 tion routine in
6a280 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72 order to.** carr
6a290 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61 y around informa
6a2a0 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f tion that is glo
6a2b0 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72 bal to the entir
6a2c0 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 e parse..**.** T
6a2d0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 he structure is
6a2e0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f divided into two
6a2f0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68 parts. When th
6a300 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64 e parser and cod
6a310 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61 e.** generate ca
6a320 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65 ll themselves re
6a330 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66 cursively, the f
6a340 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65 irst part of the
6a350 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 structure.** is
6a360 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68 constant but th
6a370 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73 e second part is
6a380 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65 reset at the be
6a390 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 ginning and end
6a3a0 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72 of.** each recur
6a3b0 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 sion..**.** The
6a3c0 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61 nTableLock and a
6a3d0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62 TableLock variab
6a3e0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65 les are only use
6a3f0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d d if the shared-
6a400 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72 cache .** featur
6a410 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66 e is enabled (if
6a420 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75 sqlite3Tsd()->u
6a430 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20 seSharedData is
6a440 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a true). They are.
6a450 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ** used to store
6a460 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c the set of tabl
6a470 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64 e-locks required
6a480 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e by the statemen
6a490 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69 t being.** compi
6a4a0 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71 led. Function sq
6a4b0 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 lite3TableLock()
6a4c0 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
6a4d0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a entries to the.*
6a4e0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 * list..*/.struc
6a4f0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69 t Parse {. sqli
6a500 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
6a510 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74 /* The main dat
6a520 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20 abase structure
6a530 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 */. int rc;
6a540 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
6a550 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78 urn code from ex
6a560 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 ecution */. cha
6a570 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 r *zErrMsg;
6a580 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65 /* An error me
6a590 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20 ssage */. Vdbe
6a5a0 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20 *pVdbe;
6a5b0 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72 /* An engine for
6a5c0 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62 executing datab
6a5d0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a ase bytecode */.
6a5e0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74 u8 colNamesSet
6a5f0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61 ; /* TRUE a
6a600 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61 fter OP_ColumnNa
6a610 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 me has been issu
6a620 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20 ed to pVdbe */.
6a630 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20 u8 nameClash;
6a640 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61 /* A perma
6a650 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20 nent table name
6a660 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d clashes with tem
6a670 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a p table name */.
6a680 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61 u8 checkSchema
6a690 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73 ; /* Causes
6a6a0 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
6a6b0 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 heck after an er
6a6c0 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74 ror */. u8 nest
6a6d0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ed; /*
6a6e0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 Number of neste
6a6f0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70 d calls to the p
6a700 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72 arser/code gener
6a710 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72 ator */. u8 par
6a720 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f seError; /
6a730 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70 * True after a p
6a740 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54 arsing error. T
6a750 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20 icket #1794 */.
6a760 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20 u8 nTempReg;
6a770 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6a780 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 of temporary reg
6a790 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52 isters in aTempR
6a7a0 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65 eg[] */. u8 nTe
6a7b0 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f mpInUse; /
6a7c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d * Number of aTem
6a7d0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79 pReg[] currently
6a7e0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a checked out */.
6a7f0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38 int aTempReg[8
6a800 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e ]; /* Holdin
6a810 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f g area for tempo
6a820 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a rary registers *
6a830 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65 /. int nRangeRe
6a840 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 g; /* Size
6a850 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72 of the temporar
6a860 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b y register block
6a870 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65 */. int iRange
6a880 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 Reg; /* Fi
6a890 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 rst register in
6a8a0 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
6a8b0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e er block */. in
6a8c0 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 t nErr;
6a8d0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6a8e0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20 errors seen */.
6a8f0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20 int nTab;
6a900 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6a910 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c of previously al
6a920 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72 located VDBE cur
6a930 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d sors */. int nM
6a940 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f em; /
6a950 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f * Number of memo
6a960 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f ry cells used so
6a970 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 far */. int nS
6a980 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f et; /
6a990 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73 * Number of sets
6a9a0 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a used so far */.
6a9b0 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20 int ckBase;
6a9c0 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 /* Base r
6a9d0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20 egister of data
6a9e0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e during check con
6a9f0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e straints */. in
6aa00 74 20 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 20 t iCacheLevel;
6aa10 20 20 20 2f 2a 20 43 6f 6c 43 61 63 68 65 20 76 /* ColCache v
6aa20 61 6c 69 64 20 77 68 65 6e 20 61 43 6f 6c 43 61 alid when aColCa
6aa30 63 68 65 5b 5d 2e 69 4c 65 76 65 6c 3c 3d 69 43 che[].iLevel<=iC
6aa40 61 63 68 65 4c 65 76 65 6c 20 2a 2f 0a 20 20 69 acheLevel */. i
6aa50 6e 74 20 69 43 61 63 68 65 43 6e 74 3b 20 20 20 nt iCacheCnt;
6aa60 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 75 /* Counter u
6aa70 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
6aa80 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 6c 72 75 20 aColCache[].lru
6aa90 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6e values */. u8 n
6aaa0 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 20 20 20 ColCache;
6aab0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
6aac0 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 6c tries in the col
6aad0 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a 20 20 75 umn cache */. u
6aae0 38 20 69 43 6f 6c 43 61 63 68 65 3b 20 20 20 20 8 iColCache;
6aaf0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 /* Next entr
6ab00 79 20 6f 66 20 74 68 65 20 63 61 63 68 65 20 74 y of the cache t
6ab10 6f 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 73 o replace */. s
6ab20 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 truct yColCache
6ab30 7b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 6c 65 {. int iTable
6ab40 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
6ab50 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 able cursor numb
6ab60 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 er */. int iC
6ab70 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 olumn;
6ab80 2f 2a 20 54 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 /* Table column
6ab90 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 75 38 number */. u8
6aba0 20 61 66 66 43 68 61 6e 67 65 3b 20 20 20 20 20 affChange;
6abb0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
6abc0 68 69 73 20 72 65 67 69 73 74 65 72 20 68 61 73 his register has
6abd0 20 68 61 64 20 61 6e 20 61 66 66 69 6e 69 74 79 had an affinity
6abe0 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75 change */. u
6abf0 38 20 74 65 6d 70 52 65 67 3b 20 20 20 20 20 20 8 tempReg;
6ac00 20 20 20 20 20 2f 2a 20 69 52 65 67 20 69 73 20 /* iReg is
6ac10 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 20 a temp register
6ac20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 that needs to be
6ac30 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e freed */. in
6ac40 74 20 69 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 t iLevel;
6ac50 20 20 20 20 2f 2a 20 4e 65 73 74 69 6e 67 20 6c /* Nesting l
6ac60 65 76 65 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 evel */. int
6ac70 69 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 iReg;
6ac80 20 20 2f 2a 20 52 65 67 20 77 69 74 68 20 76 61 /* Reg with va
6ac90 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 lue of this colu
6aca0 6d 6e 2e 20 30 20 6d 65 61 6e 73 20 6e 6f 6e 65 mn. 0 means none
6acb0 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 75 . */. int lru
6acc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
6acd0 2a 20 4c 65 61 73 74 20 72 65 63 65 6e 74 6c 79 * Least recently
6ace0 20 75 73 65 64 20 65 6e 74 72 79 20 68 61 73 20 used entry has
6acf0 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c the smallest val
6ad00 75 65 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43 61 ue */. } aColCa
6ad10 63 68 65 5b 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c che[SQLITE_N_COL
6ad20 43 41 43 48 45 5d 3b 20 20 2f 2a 20 4f 6e 65 20 CACHE]; /* One
6ad30 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 for each column
6ad40 63 61 63 68 65 20 65 6e 74 72 79 20 2a 2f 0a 20 cache entry */.
6ad50 20 75 33 32 20 77 72 69 74 65 4d 61 73 6b 3b 20 u32 writeMask;
6ad60 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 61 /* Start a
6ad70 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
6ad80 6f 6e 20 6f 6e 20 74 68 65 73 65 20 64 61 74 61 on on these data
6ad90 62 61 73 65 73 20 2a 2f 0a 20 20 75 33 32 20 63 bases */. u32 c
6ada0 6f 6f 6b 69 65 4d 61 73 6b 3b 20 20 20 20 20 20 ookieMask;
6adb0 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 73 63 /* Bitmask of sc
6adc0 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 64 61 hema verified da
6add0 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 75 38 20 tabases */. u8
6ade0 69 73 4d 75 6c 74 69 57 72 69 74 65 3b 20 20 20 isMultiWrite;
6adf0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 /* True if sta
6ae00 74 65 6d 65 6e 74 20 6d 61 79 20 61 66 66 65 63 tement may affec
6ae10 74 2f 69 6e 73 65 72 74 20 6d 75 6c 74 69 70 6c t/insert multipl
6ae20 65 20 72 6f 77 73 20 2a 2f 0a 20 20 75 38 20 6d e rows */. u8 m
6ae30 61 79 41 62 6f 72 74 3b 20 20 20 20 20 20 20 20 ayAbort;
6ae40 20 2f 2a 20 54 72 75 65 20 69 66 20 73 74 61 74 /* True if stat
6ae50 65 6d 65 6e 74 20 6d 61 79 20 74 68 72 6f 77 20 ement may throw
6ae60 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 an ABORT excepti
6ae70 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b on */. int cook
6ae80 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20 ieGoto; /*
6ae90 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f Address of OP_Go
6aea0 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72 to to cookie ver
6aeb0 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65 ifier subroutine
6aec0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 */. int cookie
6aed0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58 Value[SQLITE_MAX
6aee0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f _ATTACHED+2]; /
6aef0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b * Values of cook
6af00 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f ies to verify */
6af10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
6af20 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
6af30 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f E. int nTableLo
6af40 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 ck; /* Nu
6af50 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e mber of locks in
6af60 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20 aTableLock */.
6af70 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62 TableLock *aTab
6af80 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69 leLock; /* Requi
6af90 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 red table locks
6afa0 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 for shared-cache
6afb0 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a mode */.#endif.
6afc0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 int regRowid;
6afd0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
6afe0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 er holding rowid
6aff0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45 of CREATE TABLE
6b000 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 entry */. int
6b010 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 regRoot;
6b020 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
6b030 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e ding root page n
6b040 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62 umber for new ob
6b050 6a 65 63 74 73 20 2a 2f 0a 20 20 41 75 74 6f 69 jects */. Autoi
6b060 6e 63 49 6e 66 6f 20 2a 70 41 69 6e 63 3b 20 20 ncInfo *pAinc;
6b070 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 /* Information a
6b080 62 6f 75 74 20 41 55 54 4f 49 4e 43 52 45 4d 45 bout AUTOINCREME
6b090 4e 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 NT counters */.
6b0a0 20 69 6e 74 20 6e 4d 61 78 41 72 67 3b 20 20 20 int nMaxArg;
6b0b0 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 61 72 67 /* Max arg
6b0c0 73 20 70 61 73 73 65 64 20 74 6f 20 75 73 65 72 s passed to user
6b0d0 20 66 75 6e 63 74 69 6f 6e 20 62 79 20 73 75 62 function by sub
6b0e0 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 0a 20 20 2f -program */.. /
6b0f0 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 * Information us
6b100 65 64 20 77 68 69 6c 65 20 63 6f 64 69 6e 67 20 ed while coding
6b110 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 trigger programs
6b120 2e 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 54 . */. Parse *pT
6b130 6f 70 6c 65 76 65 6c 3b 20 20 20 20 2f 2a 20 50 oplevel; /* P
6b140 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66 arse structure f
6b150 6f 72 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20 or main program
6b160 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 54 (or NULL) */. T
6b170 61 62 6c 65 20 2a 70 54 72 69 67 67 65 72 54 61 able *pTriggerTa
6b180 62 3b 20 20 2f 2a 20 54 61 62 6c 65 20 74 72 69 b; /* Table tri
6b190 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20 ggers are being
6b1a0 63 6f 64 65 64 20 66 6f 72 20 2a 2f 0a 20 20 75 coded for */. u
6b1b0 33 32 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20 32 oldmask;
6b1c0 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f /* Mask of o
6b1d0 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 72 65 66 ld.* columns ref
6b1e0 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 erenced */. u8
6b1f0 65 54 72 69 67 67 65 72 4f 70 3b 20 20 20 20 20 eTriggerOp;
6b200 20 20 2f 2a 20 54 4b 5f 55 50 44 41 54 45 2c 20 /* TK_UPDATE,
6b210 54 4b 5f 49 4e 53 45 52 54 20 6f 72 20 54 4b 5f TK_INSERT or TK_
6b220 44 45 4c 45 54 45 20 2a 2f 0a 20 20 75 38 20 65 DELETE */. u8 e
6b230 4f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 Orconf;
6b240 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 /* Default ON C
6b250 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 ONFLICT policy f
6b260 6f 72 20 74 72 69 67 67 65 72 20 73 74 65 70 73 or trigger steps
6b270 20 2a 2f 0a 20 20 75 38 20 64 69 73 61 62 6c 65 */. u8 disable
6b280 54 72 69 67 67 65 72 73 3b 20 20 2f 2a 20 54 72 Triggers; /* Tr
6b290 75 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 72 ue to disable tr
6b2a0 69 67 67 65 72 73 20 2a 2f 0a 0a 20 20 2f 2a 20 iggers */.. /*
6b2b0 41 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e Above is constan
6b2c0 74 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 t between recurs
6b2d0 69 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 ions. Below is
6b2e0 72 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 reset before and
6b2f0 20 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 after. ** each
6b300 20 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 recursion */..
6b310 20 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 int nVar;
6b320 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
6b330 6f 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 of '?' variables
6b340 20 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c seen in the SQL
6b350 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 so far */. int
6b360 20 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 nVarExpr;
6b370 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 /* Number of u
6b380 73 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 sed slots in apV
6b390 61 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e arExpr[] */. in
6b3a0 74 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b t nVarExprAlloc;
6b3b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
6b3c0 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 allocated slots
6b3d0 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a in apVarExpr[] *
6b3e0 2f 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 /. Expr **apVar
6b3f0 45 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e Expr; /* Poin
6b400 74 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 ters to :aaa and
6b410 20 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 $aaaa wildcard
6b420 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 expressions */.
6b430 20 56 64 62 65 20 2a 70 52 65 70 72 65 70 61 72 Vdbe *pReprepar
6b440 65 3b 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e e; /* VM bein
6b450 67 20 72 65 70 72 65 70 61 72 65 64 20 28 73 71 g reprepared (sq
6b460 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 29 lite3Reprepare()
6b470 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 69 61 ) */. int nAlia
6b480 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e s; /* N
6b490 75 6d 62 65 72 20 6f 66 20 61 6c 69 61 73 65 64 umber of aliased
6b4a0 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 result set colu
6b4b0 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c mns */. int nAl
6b4c0 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a iasAlloc; /*
6b4d0 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 Number of alloc
6b4e0 61 74 65 64 20 73 6c 6f 74 73 20 66 6f 72 20 61 ated slots for a
6b4f0 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20 69 6e 74 Alias[] */. int
6b500 20 2a 61 41 6c 69 61 73 3b 20 20 20 20 20 20 20 *aAlias;
6b510 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 /* Register us
6b520 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 69 61 73 ed to hold alias
6b530 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75 ed result */. u
6b540 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 8 explain;
6b550 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
6b560 68 65 20 45 58 50 4c 41 49 4e 20 66 6c 61 67 20 he EXPLAIN flag
6b570 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74 68 65 20 is found on the
6b580 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f 6b 65 6e query */. Token
6b590 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20 20 20 20 sNameToken;
6b5a0 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68 20 75 6e /* Token with un
6b5b0 71 75 61 6c 69 66 69 65 64 20 73 63 68 65 6d 61 qualified schema
6b5c0 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a object name */.
6b5d0 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74 54 6f 6b Token sLastTok
6b5e0 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 6c 61 en; /* The la
6b5f0 73 74 20 74 6f 6b 65 6e 20 70 61 72 73 65 64 20 st token parsed
6b600 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
6b610 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41 6c 6c *zTail; /* All
6b620 20 53 51 4c 20 74 65 78 74 20 70 61 73 74 20 74 SQL text past t
6b630 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f 6c 6f he last semicolo
6b640 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 54 61 n parsed */. Ta
6b650 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65 3b 20 ble *pNewTable;
6b660 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 62 65 /* A table be
6b670 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 ing constructed
6b680 62 79 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 by CREATE TABLE
6b690 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4e */. Trigger *pN
6b6a0 65 77 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f ewTrigger; /
6b6b0 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65 72 20 * Trigger under
6b6c0 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61 20 43 construct by a C
6b6d0 52 45 41 54 45 20 54 52 49 47 47 45 52 20 2a 2f REATE TRIGGER */
6b6e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
6b6f0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20 AuthContext; /*
6b700 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 The 6th paramete
6b710 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63 r to db->xAuth c
6b720 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e allbacks */.#ifn
6b730 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
6b740 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54 VIRTUALTABLE. T
6b750 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20 oken sArg;
6b760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d /* Com
6b770 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20 plete text of a
6b780 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20 module argument
6b790 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56 */. u8 declareV
6b7a0 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 tab;
6b7b0 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64 /* True if insid
6b7c0 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 e sqlite3_declar
6b7d0 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e e_vtab() */. in
6b7e0 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 t nVtabLock;
6b7f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
6b800 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 er of virtual ta
6b810 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a bles to lock */.
6b820 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62 Table **apVtab
6b830 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 Lock; /*
6b840 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75 Pointer to virtu
6b850 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e al tables needin
6b860 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e g locking */.#en
6b870 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 dif. int nHeigh
6b880 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
6b890 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 Expression tree
6b8a0 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65 height of curre
6b8b0 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f nt sub-select */
6b8c0 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69 . Table *pZombi
6b8d0 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69 eTab; /* Li
6b8e0 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65 st of Table obje
6b8f0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66 cts to delete af
6b900 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a ter code gen */.
6b910 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70 54 TriggerPrg *pT
6b920 72 69 67 67 65 72 50 72 67 3b 20 20 20 20 2f 2a riggerPrg; /*
6b930 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 Linked list of
6b940 63 6f 64 65 64 20 74 72 69 67 67 65 72 73 20 2a coded triggers *
6b950 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c /.};..#ifdef SQL
6b960 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
6b970 54 41 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 TABLE. #define
6b980 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 IN_DECLARE_VTAB
6b990 30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 0.#else. #defin
6b9a0 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 e IN_DECLARE_VTA
6b9b0 42 20 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 B (pParse->decla
6b9c0 72 65 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a reVtab).#endif..
6b9d0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
6b9e0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
6b9f0 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e ng structure can
6ba00 20 62 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 be declared on
6ba10 61 20 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 a stack and used
6ba20 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 .** to save the
6ba30 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 Parse.zAuthConte
6ba40 78 74 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 xt value so that
6ba50 20 69 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f it can be resto
6ba60 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 red later..*/.st
6ba70 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 ruct AuthContext
6ba80 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 {. const char
6ba90 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 *zAuthContext;
6baa0 20 2f 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 /* Put saved Pa
6bab0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 rse.zAuthContext
6bac0 20 68 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 here */. Parse
6bad0 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
6bae0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 /* The Pa
6baf0 72 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f rse structure */
6bb00 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 .};../*.** Bitfi
6bb10 65 6c 64 20 66 6c 61 67 73 20 66 6f 72 20 50 35 eld flags for P5
6bb20 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 value in OP_Ins
6bb30 65 72 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 ert and OP_Delet
6bb40 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 e.*/.#define OPF
6bb50 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20 20 20 20 LAG_NCHANGE
6bb60 20 20 30 78 30 31 20 20 20 20 2f 2a 20 53 65 74 0x01 /* Set
6bb70 20 74 6f 20 75 70 64 61 74 65 20 64 62 2d 3e 6e to update db->n
6bb80 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e Change */.#defin
6bb90 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 e OPFLAG_LASTROW
6bba0 49 44 20 20 20 20 20 30 78 30 32 20 20 20 20 2f ID 0x02 /
6bbb0 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 * Set to update
6bbc0 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f db->lastRowid */
6bbd0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f .#define OPFLAG_
6bbe0 49 53 55 50 44 41 54 45 20 20 20 20 20 20 30 78 ISUPDATE 0x
6bbf0 30 34 20 20 20 20 2f 2a 20 54 68 69 73 20 4f 50 04 /* This OP
6bc00 5f 49 6e 73 65 72 74 20 69 73 20 61 6e 20 73 71 _Insert is an sq
6bc10 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 l UPDATE */.#def
6bc20 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e ine OPFLAG_APPEN
6bc30 44 20 20 20 20 20 20 20 20 30 78 30 38 20 20 20 D 0x08
6bc40 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69 6b 65 /* This is like
6bc50 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 ly to be an appe
6bc60 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 nd */.#define OP
6bc70 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 FLAG_USESEEKRESU
6bc80 4c 54 20 30 78 31 30 20 20 20 20 2f 2a 20 54 72 LT 0x10 /* Tr
6bc90 79 20 74 6f 20 61 76 6f 69 64 20 61 20 73 65 65 y to avoid a see
6bca0 6b 20 69 6e 20 42 74 72 65 65 49 6e 73 65 72 74 k in BtreeInsert
6bcb0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 () */.#define OP
6bcc0 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 20 FLAG_CLEARCACHE
6bcd0 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 43 6c 0x20 /* Cl
6bce0 65 61 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 ear pseudo-table
6bcf0 20 63 61 63 68 65 20 69 6e 20 4f 50 5f 43 6f 6c cache in OP_Col
6bd00 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45 61 umn */../*. * Ea
6bd10 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73 65 ch trigger prese
6bd20 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 nt in the databa
6bd30 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74 6f se schema is sto
6bd40 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61 6e red as an instan
6bd50 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74 20 ce of. * struct
6bd60 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a 20 Trigger. . *. *
6bd70 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73 74 Pointers to inst
6bd80 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 ances of struct
6bd90 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f 72 Trigger are stor
6bda0 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e 0a ed in two ways..
6bdb0 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74 72 * 1. In the "tr
6bdc0 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61 62 igHash" hash tab
6bdd0 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 le (part of the
6bde0 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72 65 sqlite3* that re
6bdf0 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20 2a presents the . *
6be00 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20 54 database). T
6be10 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67 67 his allows Trigg
6be20 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74 6f er structures to
6be30 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62 79 be retrieved by
6be40 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c 6c name.. * 2. All
6be50 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69 triggers associ
6be60 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 ated with a sing
6be70 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61 20 le table form a
6be80 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73 69 linked list, usi
6be90 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e 65 ng the. * pNe
6bea0 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74 72 xt member of str
6beb0 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20 70 uct Trigger. A p
6bec0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 ointer to the fi
6bed0 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 rst element of t
6bee0 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64 20 he. * linked
6bef0 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20 61 list is stored a
6bf00 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72 22 s the "pTrigger"
6bf10 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 61 member of the a
6bf20 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20 20 ssociated. *
6bf30 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20 2a struct Table.. *
6bf40 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c 69 . * The "step_li
6bf50 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74 st" member point
6bf60 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 s to the first e
6bf70 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e 6b lement of a link
6bf80 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74 61 ed list. * conta
6bf90 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 ining the SQL st
6bfa0 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66 69 atements specifi
6bfb0 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67 65 ed as the trigge
6bfc0 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a 73 r program.. */.s
6bfd0 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b 0a truct Trigger {.
6bfe0 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 char *zName;
6bff0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
6c000 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 name of the tri
6c010 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 gger
6c020 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 */.
6c030 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20 20 char *table;
6c040 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
6c050 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74 6f table or view to
6c060 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67 67 which the trigg
6c070 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20 20 er applies */.
6c080 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 u8 op;
6c090 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f /* One o
6c0a0 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b 5f f TK_DELETE, TK_
6c0b0 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 UPDATE, TK_INSER
6c0c0 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 75 T */. u
6c0d0 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20 20 8 tr_tm;
6c0e0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 /* One of
6c0f0 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c TRIGGER_BEFORE,
6c100 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a TRIGGER_AFTER *
6c110 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 3b /. Expr *pWhen;
6c120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
6c130 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f he WHEN clause o
6c140 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
6c150 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a (may be NULL) *
6c160 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c /. IdList *pCol
6c170 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49 umns; /* I
6c180 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44 f this is an UPD
6c190 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c ATE OF <column-l
6c1a0 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20 ist> trigger,.
6c1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6c1c0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c the <
6c1d0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20 column-list> is
6c1e0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 stored here */.
6c1f0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 Schema *pSchema
6c200 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65 ; /* Sche
6c210 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 ma containing th
6c220 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 e trigger */. S
6c230 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d chema *pTabSchem
6c240 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 a; /* Schema
6c250 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
6c260 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67 table */. Trigg
6c270 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73 erStep *step_lis
6c280 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20 t; /* Link list
6c290 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 of trigger progr
6c2a0 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20 am steps
6c2b0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65 */. Trigge
6c2c0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 r *pNext;
6c2d0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 /* Next trigge
6c2e0 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 r associated wit
6c2f0 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d h the table */.}
6c300 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67 ;../*.** A trigg
6c310 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42 er is either a B
6c320 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45 EFORE or an AFTE
6c330 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 R trigger. The
6c340 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 following consta
6c350 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 nts.** determine
6c360 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49 which. .**.** I
6c370 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74 f there are mult
6c380 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79 iple triggers, y
6c390 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65 ou might of some
6c3a0 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65 BEFORE and some
6c3b0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68 AFTER..** In th
6c3c0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f at cases, the co
6c3d0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61 nstants below ca
6c3e0 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68 n be ORed togeth
6c3f0 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 er..*/.#define T
6c400 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31 RIGGER_BEFORE 1
6c410 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52 .#define TRIGGER
6c420 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20 _AFTER 2../*.
6c430 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 * An instance of
6c440 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53 struct TriggerS
6c450 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73 tep is used to s
6c460 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51 tore a single SQ
6c470 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74 L statement. * t
6c480 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66 hat is a part of
6c490 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72 a trigger-progr
6c4a0 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61 am. . *. * Insta
6c4b0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54 nces of struct T
6c4c0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73 riggerStep are s
6c4d0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c tored in a singl
6c4e0 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c y linked list (l
6c4f0 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74 inked. * using t
6c500 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65 he "pNext" membe
6c510 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 r) referenced by
6c520 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22 the "step_list"
6c530 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a member of the .
6c540 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74 * associated st
6c550 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73 ruct Trigger ins
6c560 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74 tance. The first
6c570 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 element of the
6c580 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20 linked list is.
6c590 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 * the first step
6c5a0 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d of the trigger-
6c5b0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20 program.. * . *
6c5c0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20 The "op" member
6c5d0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65 indicates whethe
6c5e0 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c r this is a "DEL
6c5f0 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20 ETE", "INSERT",
6c600 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22 "UPDATE" or. * "
6c610 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e SELECT" statemen
6c620 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 t. The meanings
6c630 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d of the other mem
6c640 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e bers is determin
6c650 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61 ed by the . * va
6c660 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66 lue of "op" as f
6c670 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f ollows:. *. * (o
6c680 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a p == TK_INSERT).
6c690 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20 * orconf ->
6c6a0 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f stores the ON CO
6c6b0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d NFLICT algorithm
6c6c0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e . * pSelect ->
6c6d0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 If this is an I
6c6e0 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 NSERT INTO ... S
6c6f0 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d ELECT ... statem
6c700 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 ent, then. *
6c710 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73 this s
6c720 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 tores a pointer
6c730 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 to the SELECT st
6c740 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69 atement. Otherwi
6c750 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67 se NULL.. * targ
6c760 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e et -> A token
6c770 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f holding the quo
6c780 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 ted name of the
6c790 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 table to insert
6c7a0 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70 72 4c 69 into.. * pExprLi
6c7b0 73 74 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 st -> If this is
6c7c0 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 an INSERT INTO
6c7d0 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e 2e 20 73 ... VALUES ... s
6c7e0 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 tatement, then.
6c7f0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 * t
6c800 68 69 73 20 73 74 6f 72 65 73 20 76 61 6c 75 65 his stores value
6c810 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 s to be inserted
6c820 2e 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c . Otherwise NULL
6c830 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20 20 20 2d .. * pIdList -
6c840 3e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 > If this is an
6c850 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 INSERT INTO ...
6c860 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 3e 29 (<column-names>)
6c870 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a 20 2a 20 VALUES ... . *
6c880 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 sta
6c890 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 69 tement, then thi
6c8a0 73 20 73 74 6f 72 65 73 20 74 68 65 20 63 6f 6c s stores the col
6c8b0 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20 62 65 0a umn-names to be.
6c8c0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6c8d0 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 2e 0a 20 inserted into..
6c8e0 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 44 *. * (op == TK_D
6c8f0 45 4c 45 54 45 29 0a 20 2a 20 74 61 72 67 65 74 ELETE). * target
6c900 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e 20 68 -> A token h
6c910 6f 6c 64 69 6e 67 20 74 68 65 20 71 75 6f 74 65 olding the quote
6c920 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 d name of the ta
6c930 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 66 72 ble to delete fr
6c940 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20 20 20 om.. * pWhere
6c950 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20 63 6c -> The WHERE cl
6c960 61 75 73 65 20 6f 66 20 74 68 65 20 44 45 4c 45 ause of the DELE
6c970 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 TE statement if
6c980 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 one is specified
6c990 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 .. *
6c9a0 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c Otherwise NULL
6c9b0 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d 3d 20 .. * . * (op ==
6c9c0 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20 74 61 TK_UPDATE). * ta
6c9d0 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b rget -> A tok
6c9e0 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 en holding the q
6c9f0 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 uoted name of th
6ca00 65 20 74 61 62 6c 65 20 74 6f 20 75 70 64 61 74 e table to updat
6ca10 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a 20 70 57 e rows of.. * pW
6ca20 68 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 here -> The W
6ca30 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
6ca40 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d he UPDATE statem
6ca50 65 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 ent if one is sp
6ca60 65 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 ecified.. *
6ca70 20 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 Otherwi
6ca80 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 45 78 70 se NULL.. * pExp
6ca90 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69 73 74 20 rList -> A list
6caa0 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 74 of the columns t
6cab0 6f 20 75 70 64 61 74 65 20 61 6e 64 20 74 68 65 o update and the
6cac0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 expressions to
6cad0 75 70 64 61 74 65 0a 20 2a 20 20 20 20 20 20 20 update. *
6cae0 20 20 20 20 20 20 20 74 68 65 6d 20 74 6f 2e 20 them to.
6caf0 53 65 65 20 73 71 6c 69 74 65 33 55 70 64 61 74 See sqlite3Updat
6cb00 65 28 29 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f e() documentatio
6cb10 6e 20 6f 66 20 22 70 43 68 61 6e 67 65 73 22 0a n of "pChanges".
6cb20 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
6cb30 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20 0a 20 2a argument.. * . *
6cb40 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72 /.struct Trigger
6cb50 53 74 65 70 20 7b 0a 20 20 75 38 20 6f 70 3b 20 Step {. u8 op;
6cb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6cb70 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 One of TK_DELET
6cb80 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b E, TK_UPDATE, TK
6cb90 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53 45 4c 45 _INSERT, TK_SELE
6cba0 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72 63 6f 6e CT */. u8 orcon
6cbb0 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 f; /*
6cbc0 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65 74 63 2e OE_Rollback etc.
6cbd0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 */. Trigger *p
6cbe0 54 72 69 67 3b 20 20 20 20 20 20 2f 2a 20 54 68 Trig; /* Th
6cbf0 65 20 74 72 69 67 67 65 72 20 74 68 61 74 20 74 e trigger that t
6cc00 68 69 73 20 73 74 65 70 20 69 73 20 61 20 70 61 his step is a pa
6cc10 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65 6c 65 63 rt of */. Selec
6cc20 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20 t *pSelect;
6cc30 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d 65 /* SELECT statme
6cc40 6e 74 20 6f 72 20 52 48 53 20 6f 66 20 49 4e 53 nt or RHS of INS
6cc50 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53 45 4c 45 ERT INTO .. SELE
6cc60 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 CT ... */. Toke
6cc70 6e 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20 n target;
6cc80 20 2f 2a 20 54 61 72 67 65 74 20 74 61 62 6c 65 /* Target table
6cc90 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 for DELETE, UPD
6cca0 41 54 45 2c 20 49 4e 53 45 52 54 20 2a 2f 0a 20 ATE, INSERT */.
6ccb0 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 Expr *pWhere;
6ccc0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 /* The WHE
6ccd0 52 45 20 63 6c 61 75 73 65 20 66 6f 72 20 44 45 RE clause for DE
6cce0 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73 LETE or UPDATE s
6ccf0 74 65 70 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 teps */. ExprLi
6cd00 73 74 20 2a 70 45 78 70 72 4c 69 73 74 3b 20 2f st *pExprList; /
6cd10 2a 20 53 45 54 20 63 6c 61 75 73 65 20 66 6f 72 * SET clause for
6cd20 20 55 50 44 41 54 45 2e 20 20 56 41 4c 55 45 53 UPDATE. VALUES
6cd30 20 63 6c 61 75 73 65 20 66 6f 72 20 49 4e 53 45 clause for INSE
6cd40 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a RT */. IdList *
6cd50 70 49 64 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 pIdList; /*
6cd60 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 Column names for
6cd70 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 54 72 69 INSERT */. Tri
6cd80 67 67 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b ggerStep *pNext;
6cd90 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 /* Next in the
6cda0 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 link-list */.
6cdb0 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 TriggerStep *pLa
6cdc0 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 st; /* Last ele
6cdd0 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 ment in link-lis
6cde0 74 2e 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 t. Valid for 1st
6cdf0 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b elem only */.};
6ce00 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c ../*.** The foll
6ce10 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
6ce20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 contains informa
6ce30 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 tion used by the
6ce40 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a sqliteFix....**
6ce50 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65 routines as the
6ce60 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65 y walk the parse
6ce70 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61 tree to make da
6ce80 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65 tabase reference
6ce90 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20 s.** explicit.
6cea0 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
6ceb0 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78 ct DbFixer DbFix
6cec0 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78 er;.struct DbFix
6ced0 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 er {. Parse *pP
6cee0 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 arse; /* Th
6cef0 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 e parsing contex
6cf00 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67 t. Error messag
6cf10 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 es written here
6cf20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
6cf30 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65 *zDb; /* Make
6cf40 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74 sure all object
6cf50 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 s are contained
6cf60 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 in this database
6cf70 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
6cf80 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70 *zType; /* Typ
6cf90 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e e of the contain
6cfa0 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 er - used for er
6cfb0 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
6cfc0 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 const Token *p
6cfd0 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 Name; /* Name of
6cfe0 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d the container -
6cff0 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 used for error
6d000 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a messages */.};..
6d010 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65 /*.** An objecte
6d020 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75 d used to accumu
6d030 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 late the text of
6d040 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20 a string where
6d050 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 we.** do not nec
6d060 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f essarily know ho
6d070 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67 w big the string
6d080 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20 will be in the
6d090 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 end..*/.struct S
6d0a0 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69 trAccum {. sqli
6d0b0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
6d0c0 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74 /* Optional dat
6d0d0 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 abase for lookas
6d0e0 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c ide. Can be NUL
6d0f0 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61 L */. char *zBa
6d100 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 se; /* A
6d110 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e base allocation
6d120 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c . Not from mall
6d130 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a oc. */. char *z
6d140 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a Text; /*
6d150 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c The string coll
6d160 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a ected so far */.
6d170 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20 int nChar;
6d180 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
6d190 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73 of the string s
6d1a0 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 o far */. int
6d1b0 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 nAlloc;
6d1c0 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61 /* Amount of spa
6d1d0 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 ce allocated in
6d1e0 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20 zText */. int
6d1f0 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 mxAlloc;
6d200 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 /* Maximum allow
6d210 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 ed string length
6d220 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f */. u8 mallo
6d230 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65 cFailed; /* Be
6d240 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e comes true if an
6d250 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 y memory allocat
6d260 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75 ion fails */. u
6d270 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20 8 useMalloc;
6d280 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a /* True if z
6d290 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 65 61 Text is enlargea
6d2a0 62 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f ble using reallo
6d2b0 63 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 c */. u8 tooB
6d2c0 69 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 ig; /* B
6d2d0 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 ecomes true if s
6d2e0 74 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 tring size excee
6d2f0 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a ds limits */.};.
6d300 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 ./*.** A pointer
6d310 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
6d320 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f re is used to co
6d330 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d mmunicate inform
6d340 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 ation.** from sq
6d350 6c 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 lite3Init and OP
6d360 5f 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 _ParseSchema int
6d370 6f 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 o the sqlite3Ini
6d380 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 tCallback..*/.ty
6d390 70 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 pedef struct {.
6d3a0 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
6d3b0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
6d3c0 62 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 base being initi
6d3d0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 alized */. int
6d3e0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
6d3f0 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 /* 0 for main da
6d400 74 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 tabase. 1 for T
6d410 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 EMP, 2.. for ATT
6d420 41 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 ACHed */. char
6d430 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f **pzErrMsg; /
6d440 2a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 * Error message
6d450 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 stored here */.
6d460 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
6d470 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 /* Result c
6d480 6f 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 ode stored here
6d490 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a */.} InitData;..
6d4a0 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 /*.** Structure
6d4b0 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 containing globa
6d4c0 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 l configuration
6d4d0 64 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c data for the SQL
6d4e0 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a ite library..**.
6d4f0 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 ** This structur
6d500 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 e also contains
6d510 73 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 some state infor
6d520 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 mation..*/.struc
6d530 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 t Sqlite3Config
6d540 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 {. int bMemstat
6d550 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d560 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f /* True to
6d570 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 enable memory s
6d580 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 tatus */. int b
6d590 43 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 CoreMutex;
6d5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d5b0 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 True to enable c
6d5c0 6f 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a ore mutexing */.
6d5d0 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 int bFullMutex
6d5e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6d5f0 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 /* True to e
6d600 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 nable full mutex
6d610 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 ing */. int mxS
6d620 74 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 trlen;
6d630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
6d640 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e ximum string len
6d650 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c gth */. int szL
6d660 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 ookaside;
6d670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
6d680 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 fault lookaside
6d690 62 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 buffer size */.
6d6a0 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b int nLookaside;
6d6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d6c0 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f /* Default lo
6d6d0 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63 okaside buffer c
6d6e0 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 ount */. sqlite
6d6f0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 3_mem_methods m;
6d700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
6d710 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 ow-level memory
6d720 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 allocation inter
6d730 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 face */. sqlite
6d740 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
6d750 6d 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c mutex; /* L
6d760 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 ow-level mutex i
6d770 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 nterface */. sq
6d780 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 lite3_pcache_met
6d790 68 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 20 hods pcache;
6d7a0 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67 /* Low-level pag
6d7b0 65 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 e-cache interfac
6d7c0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65 e */. void *pHe
6d7d0 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ap;
6d7e0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70 /* Heap
6d7f0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a storage space *
6d800 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20 /. int nHeap;
6d810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d820 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 /* Size of
6d830 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e pHeap[] */. in
6d840 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20 t mnReq, mxReq;
6d850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d860 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68 /* Min and max h
6d870 65 61 70 20 72 65 71 75 65 73 74 73 20 73 69 7a eap requests siz
6d880 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 es */. void *pS
6d890 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 cratch;
6d8a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72 /* Scr
6d8b0 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 atch memory */.
6d8c0 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 int szScratch;
6d8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d8e0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 /* Size of ea
6d8f0 63 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65 ch scratch buffe
6d900 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61 r */. int nScra
6d910 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 tch;
6d920 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
6d930 65 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 er of scratch bu
6d940 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 ffers */. void
6d950 2a 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 *pPage;
6d960 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
6d970 50 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 Page cache memor
6d980 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 y */. int szPag
6d990 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
6d9a0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 /* Size
6d9b0 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e of each page in
6d9c0 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e pPage[] */. in
6d9d0 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 t nPage;
6d9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6d9f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 /* Number of pag
6da00 65 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f es in pPage[] */
6da10 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53 . int mxParserS
6da20 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 tack;
6da30 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20 /* maximum
6da40 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 depth of the par
6da50 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 ser stack */. i
6da60 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 6e nt sharedCacheEn
6da70 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 abled;
6da80 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 72 /* true if shar
6da90 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e ed-cache mode en
6daa0 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a 20 54 68 abled */. /* Th
6dab0 65 20 61 62 6f 76 65 20 6d 69 67 68 74 20 62 65 e above might be
6dac0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 initialized to
6dad0 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68 65 20 66 non-zero. The f
6dae0 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64 20 74 6f ollowing need to
6daf0 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 69 6e 69 always. ** ini
6db00 74 69 61 6c 6c 79 20 62 65 20 7a 65 72 6f 2c 20 tially be zero,
6db10 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20 20 69 6e however. */. in
6db20 74 20 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 t isInit;
6db30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6db40 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 69 6e /* True after in
6db50 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68 61 73 itialization has
6db60 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 69 finished */. i
6db70 6e 74 20 69 6e 50 72 6f 67 72 65 73 73 3b 20 20 nt inProgress;
6db80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6db90 20 2f 2a 20 54 72 75 65 20 77 68 69 6c 65 20 69 /* True while i
6dba0 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 6e nitialization in
6dbb0 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a 20 20 69 progress */. i
6dbc0 6e 74 20 69 73 4d 75 74 65 78 49 6e 69 74 3b 20 nt isMutexInit;
6dbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6dbe0 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d /* True after m
6dbf0 75 74 65 78 65 73 20 61 72 65 20 69 6e 69 74 69 utexes are initi
6dc00 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 alized */. int
6dc10 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b 20 20 20 isMallocInit;
6dc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
6dc30 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c True after mall
6dc40 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 oc is initialize
6dc50 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 43 61 d */. int isPCa
6dc60 63 68 65 49 6e 69 74 3b 20 20 20 20 20 20 20 20 cheInit;
6dc70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
6dc80 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63 20 69 73 after malloc is
6dc90 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
6dca0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
6dcb0 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20 20 20 20 *pInitMutex;
6dcc0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 /* Mutex use
6dcd0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 d by sqlite3_ini
6dce0 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a 20 20 69 tialize() */. i
6dcf0 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 nt nRefInitMutex
6dd00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
6dd10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 /* Number of us
6dd20 65 72 73 20 6f 66 20 70 49 6e 69 74 4d 75 74 65 ers of pInitMute
6dd30 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 x */.};../*.** C
6dd40 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 70 ontext pointer p
6dd50 61 73 73 65 64 20 64 6f 77 6e 20 74 68 72 6f 75 assed down throu
6dd60 67 68 20 74 68 65 20 74 72 65 65 2d 77 61 6c 6b gh the tree-walk
6dd70 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 61 6c 6b ..*/.struct Walk
6dd80 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 45 78 er {. int (*xEx
6dd90 70 72 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b prCallback)(Walk
6dda0 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20 20 20 20 er*, Expr*);
6ddb0 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 /* Callback for
6ddc0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a expressions */.
6ddd0 20 20 69 6e 74 20 28 2a 78 53 65 6c 65 63 74 43 int (*xSelectC
6dde0 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b 65 72 2a allback)(Walker*
6ddf0 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f 2a 20 43 ,Select*); /* C
6de00 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53 45 4c 45 allback for SELE
6de10 43 54 73 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a CTs */. Parse *
6de20 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
6de30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6de40 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e /* Parser con
6de50 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75 6e 69 6f text. */. unio
6de60 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 n {
6de70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6de80 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 64 /* Extra d
6de90 61 74 61 20 66 6f 72 20 63 61 6c 6c 62 61 63 6b ata for callback
6dea0 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 */. NameCont
6deb0 65 78 74 20 2a 70 4e 43 3b 20 20 20 20 20 20 20 ext *pNC;
6dec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6ded0 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20 63 6f 6e /* Naming con
6dee0 74 65 78 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 text */. int
6def0 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
6df00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6df10 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 /* Intege
6df20 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75 r value */. } u
6df30 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 ;.};../* Forward
6df40 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f declarations */
6df50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6df60 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 int sqlite3WalkE
6df70 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 xpr(Walker*, Exp
6df80 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 r*);.SQLITE_PRIV
6df90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
6dfa0 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b alkExprList(Walk
6dfb0 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b er*, ExprList*);
6dfc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6dfd0 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 int sqlite3WalkS
6dfe0 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a 2c 20 53 elect(Walker*, S
6dff0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
6e000 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6e010 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 te3WalkSelectExp
6e020 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 r(Walker*, Selec
6e030 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
6e040 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 ATE int sqlite3W
6e050 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 57 61 alkSelectFrom(Wa
6e060 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b lker*, Select*);
6e070 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 63 ../*.** Return c
6e080 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 ode from the par
6e090 73 65 2d 74 72 65 65 20 77 61 6c 6b 69 6e 67 20 se-tree walking
6e0a0 70 72 69 6d 69 74 69 76 65 73 20 61 6e 64 20 74 primitives and t
6e0b0 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b heir.** callback
6e0c0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 s..*/.#define WR
6e0d0 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20 20 30 20 C_Continue 0
6e0e0 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 64 6f /* Continue do
6e0f0 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64 72 65 6e wn into children
6e100 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f */.#define WRC_
6e110 50 72 75 6e 65 20 20 20 20 20 20 20 31 20 20 20 Prune 1
6e120 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64 72 65 6e /* Omit children
6e130 20 62 75 74 20 63 6f 6e 74 69 6e 75 65 20 77 61 but continue wa
6e140 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67 73 20 2a lking siblings *
6e150 2f 0a 23 64 65 66 69 6e 65 20 57 52 43 5f 41 62 /.#define WRC_Ab
6e160 6f 72 74 20 20 20 20 20 20 20 32 20 20 20 2f 2a ort 2 /*
6e170 20 41 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 Abandon the tre
6e180 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a e walk */../*.**
6e190 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f Assuming zIn po
6e1a0 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 ints to the firs
6e1b0 74 20 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d t byte of a UTF-
6e1c0 38 20 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 8 character,.**
6e1d0 61 64 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 advance zIn to p
6e1e0 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 oint to the firs
6e1f0 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 t byte of the ne
6e200 78 74 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 xt UTF-8 charact
6e210 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 er..*/.#define S
6e220 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 QLITE_SKIP_UTF8(
6e230 7a 49 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20 zIn) {
6e240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
6e250 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 if( (*(zIn++))
6e260 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 >=0xc0 ){
6e270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e280 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 \. whi
6e290 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 le( (*zIn & 0xc0
6e2a0 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b )==0x80 ){ zIn++
6e2b0 3b 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 ; }
6e2c0 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 \. }
6e2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
6e2f0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a \.}../*
6e300 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 .** The SQLITE_C
6e310 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 ORRUPT_BKPT macr
6e320 6f 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 o can be either
6e330 61 20 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 a constant (for
6e340 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 production.** bu
6e350 69 6c 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74 ilds) or a funct
6e360 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 ion call (for de
6e370 62 75 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74 bugging). If it
6e380 20 69 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 is a function c
6e390 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 all,.** it allow
6e3a0 73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 s the operator t
6e3b0 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 o set a breakpoi
6e3c0 6e 74 20 61 74 20 74 68 65 20 73 70 6f 74 20 77 nt at the spot w
6e3d0 68 65 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a here database.**
6e3e0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 corruption is f
6e3f0 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a irst detected..*
6e400 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
6e410 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
6e420 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
6e430 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b e3Corrupt(void);
6e440 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 .# define SQLITE
6e450 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71 _CORRUPT_BKPT sq
6e460 6c 69 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23 lite3Corrupt().#
6e470 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 else.# define SQ
6e480 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
6e490 54 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 T SQLITE_CORRUPT
6e4a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 .#endif../*.** T
6e4b0 68 65 20 63 74 79 70 65 2e 68 20 68 65 61 64 65 he ctype.h heade
6e4c0 72 20 69 73 20 6e 65 65 64 65 64 20 66 6f 72 20 r is needed for
6e4d0 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73 74 65 6d non-ASCII system
6e4e0 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f 0a 2a s. It is also.*
6e4f0 2a 20 6e 65 65 64 65 64 20 62 79 20 46 54 53 33 * needed by FTS3
6e500 20 77 68 65 6e 20 46 54 53 33 20 69 73 20 69 6e when FTS3 is in
6e510 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 61 6d cluded in the am
6e520 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 algamation..*/.#
6e530 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
6e540 54 45 5f 41 53 43 49 49 29 20 7c 7c 20 5c 0a 20 TE_ASCII) || \.
6e550 20 20 20 28 64 65 66 69 6e 65 64 28 53 51 4c 49 (defined(SQLI
6e560 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 TE_ENABLE_FTS3)
6e570 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 && defined(SQLIT
6e580 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 29 29 E_AMALGAMATION))
6e590 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 .# include <ctyp
6e5a0 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a e.h>.#endif../*.
6e5b0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
6e5c0 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63 20 74 68 macros mimic th
6e5d0 65 20 73 74 61 6e 64 61 72 64 20 6c 69 62 72 61 e standard libra
6e5e0 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 75 ry functions tou
6e5f0 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73 73 70 61 pper(),.** isspa
6e600 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d 28 29 2c ce(), isalnum(),
6e610 20 69 73 64 69 67 69 74 28 29 20 61 6e 64 20 69 isdigit() and i
6e620 73 78 64 69 67 69 74 28 29 2c 20 72 65 73 70 65 sxdigit(), respe
6e630 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 ctively. The.**
6e640 73 71 6c 69 74 65 20 76 65 72 73 69 6f 6e 73 20 sqlite versions
6e650 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72 20 41 53 only work for AS
6e660 43 49 49 20 63 68 61 72 61 63 74 65 72 73 2c 20 CII characters,
6e670 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f regardless of lo
6e680 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 cale..*/.#ifdef
6e690 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20 64 SQLITE_ASCII.# d
6e6a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 75 efine sqlite3Tou
6e6b0 70 70 65 72 28 78 29 20 20 28 28 78 29 26 7e 28 pper(x) ((x)&~(
6e6c0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b sqlite3CtypeMap[
6e6d0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
6e6e0 78 29 5d 26 30 78 32 30 29 29 0a 23 20 64 65 66 x)]&0x20)).# def
6e6f0 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 ine sqlite3Isspa
6e700 63 65 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 ce(x) (sqlite3
6e710 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
6e720 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
6e730 31 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 1).# define sqli
6e740 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 te3Isalnum(x)
6e750 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 (sqlite3CtypeMap
6e760 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
6e770 28 78 29 5d 26 30 78 30 36 29 0a 23 20 64 65 66 (x)]&0x06).# def
6e780 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70 ine sqlite3Isalp
6e790 68 61 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 ha(x) (sqlite3
6e7a0 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
6e7b0 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
6e7c0 32 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 2).# define sqli
6e7d0 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 20 te3Isdigit(x)
6e7e0 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 (sqlite3CtypeMap
6e7f0 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 [(unsigned char)
6e800 28 78 29 5d 26 30 78 30 34 29 0a 23 20 64 65 66 (x)]&0x04).# def
6e810 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 64 69 ine sqlite3Isxdi
6e820 67 69 74 28 78 29 20 20 28 73 71 6c 69 74 65 33 git(x) (sqlite3
6e830 43 74 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e CtypeMap[(unsign
6e840 65 64 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 ed char)(x)]&0x0
6e850 38 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 8).# define sqli
6e860 74 65 33 54 6f 6c 6f 77 65 72 28 78 29 20 20 20 te3Tolower(x)
6e870 28 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c (sqlite3UpperToL
6e880 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 ower[(unsigned c
6e890 68 61 72 29 28 78 29 5d 29 0a 23 65 6c 73 65 0a har)(x)]).#else.
6e8a0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 # define sqlite3
6e8b0 54 6f 75 70 70 65 72 28 78 29 20 20 20 74 6f 75 Toupper(x) tou
6e8c0 70 70 65 72 28 28 75 6e 73 69 67 6e 65 64 20 63 pper((unsigned c
6e8d0 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e har)(x)).# defin
6e8e0 65 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 e sqlite3Isspace
6e8f0 28 78 29 20 20 20 69 73 73 70 61 63 65 28 28 75 (x) isspace((u
6e900 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 nsigned char)(x)
6e910 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 ).# define sqlit
6e920 65 33 49 73 61 6c 6e 75 6d 28 78 29 20 20 20 69 e3Isalnum(x) i
6e930 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67 6e 65 64 salnum((unsigned
6e940 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 char)(x)).# def
6e950 69 6e 65 20 73 71 6c 69 74 65 33 49 73 61 6c 70 ine sqlite3Isalp
6e960 68 61 28 78 29 20 20 20 69 73 61 6c 70 68 61 28 ha(x) isalpha(
6e970 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 (unsigned char)(
6e980 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c x)).# define sql
6e990 69 74 65 33 49 73 64 69 67 69 74 28 78 29 20 20 ite3Isdigit(x)
6e9a0 20 69 73 64 69 67 69 74 28 28 75 6e 73 69 67 6e isdigit((unsign
6e9b0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 ed char)(x)).# d
6e9c0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 78 efine sqlite3Isx
6e9d0 64 69 67 69 74 28 78 29 20 20 69 73 78 64 69 67 digit(x) isxdig
6e9e0 69 74 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 it((unsigned cha
6e9f0 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 r)(x)).# define
6ea00 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 sqlite3Tolower(x
6ea10 29 20 20 20 74 6f 6c 6f 77 65 72 28 28 75 6e 73 ) tolower((uns
6ea20 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a igned char)(x)).
6ea30 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e #endif../*.** In
6ea40 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 ternal function
6ea50 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 prototypes.*/.SQ
6ea60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6ea70 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 sqlite3StrICmp(
6ea80 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f const char *, co
6ea90 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c nst char *);.SQL
6eaa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
6eab0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
6eac0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 const char*, int
6ead0 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 *, u8);.SQLITE_P
6eae0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
6eaf0 65 33 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 e3Strlen30(const
6eb00 20 63 68 61 72 2a 29 3b 0a 23 64 65 66 69 6e 65 char*);.#define
6eb10 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 sqlite3StrNICmp
6eb20 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d sqlite3_strnicm
6eb30 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 p..SQLITE_PRIVAT
6eb40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c E int sqlite3Mal
6eb50 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 3b 0a 53 locInit(void);.S
6eb60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6eb70 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 id sqlite3Malloc
6eb80 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 End(void);.SQLIT
6eb90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a E_PRIVATE void *
6eba0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 69 6e sqlite3Malloc(in
6ebb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
6ebc0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
6ebd0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 29 3b MallocZero(int);
6ebe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ebf0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d void *sqlite3DbM
6ec00 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 allocZero(sqlite
6ec10 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 3*, int);.SQLITE
6ec20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
6ec30 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 qlite3DbMallocRa
6ec40 77 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 w(sqlite3*, int)
6ec50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6ec60 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 char *sqlite3Db
6ec70 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c StrDup(sqlite3*,
6ec80 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
6ec90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
6eca0 72 20 2a 73 71 6c 69 74 65 33 44 62 53 74 72 4e r *sqlite3DbStrN
6ecb0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e Dup(sqlite3*,con
6ecc0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a st char*, int);.
6ecd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6ece0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c oid *sqlite3Real
6ecf0 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b loc(void*, int);
6ed00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ed10 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 void *sqlite3DbR
6ed20 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c eallocOrFree(sql
6ed30 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 ite3 *, void *,
6ed40 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
6ed50 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 VATE void *sqlit
6ed60 65 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 e3DbRealloc(sqli
6ed70 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69 te3 *, void *, i
6ed80 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6ed90 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6eda0 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 2a 2c DbFree(sqlite3*,
6edb0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f void*);.SQLITE_
6edc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6edd0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f te3MallocSize(vo
6ede0 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 id*);.SQLITE_PRI
6edf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
6ee00 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 73 71 6c DbMallocSize(sql
6ee10 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 ite3*, void*);.S
6ee20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ee30 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74 id *sqlite3Scrat
6ee40 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 chMalloc(int);.S
6ee50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6ee60 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 id sqlite3Scratc
6ee70 68 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 hFree(void*);.SQ
6ee80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6ee90 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 d *sqlite3PageMa
6eea0 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 lloc(int);.SQLIT
6eeb0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6eec0 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 76 qlite3PageFree(v
6eed0 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 oid*);.SQLITE_PR
6eee0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6eef0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 e3MemSetDefault(
6ef00 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 void);.SQLITE_PR
6ef10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6ef20 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f e3BenignMallocHo
6ef30 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 oks(void (*)(voi
6ef40 64 29 2c 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 d), void (*)(voi
6ef50 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 d));.SQLITE_PRIV
6ef60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d ATE int sqlite3M
6ef70 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f 69 64 20 emoryAlarm(void
6ef80 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 (*)(void*, sqlit
6ef90 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74 29 2c 20 e3_int64, int),
6efa0 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 void*, sqlite3_i
6efb0 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4f 6e nt64);../*.** On
6efc0 20 73 79 73 74 65 6d 73 20 77 69 74 68 20 61 6d systems with am
6efd0 70 6c 65 20 73 74 61 63 6b 20 73 70 61 63 65 20 ple stack space
6efe0 61 6e 64 20 74 68 61 74 20 73 75 70 70 6f 72 74 and that support
6eff0 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61 6b 65 0a alloca(), make.
6f000 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c 6f 63 61 ** use of alloca
6f010 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 () to obtain spa
6f020 63 65 20 66 6f 72 20 6c 61 72 67 65 20 61 75 74 ce for large aut
6f030 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74 73 2e 20 omatic objects.
6f040 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a 20 By default,.**
6f050 6f 62 74 61 69 6e 20 73 70 61 63 65 20 66 72 6f obtain space fro
6f060 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a m malloc()..**.*
6f070 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28 29 20 72 * The alloca() r
6f080 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 72 65 74 outine never ret
6f090 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54 68 69 73 urns NULL. This
6f0a0 20 77 69 6c 6c 20 63 61 75 73 65 20 63 6f 64 65 will cause code
6f0b0 20 70 61 74 68 73 0a 2a 2a 20 74 68 61 74 20 64 paths.** that d
6f0c0 65 61 6c 20 77 69 74 68 20 73 71 6c 69 74 65 33 eal with sqlite3
6f0d0 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20 66 61 69 StackAlloc() fai
6f0e0 6c 75 72 65 73 20 74 6f 20 62 65 20 75 6e 72 65 lures to be unre
6f0f0 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 64 achable..*/.#ifd
6f100 65 66 20 53 51 4c 49 54 45 5f 55 53 45 5f 41 4c ef SQLITE_USE_AL
6f110 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65 20 73 71 LOCA.# define sq
6f120 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 lite3StackAllocR
6f130 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c 6f 63 61 aw(D,N) alloca
6f140 28 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c (N).# define sql
6f150 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 ite3StackAllocZe
6f160 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73 65 74 28 ro(D,N) memset(
6f170 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c 20 4e 29 alloca(N), 0, N)
6f180 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
6f190 33 53 74 61 63 6b 46 72 65 65 28 44 2c 50 29 20 3StackFree(D,P)
6f1a0 20 20 20 20 20 20 0a 23 65 6c 73 65 0a 23 20 64 .#else.# d
6f1b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 efine sqlite3Sta
6f1c0 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 ckAllocRaw(D,N)
6f1d0 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f sqlite3DbMallo
6f1e0 63 52 61 77 28 44 2c 4e 29 0a 23 20 64 65 66 69 cRaw(D,N).# defi
6f1f0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 ne sqlite3StackA
6f200 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 73 llocZero(D,N) s
6f210 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
6f220 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66 69 6e 65 ro(D,N).# define
6f230 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 sqlite3StackFre
6f240 65 28 44 2c 50 29 20 20 20 20 20 20 20 73 71 6c e(D,P) sql
6f250 69 74 65 33 44 62 46 72 65 65 28 44 2c 50 29 0a ite3DbFree(D,P).
6f260 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
6f270 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
6f280 53 59 53 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 SYS3.SQLITE_PRIV
6f290 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
6f2a0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
6f2b0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
6f2c0 79 73 33 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 ys3(void);.#endi
6f2d0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f f.#ifdef SQLITE_
6f2e0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 53 ENABLE_MEMSYS5.S
6f2f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
6f300 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f nst sqlite3_mem_
6f310 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
6f320 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28 76 6f MemGetMemsys5(vo
6f330 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 id);.#endif...#i
6f340 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 fndef SQLITE_MUT
6f350 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54 45 5f 50 EX_OMIT.SQLITE_P
6f360 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 33 RIVATE sqlite3
6f370 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a _mutex_methods *
6f380 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 sqlite3DefaultMu
6f390 74 65 78 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 tex(void);.SQLIT
6f3a0 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 E_PRIVATE sqli
6f3b0 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 te3_mutex *sqlit
6f3c0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 e3MutexAlloc(int
6f3d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f3e0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d E int sqlite3M
6f3f0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3b 0a utexInit(void);.
6f400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
6f410 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 int sqlite3Mute
6f420 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23 65 6e 64 xEnd(void);.#end
6f430 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 if..SQLITE_PRIVA
6f440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 TE int sqlite3St
6f450 61 74 75 73 56 61 6c 75 65 28 69 6e 74 29 3b 0a atusValue(int);.
6f460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f470 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
6f480 73 41 64 64 28 69 6e 74 2c 20 69 6e 74 29 3b 0a sAdd(int, int);.
6f490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f4a0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
6f4b0 73 53 65 74 28 69 6e 74 2c 20 69 6e 74 29 3b 0a sSet(int, int);.
6f4c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f4d0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e int sqlite3IsNaN
6f4e0 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51 4c 49 54 (double);..SQLIT
6f4f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f500 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 53 qlite3VXPrintf(S
6f510 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74 2c 20 63 trAccum*, int, c
6f520 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c onst char*, va_l
6f530 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ist);.SQLITE_PRI
6f540 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
6f550 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 e3MPrintf(sqlite
6f560 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 3*,const char*,
6f570 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ...);.SQLITE_PRI
6f580 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 VATE char *sqlit
6f590 65 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 e3VMPrintf(sqlit
6f5a0 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c e3*,const char*,
6f5b0 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 va_list);.SQLIT
6f5c0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
6f5d0 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 sqlite3MAppendf(
6f5e0 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2c 63 sqlite3*,char*,c
6f5f0 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b onst char*,...);
6f600 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
6f610 49 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 ITE_TEST) || def
6f620 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
6f630 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 G).SQLITE_PRIVAT
6f640 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
6f650 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e 73 DebugPrintf(cons
6f660 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 t char*, ...);.#
6f670 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 endif.#if define
6f680 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 53 d(SQLITE_TEST).S
6f690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
6f6a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 54 65 73 void *sqlite3Tes
6f6b0 74 54 65 78 74 54 6f 50 74 72 28 63 6f 6e 73 74 tTextToPtr(const
6f6c0 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64 69 66 0a char*);.#endif.
6f6d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
6f6e0 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 oid sqlite3SetSt
6f6f0 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c 20 73 71 ring(char **, sq
6f700 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 lite3*, const ch
6f710 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 ar*, ...);.SQLIT
6f720 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6f730 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50 qlite3ErrorMsg(P
6f740 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 arse*, const cha
6f750 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 r*, ...);.SQLITE
6f760 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
6f770 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 lite3ErrorClear(
6f780 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6f790 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6f7a0 74 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 2a te3Dequote(char*
6f7b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6f7c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 E int sqlite3Key
6f7d0 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 wordCode(const u
6f7e0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 nsigned char*, i
6f7f0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f800 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
6f810 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 2a 2c unParser(Parse*,
6f820 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 68 const char*, ch
6f830 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ar **);.SQLITE_P
6f840 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6f850 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 te3FinishCoding(
6f860 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Parse*);.SQLITE_
6f870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
6f880 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61 te3GetTempReg(Pa
6f890 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 rse*);.SQLITE_PR
6f8a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6f8b0 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
6f8c0 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 (Parse*,int);.SQ
6f8d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
6f8e0 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
6f8f0 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 ange(Parse*,int)
6f900 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
6f910 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c void sqlite3Rel
6f920 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50 61 easeTempRange(Pa
6f930 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 rse*,int,int);.S
6f940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 QLITE_PRIVATE Ex
6f950 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 pr *sqlite3ExprA
6f960 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a 2c 69 6e lloc(sqlite3*,in
6f970 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 2c 69 t,const Token*,i
6f980 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
6f990 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
6f9a0 33 45 78 70 72 28 73 71 6c 69 74 65 33 2a 2c 69 3Expr(sqlite3*,i
6f9b0 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b nt,const char*);
6f9c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6f9d0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
6f9e0 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 73 AttachSubtrees(s
6f9f0 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 45 78 qlite3*,Expr*,Ex
6fa00 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 pr*,Expr*);.SQLI
6fa10 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
6fa20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 50 61 *sqlite3PExpr(Pa
6fa30 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a rse*, int, Expr*
6fa40 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 , Expr*, const T
6fa50 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6fa60 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c RIVATE Expr *sql
6fa70 69 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 ite3ExprAnd(sqli
6fa80 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45 78 70 72 te3*,Expr*, Expr
6fa90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
6faa0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
6fab0 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 ExprFunction(Par
6fac0 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54 se*,ExprList*, T
6fad0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
6fae0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6faf0 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 te3ExprAssignVar
6fb00 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45 Number(Parse*, E
6fb10 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
6fb20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6fb30 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69 e3ExprClear(sqli
6fb40 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 te3*, Expr*);.SQ
6fb50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6fb60 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c d sqlite3ExprDel
6fb70 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 ete(sqlite3*, Ex
6fb80 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
6fb90 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 VATE ExprList *s
6fba0 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 qlite3ExprListAp
6fbb0 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72 pend(Parse*,Expr
6fbc0 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 List*,Expr*);.SQ
6fbd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6fbe0 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 d sqlite3ExprLis
6fbf0 74 53 65 74 4e 61 6d 65 28 50 61 72 73 65 2a 2c tSetName(Parse*,
6fc00 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b 65 6e 2a ExprList*,Token*
6fc10 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ,int);.SQLITE_PR
6fc20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6fc30 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70 61 e3ExprListSetSpa
6fc40 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 n(Parse*,ExprLis
6fc50 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 t*,ExprSpan*);.S
6fc60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
6fc70 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 id sqlite3ExprLi
6fc80 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 stDelete(sqlite3
6fc90 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 *, ExprList*);.S
6fca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
6fcb0 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71 t sqlite3Init(sq
6fcc0 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29 3b lite3*, char**);
6fcd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fce0 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43 int sqlite3InitC
6fcf0 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20 69 allback(void*, i
6fd00 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61 72 nt, char**, char
6fd10 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
6fd20 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
6fd30 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54 6f Pragma(Parse*,To
6fd40 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 ken*,Token*,Toke
6fd50 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f n*,int);.SQLITE_
6fd60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
6fd70 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
6fd80 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 2a lSchema(sqlite3*
6fd90 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
6fda0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
6fdb0 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 50 61 te3BeginParse(Pa
6fdc0 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 rse*,int);.SQLIT
6fdd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
6fde0 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 qlite3CommitInte
6fdf0 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c 69 rnalChanges(sqli
6fe00 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 te3*);.SQLITE_PR
6fe10 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
6fe20 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 ite3ResultSetOfS
6fe30 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65 6c elect(Parse*,Sel
6fe40 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ect*);.SQLITE_PR
6fe50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6fe60 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c e3OpenMasterTabl
6fe70 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29 3b e(Parse *, int);
6fe80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6fe90 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 72 void sqlite3Star
6fea0 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f tTable(Parse*,To
6feb0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c ken*,Token*,int,
6fec0 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 int,int,int);.SQ
6fed0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
6fee0 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 d sqlite3AddColu
6fef0 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a mn(Parse*,Token*
6ff00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ff10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
6ff20 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a 2c dNotNull(Parse*,
6ff30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
6ff40 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
6ff50 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 e3AddPrimaryKey(
6ff60 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 Parse*, ExprList
6ff70 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
6ff80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
6ff90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 E void sqlite3Ad
6ffa0 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 dCheckConstraint
6ffb0 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b (Parse*, Expr*);
6ffc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
6ffd0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 void sqlite3AddC
6ffe0 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 2a olumnType(Parse*
6fff0 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 ,Token*);.SQLITE
70000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70010 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 lite3AddDefaultV
70020 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70 72 alue(Parse*,Expr
70030 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 Span*);.SQLITE_P
70040 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70050 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 te3AddCollateTyp
70060 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a e(Parse*, Token*
70070 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70080 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e E void sqlite3En
70090 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 6f dTable(Parse*,To
700a0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c 65 ken*,Token*,Sele
700b0 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 ct*);..SQLITE_PR
700c0 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73 71 IVATE Bitvec *sq
700d0 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 lite3BitvecCreat
700e0 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 e(u32);.SQLITE_P
700f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
70100 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74 e3BitvecTest(Bit
70110 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c 49 vec*, u32);.SQLI
70120 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
70130 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
70140 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 Bitvec*, u32);.S
70150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70160 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 id sqlite3Bitvec
70170 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20 75 Clear(Bitvec*, u
70180 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 32, void*);.SQLI
70190 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
701a0 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 sqlite3BitvecDes
701b0 74 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a 53 troy(Bitvec*);.S
701c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 QLITE_PRIVATE u3
701d0 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 2 sqlite3BitvecS
701e0 69 7a 65 28 42 69 74 76 65 63 2a 29 3b 0a 53 51 ize(Bitvec*);.SQ
701f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
70200 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 sqlite3BitvecBu
70210 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69 6e iltinTest(int,in
70220 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 t*);..SQLITE_PRI
70230 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c VATE RowSet *sql
70240 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 ite3RowSetInit(s
70250 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c 20 qlite3*, void*,
70260 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 53 unsigned int);.S
70270 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70280 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 id sqlite3RowSet
70290 43 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b 0a Clear(RowSet*);.
702a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
702b0 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65 oid sqlite3RowSe
702c0 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a 2c tInsert(RowSet*,
702d0 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 i64);.SQLITE_PR
702e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
702f0 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77 53 3RowSetTest(RowS
70300 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c 20 et*, u8 iBatch,
70310 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 i64);.SQLITE_PRI
70320 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
70330 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53 65 RowSetNext(RowSe
70340 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c 49 t*, i64*);..SQLI
70350 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70360 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 sqlite3CreateVie
70370 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 2c w(Parse*,Token*,
70380 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 Token*,Token*,Se
70390 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a lect*,int,int);.
703a0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
703b0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 LITE_OMIT_VIEW)
703c0 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
703d0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
703e0 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 ABLE).SQLITE_PRI
703f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 VATE int sqlit
70400 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e e3ViewGetColumnN
70410 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62 6c ames(Parse*,Tabl
70420 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 e*);.#else.# def
70430 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77 47 ine sqlite3ViewG
70440 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41 2c etColumnNames(A,
70450 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c B) 0.#endif..SQL
70460 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70470 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c sqlite3DropTabl
70480 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 e(Parse*, SrcLis
70490 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 t*, int, int);.S
704a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
704b0 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 id sqlite3Delete
704c0 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a 23 Table(Table*);.#
704d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
704e0 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 IT_AUTOINCREMENT
704f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70500 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 void sqlite3Au
70510 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e toincrementBegin
70520 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b (Parse *pParse);
70530 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
70540 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 void sqlite3Au
70550 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 50 toincrementEnd(P
70560 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 23 arse *pParse);.#
70570 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 else.# define sq
70580 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 lite3Autoincreme
70590 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 64 65 66 ntBegin(X).# def
705a0 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69 ine sqlite3Autoi
705b0 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29 0a 23 ncrementEnd(X).#
705c0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
705d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
705e0 33 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c 20 3Insert(Parse*,
705f0 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 SrcList*, ExprLi
70600 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 64 st*, Select*, Id
70610 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c List*, int);.SQL
70620 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
70630 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c *sqlite3ArrayAl
70640 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a 2c locate(sqlite3*,
70650 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e void*,int,int,in
70660 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 53 t*,int*,int*);.S
70670 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 QLITE_PRIVATE Id
70680 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c List *sqlite3IdL
70690 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65 istAppend(sqlite
706a0 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f 6b 3*, IdList*, Tok
706b0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 en*);.SQLITE_PRI
706c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
706d0 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69 IdListIndex(IdLi
706e0 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 st*,const char*)
706f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70700 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 SrcList *sqlite
70710 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 3SrcListEnlarge(
70720 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 sqlite3*, SrcLis
70730 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 t*, int, int);.S
70740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 QLITE_PRIVATE Sr
70750 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 cList *sqlite3Sr
70760 63 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 cListAppend(sqli
70770 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 te3*, SrcList*,
70780 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Token*, Token*);
70790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
707a0 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 SrcList *sqlite3
707b0 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f SrcListAppendFro
707c0 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53 72 mTerm(Parse*, Sr
707d0 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 cList*, Token*,
707e0 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 Token*,.
707f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f To
70810 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 ken*, Select*, E
70820 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a xpr*, IdList*);.
70830 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70840 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 oid sqlite3SrcLi
70850 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 73 stIndexedBy(Pars
70860 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 e *, SrcList *,
70870 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 Token *);.SQLITE
70880 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
70890 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f ite3IndexedByLoo
708a0 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74 72 kup(Parse *, str
708b0 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
708c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
708d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
708e0 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e SrcListShiftJoin
708f0 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b 0a Type(SrcList*);.
70900 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70910 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 oid sqlite3SrcLi
70920 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 stAssignCursors(
70930 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a Parse*, SrcList*
70940 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70950 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 E void sqlite3Id
70960 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 ListDelete(sqlit
70970 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 e3*, IdList*);.S
70980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
70990 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 id sqlite3SrcLis
709a0 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a tDelete(sqlite3*
709b0 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c , SrcList*);.SQL
709c0 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 ITE_PRIVATE Inde
709d0 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 x *sqlite3Create
709e0 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f 6b Index(Parse*,Tok
709f0 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c 69 en*,Token*,SrcLi
70a00 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e st*,ExprList*,in
70a10 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 t,Token*,.
70a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
70a30 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 69 Token*, int, i
70a40 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70a50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70a60 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 2a DropIndex(Parse*
70a70 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 , SrcList*, int)
70a80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70a90 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 int sqlite3Sele
70aa0 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 ct(Parse*, Selec
70ab0 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a 29 t*, SelectDest*)
70ac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70ad0 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 Select *sqlite3
70ae0 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65 2a SelectNew(Parse*
70af0 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c 69 ,ExprList*,SrcLi
70b00 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c 69 st*,Expr*,ExprLi
70b10 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 st*,.
70b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78 Ex
70b30 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e pr*,ExprList*,in
70b40 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a t,Expr*,Expr*);.
70b50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
70b60 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 oid sqlite3Selec
70b70 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a tDelete(sqlite3*
70b80 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 , Select*);.SQLI
70b90 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 TE_PRIVATE Table
70ba0 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 *sqlite3SrcList
70bb0 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20 53 Lookup(Parse*, S
70bc0 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 rcList*);.SQLITE
70bd0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
70be0 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 ite3IsReadOnly(P
70bf0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 arse*, Table*, i
70c00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
70c10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
70c20 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65 2a OpenTable(Parse*
70c30 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74 20 , int iCur, int
70c40 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 iDb, Table*, int
70c50 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 );.#if defined(S
70c60 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 QLITE_ENABLE_UPD
70c70 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 ATE_DELETE_LIMIT
70c80 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 ) && !defined(SQ
70c90 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
70ca0 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 RY).SQLITE_PRIVA
70cb0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
70cc0 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73 65 LimitWhere(Parse
70cd0 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 45 *, SrcList *, E
70ce0 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74 20 xpr *, ExprList
70cf0 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 20 *, Expr *, Expr
70d00 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 *, char *);.#end
70d10 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
70d20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 E void sqlite3De
70d30 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c leteFrom(Parse*,
70d40 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a SrcList*, Expr*
70d50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
70d60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 70 E void sqlite3Up
70d70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72 63 date(Parse*, Src
70d80 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a List*, ExprList*
70d90 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 , Expr*, int);.S
70da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57 68 QLITE_PRIVATE Wh
70db0 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 ereInfo *sqlite3
70dc0 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73 65 WhereBegin(Parse
70dd0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 *, SrcList*, Exp
70de0 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c 20 r*, ExprList**,
70df0 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u16);.SQLITE_PRI
70e00 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
70e10 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65 49 3WhereEnd(WhereI
70e20 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 nfo*);.SQLITE_PR
70e30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
70e40 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 3ExprCodeGetColu
70e50 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 mn(Parse*, Table
70e60 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 *, int, int, int
70e70 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70e80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70e90 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 te3ExprCodeMove(
70ea0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Parse*, int, int
70eb0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70ec0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70ed0 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 te3ExprCodeCopy(
70ee0 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Parse*, int, int
70ef0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
70f00 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
70f10 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 te3ExprCacheStor
70f20 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 e(Parse*, int, i
70f30 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 nt, int);.SQLITE
70f40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
70f50 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75 lite3ExprCachePu
70f60 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 sh(Parse*);.SQLI
70f70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
70f80 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
70f90 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 Pop(Parse*, int)
70fa0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
70fb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
70fc0 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 rCacheRemove(Par
70fd0 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 se*, int);.SQLIT
70fe0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
70ff0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 qlite3ExprCacheC
71000 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 lear(Parse*);.SQ
71010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71020 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
71030 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 heAffinityChange
71040 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e (Parse*, int, in
71050 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
71060 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
71070 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72 73 xprHardCopy(Pars
71080 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c e*,int,int);.SQL
71090 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
710a0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
710b0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 Parse*, Expr*, i
710c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 nt);.SQLITE_PRIV
710d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 ATE int sqlite3E
710e0 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72 73 xprCodeTemp(Pars
710f0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a 29 e*, Expr*, int*)
71100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71110 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
71120 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65 CodeTarget(Parse
71130 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
71140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
71150 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f nt sqlite3ExprCo
71160 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 deAndCache(Parse
71170 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a *, Expr*, int);.
71180 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
71190 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
711a0 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61 72 odeConstants(Par
711b0 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c se*, Expr*);.SQL
711c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
711d0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 sqlite3ExprCodeE
711e0 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c 20 xprList(Parse*,
711f0 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 ExprList*, int,
71200 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71210 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71220 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72 73 3ExprIfTrue(Pars
71230 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 e*, Expr*, int,
71240 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71250 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
71260 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61 72 3ExprIfFalse(Par
71270 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c se*, Expr*, int,
71280 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
71290 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c IVATE Table *sql
712a0 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73 71 ite3FindTable(sq
712b0 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 lite3*,const cha
712c0 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 r*, const char*)
712d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
712e0 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c Table *sqlite3L
712f0 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73 65 ocateTable(Parse
71300 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f 6e *,int isView,con
71310 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 st char*, const
71320 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
71330 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73 71 RIVATE Index *sq
71340 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 73 lite3FindIndex(s
71350 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 qlite3*,const ch
71360 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a ar*, const char*
71370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
71380 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e E void sqlite3Un
71390 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 linkAndDeleteTab
713a0 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c le(sqlite3*,int,
713b0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 const char*);.SQ
713c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
713d0 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 d sqlite3UnlinkA
713e0 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73 71 ndDeleteIndex(sq
713f0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 lite3*,int,const
71400 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f char*);.SQLITE_
71410 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71420 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73 65 ite3Vacuum(Parse
71430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 TE int sqlite3Ru
71450 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c 20 nVacuum(char**,
71460 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 sqlite3*);.SQLIT
71470 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a E_PRIVATE char *
71480 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 sqlite3NameFromT
71490 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20 54 oken(sqlite3*, T
714a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 oken*);.SQLITE_P
714b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
714c0 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 e3ExprCompare(Ex
714d0 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c pr*, Expr*);.SQL
714e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
714f0 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c sqlite3ExprAnal
71500 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61 yzeAggregates(Na
71510 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72 meContext*, Expr
71520 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
71530 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
71540 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 xprAnalyzeAggLis
71550 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 45 t(NameContext*,E
71560 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 xprList*);.SQLIT
71570 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a E_PRIVATE Vdbe *
71580 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 50 sqlite3GetVdbe(P
71590 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
715a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
715b0 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 te3PrngSaveState
715c0 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 (void);.SQLITE_P
715d0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
715e0 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 74 te3PrngRestoreSt
715f0 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 ate(void);.SQLIT
71600 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71610 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 qlite3PrngResetS
71620 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 tate(void);.SQLI
71630 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
71640 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
71650 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 ll(sqlite3*);.SQ
71660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71670 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 d sqlite3CodeVer
71680 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 2a ifySchema(Parse*
71690 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
716a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
716b0 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 te3BeginTransact
716c0 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 ion(Parse*, int)
716d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
716e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d void sqlite3Com
716f0 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 mitTransaction(P
71700 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 arse*);.SQLITE_P
71710 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
71720 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 te3RollbackTrans
71730 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a action(Parse*);.
71740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
71750 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65 70 oid sqlite3Savep
71760 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 oint(Parse*, int
71770 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
71780 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71790 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 qlite3CloseSavep
717a0 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 29 oints(sqlite3 *)
717b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
717c0 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 int sqlite3Expr
717d0 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72 2a IsConstant(Expr*
717e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
717f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 E int sqlite3Exp
71800 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f rIsConstantNotJo
71810 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 in(Expr*);.SQLIT
71820 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
71830 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 lite3ExprIsConst
71840 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 antOrFunction(Ex
71850 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
71860 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
71870 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78 ExprIsInteger(Ex
71880 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 pr*, int*);.SQLI
71890 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
718a0 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e qlite3ExprCanBeN
718b0 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a 29 ull(const Expr*)
718c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
718d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
718e0 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 rCodeIsNullJump(
718f0 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78 70 Vdbe*, const Exp
71900 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 r*, int, int);.S
71910 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
71920 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 t sqlite3ExprNee
71930 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e dsNoAffinityChan
71940 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a 2c 20 ge(const Expr*,
71950 63 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 char);.SQLITE_PR
71960 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
71970 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 3IsRowid(const c
71980 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 har*);.SQLITE_PR
71990 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
719a0 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c e3GenerateRowDel
719b0 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62 6c ete(Parse*, Tabl
719c0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e e*, int, int, in
719d0 74 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e t, Trigger *, in
719e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
719f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 TE void sqlite3G
71a00 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 enerateRowIndexD
71a10 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 elete(Parse*, Ta
71a20 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 ble*, int, int*)
71a30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
71a40 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65 int sqlite3Gene
71a50 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61 72 rateIndexKey(Par
71a60 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e 74 se*, Index*, int
71a70 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c , int, int);.SQL
71a80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71a90 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 sqlite3Generate
71aa0 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 ConstraintChecks
71ab0 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c 69 (Parse*,Table*,i
71ac0 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20 20 nt,int,.
71ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 int
71af0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e *,int,int,int,in
71b00 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f t,int*);.SQLITE_
71b10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
71b20 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 ite3CompleteInse
71b30 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54 61 rtion(Parse*, Ta
71b40 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 ble*, int, int,
71b50 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 int*, int, int,
71b60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
71b70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
71b80 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 OpenTableAndIndi
71b90 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c ces(Parse*, Tabl
71ba0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 e*, int, int);.S
71bb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71bc0 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 id sqlite3BeginW
71bd0 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 riteOperation(Pa
71be0 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b rse*, int, int);
71bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71c00 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c 74 void sqlite3Mult
71c10 69 57 72 69 74 65 28 50 61 72 73 65 2a 29 3b 0a iWrite(Parse*);.
71c20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
71c30 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41 62 oid sqlite3MayAb
71c40 6f 72 74 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c ort(Parse*);.SQL
71c50 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
71c60 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 sqlite3HaltCons
71c70 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 traint(Parse*, i
71c80 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29 3b nt, char*, int);
71c90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71ca0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 Expr *sqlite3Exp
71cb0 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 rDup(sqlite3*,Ex
71cc0 70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 pr*,int);.SQLITE
71cd0 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 _PRIVATE ExprLis
71ce0 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 t *sqlite3ExprLi
71cf0 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 stDup(sqlite3*,E
71d00 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 xprList*,int);.S
71d10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 QLITE_PRIVATE Sr
71d20 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 cList *sqlite3Sr
71d30 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 cListDup(sqlite3
71d40 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29 3b *,SrcList*,int);
71d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
71d60 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 IdList *sqlite3I
71d70 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 dListDup(sqlite3
71d80 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 *,IdList*);.SQLI
71d90 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 TE_PRIVATE Selec
71da0 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 t *sqlite3Select
71db0 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c Dup(sqlite3*,Sel
71dc0 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 ect*,int);.SQLIT
71dd0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
71de0 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 qlite3FuncDefIns
71df0 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68 2a ert(FuncDefHash*
71e00 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c , FuncDef*);.SQL
71e10 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 ITE_PRIVATE Func
71e20 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 Def *sqlite3Find
71e30 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 Function(sqlite3
71e40 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e *,const char*,in
71e50 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a 53 t,int,u8,int);.S
71e60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
71e70 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 id sqlite3Regist
71e80 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69 6f erBuiltinFunctio
71e90 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 ns(sqlite3*);.SQ
71ea0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
71eb0 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
71ec0 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
71ed0 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 ns(void);.SQLITE
71ee0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
71ef0 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f lite3RegisterGlo
71f00 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 balFunctions(voi
71f10 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 d);.#ifdef SQLIT
71f20 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 E_DEBUG.SQLITE_P
71f30 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
71f40 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c ite3SafetyOn(sql
71f50 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ite3*);.SQLITE_P
71f60 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c RIVATE int sql
71f70 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 ite3SafetyOff(sq
71f80 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 lite3*);.#else.#
71f90 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 define sqlite3S
71fa0 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20 64 afetyOn(A) 0.# d
71fb0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61 66 efine sqlite3Saf
71fc0 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e 64 etyOff(A) 0.#end
71fd0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 if.SQLITE_PRIVAT
71fe0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 E int sqlite3Saf
71ff0 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 etyCheckOk(sqlit
72000 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 e3*);.SQLITE_PRI
72010 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72020 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f SafetyCheckSickO
72030 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 rOk(sqlite3*);.S
72040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
72050 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 id sqlite3Change
72060 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20 69 Cookie(Parse*, i
72070 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e nt);..#if !defin
72080 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
72090 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 IEW) && !defined
720a0 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 (SQLITE_OMIT_TRI
720b0 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 GGER).SQLITE_PRI
720c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
720d0 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 3MaterializeView
720e0 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
720f0 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23 65 Expr*, int);.#e
72100 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
72110 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
72120 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 R.SQLITE_PRIVATE
72130 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
72140 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72 73 eginTrigger(Pars
72150 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e e*, Token*,Token
72160 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73 74 *,int,int,IdList
72170 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20 20 *,SrcList*,.
72180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72190 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e 74 Expr*,int
721a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
721b0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
721c0 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 lite3FinishTrigg
721d0 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67 er(Parse*, Trigg
721e0 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a 29 erStep*, Token*)
721f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72200 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 void sqlite3D
72210 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73 65 ropTrigger(Parse
72220 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 *, SrcList*, int
72230 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72240 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
72250 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 50 DropTriggerPtr(P
72260 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a 29 arse*, Trigger*)
72270 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72280 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 Trigger *sqli
72290 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 te3TriggersExist
722a0 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65 2a (Parse *, Table*
722b0 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a , int, ExprList*
722c0 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a 53 , int *pMask);.S
722d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
722e0 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 Trigger *sqlite3
722f0 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72 73 TriggerList(Pars
72300 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 e *, Table *);.S
72310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
72320 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 void sqlite3Code
72330 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73 65 RowTrigger(Parse
72340 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69 6e *, Trigger *, in
72350 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e t, ExprList*, in
72360 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20 20 t, Table *,.
72370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72380 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e 74 int, int
72390 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 , int);.SQLITE_P
723a0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
723b0 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 lite3CodeRowTrig
723c0 67 65 72 44 69 72 65 63 74 28 50 61 72 73 65 20 gerDirect(Parse
723d0 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 54 61 *, Trigger *, Ta
723e0 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c ble *, int, int,
723f0 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73 71 int);. void sq
72400 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72 73 liteViewTriggers
72410 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c (Parse*, Table*,
72420 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 Expr*, int, Exp
72430 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f rList*);.SQLITE_
72440 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
72450 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 qlite3DeleteTrig
72460 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33 2a gerStep(sqlite3*
72470 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29 3b , TriggerStep*);
72480 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72490 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 TriggerStep *s
724a0 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65 6c qlite3TriggerSel
724b0 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33 2a ectStep(sqlite3*
724c0 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 ,Select*);.SQLIT
724d0 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 E_PRIVATE Trig
724e0 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 gerStep *sqlite3
724f0 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 TriggerInsertSte
72500 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e p(sqlite3*,Token
72510 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 *, IdList*,.
72520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
72540 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 ExprList*,Se
72550 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49 54 lect*,u8);.SQLIT
72560 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 E_PRIVATE Trig
72570 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 gerStep *sqlite3
72580 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 TriggerUpdateSte
72590 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e p(sqlite3*,Token
725a0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 *,ExprList*, Exp
725b0 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f r*, u8);.SQLITE_
725c0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 PRIVATE Trigge
725d0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 rStep *sqlite3Tr
725e0 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 iggerDeleteStep(
725f0 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c sqlite3*,Token*,
72600 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f Expr*);.SQLITE_
72610 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
72620 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 qlite3DeleteTrig
72630 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 ger(sqlite3*, Tr
72640 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f igger*);.SQLITE_
72650 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 PRIVATE void s
72660 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 qlite3UnlinkAndD
72670 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c eleteTrigger(sql
72680 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 ite3*,int,const
72690 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 char*);.SQLITE_P
726a0 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c RIVATE u32 sql
726b0 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 ite3TriggerOldma
726c0 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67 65 sk(Parse*,Trigge
726d0 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 61 62 r*,ExprList*,Tab
726e0 6c 65 2a 2c 69 6e 74 29 3b 0a 23 20 64 65 66 69 le*,int);.# defi
726f0 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 ne sqlite3ParseT
72700 6f 70 6c 65 76 65 6c 28 70 29 20 28 28 70 29 2d oplevel(p) ((p)-
72710 3e 70 54 6f 70 6c 65 76 65 6c 20 3f 20 28 70 29 >pToplevel ? (p)
72720 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3a 20 28 70 ->pToplevel : (p
72730 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e )).#else.# defin
72740 65 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 e sqlite3Trigger
72750 73 45 78 69 73 74 28 42 2c 43 2c 44 2c 45 2c 46 sExist(B,C,D,E,F
72760 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ) 0.# define sql
72770 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 ite3DeleteTrigge
72780 72 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 r(A,B).# define
72790 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 sqlite3DropTrigg
727a0 65 72 50 74 72 28 41 2c 42 29 0a 23 20 64 65 66 erPtr(A,B).# def
727b0 69 6e 65 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e ine sqlite3Unlin
727c0 6b 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 kAndDeleteTrigge
727d0 72 28 41 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e r(A,B,C).# defin
727e0 65 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 e sqlite3CodeRow
727f0 54 72 69 67 67 65 72 28 41 2c 42 2c 43 2c 44 2c Trigger(A,B,C,D,
72800 45 2c 46 2c 47 2c 48 2c 49 29 0a 23 20 64 65 66 E,F,G,H,I).# def
72810 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64 65 52 ine sqlite3CodeR
72820 6f 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28 owTriggerDirect(
72830 41 2c 42 2c 43 2c 44 2c 45 2c 46 29 0a 23 20 64 A,B,C,D,E,F).# d
72840 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 efine sqlite3Tri
72850 67 67 65 72 4c 69 73 74 28 58 2c 20 59 29 20 30 ggerList(X, Y) 0
72860 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 .# define sqlite
72870 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 3ParseToplevel(p
72880 29 20 70 0a 23 20 64 65 66 69 6e 65 20 73 71 6c ) p.# define sql
72890 69 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 ite3TriggerOldma
728a0 73 6b 28 41 2c 42 2c 43 2c 44 2c 45 29 20 30 0a sk(A,B,C,D,E) 0.
728b0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
728c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
728d0 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65 e3JoinType(Parse
728e0 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e *, Token*, Token
728f0 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
72900 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
72910 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72 sqlite3CreateFor
72920 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 eignKey(Parse*,
72930 45 78 70 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e ExprList*, Token
72940 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e *, ExprList*, in
72950 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
72960 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
72970 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 eferForeignKey(P
72980 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 arse*, int);.#if
72990 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
729a0 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 _AUTHORIZATION.S
729b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
729c0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
729d0 52 65 61 64 28 50 61 72 73 65 2a 2c 45 78 70 72 Read(Parse*,Expr
729e0 2a 2c 53 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73 *,Schema*,SrcLis
729f0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
72a00 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
72a10 33 41 75 74 68 43 68 65 63 6b 28 50 61 72 73 65 3AuthCheck(Parse
72a20 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 *,int, const cha
72a30 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c r*, const char*,
72a40 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 const char*);.S
72a50 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
72a60 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 void sqlite3Auth
72a70 43 6f 6e 74 65 78 74 50 75 73 68 28 50 61 72 73 ContextPush(Pars
72a80 65 2a 2c 20 41 75 74 68 43 6f 6e 74 65 78 74 2a e*, AuthContext*
72a90 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
72aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
72ab0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 void sqlite3Aut
72ac0 68 43 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68 hContextPop(Auth
72ad0 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 Context*);.SQLIT
72ae0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
72af0 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 43 sqlite3AuthReadC
72b00 6f 6c 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 ol(Parse*, const
72b10 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 char *, const c
72b20 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c har *, int);.#el
72b30 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 se.# define sqli
72b40 74 65 33 41 75 74 68 52 65 61 64 28 61 2c 62 2c te3AuthRead(a,b,
72b50 63 2c 64 29 0a 23 20 64 65 66 69 6e 65 20 73 71 c,d).# define sq
72b60 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 61 lite3AuthCheck(a
72b70 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 53 51 4c ,b,c,d,e) SQL
72b80 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 ITE_OK.# define
72b90 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 sqlite3AuthConte
72ba0 78 74 50 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 xtPush(a,b,c).#
72bb0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 define sqlite3Au
72bc0 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 thContextPop(a)
72bd0 20 28 28 76 6f 69 64 29 28 61 29 29 0a 23 65 6e ((void)(a)).#en
72be0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
72bf0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 TE void sqlite3A
72c00 74 74 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 ttach(Parse*, Ex
72c10 70 72 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 pr*, Expr*, Expr
72c20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
72c30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
72c40 65 74 61 63 68 28 50 61 72 73 65 2a 2c 20 45 78 etach(Parse*, Ex
72c50 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 pr*);.SQLITE_PRI
72c60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72c70 42 74 72 65 65 46 61 63 74 6f 72 79 28 73 71 6c BtreeFactory(sql
72c80 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
72c90 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c char *zFilename,
72ca0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
72cb0 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 int omit
72cc0 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 Journal, int nCa
72cd0 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 che, int flags,
72ce0 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29 Btree **ppBtree)
72cf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
72d00 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 int sqlite3FixI
72d10 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 nit(DbFixer*, Pa
72d20 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 rse*, int, const
72d30 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f char*, const To
72d40 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ken*);.SQLITE_PR
72d50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
72d60 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 3FixSrcList(DbFi
72d70 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b xer*, SrcList*);
72d80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72d90 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 int sqlite3FixSe
72da0 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 lect(DbFixer*, S
72db0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f elect*);.SQLITE_
72dc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72dd0 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69 78 te3FixExpr(DbFix
72de0 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c er*, Expr*);.SQL
72df0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
72e00 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 sqlite3FixExprLi
72e10 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 st(DbFixer*, Exp
72e20 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f rList*);.SQLITE_
72e30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
72e40 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65 te3FixTriggerSte
72e50 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 p(DbFixer*, Trig
72e60 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 gerStep*);.SQLIT
72e70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
72e80 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 lite3AtoF(const
72e90 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a char *z, double*
72ea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
72eb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 E int sqlite3Get
72ec0 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 Int32(const char
72ed0 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 *, int*);.SQLIT
72ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
72ef0 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 lite3FitsIn64Bit
72f00 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 s(const char *,
72f10 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
72f20 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
72f30 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e Utf16ByteLen(con
72f40 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 st void *pData,
72f50 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 int nChar);.SQLI
72f60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
72f70 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 qlite3Utf8CharLe
72f80 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 n(const char *pD
72f90 61 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b ata, int nByte);
72fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
72fb0 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 int sqlite3Utf8R
72fc0 65 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 ead(const u8*, c
72fd0 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a onst u8**);../*.
72fe0 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 ** Routines to r
72ff0 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 ead and write va
73000 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
73010 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 tegers. These u
73020 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 sed to.** be def
73030 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 ined locally, bu
73040 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 t now we use the
73050 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 varint routines
73060 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a in the util.c.*
73070 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 * file. Code sh
73080 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 ould use the MAC
73090 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 RO forms below,
730a0 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 as the Varint32
730b0 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 versions.** are
730c0 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 coded to assume
730d0 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 the single byte
730e0 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 case is already
730f0 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a handled (which .
73100 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 ** the MACRO for
73110 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 m does)..*/.SQLI
73120 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
73130 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 qlite3PutVarint(
73140 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 unsigned char*,
73150 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 u64);.SQLITE_PRI
73160 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
73170 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 PutVarint32(unsi
73180 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 gned char*, u32)
73190 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
731a0 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 u8 sqlite3GetVa
731b0 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 rint(const unsig
731c0 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 ned char *, u64
731d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
731e0 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 TE u8 sqlite3Get
731f0 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 Varint32(const u
73200 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 nsigned char *,
73210 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 u32 *);.SQLITE_P
73220 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73230 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 e3VarintLen(u64
73240 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 v);../*.** The h
73250 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 eader of a recor
73260 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 d consists of a
73270 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c sequence variabl
73280 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e-length integer
73290 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 s..** These inte
732a0 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 gers are almost
732b0 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 always small and
732c0 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 are encoded as
732d0 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a a single byte..*
732e0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
732f0 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 macros take adva
73300 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74 20 ntage this fact
73310 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 to provide a fas
73320 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 t encode.** and
73330 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e decode of the in
73340 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f tegers in a reco
73350 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 rd header. It i
73360 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 s faster for the
73370 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 common.** case
73380 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 where the intege
73390 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 r is a single by
733a0 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 te. It is a lit
733b0 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 tle slower when
733c0 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 the.** integer i
733d0 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 s two or more by
733e0 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c tes. But overal
733f0 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a l it is faster..
73400 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 **.** The follow
73410 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ing expressions
73420 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a are equivalent:.
73430 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 **.** x = sq
73440 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 lite3GetVarint32
73450 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 ( A, &B );.**
73460 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 x = sqlite3Put
73470 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 Varint32( A, B )
73480 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 ;.**.** x =
73490 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 getVarint32( A,
734a0 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 B );.** x =
734b0 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 putVarint32( A,
734c0 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 B );.**.*/.#defi
734d0 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 ne getVarint32(A
734e0 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c ,B) (u8)((*(A)<
734f0 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 (u8)0x80) ? ((B)
73500 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 = (u32)*(A)),1
73510 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 : sqlite3GetVari
73520 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a nt32((A), (u32 *
73530 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 )&(B))).#define
73540 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 putVarint32(A,B)
73550 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42 29 (u8)(((u32)(B)
73560 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a <(u32)0x80) ? (*
73570 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 (A) = (unsigned
73580 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 char)(B)),1 : sq
73590 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 lite3PutVarint32
735a0 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 ((A), (B))).#def
735b0 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 ine getVarint
735c0 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
735d0 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 t.#define putVar
735e0 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 int sqlite3Pu
735f0 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 tVarint...SQLITE
73600 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
73610 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 har *sqlite3Inde
73620 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 xAffinityStr(Vdb
73630 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 e *, Index *);.S
73640 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
73650 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 id sqlite3TableA
73660 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 ffinityStr(Vdbe
73670 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c *, Table *);.SQL
73680 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
73690 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
736a0 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 ffinity(Expr *pE
736b0 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29 3b xpr, char aff2);
736c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
736d0 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 int sqlite3Index
736e0 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 AffinityOk(Expr
736f0 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 *pExpr, char idx
73700 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 _affinity);.SQLI
73710 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 TE_PRIVATE char
73720 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
73730 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 ity(Expr *pExpr)
73740 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73750 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 int sqlite3Atoi
73760 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 64(const char*,
73770 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 i64*);.SQLITE_PR
73780 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
73790 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2a e3Error(sqlite3*
737a0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 , int, const cha
737b0 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f r*,...);.SQLITE_
737c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
737d0 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 lite3HexToBlob(s
737e0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 qlite3*, const c
737f0 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a har *z, int n);.
73800 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
73810 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 nt sqlite3TwoPar
73820 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 tName(Parse *, T
73830 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c oken *, Token *,
73840 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 Token **);.SQLI
73850 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 TE_PRIVATE const
73860 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 char *sqlite3Er
73870 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 rStr(int);.SQLIT
73880 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
73890 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 lite3ReadSchema(
738a0 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a Parse *pParse);.
738b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
738c0 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46 ollSeq *sqlite3F
738d0 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 indCollSeq(sqlit
738e0 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 e3*,u8 enc, cons
738f0 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 t char*,int);.SQ
73900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c LITE_PRIVATE Col
73910 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 lSeq *sqlite3Loc
73920 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 ateCollSeq(Parse
73930 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 *pParse, const
73940 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 4c char*zName);.SQL
73950 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c ITE_PRIVATE Coll
73960 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 Seq *sqlite3Expr
73970 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 CollSeq(Parse *p
73980 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
73990 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 pr);.SQLITE_PRIV
739a0 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
739b0 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 3ExprSetColl(Par
739c0 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
739d0 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 *, Token *);.SQ
739e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
739f0 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c sqlite3CheckCol
73a00 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 43 6f lSeq(Parse *, Co
73a10 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 llSeq *);.SQLITE
73a20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73a30 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e ite3CheckObjectN
73a40 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e ame(Parse *, con
73a50 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 st char *);.SQLI
73a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
73a70 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 sqlite3VdbeSetCh
73a80 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 2c anges(sqlite3 *,
73a90 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 int);..SQLITE_P
73aa0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 RIVATE const voi
73ab0 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 d *sqlite3ValueT
73ac0 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ext(sqlite3_valu
73ad0 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f e*, u8);.SQLITE_
73ae0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73af0 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 te3ValueBytes(sq
73b00 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 lite3_value*, u8
73b10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
73b20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 E void sqlite3Va
73b30 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69 74 65 lueSetStr(sqlite
73b40 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 3_value*, int, c
73b50 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 onst void *,u8,
73b60 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
73b70 20 20 20 20 20 20 20 20 20 76 6f 69 64 28 2a 29 void(*)
73b80 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 (void*));.SQLITE
73b90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
73ba0 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 lite3ValueFree(s
73bb0 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a qlite3_value*);.
73bc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
73bd0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 qlite3_value *sq
73be0 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 lite3ValueNew(sq
73bf0 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 lite3 *);.SQLITE
73c00 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 _PRIVATE char *s
73c10 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 qlite3Utf16to8(s
73c20 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 qlite3 *, const
73c30 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 void*, int);.#if
73c40 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
73c50 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 E_STAT2.SQLITE_P
73c60 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
73c70 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c ite3Utf8to16(sql
73c80 69 74 65 33 20 2a 2c 20 75 38 2c 20 63 68 61 72 ite3 *, u8, char
73c90 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b *, int, int *);
73ca0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
73cb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
73cc0 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 e3ValueFromExpr(
73cd0 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 sqlite3 *, Expr
73ce0 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 *, u8, u8, sqlit
73cf0 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 e3_value **);.SQ
73d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
73d10 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 d sqlite3ValueAp
73d20 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 plyAffinity(sqli
73d30 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c te3_value *, u8,
73d40 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 u8);.#ifndef SQ
73d50 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f LITE_AMALGAMATIO
73d60 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 N.SQLITE_PRIVATE
73d70 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
73d80 63 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63 6f char sqlite3Opco
73d90 64 65 50 72 6f 70 65 72 74 79 5b 5d 3b 0a 53 51 deProperty[];.SQ
73da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e LITE_PRIVATE con
73db0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
73dc0 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c sqlite3UpperToL
73dd0 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 ower[];.SQLITE_P
73de0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 RIVATE const uns
73df0 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 igned char sqlit
73e00 65 33 43 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 e3CtypeMap[];.SQ
73e10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c LITE_PRIVATE SQL
73e20 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 53 ITE_WSD struct S
73e30 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73 71 6c qlite3Config sql
73e40 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 ite3Config;.SQLI
73e50 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 TE_PRIVATE SQLIT
73e60 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 E_WSD FuncDefHas
73e70 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 h sqlite3GlobalF
73e80 75 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 unctions;.SQLITE
73e90 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
73ea0 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 3b ite3PendingByte;
73eb0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 .#endif.SQLITE_P
73ec0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
73ed0 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 te3RootPageMoved
73ee0 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b (Db*, int, int);
73ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
73f00 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e void sqlite3Rein
73f10 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 dex(Parse*, Toke
73f20 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c n*, Token*);.SQL
73f30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
73f40 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e sqlite3AlterFun
73f50 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 ctions(sqlite3*)
73f60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
73f70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
73f80 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 erRenameTable(Pa
73f90 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 rse*, SrcList*,
73fa0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f Token*);.SQLITE_
73fb0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
73fc0 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 te3GetToken(cons
73fd0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
73fe0 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 *, int *);.SQLIT
73ff0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74000 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 qlite3NestedPars
74010 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 e(Parse*, const
74020 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c char*, ...);.SQL
74030 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74040 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 sqlite3ExpirePr
74050 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 eparedStatements
74060 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 (sqlite3*);.SQLI
74070 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74080 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c qlite3CodeSubsel
74090 65 63 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70 ect(Parse *, Exp
740a0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a r *, int, int);.
740b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
740c0 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 oid sqlite3Selec
740d0 74 50 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65 tPrep(Parse*, Se
740e0 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 lect*, NameConte
740f0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 xt*);.SQLITE_PRI
74100 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
74110 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
74120 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 (NameContext*, E
74130 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 xpr*);.SQLITE_PR
74140 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
74150 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e e3ResolveSelectN
74160 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c ames(Parse*, Sel
74170 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 ect*, NameContex
74180 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
74190 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 ATE int sqlite3R
741a0 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 esolveOrderGroup
741b0 42 79 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 By(Parse*, Selec
741c0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 t*, ExprList*, c
741d0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c onst char*);.SQL
741e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
741f0 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 sqlite3ColumnDe
74200 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 fault(Vdbe *, Ta
74210 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 ble *, int, int)
74220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74230 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 void sqlite3Alt
74240 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d erFinishAddColum
74250 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e n(Parse *, Token
74260 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
74270 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
74280 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c AlterBeginAddCol
74290 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 53 72 63 umn(Parse *, Src
742a0 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f List *);.SQLITE_
742b0 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 PRIVATE CollSeq
742c0 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 *sqlite3GetCollS
742d0 65 71 28 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c eq(sqlite3*, u8,
742e0 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 CollSeq *, cons
742f0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 t char*);.SQLITE
74300 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 _PRIVATE char sq
74310 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 lite3AffinityTyp
74320 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a e(const char*);.
74330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
74340 6f 69 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 oid sqlite3Analy
74350 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e ze(Parse*, Token
74360 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 *, Token*);.SQLI
74370 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
74380 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 qlite3InvokeBusy
74390 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 Handler(BusyHand
743a0 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ler*);.SQLITE_PR
743b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
743c0 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 3FindDb(sqlite3*
743d0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
743e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
743f0 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 lite3FindDbName(
74400 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 sqlite3 *, const
74410 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 char *);.SQLITE
74420 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
74430 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 ite3AnalysisLoad
74440 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 (sqlite3*,int iD
74450 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 B);.SQLITE_PRIVA
74460 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 TE void sqlite3D
74470 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 eleteIndexSample
74480 73 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 s(Index*);.SQLIT
74490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
744a0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77 qlite3DefaultRow
744b0 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c Est(Index*);.SQL
744c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
744d0 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 sqlite3Register
744e0 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 LikeFunctions(sq
744f0 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 lite3*, int);.SQ
74500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
74510 20 73 71 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 sqlite3IsLikeFu
74520 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c nction(sqlite3*,
74530 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a Expr*,int*,char*
74540 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74550 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 E void sqlite3Mi
74560 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 nimumFileFormat(
74570 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 Parse*, int, int
74580 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74590 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 E void sqlite3Sc
745a0 68 65 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 29 hemaFree(void *)
745b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
745c0 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 Schema *sqlite3
745d0 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69 74 65 SchemaGet(sqlite
745e0 33 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 3 *, Btree *);.S
745f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
74600 74 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 t sqlite3SchemaT
74610 6f 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a oIndex(sqlite3 *
74620 64 62 2c 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 db, Schema *);.S
74630 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b 65 QLITE_PRIVATE Ke
74640 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e yInfo *sqlite3In
74650 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 dexKeyinfo(Parse
74660 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 *, Index *);.SQ
74670 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
74680 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 sqlite3CreateFu
74690 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f nc(sqlite3 *, co
746a0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c nst char *, int,
746b0 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 int, void *, .
746c0 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65 void (*)(sqlite
746d0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 3_context*,int,s
746e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 qlite3_value **)
746f0 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c ,. void (*)(sql
74700 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
74710 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 t,sqlite3_value
74720 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 **), void (*)(sq
74730 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 lite3_context*))
74740 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74750 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 int sqlite3ApiE
74760 78 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c xit(sqlite3 *db,
74770 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 int);.SQLITE_PR
74780 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
74790 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 3OpenTempDatabas
747a0 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c e(Parse *);..SQL
747b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
747c0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
747d0 49 6e 69 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 Init(StrAccum*,
747e0 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 char*, int, int)
747f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
74800 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 void sqlite3Str
74810 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74 72 41 AccumAppend(StrA
74820 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 ccum*,const char
74830 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 *,int);.SQLITE_P
74840 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c RIVATE char *sql
74850 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 ite3StrAccumFini
74860 73 68 28 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 sh(StrAccum*);.S
74870 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74880 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 id sqlite3StrAcc
74890 75 6d 52 65 73 65 74 28 53 74 72 41 63 63 75 6d umReset(StrAccum
748a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
748b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 TE void sqlite3S
748c0 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 53 65 electDestInit(Se
748d0 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e lectDest*,int,in
748e0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 t);.SQLITE_PRIVA
748f0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
74900 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 CreateColumnExpr
74910 28 73 71 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c (sqlite3 *, SrcL
74920 69 73 74 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 ist *, int, int)
74930 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ;..SQLITE_PRIVAT
74940 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 61 E void sqlite3Ba
74950 63 6b 75 70 52 65 73 74 61 72 74 28 73 71 6c 69 ckupRestart(sqli
74960 74 65 33 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 te3_backup *);.S
74970 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
74980 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 id sqlite3Backup
74990 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 62 Update(sqlite3_b
749a0 61 63 6b 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 ackup *, Pgno, c
749b0 6f 6e 73 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a onst u8 *);../*.
749c0 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
749d0 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 to the LEMON-ge
749e0 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a 2a nerated parser.*
749f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
74a00 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 void *sqlite3Pa
74a10 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 rserAlloc(void*(
74a20 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c *)(size_t));.SQL
74a30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
74a40 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 sqlite3ParserFr
74a50 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a ee(void*, void(*
74a60 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 )(void*));.SQLIT
74a70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
74a80 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f 69 qlite3Parser(voi
74a90 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 d*, int, Token,
74aa0 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 Parse*);.#ifdef
74ab0 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 YYTRACKMAXSTACKD
74ac0 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 EPTH.SQLITE_PRIV
74ad0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 ATE int sqlite
74ae0 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 3ParserStackPeak
74af0 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a (void*);.#endif.
74b00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
74b10 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f void sqlite3Auto
74b20 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 LoadExtensions(s
74b30 71 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 qlite3*);.#ifnde
74b40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f f SQLITE_OMIT_LO
74b50 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c AD_EXTENSION.SQL
74b60 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f ITE_PRIVATE vo
74b70 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 id sqlite3CloseE
74b80 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 xtensions(sqlite
74b90 33 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 3*);.#else.# def
74ba0 69 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 ine sqlite3Close
74bb0 45 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 Extensions(X).#e
74bc0 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 ndif..#ifndef SQ
74bd0 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
74be0 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 _CACHE.SQLITE_PR
74bf0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
74c00 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 ite3TableLock(Pa
74c10 72 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c rse *, int, int,
74c20 20 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 u8, const char
74c30 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 *);.#else. #def
74c40 69 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 ine sqlite3Table
74c50 4c 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a Lock(v,w,x,y,z).
74c60 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
74c70 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
74c80 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 E_PRIVATE int
74c90 73 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 sqlite3Utf8To8(u
74ca0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a nsigned char*);.
74cb0 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
74cc0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
74cd0 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e ALTABLE.# defin
74ce0 65 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 e sqlite3VtabCle
74cf0 61 72 28 59 29 0a 23 20 20 64 65 66 69 6e 65 20 ar(Y).# define
74d00 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 sqlite3VtabSync(
74d10 58 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 X,Y) SQLITE_OK.#
74d20 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
74d30 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a VtabRollback(X).
74d40 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 # define sqlite
74d50 33 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 3VtabCommit(X).#
74d60 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
74d70 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30 VtabInSync(db) 0
74d80 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
74d90 65 33 56 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23 e3VtabLock(X) .#
74da0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 define sqlite3
74db0 56 74 61 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20 VtabUnlock(X).#
74dc0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
74dd0 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29 tabUnlockList(X)
74de0 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 .#else.SQLITE_PR
74df0 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 IVATE void sq
74e00 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 lite3VtabClear(T
74e10 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 able*);.SQLITE_P
74e20 52 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 RIVATE int sq
74e30 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 lite3VtabSync(sq
74e40 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 lite3 *db, char
74e50 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 **);.SQLITE_PRIV
74e60 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 ATE int sqlit
74e70 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 e3VtabRollback(s
74e80 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c qlite3 *db);.SQL
74e90 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 ITE_PRIVATE i
74ea0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f nt sqlite3VtabCo
74eb0 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 mmit(sqlite3 *db
74ec0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
74ed0 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 E void sqlite
74ee0 33 56 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 3VtabLock(VTable
74ef0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 *);.SQLITE_PRIV
74f00 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 ATE void sqli
74f10 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 te3VtabUnlock(VT
74f20 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f able *);.SQLITE_
74f30 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 PRIVATE void
74f40 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 sqlite3VtabUnloc
74f50 6b 4c 69 73 74 28 73 71 6c 69 74 65 33 2a 29 3b kList(sqlite3*);
74f60 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 .# define sqlit
74f70 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 e3VtabInSync(db)
74f80 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e ((db)->nVTrans>
74f90 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72 61 0 && (db)->aVTra
74fa0 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 ns==0).#endif.SQ
74fb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
74fc0 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b d sqlite3VtabMak
74fd0 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 2a eWritable(Parse*
74fe0 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 ,Table*);.SQLITE
74ff0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
75000 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 lite3VtabBeginPa
75010 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 rse(Parse*, Toke
75020 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 n*, Token*, Toke
75030 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 n*);.SQLITE_PRIV
75040 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
75050 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 VtabFinishParse(
75060 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b Parse*, Token*);
75070 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
75080 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
75090 41 72 67 49 6e 69 74 28 50 61 72 73 65 2a 29 3b ArgInit(Parse*);
750a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
750b0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 void sqlite3Vtab
750c0 41 72 67 45 78 74 65 6e 64 28 50 61 72 73 65 2a ArgExtend(Parse*
750d0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 , Token*);.SQLIT
750e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
750f0 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 lite3VtabCallCre
75100 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e ate(sqlite3*, in
75110 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c t, const char *,
75120 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 char **);.SQLIT
75130 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
75140 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e lite3VtabCallCon
75150 6e 65 63 74 28 50 61 72 73 65 2a 2c 20 54 61 62 nect(Parse*, Tab
75160 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 le*);.SQLITE_PRI
75170 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
75180 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 VtabCallDestroy(
75190 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 sqlite3*, int, c
751a0 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 onst char *);.SQ
751b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
751c0 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 sqlite3VtabBegi
751d0 6e 28 73 71 6c 69 74 65 33 20 2a 2c 20 56 54 61 n(sqlite3 *, VTa
751e0 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 ble *);.SQLITE_P
751f0 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a RIVATE FuncDef *
75200 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c sqlite3VtabOverl
75210 6f 61 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 oadFunction(sqli
75220 74 65 33 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 te3 *,FuncDef*,
75230 69 6e 74 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 int nArg, Expr*)
75240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75250 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76 void sqlite3Inv
75260 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c alidFunction(sql
75270 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e ite3_context*,in
75280 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a t,sqlite3_value*
75290 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
752a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 72 TE int sqlite3Tr
752b0 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28 73 ansferBindings(s
752c0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2c 20 73 qlite3_stmt *, s
752d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 3b 0a qlite3_stmt *);.
752e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
752f0 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 nt sqlite3Reprep
75300 61 72 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 are(Vdbe*);.SQLI
75310 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
75320 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 sqlite3ExprListC
75330 68 65 63 6b 4c 65 6e 67 74 68 28 50 61 72 73 65 heckLength(Parse
75340 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f *, ExprList*, co
75350 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 nst char*);.SQLI
75360 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 TE_PRIVATE CollS
75370 65 71 20 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 eq *sqlite3Binar
75380 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 yCompareCollSeq(
75390 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a 2c Parse *, Expr *,
753a0 20 45 78 70 72 20 2a 29 3b 0a 53 51 4c 49 54 45 Expr *);.SQLITE
753b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
753c0 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 ite3TempInMemory
753d0 28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 2a 29 (const sqlite3*)
753e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
753f0 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 VTable *sqlite3
75400 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69 74 65 GetVTable(sqlite
75410 33 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 0a 2f 2a 3*, Table*);../*
75420 20 44 65 63 6c 61 72 61 74 69 6f 6e 73 20 66 6f Declarations fo
75430 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 66 r functions in f
75440 6b 65 79 2e 63 2e 20 41 6c 6c 20 6f 66 20 74 68 key.c. All of th
75450 65 73 65 20 61 72 65 20 72 65 70 6c 61 63 65 64 ese are replaced
75460 20 62 79 0a 2a 2a 20 6e 6f 2d 6f 70 20 6d 61 63 by.** no-op mac
75470 72 6f 73 20 69 66 20 4f 4d 49 54 5f 46 4f 52 45 ros if OMIT_FORE
75480 49 47 4e 5f 4b 45 59 20 69 73 20 64 65 66 69 6e IGN_KEY is defin
75490 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ed. In this case
754a0 20 6e 6f 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b no foreign.** k
754b0 65 79 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 ey functionality
754c0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 20 49 is available. I
754d0 66 20 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 69 f OMIT_TRIGGER i
754e0 73 20 64 65 66 69 6e 65 64 20 62 75 74 0a 2a 2a s defined but.**
754f0 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 OMIT_FOREIGN_KE
75500 59 20 69 73 20 6e 6f 74 2c 20 6f 6e 6c 79 20 73 Y is not, only s
75510 6f 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 ome of the funct
75520 69 6f 6e 73 20 61 72 65 20 6e 6f 2d 6f 70 65 64 ions are no-oped
75530 2e 20 49 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73 . In.** this cas
75540 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 e foreign keys a
75550 72 65 20 70 61 72 73 65 64 2c 20 62 75 74 20 6e re parsed, but n
75560 6f 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e o other function
75570 61 6c 69 74 79 20 69 73 20 0a 2a 2a 20 70 72 6f ality is .** pro
75580 76 69 64 65 64 20 28 65 6e 66 6f 72 63 65 6d 65 vided (enforceme
75590 6e 74 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61 nt of FK constra
755a0 69 6e 74 73 20 72 65 71 75 69 72 65 73 20 74 68 ints requires th
755b0 65 20 74 72 69 67 67 65 72 73 20 73 75 62 2d 73 e triggers sub-s
755c0 79 73 74 65 6d 29 2e 0a 2a 2f 0a 23 69 66 20 21 ystem)..*/.#if !
755d0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
755e0 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 MIT_FOREIGN_KEY)
755f0 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
75600 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
75610 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ).SQLITE_PRIVATE
75620 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 void sqlite3F
75630 6b 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 20 54 kCheck(Parse*, T
75640 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 able*, int, int)
75650 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75660 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 void sqlite3F
75670 6b 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73 65 kDropTable(Parse
75680 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 54 61 *, SrcList *, Ta
75690 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 ble*);.SQLITE_PR
756a0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c IVATE void sql
756b0 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 50 61 ite3FkActions(Pa
756c0 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 rse*, Table*, Ex
756d0 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 prList*, int);.S
756e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
756f0 69 6e 74 20 73 71 6c 69 74 65 33 46 6b 52 65 71 int sqlite3FkReq
75700 75 69 72 65 64 28 50 61 72 73 65 2a 2c 20 54 61 uired(Parse*, Ta
75710 62 6c 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 ble*, int*, int)
75720 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
75730 20 20 20 75 33 32 20 73 71 6c 69 74 65 33 46 6b u32 sqlite3Fk
75740 4f 6c 64 6d 61 73 6b 28 50 61 72 73 65 2a 2c 20 Oldmask(Parse*,
75750 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f Table*);.SQLITE_
75760 50 52 49 56 41 54 45 20 20 20 46 4b 65 79 20 2a PRIVATE FKey *
75770 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e sqlite3FkReferen
75780 63 65 73 28 54 61 62 6c 65 20 2a 29 3b 0a 23 65 ces(Table *);.#e
75790 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 lse. #define sq
757a0 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 61 lite3FkActions(a
757b0 2c 62 2c 63 2c 64 29 0a 20 20 23 64 65 66 69 6e ,b,c,d). #defin
757c0 65 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b e sqlite3FkCheck
757d0 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23 64 65 66 (a,b,c,d). #def
757e0 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 44 72 6f ine sqlite3FkDro
757f0 70 54 61 62 6c 65 28 61 2c 62 2c 63 29 0a 20 20 pTable(a,b,c).
75800 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 #define sqlite3F
75810 6b 4f 6c 64 6d 61 73 6b 28 61 2c 62 29 20 20 20 kOldmask(a,b)
75820 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 73 0. #define s
75830 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 qlite3FkRequired
75840 28 61 2c 62 2c 63 2c 64 29 20 30 0a 23 65 6e 64 (a,b,c,d) 0.#end
75850 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 if.#ifndef SQLIT
75860 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b E_OMIT_FOREIGN_K
75870 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 EY.SQLITE_PRIVAT
75880 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
75890 46 6b 44 65 6c 65 74 65 28 54 61 62 6c 65 2a 29 FkDelete(Table*)
758a0 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e ;.#else. #defin
758b0 65 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 e sqlite3FkDelet
758c0 65 28 61 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a e(a).#endif.../*
758d0 0a 2a 2a 20 41 76 61 69 6c 61 62 6c 65 20 66 61 .** Available fa
758e0 75 6c 74 20 69 6e 6a 65 63 74 6f 72 73 2e 20 20 ult injectors.
758f0 53 68 6f 75 6c 64 20 62 65 20 6e 75 6d 62 65 72 Should be number
75900 65 64 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 ed beginning wit
75910 68 20 30 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 h 0..*/.#define
75920 53 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 SQLITE_FAULTINJE
75930 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20 20 20 20 20 CTOR_MALLOC
75940 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 0.#define SQLITE
75950 5f 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 43 _FAULTINJECTOR_C
75960 4f 55 4e 54 20 20 20 20 20 20 31 0a 0a 2f 2a 0a OUNT 1../*.
75970 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 ** The interface
75980 20 74 6f 20 74 68 65 20 63 6f 64 65 20 69 6e 20 to the code in
75990 66 61 75 6c 74 2e 63 20 75 73 65 64 20 66 6f 72 fault.c used for
759a0 20 69 64 65 6e 74 69 66 79 69 6e 67 20 22 62 65 identifying "be
759b0 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 nign".** malloc
759c0 66 61 69 6c 75 72 65 73 2e 20 54 68 69 73 20 69 failures. This i
759d0 73 20 6f 6e 6c 79 20 70 72 65 73 65 6e 74 20 69 s only present i
759e0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 f SQLITE_OMIT_BU
759f0 49 4c 54 49 4e 5f 54 45 53 54 0a 2a 2a 20 69 73 ILTIN_TEST.** is
75a00 20 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2f not defined..*/
75a10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
75a20 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
75a30 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 T.SQLITE_PRIVATE
75a40 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 void sqlite3B
75a50 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 eginBenignMalloc
75a60 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 (void);.SQLITE_P
75a70 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 RIVATE void sq
75a80 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 lite3EndBenignMa
75a90 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 23 65 6c 73 lloc(void);.#els
75aa0 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 e. #define sqli
75ab0 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 te3BeginBenignMa
75ac0 6c 6c 6f 63 28 29 0a 20 20 23 64 65 66 69 6e 65 lloc(). #define
75ad0 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
75ae0 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65 6e 64 69 66 nMalloc().#endif
75af0 0a 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 ..#define IN_IND
75b00 45 58 5f 52 4f 57 49 44 20 20 20 20 20 20 20 20 EX_ROWID
75b10 20 20 20 31 0a 23 64 65 66 69 6e 65 20 49 4e 5f 1.#define IN_
75b20 49 4e 44 45 58 5f 45 50 48 20 20 20 20 20 20 20 INDEX_EPH
75b30 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 2.#define
75b40 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20 20 IN_INDEX_INDEX
75b50 20 20 20 20 20 20 20 20 20 33 0a 53 51 4c 49 54 3.SQLIT
75b60 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
75b70 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 lite3FindInIndex
75b80 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a (Parse *, Expr *
75b90 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69 66 64 65 66 , int*);..#ifdef
75ba0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 SQLITE_ENABLE_A
75bb0 54 4f 4d 49 43 5f 57 52 49 54 45 0a 53 51 4c 49 TOMIC_WRITE.SQLI
75bc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
75bd0 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f sqlite3JournalO
75be0 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 pen(sqlite3_vfs
75bf0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c *, const char *,
75c00 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2c sqlite3_file *,
75c10 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 int, int);.SQLI
75c20 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 TE_PRIVATE int
75c30 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 sqlite3JournalS
75c40 69 7a 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 ize(sqlite3_vfs
75c50 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
75c60 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 TE int sqlite3
75c70 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 71 JournalCreate(sq
75c80 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 23 lite3_file *);.#
75c90 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 else. #define s
75ca0 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a qlite3JournalSiz
75cb0 65 28 70 56 66 73 29 20 28 28 70 56 66 73 29 2d e(pVfs) ((pVfs)-
75cc0 3e 73 7a 4f 73 46 69 6c 65 29 0a 23 65 6e 64 69 >szOsFile).#endi
75cd0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
75ce0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 E void sqlite3Me
75cf0 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c mJournalOpen(sql
75d00 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 53 51 ite3_file *);.SQ
75d10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
75d20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
75d30 61 6c 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53 51 alSize(void);.SQ
75d40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
75d50 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 sqlite3IsMemJou
75d60 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c rnal(sqlite3_fil
75d70 65 20 2a 29 3b 0a 0a 23 69 66 20 53 51 4c 49 54 e *);..#if SQLIT
75d80 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 E_MAX_EXPR_DEPTH
75d90 3e 30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 >0.SQLITE_PRIVAT
75da0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
75db0 45 78 70 72 53 65 74 48 65 69 67 68 74 28 50 61 ExprSetHeight(Pa
75dc0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 rse *pParse, Exp
75dd0 72 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 r *p);.SQLITE_PR
75de0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 IVATE int sqli
75df0 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 te3SelectExprHei
75e00 67 68 74 28 53 65 6c 65 63 74 20 2a 29 3b 0a 53 ght(Select *);.S
75e10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
75e20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
75e30 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 heckHeight(Parse
75e40 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 20 *, int);.#else.
75e50 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
75e60 45 78 70 72 53 65 74 48 65 69 67 68 74 28 78 2c ExprSetHeight(x,
75e70 79 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c y). #define sql
75e80 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 ite3SelectExprHe
75e90 69 67 68 74 28 78 29 20 30 0a 20 20 23 64 65 66 ight(x) 0. #def
75ea0 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72 43 ine sqlite3ExprC
75eb0 68 65 63 6b 48 65 69 67 68 74 28 78 2c 79 29 0a heckHeight(x,y).
75ec0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 #endif..SQLITE_P
75ed0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
75ee0 65 33 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 e3Get4byte(const
75ef0 20 75 38 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 u8*);.SQLITE_PR
75f00 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
75f10 65 33 50 75 74 34 62 79 74 65 28 75 38 2a 2c 20 e3Put4byte(u8*,
75f20 75 33 32 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 u32);..#ifdef SQ
75f30 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f LITE_ENABLE_UNLO
75f40 43 4b 5f 4e 4f 54 49 46 59 0a 53 51 4c 49 54 45 CK_NOTIFY.SQLITE
75f50 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 _PRIVATE void
75f60 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
75f70 6e 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 nBlocked(sqlite3
75f80 20 2a 2c 20 73 71 6c 69 74 65 33 20 2a 29 3b 0a *, sqlite3 *);.
75f90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
75fa0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e void sqlite3Con
75fb0 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 nectionUnlocked(
75fc0 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 sqlite3 *db);.SQ
75fd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 LITE_PRIVATE v
75fe0 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 oid sqlite3Conne
75ff0 63 74 69 6f 6e 43 6c 6f 73 65 64 28 73 71 6c 69 ctionClosed(sqli
76000 74 65 33 20 2a 64 62 29 3b 0a 23 65 6c 73 65 0a te3 *db);.#else.
76010 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 #define sqlite
76020 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 3ConnectionBlock
76030 65 64 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e ed(x,y). #defin
76040 65 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 e sqlite3Connect
76050 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 78 29 0a 20 ionUnlocked(x).
76060 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 #define sqlite3
76070 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 ConnectionClosed
76080 28 78 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 (x).#endif..#ifd
76090 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
760a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 SQLITE_PRIVATE
760b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 void sqlite3Par
760c0 73 65 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 serTrace(FILE*,
760d0 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a char *);.#endif.
760e0 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 ./*.** If the SQ
760f0 4c 49 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 LITE_ENABLE IOTR
76100 41 43 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 ACE exists then
76110 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 the global varia
76120 62 6c 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f ble.** sqlite3Io
76130 54 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 Trace is a point
76140 65 72 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c er to a printf-l
76150 69 6b 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 ike routine used
76160 20 74 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f to.** print I/O
76170 20 74 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 tracing message
76180 73 2e 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 s. .*/.#ifdef SQ
76190 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 LITE_ENABLE_IOTR
761a0 41 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 ACE.# define IOT
761b0 52 41 43 45 28 41 29 20 20 69 66 28 20 73 71 6c RACE(A) if( sql
761c0 69 74 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 ite3IoTrace ){ s
761d0 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b qlite3IoTrace A;
761e0 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }.SQLITE_PRIVAT
761f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
76200 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 VdbeIOTraceSql(V
76210 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 dbe*);.SQLITE_PR
76220 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c IVATE void (*sql
76230 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e ite3IoTrace)(con
76240 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 st char*,...);.#
76250 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f else.# define IO
76260 54 52 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e TRACE(A).# defin
76270 65 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 e sqlite3VdbeIOT
76280 72 61 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 raceSql(X).#endi
76290 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a f..#endif../****
762a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
762b0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a f sqliteInt.h **
762c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
762d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
762e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
762f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
76300 20 66 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a file global.c *
76310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
76340 20 32 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 2008 June 13.**
76350 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
76360 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
76370 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
76380 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
76390 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
763a0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
763b0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
763c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
763d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
763e0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
763f0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
76400 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
76410 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
76420 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
76430 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
76440 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
76450 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
76460 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
76470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
76490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
764a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
764b0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
764c0 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e tains definition
764d0 73 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 s of global vari
764e0 61 62 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e ables and contan
764f0 74 73 2e 0a 2a 2f 0a 0a 2f 2a 20 41 6e 20 61 72 ts..*/../* An ar
76500 72 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75 ray to map all u
76510 70 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 pper-case charac
76520 74 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20 ters into their
76530 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a corresponding.**
76540 20 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72 lower-case char
76550 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 acter. .**.** SQ
76560 4c 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 Lite only consid
76570 65 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f 72 ers US-ASCII (or
76580 20 45 42 43 44 49 43 29 20 63 68 61 72 61 63 74 EBCDIC) charact
76590 65 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a ers. We do not.
765a0 2a 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20 63 ** handle case c
765b0 6f 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 74 onversions for t
765c0 68 65 20 55 54 46 20 63 68 61 72 61 63 74 65 72 he UTF character
765d0 20 73 65 74 20 73 69 6e 63 65 20 74 68 65 20 74 set since the t
765e0 61 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 ables.** involve
765f0 64 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73 20 d are nearly as
76600 62 69 67 20 6f 72 20 62 69 67 67 65 72 20 74 68 big or bigger th
76610 61 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 an SQLite itself
76620 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
76630 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e ATE const unsign
76640 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 ed char sqlite3U
76650 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 pperToLower[] =
76660 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f {.#ifdef SQLITE_
76670 41 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20 ASCII. 0,
76680 31 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 1, 2, 3, 4,
76690 35 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 5, 6, 7, 8,
766a0 39 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 9, 10, 11, 12, 1
766b0 33 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31 3, 14, 15, 16, 1
766c0 37 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20 7,. 18, 19,
766d0 32 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 20, 21, 22, 23,
766e0 32 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 24, 25, 26, 27,
766f0 32 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 28, 29, 30, 31,
76700 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a 32, 33, 34, 35,.
76710 20 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c 36, 37, 38,
76720 20 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 39, 40, 41, 42,
76730 20 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 43, 44, 45, 46,
76740 20 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c 47, 48, 49, 50,
76750 20 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20 51, 52, 53,.
76760 20 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 54, 55, 56, 57
76770 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 , 58, 59, 60, 61
76780 2c 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37 , 62, 63, 64, 97
76790 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 , 98, 99,100,101
767a0 2c 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30 ,102,103,. 10
767b0 34 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30 4,105,106,107,10
767c0 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 8,109,110,111,11
767d0 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 2,113,114,115,11
767e0 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 6,117,118,119,12
767f0 30 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20 0,121,. 122,
76800 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 91, 92, 93, 94,
76810 39 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20 95, 96, 97, 98,
76820 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 99,100,101,102,1
76830 30 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 03,104,105,106,1
76840 30 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c 07,. 108,109,
76850 31 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 110,111,112,113,
76860 31 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 114,115,116,117,
76870 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 118,119,120,121,
76880 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 122,123,124,125,
76890 0a 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38 . 126,127,128
768a0 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 ,129,130,131,132
768b0 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 ,133,134,135,136
768c0 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 ,137,138,139,140
768d0 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20 ,141,142,143,.
768e0 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 144,145,146,14
768f0 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 7,148,149,150,15
76900 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 1,152,153,154,15
76910 35 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35 5,156,157,158,15
76920 39 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31 9,160,161,. 1
76930 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 62,163,164,165,1
76940 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 66,167,168,169,1
76950 37 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 70,171,172,173,1
76960 37 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 74,175,176,177,1
76970 37 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c 78,179,. 180,
76980 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 181,182,183,184,
76990 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 185,186,187,188,
769a0 31 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 189,190,191,192,
769b0 31 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 193,194,195,196,
769c0 31 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39 197,. 198,199
769d0 2c 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 ,200,201,202,203
769e0 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 ,204,205,206,207
769f0 2c 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 ,208,209,210,211
76a00 2c 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 ,212,213,214,215
76a10 2c 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31 ,. 216,217,21
76a20 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 8,219,220,221,22
76a30 32 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32 2,223,224,225,22
76a40 36 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33 6,227,228,229,23
76a50 30 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20 0,231,232,233,.
76a60 20 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32 234,235,236,2
76a70 33 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32 37,238,239,240,2
76a80 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 41,242,243,244,2
76a90 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 45,246,247,248,2
76aa0 34 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20 49,250,251,.
76ab0 32 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a 252,253,254,255.
76ac0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
76ad0 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20 LITE_EBCDIC.
76ae0 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 0, 1, 2, 3,
76af0 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 4, 5, 6, 7,
76b00 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 8, 9, 10, 11,
76b10 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 12, 13, 14, 15,
76b20 20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31 /* 0x */. 1
76b30 36 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32 6, 17, 18, 19, 2
76b40 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 0, 21, 22, 23, 2
76b50 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 4, 25, 26, 27, 2
76b60 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f 8, 29, 30, 31, /
76b70 2a 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c * 1x */. 32,
76b80 20 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c 33, 34, 35, 36,
76b90 20 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 37, 38, 39, 40,
76ba0 20 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 41, 42, 43, 44,
76bb0 20 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20 45, 46, 47, /*
76bc0 32 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34 2x */. 48, 4
76bd0 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 9, 50, 51, 52, 5
76be0 33 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 3, 54, 55, 56, 5
76bf0 37 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 7, 58, 59, 60, 6
76c00 31 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78 1, 62, 63, /* 3x
76c10 20 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c */. 64, 65,
76c20 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 66, 67, 68, 69,
76c30 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 70, 71, 72, 73,
76c40 20 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c 74, 75, 76, 77,
76c50 20 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a 78, 79, /* 4x *
76c60 2f 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38 /. 80, 81, 8
76c70 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 2, 83, 84, 85, 8
76c80 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39 6, 87, 88, 89, 9
76c90 30 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 0, 91, 92, 93, 9
76ca0 34 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 4, 95, /* 5x */.
76cb0 20 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c 96, 97, 66,
76cc0 20 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 67, 68, 69, 70,
76cd0 20 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c 71, 72, 73,106,
76ce0 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 107,108,109,110,
76cf0 31 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 111, /* 6x */.
76d00 20 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38 112, 81, 82, 8
76d10 33 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 3, 84, 85, 86, 8
76d20 37 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32 7, 88, 89,122,12
76d30 33 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32 3,124,125,126,12
76d40 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 7, /* 7x */.
76d50 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 128,129,130,131,
76d60 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 132,133,134,135,
76d70 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 136,137,138,139,
76d80 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 140,141,142,143,
76d90 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34 /* 8x */. 14
76da0 34 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 4,145,146,147,14
76db0 38 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 8,149,150,151,15
76dc0 32 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 2,153,154,155,15
76dd0 36 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 6,157,156,159, /
76de0 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c * 9x */. 160,
76df0 31 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 161,162,163,164,
76e00 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 165,166,167,168,
76e10 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 169,170,171,140,
76e20 31 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 141,142,175, /*
76e30 41 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37 Ax */. 176,17
76e40 37 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38 7,178,179,180,18
76e50 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 1,182,183,184,18
76e60 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 5,186,187,188,18
76e70 39 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78 9,190,191, /* Bx
76e80 20 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c */. 192,129,
76e90 31 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 130,131,132,133,
76ea0 31 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 134,135,136,137,
76eb0 32 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 202,203,204,205,
76ec0 32 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a 206,207, /* Cx *
76ed0 2f 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34 /. 208,145,14
76ee0 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 6,147,148,149,15
76ef0 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31 0,151,152,153,21
76f00 38 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 8,219,220,221,22
76f10 32 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 2,223, /* Dx */.
76f20 20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c 224,225,162,
76f30 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 163,164,165,166,
76f40 31 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c 167,168,169,232,
76f50 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 203,204,205,206,
76f60 32 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 207, /* Ex */.
76f70 20 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 239,240,241,24
76f80 32 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 2,243,244,245,24
76f90 36 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31 6,247,248,249,21
76fa0 39 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35 9,220,221,222,25
76fb0 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 5, /* Fx */.#end
76fc0 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 if.};../*.** The
76fd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 32 35 36 20 62 following 256 b
76fe0 79 74 65 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 yte lookup table
76ff0 20 69 73 20 75 73 65 64 20 74 6f 20 73 75 70 70 is used to supp
77000 6f 72 74 20 53 51 4c 69 74 65 73 20 62 75 69 6c ort SQLites buil
77010 74 2d 69 6e 0a 2a 2a 20 65 71 75 69 76 61 6c 65 t-in.** equivale
77020 6e 74 73 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f nts to the follo
77030 77 69 6e 67 20 73 74 61 6e 64 61 72 64 20 6c 69 wing standard li
77040 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 3a brary functions:
77050 0a 2a 2a 0a 2a 2a 20 20 20 69 73 73 70 61 63 65 .**.** isspace
77060 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ()
77070 20 20 20 20 20 20 20 20 20 20 30 78 30 31 0a 2a 0x01.*
77080 2a 20 20 20 69 73 61 6c 70 68 61 28 29 20 20 20 * isalpha()
77090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
770a0 20 20 20 20 20 30 78 30 32 0a 2a 2a 20 20 20 69 0x02.** i
770b0 73 64 69 67 69 74 28 29 20 20 20 20 20 20 20 20 sdigit()
770c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
770d0 30 78 30 34 0a 2a 2a 20 20 20 69 73 61 6c 6e 75 0x04.** isalnu
770e0 6d 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20 m()
770f0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 36 0a 0x06.
77100 2a 2a 20 20 20 69 73 78 64 69 67 69 74 28 29 20 ** isxdigit()
77110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77120 20 20 20 20 20 20 30 78 30 38 0a 2a 2a 20 20 20 0x08.**
77130 74 6f 75 70 70 65 72 28 29 20 20 20 20 20 20 20 toupper()
77140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77150 20 30 78 32 30 0a 2a 2a 20 20 20 53 51 4c 69 74 0x20.** SQLit
77160 65 20 69 64 65 6e 74 69 66 69 65 72 20 63 68 61 e identifier cha
77170 72 61 63 74 65 72 20 20 20 20 20 20 30 78 34 30 racter 0x40
77180 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 32 30 20 .**.** Bit 0x20
77190 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6d 61 is set if the ma
771a0 70 70 65 64 20 63 68 61 72 61 63 74 65 72 20 72 pped character r
771b0 65 71 75 69 72 65 73 20 74 72 61 6e 73 6c 61 74 equires translat
771c0 69 6f 6e 20 74 6f 20 75 70 70 65 72 0a 2a 2a 20 ion to upper.**
771d0 63 61 73 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 case. i.e. if th
771e0 65 20 63 68 61 72 61 63 74 65 72 20 69 73 20 61 e character is a
771f0 20 6c 6f 77 65 72 2d 63 61 73 65 20 41 53 43 49 lower-case ASCI
77200 49 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 I character..**
77210 49 66 20 78 20 69 73 20 61 20 6c 6f 77 65 72 2d If x is a lower-
77220 63 61 73 65 20 41 53 43 49 49 20 63 68 61 72 61 case ASCII chara
77230 63 74 65 72 2c 20 74 68 65 6e 20 69 74 73 20 75 cter, then its u
77240 70 70 65 72 2d 63 61 73 65 20 65 71 75 69 76 61 pper-case equiva
77250 6c 65 6e 74 0a 2a 2a 20 69 73 20 28 78 20 2d 20 lent.** is (x -
77260 30 78 32 30 29 2e 20 54 68 65 72 65 66 6f 72 65 0x20). Therefore
77270 20 74 6f 75 70 70 65 72 28 29 20 63 61 6e 20 62 toupper() can b
77280 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 e implemented as
77290 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78 20 26 20 7e :.**.** (x & ~
772a0 28 6d 61 70 5b 78 5d 26 30 78 32 30 29 29 0a 2a (map[x]&0x20)).*
772b0 2a 0a 2a 2a 20 53 74 61 6e 64 61 72 64 20 66 75 *.** Standard fu
772c0 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77 65 72 28 29 nction tolower()
772d0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 is implemented
772e0 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 using the sqlite
772f0 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 0a 3UpperToLower[].
77300 2a 2a 20 61 72 72 61 79 2e 20 74 6f 6c 6f 77 65 ** array. tolowe
77310 72 28 29 20 69 73 20 75 73 65 64 20 6d 6f 72 65 r() is used more
77320 20 6f 66 74 65 6e 20 74 68 61 6e 20 74 6f 75 70 often than toup
77330 70 65 72 28 29 20 62 79 20 53 51 4c 69 74 65 2e per() by SQLite.
77340 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 78 34 30 20 .**.** Bit 0x40
77350 69 73 20 73 65 74 20 69 66 20 74 68 65 20 63 68 is set if the ch
77360 61 72 61 63 74 65 72 20 6e 6f 6e 2d 61 6c 70 68 aracter non-alph
77370 61 6e 75 6d 65 72 69 63 20 61 6e 64 20 63 61 6e anumeric and can
77380 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20 0a be used in an .
77390 2a 2a 20 53 51 4c 69 74 65 20 69 64 65 6e 74 69 ** SQLite identi
773a0 66 69 65 72 2e 20 20 49 64 65 6e 74 69 66 69 65 fier. Identifie
773b0 72 73 20 61 72 65 20 61 6c 70 68 61 6e 75 6d 65 rs are alphanume
773c0 72 69 63 73 2c 20 22 5f 22 2c 20 22 24 22 2c 20 rics, "_", "$",
773d0 61 6e 64 20 61 6e 79 0a 2a 2a 20 6e 6f 6e 2d 41 and any.** non-A
773e0 53 43 49 49 20 55 54 46 20 63 68 61 72 61 63 74 SCII UTF charact
773f0 65 72 2e 20 48 65 6e 63 65 20 74 68 65 20 74 65 er. Hence the te
77400 73 74 20 66 6f 72 20 77 68 65 74 68 65 72 20 6f st for whether o
77410 72 20 6e 6f 74 20 61 20 63 68 61 72 61 63 74 65 r not a characte
77420 72 20 69 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 r is.** part of
77430 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 69 73 an identifier is
77440 20 30 78 34 36 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 0x46..**.** SQL
77450 69 74 65 27 73 20 76 65 72 73 69 6f 6e 73 20 61 ite's versions a
77460 72 65 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 re identical to
77470 74 68 65 20 73 74 61 6e 64 61 72 64 20 76 65 72 the standard ver
77480 73 69 6f 6e 73 20 61 73 73 75 6d 69 6e 67 20 61 sions assuming a
77490 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f 66 20 22 43 .** locale of "C
774a0 22 2e 20 54 68 65 79 20 61 72 65 20 69 6d 70 6c ". They are impl
774b0 65 6d 65 6e 74 65 64 20 61 73 20 6d 61 63 72 6f emented as macro
774c0 73 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 s in sqliteInt.h
774d0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
774e0 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f TE_ASCII.SQLITE_
774f0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e PRIVATE const un
77500 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 signed char sqli
77510 74 65 33 43 74 79 70 65 4d 61 70 5b 32 35 36 5d te3CtypeMap[256]
77520 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78 30 = {. 0x00, 0x0
77530 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77540 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
77550 20 30 78 30 30 2c 20 20 2f 2a 20 30 30 2e 2e 30 0x00, /* 00..0
77560 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 7 ........ */
77570 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 . 0x00, 0x01, 0
77580 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c x01, 0x01, 0x01,
77590 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78 30 0x01, 0x00, 0x0
775a0 30 2c 20 20 2f 2a 20 30 38 2e 2e 30 66 20 20 20 0, /* 08..0f
775b0 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 ........ */. 0
775c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
775d0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
775e0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 0, 0x00, 0x00,
775f0 2f 2a 20 31 30 2e 2e 31 37 20 20 20 20 2e 2e 2e /* 10..17 ...
77600 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c ..... */. 0x00,
77610 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
77620 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77630 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 31 x00, 0x00, /* 1
77640 38 2e 2e 31 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 8..1f .......
77650 2e 20 2a 2f 0a 20 20 30 78 30 31 2c 20 30 78 30 . */. 0x01, 0x0
77660 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77670 78 34 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x40, 0x00, 0x00,
77680 20 30 78 30 30 2c 20 20 2f 2a 20 32 30 2e 2e 32 0x00, /* 20..2
77690 37 20 20 20 20 20 21 22 23 24 25 26 27 20 2a 2f 7 !"#$%&' */
776a0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 . 0x00, 0x00, 0
776b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c x00, 0x00, 0x00,
776c0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 0x00, 0x00, 0x0
776d0 30 2c 20 20 2f 2a 20 32 38 2e 2e 32 66 20 20 20 0, /* 28..2f
776e0 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f 0a 20 20 30 ()*+,-./ */. 0
776f0 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c x0c, 0x0c, 0x0c,
77700 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 0x0c, 0x0c, 0x0
77710 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 20 c, 0x0c, 0x0c,
77720 2f 2a 20 33 30 2e 2e 33 37 20 20 20 20 30 31 32 /* 30..37 012
77730 33 34 35 36 37 20 2a 2f 0a 20 20 30 78 30 63 2c 34567 */. 0x0c,
77740 20 30 78 30 63 2c 20 30 78 30 30 2c 20 30 78 30 0x0c, 0x00, 0x0
77750 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 0, 0x00, 0x00, 0
77760 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 33 x00, 0x00, /* 3
77770 38 2e 2e 33 66 20 20 20 20 38 39 3a 3b 3c 3d 3e 8..3f 89:;<=>
77780 3f 20 2a 2f 0a 0a 20 20 30 78 30 30 2c 20 30 78 ? */.. 0x00, 0x
77790 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 0a, 0x0a, 0x0a,
777a0 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 0x0a, 0x0a, 0x0a
777b0 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 30 2e 2e , 0x02, /* 40..
777c0 34 37 20 20 20 20 40 41 42 43 44 45 46 47 20 2a 47 @ABCDEFG *
777d0 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 /. 0x02, 0x02,
777e0 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
777f0 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
77800 30 32 2c 20 20 2f 2a 20 34 38 2e 2e 34 66 20 20 02, /* 48..4f
77810 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a 2f 0a 20 20 HIJKLMNO */.
77820 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 0x02, 0x02, 0x02
77830 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
77840 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 02, 0x02, 0x02,
77850 20 2f 2a 20 35 30 2e 2e 35 37 20 20 20 20 50 51 /* 50..57 PQ
77860 52 53 54 55 56 57 20 2a 2f 0a 20 20 30 78 30 32 RSTUVW */. 0x02
77870 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 , 0x02, 0x02, 0x
77880 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
77890 30 78 30 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 0x00, 0x40, /*
778a0 35 38 2e 2e 35 66 20 20 20 20 58 59 5a 5b 5c 5d 58..5f XYZ[\]
778b0 5e 5f 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 ^_ */. 0x00, 0x
778c0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 2a, 0x2a, 0x2a,
778d0 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 0x2a, 0x2a, 0x2a
778e0 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 30 2e 2e , 0x22, /* 60..
778f0 36 37 20 20 20 20 60 61 62 63 64 65 66 67 20 2a 67 `abcdefg *
77900 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20 /. 0x22, 0x22,
77910 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77920 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
77930 32 32 2c 20 20 2f 2a 20 36 38 2e 2e 36 66 20 20 22, /* 68..6f
77940 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a 2f 0a 20 20 hijklmno */.
77950 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 0x22, 0x22, 0x22
77960 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
77970 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 22, 0x22, 0x22,
77980 20 2f 2a 20 37 30 2e 2e 37 37 20 20 20 20 70 71 /* 70..77 pq
77990 72 73 74 75 76 77 20 2a 2f 0a 20 20 30 78 32 32 rstuvw */. 0x22
779a0 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 , 0x22, 0x22, 0x
779b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 00, 0x00, 0x00,
779c0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 0x00, 0x00, /*
779d0 37 38 2e 2e 37 66 20 20 20 20 78 79 7a 7b 7c 7d 78..7f xyz{|}
779e0 7e 2e 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 30 ~. */.. 0x40, 0
779f0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77a00 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77a10 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 38 30 2e 0, 0x40, /* 80.
77a20 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .87 ........
77a30 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c */. 0x40, 0x40,
77a40 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77a50 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77a60 78 34 30 2c 20 20 2f 2a 20 38 38 2e 2e 38 66 20 x40, /* 88..8f
77a70 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
77a80 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77a90 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77aa0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77ab0 20 20 2f 2a 20 39 30 2e 2e 39 37 20 20 20 20 2e /* 90..97 .
77ac0 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 ....... */. 0x4
77ad0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77ae0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77af0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 0x40, 0x40, /*
77b00 20 39 38 2e 2e 39 66 20 20 20 20 2e 2e 2e 2e 2e 98..9f .....
77b10 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 ... */. 0x40, 0
77b20 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77b30 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77b40 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 61 30 2e 0, 0x40, /* a0.
77b50 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 .a7 ........
77b60 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c */. 0x40, 0x40,
77b70 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77b80 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77b90 78 34 30 2c 20 20 2f 2a 20 61 38 2e 2e 61 66 20 x40, /* a8..af
77ba0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 ........ */.
77bb0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 0x40, 0x40, 0x4
77bc0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77bd0 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77be0 20 20 2f 2a 20 62 30 2e 2e 62 37 20 20 20 20 2e /* b0..b7 .
77bf0 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 ....... */. 0x4
77c00 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 0, 0x40, 0x40, 0
77c10 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c x40, 0x40, 0x40,
77c20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 0x40, 0x40, /*
77c30 20 62 38 2e 2e 62 66 20 20 20 20 2e 2e 2e 2e 2e b8..bf .....
77c40 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78 34 30 2c 20 ... */.. 0x40,
77c50 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77c60 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77c70 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 63 30 40, 0x40, /* c0
77c80 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..c7 ........
77c90 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 */. 0x40, 0x40
77ca0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77cb0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77cc0 30 78 34 30 2c 20 20 2f 2a 20 63 38 2e 2e 63 66 0x40, /* c8..cf
77cd0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
77ce0 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 0x40, 0x40, 0x
77cf0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77d00 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77d10 2c 20 20 2f 2a 20 64 30 2e 2e 64 37 20 20 20 20 , /* d0..d7
77d20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
77d30 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77d40 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77d50 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f , 0x40, 0x40, /
77d60 2a 20 64 38 2e 2e 64 66 20 20 20 20 2e 2e 2e 2e * d8..df ....
77d70 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 .... */. 0x40,
77d80 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77d90 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77da0 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 30 40, 0x40, /* e0
77db0 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e ..e7 ........
77dc0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 */. 0x40, 0x40
77dd0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 , 0x40, 0x40, 0x
77de0 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77df0 30 78 34 30 2c 20 20 2f 2a 20 65 38 2e 2e 65 66 0x40, /* e8..ef
77e00 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a ........ */.
77e10 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 0x40, 0x40, 0x
77e20 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77e30 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77e40 2c 20 20 2f 2a 20 66 30 2e 2e 66 37 20 20 20 20 , /* f0..f7
77e50 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 ........ */. 0x
77e60 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 40, 0x40, 0x40,
77e70 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 0x40, 0x40, 0x40
77e80 2c 20 30 78 34 30 2c 20 30 78 34 30 20 20 20 2f , 0x40, 0x40 /
77e90 2a 20 66 38 2e 2e 66 66 20 20 20 20 2e 2e 2e 2e * f8..ff ....
77ea0 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 .... */.};.#endi
77eb0 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 f..../*.** The f
77ec0 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65 74 ollowing singlet
77ed0 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 on contains the
77ee0 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 global configura
77ef0 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65 20 tion for.** the
77f00 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a SQLite library..
77f10 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
77f20 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 E SQLITE_WSD str
77f30 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 uct Sqlite3Confi
77f40 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 g sqlite3Config
77f50 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44 45 = {. SQLITE_DE
77f60 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53 2c FAULT_MEMSTATUS,
77f70 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a 2f /* bMemstat */
77f80 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 . 1,
77f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
77fa0 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f 0a * bCoreMutex */.
77fb0 20 20 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 SQLITE_THREAD
77fc0 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f 2a SAFE==1, /*
77fd0 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a 20 bFullMutex */.
77fe0 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 20 20 0x7ffffffe,
77ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
78000 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20 31 mxStrlen */. 1
78010 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 00,
78020 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 4c /* szL
78030 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 35 ookaside */. 5
78040 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 00,
78050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c 6f /* nLo
78060 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 7b 30 okaside */. {0
78070 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c ,0,0,0,0,0,0,0},
78080 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 20 2a 2f /* m */
78090 0a 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 . {0,0,0,0,0,0
780a0 2c 30 2c 30 2c 30 7d 2c 20 20 20 20 20 20 20 2f ,0,0,0}, /
780b0 2a 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 7b 30 * mutex */. {0
780c0 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 ,0,0,0,0,0,0,0,0
780d0 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20 70 63 61 63 ,0,0}, /* pcac
780e0 68 65 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 he */. (void*)
780f0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78100 20 20 20 20 2f 2a 20 70 48 65 61 70 20 2a 2f 0a /* pHeap */.
78110 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 0,
78120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
78130 20 6e 48 65 61 70 20 2a 2f 0a 20 20 20 30 2c 20 nHeap */. 0,
78140 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
78150 20 20 20 20 20 20 20 20 2f 2a 20 6d 6e 48 65 61 /* mnHea
78160 70 2c 20 6d 78 48 65 61 70 20 2a 2f 0a 20 20 20 p, mxHeap */.
78170 28 76 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 (void*)0,
78180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 53 /* pS
78190 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 cratch */. 0,
781a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
781b0 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 53 63 72 /* szScr
781c0 61 74 63 68 20 2a 2f 0a 20 20 20 30 2c 20 20 20 atch */. 0,
781d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
781e0 20 20 20 20 20 20 2f 2a 20 6e 53 63 72 61 74 63 /* nScratc
781f0 68 20 2a 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 h */. (void*)0
78200 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
78210 20 20 20 2f 2a 20 70 50 61 67 65 20 2a 2f 0a 20 /* pPage */.
78220 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 0,
78230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
78240 73 7a 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 szPage */. 0,
78250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78260 20 20 20 20 20 20 20 20 2f 2a 20 6e 50 61 67 65 /* nPage
78270 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
78280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78290 20 20 2f 2a 20 6d 78 50 61 72 73 65 72 53 74 61 /* mxParserSta
782a0 63 6b 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 ck */. 0,
782b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
782c0 20 20 20 20 2f 2a 20 73 68 61 72 65 64 43 61 63 /* sharedCac
782d0 68 65 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20 heEnabled */.
782e0 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 74 20 /* All the rest
782f0 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 should always be
78300 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 initialized to
78310 7a 65 72 6f 20 2a 2f 0a 20 20 20 30 2c 20 20 20 zero */. 0,
78320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78330 20 20 20 20 20 20 2f 2a 20 69 73 49 6e 69 74 20 /* isInit
78340 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
78350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78360 20 2f 2a 20 69 6e 50 72 6f 67 72 65 73 73 20 2a /* inProgress *
78370 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
78380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78390 2f 2a 20 69 73 4d 75 74 65 78 49 6e 69 74 20 2a /* isMutexInit *
783a0 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 /. 0,
783b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
783c0 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 /* isMallocInit
783d0 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
783e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
783f0 20 2f 2a 20 69 73 50 43 61 63 68 65 49 6e 69 74 /* isPCacheInit
78400 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 */. 0,
78410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78420 20 20 2f 2a 20 70 49 6e 69 74 4d 75 74 65 78 20 /* pInitMutex
78430 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20 */. 0,
78440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
78450 20 2f 2a 20 6e 52 65 66 49 6e 69 74 4d 75 74 65 /* nRefInitMute
78460 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 x */.};.../*.**
78470 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 67 Hash table for g
78480 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 73 20 lobal functions
78490 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6d 6d - functions comm
784a0 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 61 74 on to all.** dat
784b0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
784c0 73 2e 20 20 41 66 74 65 72 20 69 6e 69 74 69 61 s. After initia
784d0 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 73 20 74 lization, this t
784e0 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 61 64 2d able is.** read-
784f0 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f only..*/.SQLITE_
78500 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 PRIVATE SQLITE_W
78510 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73 SD FuncDefHash s
78520 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 qlite3GlobalFunc
78530 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 tions;../*.** Th
78540 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 22 e value of the "
78550 70 65 6e 64 69 6e 67 22 20 62 79 74 65 20 6d 75 pending" byte mu
78560 73 74 20 62 65 20 30 78 34 30 30 30 30 30 30 30 st be 0x40000000
78570 20 28 31 20 62 79 74 65 20 70 61 73 74 20 74 68 (1 byte past th
78580 65 0a 2a 2a 20 31 2d 67 69 62 61 62 79 74 65 20 e.** 1-gibabyte
78590 62 6f 75 6e 64 61 72 79 29 20 69 6e 20 61 20 63 boundary) in a c
785a0 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61 62 61 ompatible databa
785b0 73 65 2e 20 20 53 51 4c 69 74 65 20 6e 65 76 65 se. SQLite neve
785c0 72 20 75 73 65 73 0a 2a 2a 20 74 68 65 20 64 61 r uses.** the da
785d0 74 61 62 61 73 65 20 70 61 67 65 20 74 68 61 74 tabase page that
785e0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 65 contains the pe
785f0 6e 64 69 6e 67 20 62 79 74 65 2e 20 20 49 74 20 nding byte. It
78600 6e 65 76 65 72 20 61 74 74 65 6d 70 74 73 0a 2a never attempts.*
78610 2a 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 * to read or wri
78620 74 65 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 te that page. T
78630 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 he pending byte
78640 70 61 67 65 20 69 73 20 73 65 74 20 61 73 73 69 page is set assi
78650 67 6e 0a 2a 2a 20 66 6f 72 20 75 73 65 20 62 79 gn.** for use by
78660 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 73 20 the VFS layers
78670 61 73 20 73 70 61 63 65 20 66 6f 72 20 6d 61 6e as space for man
78680 61 67 69 6e 67 20 66 69 6c 65 20 6c 6f 63 6b 73 aging file locks
78690 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 ..**.** During t
786a0 65 73 74 69 6e 67 2c 20 69 74 20 69 73 20 6f 66 esting, it is of
786b0 74 65 6e 20 64 65 73 69 72 61 62 6c 65 20 74 6f ten desirable to
786c0 20 6d 6f 76 65 20 74 68 65 20 70 65 6e 64 69 6e move the pendin
786d0 67 20 62 79 74 65 20 74 6f 0a 2a 2a 20 61 20 64 g byte to.** a d
786e0 69 66 66 65 72 65 6e 74 20 70 6f 73 69 74 69 6f ifferent positio
786f0 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 n in the file.
78700 54 68 69 73 20 61 6c 6c 6f 77 73 20 63 6f 64 65 This allows code
78710 20 74 68 61 74 20 68 61 73 20 74 6f 0a 2a 2a 20 that has to.**
78720 64 65 61 6c 20 77 69 74 68 20 74 68 65 20 70 65 deal with the pe
78730 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 72 75 nding byte to ru
78740 6e 20 6f 6e 20 66 69 6c 65 73 20 74 68 61 74 20 n on files that
78750 61 72 65 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 are much smaller
78760 0a 2a 2a 20 74 68 61 6e 20 31 20 47 69 42 2e 20 .** than 1 GiB.
78770 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 The sqlite3_tes
78780 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 t_control() inte
78790 72 66 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 rface can be use
787a0 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65 20 74 68 65 d to.** move the
787b0 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 0a 2a pending byte..*
787c0 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41 4e 54 3a 20 *.** IMPORTANT:
787d0 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 70 65 Changing the pe
787e0 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 20 61 6e nding byte to an
787f0 79 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 y value other th
78800 61 6e 0a 2a 2a 20 30 78 34 30 30 30 30 30 30 30 an.** 0x40000000
78810 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 69 results in an i
78820 6e 63 6f 6d 70 61 74 69 62 6c 65 20 64 61 74 61 ncompatible data
78830 62 61 73 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 base file format
78840 21 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 !.** Changing th
78850 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 64 e pending byte d
78860 75 72 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 uring operating
78870 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 results in undef
78880 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 64 69 6c 65 ined.** and dile
78890 74 65 72 69 6f 75 73 20 62 65 68 61 76 69 6f 72 terious behavior
788a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
788b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
788c0 65 6e 64 69 6e 67 42 79 74 65 20 3d 20 30 78 34 endingByte = 0x4
788d0 30 30 30 30 30 30 30 3b 0a 0a 2f 2a 0a 2a 2a 20 0000000;../*.**
788e0 50 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 Properties of op
788f0 63 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c codes. The OPFL
78900 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 G_INITIALIZER ma
78910 63 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 cro is.** create
78920 64 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 d by mkopcodeh.a
78930 77 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c wk during compil
78940 61 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 ation. Data is
78950 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d obtained.** from
78960 20 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f the comments fo
78970 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 llowing the "cas
78980 65 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 e OP_xxxx:" stat
78990 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 65 ements in.** the
789a0 20 76 64 62 65 2e 63 20 66 69 6c 65 2e 20 20 0a vdbe.c file. .
789b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
789c0 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 E const unsigned
789d0 20 63 68 61 72 20 73 71 6c 69 74 65 33 4f 70 63 char sqlite3Opc
789e0 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d 20 odeProperty[] =
789f0 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 OPFLG_INITIALIZE
78a00 52 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a R;../***********
78a10 2a 2a 2a 20 45 6e 64 20 6f 66 20 67 6c 6f 62 61 *** End of globa
78a20 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a l.c ************
78a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78a50 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
78a60 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 *** Begin file s
78a70 74 61 74 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a tatus.c ********
78a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78aa0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a **/./*.** 2008 J
78ab0 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 une 18.**.** The
78ac0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
78ad0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
78ae0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
78af0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
78b00 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
78b10 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
78b20 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
78b30 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
78b40 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
78b50 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
78b60 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
78b70 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
78b80 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
78b90 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
78ba0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
78bb0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
78bc0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
78bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
78c10 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
78c20 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e module implemen
78c30 74 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 ts the sqlite3_s
78c40 74 61 74 75 73 28 29 20 69 6e 74 65 72 66 61 63 tatus() interfac
78c50 65 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a e and related.**
78c60 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e 0a functionality..
78c70 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 */../*.** Variab
78c80 6c 65 73 20 69 6e 20 77 68 69 63 68 20 74 6f 20 les in which to
78c90 72 65 63 6f 72 64 20 73 74 61 74 75 73 20 69 6e record status in
78ca0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 formation..*/.ty
78cb0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c pedef struct sql
78cc0 69 74 65 33 53 74 61 74 54 79 70 65 20 73 71 6c ite3StatType sql
78cd0 69 74 65 33 53 74 61 74 54 79 70 65 3b 0a 73 74 ite3StatType;.st
78ce0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 atic SQLITE_WSD
78cf0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 53 74 struct sqlite3St
78d00 61 74 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e atType {. int n
78d10 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 owValue[9];
78d20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 /* Current v
78d30 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 alue */. int mx
78d40 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 Value[9];
78d50 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 /* Maximum va
78d60 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 lue */.} sqlite3
78d70 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b Stat = { {0,}, {
78d80 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 0,} };.../* The
78d90 22 77 73 64 53 74 61 74 22 20 6d 61 63 72 6f 20 "wsdStat" macro
78da0 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 will resolve to
78db0 74 68 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 the status infor
78dc0 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 mation.** state
78dd0 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 74 vector. If writ
78de0 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 able static data
78df0 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 is unsupported
78e00 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a on the target,.*
78e10 2a 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 * we have to loc
78e20 61 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 ate the state ve
78e30 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 ctor at run-time
78e40 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 . In the more c
78e50 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 ommon.** case wh
78e60 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 ere writable sta
78e70 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70 70 tic data is supp
78e80 6f 72 74 65 64 2c 20 77 73 64 53 74 61 74 20 63 orted, wsdStat c
78e90 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c an refer directl
78ea0 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c y.** to the "sql
78eb0 69 74 65 33 53 74 61 74 22 20 73 74 61 74 65 20 ite3Stat" state
78ec0 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 vector declared
78ed0 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 above..*/.#ifdef
78ee0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 SQLITE_OMIT_WSD
78ef0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
78f00 74 49 6e 69 74 20 20 73 71 6c 69 74 65 33 53 74 tInit sqlite3St
78f10 61 74 54 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f atType *x = &GLO
78f20 42 41 4c 28 73 71 6c 69 74 65 33 53 74 61 74 54 BAL(sqlite3StatT
78f30 79 70 65 2c 73 71 6c 69 74 65 33 53 74 61 74 29 ype,sqlite3Stat)
78f40 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 .# define wsdSta
78f50 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 t x[0].#else.# d
78f60 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e 69 efine wsdStatIni
78f70 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 t.# define wsdSt
78f80 61 74 20 73 71 6c 69 74 65 33 53 74 61 74 0a 23 at sqlite3Stat.#
78f90 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 endif../*.** Ret
78fa0 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 urn the current
78fb0 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 value of a statu
78fc0 73 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a s parameter..*/.
78fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
78fe0 6e 74 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 nt sqlite3Status
78ff0 56 61 6c 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 Value(int op){.
79000 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 wsdStatInit;.
79010 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 assert( op>=0 &&
79020 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 op<ArraySize(ws
79030 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 dStat.nowValue)
79040 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 53 );. return wsdS
79050 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
79060 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e ;.}../*.** Add N
79070 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
79080 20 61 20 73 74 61 74 75 73 20 72 65 63 6f 72 64 a status record
79090 2e 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 . It is assumed
790a0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c that the.** cal
790b0 6c 65 72 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 ler holds approp
790c0 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a riate locks..*/.
790d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
790e0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 74 75 oid sqlite3Statu
790f0 73 41 64 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 sAdd(int op, int
79100 20 4e 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e N){. wsdStatIn
79110 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 it;. assert( op
79120 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 >=0 && op<ArrayS
79130 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 ize(wsdStat.nowV
79140 61 6c 75 65 29 20 29 3b 0a 20 20 77 73 64 53 74 alue) );. wsdSt
79150 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 at.nowValue[op]
79160 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 += N;. if( wsdS
79170 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d tat.nowValue[op]
79180 3e 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 >wsdStat.mxValue
79190 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 [op] ){. wsdS
791a0 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 tat.mxValue[op]
791b0 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c = wsdStat.nowVal
791c0 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f ue[op];. }.}../
791d0 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61 6c *.** Set the val
791e0 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 74 ue of a status t
791f0 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 o X..*/.SQLITE_P
79200 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
79210 74 65 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 te3StatusSet(int
79220 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 op, int X){. w
79230 73 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 sdStatInit;. as
79240 73 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f sert( op>=0 && o
79250 70 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 p<ArraySize(wsdS
79260 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b tat.nowValue) );
79270 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 . wsdStat.nowVa
79280 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 lue[op] = X;. i
79290 66 28 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 f( wsdStat.nowVa
792a0 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e lue[op]>wsdStat.
792b0 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 mxValue[op] ){.
792c0 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c wsdStat.mxVal
792d0 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 ue[op] = wsdStat
792e0 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 .nowValue[op];.
792f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 }.}../*.** Quer
79300 79 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 y status informa
79310 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tion..**.** This
79320 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
79330 61 73 73 75 6d 65 73 20 74 68 61 74 20 72 65 61 assumes that rea
79340 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 ding or writing
79350 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 an aligned.** 32
79360 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 -bit integer is
79370 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 an atomic operat
79380 69 6f 6e 2e 20 20 49 66 20 74 68 61 74 20 61 73 ion. If that as
79390 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 sumption is not
793a0 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 true,.** then th
793b0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f is routine is no
793c0 74 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f t threadsafe..*/
793d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
793e0 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69 sqlite3_status(i
793f0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 nt op, int *pCur
79400 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 rent, int *pHigh
79410 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74 water, int reset
79420 46 6c 61 67 29 7b 0a 20 20 77 73 64 53 74 61 74 Flag){. wsdStat
79430 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 Init;. if( op<0
79440 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a || op>=ArraySiz
79450 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c e(wsdStat.nowVal
79460 75 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ue) ){. retur
79470 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b n SQLITE_MISUSE;
79480 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 . }. *pCurrent
79490 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 = wsdStat.nowVa
794a0 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 lue[op];. *pHig
794b0 68 77 61 74 65 72 20 3d 20 77 73 64 53 74 61 74 hwater = wsdStat
794c0 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 .mxValue[op];.
794d0 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 7b if( resetFlag ){
794e0 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78 56 . wsdStat.mxV
794f0 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 alue[op] = wsdSt
79500 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b at.nowValue[op];
79510 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
79520 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
79530 2a 20 51 75 65 72 79 20 73 74 61 74 75 73 20 69 * Query status i
79540 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 nformation for a
79550 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
79560 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 connection.*/.S
79570 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
79580 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 lite3_db_status(
79590 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 . sqlite3 *db,
795a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
795b0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
795c0 69 6f 6e 20 77 68 6f 73 65 20 73 74 61 74 75 73 ion whose status
795d0 20 69 73 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 is desired */.
795e0 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 int op,
795f0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 /* Status
79600 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a verb */. int *
79610 70 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 20 pCurrent,
79620 20 2f 2a 20 57 72 69 74 65 20 63 75 72 72 65 6e /* Write curren
79630 74 20 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a t value here */.
79640 20 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65 int *pHighwate
79650 72 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 r, /* Write
79660 20 68 69 67 68 2d 77 61 74 65 72 20 6d 61 72 6b high-water mark
79670 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 here */. int r
79680 65 73 65 74 46 6c 61 67 20 20 20 20 20 20 20 20 esetFlag
79690 20 2f 2a 20 52 65 73 65 74 20 68 69 67 68 2d 77 /* Reset high-w
796a0 61 74 65 72 20 6d 61 72 6b 20 69 66 20 74 72 75 ater mark if tru
796b0 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 e */.){. switch
796c0 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 ( op ){. case
796d0 20 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 SQLITE_DBSTATUS
796e0 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a _LOOKASIDE_USED:
796f0 20 7b 0a 20 20 20 20 20 20 2a 70 43 75 72 72 65 {. *pCurre
79700 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 nt = db->lookasi
79710 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a de.nOut;. *
79720 70 48 69 67 68 77 61 74 65 72 20 3d 20 64 62 2d pHighwater = db-
79730 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 >lookaside.mxOut
79740 3b 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 65 ;. if( rese
79750 74 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 tFlag ){.
79760 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d db->lookaside.m
79770 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 xOut = db->looka
79780 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 side.nOut;.
79790 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
797a0 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
797b0 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 t: {. retur
797c0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a n SQLITE_ERROR;.
797d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
797e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
797f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
79800 20 45 6e 64 20 6f 66 20 73 74 61 74 75 73 2e 63 End of status.c
79810 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
79820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
79840 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
79850 20 42 65 67 69 6e 20 66 69 6c 65 20 64 61 74 65 Begin file date
79860 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
79870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
79890 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f ./*.** 2003 Octo
798a0 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 ber 31.**.** The
798b0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
798c0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
798d0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
798e0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
798f0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
79900 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
79910 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
79920 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
79930 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
79940 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
79950 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
79960 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
79970 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
79980 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
79990 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
799a0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
799b0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
799c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
799d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
799e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
799f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
79a00 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
79a10 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 le contains the
79a20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 C functions that
79a30 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 implement date
79a40 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 and time.** func
79a50 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 tions for SQLite
79a60 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 . .**.** There
79a70 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f is only one expo
79a80 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 rted symbol in t
79a90 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20 66 his file - the f
79aa0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 unction.** sqlit
79ab0 65 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 e3RegisterDateTi
79ac0 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f meFunctions() fo
79ad0 75 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f und at the botto
79ae0 6d 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a m of the file..*
79af0 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 * All other code
79b00 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e has file scope.
79b10 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 .**.** SQLite pr
79b20 6f 63 65 73 73 65 73 20 61 6c 6c 20 74 69 6d 65 ocesses all time
79b30 73 20 61 6e 64 20 64 61 74 65 73 20 61 73 20 4a s and dates as J
79b40 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 ulian Day number
79b50 73 2e 20 20 54 68 65 0a 2a 2a 20 64 61 74 65 73 s. The.** dates
79b60 20 61 6e 64 20 74 69 6d 65 73 20 61 72 65 20 73 and times are s
79b70 74 6f 72 65 64 20 61 73 20 74 68 65 20 6e 75 6d tored as the num
79b80 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 ber of days sinc
79b90 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 e noon.** in Gre
79ba0 65 6e 77 69 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 enwich on Novemb
79bb0 65 72 20 32 34 2c 20 34 37 31 34 20 42 2e 43 2e er 24, 4714 B.C.
79bc0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
79bd0 65 20 47 72 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 e Gregorian.** c
79be0 61 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 2e 20 alendar system.
79bf0 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d 30 31 2d 30 .**.** 1970-01-0
79c00 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20 4a 44 1 00:00:00 is JD
79c10 20 32 34 34 30 35 38 37 2e 35 0a 2a 2a 20 32 30 2440587.5.** 20
79c20 30 30 2d 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 00-01-01 00:00:0
79c30 30 20 69 73 20 4a 44 20 32 34 35 31 35 34 34 2e 0 is JD 2451544.
79c40 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 5.**.** This imp
79c50 6c 65 6d 65 6e 74 69 6f 6e 20 72 65 71 75 69 72 lemention requir
79c60 65 73 20 79 65 61 72 73 20 74 6f 20 62 65 20 65 es years to be e
79c70 78 70 72 65 73 73 65 64 20 61 73 20 61 20 34 2d xpressed as a 4-
79c80 64 69 67 69 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 digit number.**
79c90 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 which means that
79ca0 20 6f 6e 6c 79 20 64 61 74 65 73 20 62 65 74 77 only dates betw
79cb0 65 65 6e 20 30 30 30 30 2d 30 31 2d 30 31 20 61 een 0000-01-01 a
79cc0 6e 64 20 39 39 39 39 2d 31 32 2d 33 31 20 63 61 nd 9999-12-31 ca
79cd0 6e 0a 2a 2a 20 62 65 20 72 65 70 72 65 73 65 6e n.** be represen
79ce0 74 65 64 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 ted, even though
79cf0 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 julian day numb
79d00 65 72 73 20 61 6c 6c 6f 77 20 61 20 6d 75 63 68 ers allow a much
79d10 20 77 69 64 65 72 0a 2a 2a 20 72 61 6e 67 65 20 wider.** range
79d20 6f 66 20 64 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 of dates..**.**
79d30 54 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 The Gregorian ca
79d40 6c 65 6e 64 61 72 20 73 79 73 74 65 6d 20 69 73 lendar system is
79d50 20 75 73 65 64 20 66 6f 72 20 61 6c 6c 20 64 61 used for all da
79d60 74 65 73 20 61 6e 64 20 74 69 6d 65 73 2c 0a 2a tes and times,.*
79d70 2a 20 65 76 65 6e 20 74 68 6f 73 65 20 74 68 61 * even those tha
79d80 74 20 70 72 65 64 61 74 65 20 74 68 65 20 47 72 t predate the Gr
79d90 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 egorian calendar
79da0 2e 20 20 48 69 73 74 6f 72 69 61 6e 73 20 75 73 . Historians us
79db0 75 61 6c 6c 79 0a 2a 2a 20 75 73 65 20 74 68 65 ually.** use the
79dc0 20 4a 75 6c 69 61 6e 20 63 61 6c 65 6e 64 61 72 Julian calendar
79dd0 20 66 6f 72 20 64 61 74 65 73 20 70 72 69 6f 72 for dates prior
79de0 20 74 6f 20 31 35 38 32 2d 31 30 2d 31 35 20 61 to 1582-10-15 a
79df0 6e 64 20 66 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 nd for some.** d
79e00 61 74 65 73 20 61 66 74 65 72 77 61 72 64 73 2c ates afterwards,
79e10 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f depending on lo
79e20 63 61 6c 65 2e 20 20 42 65 77 61 72 65 20 6f 66 cale. Beware of
79e30 20 74 68 69 73 20 64 69 66 66 65 72 65 6e 63 65 this difference
79e40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 ..**.** The conv
79e50 65 72 73 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d ersion algorithm
79e60 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 s are implemente
79e70 64 20 62 61 73 65 64 20 6f 6e 20 64 65 73 63 72 d based on descr
79e80 69 70 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 iptions.** in th
79e90 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 78 74 e following text
79ea0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 4a 65 61 :.**.** Jea
79eb0 6e 20 4d 65 65 75 73 0a 2a 2a 20 20 20 20 20 20 n Meeus.**
79ec0 41 73 74 72 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 Astronomical Alg
79ed0 6f 72 69 74 68 6d 73 2c 20 32 6e 64 20 45 64 69 orithms, 2nd Edi
79ee0 74 69 6f 6e 2c 20 31 39 39 38 0a 2a 2a 20 20 20 tion, 1998.**
79ef0 20 20 20 49 53 42 4d 20 30 2d 39 34 33 33 39 36 ISBM 0-943396
79f00 2d 36 31 2d 31 0a 2a 2a 20 20 20 20 20 20 57 69 -61-1.** Wi
79f10 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 llmann-Bell, Inc
79f20 0a 2a 2a 20 20 20 20 20 20 52 69 63 68 6d 6f 6e .** Richmon
79f30 64 2c 20 56 69 72 67 69 6e 69 61 20 28 55 53 41 d, Virginia (USA
79f40 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 74 ).*/.#include <t
79f50 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 ime.h>..#ifndef
79f60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
79f70 54 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a TIME_FUNCS../*.*
79f80 2a 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 * On recent Wind
79f90 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 ows platforms, t
79fa0 68 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 he localtime_s()
79fb0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 function is ava
79fc0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 ilable.** as par
79fd0 74 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 t of the "Secure
79fe0 20 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 CRT". It is ess
79ff0 65 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c entially equival
7a000 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c ent to .** local
7a010 74 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 time_r() availab
7a020 6c 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f le under most PO
7a030 53 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 SIX platforms, e
7a040 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a xcept that the .
7a050 2a 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 ** order of the
7a060 70 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 parameters is re
7a070 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 versed..**.** Se
7a080 65 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 e http://msdn.mi
7a090 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 crosoft.com/en-u
7a0a0 73 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 s/library/a442x3
7a0b0 79 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a ye(VS.80).aspx..
7a0c0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 **.** If the use
7a0d0 72 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 r has not indica
7a0e0 74 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c ted to use local
7a0f0 74 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 time_r() or loca
7a100 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 ltime_s().** alr
7a110 65 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 eady, check for
7a120 61 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e an MSVC build en
7a130 76 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 vironment that p
7a140 72 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 rovides .** loca
7a150 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 ltime_s()..*/.#i
7a160 66 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f f !defined(HAVE_
7a170 4c 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 LOCALTIME_R) &&
7a180 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f !defined(HAVE_LO
7a190 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a CALTIME_S) && \.
7a1a0 20 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 defined(_MS
7a1b0 43 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 C_VER) && define
7a1c0 64 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f d(_CRT_INSECURE_
7a1d0 44 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 DEPRECATE).#defi
7a1e0 6e 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d ne HAVE_LOCALTIM
7a1f0 45 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a E_S 1.#endif../*
7a200 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 .** A structure
7a210 66 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 for holding a si
7a220 6e 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 ngle date and ti
7a230 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 me..*/.typedef s
7a240 74 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 truct DateTime D
7a250 61 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 ateTime;.struct
7a260 44 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c DateTime {. sql
7a270 69 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 ite3_int64 iJD;
7a280 2f 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 /* The julian da
7a290 79 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 y number times 8
7a2a0 36 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 6400000 */. int
7a2b0 20 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 Y, M, D;
7a2c0 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 /* Year, month,
7a2d0 61 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 and day */. int
7a2e0 20 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 h, m;
7a2f0 2f 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 /* Hour and minu
7a300 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b tes */. int tz;
7a310 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
7a320 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 imezone offset i
7a330 6e 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 n minutes */. d
7a340 6f 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 ouble s;
7a350 20 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a /* Seconds */.
7a360 20 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b char validYMD;
7a370 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 /* True (1)
7a380 20 69 66 20 59 2c 4d 2c 44 20 61 72 65 20 76 61 if Y,M,D are va
7a390 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 lid */. char va
7a3a0 6c 69 64 48 4d 53 3b 20 20 20 20 20 2f 2a 20 54 lidHMS; /* T
7a3b0 72 75 65 20 28 31 29 20 69 66 20 68 2c 6d 2c 73 rue (1) if h,m,s
7a3c0 20 61 72 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 are valid */.
7a3d0 63 68 61 72 20 76 61 6c 69 64 4a 44 3b 20 20 20 char validJD;
7a3e0 20 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 /* True (1) i
7a3f0 66 20 69 4a 44 20 69 73 20 76 61 6c 69 64 20 2a f iJD is valid *
7a400 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 54 5a /. char validTZ
7a410 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28 ; /* True (
7a420 31 29 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 1) if tz is vali
7a430 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 d */.};.../*.**
7a440 43 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e Convert zDate in
7a450 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 to one or more i
7a460 6e 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 ntegers. Additi
7a470 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a onal arguments.*
7a480 2a 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 * come in groups
7a490 20 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 of 5 as follows
7a4a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 :.**.** N
7a4b0 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 number of
7a4c0 64 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e digits in the in
7a4d0 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d teger.** m
7a4e0 69 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 in minimum a
7a4f0 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 llowed value of
7a500 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 the integer.**
7a510 20 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 max max
7a520 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c imum allowed val
7a530 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 ue of the intege
7a540 72 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 r.** nextC
7a550 20 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74 first charact
7a560 65 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 er after the int
7a570 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 eger.** pV
7a580 61 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 al where to w
7a590 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 rite the integer
7a5a0 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 s value..**.** C
7a5b0 6f 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 onversions conti
7a5c0 6e 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 nue until one wi
7a5d0 74 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 th nextC==0 is e
7a5e0 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 ncountered..** T
7a5f0 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 he function retu
7a600 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f rns the number o
7a610 66 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e f successful con
7a620 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 versions..*/.sta
7a630 74 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 tic int getDigit
7a640 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 s(const char *zD
7a650 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f ate, ...){. va_
7a660 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 list ap;. int v
7a670 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 al;. int N;. i
7a680 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 nt min;. int ma
7a690 78 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a x;. int nextC;.
7a6a0 20 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 int *pVal;. i
7a6b0 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 nt cnt = 0;. va
7a6c0 5f 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 _start(ap, zDate
7a6d0 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d );. do{. N =
7a6e0 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 va_arg(ap, int)
7a6f0 3b 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 ;. min = va_a
7a700 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 rg(ap, int);.
7a710 20 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 max = va_arg(ap
7a720 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 , int);. next
7a730 43 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 C = va_arg(ap, i
7a740 6e 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 nt);. pVal =
7a750 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 va_arg(ap, int*)
7a760 3b 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 ;. val = 0;.
7a770 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b while( N-- ){
7a780 0a 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 . if( !sqli
7a790 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 74 te3Isdigit(*zDat
7a7a0 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f e) ){. go
7a7b0 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69 74 73 to end_getDigits
7a7c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7a7d0 76 61 6c 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a val = val*10 + *
7a7e0 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 zDate - '0';.
7a7f0 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 zDate++;.
7a800 7d 0a 20 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 }. if( val<mi
7a810 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 n || val>max ||
7a820 28 6e 65 78 74 43 21 3d 30 20 26 26 20 6e 65 78 (nextC!=0 && nex
7a830 74 43 21 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 tC!=*zDate) ){.
7a840 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 goto end_ge
7a850 74 44 69 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 tDigits;. }.
7a860 20 20 20 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a *pVal = val;.
7a870 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
7a880 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 cnt++;. }while
7a890 28 20 6e 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 ( nextC );.end_g
7a8a0 65 74 44 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 etDigits:. va_e
7a8b0 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e nd(ap);. return
7a8c0 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 cnt;.}../*.** R
7a8d0 65 61 64 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b ead text from z[
7a8e0 5d 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e ] and convert in
7a8f0 74 6f 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f to a floating po
7a900 69 6e 74 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 int number. Ret
7a910 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 urn.** the numbe
7a920 72 20 6f 66 20 64 69 67 69 74 73 20 63 6f 6e 76 r of digits conv
7a930 65 72 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e erted..*/.#defin
7a940 65 20 67 65 74 56 61 6c 75 65 20 73 71 6c 69 74 e getValue sqlit
7a950 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 e3AtoF../*.** Pa
7a960 72 73 65 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 rse a timezone e
7a970 78 74 65 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 xtension on the
7a980 65 6e 64 20 6f 66 20 61 20 64 61 74 65 2d 74 69 end of a date-ti
7a990 6d 65 2e 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e me..** The exten
7a9a0 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 sion is of the f
7a9b0 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 orm:.**.**
7a9c0 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a (+/-)HH:MM.**.
7a9d0 2a 2a 20 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 ** Or the "zulu"
7a9e0 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a notation:.**.**
7a9f0 20 20 20 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 Z.**.**
7aa00 49 66 20 74 68 65 20 70 61 72 73 65 20 69 73 20 If the parse is
7aa10 73 75 63 63 65 73 73 66 75 6c 2c 20 77 72 69 74 successful, writ
7aa20 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 e the number of
7aa30 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 minutes.** of ch
7aa40 61 6e 67 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e ange in p->tz an
7aa50 64 20 72 65 74 75 72 6e 20 30 2e 20 20 49 66 20 d return 0. If
7aa60 61 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6f a parser error o
7aa70 63 63 75 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e ccurs,.** return
7aa80 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a non-zero..**.**
7aa90 20 41 20 6d 69 73 73 69 6e 67 20 73 70 65 63 69 A missing speci
7aaa0 66 69 65 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 fier is not cons
7aab0 69 64 65 72 65 64 20 61 6e 20 65 72 72 6f 72 2e idered an error.
7aac0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
7aad0 61 72 73 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e arseTimezone(con
7aae0 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 st char *zDate,
7aaf0 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 DateTime *p){.
7ab00 69 6e 74 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 int sgn = 0;. i
7ab10 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 nt nHr, nMn;. i
7ab20 6e 74 20 63 3b 0a 20 20 77 68 69 6c 65 28 20 73 nt c;. while( s
7ab30 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a qlite3Isspace(*z
7ab40 44 61 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b Date) ){ zDate++
7ab50 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20 3d 20 30 3b ; }. p->tz = 0;
7ab60 0a 20 20 63 20 3d 20 2a 7a 44 61 74 65 3b 0a 20 . c = *zDate;.
7ab70 20 69 66 28 20 63 3d 3d 27 2d 27 20 29 7b 0a 20 if( c=='-' ){.
7ab80 20 20 20 73 67 6e 20 3d 20 2d 31 3b 0a 20 20 7d sgn = -1;. }
7ab90 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 2b 27 20 else if( c=='+'
7aba0 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2b 31 3b ){. sgn = +1;
7abb0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d . }else if( c==
7abc0 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a 27 20 29 7b 'Z' || c=='z' ){
7abd0 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 . zDate++;.
7abe0 20 20 67 6f 74 6f 20 7a 75 6c 75 5f 74 69 6d 65 goto zulu_time
7abf0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
7ac00 65 74 75 72 6e 20 63 21 3d 30 3b 0a 20 20 7d 0a eturn c!=0;. }.
7ac10 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 69 66 28 zDate++;. if(
7ac20 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 getDigits(zDate
7ac30 2c 20 32 2c 20 30 2c 20 31 34 2c 20 27 3a 27 2c , 2, 0, 14, ':',
7ac40 20 26 6e 48 72 2c 20 32 2c 20 30 2c 20 35 39 2c &nHr, 2, 0, 59,
7ac50 20 30 2c 20 26 6e 4d 6e 29 21 3d 32 20 29 7b 0a 0, &nMn)!=2 ){.
7ac60 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 return 1;.
7ac70 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a }. zDate += 5;.
7ac80 20 20 70 2d 3e 74 7a 20 3d 20 73 67 6e 2a 28 6e p->tz = sgn*(n
7ac90 4d 6e 20 2b 20 6e 48 72 2a 36 30 29 3b 0a 7a 75 Mn + nHr*60);.zu
7aca0 6c 75 5f 74 69 6d 65 3a 0a 20 20 77 68 69 6c 65 lu_time:. while
7acb0 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 ( sqlite3Isspace
7acc0 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61 74 (*zDate) ){ zDat
7acd0 65 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 e++; }. return
7ace0 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a *zDate!=0;.}../*
7acf0 0a 2a 2a 20 50 61 72 73 65 20 74 69 6d 65 73 20 .** Parse times
7ad00 6f 66 20 74 68 65 20 66 6f 72 6d 20 48 48 3a 4d of the form HH:M
7ad10 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 20 6f 72 M or HH:MM:SS or
7ad20 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 46 2e 0a HH:MM:SS.FFFF..
7ad30 2a 2a 20 54 68 65 20 48 48 2c 20 4d 4d 2c 20 61 ** The HH, MM, a
7ad40 6e 64 20 53 53 20 6d 75 73 74 20 65 61 63 68 20 nd SS must each
7ad50 62 65 20 65 78 61 63 74 6c 79 20 32 20 64 69 67 be exactly 2 dig
7ad60 69 74 73 2e 20 20 54 68 65 0a 2a 2a 20 66 72 61 its. The.** fra
7ad70 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20 ctional seconds
7ad80 46 46 46 46 20 63 61 6e 20 62 65 20 6f 6e 65 20 FFFF can be one
7ad90 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 73 2e 0a or more digits..
7ada0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 **.** Return 1 i
7adb0 66 20 74 68 65 72 65 20 69 73 20 61 20 70 61 72 f there is a par
7adc0 73 69 6e 67 20 65 72 72 6f 72 20 61 6e 64 20 30 sing error and 0
7add0 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a on success..*/.
7ade0 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 static int parse
7adf0 48 68 4d 6d 53 73 28 63 6f 6e 73 74 20 63 68 61 HhMmSs(const cha
7ae00 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 r *zDate, DateTi
7ae10 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 68 2c me *p){. int h,
7ae20 20 6d 2c 20 73 3b 0a 20 20 64 6f 75 62 6c 65 20 m, s;. double
7ae30 6d 73 20 3d 20 30 2e 30 3b 0a 20 20 69 66 28 20 ms = 0.0;. if(
7ae40 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c getDigits(zDate,
7ae50 20 32 2c 20 30 2c 20 32 34 2c 20 27 3a 27 2c 20 2, 0, 24, ':',
7ae60 26 68 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c &h, 2, 0, 59, 0,
7ae70 20 26 6d 29 21 3d 32 20 29 7b 0a 20 20 20 20 72 &m)!=2 ){. r
7ae80 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a eturn 1;. }. z
7ae90 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 69 66 28 Date += 5;. if(
7aea0 20 2a 7a 44 61 74 65 3d 3d 27 3a 27 20 29 7b 0a *zDate==':' ){.
7aeb0 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 zDate++;.
7aec0 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a if( getDigits(z
7aed0 44 61 74 65 2c 20 32 2c 20 30 2c 20 35 39 2c 20 Date, 2, 0, 59,
7aee0 30 2c 20 26 73 29 21 3d 31 20 29 7b 0a 20 20 20 0, &s)!=1 ){.
7aef0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
7af00 20 7d 0a 20 20 20 20 7a 44 61 74 65 20 2b 3d 20 }. zDate +=
7af10 32 3b 0a 20 20 20 20 69 66 28 20 2a 7a 44 61 74 2;. if( *zDat
7af20 65 3d 3d 27 2e 27 20 26 26 20 73 71 6c 69 74 65 e=='.' && sqlite
7af30 33 49 73 64 69 67 69 74 28 7a 44 61 74 65 5b 31 3Isdigit(zDate[1
7af40 5d 29 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 ]) ){. doub
7af50 6c 65 20 72 53 63 61 6c 65 20 3d 20 31 2e 30 3b le rScale = 1.0;
7af60 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a . zDate++;.
7af70 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c while( sql
7af80 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 44 61 ite3Isdigit(*zDa
7af90 74 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d te) ){. m
7afa0 73 20 3d 20 6d 73 2a 31 30 2e 30 20 2b 20 2a 7a s = ms*10.0 + *z
7afb0 44 61 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 Date - '0';.
7afc0 20 20 20 20 72 53 63 61 6c 65 20 2a 3d 20 31 30 rScale *= 10
7afd0 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 44 61 74 .0;. zDat
7afe0 65 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 e++;. }.
7aff0 20 20 20 6d 73 20 2f 3d 20 72 53 63 61 6c 65 3b ms /= rScale;
7b000 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
7b010 20 20 20 20 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 s = 0;. }.
7b020 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b p->validJD = 0;
7b030 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d . p->validHMS =
7b040 20 31 3b 0a 20 20 70 2d 3e 68 20 3d 20 68 3b 0a 1;. p->h = h;.
7b050 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a 20 20 70 2d p->m = m;. p-
7b060 3e 73 20 3d 20 73 20 2b 20 6d 73 3b 0a 20 20 69 >s = s + ms;. i
7b070 66 28 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65 f( parseTimezone
7b080 28 7a 44 61 74 65 2c 20 70 29 20 29 20 72 65 74 (zDate, p) ) ret
7b090 75 72 6e 20 31 3b 0a 20 20 70 2d 3e 76 61 6c 69 urn 1;. p->vali
7b0a0 64 54 5a 20 3d 20 28 70 2d 3e 74 7a 21 3d 30 29 dTZ = (p->tz!=0)
7b0b0 3f 31 3a 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 ?1:0;. return 0
7b0c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
7b0d0 72 74 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d rt from YYYY-MM-
7b0e0 44 44 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a DD HH:MM:SS to j
7b0f0 75 6c 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 ulian day. We a
7b100 6c 77 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 lways assume.**
7b110 74 68 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d that the YYYY-MM
7b120 2d 44 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 -DD is according
7b130 20 74 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 to the Gregoria
7b140 6e 20 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a n calendar..**.*
7b150 2a 20 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 * Reference: Me
7b160 65 75 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 eus page 61.*/.s
7b170 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 tatic void compu
7b180 74 65 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 teJD(DateTime *p
7b190 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 ){. int Y, M, D
7b1a0 2c 20 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a , A, B, X1, X2;.
7b1b0 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a . if( p->validJ
7b1c0 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 D ) return;. if
7b1d0 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b ( p->validYMD ){
7b1e0 0a 20 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 . Y = p->Y;.
7b1f0 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 M = p->M;.
7b200 20 44 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c D = p->D;. }el
7b210 73 65 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 se{. Y = 2000
7b220 3b 20 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 ; /* If no YMD
7b230 73 70 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d specified, assum
7b240 65 20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f e 2000-Jan-01 */
7b250 0a 20 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 . M = 1;.
7b260 44 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 D = 1;. }. if(
7b270 20 4d 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d M<=2 ){. Y--
7b280 3b 0a 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 ;. M += 12;.
7b290 20 7d 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a }. A = Y/100;.
7b2a0 20 20 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 B = 2 - A + (A
7b2b0 2f 34 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 32 /4);. X1 = 3652
7b2c0 35 2a 28 59 2b 34 37 31 36 29 2f 31 30 30 3b 0a 5*(Y+4716)/100;.
7b2d0 20 20 58 32 20 3d 20 33 30 36 30 30 31 2a 28 4d X2 = 306001*(M
7b2e0 2b 31 29 2f 31 30 30 30 30 3b 0a 20 20 70 2d 3e +1)/10000;. p->
7b2f0 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7b300 6e 74 36 34 29 28 28 58 31 20 2b 20 58 32 20 2b nt64)((X1 + X2 +
7b310 20 44 20 2b 20 42 20 2d 20 31 35 32 34 2e 35 20 D + B - 1524.5
7b320 29 20 2a 20 38 36 34 30 30 30 30 30 29 3b 0a 20 ) * 86400000);.
7b330 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b p->validJD = 1;
7b340 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 . if( p->validH
7b350 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 MS ){. p->iJD
7b360 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30 30 30 += p->h*3600000
7b370 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20 2b 20 + p->m*60000 +
7b380 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7b390 70 2d 3e 73 2a 31 30 30 30 29 3b 0a 20 20 20 20 p->s*1000);.
7b3a0 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 if( p->validTZ )
7b3b0 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d {. p->iJD -
7b3c0 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a 20 = p->tz*60000;.
7b3d0 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 p->validYMD
7b3e0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 = 0;. p->v
7b3f0 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 20 alidHMS = 0;.
7b400 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 p->validTZ =
7b410 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0;. }. }.}..
7b420 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74 65 /*.** Parse date
7b430 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a s of the form.**
7b440 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d .** YYYY-MM-
7b450 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 0a DD HH:MM:SS.FFF.
7b460 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 ** YYYY-MM-D
7b470 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20 20 D HH:MM:SS.**
7b480 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a YYYY-MM-DD HH:
7b490 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d MM.** YYYY-M
7b4a0 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 M-DD.**.** Write
7b4b0 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f the result into
7b4c0 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 the DateTime st
7b4d0 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 ructure and retu
7b4e0 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 rn 0.** on succe
7b4f0 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 20 ss and 1 if the
7b500 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73 20 input string is
7b510 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 not a well-forme
7b520 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 d.** date..*/.st
7b530 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 59 79 atic int parseYy
7b540 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68 61 yyMmDd(const cha
7b550 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54 69 r *zDate, DateTi
7b560 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c me *p){. int Y,
7b570 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20 69 M, D, neg;.. i
7b580 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d 27 f( zDate[0]=='-'
7b590 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b ){. zDate++;
7b5a0 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 . neg = 1;.
7b5b0 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d }else{. neg =
7b5c0 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 65 0;. }. if( ge
7b5d0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34 2c tDigits(zDate,4,
7b5e0 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32 2c 0,9999,'-',&Y,2,
7b5f0 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31 2c 1,12,'-',&M,2,1,
7b600 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a 20 31,0,&D)!=3 ){.
7b610 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d return 1;. }
7b620 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b 0a . zDate += 10;.
7b630 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 while( sqlite3
7b640 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29 20 Isspace(*zDate)
7b650 7c 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 || 'T'==*(u8*)zD
7b660 61 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 ate ){ zDate++;
7b670 7d 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d }. if( parseHhM
7b680 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 mSs(zDate, p)==0
7b690 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f ){. /* We go
7b6a0 74 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 t the time */.
7b6b0 7d 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 }else if( *zDate
7b6c0 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 ==0 ){. p->va
7b6d0 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 lidHMS = 0;. }e
7b6e0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
7b6f0 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 1;. }. p->vali
7b700 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 dJD = 0;. p->va
7b710 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d lidYMD = 1;. p-
7b720 3e 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 >Y = neg ? -Y :
7b730 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 Y;. p->M = M;.
7b740 20 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 p->D = D;. if(
7b750 20 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 p->validTZ ){.
7b760 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b computeJD(p);
7b770 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
7b780 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
7b790 65 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 e time to the cu
7b7a0 72 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 rrent time repor
7b7b0 74 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a ted by the VFS.*
7b7c0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 /.static void se
7b7d0 74 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 tDateTimeToCurre
7b7e0 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 nt(sqlite3_conte
7b7f0 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 xt *context, Dat
7b800 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 eTime *p){. dou
7b810 62 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 ble r;. sqlite3
7b820 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 *db = sqlite3_c
7b830 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 ontext_db_handle
7b840 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c (context);. sql
7b850 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d ite3OsCurrentTim
7b860 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b e(db->pVfs, &r);
7b870 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c . p->iJD = (sql
7b880 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 ite3_int64)(r*86
7b890 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 400000.0 + 0.5);
7b8a0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 . p->validJD =
7b8b0 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 1;.}../*.** Atte
7b8c0 6d 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 mpt to parse the
7b8d0 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e given string in
7b8e0 74 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 to a Julian Day
7b8f0 4e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a Number. Return.
7b900 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ** the number of
7b910 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 errors..**.** T
7b920 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
7b930 20 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d acceptable form
7b940 73 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 s for the input
7b950 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 string:.**.**
7b960 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 YYYY-MM-DD HH
7b970 3a 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 :MM:SS.FFF +/-H
7b980 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 H:MM.** DDD
7b990 44 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f D.DD .** no
7b9a0 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 w.**.** In the f
7b9b0 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b irst form, the +
7b9c0 2f 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 /-HH:MM is alway
7b9d0 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 s optional. The
7b9e0 20 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 fractional.** s
7b9f0 65 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e econds extension
7ba00 20 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 (the ".FFF") is
7ba10 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 optional. The
7ba20 73 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a seconds portion.
7ba30 2a 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 ** (":SS.FFF") i
7ba40 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 s option. The y
7ba50 65 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e ear and date can
7ba60 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c be omitted as l
7ba70 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 ong.** as there
7ba80 69 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 is a time string
7ba90 2e 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 . The time stri
7baa0 6e 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 ng can be omitte
7bab0 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 d as long.** as
7bac0 74 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20 there is a year
7bad0 61 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 and date..*/.sta
7bae0 74 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 tic int parseDat
7baf0 65 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 eOrTime(. sqlit
7bb00 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 e3_context *cont
7bb10 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 ext, . const ch
7bb20 61 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 ar *zDate, . Da
7bb30 74 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 teTime *p.){. i
7bb40 6e 74 20 69 73 52 65 61 6c 4e 75 6d 3b 20 20 20 nt isRealNum;
7bb50 20 2f 2a 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 /* Return from
7bb60 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 sqlite3IsNumber(
7bb70 29 2e 20 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a ). Not used */.
7bb80 20 20 69 66 28 20 70 61 72 73 65 59 79 79 79 4d if( parseYyyyM
7bb90 6d 44 64 28 7a 44 61 74 65 2c 70 29 3d 3d 30 20 mDd(zDate,p)==0
7bba0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
7bbb0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 72 . }else if( par
7bbc0 73 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 seHhMmSs(zDate,
7bbd0 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 p)==0 ){. ret
7bbe0 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 urn 0;. }else i
7bbf0 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d f( sqlite3StrICm
7bc00 70 28 7a 44 61 74 65 2c 22 6e 6f 77 22 29 3d 3d p(zDate,"now")==
7bc10 30 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 0){. setDateT
7bc20 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e imeToCurrent(con
7bc30 74 65 78 74 2c 20 70 29 3b 0a 20 20 20 20 72 65 text, p);. re
7bc40 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 turn 0;. }else
7bc50 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d if( sqlite3IsNum
7bc60 62 65 72 28 7a 44 61 74 65 2c 20 26 69 73 52 65 ber(zDate, &isRe
7bc70 61 6c 4e 75 6d 2c 20 53 51 4c 49 54 45 5f 55 54 alNum, SQLITE_UT
7bc80 46 38 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c F8) ){. doubl
7bc90 65 20 72 3b 0a 20 20 20 20 67 65 74 56 61 6c 75 e r;. getValu
7bca0 65 28 7a 44 61 74 65 2c 20 26 72 29 3b 0a 20 20 e(zDate, &r);.
7bcb0 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 p->iJD = (sqli
7bcc0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34 te3_int64)(r*864
7bcd0 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 00000.0 + 0.5);.
7bce0 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d p->validJD =
7bcf0 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 1;. return 0
7bd00 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 ;. }. return 1
7bd10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 ;.}../*.** Compu
7bd20 74 65 20 74 68 65 20 59 65 61 72 2c 20 4d 6f 6e te the Year, Mon
7bd30 74 68 2c 20 61 6e 64 20 44 61 79 20 66 72 6f 6d th, and Day from
7bd40 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 the julian day
7bd50 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 number..*/.stati
7bd60 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d c void computeYM
7bd70 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a D(DateTime *p){.
7bd80 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42 2c 20 43 int Z, A, B, C
7bd90 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20 20 69 66 , D, E, X1;. if
7bda0 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 20 ( p->validYMD )
7bdb0 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 70 return;. if( !p
7bdc0 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20 ->validJD ){.
7bdd0 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b 0a 20 20 p->Y = 2000;.
7bde0 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 p->M = 1;.
7bdf0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 p->D = 1;. }els
7be00 65 7b 0a 20 20 20 20 5a 20 3d 20 28 69 6e 74 29 e{. Z = (int)
7be10 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 ((p->iJD + 43200
7be20 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0a 000)/86400000);.
7be30 20 20 20 20 41 20 3d 20 28 69 6e 74 29 28 28 5a A = (int)((Z
7be40 20 2d 20 31 38 36 37 32 31 36 2e 32 35 29 2f 33 - 1867216.25)/3
7be50 36 35 32 34 2e 32 35 29 3b 0a 20 20 20 20 41 20 6524.25);. A
7be60 3d 20 5a 20 2b 20 31 20 2b 20 41 20 2d 20 28 41 = Z + 1 + A - (A
7be70 2f 34 29 3b 0a 20 20 20 20 42 20 3d 20 41 20 2b /4);. B = A +
7be80 20 31 35 32 34 3b 0a 20 20 20 20 43 20 3d 20 28 1524;. C = (
7be90 69 6e 74 29 28 28 42 20 2d 20 31 32 32 2e 31 29 int)((B - 122.1)
7bea0 2f 33 36 35 2e 32 35 29 3b 0a 20 20 20 20 44 20 /365.25);. D
7beb0 3d 20 28 33 36 35 32 35 2a 43 29 2f 31 30 30 3b = (36525*C)/100;
7bec0 0a 20 20 20 20 45 20 3d 20 28 69 6e 74 29 28 28 . E = (int)((
7bed0 42 2d 44 29 2f 33 30 2e 36 30 30 31 29 3b 0a 20 B-D)/30.6001);.
7bee0 20 20 20 58 31 20 3d 20 28 69 6e 74 29 28 33 30 X1 = (int)(30
7bef0 2e 36 30 30 31 2a 45 29 3b 0a 20 20 20 20 70 2d .6001*E);. p-
7bf00 3e 44 20 3d 20 42 20 2d 20 44 20 2d 20 58 31 3b >D = B - D - X1;
7bf10 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 45 3c 31 34 . p->M = E<14
7bf20 20 3f 20 45 2d 31 20 3a 20 45 2d 31 33 3b 0a 20 ? E-1 : E-13;.
7bf30 20 20 20 70 2d 3e 59 20 3d 20 70 2d 3e 4d 3e 32 p->Y = p->M>2
7bf40 20 3f 20 43 20 2d 20 34 37 31 36 20 3a 20 43 20 ? C - 4716 : C
7bf50 2d 20 34 37 31 35 3b 0a 20 20 7d 0a 20 20 70 2d - 4715;. }. p-
7bf60 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 7d >validYMD = 1;.}
7bf70 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 ../*.** Compute
7bf80 74 68 65 20 48 6f 75 72 2c 20 4d 69 6e 75 74 65 the Hour, Minute
7bf90 2c 20 61 6e 64 20 53 65 63 6f 6e 64 73 20 66 72 , and Seconds fr
7bfa0 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 om the julian da
7bfb0 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 y number..*/.sta
7bfc0 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
7bfd0 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70 29 HMS(DateTime *p)
7bfe0 7b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 66 28 {. int s;. if(
7bff0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 20 72 p->validHMS ) r
7c000 65 74 75 72 6e 3b 0a 20 20 63 6f 6d 70 75 74 65 eturn;. compute
7c010 4a 44 28 70 29 3b 0a 20 20 73 20 3d 20 28 69 6e JD(p);. s = (in
7c020 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34 33 32 t)((p->iJD + 432
7c030 30 30 30 30 30 29 20 25 20 38 36 34 30 30 30 30 00000) % 8640000
7c040 30 29 3b 0a 20 20 70 2d 3e 73 20 3d 20 73 2f 31 0);. p->s = s/1
7c050 30 30 30 2e 30 3b 0a 20 20 73 20 3d 20 28 69 6e 000.0;. s = (in
7c060 74 29 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d t)p->s;. p->s -
7c070 3d 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f = s;. p->h = s/
7c080 33 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 3600;. s -= p->
7c090 68 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d h*3600;. p->m =
7c0a0 20 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d s/60;. p->s +=
7c0b0 20 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 s - p->m*60;.
7c0c0 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b p->validHMS = 1;
7c0d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 .}../*.** Comput
7c0e0 65 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 e both YMD and H
7c0f0 4d 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 MS.*/.static voi
7c100 64 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 d computeYMD_HMS
7c110 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 (DateTime *p){.
7c120 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a computeYMD(p);.
7c130 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b computeHMS(p);
7c140 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
7c150 74 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 the YMD and HMS
7c160 61 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 and the TZ.*/.st
7c170 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 atic void clearY
7c180 4d 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 MD_HMS_TZ(DateTi
7c190 6d 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c me *p){. p->val
7c1a0 69 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e idYMD = 0;. p->
7c1b0 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 validHMS = 0;.
7c1c0 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
7c1d0 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
7c1e0 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 E_OMIT_LOCALTIME
7c1f0 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 ./*.** Compute t
7c200 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 he difference (i
7c210 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a n milliseconds).
7c220 2a 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c ** between local
7c230 74 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e time and UTC (a.
7c240 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 k.a. GMT).** for
7c250 20 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 the time value
7c260 70 20 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 p where p is in
7c270 55 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 UTC..*/.static s
7c280 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6c 6f 63 qlite3_int64 loc
7c290 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 44 61 74 altimeOffset(Dat
7c2a0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 44 61 74 eTime *p){. Dat
7c2b0 65 54 69 6d 65 20 78 2c 20 79 3b 0a 20 20 74 69 eTime x, y;. ti
7c2c0 6d 65 5f 74 20 74 3b 0a 20 20 78 20 3d 20 2a 70 me_t t;. x = *p
7c2d0 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 ;. computeYMD_H
7c2e0 4d 53 28 26 78 29 3b 0a 20 20 69 66 28 20 78 2e MS(&x);. if( x.
7c2f0 59 3c 31 39 37 31 20 7c 7c 20 78 2e 59 3e 3d 32 Y<1971 || x.Y>=2
7c300 30 33 38 20 29 7b 0a 20 20 20 20 78 2e 59 20 3d 038 ){. x.Y =
7c310 20 32 30 30 30 3b 0a 20 20 20 20 78 2e 4d 20 3d 2000;. x.M =
7c320 20 31 3b 0a 20 20 20 20 78 2e 44 20 3d 20 31 3b 1;. x.D = 1;
7c330 0a 20 20 20 20 78 2e 68 20 3d 20 30 3b 0a 20 20 . x.h = 0;.
7c340 20 20 78 2e 6d 20 3d 20 30 3b 0a 20 20 20 20 78 x.m = 0;. x
7c350 2e 73 20 3d 20 30 2e 30 3b 0a 20 20 7d 20 65 6c .s = 0.0;. } el
7c360 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 73 20 3d se {. int s =
7c370 20 28 69 6e 74 29 28 78 2e 73 20 2b 20 30 2e 35 (int)(x.s + 0.5
7c380 29 3b 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a );. x.s = s;.
7c390 20 20 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a }. x.tz = 0;.
7c3a0 20 20 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b x.validJD = 0;
7c3b0 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 . computeJD(&x)
7c3c0 3b 0a 20 20 74 20 3d 20 28 74 69 6d 65 5f 74 29 ;. t = (time_t)
7c3d0 28 78 2e 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 (x.iJD/1000 - 21
7c3e0 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30 086676*(i64)1000
7c3f0 30 29 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 0);.#ifdef HAVE_
7c400 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20 7b 0a LOCALTIME_R. {.
7c410 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c struct tm sL
7c420 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 ocal;. localt
7c430 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f 63 61 ime_r(&t, &sLoca
7c440 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c l);. y.Y = sL
7c450 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 ocal.tm_year + 1
7c460 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 900;. y.M = s
7c470 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 Local.tm_mon + 1
7c480 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 ;. y.D = sLoc
7c490 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 al.tm_mday;.
7c4a0 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f y.h = sLocal.tm_
7c4b0 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 hour;. y.m =
7c4c0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 sLocal.tm_min;.
7c4d0 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e y.s = sLocal.
7c4e0 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 69 tm_sec;. }.#eli
7c4f0 66 20 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c f defined(HAVE_L
7c500 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 48 OCALTIME_S) && H
7c510 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 0a AVE_LOCALTIME_S.
7c520 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 {. struct t
7c530 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f m sLocal;. lo
7c540 63 61 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 caltime_s(&sLoca
7c550 6c 2c 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 l, &t);. y.Y
7c560 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 = sLocal.tm_year
7c570 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d + 1900;. y.M
7c580 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e = sLocal.tm_mon
7c590 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 + 1;. y.D =
7c5a0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a sLocal.tm_mday;.
7c5b0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c y.h = sLocal
7c5c0 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e .tm_hour;. y.
7c5d0 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 m = sLocal.tm_mi
7c5e0 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f n;. y.s = sLo
7c5f0 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a cal.tm_sec;. }.
7c600 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 #else. {. st
7c610 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 ruct tm *pTm;.
7c620 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
7c630 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 enter(sqlite3Mut
7c640 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d exAlloc(SQLITE_M
7c650 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
7c660 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 ER));. pTm =
7c670 6c 6f 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 localtime(&t);.
7c680 20 20 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d y.Y = pTm->tm
7c690 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 _year + 1900;.
7c6a0 20 20 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f y.M = pTm->tm_
7c6b0 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 mon + 1;. y.D
7c6c0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b = pTm->tm_mday;
7c6d0 0a 20 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e . y.h = pTm->
7c6e0 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d tm_hour;. y.m
7c6f0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a = pTm->tm_min;.
7c700 20 20 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 y.s = pTm->t
7c710 6d 5f 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 m_sec;. sqlit
7c720 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
7c730 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
7c740 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
7c750 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 ATIC_MASTER));.
7c760 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 }.#endif. y.va
7c770 6c 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e lidYMD = 1;. y.
7c780 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 validHMS = 1;.
7c790 79 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 y.validJD = 0;.
7c7a0 20 79 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a y.validTZ = 0;.
7c7b0 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b computeJD(&y);
7c7c0 0a 20 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 . return y.iJD
7c7d0 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 - x.iJD;.}.#endi
7c7e0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
7c7f0 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f _LOCALTIME */../
7c800 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d *.** Process a m
7c810 6f 64 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 odifier to a dat
7c820 65 2d 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 e-time stamp. T
7c830 68 65 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 he modifiers are
7c840 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a .** as follows:.
7c850 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 **.** NNN da
7c860 79 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f ys.** NNN ho
7c870 75 72 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d urs.** NNN m
7c880 69 6e 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e inutes.** NN
7c890 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a N.NNNN seconds.*
7c8a0 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 * NNN months
7c8b0 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 .** NNN year
7c8c0 73 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f s.** start o
7c8d0 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 f month.** s
7c8e0 74 61 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 tart of year.**
7c8f0 20 20 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 start of wee
7c900 6b 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f k.** start o
7c910 66 20 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 f day.** wee
7c920 6b 64 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e kday N.** un
7c930 69 78 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c ixepoch.** l
7c940 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 ocaltime.**
7c950 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e utc.**.** Return
7c960 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 0 on success an
7c970 64 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 d 1 if there is
7c980 61 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f any kind of erro
7c990 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
7c9a0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 parseModifier(c
7c9b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c onst char *zMod,
7c9c0 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 DateTime *p){.
7c9d0 20 69 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 int rc = 1;. i
7c9e0 6e 74 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 nt n;. double r
7c9f0 3b 0a 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 ;. char *z, zBu
7ca00 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 f[30];. z = zBu
7ca10 66 3b 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c f;. for(n=0; n<
7ca20 41 72 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d ArraySize(zBuf)-
7ca30 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 1 && zMod[n]; n+
7ca40 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 28 +){. z[n] = (
7ca50 63 68 61 72 29 73 71 6c 69 74 65 33 55 70 70 65 char)sqlite3Uppe
7ca60 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29 7a 4d 6f rToLower[(u8)zMo
7ca70 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6e d[n]];. }. z[n
7ca80 5d 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 ] = 0;. switch(
7ca90 20 7a 5b 30 5d 20 29 7b 0a 23 69 66 6e 64 65 66 z[0] ){.#ifndef
7caa0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 SQLITE_OMIT_LOC
7cab0 41 4c 54 49 4d 45 0a 20 20 20 20 63 61 73 65 20 ALTIME. case
7cac0 27 6c 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 'l': {. /*
7cad0 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 20 20 20 localtime.
7cae0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 **. ** A
7caf0 73 73 75 6d 69 6e 67 20 74 68 65 20 63 75 72 72 ssuming the curr
7cb00 65 6e 74 20 74 69 6d 65 20 76 61 6c 75 65 20 69 ent time value i
7cb10 73 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20 47 4d s UTC (a.k.a. GM
7cb20 54 29 2c 20 73 68 69 66 74 20 69 74 20 74 6f 0a T), shift it to.
7cb30 20 20 20 20 20 20 2a 2a 20 73 68 6f 77 20 6c 6f ** show lo
7cb40 63 61 6c 20 74 69 6d 65 2e 0a 20 20 20 20 20 20 cal time..
7cb50 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 */. if( str
7cb60 63 6d 70 28 7a 2c 20 22 6c 6f 63 61 6c 74 69 6d cmp(z, "localtim
7cb70 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 e")==0 ){.
7cb80 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a computeJD(p);.
7cb90 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b p->iJD +
7cba0 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 = localtimeOffse
7cbb0 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c t(p);. cl
7cbc0 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 earYMD_HMS_TZ(p)
7cbd0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 ;. rc = 0
7cbe0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7cbf0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e break;. }.#en
7cc00 64 69 66 0a 20 20 20 20 63 61 73 65 20 27 75 27 dif. case 'u'
7cc10 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 : {. /*.
7cc20 20 20 20 2a 2a 20 20 20 20 75 6e 69 78 65 70 6f ** unixepo
7cc30 63 68 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 ch. **.
7cc40 20 20 2a 2a 20 54 72 65 61 74 20 74 68 65 20 63 ** Treat the c
7cc50 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 urrent value of
7cc60 70 2d 3e 69 4a 44 20 61 73 20 74 68 65 20 6e 75 p->iJD as the nu
7cc70 6d 62 65 72 20 6f 66 0a 20 20 20 20 20 20 2a 2a mber of. **
7cc80 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 seconds since 1
7cc90 39 37 30 2e 20 20 43 6f 6e 76 65 72 74 20 74 6f 970. Convert to
7cca0 20 61 20 72 65 61 6c 20 6a 75 6c 69 61 6e 20 64 a real julian d
7ccb0 61 79 20 6e 75 6d 62 65 72 2e 0a 20 20 20 20 20 ay number..
7ccc0 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 74 */. if( st
7ccd0 72 63 6d 70 28 7a 2c 20 22 75 6e 69 78 65 70 6f rcmp(z, "unixepo
7cce0 63 68 22 29 3d 3d 30 20 26 26 20 70 2d 3e 76 61 ch")==0 && p->va
7ccf0 6c 69 64 4a 44 20 29 7b 0a 20 20 20 20 20 20 20 lidJD ){.
7cd00 20 70 2d 3e 69 4a 44 20 3d 20 28 70 2d 3e 69 4a p->iJD = (p->iJ
7cd10 44 20 2b 20 34 33 32 30 30 29 2f 38 36 34 30 30 D + 43200)/86400
7cd20 20 2b 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 + 21086676*(i64
7cd30 29 31 30 30 30 30 30 30 30 3b 0a 20 20 20 20 20 )10000000;.
7cd40 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f clearYMD_HMS_
7cd50 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 TZ(p);. r
7cd60 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 c = 0;. }.#
7cd70 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
7cd80 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 IT_LOCALTIME.
7cd90 20 20 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 else if( strc
7cda0 6d 70 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 mp(z, "utc")==0
7cdb0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
7cdc0 65 33 5f 69 6e 74 36 34 20 63 31 3b 0a 20 20 20 e3_int64 c1;.
7cdd0 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 computeJD(p
7cde0 29 3b 0a 20 20 20 20 20 20 20 20 63 31 20 3d 20 );. c1 =
7cdf0 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 localtimeOffset(
7ce00 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 p);. p->i
7ce10 4a 44 20 2d 3d 20 63 31 3b 0a 20 20 20 20 20 20 JD -= c1;.
7ce20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 clearYMD_HMS_T
7ce30 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d Z(p);. p-
7ce40 3e 69 4a 44 20 2b 3d 20 63 31 20 2d 20 6c 6f 63 >iJD += c1 - loc
7ce50 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b altimeOffset(p);
7ce60 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
7ce70 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
7ce80 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7ce90 20 7d 0a 20 20 20 20 63 61 73 65 20 27 77 27 3a }. case 'w':
7cea0 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 {. /*.
7ceb0 20 20 2a 2a 20 20 20 20 77 65 65 6b 64 61 79 20 ** weekday
7cec0 4e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 N. **.
7ced0 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61 74 ** Move the dat
7cee0 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 69 e to the same ti
7cef0 6d 65 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 6f me on the next o
7cf00 63 63 75 72 72 65 6e 63 65 20 6f 66 0a 20 20 20 ccurrence of.
7cf10 20 20 20 2a 2a 20 77 65 65 6b 64 61 79 20 4e 20 ** weekday N
7cf20 77 68 65 72 65 20 30 3d 3d 53 75 6e 64 61 79 2c where 0==Sunday,
7cf30 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20 61 6e 64 20 1==Monday, and
7cf40 73 6f 20 66 6f 72 74 68 2e 20 20 49 66 20 74 68 so forth. If th
7cf50 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 65 20 e. ** date
7cf60 69 73 20 61 6c 72 65 61 64 79 20 6f 6e 20 74 68 is already on th
7cf70 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 77 65 e appropriate we
7cf80 65 6b 64 61 79 2c 20 74 68 69 73 20 69 73 20 61 ekday, this is a
7cf90 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 20 20 2a 2f no-op.. */
7cfa0 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 . if( strnc
7cfb0 6d 70 28 7a 2c 20 22 77 65 65 6b 64 61 79 20 22 mp(z, "weekday "
7cfc0 2c 20 38 29 3d 3d 30 20 26 26 20 67 65 74 56 61 , 8)==0 && getVa
7cfd0 6c 75 65 28 26 7a 5b 38 5d 2c 26 72 29 3e 30 0a lue(&z[8],&r)>0.
7cfe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7cff0 20 26 26 20 28 6e 3d 28 69 6e 74 29 72 29 3d 3d && (n=(int)r)==
7d000 72 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 r && n>=0 && r<7
7d010 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
7d020 74 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 te3_int64 Z;.
7d030 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f computeYMD_
7d040 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 HMS(p);.
7d050 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a p->validTZ = 0;.
7d060 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 p->valid
7d070 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 JD = 0;.
7d080 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 computeJD(p);.
7d090 20 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 Z = ((p->i
7d0a0 4a 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f JD + 129600000)/
7d0b0 38 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 86400000) % 7;.
7d0c0 20 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 if( Z>n )
7d0d0 20 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 Z -= 7;.
7d0e0 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 p->iJD += (n -
7d0f0 5a 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 Z)*86400000;.
7d100 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d clearYMD_HM
7d110 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 S_TZ(p);.
7d120 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d rc = 0;. }
7d130 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
7d140 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27 }. case 's'
7d150 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 : {. /*.
7d160 20 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f ** start o
7d170 66 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a f TTTTT. **
7d180 0a 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 . ** Move t
7d190 68 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 he date backward
7d1a0 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 s to the beginni
7d1b0 6e 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e ng of the curren
7d1c0 74 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 t day,. **
7d1d0 6f 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 or month or year
7d1e0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
7d1f0 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 if( strncmp(z,
7d200 22 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 "start of ", 9)!
7d210 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
7d220 20 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 z += 9;.
7d230 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 computeYMD(p);.
7d240 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 p->validHMS
7d250 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 = 1;. p->h
7d260 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 = p->m = 0;.
7d270 20 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 p->s = 0.0;.
7d280 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 p->validTZ
7d290 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 = 0;. p->va
7d2a0 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 lidJD = 0;.
7d2b0 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d if( strcmp(z,"m
7d2c0 6f 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 onth")==0 ){.
7d2d0 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 p->D = 1;.
7d2e0 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 rc = 0;.
7d2f0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 }else if( s
7d300 74 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d trcmp(z,"year")=
7d310 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f =0 ){. co
7d320 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 mputeYMD(p);.
7d330 20 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 p->M = 1;.
7d340 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b p->D = 1;
7d350 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b . rc = 0;
7d360 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
7d370 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 strcmp(z,"day")
7d380 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ==0 ){. 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 2b 27 3a 0a }. case '+':.
7d3c0 20 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 case '-':.
7d3d0 20 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 case '0':.
7d3e0 63 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 case '1':. ca
7d3f0 73 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 se '2':. case
7d400 20 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 '3':. case '
7d410 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 4':. case '5'
7d420 3a 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a :. case '6':.
7d430 20 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 case '7':.
7d440 20 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 case '8':.
7d450 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 case '9': {.
7d460 20 20 64 6f 75 62 6c 65 20 72 52 6f 75 6e 64 65 double rRounde
7d470 72 3b 0a 20 20 20 20 20 20 6e 20 3d 20 67 65 74 r;. n = get
7d480 56 61 6c 75 65 28 7a 2c 20 26 72 29 3b 0a 20 20 Value(z, &r);.
7d490 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 31 assert( n>=1
7d4a0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b );. if( z[
7d4b0 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 20 n]==':' ){.
7d4c0 20 20 20 2f 2a 20 41 20 6d 6f 64 69 66 69 65 72 /* A modifier
7d4d0 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 28 2b 7c of the form (+|
7d4e0 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 61 -)HH:MM:SS.FFF a
7d4f0 64 64 73 20 28 6f 72 20 73 75 62 74 72 61 63 74 dds (or subtract
7d500 73 29 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a s) the. *
7d510 2a 20 73 70 65 63 69 66 69 65 64 20 6e 75 6d 62 * specified numb
7d520 65 72 20 6f 66 20 68 6f 75 72 73 2c 20 6d 69 6e er of hours, min
7d530 75 74 65 73 2c 20 73 65 63 6f 6e 64 73 2c 20 61 utes, seconds, a
7d540 6e 64 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 nd fractional se
7d550 63 6f 6e 64 73 0a 20 20 20 20 20 20 20 20 2a 2a conds. **
7d560 20 74 6f 20 74 68 65 20 74 69 6d 65 2e 20 20 54 to the time. T
7d570 68 65 20 22 2e 46 46 46 22 20 6d 61 79 20 62 65 he ".FFF" may be
7d580 20 6f 6d 69 74 74 65 64 2e 20 20 54 68 65 20 22 omitted. The "
7d590 3a 53 53 2e 46 46 46 22 20 6d 61 79 20 62 65 0a :SS.FFF" may be.
7d5a0 20 20 20 20 20 20 20 20 2a 2a 20 6f 6d 69 74 74 ** omitt
7d5b0 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 ed.. */.
7d5c0 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 const cha
7d5d0 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 20 20 20 r *z2 = z;.
7d5e0 20 20 20 44 61 74 65 54 69 6d 65 20 74 78 3b 0a DateTime tx;.
7d5f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
7d600 69 6e 74 36 34 20 64 61 79 3b 0a 20 20 20 20 20 int64 day;.
7d610 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 if( !sqlite3I
7d620 73 64 69 67 69 74 28 2a 7a 32 29 20 29 20 7a 32 sdigit(*z2) ) z2
7d630 2b 2b 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 ++;. mems
7d640 65 74 28 26 74 78 2c 20 30 2c 20 73 69 7a 65 6f et(&tx, 0, sizeo
7d650 66 28 74 78 29 29 3b 0a 20 20 20 20 20 20 20 20 f(tx));.
7d660 69 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 if( parseHhMmSs(
7d670 7a 32 2c 20 26 74 78 29 20 29 20 62 72 65 61 6b z2, &tx) ) break
7d680 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 ;. comput
7d690 65 4a 44 28 26 74 78 29 3b 0a 20 20 20 20 20 20 eJD(&tx);.
7d6a0 20 20 74 78 2e 69 4a 44 20 2d 3d 20 34 33 32 30 tx.iJD -= 4320
7d6b0 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 64 61 0000;. da
7d6c0 79 20 3d 20 74 78 2e 69 4a 44 2f 38 36 34 30 30 y = tx.iJD/86400
7d6d0 30 30 30 3b 0a 20 20 20 20 20 20 20 20 74 78 2e 000;. tx.
7d6e0 69 4a 44 20 2d 3d 20 64 61 79 2a 38 36 34 30 30 iJD -= day*86400
7d6f0 30 30 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 000;. if(
7d700 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29 20 74 78 2e z[0]=='-' ) tx.
7d710 69 4a 44 20 3d 20 2d 74 78 2e 69 4a 44 3b 0a 20 iJD = -tx.iJD;.
7d720 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 computeJD
7d730 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63 6c 65 (p);. cle
7d740 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b arYMD_HMS_TZ(p);
7d750 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 . p->iJD
7d760 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 += tx.iJD;.
7d770 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 rc = 0;.
7d780 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
7d790 7d 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a }. z += n;.
7d7a0 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c while( sql
7d7b0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 ite3Isspace(*z)
7d7c0 29 20 7a 2b 2b 3b 0a 20 20 20 20 20 20 6e 20 3d ) z++;. n =
7d7d0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
7d7e0 28 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e (z);. if( n
7d7f0 3e 31 30 20 7c 7c 20 6e 3c 33 20 29 20 62 72 65 >10 || n<3 ) bre
7d800 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b ak;. if( z[
7d810 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b 20 7a 5b 6e n-1]=='s' ){ z[n
7d820 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d 3b 20 7d 0a -1] = 0; n--; }.
7d830 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7d840 70 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 30 p);. rc = 0
7d850 3b 0a 20 20 20 20 20 20 72 52 6f 75 6e 64 65 72 ;. rRounder
7d860 20 3d 20 72 3c 30 20 3f 20 2d 30 2e 35 20 3a 20 = r<0 ? -0.5 :
7d870 2b 30 2e 35 3b 0a 20 20 20 20 20 20 69 66 28 20 +0.5;. if(
7d880 6e 3d 3d 33 20 26 26 20 73 74 72 63 6d 70 28 7a n==3 && strcmp(z
7d890 2c 22 64 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 ,"day")==0 ){.
7d8a0 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
7d8b0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7d8c0 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 r*86400000.0 + r
7d8d0 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 Rounder);.
7d8e0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26 }else if( n==4 &
7d8f0 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75 72 & strcmp(z,"hour
7d900 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ")==0 ){.
7d910 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 p->iJD += (sqli
7d920 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 te3_int64)(r*(86
7d930 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20 2b 400000.0/24.0) +
7d940 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 rRounder);.
7d950 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 }else if( n==6
7d960 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 69 && strcmp(z,"mi
7d970 6e 75 74 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 nute")==0 ){.
7d980 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 p->iJD += (
7d990 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 sqlite3_int64)(r
7d9a0 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 *(86400000.0/(24
7d9b0 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 .0*60.0)) + rRou
7d9c0 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
7d9d0 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73 se if( n==6 && s
7d9e0 74 72 63 6d 70 28 7a 2c 22 73 65 63 6f 6e 64 22 trcmp(z,"second"
7d9f0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
7da00 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
7da10 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34 e3_int64)(r*(864
7da20 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30 00000.0/(24.0*60
7da30 2e 30 2a 36 30 2e 30 29 29 20 2b 20 72 52 6f 75 .0*60.0)) + rRou
7da40 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c nder);. }el
7da50 73 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 se if( n==5 && s
7da60 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 trcmp(z,"month")
7da70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 ==0 ){. i
7da80 6e 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 nt x, y;.
7da90 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
7daa0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d p);. p->M
7dab0 20 2b 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 += (int)r;.
7dac0 20 20 20 20 78 20 3d 20 70 2d 3e 4d 3e 30 20 3f x = p->M>0 ?
7dad0 20 28 70 2d 3e 4d 2d 31 29 2f 31 32 20 3a 20 28 (p->M-1)/12 : (
7dae0 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b 0a 20 20 20 p->M-12)/12;.
7daf0 20 20 20 20 20 70 2d 3e 59 20 2b 3d 20 78 3b 0a p->Y += x;.
7db00 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 2d 3d 20 p->M -=
7db10 78 2a 31 32 3b 0a 20 20 20 20 20 20 20 20 70 2d x*12;. p-
7db20 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 >validJD = 0;.
7db30 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 computeJD(
7db40 70 29 3b 0a 20 20 20 20 20 20 20 20 79 20 3d 20 p);. y =
7db50 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 (int)r;.
7db60 69 66 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 if( y!=r ){.
7db70 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 p->iJD +=
7db80 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 (sqlite3_int64)(
7db90 28 72 20 2d 20 79 29 2a 33 30 2e 30 2a 38 36 34 (r - y)*30.0*864
7dba0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 00000.0 + rRound
7dbb0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 er);. }.
7dbc0 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
7dbd0 3d 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c ==4 && strcmp(z,
7dbe0 22 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 "year")==0 ){.
7dbf0 20 20 20 20 20 20 69 6e 74 20 79 20 3d 20 28 69 int y = (i
7dc00 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 63 6f nt)r;. co
7dc10 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b mputeYMD_HMS(p);
7dc20 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d . p->Y +=
7dc30 20 79 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 y;. p->v
7dc40 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 alidJD = 0;.
7dc50 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 computeJD(p)
7dc60 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 21 ;. if( y!
7dc70 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =r ){.
7dc80 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74 p->iJD += (sqlit
7dc90 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20 79 e3_int64)((r - y
7dca0 29 2a 33 36 35 2e 30 2a 38 36 34 30 30 30 30 30 )*365.0*86400000
7dcb0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a .0 + rRounder);.
7dcc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
7dcd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 }else{. r
7dce0 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 c = 1;. }.
7dcf0 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d clearYMD_HM
7dd00 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 S_TZ(p);. b
7dd10 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
7dd20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
7dd30 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
7dd40 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
7dd50 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 ../*.** Process
7dd60 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 time function ar
7dd70 67 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 guments. argv[0
7dd80 5d 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 ] is a date-time
7dd90 20 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b stamp..** argv[
7dda0 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 1] and following
7ddb0 20 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 are modifiers.
7ddc0 20 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 Parse them all
7ddd0 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 and write.** the
7dde0 20 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 resulting time
7ddf0 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d into the DateTim
7de00 65 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 e structure p.
7de10 52 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 Return 0.** on s
7de20 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 uccess and 1 if
7de30 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 there are any er
7de40 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 rors..**.** If t
7de50 68 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 here are zero pa
7de60 72 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 rameters (if eve
7de70 6e 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 n argv[0] is und
7de80 65 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 efined).** then
7de90 61 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 assume a default
7dea0 20 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 value of "now"
7deb0 66 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a for argv[0]..*/.
7dec0 73 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 static int isDat
7ded0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e e(. sqlite3_con
7dee0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a text *context, .
7def0 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 int argc, . s
7df00 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
7df10 72 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 rgv, . DateTime
7df20 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a *p.){. int i;.
7df30 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 const unsigned
7df40 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 char *z;. int
7df50 65 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 eType;. memset(
7df60 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 p, 0, sizeof(*p)
7df70 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 );. if( argc==0
7df80 20 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 ){. setDateT
7df90 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e imeToCurrent(con
7dfa0 74 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 text, p);. }els
7dfb0 65 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 e if( (eType = s
7dfc0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 qlite3_value_typ
7dfd0 65 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c e(argv[0]))==SQL
7dfe0 49 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 ITE_FLOAT.
7dff0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 ||
7e000 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e eType==SQLITE_IN
7e010 54 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e TEGER ){. p->
7e020 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 iJD = (sqlite3_i
7e030 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 76 61 nt64)(sqlite3_va
7e040 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b lue_double(argv[
7e050 30 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 0])*86400000.0 +
7e060 20 30 2e 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 0.5);. p->va
7e070 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c lidJD = 1;. }el
7e080 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 se{. z = sqli
7e090 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 te3_value_text(a
7e0a0 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 rgv[0]);. if(
7e0b0 20 21 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 !z || parseDate
7e0c0 4f 72 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 OrTime(context,
7e0d0 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a (char*)z, p) ){.
7e0e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
7e0f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 }. }. for(
7e100 69 3d 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b i=1; i<argc; i++
7e110 29 7b 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 ){. if( (z =
7e120 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
7e130 78 74 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 xt(argv[i]))==0
7e140 7c 7c 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 || parseModifier
7e150 28 28 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b ((char*)z, p) ){
7e160 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
7e170 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
7e180 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn 0;.}.../*.**
7e190 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
7e1a0 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e outines implemen
7e1b0 74 20 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 t the various da
7e1c0 74 65 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 te and time func
7e1d0 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 tions.** of SQLi
7e1e0 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 te..*/../*.**
7e1f0 20 6a 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 julianday( TIME
7e200 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 STRING, MOD, MOD
7e210 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 , ...).**.** Ret
7e220 75 72 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 urn the julian d
7e230 61 79 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ay number of the
7e240 20 64 61 74 65 20 73 70 65 63 69 66 69 65 64 20 date specified
7e250 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 in the arguments
7e260 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7e270 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 juliandayFunc(.
7e280 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 sqlite3_context
7e290 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 *context,. int
7e2a0 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 argc,. sqlite3
7e2b0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b _value **argv.){
7e2c0 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 . DateTime x;.
7e2d0 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 if( isDate(cont
7e2e0 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c ext, argc, argv,
7e2f0 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 &x)==0 ){. c
7e300 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 omputeJD(&x);.
7e310 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 sqlite3_result
7e320 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c _double(context,
7e330 20 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e x.iJD/86400000.
7e340 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 0);. }.}../*.**
7e350 20 20 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 datetime( TI
7e360 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d MESTRING, MOD, M
7e370 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 OD, ...).**.** R
7e380 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 eturn YYYY-MM-DD
7e390 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 HH:MM:SS.*/.sta
7e3a0 74 69 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d tic void datetim
7e3b0 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7e3c0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7e3d0 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7e3e0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7e3f0 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
7e400 69 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 ime x;. if( isD
7e410 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 ate(context, arg
7e420 63 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 c, argv, &x)==0
7e430 29 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 ){. char zBuf
7e440 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 [100];. compu
7e450 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 teYMD_HMS(&x);.
7e460 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
7e470 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 ntf(sizeof(zBuf)
7e480 2c 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 , zBuf, "%04d-%0
7e490 32 64 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 2d-%02d %02d:%02
7e4a0 64 3a 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 d:%02d",.
7e4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 78 2e x.
7e4c0 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 Y, x.M, x.D, x.h
7e4d0 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 , x.m, (int)(x.s
7e4e0 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ));. sqlite3_
7e4f0 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 result_text(cont
7e500 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 ext, zBuf, -1, S
7e510 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
7e520 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 ;. }.}../*.**
7e530 20 20 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 time( TIMESTRI
7e540 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e NG, MOD, MOD, ..
7e550 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 .).**.** Return
7e560 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 HH:MM:SS.*/.stat
7e570 69 63 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 ic void timeFunc
7e580 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
7e590 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 ext *context,.
7e5a0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 int argc,. sqli
7e5b0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 te3_value **argv
7e5c0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 .){. DateTime x
7e5d0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 ;. if( isDate(c
7e5e0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 ontext, argc, ar
7e5f0 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 gv, &x)==0 ){.
7e600 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d char zBuf[100]
7e610 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 ;. computeHMS
7e620 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 (&x);. sqlite
7e630 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
7e640 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 f(zBuf), zBuf, "
7e650 25 30 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c %02d:%02d:%02d",
7e660 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 x.h, x.m, (int)
7e670 78 2e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 x.s);. sqlite
7e680 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 3_result_text(co
7e690 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c ntext, zBuf, -1,
7e6a0 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
7e6b0 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a T);. }.}../*.**
7e6c0 20 20 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 date( TIMEST
7e6d0 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 RING, MOD, MOD,
7e6e0 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 ...).**.** Retur
7e6f0 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a n YYYY-MM-DD.*/.
7e700 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 static void date
7e710 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f Func(. sqlite3_
7e720 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 context *context
7e730 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 ,. int argc,.
7e740 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
7e750 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 argv.){. DateTi
7e760 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 me x;. if( isDa
7e770 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 te(context, argc
7e780 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 , argv, &x)==0 )
7e790 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b {. char zBuf[
7e7a0 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 100];. comput
7e7b0 65 59 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 eYMD(&x);. sq
7e7c0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 lite3_snprintf(s
7e7d0 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 izeof(zBuf), zBu
7e7e0 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 f, "%04d-%02d-%0
7e7f0 32 64 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2d", x.Y, x.M, x
7e800 2e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 .D);. sqlite3
7e810 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e _result_text(con
7e820 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 text, zBuf, -1,
7e830 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 SQLITE_TRANSIENT
7e840 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
7e850 20 20 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 strftime( FOR
7e860 4d 41 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c MAT, TIMESTRING,
7e870 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a MOD, MOD, ...).
7e880 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 **.** Return a s
7e890 74 72 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 tring described
7e8a0 62 79 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 by FORMAT. Conv
7e8b0 65 72 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f ersions as follo
7e8c0 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 ws:.**.** %d
7e8d0 64 61 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 day of month.**
7e8e0 20 20 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f %f ** fractio
7e8f0 6e 61 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e nal seconds SS.
7e900 53 53 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 SSS.** %H hou
7e910 72 20 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 r 00-24.** %j
7e920 20 64 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 day of year 000
7e930 2d 33 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a -366.** %J **
7e940 20 4a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 Julian day numb
7e950 65 72 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 er.** %m mont
7e960 68 20 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 h 01-12.** %M
7e970 20 6d 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a minute 00-59.**
7e980 20 20 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 %s seconds s
7e990 69 6e 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a ince 1970-01-01.
7e9a0 2a 2a 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 ** %S seconds
7e9b0 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 00-59.** %w
7e9c0 64 61 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 day of week 0-6
7e9d0 20 73 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 sunday==0.**
7e9e0 25 57 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 %W week of year
7e9f0 20 30 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 00-53.** %Y
7ea00 79 65 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a year 0000-9999.*
7ea10 2a 20 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 * %% %.*/.sta
7ea20 74 69 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d tic void strftim
7ea30 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 eFunc(. sqlite3
7ea40 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 _context *contex
7ea50 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 t,. int argc,.
7ea60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7ea70 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 *argv.){. DateT
7ea80 69 6d 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a ime x;. u64 n;.
7ea90 20 20 73 69 7a 65 5f 74 20 69 2c 6a 3b 0a 20 20 size_t i,j;.
7eaa0 63 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 char *z;. sqlit
7eab0 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20 e3 *db;. const
7eac0 63 68 61 72 20 2a 7a 46 6d 74 20 3d 20 28 63 6f char *zFmt = (co
7ead0 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74 65 nst char*)sqlite
7eae0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 3_value_text(arg
7eaf0 76 5b 30 5d 29 3b 0a 20 20 63 68 61 72 20 7a 42 v[0]);. char zB
7eb00 75 66 5b 31 30 30 5d 3b 0a 20 20 69 66 28 20 7a uf[100];. if( z
7eb10 46 6d 74 3d 3d 30 20 7c 7c 20 69 73 44 61 74 65 Fmt==0 || isDate
7eb20 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2d 31 (context, argc-1
7eb30 2c 20 61 72 67 76 2b 31 2c 20 26 78 29 20 29 20 , argv+1, &x) )
7eb40 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 73 return;. db = s
7eb50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 qlite3_context_d
7eb60 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 b_handle(context
7eb70 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6e 3d );. for(i=0, n=
7eb80 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 1; zFmt[i]; i++,
7eb90 20 6e 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a n++){. if( z
7eba0 46 6d 74 5b 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 Fmt[i]=='%' ){.
7ebb0 20 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d switch( zFm
7ebc0 74 5b 69 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 t[i+1] ){.
7ebd0 20 20 63 61 73 65 20 27 64 27 3a 0a 20 20 20 20 case 'd':.
7ebe0 20 20 20 20 63 61 73 65 20 27 48 27 3a 0a 20 20 case 'H':.
7ebf0 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a 0a case 'm':.
7ec00 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4d 27 case 'M'
7ec10 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 :. case '
7ec20 53 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 S':. case
7ec30 20 27 57 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'W':.
7ec40 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 2f n++;. /
7ec50 2a 20 66 61 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 * fall thru */.
7ec60 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a case 'w':
7ec70 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 25 . case '%
7ec80 27 3a 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 ':. bre
7ec90 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
7eca0 20 27 66 27 3a 0a 20 20 20 20 20 20 20 20 20 20 'f':.
7ecb0 6e 20 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 n += 8;.
7ecc0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
7ecd0 20 63 61 73 65 20 27 6a 27 3a 0a 20 20 20 20 20 case 'j':.
7ece0 20 20 20 20 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 n += 3;.
7ecf0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
7ed00 20 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 0a case 'Y':.
7ed10 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 38 n += 8
7ed20 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
7ed30 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
7ed40 27 73 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 's':. cas
7ed50 65 20 27 4a 27 3a 0a 20 20 20 20 20 20 20 20 20 e 'J':.
7ed60 20 6e 20 2b 3d 20 35 30 3b 0a 20 20 20 20 20 20 n += 50;.
7ed70 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
7ed80 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 default:.
7ed90 20 20 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f return; /
7eda0 2a 20 45 52 52 4f 52 2e 20 20 72 65 74 75 72 6e * ERROR. return
7edb0 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 a NULL */.
7edc0 20 7d 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 }. i++;.
7edd0 20 20 7d 0a 20 20 7d 0a 20 20 74 65 73 74 63 61 }. }. testca
7ede0 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 se( n==sizeof(zB
7edf0 75 66 29 2d 31 20 29 3b 0a 20 20 74 65 73 74 63 uf)-1 );. testc
7ee00 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f 66 28 7a ase( n==sizeof(z
7ee10 42 75 66 29 20 29 3b 0a 20 20 74 65 73 74 63 61 Buf) );. testca
7ee20 73 65 28 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e se( n==(u64)db->
7ee30 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
7ee40 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b MIT_LENGTH]+1 );
7ee50 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d . testcase( n==
7ee60 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b (u64)db->aLimit[
7ee70 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e SQLITE_LIMIT_LEN
7ee80 47 54 48 5d 20 29 3b 0a 20 20 69 66 28 20 6e 3c GTH] );. if( n<
7ee90 73 69 7a 65 6f 66 28 7a 42 75 66 29 20 29 7b 0a sizeof(zBuf) ){.
7eea0 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
7eeb0 7d 65 6c 73 65 20 69 66 28 20 6e 3e 28 75 36 34 }else if( n>(u64
7eec0 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 )db->aLimit[SQLI
7eed0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
7eee0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
7eef0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
7ef00 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 big(context);.
7ef10 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 return;. }els
7ef20 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 e{. z = sqlit
7ef30 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 e3DbMallocRaw(db
7ef40 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 20 20 69 , (int)n);. i
7ef50 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 f( z==0 ){.
7ef60 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
7ef70 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 error_nomem(cont
7ef80 65 78 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ext);. retu
7ef90 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
7efa0 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 computeJD(&x);.
7efb0 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 computeYMD_HMS(
7efc0 26 78 29 3b 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 &x);. for(i=j=0
7efd0 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69 2b 2b 29 7b ; zFmt[i]; i++){
7efe0 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d . if( zFmt[i]
7eff0 21 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 7a !='%' ){. z
7f000 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74 5b 69 5d 3b [j++] = zFmt[i];
7f010 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
7f020 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 73 77 69 i++;. swi
7f030 74 63 68 28 20 7a 46 6d 74 5b 69 5d 20 29 7b 0a tch( zFmt[i] ){.
7f040 20 20 20 20 20 20 20 20 63 61 73 65 20 27 64 27 case 'd'
7f050 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
7f060 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 ntf(3, &z[j],"%0
7f070 32 64 22 2c 78 2e 44 29 3b 20 6a 2b 3d 32 3b 20 2d",x.D); j+=2;
7f080 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 break;. c
7f090 61 73 65 20 27 66 27 3a 20 7b 0a 20 20 20 20 20 ase 'f': {.
7f0a0 20 20 20 20 20 64 6f 75 62 6c 65 20 73 20 3d 20 double s =
7f0b0 78 2e 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 x.s;. i
7f0c0 66 28 20 73 3e 35 39 2e 39 39 39 20 29 20 73 20 f( s>59.999 ) s
7f0d0 3d 20 35 39 2e 39 39 39 3b 0a 20 20 20 20 20 20 = 59.999;.
7f0e0 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
7f0f0 69 6e 74 66 28 37 2c 20 26 7a 5b 6a 5d 2c 22 25 intf(7, &z[j],"%
7f100 30 36 2e 33 66 22 2c 20 73 29 3b 0a 20 20 20 20 06.3f", s);.
7f110 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 j += sqlit
7f120 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d e3Strlen30(&z[j]
7f130 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 );. bre
7f140 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ak;. }.
7f150 20 20 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 case 'H':
7f160 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
7f170 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 f(3, &z[j],"%02d
7f180 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 ",x.h); j+=2; br
7f190 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 eak;. cas
7f1a0 65 20 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 e 'W': /* Fall t
7f1b0 68 72 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 hru */. c
7f1c0 61 73 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 ase 'j': {.
7f1d0 20 20 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 int nDay;
7f1e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
7f1f0 6d 62 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e mber of days sin
7f200 63 65 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 ce 1st day of ye
7f210 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 ar */.
7f220 44 61 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a DateTime y = x;.
7f230 20 20 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 y.vali
7f240 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 dJD = 0;.
7f250 20 20 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 y.M = 1;.
7f260 20 20 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 y.D = 1;.
7f270 20 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 compute
7f280 4a 44 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 JD(&y);.
7f290 20 20 6e 44 61 79 20 3d 20 28 69 6e 74 29 28 28 nDay = (int)((
7f2a0 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b 34 33 32 30 x.iJD-y.iJD+4320
7f2b0 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 3b 0000)/86400000);
7f2c0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a . if( z
7f2d0 46 6d 74 5b 69 5d 3d 3d 27 57 27 20 29 7b 0a 20 Fmt[i]=='W' ){.
7f2e0 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 77 int w
7f2f0 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f 6e 64 61 79 d; /* 0=Monday
7f300 2c 20 31 3d 54 75 65 73 64 61 79 2c 20 2e 2e 2e , 1=Tuesday, ...
7f310 20 36 3d 53 75 6e 64 61 79 20 2a 2f 0a 20 20 20 6=Sunday */.
7f320 20 20 20 20 20 20 20 20 20 77 64 20 3d 20 28 69 wd = (i
7f330 6e 74 29 28 28 28 78 2e 69 4a 44 2b 34 33 32 30 nt)(((x.iJD+4320
7f340 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29 25 0000)/86400000)%
7f350 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7);.
7f360 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
7f370 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 (3, &z[j],"%02d"
7f380 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f 37 29 3b ,(nDay+7-wd)/7);
7f390 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b . j +
7f3a0 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 20 20 7d = 2;. }
7f3b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
7f3c0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
7f3d0 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22 25 30 33 tf(4, &z[j],"%03
7f3e0 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20 20 20 20 d",nDay+1);.
7f3f0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 33 3b 0a j += 3;.
7f400 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
7f410 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7f420 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 }. c
7f430 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20 20 20 20 ase 'J': {.
7f440 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 sqlite3_snp
7f450 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b 6a 5d 2c rintf(20, &z[j],
7f460 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44 2f 38 36 "%.16g",x.iJD/86
7f470 34 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 20 400000.0);.
7f480 20 20 20 20 20 6a 2b 3d 73 71 6c 69 74 65 33 53 j+=sqlite3S
7f490 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a trlen30(&z[j]);.
7f4a0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
7f4b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
7f4c0 20 20 20 63 61 73 65 20 27 6d 27 3a 20 20 73 71 case 'm': sq
7f4d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 lite3_snprintf(3
7f4e0 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 , &z[j],"%02d",x
7f4f0 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b .M); j+=2; break
7f500 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 ;. case '
7f510 4d 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 M': sqlite3_snp
7f520 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 rintf(3, &z[j],"
7f530 25 30 32 64 22 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 %02d",x.m); j+=2
7f540 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 ; break;.
7f550 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20 20 20 case 's': {.
7f560 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 sqlite3_s
7f570 6e 70 72 69 6e 74 66 28 33 30 2c 26 7a 5b 6a 5d nprintf(30,&z[j]
7f580 2c 22 25 6c 6c 64 22 2c 0a 20 20 20 20 20 20 20 ,"%lld",.
7f590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f5a0 20 20 20 20 28 69 36 34 29 28 78 2e 69 4a 44 2f (i64)(x.iJD/
7f5b0 31 30 30 30 20 2d 20 32 31 30 38 36 36 37 36 2a 1000 - 21086676*
7f5c0 28 69 36 34 29 31 30 30 30 30 29 29 3b 0a 20 20 (i64)10000));.
7f5d0 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73 71 6c j += sql
7f5e0 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b ite3Strlen30(&z[
7f5f0 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 j]);. b
7f600 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a reak;. }.
7f610 20 20 20 20 20 20 20 20 63 61 73 65 20 27 53 27 case 'S'
7f620 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 : sqlite3_snpri
7f630 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 ntf(3,&z[j],"%02
7f640 64 22 2c 28 69 6e 74 29 78 2e 73 29 3b 20 6a 2b d",(int)x.s); j+
7f650 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 =2; break;.
7f660 20 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 case 'w': {.
7f670 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 z[j++]
7f680 3d 20 28 63 68 61 72 29 28 28 28 78 2e 69 4a 44 = (char)(((x.iJD
7f690 2b 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 +129600000)/8640
7f6a0 30 30 30 30 29 20 25 20 37 29 20 2b 20 27 30 27 0000) % 7) + '0'
7f6b0 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
7f6c0 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
7f6d0 20 20 20 20 20 63 61 73 65 20 27 59 27 3a 20 7b case 'Y': {
7f6e0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
7f6f0 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 2c 26 7a e3_snprintf(5,&z
7f700 5b 6a 5d 2c 22 25 30 34 64 22 2c 78 2e 59 29 3b [j],"%04d",x.Y);
7f710 20 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 j+=sqlite3Strle
7f720 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 n30(&z[j]);.
7f730 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
7f740 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 }. d
7f750 65 66 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d efault: z[j++]
7f760 20 3d 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 = '%'; break;.
7f770 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
7f780 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 . z[j] = 0;. s
7f790 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 qlite3_result_te
7f7a0 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d xt(context, z, -
7f7b0 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 1,.
7f7c0 20 20 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 z==zBuf
7f7d0 20 3f 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 ? SQLITE_TRANSI
7f7e0 45 4e 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e ENT : SQLITE_DYN
7f7f0 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 AMIC);.}../*.**
7f800 63 75 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a current_time().*
7f810 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
7f820 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 on returns the s
7f830 61 6d 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d ame value as tim
7f840 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 e('now')..*/.sta
7f850 74 69 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 tic void ctimeFu
7f860 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7f870 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7f880 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
7f890 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7f8a0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 *NotUsed2.){. U
7f8b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
7f8c0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
7f8d0 64 32 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 d2);. timeFunc(
7f8e0 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a context, 0, 0);.
7f8f0 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 }../*.** current
7f900 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 _date().**.** Th
7f910 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
7f920 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c rns the same val
7f930 75 65 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 ue as date('now'
7f940 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
7f950 64 20 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 d cdateFunc(. s
7f960 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
7f970 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e context,. int N
7f980 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 otUsed,. sqlite
7f990 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 3_value **NotUse
7f9a0 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 d2.){. UNUSED_P
7f9b0 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 ARAMETER2(NotUse
7f9c0 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 d, NotUsed2);.
7f9d0 64 61 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 dateFunc(context
7f9e0 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a , 0, 0);.}../*.*
7f9f0 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 * current_timest
7fa00 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 amp().**.** This
7fa10 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
7fa20 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 s the same value
7fa30 20 61 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f as datetime('no
7fa40 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 w')..*/.static v
7fa50 6f 69 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 oid ctimestampFu
7fa60 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f nc(. sqlite3_co
7fa70 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a ntext *context,.
7fa80 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 int NotUsed,.
7fa90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
7faa0 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 *NotUsed2.){. U
7fab0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
7fac0 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
7fad0 64 32 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 d2);. datetimeF
7fae0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 unc(context, 0,
7faf0 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 0);.}.#endif /*
7fb00 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
7fb10 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 OMIT_DATETIME_FU
7fb20 4e 43 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 NCS) */..#ifdef
7fb30 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 SQLITE_OMIT_DATE
7fb40 54 49 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a TIME_FUNCS./*.**
7fb50 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 If the library
7fb60 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f is compiled to o
7fb70 6d 69 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 mit the full-sca
7fb80 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 le date and time
7fb90 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f .** handling (to
7fba0 20 67 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 get a smaller b
7fbb0 69 6e 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c inary), the foll
7fbc0 6f 77 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 owing minimal ve
7fbd0 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 rsion.** of the
7fbe0 66 75 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e functions curren
7fbf0 74 5f 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e t_time(), curren
7fc00 74 5f 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 t_date() and cur
7fc10 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 rent_timestamp()
7fc20 0a 2a 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 .** are included
7fc30 20 69 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 instead. This i
7fc40 73 20 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c s to support col
7fc50 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 umn declarations
7fc60 20 74 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 that.** include
7fc70 20 22 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e "DEFAULT CURREN
7fc80 54 5f 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a T_TIME" etc..**.
7fc90 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
7fca0 20 75 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 uses the C-libr
7fcb0 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 ary functions ti
7fcc0 6d 65 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a me(), gmtime().*
7fcd0 2a 20 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 * and strftime()
7fce0 2e 20 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 . The format str
7fcf0 69 6e 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 ing to pass to s
7fd00 74 72 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 trftime() is sup
7fd10 70 6c 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 plied.** as the
7fd20 75 73 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 user-data for th
7fd30 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 e function..*/.s
7fd40 74 61 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 tatic void curre
7fd50 6e 74 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 ntTimeFunc(. sq
7fd60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 lite3_context *c
7fd70 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 ontext,. int ar
7fd80 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 gc,. sqlite3_va
7fd90 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 lue **argv.){.
7fda0 74 69 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 time_t t;. char
7fdb0 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 *zFormat = (cha
7fdc0 72 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 r *)sqlite3_user
7fdd0 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a _data(context);.
7fde0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 sqlite3 *db;.
7fdf0 20 64 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 double rT;. ch
7fe00 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 ar zBuf[20];..
7fe10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
7fe20 28 61 72 67 63 29 3b 0a 20 20 55 4e 55 53 45 44 (argc);. UNUSED
7fe30 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 76 29 _PARAMETER(argv)
7fe40 3b 0a 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 ;.. db = sqlite
7fe50 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 3_context_db_han
7fe60 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 dle(context);.
7fe70 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 sqlite3OsCurrent
7fe80 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 Time(db->pVfs, &
7fe90 72 54 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c rT);.#ifndef SQL
7fea0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e ITE_OMIT_FLOATIN
7feb0 47 5f 50 4f 49 4e 54 0a 20 20 74 20 3d 20 38 36 G_POINT. t = 86
7fec0 34 30 30 2e 30 2a 28 72 54 20 2d 20 32 34 34 30 400.0*(rT - 2440
7fed0 35 38 37 2e 35 29 20 2b 20 30 2e 35 3b 0a 23 65 587.5) + 0.5;.#e
7fee0 6c 73 65 0a 20 20 2f 2a 20 77 69 74 68 6f 75 74 lse. /* without
7fef0 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
7ff00 73 75 70 70 6f 72 74 2c 20 72 54 20 77 69 6c 6c support, rT will
7ff10 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61 have. ** alrea
7ff20 64 79 20 6c 6f 73 74 20 66 72 61 63 74 69 6f 6e dy lost fraction
7ff30 61 6c 20 64 61 79 20 70 72 65 63 69 73 69 6f 6e al day precision
7ff40 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d 20 38 36 34 .. */. t = 864
7ff50 30 30 20 2a 20 28 72 54 20 2d 20 32 34 34 30 35 00 * (rT - 24405
7ff60 38 37 29 20 2d 20 34 33 32 30 30 3b 0a 23 65 6e 87) - 43200;.#en
7ff70 64 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f dif.#ifdef HAVE_
7ff80 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 GMTIME_R. {.
7ff90 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b struct tm sNow;
7ffa0 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74 . gmtime_r(&t
7ffb0 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74 , &sNow);. st
7ffc0 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c rftime(zBuf, 20,
7ffd0 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29 zFormat, &sNow)
7ffe0 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a ;. }.#else. {.
7fff0 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 struct tm *p
80000 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f Tm;. sqlite3_
80010 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 mutex_enter(sqli
80020 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 te3MutexAlloc(SQ
80030 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
80040 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 C_MASTER));.
80050 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29 pTm = gmtime(&t)
80060 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a ;. strftime(z
80070 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74 Buf, 20, zFormat
80080 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69 , pTm);. sqli
80090 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
800a0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
800b0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
800c0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a TATIC_MASTER));.
800d0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 }.#endif.. sq
800e0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
800f0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c t(context, zBuf,
80100 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e -1, SQLITE_TRAN
80110 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 SIENT);.}.#endif
80120 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
80130 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 ction registered
80140 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 all of the abov
80150 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 e C functions as
80160 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e SQL.** function
80170 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 s. This should
80180 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 be the only rout
80190 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 ine in this file
801a0 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61 with.** externa
801b0 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51 l linkage..*/.SQ
801c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
801d0 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 d sqlite3Registe
801e0 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f rDateTimeFunctio
801f0 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 ns(void){. stat
80200 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 ic SQLITE_WSD Fu
80210 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46 ncDef aDateTimeF
80220 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64 uncs[] = {.#ifnd
80230 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
80240 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20 ATETIME_FUNCS.
80250 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61 FUNCTION(julia
80260 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c nday, -1,
80270 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79 0, 0, julianday
80280 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43 Func ),. FUNC
80290 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20 TION(date,
802a0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c -1, 0, 0,
802b0 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29 dateFunc )
802c0 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 ,. FUNCTION(t
802d0 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 ime,
802e0 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46 -1, 0, 0, timeF
802f0 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 unc ),.
80300 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d FUNCTION(datetim
80310 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 e, -1, 0
80320 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e , 0, datetimeFun
80330 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 c ),. FUNCTI
80340 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20 ON(strftime,
80350 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73 -1, 0, 0, s
80360 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a trftimeFunc ),.
80370 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 FUNCTION(cur
80380 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20 rent_time,
80390 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75 0, 0, 0, ctimeFu
803a0 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 nc ),. FU
803b0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 NCTION(current_t
803c0 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20 imestamp, 0, 0,
803d0 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 0, ctimestampFun
803e0 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e c),. FUNCTION
803f0 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20 (current_date,
80400 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61 0, 0, 0, cda
80410 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65 teFunc ),.#e
80420 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 lse. STR_FUNC
80430 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d TION(current_tim
80440 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25 e, 0, "%H:%
80450 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20 M:%S",
80460 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75 0, currentTimeFu
80470 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e nc),. STR_FUN
80480 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 CTION(current_ti
80490 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d mestamp, 0, "%Y-
804a0 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20 %m-%d",
804b0 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 0, currentTimeF
804c0 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 unc),. STR_FU
804d0 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 NCTION(current_d
804e0 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59 ate, 0, "%Y
804f0 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22 -%m-%d %H:%M:%S"
80500 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 , 0, currentTime
80510 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20 Func),.#endif.
80520 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75 };. int i;. Fu
80530 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 ncDefHash *pHash
80540 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 = &GLOBAL(FuncD
80550 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47 efHash, sqlite3G
80560 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b lobalFunctions);
80570 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e . FuncDef *aFun
80580 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47 c = (FuncDef*)&G
80590 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61 LOBAL(FuncDef, a
805a0 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a DateTimeFuncs);.
805b0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 . for(i=0; i<Ar
805c0 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d raySize(aDateTim
805d0 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20 eFuncs); i++){.
805e0 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 sqlite3FuncDe
805f0 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 fInsert(pHash, &
80600 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d aFunc[i]);. }.}
80610 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
80620 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20 * End of date.c
80630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80660 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /./*************
80670 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e * Begin file os.
80680 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
80690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
806a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
806b0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76 /./*.** 2005 Nov
806c0 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54 ember 29.**.** T
806d0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 he author discla
806e0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f ims copyright to
806f0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 this source cod
80700 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a e. In place of.
80710 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 ** a legal notic
80720 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 e, here is a ble
80730 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 ssing:.**.**
80740 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 May you do good
80750 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a and not evil..**
80760 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 May you find
80770 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 forgiveness for
80780 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f yourself and fo
80790 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a rgive others..**
807a0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 May you shar
807b0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 e freely, never
807c0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e taking more than
807d0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a you give..**.**
807e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
807f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
80820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
80830 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
80840 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61 tains OS interfa
80850 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ce code that is
80860 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a common to all.**
80870 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a architectures..
80880 2a 2f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 */.#define _SQLI
80890 54 45 5f 4f 53 5f 43 5f 20 31 0a 23 75 6e 64 65 TE_OS_C_ 1.#unde
808a0 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 0a f _SQLITE_OS_C_.
808b0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 ./*.** The defau
808c0 6c 74 20 53 51 4c 69 74 65 20 73 71 6c 69 74 65 lt SQLite sqlite
808d0 33 5f 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61 3_vfs implementa
808e0 74 69 6f 6e 73 20 64 6f 20 6e 6f 74 20 61 6c 6c tions do not all
808f0 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 ocate.** memory
80900 28 61 63 74 75 61 6c 6c 79 2c 20 6f 73 5f 75 6e (actually, os_un
80910 69 78 2e 63 20 61 6c 6c 6f 63 61 74 65 73 20 61 ix.c allocates a
80920 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 small amount of
80930 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66 72 6f 6d 20 memory.** from
80940 77 69 74 68 69 6e 20 4f 73 4f 70 65 6e 28 29 29 within OsOpen())
80950 2c 20 62 75 74 20 73 6f 6d 65 20 74 68 69 72 64 , but some third
80960 2d 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 -party implement
80970 61 74 69 6f 6e 73 20 6d 61 79 2e 0a 2a 2a 20 53 ations may..** S
80980 6f 20 77 65 20 74 65 73 74 20 74 68 65 20 65 66 o we test the ef
80990 66 65 63 74 73 20 6f 66 20 61 20 6d 61 6c 6c 6f fects of a mallo
809a0 63 28 29 20 66 61 69 6c 69 6e 67 20 61 6e 64 20 c() failing and
809b0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 58 58 58 the sqlite3OsXXX
809c0 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 ().** function r
809d0 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f eturning SQLITE_
809e0 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 75 73 69 6e IOERR_NOMEM usin
809f0 67 20 74 68 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c g the DO_OS_MALL
80a00 4f 43 5f 54 45 53 54 20 6d 61 63 72 6f 2e 0a 2a OC_TEST macro..*
80a10 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
80a20 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 ng functions are
80a30 20 69 6e 73 74 72 75 6d 65 6e 74 65 64 20 66 6f instrumented fo
80a40 72 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 r malloc() failu
80a50 72 65 20 0a 2a 2a 20 74 65 73 74 69 6e 67 3a 0a re .** testing:.
80a60 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 **.** sqlite
80a70 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 3OsOpen().**
80a80 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 29 sqlite3OsRead()
80a90 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f .** sqlite3O
80aa0 73 57 72 69 74 65 28 29 0a 2a 2a 20 20 20 20 20 sWrite().**
80ab0 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 0a sqlite3OsSync().
80ac0 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 ** sqlite3Os
80ad0 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a 2f 0a 23 69 66 Lock().**.*/.#if
80ae0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
80af0 54 45 53 54 29 20 26 26 20 28 53 51 4c 49 54 45 TEST) && (SQLITE
80b00 5f 4f 53 5f 57 49 4e 3d 3d 30 29 0a 20 20 23 64 _OS_WIN==0). #d
80b10 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c efine DO_OS_MALL
80b20 4f 43 5f 54 45 53 54 28 78 29 20 69 66 20 28 21 OC_TEST(x) if (!
80b30 78 20 7c 7c 20 21 73 71 6c 69 74 65 33 49 73 4d x || !sqlite3IsM
80b40 65 6d 4a 6f 75 72 6e 61 6c 28 78 29 29 20 7b 20 emJournal(x)) {
80b50 20 20 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a \. void *
80b60 70 54 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 pTstAlloc = sqli
80b70 74 65 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 te3Malloc(10);
80b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80b90 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
80ba0 20 69 66 20 28 21 70 54 73 74 41 6c 6c 6f 63 29 if (!pTstAlloc)
80bb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
80bc0 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20 20 20 20 20 OERR_NOMEM;
80bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80be0 20 20 5c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f \. sqlite3_
80bf0 66 72 65 65 28 70 54 73 74 41 6c 6c 6f 63 29 3b free(pTstAlloc);
80c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
80c20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 \. }.#
80c30 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 else. #define D
80c40 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 O_OS_MALLOC_TEST
80c50 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (x).#endif../*.*
80c60 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
80c70 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e routines are con
80c80 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 venience wrapper
80c90 73 20 61 72 6f 75 6e 64 20 6d 65 74 68 6f 64 73 s around methods
80ca0 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 .** of the sqlit
80cb0 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 20 e3_file object.
80cc0 20 54 68 69 73 20 69 73 20 6d 6f 73 74 6c 79 20 This is mostly
80cd0 6a 75 73 74 20 73 79 6e 74 61 63 74 69 63 20 73 just syntactic s
80ce0 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a 20 6f 66 20 ugar. All.** of
80cf0 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 63 6f this would be co
80d00 6d 70 6c 65 74 65 6c 79 20 61 75 74 6f 6d 61 74 mpletely automat
80d10 69 63 20 69 66 20 53 51 4c 69 74 65 20 77 65 72 ic if SQLite wer
80d20 65 20 63 6f 64 65 64 20 75 73 69 6e 67 0a 2a 2a e coded using.**
80d30 20 43 2b 2b 20 69 6e 73 74 65 61 64 20 6f 66 20 C++ instead of
80d40 70 6c 61 69 6e 20 6f 6c 64 20 43 2e 0a 2a 2f 0a plain old C..*/.
80d50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
80d60 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 nt sqlite3OsClos
80d70 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
80d80 70 49 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d pId){. int rc =
80d90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
80da0 28 20 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 ( pId->pMethods
80db0 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 49 64 2d ){. rc = pId-
80dc0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 43 6c 6f 73 >pMethods->xClos
80dd0 65 28 70 49 64 29 3b 0a 20 20 20 20 70 49 64 2d e(pId);. pId-
80de0 3e 70 4d 65 74 68 6f 64 73 20 3d 20 30 3b 0a 20 >pMethods = 0;.
80df0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
80e00 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
80e10 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 65 int sqlite3OsRe
80e20 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ad(sqlite3_file
80e30 2a 69 64 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c *id, void *pBuf,
80e40 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 int amt, i64 of
80e50 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d fset){. DO_OS_M
80e60 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a ALLOC_TEST(id);.
80e70 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
80e80 74 68 6f 64 73 2d 3e 78 52 65 61 64 28 69 64 2c thods->xRead(id,
80e90 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 pBuf, amt, offs
80ea0 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 et);.}.SQLITE_PR
80eb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
80ec0 33 4f 73 57 72 69 74 65 28 73 71 6c 69 74 65 33 3OsWrite(sqlite3
80ed0 5f 66 69 6c 65 20 2a 69 64 2c 20 63 6f 6e 73 74 _file *id, const
80ee0 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 void *pBuf, int
80ef0 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 amt, i64 offset
80f00 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
80f10 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
80f20 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
80f30 73 2d 3e 78 57 72 69 74 65 28 69 64 2c 20 70 42 s->xWrite(id, pB
80f40 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73 65 74 29 uf, amt, offset)
80f50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80f60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80f70 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 Truncate(sqlite3
80f80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 73 _file *id, i64 s
80f90 69 7a 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 ize){. return i
80fa0 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 54 72 d->pMethods->xTr
80fb0 75 6e 63 61 74 65 28 69 64 2c 20 73 69 7a 65 29 uncate(id, size)
80fc0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 ;.}.SQLITE_PRIVA
80fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
80fe0 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c Sync(sqlite3_fil
80ff0 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 e *id, int flags
81000 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f ){. DO_OS_MALLO
81010 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65 C_TEST(id);. re
81020 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 turn id->pMethod
81030 73 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 s->xSync(id, fla
81040 67 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 gs);.}.SQLITE_PR
81050 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
81060 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 3OsFileSize(sqli
81070 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 te3_file *id, i6
81080 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4 *pSize){. DO_
81090 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 OS_MALLOC_TEST(i
810a0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d d);. return id-
810b0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 >pMethods->xFile
810c0 53 69 7a 65 28 69 64 2c 20 70 53 69 7a 65 29 3b Size(id, pSize);
810d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
810e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4c E int sqlite3OsL
810f0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
81100 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 *id, int lockTy
81110 70 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c pe){. DO_OS_MAL
81120 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 LOC_TEST(id);.
81130 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 return id->pMeth
81140 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69 64 2c 20 6c ods->xLock(id, l
81150 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 ockType);.}.SQLI
81160 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
81170 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 qlite3OsUnlock(s
81180 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
81190 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a int lockType){.
811a0 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 return id->pMe
811b0 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f 63 6b 28 69 thods->xUnlock(i
811c0 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a d, lockType);.}.
811d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
811e0 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 nt sqlite3OsChec
811f0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 kReservedLock(sq
81200 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
81210 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 int *pResOut){.
81220 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
81230 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e ST(id);. return
81240 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 id->pMethods->x
81250 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
81260 6b 28 69 64 2c 20 70 52 65 73 4f 75 74 29 3b 0a k(id, pResOut);.
81270 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
81280 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 int sqlite3OsFi
81290 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 leControl(sqlite
812a0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
812b0 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b op, void *pArg){
812c0 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d . return id->pM
812d0 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65 43 6f 6e ethods->xFileCon
812e0 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20 70 41 72 trol(id, op, pAr
812f0 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 g);.}.SQLITE_PRI
81300 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
81310 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c OsSectorSize(sql
81320 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
81330 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 int (*xSectorS
81340 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c ize)(sqlite3_fil
81350 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65 74 68 6f e*) = id->pMetho
81360 64 73 2d 3e 78 53 65 63 74 6f 72 53 69 7a 65 3b ds->xSectorSize;
81370 0a 20 20 72 65 74 75 72 6e 20 28 78 53 65 63 74 . return (xSect
81380 6f 72 53 69 7a 65 20 3f 20 78 53 65 63 74 6f 72 orSize ? xSector
81390 53 69 7a 65 28 69 64 29 20 3a 20 53 51 4c 49 54 Size(id) : SQLIT
813a0 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
813b0 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 _SIZE);.}.SQLITE
813c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
813d0 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 ite3OsDeviceChar
813e0 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 acteristics(sqli
813f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 te3_file *id){.
81400 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 return id->pMet
81410 68 6f 64 73 2d 3e 78 44 65 76 69 63 65 43 68 61 hods->xDeviceCha
81420 72 61 63 74 65 72 69 73 74 69 63 73 28 69 64 29 racteristics(id)
81430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e ;.}../*.** The n
81440 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 ext group of rou
81450 74 69 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e tines are conven
81460 69 65 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 ience wrappers a
81470 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20 56 46 53 round the.** VFS
81480 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c methods..*/.SQL
81490 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
814a0 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 0a 20 sqlite3OsOpen(.
814b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 sqlite3_vfs *pV
814c0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 fs, . const cha
814d0 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c r *zPath, . sql
814e0 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 ite3_file *pFile
814f0 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 , . int flags,
81500 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 4f 75 . int *pFlagsOu
81510 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 t.){. int rc;.
81520 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 DO_OS_MALLOC_TE
81530 53 54 28 30 29 3b 0a 20 20 2f 2a 20 30 78 37 66 ST(0);. /* 0x7f
81540 31 66 20 69 73 20 61 20 6d 61 73 6b 20 6f 66 20 1f is a mask of
81550 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 SQLITE_OPEN_ fla
81560 67 73 20 74 68 61 74 20 61 72 65 20 76 61 6c 69 gs that are vali
81570 64 20 74 6f 20 62 65 20 70 61 73 73 65 64 0a 20 d to be passed.
81580 20 2a 2a 20 64 6f 77 6e 20 69 6e 74 6f 20 74 68 ** down into th
81590 65 20 56 46 53 20 6c 61 79 65 72 2e 20 20 53 6f e VFS layer. So
815a0 6d 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 20 me SQLITE_OPEN_
815b0 66 6c 61 67 73 20 28 66 6f 72 20 65 78 61 6d 70 flags (for examp
815c0 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f le,. ** SQLITE_
815d0 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 6f OPEN_FULLMUTEX o
815e0 72 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 r SQLITE_OPEN_SH
815f0 41 52 45 44 43 41 43 48 45 29 20 61 72 65 20 62 AREDCACHE) are b
81600 6c 6f 63 6b 65 64 20 62 65 66 6f 72 65 0a 20 20 locked before.
81610 2a 2a 20 72 65 61 63 68 69 6e 67 20 74 68 65 20 ** reaching the
81620 56 46 53 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 70 VFS. */. rc = p
81630 56 66 73 2d 3e 78 4f 70 65 6e 28 70 56 66 73 2c Vfs->xOpen(pVfs,
81640 20 7a 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 66 zPath, pFile, f
81650 6c 61 67 73 20 26 20 30 78 37 66 31 66 2c 20 70 lags & 0x7f1f, p
81660 46 6c 61 67 73 4f 75 74 29 3b 0a 20 20 61 73 73 FlagsOut);. ass
81670 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
81680 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d 65 OK || pFile->pMe
81690 74 68 6f 64 73 3d 3d 30 20 29 3b 0a 20 20 72 65 thods==0 );. re
816a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
816b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
816c0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71 lite3OsDelete(sq
816d0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
816e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 const char *zPa
816f0 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29 th, int dirSync)
81700 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
81710 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a >xDelete(pVfs, z
81720 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a Path, dirSync);.
81730 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
81740 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 int sqlite3OsAc
81750 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f cess(. sqlite3_
81760 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f vfs *pVfs, . co
81770 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c nst char *zPath,
81780 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a . int flags, .
81790 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29 int *pResOut.)
817a0 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 {. DO_OS_MALLOC
817b0 5f 54 45 53 54 28 30 29 3b 0a 20 20 72 65 74 75 _TEST(0);. retu
817c0 72 6e 20 70 56 66 73 2d 3e 78 41 63 63 65 73 73 rn pVfs->xAccess
817d0 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c (pVfs, zPath, fl
817e0 61 67 73 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d ags, pResOut);.}
817f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81800 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c int sqlite3OsFul
81810 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c lPathname(. sql
81820 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
81830 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
81840 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 Path, . int nPa
81850 74 68 4f 75 74 2c 20 0a 20 20 63 68 61 72 20 2a thOut, . char *
81860 7a 50 61 74 68 4f 75 74 0a 29 7b 0a 20 20 7a 50 zPathOut.){. zP
81870 61 74 68 4f 75 74 5b 30 5d 20 3d 20 30 3b 0a 20 athOut[0] = 0;.
81880 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 46 return pVfs->xF
81890 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 ullPathname(pVfs
818a0 2c 20 7a 50 61 74 68 2c 20 6e 50 61 74 68 4f 75 , zPath, nPathOu
818b0 74 2c 20 7a 50 61 74 68 4f 75 74 29 3b 0a 7d 0a t, zPathOut);.}.
818c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
818d0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
818e0 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 ON.SQLITE_PRIVAT
818f0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f E void *sqlite3O
81900 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f sDlOpen(sqlite3_
81910 76 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 vfs *pVfs, const
81920 20 63 68 61 72 20 2a 7a 50 61 74 68 29 7b 0a 20 char *zPath){.
81930 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 44 return pVfs->xD
81940 6c 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 lOpen(pVfs, zPat
81950 68 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 h);.}.SQLITE_PRI
81960 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
81970 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 3OsDlError(sqlit
81980 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
81990 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a t nByte, char *z
819a0 42 75 66 4f 75 74 29 7b 0a 20 20 70 56 66 73 2d BufOut){. pVfs-
819b0 3e 78 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 >xDlError(pVfs,
819c0 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b nByte, zBufOut);
819d0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
819e0 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 E void (*sqlite3
819f0 4f 73 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f OsDlSym(sqlite3_
81a00 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 vfs *pVfs, void
81a10 2a 70 48 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 *pHdle, const ch
81a20 61 72 20 2a 7a 53 79 6d 29 29 28 76 6f 69 64 29 ar *zSym))(void)
81a30 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d {. return pVfs-
81a40 3e 78 44 6c 53 79 6d 28 70 56 66 73 2c 20 70 48 >xDlSym(pVfs, pH
81a50 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 53 51 dle, zSym);.}.SQ
81a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
81a70 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f d sqlite3OsDlClo
81a80 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
81a90 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e pVfs, void *pHan
81aa0 64 6c 65 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 dle){. pVfs->xD
81ab0 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 70 48 61 lClose(pVfs, pHa
81ac0 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 ndle);.}.#endif
81ad0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c /* SQLITE_OMIT_L
81ae0 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f OAD_EXTENSION */
81af0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81b00 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e int sqlite3OsRan
81b10 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f domness(sqlite3_
81b20 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
81b30 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 Byte, char *zBuf
81b40 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 Out){. return p
81b50 56 66 73 2d 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 Vfs->xRandomness
81b60 28 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 (pVfs, nByte, zB
81b70 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 ufOut);.}.SQLITE
81b80 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
81b90 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 ite3OsSleep(sqli
81ba0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
81bb0 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a 20 20 72 65 nt nMicro){. re
81bc0 74 75 72 6e 20 70 56 66 73 2d 3e 78 53 6c 65 65 turn pVfs->xSlee
81bd0 70 28 70 56 66 73 2c 20 6e 4d 69 63 72 6f 29 3b p(pVfs, nMicro);
81be0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
81bf0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 E int sqlite3OsC
81c00 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
81c10 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f e3_vfs *pVfs, do
81c20 75 62 6c 65 20 2a 70 54 69 6d 65 4f 75 74 29 7b uble *pTimeOut){
81c30 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e . return pVfs->
81c40 78 43 75 72 72 65 6e 74 54 69 6d 65 28 70 56 66 xCurrentTime(pVf
81c50 73 2c 20 70 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a s, pTimeOut);.}.
81c60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
81c70 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 int sqlite3OsOpe
81c80 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73 71 6c 69 74 nMalloc(. sqlit
81c90 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 e3_vfs *pVfs, .
81ca0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 const char *zFi
81cb0 6c 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 le, . sqlite3_f
81cc0 69 6c 65 20 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 ile **ppFile, .
81cd0 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69 6e int flags,. in
81ce0 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b 0a t *pOutFlags.){.
81cf0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
81d00 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 E_NOMEM;. sqlit
81d10 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 3b 0a e3_file *pFile;.
81d20 20 20 70 46 69 6c 65 20 3d 20 28 73 71 6c 69 74 pFile = (sqlit
81d30 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74 65 e3_file *)sqlite
81d40 33 4d 61 6c 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 3Malloc(pVfs->sz
81d50 4f 73 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 OsFile);. if( p
81d60 46 69 6c 65 20 29 7b 0a 20 20 20 20 72 63 20 3d File ){. rc =
81d70 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 sqlite3OsOpen(p
81d80 56 66 73 2c 20 7a 46 69 6c 65 2c 20 70 46 69 6c Vfs, zFile, pFil
81d90 65 2c 20 66 6c 61 67 73 2c 20 70 4f 75 74 46 6c e, flags, pOutFl
81da0 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 ags);. if( rc
81db0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
81dc0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
81dd0 65 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 65 e(pFile);. }e
81de0 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 70 46 69 lse{. *ppFi
81df0 6c 65 20 3d 20 70 46 69 6c 65 3b 0a 20 20 20 20 le = pFile;.
81e00 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 }. }. return r
81e10 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 c;.}.SQLITE_PRIV
81e20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f ATE int sqlite3O
81e30 73 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 sCloseFree(sqlit
81e40 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 29 7b e3_file *pFile){
81e50 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
81e60 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 TE_OK;. assert(
81e70 20 70 46 69 6c 65 20 29 3b 0a 20 20 72 63 20 3d pFile );. rc =
81e80 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
81e90 70 46 69 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 pFile);. sqlite
81ea0 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 3_free(pFile);.
81eb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
81ec0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
81ed0 6f 6e 20 69 73 20 61 20 77 72 61 70 70 65 72 20 on is a wrapper
81ee0 61 72 6f 75 6e 64 20 74 68 65 20 4f 53 20 73 70 around the OS sp
81ef0 65 63 69 66 69 63 20 69 6d 70 6c 65 6d 65 6e 74 ecific implement
81f00 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 73 71 6c 69 ation of.** sqli
81f10 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 54 te3_os_init(). T
81f20 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 he purpose of th
81f30 65 20 77 72 61 70 70 65 72 20 69 73 20 74 6f 20 e wrapper is to
81f40 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20 61 provide the.** a
81f50 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 bility to simula
81f60 74 65 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c te a malloc fail
81f70 75 72 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 ure, so that the
81f80 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 61 6e 0a handling of an.
81f90 2a 2a 20 65 72 72 6f 72 20 69 6e 20 73 71 6c 69 ** error in sqli
81fa0 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 62 79 te3_os_init() by
81fb0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 the upper layer
81fc0 73 20 63 61 6e 20 62 65 20 74 65 73 74 65 64 2e s can be tested.
81fd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
81fe0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 TE int sqlite3Os
81ff0 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 76 6f Init(void){. vo
82000 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f id *p = sqlite3_
82010 6d 61 6c 6c 6f 63 28 31 30 29 3b 0a 20 20 69 66 malloc(10);. if
82020 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ( p==0 ) return
82030 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
82040 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
82050 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
82060 33 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 7d 0a 0a 3_os_init();.}..
82070 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69 73 74 20 6f /*.** The list o
82080 66 20 61 6c 6c 20 72 65 67 69 73 74 65 72 65 64 f all registered
82090 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 VFS implementat
820a0 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ions..*/.static
820b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20 53 51 sqlite3_vfs * SQ
820c0 4c 49 54 45 5f 57 53 44 20 76 66 73 4c 69 73 74 LITE_WSD vfsList
820d0 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 76 66 = 0;.#define vf
820e0 73 4c 69 73 74 20 47 4c 4f 42 41 4c 28 73 71 6c sList GLOBAL(sql
820f0 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 66 73 4c ite3_vfs *, vfsL
82100 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 ist)../*.** Loca
82110 74 65 20 61 20 56 46 53 20 62 79 20 6e 61 6d 65 te a VFS by name
82120 2e 20 20 49 66 20 6e 6f 20 6e 61 6d 65 20 69 73 . If no name is
82130 20 67 69 76 65 6e 2c 20 73 69 6d 70 6c 79 20 72 given, simply r
82140 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 66 69 72 eturn the.** fir
82150 73 74 20 56 46 53 20 6f 6e 20 74 68 65 20 6c 69 st VFS on the li
82160 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 st..*/.SQLITE_AP
82170 49 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 I sqlite3_vfs *s
82180 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 qlite3_vfs_find(
82190 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 const char *zVfs
821a0 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 ){. sqlite3_vfs
821b0 20 2a 70 56 66 73 20 3d 20 30 3b 0a 23 69 66 20 *pVfs = 0;.#if
821c0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
821d0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
821e0 78 20 2a 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 x *mutex;.#endif
821f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
82200 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 OMIT_AUTOINIT.
82210 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
82220 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 _initialize();.
82230 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
82240 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 0;.#endif.#if S
82250 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 QLITE_THREADSAFE
82260 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 . mutex = sqlit
82270 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
82280 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
82290 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 _MASTER);.#endif
822a0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
822b0 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 _enter(mutex);.
822c0 20 66 6f 72 28 70 56 66 73 20 3d 20 76 66 73 4c for(pVfs = vfsL
822d0 69 73 74 3b 20 70 56 66 73 3b 20 70 56 66 73 3d ist; pVfs; pVfs=
822e0 70 56 66 73 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 pVfs->pNext){.
822f0 20 20 69 66 28 20 7a 56 66 73 3d 3d 30 20 29 20 if( zVfs==0 )
82300 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 break;. if( s
82310 74 72 63 6d 70 28 7a 56 66 73 2c 20 70 56 66 73 trcmp(zVfs, pVfs
82320 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 ->zName)==0 ) br
82330 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 eak;. }. sqlit
82340 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
82350 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 utex);. return
82360 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 pVfs;.}../*.** U
82370 6e 6c 69 6e 6b 20 61 20 56 46 53 20 66 72 6f 6d nlink a VFS from
82380 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 the linked list
82390 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
823a0 76 66 73 55 6e 6c 69 6e 6b 28 73 71 6c 69 74 65 vfsUnlink(sqlite
823b0 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 3_vfs *pVfs){.
823c0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
823d0 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 mutex_held(sqlit
823e0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
823f0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
82400 5f 4d 41 53 54 45 52 29 29 20 29 3b 0a 20 20 69 _MASTER)) );. i
82410 66 28 20 70 56 66 73 3d 3d 30 20 29 7b 0a 20 20 f( pVfs==0 ){.
82420 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 /* No-op */.
82430 7d 65 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 }else if( vfsLis
82440 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 76 t==pVfs ){. v
82450 66 73 4c 69 73 74 20 3d 20 70 56 66 73 2d 3e 70 fsList = pVfs->p
82460 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 Next;. }else if
82470 28 20 76 66 73 4c 69 73 74 20 29 7b 0a 20 20 20 ( vfsList ){.
82480 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 20 sqlite3_vfs *p
82490 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 77 = vfsList;. w
824a0 68 69 6c 65 28 20 70 2d 3e 70 4e 65 78 74 20 26 hile( p->pNext &
824b0 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 56 66 73 & p->pNext!=pVfs
824c0 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d ){. p = p-
824d0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 >pNext;. }.
824e0 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 3d 3d if( p->pNext==
824f0 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 2d pVfs ){. p-
82500 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 2d 3e 70 >pNext = pVfs->p
82510 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Next;. }. }.
82520 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 }../*.** Registe
82530 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68 65 r a VFS with the
82540 20 73 79 73 74 65 6d 2e 20 20 49 74 20 69 73 20 system. It is
82550 68 61 72 6d 6c 65 73 73 20 74 6f 20 72 65 67 69 harmless to regi
82560 73 74 65 72 20 74 68 65 20 73 61 6d 65 0a 2a 2a ster the same.**
82570 20 56 46 53 20 6d 75 6c 74 69 70 6c 65 20 74 69 VFS multiple ti
82580 6d 65 73 2e 20 20 54 68 65 20 6e 65 77 20 56 46 mes. The new VF
82590 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65 S becomes the de
825a0 66 61 75 6c 74 20 69 66 20 6d 61 6b 65 44 66 6c fault if makeDfl
825b0 74 20 69 73 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f t is.** true..*/
825c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
825d0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 sqlite3_vfs_regi
825e0 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 ster(sqlite3_vfs
825f0 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 61 6b 65 *pVfs, int make
82600 44 66 6c 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 Dflt){. sqlite3
82610 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
82620 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 0;.#ifndef SQLIT
82630 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
82640 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 int rc = sqlit
82650 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b e3_initialize();
82660 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 . if( rc ) retu
82670 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 rn rc;.#endif.
82680 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
82690 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
826a0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
826b0 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 STER);. sqlite3
826c0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 _mutex_enter(mut
826d0 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b ex);. vfsUnlink
826e0 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 6d 61 (pVfs);. if( ma
826f0 6b 65 44 66 6c 74 20 7c 7c 20 76 66 73 4c 69 73 keDflt || vfsLis
82700 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 56 66 73 t==0 ){. pVfs
82710 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 ->pNext = vfsLis
82720 74 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d t;. vfsList =
82730 20 70 56 66 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a pVfs;. }else{.
82740 20 20 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 pVfs->pNext
82750 3d 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 = vfsList->pNext
82760 3b 0a 20 20 20 20 76 66 73 4c 69 73 74 2d 3e 70 ;. vfsList->p
82770 4e 65 78 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d Next = pVfs;. }
82780 0a 20 20 61 73 73 65 72 74 28 76 66 73 4c 69 73 . assert(vfsLis
82790 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 t);. sqlite3_mu
827a0 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 tex_leave(mutex)
827b0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
827c0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 E_OK;.}../*.** U
827d0 6e 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 nregister a VFS
827e0 73 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f so that it is no
827f0 20 6c 6f 6e 67 65 72 20 61 63 63 65 73 73 69 62 longer accessib
82800 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
82810 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 I int sqlite3_vf
82820 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c s_unregister(sql
82830 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b ite3_vfs *pVfs){
82840 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 .#if SQLITE_THRE
82850 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 ADSAFE. sqlite3
82860 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 _mutex *mutex =
82870 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f sqlite3MutexAllo
82880 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 c(SQLITE_MUTEX_S
82890 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 TATIC_MASTER);.#
828a0 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f endif. sqlite3_
828b0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 mutex_enter(mute
828c0 78 29 3b 0a 20 20 76 66 73 55 6e 6c 69 6e 6b 28 x);. vfsUnlink(
828d0 70 56 66 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 pVfs);. sqlite3
828e0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 _mutex_leave(mut
828f0 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ex);. return SQ
82900 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a LITE_OK;.}../***
82910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
82920 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a of os.c ********
82930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
82960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
82970 6e 20 66 69 6c 65 20 66 61 75 6c 74 2e 63 20 2a n file fault.c *
82980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
829a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
829b0 2a 20 32 30 30 38 20 4a 61 6e 20 32 32 0a 2a 2a * 2008 Jan 22.**
829c0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
829d0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
829e0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
829f0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
82a00 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
82a10 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
82a20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
82a30 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
82a40 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
82a50 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
82a60 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
82a70 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
82a80 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
82a90 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
82aa0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
82ab0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
82ac0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
82ad0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
82ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
82b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
82b20 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
82b30 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 73 75 tains code to su
82b40 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63 65 70 pport the concep
82b50 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20 0a 2a t of "benign" .*
82b60 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 * malloc failure
82b70 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d 61 6c s (when the xMal
82b80 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c 6c 6f loc() or xReallo
82b90 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 c() method of th
82ba0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d e.** sqlite3_mem
82bb0 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 _methods structu
82bc0 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f re fails to allo
82bd0 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 cate a block of
82be0 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20 72 65 memory.** and re
82bf0 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a 2a 2a turns 0). .**.**
82c00 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66 61 69 Most malloc fai
82c10 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d 62 65 lures are non-be
82c20 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68 65 79 nign. After they
82c30 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65 0a 2a occur, SQLite.*
82c40 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65 20 63 * abandons the c
82c50 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e urrent operation
82c60 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 and returns an
82c70 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 error code (usua
82c80 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f lly.** SQLITE_NO
82c90 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73 65 72 MEM) to the user
82ca0 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d 65 74 . However, somet
82cb0 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69 73 20 imes a fault is
82cc0 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a not necessarily.
82cd0 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20 65 78 ** fatal. For ex
82ce0 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61 6c 6c ample, if a mall
82cf0 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 72 oc fails while r
82d00 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68 20 74 esizing a hash t
82d10 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a 20 69 able, this .** i
82d20 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72 65 63 s completely rec
82d30 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c 79 20 overable simply
82d40 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e 67 20 by not carrying
82d50 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65 2e 20 out the resize.
82d60 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 The .** hash tab
82d70 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 le will continue
82d80 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e 6f 72 to function nor
82d90 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d 61 6c mally. So a mal
82da0 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 loc failure .**
82db0 64 75 72 69 6e 67 20 61 20 68 61 73 68 20 74 61 during a hash ta
82dc0 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20 61 20 ble resize is a
82dd0 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a 2a 2f benign fault..*/
82de0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
82df0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 E_OMIT_BUILTIN_T
82e00 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 EST../*.** Globa
82e10 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2f 0a l variables..*/.
82e20 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
82e30 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
82e40 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f BenignMallocHoo
82e50 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 ks;.static SQLIT
82e60 45 5f 57 53 44 20 73 74 72 75 63 74 20 42 65 6e E_WSD struct Ben
82e70 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 7b ignMallocHooks {
82e80 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 . void (*xBenig
82e90 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b 0a 20 nBegin)(void);.
82ea0 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 45 void (*xBenignE
82eb0 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73 71 6c nd)(void);.} sql
82ec0 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20 30 2c ite3Hooks = { 0,
82ed0 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 0 };../* The "w
82ee0 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f 20 77 sdHooks" macro w
82ef0 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 ill resolve to t
82f00 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 42 he appropriate B
82f10 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 enignMallocHooks
82f20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 .** structure.
82f30 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 If writable stat
82f40 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 ic data is unsup
82f50 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 ported on the ta
82f60 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 rget,.** we have
82f70 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 to locate the s
82f80 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 tate vector at r
82f90 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 un-time. In the
82fa0 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 more common.**
82fb0 63 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 case where writa
82fc0 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 ble static data
82fd0 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 is supported, ws
82fe0 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66 65 72 dHooks can refer
82ff0 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 directly.** to
83000 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f 6f 6b the "sqlite3Hook
83010 73 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 s" state vector
83020 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a declared above..
83030 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
83040 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 _OMIT_WSD.# defi
83050 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 20 ne wsdHooksInit
83060 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 \. BenignMalloc
83070 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c 4f 42 Hooks *x = &GLOB
83080 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 AL(BenignMallocH
83090 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f 6f 6b ooks,sqlite3Hook
830a0 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 s).# define wsdH
830b0 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73 65 0a ooks x[0].#else.
830c0 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b # define wsdHook
830d0 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 sInit.# define w
830e0 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65 33 48 sdHooks sqlite3H
830f0 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a ooks.#endif.../*
83100 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6f .** Register hoo
83110 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e 20 ks to call when
83120 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
83130 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a gnMalloc() and.*
83140 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 * sqlite3EndBeni
83150 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65 20 63 gnMalloc() are c
83160 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74 69 76 alled, respectiv
83170 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ely..*/.SQLITE_P
83180 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
83190 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 te3BenignMallocH
831a0 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28 2a 78 ooks(. void (*x
831b0 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 BenignBegin)(voi
831c0 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 d),. void (*xBe
831d0 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 0a 29 nignEnd)(void).)
831e0 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 {. wsdHooksInit
831f0 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 ;. wsdHooks.xBe
83200 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42 65 6e nignBegin = xBen
83210 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73 64 48 ignBegin;. wsdH
83220 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 ooks.xBenignEnd
83230 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 7d 0a = xBenignEnd;.}.
83240 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73 71 6c ./*.** This (sql
83250 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c ite3EndBenignMal
83260 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c 65 64 loc()) is called
83270 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64 65 20 by SQLite code
83280 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 to indicate that
83290 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 6d .** subsequent m
832a0 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 alloc failures a
832b0 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63 61 6c re benign. A cal
832c0 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e 64 42 l to sqlite3EndB
832d0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 2a 2a enignMalloc().**
832e0 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
832f0 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f subsequent mallo
83300 63 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e c failures are n
83310 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a 53 51 on-benign..*/.SQ
83320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
83330 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 d sqlite3BeginBe
83340 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 nignMalloc(void)
83350 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e 69 74 {. wsdHooksInit
83360 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f 6b 73 ;. if( wsdHooks
83370 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 29 7b .xBenignBegin ){
83380 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 . wsdHooks.xB
83390 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a 20 20 enignBegin();.
833a0 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }.}.SQLITE_PRIVA
833b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 TE void sqlite3E
833c0 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 ndBenignMalloc(v
833d0 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 oid){. wsdHooks
833e0 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 Init;. if( wsdH
833f0 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 20 ooks.xBenignEnd
83400 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e ){. wsdHooks.
83410 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a 20 20 xBenignEnd();.
83420 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20 2f 2a }.}..#endif /*
83430 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f #ifndef SQLITE_
83440 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 OMIT_BUILTIN_TES
83450 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a T */../*********
83460 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 61 75 ***** End of fau
83470 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a lt.c ***********
83480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
834a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
834b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
834c0 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a mem0.c ********
834d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
834e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
834f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 ****/./*.** 2008
83500 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a 0a 2a October 28.**.*
83510 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
83520 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
83530 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
83540 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
83550 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
83560 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
83570 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
83580 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
83590 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
835a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
835b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
835c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
835d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
835e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
835f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
83600 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
83610 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
83620 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
83630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
83670 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
83680 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65 6d 6f ins a no-op memo
83690 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 ry allocation dr
836a0 69 76 65 72 73 20 66 6f 72 20 75 73 65 20 77 68 ivers for use wh
836b0 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a 45 52 en.** SQLITE_ZER
836c0 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65 66 69 O_MALLOC is defi
836d0 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 ned. The alloca
836e0 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69 6d 70 tion drivers imp
836f0 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65 72 65 lemented.** here
83700 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20 20 53 always fail. S
83710 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 6f QLite will not o
83720 70 65 72 61 74 65 20 77 69 74 68 20 74 68 65 73 perate with thes
83730 65 20 64 72 69 76 65 72 73 2e 20 20 54 68 65 73 e drivers. Thes
83740 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c 79 20 e.** are merely
83750 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20 20 52 placeholders. R
83760 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75 73 74 eal drivers must
83770 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20 be substituted
83780 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 using.** sqlite3
83790 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f 72 65 _config() before
837a0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 70 65 SQLite will ope
837b0 72 61 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 rate..*/../*.**
837c0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 This version of
837d0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
837e0 61 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 ator is the defa
837f0 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 ult. It is.** u
83800 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 sed when no othe
83810 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 r memory allocat
83820 6f 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 or is specified
83830 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 using compile-ti
83840 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f me.** macros..*/
83850 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 5a .#ifdef SQLITE_Z
83860 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a ERO_MALLOC../*.*
83870 2a 20 4e 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 * No-op versions
83880 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 of all memory a
83890 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e llocation routin
838a0 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 es.*/.static voi
838b0 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c d *sqlite3MemMal
838c0 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 loc(int nByte){
838d0 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 return 0; }.stat
838e0 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d ic void sqlite3M
838f0 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 emFree(void *pPr
83900 69 6f 72 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a ior){ return; }.
83910 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c static void *sql
83920 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 ite3MemRealloc(v
83930 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 oid *pPrior, int
83940 20 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 nByte){ return
83950 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 0; }.static int
83960 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 sqlite3MemSize(v
83970 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 oid *pPrior){ re
83980 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 turn 0; }.static
83990 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 int sqlite3MemR
839a0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 oundup(int n){ r
839b0 65 74 75 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 eturn n; }.stati
839c0 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d c int sqlite3Mem
839d0 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 Init(void *NotUs
839e0 65 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 ed){ return SQLI
839f0 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 TE_OK; }.static
83a00 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
83a10 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f hutdown(void *No
83a20 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 tUsed){ return;
83a30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
83a40 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c utine is the onl
83a50 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 y routine in thi
83a60 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 s file with exte
83a70 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a rnal linkage..**
83a80 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 .** Populate the
83a90 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
83aa0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
83ab0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 ction pointers i
83ac0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 n.** sqlite3Glob
83ad0 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 alConfig.m with
83ae0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 pointers to the
83af0 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 routines in this
83b00 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
83b10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
83b20 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
83b30 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 lt(void){. stat
83b40 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
83b50 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 _mem_methods def
83b60 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a aultMethods = {.
83b70 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d sqlite3MemM
83b80 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
83b90 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 te3MemFree,.
83ba0 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c sqlite3MemReall
83bb0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
83bc0 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 MemSize,. sq
83bd0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c lite3MemRoundup,
83be0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
83bf0 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 Init,. sqlit
83c00 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 e3MemShutdown,.
83c10 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 0. };. sql
83c20 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 ite3_config(SQLI
83c30 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 TE_CONFIG_MALLOC
83c40 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 , &defaultMethod
83c50 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a s);.}..#endif /*
83c60 20 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c SQLITE_ZERO_MAL
83c70 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a LOC */../*******
83c80 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
83c90 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em0.c **********
83ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83cc0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
83cd0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
83ce0 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a le mem1.c ******
83cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83d10 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
83d20 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 07 August 14.**.
83d30 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
83d40 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
83d50 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
83d60 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
83d70 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
83d80 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
83d90 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
83da0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
83db0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
83dc0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
83dd0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
83de0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
83df0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
83e00 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
83e10 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
83e20 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
83e30 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
83e40 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
83e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
83e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
83e90 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
83ea0 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d ains low-level m
83eb0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
83ec0 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 drivers for whe
83ed0 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c n.** SQLite will
83ee0 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 use the standar
83ef0 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c d C-library mall
83f00 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 oc/realloc/free
83f10 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 interface.** to
83f20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 obtain the memor
83f30 79 20 69 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a y it needs..**.*
83f40 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
83f50 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 ains implementat
83f60 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d ions of the low-
83f70 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c level memory all
83f80 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 ocation.** routi
83f90 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69 6e nes specified in
83fa0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d the sqlite3_mem
83fb0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e _methods object.
83fc0 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 .*/../*.** This
83fd0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
83fe0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 emory allocator
83ff0 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 is the default.
84000 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 It is.** used w
84010 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d hen no other mem
84020 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 ory allocator is
84030 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 specified using
84040 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a compile-time.**
84050 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 macros..*/.#ifd
84060 65 66 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d ef SQLITE_SYSTEM
84070 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c _MALLOC../*.** L
84080 69 6b 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 ike malloc(), bu
84090 74 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 73 t remember the s
840a0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
840b0 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 ation.** so that
840c0 20 77 65 20 63 61 6e 20 66 69 6e 64 20 69 74 20 we can find it
840d0 6c 61 74 65 72 20 75 73 69 6e 67 20 73 71 6c 69 later using sqli
840e0 74 65 33 4d 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a te3MemSize()..**
840f0 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 .** For this low
84100 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 -level routine,
84110 77 65 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 we are guarantee
84120 64 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 d that nByte>0 b
84130 65 63 61 75 73 65 0a 2a 2a 20 63 61 73 65 73 20 ecause.** cases
84140 6f 66 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c of nByte<=0 will
84150 20 62 65 20 69 6e 74 65 72 63 65 70 74 65 64 20 be intercepted
84160 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68 20 62 and dealt with b
84170 79 20 68 69 67 68 65 72 20 6c 65 76 65 6c 0a 2a y higher level.*
84180 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 * routines..*/.s
84190 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 tatic void *sqli
841a0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 te3MemMalloc(int
841b0 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 nByte){. sqlit
841c0 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 e3_int64 *p;. a
841d0 73 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 ssert( nByte>0 )
841e0 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e ;. nByte = ROUN
841f0 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d D8(nByte);. p =
84200 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2b 38 malloc( nByte+8
84210 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
84220 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b p[0] = nByte;
84230 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 . p++;. }.
84240 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 70 return (void *)p
84250 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 ;.}../*.** Like
84260 66 72 65 65 28 29 20 62 75 74 20 77 6f 72 6b 73 free() but works
84270 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 for allocations
84280 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
84290 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 qlite3MemMalloc(
842a0 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 4d ).** or sqlite3M
842b0 65 6d 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a emRealloc()..**.
842c0 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d ** For this low-
842d0 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 level routine, w
842e0 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 e already know t
842f0 68 61 74 20 70 50 72 69 6f 72 21 3d 30 20 73 69 hat pPrior!=0 si
84300 6e 63 65 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 nce.** cases whe
84310 72 65 20 70 50 72 69 6f 72 3d 3d 30 20 77 69 6c re pPrior==0 wil
84320 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 l have been inte
84330 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74 cepted and dealt
84340 20 77 69 74 68 0a 2a 2a 20 62 79 20 68 69 67 68 with.** by high
84350 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 er-level routine
84360 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
84370 64 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 d sqlite3MemFree
84380 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a (void *pPrior){.
84390 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
843a0 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e *p = (sqlite3_in
843b0 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 t64*)pPrior;. a
843c0 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 ssert( pPrior!=0
843d0 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20 20 66 72 65 );. p--;. fre
843e0 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c e(p);.}../*.** L
843f0 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 2e 20 20 ike realloc().
84400 52 65 73 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 Resize an alloca
84410 74 69 6f 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 tion previously
84420 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a obtained from.**
84430 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f sqlite3MemMallo
84440 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 c()..**.** For t
84450 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 69 6e his low-level in
84460 74 65 72 66 61 63 65 2c 20 77 65 20 6b 6e 6f 77 terface, we know
84470 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d 30 2e that pPrior!=0.
84480 20 20 43 61 73 65 73 20 77 68 65 72 65 0a 2a 2a Cases where.**
84490 20 70 50 72 69 6f 72 3d 3d 30 20 77 68 69 6c 65 pPrior==0 while
844a0 20 68 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 have been inter
844b0 63 65 70 74 65 64 20 62 79 20 68 69 67 68 65 72 cepted by higher
844c0 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 20 61 -level routine a
844d0 6e 64 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64 nd.** redirected
844e0 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e 20 20 53 69 to xMalloc. Si
844f0 6d 69 6c 61 72 6c 79 2c 20 77 65 20 6b 6e 6f 77 milarly, we know
84500 20 74 68 61 74 20 6e 42 79 74 65 3e 30 20 62 65 that nByte>0 be
84510 63 61 75 73 65 73 0a 2a 2a 20 63 61 73 65 73 20 causes.** cases
84520 77 68 65 72 65 20 6e 42 79 74 65 3c 3d 30 20 77 where nByte<=0 w
84530 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 69 6e ill have been in
84540 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69 67 tercepted by hig
84550 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 her-level.** rou
84560 74 69 6e 65 73 20 61 6e 64 20 72 65 64 69 72 65 tines and redire
84570 63 74 65 64 20 74 6f 20 78 46 72 65 65 2e 0a 2a cted to xFree..*
84580 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 /.static void *s
84590 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
845a0 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 (void *pPrior, i
845b0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 71 6c nt nByte){. sql
845c0 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20 ite3_int64 *p =
845d0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 (sqlite3_int64*)
845e0 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 pPrior;. assert
845f0 28 20 70 50 72 69 6f 72 21 3d 30 20 26 26 20 6e ( pPrior!=0 && n
84600 42 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 Byte>0 );. nByt
84610 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 e = ROUND8(nByte
84620 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 );. p = (sqlite
84630 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
84640 0a 20 20 70 2d 2d 3b 0a 20 20 70 20 3d 20 72 65 . p--;. p = re
84650 61 6c 6c 6f 63 28 70 2c 20 6e 42 79 74 65 2b 38 alloc(p, nByte+8
84660 20 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 );. if( p ){.
84670 20 20 20 70 5b 30 5d 20 3d 20 6e 42 79 74 65 3b p[0] = nByte;
84680 0a 20 20 20 20 70 2b 2b 3b 0a 20 20 7d 0a 20 20 . p++;. }.
84690 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b return (void*)p;
846a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 .}../*.** Report
846b0 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 the allocated s
846c0 69 7a 65 20 6f 66 20 61 20 70 72 69 6f 72 20 72 ize of a prior r
846d0 65 74 75 72 6e 20 66 72 6f 6d 20 78 4d 61 6c 6c eturn from xMall
846e0 6f 63 28 29 0a 2a 2a 20 6f 72 20 78 52 65 61 6c oc().** or xReal
846f0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 loc()..*/.static
84700 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 int sqlite3MemS
84710 69 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 ize(void *pPrior
84720 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 ){. sqlite3_int
84730 36 34 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 64 *p;. if( pPr
84740 69 6f 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ior==0 ) return
84750 30 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65 0;. p = (sqlite
84760 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 3_int64*)pPrior;
84770 0a 20 20 70 2d 2d 3b 0a 20 20 72 65 74 75 72 6e . p--;. return
84780 20 28 69 6e 74 29 70 5b 30 5d 3b 0a 7d 0a 0a 2f (int)p[0];.}../
84790 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 *.** Round up a
847a0 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 request size to
847b0 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 the next valid a
847c0 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a llocation size..
847d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 */.static int sq
847e0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 lite3MemRoundup(
847f0 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e int n){. return
84800 20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f ROUND8(n);.}../
84810 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
84820 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a this module..*/.
84830 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 static int sqlit
84840 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a e3MemInit(void *
84850 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
84860 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
84870 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
84880 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
84890 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 .** Deinitialize
848a0 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f this module..*/
848b0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c .static void sql
848c0 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 ite3MemShutdown(
848d0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a void *NotUsed){.
848e0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
848f0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 ER(NotUsed);. r
84900 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 eturn;.}../*.**
84910 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
84920 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 the only routine
84930 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 in this file wi
84940 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b th external link
84950 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c age..**.** Popul
84960 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 ate the low-leve
84970 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 l memory allocat
84980 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 ion function poi
84990 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 nters in.** sqli
849a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
849b0 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 m with pointers
849c0 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 to the routines
849d0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f in this file..*/
849e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
849f0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 void sqlite3MemS
84a00 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b etDefault(void){
84a10 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 . static const
84a20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
84a30 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f ods defaultMetho
84a40 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 ds = {. sqli
84a50 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 te3MemMalloc,.
84a60 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 sqlite3MemFre
84a70 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d e,. sqlite3M
84a80 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 emRealloc,.
84a90 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a sqlite3MemSize,.
84aa0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 sqlite3MemR
84ab0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c oundup,. sql
84ac0 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 ite3MemInit,.
84ad0 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 sqlite3MemShut
84ae0 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d down,. 0. }
84af0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 ;. sqlite3_conf
84b00 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 ig(SQLITE_CONFIG
84b10 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c _MALLOC, &defaul
84b20 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 tMethods);.}..#e
84b30 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 53 ndif /* SQLITE_S
84b40 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a YSTEM_MALLOC */.
84b50 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
84b60 20 45 6e 64 20 6f 66 20 6d 65 6d 31 2e 63 20 2a End of mem1.c *
84b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
84ba0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
84bb0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 32 Begin file mem2
84bc0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
84bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
84bf0 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
84c00 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 15.**.** The
84c10 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
84c20 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
84c30 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
84c40 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
84c50 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
84c60 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
84c70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
84c80 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
84c90 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
84ca0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
84cb0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
84cc0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
84cd0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
84ce0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
84cf0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
84d00 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
84d10 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
84d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
84d60 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
84d70 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f file contains lo
84d80 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 w-level memory a
84d90 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 llocation driver
84da0 73 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 s for when.** SQ
84db0 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 Lite will use th
84dc0 65 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 e standard C-lib
84dd0 72 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c rary malloc/real
84de0 6c 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 loc/free interfa
84df0 63 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 ce.** to obtain
84e00 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 the memory it ne
84e10 65 64 73 20 77 68 69 6c 65 20 61 64 64 69 6e 67 eds while adding
84e20 20 6c 6f 74 73 20 6f 66 20 61 64 64 69 74 69 6f lots of additio
84e30 6e 61 6c 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a nal debugging.**
84e40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 information to
84e50 65 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 each allocation
84e60 69 6e 20 6f 72 64 65 72 20 74 6f 20 68 65 6c 70 in order to help
84e70 20 64 65 74 65 63 74 20 61 6e 64 20 66 69 78 20 detect and fix
84e80 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65 61 6b 73 20 memory.** leaks
84e90 61 6e 64 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 and memory usage
84ea0 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 errors..**.** T
84eb0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
84ec0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e s implementation
84ed0 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76 s of the low-lev
84ee0 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 el memory alloca
84ef0 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 tion.** routines
84f00 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 specified in th
84f10 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 e sqlite3_mem_me
84f20 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2f thods object..*/
84f30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 ../*.** This ver
84f40 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f sion of the memo
84f50 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 ry allocator is
84f60 75 73 65 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 used only if the
84f70 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 .** SQLITE_MEMDE
84f80 42 55 47 20 6d 61 63 72 6f 20 69 73 20 64 65 66 BUG macro is def
84f90 69 6e 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ined.*/.#ifdef S
84fa0 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a QLITE_MEMDEBUG..
84fb0 2f 2a 0a 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 /*.** The backtr
84fc0 61 63 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 ace functionalit
84fd0 79 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 y is only availa
84fe0 62 6c 65 20 77 69 74 68 20 47 4c 49 42 43 0a 2a ble with GLIBC.*
84ff0 2f 0a 23 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 /.#ifdef __GLIBC
85000 5f 5f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 __. extern int
85010 62 61 63 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a backtrace(void**
85020 2c 69 6e 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 ,int);. extern
85030 76 6f 69 64 20 62 61 63 6b 74 72 61 63 65 5f 73 void backtrace_s
85040 79 6d 62 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 ymbols_fd(void*c
85050 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a onst*,int,int);.
85060 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 62 #else.# define b
85070 61 63 6b 74 72 61 63 65 28 41 2c 42 29 20 31 0a acktrace(A,B) 1.
85080 23 20 64 65 66 69 6e 65 20 62 61 63 6b 74 72 61 # define backtra
85090 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c ce_symbols_fd(A,
850a0 42 2c 43 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a B,C).#endif../*.
850b0 2a 2a 20 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 ** Each memory a
850c0 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 llocation looks
850d0 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a like this:.**.**
850e0 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
850f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85100 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85110 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85120 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c ----------.** |
85130 20 54 69 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 Title | backtr
85140 61 63 65 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 ace pointers |
85150 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 MemBlockHdr | a
85160 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 llocation | End
85170 47 75 61 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d Guard |.** ----
85180 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
85190 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
851a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
851b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
851c0 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 ----.**.** The a
851d0 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 pplication code
851e0 73 65 65 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e sees only a poin
851f0 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 ter to the alloc
85200 61 74 69 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a ation. We have.
85210 2a 2a 20 74 6f 20 62 61 63 6b 20 75 70 20 66 72 ** to back up fr
85220 6f 6d 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f om the allocatio
85230 6e 20 70 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e n pointer to fin
85240 64 20 74 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 d the MemBlockHd
85250 72 2e 20 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c r. The.** MemBl
85260 6f 63 6b 48 64 72 20 74 65 6c 6c 73 20 75 73 20 ockHdr tells us
85270 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
85280 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 allocation and t
85290 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 he number of.**
852a0 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 backtrace pointe
852b0 72 73 2e 20 20 54 68 65 72 65 20 69 73 20 61 6c rs. There is al
852c0 73 6f 20 61 20 67 75 61 72 64 20 77 6f 72 64 20 so a guard word
852d0 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 at the end of th
852e0 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 e.** MemBlockHdr
852f0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 ..*/.struct MemB
85300 6c 6f 63 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 lockHdr {. i64
85310 69 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 iSize;
85320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85330 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 /* Size of this
85340 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 allocation */.
85350 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 struct MemBlockH
85360 64 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 dr *pNext, *pPre
85370 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 v; /* Linked li
85380 73 74 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 st of all unfree
85390 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 d memory */. ch
853a0 61 72 20 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 ar nBacktrace;
853b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
853c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
853d0 61 63 6b 74 72 61 63 65 73 20 6f 6e 20 74 68 69 acktraces on thi
853e0 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 s alloc */. cha
853f0 72 20 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 r nBacktraceSlot
85400 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
85410 20 2f 2a 20 41 76 61 69 6c 61 62 6c 65 20 62 61 /* Available ba
85420 63 6b 74 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f cktrace slots */
85430 0a 20 20 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b . short nTitle;
85440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
85450 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 /* Bytes
85460 6f 66 20 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 of title; includ
85470 65 73 20 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 es '\0' */. int
85480 20 69 46 6f 72 65 47 75 61 72 64 3b 20 20 20 20 iForeGuard;
85490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
854a0 20 2f 2a 20 47 75 61 72 64 20 77 6f 72 64 20 66 /* Guard word f
854b0 6f 72 20 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a or sanity */.};.
854c0 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 ./*.** Guard wor
854d0 64 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f ds.*/.#define FO
854e0 52 45 47 55 41 52 44 20 30 78 38 30 46 35 45 31 REGUARD 0x80F5E1
854f0 35 33 0a 23 64 65 66 69 6e 65 20 52 45 41 52 47 53.#define REARG
85500 55 41 52 44 20 30 78 45 34 36 37 36 42 35 33 0a UARD 0xE4676B53.
85510 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 ./*.** Number of
85520 20 6d 61 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 malloc size inc
85530 72 65 6d 65 6e 74 73 20 74 6f 20 74 72 61 63 6b rements to track
85540 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 ..*/.#define NCS
85550 49 5a 45 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a IZE 1000../*.**
85560 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 All of the stat
85570 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 ic variables use
85580 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 d by this module
85590 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a are collected.*
855a0 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 * into a single
855b0 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 structure named
855c0 22 6d 65 6d 22 2e 20 20 54 68 69 73 20 69 73 20 "mem". This is
855d0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 to keep the.** s
855e0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 tatic variables
855f0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f organized and to
85600 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 reduce namespac
85610 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 e pollution.** w
85620 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 hen this module
85630 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 is combined with
85640 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d other in the am
85650 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 algamation..*/.s
85660 74 61 74 69 63 20 73 74 72 75 63 74 20 7b 0a 20 tatic struct {.
85670 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 . /*. ** Mute
85680 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 x to control acc
85690 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 ess to the memor
856a0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 y allocation sub
856b0 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 system.. */. s
856c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 qlite3_mutex *mu
856d0 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 tex;.. /*. **
856e0 48 65 61 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 Head and tail of
856f0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f a linked list o
85700 66 20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e f all outstandin
85710 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 g allocations.
85720 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 */. struct MemB
85730 6c 6f 63 6b 48 64 72 20 2a 70 46 69 72 73 74 3b lockHdr *pFirst;
85740 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
85750 63 6b 48 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 ckHdr *pLast;.
85760 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e . /*. ** The n
85770 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 umber of levels
85780 6f 66 20 62 61 63 6b 74 72 61 63 65 20 74 6f 20 of backtrace to
85790 73 61 76 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f save in new allo
857a0 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 cations.. */.
857b0 69 6e 74 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a int nBacktrace;.
857c0 20 20 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 void (*xBacktr
857d0 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 ace)(int, int, v
857e0 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 oid **);.. /*.
857f0 20 2a 2a 20 54 69 74 6c 65 20 74 65 78 74 20 74 ** Title text t
85800 6f 20 69 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e o insert in fron
85810 74 20 6f 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a t of each block.
85820 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c */. int nTitl
85830 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 e; /* Byt
85840 65 73 20 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 es of zTitle to
85850 73 61 76 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 save. Includes
85860 27 5c 30 27 20 61 6e 64 20 70 61 64 64 69 6e 67 '\0' and padding
85870 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 69 74 6c */. char zTitl
85880 65 5b 31 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 e[100]; /* The
85890 74 69 74 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 title text */..
858a0 20 2f 2a 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 /* . ** sqlite
858b0 33 4d 61 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 3MallocDisallow(
858c0 29 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 ) increments the
858d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 following count
858e0 65 72 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 er.. ** sqlite3
858f0 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 MallocAllow() de
85900 63 72 65 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a crements it.. *
85910 2f 0a 20 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 /. int disallow
85920 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f ; /* Do not allo
85930 77 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 w memory allocat
85940 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a ion */.. /*. *
85950 2a 20 47 61 74 68 65 72 20 73 74 61 74 69 73 74 * Gather statist
85960 69 63 73 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 ics on the sizes
85970 20 6f 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 of memory alloc
85980 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c ations.. ** nAl
85990 6c 6f 63 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 loc[i] is the nu
859a0 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 mber of allocati
859b0 6f 6e 20 61 74 74 65 6d 70 74 73 20 6f 66 20 69 on attempts of i
859c0 2a 38 0a 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 *8. ** bytes.
859d0 69 3d 3d 4e 43 53 49 5a 45 20 69 73 20 74 68 65 i==NCSIZE is the
859e0 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 number of alloc
859f0 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 ation attempts f
85a00 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f or. ** sizes mo
85a10 72 65 20 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 re than NCSIZE*8
85a20 20 62 79 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 bytes.. */. i
85a30 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 nt nAlloc[NCSIZE
85a40 5d 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c ]; /* Total
85a50 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 number of alloc
85a60 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 ations */. int
85a70 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d nCurrent[NCSIZE]
85a80 3b 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 ; /* Current
85a90 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 number of alloca
85aa0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d tions */. int m
85ab0 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d xCurrent[NCSIZE]
85ac0 3b 20 20 20 2f 2a 20 48 69 67 68 77 61 74 65 72 ; /* Highwater
85ad0 20 6d 61 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 mark for nCurre
85ae0 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a nt */..} mem;...
85af0 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d /*.** Adjust mem
85b00 6f 72 79 20 75 73 61 67 65 20 73 74 61 74 69 73 ory usage statis
85b10 74 69 63 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tics.*/.static v
85b20 6f 69 64 20 61 64 6a 75 73 74 53 74 61 74 73 28 oid adjustStats(
85b30 69 6e 74 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 int iSize, int i
85b40 6e 63 72 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 ncrement){. int
85b50 20 69 20 3d 20 52 4f 55 4e 44 38 28 69 53 69 7a i = ROUND8(iSiz
85b60 65 29 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 e)/8;. if( i>NC
85b70 53 49 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 SIZE-1 ){. i
85b80 3d 20 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 = NCSIZE - 1;.
85b90 7d 0a 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e }. if( incremen
85ba0 74 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e t>0 ){. mem.n
85bb0 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 Alloc[i]++;.
85bc0 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b mem.nCurrent[i]+
85bd0 2b 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e +;. if( mem.n
85be0 43 75 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d Current[i]>mem.m
85bf0 78 43 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 xCurrent[i] ){.
85c00 20 20 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 mem.mxCurre
85c10 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 nt[i] = mem.nCur
85c20 72 65 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 rent[i];. }.
85c30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e }else{. mem.
85c40 6e 43 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 nCurrent[i]--;.
85c50 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e assert( mem.n
85c60 43 75 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b Current[i]>=0 );
85c70 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 . }.}../*.** Gi
85c80 76 65 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f ven an allocatio
85c90 6e 2c 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 n, find the MemB
85ca0 6c 6f 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 lockHdr for that
85cb0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a allocation..**.
85cc0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
85cd0 63 68 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 checks the guard
85ce0 73 20 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 s at either end
85cf0 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f of the allocatio
85d00 6e 20 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 n and.** if they
85d10 20 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 are incorrect i
85d20 74 20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 t asserts..*/.st
85d30 61 74 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 atic struct MemB
85d40 6c 6f 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 lockHdr *sqlite3
85d50 4d 65 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 MemsysGetHeader(
85d60 76 6f 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f void *pAllocatio
85d70 6e 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d n){. struct Mem
85d80 42 6c 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 BlockHdr *p;. i
85d90 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a nt *pInt;. u8 *
85da0 70 55 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 pU8;. int nRese
85db0 72 76 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 rve;.. p = (str
85dc0 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a uct MemBlockHdr*
85dd0 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 )pAllocation;.
85de0 70 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 p--;. assert( p
85df0 2d 3e 69 46 6f 72 65 47 75 61 72 64 3d 3d 28 69 ->iForeGuard==(i
85e00 6e 74 29 46 4f 52 45 47 55 41 52 44 20 29 3b 0a nt)FOREGUARD );.
85e10 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55 nReserve = ROU
85e20 4e 44 38 28 70 2d 3e 69 53 69 7a 65 29 3b 0a 20 ND8(p->iSize);.
85e30 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a 29 70 41 pInt = (int*)pA
85e40 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 55 38 llocation;. pU8
85e50 20 3d 20 28 75 38 2a 29 70 41 6c 6c 6f 63 61 74 = (u8*)pAllocat
85e60 69 6f 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ion;. assert( p
85e70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a Int[nReserve/siz
85e80 65 6f 66 28 69 6e 74 29 5d 3d 3d 28 69 6e 74 29 eof(int)]==(int)
85e90 52 45 41 52 47 55 41 52 44 20 29 3b 0a 20 20 2f REARGUARD );. /
85ea0 2a 20 54 68 69 73 20 63 68 65 63 6b 73 20 61 6e * This checks an
85eb0 79 20 6f 66 20 74 68 65 20 22 65 78 74 72 61 22 y of the "extra"
85ec0 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 bytes allocated
85ed0 20 64 75 65 0a 20 20 2a 2a 20 74 6f 20 72 6f 75 due. ** to rou
85ee0 6e 64 69 6e 67 20 75 70 20 74 6f 20 61 6e 20 38 nding up to an 8
85ef0 20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 20 74 byte boundary t
85f00 6f 20 65 6e 73 75 72 65 20 0a 20 20 2a 2a 20 74 o ensure . ** t
85f10 68 65 79 20 68 61 76 65 6e 27 74 20 62 65 65 6e hey haven't been
85f20 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 20 20 overwritten..
85f30 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 52 65 73 */. while( nRes
85f40 65 72 76 65 2d 2d 20 3e 20 70 2d 3e 69 53 69 7a erve-- > p->iSiz
85f50 65 20 29 20 61 73 73 65 72 74 28 20 70 55 38 5b e ) assert( pU8[
85f60 6e 52 65 73 65 72 76 65 5d 3d 3d 30 78 36 35 20 nReserve]==0x65
85f70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d );. return p;.}
85f80 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
85f90 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
85fa0 65 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c es currently all
85fb0 6f 63 61 74 65 64 20 61 74 20 61 64 64 72 65 73 ocated at addres
85fc0 73 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 s p..*/.static i
85fd0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a nt sqlite3MemSiz
85fe0 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73 74 e(void *p){. st
85ff0 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 ruct MemBlockHdr
86000 20 2a 70 48 64 72 3b 0a 20 20 69 66 28 20 21 70 *pHdr;. if( !p
86010 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
86020 3b 0a 20 20 7d 0a 20 20 70 48 64 72 20 3d 20 73 ;. }. pHdr = s
86030 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 qlite3MemsysGetH
86040 65 61 64 65 72 28 70 29 3b 0a 20 20 72 65 74 75 eader(p);. retu
86050 72 6e 20 70 48 64 72 2d 3e 69 53 69 7a 65 3b 0a rn pHdr->iSize;.
86060 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
86070 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ize the memory a
86080 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
86090 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 tem..*/.static i
860a0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 nt sqlite3MemIni
860b0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 t(void *NotUsed)
860c0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d {. UNUSED_PARAM
860d0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
860e0 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 6f 66 assert( (sizeof
860f0 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b (struct MemBlock
86100 48 64 72 29 26 37 29 20 3d 3d 20 30 20 29 3b 0a Hdr)&7) == 0 );.
86110 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c if( !sqlite3Gl
86120 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 obalConfig.bMems
86130 74 61 74 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 tat ){. /* If
86140 20 6d 65 6d 6f 72 79 20 73 74 61 74 75 73 20 69 memory status i
86150 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 s enabled, then
86160 74 68 65 20 6d 61 6c 6c 6f 63 2e 63 20 77 72 61 the malloc.c wra
86170 70 70 65 72 20 77 69 6c 6c 20 61 6c 72 65 61 64 pper will alread
86180 79 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 20 74 68 y. ** hold th
86190 65 20 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 e STATIC_MEM mut
861a0 65 78 20 77 68 65 6e 20 74 68 65 20 72 6f 75 74 ex when the rout
861b0 69 6e 65 73 20 68 65 72 65 20 61 72 65 20 69 6e ines here are in
861c0 76 6f 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 6d 65 voked. */. me
861d0 6d 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 m.mutex = sqlite
861e0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 3MutexAlloc(SQLI
861f0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
86200 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 MEM);. }. retu
86210 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
86220 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c ./*.** Deinitial
86230 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ize the memory a
86240 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 llocation subsys
86250 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 tem..*/.static v
86260 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 oid sqlite3MemSh
86270 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 utdown(void *Not
86280 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f Used){. UNUSED_
86290 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
862a0 64 29 3b 0a 20 20 6d 65 6d 2e 6d 75 74 65 78 20 d);. mem.mutex
862b0 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f = 0;.}../*.** Ro
862c0 75 6e 64 20 75 70 20 61 20 72 65 71 75 65 73 74 und up a request
862d0 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 size to the nex
862e0 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 t valid allocati
862f0 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 on size..*/.stat
86300 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 ic int sqlite3Me
86310 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b mRoundup(int n){
86320 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 . return ROUND8
86330 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c (n);.}../*.** Al
86340 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 locate nByte byt
86350 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f es of memory..*/
86360 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 71 .static void *sq
86370 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 lite3MemMalloc(i
86380 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 nt nByte){. str
86390 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
863a0 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a *pHdr;. void **
863b0 70 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a pBt;. char *z;.
863c0 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20 76 int *pInt;. v
863d0 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e oid *p = 0;. in
863e0 74 20 74 6f 74 61 6c 53 69 7a 65 3b 0a 20 20 69 t totalSize;. i
863f0 6e 74 20 6e 52 65 73 65 72 76 65 3b 0a 20 20 73 nt nReserve;. s
86400 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
86410 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 er(mem.mutex);.
86420 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 assert( mem.dis
86430 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 6e 52 allow==0 );. nR
86440 65 73 65 72 76 65 20 3d 20 52 4f 55 4e 44 38 28 eserve = ROUND8(
86450 6e 42 79 74 65 29 3b 0a 20 20 74 6f 74 61 6c 53 nByte);. totalS
86460 69 7a 65 20 3d 20 6e 52 65 73 65 72 76 65 20 2b ize = nReserve +
86470 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b sizeof(*pHdr) +
86480 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 0a 20 sizeof(int) +.
86490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 me
864a0 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2a 73 69 7a m.nBacktrace*siz
864b0 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 6d 65 6d eof(void*) + mem
864c0 2e 6e 54 69 74 6c 65 3b 0a 20 20 70 20 3d 20 6d .nTitle;. p = m
864d0 61 6c 6c 6f 63 28 74 6f 74 61 6c 53 69 7a 65 29 alloc(totalSize)
864e0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
864f0 20 7a 20 3d 20 70 3b 0a 20 20 20 20 70 42 74 20 z = p;. pBt
86500 3d 20 28 76 6f 69 64 2a 2a 29 26 7a 5b 6d 65 6d = (void**)&z[mem
86510 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20 20 20 70 48 .nTitle];. pH
86520 64 72 20 3d 20 28 73 74 72 75 63 74 20 4d 65 6d dr = (struct Mem
86530 42 6c 6f 63 6b 48 64 72 2a 29 26 70 42 74 5b 6d BlockHdr*)&pBt[m
86540 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 5d 3b 0a em.nBacktrace];.
86550 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 20 pHdr->pNext
86560 3d 20 30 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 = 0;. pHdr->p
86570 50 72 65 76 20 3d 20 6d 65 6d 2e 70 4c 61 73 74 Prev = mem.pLast
86580 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 70 4c ;. if( mem.pL
86590 61 73 74 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ast ){. mem
865a0 2e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20 .pLast->pNext =
865b0 70 48 64 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b pHdr;. }else{
865c0 0a 20 20 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 . mem.pFirs
865d0 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 0a t = pHdr;. }.
865e0 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 mem.pLast =
865f0 70 48 64 72 3b 0a 20 20 20 20 70 48 64 72 2d 3e pHdr;. pHdr->
86600 69 46 6f 72 65 47 75 61 72 64 20 3d 20 46 4f 52 iForeGuard = FOR
86610 45 47 55 41 52 44 3b 0a 20 20 20 20 70 48 64 72 EGUARD;. pHdr
86620 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 ->nBacktraceSlot
86630 73 20 3d 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 s = mem.nBacktra
86640 63 65 3b 0a 20 20 20 20 70 48 64 72 2d 3e 6e 54 ce;. pHdr->nT
86650 69 74 6c 65 20 3d 20 6d 65 6d 2e 6e 54 69 74 6c itle = mem.nTitl
86660 65 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e e;. if( mem.n
86670 42 61 63 6b 74 72 61 63 65 20 29 7b 0a 20 20 20 Backtrace ){.
86680 20 20 20 76 6f 69 64 20 2a 61 41 64 64 72 5b 34 void *aAddr[4
86690 30 5d 3b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e 0];. pHdr->
866a0 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 62 61 63 nBacktrace = bac
866b0 6b 74 72 61 63 65 28 61 41 64 64 72 2c 20 6d 65 ktrace(aAddr, me
866c0 6d 2e 6e 42 61 63 6b 74 72 61 63 65 2b 31 29 2d m.nBacktrace+1)-
866d0 31 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 1;. memcpy(
866e0 70 42 74 2c 20 26 61 41 64 64 72 5b 31 5d 2c 20 pBt, &aAddr[1],
866f0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 pHdr->nBacktrace
86700 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 3b *sizeof(void*));
86710 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 42 . assert(pB
86720 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 t[0]);. if(
86730 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 mem.xBacktrace
86740 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 2e 78 ){. mem.x
86750 42 61 63 6b 74 72 61 63 65 28 6e 42 79 74 65 2c Backtrace(nByte,
86760 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
86770 65 2d 31 2c 20 26 61 41 64 64 72 5b 31 5d 29 3b e-1, &aAddr[1]);
86780 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
86790 73 65 7b 0a 20 20 20 20 20 20 70 48 64 72 2d 3e se{. pHdr->
867a0 6e 42 61 63 6b 74 72 61 63 65 20 3d 20 30 3b 0a nBacktrace = 0;.
867b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65 }. if( me
867c0 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a 20 20 20 20 m.nTitle ){.
867d0 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d 65 6d 2e memcpy(z, mem.
867e0 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e 54 69 74 zTitle, mem.nTit
867f0 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 le);. }. p
86800 48 64 72 2d 3e 69 53 69 7a 65 20 3d 20 6e 42 79 Hdr->iSize = nBy
86810 74 65 3b 0a 20 20 20 20 61 64 6a 75 73 74 53 74 te;. adjustSt
86820 61 74 73 28 6e 42 79 74 65 2c 20 2b 31 29 3b 0a ats(nByte, +1);.
86830 20 20 20 20 70 49 6e 74 20 3d 20 28 69 6e 74 2a pInt = (int*
86840 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20 20 20 70 )&pHdr[1];. p
86850 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f 73 69 7a Int[nReserve/siz
86860 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52 45 41 52 eof(int)] = REAR
86870 47 55 41 52 44 3b 0a 20 20 20 20 6d 65 6d 73 65 GUARD;. memse
86880 74 28 70 49 6e 74 2c 20 30 78 36 35 2c 20 6e 52 t(pInt, 0x65, nR
86890 65 73 65 72 76 65 29 3b 0a 20 20 20 20 70 20 3d eserve);. p =
868a0 20 28 76 6f 69 64 2a 29 70 49 6e 74 3b 0a 20 20 (void*)pInt;.
868b0 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 }. sqlite3_mute
868c0 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 x_leave(mem.mute
868d0 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 20 x);. return p;
868e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d .}../*.** Free m
868f0 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 emory..*/.static
86900 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d void sqlite3Mem
86910 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f Free(void *pPrio
86920 72 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d r){. struct Mem
86930 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a BlockHdr *pHdr;.
86940 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 void **pBt;.
86950 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73 73 65 72 char *z;. asser
86960 74 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c t( sqlite3Global
86970 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 Config.bMemstat
86980 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21 3d 30 20 || mem.mutex!=0
86990 29 3b 0a 20 20 70 48 64 72 20 3d 20 73 71 6c 69 );. pHdr = sqli
869a0 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61 64 te3MemsysGetHead
869b0 65 72 28 70 50 72 69 6f 72 29 3b 0a 20 20 70 42 er(pPrior);. pB
869c0 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 t = (void**)pHdr
869d0 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d ;. pBt -= pHdr-
869e0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 >nBacktraceSlots
869f0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
86a00 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 x_enter(mem.mute
86a10 78 29 3b 0a 20 20 69 66 28 20 70 48 64 72 2d 3e x);. if( pHdr->
86a20 70 50 72 65 76 20 29 7b 0a 20 20 20 20 61 73 73 pPrev ){. ass
86a30 65 72 74 28 20 70 48 64 72 2d 3e 70 50 72 65 76 ert( pHdr->pPrev
86a40 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 72 20 29 3b ->pNext==pHdr );
86a50 0a 20 20 20 20 70 48 64 72 2d 3e 70 50 72 65 76 . pHdr->pPrev
86a60 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 2d 3e ->pNext = pHdr->
86a70 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a pNext;. }else{.
86a80 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e assert( mem.
86a90 70 46 69 72 73 74 3d 3d 70 48 64 72 20 29 3b 0a pFirst==pHdr );.
86aa0 20 20 20 20 6d 65 6d 2e 70 46 69 72 73 74 20 3d mem.pFirst =
86ab0 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 pHdr->pNext;.
86ac0 7d 0a 20 20 69 66 28 20 70 48 64 72 2d 3e 70 4e }. if( pHdr->pN
86ad0 65 78 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ext ){. asser
86ae0 74 28 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e t( pHdr->pNext->
86af0 70 50 72 65 76 3d 3d 70 48 64 72 20 29 3b 0a 20 pPrev==pHdr );.
86b00 20 20 20 70 48 64 72 2d 3e 70 4e 65 78 74 2d 3e pHdr->pNext->
86b10 70 50 72 65 76 20 3d 20 70 48 64 72 2d 3e 70 50 pPrev = pHdr->pP
86b20 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 rev;. }else{.
86b30 20 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 70 4c assert( mem.pL
86b40 61 73 74 3d 3d 70 48 64 72 20 29 3b 0a 20 20 20 ast==pHdr );.
86b50 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20 70 48 64 mem.pLast = pHd
86b60 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 0a 20 20 r->pPrev;. }.
86b70 7a 20 3d 20 28 63 68 61 72 2a 29 70 42 74 3b 0a z = (char*)pBt;.
86b80 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 54 69 z -= pHdr->nTi
86b90 74 6c 65 3b 0a 20 20 61 64 6a 75 73 74 53 74 61 tle;. adjustSta
86ba0 74 73 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 ts(pHdr->iSize,
86bb0 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 2c -1);. memset(z,
86bc0 20 30 78 32 62 2c 20 73 69 7a 65 6f 66 28 76 6f 0x2b, sizeof(vo
86bd0 69 64 2a 29 2a 70 48 64 72 2d 3e 6e 42 61 63 6b id*)*pHdr->nBack
86be0 74 72 61 63 65 53 6c 6f 74 73 20 2b 20 73 69 7a traceSlots + siz
86bf0 65 6f 66 28 2a 70 48 64 72 29 20 2b 0a 20 20 20 eof(*pHdr) +.
86c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
86c10 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20 73 69 7a Hdr->iSize + siz
86c20 65 6f 66 28 69 6e 74 29 20 2b 20 70 48 64 72 2d eof(int) + pHdr-
86c30 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 66 72 65 65 >nTitle);. free
86c40 28 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d (z);. sqlite3_m
86c50 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d utex_leave(mem.m
86c60 75 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a utex); .}../*.*
86c70 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a * Change the siz
86c80 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 e of an existing
86c90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
86ca0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 on..**.** For th
86cb0 69 73 20 64 65 62 75 67 67 69 6e 67 20 69 6d 70 is debugging imp
86cc0 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 77 65 20 lementation, we
86cd0 2a 61 6c 77 61 79 73 2a 20 6d 61 6b 65 20 61 20 *always* make a
86ce0 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 61 copy of the.** a
86cf0 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 6f 20 61 llocation into a
86d00 20 6e 65 77 20 70 6c 61 63 65 20 69 6e 20 6d 65 new place in me
86d10 6d 6f 72 79 2e 20 20 49 6e 20 74 68 69 73 20 77 mory. In this w
86d20 61 79 2c 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 ay, if the .** h
86d30 69 67 68 65 72 20 6c 65 76 65 6c 20 63 6f 64 65 igher level code
86d40 20 69 73 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 is using pointe
86d50 72 20 74 6f 20 74 68 65 20 6f 6c 64 20 61 6c 6c r to the old all
86d60 6f 63 61 74 69 6f 6e 2c 20 69 74 20 69 73 20 0a ocation, it is .
86d70 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20 6c 69 6b ** much more lik
86d80 65 6c 79 20 74 6f 20 62 72 65 61 6b 20 61 6e 64 ely to break and
86d90 20 77 65 20 61 72 65 20 6d 75 63 68 20 6d 6f 72 we are much mor
86da0 65 20 6c 69 6b 69 6e 67 20 74 6f 20 66 69 6e 64 e liking to find
86db0 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a .** the error..*
86dc0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 73 /.static void *s
86dd0 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 qlite3MemRealloc
86de0 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 (void *pPrior, i
86df0 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73 74 72 nt nByte){. str
86e00 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 uct MemBlockHdr
86e10 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76 6f 69 64 *pOldHdr;. void
86e20 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73 65 72 74 *pNew;. assert
86e30 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d ( mem.disallow==
86e40 30 20 29 3b 0a 20 20 70 4f 6c 64 48 64 72 20 3d 0 );. pOldHdr =
86e50 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 sqlite3MemsysGe
86e60 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b tHeader(pPrior);
86e70 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 . pNew = sqlite
86e80 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 3MemMalloc(nByte
86e90 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b );. if( pNew ){
86ea0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 . memcpy(pNew
86eb0 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 3c , pPrior, nByte<
86ec0 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 3f pOldHdr->iSize ?
86ed0 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64 48 64 72 nByte : pOldHdr
86ee0 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 ->iSize);. if
86ef0 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48 64 72 2d ( nByte>pOldHdr-
86f00 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 >iSize ){.
86f10 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 memset(&((char*)
86f20 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72 2d 3e 69 pNew)[pOldHdr->i
86f30 53 69 7a 65 5d 2c 20 30 78 32 62 2c 20 6e 42 79 Size], 0x2b, nBy
86f40 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d 3e 69 53 te - pOldHdr->iS
86f50 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ize);. }.
86f60 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 70 sqlite3MemFree(p
86f70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 72 65 Prior);. }. re
86f80 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a turn pNew;.}../*
86f90 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 .** Populate the
86fa0 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 low-level memor
86fb0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e y allocation fun
86fc0 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 ction pointers i
86fd0 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 n.** sqlite3Glob
86fe0 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 alConfig.m with
86ff0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 pointers to the
87000 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 routines in this
87010 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
87020 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
87030 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 lite3MemSetDefau
87040 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 lt(void){. stat
87050 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
87060 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 _mem_methods def
87070 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a aultMethods = {.
87080 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d sqlite3MemM
87090 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 alloc,. sqli
870a0 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 te3MemFree,.
870b0 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c sqlite3MemReall
870c0 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 oc,. sqlite3
870d0 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 MemSize,. sq
870e0 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c lite3MemRoundup,
870f0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d . sqlite3Mem
87100 49 6e 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 Init,. sqlit
87110 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 e3MemShutdown,.
87120 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 0. };. sql
87130 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 ite3_config(SQLI
87140 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 TE_CONFIG_MALLOC
87150 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 , &defaultMethod
87160 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 s);.}../*.** Set
87170 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
87180 61 63 6b 74 72 61 63 65 20 6c 65 76 65 6c 73 20 acktrace levels
87190 6b 65 70 74 20 66 6f 72 20 65 61 63 68 20 61 6c kept for each al
871a0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20 41 20 76 location..** A v
871b0 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20 74 75 72 alue of zero tur
871c0 6e 73 20 6f 66 66 20 62 61 63 6b 74 72 61 63 69 ns off backtraci
871d0 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 ng. The number
871e0 69 73 20 61 6c 77 61 79 73 20 72 6f 75 6e 64 65 is always rounde
871f0 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20 6d 75 6c d.** up to a mul
87200 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a 2f 0a 53 tiple of 2..*/.S
87210 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
87220 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 id sqlite3Memdeb
87230 75 67 42 61 63 6b 74 72 61 63 65 28 69 6e 74 20 ugBacktrace(int
87240 64 65 70 74 68 29 7b 0a 20 20 69 66 28 20 64 65 depth){. if( de
87250 70 74 68 3c 30 20 29 7b 20 64 65 70 74 68 20 3d pth<0 ){ depth =
87260 20 30 3b 20 7d 0a 20 20 69 66 28 20 64 65 70 74 0; }. if( dept
87270 68 3e 32 30 20 29 7b 20 64 65 70 74 68 20 3d 20 h>20 ){ depth =
87280 32 30 3b 20 7d 0a 20 20 64 65 70 74 68 20 3d 20 20; }. depth =
87290 28 64 65 70 74 68 2b 31 29 26 30 78 66 65 3b 0a (depth+1)&0xfe;.
872a0 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 mem.nBacktrace
872b0 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a 53 51 4c = depth;.}..SQL
872c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
872d0 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 sqlite3Memdebug
872e0 42 61 63 6b 74 72 61 63 65 43 61 6c 6c 62 61 63 BacktraceCallbac
872f0 6b 28 76 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 k(void (*xBacktr
87300 61 63 65 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 ace)(int, int, v
87310 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d 65 6d 2e oid **)){. mem.
87320 78 42 61 63 6b 74 72 61 63 65 20 3d 20 78 42 61 xBacktrace = xBa
87330 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a cktrace;.}../*.*
87340 2a 20 53 65 74 20 74 68 65 20 74 69 74 6c 65 20 * Set the title
87350 73 74 72 69 6e 67 20 66 6f 72 20 73 75 62 73 65 string for subse
87360 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e quent allocation
87370 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
87380 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
87390 33 4d 65 6d 64 65 62 75 67 53 65 74 74 69 74 6c 3MemdebugSettitl
873a0 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 e(const char *zT
873b0 69 74 6c 65 29 7b 0a 20 20 75 6e 73 69 67 6e 65 itle){. unsigne
873c0 64 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 d int n = sqlite
873d0 33 53 74 72 6c 65 6e 33 30 28 7a 54 69 74 6c 65 3Strlen30(zTitle
873e0 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 ) + 1;. sqlite3
873f0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
87400 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e .mutex);. if( n
87410 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 >=sizeof(mem.zTi
87420 74 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65 6f tle) ) n = sizeo
87430 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31 3b f(mem.zTitle)-1;
87440 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a 54 . memcpy(mem.zT
87450 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e 29 itle, zTitle, n)
87460 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b 6e ;. mem.zTitle[n
87470 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54 69 ] = 0;. mem.nTi
87480 74 6c 65 20 3d 20 52 4f 55 4e 44 38 28 6e 29 3b tle = ROUND8(n);
87490 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 . sqlite3_mutex
874a0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78 _leave(mem.mutex
874b0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 );.}..SQLITE_PRI
874c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
874d0 33 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b 3MemdebugSync(){
874e0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f . struct MemBlo
874f0 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66 ckHdr *pHdr;. f
87500 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 or(pHdr=mem.pFir
87510 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70 st; pHdr; pHdr=p
87520 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 Hdr->pNext){.
87530 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76 void **pBt = (v
87540 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20 oid**)pHdr;.
87550 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61 pBt -= pHdr->nBa
87560 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20 cktraceSlots;.
87570 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 mem.xBacktrace
87580 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48 (pHdr->iSize, pH
87590 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31 dr->nBacktrace-1
875a0 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a , &pBt[1]);. }.
875b0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 }../*.** Open th
875c0 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 e file indicated
875d0 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 and write a log
875e0 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 of all unfreed
875f0 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 memory .** alloc
87600 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 ations into that
87610 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f log..*/.SQLITE_
87620 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
87630 69 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70 ite3MemdebugDump
87640 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 (const char *zFi
87650 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 lename){. FILE
87660 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d *out;. struct M
87670 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 emBlockHdr *pHdr
87680 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a ;. void **pBt;.
87690 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d int i;. out =
876a0 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 fopen(zFilename
876b0 2c 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75 , "w");. if( ou
876c0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69 t==0 ){. fpri
876d0 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 ntf(stderr, "**
876e0 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 Unable to output
876f0 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 memory debug ou
87700 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c tput log: %s **\
87710 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
87720 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d zFilenam
87730 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a e);. return;.
87740 20 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d }. for(pHdr=m
87750 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b em.pFirst; pHdr;
87760 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 pHdr=pHdr->pNex
87770 74 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 t){. char *z
87780 3d 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20 = (char*)pHdr;.
87790 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 z -= pHdr->nB
877a0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69 acktraceSlots*si
877b0 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48 zeof(void*) + pH
877c0 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 dr->nTitle;.
877d0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a fprintf(out, "**
877e0 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74 ** %lld bytes at
877f0 20 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a %p from %s ****
87800 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 \n", .
87810 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26 pHdr->iSize, &
87820 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e pHdr[1], pHdr->n
87830 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f Title ? z : "???
87840 22 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72 ");. if( pHdr
87850 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a ->nBacktrace ){.
87860 20 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74 fflush(out
87870 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28 );. pBt = (
87880 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 void**)pHdr;.
87890 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e pBt -= pHdr->
878a0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b nBacktraceSlots;
878b0 0a 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65 . backtrace
878c0 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c _symbols_fd(pBt,
878d0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 pHdr->nBacktrac
878e0 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b e, fileno(out));
878f0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f . fprintf(o
87900 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d ut, "\n");. }
87910 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f . }. fprintf(o
87920 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29 ut, "COUNTS:\n")
87930 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e ;. for(i=0; i<N
87940 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20 CSIZE-1; i++){.
87950 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f if( mem.nAllo
87960 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70 c[i] ){. fp
87970 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 rintf(out, " %
87980 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 5d: %10d %10d %1
87990 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 0d\n", .
879a0 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c i*8, mem.nAl
879b0 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72 loc[i], mem.nCur
879c0 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43 rent[i], mem.mxC
879d0 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 urrent[i]);.
879e0 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e }. }. if( mem.
879f0 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d nAlloc[NCSIZE-1]
87a00 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ){. fprintf(
87a10 6f 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31 out, " %5d: %1
87a20 30 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c 0d %10d %10d\n",
87a30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43 . NC
87a40 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41 SIZE*8-8, mem.nA
87a50 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a lloc[NCSIZE-1],.
87a60 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d mem
87a70 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 .nCurrent[NCSIZE
87a80 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65 -1], mem.mxCurre
87a90 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20 nt[NCSIZE-1]);.
87aa0 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 }. fclose(out)
87ab0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
87ac0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
87ad0 74 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d times sqlite3Mem
87ae0 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65 Malloc() has bee
87af0 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c n called..*/.SQL
87b00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
87b10 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d sqlite3MemdebugM
87b20 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20 allocCount(){.
87b30 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f int i;. int nTo
87b40 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 tal = 0;. for(i
87b50 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b =0; i<NCSIZE; i+
87b60 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b +){. nTotal +
87b70 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b = mem.nAlloc[i];
87b80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54 . }. return nT
87b90 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66 otal;.}...#endif
87ba0 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 /* SQLITE_MEMDE
87bb0 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a BUG */../*******
87bc0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
87bd0 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em2.c **********
87be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87c00 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
87c10 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
87c20 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a le mem3.c ******
87c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87c50 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
87c60 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a 07 October 14.**
87c70 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
87c80 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
87c90 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
87ca0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
87cb0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
87cc0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
87cd0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
87ce0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
87cf0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
87d00 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
87d10 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
87d20 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
87d30 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
87d40 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
87d50 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
87d60 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
87d70 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
87d80 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
87d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
87dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
87dd0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
87de0 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f ns the C functio
87df0 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e ns that implemen
87e00 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c t a memory.** al
87e10 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 location subsyst
87e20 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 em for use by SQ
87e30 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 Lite. .**.** Thi
87e40 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 s version of the
87e50 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
87e60 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69 on subsystem omi
87e70 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66 ts all.** use of
87e80 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53 malloc(). The S
87e90 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c QLite user suppl
87ea0 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d ies a block of m
87eb0 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 emory.** before
87ec0 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f calling sqlite3_
87ed0 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f initialize() fro
87ee0 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 m which allocati
87ef0 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 ons.** are made
87f00 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 and returned by
87f10 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e the xMalloc() an
87f20 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a d xRealloc() .**
87f30 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
87f40 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 . Once sqlite3_i
87f50 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 nitialize() has
87f60 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 been called,.**
87f70 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 the amount of me
87f80 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 mory available t
87f90 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 o SQLite is fixe
87fa0 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 d and cannot.**
87fb0 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a be changed..**.*
87fc0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
87fd0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
87fe0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
87ff0 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a m is included.**
88000 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e in the build on
88010 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ly if SQLITE_ENA
88020 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64 BLE_MEMSYS3 is d
88030 65 66 69 6e 65 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a efined..*/../*.*
88040 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
88050 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
88060 6f 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 ocator is only b
88070 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 uilt into the li
88080 62 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f brary.** SQLITE_
88090 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 ENABLE_MEMSYS3 i
880a0 73 20 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e s defined. Defin
880b0 69 6e 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 ing this symbol
880c0 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e does not.** mean
880d0 20 74 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 that the librar
880e0 79 20 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d y will use a mem
880f0 6f 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 ory-pool by defa
88100 75 6c 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a ult, just that.*
88110 2a 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c * it is availabl
88120 65 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 e. The mempool a
88130 6c 6c 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 llocator is acti
88140 76 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 vated by calling
88150 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 .** sqlite3_conf
88160 69 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ig()..*/.#ifdef
88170 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 SQLITE_ENABLE_ME
88180 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 MSYS3../*.** Max
88190 69 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 imum size (in Me
881a0 6d 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 m3Blocks) of a "
881b0 73 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f small" chunk..*/
881c0 0a 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c .#define MX_SMAL
881d0 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d L 10.../*.** Num
881e0 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 ber of freelist
881f0 68 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 hash slots.*/.#d
88200 65 66 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 efine N_HASH 61
88210 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 ../*.** A memory
88220 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 allocation (als
88230 6f 20 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e o called a "chun
88240 6b 22 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 k") consists of
88250 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 two or .** more
88260 62 6c 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 blocks where eac
88270 68 20 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 h block is 8 byt
88280 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 es. The first 8
88290 20 62 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 bytes are .** a
882a0 20 68 65 61 64 65 72 20 74 68 61 74 20 69 73 20 header that is
882b0 6e 6f 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 not returned to
882c0 74 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 the user..**.**
882d0 41 20 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f A chunk is two o
882e0 72 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 r more blocks th
882f0 61 74 20 69 73 20 65 69 74 68 65 72 20 63 68 65 at is either che
88300 63 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 cked out or.** f
88310 72 65 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 ree. The first
88320 62 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 block has format
88330 20 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 u.hdr. u.hdr.s
88340 69 7a 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 ize4x is 4 times
88350 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 the.** size of
88360 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 the allocation i
88370 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 n blocks if the
88380 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 allocation is fr
88390 65 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 ee..** The u.hdr
883a0 2e 73 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 .size4x&1 bit is
883b0 20 74 72 75 65 20 69 66 20 74 68 65 20 63 68 75 true if the chu
883c0 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 nk is checked ou
883d0 74 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 t and.** false i
883e0 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f f the chunk is o
883f0 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 n the freelist.
88400 20 54 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 The u.hdr.size4
88410 78 26 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 x&2 bit.** is tr
88420 75 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f ue if the previo
88430 75 73 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 us chunk is chec
88440 6b 65 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 ked out and fals
88450 65 20 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 e if the.** prev
88460 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 ious chunk is fr
88470 65 65 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 ee. The u.hdr.p
88480 72 65 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 revSize field is
88490 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 the size of.**
884a0 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 the previous chu
884b0 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 nk in blocks if
884c0 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 the previous chu
884d0 6e 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 nk is on the.**
884e0 66 72 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 freelist. If the
884f0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 previous chunk
88500 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 is checked out,
88510 74 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 then.** u.hdr.pr
88520 65 76 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 evSize can be pa
88530 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 rt of the data f
88540 6f 72 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e or that chunk an
88550 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 d should.** not
88560 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 be read or writt
88570 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 en..**.** We oft
88580 65 6e 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 en identify a ch
88590 75 6e 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 unk by its index
885a0 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d in mem3.aPool[]
885b0 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 . When.** this
885c0 69 73 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 is done, the chu
885d0 6e 6b 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 nk index refers
885e0 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c to the second bl
885f0 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 ock of.** the ch
88600 75 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 unk. In this wa
88610 79 2c 20 74 68 65 20 66 69 72 73 74 20 63 68 75 y, the first chu
88620 6e 6b 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 nk has an index
88630 6f 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b of 1..** A chunk
88640 20 69 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e index of 0 mean
88650 73 20 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b s "no such chunk
88660 22 20 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 " and is the equ
88670 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 ivalent.** of a
88680 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a NULL pointer..**
88690 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 .** The second b
886a0 6c 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 lock of free chu
886b0 6e 6b 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f nks is of the fo
886c0 72 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a rm u.list. The.
886d0 2a 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f ** two fields fo
886e0 72 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b rm a double-link
886f0 65 64 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b ed list of chunk
88700 73 20 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a s of related siz
88710 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 es..** Pointers
88720 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 to the head of t
88730 68 65 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 he list are stor
88740 65 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 ed in mem3.aiSma
88750 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 ll[] .** for sma
88760 6c 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 ller chunks and
88770 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f mem3.aiHash[] fo
88780 72 20 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e r larger chunks.
88790 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e .**.** The secon
887a0 64 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 d block of a chu
887b0 6e 6b 20 69 73 20 75 73 65 72 20 64 61 74 61 20 nk is user data
887c0 69 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 if the chunk is
887d0 63 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e checked .** out.
887e0 20 20 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 If a chunk is
887f0 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 checked out, the
88800 20 75 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 user data may e
88810 78 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 xtend into.** th
88820 65 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 e u.hdr.prevSize
88830 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f value of the fo
88840 6c 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a llowing chunk..*
88850 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 /.typedef struct
88860 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 Mem3Block Mem3B
88870 6c 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d lock;.struct Mem
88880 33 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 3Block {. union
88890 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a {. struct {.
888a0 20 20 20 20 20 20 75 33 32 20 70 72 65 76 53 69 u32 prevSi
888b0 7a 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 ze; /* Size of
888c0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 previous chunk
888d0 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 in Mem3Block ele
888e0 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 ments */. u
888f0 33 32 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 32 size4x; /
88900 2a 20 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 * 4x the size of
88910 20 63 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 current chunk i
88920 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d n Mem3Block elem
88930 65 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 ents */. } hd
88940 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a r;. struct {.
88950 20 20 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 u32 next;
88960 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 /* Index i
88970 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f n mem3.aPool[] o
88980 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e f next free chun
88990 6b 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 k */. u32 p
889a0 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e rev; /* In
889b0 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f dex in mem3.aPoo
889c0 6c 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 l[] of previous
889d0 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 free chunk */.
889e0 20 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b } list;. } u;
889f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f .};../*.** All o
88a00 66 20 74 68 65 20 73 74 61 74 69 63 20 76 61 72 f the static var
88a10 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 iables used by t
88a20 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 his module are c
88a30 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f ollected.** into
88a40 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 a single struct
88a50 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 ure named "mem3"
88a60 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 . This is to ke
88a70 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 ep the.** static
88a80 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e variables organ
88a90 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 ized and to redu
88aa0 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c ce namespace pol
88ab0 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 lution.** when t
88ac0 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f his module is co
88ad0 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 mbined with othe
88ae0 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d r in the amalgam
88af0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ation..*/.static
88b00 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 SQLITE_WSD stru
88b10 63 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a ct Mem3Global {.
88b20 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 /*. ** Memory
88b30 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 available for a
88b40 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c llocation. nPool
88b50 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 is the size of
88b60 74 68 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 the array. ** (
88b70 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 in Mem3Blocks) p
88b80 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f ointed to by aPo
88b90 6f 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a ol less 2.. */.
88ba0 20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d u32 nPool;. M
88bb0 65 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b em3Block *aPool;
88bc0 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 .. /*. ** True
88bd0 20 69 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 if we are evalu
88be0 61 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d ating an out-of-
88bf0 6d 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e memory callback.
88c00 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 . */. int alar
88c10 6d 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 mBusy;. . /*.
88c20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e ** Mutex to con
88c30 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 trol access to t
88c40 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
88c50 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a tion subsystem..
88c60 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d */. sqlite3_m
88c70 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a utex *mutex;. .
88c80 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 /*. ** The mi
88c90 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 nimum amount of
88ca0 66 72 65 65 20 73 70 61 63 65 20 74 68 61 74 20 free space that
88cb0 77 65 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 we have seen..
88cc0 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 */. u32 mnMaste
88cd0 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d r;.. /*. ** iM
88ce0 61 73 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 aster is the ind
88cf0 65 78 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 ex of the master
88d00 20 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 chunk. Most ne
88d10 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 w allocations.
88d20 2a 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 ** occur off of
88d30 74 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d this chunk. szM
88d40 61 73 74 65 72 20 69 73 20 74 68 65 20 73 69 7a aster is the siz
88d50 65 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 e (in Mem3Blocks
88d60 29 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 ). ** of the cu
88d70 72 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 rrent master. i
88d80 4d 61 73 74 65 72 20 69 73 20 30 20 69 66 20 74 Master is 0 if t
88d90 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 here is not mast
88da0 65 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 er chunk.. ** T
88db0 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 he master chunk
88dc0 69 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 is not in either
88dd0 20 74 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 the aiHash[] or
88de0 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f aiSmall[].. */
88df0 0a 20 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a . u32 iMaster;.
88e00 20 20 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a u32 szMaster;.
88e10 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 . /*. ** Array
88e20 20 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 of lists of fre
88e30 65 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 e blocks accordi
88e40 6e 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 ng to the block
88e50 73 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 size . ** for s
88e60 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f maller chunks, o
88e70 72 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 r a hash on the
88e80 62 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c block size for l
88e90 61 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b arger. ** chunk
88ea0 73 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 s.. */. u32 ai
88eb0 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 Small[MX_SMALL-1
88ec0 5d 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 ]; /* For size
88ed0 73 20 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 s 2 through MX_S
88ee0 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 MALL, inclusive
88ef0 2a 2f 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b */. u32 aiHash[
88f00 4e 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 N_HASH];
88f10 2f 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f /* For sizes MX_
88f20 53 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 SMALL+1 and larg
88f30 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b er */.} mem3 = {
88f40 20 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 97535575 };..#d
88f50 65 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 efine mem3 GLOBA
88f60 4c 28 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f L(struct Mem3Glo
88f70 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a bal, mem3)../*.*
88f80 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 * Unlink the chu
88f90 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c nk at mem3.aPool
88fa0 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 [i] from list it
88fb0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a is currently.**
88fc0 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 on. *pRoot is
88fd0 74 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 the list that i
88fe0 69 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a is a member of..
88ff0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
89000 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d emsys3UnlinkFrom
89010 4c 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 List(u32 i, u32
89020 2a 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e *pRoot){. u32 n
89030 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c ext = mem3.aPool
89040 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b [i].u.list.next;
89050 0a 20 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 . u32 prev = me
89060 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 m3.aPool[i].u.li
89070 73 74 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 st.prev;. asser
89080 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
89090 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 _held(mem3.mutex
890a0 29 20 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d ) );. if( prev=
890b0 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 =0 ){. *pRoot
890c0 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 = next;. }else
890d0 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c {. mem3.aPool
890e0 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 [prev].u.list.ne
890f0 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 xt = next;. }.
89100 20 69 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 if( next ){.
89110 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 mem3.aPool[next
89120 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 ].u.list.prev =
89130 70 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 prev;. }. mem3
89140 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 .aPool[i].u.list
89150 2e 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d .next = 0;. mem
89160 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 3.aPool[i].u.lis
89170 74 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f t.prev = 0;.}../
89180 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 *.** Unlink the
89190 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 chunk at index i
891a0 20 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 from .** whatev
891b0 65 72 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 er list is curre
891c0 6e 74 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 ntly a member of
891d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
891e0 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 memsys3Unlink(u
891f0 33 32 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 32 i){. u32 siz
89200 65 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 e, hash;. asser
89210 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
89220 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 _held(mem3.mutex
89230 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 ) );. assert( (
89240 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
89250 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 u.hdr.size4x & 1
89260 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 )==0 );. assert
89270 28 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 ( i>=1 );. size
89280 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
89290 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
892a0 34 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 4;. assert( siz
892b0 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b e==mem3.aPool[i+
892c0 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 size-1].u.hdr.pr
892d0 65 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 evSize );. asse
892e0 72 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 rt( size>=2 );.
892f0 20 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f if( size <= MX_
89300 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d SMALL ){. mem
89310 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 sys3UnlinkFromLi
89320 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d st(i, &mem3.aiSm
89330 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 all[size-2]);.
89340 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 }else{. hash
89350 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b = size % N_HASH;
89360 0a 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 . memsys3Unli
89370 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d nkFromList(i, &m
89380 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d em3.aiHash[hash]
89390 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
893a0 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 Link the chunk a
893b0 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 t mem3.aPool[i]
893c0 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 so that is on th
893d0 65 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a e list rooted.**
893e0 20 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 at *pRoot..*/.s
893f0 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 tatic void memsy
89400 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 s3LinkIntoList(u
89410 33 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 32 i, u32 *pRoot
89420 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
89430 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
89440 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem3.mutex) );.
89450 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 mem3.aPool[i].u
89460 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 .list.next = *pR
89470 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f oot;. mem3.aPoo
89480 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 l[i].u.list.prev
89490 20 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f = 0;. if( *pRo
894a0 6f 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 ot ){. mem3.a
894b0 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c Pool[*pRoot].u.l
894c0 69 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 ist.prev = i;.
894d0 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a }. *pRoot = i;.
894e0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 }../*.** Link th
894f0 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 e chunk at index
89500 20 69 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 i into either t
89510 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a he appropriate.*
89520 2a 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 * small chunk li
89530 73 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 st, or into the
89540 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 large chunk hash
89550 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 table..*/.stati
89560 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 c void memsys3Li
89570 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 nk(u32 i){. u32
89580 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 size, hash;. a
89590 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
895a0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d utex_held(mem3.m
895b0 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
895c0 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 t( i>=1 );. ass
895d0 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c ert( (mem3.aPool
895e0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
895f0 34 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 4x & 1)==0 );.
89600 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f size = mem3.aPoo
89610 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a l[i-1].u.hdr.siz
89620 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 e4x/4;. assert(
89630 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f size==mem3.aPoo
89640 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 l[i+size-1].u.hd
89650 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 r.prevSize );.
89660 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 assert( size>=2
89670 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d );. if( size <=
89680 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 MX_SMALL ){.
89690 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f memsys3LinkInto
896a0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 List(i, &mem3.ai
896b0 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a Small[size-2]);.
896c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 }else{. has
896d0 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 h = size % N_HAS
896e0 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 H;. memsys3Li
896f0 6e 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d nkIntoList(i, &m
89700 65 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d em3.aiHash[hash]
89710 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
89720 49 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 If the STATIC_ME
89730 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 M mutex is not a
89740 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 lready held, obt
89750 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 ain it now. The
89760 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c mutex.** will al
89770 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f ready be held (o
89780 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 btained by code
89790 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a in malloc.c) if.
897a0 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c ** sqlite3Global
897b0 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 Config.bMemStat
897c0 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 is true..*/.stat
897d0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 ic void memsys3E
897e0 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 nter(void){. if
897f0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
89800 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d onfig.bMemstat==
89810 30 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 0 && mem3.mutex=
89820 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d =0 ){. mem3.m
89830 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 utex = sqlite3Mu
89840 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
89850 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d MUTEX_STATIC_MEM
89860 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
89870 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d _mutex_enter(mem
89880 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 3.mutex);.}.stat
89890 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c ic void memsys3L
898a0 65 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 eave(void){. sq
898b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 lite3_mutex_leav
898c0 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d e(mem3.mutex);.}
898d0 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 ../*.** Called w
898e0 68 65 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c hen we are unabl
898f0 65 20 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 e to satisfy an
89900 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 allocation of nB
89910 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ytes..*/.static
89920 76 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f void memsys3OutO
89930 66 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 fMemory(int nByt
89940 65 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e e){. if( !mem3.
89950 61 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 alarmBusy ){.
89960 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 mem3.alarmBusy
89970 3d 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 = 1;. assert(
89980 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
89990 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 eld(mem3.mutex)
899a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
899b0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e utex_leave(mem3.
899c0 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 mutex);. sqli
899d0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f te3_release_memo
899e0 72 79 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 ry(nByte);. s
899f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
89a00 65 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a er(mem3.mutex);.
89a10 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 mem3.alarmBu
89a20 73 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a sy = 0;. }.}...
89a30 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 /*.** Chunk i is
89a40 20 61 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 a free chunk th
89a50 61 74 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 at has been unli
89a60 6e 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 nked. Adjust it
89a70 73 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d s .** size param
89a80 65 74 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d eters for check-
89a90 6f 75 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 out and return a
89aa0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
89ab0 0a 2a 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e .** user portion
89ac0 20 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a of the chunk..*
89ad0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d /.static void *m
89ae0 65 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 emsys3Checkout(u
89af0 33 32 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 32 i, u32 nBlock
89b00 29 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 ){. u32 x;. as
89b10 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
89b20 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 tex_held(mem3.mu
89b30 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
89b40 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 ( i>=1 );. asse
89b50 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 rt( mem3.aPool[i
89b60 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 -1].u.hdr.size4x
89b70 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 /4==nBlock );.
89b80 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f assert( mem3.aPo
89b90 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 ol[i+nBlock-1].u
89ba0 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e .hdr.prevSize==n
89bb0 42 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d Block );. x = m
89bc0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
89bd0 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d .hdr.size4x;. m
89be0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 em3.aPool[i-1].u
89bf0 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 .hdr.size4x = nB
89c00 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 lock*4 | 1 | (x&
89c10 32 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 2);. mem3.aPool
89c20 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 [i+nBlock-1].u.h
89c30 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 dr.prevSize = nB
89c40 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f lock;. mem3.aPo
89c50 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 ol[i+nBlock-1].u
89c60 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 .hdr.size4x |= 2
89c70 3b 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 ;. return &mem3
89c80 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a .aPool[i];.}../*
89c90 0a 2a 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 .** Carve a piec
89ca0 65 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 e off of the end
89cb0 20 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 of the mem3.iMa
89cc0 73 74 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e ster free chunk.
89cd0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
89ce0 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 nter to the new
89cf0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c allocation. Or,
89d00 20 69 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 if the master c
89d10 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c hunk.** is not l
89d20 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 arge enough, ret
89d30 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 urn 0..*/.static
89d40 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 void *memsys3Fr
89d50 6f 6d 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c omMaster(u32 nBl
89d60 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ock){. assert(
89d70 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
89d80 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
89d90 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 ;. assert( mem3
89da0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 .szMaster>=nBloc
89db0 6b 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 k );. if( nBloc
89dc0 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 k>=mem3.szMaster
89dd0 2d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 -1 ){. /* Use
89de0 20 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 the entire mast
89df0 65 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a er */. void *
89e00 70 20 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b p = memsys3Check
89e10 6f 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 out(mem3.iMaster
89e20 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 , mem3.szMaster)
89e30 3b 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 ;. mem3.iMast
89e40 65 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 er = 0;. mem3
89e50 2e 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 .szMaster = 0;.
89e60 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 mem3.mnMaster
89e70 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
89e80 20 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 p;. }else{.
89e90 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 /* Split the ma
89ea0 73 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 ster block. Ret
89eb0 75 72 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f urn the tail. */
89ec0 0a 20 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 . u32 newi, x
89ed0 3b 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d ;. newi = mem
89ee0 33 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 3.iMaster + mem3
89ef0 2e 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f .szMaster - nBlo
89f00 63 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ck;. assert(
89f10 6e 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 newi > mem3.iMas
89f20 74 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d ter+1 );. mem
89f30 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 3.aPool[mem3.iMa
89f40 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 ster+mem3.szMast
89f50 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 er-1].u.hdr.prev
89f60 53 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 Size = nBlock;.
89f70 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 mem3.aPool[me
89f80 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e m3.iMaster+mem3.
89f90 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 szMaster-1].u.hd
89fa0 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 r.size4x |= 2;.
89fb0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 mem3.aPool[ne
89fc0 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 wi-1].u.hdr.size
89fd0 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 4x = nBlock*4 +
89fe0 31 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 1;. mem3.szMa
89ff0 73 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a ster -= nBlock;.
8a000 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e mem3.aPool[n
8a010 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ewi-1].u.hdr.pre
8a020 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d vSize = mem3.szM
8a030 61 73 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d aster;. x = m
8a040 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8a050 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e Master-1].u.hdr.
8a060 73 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 size4x & 2;.
8a070 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e mem3.aPool[mem3.
8a080 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 iMaster-1].u.hdr
8a090 2e 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 .size4x = mem3.s
8a0a0 7a 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 zMaster*4 | x;.
8a0b0 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 if( mem3.szMa
8a0c0 73 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 ster < mem3.mnMa
8a0d0 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 ster ){. me
8a0e0 6d 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 m3.mnMaster = me
8a0f0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 m3.szMaster;.
8a100 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 }. return (v
8a110 6f 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c oid*)&mem3.aPool
8a120 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f [newi];. }.}../
8a130 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 *.** *pRoot is t
8a140 68 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 he head of a lis
8a150 74 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 t of free chunks
8a160 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a of the same siz
8a170 65 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a e.** or same siz
8a180 65 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 e hash. In othe
8a190 72 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 r words, *pRoot
8a1a0 69 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 is an entry in e
8a1b0 69 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 ither.** mem3.ai
8a1c0 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e Small[] or mem3.
8a1d0 61 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a aiHash[]. .**.*
8a1e0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 * This routine e
8a1f0 78 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 xamines all entr
8a200 69 65 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e ies on the given
8a210 20 6c 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a list and tries.
8a220 2a 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 ** to coalesce e
8a230 61 63 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 ach entries with
8a240 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 adjacent free c
8a250 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 hunks. .**.** I
8a260 66 20 69 74 20 73 65 65 73 20 61 20 63 68 75 6e f it sees a chun
8a270 6b 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 k that is larger
8a280 20 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 than mem3.iMast
8a290 65 72 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 er, it replaces
8a2a0 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 .** the current
8a2b0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 mem3.iMaster wit
8a2c0 68 20 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 h the new larger
8a2d0 20 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 chunk. In orde
8a2e0 72 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 r for.** this me
8a2f0 6d 33 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 m3.iMaster repla
8a300 63 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 cement to work,
8a310 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b the master chunk
8a320 20 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b must be.** link
8a330 65 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 ed into the hash
8a340 20 74 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 tables. That i
8a350 73 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c s not the normal
8a360 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 state of.** aff
8a370 61 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e airs, of course.
8a380 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f The calling ro
8a390 75 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 utine must link
8a3a0 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 the master.** ch
8a3b0 75 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b unk before invok
8a3c0 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ing this routine
8a3d0 2c 20 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 , then must unli
8a3e0 6e 6b 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 nk the (possibly
8a3f0 0a 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 .** changed) mas
8a400 74 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 ter chunk once t
8a410 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 his routine has
8a420 66 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 finished..*/.sta
8a430 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 tic void memsys3
8a440 4d 65 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 Merge(u32 *pRoot
8a450 29 7b 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 ){. u32 iNext,
8a460 70 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 prev, size, i, x
8a470 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
8a480 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
8a490 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 mem3.mutex) );.
8a4a0 20 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 for(i=*pRoot; i
8a4b0 3e 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 >0; i=iNext){.
8a4c0 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 iNext = mem3.a
8a4d0 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[i].u.list.n
8a4e0 65 78 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 ext;. size =
8a4f0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e mem3.aPool[i-1].
8a500 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 u.hdr.size4x;.
8a510 20 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 assert( (size&
8a520 31 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 1)==0 );. if(
8a530 20 28 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a (size&2)==0 ){.
8a540 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c memsys3Unl
8a550 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 inkFromList(i, p
8a560 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 Root);. ass
8a570 65 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 ert( i > mem3.aP
8a580 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 ool[i-1].u.hdr.p
8a590 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 revSize );.
8a5a0 20 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 prev = i - mem3
8a5b0 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 .aPool[i-1].u.hd
8a5c0 72 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 r.prevSize;.
8a5d0 20 20 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 if( prev==iNex
8a5e0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 t ){. iNe
8a5f0 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b xt = mem3.aPool[
8a600 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 prev].u.list.nex
8a610 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
8a620 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 memsys3Unlink(p
8a630 72 65 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 rev);. size
8a640 20 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 = i + size/4 -
8a650 70 72 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 prev;. x =
8a660 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d mem3.aPool[prev-
8a670 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 1].u.hdr.size4x
8a680 26 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e & 2;. mem3.
8a690 61 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e aPool[prev-1].u.
8a6a0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a hdr.size4x = siz
8a6b0 65 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d e*4 | x;. m
8a6c0 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 em3.aPool[prev+s
8a6d0 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 ize-1].u.hdr.pre
8a6e0 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 vSize = size;.
8a6f0 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 memsys3Link(
8a700 70 72 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d prev);. i =
8a710 20 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 prev;. }else
8a720 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 {. size /=
8a730 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 4;. }. if(
8a740 20 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 size>mem3.szMas
8a750 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d ter ){. mem
8a760 33 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 3.iMaster = i;.
8a770 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 mem3.szMast
8a780 65 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d er = size;. }
8a790 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
8a7a0 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 turn a block of
8a7b0 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 memory of at lea
8a7c0 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a st nBytes in siz
8a7d0 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c e..** Return NUL
8a7e0 4c 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a L if unable..**.
8a7f0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
8a800 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 assumes that th
8a810 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 e necessary mute
8a820 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 xes, if any, are
8a830 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 .** already held
8a840 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 by the caller.
8a850 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a Hence "Unsafe"..
8a860 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a */.static void *
8a870 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 memsys3MallocUns
8a880 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a afe(int nByte){.
8a890 20 20 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e u32 i;. u32 n
8a8a0 42 6c 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 Block;. u32 toF
8a8b0 72 65 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ree;.. assert(
8a8c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
8a8d0 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 ld(mem3.mutex) )
8a8e0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 ;. assert( size
8a8f0 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 of(Mem3Block)==8
8a900 20 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c );. if( nByte<
8a910 3d 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 =12 ){. nBloc
8a920 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a k = 2;. }else{.
8a930 20 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 nBlock = (nB
8a940 79 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d yte + 11)/8;. }
8a950 0a 20 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 . assert( nBloc
8a960 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 k>=2 );.. /* ST
8a970 45 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 EP 1:. ** Look
8a980 66 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 for an entry of
8a990 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 the correct size
8a9a0 20 69 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 in either the s
8a9b0 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 mall. ** chunk
8a9c0 74 61 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 table or in the
8a9d0 6c 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 large chunk hash
8a9e0 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 table. This is
8a9f0 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c . ** successful
8aa00 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d most of the tim
8aa10 65 20 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 e (about 9 times
8aa20 20 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a out of 10).. *
8aa30 2f 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c /. if( nBlock <
8aa40 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 = MX_SMALL ){.
8aa50 20 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 i = mem3.aiSma
8aa60 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 ll[nBlock-2];.
8aa70 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 if( i>0 ){.
8aa80 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b memsys3Unlink
8aa90 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d FromList(i, &mem
8aaa0 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 3.aiSmall[nBlock
8aab0 2d 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 -2]);. retu
8aac0 72 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f rn memsys3Checko
8aad0 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 ut(i, nBlock);.
8aae0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
8aaf0 20 20 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c int hash = nBl
8ab00 6f 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 ock % N_HASH;.
8ab10 20 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 for(i=mem3.aiH
8ab20 61 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 ash[hash]; i>0;
8ab30 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e i=mem3.aPool[i].
8ab40 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 u.list.next){.
8ab50 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f if( mem3.aPo
8ab60 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ol[i-1].u.hdr.si
8ab70 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 ze4x/4==nBlock )
8ab80 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 {. memsys
8ab90 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 3UnlinkFromList(
8aba0 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b i, &mem3.aiHash[
8abb0 68 61 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 hash]);.
8abc0 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 return memsys3Ch
8abd0 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b eckout(i, nBlock
8abe0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
8abf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 . }.. /* STEP
8ac00 32 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 2:. ** Try to s
8ac10 61 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 atisfy the alloc
8ac20 61 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 ation by carving
8ac30 20 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 a piece off of
8ac40 74 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 the end. ** of
8ac50 74 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b the master chunk
8ac60 2e 20 20 54 68 69 73 20 73 74 65 70 20 75 73 75 . This step usu
8ac70 61 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 ally works if st
8ac80 65 70 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f ep 1 fails.. */
8ac90 0a 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 . if( mem3.szMa
8aca0 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a ster>=nBlock ){.
8acb0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 return memsy
8acc0 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c s3FromMaster(nBl
8acd0 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a ock);. }... /*
8ace0 20 53 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 STEP 3: . **
8acf0 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 Loop through the
8ad00 20 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 entire memory p
8ad10 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 ool. Coalesce a
8ad20 64 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a djacent free. *
8ad30 2a 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d * chunks. Recom
8ad40 70 75 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 pute the master
8ad50 63 68 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 chunk as the lar
8ad60 67 65 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e gest free chunk.
8ad70 0a 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 . ** Then try a
8ad80 67 61 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 gain to satisfy
8ad90 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 the allocation b
8ada0 79 20 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 y carving a piec
8adb0 65 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 e off. ** of th
8adc0 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 e end of the mas
8add0 74 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 ter chunk. This
8ade0 20 73 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 step happens ve
8adf0 72 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 ry. ** rarely (
8ae00 77 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 we hope!). */.
8ae10 20 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f for(toFree=nBlo
8ae20 63 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d ck*16; toFree<(m
8ae30 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 em3.nPool*16); t
8ae40 6f 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 oFree *= 2){.
8ae50 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d memsys3OutOfMem
8ae60 6f 72 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 ory(toFree);.
8ae70 20 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 if( mem3.iMaste
8ae80 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 r ){. memsy
8ae90 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 s3Link(mem3.iMas
8aea0 74 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 ter);. mem3
8aeb0 2e 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 .iMaster = 0;.
8aec0 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 mem3.szMaste
8aed0 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 r = 0;. }.
8aee0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 for(i=0; i<N_HA
8aef0 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 SH; i++){.
8af00 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 memsys3Merge(&me
8af10 6d 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 m3.aiHash[i]);.
8af20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 }. for(i=0
8af30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 ; i<MX_SMALL-1;
8af40 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 i++){. mems
8af50 79 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 ys3Merge(&mem3.a
8af60 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 iSmall[i]);.
8af70 7d 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 }. if( mem3.s
8af80 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 zMaster ){.
8af90 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d memsys3Unlink(m
8afa0 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 em3.iMaster);.
8afb0 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d if( mem3.szM
8afc0 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b aster>=nBlock ){
8afd0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
8afe0 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 memsys3FromMaste
8aff0 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 r(nBlock);.
8b000 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
8b010 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 /* If none of th
8b020 65 20 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 e above worked,
8b030 74 68 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f then we fail. */
8b040 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
8b050 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 /*.** Free an ou
8b060 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 tstanding memory
8b070 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a allocation..**.
8b080 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
8b090 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 assumes that th
8b0a0 65 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 e necessary mute
8b0b0 78 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 xes, if any, are
8b0c0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 .** already held
8b0d0 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 by the caller.
8b0e0 48 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a Hence "Unsafe"..
8b0f0 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 */.void memsys3F
8b100 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a reeUnsafe(void *
8b110 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f pOld){. Mem3Blo
8b120 63 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f ck *p = (Mem3Blo
8b130 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 ck*)pOld;. int
8b140 69 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 i;. u32 size, x
8b150 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
8b160 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d te3_mutex_held(m
8b170 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 em3.mutex) );.
8b180 61 73 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 assert( p>mem3.a
8b190 50 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e Pool && p<&mem3.
8b1a0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c aPool[mem3.nPool
8b1b0 5d 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d ] );. i = p - m
8b1c0 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 em3.aPool;. ass
8b1d0 65 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c ert( (mem3.aPool
8b1e0 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 [i-1].u.hdr.size
8b1f0 34 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 4x&1)==1 );. si
8b200 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b ze = mem3.aPool[
8b210 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 i-1].u.hdr.size4
8b220 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 x/4;. assert( i
8b230 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f +size<=mem3.nPoo
8b240 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 l+1 );. mem3.aP
8b250 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 ool[i-1].u.hdr.s
8b260 69 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d ize4x &= ~1;. m
8b270 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 em3.aPool[i+size
8b280 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 -1].u.hdr.prevSi
8b290 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d ze = size;. mem
8b2a0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 3.aPool[i+size-1
8b2b0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 ].u.hdr.size4x &
8b2c0 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c = ~2;. memsys3L
8b2d0 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 ink(i);.. /* Tr
8b2e0 79 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 y to expand the
8b2f0 6d 61 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 master using the
8b300 20 6e 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 newly freed chu
8b310 6e 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 nk */. if( mem3
8b320 2e 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 .iMaster ){.
8b330 77 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f while( (mem3.aPo
8b340 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d ol[mem3.iMaster-
8b350 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 1].u.hdr.size4x&
8b360 32 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 2)==0 ){. s
8b370 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c ize = mem3.aPool
8b380 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
8b390 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b .u.hdr.prevSize;
8b3a0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 . mem3.iMas
8b3b0 74 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 ter -= size;.
8b3c0 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 mem3.szMaster
8b3d0 20 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 += size;.
8b3e0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 memsys3Unlink(me
8b3f0 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 m3.iMaster);.
8b400 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f x = mem3.aPoo
8b410 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 l[mem3.iMaster-1
8b420 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 ].u.hdr.size4x &
8b430 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 2;. mem3.a
8b440 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b450 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 r-1].u.hdr.size4
8b460 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 x = mem3.szMaste
8b470 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d r*4 | x;. m
8b480 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8b490 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 Master+mem3.szMa
8b4a0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 ster-1].u.hdr.pr
8b4b0 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a evSize = mem3.sz
8b4c0 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 Master;. }.
8b4d0 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c x = mem3.aPool
8b4e0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d [mem3.iMaster-1]
8b4f0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 .u.hdr.size4x &
8b500 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 2;. while( (m
8b510 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 em3.aPool[mem3.i
8b520 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 Master+mem3.szMa
8b530 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 ster-1].u.hdr.si
8b540 7a 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 ze4x&1)==0 ){.
8b550 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e memsys3Unlin
8b560 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d k(mem3.iMaster+m
8b570 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 em3.szMaster);.
8b580 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 mem3.szMast
8b590 65 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c er += mem3.aPool
8b5a0 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 [mem3.iMaster+me
8b5b0 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 m3.szMaster-1].u
8b5c0 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 .hdr.size4x/4;.
8b5d0 20 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b mem3.aPool[
8b5e0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e mem3.iMaster-1].
8b5f0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d u.hdr.size4x = m
8b600 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c em3.szMaster*4 |
8b610 20 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 x;. mem3.a
8b620 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 Pool[mem3.iMaste
8b630 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d r+mem3.szMaster-
8b640 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 1].u.hdr.prevSiz
8b650 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 e = mem3.szMaste
8b660 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a r;. }. }.}..
8b670 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
8b680 20 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 size of an outs
8b690 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 tanding allocati
8b6a0 6f 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 on, in bytes. T
8b6b0 68 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 he.** size retur
8b6c0 6e 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d ned omits the 8-
8b6d0 62 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 byte header over
8b6e0 68 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 head. This only
8b6f0 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 .** works for ch
8b700 75 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 unks that are cu
8b710 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 rrently checked
8b720 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 out..*/.static i
8b730 6e 74 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 nt memsys3Size(v
8b740 6f 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 oid *p){. Mem3B
8b750 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 lock *pBlock;.
8b760 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
8b770 6e 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 n 0;. pBlock =
8b780 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 (Mem3Block*)p;.
8b790 20 61 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b assert( (pBlock
8b7a0 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 [-1].u.hdr.size4
8b7b0 78 26 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 x&1)!=0 );. ret
8b7c0 75 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e urn (pBlock[-1].
8b7d0 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 u.hdr.size4x&~3)
8b7e0 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a *2 - 4;.}../*.**
8b7f0 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 Round up a requ
8b800 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 est size to the
8b810 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 next valid alloc
8b820 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 ation size..*/.s
8b830 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 tatic int memsys
8b840 33 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 3Roundup(int n){
8b850 0a 20 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a . if( n<=12 ){.
8b860 20 20 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 return 12;.
8b870 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
8b880 72 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d rn ((n+11)&~7) -
8b890 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 4;. }.}../*.**
8b8a0 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 Allocate nBytes
8b8b0 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 of memory..*/.s
8b8c0 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 tatic void *mems
8b8d0 79 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 ys3Malloc(int nB
8b8e0 79 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 ytes){. sqlite3
8b8f0 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 _int64 *p;. ass
8b900 65 72 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b ert( nBytes>0 );
8b910 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c /* mal
8b920 6c 6f 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 loc.c filters ou
8b930 74 20 30 20 62 79 74 65 20 72 65 71 75 65 73 74 t 0 byte request
8b940 73 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e s */. memsys3En
8b950 74 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d ter();. p = mem
8b960 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 sys3MallocUnsafe
8b970 28 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 (nBytes);. mems
8b980 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 ys3Leave();. re
8b990 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a turn (void*)p; .
8b9a0 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 }../*.** Free me
8b9b0 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 mory..*/.void me
8b9c0 6d 73 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a msys3Free(void *
8b9d0 70 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 pPrior){. asser
8b9e0 74 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d t( pPrior );. m
8b9f0 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 emsys3Enter();.
8ba00 20 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 memsys3FreeUnsa
8ba10 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 fe(pPrior);. me
8ba20 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a msys3Leave();.}.
8ba30 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
8ba40 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 e size of an exi
8ba50 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c sting memory all
8ba60 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 ocation.*/.void
8ba70 2a 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 *memsys3Realloc(
8ba80 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e void *pPrior, in
8ba90 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 t nBytes){. int
8baa0 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 nOld;. void *p
8bab0 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d ;. if( pPrior==
8bac0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
8bad0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e sqlite3_malloc(n
8bae0 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 Bytes);. }. if
8baf0 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 ( nBytes<=0 ){.
8bb00 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
8bb10 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 pPrior);. ret
8bb20 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c urn 0;. }. nOl
8bb30 64 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 d = memsys3Size(
8bb40 70 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e pPrior);. if( n
8bb50 42 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e Bytes<=nOld && n
8bb60 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 Bytes>=nOld-128
8bb70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 ){. return pP
8bb80 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 rior;. }. mems
8bb90 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 ys3Enter();. p
8bba0 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 = memsys3MallocU
8bbb0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 nsafe(nBytes);.
8bbc0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 if( p ){. if
8bbd0 28 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b ( nOld<nBytes ){
8bbe0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c . memcpy(p,
8bbf0 20 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a pPrior, nOld);.
8bc00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8bc10 20 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f memcpy(p, pPrio
8bc20 72 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 r, nBytes);.
8bc30 7d 0a 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 }. memsys3Fre
8bc40 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b eUnsafe(pPrior);
8bc50 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 . }. memsys3Le
8bc60 61 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 ave();. return
8bc70 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 p;.}../*.** Init
8bc80 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 ialize this modu
8bc90 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
8bca0 74 20 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f t memsys3Init(vo
8bcb0 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
8bcc0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
8bcd0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 (NotUsed);. if(
8bce0 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 !sqlite3GlobalC
8bcf0 6f 6e 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 onfig.pHeap ){.
8bd00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
8bd10 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f _ERROR;. }.. /
8bd20 2a 20 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 * Store a pointe
8bd30 72 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 r to the memory
8bd40 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 block in global
8bd50 73 74 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 structure mem3.
8bd60 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a */. assert( siz
8bd70 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d eof(Mem3Block)==
8bd80 38 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 8 );. mem3.aPoo
8bd90 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a l = (Mem3Block *
8bda0 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f )sqlite3GlobalCo
8bdb0 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 nfig.pHeap;. me
8bdc0 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 m3.nPool = (sqli
8bdd0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
8bde0 6e 48 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d nHeap / sizeof(M
8bdf0 65 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a em3Block)) - 2;.
8be00 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 . /* Initialize
8be10 20 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 the master bloc
8be20 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d k. */. mem3.szM
8be30 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f aster = mem3.nPo
8be40 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 ol;. mem3.mnMas
8be50 74 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 ter = mem3.szMas
8be60 74 65 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 ter;. mem3.iMas
8be70 74 65 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e ter = 1;. mem3.
8be80 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 aPool[0].u.hdr.s
8be90 69 7a 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a ize4x = (mem3.sz
8bea0 4d 61 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a Master<<2) + 2;.
8beb0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d mem3.aPool[mem
8bec0 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 3.nPool].u.hdr.p
8bed0 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e revSize = mem3.n
8bee0 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f Pool;. mem3.aPo
8bef0 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 ol[mem3.nPool].u
8bf00 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b .hdr.size4x = 1;
8bf10 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 .. return SQLIT
8bf20 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 E_OK;.}../*.** D
8bf30 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 einitialize this
8bf40 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 module..*/.stat
8bf50 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 ic void memsys3S
8bf60 68 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f hutdown(void *No
8bf70 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 tUsed){. UNUSED
8bf80 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 _PARAMETER(NotUs
8bf90 65 64 29 3b 0a 20 20 6d 65 6d 33 2e 6d 75 74 65 ed);. mem3.mute
8bfa0 78 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 3b x = 0;. return;
8bfb0 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e .}..../*.** Open
8bfc0 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63 61 the file indica
8bfd0 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61 20 ted and write a
8bfe0 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 log of all unfre
8bff0 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c ed memory .** al
8c000 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 locations into t
8c010 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 hat log..*/.SQLI
8c020 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
8c030 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 33 44 75 sqlite3Memsys3Du
8c040 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a mp(const char *z
8c050 46 69 6c 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65 Filename){.#ifde
8c060 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
8c070 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33 FILE *out;. u3
8c080 32 20 69 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69 2 i, j;. u32 si
8c090 7a 65 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e ze;. if( zFilen
8c0a0 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e ame==0 || zFilen
8c0b0 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 ame[0]==0 ){.
8c0c0 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 out = stdout;.
8c0d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 }else{. out
8c0e0 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d = fopen(zFilenam
8c0f0 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 e, "w");. if(
8c100 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 out==0 ){.
8c110 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
8c120 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f "** Unable to o
8c130 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 utput memory deb
8c140 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 ug output log: %
8c150 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 s **\n",.
8c160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a z
8c170 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Filename);.
8c180 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 return;. }.
8c190 20 7d 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 }. memsys3Ente
8c1a0 72 28 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f r();. fprintf(o
8c1b0 75 74 2c 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 ut, "CHUNKS:\n")
8c1c0 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d ;. for(i=1; i<=
8c1d0 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 mem3.nPool; i+=s
8c1e0 69 7a 65 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65 ize/4){. size
8c1f0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d = mem3.aPool[i-
8c200 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 1].u.hdr.size4x;
8c210 0a 20 20 20 20 69 66 28 20 73 69 7a 65 2f 34 3c . if( size/4<
8c220 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 =1 ){. fpri
8c230 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 73 69 7a ntf(out, "%p siz
8c240 65 20 65 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d e error\n", &mem
8c250 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 3.aPool[i]);.
8c260 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a assert( 0 );.
8c270 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
8c280 20 7d 0a 20 20 20 20 69 66 28 20 28 73 69 7a 65 }. if( (size
8c290 26 31 29 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61 &1)==0 && mem3.a
8c2a0 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d Pool[i+size/4-1]
8c2b0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 21 .u.hdr.prevSize!
8c2c0 3d 73 69 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 =size/4 ){.
8c2d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
8c2e0 70 20 74 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 p tail size does
8c2f0 20 6e 6f 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 not match\n", &
8c300 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a mem3.aPool[i]);.
8c310 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 assert( 0
8c320 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
8c330 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 28 }. if( ((
8c340 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a mem3.aPool[i+siz
8c350 65 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a e/4-1].u.hdr.siz
8c360 65 34 78 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a e4x&2)>>1)!=(siz
8c370 65 26 31 29 20 29 7b 0a 20 20 20 20 20 20 66 70 e&1) ){. fp
8c380 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74 rintf(out, "%p t
8c390 61 69 6c 20 63 68 65 63 6b 6f 75 74 20 62 69 74 ail checkout bit
8c3a0 20 69 73 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22 is incorrect\n"
8c3b0 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d , &mem3.aPool[i]
8c3c0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
8c3d0 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 0 );. brea
8c3e0 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 k;. }. if(
8c3f0 20 73 69 7a 65 26 31 20 29 7b 0a 20 20 20 20 20 size&1 ){.
8c400 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 fprintf(out, "%
8c410 70 20 25 36 64 20 62 79 74 65 73 20 63 68 65 63 p %6d bytes chec
8c420 6b 65 64 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d ked out\n", &mem
8c430 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 3.aPool[i], (siz
8c440 65 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d e/4)*8-8);. }
8c450 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 70 72 69 else{. fpri
8c460 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25 36 64 ntf(out, "%p %6d
8c470 20 62 79 74 65 73 20 66 72 65 65 25 73 5c 6e 22 bytes free%s\n"
8c480 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d , &mem3.aPool[i]
8c490 2c 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a , (size/4)*8-8,.
8c4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8c4b0 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 i==mem3.iMaste
8c4c0 72 20 3f 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a r ? " **master**
8c4d0 22 20 3a 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20 " : "");. }.
8c4e0 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
8c4f0 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 MX_SMALL-1; i++)
8c500 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 {. if( mem3.a
8c510 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 iSmall[i]==0 ) c
8c520 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 ontinue;. fpr
8c530 69 6e 74 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c intf(out, "small
8c540 28 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 (%2d):", i);.
8c550 20 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 for(j = mem3.ai
8c560 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a Small[i]; j>0; j
8c570 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 =mem3.aPool[j].u
8c580 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 .list.next){.
8c590 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
8c5a0 22 20 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 " %p(%d)", &mem3
8c5b0 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 .aPool[j],.
8c5c0 20 20 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 (mem3.a
8c5d0 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e Pool[j-1].u.hdr.
8c5e0 73 69 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a size4x/4)*8-8);.
8c5f0 20 20 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 }. fprint
8c600 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 f(out, "\n"); .
8c610 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
8c620 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 N_HASH; i++){.
8c630 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 if( mem3.aiHas
8c640 68 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e h[i]==0 ) contin
8c650 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 ue;. fprintf(
8c660 6f 75 74 2c 20 22 68 61 73 68 28 25 32 64 29 3a out, "hash(%2d):
8c670 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a ", i);. for(j
8c680 20 3d 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 = mem3.aiHash[i
8c690 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 ]; j>0; j=mem3.a
8c6a0 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e Pool[j].u.list.n
8c6b0 65 78 74 29 7b 0a 20 20 20 20 20 20 66 70 72 69 ext){. fpri
8c6c0 6e 74 66 28 6f 75 74 2c 20 22 20 25 70 28 25 64 ntf(out, " %p(%d
8c6d0 29 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b )", &mem3.aPool[
8c6e0 6a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 j],.
8c6f0 20 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d (mem3.aPool[j-
8c700 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 1].u.hdr.size4x/
8c710 34 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 4)*8-8);. }.
8c720 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
8c730 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 "\n"); . }. fp
8c740 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 rintf(out, "mast
8c750 65 72 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 er=%d\n", mem3.i
8c760 4d 61 73 74 65 72 29 3b 0a 20 20 66 70 72 69 6e Master);. fprin
8c770 74 66 28 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 tf(out, "nowUsed
8c780 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f =%d\n", mem3.nPo
8c790 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 ol*8 - mem3.szMa
8c7a0 73 74 65 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e ster*8);. fprin
8c7b0 74 66 28 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d tf(out, "mxUsed=
8c7c0 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f %d\n", mem3.nPoo
8c7d0 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 l*8 - mem3.mnMas
8c7e0 74 65 72 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65 ter*8);. sqlite
8c7f0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
8c800 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 m3.mutex);. if(
8c810 20 6f 75 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a out==stdout ){.
8c820 20 20 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 fflush(stdou
8c830 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 t);. }else{.
8c840 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 fclose(out);.
8c850 7d 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 }.#else. UNUSED
8c860 5f 50 41 52 41 4d 45 54 45 52 28 7a 46 69 6c 65 _PARAMETER(zFile
8c870 6e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a name);.#endif.}.
8c880 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
8c890 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 ine is the only
8c8a0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 routine in this
8c8b0 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e file with extern
8c8c0 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a al .** linkage..
8c8d0 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 **.** Populate t
8c8e0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d he low-level mem
8c8f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 ory allocation f
8c900 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 unction pointers
8c910 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c in.** sqlite3Gl
8c920 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 obalConfig.m wit
8c930 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 h pointers to th
8c940 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 e routines in th
8c950 69 73 20 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 is file. The.**
8c960 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 arguments specif
8c970 79 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d y the block of m
8c980 65 6d 6f 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e emory to manage.
8c990 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
8c9a0 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ine is only call
8c9b0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f ed by sqlite3_co
8c9c0 6e 66 69 67 28 29 2c 20 61 6e 64 20 74 68 65 72 nfig(), and ther
8c9d0 65 66 6f 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 efore.** is not
8c9e0 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 74 required to be t
8c9f0 68 72 65 61 64 73 61 66 65 20 28 69 74 20 69 73 hreadsafe (it is
8ca00 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 not)..*/.SQLITE
8ca10 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
8ca20 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
8ca30 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 ds *sqlite3MemGe
8ca40 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a tMemsys3(void){.
8ca50 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
8ca60 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f qlite3_mem_metho
8ca70 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 ds mempoolMethod
8ca80 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 s = {. memsy
8ca90 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d s3Malloc,. m
8caa0 65 6d 73 79 73 33 46 72 65 65 2c 0a 20 20 20 20 emsys3Free,.
8cab0 20 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c memsys3Realloc,
8cac0 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 53 69 7a . memsys3Siz
8cad0 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 e,. memsys3R
8cae0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d oundup,. mem
8caf0 73 79 73 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d sys3Init,. m
8cb00 65 6d 73 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a emsys3Shutdown,.
8cb10 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 0. };. re
8cb20 74 75 72 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 turn &mempoolMet
8cb30 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 hods;.}..#endif
8cb40 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
8cb50 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a _MEMSYS3 */../**
8cb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
8cb70 20 6f 66 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a of mem3.c *****
8cb80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
8cbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
8cbc0 69 6e 20 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a in file mem5.c *
8cbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
8cc00 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 ** 2007 October
8cc10 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
8cc20 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
8cc30 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
8cc40 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
8cc50 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
8cc60 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
8cc70 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
8cc80 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
8cc90 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
8cca0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
8ccb0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
8ccc0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
8ccd0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
8cce0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
8ccf0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
8cd00 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
8cd10 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
8cd20 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
8cd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cd50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
8cd70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
8cd80 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 ontains the C fu
8cd90 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 nctions that imp
8cda0 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a lement a memory.
8cdb0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 ** allocation su
8cdc0 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 bsystem for use
8cdd0 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a by SQLite. .**.*
8cde0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f * This version o
8cdf0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c f the memory all
8ce00 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 ocation subsyste
8ce10 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 m omits all.** u
8ce20 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 se of malloc().
8ce30 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 The application
8ce40 67 69 76 65 73 20 53 51 4c 69 74 65 20 61 20 62 gives SQLite a b
8ce50 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a lock of memory.*
8ce60 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 * before calling
8ce70 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
8ce80 69 7a 65 28 29 20 66 72 6f 6d 20 77 68 69 63 68 ize() from which
8ce90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 allocations.**
8cea0 61 72 65 20 6d 61 64 65 20 61 6e 64 20 72 65 74 are made and ret
8ceb0 75 72 6e 65 64 20 62 79 20 74 68 65 20 78 4d 61 urned by the xMa
8cec0 6c 6c 6f 63 28 29 20 61 6e 64 20 78 52 65 61 6c lloc() and xReal
8ced0 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d loc() .** implem
8cee0 65 6e 74 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 entations. Once
8cef0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
8cf00 7a 65 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 ze() has been ca
8cf10 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f lled,.** the amo
8cf20 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 unt of memory av
8cf30 61 69 6c 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 ailable to SQLit
8cf40 65 20 69 73 20 66 69 78 65 64 20 61 6e 64 20 63 e is fixed and c
8cf50 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e annot.** be chan
8cf60 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ged..**.** This
8cf70 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d version of the m
8cf80 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
8cf90 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e subsystem is in
8cfa0 63 6c 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 cluded.** in the
8cfb0 20 62 75 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 build only if S
8cfc0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d QLITE_ENABLE_MEM
8cfd0 53 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e SYS5 is defined.
8cfe0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 6d 6f .**.** This memo
8cff0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 75 73 65 ry allocator use
8d000 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 s the following
8d010 61 6c 67 6f 72 69 74 68 6d 3a 0a 2a 2a 0a 2a 2a algorithm:.**.**
8d020 20 20 20 31 2e 20 20 41 6c 6c 20 6d 65 6d 6f 72 1. All memor
8d030 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 73 69 y allocations si
8d040 7a 65 73 20 61 72 65 20 72 6f 75 6e 64 65 64 20 zes are rounded
8d050 75 70 20 74 6f 20 61 20 70 6f 77 65 72 20 6f 66 up to a power of
8d060 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 2..**.** 2.
8d070 49 66 20 74 77 6f 20 61 64 6a 61 63 65 6e 74 20 If two adjacent
8d080 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 72 65 20 free blocks are
8d090 74 68 65 20 68 61 6c 76 65 73 20 6f 66 20 61 20 the halves of a
8d0a0 6c 61 72 67 65 72 20 62 6c 6f 63 6b 2c 0a 2a 2a larger block,.**
8d0b0 20 20 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 then the
8d0c0 74 77 6f 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 two blocks are c
8d0d0 6f 61 6c 65 73 65 64 20 69 6e 74 6f 20 74 68 65 oalesed into the
8d0e0 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 72 20 62 single larger b
8d0f0 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e lock..**.** 3.
8d100 20 20 4e 65 77 20 6d 65 6d 6f 72 79 20 69 73 20 New memory is
8d110 61 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 20 74 allocated from t
8d120 68 65 20 66 69 72 73 74 20 61 76 61 69 6c 61 62 he first availab
8d130 6c 65 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a le free block..*
8d140 2a 0a 2a 2a 20 54 68 69 73 20 61 6c 67 6f 72 69 *.** This algori
8d150 74 68 6d 20 69 73 20 64 65 73 63 72 69 62 65 64 thm is described
8d160 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52 6f 62 73 6f in: J. M. Robso
8d170 6e 2e 20 22 42 6f 75 6e 64 73 20 66 6f 72 20 53 n. "Bounds for S
8d180 6f 6d 65 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a ome Functions.**
8d190 20 43 6f 6e 63 65 72 6e 69 6e 67 20 44 79 6e 61 Concerning Dyna
8d1a0 6d 69 63 20 53 74 6f 72 61 67 65 20 41 6c 6c 6f mic Storage Allo
8d1b0 63 61 74 69 6f 6e 22 2e 20 4a 6f 75 72 6e 61 6c cation". Journal
8d1c0 20 6f 66 20 74 68 65 20 41 73 73 6f 63 69 61 74 of the Associat
8d1d0 69 6f 6e 20 66 6f 72 0a 2a 2a 20 43 6f 6d 70 75 ion for.** Compu
8d1e0 74 69 6e 67 20 4d 61 63 68 69 6e 65 72 79 2c 20 ting Machinery,
8d1f0 56 6f 6c 75 6d 65 20 32 31 2c 20 4e 75 6d 62 65 Volume 21, Numbe
8d200 72 20 38 2c 20 4a 75 6c 79 20 31 39 37 34 2c 20 r 8, July 1974,
8d210 70 61 67 65 73 20 34 39 31 2d 34 39 39 2e 0a 2a pages 491-499..*
8d220 2a 20 0a 2a 2a 20 4c 65 74 20 6e 20 62 65 20 74 * .** Let n be t
8d230 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c he size of the l
8d240 61 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f argest allocatio
8d250 6e 20 64 69 76 69 64 65 64 20 62 79 20 74 68 65 n divided by the
8d260 20 6d 69 6e 69 6d 75 6d 0a 2a 2a 20 61 6c 6c 6f minimum.** allo
8d270 63 61 74 69 6f 6e 20 73 69 7a 65 20 28 61 66 74 cation size (aft
8d280 65 72 20 72 6f 75 6e 64 69 6e 67 20 61 6c 6c 20 er rounding all
8d290 73 69 7a 65 73 20 75 70 20 74 6f 20 61 20 70 6f sizes up to a po
8d2a0 77 65 72 20 6f 66 20 32 2e 29 20 20 4c 65 74 20 wer of 2.) Let
8d2b0 4d 0a 2a 2a 20 62 65 20 74 68 65 20 6d 61 78 69 M.** be the maxi
8d2c0 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 mum amount of me
8d2d0 6d 6f 72 79 20 65 76 65 72 20 6f 75 74 73 74 61 mory ever outsta
8d2e0 6e 64 69 6e 67 20 61 74 20 6f 6e 65 20 74 69 6d nding at one tim
8d2f0 65 2e 20 20 4c 65 74 0a 2a 2a 20 4e 20 62 65 20 e. Let.** N be
8d300 74 68 65 20 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 the total amount
8d310 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c of memory avail
8d320 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 able for allocat
8d330 69 6f 6e 2e 20 20 52 6f 62 73 6f 6e 0a 2a 2a 20 ion. Robson.**
8d340 70 72 6f 76 65 64 20 74 68 61 74 20 74 68 69 73 proved that this
8d350 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f memory allocato
8d360 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 72 65 r will never bre
8d370 61 6b 64 6f 77 6e 20 64 75 65 20 74 6f 20 0a 2a akdown due to .*
8d380 2a 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 * fragmentation
8d390 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 66 as long as the f
8d3a0 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 72 61 ollowing constra
8d3b0 69 6e 74 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a int holds:.**.**
8d3c0 20 20 20 20 20 20 4e 20 3e 3d 20 20 4d 2a 28 31 N >= M*(1
8d3d0 20 2b 20 6c 6f 67 32 28 6e 29 2f 32 29 20 2d 20 + log2(n)/2) -
8d3e0 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 n + 1.**.** The
8d3f0 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 sqlite3_status()
8d400 20 6c 6f 67 69 63 20 74 72 61 63 6b 73 20 74 68 logic tracks th
8d410 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 73 e maximum values
8d420 20 6f 66 20 6e 20 61 6e 64 20 4d 20 73 6f 0a 2a of n and M so.*
8d430 2a 20 74 68 61 74 20 61 6e 20 61 70 70 6c 69 63 * that an applic
8d440 61 74 69 6f 6e 20 63 61 6e 2c 20 61 74 20 61 6e ation can, at an
8d450 79 20 74 69 6d 65 2c 20 76 65 72 69 66 79 20 74 y time, verify t
8d460 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a his constraint..
8d470 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 */../*.** This v
8d480 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 ersion of the me
8d490 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 mory allocator i
8d4a0 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e s used only when
8d4b0 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 .** SQLITE_ENAB
8d4c0 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 LE_MEMSYS5 is de
8d4d0 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 fined..*/.#ifdef
8d4e0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d SQLITE_ENABLE_M
8d4f0 45 4d 53 59 53 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 EMSYS5../*.** A
8d500 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 minimum allocati
8d510 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 on is an instanc
8d520 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
8d530 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ng structure..**
8d540 20 4c 61 72 67 65 72 20 61 6c 6c 6f 63 61 74 69 Larger allocati
8d550 6f 6e 73 20 61 72 65 20 61 6e 20 61 72 72 61 79 ons are an array
8d560 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 of these struct
8d570 75 72 65 73 20 77 68 65 72 65 20 74 68 65 0a 2a ures where the.*
8d580 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72 * size of the ar
8d590 72 61 79 20 69 73 20 61 20 70 6f 77 65 72 20 6f ray is a power o
8d5a0 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 f 2..**.** The s
8d5b0 69 7a 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 ize of this obje
8d5c0 63 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 ct must be a pow
8d5d0 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 er of two. That
8d5e0 20 66 61 63 74 20 69 73 0a 2a 2a 20 76 65 72 69 fact is.** veri
8d5f0 66 69 65 64 20 69 6e 20 6d 65 6d 73 79 73 35 49 fied in memsys5I
8d600 6e 69 74 28 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 nit()..*/.typede
8d610 66 20 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e f struct Mem5Lin
8d620 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a 73 74 72 75 k Mem5Link;.stru
8d630 63 74 20 4d 65 6d 35 4c 69 6e 6b 20 7b 0a 20 20 ct Mem5Link {.
8d640 69 6e 74 20 6e 65 78 74 3b 20 20 20 20 20 20 20 int next;
8d650 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 /* Index of next
8d660 20 66 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 free chunk */.
8d670 20 69 6e 74 20 70 72 65 76 3b 20 20 20 20 20 20 int prev;
8d680 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 70 72 65 /* Index of pre
8d690 76 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b vious free chunk
8d6a0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 */.};../*.** Ma
8d6b0 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e ximum size of an
8d6c0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 y allocation is
8d6d0 28 28 31 3c 3c 4c 4f 47 4d 41 58 29 2a 6d 65 6d ((1<<LOGMAX)*mem
8d6e0 35 2e 73 7a 41 74 6f 6d 29 2e 20 53 69 6e 63 65 5.szAtom). Since
8d6f0 0a 2a 2a 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 .** mem5.szAtom
8d700 69 73 20 61 6c 77 61 79 73 20 61 74 20 6c 65 61 is always at lea
8d710 73 74 20 38 20 61 6e 64 20 33 32 2d 62 69 74 20 st 8 and 32-bit
8d720 69 6e 74 65 67 65 72 73 20 61 72 65 20 75 73 65 integers are use
8d730 64 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 d,.** it is not
8d740 61 63 74 75 61 6c 6c 79 20 70 6f 73 73 69 62 6c actually possibl
8d750 65 20 74 6f 20 72 65 61 63 68 20 74 68 69 73 20 e to reach this
8d760 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e limit..*/.#defin
8d770 65 20 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f 2a 0a e LOGMAX 30../*.
8d780 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20 66 6f ** Masks used fo
8d790 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 65 r mem5.aCtrl[] e
8d7a0 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 lements..*/.#def
8d7b0 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 ine CTRL_LOGSIZE
8d7c0 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c 6f 67 0x1f /* Log
8d7d0 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73 20 62 2 Size of this b
8d7e0 6c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 lock */.#define
8d7f0 43 54 52 4c 5f 46 52 45 45 20 20 20 20 20 30 78 CTRL_FREE 0x
8d800 32 30 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
8d810 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 6f 75 74 not checked out
8d820 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f */../*.** All o
8d830 66 20 74 68 65 20 73 74 61 74 69 63 20 76 61 72 f the static var
8d840 69 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 iables used by t
8d850 68 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 his module are c
8d860 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f ollected.** into
8d870 20 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 a single struct
8d880 75 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 35 22 ure named "mem5"
8d890 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 . This is to ke
8d8a0 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 ep the.** static
8d8b0 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e variables organ
8d8c0 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 ized and to redu
8d8d0 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c ce namespace pol
8d8e0 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 lution.** when t
8d8f0 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f his module is co
8d900 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 mbined with othe
8d910 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d r in the amalgam
8d920 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ation..*/.static
8d930 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 SQLITE_WSD stru
8d940 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 20 7b 0a ct Mem5Global {.
8d950 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 /*. ** Memory
8d960 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 available for a
8d970 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20 2a 2f 0a 20 llocation. */.
8d980 20 69 6e 74 20 73 7a 41 74 6f 6d 3b 20 20 20 20 int szAtom;
8d990 20 20 2f 2a 20 53 6d 61 6c 6c 65 73 74 20 70 6f /* Smallest po
8d9a0 73 73 69 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f ssible allocatio
8d9b0 6e 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 n in bytes */.
8d9c0 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 int nBlock;
8d9d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 7a /* Number of sz
8d9e0 41 74 6f 6d 20 73 69 7a 65 64 20 62 6c 6f 63 6b Atom sized block
8d9f0 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a 2f 0a 20 20 s in zPool */.
8da00 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20 20 20 20 20 u8 *zPool;
8da10 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c /* Memory avail
8da20 61 62 6c 65 20 74 6f 20 62 65 20 61 6c 6c 6f 63 able to be alloc
8da30 61 74 65 64 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a ated */. . /*.
8da40 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f ** Mutex to co
8da50 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 ntrol access to
8da60 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
8da70 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
8da80 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f . */. sqlite3_
8da90 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 mutex *mutex;..
8daa0 20 2f 2a 0a 20 20 2a 2a 20 50 65 72 66 6f 72 6d /*. ** Perform
8dab0 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 0a ance statistics.
8dac0 20 20 2a 2f 0a 20 20 75 36 34 20 6e 41 6c 6c 6f */. u64 nAllo
8dad0 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f c; /* To
8dae0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 tal number of ca
8daf0 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f 63 20 2a 2f lls to malloc */
8db00 0a 20 20 75 36 34 20 74 6f 74 61 6c 41 6c 6c 6f . u64 totalAllo
8db10 63 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 c; /* Total
8db20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f 63 20 63 61 of all malloc ca
8db30 6c 6c 73 20 2d 20 69 6e 63 6c 75 64 65 73 20 69 lls - includes i
8db40 6e 74 65 72 6e 61 6c 20 66 72 61 67 20 2a 2f 0a nternal frag */.
8db50 20 20 75 36 34 20 74 6f 74 61 6c 45 78 63 65 73 u64 totalExces
8db60 73 3b 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 69 s; /* Total i
8db70 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 nternal fragment
8db80 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 ation */. u32 c
8db90 75 72 72 65 6e 74 4f 75 74 3b 20 20 20 20 20 2f urrentOut; /
8dba0 2a 20 43 75 72 72 65 6e 74 20 63 68 65 63 6b 6f * Current checko
8dbb0 75 74 2c 20 69 6e 63 6c 75 64 69 6e 67 20 69 6e ut, including in
8dbc0 74 65 72 6e 61 6c 20 66 72 61 67 6d 65 6e 74 61 ternal fragmenta
8dbd0 74 69 6f 6e 20 2a 2f 0a 20 20 75 33 32 20 63 75 tion */. u32 cu
8dbe0 72 72 65 6e 74 43 6f 75 6e 74 3b 20 20 20 2f 2a rrentCount; /*
8dbf0 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 Current number
8dc00 6f 66 20 64 69 73 74 69 6e 63 74 20 63 68 65 63 of distinct chec
8dc10 6b 6f 75 74 73 20 2a 2f 0a 20 20 75 33 32 20 6d kouts */. u32 m
8dc20 61 78 4f 75 74 3b 20 20 20 20 20 20 20 20 20 2f axOut; /
8dc30 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e * Maximum instan
8dc40 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74 4f taneous currentO
8dc50 75 74 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 43 ut */. u32 maxC
8dc60 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4d ount; /* M
8dc70 61 78 69 6d 75 6d 20 69 6e 73 74 61 6e 74 61 6e aximum instantan
8dc80 65 6f 75 73 20 63 75 72 72 65 6e 74 43 6f 75 6e eous currentCoun
8dc90 74 20 2a 2f 0a 20 20 75 33 32 20 6d 61 78 52 65 t */. u32 maxRe
8dca0 71 75 65 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 quest; /* La
8dcb0 72 67 65 73 74 20 61 6c 6c 6f 63 61 74 69 6f 6e rgest allocation
8dcc0 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 69 (exclusive of i
8dcd0 6e 74 65 72 6e 61 6c 20 66 72 61 67 29 20 2a 2f nternal frag) */
8dce0 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 69 . . /*. ** Li
8dcf0 73 74 73 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 sts of free bloc
8dd00 6b 73 2e 20 20 61 69 46 72 65 65 6c 69 73 74 5b ks. aiFreelist[
8dd10 30 5d 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 0] is a list of
8dd20 66 72 65 65 20 62 6c 6f 63 6b 73 20 6f 66 0a 20 free blocks of.
8dd30 20 2a 2a 20 73 69 7a 65 20 6d 65 6d 35 2e 73 7a ** size mem5.sz
8dd40 41 74 6f 6d 2e 20 20 61 69 46 72 65 65 6c 69 73 Atom. aiFreelis
8dd50 74 5b 31 5d 20 68 6f 6c 64 73 20 62 6c 6f 63 6b t[1] holds block
8dd60 73 20 6f 66 20 73 69 7a 65 20 73 7a 41 74 6f 6d s of size szAtom
8dd70 2a 32 2e 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 *2.. ** and so
8dd80 66 6f 72 74 68 2e 0a 20 20 2a 2f 0a 20 20 69 6e forth.. */. in
8dd90 74 20 61 69 46 72 65 65 6c 69 73 74 5b 4c 4f 47 t aiFreelist[LOG
8dda0 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f 2a 0a 20 20 MAX+1];.. /*.
8ddb0 2a 2a 20 53 70 61 63 65 20 66 6f 72 20 74 72 61 ** Space for tra
8ddc0 63 6b 69 6e 67 20 77 68 69 63 68 20 62 6c 6f 63 cking which bloc
8ddd0 6b 73 20 61 72 65 20 63 68 65 63 6b 65 64 20 6f ks are checked o
8dde0 75 74 20 61 6e 64 20 74 68 65 20 73 69 7a 65 0a ut and the size.
8ddf0 20 20 2a 2a 20 6f 66 20 65 61 63 68 20 62 6c 6f ** of each blo
8de00 63 6b 2e 20 20 4f 6e 65 20 62 79 74 65 20 70 65 ck. One byte pe
8de10 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 r block.. */.
8de20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a 7d 20 6d 65 u8 *aCtrl;..} me
8de30 6d 35 20 3d 20 7b 20 30 20 7d 3b 0a 0a 2f 2a 0a m5 = { 0 };../*.
8de40 2a 2a 20 41 63 63 65 73 73 20 74 68 65 20 73 74 ** Access the st
8de50 61 74 69 63 20 76 61 72 69 61 62 6c 65 20 74 68 atic variable th
8de60 72 6f 75 67 68 20 61 20 6d 61 63 72 6f 20 66 6f rough a macro fo
8de70 72 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 r SQLITE_OMIT_WS
8de80 44 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6d 65 6d D.*/.#define mem
8de90 35 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 5 GLOBAL(struct
8dea0 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20 6d 65 6d 35 Mem5Global, mem5
8deb0 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e )../*.** Assumin
8dec0 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 69 73 20 g mem5.zPool is
8ded0 64 69 76 69 64 65 64 20 75 70 20 69 6e 74 6f 20 divided up into
8dee0 61 6e 20 61 72 72 61 79 20 6f 66 20 4d 65 6d 35 an array of Mem5
8def0 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75 63 74 75 72 Link.** structur
8df00 65 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 es, return a poi
8df10 6e 74 65 72 20 74 6f 20 74 68 65 20 69 64 78 2d nter to the idx-
8df20 74 68 20 73 75 63 68 20 6c 69 6b 2e 0a 2a 2f 0a th such lik..*/.
8df30 23 64 65 66 69 6e 65 20 4d 45 4d 35 4c 49 4e 4b #define MEM5LINK
8df40 28 69 64 78 29 20 28 28 4d 65 6d 35 4c 69 6e 6b (idx) ((Mem5Link
8df50 20 2a 29 28 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b *)(&mem5.zPool[
8df60 28 69 64 78 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f (idx)*mem5.szAto
8df70 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 m]))../*.** Unli
8df80 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 nk the chunk at
8df90 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 66 72 mem5.aPool[i] fr
8dfa0 6f 6d 20 6c 69 73 74 20 69 74 20 69 73 20 63 75 om list it is cu
8dfb0 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 rrently.** on.
8dfc0 49 74 20 73 68 6f 75 6c 64 20 62 65 20 66 6f 75 It should be fou
8dfd0 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61 69 46 72 65 nd on mem5.aiFre
8dfe0 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 5d 2e elist[iLogsize].
8dff0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8e000 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 6e memsys5Unlink(in
8e010 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a t i, int iLogsiz
8e020 65 29 7b 0a 20 20 69 6e 74 20 6e 65 78 74 2c 20 e){. int next,
8e030 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74 28 20 prev;. assert(
8e040 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e 6e i>=0 && i<mem5.n
8e050 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 Block );. asser
8e060 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 t( iLogsize>=0 &
8e070 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d & iLogsize<=LOGM
8e080 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 AX );. assert(
8e090 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 26 (mem5.aCtrl[i] &
8e0a0 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d 3d CTRL_LOGSIZE)==
8e0b0 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 6e iLogsize );.. n
8e0c0 65 78 74 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 ext = MEM5LINK(i
8e0d0 29 2d 3e 6e 65 78 74 3b 0a 20 20 70 72 65 76 20 )->next;. prev
8e0e0 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d 3e 70 = MEM5LINK(i)->p
8e0f0 72 65 76 3b 0a 20 20 69 66 28 20 70 72 65 76 3c rev;. if( prev<
8e100 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 0 ){. mem5.ai
8e110 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a Freelist[iLogsiz
8e120 65 5d 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c e] = next;. }el
8e130 73 65 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b se{. MEM5LINK
8e140 28 70 72 65 76 29 2d 3e 6e 65 78 74 20 3d 20 6e (prev)->next = n
8e150 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e ext;. }. if( n
8e160 65 78 74 3e 3d 30 20 29 7b 0a 20 20 20 20 4d 45 ext>=0 ){. ME
8e170 4d 35 4c 49 4e 4b 28 6e 65 78 74 29 2d 3e 70 72 M5LINK(next)->pr
8e180 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 7d ev = prev;. }.}
8e190 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 ../*.** Link the
8e1a0 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 chunk at mem5.a
8e1b0 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 Pool[i] so that
8e1c0 69 73 20 6f 6e 20 74 68 65 20 69 4c 6f 67 73 69 is on the iLogsi
8e1d0 7a 65 0a 2a 2a 20 66 72 65 65 20 6c 69 73 74 2e ze.** free list.
8e1e0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
8e1f0 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 6e 74 20 memsys5Link(int
8e200 69 2c 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 29 i, int iLogsize)
8e210 7b 0a 20 20 69 6e 74 20 78 3b 0a 20 20 61 73 73 {. int x;. ass
8e220 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
8e230 65 78 5f 68 65 6c 64 28 6d 65 6d 35 2e 6d 75 74 ex_held(mem5.mut
8e240 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
8e250 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65 6d 35 2e i>=0 && i<mem5.
8e260 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 nBlock );. asse
8e270 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e 3d 30 20 rt( iLogsize>=0
8e280 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d 4c 4f 47 && iLogsize<=LOG
8e290 4d 41 58 20 29 3b 0a 20 20 61 73 73 65 72 74 28 MAX );. assert(
8e2a0 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 20 (mem5.aCtrl[i]
8e2b0 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 3d & CTRL_LOGSIZE)=
8e2c0 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a 0a 20 20 =iLogsize );..
8e2d0 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 2d x = MEM5LINK(i)-
8e2e0 3e 6e 65 78 74 20 3d 20 6d 65 6d 35 2e 61 69 46 >next = mem5.aiF
8e2f0 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a 65 reelist[iLogsize
8e300 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 ];. MEM5LINK(i)
8e310 2d 3e 70 72 65 76 20 3d 20 2d 31 3b 0a 20 20 69 ->prev = -1;. i
8e320 66 28 20 78 3e 3d 30 20 29 7b 0a 20 20 20 20 61 f( x>=0 ){. a
8e330 73 73 65 72 74 28 20 78 3c 6d 65 6d 35 2e 6e 42 ssert( x<mem5.nB
8e340 6c 6f 63 6b 20 29 3b 0a 20 20 20 20 4d 45 4d 35 lock );. MEM5
8e350 4c 49 4e 4b 28 78 29 2d 3e 70 72 65 76 20 3d 20 LINK(x)->prev =
8e360 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 69 i;. }. mem5.ai
8e370 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 7a Freelist[iLogsiz
8e380 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a e] = i;.}../*.**
8e390 20 49 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d If the STATIC_M
8e3a0 45 4d 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 EM mutex is not
8e3b0 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 already held, ob
8e3c0 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 tain it now. The
8e3d0 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 mutex.** will a
8e3e0 6c 72 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 lready be held (
8e3f0 6f 62 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 obtained by code
8e400 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 in malloc.c) if
8e410 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 .** sqlite3Globa
8e420 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 lConfig.bMemStat
8e430 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 is true..*/.sta
8e440 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 tic void memsys5
8e450 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 73 Enter(void){. s
8e460 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
8e470 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78 29 3b 0a er(mem5.mutex);.
8e480 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 }.static void me
8e490 6d 73 79 73 35 4c 65 61 76 65 28 76 6f 69 64 29 msys5Leave(void)
8e4a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 {. sqlite3_mute
8e4b0 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e 6d 75 74 x_leave(mem5.mut
8e4c0 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ex);.}../*.** Re
8e4d0 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 turn the size of
8e4e0 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 an outstanding
8e4f0 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 allocation, in b
8e500 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 ytes. The.** si
8e510 7a 65 20 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 ze returned omit
8e520 73 20 74 68 65 20 38 2d 62 79 74 65 20 68 65 61 s the 8-byte hea
8e530 64 65 72 20 6f 76 65 72 68 65 61 64 2e 20 20 54 der overhead. T
8e540 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b his only.** work
8e550 73 20 66 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 s for chunks tha
8e560 74 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 t are currently
8e570 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a checked out..*/.
8e580 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
8e590 73 35 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b s5Size(void *p){
8e5a0 0a 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 30 . int iSize = 0
8e5b0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 ;. if( p ){.
8e5c0 20 69 6e 74 20 69 20 3d 20 28 28 75 38 20 2a 29 int i = ((u8 *)
8e5d0 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 p-mem5.zPool)/me
8e5e0 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20 20 20 20 61 m5.szAtom;. a
8e5f0 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 ssert( i>=0 && i
8e600 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a <mem5.nBlock );.
8e610 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65 6d 35 iSize = mem5
8e620 2e 73 7a 41 74 6f 6d 20 2a 20 28 31 20 3c 3c 20 .szAtom * (1 <<
8e630 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26 43 (mem5.aCtrl[i]&C
8e640 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a 20 TRL_LOGSIZE));.
8e650 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69 7a }. return iSiz
8e660 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 e;.}../*.** Find
8e670 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 the first entry
8e680 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
8e690 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c 69 iLogsize. Unli
8e6a0 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72 79 nk that.** entry
8e6b0 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73 20 and return its
8e6c0 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74 69 index. .*/.stati
8e6d0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e 6c c int memsys5Unl
8e6e0 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c 6f inkFirst(int iLo
8e6f0 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69 3b gsize){. int i;
8e700 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a 0a . int iFirst;..
8e710 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 assert( iLogsi
8e720 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a ze>=0 && iLogsiz
8e730 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 69 e<=LOGMAX );. i
8e740 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d 35 = iFirst = mem5
8e750 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 .aiFreelist[iLog
8e760 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 size];. assert(
8e770 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20 20 iFirst>=0 );.
8e780 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 while( i>0 ){.
8e790 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20 29 if( i<iFirst )
8e7a0 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20 20 iFirst = i;.
8e7b0 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69 29 i = MEM5LINK(i)
8e7c0 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d 65 ->next;. }. me
8e7d0 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69 72 msys5Unlink(iFir
8e7e0 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 st, iLogsize);.
8e7f0 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b 0a return iFirst;.
8e800 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
8e810 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 a block of memor
8e820 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 42 y of at least nB
8e830 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a ytes in size..**
8e840 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 Return NULL if
8e850 75 6e 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 unable. Return
8e860 4e 55 4c 4c 20 69 66 20 6e 42 79 74 65 73 3d 3d NULL if nBytes==
8e870 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 0..**.** The cal
8e880 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 ler guarantees t
8e890 68 61 74 20 6e 42 79 74 65 20 70 6f 73 69 74 69 hat nByte positi
8e8a0 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 ve..**.** The ca
8e8b0 6c 6c 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 ller has obtaine
8e8c0 64 20 61 20 6d 75 74 65 78 20 70 72 69 6f 72 20 d a mutex prior
8e8d0 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 to invoking this
8e8e0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 .** routine so t
8e8f0 68 65 72 65 20 69 73 20 6e 65 76 65 72 20 61 6e here is never an
8e900 79 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 77 y chance that tw
8e910 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 74 68 72 o or more.** thr
8e920 65 61 64 73 20 63 61 6e 20 62 65 20 69 6e 20 74 eads can be in t
8e930 68 69 73 20 72 6f 75 74 69 6e 65 20 61 74 20 74 his routine at t
8e940 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2f he same time..*/
8e950 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 .static void *me
8e960 6d 73 79 73 35 4d 61 6c 6c 6f 63 55 6e 73 61 66 msys5MallocUnsaf
8e970 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 e(int nByte){.
8e980 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
8e990 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 6d /* Index of a m
8e9a0 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 em5.aPool[] slot
8e9b0 20 2a 2f 0a 20 20 69 6e 74 20 69 42 69 6e 3b 20 */. int iBin;
8e9c0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
8e9d0 69 6e 74 6f 20 6d 65 6d 35 2e 61 69 46 72 65 65 into mem5.aiFree
8e9e0 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 list[] */. int
8e9f0 69 46 75 6c 6c 53 7a 3b 20 20 20 20 20 2f 2a 20 iFullSz; /*
8ea00 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69 Size of allocati
8ea10 6f 6e 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f on rounded up to
8ea20 20 70 6f 77 65 72 20 6f 66 20 32 20 2a 2f 0a 20 power of 2 */.
8ea30 20 69 6e 74 20 69 4c 6f 67 73 69 7a 65 3b 20 20 int iLogsize;
8ea40 20 20 2f 2a 20 4c 6f 67 32 20 6f 66 20 69 46 75 /* Log2 of iFu
8ea50 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49 4e 20 2a 2f llSz/POW2_MIN */
8ea60 0a 0a 20 20 2f 2a 20 6e 42 79 74 65 20 6d 75 73 .. /* nByte mus
8ea70 74 20 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 t be a positive
8ea80 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 */. assert( nBy
8ea90 74 65 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4b 65 te>0 );.. /* Ke
8eaa0 65 70 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 ep track of the
8eab0 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 maximum allocati
8eac0 6f 6e 20 72 65 71 75 65 73 74 2e 20 20 45 76 65 on request. Eve
8ead0 6e 20 75 6e 66 75 6c 66 69 6c 6c 65 64 0a 20 20 n unfulfilled.
8eae0 2a 2a 20 72 65 71 75 65 73 74 73 20 61 72 65 20 ** requests are
8eaf0 63 6f 75 6e 74 65 64 20 2a 2f 0a 20 20 69 66 28 counted */. if(
8eb00 20 28 75 33 32 29 6e 42 79 74 65 3e 6d 65 6d 35 (u32)nByte>mem5
8eb10 2e 6d 61 78 52 65 71 75 65 73 74 20 29 7b 0a 20 .maxRequest ){.
8eb20 20 20 20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 mem5.maxReque
8eb30 73 74 20 3d 20 6e 42 79 74 65 3b 0a 20 20 7d 0a st = nByte;. }.
8eb40 0a 20 20 2f 2a 20 41 62 6f 72 74 20 69 66 20 74 . /* Abort if t
8eb50 68 65 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c he requested all
8eb60 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 69 73 20 ocation size is
8eb70 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
8eb80 6c 61 72 67 65 73 74 0a 20 20 2a 2a 20 70 6f 77 largest. ** pow
8eb90 65 72 20 6f 66 20 74 77 6f 20 74 68 61 74 20 77 er of two that w
8eba0 65 20 63 61 6e 20 72 65 70 72 65 73 65 6e 74 20 e can represent
8ebb0 75 73 69 6e 67 20 33 32 2d 62 69 74 20 73 69 67 using 32-bit sig
8ebc0 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 ned integers..
8ebd0 2a 2f 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3e */. if( nByte >
8ebe0 20 30 78 34 30 30 30 30 30 30 30 20 29 7b 0a 20 0x40000000 ){.
8ebf0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
8ec00 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20 6e 42 79 .. /* Round nBy
8ec10 74 65 20 75 70 20 74 6f 20 74 68 65 20 6e 65 78 te up to the nex
8ec20 74 20 76 61 6c 69 64 20 70 6f 77 65 72 20 6f 66 t valid power of
8ec30 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 46 two */. for(iF
8ec40 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f ullSz=mem5.szAto
8ec50 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b 20 69 m, iLogsize=0; i
8ec60 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20 69 46 FullSz<nByte; iF
8ec70 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c 6f 67 ullSz *= 2, iLog
8ec80 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f 2a 20 size++){}.. /*
8ec90 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35 2e 61 Make sure mem5.a
8eca0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69 iFreelist[iLogsi
8ecb0 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61 74 20 ze] contains at
8ecc0 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65 0a 20 least one free.
8ecd0 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 6e ** block. If n
8ece0 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74 20 61 ot, then split a
8ecf0 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20 6e 65 block of the ne
8ed00 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65 72 20 xt larger power
8ed10 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e 20 6f of. ** two in o
8ed20 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 rder to create a
8ed30 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 new free block
8ed40 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69 7a 65 of size iLogsize
8ed50 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 42 69 .. */. for(iBi
8ed60 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65 6d 35 n=iLogsize; mem5
8ed70 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42 69 6e .aiFreelist[iBin
8ed80 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c 4f 47 ]<0 && iBin<=LOG
8ed90 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d 0a 20 MAX; iBin++){}.
8eda0 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d 41 58 if( iBin>LOGMAX
8edb0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 ) return 0;. i
8edc0 20 3d 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b = memsys5Unlink
8edd0 46 69 72 73 74 28 69 42 69 6e 29 3b 0a 20 20 77 First(iBin);. w
8ede0 68 69 6c 65 28 20 69 42 69 6e 3e 69 4c 6f 67 73 hile( iBin>iLogs
8edf0 69 7a 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e ize ){. int n
8ee00 65 77 53 69 7a 65 3b 0a 0a 20 20 20 20 69 42 69 ewSize;.. iBi
8ee10 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 n--;. newSize
8ee20 20 3d 20 31 20 3c 3c 20 69 42 69 6e 3b 0a 20 20 = 1 << iBin;.
8ee30 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 2b 6e mem5.aCtrl[i+n
8ee40 65 77 53 69 7a 65 5d 20 3d 20 43 54 52 4c 5f 46 ewSize] = CTRL_F
8ee50 52 45 45 20 7c 20 69 42 69 6e 3b 0a 20 20 20 20 REE | iBin;.
8ee60 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2b 6e 65 memsys5Link(i+ne
8ee70 77 53 69 7a 65 2c 20 69 42 69 6e 29 3b 0a 20 20 wSize, iBin);.
8ee80 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 }. mem5.aCtrl[i
8ee90 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a 0a 20 ] = iLogsize;..
8eea0 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c 6f 63 /* Update alloc
8eeb0 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 ator performance
8eec0 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a 2f 0a statistics. */.
8eed0 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b 2b 3b mem5.nAlloc++;
8eee0 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c . mem5.totalAll
8eef0 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 oc += iFullSz;.
8ef00 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 mem5.totalExces
8ef10 73 20 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d 20 6e s += iFullSz - n
8ef20 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 Byte;. mem5.cur
8ef30 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20 20 6d rentCount++;. m
8ef40 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2b em5.currentOut +
8ef50 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69 66 28 = iFullSz;. if(
8ef60 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 3c 6d mem5.maxCount<m
8ef70 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
8ef80 20 29 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 ) mem5.maxCount
8ef90 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 = mem5.currentC
8efa0 6f 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65 6d 35 ount;. if( mem5
8efb0 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 2e 63 75 72 .maxOut<mem5.cur
8efc0 72 65 6e 74 4f 75 74 20 29 20 6d 65 6d 35 2e 6d rentOut ) mem5.m
8efd0 61 78 4f 75 74 20 3d 20 6d 65 6d 35 2e 63 75 72 axOut = mem5.cur
8efe0 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a 20 52 rentOut;.. /* R
8eff0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
8f000 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 to the allocated
8f010 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20 72 65 memory. */. re
8f020 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d turn (void*)&mem
8f030 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35 2e 73 5.zPool[i*mem5.s
8f040 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a zAtom];.}../*.**
8f050 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61 6e Free an outstan
8f060 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f ding memory allo
8f070 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 cation..*/.stati
8f080 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 c void memsys5Fr
8f090 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70 eeUnsafe(void *p
8f0a0 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a 65 Old){. u32 size
8f0b0 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69 6e , iLogsize;. in
8f0c0 74 20 69 42 6c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 t iBlock;.. /*
8f0d0 53 65 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 Set iBlock to th
8f0e0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 62 e index of the b
8f0f0 6c 6f 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 lock pointed to
8f100 62 79 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a by pOld in . **
8f110 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6d 65 the array of me
8f120 6d 35 2e 73 7a 41 74 6f 6d 20 62 79 74 65 20 62 m5.szAtom byte b
8f130 6c 6f 63 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f locks pointed to
8f140 20 62 79 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a by mem5.zPool..
8f150 20 20 2a 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 */. iBlock =
8f160 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 ((u8 *)pOld-mem5
8f170 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35 2e 73 7a 41 .zPool)/mem5.szA
8f180 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b tom;.. /* Check
8f190 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 that the pointe
8f1a0 72 20 70 4f 6c 64 20 70 6f 69 6e 74 73 20 74 6f r pOld points to
8f1b0 20 61 20 76 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 a valid, non-fr
8f1c0 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 ee block. */. a
8f1d0 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 ssert( iBlock>=0
8f1e0 20 26 26 20 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e && iBlock<mem5.
8f1f0 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61 73 73 65 nBlock );. asse
8f200 72 74 28 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d rt( ((u8 *)pOld-
8f210 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 mem5.zPool)%mem5
8f220 2e 73 7a 41 74 6f 6d 3d 3d 30 20 29 3b 0a 20 20 .szAtom==0 );.
8f230 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 assert( (mem5.aC
8f240 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 trl[iBlock] & CT
8f250 52 4c 5f 46 52 45 45 29 3d 3d 30 20 29 3b 0a 0a RL_FREE)==0 );..
8f260 20 20 69 4c 6f 67 73 69 7a 65 20 3d 20 6d 65 6d iLogsize = mem
8f270 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8f280 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 3b 0a & CTRL_LOGSIZE;.
8f290 20 20 73 69 7a 65 20 3d 20 31 3c 3c 69 4c 6f 67 size = 1<<iLog
8f2a0 73 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 size;. assert(
8f2b0 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 3c 28 75 iBlock+size-1<(u
8f2c0 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 32)mem5.nBlock )
8f2d0 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b ;.. mem5.aCtrl[
8f2e0 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43 54 52 4c 5f iBlock] |= CTRL_
8f2f0 46 52 45 45 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 FREE;. mem5.aCt
8f300 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d 31 rl[iBlock+size-1
8f310 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b 0a ] |= CTRL_FREE;.
8f320 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 assert( mem5.c
8f330 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 29 3b urrentCount>0 );
8f340 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e . assert( mem5.
8f350 63 75 72 72 65 6e 74 4f 75 74 3e 3d 28 73 69 7a currentOut>=(siz
8f360 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 20 29 e*mem5.szAtom) )
8f370 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 ;. mem5.current
8f380 43 6f 75 6e 74 2d 2d 3b 0a 20 20 6d 65 6d 35 2e Count--;. mem5.
8f390 63 75 72 72 65 6e 74 4f 75 74 20 2d 3d 20 73 69 currentOut -= si
8f3a0 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a ze*mem5.szAtom;.
8f3b0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63 assert( mem5.c
8f3c0 75 72 72 65 6e 74 4f 75 74 3e 30 20 7c 7c 20 6d urrentOut>0 || m
8f3d0 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 em5.currentCount
8f3e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
8f3f0 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 mem5.currentCou
8f400 6e 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 nt>0 || mem5.cur
8f410 72 65 6e 74 4f 75 74 3d 3d 30 20 29 3b 0a 0a 20 rentOut==0 );..
8f420 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f mem5.aCtrl[iBlo
8f430 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 ck] = CTRL_FREE
8f440 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 77 68 | iLogsize;. wh
8f450 69 6c 65 28 20 41 4c 57 41 59 53 28 69 4c 6f 67 ile( ALWAYS(iLog
8f460 73 69 7a 65 3c 4c 4f 47 4d 41 58 29 20 29 7b 0a size<LOGMAX) ){.
8f470 20 20 20 20 69 6e 74 20 69 42 75 64 64 79 3b 0a int iBuddy;.
8f480 20 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b 3e if( (iBlock>
8f490 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20 29 >iLogsize) & 1 )
8f4a0 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79 20 3d {. iBuddy =
8f4b0 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b 0a iBlock - size;.
8f4c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
8f4d0 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63 6b iBuddy = iBlock
8f4e0 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 + size;. }.
8f4f0 20 20 20 61 73 73 65 72 74 28 20 69 42 75 64 64 assert( iBudd
8f500 79 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 y>=0 );. if(
8f510 28 69 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f 67 (iBuddy+(1<<iLog
8f520 73 69 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c 6f size))>mem5.nBlo
8f530 63 6b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 ck ) break;.
8f540 69 66 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 if( mem5.aCtrl[i
8f550 42 75 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46 52 Buddy]!=(CTRL_FR
8f560 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20 29 EE | iLogsize) )
8f570 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d 73 break;. mems
8f580 79 73 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64 79 ys5Unlink(iBuddy
8f590 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20 20 , iLogsize);.
8f5a0 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20 20 iLogsize++;.
8f5b0 20 69 66 28 20 69 42 75 64 64 79 3c 69 42 6c 6f if( iBuddy<iBlo
8f5c0 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35 ck ){. mem5
8f5d0 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20 3d .aCtrl[iBuddy] =
8f5e0 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f CTRL_FREE | iLo
8f5f0 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d gsize;. mem
8f600 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8f610 3d 20 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f 63 = 0;. iBloc
8f620 6b 20 3d 20 69 42 75 64 64 79 3b 0a 20 20 20 20 k = iBuddy;.
8f630 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d }else{. mem
8f640 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 5.aCtrl[iBlock]
8f650 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c = CTRL_FREE | iL
8f660 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 ogsize;. me
8f670 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d m5.aCtrl[iBuddy]
8f680 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
8f690 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a 20 size *= 2;. }.
8f6a0 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42 6c memsys5Link(iBl
8f6b0 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a ock, iLogsize);.
8f6c0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 }../*.** Allocat
8f6d0 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f e nBytes of memo
8f6e0 72 79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ry.*/.static voi
8f6f0 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 d *memsys5Malloc
8f700 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 (int nBytes){.
8f710 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 sqlite3_int64 *p
8f720 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 42 79 74 = 0;. if( nByt
8f730 65 73 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 es>0 ){. mems
8f740 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 20 20 ys5Enter();.
8f750 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f p = memsys5Mallo
8f760 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b cUnsafe(nBytes);
8f770 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 . memsys5Leav
8f780 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 e();. }. retur
8f790 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a 0a n (void*)p; .}..
8f7a0 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 /*.** Free memor
8f7b0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 y..**.** The out
8f7c0 65 72 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 20 er layer memory
8f7d0 61 6c 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 6e allocator preven
8f7e0 74 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 ts this routine
8f7f0 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 61 from.** being ca
8f800 6c 6c 65 64 20 77 69 74 68 20 70 50 72 69 6f 72 lled with pPrior
8f810 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ==0..*/.static v
8f820 6f 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 oid memsys5Free(
8f830 76 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 void *pPrior){.
8f840 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 assert( pPrior!
8f850 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 79 73 35 45 =0 );. memsys5E
8f860 6e 74 65 72 28 29 3b 0a 20 20 6d 65 6d 73 79 73 nter();. memsys
8f870 35 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69 5FreeUnsafe(pPri
8f880 6f 72 29 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 or);. memsys5Le
8f890 61 76 65 28 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a ave(); .}../*.*
8f8a0 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 69 7a * Change the siz
8f8b0 65 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 e of an existing
8f8c0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
8f8d0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 75 on..**.** The ou
8f8e0 74 65 72 20 6c 61 79 65 72 20 6d 65 6d 6f 72 79 ter layer memory
8f8f0 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72 65 76 65 allocator preve
8f900 6e 74 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 nts this routine
8f910 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e 67 20 63 from.** being c
8f920 61 6c 6c 65 64 20 77 69 74 68 20 70 50 72 69 6f alled with pPrio
8f930 72 3d 3d 30 2e 20 20 0a 2a 2a 0a 2a 2a 20 6e 42 r==0. .**.** nB
8f940 79 74 65 73 20 69 73 20 61 6c 77 61 79 73 20 61 ytes is always a
8f950 20 76 61 6c 75 65 20 6f 62 74 61 69 6e 65 64 20 value obtained
8f960 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 61 6c from a prior cal
8f970 6c 20 74 6f 0a 2a 2a 20 6d 65 6d 73 79 73 35 52 l to.** memsys5R
8f980 6f 75 6e 64 28 29 2e 20 20 48 65 6e 63 65 20 6e ound(). Hence n
8f990 42 79 74 65 73 20 69 73 20 61 6c 77 61 79 73 20 Bytes is always
8f9a0 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 70 a non-negative p
8f9b0 6f 77 65 72 0a 2a 2a 20 6f 66 20 74 77 6f 2e 20 ower.** of two.
8f9c0 20 49 66 20 6e 42 79 74 65 73 3d 3d 30 20 74 68 If nBytes==0 th
8f9d0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e at means that an
8f9e0 20 6f 76 65 72 73 69 7a 65 20 61 6c 6c 6f 63 61 oversize alloca
8f9f0 74 69 6f 6e 0a 2a 2a 20 28 61 6e 20 61 6c 6c 6f tion.** (an allo
8fa00 63 61 74 69 6f 6e 20 6c 61 72 67 65 72 20 74 68 cation larger th
8fa10 61 6e 20 30 78 34 30 30 30 30 30 30 30 29 20 77 an 0x40000000) w
8fa20 61 73 20 72 65 71 75 65 73 74 65 64 20 61 6e 64 as requested and
8fa30 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 this.** routine
8fa40 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 30 should return 0
8fa50 20 77 69 74 68 6f 75 74 20 66 72 65 65 69 6e 67 without freeing
8fa60 20 70 50 72 69 6f 72 2e 0a 2a 2f 0a 73 74 61 74 pPrior..*/.stat
8fa70 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 ic void *memsys5
8fa80 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 Realloc(void *pP
8fa90 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 rior, int nBytes
8faa0 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 ){. int nOld;.
8fab0 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 void *p;. asse
8fac0 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b rt( pPrior!=0 );
8fad0 0a 20 20 61 73 73 65 72 74 28 20 28 6e 42 79 74 . assert( (nByt
8fae0 65 73 26 28 6e 42 79 74 65 73 2d 31 29 29 3d 3d es&(nBytes-1))==
8faf0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
8fb00 42 79 74 65 73 3e 3d 30 20 29 3b 0a 20 20 69 66 Bytes>=0 );. if
8fb10 28 20 6e 42 79 74 65 73 3d 3d 30 20 29 7b 0a 20 ( nBytes==0 ){.
8fb20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
8fb30 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 . nOld = memsys
8fb40 35 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 5Size(pPrior);.
8fb50 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c if( nBytes<=nOl
8fb60 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 d ){. return
8fb70 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 pPrior;. }. me
8fb80 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 msys5Enter();.
8fb90 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f p = memsys5Mallo
8fba0 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b cUnsafe(nBytes);
8fbb0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 . if( p ){.
8fbc0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72 memcpy(p, pPrior
8fbd0 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d , nOld);. mem
8fbe0 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 70 sys5FreeUnsafe(p
8fbf0 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 Prior);. }. me
8fc00 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20 msys5Leave();.
8fc10 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
8fc20 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 ** Round up a re
8fc30 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 quest size to th
8fc40 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c e next valid all
8fc50 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 20 20 49 ocation size. I
8fc60 66 0a 2a 2a 20 74 68 65 20 61 6c 6c 6f 63 61 74 f.** the allocat
8fc70 69 6f 6e 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 ion is too large
8fc80 20 74 6f 20 62 65 20 68 61 6e 64 6c 65 64 20 62 to be handled b
8fc90 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f y this allocatio
8fca0 6e 20 73 79 73 74 65 6d 2c 0a 2a 2a 20 72 65 74 n system,.** ret
8fcb0 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c urn 0..**.** All
8fcc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6d 75 73 allocations mus
8fcd0 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 t be a power of
8fce0 74 77 6f 20 61 6e 64 20 6d 75 73 74 20 62 65 20 two and must be
8fcf0 65 78 70 72 65 73 73 65 64 20 62 79 20 61 0a 2a expressed by a.*
8fd00 2a 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 * 32-bit signed
8fd10 69 6e 74 65 67 65 72 2e 20 20 48 65 6e 63 65 20 integer. Hence
8fd20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f the largest allo
8fd30 63 61 74 69 6f 6e 20 69 73 20 30 78 34 30 30 30 cation is 0x4000
8fd40 30 30 30 30 0a 2a 2a 20 6f 72 20 31 30 37 33 37 0000.** or 10737
8fd50 34 31 38 32 34 20 62 79 74 65 73 2e 0a 2a 2f 0a 41824 bytes..*/.
8fd60 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 static int memsy
8fd70 73 35 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 s5Roundup(int n)
8fd80 7b 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b {. int iFullSz;
8fd90 0a 20 20 69 66 28 20 6e 20 3e 20 30 78 34 30 30 . if( n > 0x400
8fda0 30 30 30 30 30 20 29 20 72 65 74 75 72 6e 20 30 00000 ) return 0
8fdb0 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d ;. for(iFullSz=
8fdc0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 20 69 46 75 mem5.szAtom; iFu
8fdd0 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 llSz<n; iFullSz
8fde0 2a 3d 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 *= 2);. return
8fdf0 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a iFullSz;.}../*.*
8fe00 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 65 69 * Return the cei
8fe10 6c 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 67 61 ling of the loga
8fe20 72 69 74 68 6d 20 62 61 73 65 20 32 20 6f 66 20 rithm base 2 of
8fe30 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 iValue..**.** Ex
8fe40 61 6d 70 6c 65 73 3a 20 20 20 6d 65 6d 73 79 73 amples: memsys
8fe50 35 4c 6f 67 28 31 29 20 2d 3e 20 30 0a 2a 2a 20 5Log(1) -> 0.**
8fe60 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 mems
8fe70 79 73 35 4c 6f 67 28 32 29 20 2d 3e 20 31 0a 2a ys5Log(2) -> 1.*
8fe80 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 * me
8fe90 6d 73 79 73 35 4c 6f 67 28 34 29 20 2d 3e 20 32 msys5Log(4) -> 2
8fea0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
8feb0 6d 65 6d 73 79 73 35 4c 6f 67 28 35 29 20 2d 3e memsys5Log(5) ->
8fec0 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 3.**
8fed0 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 38 29 20 memsys5Log(8)
8fee0 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 20 20 20 -> 3.**
8fef0 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 39 memsys5Log(9
8ff00 29 20 2d 3e 20 34 0a 2a 2f 0a 73 74 61 74 69 63 ) -> 4.*/.static
8ff10 20 69 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67 28 int memsys5Log(
8ff20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 69 int iValue){. i
8ff30 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28 69 nt iLog;. for(i
8ff40 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67 29 Log=0; (1<<iLog)
8ff50 3c 69 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b 29 <iValue; iLog++)
8ff60 3b 0a 20 20 72 65 74 75 72 6e 20 69 4c 6f 67 3b ;. return iLog;
8ff70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 .}../*.** Initia
8ff80 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 lize the memory
8ff90 61 6c 6c 6f 63 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a allocator..**.**
8ffa0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
8ffb0 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e not threadsafe.
8ffc0 20 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 The caller mus
8ffd0 74 20 62 65 20 68 6f 6c 64 69 6e 67 20 61 20 6d t be holding a m
8ffe0 75 74 65 78 0a 2a 2a 20 74 6f 20 70 72 65 76 65 utex.** to preve
8fff0 6e 74 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 nt multiple thre
90000 61 64 73 20 66 72 6f 6d 20 65 6e 74 65 72 69 6e ads from enterin
90010 67 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 g at the same ti
90020 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e me..*/.static in
90030 74 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f t memsys5Init(vo
90040 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 id *NotUsed){.
90050 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 int ii;
90060 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
90070 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 er */. int nByt
90080 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 e; /* Nu
90090 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
900a0 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c memory availabl
900b0 65 20 74 6f 20 74 68 69 73 20 61 6c 6c 6f 63 61 e to this alloca
900c0 74 6f 72 20 2a 2f 0a 20 20 75 38 20 2a 7a 42 79 tor */. u8 *zBy
900d0 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d te; /* M
900e0 65 6d 6f 72 79 20 75 73 61 62 6c 65 20 62 79 20 emory usable by
900f0 74 68 69 73 20 61 6c 6c 6f 63 61 74 6f 72 20 2a this allocator *
90100 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b /. int nMinLog;
90110 20 20 20 20 20 20 20 2f 2a 20 4c 6f 67 20 62 61 /* Log ba
90120 73 65 20 32 20 6f 66 20 6d 69 6e 69 6d 75 6d 20 se 2 of minimum
90130 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20 allocation size
90140 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e in bytes */. in
90150 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 t iOffset;
90160 20 2f 2a 20 41 6e 20 6f 66 66 73 65 74 20 69 6e /* An offset in
90170 74 6f 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d 20 to mem5.aCtrl[]
90180 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 */.. UNUSED_PAR
90190 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
901a0 0a 0a 20 20 2f 2a 20 46 6f 72 20 74 68 65 20 70 .. /* For the p
901b0 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 urposes of this
901c0 72 6f 75 74 69 6e 65 2c 20 64 69 73 61 62 6c 65 routine, disable
901d0 20 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 the mutex */.
901e0 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0a mem5.mutex = 0;.
901f0 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65 20 6f . /* The size o
90200 66 20 61 20 4d 65 6d 35 4c 69 6e 6b 20 6f 62 6a f a Mem5Link obj
90210 65 63 74 20 6d 75 73 74 20 62 65 20 61 20 70 6f ect must be a po
90220 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 56 65 72 wer of two. Ver
90230 69 66 79 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 ify that. ** th
90240 69 73 20 69 73 20 63 61 73 65 2e 0a 20 20 2a 2f is is case.. */
90250 0a 20 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 . assert( (size
90260 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 26 28 73 69 of(Mem5Link)&(si
90270 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 2d 31 zeof(Mem5Link)-1
90280 29 29 3d 3d 30 20 29 3b 0a 0a 20 20 6e 42 79 74 ))==0 );.. nByt
90290 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 e = sqlite3Globa
902a0 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a 20 lConfig.nHeap;.
902b0 20 7a 42 79 74 65 20 3d 20 28 75 38 2a 29 73 71 zByte = (u8*)sq
902c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
902d0 67 2e 70 48 65 61 70 3b 0a 20 20 61 73 73 65 72 g.pHeap;. asser
902e0 74 28 20 7a 42 79 74 65 21 3d 30 20 29 3b 20 20 t( zByte!=0 );
902f0 2f 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 /* sqlite3_confi
90300 67 28 29 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c g() does not all
90310 6f 77 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a ow otherwise */.
90320 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d . nMinLog = mem
90330 73 79 73 35 4c 6f 67 28 73 71 6c 69 74 65 33 47 sys5Log(sqlite3G
90340 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 lobalConfig.mnRe
90350 71 29 3b 0a 20 20 6d 65 6d 35 2e 73 7a 41 74 6f q);. mem5.szAto
90360 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e 4c 6f 67 29 m = (1<<nMinLog)
90370 3b 0a 20 20 77 68 69 6c 65 28 20 28 69 6e 74 29 ;. while( (int)
90380 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29 sizeof(Mem5Link)
90390 3e 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 29 7b 0a >mem5.szAtom ){.
903a0 20 20 20 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 mem5.szAtom
903b0 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c = mem5.szAtom <<
903c0 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 35 2e 1;. }.. mem5.
903d0 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74 65 20 nBlock = (nByte
903e0 2f 20 28 6d 65 6d 35 2e 73 7a 41 74 6f 6d 2b 73 / (mem5.szAtom+s
903f0 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20 6d izeof(u8)));. m
90400 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79 74 em5.zPool = zByt
90410 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 20 e;. mem5.aCtrl
90420 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a 50 = (u8 *)&mem5.zP
90430 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 2a ool[mem5.nBlock*
90440 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 0a 20 mem5.szAtom];..
90450 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c for(ii=0; ii<=L
90460 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20 OGMAX; ii++){.
90470 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 mem5.aiFreelis
90480 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a t[ii] = -1;. }.
90490 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a . iOffset = 0;.
904a0 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b for(ii=LOGMAX;
904b0 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20 ii>=0; ii--){.
904c0 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 int nAlloc =
904d0 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28 (1<<ii);. if(
904e0 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 (iOffset+nAlloc
904f0 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 )<=mem5.nBlock )
90500 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74 {. mem5.aCt
90510 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69 rl[iOffset] = ii
90520 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 | CTRL_FREE;.
90530 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 memsys5Link(
90540 69 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20 iOffset, ii);.
90550 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e iOffset += n
90560 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 Alloc;. }.
90570 20 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74 assert((iOffset
90580 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42 +nAlloc)>mem5.nB
90590 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a lock);. }.. /*
905a0 20 49 66 20 61 20 6d 75 74 65 78 20 69 73 20 72 If a mutex is r
905b0 65 71 75 69 72 65 64 20 66 6f 72 20 6e 6f 72 6d equired for norm
905c0 61 6c 20 6f 70 65 72 61 74 69 6f 6e 2c 20 61 6c al operation, al
905d0 6c 6f 63 61 74 65 20 6f 6e 65 20 2a 2f 0a 20 20 locate one */.
905e0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 if( sqlite3Globa
905f0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 lConfig.bMemstat
90600 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e ==0 ){. mem5.
90610 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d mutex = sqlite3M
90620 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
90630 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 _MUTEX_STATIC_ME
90640 4d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 M);. }.. retur
90650 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
90660 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 /*.** Deinitiali
90670 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a ze this module..
90680 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
90690 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 28 76 emsys5Shutdown(v
906a0 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
906b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
906c0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 R(NotUsed);. me
906d0 6d 35 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 m5.mutex = 0;.
906e0 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23 69 66 64 65 return;.}..#ifde
906f0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a f SQLITE_TEST./*
90700 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c .** Open the fil
90710 65 20 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 e indicated and
90720 77 72 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 write a log of a
90730 6c 6c 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 ll unfreed memor
90740 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e y .** allocation
90750 73 20 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e s into that log.
90760 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
90770 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d TE void sqlite3M
90780 65 6d 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 emsys5Dump(const
90790 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
907a0 29 7b 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a ){. FILE *out;.
907b0 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 int i, j, n;.
907c0 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a 0a 20 int nMinLog;..
907d0 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d if( zFilename==
907e0 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 0 || zFilename[0
907f0 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75 74 20 ]==0 ){. out
90800 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65 6c 73 = stdout;. }els
90810 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 e{. out = fop
90820 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 22 77 en(zFilename, "w
90830 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 3d ");. if( out=
90840 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 =0 ){. fpri
90850 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20 ntf(stderr, "**
90860 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74 Unable to output
90870 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75 memory debug ou
90880 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c tput log: %s **\
90890 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n",.
908a0 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e zFilen
908b0 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ame);. retu
908c0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
908d0 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a memsys5Enter();.
908e0 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65 6d 73 nMinLog = mems
908f0 79 73 35 4c 6f 67 28 6d 65 6d 35 2e 73 7a 41 74 ys5Log(mem5.szAt
90900 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 om);. for(i=0;
90910 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b 6e i<=LOGMAX && i+n
90920 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29 7b MinLog<32; i++){
90930 0a 20 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a 3d . for(n=0, j=
90940 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b mem5.aiFreelist[
90950 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d 45 i]; j>=0; j = ME
90960 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74 2c M5LINK(j)->next,
90970 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72 69 n++){}. fpri
90980 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65 6c 69 ntf(out, "freeli
90990 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69 7a 65 st items of size
909a0 20 25 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d 35 %d: %d\n", mem5
909b0 2e 73 7a 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29 .szAtom << i, n)
909c0 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 ;. }. fprintf(
909d0 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f out, "mem5.nAllo
909e0 63 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e c = %llu\n
909f0 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b ", mem5.nAlloc);
90a00 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 . fprintf(out,
90a10 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 "mem5.totalAlloc
90a20 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 = %llu\n", me
90a30 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a m5.totalAlloc);.
90a40 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
90a50 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 mem5.totalExcess
90a60 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d = %llu\n", mem
90a70 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a 5.totalExcess);.
90a80 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
90a90 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 mem5.currentOut
90aa0 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %u\n", mem5.
90ab0 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66 currentOut);. f
90ac0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d printf(out, "mem
90ad0 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d 5.currentCount =
90ae0 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72 %u\n", mem5.cur
90af0 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70 rentCount);. fp
90b00 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 rintf(out, "mem5
90b10 2e 6d 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20 .maxOut =
90b20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f %u\n", mem5.maxO
90b30 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f ut);. fprintf(o
90b40 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75 ut, "mem5.maxCou
90b50 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20 nt = %u\n",
90b60 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a mem5.maxCount);.
90b70 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 fprintf(out, "
90b80 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20 mem5.maxRequest
90b90 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e = %u\n", mem5.
90ba0 6d 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d maxRequest);. m
90bb0 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 emsys5Leave();.
90bc0 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74 if( out==stdout
90bd0 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73 ){. fflush(s
90be0 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b tdout);. }else{
90bf0 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 . fclose(out)
90c00 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a ;. }.}.#endif..
90c10 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
90c20 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 ne is the only r
90c30 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 outine in this f
90c40 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 ile with externa
90c50 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 20 49 l .** linkage. I
90c60 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e t returns a poin
90c70 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 ter to a static
90c80 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 sqlite3_mem_meth
90c90 6f 64 73 0a 2a 2a 20 73 74 72 75 63 74 20 70 6f ods.** struct po
90ca0 70 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 pulated with the
90cb0 20 6d 65 6d 73 79 73 35 20 6d 65 74 68 6f 64 73 memsys5 methods
90cc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
90cd0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 ATE const sqlite
90ce0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 3_mem_methods *s
90cf0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 qlite3MemGetMems
90d00 79 73 35 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 ys5(void){. sta
90d10 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
90d20 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 65 3_mem_methods me
90d30 6d 73 79 73 35 4d 65 74 68 6f 64 73 20 3d 20 7b msys5Methods = {
90d40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 4d 61 6c . memsys5Mal
90d50 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 loc,. memsys
90d60 35 46 72 65 65 2c 0a 20 20 20 20 20 6d 65 6d 73 5Free,. mems
90d70 79 73 35 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 ys5Realloc,.
90d80 20 6d 65 6d 73 79 73 35 53 69 7a 65 2c 0a 20 20 memsys5Size,.
90d90 20 20 20 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 memsys5Roundu
90da0 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 49 p,. memsys5I
90db0 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 nit,. memsys
90dc0 35 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 5Shutdown,.
90dd0 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 0. };. return
90de0 26 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73 3b &memsys5Methods;
90df0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 .}..#endif /* SQ
90e00 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 LITE_ENABLE_MEMS
90e10 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a YS5 */../*******
90e20 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d ******* End of m
90e30 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a em5.c **********
90e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90e60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
90e70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
90e80 6c 65 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a le mutex.c *****
90e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
90eb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
90ec0 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 07 August 14.**.
90ed0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
90ee0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
90ef0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
90f00 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
90f10 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
90f20 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
90f30 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
90f40 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
90f50 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
90f60 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
90f70 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
90f80 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
90f90 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
90fa0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
90fb0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
90fc0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
90fd0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
90fe0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
90ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
91030 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
91040 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e s the C function
91050 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 s that implement
91060 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 mutexes..**.**
91070 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
91080 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 ns code that is
91090 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73 73 20 61 6c common across al
910a0 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e l mutex implemen
910b0 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 0a 23 69 66 tations..*/..#if
910c0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
910d0 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e DEBUG) && !defin
910e0 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f ed(SQLITE_MUTEX_
910f0 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 OMIT)./*.** For
91100 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 debugging purpos
91110 65 73 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 es, record when
91120 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 the mutex subsys
91130 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a tem is initializ
91140 65 64 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 ed.** and uninit
91150 69 61 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 ialized so that
91160 77 65 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 we can assert()
91170 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 if there is an a
91180 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c ttempt to.** all
91190 6f 63 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 ocate a mutex wh
911a0 69 6c 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 ile the system i
911b0 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e s uninitialized.
911c0 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
911d0 45 5f 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 E_WSD int mutexI
911e0 73 49 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 sInit = 0;.#endi
911f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 f /* SQLITE_DEBU
91200 47 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 G */...#ifndef S
91210 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 QLITE_MUTEX_OMIT
91220 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a ./*.** Initializ
91230 65 20 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 e the mutex syst
91240 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 em..*/.SQLITE_PR
91250 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
91260 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 3MutexInit(void)
91270 7b 20 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 { . int rc = SQ
91280 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 LITE_OK;. if( s
91290 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
912a0 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b ig.bCoreMutex ){
912b0 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 . if( !sqlite
912c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 3GlobalConfig.mu
912d0 74 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 tex.xMutexAlloc
912e0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 ){. /* If t
912f0 68 65 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d he xMutexAlloc m
91300 65 74 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 ethod has not be
91310 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 en set, then the
91320 20 75 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 user did not.
91330 20 20 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 ** install a
91340 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 mutex implement
91350 61 74 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 ation via sqlite
91360 33 5f 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 3_config() prior
91370 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 to . ** sq
91380 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
91390 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e () being called.
913a0 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 This block copi
913b0 65 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 es pointers to.
913c0 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 ** the defa
913d0 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 ult implementati
913e0 6f 6e 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 on into the sqli
913f0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 te3GlobalConfig
91400 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 structure..
91410 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
91420 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 3_mutex_methods
91430 2a 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 *pFrom = sqlite3
91440 44 65 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a DefaultMutex();.
91450 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
91460 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f tex_methods *pTo
91470 20 3d 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 = &sqlite3Globa
91480 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a lConfig.mutex;..
91490 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f memcpy(pTo
914a0 2c 20 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f , pFrom, offseto
914b0 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f f(sqlite3_mutex_
914c0 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 methods, xMutexA
914d0 6c 6c 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 lloc));. me
914e0 6d 63 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 mcpy(&pTo->xMute
914f0 78 46 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 xFree, &pFrom->x
91500 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 MutexFree,.
91510 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a sizeof(*
91520 70 54 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 pTo) - offsetof(
91530 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
91540 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 thods, xMutexFre
91550 65 29 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e e));. pTo->
91560 78 4d 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 xMutexAlloc = pF
91570 72 6f 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 rom->xMutexAlloc
91580 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
91590 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
915a0 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
915b0 78 49 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 xInit();. }..#i
915c0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
915d0 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 G. GLOBAL(int,
915e0 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 mutexIsInit) = 1
915f0 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 ;.#endif.. retu
91600 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
91610 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 Shutdown the mut
91620 65 78 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 ex system. This
91630 63 61 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 call frees resou
91640 72 63 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 rces allocated b
91650 79 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 y.** sqlite3Mute
91660 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 xInit()..*/.SQLI
91670 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
91680 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 qlite3MutexEnd(v
91690 6f 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d oid){. int rc =
916a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
916b0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
916c0 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
916d0 65 78 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 exEnd ){. rc
916e0 3d 20 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 2e 78 4d 75 74 onfig.mutex.xMut
91700 65 78 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 exEnd();. }..#i
91710 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
91720 47 0a 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 G. GLOBAL(int,
91730 6d 75 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 mutexIsInit) = 0
91740 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 ;.#endif.. retu
91750 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
91760 52 65 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 Retrieve a point
91770 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d er to a static m
91780 75 74 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 utex or allocate
91790 20 61 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f a new dynamic o
917a0 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ne..*/.SQLITE_AP
917b0 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 I sqlite3_mutex
917c0 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 *sqlite3_mutex_a
917d0 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 lloc(int id){.#i
917e0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
917f0 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 T_AUTOINIT. if(
91800 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c sqlite3_initial
91810 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 ize() ) return 0
91820 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 ;.#endif. retur
91830 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 n sqlite3GlobalC
91840 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 onfig.mutex.xMut
91850 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a exAlloc(id);.}..
91860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
91870 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 qlite3_mutex *sq
91880 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
91890 69 6e 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 int id){. if( !
918a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
918b0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 fig.bCoreMutex )
918c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a {. return 0;.
918d0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c }. assert( GL
918e0 4f 42 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 OBAL(int, mutexI
918f0 73 49 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 sInit) );. retu
91900 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c rn sqlite3Global
91910 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 Config.mutex.xMu
91920 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a texAlloc(id);.}.
91930 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 ./*.** Free a dy
91940 6e 61 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a namic mutex..*/.
91950 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 SQLITE_API void
91960 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 sqlite3_mutex_fr
91970 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ee(sqlite3_mutex
91980 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b *p){. if( p ){
91990 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
919a0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
919b0 4d 75 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 MutexFree(p);.
919c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 }.}../*.** Obtai
919d0 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 n the mutex p. I
919e0 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 f some other thr
919f0 65 61 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 ead already has
91a00 74 68 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b the mutex, block
91a10 0a 2a 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e .** until it can
91a20 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f be obtained..*/
91a30 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
91a40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
91a50 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 nter(sqlite3_mut
91a60 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 ex *p){. if( p
91a70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
91a80 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 obalConfig.mutex
91a90 2e 78 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b .xMutexEnter(p);
91aa0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 . }.}../*.** Ob
91ab0 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 tain the mutex p
91ac0 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c . If successful,
91ad0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
91ae0 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 K. Otherwise, if
91af0 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 another.** thre
91b00 61 64 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 ad holds the mut
91b10 65 78 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 ex and it cannot
91b20 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 be obtained, re
91b30 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 turn SQLITE_BUSY
91b40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
91b50 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 int sqlite3_mute
91b60 78 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 x_try(sqlite3_mu
91b70 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 tex *p){. int r
91b80 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
91b90 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 if( p ){. re
91ba0 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 turn sqlite3Glob
91bb0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91bc0 4d 75 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d MutexTry(p);. }
91bd0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
91be0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
91bf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 e3_mutex_leave()
91c00 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 routine exits a
91c10 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 20 mutex that was
91c20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e previously.** en
91c30 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 6d tered by the sam
91c40 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 e thread. The b
91c50 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 ehavior is undef
91c60 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 ined if the mute
91c70 78 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 x .** is not cur
91c80 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 rently entered.
91c90 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 If a NULL pointe
91ca0 72 20 69 73 20 70 61 73 73 65 64 20 61 73 20 61 r is passed as a
91cb0 6e 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 n argument.** th
91cc0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
91cd0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 no-op..*/.SQLIT
91ce0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
91cf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 e3_mutex_leave(s
91d00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
91d10 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 {. if( p ){.
91d20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
91d30 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
91d40 78 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d xLeave(p);. }.}
91d50 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
91d60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
91d70 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
91d80 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
91d90 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 x_notheld() rout
91da0 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e ine are.** inten
91db0 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 ded for use insi
91dc0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
91dd0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ements..*/.SQLIT
91de0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
91df0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 3_mutex_held(sql
91e00 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
91e10 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c return p==0 ||
91e20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
91e30 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 nfig.mutex.xMute
91e40 78 48 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 xHeld(p);.}.SQLI
91e50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
91e60 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
91e70 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
91e80 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d p){. return p==
91e90 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 0 || sqlite3Glob
91ea0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 alConfig.mutex.x
91eb0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b MutexNotheld(p);
91ec0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 .}.#endif..#endi
91ed0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 f /* SQLITE_MUTE
91ee0 58 5f 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a X_OMIT */../****
91ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
91f00 66 20 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a f mutex.c ******
91f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
91f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
91f50 20 66 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 file mutex_noop
91f60 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
91f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
91f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
91f90 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 2008 October 07
91fa0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
91fb0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
91fc0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
91fd0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
91fe0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
91ff0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
92000 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
92010 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
92020 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
92030 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
92040 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
92050 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
92060 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
92070 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
92080 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
92090 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
920a0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
920b0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
920c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
920d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
920e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
920f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
92100 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e ** This file con
92110 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 tains the C func
92120 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 tions that imple
92130 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a ment mutexes..**
92140 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 .** This impleme
92150 6e 74 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 ntation in this
92160 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 file does not pr
92170 6f 76 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c ovide any mutual
92180 0a 2a 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e .** exclusion an
92190 64 20 69 73 20 74 68 75 73 20 73 75 69 74 61 62 d is thus suitab
921a0 6c 65 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 le for use only
921b0 69 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a in applications.
921c0 2a 2a 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 ** that use SQLi
921d0 74 65 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 te in a single t
921e0 68 72 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 hread. The rout
921f0 69 6e 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 ines defined.**
92200 68 65 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 here are place-h
92210 6f 6c 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 olders. Applica
92220 74 69 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 tions can substi
92230 74 75 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 tute working.**
92240 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 mutex routines a
92250 74 20 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 t start-time usi
92260 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 ng the.**.**
92270 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 sqlite3_config(
92280 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 SQLITE_CONFIG_MU
92290 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 TEX,...).**.** i
922a0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 nterface..**.**
922b0 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 If compiled with
922c0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 SQLITE_DEBUG, t
922d0 68 65 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c hen additional l
922e0 6f 67 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 ogic is inserted
922f0 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 .** that does er
92300 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 ror checking on
92310 6d 75 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 mutexes to make
92320 73 75 72 65 20 74 68 65 79 20 61 72 65 20 62 65 sure they are be
92330 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f ing.** called co
92340 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 23 69 rrectly..*/...#i
92350 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 f defined(SQLITE
92360 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 _MUTEX_NOOP) &&
92370 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
92380 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53 74 75 DEBUG)./*.** Stu
92390 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 b routines for a
923a0 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73 ll mutex methods
923b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ..**.** This rou
923c0 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 6e 6f tines provide no
923d0 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f mutual exclusio
923e0 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65 63 6b n or error check
923f0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
92400 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64 nt noopMutexHeld
92410 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
92420 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a p){ return 1; }.
92430 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d static int noopM
92440 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
92450 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 te3_mutex *p){ r
92460 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 eturn 1; }.stati
92470 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 49 c int noopMutexI
92480 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 nit(void){ retur
92490 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 n SQLITE_OK; }.s
924a0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 tatic int noopMu
924b0 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 texEnd(void){ re
924c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 turn SQLITE_OK;
924d0 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 }.static sqlite3
924e0 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75 74 65 _mutex *noopMute
924f0 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b 20 xAlloc(int id){
92500 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f return (sqlite3_
92510 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74 61 74 mutex*)8; }.stat
92520 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 ic void noopMute
92530 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 xFree(sqlite3_mu
92540 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b tex *p){ return;
92550 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e }.static void n
92560 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 oopMutexEnter(sq
92570 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
92580 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 return; }.stati
92590 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 54 c int noopMutexT
925a0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ry(sqlite3_mutex
925b0 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53 51 4c *p){ return SQL
925c0 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
925d0 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65 78 4c void noopMutexL
925e0 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 eave(sqlite3_mut
925f0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 ex *p){ return;
92600 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 }..SQLITE_PRIVAT
92610 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
92620 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
92630 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
92640 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
92650 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
92660 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
92670 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 2c noopMutexInit,
92680 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e . noopMutexEn
92690 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 d,. noopMutex
926a0 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f 70 4d Alloc,. noopM
926b0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6e 6f utexFree,. no
926c0 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 opMutexEnter,.
926d0 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79 2c 0a noopMutexTry,.
926e0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 noopMutexLea
926f0 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 ve,.. noopMut
92700 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f 6f 70 exHeld,. noop
92710 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20 20 7d MutexNotheld. }
92720 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 ;.. return &sMu
92730 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a tex;.}.#endif /*
92740 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
92750 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 21 MUTEX_NOOP) && !
92760 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 defined(SQLITE_D
92770 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20 64 65 EBUG) */..#if de
92780 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 fined(SQLITE_MUT
92790 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 EX_NOOP) && defi
927a0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
927b0 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 )./*.** In this
927c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 implementation,
927d0 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 error checking i
927e0 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72 20 74 s provided for t
927f0 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64 65 esting.** and de
92800 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73 bugging purposes
92810 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73 20 73 . The mutexes s
92820 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72 6f 76 till do not prov
92830 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74 75 61 ide any.** mutua
92840 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a 2f 0a l exclusion..*/.
92850 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 ./*.** The mutex
92860 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72 75 63 object.*/.struc
92870 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 t sqlite3_mutex
92880 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 {. int id;
92890 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74 79 70 /* The mutex typ
928a0 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 e */. int cnt;
928b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
928c0 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75 74 20 entries without
928d0 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61 76 65 a matching leave
928e0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 */.};../*.** Th
928f0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
92900 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
92910 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
92920 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
92930 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
92940 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 se inside assert
92950 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a () statements..*
92960 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 /.static int deb
92970 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 ugMutexHeld(sqli
92980 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
92990 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 return p==0 ||
929a0 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74 61 74 p->cnt>0;.}.stat
929b0 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 ic int debugMute
929c0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 xNotheld(sqlite3
929d0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 _mutex *p){. re
929e0 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e turn p==0 || p->
929f0 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a cnt==0;.}../*.**
92a00 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 Initialize and
92a10 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 deinitialize the
92a20 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d mutex subsystem
92a30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
92a40 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 28 76 debugMutexInit(v
92a50 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c oid){ return SQL
92a60 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 ITE_OK; }.static
92a70 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 45 int debugMutexE
92a80 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e nd(void){ return
92a90 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f SQLITE_OK; }../
92aa0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 *.** The sqlite3
92ab0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 _mutex_alloc() r
92ac0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 outine allocates
92ad0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 a new.** mutex
92ae0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f and returns a po
92af0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 inter to it. If
92b00 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c it returns NULL
92b10 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 .** that means t
92b20 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c hat a mutex coul
92b30 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 d not be allocat
92b40 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ed. .*/.static s
92b50 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 64 65 qlite3_mutex *de
92b60 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e bugMutexAlloc(in
92b70 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 t id){. static
92b80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 61 53 sqlite3_mutex aS
92b90 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71 6c 69 tatic[6];. sqli
92ba0 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65 77 20 te3_mutex *pNew
92bb0 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28 20 69 = 0;. switch( i
92bc0 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 d ){. case SQ
92bd0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a LITE_MUTEX_FAST:
92be0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
92bf0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
92c00 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d : {. pNew =
92c10 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 73 sqlite3Malloc(s
92c20 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 izeof(*pNew));.
92c30 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
92c40 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 . pNew->i
92c50 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 20 20 d = id;.
92c60 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 pNew->cnt = 0;.
92c70 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
92c80 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
92c90 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
92ca0 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d 20 30 ssert( id-2 >= 0
92cb0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
92cc0 28 20 69 64 2d 32 20 3c 20 28 69 6e 74 29 28 73 ( id-2 < (int)(s
92cd0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 29 2f 73 izeof(aStatic)/s
92ce0 69 7a 65 6f 66 28 61 53 74 61 74 69 63 5b 30 5d izeof(aStatic[0]
92cf0 29 29 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 )) );. pNew
92d00 20 3d 20 26 61 53 74 61 74 69 63 5b 69 64 2d 32 = &aStatic[id-2
92d10 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 ];. pNew->i
92d20 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20 62 72 d = id;. br
92d30 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 eak;. }. }.
92d40 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
92d50 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
92d60 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 ine deallocates
92d70 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c a previously all
92d80 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2f ocated mutex..*/
92d90 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 .static void deb
92da0 75 67 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 ugMutexFree(sqli
92db0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
92dc0 20 61 73 73 65 72 74 28 20 70 2d 3e 63 6e 74 3d assert( p->cnt=
92dd0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
92de0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 p->id==SQLITE_MU
92df0 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 TEX_FAST || p->i
92e00 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
92e10 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 73 RECURSIVE );. s
92e20 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
92e30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
92e40 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
92e50 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
92e60 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
92e70 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
92e80 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
92e90 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
92ea0 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
92eb0 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
92ec0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
92ed0 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
92ee0 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
92ef0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
92f00 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
92f10 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
92f20 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
92f30 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
92f40 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
92f50 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
92f60 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
92f70 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
92f80 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
92f90 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
92fa0 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
92fb0 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
92fc0 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
92fd0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
92fe0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
92ff0 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
93000 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
93010 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
93020 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
93030 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
93040 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
93050 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
93060 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
93070 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
93080 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
93090 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
930a0 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 62 75 static void debu
930b0 67 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 gMutexEnter(sqli
930c0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
930d0 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d assert( p->id==
930e0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
930f0 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67 4d URSIVE || debugM
93100 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 utexNotheld(p) )
93110 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a ;. p->cnt++;.}.
93120 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 static int debug
93130 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 MutexTry(sqlite3
93140 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 _mutex *p){. as
93150 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
93160 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
93170 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 IVE || debugMute
93180 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 xNotheld(p) );.
93190 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 72 65 74 p->cnt++;. ret
931a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
931b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ../*.** The sqli
931c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
931d0 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 20 ) routine exits
931e0 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 73 a mutex that was
931f0 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 65 .** previously e
93200 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 61 ntered by the sa
93210 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 me thread. The
93220 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 75 behavior.** is u
93230 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20 ndefined if the
93240 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 72 mutex is not cur
93250 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 6f rently entered o
93260 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 r.** is not curr
93270 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 2e ently allocated.
93280 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 SQLite will ne
93290 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a 2a ver do either..*
932a0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 /.static void de
932b0 62 75 67 4d 75 74 65 78 4c 65 61 76 65 28 73 71 bugMutexLeave(sq
932c0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
932d0 0a 20 20 61 73 73 65 72 74 28 20 64 65 62 75 67 . assert( debug
932e0 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a MutexHeld(p) );.
932f0 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a 20 20 61 73 p->cnt--;. as
93300 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c sert( p->id==SQL
93310 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
93320 49 56 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 IVE || debugMute
93330 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 7d xNotheld(p) );.}
93340 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ..SQLITE_PRIVATE
93350 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d sqlite3_mutex_m
93360 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 ethods *sqlite3D
93370 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 efaultMutex(void
93380 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
93390 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 te3_mutex_method
933a0 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 s sMutex = {.
933b0 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74 2c debugMutexInit,
933c0 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45 . debugMutexE
933d0 6e 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 nd,. debugMut
933e0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 64 65 62 exAlloc,. deb
933f0 75 67 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 ugMutexFree,.
93400 20 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 debugMutexEnter
93410 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 ,. debugMutex
93420 54 72 79 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 Try,. debugMu
93430 74 65 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 64 texLeave,.. d
93440 65 62 75 67 4d 75 74 65 78 48 65 6c 64 2c 0a 20 ebugMutexHeld,.
93450 20 20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 debugMutexNot
93460 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 held. };.. ret
93470 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 urn &sMutex;.}.#
93480 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 endif /* defined
93490 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f (SQLITE_MUTEX_NO
934a0 4f 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 OP) && defined(S
934b0 51 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a QLITE_DEBUG) */.
934c0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
934d0 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 6e 6f End of mutex_no
934e0 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a op.c ***********
934f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
93510 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
93520 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 Begin file mute
93530 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a x_os2.c ********
93540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
93550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
93560 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 ./*.** 2007 Augu
93570 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 st 28.**.** The
93580 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
93590 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
935a0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
935b0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
935c0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
935d0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
935e0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
935f0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
93600 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
93610 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
93620 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
93630 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
93640 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
93650 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
93660 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
93670 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
93680 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
93690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
936a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
936b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
936c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
936d0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c ****.** This fil
936e0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 e contains the C
936f0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 functions that
93700 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
93710 73 20 66 6f 72 20 4f 53 2f 32 0a 2a 2f 0a 0a 2f s for OS/2.*/../
93720 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e *.** The code in
93730 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e this file is on
93740 6c 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 ly used if SQLIT
93750 45 5f 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 E_MUTEX_OS2 is d
93760 65 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 efined..** See t
93770 68 65 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 he mutex.h file
93780 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a for details..*/.
93790 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 #ifdef SQLITE_MU
937a0 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a TEX_OS2../******
937b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
937c0 20 4f 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c OS/2 Mutex Impl
937d0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a ementation *****
937e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
937f0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 *.**.** This imp
93800 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d lementation of m
93810 75 74 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 utexes is built
93820 75 73 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 using the OS/2 A
93830 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 PI..*/../*.** Th
93840 65 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a e mutex object.*
93850 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 * Each recursive
93860 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 mutex is an ins
93870 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
93880 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
93890 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
938a0 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d te3_mutex {. HM
938b0 54 58 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 TX mutex;
938c0 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c /* Mutex control
938d0 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f ling the lock */
938e0 0a 20 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 . int id;
938f0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 /* Mutex ty
93900 70 65 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 pe */. int nRe
93910 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d f; /* Num
93920 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
93930 73 20 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 s */. TID owne
93940 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 r; /* Thre
93950 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 ad holding this
93960 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 mutex */.};..#de
93970 66 69 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 fine OS2_MUTEX_I
93980 4e 49 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 NITIALIZER 0,0
93990 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 ,0,0../*.** Init
939a0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 ialize and deini
939b0 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 tialize the mute
939c0 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a x subsystem..*/.
939d0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 static int os2Mu
939e0 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 texInit(void){ r
939f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
93a00 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 }.static int os
93a10 32 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 2MutexEnd(void){
93a20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
93a30 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 K; }../*.** The
93a40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
93a50 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c loc() routine al
93a60 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a locates a new.**
93a70 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 mutex and retur
93a80 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
93a90 69 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 it. If it retur
93aa0 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 ns NULL.** that
93ab0 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 means that a mut
93ac0 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 ex could not be
93ad0 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 allocated. .** S
93ae0 51 4c 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e QLite will unwin
93af0 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 d its stack and
93b00 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e return an error.
93b10 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a The argument.*
93b20 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 * to sqlite3_mut
93b30 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e ex_alloc() is on
93b40 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 e of these integ
93b50 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a er constants:.**
93b60 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e .** <ul>.** <li>
93b70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 SQLITE_MUTEX_F
93b80 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 AST
93b90 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 0.** <li> SQL
93ba0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
93bb0 49 56 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a IVE 1.*
93bc0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
93bd0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
93be0 45 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 ER 2.** <li
93bf0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f > SQLITE_MUTEX_
93c00 53 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 STATIC_MEM
93c10 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 3.** <li> SQ
93c20 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 LITE_MUTEX_STATI
93c30 43 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a C_PRNG 4.
93c40 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 ** </ul>.**.** T
93c50 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e he first two con
93c60 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c stants cause sql
93c70 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 ite3_mutex_alloc
93c80 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 () to create.**
93c90 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 a new mutex. Th
93ca0 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 e new mutex is r
93cb0 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 ecursive when SQ
93cc0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
93cd0 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 SIVE.** is used
93ce0 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 but not necessar
93cf0 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 ily so when SQLI
93d00 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 TE_MUTEX_FAST is
93d10 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 used..** The mu
93d20 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 tex implementati
93d30 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 on does not need
93d40 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 to make a disti
93d50 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 nction.** betwee
93d60 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 n SQLITE_MUTEX_R
93d70 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c ECURSIVE and SQL
93d80 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 ITE_MUTEX_FAST i
93d90 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 f it does.** not
93da0 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 want to. But S
93db0 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 QLite will only
93dc0 72 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 request a recurs
93dd0 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 ive mutex in.**
93de0 63 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 cases where it r
93df0 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e eally needs one.
93e00 20 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f If a faster no
93e10 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 n-recursive mute
93e20 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 x.** implementat
93e30 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 ion is available
93e40 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 on the host pla
93e50 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 tform, the mutex
93e60 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 subsystem.** mi
93e70 67 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 ght return such
93e80 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f a mutex in respo
93e90 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 nse to SQLITE_MU
93ea0 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 TEX_FAST..**.**
93eb0 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 The other allowe
93ec0 64 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 d parameters to
93ed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
93ee0 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 loc() each retur
93ef0 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 n.** a pointer t
93f00 6f 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 o a static preex
93f10 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 isting mutex. T
93f20 68 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 hree static mute
93f30 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 xes are.** used
93f40 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 by the current v
93f50 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 ersion of SQLite
93f60 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f . Future versio
93f70 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 ns of SQLite.**
93f80 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e may add addition
93f90 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 al static mutexe
93fa0 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 s. Static mutex
93fb0 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 es are for inter
93fc0 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 nal.** use by SQ
93fd0 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c Lite only. Appl
93fe0 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 ications that us
93ff0 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 e SQLite mutexes
94000 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f should.** use o
94010 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 nly the dynamic
94020 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 mutexes returned
94030 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 by SQLITE_MUTEX
94040 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 _FAST or.** SQLI
94050 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
94060 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 VE..**.** Note t
94070 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 hat if one of th
94080 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 e dynamic mutex
94090 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 parameters (SQLI
940a0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a TE_MUTEX_FAST.**
940b0 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 or SQLITE_MUTEX
940c0 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 _RECURSIVE) is u
940d0 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 sed then sqlite3
940e0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a _mutex_alloc().*
940f0 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 * returns a diff
94100 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 erent mutex on e
94110 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 very call. But
94120 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a for the static.*
94130 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 * mutex types, t
94140 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 he same mutex is
94150 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 returned on eve
94160 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 ry call that has
94170 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 .** the same typ
94180 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 e number..*/.sta
94190 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 tic sqlite3_mute
941a0 78 20 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 x *os2MutexAlloc
941b0 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 (int iType){. s
941c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 qlite3_mutex *p
941d0 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 = NULL;. switch
941e0 28 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 ( iType ){. c
941f0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
94200 5f 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 _FAST:. case
94210 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
94220 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 URSIVE: {.
94230 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f p = sqlite3Mallo
94240 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 cZero( sizeof(*p
94250 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ) );. if( p
94260 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 ){. p->i
94270 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 d = iType;.
94280 20 20 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 if( DosCreate
94290 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d MutexSem( 0, &p-
942a0 3e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 >mutex, 0, FALSE
942b0 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 ) != NO_ERROR )
942c0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 {. sqli
942d0 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 te3_free( p );.
942e0 20 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c p = NUL
942f0 4c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 L;. }.
94300 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
94310 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 ;. }. defa
94320 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 ult: {. sta
94330 74 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 tic volatile int
94340 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 isInit = 0;.
94350 20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 static sqlite
94360 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 3_mutex staticMu
94370 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 texes[] = {.
94380 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
94390 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
943a0 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
943b0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
943c0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 },. { OS
943d0 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 2_MUTEX_INITIALI
943e0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 ZER, },.
943f0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 { OS2_MUTEX_INIT
94400 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 IALIZER, },.
94410 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f { OS2_MUTEX_
94420 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a INITIALIZER, },.
94430 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 { OS2_MU
94440 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c TEX_INITIALIZER,
94450 20 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 },. };.
94460 20 20 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 if ( !isInit
94470 29 7b 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 ){. APIRE
94480 54 20 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 T rc;. PT
94490 49 42 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 IB ptib;.
944a0 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 PPIB ppib;.
944b0 20 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a HMTX mutex;.
944c0 20 20 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d char nam
944d0 65 5b 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 e[32];. D
944e0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 osGetInfoBlocks(
944f0 20 26 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b &ptib, &ppib );
94500 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
94510 5f 73 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f _snprintf( sizeo
94520 66 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 f(name), name, "
94530 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 \\SEM32\\SQLITE%
94540 30 34 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 04x",.
94550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
94560 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 ppib->pib_ulpid
94570 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 );. while
94580 28 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 ( !isInit ){.
94590 20 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 mutex = 0
945a0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
945b0 20 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 DosCreateMutexS
945c0 65 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 em( name, &mutex
945d0 2c 20 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 , 0, FALSE);.
945e0 20 20 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d if( rc ==
945f0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
94600 20 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 unsigne
94610 64 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 d int i;.
94620 20 20 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 if( !isInit
94630 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
94640 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 for( i = 0; i
94650 3c 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d < sizeof(staticM
94660 75 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 utexes)/sizeof(s
94670 74 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 taticMutexes[0])
94680 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 ; i++ ){.
94690 20 20 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 DosCrea
946a0 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 teMutexSem( 0, &
946b0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d staticMutexes[i]
946c0 2e 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 .mutex, 0, FALSE
946d0 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
946e0 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
946f0 20 20 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 isInit = 1;.
94700 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
94710 20 20 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 DosClose
94720 4d 75 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 MutexSem( mutex
94730 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
94740 73 65 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 se if( rc == ERR
94750 4f 52 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d OR_DUPLICATE_NAM
94760 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 E ){.
94770 20 44 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a DosSleep( 1 );.
94780 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
94790 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 . ret
947a0 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 urn p;.
947b0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
947c0 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 }. asser
947d0 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 t( iType-2 >= 0
947e0 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
947f0 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f iType-2 < sizeo
94800 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 f(staticMutexes)
94810 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 /sizeof(staticMu
94820 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 texes[0]) );.
94830 20 20 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 p = &staticMu
94840 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a texes[iType-2];.
94850 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
94860 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ype;. break
94870 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
94880 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a turn p;.}.../*.*
94890 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 * This routine d
948a0 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 eallocates a pre
948b0 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 viously allocate
948c0 64 20 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 d mutex..** SQLi
948d0 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f te is careful to
948e0 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 deallocate ever
948f0 79 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 y mutex that it
94900 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 allocates..*/.st
94910 61 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 atic void os2Mut
94920 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d exFree(sqlite3_m
94930 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 utex *p){. if(
94940 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 p==0 ) return;.
94950 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 assert( p->nRef
94960 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
94970 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d p->id==SQLITE_M
94980 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e UTEX_FAST || p->
94990 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 id==SQLITE_MUTEX
949a0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 _RECURSIVE );.
949b0 44 6f 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d DosCloseMutexSem
949c0 28 20 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 ( p->mutex );.
949d0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 sqlite3_free( p
949e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 );.}../*.** The
949f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
94a00 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 ter() and sqlite
94a10 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 3_mutex_try() ro
94a20 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a utines attempt.*
94a30 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 * to enter a mut
94a40 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 ex. If another
94a50 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 thread is alread
94a60 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 y within the mut
94a70 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ex,.** sqlite3_m
94a80 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c utex_enter() wil
94a90 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 l block and sqli
94aa0 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
94ab0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 will return.** S
94ac0 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 QLITE_BUSY. The
94ad0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 sqlite3_mutex_t
94ae0 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 ry() interface r
94af0 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
94b00 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 .** upon success
94b10 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 ful entry. Mute
94b20 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e xes created usin
94b30 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 g SQLITE_MUTEX_R
94b40 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 ECURSIVE can.**
94b50 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 be entered multi
94b60 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 ple times by the
94b70 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 same thread. I
94b80 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68 65 n such cases the
94b90 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 ,.** mutex must
94ba0 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 be exited an equ
94bb0 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d al number of tim
94bc0 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 es before anothe
94bd0 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 r thread.** can
94be0 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 enter. If the s
94bf0 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65 73 ame thread tries
94c00 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 to enter any ot
94c10 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 her kind of mute
94c20 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f x.** more than o
94c30 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f nce, the behavio
94c40 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a r is undefined..
94c50 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f */.static void o
94c60 73 32 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c s2MutexEnter(sql
94c70 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a ite3_mutex *p){.
94c80 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 TID tid;. PID
94c90 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e holder1;. ULON
94ca0 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 G holder2;. if(
94cb0 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a p==0 ) return;.
94cc0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d assert( p->id=
94cd0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
94ce0 43 55 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 CURSIVE || os2Mu
94cf0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b texNotheld(p) );
94d00 0a 20 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 . DosRequestMut
94d10 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 exSem(p->mutex,
94d20 53 45 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57 SEM_INDEFINITE_W
94d30 41 49 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 AIT);. DosQuery
94d40 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 MutexSem(p->mute
94d50 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 x, &holder1, &ti
94d60 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 d, &holder2);.
94d70 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a p->owner = tid;.
94d80 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 p->nRef++;.}.s
94d90 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 tatic int os2Mut
94da0 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 exTry(sqlite3_mu
94db0 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 tex *p){. int r
94dc0 63 3b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 c;. TID tid;.
94dd0 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 PID holder1;. U
94de0 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 LONG holder2;.
94df0 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 if( p==0 ) retur
94e00 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 n SQLITE_OK;. a
94e10 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
94e20 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
94e30 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 SIVE || os2Mutex
94e40 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
94e50 69 66 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75 if( DosRequestMu
94e60 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c texSem(p->mutex,
94e70 20 53 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52 SEM_IMMEDIATE_R
94e80 45 54 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52 ETURN) == NO_ERR
94e90 4f 52 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 OR) {. DosQue
94ea0 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 ryMutexSem(p->mu
94eb0 74 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 tex, &holder1, &
94ec0 74 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a tid, &holder2);.
94ed0 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 p->owner = t
94ee0 69 64 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b id;. p->nRef+
94ef0 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 +;. rc = SQLI
94f00 54 45 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20 TE_OK;. } else
94f10 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
94f20 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72 E_BUSY;. }.. r
94f30 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
94f40 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d ** The sqlite3_m
94f50 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 utex_leave() rou
94f60 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 tine exits a mut
94f70 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 ex that was.** p
94f80 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 reviously entere
94f90 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 d by the same th
94fa0 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 read. The behav
94fb0 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 ior.** is undefi
94fc0 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 ned if the mutex
94fd0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c is not currentl
94fe0 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 y entered or.**
94ff0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 is not currently
95000 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c allocated. SQL
95010 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 ite will never d
95020 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 o either..*/.sta
95030 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 tic void os2Mute
95040 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d xLeave(sqlite3_m
95050 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 utex *p){. TID
95060 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 tid;. PID holde
95070 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 r1;. ULONG hold
95080 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 er2;. if( p==0
95090 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 ) return;. asse
950a0 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b rt( p->nRef>0 );
950b0 0a 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 . DosQueryMutex
950c0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 Sem(p->mutex, &h
950d0 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 older1, &tid, &h
950e0 6f 6c 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 older2);. asser
950f0 74 28 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 t( p->owner==tid
95100 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b );. p->nRef--;
95110 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
95120 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d ef==0 || p->id==
95130 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
95140 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 URSIVE );. DosR
95150 65 6c 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 eleaseMutexSem(p
95160 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 ->mutex);.}..#if
95170 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
95180 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
95190 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 e3_mutex_held()
951a0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 and sqlite3_mute
951b0 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 x_notheld() rout
951c0 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e ine are.** inten
951d0 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 ded for use insi
951e0 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 de assert() stat
951f0 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 ements..*/.stati
95200 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 c int os2MutexHe
95210 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
95220 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b *p){. TID tid;
95230 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c . PID pid;. UL
95240 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 ONG ulCount;. P
95250 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 TIB ptib;. if(
95260 70 21 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 p!=0 ) {. Dos
95270 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d QueryMutexSem(p-
95280 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 >mutex, &pid, &t
95290 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 id, &ulCount);.
952a0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f } else {. Do
952b0 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 sGetInfoBlocks(&
952c0 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 ptib, NULL);.
952d0 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 tid = ptib->tib
952e0 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 _ptib2->tib2_ult
952f0 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e id;. }. return
95300 20 70 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 p==0 || (p->nRe
95310 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 f!=0 && p->owner
95320 3d 3d 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 ==tid);.}.static
95330 20 69 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 int os2MutexNot
95340 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 held(sqlite3_mut
95350 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 ex *p){. TID ti
95360 64 3b 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 d;. PID pid;.
95370 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 ULONG ulCount;.
95380 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 PTIB ptib;. if
95390 28 20 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 ( p!= 0 ) {.
953a0 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d DosQueryMutexSem
953b0 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c (p->mutex, &pid,
953c0 20 26 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 &tid, &ulCount)
953d0 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
953e0 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b DosGetInfoBlock
953f0 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a s(&ptib, NULL);.
95400 20 20 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e tid = ptib->
95410 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f tib_ptib2->tib2_
95420 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 ultid;. }. ret
95430 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e urn p==0 || p->n
95440 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e Ref==0 || p->own
95450 65 72 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 er!=tid;.}.#endi
95460 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 f..SQLITE_PRIVAT
95470 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f E sqlite3_mutex_
95480 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 methods *sqlite3
95490 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 DefaultMutex(voi
954a0 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c d){. static sql
954b0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
954c0 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 ds sMutex = {.
954d0 20 20 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a os2MutexInit,.
954e0 20 20 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c os2MutexEnd,
954f0 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c . os2MutexAll
95500 6f 63 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 oc,. os2Mutex
95510 46 72 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 Free,. os2Mut
95520 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 exEnter,. os2
95530 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 MutexTry,. os
95540 32 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 2MutexLeave,.#if
95550 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
95560 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c . os2MutexHel
95570 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e d,. os2MutexN
95580 6f 74 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 otheld.#endif.
95590 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d };.. return &sM
955a0 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f utex;.}.#endif /
955b0 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f * SQLITE_MUTEX_O
955c0 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a S2 */../********
955d0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 ****** End of mu
955e0 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a tex_os2.c ******
955f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95610 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
95620 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
95630 65 20 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a e mutex_unix.c *
95640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
95660 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
95670 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 7 August 28.**.*
95680 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
95690 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
956a0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
956b0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
956c0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
956d0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
956e0 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
956f0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
95700 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
95710 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
95720 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
95730 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
95740 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
95750 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
95760 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
95770 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
95780 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
95790 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
957a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
957b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
957c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
957d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 **********.** Th
957e0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 is file contains
957f0 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 the C functions
95800 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 that implement
95810 6d 75 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 mutexes for pthr
95820 65 61 64 73 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 eads.*/../*.** T
95830 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 he code in this
95840 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 file is only use
95850 64 20 69 66 20 77 65 20 61 72 65 20 63 6f 6d 70 d if we are comp
95860 69 6c 69 6e 67 20 74 68 72 65 61 64 73 61 66 65 iling threadsafe
95870 0a 2a 2a 20 75 6e 64 65 72 20 75 6e 69 78 20 77 .** under unix w
95880 69 74 68 20 70 74 68 72 65 61 64 73 2e 0a 2a 2a ith pthreads..**
95890 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 .** Note that th
958a0 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
958b0 6e 20 72 65 71 75 69 72 65 73 20 61 20 76 65 72 n requires a ver
958c0 73 69 6f 6e 20 6f 66 20 70 74 68 72 65 61 64 73 sion of pthreads
958d0 20 74 68 61 74 0a 2a 2a 20 73 75 70 70 6f 72 74 that.** support
958e0 73 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 s recursive mute
958f0 78 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 xes..*/.#ifdef S
95900 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 QLITE_MUTEX_PTHR
95910 45 41 44 53 0a 0a 23 69 6e 63 6c 75 64 65 20 3c EADS..#include <
95920 70 74 68 72 65 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a pthread.h>.../*.
95930 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69 76 ** Each recursiv
95940 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e e mutex is an in
95950 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
95960 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
95970 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c e..*/.struct sql
95980 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 70 ite3_mutex {. p
95990 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 20 6d thread_mutex_t m
959a0 75 74 65 78 3b 20 20 20 20 20 2f 2a 20 4d 75 74 utex; /* Mut
959b0 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 ex controlling t
959c0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 he lock */. int
959d0 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 id;
959e0 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 /* Mutex
959f0 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e type */. int n
95a00 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
95a10 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
95a20 6f 66 20 65 6e 74 72 61 6e 63 65 73 20 2a 2f 0a of entrances */.
95a30 20 20 70 74 68 72 65 61 64 5f 74 20 6f 77 6e 65 pthread_t owne
95a40 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 r; /*
95a50 54 68 72 65 61 64 20 74 68 61 74 20 69 73 20 77 Thread that is w
95a60 69 74 68 69 6e 20 74 68 69 73 20 6d 75 74 65 78 ithin this mutex
95a70 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
95a80 45 5f 44 45 42 55 47 0a 20 20 69 6e 74 20 74 72 E_DEBUG. int tr
95a90 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ace;
95aa0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 /* True to
95ab0 74 72 61 63 65 20 63 68 61 6e 67 65 73 20 2a 2f trace changes */
95ac0 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23 69 66 64 65 .#endif.};.#ifde
95ad0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 f SQLITE_DEBUG.#
95ae0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d define SQLITE3_M
95af0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 UTEX_INITIALIZER
95b00 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 { PTHREAD_MUTEX
95b10 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c _INITIALIZER, 0,
95b20 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29 30 0, (pthread_t)0
95b30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 , 0 }.#else.#def
95b40 69 6e 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 ine SQLITE3_MUTE
95b50 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 X_INITIALIZER {
95b60 50 54 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e PTHREAD_MUTEX_IN
95b70 49 54 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c ITIALIZER, 0, 0,
95b80 20 28 70 74 68 72 65 61 64 5f 74 29 30 20 7d 0a (pthread_t)0 }.
95b90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
95ba0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f e sqlite3_mutex_
95bb0 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 held() and sqlit
95bc0 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 e3_mutex_notheld
95bd0 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a () routine are.*
95be0 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 * intended for u
95bf0 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 se only inside a
95c00 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e ssert() statemen
95c10 74 73 2e 20 20 4f 6e 20 73 6f 6d 65 20 70 6c 61 ts. On some pla
95c20 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74 68 65 72 65 tforms,.** there
95c30 20 6d 69 67 68 74 20 62 65 20 72 61 63 65 20 63 might be race c
95c40 6f 6e 64 69 74 69 6f 6e 73 20 74 68 61 74 20 63 onditions that c
95c50 61 6e 20 63 61 75 73 65 20 74 68 65 73 65 20 72 an cause these r
95c60 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 64 65 outines to.** de
95c70 6c 69 76 65 72 20 69 6e 63 6f 72 72 65 63 74 20 liver incorrect
95c80 72 65 73 75 6c 74 73 2e 20 20 49 6e 20 70 61 72 results. In par
95c90 74 69 63 75 6c 61 72 2c 20 69 66 20 70 74 68 72 ticular, if pthr
95ca0 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 0a 2a ead_equal() is.*
95cb0 2a 20 6e 6f 74 20 61 6e 20 61 74 6f 6d 69 63 20 * not an atomic
95cc0 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 operation, then
95cd0 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d these routines m
95ce0 69 67 68 74 20 64 65 6c 69 76 65 72 79 0a 2a 2a ight delivery.**
95cf0 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c incorrect resul
95d00 74 73 2e 20 20 4f 6e 20 6d 6f 73 74 20 70 6c 61 ts. On most pla
95d10 74 66 6f 72 6d 73 2c 20 70 74 68 72 65 61 64 5f tforms, pthread_
95d20 65 71 75 61 6c 28 29 20 69 73 20 61 20 0a 2a 2a equal() is a .**
95d30 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 comparison of t
95d40 77 6f 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 wo integers and
95d50 69 73 20 74 68 65 72 65 66 6f 72 65 20 61 74 6f is therefore ato
95d60 6d 69 63 2e 20 20 42 75 74 20 77 65 20 61 72 65 mic. But we are
95d70 0a 2a 2a 20 74 6f 6c 64 20 74 68 61 74 20 48 50 .** told that HP
95d80 55 58 20 69 73 20 6e 6f 74 20 73 75 63 68 20 61 UX is not such a
95d90 20 70 6c 61 74 66 6f 72 6d 2e 20 20 49 66 20 73 platform. If s
95da0 6f 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f o, then these ro
95db0 75 74 69 6e 65 73 0a 2a 2a 20 77 69 6c 6c 20 6e utines.** will n
95dc0 6f 74 20 61 6c 77 61 79 73 20 77 6f 72 6b 20 63 ot always work c
95dd0 6f 72 72 65 63 74 6c 79 20 6f 6e 20 48 50 55 58 orrectly on HPUX
95de0 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 68 6f 73 65 ..**.** On those
95df0 20 70 6c 61 74 66 6f 72 6d 73 20 77 68 65 72 65 platforms where
95e00 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 pthread_equal()
95e10 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2c 20 is not atomic,
95e20 53 51 4c 69 74 65 0a 2a 2a 20 73 68 6f 75 6c 64 SQLite.** should
95e30 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 be compiled wit
95e40 68 6f 75 74 20 2d 44 53 51 4c 49 54 45 5f 44 45 hout -DSQLITE_DE
95e50 42 55 47 20 61 6e 64 20 77 69 74 68 20 2d 44 4e BUG and with -DN
95e60 44 45 42 55 47 20 74 6f 0a 2a 2a 20 6d 61 6b 65 DEBUG to.** make
95e70 20 73 75 72 65 20 6e 6f 20 61 73 73 65 72 74 28 sure no assert(
95e80 29 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 ) statements are
95e90 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 68 evaluated and h
95ea0 65 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20 72 6f ence these.** ro
95eb0 75 74 69 6e 65 73 20 61 72 65 20 6e 65 76 65 72 utines are never
95ec0 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 20 called..*/.#if
95ed0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 !defined(NDEBUG)
95ee0 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 || defined(SQLI
95ef0 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 TE_DEBUG).static
95f00 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 int pthreadMute
95f10 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 xHeld(sqlite3_mu
95f20 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 tex *p){. retur
95f30 6e 20 28 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 n (p->nRef!=0 &&
95f40 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 pthread_equal(p
95f50 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64 ->owner, pthread
95f60 5f 73 65 6c 66 28 29 29 29 3b 0a 7d 0a 73 74 61 _self()));.}.sta
95f70 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d tic int pthreadM
95f80 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 utexNotheld(sqli
95f90 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
95fa0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d return p->nRef=
95fb0 3d 30 20 7c 7c 20 70 74 68 72 65 61 64 5f 65 71 =0 || pthread_eq
95fc0 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 ual(p->owner, pt
95fd0 68 72 65 61 64 5f 73 65 6c 66 28 29 29 3d 3d 30 hread_self())==0
95fe0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
95ff0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 * Initialize and
96000 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 deinitialize th
96010 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 e mutex subsyste
96020 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 m..*/.static int
96030 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69 pthreadMutexIni
96040 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 t(void){ return
96050 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 SQLITE_OK; }.sta
96060 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d tic int pthreadM
96070 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 utexEnd(void){ r
96080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
96090 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 }../*.** The sq
960a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
960b0 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f c() routine allo
960c0 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d cates a new.** m
960d0 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 utex and returns
960e0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 a pointer to it
960f0 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 . If it returns
96100 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 NULL.** that me
96110 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 ans that a mutex
96120 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c could not be al
96130 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 located. SQLite
96140 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 .** will unwind
96150 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65 its stack and re
96160 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 turn an error.
96170 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 The argument.**
96180 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 to sqlite3_mutex
96190 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 _alloc() is one
961a0 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 of these integer
961b0 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a constants:.**.*
961c0 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 * <ul>.** <li>
961d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
961e0 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 T.** <li> SQLIT
961f0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 E_MUTEX_RECURSIV
96200 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 E.** <li> SQLIT
96210 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d E_MUTEX_STATIC_M
96220 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 ASTER.** <li> S
96230 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
96240 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 IC_MEM.** <li>
96250 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
96260 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e TIC_MEM2.** <li>
96270 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
96280 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c TATIC_PRNG.** <l
96290 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 i> SQLITE_MUTEX
962a0 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c _STATIC_LRU.** <
962b0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 li> SQLITE_MUTE
962c0 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a X_STATIC_LRU2.**
962d0 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 </ul>.**.** The
962e0 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 first two const
962f0 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74 ants cause sqlit
96300 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
96310 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 to create.** a
96320 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20 new mutex. The
96330 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63 new mutex is rec
96340 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49 ursive when SQLI
96350 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 TE_MUTEX_RECURSI
96360 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75 VE.** is used bu
96370 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c t not necessaril
96380 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 y so when SQLITE
96390 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75 _MUTEX_FAST is u
963a0 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 sed..** The mute
963b0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e x implementation
963c0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 does not need t
963d0 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 o make a distinc
963e0 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 tion.** between
963f0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
96400 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 URSIVE and SQLIT
96410 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 E_MUTEX_FAST if
96420 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 it does.** not w
96430 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c ant to. But SQL
96440 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 ite will only re
96450 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76 quest a recursiv
96460 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 e mutex in.** ca
96470 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61 ses where it rea
96480 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 lly needs one.
96490 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d If a faster non-
964a0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a recursive mutex.
964b0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
964c0 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f n is available o
964d0 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66 n the host platf
964e0 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73 orm, the mutex s
964f0 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 ubsystem.** migh
96500 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20 t return such a
96510 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 mutex in respons
96520 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 e to SQLITE_MUTE
96530 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 X_FAST..**.** Th
96540 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 e other allowed
96550 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71 parameters to sq
96560 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f lite3_mutex_allo
96570 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a c() each return.
96580 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ** a pointer to
96590 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73 a static preexis
965a0 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69 78 ting mutex. Six
965b0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 static mutexes
965c0 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 are.** used by t
965d0 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69 he current versi
965e0 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 on of SQLite. F
965f0 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f uture versions o
96600 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 f SQLite.** may
96610 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 add additional s
96620 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20 tatic mutexes.
96630 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 Static mutexes a
96640 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a re for internal.
96650 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 ** use by SQLite
96660 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 only. Applicat
96670 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51 ions that use SQ
96680 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f Lite mutexes sho
96690 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 uld.** use only
966a0 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 the dynamic mute
966b0 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 xes returned by
966c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 SQLITE_MUTEX_FAS
966d0 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d T or.** SQLITE_M
966e0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a UTEX_RECURSIVE..
966f0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 **.** Note that
96700 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 if one of the dy
96710 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 namic mutex para
96720 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d meters (SQLITE_M
96730 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 UTEX_FAST.** or
96740 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 SQLITE_MUTEX_REC
96750 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20 URSIVE) is used
96760 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 then sqlite3_mut
96770 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 ex_alloc().** re
96780 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e turns a differen
96790 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 t mutex on every
967a0 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 call. But for
967b0 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d the static .** m
967c0 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65 20 utex types, the
967d0 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72 65 same mutex is re
967e0 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 turned on every
967f0 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a call that has.**
96800 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 6e the same type n
96810 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 umber..*/.static
96820 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
96830 70 74 68 72 65 61 64 4d 75 74 65 78 41 6c 6c 6f pthreadMutexAllo
96840 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 c(int iType){.
96850 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d static sqlite3_m
96860 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65 78 utex staticMutex
96870 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 53 51 4c es[] = {. SQL
96880 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 ITE3_MUTEX_INITI
96890 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 ALIZER,. SQLI
968a0 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 TE3_MUTEX_INITIA
968b0 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 LIZER,. SQLIT
968c0 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c E3_MUTEX_INITIAL
968d0 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 IZER,. SQLITE
968e0 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 3_MUTEX_INITIALI
968f0 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 ZER,. SQLITE3
96900 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a _MUTEX_INITIALIZ
96910 45 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f ER,. SQLITE3_
96920 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 MUTEX_INITIALIZE
96930 52 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 R. };. sqlite3
96940 5f 6d 75 74 65 78 20 2a 70 3b 0a 20 20 73 77 69 _mutex *p;. swi
96950 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20 tch( iType ){.
96960 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 case SQLITE_MU
96970 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20 7b TEX_RECURSIVE: {
96980 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 . p = sqlit
96990 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 e3MallocZero( si
969a0 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 zeof(*p) );.
969b0 20 20 69 66 28 20 70 20 29 7b 0a 23 69 66 64 65 if( p ){.#ifde
969c0 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f f SQLITE_HOMEGRO
969d0 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 WN_RECURSIVE_MUT
969e0 45 58 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 EX. /* If
969f0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
96a00 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c es are not avail
96a10 61 62 6c 65 2c 20 77 65 20 77 69 6c 6c 20 68 61 able, we will ha
96a20 76 65 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a ve to. **
96a30 20 62 75 69 6c 64 20 6f 75 72 20 6f 77 6e 2e 20 build our own.
96a40 20 53 65 65 20 62 65 6c 6f 77 2e 20 2a 2f 0a 20 See below. */.
96a50 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96a60 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 utex_init(&p->mu
96a70 74 65 78 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 tex, 0);.#else.
96a80 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 /* Use a
96a90 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 recursive mutex
96aa0 69 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 if it is availab
96ab0 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 le */. pt
96ac0 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f hread_mutexattr_
96ad0 74 20 72 65 63 75 72 73 69 76 65 41 74 74 72 3b t recursiveAttr;
96ae0 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96af0 5f 6d 75 74 65 78 61 74 74 72 5f 69 6e 69 74 28 _mutexattr_init(
96b00 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29 3b &recursiveAttr);
96b10 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64 . pthread
96b20 5f 6d 75 74 65 78 61 74 74 72 5f 73 65 74 74 79 _mutexattr_setty
96b30 70 65 28 26 72 65 63 75 72 73 69 76 65 41 74 74 pe(&recursiveAtt
96b40 72 2c 20 50 54 48 52 45 41 44 5f 4d 55 54 45 58 r, PTHREAD_MUTEX
96b50 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20 20 20 _RECURSIVE);.
96b60 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 pthread_mut
96b70 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 ex_init(&p->mute
96b80 78 2c 20 26 72 65 63 75 72 73 69 76 65 41 74 74 x, &recursiveAtt
96b90 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 r);. pthr
96ba0 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 64 65 ead_mutexattr_de
96bb0 73 74 72 6f 79 28 26 72 65 63 75 72 73 69 76 65 stroy(&recursive
96bc0 41 74 74 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 Attr);.#endif.
96bd0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 p->id = iT
96be0 79 70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ype;. }.
96bf0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
96c00 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
96c10 4d 55 54 45 58 5f 46 41 53 54 3a 20 7b 0a 20 20 MUTEX_FAST: {.
96c20 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
96c30 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
96c40 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
96c50 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 f( p ){.
96c60 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 p->id = iType;.
96c70 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d pthread_m
96c80 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e 6d 75 utex_init(&p->mu
96c90 74 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d tex, 0);. }
96ca0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
96cb0 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
96cc0 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
96cd0 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b iType-2 >= 0 );
96ce0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 . assert( i
96cf0 54 79 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69 Type-2 < ArraySi
96d00 7a 65 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 ze(staticMutexes
96d10 29 20 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 ) );. p = &
96d20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 staticMutexes[iT
96d30 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d ype-2];. p-
96d40 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 >id = iType;.
96d50 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
96d60 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
96d70 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 }.../*.** This r
96d80 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 outine deallocat
96d90 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a es a previously.
96da0 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 ** allocated mut
96db0 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63 ex. SQLite is c
96dc0 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f areful to deallo
96dd0 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75 cate every.** mu
96de0 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f tex that it allo
96df0 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 cates..*/.static
96e00 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75 74 void pthreadMut
96e10 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d exFree(sqlite3_m
96e20 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 utex *p){. asse
96e30 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 rt( p->nRef==0 )
96e40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 ;. assert( p->i
96e50 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f d==SQLITE_MUTEX_
96e60 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 FAST || p->id==S
96e70 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
96e80 52 53 49 56 45 20 29 3b 0a 20 20 70 74 68 72 65 RSIVE );. pthre
96e90 61 64 5f 6d 75 74 65 78 5f 64 65 73 74 72 6f 79 ad_mutex_destroy
96ea0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 (&p->mutex);. s
96eb0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
96ec0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
96ed0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
96ee0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d () and sqlite3_m
96ef0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 utex_try() routi
96f00 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 nes attempt.** t
96f10 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e o enter a mutex.
96f20 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 If another thr
96f30 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77 ead is already w
96f40 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c ithin the mutex,
96f50 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .** sqlite3_mute
96f60 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 x_enter() will b
96f70 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 lock and sqlite3
96f80 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c _mutex_try() wil
96f90 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 l return.** SQLI
96fa0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71 TE_BUSY. The sq
96fb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
96fc0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 ) interface retu
96fd0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a rns SQLITE_OK.**
96fe0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c upon successful
96ff0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73 entry. Mutexes
97000 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53 created using S
97010 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 QLITE_MUTEX_RECU
97020 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 RSIVE can.** be
97030 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 entered multiple
97040 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61 times by the sa
97050 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73 me thread. In s
97060 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a uch cases the,.*
97070 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 * mutex must be
97080 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20 exited an equal
97090 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 number of times
970a0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 before another t
970b0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 hread.** can ent
970c0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65 er. If the same
970d0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f thread tries to
970e0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 enter any other
970f0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a kind of mutex.*
97100 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 * more than once
97110 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 , the behavior i
97120 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a s undefined..*/.
97130 73 74 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 static void pthr
97140 65 61 64 4d 75 74 65 78 45 6e 74 65 72 28 73 71 eadMutexEnter(sq
97150 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b lite3_mutex *p){
97160 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 . assert( p->id
97170 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 ==SQLITE_MUTEX_R
97180 45 43 55 52 53 49 56 45 20 7c 7c 20 70 74 68 72 ECURSIVE || pthr
97190 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 eadMutexNotheld(
971a0 70 29 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 p) );..#ifdef SQ
971b0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 LITE_HOMEGROWN_R
971c0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 ECURSIVE_MUTEX.
971d0 20 2f 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 /* If recursive
971e0 20 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 mutexes are not
971f0 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e available, then
97200 20 77 65 20 68 61 76 65 20 74 6f 20 67 72 6f 77 we have to grow
97210 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 . ** our own.
97220 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 This implementat
97230 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 ion assumes that
97240 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 pthread_equal()
97250 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d 69 63 20 . ** is atomic
97260 2d 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 - that it cannot
97270 20 62 65 20 64 65 63 65 69 76 65 64 20 69 6e 74 be deceived int
97280 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65 6c 66 0a o thinking self.
97290 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 ** and p->owne
972a0 72 20 61 72 65 20 65 71 75 61 6c 20 69 66 20 70 r are equal if p
972b0 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67 65 73 20 ->owner changes
972c0 62 65 74 77 65 65 6e 20 74 77 6f 20 76 61 6c 75 between two valu
972d0 65 73 0a 20 20 2a 2a 20 74 68 61 74 20 61 72 65 es. ** that are
972e0 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 73 65 not equal to se
972f0 6c 66 20 77 68 69 6c 65 20 74 68 65 20 63 6f 6d lf while the com
97300 70 61 72 69 73 6f 6e 20 69 73 20 74 61 6b 69 6e parison is takin
97310 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a 20 54 68 g place.. ** Th
97320 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f is implementatio
97330 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 61 n also assumes a
97340 20 63 6f 68 65 72 65 6e 74 20 63 61 63 68 65 20 coherent cache
97350 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20 73 65 70 - that . ** sep
97360 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20 arate processes
97370 63 61 6e 6e 6f 74 20 72 65 61 64 20 64 69 66 66 cannot read diff
97380 65 72 65 6e 74 20 76 61 6c 75 65 73 20 66 72 6f erent values fro
97390 6d 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20 m the same. **
973a0 61 64 64 72 65 73 73 20 61 74 20 74 68 65 20 73 address at the s
973b0 61 6d 65 20 74 69 6d 65 2e 20 20 49 66 20 65 69 ame time. If ei
973c0 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 ther of these tw
973d0 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a 20 20 2a o conditions. *
973e0 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74 2c 20 74 * are not met, t
973f0 68 65 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 hen the mutexes
97400 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64 20 70 72 will fail and pr
97410 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72 65 73 75 oblems will resu
97420 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 lt.. */. {.
97430 20 70 74 68 72 65 61 64 5f 74 20 73 65 6c 66 20 pthread_t self
97440 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 = pthread_self()
97450 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 ;. if( p->nRe
97460 66 3e 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 f>0 && pthread_e
97470 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 qual(p->owner, s
97480 65 6c 66 29 20 29 7b 0a 20 20 20 20 20 20 70 2d elf) ){. p-
97490 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 65 6c >nRef++;. }el
974a0 73 65 7b 0a 20 20 20 20 20 20 70 74 68 72 65 61 se{. pthrea
974b0 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d d_mutex_lock(&p-
974c0 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61 >mutex);. a
974d0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d ssert( p->nRef==
974e0 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 0 );. p->ow
974f0 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 ner = self;.
97500 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 p->nRef = 1;.
97510 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 }. }.#else.
97520 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c /* Use the buil
97530 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d t-in recursive m
97540 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61 utexes if they a
97550 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 re available..
97560 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 */. pthread_mut
97570 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 ex_lock(&p->mute
97580 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d x);. p->owner =
97590 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b pthread_self();
975a0 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 65 . p->nRef++;.#e
975b0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c ndif..#ifdef SQL
975c0 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 ITE_DEBUG. if(
975d0 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 p->trace ){.
975e0 70 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 printf("enter mu
975f0 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 tex %p (%d) with
97600 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 nRef=%d\n", p,
97610 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 p->trace, p->nRe
97620 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d f);. }.#endif.}
97630 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72 .static int pthr
97640 65 61 64 4d 75 74 65 78 54 72 79 28 73 71 6c 69 eadMutexTry(sqli
97650 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
97660 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
97670 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
97680 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 _MUTEX_RECURSIVE
97690 20 7c 7c 20 70 74 68 72 65 61 64 4d 75 74 65 78 || pthreadMutex
976a0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 0a 23 Notheld(p) );..#
976b0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d ifdef SQLITE_HOM
976c0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 EGROWN_RECURSIVE
976d0 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49 66 20 72 _MUTEX. /* If r
976e0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 ecursive mutexes
976f0 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 are not availab
97700 6c 65 2c 20 74 68 65 6e 20 77 65 20 68 61 76 65 le, then we have
97710 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 to grow. ** ou
97720 72 20 6f 77 6e 2e 20 20 54 68 69 73 20 69 6d 70 r own. This imp
97730 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 lementation assu
97740 6d 65 73 20 74 68 61 74 20 70 74 68 72 65 61 64 mes that pthread
97750 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a 20 69 73 _equal(). ** is
97760 20 61 74 6f 6d 69 63 20 2d 20 74 68 61 74 20 69 atomic - that i
97770 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 65 63 65 t cannot be dece
97780 69 76 65 64 20 69 6e 74 6f 20 74 68 69 6e 6b 69 ived into thinki
97790 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 ng self. ** and
977a0 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65 20 65 71 p->owner are eq
977b0 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e 65 72 20 ual if p->owner
977c0 63 68 61 6e 67 65 73 20 62 65 74 77 65 65 6e 20 changes between
977d0 74 77 6f 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 two values. **
977e0 74 68 61 74 20 61 72 65 20 6e 6f 74 20 65 71 75 that are not equ
977f0 61 6c 20 74 6f 20 73 65 6c 66 20 77 68 69 6c 65 al to self while
97800 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
97810 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65 2e is taking place.
97820 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 . ** This imple
97830 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73 6f 20 61 mentation also a
97840 73 73 75 6d 65 73 20 61 20 63 6f 68 65 72 65 6e ssumes a coheren
97850 74 20 63 61 63 68 65 20 2d 20 74 68 61 74 20 0a t cache - that .
97860 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 70 72 ** separate pr
97870 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f 74 20 72 ocesses cannot r
97880 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 76 61 ead different va
97890 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 lues from the sa
978a0 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65 73 73 20 me. ** address
978b0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 at the same time
978c0 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 . If either of
978d0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 64 69 74 these two condit
978e0 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65 20 6e 6f ions. ** are no
978f0 74 20 6d 65 74 2c 20 74 68 65 6e 20 74 68 65 20 t met, then the
97900 6d 75 74 65 78 65 73 20 77 69 6c 6c 20 66 61 69 mutexes will fai
97910 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d 73 20 77 l and problems w
97920 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20 20 2a 2f ill result.. */
97930 0a 20 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 . {. pthread
97940 5f 74 20 73 65 6c 66 20 3d 20 70 74 68 72 65 61 _t self = pthrea
97950 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 69 66 d_self();. if
97960 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26 26 20 70 ( p->nRef>0 && p
97970 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e thread_equal(p->
97980 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20 29 7b 0a owner, self) ){.
97990 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b p->nRef++;
979a0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
979b0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 TE_OK;. }else
979c0 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 if( pthread_mut
979d0 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d ex_trylock(&p->m
979e0 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 utex)==0 ){.
979f0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 assert( p->nRe
97a00 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d f==0 );. p-
97a10 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a 20 >owner = self;.
97a20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20 31 p->nRef = 1
97a30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c ;. rc = SQL
97a40 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 ITE_OK;. }els
97a50 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 e{. rc = SQ
97a60 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
97a70 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 . }.#else. /*
97a80 55 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e Use the built-in
97a90 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 recursive mutex
97aa0 65 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 es if they are a
97ab0 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 vailable.. */.
97ac0 20 69 66 28 20 70 74 68 72 65 61 64 5f 6d 75 74 if( pthread_mut
97ad0 65 78 5f 74 72 79 6c 6f 63 6b 28 26 70 2d 3e 6d ex_trylock(&p->m
97ae0 75 74 65 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 utex)==0 ){.
97af0 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72 65 p->owner = pthre
97b00 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20 20 70 ad_self();. p
97b10 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 ->nRef++;. rc
97b20 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
97b30 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
97b40 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d SQLITE_BUSY;. }
97b50 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
97b60 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 SQLITE_DEBUG. i
97b70 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
97b80 20 26 26 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a && p->trace ){.
97b90 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65 printf("ente
97ba0 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20 r mutex %p (%d)
97bb0 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c with nRef=%d\n",
97bc0 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d p, p->trace, p-
97bd0 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 >nRef);. }.#end
97be0 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a if. return rc;.
97bf0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c }../*.** The sql
97c00 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
97c10 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73 () routine exits
97c20 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61 a mutex that wa
97c30 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 s.** previously
97c40 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73 entered by the s
97c50 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 ame thread. The
97c60 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20 behavior.** is
97c70 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 undefined if the
97c80 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75 mutex is not cu
97c90 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 rrently entered
97ca0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 or.** is not cur
97cb0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 rently allocated
97cc0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e . SQLite will n
97cd0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a ever do either..
97ce0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
97cf0 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 76 65 threadMutexLeave
97d00 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
97d10 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 74 p){. assert( pt
97d20 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64 28 70 hreadMutexHeld(p
97d30 29 20 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d ) );. p->nRef--
97d40 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e ;. assert( p->n
97d50 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d Ref==0 || p->id=
97d60 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 =SQLITE_MUTEX_RE
97d70 43 55 52 53 49 56 45 20 29 3b 0a 0a 23 69 66 64 CURSIVE );..#ifd
97d80 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 ef SQLITE_HOMEGR
97d90 4f 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 OWN_RECURSIVE_MU
97da0 54 45 58 0a 20 20 69 66 28 20 70 2d 3e 6e 52 65 TEX. if( p->nRe
97db0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68 72 f==0 ){. pthr
97dc0 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b ead_mutex_unlock
97dd0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d (&p->mutex);. }
97de0 0a 23 65 6c 73 65 0a 20 20 70 74 68 72 65 61 64 .#else. pthread
97df0 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 _mutex_unlock(&p
97e00 2d 3e 6d 75 74 65 78 29 3b 0a 23 65 6e 64 69 66 ->mutex);.#endif
97e10 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
97e20 44 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 DEBUG. if( p->t
97e30 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e race ){. prin
97e40 74 66 28 22 6c 65 61 76 65 20 6d 75 74 65 78 20 tf("leave mutex
97e50 25 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 %p (%d) with nRe
97e60 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 f=%d\n", p, p->t
97e70 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a race, p->nRef);.
97e80 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 53 51 }.#endif.}..SQ
97e90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c LITE_PRIVATE sql
97ea0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f ite3_mutex_metho
97eb0 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 ds *sqlite3Defau
97ec0 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 ltMutex(void){.
97ed0 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f static sqlite3_
97ee0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d mutex_methods sM
97ef0 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 70 74 68 utex = {. pth
97f00 72 65 61 64 4d 75 74 65 78 49 6e 69 74 2c 0a 20 readMutexInit,.
97f10 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 45 pthreadMutexE
97f20 6e 64 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d nd,. pthreadM
97f30 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 70 utexAlloc,. p
97f40 74 68 72 65 61 64 4d 75 74 65 78 46 72 65 65 2c threadMutexFree,
97f50 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 . pthreadMute
97f60 78 45 6e 74 65 72 2c 0a 20 20 20 20 70 74 68 72 xEnter,. pthr
97f70 65 61 64 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 eadMutexTry,.
97f80 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c 65 61 pthreadMutexLea
97f90 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 ve,.#ifdef SQLIT
97fa0 45 5f 44 45 42 55 47 0a 20 20 20 20 70 74 68 72 E_DEBUG. pthr
97fb0 65 61 64 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 eadMutexHeld,.
97fc0 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f pthreadMutexNo
97fd0 74 68 65 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 theld.#else.
97fe0 30 2c 0a 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0,. 0.#endif.
97ff0 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 };.. return &
98000 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23 65 6e 64 69 sMutex;.}..#endi
98010 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 f /* SQLITE_MUTE
98020 58 5f 50 54 48 52 45 41 44 20 2a 2f 0a 0a 2f 2a X_PTHREAD */../*
98030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
98040 64 20 6f 66 20 6d 75 74 65 78 5f 75 6e 69 78 2e d of mutex_unix.
98050 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
98060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
98080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
98090 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 77 gin file mutex_w
980a0 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 32.c ***********
980b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
980c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
980d0 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 .** 2007 August
980e0 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 14.**.** The aut
980f0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
98100 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
98110 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
98120 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
98130 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
98140 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
98150 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
98160 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
98170 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
98180 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
98190 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
981a0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
981b0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
981c0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
981d0 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
981e0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
981f0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
98200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
98240 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
98250 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 ontains the C fu
98260 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 nctions that imp
98270 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 lement mutexes f
98280 6f 72 20 77 69 6e 33 32 0a 2a 2f 0a 0a 2f 2a 0a or win32.*/../*.
98290 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 ** The code in t
982a0 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 his file is only
982b0 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20 used if we are
982c0 63 6f 6d 70 69 6c 69 6e 67 20 6d 75 6c 74 69 74 compiling multit
982d0 68 72 65 61 64 65 64 0a 2a 2a 20 6f 6e 20 61 20 hreaded.** on a
982e0 77 69 6e 33 32 20 73 79 73 74 65 6d 2e 0a 2a 2f win32 system..*/
982f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d .#ifdef SQLITE_M
98300 55 54 45 58 5f 57 33 32 0a 0a 2f 2a 0a 2a 2a 20 UTEX_W32../*.**
98310 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d Each recursive m
98320 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 utex is an insta
98330 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
98340 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a wing structure..
98350 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 */.struct sqlite
98360 33 5f 6d 75 74 65 78 20 7b 0a 20 20 43 52 49 54 3_mutex {. CRIT
98370 49 43 41 4c 5f 53 45 43 54 49 4f 4e 20 6d 75 74 ICAL_SECTION mut
98380 65 78 3b 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 ex; /* Mutex
98390 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 controlling the
983a0 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 lock */. int id
983b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
983c0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 /* Mutex ty
983d0 70 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 pe */. int nRef
983e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
983f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
98400 65 6e 74 65 72 61 6e 63 65 73 20 2a 2f 0a 20 20 enterances */.
98410 44 57 4f 52 44 20 6f 77 6e 65 72 3b 20 20 20 20 DWORD owner;
98420 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
98430 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 read holding thi
98440 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f s mutex */.};../
98450 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 *.** Return true
98460 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 77 (non-zero) if w
98470 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 75 6e e are running un
98480 64 65 72 20 57 69 6e 4e 54 2c 20 57 69 6e 32 4b der WinNT, Win2K
98490 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 , WinXP,.** or W
984a0 69 6e 43 45 2e 20 20 52 65 74 75 72 6e 20 66 61 inCE. Return fa
984b0 6c 73 65 20 28 7a 65 72 6f 29 20 66 6f 72 20 57 lse (zero) for W
984c0 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 6f 72 20 in95, Win98, or
984d0 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 WinME..**.** Her
984e0 65 20 69 73 20 61 6e 20 69 6e 74 65 72 65 73 74 e is an interest
984f0 69 6e 67 20 6f 62 73 65 72 76 61 74 69 6f 6e 3a ing observation:
98500 20 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c 20 Win95, Win98,
98510 61 6e 64 20 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a and WinME lack.*
98520 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 * the LockFileEx
98530 28 29 20 41 50 49 2e 20 20 42 75 74 20 77 65 20 () API. But we
98540 63 61 6e 20 73 74 69 6c 6c 20 73 74 61 74 69 63 can still static
98550 61 6c 6c 79 20 6c 69 6e 6b 20 61 67 61 69 6e 73 ally link agains
98560 74 20 74 68 61 74 0a 2a 2a 20 41 50 49 20 61 73 t that.** API as
98570 20 6c 6f 6e 67 20 61 73 20 77 65 20 64 6f 6e 27 long as we don'
98580 74 20 63 61 6c 6c 20 69 74 20 77 69 6e 20 72 75 t call it win ru
98590 6e 6e 69 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d nning Win95/98/M
985a0 45 2e 20 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a E. A call to.**
985b0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
985c0 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 used to determi
985d0 6e 65 20 69 66 20 74 68 65 20 68 6f 73 74 20 69 ne if the host i
985e0 73 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 s Win95/98/ME or
985f0 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 .** WinNT/2K/XP
98600 73 6f 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 so that we will
98610 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f 72 20 know whether or
98620 6e 6f 74 20 77 65 20 63 61 6e 20 73 61 66 65 6c not we can safel
98630 79 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f y call.** the Lo
98640 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e 0a ckFileEx() API..
98650 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49 73 4e 54 28 **.** mutexIsNT(
98660 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 ) is only used f
98670 6f 72 20 74 68 65 20 54 72 79 45 6e 74 65 72 43 or the TryEnterC
98680 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 riticalSection()
98690 20 41 50 49 20 63 61 6c 6c 2c 0a 2a 2a 20 77 68 API call,.** wh
986a0 69 63 68 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 ich is only avai
986b0 6c 61 62 6c 65 20 69 66 20 79 6f 75 72 20 61 70 lable if your ap
986c0 70 6c 69 63 61 74 69 6f 6e 20 77 61 73 20 63 6f plication was co
986d0 6d 70 69 6c 65 64 20 77 69 74 68 20 0a 2a 2a 20 mpiled with .**
986e0 5f 57 49 4e 33 32 5f 57 49 4e 4e 54 20 64 65 66 _WIN32_WINNT def
986f0 69 6e 65 64 20 74 6f 20 61 20 76 61 6c 75 65 20 ined to a value
98700 3e 3d 20 30 78 30 34 30 30 2e 20 20 43 75 72 72 >= 0x0400. Curr
98710 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 0a ently, the only.
98720 2a 2a 20 63 61 6c 6c 20 74 6f 20 54 72 79 45 6e ** call to TryEn
98730 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69 terCriticalSecti
98740 6f 6e 28 29 20 69 73 20 23 69 66 64 65 66 27 65 on() is #ifdef'e
98750 64 20 6f 75 74 2c 20 73 6f 20 23 69 66 64 65 66 d out, so #ifdef
98760 20 0a 2a 2a 20 74 68 69 73 20 6f 75 74 20 61 73 .** this out as
98770 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69 66 20 30 0a well..*/.#if 0.
98780 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
98790 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 6d 75 74 NCE.# define mut
987a0 65 78 49 73 4e 54 28 29 20 20 28 31 29 0a 23 65 exIsNT() (1).#e
987b0 6c 73 65 0a 20 20 73 74 61 74 69 63 20 69 6e 74 lse. static int
987c0 20 6d 75 74 65 78 49 73 4e 54 28 76 6f 69 64 29 mutexIsNT(void)
987d0 7b 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 {. static int
987e0 20 6f 73 54 79 70 65 20 3d 20 30 3b 0a 20 20 20 osType = 0;.
987f0 20 69 66 28 20 6f 73 54 79 70 65 3d 3d 30 20 29 if( osType==0 )
98800 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53 49 4f {. OSVERSIO
98810 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 NINFO sInfo;.
98820 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56 65 72 sInfo.dwOSVer
98830 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d 20 73 sionInfoSize = s
98840 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a 20 20 izeof(sInfo);.
98850 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e 45 78 GetVersionEx
98860 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 (&sInfo);.
98870 6f 73 54 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 osType = sInfo.d
98880 77 50 6c 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 wPlatformId==VER
98890 5f 50 4c 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f _PLATFORM_WIN32_
988a0 4e 54 20 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 NT ? 2 : 1;.
988b0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 73 54 }. return osT
988c0 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 ype==2;. }.#end
988d0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
988e0 57 49 4e 43 45 20 2a 2f 0a 23 65 6e 64 69 66 0a WINCE */.#endif.
988f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
98900 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 EBUG./*.** The s
98910 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
98920 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f d() and sqlite3_
98930 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 mutex_notheld()
98940 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 routine are.** i
98950 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 ntended for use
98960 6f 6e 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 only inside asse
98970 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
98980 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
98990 69 6e 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 inMutexHeld(sqli
989a0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 te3_mutex *p){.
989b0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 21 return p->nRef!
989c0 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d =0 && p->owner==
989d0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 GetCurrentThread
989e0 49 64 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 Id();.}.static i
989f0 6e 74 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 nt winMutexNothe
98a00 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ld(sqlite3_mutex
98a10 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 *p){. return p
98a20 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d 3e ->nRef==0 || p->
98a30 6f 77 6e 65 72 21 3d 47 65 74 43 75 72 72 65 6e owner!=GetCurren
98a40 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 23 tThreadId();.}.#
98a50 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e endif.../*.** In
98a60 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
98a70 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 nitialize the mu
98a80 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a tex subsystem..*
98a90 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 /.static sqlite3
98aa0 5f 6d 75 74 65 78 20 77 69 6e 4d 75 74 65 78 5f _mutex winMutex_
98ab0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36 5d staticMutexes[6]
98ac0 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e ;.static int win
98ad0 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 Mutex_isInit = 0
98ae0 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d 75 74 65 78 ;./* As winMutex
98af0 49 6e 69 74 28 29 20 61 6e 64 20 77 69 6e 4d 75 Init() and winMu
98b00 74 65 78 45 6e 64 28 29 20 61 72 65 20 63 61 6c texEnd() are cal
98b10 6c 65 64 20 61 73 20 70 61 72 74 0a 2a 2a 20 6f led as part.** o
98b20 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e f the sqlite3_in
98b30 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 71 6c itialize and sql
98b40 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a ite3_shutdown().
98b50 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 2c 20 74 ** processing, t
98b60 68 65 20 22 69 6e 74 65 72 6c 6f 63 6b 65 64 22 he "interlocked"
98b70 20 6d 61 67 69 63 20 69 73 20 70 72 6f 62 61 62 magic is probab
98b80 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74 72 69 63 74 ly not.** strict
98b90 6c 79 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f ly necessary..*/
98ba0 0a 73 74 61 74 69 63 20 6c 6f 6e 67 20 77 69 6e .static long win
98bb0 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d 20 30 3b 0a Mutex_lock = 0;.
98bc0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d .static int winM
98bd0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 utexInit(void){
98be0 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 . /* The first
98bf0 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 to increment to
98c00 31 20 64 6f 65 73 20 61 63 74 75 61 6c 20 69 6e 1 does actual in
98c10 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a itialization */.
98c20 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 if( Interlocke
98c30 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e 67 65 dCompareExchange
98c40 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c (&winMutex_lock,
98c50 20 31 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 1, 0)==0 ){.
98c60 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 int i;. for(
98c70 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 i=0; i<ArraySize
98c80 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 (winMutex_static
98c90 4d 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a Mutexes); i++){.
98ca0 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65 Initialize
98cb0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 CriticalSection(
98cc0 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 &winMutex_static
98cd0 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 Mutexes[i].mutex
98ce0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 69 6e );. }. win
98cf0 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20 31 Mutex_isInit = 1
98d00 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
98d10 2a 20 53 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 69 * Someone else i
98d20 73 20 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73 s in the process
98d30 20 6f 66 20 69 6e 69 74 69 6e 67 20 74 68 65 20 of initing the
98d40 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 2a static mutexes *
98d50 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 21 77 69 /. while( !wi
98d60 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 29 7b nMutex_isInit ){
98d70 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b . Sleep(1);
98d80 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
98d90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a urn SQLITE_OK; .
98da0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 }..static int wi
98db0 6e 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b nMutexEnd(void){
98dc0 20 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 . /* The first
98dd0 20 74 6f 20 64 65 63 72 65 6d 65 6e 74 20 74 6f to decrement to
98de0 20 30 20 64 6f 65 73 20 61 63 74 75 61 6c 20 73 0 does actual s
98df0 68 75 74 64 6f 77 6e 20 0a 20 20 2a 2a 20 28 77 hutdown . ** (w
98e00 68 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 74 hich should be t
98e10 68 65 20 6c 61 73 74 20 74 6f 20 73 68 75 74 64 he last to shutd
98e20 6f 77 6e 2e 29 20 2a 2f 0a 20 20 69 66 28 20 49 own.) */. if( I
98e30 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72 nterlockedCompar
98e40 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75 eExchange(&winMu
98e50 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c 20 31 29 3d tex_lock, 0, 1)=
98e60 3d 31 20 29 7b 0a 20 20 20 20 69 66 28 20 77 69 =1 ){. if( wi
98e70 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 nMutex_isInit==1
98e80 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b ){. int i;
98e90 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
98ea0 69 3c 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d i<ArraySize(winM
98eb0 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 utex_staticMutex
98ec0 65 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 es); i++){.
98ed0 20 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 DeleteCritica
98ee0 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74 lSection(&winMut
98ef0 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73 ex_staticMutexes
98f00 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 [i].mutex);.
98f10 20 20 7d 0a 20 20 20 20 20 20 77 69 6e 4d 75 74 }. winMut
98f20 65 78 5f 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 ex_isInit = 0;.
98f30 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
98f40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
98f50 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 ./*.** The sqlit
98f60 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
98f70 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 routine allocat
98f80 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 es a new.** mute
98f90 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 x and returns a
98fa0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 pointer to it.
98fb0 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 If it returns NU
98fc0 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 LL.** that means
98fd0 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f that a mutex co
98fe0 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 uld not be alloc
98ff0 61 74 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a ated. SQLite.**
99000 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 will unwind its
99010 20 73 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 stack and retur
99020 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 n an error. The
99030 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 argument.** to
99040 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c sqlite3_mutex_al
99050 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 loc() is one of
99060 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f these integer co
99070 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c nstants:.**.** <
99080 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c ul>.** <li> SQL
99090 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a ITE_MUTEX_FAST.*
990a0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
990b0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a UTEX_RECURSIVE.*
990c0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d * <li> SQLITE_M
990d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 UTEX_STATIC_MAST
990e0 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 ER.** <li> SQLI
990f0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f TE_MUTEX_STATIC_
99100 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c MEM.** <li> SQL
99110 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
99120 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 _MEM2.** <li> S
99130 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 QLITE_MUTEX_STAT
99140 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 IC_PRNG.** <li>
99150 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 SQLITE_MUTEX_ST
99160 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e ATIC_LRU.** <li>
99170 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 SQLITE_MUTEX_S
99180 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f TATIC_LRU2.** </
99190 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ul>.**.** The fi
991a0 72 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 rst two constant
991b0 73 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f s cause sqlite3_
991c0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f mutex_alloc() to
991d0 20 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 create.** a new
991e0 20 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 mutex. The new
991f0 20 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 mutex is recurs
99200 69 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f ive when SQLITE_
99210 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a MUTEX_RECURSIVE.
99220 2a 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e ** is used but n
99230 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 ot necessarily s
99240 6f 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 o when SQLITE_MU
99250 54 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 TEX_FAST is used
99260 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 ..** The mutex i
99270 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f mplementation do
99280 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d es not need to m
99290 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f ake a distinctio
992a0 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c n.** between SQL
992b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
992c0 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d IVE and SQLITE_M
992d0 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 UTEX_FAST if it
992e0 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 does.** not want
992f0 20 74 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 to. But SQLite
99300 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 will only reque
99310 73 74 20 61 20 72 65 63 75 72 73 69 76 65 20 6d st a recursive m
99320 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 utex in.** cases
99330 20 77 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 where it really
99340 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 needs one. If
99350 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 a faster non-rec
99360 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 ursive mutex.**
99370 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 implementation i
99380 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 s available on t
99390 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d he host platform
993a0 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 , the mutex subs
993b0 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 ystem.** might r
993c0 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 eturn such a mut
993d0 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 ex in response t
993e0 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 o SQLITE_MUTEX_F
993f0 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f AST..**.** The o
99400 74 68 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 ther allowed par
99410 61 6d 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 ameters to sqlit
99420 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 e3_mutex_alloc()
99430 20 65 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 each return.**
99440 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 a pointer to a s
99450 74 61 74 69 63 20 70 72 65 65 78 69 73 74 69 6e tatic preexistin
99460 67 20 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 g mutex. Six st
99470 61 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 atic mutexes are
99480 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 .** used by the
99490 63 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 current version
994a0 6f 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 of SQLite. Futu
994b0 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 re versions of S
994c0 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 QLite.** may add
994d0 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 additional stat
994e0 69 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 ic mutexes. Sta
994f0 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 tic mutexes are
99500 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 for internal.**
99510 75 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e use by SQLite on
99520 6c 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e ly. Application
99530 73 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 s that use SQLit
99540 65 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 e mutexes should
99550 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 .** use only the
99560 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 dynamic mutexes
99570 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c returned by SQL
99580 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f ITE_MUTEX_FAST o
99590 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 r.** SQLITE_MUTE
995a0 58 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a X_RECURSIVE..**.
995b0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 ** Note that if
995c0 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d one of the dynam
995d0 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 ic mutex paramet
995e0 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 ers (SQLITE_MUTE
995f0 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c X_FAST.** or SQL
99600 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 ITE_MUTEX_RECURS
99610 49 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 IVE) is used the
99620 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f n sqlite3_mutex_
99630 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 alloc().** retur
99640 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d ns a different m
99650 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 utex on every ca
99660 6c 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 ll. But for the
99670 20 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 static .** mute
99680 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d x types, the sam
99690 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 e mutex is retur
996a0 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c ned on every cal
996b0 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 l that has.** th
996c0 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 e same type numb
996d0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 er..*/.static sq
996e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e lite3_mutex *win
996f0 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 MutexAlloc(int i
99700 54 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 Type){. sqlite3
99710 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 _mutex *p;.. sw
99720 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 itch( iType ){.
99730 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d case SQLITE_M
99740 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 UTEX_FAST:. c
99750 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 ase SQLITE_MUTEX
99760 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 _RECURSIVE: {.
99770 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d p = sqlite3M
99780 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f allocZero( sizeo
99790 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 f(*p) );. i
997a0 66 28 20 70 20 29 7b 20 20 0a 20 20 20 20 20 20 f( p ){ .
997b0 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b p->id = iType;
997c0 0a 20 20 20 20 20 20 20 20 49 6e 69 74 69 61 6c . Initial
997d0 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 izeCriticalSecti
997e0 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 on(&p->mutex);.
997f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
99800 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 ak;. }. de
99810 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 fault: {. a
99820 73 73 65 72 74 28 20 77 69 6e 4d 75 74 65 78 5f ssert( winMutex_
99830 69 73 49 6e 69 74 3d 3d 31 20 29 3b 0a 20 20 20 isInit==1 );.
99840 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 assert( iType
99850 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 -2 >= 0 );.
99860 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 assert( iType-2
99870 20 3c 20 41 72 72 61 79 53 69 7a 65 28 77 69 6e < ArraySize(win
99880 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 Mutex_staticMute
99890 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70 20 xes) );. p
998a0 3d 20 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 = &winMutex_stat
998b0 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d icMutexes[iType-
998c0 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 2];. p->id
998d0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 = iType;. b
998e0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
998f0 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a return p;.}...
99900 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
99910 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 ne deallocates a
99920 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 previously.** a
99930 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 llocated mutex.
99940 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 SQLite is caref
99950 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 ul to deallocate
99960 20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 every.** mutex
99970 74 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 that it allocate
99980 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 s..*/.static voi
99990 64 20 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 d winMutexFree(s
999a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 qlite3_mutex *p)
999b0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 29 3b {. assert( p );
999c0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 . assert( p->nR
999d0 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 ef==0 );. asser
999e0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 t( p->id==SQLITE
999f0 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 _MUTEX_FAST || p
99a00 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
99a10 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
99a20 20 20 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c DeleteCritical
99a30 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 Section(&p->mute
99a40 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 x);. sqlite3_fr
99a50 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ee(p);.}../*.**
99a60 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
99a70 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 x_enter() and sq
99a80 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 lite3_mutex_try(
99a90 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d ) routines attem
99aa0 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 pt.** to enter a
99ab0 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 mutex. If anot
99ac0 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c her thread is al
99ad0 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 ready within the
99ae0 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 mutex,.** sqlit
99af0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 e3_mutex_enter()
99b00 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 will block and
99b10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 sqlite3_mutex_tr
99b20 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a y() will return.
99b30 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 ** SQLITE_BUSY.
99b40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 The sqlite3_mut
99b50 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 ex_try() interfa
99b60 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 ce returns SQLIT
99b70 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 E_OK.** upon suc
99b80 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 cessful entry.
99b90 4d 75 74 65 78 65 73 20 63 72 65 61 74 65 64 20 Mutexes created
99ba0 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 using SQLITE_MUT
99bb0 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e EX_RECURSIVE can
99bc0 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d .** be entered m
99bd0 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 ultiple times by
99be0 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
99bf0 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 . In such cases
99c00 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d the,.** mutex m
99c10 75 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e ust be exited an
99c20 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 equal number of
99c30 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e times before an
99c40 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 other thread.**
99c50 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 can enter. If t
99c60 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 he same thread t
99c70 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e ries to enter an
99c80 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 y other kind of
99c90 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 mutex.** more th
99ca0 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 an once, the beh
99cb0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
99cc0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
99cd0 69 64 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 id winMutexEnter
99ce0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a (sqlite3_mutex *
99cf0 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p){. assert( p-
99d00 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 >id==SQLITE_MUTE
99d10 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 X_RECURSIVE || w
99d20 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 inMutexNotheld(p
99d30 29 20 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 ) );. EnterCrit
99d40 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
99d50 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e mutex);. p->own
99d60 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 er = GetCurrentT
99d70 68 72 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d hreadId(); . p-
99d80 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 >nRef++;.}.stati
99d90 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 c int winMutexTr
99da0 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 y(sqlite3_mutex
99db0 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 *p){. int rc =
99dc0 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 SQLITE_BUSY;. a
99dd0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 ssert( p->id==SQ
99de0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 LITE_MUTEX_RECUR
99df0 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 SIVE || winMutex
99e00 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 Notheld(p) );.
99e10 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 /*. ** The sqli
99e20 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 te3_mutex_try()
99e30 72 6f 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 routine is very
99e40 72 61 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 rarely used, and
99e50 20 77 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 when it. ** is
99e60 20 75 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 used it is mere
99e70 6c 79 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 ly an optimizati
99e80 6f 6e 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b on. So it is OK
99e90 20 66 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 for it to alway
99ea0 73 0a 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 s. ** fail. .
99eb0 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 **. ** The Try
99ec0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 EnterCriticalSec
99ed0 74 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 tion() interface
99ee0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 is only availab
99ef0 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a le on WinNT.. *
99f00 2a 20 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f * And some windo
99f10 77 73 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d ws compilers com
99f20 70 6c 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 plain if you try
99f30 20 74 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f to use it witho
99f40 75 74 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f ut. ** first do
99f50 69 6e 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 ing some #define
99f60 73 20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 s that prevent S
99f70 51 4c 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 QLite from build
99f80 69 6e 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 ing on Win98..
99f90 2a 2a 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 ** For that reas
99fa0 6f 6e 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 on, we will omit
99fb0 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 this optimizati
99fc0 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 on for now. See
99fd0 0a 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 . ** ticket #26
99fe0 38 35 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 85.. */.#if 0.
99ff0 20 69 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 if( mutexIsNT()
9a000 20 26 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 && TryEnterCrit
9a010 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e icalSection(&p->
9a020 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d mutex) ){. p-
9a030 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 >owner = GetCurr
9a040 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 entThreadId();.
9a050 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 p->nRef++;.
9a060 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
9a070 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e ;. }.#else. UN
9a080 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 USED_PARAMETER(p
9a090 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 );.#endif. retu
9a0a0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
9a0b0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 The sqlite3_mute
9a0c0 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69 6e x_leave() routin
9a0d0 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78 20 e exits a mutex
9a0e0 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 that was.** prev
9a0f0 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20 62 iously entered b
9a100 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 y the same threa
9a110 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f 72 d. The behavior
9a120 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 .** is undefined
9a130 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 if the mutex is
9a140 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 not currently e
9a150 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 ntered or.** is
9a160 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c not currently al
9a170 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74 65 located. SQLite
9a180 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 will never do e
9a190 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 ither..*/.static
9a1a0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c 65 void winMutexLe
9a1b0 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 ave(sqlite3_mute
9a1c0 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 x *p){. assert(
9a1d0 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 p->nRef>0 );.
9a1e0 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65 72 assert( p->owner
9a1f0 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 ==GetCurrentThre
9a200 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e 6e adId() );. p->n
9a210 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 Ref--;. assert(
9a220 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 p->nRef==0 || p
9a230 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 ->id==SQLITE_MUT
9a240 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a EX_RECURSIVE );.
9a250 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c 53 LeaveCriticalS
9a260 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 ection(&p->mutex
9a270 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 );.}..SQLITE_PRI
9a280 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 VATE sqlite3_mut
9a290 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 ex_methods *sqli
9a2a0 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 te3DefaultMutex(
9a2b0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 void){. static
9a2c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 sqlite3_mutex_me
9a2d0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b thods sMutex = {
9a2e0 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e 69 . winMutexIni
9a2f0 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 t,. winMutexE
9a300 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 nd,. winMutex
9a310 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d 75 Alloc,. winMu
9a320 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69 6e texFree,. win
9a330 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20 MutexEnter,.
9a340 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 winMutexTry,.
9a350 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c 0a winMutexLeave,.
9a360 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
9a370 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 BUG. winMutex
9a380 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 Held,. winMut
9a390 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a exNotheld.#else.
9a3a0 20 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e 0,. 0.#en
9a3b0 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 dif. };.. retu
9a3c0 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 rn &sMutex;.}.#e
9a3d0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d ndif /* SQLITE_M
9a3e0 55 54 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a UTEX_W32 */../**
9a3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
9a400 20 6f 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 of mutex_w32.c
9a410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
9a440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
9a450 69 6e 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 in file malloc.c
9a460 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
9a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
9a490 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
9a4a0 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 15.**.** The a
9a4b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
9a4c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
9a4d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
9a4e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
9a4f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
9a500 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
9a510 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
9a520 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
9a530 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
9a540 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
9a550 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
9a560 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
9a570 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
9a580 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
9a590 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
9a5a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
9a5b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
9a5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9a600 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 ***.**.** Memory
9a610 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 allocation func
9a620 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 tions used throu
9a630 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2f ghout sqlite..*/
9a640 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 ../*.** This rou
9a650 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74 tine runs when t
9a660 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 he memory alloca
9a670 74 6f 72 20 73 65 65 73 20 74 68 61 74 20 74 68 tor sees that th
9a680 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 e.** total memor
9a690 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 y allocation is
9a6a0 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20 about to exceed
9a6b0 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a the soft heap.**
9a6c0 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 limit..*/.stati
9a6d0 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c c void softHeapL
9a6e0 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 imitEnforcer(.
9a6f0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a void *NotUsed, .
9a700 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
9a710 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20 NotUsed2,. int
9a720 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55 allocSize.){. U
9a730 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 NUSED_PARAMETER2
9a740 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 (NotUsed, NotUse
9a750 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 d2);. sqlite3_r
9a760 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c elease_memory(al
9a770 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a locSize);.}../*.
9a780 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 ** Set the soft
9a790 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 heap-size limit
9a7a0 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e for the library.
9a7b0 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 Passing a zero
9a7c0 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 or .** negative
9a7d0 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 value indicates
9a7e0 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c no limit..*/.SQL
9a7f0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c ITE_API void sql
9a800 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c ite3_soft_heap_l
9a810 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 imit(int n){. s
9a820 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c qlite3_uint64 iL
9a830 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 imit;. int over
9a840 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 age;. if( n<0 )
9a850 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 {. iLimit = 0
9a860 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
9a870 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 23 Limit = n;. }.#
9a880 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
9a890 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 73 71 IT_AUTOINIT. sq
9a8a0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 lite3_initialize
9a8b0 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 ();.#endif. if(
9a8c0 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 iLimit>0 ){.
9a8d0 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c sqlite3MemoryAl
9a8e0 61 72 6d 28 73 6f 66 74 48 65 61 70 4c 69 6d 69 arm(softHeapLimi
9a8f0 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c 20 69 4c tEnforcer, 0, iL
9a900 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a imit);. }else{.
9a910 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 sqlite3Memor
9a920 79 41 6c 61 72 6d 28 30 2c 20 30 2c 20 30 29 3b yAlarm(0, 0, 0);
9a930 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65 20 3d . }. overage =
9a940 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 6d (int)(sqlite3_m
9a950 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d 20 28 emory_used() - (
9a960 69 36 34 29 6e 29 3b 0a 20 20 69 66 28 20 6f 76 i64)n);. if( ov
9a970 65 72 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 erage>0 ){. s
9a980 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d qlite3_release_m
9a990 65 6d 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a emory(overage);.
9a9a0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 }.}../*.** Att
9a9b0 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 empt to release
9a9c0 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 up to n bytes of
9a9d0 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d non-essential m
9a9e0 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a emory currently.
9a9f0 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 ** held by SQLit
9aa00 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 e. An example of
9aa10 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d non-essential m
9aa20 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 emory is memory
9aa30 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 used to.** cache
9aa40 20 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 database pages
9aa50 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 that are not cur
9aa60 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a rently in use..*
9aa70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
9aa80 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 sqlite3_release
9aa90 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a _memory(int n){.
9aaa0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
9aab0 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 ABLE_MEMORY_MANA
9aac0 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 GEMENT. int nRe
9aad0 74 20 3d 20 30 3b 0a 20 20 6e 52 65 74 20 2b 3d t = 0;. nRet +=
9aae0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
9aaf0 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d 6e 52 leaseMemory(n-nR
9ab00 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 52 et);. return nR
9ab10 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 et;.#else. UNUS
9ab20 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 29 3b ED_PARAMETER(n);
9ab30 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
9ab40 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f _OK;.#endif.}../
9ab50 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66 6f 72 *.** State infor
9ab60 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74 6f 20 mation local to
9ab70 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 the memory alloc
9ab80 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e ation subsystem.
9ab90 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 .*/.static SQLIT
9aba0 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d 65 6d E_WSD struct Mem
9abb0 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a 20 4e 0Global {. /* N
9abc0 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61 umber of free pa
9abd0 67 65 73 20 66 6f 72 20 73 63 72 61 74 63 68 20 ges for scratch
9abe0 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65 20 6d and page-cache m
9abf0 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32 20 6e emory */. u32 n
9ac00 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 ScratchFree;. u
9ac10 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a 0a 20 32 nPageFree;..
9ac20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
9ac30 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 2f mutex; /
9ac40 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72 69 61 * Mutex to seria
9ac50 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f 0a 0a lize access */..
9ac60 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 61 6c /*. ** The al
9ac70 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 arm callback and
9ac80 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73 2e 20 its arguments.
9ac90 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65 78 20 The mem0.mutex
9aca0 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 lock will. ** b
9acb0 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74 68 65 e held while the
9acc0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 75 6e callback is run
9acd0 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69 76 65 ning. Recursive
9ace0 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a calls into. **
9acf0 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 the memory subs
9ad00 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f 77 65 ystem are allowe
9ad10 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20 63 61 d, but no new ca
9ad20 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 65 0a llbacks will be.
9ad30 20 20 2a 2a 20 69 73 73 75 65 64 2e 0a 20 20 2a ** issued.. *
9ad40 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 /. sqlite3_int6
9ad50 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 4 alarmThreshold
9ad60 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61 72 6d ;. void (*alarm
9ad70 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c Callback)(void*,
9ad80 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 sqlite3_int64,i
9ad90 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61 6c 61 nt);. void *ala
9ada0 72 6d 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20 20 2a rmArg;.. /*. *
9adb0 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 * Pointers to th
9adc0 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 e end of sqlite3
9add0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9ade0 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a 20 73 ratch and. ** s
9adf0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9ae00 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20 62 6c ig.pPage to a bl
9ae10 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74 68 ock of memory th
9ae20 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a 2a 20 at records. **
9ae30 77 68 69 63 68 20 70 61 67 65 73 20 61 72 65 20 which pages are
9ae40 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a available.. */.
9ae50 20 20 75 33 32 20 2a 61 53 63 72 61 74 63 68 46 u32 *aScratchF
9ae60 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50 61 67 ree;. u32 *aPag
9ae70 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20 3d 20 eFree;.} mem0 =
9ae80 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c { 0, 0, 0, 0, 0,
9ae90 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23 64 65 0, 0, 0 };..#de
9aea0 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42 41 4c fine mem0 GLOBAL
9aeb0 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c 6f 62 (struct Mem0Glob
9aec0 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a 2a 2a al, mem0)../*.**
9aed0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
9aee0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
9aef0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a n subsystem..*/.
9af00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
9af10 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 nt sqlite3Malloc
9af20 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 69 66 Init(void){. if
9af30 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9af40 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d onfig.m.xMalloc=
9af50 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
9af60 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3MemSetDefault()
9af70 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 ;. }. memset(&
9af80 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f 66 28 mem0, 0, sizeof(
9af90 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20 73 71 mem0));. if( sq
9afa0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9afb0 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
9afc0 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78 20 3d mem0.mutex =
9afd0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c sqlite3MutexAll
9afe0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f oc(SQLITE_MUTEX_
9aff0 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d STATIC_MEM);. }
9b000 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
9b010 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 obalConfig.pScra
9b020 74 63 68 20 26 26 20 73 71 6c 69 74 65 33 47 6c tch && sqlite3Gl
9b030 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9b040 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20 20 20 atch>=100.
9b050 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c && sqlite3Global
9b060 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3e Config.nScratch>
9b070 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b =0 ){. int i;
9b080 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 . sqlite3Glob
9b090 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 alConfig.szScrat
9b0a0 63 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 ch = ROUNDDOWN8(
9b0b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b0c0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2d 34 29 fig.szScratch-4)
9b0d0 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 ;. mem0.aScra
9b0e0 74 63 68 46 72 65 65 20 3d 20 28 75 33 32 2a 29 tchFree = (u32*)
9b0f0 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 &((char*)sqlite3
9b100 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9b110 72 61 74 63 68 29 0a 20 20 20 20 20 20 20 20 20 ratch).
9b120 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
9b130 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
9b140 53 63 72 61 74 63 68 2a 73 71 6c 69 74 65 33 47 Scratch*sqlite3G
9b150 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 lobalConfig.nScr
9b160 61 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72 28 69 atch];. for(i
9b170 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f =0; i<sqlite3Glo
9b180 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 balConfig.nScrat
9b190 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 ch; i++){ mem0.a
9b1a0 53 63 72 61 74 63 68 46 72 65 65 5b 69 5d 20 3d ScratchFree[i] =
9b1b0 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e 6e i; }. mem0.n
9b1c0 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 73 71 ScratchFree = sq
9b1d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9b1e0 67 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20 7d 65 g.nScratch;. }e
9b1f0 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
9b200 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 GlobalConfig.pSc
9b210 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 73 ratch = 0;. s
9b220 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9b230 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 30 ig.szScratch = 0
9b240 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 ;. }. if( sqli
9b250 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b260 70 50 61 67 65 20 26 26 20 73 71 6c 69 74 65 33 pPage && sqlite3
9b270 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 GlobalConfig.szP
9b280 61 67 65 3e 3d 35 31 32 0a 20 20 20 20 20 20 26 age>=512. &
9b290 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 & sqlite3GlobalC
9b2a0 6f 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31 20 29 onfig.nPage>=1 )
9b2b0 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 {. int i;.
9b2c0 20 69 6e 74 20 6f 76 65 72 68 65 61 64 3b 0a 20 int overhead;.
9b2d0 20 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f 55 4e int sz = ROUN
9b2e0 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c DDOWN8(sqlite3Gl
9b2f0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 obalConfig.szPag
9b300 65 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 e);. int n =
9b310 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b320 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f fig.nPage;. o
9b330 76 65 72 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b verhead = (4*n +
9b340 20 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 sz - 1)/sz;.
9b350 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9b360 6e 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 nfig.nPage -= ov
9b370 65 72 68 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 erhead;. mem0
9b380 2e 61 50 61 67 65 46 72 65 65 20 3d 20 28 75 33 .aPageFree = (u3
9b390 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 2*)&((char*)sqli
9b3a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9b3b0 70 50 61 67 65 29 0a 20 20 20 20 20 20 20 20 20 pPage).
9b3c0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 [sqlite
9b3d0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
9b3e0 50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 Page*sqlite3Glob
9b3f0 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b alConfig.nPage];
9b400 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
9b410 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b420 66 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b fig.nPage; i++){
9b430 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b mem0.aPageFree[
9b440 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 i] = i; }. me
9b450 6d 30 2e 6e 50 61 67 65 46 72 65 65 20 3d 20 73 m0.nPageFree = s
9b460 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9b470 69 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 ig.nPage;. }els
9b480 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c e{. sqlite3Gl
9b490 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 obalConfig.pPage
9b4a0 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 0;. sqlite
9b4b0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 3GlobalConfig.sz
9b4c0 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 Page = 0;. }.
9b4d0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c return sqlite3Gl
9b4e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e obalConfig.m.xIn
9b4f0 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c it(sqlite3Global
9b500 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 Config.m.pAppDat
9b510 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 a);.}../*.** Dei
9b520 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 nitialize the me
9b530 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
9b540 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 subsystem..*/.SQ
9b550 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9b560 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 d sqlite3MallocE
9b570 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 nd(void){. if(
9b580 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9b590 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 20 fig.m.xShutdown
9b5a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c ){. sqlite3Gl
9b5b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 obalConfig.m.xSh
9b5c0 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c utdown(sqlite3Gl
9b5d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 obalConfig.m.pAp
9b5e0 70 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20 6d 65 pData);. }. me
9b5f0 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 mset(&mem0, 0, s
9b600 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a izeof(mem0));.}.
9b610 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
9b620 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f e amount of memo
9b630 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 ry currently che
9b640 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c cked out..*/.SQL
9b650 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
9b660 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 int64 sqlite3_me
9b670 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b mory_used(void){
9b680 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 . int n, mx;.
9b690 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 sqlite3_int64 re
9b6a0 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 s;. sqlite3_sta
9b6b0 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 tus(SQLITE_STATU
9b6c0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 S_MEMORY_USED, &
9b6d0 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 n, &mx, 0);. re
9b6e0 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 s = (sqlite3_int
9b6f0 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 64)n; /* Work a
9b700 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 round bug in Bor
9b710 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 land C. Ticket #
9b720 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3216 */. return
9b730 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 res;.}../*.** R
9b740 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 eturn the maximu
9b750 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f m amount of memo
9b760 72 79 20 74 68 61 74 20 68 61 73 20 65 76 65 72 ry that has ever
9b770 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 been.** checked
9b780 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 out since eithe
9b790 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 r the beginning
9b7a0 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a of this process.
9b7b0 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 ** or since the
9b7c0 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 most recent rese
9b7d0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 t..*/.SQLITE_API
9b7e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 sqlite3_int64 s
9b7f0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 qlite3_memory_hi
9b800 67 68 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 ghwater(int rese
9b810 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c tFlag){. int n,
9b820 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 mx;. sqlite3_i
9b830 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 nt64 res;. sqli
9b840 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 te3_status(SQLIT
9b850 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9b860 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 USED, &n, &mx, r
9b870 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 esetFlag);. res
9b880 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 = (sqlite3_int6
9b890 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 4)mx; /* Work a
9b8a0 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 round bug in Bor
9b8b0 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 land C. Ticket #
9b8c0 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3216 */. return
9b8d0 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 res;.}../*.** C
9b8e0 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 hange the alarm
9b8f0 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 callback.*/.SQLI
9b900 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
9b910 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 qlite3MemoryAlar
9b920 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c m(. void(*xCall
9b930 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 back)(void *pArg
9b940 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 , sqlite3_int64
9b950 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 used,int N),. v
9b960 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c oid *pArg,. sql
9b970 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 ite3_int64 iThre
9b980 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 shold.){. sqlit
9b990 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
9b9a0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 em0.mutex);. me
9b9b0 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b m0.alarmCallback
9b9c0 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 = xCallback;.
9b9d0 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 mem0.alarmArg =
9b9e0 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 pArg;. mem0.ala
9b9f0 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 rmThreshold = iT
9ba00 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 hreshold;. sqli
9ba10 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 te3_mutex_leave(
9ba20 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 mem0.mutex);. r
9ba30 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
9ba40 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
9ba50 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 TE_OMIT_DEPRECAT
9ba60 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 ED./*.** Depreca
9ba70 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 ted external int
9ba80 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 erface. Interna
9ba90 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f l/core SQLite co
9baa0 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c de.** should cal
9bab0 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 l sqlite3MemoryA
9bac0 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f larm..*/.SQLITE_
9bad0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
9bae0 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 memory_alarm(.
9baf0 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 void(*xCallback)
9bb00 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c (void *pArg, sql
9bb10 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c ite3_int64 used,
9bb20 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a int N),. void *
9bb30 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f pArg,. sqlite3_
9bb40 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 int64 iThreshold
9bb50 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c .){. return sql
9bb60 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 ite3MemoryAlarm(
9bb70 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c xCallback, pArg,
9bb80 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a iThreshold);.}.
9bb90 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 #endif../*.** Tr
9bba0 69 67 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 igger the alarm
9bbb0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
9bbc0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 sqlite3MallocAla
9bbd0 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 rm(int nByte){.
9bbe0 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 void (*xCallbac
9bbf0 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 k)(void*,sqlite3
9bc00 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 _int64,int);. s
9bc10 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 qlite3_int64 now
9bc20 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 Used;. void *pA
9bc30 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 rg;. if( mem0.a
9bc40 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 larmCallback==0
9bc50 29 20 72 65 74 75 72 6e 3b 0a 20 20 78 43 61 6c ) return;. xCal
9bc60 6c 62 61 63 6b 20 3d 20 6d 65 6d 30 2e 61 6c 61 lback = mem0.ala
9bc70 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6e 6f rmCallback;. no
9bc80 77 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 wUsed = sqlite3S
9bc90 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 tatusValue(SQLIT
9bca0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9bcb0 55 53 45 44 29 3b 0a 20 20 70 41 72 67 20 3d 20 USED);. pArg =
9bcc0 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 3b 0a 20 mem0.alarmArg;.
9bcd0 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 mem0.alarmCallb
9bce0 61 63 6b 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 ack = 0;. sqlit
9bcf0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
9bd00 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 em0.mutex);. xC
9bd10 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f allback(pArg, no
9bd20 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 wUsed, nByte);.
9bd30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
9bd40 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 nter(mem0.mutex)
9bd50 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 ;. mem0.alarmCa
9bd60 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 llback = xCallba
9bd70 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d ck;. mem0.alarm
9bd80 41 72 67 20 3d 20 70 41 72 67 3b 0a 7d 0a 0a 2f Arg = pArg;.}../
9bd90 2a 0a 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f 72 79 *.** Do a memory
9bda0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 69 74 68 allocation with
9bdb0 20 73 74 61 74 69 73 74 69 63 73 20 61 6e 64 20 statistics and
9bdc0 61 6c 61 72 6d 73 2e 20 20 41 73 73 75 6d 65 20 alarms. Assume
9bdd0 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 the.** lock is a
9bde0 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 2a 2f 0a lready held..*/.
9bdf0 73 74 61 74 69 63 20 69 6e 74 20 6d 61 6c 6c 6f static int mallo
9be00 63 57 69 74 68 41 6c 61 72 6d 28 69 6e 74 20 6e cWithAlarm(int n
9be10 2c 20 76 6f 69 64 20 2a 2a 70 70 29 7b 0a 20 20 , void **pp){.
9be20 69 6e 74 20 6e 46 75 6c 6c 3b 0a 20 20 76 6f 69 int nFull;. voi
9be30 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 d *p;. assert(
9be40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
9be50 6c 64 28 6d 65 6d 30 2e 6d 75 74 65 78 29 20 29 ld(mem0.mutex) )
9be60 3b 0a 20 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 ;. nFull = sqli
9be70 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9be80 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 29 3b 0a 20 m.xRoundup(n);.
9be90 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 sqlite3StatusSe
9bea0 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f t(SQLITE_STATUS_
9beb0 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 29 3b MALLOC_SIZE, n);
9bec0 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61 72 . if( mem0.alar
9bed0 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 20 29 7b 0a mCallback!=0 ){.
9bee0 20 20 20 20 69 6e 74 20 6e 55 73 65 64 20 3d 20 int nUsed =
9bef0 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c sqlite3StatusVal
9bf00 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 ue(SQLITE_STATUS
9bf10 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 _MEMORY_USED);.
9bf20 20 20 20 69 66 28 20 6e 55 73 65 64 2b 6e 46 75 if( nUsed+nFu
9bf30 6c 6c 20 3e 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d ll >= mem0.alarm
9bf40 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 20 Threshold ){.
9bf50 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 sqlite3Malloc
9bf60 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 Alarm(nFull);.
9bf70 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71 }. }. p = sq
9bf80 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9bf90 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 6c g.m.xMalloc(nFul
9bfa0 6c 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 26 l);. if( p==0 &
9bfb0 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c & mem0.alarmCall
9bfc0 62 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 back ){. sqli
9bfd0 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e te3MallocAlarm(n
9bfe0 46 75 6c 6c 29 3b 0a 20 20 20 20 70 20 3d 20 73 Full);. p = s
9bff0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 qlite3GlobalConf
9c000 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46 75 ig.m.xMalloc(nFu
9c010 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ll);. }. if( p
9c020 20 29 7b 0a 20 20 20 20 6e 46 75 6c 6c 20 3d 20 ){. nFull =
9c030 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
9c040 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 e(p);. sqlite
9c050 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9c060 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9c070 55 53 45 44 2c 20 6e 46 75 6c 6c 29 3b 0a 20 20 USED, nFull);.
9c080 7d 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 }. *pp = p;. r
9c090 65 74 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d 0a 0a eturn nFull;.}..
9c0a0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d /*.** Allocate m
9c0b0 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 72 6f 75 emory. This rou
9c0c0 74 69 6e 65 20 69 73 20 6c 69 6b 65 20 73 71 6c tine is like sql
9c0d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 65 78 ite3_malloc() ex
9c0e0 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a 2a 20 cept that it.**
9c0f0 61 73 73 75 6d 65 73 20 74 68 65 20 6d 65 6d 6f assumes the memo
9c100 72 79 20 73 75 62 73 79 73 74 65 6d 20 68 61 73 ry subsystem has
9c110 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e already been in
9c120 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 itialized..*/.SQ
9c130 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
9c140 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 d *sqlite3Malloc
9c150 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 (int n){. void
9c160 2a 70 3b 0a 20 20 69 66 28 20 6e 3c 3d 30 20 7c *p;. if( n<=0 |
9c170 7c 20 6e 3e 3d 30 78 37 66 66 66 66 66 30 30 20 | n>=0x7fffff00
9c180 29 7b 0a 20 20 20 20 2f 2a 20 41 20 6d 65 6d 6f ){. /* A memo
9c190 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 ry allocation of
9c1a0 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 a number of byt
9c1b0 65 73 20 77 68 69 63 68 20 69 73 20 6e 65 61 72 es which is near
9c1c0 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 the maximum.
9c1d0 20 2a 2a 20 73 69 67 6e 65 64 20 69 6e 74 65 67 ** signed integ
9c1e0 65 72 20 76 61 6c 75 65 20 6d 69 67 68 74 20 63 er value might c
9c1f0 61 75 73 65 20 61 6e 20 69 6e 74 65 67 65 72 20 ause an integer
9c200 6f 76 65 72 66 6c 6f 77 20 69 6e 73 69 64 65 20 overflow inside
9c210 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 78 4d of the. ** xM
9c220 61 6c 6c 6f 63 28 29 2e 20 20 48 65 6e 63 65 20 alloc(). Hence
9c230 77 65 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 we limit the max
9c240 69 6d 75 6d 20 73 69 7a 65 20 74 6f 20 30 78 37 imum size to 0x7
9c250 66 66 66 66 66 30 30 2c 20 67 69 76 69 6e 67 0a fffff00, giving.
9c260 20 20 20 20 2a 2a 20 32 35 35 20 62 79 74 65 73 ** 255 bytes
9c270 20 6f 66 20 6f 76 65 72 68 65 61 64 2e 20 20 53 of overhead. S
9c280 51 4c 69 74 65 20 69 74 73 65 6c 66 20 77 69 6c QLite itself wil
9c290 6c 20 6e 65 76 65 72 20 75 73 65 20 61 6e 79 74 l never use anyt
9c2a0 68 69 6e 67 20 6e 65 61 72 0a 20 20 20 20 2a 2a hing near. **
9c2b0 20 74 68 69 73 20 61 6d 6f 75 6e 74 2e 20 20 54 this amount. T
9c2c0 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 72 he only way to r
9c2d0 65 61 63 68 20 74 68 65 20 6c 69 6d 69 74 20 69 each the limit i
9c2e0 73 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 6d s with sqlite3_m
9c2f0 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 70 alloc() */. p
9c300 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
9c310 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9c320 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
9c330 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
9c340 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
9c350 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f utex);. mallo
9c360 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 cWithAlarm(n, &p
9c370 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
9c380 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e utex_leave(mem0.
9c390 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b mutex);. }else{
9c3a0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
9c3b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9c3c0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20 Malloc(n);. }.
9c3d0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a return p;.}../*
9c3e0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e .** This version
9c3f0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 of the memory a
9c400 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72 llocation is for
9c410 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c use by the appl
9c420 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73 ication..** Firs
9c430 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 t make sure the
9c440 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d memory subsystem
9c450 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c is initialized,
9c460 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20 then do the.**
9c470 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 allocation..*/.S
9c480 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a QLITE_API void *
9c490 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 sqlite3_malloc(i
9c4a0 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 nt n){.#ifndef S
9c4b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 QLITE_OMIT_AUTOI
9c4c0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 NIT. if( sqlite
9c4d0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 3_initialize() )
9c4e0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 return 0;.#endi
9c4f0 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 f. return sqlit
9c500 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a e3Malloc(n);.}..
9c510 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61 /*.** Each threa
9c520 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 d may only have
9c530 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e a single outstan
9c540 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 ding allocation
9c550 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68 from.** xScratch
9c560 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65 Malloc(). We ve
9c570 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 rify this constr
9c580 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67 aint in the sing
9c590 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63 le-threaded.** c
9c5a0 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73 ase by setting s
9c5b0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74 cratchAllocOut t
9c5c0 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f o 1 when an allo
9c5d0 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74 cation.** is out
9c5e0 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e standing clearin
9c5f0 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c g it when the al
9c600 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 location is free
9c610 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 d..*/.#if SQLITE
9c620 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
9c630 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
9c640 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 G).static int sc
9c650 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 ratchAllocOut =
9c660 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 0;.#endif.../*.*
9c670 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 * Allocate memor
9c680 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 y that is to be
9c690 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 used and release
9c6a0 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a d right away..**
9c6b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
9c6c0 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f similar to allo
9c6d0 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20 ca() in that it
9c6e0 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a is not intended.
9c6f0 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e ** for situation
9c700 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f s where the memo
9c710 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64 ry might be held
9c720 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69 long-term. Thi
9c730 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 s.** routine is
9c740 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20 intended to get
9c750 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61 memory to old la
9c760 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61 rge transient da
9c770 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 ta.** structures
9c780 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20 that would not
9c790 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20 normally fit on
9c7a0 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a the stack of an.
9c7b0 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63 ** embedded proc
9c7c0 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 essor..*/.SQLITE
9c7d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 _PRIVATE void *s
9c7e0 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c qlite3ScratchMal
9c7f0 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f loc(int n){. vo
9c800 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 id *p;. assert(
9c810 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c n>0 );..#if SQL
9c820 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9c830 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
9c840 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66 EBUG). /* Verif
9c850 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 y that no more t
9c860 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 han one scratch
9c870 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 allocation per t
9c880 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75 hread. ** is ou
9c890 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65 tstanding at one
9c8a0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73 time. (This is
9c8b0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e only checked in
9c8c0 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 the. ** single
9c8d0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73 -threaded case s
9c8e0 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e ince checking in
9c8f0 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 the multi-threa
9c900 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f ded case. ** wo
9c910 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65 uld be much more
9c920 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a complicated.) *
9c930 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61 /. assert( scra
9c940 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29 tchAllocOut==0 )
9c950 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 ;.#endif.. if(
9c960 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9c970 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20 fig.szScratch<n
9c980 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61 ){. goto scra
9c990 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20 tch_overflow;.
9c9a0 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c }else{ . sql
9c9b0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
9c9c0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 (mem0.mutex);.
9c9d0 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61 if( mem0.nScra
9c9e0 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20 tchFree==0 ){.
9c9f0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
9ca00 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 x_leave(mem0.mut
9ca10 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 ex);. goto
9ca20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 scratch_overflow
9ca30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
9ca40 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
9ca50 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 i = mem0.aScratc
9ca60 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63 hFree[--mem0.nSc
9ca70 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20 ratchFree];.
9ca80 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c i *= sqlite3Gl
9ca90 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 obalConfig.szScr
9caa0 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69 atch;. sqli
9cab0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c te3StatusAdd(SQL
9cac0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 ITE_STATUS_SCRAT
9cad0 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20 CH_USED, 1);.
9cae0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9caf0 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Set(SQLITE_STATU
9cb00 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 S_SCRATCH_SIZE,
9cb10 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 n);. sqlite
9cb20 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 3_mutex_leave(me
9cb30 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 m0.mutex);.
9cb40 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 p = (void*)&((c
9cb50 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 har*)sqlite3Glob
9cb60 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 alConfig.pScratc
9cb70 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 h)[i];. ass
9cb80 65 72 74 28 20 20 28 28 28 75 38 2a 29 70 20 2d ert( (((u8*)p -
9cb90 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d 3d 30 (u8*)0) & 7)==0
9cba0 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 );. }. }.#i
9cbb0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
9cbc0 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e AFE==0 && !defin
9cbd0 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 ed(NDEBUG). scr
9cbe0 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 atchAllocOut = p
9cbf0 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 72 !=0;.#endif.. r
9cc00 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61 74 63 eturn p;..scratc
9cc10 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20 69 66 h_overflow:. if
9cc20 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9cc30 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 onfig.bMemstat )
9cc40 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 {. sqlite3_mu
9cc50 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d tex_enter(mem0.m
9cc60 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 utex);. sqlit
9cc70 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 e3StatusSet(SQLI
9cc80 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 TE_STATUS_SCRATC
9cc90 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 H_SIZE, n);.
9cca0 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c n = mallocWithAl
9ccb0 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 arm(n, &p);.
9ccc0 69 66 28 20 70 20 29 20 73 71 6c 69 74 65 33 53 if( p ) sqlite3S
9ccd0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f tatusAdd(SQLITE_
9cce0 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f STATUS_SCRATCH_O
9ccf0 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20 20 20 VERFLOW, n);.
9cd00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
9cd10 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 eave(mem0.mutex)
9cd20 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
9cd30 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c = sqlite3Global
9cd40 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 Config.m.xMalloc
9cd50 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c (n);. }.#if SQL
9cd60 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d ITE_THREADSAFE==
9cd70 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 0 && !defined(ND
9cd80 45 42 55 47 29 0a 20 20 73 63 72 61 74 63 68 41 EBUG). scratchA
9cd90 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30 3b 0a llocOut = p!=0;.
9cda0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 #endif. return
9cdb0 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f p; .}.SQLITE_
9cdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
9cdd0 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65 28 ite3ScratchFree(
9cde0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 void *p){. if(
9cdf0 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49 54 45 p ){..#if SQLITE
9ce00 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 _THREADSAFE==0 &
9ce10 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 & !defined(NDEBU
9ce20 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 G). /* Verify
9ce30 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74 68 that no more th
9ce40 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20 61 an one scratch a
9ce50 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74 68 llocation per th
9ce60 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73 20 6f read. ** is o
9ce70 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e utstanding at on
9ce80 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 e time. (This i
9ce90 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 s only checked i
9cea0 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 69 6e n the. ** sin
9ceb0 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 gle-threaded cas
9cec0 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 e since checking
9ced0 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 in the multi-th
9cee0 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 20 20 readed case.
9cef0 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 ** would be much
9cf00 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 more complicate
9cf10 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 d.) */. asser
9cf20 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f t( scratchAllocO
9cf30 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73 63 72 ut==1 );. scr
9cf40 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 30 atchAllocOut = 0
9cf50 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 ;.#endif.. if
9cf60 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 ( sqlite3GlobalC
9cf70 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 3d 3d onfig.pScratch==
9cf80 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 0. ||
9cf90 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 p<sqlite3GlobalC
9cfa0 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 0a 20 onfig.pScratch.
9cfb0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d || p>=
9cfc0 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53 63 72 (void*)mem0.aScr
9cfd0 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20 20 20 atchFree ){.
9cfe0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
9cff0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 balConfig.bMemst
9d000 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e at ){. in
9d010 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 t iSize = sqlite
9d020 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 3MallocSize(p);.
9d030 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
9d040 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 mutex_enter(mem0
9d050 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 .mutex);.
9d060 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9d070 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9d080 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 SCRATCH_OVERFLOW
9d090 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 , -iSize);.
9d0a0 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 sqlite3Status
9d0b0 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 Add(SQLITE_STATU
9d0c0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d S_MEMORY_USED, -
9d0d0 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 iSize);.
9d0e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
9d0f0 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a fig.m.xFree(p);.
9d100 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
9d110 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9d120 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d .mutex);. }
9d130 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 else{. sq
9d140 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
9d150 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 g.m.xFree(p);.
9d160 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
9d170 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
9d180 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28 28 75 i = (int)((u
9d190 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71 6c 69 8*)p - (u8*)sqli
9d1a0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
9d1b0 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20 20 20 pScratch);.
9d1c0 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f i /= sqlite3Glo
9d1d0 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 balConfig.szScra
9d1e0 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 tch;. asser
9d1f0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c t( i>=0 && i<sql
9d200 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9d210 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 .nScratch );.
9d220 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
9d230 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 _enter(mem0.mute
9d240 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 x);. assert
9d250 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 ( mem0.nScratchF
9d260 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 ree<(u32)sqlite3
9d270 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 GlobalConfig.nSc
9d280 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d ratch );. m
9d290 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 em0.aScratchFree
9d2a0 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 [mem0.nScratchFr
9d2b0 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 ee++] = i;.
9d2c0 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 sqlite3StatusAd
9d2d0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f d(SQLITE_STATUS_
9d2e0 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 SCRATCH_USED, -1
9d2f0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
9d300 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d _mutex_leave(mem
9d310 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 0.mutex);. }.
9d320 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 }.}../*.** TRU
9d330 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b E if p is a look
9d340 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c aside memory all
9d350 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a ocation from db.
9d360 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
9d370 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 E_OMIT_LOOKASIDE
9d380 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f .static int isLo
9d390 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 okaside(sqlite3
9d3a0 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 *db, void *p){.
9d3b0 20 72 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 return db && p
9d3c0 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 && p>=db->lookas
9d3d0 69 64 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c ide.pStart && p<
9d3e0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 db->lookaside.pE
9d3f0 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 nd;.}.#else.#def
9d400 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 ine isLookaside(
9d410 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f A,B) 0.#endif../
9d420 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
9d430 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 size of a memory
9d440 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 allocation prev
9d450 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 iously obtained
9d460 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d from.** sqlite3M
9d470 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 alloc() or sqlit
9d480 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a e3_malloc()..*/.
9d490 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
9d4a0 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 nt sqlite3Malloc
9d4b0 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 Size(void *p){.
9d4c0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 return sqlite3G
9d4d0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 lobalConfig.m.xS
9d4e0 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 ize(p);.}.SQLITE
9d4f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
9d500 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 ite3DbMallocSize
9d510 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f (sqlite3 *db, vo
9d520 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 id *p){. assert
9d530 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ( db==0 || sqlit
9d540 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
9d550 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 ->mutex) );. if
9d560 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 64 62 ( isLookaside(db
9d570 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , p) ){. retu
9d580 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 rn db->lookaside
9d590 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 .sz;. }else{.
9d5a0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
9d5b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 GlobalConfig.m.x
9d5c0 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a Size(p);. }.}..
9d5d0 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 /*.** Free memor
9d5e0 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 y previously obt
9d5f0 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 ained from sqlit
9d600 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 e3Malloc()..*/.S
9d610 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
9d620 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 qlite3_free(void
9d630 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 *p){. if( p==0
9d640 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 ) return;. if(
9d650 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f sqlite3GlobalCo
9d660 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b nfig.bMemstat ){
9d670 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
9d680 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 ex_enter(mem0.mu
9d690 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 tex);. sqlite
9d6a0 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 3StatusAdd(SQLIT
9d6b0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f E_STATUS_MEMORY_
9d6c0 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33 4d 61 USED, -sqlite3Ma
9d6d0 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a 20 20 llocSize(p));.
9d6e0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
9d6f0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 onfig.m.xFree(p)
9d700 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 ;. sqlite3_mu
9d710 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d tex_leave(mem0.m
9d720 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a utex);. }else{.
9d730 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 sqlite3Globa
9d740 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 lConfig.m.xFree(
9d750 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a p);. }.}../*.**
9d760 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74 68 61 Free memory tha
9d770 74 20 6d 69 67 68 74 20 62 65 20 61 73 73 6f 63 t might be assoc
9d780 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72 iated with a par
9d790 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 ticular database
9d7a0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a .** connection..
9d7b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
9d7c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 62 E void sqlite3Db
9d7d0 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 Free(sqlite3 *db
9d7e0 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 , void *p){. as
9d7f0 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 sert( db==0 || s
9d800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
9d810 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a d(db->mutex) );.
9d820 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 if( isLookasid
9d830 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 e(db, p) ){.
9d840 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 LookasideSlot *p
9d850 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 Buf = (Lookaside
9d860 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 42 75 Slot*)p;. pBu
9d870 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 6c f->pNext = db->l
9d880 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 3b 0a ookaside.pFree;.
9d890 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
9d8a0 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66 3b 0a e.pFree = pBuf;.
9d8b0 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 db->lookasid
9d8c0 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65 6c 73 e.nOut--;. }els
9d8d0 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 e{. sqlite3_f
9d8e0 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ree(p);. }.}../
9d8f0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
9d900 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74 size of an exist
9d910 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 ing memory alloc
9d920 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ation.*/.SQLITE_
9d930 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
9d940 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 lite3Realloc(voi
9d950 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 42 79 d *pOld, int nBy
9d960 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 tes){. int nOld
9d970 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64 20 2a , nNew;. void *
9d980 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f 6c 64 pNew;. if( pOld
9d990 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
9d9a0 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 n sqlite3Malloc(
9d9b0 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 nBytes);. }. i
9d9c0 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a f( nBytes<=0 ){.
9d9d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
9d9e0 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 65 74 75 (pOld);. retu
9d9f0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
9da00 6e 42 79 74 65 73 3e 3d 30 78 37 66 66 66 66 66 nBytes>=0x7fffff
9da10 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 00 ){. /* The
9da20 20 30 78 37 66 66 66 66 30 30 20 6c 69 6d 69 74 0x7ffff00 limit
9da30 20 74 65 72 6d 20 69 73 20 65 78 70 6c 61 69 6e term is explain
9da40 65 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73 20 6f ed in comments o
9da50 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 n sqlite3Malloc(
9da60 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 ) */. return
9da70 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 0;. }. nOld =
9da80 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a sqlite3MallocSiz
9da90 65 28 70 4f 6c 64 29 3b 0a 20 20 6e 4e 65 77 20 e(pOld);. nNew
9daa0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 = sqlite3GlobalC
9dab0 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 onfig.m.xRoundup
9dac0 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 (nBytes);. if(
9dad0 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a 20 20 nOld==nNew ){.
9dae0 20 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b 0a 20 pNew = pOld;.
9daf0 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 }else if( sqlit
9db00 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 e3GlobalConfig.b
9db10 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 Memstat ){. s
9db20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
9db30 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a er(mem0.mutex);.
9db40 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
9db50 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 sSet(SQLITE_STAT
9db60 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 US_MALLOC_SIZE,
9db70 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 nBytes);. if(
9db80 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 sqlite3StatusVa
9db90 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 lue(SQLITE_STATU
9dba0 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 2b 6e S_MEMORY_USED)+n
9dbb0 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20 20 20 New-nOld >= .
9dbc0 20 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c 61 72 mem0.alar
9dbd0 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20 20 mThreshold ){.
9dbe0 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f sqlite3Mallo
9dbf0 63 41 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 cAlarm(nNew-nOld
9dc00 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 );. }. pNe
9dc10 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 w = sqlite3Globa
9dc20 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c lConfig.m.xReall
9dc30 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a oc(pOld, nNew);.
9dc40 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 if( pNew==0
9dc50 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c && mem0.alarmCal
9dc60 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 lback ){. s
9dc70 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 qlite3MallocAlar
9dc80 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 m(nBytes);.
9dc90 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 pNew = sqlite3G
9dca0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 lobalConfig.m.xR
9dcb0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 ealloc(pOld, nNe
9dcc0 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 w);. }. if
9dcd0 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 ( pNew ){.
9dce0 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 nNew = sqlite3Ma
9dcf0 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29 3b 0a llocSize(pNew);.
9dd00 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 sqlite3Sta
9dd10 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
9dd20 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 ATUS_MEMORY_USED
9dd30 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 , nNew-nOld);.
9dd40 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f }. sqlite3_
9dd50 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 mutex_leave(mem0
9dd60 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 .mutex);. }else
9dd70 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c {. pNew = sql
9dd80 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
9dd90 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 .m.xRealloc(pOld
9dda0 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 72 , nNew);. }. r
9ddb0 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f eturn pNew;.}../
9ddc0 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c 69 63 20 *.** The public
9ddd0 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73 71 6c interface to sql
9dde0 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 20 4d 61 ite3Realloc. Ma
9ddf0 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
9de00 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 62 73 79 memory.** subsy
9de10 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 stem is initiali
9de20 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 zed prior to inv
9de30 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 65 61 6c oking sqliteReal
9de40 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 loc..*/.SQLITE_A
9de50 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 PI void *sqlite3
9de60 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 _realloc(void *p
9de70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a 23 69 66 Old, int n){.#if
9de80 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
9de90 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 _AUTOINIT. if(
9dea0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 sqlite3_initiali
9deb0 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b ze() ) return 0;
9dec0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e .#endif. return
9ded0 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 sqlite3Realloc(
9dee0 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a pOld, n);.}.../*
9def0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 .** Allocate and
9df00 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2f zero memory..*/
9df10 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 .SQLITE_PRIVATE
9df20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 void *sqlite3Ma
9df30 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 6e 29 7b llocZero(int n){
9df40 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c . void *p = sql
9df50 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 ite3Malloc(n);.
9df60 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 if( p ){. me
9df70 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 mset(p, 0, n);.
9df80 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d }. return p;.}
9df90 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
9dfa0 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 and zero memory
9dfb0 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 . If the alloca
9dfc0 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 tion fails, make
9dfd0 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 .** the mallocFa
9dfe0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 iled flag in the
9dff0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e connection poin
9e000 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ter..*/.SQLITE_P
9e010 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c RIVATE void *sql
9e020 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
9e030 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e (sqlite3 *db, in
9e040 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 t n){. void *p
9e050 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
9e060 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 69 cRaw(db, n);. i
9e070 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 f( p ){. mems
9e080 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d et(p, 0, n);. }
9e090 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
9e0a0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 /*.** Allocate a
9e0b0 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 20 nd zero memory.
9e0c0 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 If the allocati
9e0d0 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65 0a 2a on fails, make.*
9e0e0 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c * the mallocFail
9e0f0 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 ed flag in the c
9e100 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e 74 65 onnection pointe
9e110 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62 21 3d r..**.** If db!=
9e120 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 0 and db->malloc
9e130 46 61 69 6c 65 64 20 69 73 20 74 72 75 65 20 28 Failed is true (
9e140 69 6e 64 69 63 61 74 69 6e 67 20 61 20 70 72 69 indicating a pri
9e150 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66 61 69 or malloc.** fai
9e160 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 lure on the same
9e170 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
9e180 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c 77 61 79 tion) then alway
9e190 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 20 48 s return 0..** H
9e1a0 65 6e 63 65 20 66 6f 72 20 61 20 70 61 72 74 69 ence for a parti
9e1b0 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 cular database c
9e1c0 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63 65 20 onnection, once
9e1d0 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 0a 2a 2a malloc starts.**
9e1e0 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 66 61 69 failing, it fai
9e1f0 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 ls consistently
9e200 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 61 69 6c until mallocFail
9e210 65 64 20 69 73 20 72 65 73 65 74 2e 0a 2a 2a 20 ed is reset..**
9e220 54 68 69 73 20 69 73 20 61 6e 20 69 6d 70 6f 72 This is an impor
9e230 74 61 6e 74 20 61 73 73 75 6d 70 74 69 6f 6e 2e tant assumption.
9e240 20 20 54 68 65 72 65 20 61 72 65 20 6d 61 6e 79 There are many
9e250 20 70 6c 61 63 65 73 20 69 6e 20 74 68 65 0a 2a places in the.*
9e260 2a 20 63 6f 64 65 20 74 68 61 74 20 64 6f 20 74 * code that do t
9e270 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 69 73 3a hings like this:
9e280 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 .**.** i
9e290 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29 73 71 nt *a = (int*)sq
9e2a0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
9e2b0 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a 20 20 20 (db, 100);.**
9e2c0 20 20 20 20 20 20 69 6e 74 20 2a 62 20 3d 20 28 int *b = (
9e2d0 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 int*)sqlite3DbMa
9e2e0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 30 30 29 llocRaw(db, 200)
9e2f0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66 28 ;.** if(
9e300 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 39 3b 0a b ) a[10] = 9;.
9e310 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 **.** In other w
9e320 6f 72 64 73 2c 20 69 66 20 61 20 73 75 62 73 65 ords, if a subse
9e330 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 28 65 78 quent malloc (ex
9e340 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 2c 20 69 : "b") worked, i
9e350 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 t is assumed.**
9e360 74 68 61 74 20 61 6c 6c 20 70 72 69 6f 72 20 6d that all prior m
9e370 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61 22 29 allocs (ex: "a")
9e380 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a 2f 0a worked too..*/.
9e390 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
9e3a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 oid *sqlite3DbMa
9e3b0 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 20 llocRaw(sqlite3
9e3c0 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 *db, int n){. v
9e3d0 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 oid *p;. assert
9e3e0 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 ( db==0 || sqlit
9e3f0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 e3_mutex_held(db
9e400 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 6e ->mutex) );.#ifn
9e410 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
9e420 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69 66 28 20 LOOKASIDE. if(
9e430 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 db ){. Lookas
9e440 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b 0a 20 ideSlot *pBuf;.
9e450 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f if( db->mallo
9e460 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
9e470 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
9e480 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f . if( db->loo
9e490 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 kaside.bEnabled
9e4a0 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 && n<=db->lookas
9e4b0 69 64 65 2e 73 7a 0a 20 20 20 20 20 20 20 20 20 ide.sz.
9e4c0 26 26 20 28 70 42 75 66 20 3d 20 64 62 2d 3e 6c && (pBuf = db->l
9e4d0 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 29 21 ookaside.pFree)!
9e4e0 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e =0 ){. db->
9e4f0 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 lookaside.pFree
9e500 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 3b 0a 20 = pBuf->pNext;.
9e510 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 db->lookasi
9e520 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20 20 20 de.nOut++;.
9e530 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 if( db->lookasi
9e540 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f 6f 6b de.nOut>db->look
9e550 61 73 69 64 65 2e 6d 78 4f 75 74 20 29 7b 0a 20 aside.mxOut ){.
9e560 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 db->looka
9e570 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d side.mxOut = db-
9e580 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b >lookaside.nOut;
9e590 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
9e5a0 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 42 75 eturn (void*)pBu
9e5b0 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c f;. }. }.#el
9e5c0 73 65 0a 20 20 69 66 28 20 64 62 20 26 26 20 64 se. if( db && d
9e5d0 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
9e5e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
9e5f0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 20 . }.#endif. p
9e600 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 = sqlite3Malloc(
9e610 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20 n);. if( !p &&
9e620 64 62 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 db ){. db->ma
9e630 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a llocFailed = 1;.
9e640 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a }. return p;.
9e650 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 }../*.** Resize
9e660 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d the block of mem
9e670 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 ory pointed to b
9e680 79 20 70 20 74 6f 20 6e 20 62 79 74 65 73 2e 20 y p to n bytes.
9e690 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 69 7a 65 If the.** resize
9e6a0 20 66 61 69 6c 73 2c 20 73 65 74 20 74 68 65 20 fails, set the
9e6b0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 mallocFailed fla
9e6c0 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 g in the connect
9e6d0 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 ion object..*/.S
9e6e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
9e6f0 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 id *sqlite3DbRea
9e700 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a 64 62 lloc(sqlite3 *db
9e710 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74 20 6e , void *p, int n
9e720 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65 77 20 ){. void *pNew
9e730 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 64 = 0;. assert( d
9e740 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 b!=0 );. assert
9e750 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
9e760 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
9e770 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c );. if( db->mal
9e780 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a locFailed==0 ){.
9e790 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a if( p==0 ){.
9e7a0 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c return sql
9e7b0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
9e7c0 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 db, n);. }.
9e7d0 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 if( isLookasid
9e7e0 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 e(db, p) ){.
9e7f0 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f if( n<=db->loo
9e800 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20 20 20 kaside.sz ){.
9e810 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 return p;.
9e820 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 }. pNe
9e830 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
9e840 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 locRaw(db, n);.
9e850 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b if( pNew ){
9e860 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 . memcpy(
9e870 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c 6f 6f pNew, p, db->loo
9e880 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20 20 20 kaside.sz);.
9e890 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 sqlite3DbFre
9e8a0 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 e(db, p);.
9e8b0 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }. }else{.
9e8c0 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 pNew = sqlite
9e8d0 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 29 3b 3_realloc(p, n);
9e8e0 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 . if( !pNew
9e8f0 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e ){. db->
9e900 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
9e910 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
9e920 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 }. return pNe
9e930 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 w;.}../*.** Atte
9e940 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63 61 74 mpt to reallocat
9e950 65 20 70 2e 20 20 49 66 20 74 68 65 20 72 65 61 e p. If the rea
9e960 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
9e970 20 74 68 65 6e 20 66 72 65 65 20 70 0a 2a 2a 20 then free p.**
9e980 61 6e 64 20 73 65 74 20 74 68 65 20 6d 61 6c 6c and set the mall
9e990 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e ocFailed flag in
9e9a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f the database co
9e9b0 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c nnection..*/.SQL
9e9c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9e9d0 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c *sqlite3DbReall
9e9e0 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74 65 33 ocOrFree(sqlite3
9e9f0 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 *db, void *p, i
9ea00 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 nt n){. void *p
9ea10 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 New;. pNew = sq
9ea20 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 lite3DbRealloc(d
9ea30 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66 28 20 b, p, n);. if(
9ea40 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 73 71 6c !pNew ){. sql
9ea50 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
9ea60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
9ea70 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d pNew;.}../*.** M
9ea80 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 61 20 ake a copy of a
9ea90 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 string in memory
9eaa0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 obtained from s
9eab0 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 qliteMalloc(). T
9eac0 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f hese .** functio
9ead0 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d ns call sqlite3M
9eae0 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72 65 63 allocRaw() direc
9eaf0 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66 20 73 tly instead of s
9eb00 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 54 qliteMalloc(). T
9eb10 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61 75 73 his.** is becaus
9eb20 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20 64 65 e when memory de
9eb30 62 75 67 67 69 6e 67 20 69 73 20 74 75 72 6e 65 bugging is turne
9eb40 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77 6f 20 d on, these two
9eb50 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 0a 2a functions are .*
9eb60 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d 61 63 * called via mac
9eb70 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72 64 20 ros that record
9eb80 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 6c 65 the current file
9eb90 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62 65 72 and line number
9eba0 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72 65 61 in the.** Threa
9ebb0 64 44 61 74 61 20 73 74 72 75 63 74 75 72 65 2e dData structure.
9ebc0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
9ebd0 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
9ebe0 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65 33 DbStrDup(sqlite3
9ebf0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
9ec00 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e *z){. char *zN
9ec10 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 3b 0a ew;. size_t n;.
9ec20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 if( z==0 ){.
9ec30 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
9ec40 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 n = sqlite3Str
9ec50 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3b 0a 20 20 len30(z) + 1;.
9ec60 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 assert( (n&0x7ff
9ec70 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a fffff)==n );. z
9ec80 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
9ec90 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e allocRaw(db, (in
9eca0 74 29 6e 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 t)n);. if( zNew
9ecb0 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a ){. memcpy(z
9ecc0 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 7d 0a New, z, n);. }.
9ecd0 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d return zNew;.}
9ece0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
9ecf0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53 char *sqlite3DbS
9ed00 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 20 2a trNDup(sqlite3 *
9ed10 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a db, const char *
9ed20 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61 z, int n){. cha
9ed30 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66 28 20 7a r *zNew;. if( z
9ed40 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
9ed50 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 n 0;. }. asser
9ed60 74 28 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 t( (n&0x7fffffff
9ed70 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d )==n );. zNew =
9ed80 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
9ed90 52 61 77 28 64 62 2c 20 6e 2b 31 29 3b 0a 20 20 Raw(db, n+1);.
9eda0 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 if( zNew ){.
9edb0 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 memcpy(zNew, z,
9edc0 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e 5d 20 n);. zNew[n]
9edd0 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 = 0;. }. retur
9ede0 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a n zNew;.}../*.**
9edf0 20 43 72 65 61 74 65 20 61 20 73 74 72 69 6e 67 Create a string
9ee00 20 66 72 6f 6d 20 74 68 65 20 7a 46 72 6f 6d 61 from the zFroma
9ee10 74 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 74 t argument and t
9ee20 68 65 20 76 61 5f 6c 69 73 74 20 74 68 61 74 20 he va_list that
9ee30 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 74 6f 72 follows..** Stor
9ee40 65 20 74 68 65 20 73 74 72 69 6e 67 20 69 6e 20 e the string in
9ee50 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 memory obtained
9ee60 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f from sqliteMallo
9ee70 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a c() and make *pz
9ee80 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 .** point to tha
9ee90 74 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c t string..*/.SQL
9eea0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
9eeb0 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e sqlite3SetStrin
9eec0 67 28 63 68 61 72 20 2a 2a 70 7a 2c 20 73 71 6c g(char **pz, sql
9eed0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 ite3 *db, const
9eee0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
9eef0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
9ef00 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 p;. char *z;..
9ef10 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
9ef20 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
9ef30 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
9ef40 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
9ef50 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
9ef60 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
9ef70 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20 3d 20 , *pz);. *pz =
9ef80 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 z;.}.../*.** Thi
9ef90 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 s function must
9efa0 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 be called before
9efb0 20 65 78 69 74 69 6e 67 20 61 6e 79 20 41 50 49 exiting any API
9efc0 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e 65 2e 20 function (i.e.
9efd0 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20 63 6f .** returning co
9efe0 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 ntrol to the use
9eff0 72 29 20 74 68 61 74 20 68 61 73 20 63 61 6c 6c r) that has call
9f000 65 64 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f ed sqlite3_mallo
9f010 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f c or.** sqlite3_
9f020 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 54 realloc..**.** T
9f030 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 he returned valu
9f040 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 e is normally a
9f050 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63 6f copy of the seco
9f060 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 nd argument to t
9f070 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e his.** function.
9f080 20 48 6f 77 65 76 65 72 2c 20 69 66 20 61 20 6d However, if a m
9f090 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 alloc() failure
9f0a0 68 61 73 20 6f 63 63 75 72 72 65 64 20 73 69 6e has occurred sin
9f0b0 63 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a ce the previous.
9f0c0 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 53 51 ** invocation SQ
9f0d0 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 LITE_NOMEM is re
9f0e0 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 20 turned instead.
9f0f0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 .**.** If the fi
9f100 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 64 62 rst argument, db
9f110 2c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e , is not NULL an
9f120 64 20 61 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 d a malloc() err
9f130 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c or has occurred,
9f140 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e .** then the con
9f150 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72 2d 63 6f nection error-co
9f160 64 65 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 de (the value re
9f170 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
9f180 33 5f 65 72 72 63 6f 64 65 28 29 29 0a 2a 2a 20 3_errcode()).**
9f190 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
9f1a0 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 _NOMEM..*/.SQLIT
9f1b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
9f1c0 6c 69 74 65 33 41 70 69 45 78 69 74 28 73 71 6c lite3ApiExit(sql
9f1d0 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 20 72 63 ite3* db, int rc
9f1e0 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 ){. /* If the d
9f1f0 62 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 b handle is not
9f200 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 20 6d 75 NULL, then we mu
9f210 73 74 20 68 6f 6c 64 20 74 68 65 20 63 6f 6e 6e st hold the conn
9f220 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 0a 20 20 ection handle.
9f230 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 2e 20 4f ** mutex here. O
9f240 74 68 65 72 77 69 73 65 20 74 68 65 20 72 65 61 therwise the rea
9f250 64 20 28 61 6e 64 20 70 6f 73 73 69 62 6c 65 20 d (and possible
9f260 77 72 69 74 65 29 20 6f 66 20 64 62 2d 3e 6d 61 write) of db->ma
9f270 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20 20 2a 2a llocFailed . **
9f280 20 69 73 20 75 6e 73 61 66 65 2c 20 61 73 20 69 is unsafe, as i
9f290 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 s the call to sq
9f2a0 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a 20 20 lite3Error()..
9f2b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 */. assert( !db
9f2c0 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
9f2d0 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 x_held(db->mutex
9f2e0 29 20 29 3b 0a 20 20 69 66 28 20 64 62 20 26 26 ) );. if( db &&
9f2f0 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c (db->mallocFail
9f300 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 ed || rc==SQLITE
9f310 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20 29 7b _IOERR_NOMEM) ){
9f320 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
9f330 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d r(db, SQLITE_NOM
9f340 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e EM, 0);. db->
9f350 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 mallocFailed = 0
9f360 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 ;. rc = SQLIT
9f370 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 E_NOMEM;. }. r
9f380 65 74 75 72 6e 20 72 63 20 26 20 28 64 62 20 3f eturn rc & (db ?
9f390 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3a 20 30 db->errMask : 0
9f3a0 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a xff);.}../******
9f3b0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 ******** End of
9f3c0 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a malloc.c *******
9f3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f3f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a *******/./******
9f400 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 ******** Begin f
9f410 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a ile printf.c ***
9f420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f440 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 54 *******/./*.** T
9f450 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f 64 65 he "printf" code
9f460 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 61 that follows da
9f470 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31 39 38 tes from the 198
9f480 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e 0a 2a 0's. It is in.*
9f490 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64 6f 6d * the public dom
9f4a0 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67 69 6e ain. The origin
9f4b0 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20 al comments are
9f4c0 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20 66 6f included here fo
9f4d0 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e 65 73 r.** completenes
9f4e0 73 2e 20 20 54 68 65 79 20 61 72 65 20 76 65 72 s. They are ver
9f4f0 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 62 75 y out-of-date bu
9f500 74 20 6d 69 67 68 74 20 62 65 20 75 73 65 66 75 t might be usefu
9f510 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 73 74 6f l as.** an histo
9f520 72 69 63 61 6c 20 72 65 66 65 72 65 6e 63 65 2e rical reference.
9f530 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 22 65 Most of the "e
9f540 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 68 61 76 nhancements" hav
9f550 65 20 62 65 65 6e 20 62 61 63 6b 65 64 0a 2a 2a e been backed.**
9f560 20 6f 75 74 20 73 6f 20 74 68 61 74 20 74 68 65 out so that the
9f570 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69 functionality i
9f580 73 20 6e 6f 77 20 74 68 65 20 73 61 6d 65 20 61 s now the same a
9f590 73 20 73 74 61 6e 64 61 72 64 20 70 72 69 6e 74 s standard print
9f5a0 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a f()..**.********
9f5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
9f5f0 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c **.**.** The fol
9f600 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69 lowing modules i
9f610 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65 s an enhanced re
9f620 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68 placement for th
9f630 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f e "printf" subro
9f640 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 utines.** found
9f650 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 in the standard
9f660 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 C library. The
9f670 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63 following enhanc
9f680 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75 ements are.** su
9f690 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 pported:.**.**
9f6a0 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61 + Additiona
9f6b0 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 l functions. Th
9f6c0 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f e standard set o
9f6d0 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74 f "printf" funct
9f6e0 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ions.**
9f6f0 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c includes printf,
9f700 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74 fprintf, sprint
9f710 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72 f, vprintf, vfpr
9f720 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 intf, and.**
9f730 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20 vsprintf.
9f740 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73 This module adds
9f750 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
9f760 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
9f770 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57 * snprintf -- W
9f780 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74 orks like sprint
9f790 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 78 f, but has an ex
9f7a0 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 tra argument.**
9f7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f7c0 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69 which i
9f7d0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 s the size of th
9f7e0 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 6e e buffer written
9f7f0 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 to..**.**
9f800 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20 * mprintf
9f810 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73 -- Similar to s
9f820 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20 printf. Writes
9f830 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79 output to memory
9f840 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9f850 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 74 obt
9f860 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
9f870 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 c..**.**
9f880 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d * xprintf --
9f890 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69 Calls a functi
9f8a0 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 on to dispose of
9f8b0 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 output..**.**
9f8c0 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69 * npri
9f8d0 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 ntf -- No outpu
9f8e0 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 74 t, but returns t
9f8f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 he number of cha
9f900 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 racters.**
9f910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9f920 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68 that would h
9f930 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20 ave been output
9f940 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a by printf..**.**
9f950 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 20 * A
9f960 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20 v- version (ex:
9f970 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76 vsnprintf) of ev
9f980 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 ery function is
9f990 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 also.**
9f9a0 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a supplied..*
9f9b0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66 *.** + A f
9f9c0 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f ew extensions to
9f9d0 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 the formatting
9f9e0 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70 notation are sup
9f9f0 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 ported:.**.**
9fa00 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 22 * The "
9fa10 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72 =" flag (similar
9fa20 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20 to "-") causes
9fa30 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65 the output to be
9fa40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
9fa50 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20 be centered in
9fa60 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c the appropriatel
9fa70 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a y sized field..*
9fa80 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a *.** *
9fa90 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f The %b field o
9faa0 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65 utputs an intege
9fab0 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61 r in binary nota
9fac0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 tion..**.**
9fad0 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 20 * The %c
9fae0 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74 field now accept
9faf0 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20 s a precision.
9fb00 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f 75 The character ou
9fb10 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 tput.**
9fb20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 64 is repeated
9fb30 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
9fb40 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 63 f times the prec
9fb50 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e ision specifies.
9fb60 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 .**.**
9fb70 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c 64 * The %' field
9fb80 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20 works like %c,
9fb90 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 73 but takes as its
9fba0 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a character the.*
9fbb0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e * n
9fbc0 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 ext character of
9fbd0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 the format stri
9fbe0 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74 ng, instead of t
9fbf0 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 he next.**
9fc00 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 argument
9fc10 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
9fc20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22 printf("%.78'-"
9fc30 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e ) prints 78 min
9fc40 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 us.**
9fc50 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61 signs, the sa
9fc60 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 25 me as printf("%
9fc70 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a .78c",'-')..**.*
9fc80 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63 * + When c
9fc90 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43 ompiled using GC
9fca0 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68 C on a SPARC, th
9fcb0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72 is version of pr
9fcc0 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20 intf is.**
9fcd0 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 74 faster than t
9fce0 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74 he library print
9fcf0 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31 f for SUN OS 4.1
9fd00 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 ..**.** +
9fd10 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 All functions ar
9fd20 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e e fully reentran
9fd30 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 t..**.*/../*.**
9fd40 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 Conversion types
9fd50 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f fall into vario
9fd60 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 73 us categories as
9fd70 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a defined by the.
9fd80 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 ** following enu
9fd90 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 meration..*/.#de
9fda0 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 20 fine etRADIX
9fdb0 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20 1 /* Integer
9fdc0 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20 types. %d, %x,
9fdd0 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 %o, and so forth
9fde0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c */.#define etFL
9fdf0 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46 OAT 2 /* F
9fe00 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 loating point.
9fe10 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %f */.#define et
9fe20 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a EXP 3 /*
9fe30 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e Exponentional n
9fe40 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20 otation. %e and
9fe50 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %E */.#define et
9fe60 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a GENERIC 4 /*
9fe70 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70 Floating or exp
9fe80 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64 onential, depend
9fe90 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e ing on exponent.
9fea0 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %g */.#define e
9feb0 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f tSIZE 5 /
9fec0 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 * Return number
9fed0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 72 of characters pr
9fee0 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20 ocessed so far.
9fef0 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 %n */.#define et
9ff00 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a STRING 6 /*
9ff10 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a Strings. %s */.
9ff20 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52 #define etDYNSTR
9ff30 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d ING 7 /* Dynam
9ff40 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 ically allocated
9ff50 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a strings. %z */.
9ff60 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e #define etPERCEN
9ff70 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65 T 8 /* Perce
9ff80 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f nt symbol. %% */
9ff90 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 58 .#define etCHARX
9ffa0 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72 9 /* Char
9ffb0 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a acters. %c */./*
9ffc0 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 78 The rest are ex
9ffd0 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f tensions, not no
9ffe0 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20 rmally found in
9fff0 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 printf() */.#def
a0000 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 20 ine etSQLESCAPE
a0010 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 10 /* Strings w
a0020 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 ith '\'' doubled
a0030 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65 . %q */.#define
a0040 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 31 etSQLESCAPE2 11
a0050 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 /* Strings with
a0060 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e '\'' doubled an
a0070 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27 d enclosed in ''
a0080 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
a0090 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c NULL
a00a0 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63 pointers replac
a00b0 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 ed by SQL NULL.
a00c0 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 %Q */.#define e
a00d0 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f tTOKEN 12 /
a00e0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 * a pointer to a
a00f0 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65 Token structure
a0100 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52 */.#define etSR
a0110 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61 CLIST 13 /* a
a0120 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72 pointer to a Sr
a0130 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 cList */.#define
a0140 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34 etPOINTER 14
a0150 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65 /* The %p conve
a0160 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 rsion */.#define
a0170 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 35 etSQLESCAPE3 15
a0180 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67 /* %w -> String
a0190 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62 s with '\"' doub
a01a0 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 led */.#define e
a01b0 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f tORDINAL 16 /
a01c0 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64 * %r -> 1st, 2nd
a01d0 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e , 3rd, 4th, etc.
a01e0 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a English only *
a01f0 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e 56 /..#define etINV
a0200 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41 6e ALID 0 /* An
a0210 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63 y unrecognized c
a0220 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 2a onversion type *
a0230 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 /.../*.** An "et
a0240 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69 Byte" is an 8-bi
a0250 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65 t unsigned value
a0260 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73 ..*/.typedef uns
a0270 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 74 igned char etByt
a0280 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 e;../*.** Each b
a0290 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f uiltin conversio
a02a0 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 3a n character (ex:
a02b0 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 22 the 'd' in "%d"
a02c0 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a ) is described.*
a02d0 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 * by an instance
a02e0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
a02f0 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74 g structure.*/.t
a0300 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 74 ypedef struct et
a0310 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 _info { /* Inf
a0320 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 ormation about e
a0330 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 ach format field
a0340 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79 */. char fmtty
a0350 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f pe; /
a0360 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65 * The format fie
a0370 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a ld code letter *
a0380 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 3b /. etByte base;
a0390 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a03a0 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 64 The base for rad
a03b0 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f ix conversion */
a03c0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b . etByte flags;
a03d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
a03e0 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c ne or more of FL
a03f0 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 AG_ constants be
a0400 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 low */. etByte
a0410 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 type;
a0420 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 /* Conversion
a0430 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74 paradigm */. et
a0440 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 20 Byte charset;
a0450 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
a0460 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20 into aDigits[]
a0470 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 74 of the digits st
a0480 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65 ring */. etByte
a0490 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 prefix;
a04a0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
a04b0 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74 o aPrefix[] of t
a04c0 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e 67 he prefix string
a04d0 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a */.} et_info;..
a04e0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 /*.** Allowed va
a04f0 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f lues for et_info
a0500 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e .flags.*/.#defin
a0510 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31 e FLAG_SIGNED 1
a0520 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a0530 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e the value to con
a0540 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a vert is signed *
a0550 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49 /.#define FLAG_I
a0560 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20 NTERN 2 /*
a0570 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65 True if for inte
a0580 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f rnal use only */
a0590 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54 .#define FLAG_ST
a05a0 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41 RING 4 /* A
a05b0 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72 llow infinity pr
a05c0 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a ecision */.../*.
a05d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
a05e0 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 68 table is search
a05f0 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20 ed linearly, so
a0600 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75 it is good to pu
a0610 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 t the.** most fr
a0620 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f equently used co
a0630 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 nversion types f
a0640 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 irst..*/.static
a0650 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 69 const char aDigi
a0660 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 ts[] = "01234567
a0670 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 37 89ABCDEF01234567
a0680 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69 89abcdef";.stati
a0690 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72 c const char aPr
a06a0 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 efix[] = "-x0\00
a06b0 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e 0X0";.static con
a06c0 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e st et_info fmtin
a06d0 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64 fo[] = {. { 'd
a06e0 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 ', 10, 1, etRADI
a06f0 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c X, 0, 0 },
a0700 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34 . { 's', 0, 4
a0710 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20 , etSTRING,
a0720 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67 0, 0 },. { 'g
a0730 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 ', 0, 1, etGENE
a0740 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c RIC, 30, 0 },
a0750 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34 . { 'z', 0, 4
a0760 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20 , etDYNSTRING,
a0770 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71 0, 0 },. { 'q
a0780 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 ', 0, 4, etSQLE
a0790 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c SCAPE, 0, 0 },
a07a0 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34 . { 'Q', 0, 4
a07b0 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20 , etSQLESCAPE2,
a07c0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77 0, 0 },. { 'w
a07d0 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45 ', 0, 4, etSQLE
a07e0 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c SCAPE3, 0, 0 },
a07f0 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30 . { 'c', 0, 0
a0800 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 20 , etCHARX,
a0810 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 0, 0 },. { 'o
a0820 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49 ', 8, 0, etRADI
a0830 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c X, 0, 2 },
a0840 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30 . { 'u', 10, 0
a0850 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
a0860 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78 0, 0 },. { 'x
a0870 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49 ', 16, 0, etRADI
a0880 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c X, 16, 1 },
a0890 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30 . { 'X', 16, 0
a08a0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 , etRADIX,
a08b0 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66 0, 4 },.#ifndef
a08c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
a08d0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 ATING_POINT. {
a08e0 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46 'f', 0, 1, etF
a08f0 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30 LOAT, 0, 0
a0900 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30 },. { 'e', 0
a0910 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20 , 1, etEXP,
a0920 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 30, 0 },. {
a0930 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45 'E', 0, 1, etE
a0940 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30 XP, 14, 0
a0950 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30 },. { 'G', 0
a0960 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20 , 1, etGENERIC,
a0970 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 14, 0 },.#end
a0980 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c if. { 'i', 10,
a0990 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 1, etRADIX,
a09a0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
a09b0 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49 'n', 0, 0, etSI
a09c0 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20 ZE, 0, 0
a09d0 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c },. { '%', 0,
a09e0 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20 0, etPERCENT,
a09f0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 0, 0 },. {
a0a00 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f 'p', 16, 0, etPO
a0a10 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20 INTER, 0, 1
a0a20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72 },../* All the r
a0a30 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c 41 est have the FLA
a0a40 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65 74 G_INTERN bit set
a0a50 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66 6f and are thus fo
a0a60 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 r internal.** us
a0a70 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 e only */. { '
a0a80 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b T', 0, 2, etTOK
a0a90 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d EN, 0, 0 }
a0aa0 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c 20 ,. { 'S', 0,
a0ab0 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20 20 2, etSRCLIST,
a0ac0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 0, 0 },. { '
a0ad0 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 44 r', 10, 3, etORD
a0ae0 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d INAL, 0, 0 }
a0af0 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 ,.};../*.** If S
a0b00 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
a0b10 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 66 ING_POINT is def
a0b20 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 ined, then none
a0b30 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 of the floating
a0b40 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 point.** convers
a0b50 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a ions will work..
a0b60 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
a0b70 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f E_OMIT_FLOATING_
a0b80 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 POINT./*.** "*va
a0b90 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73 l" is a double s
a0ba0 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20 uch that 0.1 <=
a0bb0 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 *val < 10.0.** R
a0bc0 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 20 eturn the ascii
a0bd0 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61 code for the lea
a0be0 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76 ding digit of *v
a0bf0 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 al, then.** mult
a0c00 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31 iply "*val" by 1
a0c10 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 0.0 to renormali
a0c20 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c ze..**.** Exampl
a0c30 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a e:.** input:
a0c40 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34 *val = 3.14
a0c50 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 159.** outpu
a0c60 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34 t: *val = 1.4
a0c70 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 20 159 function
a0c80 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a return = '3'.**.
a0c90 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20 2a ** The counter *
a0ca0 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 cnt is increment
a0cb0 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20 41 ed each time. A
a0cc0 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78 63 fter counter exc
a0cd0 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 20 eeds.** 16 (the
a0ce0 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66 number of signif
a0cf0 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e 20 icant digits in
a0d00 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 20 a 64-bit float)
a0d10 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73 '0' is.** always
a0d20 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 returned..*/.st
a0d30 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65 74 atic char et_get
a0d40 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 digit(LONGDOUBLE
a0d50 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 _TYPE *val, int
a0d60 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 67 *cnt){. int dig
a0d70 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 it;. LONGDOUBLE
a0d80 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20 28 _TYPE d;. if( (
a0d90 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20 *cnt)++ >= 16 )
a0da0 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 69 return '0';. di
a0db0 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b git = (int)*val;
a0dc0 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20 . d = digit;.
a0dd0 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20 digit += '0';.
a0de0 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64 *val = (*val - d
a0df0 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e )*10.0;. return
a0e00 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a (char)digit;.}.
a0e10 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
a0e20 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
a0e30 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 OINT */../*.** A
a0e40 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68 ppend N space ch
a0e50 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20 aracters to the
a0e60 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66 given string buf
a0e70 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 fer..*/.static v
a0e80 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 28 oid appendSpace(
a0e90 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d StrAccum *pAccum
a0ea0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74 , int N){. stat
a0eb0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 ic const char zS
a0ec0 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20 paces[] = "
a0ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a0ee0 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69 ";. whi
a0ef0 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 le( N>=(int)size
a0f00 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b of(zSpaces)-1 ){
a0f10 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 . sqlite3StrA
a0f20 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a0f30 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65 m, zSpaces, size
a0f40 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a of(zSpaces)-1);.
a0f50 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 N -= sizeof(
a0f60 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a zSpaces)-1;. }.
a0f70 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20 if( N>0 ){.
a0f80 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a0f90 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a Append(pAccum, z
a0fa0 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a Spaces, N);. }.
a0fb0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 }../*.** On mach
a0fc0 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c ines with a smal
a0fd0 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f l stack size, yo
a0fe0 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74 u can redefine t
a0ff0 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49 he.** SQLITE_PRI
a1000 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62 NT_BUF_SIZE to b
a1010 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e e less than 350.
a1020 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
a1030 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a TE_PRINT_BUF_SIZ
a1040 45 0a 23 20 69 66 20 64 65 66 69 6e 65 64 28 53 E.# if defined(S
a1050 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 QLITE_SMALL_STAC
a1060 4b 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 K).# define SQ
a1070 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 LITE_PRINT_BUF_S
a1080 49 5a 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 20 IZE 50.# else.#
a1090 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f define SQLITE_
a10a0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33 PRINT_BUF_SIZE 3
a10b0 35 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 50.# endif.#endi
a10c0 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 53 f.#define etBUFS
a10d0 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 IZE SQLITE_PRINT
a10e0 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69 _BUF_SIZE /* Si
a10f0 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 ze of the output
a1100 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a buffer */../*.*
a1110 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72 * The root progr
a1120 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69 am. All variati
a1130 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f ons call this co
a1140 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 re..**.** INPUTS
a1150 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68 :.** func Th
a1160 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 is is a pointer
a1170 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 to a function ta
a1180 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d king three argum
a1190 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 ents.**
a11a0 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20 1. A pointer
a11b0 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61 to anything. Sa
a11c0 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22 20 me as the "arg"
a11d0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 parameter..**
a11e0 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f 2. A po
a11f0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73 inter to the lis
a1200 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 t of characters
a1210 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20 to be output.**
a1220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e (N
a1230 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69 ote, this list i
a1240 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69 s NOT null termi
a1250 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20 nated.).**
a1260 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65 3. An inte
a1270 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 ger number of ch
a1280 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f aracters to be o
a1290 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20 utput..**
a12a0 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54 (Note: T
a12b0 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74 his number might
a12c0 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a be zero.).**.**
a12d0 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20 69 arg This i
a12e0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f s the pointer to
a12f0 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20 anything which
a1300 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61 will be passed a
a1310 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 s the.**
a1320 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 first argument
a1330 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65 to "func". Use
a1340 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 72 it for whatever
a1350 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a you like..**.**
a1360 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 69 fmt This i
a1370 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 s the format str
a1380 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75 ing, as in the u
a1390 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a sual print..**.*
a13a0 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73 20 * ap This
a13b0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
a13c0 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 a list of argume
a13d0 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e nts. Same as in
a13e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 70 .** vfp
a13f0 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 rint..**.** OUTP
a1400 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 UTS:.**
a1410 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
a1420 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e e is the total n
a1430 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 umber of charact
a1440 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 ers sent to.**
a1450 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e 63 the func
a1460 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65 tion "func". Re
a1470 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72 turns -1 on a er
a1480 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 ror..**.** Note
a1490 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 69 that the order i
a14a0 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69 n which automati
a14b0 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 c variables are
a14c0 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a declared below.*
a14d0 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20 * seems to make
a14e0 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 65 a big difference
a14f0 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 in determining
a1500 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 65 how fast this be
a1510 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e ast.** will run.
a1520 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a1530 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
a1540 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63 XPrintf(. StrAc
a1550 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20 cum *pAccum,
a1560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a1570 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75 Accumulate resu
a1580 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e lts here */. in
a1590 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20 t useExtended,
a15a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a15b0 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64 /* Allow extend
a15c0 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 ed %-conversions
a15d0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
a15e0 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20 *fmt,
a15f0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d /* Form
a1600 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76 at string */. v
a1610 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 20 a_list ap
a1620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1630 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a /* arguments *
a1640 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20 /.){. int c;
a1650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1660 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63 /* Next charac
a1670 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61 ter in the forma
a1680 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 t string */. ch
a1690 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20 ar *bufpt;
a16a0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
a16b0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65 ter to the conve
a16c0 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a rsion buffer */.
a16d0 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b int precision;
a16e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
a16f0 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65 Precision of the
a1700 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a current field *
a1710 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20 /. int length;
a1720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a1730 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 * Length of the
a1740 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69 field */. int i
a1750 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dx;
a1760 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72 /* A gener
a1770 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20 al purpose loop
a1780 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
a1790 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 20 width;
a17a0 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68 /* Width
a17b0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 of the current
a17c0 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74 field */. etByt
a17d0 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 e flag_leftjusti
a17e0 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 fy; /* True if
a17f0 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65 "-" flag is pre
a1800 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a1810 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20 flag_plussign;
a1820 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a1830 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 "+" flag is pres
a1840 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 ent */. etByte
a1850 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 flag_blanksign;
a1860 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 /* True if "
a1870 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 " flag is prese
a1880 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 nt */. etByte f
a1890 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
a18a0 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23 m; /* True if "#
a18b0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e " flag is presen
a18c0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c t */. etByte fl
a18d0 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20 ag_altform2;
a18e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22 /* True if "!"
a18f0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a1900 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a1910 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20 g_zeropad;
a1920 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c /* True if fiel
a1930 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74 d width constant
a1940 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 72 starts with zer
a1950 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c o */. etByte fl
a1960 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20 ag_long;
a1970 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22 /* True if "l"
a1980 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 flag is present
a1990 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 */. etByte fla
a19a0 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 g_longlong;
a19b0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 /* True if the
a19c0 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 "ll" flag is pre
a19d0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 sent */. etByte
a19e0 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 done;
a19f0 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 /* Loop ter
a1a00 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f mination flag */
a1a10 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 . sqlite_uint64
a1a20 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a longvalue; /*
a1a30 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67 Value for integ
a1a40 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f er types */. LO
a1a50 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65 NGDOUBLE_TYPE re
a1a60 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 alvalue; /* Valu
a1a70 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73 e for real types
a1a80 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69 */. const et_i
a1a90 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20 nfo *infop;
a1aa0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 /* Pointer to t
a1ab0 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 he appropriate i
a1ac0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f nfo structure */
a1ad0 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 55 . char buf[etBU
a1ae0 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a FSIZE]; /*
a1af0 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 Conversion buff
a1b00 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65 er */. char pre
a1b10 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 fix;
a1b20 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61 /* Prefix cha
a1b30 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20 racter. "+" or
a1b40 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c "-" or " " or '\
a1b50 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 0'. */. etByte
a1b60 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 xtype = 0;
a1b70 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f /* Conversio
a1b80 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 n paradigm */.
a1b90 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20 char *zExtra;
a1ba0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
a1bb0 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 tra memory used
a1bc0 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20 for etTCLESCAPE
a1bd0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 conversions */.#
a1be0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
a1bf0 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
a1c00 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32 T. int exp, e2
a1c10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
a1c20 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65 * exponent of re
a1c30 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 al numbers */.
a1c40 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20 double rounder;
a1c50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
a1c60 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20 ed for rounding
a1c70 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 floating point v
a1c80 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74 alues */. etByt
a1c90 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20 e flag_dp;
a1ca0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
a1cb0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73 decimal point s
a1cc0 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a hould be shown *
a1cd0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f /. etByte flag_
a1ce0 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f rtz; /
a1cf0 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69 * True if traili
a1d00 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20 ng zeros should
a1d10 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 be removed */.
a1d20 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b etByte flag_exp;
a1d30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
a1d40 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70 ue to force disp
a1d50 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e lay of the expon
a1d60 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 ent */. int nsd
a1d70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a1d80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
a1d90 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 significant dig
a1da0 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a its returned */.
a1db0 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68 #endif.. length
a1dc0 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20 = 0;. bufpt =
a1dd0 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a 0;. for(; (c=(*
a1de0 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 fmt))!=0; ++fmt)
a1df0 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27 {. if( c!='%'
a1e00 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d ){. int am
a1e10 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d t;. bufpt =
a1e20 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20 (char *)fmt;.
a1e30 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20 amt = 1;.
a1e40 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b while( (c=(*+
a1e50 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63 +fmt))!='%' && c
a1e60 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 !=0 ) amt++;.
a1e70 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a1e80 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a1e90 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20 bufpt, amt);.
a1ea0 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62 if( c==0 ) b
a1eb0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
a1ec0 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 if( (c=(*++fmt))
a1ed0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
a1ee0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a1ef0 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20 nd(pAccum, "%",
a1f00 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 1);. break;
a1f10 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69 . }. /* Fi
a1f20 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67 nd out what flag
a1f30 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f s are present */
a1f40 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 . flag_leftju
a1f50 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75 stify = flag_plu
a1f60 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 ssign = flag_bla
a1f70 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66 nksign = . f
a1f80 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 lag_alternatefor
a1f90 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d m = flag_altform
a1fa0 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 2 = flag_zeropad
a1fb0 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d = 0;. done =
a1fc0 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 0;. do{.
a1fd0 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20 switch( c ){.
a1fe0 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a case '-':
a1ff0 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 flag_leftjust
a2000 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65 ify = 1; bre
a2010 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 ak;. case
a2020 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75 '+': flag_plu
a2030 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20 ssign = 1;
a2040 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
a2050 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61 case ' ': fla
a2060 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b g_blanksign = 1;
a2070 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a2080 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 case '#':
a2090 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 flag_alternate
a20a0 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61 form = 1; brea
a20b0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 k;. case
a20c0 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66 '!': flag_altf
a20d0 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20 orm2 = 1;
a20e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
a20f0 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67 case '0': flag
a2100 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20 _zeropad = 1;
a2110 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
a2120 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 default:
a2130 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20 done = 1;
a2140 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
a2150 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77 ;. }. }w
a2160 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28 hile( !done && (
a2170 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29 c=(*++fmt))!=0 )
a2180 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 ;. /* Get the
a2190 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a field width */.
a21a0 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 width = 0;.
a21b0 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b if( c=='*' ){
a21c0 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76 . width = v
a21d0 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 a_arg(ap,int);.
a21e0 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30 if( width<0
a21f0 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 ){. flag
a2200 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31 _leftjustify = 1
a2210 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20 ;. width
a2220 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20 = -width;.
a2230 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 }. c = *++f
a2240 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 mt;. }else{.
a2250 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 while( c>='
a2260 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 0' && c<='9' ){.
a2270 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 width =
a2280 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27 width*10 + c - '
a2290 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 0';. c =
a22a0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a *++fmt;. }.
a22b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69 }. if( wi
a22c0 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d dth > etBUFSIZE-
a22d0 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 10 ){. widt
a22e0 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30 h = etBUFSIZE-10
a22f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 ;. }. /* G
a2300 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e et the precision
a2310 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 */. if( c=='
a2320 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63 .' ){. prec
a2330 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 ision = 0;.
a2340 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 c = *++fmt;.
a2350 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b if( c=='*' ){
a2360 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 . precisi
a2370 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 on = va_arg(ap,i
a2380 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 nt);. if(
a2390 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 precision<0 ) p
a23a0 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63 recision = -prec
a23b0 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63 ision;. c
a23c0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 = *++fmt;.
a23d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
a23e0 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26 while( c>='0' &&
a23f0 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 c<='9' ){.
a2400 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d precision =
a2410 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20 precision*10 +
a2420 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 c - '0';.
a2430 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a2440 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
a2450 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
a2460 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31 precision = -1
a2470 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47 ;. }. /* G
a2480 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f et the conversio
a2490 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20 n type modifier
a24a0 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c */. if( c=='l
a24b0 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f ' ){. flag_
a24c0 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 long = 1;.
a24d0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 c = *++fmt;.
a24e0 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a if( c=='l' ){.
a24f0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e flag_lon
a2500 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 glong = 1;.
a2510 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 c = *++fmt;.
a2520 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a2530 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e flag_longlon
a2540 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 g = 0;. }.
a2550 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a2560 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 flag_long = flag
a2570 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 _longlong = 0;.
a2580 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63 }. /* Fetc
a2590 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79 h the info entry
a25a0 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a for the field *
a25b0 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66 /. infop = &f
a25c0 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 mtinfo[0];. x
a25d0 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 44 type = etINVALID
a25e0 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b ;. for(idx=0;
a25f0 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28 66 idx<ArraySize(f
a2600 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b mtinfo); idx++){
a2610 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d . if( c==fm
a2620 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 tinfo[idx].fmtty
a2630 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e pe ){. in
a2640 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 fop = &fmtinfo[i
a2650 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 dx];. if(
a2660 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20 useExtended ||
a2670 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 (infop->flags &
a2680 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 FLAG_INTERN)==0
a2690 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 79 ){. xty
a26a0 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65 pe = infop->type
a26b0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
a26c0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
a26d0 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 n;. }.
a26e0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
a26f0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45 }. }. zE
a2700 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 xtra = 0;...
a2710 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65 /* Limit the pre
a2720 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e cision to preven
a2730 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75 t overflowing bu
a2740 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65 f[] during conve
a2750 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 rsion */. if(
a2760 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 precision>etBUF
a2770 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f SIZE-40 && (info
a2780 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f p->flags & FLAG_
a2790 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 STRING)==0 ){.
a27a0 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 precision =
a27b0 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20 etBUFSIZE-40;.
a27c0 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 }.. /*.
a27d0 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 ** At this point
a27e0 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 , variables are
a27f0 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66 initialized as f
a2800 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 ollows:. **.
a2810 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 ** flag_alt
a2820 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20 ernateform
a2830 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 23 TRUE if a '#
a2840 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 ' is present..
a2850 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66 ** flag_altf
a2860 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 20 orm2
a2870 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 27 TRUE if a '!'
a2880 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 is present..
a2890 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 ** flag_pluss
a28a0 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 ign
a28b0 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20 TRUE if a '+'
a28c0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 is present..
a28d0 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 ** flag_leftju
a28e0 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 20 stify
a28f0 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 69 TRUE if a '-' i
a2900 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20 s present or if
a2910 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 the. **
a2920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2930 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 77 field w
a2940 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 76 idth was negativ
a2950 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 e.. ** flag
a2960 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 20 _zeropad
a2970 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 TRUE if
a2980 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e 20 the width began
a2990 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20 with 0.. **
a29a0 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20 flag_long
a29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 TRU
a29c0 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 E if the letter
a29d0 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78 'l' (ell) prefix
a29e0 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 ed. **
a29f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a00 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 the conv
a2a10 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 ersion character
a2a20 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f .. ** flag_
a2a30 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20 longlong
a2a40 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74 TRUE if t
a2a50 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28 he letter 'll' (
a2a60 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65 ell ell) prefixe
a2a70 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 d. **
a2a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2a90 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65 the conve
a2aa0 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e rsion character.
a2ab0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62 . ** flag_b
a2ac0 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20 lanksign
a2ad0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 TRUE if a
a2ae0 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a ' ' is present..
a2af0 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20 ** width
a2b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b10 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69 The specifi
a2b20 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20 ed field width.
a2b30 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20 This is. **
a2b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c al
a2b60 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 ways non-negativ
a2b70 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20 e. Zero is the
a2b80 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 default.. **
a2b90 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20 precision
a2ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 Th
a2bb0 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 63 e specified prec
a2bc0 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 ision. The defa
a2bd0 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 ult. **
a2be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2bf0 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a is -1..
a2c00 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20 ** xtype
a2c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2c20 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f The class o
a2c30 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e f the conversion
a2c40 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70 .. ** infop
a2c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2c60 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 Pointer t
a2c70 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 o the appropriat
a2c80 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20 e info struct..
a2c90 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68 */. switch
a2ca0 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20 ( xtype ){.
a2cb0 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a case etPOINTER:
a2cc0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f . flag_lo
a2cd0 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 nglong = sizeof(
a2ce0 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 char*)==sizeof(i
a2cf0 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 64);. fla
a2d00 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 g_long = sizeof(
a2d10 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c char*)==sizeof(l
a2d20 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 ong int);.
a2d30 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
a2d40 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 h into the next
a2d50 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 case */. ca
a2d60 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 se etORDINAL:.
a2d70 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 58 case etRADIX
a2d80 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e :. if( in
a2d90 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 fop->flags & FLA
a2da0 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20 G_SIGNED ){.
a2db0 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 i64 v;.
a2dc0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f if( flag_
a2dd0 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 longlong ){.
a2de0 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 v = va_a
a2df0 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 rg(ap,i64);.
a2e00 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
a2e10 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 flag_long ){.
a2e20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f v = va_
a2e30 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 arg(ap,long int)
a2e40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
a2e50 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 e{. v
a2e60 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 = va_arg(ap,int
a2e70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
a2e80 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 30 if( v<0
a2e90 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a2ea0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a longvalue = -v;.
a2eb0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 pref
a2ec0 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 ix = '-';.
a2ed0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
a2ee0 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 longvalue
a2ef0 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20 = v;.
a2f00 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 if( flag_pluss
a2f10 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 65 ign ) pre
a2f20 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 fix = '+';.
a2f30 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 else if(
a2f40 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 flag_blanksign )
a2f50 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a prefix = ' ';.
a2f60 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 else
a2f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2f80 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 prefix =
a2f90 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 0;. }.
a2fa0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a2fb0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 if( flag
a2fc0 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 _longlong ){.
a2fd0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a2fe0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 ue = va_arg(ap,u
a2ff0 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 64);. }
a3000 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f else if( flag_lo
a3010 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ng ){.
a3020 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61 longvalue = va
a3030 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64 _arg(ap,unsigned
a3040 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 long int);.
a3050 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a3060 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a3070 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 ue = va_arg(ap,u
a3080 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 nsigned int);.
a3090 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a30a0 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a prefix = 0;.
a30b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
a30c0 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d if( longvalue=
a30d0 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e =0 ) flag_altern
a30e0 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 ateform = 0;.
a30f0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65 if( flag_ze
a3100 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 69 ropad && precisi
a3110 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78 on<width-(prefix
a3120 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 !=0) ){.
a3130 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69 precision = wi
a3140 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b dth-(prefix!=0);
a3150 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a3160 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b bufpt = &buf[
a3170 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 etBUFSIZE-1];.
a3180 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d if( xtype=
a3190 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20 =etORDINAL ){.
a31a0 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 static c
a31b0 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d onst char zOrd[]
a31c0 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20 = "thstndrd";.
a31d0 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d int x =
a31e0 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65 (int)(longvalue
a31f0 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20 20 % 10);.
a3200 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c if( x>=4 || (l
a3210 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 3d ongvalue/10)%10=
a3220 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =1 ){.
a3230 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 x = 0;.
a3240 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 }. b
a3250 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d 20 uf[etBUFSIZE-3]
a3260 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 = zOrd[x*2];.
a3270 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46 buf[etBUF
a3280 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 SIZE-2] = zOrd[x
a3290 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 *2+1];.
a32a0 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20 bufpt -= 2;.
a32b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7b }. {
a32c0 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73 . regis
a32d0 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ter const char *
a32e0 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73 cset; /* Us
a32f0 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 e registers for
a3300 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 speed */.
a3310 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20 register int
a3320 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 base;.
a3330 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 5b cset = &aDigits[
a3340 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b infop->charset];
a3350 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 20 . base
a3360 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 = infop->base;.
a3370 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 20 do{
a3380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a3390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a33a0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 /* Conver
a33b0 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20 t to ascii */.
a33c0 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 *(--bu
a33d0 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 fpt) = cset[long
a33e0 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 20 value%base];.
a33f0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c longval
a3400 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 ue = longvalue/b
a3410 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ase;. }
a3420 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65 while( longvalue
a3430 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a >0 );. }.
a3440 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a3450 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 (int)(&buf[etBU
a3460 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b FSIZE-1]-bufpt);
a3470 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 . for(idx
a3480 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 =precision-lengt
a3490 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29 h; idx>0; idx--)
a34a0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d {. *(--
a34b0 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20 bufpt) = '0';
a34c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a34d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72 /* Zer
a34e0 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20 o pad */.
a34f0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 }. if( p
a3500 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70 refix ) *(--bufp
a3510 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20 t) = prefix;
a3520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
a3530 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20 d sign */.
a3540 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72 if( flag_alter
a3550 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f nateform && info
a3560 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20 p->prefix ){
a3570 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20 /* Add "0" or
a3580 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20 "0x" */.
a3590 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 const char *pr
a35a0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 e;. cha
a35b0 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70 r x;. p
a35c0 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e re = &aPrefix[in
a35d0 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20 fop->prefix];.
a35e0 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28 78 for(; (x
a35f0 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 65 =(*pre))!=0; pre
a3600 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d ++) *(--bufpt) =
a3610 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 x;. }.
a3620 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 length = (
a3630 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 int)(&buf[etBUFS
a3640 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 IZE-1]-bufpt);.
a3650 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a3660 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 54 case etFLOAT
a3670 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 45 :. case etE
a3680 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 XP:. case e
a3690 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 tGENERIC:.
a36a0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61 realvalue = va
a36b0 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b _arg(ap,double);
a36c0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
a36d0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
a36e0 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28 20 INT. if(
a36f0 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 precision<0 ) pr
a3700 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20 ecision = 6;
a3710 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66 61 /* Set defa
a3720 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f ult precision */
a3730 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 . if( pre
a3740 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 cision>etBUFSIZE
a3750 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69 6f /2-10 ) precisio
a3760 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 2d n = etBUFSIZE/2-
a3770 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 10;. if(
a3780 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b realvalue<0.0 ){
a3790 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 . realv
a37a0 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75 alue = -realvalu
a37b0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 e;. pre
a37c0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 fix = '-';.
a37d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
a37e0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 if( flag_plu
a37f0 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 20 ssign )
a3800 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20 prefix = '+';.
a3810 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 else if
a3820 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e ( flag_blanksign
a3830 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 ) prefix = '
a3840 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c ';. el
a3850 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 se
a3860 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69 prefi
a3870 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d x = 0;. }
a3880 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79 . if( xty
a3890 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 26 pe==etGENERIC &&
a38a0 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 70 precision>0 ) p
a38b0 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 recision--;.#if
a38c0 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 0. /* Rou
a38d0 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65 nding works like
a38e0 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63 6f BSD when the co
a38f0 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69 73 nstant 0.4999 is
a3900 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20 2a used. Wierd! *
a3910 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 /. for(id
a3920 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 x=precision, rou
a3930 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 78 nder=0.4999; idx
a3940 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 >0; idx--, round
a3950 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a er*=0.1);.#else.
a3960 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d 61 /* It ma
a3970 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 kes more sense t
a3980 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20 o use 0.5 */.
a3990 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 for(idx=pre
a39a0 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d cision, rounder=
a39b0 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 0.5; idx>0; idx-
a39c0 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 -, rounder*=0.1)
a39d0 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 {}.#endif.
a39e0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 46 if( xtype==etF
a39f0 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75 65 LOAT ) realvalue
a3a00 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20 += rounder;.
a3a10 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a /* Normaliz
a3a20 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 77 e realvalue to w
a3a30 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 61 ithin 10.0 > rea
a3a40 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f lvalue >= 1.0 */
a3a50 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20 30 . exp = 0
a3a60 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 ;. if( sq
a3a70 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62 lite3IsNaN((doub
a3a80 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b le)realvalue) ){
a3a90 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 . bufpt
a3aa0 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20 = "NaN";.
a3ab0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a length = 3;.
a3ac0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
a3ad0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a3ae0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65 if( realvalue
a3af0 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 >0.0 ){.
a3b00 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c while( realval
a3b10 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c ue>=1e32 && exp<
a3b20 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 =350 ){ realvalu
a3b30 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b e *= 1e-32; exp+
a3b40 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 =32; }.
a3b50 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 while( realvalu
a3b60 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33 e>=1e8 && exp<=3
a3b70 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 50 ){ realvalue
a3b80 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b *= 1e-8; exp+=8;
a3b90 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 }. whi
a3ba0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 le( realvalue>=1
a3bb0 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20 0.0 && exp<=350
a3bc0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 ){ realvalue *=
a3bd0 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 0.1; exp++; }.
a3be0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 while( r
a3bf0 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b ealvalue<1e-8 ){
a3c00 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65 realvalue *= 1e
a3c10 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 8; exp-=8; }.
a3c20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 while( re
a3c30 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72 alvalue<1.0 ){ r
a3c40 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30 ealvalue *= 10.0
a3c50 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 ; exp--; }.
a3c60 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 30 if( exp>350
a3c70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a3c80 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 20 if( prefix=='-'
a3c90 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a3ca0 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b bufpt = "-Inf";
a3cb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
a3cc0 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27 se if( prefix=='
a3cd0 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 +' ){.
a3ce0 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49 6e bufpt = "+In
a3cf0 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 f";.
a3d00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a3d10 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e bufpt = "In
a3d20 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 f";.
a3d30 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65 }. le
a3d40 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 ngth = sqlite3St
a3d50 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 rlen30(bufpt);.
a3d60 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b break
a3d70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a3d80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3d90 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 bufpt = buf;.
a3da0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 20 /*.
a3db0 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64 20 ** If the field
a3dc0 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52 49 type is etGENERI
a3dd0 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 C, then convert
a3de0 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50 0a to either etEXP.
a3df0 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 74 ** or et
a3e00 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f 70 FLOAT, as approp
a3e10 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20 2a riate.. *
a3e20 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 65 /. flag_e
a3e30 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 58 xp = xtype==etEX
a3e40 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 P;. if( x
a3e50 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b type!=etFLOAT ){
a3e60 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76 . realv
a3e70 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b alue += rounder;
a3e80 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 . if( r
a3e90 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29 ealvalue>=10.0 )
a3ea0 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 { realvalue *= 0
a3eb0 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 .1; exp++; }.
a3ec0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
a3ed0 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 f( xtype==etGENE
a3ee0 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 RIC ){.
a3ef0 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61 flag_rtz = !fla
a3f00 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b g_alternateform;
a3f10 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 . if( e
a3f20 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65 xp<-4 || exp>pre
a3f30 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 cision ){.
a3f40 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 74 xtype = et
a3f50 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d EXP;. }
a3f60 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a3f70 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 precision = pr
a3f80 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 ecision - exp;.
a3f90 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 xtype
a3fa0 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20 = etFLOAT;.
a3fb0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a3fc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
a3fd0 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 flag_rtz = 0;.
a3fe0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
a3ff0 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45 58 if( xtype==etEX
a4000 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 P ){. e
a4010 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 2 = 0;. }
a4020 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a4030 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 20 e2 = exp;.
a4040 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 20 }. nsd
a4050 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 = 0;. fla
a4060 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69 6f g_dp = (precisio
a4070 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67 n>0 ?1:0) | flag
a4080 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c _alternateform |
a4090 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a flag_altform2;.
a40a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
a40b0 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 ign in front of
a40c0 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 the number */.
a40d0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 if( prefix
a40e0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
a40f0 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 bufpt++) = prefi
a4100 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 x;. }.
a4110 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70 /* Digits p
a4120 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69 rior to the deci
a4130 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 mal point */.
a4140 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b if( e2<0 ){
a4150 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 . *(buf
a4160 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 pt++) = '0';.
a4170 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
a4180 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d for(; e2>=
a4190 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 0; e2--){.
a41a0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a41b0 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28 26 = et_getdigit(&
a41c0 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b realvalue,&nsd);
a41d0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
a41e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f }. /
a41f0 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f * The decimal po
a4200 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 int */. i
a4210 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 f( flag_dp ){.
a4220 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b *(bufpt+
a4230 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 +) = '.';.
a4240 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 }. /* "
a4250 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72 20 0" digits after
a4260 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e the decimal poin
a4270 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 65 t but before the
a4280 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a first. *
a4290 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 * significant di
a42a0 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 git of the numbe
a42b0 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 r */. for
a42c0 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 (e2++; e2<0; pre
a42d0 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b cision--, e2++){
a42e0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
a42f0 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 t( precision>0 )
a4300 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 ;. *(bu
a4310 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 fpt++) = '0';.
a4320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4330 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64 /* Significant d
a4340 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20 igits after the
a4350 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f decimal point */
a4360 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 . while(
a4370 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 (precision--)>0
a4380 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 ){. *(b
a4390 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 ufpt++) = et_get
a43a0 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65 digit(&realvalue
a43b0 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20 ,&nsd);.
a43c0 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d }. /* Rem
a43d0 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 ove trailing zer
a43e0 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69 os and the "." i
a43f0 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c f no digits foll
a4400 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 ow the "." */.
a4410 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72 if( flag_r
a4420 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b tz && flag_dp ){
a4430 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 . while
a4440 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 ( bufpt[-1]=='0'
a4450 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 ) *(--bufpt) =
a4460 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 0;. ass
a4470 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20 29 ert( bufpt>buf )
a4480 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
a4490 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 bufpt[-1]=='.' )
a44a0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 {. if
a44b0 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 ( flag_altform2
a44c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
a44d0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 *(bufpt++) = '0
a44e0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d ';. }
a44f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a4500 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d *(--bufpt) =
a4510 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 0;.
a4520 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
a4530 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
a4540 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e /* Add the "eNNN
a4550 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 " suffix */.
a4560 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70 if( flag_exp
a4570 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50 || xtype==etEXP
a4580 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 ){. *(
a4590 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 bufpt++) = aDigi
a45a0 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 ts[infop->charse
a45b0 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 t];. if
a45c0 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 ( exp<0 ){.
a45d0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b *(bufpt++
a45e0 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d ) = '-'; exp = -
a45f0 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d exp;. }
a4600 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 else{.
a4610 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 *(bufpt++) = '
a4620 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a +';. }.
a4630 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 if( ex
a4640 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20 p>=100 ){.
a4650 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 *(bufpt++)
a4660 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f 31 = (char)((exp/1
a4670 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20 20 00)+'0');
a4680 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 20 /* 100's digit
a4690 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 */. e
a46a0 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 xp %= 100;.
a46b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
a46c0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63 *(bufpt++) = (c
a46d0 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27 29 har)(exp/10+'0')
a46e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
a46f0 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 10's digit */.
a4700 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 *(bufpt
a4710 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 70 ++) = (char)(exp
a4720 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20 %10+'0');
a4730 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67 /* 1's dig
a4740 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a it */. }.
a4750 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d *bufpt =
a4760 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 0;.. /*
a4770 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 The converted nu
a4780 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d mber is in buf[]
a4790 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e and zero termin
a47a0 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e ated. Output it.
a47b0 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 . ** Note
a47c0 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 that the number
a47d0 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c is in the usual
a47e0 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 order, not reve
a47f0 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 20 rsed as with.
a4800 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 ** integer
a4810 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a conversions. */.
a4820 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4830 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75 66 (int)(bufpt-buf
a4840 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 );. bufpt
a4850 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 20 = buf;..
a4860 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 /* Special case
a4870 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a : Add leading z
a4880 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67 eros if the flag
a4890 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73 _zeropad flag is
a48a0 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20 . ** set
a48b0 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c and we are not l
a48c0 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f eft justified */
a48d0 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 . if( fla
a48e0 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c g_zeropad && !fl
a48f0 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26 ag_leftjustify &
a4900 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68 & length < width
a4910 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 ){. int
a4920 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e i;. in
a4930 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d t nPad = width -
a4940 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20 length;.
a4950 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20 for(i=width;
a4960 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 i>=nPad; i--){.
a4970 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74 bufpt
a4980 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50 [i] = bufpt[i-nP
a4990 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d ad];. }
a49a0 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 70 . i = p
a49b0 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20 refix!=0;.
a49c0 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d while( nPad-
a49d0 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d - ) bufpt[i++] =
a49e0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 '0';.
a49f0 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a length = width;.
a4a00 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
a4a10 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
a4a20 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49 5a case etSIZ
a4a30 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61 5f E:. *(va_
a4a40 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20 arg(ap,int*)) =
a4a50 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 pAccum->nChar;.
a4a60 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a4a70 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 width = 0;.
a4a80 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a4a90 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a case etPERCENT:.
a4aa0 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d buf[0] =
a4ab0 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75 '%';. bu
a4ac0 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 fpt = buf;.
a4ad0 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20 length = 1;.
a4ae0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
a4af0 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 58 case etCHARX
a4b00 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76 61 :. c = va
a4b10 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 _arg(ap,int);.
a4b20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 28 buf[0] = (
a4b30 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20 char)c;.
a4b40 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 if( precision>=0
a4b50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f ){. fo
a4b60 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 65 r(idx=1; idx<pre
a4b70 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 cision; idx++) b
a4b80 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 29 uf[idx] = (char)
a4b90 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e c;. len
a4ba0 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b gth = precision;
a4bb0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
a4bc0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 length
a4bd0 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 =1;. }.
a4be0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a4bf0 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 uf;. brea
a4c00 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 k;. case et
a4c10 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 STRING:. ca
a4c20 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a se etDYNSTRING:.
a4c30 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 bufpt =
a4c40 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 va_arg(ap,char*)
a4c50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75 ;. if( bu
a4c60 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 fpt==0 ){.
a4c70 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a bufpt = "";.
a4c80 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 }else if
a4c90 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 ( xtype==etDYNST
a4ca0 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 RING ){.
a4cb0 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 zExtra = bufpt
a4cc0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
a4cd0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f if( precisio
a4ce0 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 n>=0 ){.
a4cf0 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 for(length=0;
a4d00 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e length<precision
a4d10 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 && bufpt[length
a4d20 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 ]; length++){}.
a4d30 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
a4d40 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d length =
a4d50 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
a4d60 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20 (bufpt);.
a4d70 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b }. break
a4d80 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 ;. case etS
a4d90 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20 20 QLESCAPE:.
a4da0 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 case etSQLESCAPE
a4db0 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 2:. case et
a4dc0 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 20 SQLESCAPE3: {.
a4dd0 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 int i, j,
a4de0 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 n, isnull;.
a4df0 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 65 int needQuote
a4e00 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63 ;. char c
a4e10 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 h;. char
a4e20 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 53 q = ((xtype==etS
a4e30 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a 27 QLESCAPE3)?'"':'
a4e40 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65 \''); /* Quote
a4e50 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 character */.
a4e60 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 61 char *esca
a4e70 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 rg = va_arg(ap,c
a4e80 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 har*);. i
a4e90 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d snull = escarg==
a4ea0 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 0;. if( i
a4eb0 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 3d snull ) escarg =
a4ec0 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 (xtype==etSQLES
a4ed0 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a CAPE2 ? "NULL" :
a4ee0 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20 "(NULL)");.
a4ef0 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 for(i=n=0; (
a4f00 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 ch=escarg[i])!=0
a4f10 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; i++){.
a4f20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e if( ch==q ) n
a4f30 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ++;. }.
a4f40 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20 needQuote
a4f50 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79 = !isnull && xty
a4f60 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 pe==etSQLESCAPE2
a4f70 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69 ;. n += i
a4f80 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65 + 1 + needQuote
a4f90 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 *2;. if(
a4fa0 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20 n>etBUFSIZE ){.
a4fb0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d bufpt =
a4fc0 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65 zExtra = sqlite
a4fd0 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 3Malloc( n );.
a4fe0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70 if( bufp
a4ff0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 t==0 ){.
a5000 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c pAccum->mall
a5010 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
a5020 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
a5030 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
a5040 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
a5050 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 bufpt = b
a5060 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 uf;. }.
a5070 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20 j = 0;.
a5080 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f if( needQuo
a5090 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 te ) bufpt[j++]
a50a0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 = q;. for
a50b0 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67 (i=0; (ch=escarg
a50c0 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 [i])!=0; i++){.
a50d0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a bufpt[j
a50e0 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 ++] = ch;.
a50f0 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 if( ch==q )
a5100 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b bufpt[j++] = ch;
a5110 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
a5120 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 if( needQuote
a5130 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 ) bufpt[j++] =
a5140 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 q;. bufpt
a5150 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 [j] = 0;.
a5160 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20 length = j;.
a5170 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 63 /* The prec
a5180 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64 ision is ignored
a5190 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a 2f on %q and %Q */
a51a0 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 20 . /* if(
a51b0 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20 precision>=0 &&
a51c0 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 precision<length
a51d0 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 ) length = prec
a51e0 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 ision; */.
a51f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
a5200 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 4f . case etTO
a5210 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 54 KEN: {. T
a5220 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 oken *pToken = v
a5230 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a a_arg(ap, Token*
a5240 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
a5250 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 Token ){.
a5260 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 sqlite3StrAcc
a5270 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c umAppend(pAccum,
a5280 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54 (const char*)pT
a5290 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d oken->z, pToken-
a52a0 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 >n);. }.
a52b0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 length =
a52c0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20 width = 0;.
a52d0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
a52e0 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53 }. case etS
a52f0 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 20 RCLIST: {.
a5300 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 SrcList *pSrc
a5310 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 63 = va_arg(ap, Src
a5320 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 20 List*);.
a5330 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 61 int k = va_arg(a
a5340 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 p, int);.
a5350 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
a5360 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
a5370 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 Src->a[k];.
a5380 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 20 assert( k>=0
a5390 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20 && k<pSrc->nSrc
a53a0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
a53b0 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 Item->zDatabase
a53c0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
a53d0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 ite3StrAccumAppe
a53e0 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d nd(pAccum, pItem
a53f0 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 29 ->zDatabase, -1)
a5400 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 ;. sqli
a5410 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a5420 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31 d(pAccum, ".", 1
a5430 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
a5440 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 sqlite3StrA
a5450 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 ccumAppend(pAccu
a5460 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c m, pItem->zName,
a5470 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c 65 -1);. le
a5480 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 ngth = width = 0
a5490 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
a54a0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
a54b0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 efault: {.
a54c0 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 3d assert( xtype=
a54d0 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 =etINVALID );.
a54e0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
a54f0 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e }. }/* En
a5500 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68 d switch over th
a5510 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f e format type */
a5520 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 . /*. ** T
a5530 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 he text of the c
a5540 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69 onversion is poi
a5550 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70 nted to by "bufp
a5560 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a t" and is. **
a5570 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63 "length" charac
a5580 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20 ters long. The
a5590 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22 field width is "
a55a0 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20 width". Do.
a55b0 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20 ** the output..
a55c0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66 */. if( !f
a55d0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 lag_leftjustify
a55e0 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65 ){. registe
a55f0 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20 r int nspace;.
a5600 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64 nspace = wid
a5610 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 th-length;.
a5620 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b if( nspace>0 ){
a5630 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53 . appendS
a5640 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70 pace(pAccum, nsp
a5650 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ace);. }.
a5660 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67 }. if( leng
a5670 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 th>0 ){. sq
a5680 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
a5690 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70 end(pAccum, bufp
a56a0 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 t, length);.
a56b0 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c }. if( flag_l
a56c0 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20 eftjustify ){.
a56d0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 register int
a56e0 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e nspace;. n
a56f0 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65 space = width-le
a5700 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20 ngth;. if(
a5710 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20 nspace>0 ){.
a5720 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28 appendSpace(
a5730 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b pAccum, nspace);
a5740 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
a5750 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b if( zExtra ){
a5760 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 . sqlite3_f
a5770 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20 ree(zExtra);.
a5780 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 }. }/* End for
a5790 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66 loop over the f
a57a0 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a ormat string */.
a57b0 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63 } /* End of func
a57c0 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 tion */../*.** A
a57d0 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66 ppend N bytes of
a57e0 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20 text from z to
a57f0 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a the StrAccum obj
a5800 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ect..*/.SQLITE_P
a5810 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a5820 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e te3StrAccumAppen
a5830 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 d(StrAccum *p, c
a5840 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e onst char *z, in
a5850 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 t N){. assert(
a5860 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a z!=0 || N==0 );.
a5870 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20 if( p->tooBig
a5880 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 | p->mallocFaile
a5890 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 d ){. testcas
a58a0 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 20 e(p->tooBig);.
a58b0 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d 61 testcase(p->ma
a58c0 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 20 llocFailed);.
a58d0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 return;. }. i
a58e0 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20 f( N<0 ){. N
a58f0 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
a5900 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 0(z);. }. if(
a5910 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d N==0 || NEVER(z=
a5920 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 =0) ){. retur
a5930 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e n;. }. if( p->
a5940 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 nChar+N >= p->nA
a5950 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72 lloc ){. char
a5960 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 *zNew;. if(
a5970 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 7b !p->useMalloc ){
a5980 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 . p->tooBig
a5990 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d 20 = 1;. N =
a59a0 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e p->nAlloc - p->n
a59b0 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 Char - 1;.
a59c0 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 20 if( N<=0 ){.
a59d0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 return;.
a59e0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
a59f0 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 3d i64 szNew =
a5a00 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20 p->nChar;.
a5a10 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 3b szNew += N + 1;
a5a20 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 77 . if( szNew
a5a30 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b > p->mxAlloc ){
a5a40 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
a5a50 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 StrAccumReset(p)
a5a60 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f 6f ;. p->too
a5a70 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 Big = 1;.
a5a80 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d return;. }
a5a90 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d else{. p-
a5aa0 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 73 >nAlloc = (int)s
a5ab0 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 zNew;. }.
a5ac0 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 zNew = sqlit
a5ad0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d e3DbMallocRaw(p-
a5ae0 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 >db, p->nAlloc )
a5af0 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 77 ;. if( zNew
a5b00 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
a5b10 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 py(zNew, p->zTex
a5b20 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 t, p->nChar);.
a5b30 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 sqlite3Str
a5b40 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 AccumReset(p);.
a5b50 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 p->zText
a5b60 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 = zNew;. }e
a5b70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e lse{. p->
a5b80 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
a5b90 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
a5ba0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 3StrAccumReset(p
a5bb0 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
a5bc0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d n;. }. }
a5bd0 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 . }. memcpy(&p
a5be0 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 ->zText[p->nChar
a5bf0 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e ], z, N);. p->n
a5c00 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a Char += N;.}../*
a5c10 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 61 .** Finish off a
a5c20 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e string by makin
a5c30 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 72 g sure it is zer
a5c40 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a o-terminated..**
a5c50 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 Return a pointe
a5c60 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 69 r to the resulti
a5c70 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75 ng string. Retu
a5c80 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 rn a NULL.** poi
a5c90 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 nter if any kind
a5ca0 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 6e of error was en
a5cb0 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 countered..*/.SQ
a5cc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
a5cd0 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 r *sqlite3StrAcc
a5ce0 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 umFinish(StrAccu
a5cf0 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e m *p){. if( p->
a5d00 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e zText ){. p->
a5d10 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 zText[p->nChar]
a5d20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e = 0;. if( p->
a5d30 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e useMalloc && p->
a5d40 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 zText==p->zBase
a5d50 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 ){. p->zTex
a5d60 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c t = sqlite3DbMal
a5d70 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d locRaw(p->db, p-
a5d80 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 >nChar+1 );.
a5d90 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 if( p->zText )
a5da0 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 {. memcpy
a5db0 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 (p->zText, p->zB
a5dc0 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 ase, p->nChar+1)
a5dd0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
a5de0 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 p->malloc
a5df0 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 Failed = 1;.
a5e00 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
a5e10 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b return p->zText;
a5e20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 .}../*.** Reset
a5e30 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 69 an StrAccum stri
a5e40 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c ng. Reclaim all
a5e50 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 malloced memory
a5e60 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
a5e70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
a5e80 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74 StrAccumReset(St
a5e90 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 rAccum *p){. if
a5ea0 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a ( p->zText!=p->z
a5eb0 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 Base ){. sqli
a5ec0 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c te3DbFree(p->db,
a5ed0 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a p->zText);. }.
a5ee0 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a p->zText = 0;.
a5ef0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c }../*.** Initial
a5f00 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63 ize a string acc
a5f10 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 umulator.*/.SQLI
a5f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
a5f30 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 sqlite3StrAccumI
a5f40 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 2c nit(StrAccum *p,
a5f50 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e char *zBase, in
a5f60 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 t n, int mx){.
a5f70 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 p->zText = p->zB
a5f80 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 ase = zBase;. p
a5f90 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e ->db = 0;. p->n
a5fa0 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e Char = 0;. p->n
a5fb0 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e Alloc = n;. p->
a5fc0 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 mxAlloc = mx;.
a5fd0 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 p->useMalloc = 1
a5fe0 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 ;. p->tooBig =
a5ff0 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 0;. p->mallocFa
a6000 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a iled = 0;.}../*.
a6010 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 ** Print into me
a6020 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
a6030 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 om sqliteMalloc(
a6040 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65 ). Use the inte
a6050 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 rnal.** %-conver
a6060 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e sion extensions.
a6070 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
a6080 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 TE char *sqlite3
a6090 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33 VMPrintf(sqlite3
a60a0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 *db, const char
a60b0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 *zFormat, va_li
a60c0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a st ap){. char *
a60d0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b z;. char zBase[
a60e0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 SQLITE_PRINT_BUF
a60f0 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 _SIZE];. StrAcc
a6100 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 74 um acc;. assert
a6110 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 6c ( db!=0 );. sql
a6120 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
a6130 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 (&acc, zBase, si
a6140 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 20 zeof(zBase),.
a6150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a6160 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 db->aLimit[SQ
a6170 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 LITE_LIMIT_LENGT
a6180 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d 20 H]);. acc.db =
a6190 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 db;. sqlite3VXP
a61a0 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 7a rintf(&acc, 1, z
a61b0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a Format, ap);. z
a61c0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 = sqlite3StrAcc
a61d0 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a umFinish(&acc);.
a61e0 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 if( acc.malloc
a61f0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 62 Failed ){. db
a6200 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
a6210 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 1;. }. return
a6220 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 z;.}../*.** Pri
a6230 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f nt into memory o
a6240 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c btained from sql
a6250 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73 iteMalloc(). Us
a6260 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a e the internal.*
a6270 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 * %-conversion e
a6280 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 xtensions..*/.SQ
a6290 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 LITE_PRIVATE cha
a62a0 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 r *sqlite3MPrint
a62b0 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 f(sqlite3 *db, c
a62c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a62d0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
a62e0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a ist ap;. char *
a62f0 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 z;. va_start(ap
a6300 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 , zFormat);. z
a6310 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 = sqlite3VMPrint
a6320 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 f(db, zFormat, a
a6330 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 p);. va_end(ap)
a6340 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a ;. return z;.}.
a6350 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 ./*.** Like sqli
a6360 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 75 te3MPrintf(), bu
a6370 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 t call sqlite3Db
a6380 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 61 Free() on zStr a
a6390 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 0a fter formatting.
a63a0 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 6e ** the string an
a63b0 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 6e d before returnn
a63c0 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 69 ing. This routi
a63d0 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 ne is intended t
a63e0 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 o be used.** to
a63f0 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 69 modify an existi
a6400 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 ng string. For
a6410 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 example:.**.**
a6420 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 x = sqlite3
a6430 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 22 MPrintf(db, x, "
a6440 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 78 prefix %s suffix
a6450 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c ", x);.**.*/.SQL
a6460 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 ITE_PRIVATE char
a6470 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 *sqlite3MAppend
a6480 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 f(sqlite3 *db, c
a6490 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 74 har *zStr, const
a64a0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 char *zFormat,
a64b0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 ...){. va_list
a64c0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 ap;. char *z;.
a64d0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
a64e0 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 ormat);. z = sq
a64f0 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 lite3VMPrintf(db
a6500 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a6510 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 va_end(ap);.
a6520 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
a6530 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 72 , zStr);. retur
a6540 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 n z;.}../*.** Pr
a6550 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 int into memory
a6560 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 obtained from sq
a6570 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 lite3_malloc().
a6580 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e Omit the intern
a6590 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 al.** %-conversi
a65a0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a on extensions..*
a65b0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 /.SQLITE_API cha
a65c0 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 r *sqlite3_vmpri
a65d0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ntf(const char *
a65e0 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 zFormat, va_list
a65f0 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b ap){. char *z;
a6600 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 . char zBase[SQ
a6610 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 LITE_PRINT_BUF_S
a6620 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d IZE];. StrAccum
a6630 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 51 acc;.#ifndef SQ
a6640 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
a6650 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 IT. if( sqlite3
a6660 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 _initialize() )
a6670 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 return 0;.#endif
a6680 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 . sqlite3StrAcc
a6690 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61 umInit(&acc, zBa
a66a0 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65 se, sizeof(zBase
a66b0 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 ), SQLITE_MAX_LE
a66c0 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 33 NGTH);. sqlite3
a66d0 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30 VXPrintf(&acc, 0
a66e0 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a , zFormat, ap);.
a66f0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 z = sqlite3Str
a6700 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 AccumFinish(&acc
a6710 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d );. return z;.}
a6720 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e ../*.** Print in
a6730 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e to memory obtain
a6740 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
a6750 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d 69 malloc()(). Omi
a6760 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a t the internal.*
a6770 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 * %-conversion e
a6780 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 xtensions..*/.SQ
a6790 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
a67a0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 qlite3_mprintf(c
a67b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d onst char *zForm
a67c0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c at, ...){. va_l
a67d0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a ist ap;. char *
a67e0 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 z;.#ifndef SQLIT
a67f0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a E_OMIT_AUTOINIT.
a6800 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e if( sqlite3_in
a6810 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 itialize() ) ret
a6820 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 urn 0;.#endif.
a6830 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f va_start(ap, zFo
a6840 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c rmat);. z = sql
a6850 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46 ite3_vmprintf(zF
a6860 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 ormat, ap);. va
a6870 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 _end(ap);. retu
a6880 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 rn z;.}../*.** s
a6890 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
a68a0 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70 ) works like snp
a68b0 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 74 rintf() except t
a68c0 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 74 hat it ignores t
a68d0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f he.** current lo
a68e0 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 20 cale settings.
a68f0 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e This is importan
a6900 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 63 t for SQLite bec
a6910 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e ause we.** are n
a6920 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 61 ot able to use a
a6930 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69 "," as the deci
a6940 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61 mal point in pla
a6950 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20 ce of "." as.**
a6960 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f 6d specified by som
a6970 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 e locales..*/.SQ
a6980 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 LITE_API char *s
a6990 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
a69a0 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75 int n, char *zBu
a69b0 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a f, const char *z
a69c0 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 Format, ...){.
a69d0 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 char *z;. va_li
a69e0 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75 st ap;. StrAccu
a69f0 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c m acc;.. if( n<
a6a00 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
a6a10 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c zBuf;. }. sql
a6a20 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
a6a30 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 (&acc, zBuf, n,
a6a40 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 0);. acc.useMal
a6a50 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 loc = 0;. va_st
a6a60 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b art(ap,zFormat);
a6a70 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e . sqlite3VXPrin
a6a80 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 tf(&acc, 0, zFor
a6a90 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 mat, ap);. va_e
a6aa0 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71 nd(ap);. z = sq
a6ab0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e lite3StrAccumFin
a6ac0 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74 ish(&acc);. ret
a6ad0 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 urn z;.}..#if de
a6ae0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
a6af0 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 UG)./*.** A vers
a6b00 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 20 ion of printf()
a6b10 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 73 that understands
a6b20 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f 72 %lld. Used for
a6b30 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 debugging..** T
a6b40 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 6c he printf() buil
a6b50 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73 t into some vers
a6b60 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 ions of windows
a6b70 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 74 does not underst
a6b80 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 and %lld.** and
a6b90 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f 75 segfaults if you
a6ba0 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 20 give it a long
a6bb0 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c long int..*/.SQL
a6bc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
a6bd0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
a6be0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a ntf(const char *
a6bf0 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
a6c00 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53 va_list ap;. S
a6c10 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 63 trAccum acc;. c
a6c20 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 har zBuf[500];.
a6c30 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d sqlite3StrAccum
a6c40 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c Init(&acc, zBuf,
a6c50 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 30 sizeof(zBuf), 0
a6c60 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c );. acc.useMall
a6c70 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 oc = 0;. va_sta
a6c80 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a rt(ap,zFormat);.
a6c90 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 sqlite3VXPrint
a6ca0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d f(&acc, 0, zForm
a6cb0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e at, ap);. va_en
a6cc0 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 d(ap);. sqlite3
a6cd0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 StrAccumFinish(&
a6ce0 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 acc);. fprintf(
a6cf0 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 75 stdout,"%s", zBu
a6d00 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 64 f);. fflush(std
a6d10 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a out);.}.#endif..
a6d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a6d30 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 20 End of printf.c
a6d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a6d70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a6d80 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 6f Begin file rando
a6d90 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a m.c ************
a6da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a6dc0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
a6dd0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
a6de0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
a6df0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
a6e00 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
a6e10 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
a6e20 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
a6e30 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
a6e40 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
a6e50 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
a6e60 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
a6e70 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
a6e80 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
a6e90 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
a6ea0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
a6eb0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
a6ec0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
a6ed0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
a6ee0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
a6ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a6f30 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
a6f40 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 ile contains cod
a6f50 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 e to implement a
a6f60 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e pseudo-random n
a6f70 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 umber.** generat
a6f80 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 51 or (PRNG) for SQ
a6f90 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 Lite..**.** Rand
a6fa0 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75 om numbers are u
a6fb0 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 74 sed by some of t
a6fc0 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b he database back
a6fd0 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a ends in order.**
a6fe0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 6e to generate ran
a6ff0 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 73 dom integer keys
a7000 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 72 for tables or r
a7010 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e andom filenames.
a7020 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74 68 72 .*/.../* All thr
a7030 65 61 64 73 20 73 68 61 72 65 20 61 20 73 69 6e eads share a sin
a7040 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 gle random numbe
a7050 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a 20 r generator..**
a7060 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 This structure i
a7070 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 s the current st
a7080 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e 65 72 ate of the gener
a7090 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ator..*/.static
a70a0 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 SQLITE_WSD struc
a70b0 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 t sqlite3PrngTyp
a70c0 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 e {. unsigned c
a70d0 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20 20 20 har isInit;
a70e0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
a70f0 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
a7100 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 unsigned char i
a7110 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 , j;
a7120 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c /* State variabl
a7130 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 es */. unsigned
a7140 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20 20 20 char s[256];
a7150 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 /* State
a7160 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d 20 73 variables */.} s
a7170 71 6c 69 74 65 33 50 72 6e 67 3b 0a 0a 2f 2a 0a qlite3Prng;../*.
a7180 2a 2a 20 47 65 74 20 61 20 73 69 6e 67 6c 65 20 ** Get a single
a7190 38 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61 6c 8-bit random val
a71a0 75 65 20 66 72 6f 6d 20 74 68 65 20 52 43 34 20 ue from the RC4
a71b0 50 52 4e 47 2e 20 20 54 68 65 20 4d 75 74 65 78 PRNG. The Mutex
a71c0 0a 2a 2a 20 6d 75 73 74 20 62 65 20 68 65 6c 64 .** must be held
a71d0 20 77 68 69 6c 65 20 65 78 65 63 75 74 69 6e 67 while executing
a71e0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
a71f0 2a 0a 2a 2a 20 57 68 79 20 6e 6f 74 20 6a 75 73 *.** Why not jus
a7200 74 20 75 73 65 20 61 20 6c 69 62 72 61 72 79 20 t use a library
a7210 72 61 6e 64 6f 6d 20 67 65 6e 65 72 61 74 6f 72 random generator
a7220 20 6c 69 6b 65 20 6c 72 61 6e 64 34 38 28 29 20 like lrand48()
a7230 66 6f 72 20 74 68 69 73 3f 0a 2a 2a 20 42 65 63 for this?.** Bec
a7240 61 75 73 65 20 74 68 65 20 4f 50 5f 4e 65 77 52 ause the OP_NewR
a7250 6f 77 69 64 20 6f 70 63 6f 64 65 20 69 6e 20 74 owid opcode in t
a7260 68 65 20 56 44 42 45 20 64 65 70 65 6e 64 73 20 he VDBE depends
a7270 6f 6e 20 68 61 76 69 6e 67 20 61 20 76 65 72 79 on having a very
a7280 0a 2a 2a 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 .** good source
a7290 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 of random number
a72a0 73 2e 20 20 54 68 65 20 6c 72 61 6e 64 34 38 28 s. The lrand48(
a72b0 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 ) library functi
a72c0 6f 6e 20 6d 61 79 0a 2a 2a 20 77 65 6c 6c 20 62 on may.** well b
a72d0 65 20 67 6f 6f 64 20 65 6e 6f 75 67 68 2e 20 20 e good enough.
a72e0 42 75 74 20 6d 61 79 62 65 20 6e 6f 74 2e 20 20 But maybe not.
a72f0 4f 72 20 6d 61 79 62 65 20 6c 72 61 6e 64 34 38 Or maybe lrand48
a7300 28 29 20 68 61 73 20 73 6f 6d 65 0a 2a 2a 20 73 () has some.** s
a7310 75 62 74 6c 65 20 70 72 6f 62 6c 65 6d 73 20 6f ubtle problems o
a7320 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 n some systems t
a7330 68 61 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 hat could cause
a7340 70 72 6f 62 6c 65 6d 73 2e 20 20 49 74 20 69 73 problems. It is
a7350 20 68 61 72 64 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 hard.** to know
a7360 2e 20 20 54 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 . To minimize t
a7370 68 65 20 72 69 73 6b 20 6f 66 20 70 72 6f 62 6c he risk of probl
a7380 65 6d 73 20 64 75 65 20 74 6f 20 62 61 64 20 6c ems due to bad l
a7390 72 61 6e 64 34 38 28 29 0a 2a 2a 20 69 6d 70 6c rand48().** impl
a73a0 65 6d 65 6e 74 61 74 69 6f 6e 73 2c 20 53 51 4c ementations, SQL
a73b0 69 74 65 20 75 73 65 73 20 74 68 69 73 20 72 61 ite uses this ra
a73c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
a73d0 72 61 74 6f 72 20 62 61 73 65 64 0a 2a 2a 20 6f rator based.** o
a73e0 6e 20 52 43 34 2c 20 77 68 69 63 68 20 77 65 20 n RC4, which we
a73f0 6b 6e 6f 77 20 77 6f 72 6b 73 20 76 65 72 79 20 know works very
a7400 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 well..**.** (Lat
a7410 65 72 29 3a 20 20 41 63 74 75 61 6c 6c 79 2c 20 er): Actually,
a7420 4f 50 5f 4e 65 77 52 6f 77 69 64 20 64 6f 65 73 OP_NewRowid does
a7430 20 6e 6f 74 20 64 65 70 65 6e 64 20 6f 6e 20 61 not depend on a
a7440 20 67 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 0a good source of.
a7450 2a 2a 20 72 61 6e 64 6f 6d 6e 65 73 73 20 61 6e ** randomness an
a7460 79 20 6d 6f 72 65 2e 20 20 42 75 74 20 77 65 20 y more. But we
a7470 77 69 6c 6c 20 6c 65 61 76 65 20 74 68 69 73 20 will leave this
a7480 63 6f 64 65 20 69 6e 20 61 6c 6c 20 74 68 65 20 code in all the
a7490 73 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 same..*/.static
a74a0 75 38 20 72 61 6e 64 6f 6d 42 79 74 65 28 76 6f u8 randomByte(vo
a74b0 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 id){. unsigned
a74c0 63 68 61 72 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 char t;... /* T
a74d0 68 65 20 22 77 73 64 50 72 6e 67 22 20 6d 61 63 he "wsdPrng" mac
a74e0 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 ro will resolve
a74f0 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 to the pseudo-ra
a7500 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
a7510 72 61 74 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65 rator. ** state
a7520 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77 72 69 vector. If wri
a7530 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74 table static dat
a7540 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 a is unsupported
a7550 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74 2c 0a on the target,.
a7560 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 ** we have to
a7570 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65 locate the state
a7580 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74 vector at run-t
a7590 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72 ime. In the mor
a75a0 65 20 63 6f 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 e common. ** ca
a75b0 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c se where writabl
a75c0 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 e static data is
a75d0 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 50 supported, wsdP
a75e0 72 6e 67 20 63 61 6e 20 72 65 66 65 72 20 64 69 rng can refer di
a75f0 72 65 63 74 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 rectly. ** to t
a7600 68 65 20 22 73 71 6c 69 74 65 33 50 72 6e 67 22 he "sqlite3Prng"
a7610 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64 65 state vector de
a7620 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 20 20 clared above..
a7630 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 */.#ifdef SQLITE
a7640 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 73 74 72 75 _OMIT_WSD. stru
a7650 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 ct sqlite3PrngTy
a7660 70 65 20 2a 70 20 3d 20 26 47 4c 4f 42 41 4c 28 pe *p = &GLOBAL(
a7670 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 struct sqlite3Pr
a7680 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 ngType, sqlite3P
a7690 72 6e 67 29 3b 0a 23 20 64 65 66 69 6e 65 20 77 rng);.# define w
a76a0 73 64 50 72 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 sdPrng p[0].#els
a76b0 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72 e.# define wsdPr
a76c0 6e 67 20 73 71 6c 69 74 65 33 50 72 6e 67 0a 23 ng sqlite3Prng.#
a76d0 65 6e 64 69 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 endif... /* Ini
a76e0 74 69 61 6c 69 7a 65 20 74 68 65 20 73 74 61 74 tialize the stat
a76f0 65 20 6f 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 e of the random
a7700 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 number generator
a7710 20 6f 6e 63 65 2c 0a 20 20 2a 2a 20 74 68 65 20 once,. ** the
a7720 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 first time this
a7730 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
a7740 64 2e 20 20 54 68 65 20 73 65 65 64 20 76 61 6c d. The seed val
a7750 75 65 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 ue does. ** not
a7760 20 6e 65 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e need to contain
a7770 20 61 20 6c 6f 74 20 6f 66 20 72 61 6e 64 6f 6d a lot of random
a7780 6e 65 73 73 20 73 69 6e 63 65 20 77 65 20 61 72 ness since we ar
a7790 65 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 69 6e e not. ** tryin
a77a0 67 20 74 6f 20 64 6f 20 73 65 63 75 72 65 20 65 g to do secure e
a77b0 6e 63 72 79 70 74 69 6f 6e 20 6f 72 20 61 6e 79 ncryption or any
a77c0 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 61 74 2e thing like that.
a77d0 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 ... **. ** Not
a77e0 68 69 6e 67 20 69 6e 20 74 68 69 73 20 66 69 6c hing in this fil
a77f0 65 20 6f 72 20 61 6e 79 77 68 65 72 65 20 65 6c e or anywhere el
a7800 73 65 20 69 6e 20 53 51 4c 69 74 65 20 64 6f 65 se in SQLite doe
a7810 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 20 20 s any kind of.
a7820 2a 2a 20 65 6e 63 72 79 70 74 69 6f 6e 2e 20 20 ** encryption.
a7830 54 68 65 20 52 43 34 20 61 6c 67 6f 72 69 74 68 The RC4 algorith
a7840 6d 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 m is being used
a7850 61 73 20 61 20 50 52 4e 47 20 28 70 73 65 75 64 as a PRNG (pseud
a7860 6f 2d 72 61 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 o-random. ** nu
a7870 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 29 20 mber generator)
a7880 6e 6f 74 20 61 73 20 61 6e 20 65 6e 63 72 79 70 not as an encryp
a7890 74 69 6f 6e 20 64 65 76 69 63 65 2e 0a 20 20 2a tion device.. *
a78a0 2f 0a 20 20 69 66 28 20 21 77 73 64 50 72 6e 67 /. if( !wsdPrng
a78b0 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 69 .isInit ){. i
a78c0 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72 20 6b nt i;. char k
a78d0 5b 32 35 36 5d 3b 0a 20 20 20 20 77 73 64 50 72 [256];. wsdPr
a78e0 6e 67 2e 6a 20 3d 20 30 3b 0a 20 20 20 20 77 73 ng.j = 0;. ws
a78f0 64 50 72 6e 67 2e 69 20 3d 20 30 3b 0a 20 20 20 dPrng.i = 0;.
a7900 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d sqlite3OsRandom
a7910 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 ness(sqlite3_vfs
a7920 5f 66 69 6e 64 28 30 29 2c 20 32 35 36 2c 20 6b _find(0), 256, k
a7930 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 );. for(i=0;
a7940 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 i<256; i++){.
a7950 20 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 wsdPrng.s[i]
a7960 3d 20 28 75 38 29 69 3b 0a 20 20 20 20 7d 0a 20 = (u8)i;. }.
a7970 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 for(i=0; i<25
a7980 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 6; i++){. w
a7990 73 64 50 72 6e 67 2e 6a 20 2b 3d 20 77 73 64 50 sdPrng.j += wsdP
a79a0 72 6e 67 2e 73 5b 69 5d 20 2b 20 6b 5b 69 5d 3b rng.s[i] + k[i];
a79b0 0a 20 20 20 20 20 20 74 20 3d 20 77 73 64 50 72 . t = wsdPr
a79c0 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b ng.s[wsdPrng.j];
a79d0 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73 . wsdPrng.s
a79e0 5b 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 77 73 [wsdPrng.j] = ws
a79f0 64 50 72 6e 67 2e 73 5b 69 5d 3b 0a 20 20 20 20 dPrng.s[i];.
a7a00 20 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d wsdPrng.s[i] =
a7a10 20 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 73 t;. }. ws
a7a20 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 3d 20 31 dPrng.isInit = 1
a7a30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 ;. }.. /* Gene
a7a40 72 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 rate and return
a7a50 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 62 79 single random by
a7a60 74 65 0a 20 20 2a 2f 0a 20 20 77 73 64 50 72 6e te. */. wsdPrn
a7a70 67 2e 69 2b 2b 3b 0a 20 20 74 20 3d 20 77 73 64 g.i++;. t = wsd
a7a80 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 Prng.s[wsdPrng.i
a7a90 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b ];. wsdPrng.j +
a7aa0 3d 20 74 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 = t;. wsdPrng.s
a7ab0 5b 77 73 64 50 72 6e 67 2e 69 5d 20 3d 20 77 73 [wsdPrng.i] = ws
a7ac0 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e dPrng.s[wsdPrng.
a7ad0 6a 5d 3b 0a 20 20 77 73 64 50 72 6e 67 2e 73 5b j];. wsdPrng.s[
a7ae0 77 73 64 50 72 6e 67 2e 6a 5d 20 3d 20 74 3b 0a wsdPrng.j] = t;.
a7af0 20 20 74 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 t += wsdPrng.s
a7b00 5b 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 72 [wsdPrng.i];. r
a7b10 65 74 75 72 6e 20 77 73 64 50 72 6e 67 2e 73 5b eturn wsdPrng.s[
a7b20 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 t];.}../*.** Ret
a7b30 75 72 6e 20 4e 20 72 61 6e 64 6f 6d 20 62 79 74 urn N random byt
a7b40 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 es..*/.SQLITE_AP
a7b50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
a7b60 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c andomness(int N,
a7b70 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 void *pBuf){.
a7b80 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a unsigned char *z
a7b90 42 75 66 20 3d 20 70 42 75 66 3b 0a 23 69 66 20 Buf = pBuf;.#if
a7ba0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
a7bb0 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 E. sqlite3_mute
a7bc0 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 x *mutex = sqlit
a7bd0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
a7be0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
a7bf0 5f 50 52 4e 47 29 3b 0a 23 65 6e 64 69 66 0a 20 _PRNG);.#endif.
a7c00 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
a7c10 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 77 nter(mutex);. w
a7c20 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 hile( N-- ){.
a7c30 20 2a 28 7a 42 75 66 2b 2b 29 20 3d 20 72 61 6e *(zBuf++) = ran
a7c40 64 6f 6d 42 79 74 65 28 29 3b 0a 20 20 7d 0a 20 domByte();. }.
a7c50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
a7c60 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 7d 0a 0a eave(mutex);.}..
a7c70 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
a7c80 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 MIT_BUILTIN_TEST
a7c90 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 65 73 74 69 ./*.** For testi
a7ca0 6e 67 20 70 75 72 70 6f 73 65 73 2c 20 77 65 20 ng purposes, we
a7cb0 73 6f 6d 65 74 69 6d 65 73 20 77 61 6e 74 20 74 sometimes want t
a7cc0 6f 20 70 72 65 73 65 72 76 65 20 74 68 65 20 73 o preserve the s
a7cd0 74 61 74 65 20 6f 66 0a 2a 2a 20 50 52 4e 47 20 tate of.** PRNG
a7ce0 61 6e 64 20 72 65 73 74 6f 72 65 20 74 68 65 20 and restore the
a7cf0 50 52 4e 47 20 74 6f 20 69 74 73 20 73 61 76 65 PRNG to its save
a7d00 64 20 73 74 61 74 65 20 61 74 20 61 20 6c 61 74 d state at a lat
a7d10 65 72 20 74 69 6d 65 2c 20 6f 72 0a 2a 2a 20 74 er time, or.** t
a7d20 6f 20 72 65 73 65 74 20 74 68 65 20 50 52 4e 47 o reset the PRNG
a7d30 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 to its initial
a7d40 73 74 61 74 65 2e 20 20 54 68 65 73 65 20 72 6f state. These ro
a7d50 75 74 69 6e 65 73 20 61 63 63 6f 6d 70 6c 69 73 utines accomplis
a7d60 68 0a 2a 2a 20 74 68 6f 73 65 20 74 61 73 6b 73 h.** those tasks
a7d70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 ..**.** The sqli
a7d80 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c te3_test_control
a7d90 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c () interface cal
a7da0 6c 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 ls these routine
a7db0 73 20 74 6f 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 s to.** control
a7dc0 74 68 65 20 50 52 4e 47 2e 0a 2a 2f 0a 73 74 61 the PRNG..*/.sta
a7dd0 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 tic SQLITE_WSD s
a7de0 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e truct sqlite3Prn
a7df0 67 54 79 70 65 20 73 71 6c 69 74 65 33 53 61 76 gType sqlite3Sav
a7e00 65 64 50 72 6e 67 3b 0a 53 51 4c 49 54 45 5f 50 edPrng;.SQLITE_P
a7e10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
a7e20 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 te3PrngSaveState
a7e30 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 (void){. memcpy
a7e40 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 (. &GLOBAL(st
a7e50 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 ruct sqlite3Prng
a7e60 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 Type, sqlite3Sav
a7e70 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c edPrng),. &GL
a7e80 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 OBAL(struct sqli
a7e90 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c te3PrngType, sql
a7ea0 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 73 ite3Prng),. s
a7eb0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e izeof(sqlite3Prn
a7ec0 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 g). );.}.SQLITE
a7ed0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
a7ee0 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 lite3PrngRestore
a7ef0 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d State(void){. m
a7f00 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42 emcpy(. &GLOB
a7f10 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 AL(struct sqlite
a7f20 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 3PrngType, sqlit
a7f30 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47 4c e3Prng),. &GL
a7f40 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 OBAL(struct sqli
a7f50 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c te3PrngType, sql
a7f60 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a ite3SavedPrng),.
a7f70 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 sizeof(sqlit
a7f80 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 e3Prng). );.}.S
a7f90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
a7fa0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 id sqlite3PrngRe
a7fb0 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b 0a setState(void){.
a7fc0 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 GLOBAL(struct
a7fd0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c sqlite3PrngType,
a7fe0 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69 73 sqlite3Prng).is
a7ff0 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 Init = 0;.}.#end
a8000 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
a8010 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a T_BUILTIN_TEST *
a8020 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a /../************
a8030 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f 6d ** End of random
a8040 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
a8050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8070 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
a8080 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 ** Begin file ut
a8090 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a f.c ************
a80a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a80b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a80c0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 */./*.** 2004 Ap
a80d0 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 ril 13.**.** The
a80e0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
a80f0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
a8100 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
a8110 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
a8120 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
a8130 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
a8140 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
a8150 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
a8160 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
a8170 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
a8180 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
a8190 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
a81a0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
a81b0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
a81c0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
a81d0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
a81e0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
a81f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8230 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
a8240 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 le contains rout
a8250 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72 61 ines used to tra
a8260 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20 55 nslate between U
a8270 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31 36 TF-8, .** UTF-16
a8280 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64 20 , UTF-16BE, and
a8290 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a 20 UTF-16LE..**.**
a82a0 4e 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a Notes on UTF-8:.
a82b0 2a 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20 **.** Byte-0
a82c0 20 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65 Byte-1 Byte
a82d0 2d 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20 -2 Byte-3
a82e0 56 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78 Value.** 0xxxxx
a82f0 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xx
a8300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a8310 20 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 00000000 0000
a8320 30 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a 0000 0xxxxxxx.**
a8330 20 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78 110yyyyy 10xx
a8340 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 xxxx
a8350 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 00000
a8360 30 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78 000 00000yyy yyx
a8370 78 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a xxxxx.** 1110zz
a8380 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30 zz 10yyyyyy 10
a8390 78 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 xxxxxx
a83a0 20 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 00000000 zzzz
a83b0 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a yyyy yyxxxxxx.**
a83c0 20 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75 11110uuu 10uu
a83d0 7a 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 zzzz 10yyyyyy
a83e0 31 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75 10xxxxxx 000uu
a83f0 75 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 uuu zzzzyyyy yyx
a8400 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e xxxxx.**.**.** N
a8410 6f 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20 otes on UTF-16:
a8420 20 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75 (with wwww+1==u
a8430 75 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 uuuu).**.**
a8440 20 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20 Word-0
a8450 20 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20 Word-1
a8460 20 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 Value.**
a8470 31 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79 110110ww wwzzzzy
a8480 79 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78 y 110111yy yyx
a8490 78 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75 xxxxx 000uuuu
a84a0 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 u zzzzyyyy yyxxx
a84b0 78 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 xxx.** zzzzyyyy
a84c0 20 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20 yyxxxxxx
a84d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a84e0 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 00000000 zzzzyy
a84f0 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a yy yyxxxxxx.**.*
a8500 2a 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65 *.** BOM or Byte
a8510 20 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 Order Mark:.**
a8520 20 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20 0xff 0xfe
a8530 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74 little-endian ut
a8540 66 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 f-16 follows.**
a8550 20 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20 0xfe 0xff
a8560 62 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 big-endian utf-1
a8570 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 6 follows.**.*/.
a8580 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a8590 49 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e Include vdbeInt.
a85a0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 h in the middle
a85b0 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a of utf.c *******
a85c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
a85d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
a85e0 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49 Begin file vdbeI
a85f0 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nt.h ***********
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 2f 0a **************/.
a8620 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 /*.** 2003 Septe
a8630 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 mber 6.**.** The
a8640 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
a8650 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
a8660 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
a8670 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
a8680 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
a8690 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
a86a0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
a86b0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
a86c0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
a86d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
a86e0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
a86f0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
a8700 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
a8710 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
a8720 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
a8730 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
a8740 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
a8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
a8790 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 *****.** This is
a87a0 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 the header file
a87b0 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e for information
a87c0 20 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65 that is private
a87d0 20 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e to the.** VDBE.
a87e0 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 This informati
a87f0 6f 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62 on used to all b
a8800 65 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 e at the top of
a8810 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f the single.** so
a8820 75 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22 urce code file "
a8830 76 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74 vdbe.c". When t
a8840 68 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20 hat file became
a8850 74 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a too big (over.**
a8860 20 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 6000 lines long
a8870 29 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75 ) it was split u
a8880 70 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73 p into several s
a8890 6d 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64 maller files and
a88a0 0a 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20 .** this header
a88b0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 information was
a88c0 66 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2f factored out..*/
a88d0 0a 23 69 66 6e 64 65 66 20 5f 56 44 42 45 49 4e .#ifndef _VDBEIN
a88e0 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 56 44 T_H_.#define _VD
a88f0 42 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 BEINT_H_../*.**
a8900 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65 SQL is translate
a8910 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 d into a sequenc
a8920 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e e of instruction
a8930 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75 s to be.** execu
a8940 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c ted by a virtual
a8950 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20 machine. Each
a8960 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 instruction is a
a8970 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 n instance.** of
a8980 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 the following s
a8990 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 tructure..*/.typ
a89a0 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 edef struct Vdbe
a89b0 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f Op Op;../*.** Bo
a89c0 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a olean values.*/.
a89d0 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 typedef unsigned
a89e0 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a char Bool;../*.
a89f0 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 ** A cursor is a
a8a00 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20 pointer into a
a8a10 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74 single BTree wit
a8a20 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 hin a database f
a8a30 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 ile..** The curs
a8a40 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61 or can seek to a
a8a50 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74 BTree entry wit
a8a60 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b h a particular k
a8a70 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f ey, or.** loop o
a8a80 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 ver all entries
a8a90 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59 of the Btree. Y
a8aa0 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65 ou can also inse
a8ab0 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20 rt new BTree.**
a8ac0 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69 entries or retri
a8ad0 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64 eve the key or d
a8ae0 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 ata from the ent
a8af0 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 ry that the curs
a8b00 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74 or.** is current
a8b10 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a ly pointing to..
a8b20 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 ** .** Every cur
a8b30 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72 sor that the vir
a8b40 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 tual machine has
a8b50 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65 open is represe
a8b60 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e nted by an.** in
a8b70 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
a8b80 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
a8b90 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
a8ba0 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 69 VdbeCursor.isTri
a8bb0 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 20 ggerRow flag is
a8bc0 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61 set it means tha
a8bd0 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 t this cursor is
a8be0 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 6e .** really a sin
a8bf0 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 70 gle row that rep
a8c00 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 20 resents the NEW
a8c10 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61 or OLD pseudo-ta
a8c20 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 ble of.** a row
a8c30 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 61 trigger. The da
a8c40 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 69 ta for the row i
a8c50 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65 s stored in Vdbe
a8c60 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64 Cursor.pData and
a8c70 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73 .** the rowid is
a8c80 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 69 in VdbeCursor.i
a8c90 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 Key..*/.struct V
a8ca0 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 dbeCursor {. Bt
a8cb0 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b Cursor *pCursor;
a8cc0 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f /* The curso
a8cd0 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74 r structure of t
a8ce0 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20 he backend */.
a8cf0 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 int iDb;
a8d00 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
a8d10 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73 f cursor databas
a8d20 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28 e in db->aDb[] (
a8d30 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20 or -1) */. i64
a8d40 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20 lastRowid;
a8d50 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20 /* Last rowid
a8d60 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e from a Next or N
a8d70 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e extIdx operation
a8d80 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f 65 */. Bool zeroe
a8d90 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 d; /* T
a8da0 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f 75 rue if zeroed ou
a8db0 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72 20 t and ready for
a8dc0 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c 20 reuse */. Bool
a8dd0 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20 20 rowidIsValid;
a8de0 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73 74 /* True if last
a8df0 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20 2a Rowid is valid *
a8e00 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73 74 /. Bool atFirst
a8e10 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 ; /* Tru
a8e20 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f e if pointing to
a8e30 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a first entry */.
a8e40 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f 6d Bool useRandom
a8e50 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65 72 Rowid; /* Gener
a8e60 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e ate new record n
a8e70 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e 64 umbers semi-rand
a8e80 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 6e omly */. Bool n
a8e90 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20 20 ullRow;
a8ea0 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74 /* True if point
a8eb0 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69 74 ing to a row wit
a8ec0 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 42 h no data */. B
a8ed0 6f 6f 6c 20 64 65 66 65 72 72 65 64 4d 6f 76 65 ool deferredMove
a8ee0 74 6f 3b 20 20 2f 2a 20 41 20 63 61 6c 6c 20 74 to; /* A call t
a8ef0 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f o sqlite3BtreeMo
a8f00 76 65 74 6f 28 29 20 69 73 20 6e 65 65 64 65 64 veto() is needed
a8f10 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 54 61 62 */. Bool isTab
a8f20 6c 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 le; /* T
a8f30 72 75 65 20 69 66 20 61 20 74 61 62 6c 65 20 72 rue if a table r
a8f40 65 71 75 69 72 69 6e 67 20 69 6e 74 65 67 65 72 equiring integer
a8f50 20 6b 65 79 73 20 2a 2f 0a 20 20 42 6f 6f 6c 20 keys */. Bool
a8f60 69 73 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 isIndex;
a8f70 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 20 69 /* True if an i
a8f80 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ndex containing
a8f90 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f 20 64 keys only - no d
a8fa0 61 74 61 20 2a 2f 0a 20 20 69 36 34 20 6d 6f 76 ata */. i64 mov
a8fb0 65 74 6f 54 61 72 67 65 74 3b 20 20 20 20 20 2f etoTarget; /
a8fc0 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 * Argument to th
a8fd0 65 20 64 65 66 65 72 72 65 64 20 73 71 6c 69 74 e deferred sqlit
a8fe0 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 e3BtreeMoveto()
a8ff0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b */. Btree *pBt;
a9000 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 /* Se
a9010 70 61 72 61 74 65 20 66 69 6c 65 20 68 6f 6c 64 parate file hold
a9020 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 ing temporary ta
a9030 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 70 73 65 ble */. int pse
a9040 75 64 6f 54 61 62 6c 65 52 65 67 3b 20 20 20 2f udoTableReg; /
a9050 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 * Register holdi
a9060 6e 67 20 70 73 65 75 64 6f 74 61 62 6c 65 20 63 ng pseudotable c
a9070 6f 6e 74 65 6e 74 2e 20 2a 2f 0a 20 20 4b 65 79 ontent. */. Key
a9080 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 Info *pKeyInfo;
a9090 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 /* Info about
a90a0 20 69 6e 64 65 78 20 6b 65 79 73 20 6e 65 65 64 index keys need
a90b0 65 64 20 62 79 20 69 6e 64 65 78 20 63 75 72 73 ed by index curs
a90c0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 ors */. int nFi
a90d0 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f eld; /
a90e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * Number of fiel
a90f0 64 73 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ds in the header
a9100 20 2a 2f 0a 20 20 69 36 34 20 73 65 71 43 6f 75 */. i64 seqCou
a9110 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 nt; /* S
a9120 65 71 75 65 6e 63 65 20 63 6f 75 6e 74 65 72 20 equence counter
a9130 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 */. sqlite3_vta
a9140 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 b_cursor *pVtabC
a9150 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65 20 63 ursor; /* The c
a9160 75 72 73 6f 72 20 66 6f 72 20 61 20 76 69 72 74 ursor for a virt
a9170 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 ual table */. c
a9180 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 onst sqlite3_mod
a9190 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 ule *pModule;
a91a0 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 66 6f 72 20 /* Module for
a91b0 63 75 72 73 6f 72 20 70 56 74 61 62 43 75 72 73 cursor pVtabCurs
a91c0 6f 72 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 73 75 or */.. /* Resu
a91d0 6c 74 20 6f 66 20 6c 61 73 74 20 73 71 6c 69 74 lt of last sqlit
a91e0 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 e3BtreeMoveto()
a91f0 64 6f 6e 65 20 62 79 20 61 6e 20 4f 50 5f 4e 6f done by an OP_No
a9200 74 45 78 69 73 74 73 20 6f 72 20 0a 20 20 2a 2a tExists or . **
a9210 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 OP_IsUnique opc
a9220 6f 64 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 ode on this curs
a9230 6f 72 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 or. */. int see
a9240 6b 52 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 43 kResult;.. /* C
a9250 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f ached informatio
a9260 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65 61 64 n about the head
a9270 65 72 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 er for the data
a9280 72 65 63 6f 72 64 20 74 68 61 74 20 74 68 65 0a record that the.
a9290 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73 20 63 ** cursor is c
a92a0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e urrently pointin
a92b0 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61 6c 69 g to. Only vali
a92c0 64 20 69 66 20 63 61 63 68 65 53 74 61 74 75 73 d if cacheStatus
a92d0 20 6d 61 74 63 68 65 73 0a 20 20 2a 2a 20 56 64 matches. ** Vd
a92e0 62 65 2e 63 61 63 68 65 43 74 72 2e 20 20 56 64 be.cacheCtr. Vd
a92f0 62 65 2e 63 61 63 68 65 43 74 72 20 77 69 6c 6c be.cacheCtr will
a9300 20 6e 65 76 65 72 20 74 61 6b 65 20 6f 6e 20 74 never take on t
a9310 68 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a he value of. **
a9320 20 43 41 43 48 45 5f 53 54 41 4c 45 20 61 6e 64 CACHE_STALE and
a9330 20 73 6f 20 73 65 74 74 69 6e 67 20 63 61 63 68 so setting cach
a9340 65 53 74 61 74 75 73 3d 43 41 43 48 45 5f 53 54 eStatus=CACHE_ST
a9350 41 4c 45 20 67 75 61 72 61 6e 74 65 65 73 20 74 ALE guarantees t
a9360 68 61 74 0a 20 20 2a 2a 20 74 68 65 20 63 61 63 hat. ** the cac
a9370 68 65 20 69 73 20 6f 75 74 20 6f 66 20 64 61 74 he is out of dat
a9380 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 52 6f e.. **. ** aRo
a9390 77 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f w might point to
a93a0 20 28 65 70 68 65 6d 65 72 61 6c 29 20 64 61 74 (ephemeral) dat
a93b0 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e a for the curren
a93c0 74 20 72 6f 77 2c 20 6f 72 20 69 74 20 6d 69 67 t row, or it mig
a93d0 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55 4c 4c 2e ht. ** be NULL.
a93e0 0a 20 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68 . */. u32 cach
a93f0 65 53 74 61 74 75 73 3b 20 20 20 20 20 20 2f 2a eStatus; /*
a9400 20 43 61 63 68 65 20 69 73 20 76 61 6c 69 64 20 Cache is valid
a9410 69 66 20 74 68 69 73 20 6d 61 74 63 68 65 73 20 if this matches
a9420 56 64 62 65 2e 63 61 63 68 65 43 74 72 20 2a 2f Vdbe.cacheCtr */
a9430 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61 64 53 69 . int payloadSi
a9440 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 ze; /* Tota
a9450 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 l number of byte
a9460 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 s in the record
a9470 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79 70 65 3b */. u32 *aType;
a9480 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 79 /* Ty
a9490 70 65 20 76 61 6c 75 65 73 20 66 6f 72 20 61 6c pe values for al
a94a0 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 l entries in the
a94b0 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 record */. u32
a94c0 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 *aOffset;
a94d0 20 20 20 2f 2a 20 43 61 63 68 65 64 20 6f 66 66 /* Cached off
a94e0 73 65 74 73 20 74 6f 20 74 68 65 20 73 74 61 72 sets to the star
a94f0 74 20 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e t of each column
a9500 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a s data */. u8 *
a9510 61 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 aRow;
a9520 20 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 74 68 /* Data for th
a9530 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 69 e current row, i
a9540 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20 70 61 67 f all on one pag
a9550 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 e */.};.typedef
a9560 73 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f struct VdbeCurso
a9570 72 20 56 64 62 65 43 75 72 73 6f 72 3b 0a 0a 2f r VdbeCursor;../
a9580 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 75 62 2d *.** When a sub-
a9590 70 72 6f 67 72 61 6d 20 69 73 20 65 78 65 63 75 program is execu
a95a0 74 65 64 20 28 4f 50 5f 50 72 6f 67 72 61 6d 29 ted (OP_Program)
a95b0 2c 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66 , a structure of
a95c0 20 74 68 69 73 20 74 79 70 65 0a 2a 2a 20 69 73 this type.** is
a95d0 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 73 74 allocated to st
a95e0 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ore the current
a95f0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 72 6f value of the pro
a9600 67 72 61 6d 20 63 6f 75 6e 74 65 72 2c 20 61 73 gram counter, as
a9610 0a 2a 2a 20 77 65 6c 6c 20 61 73 20 74 68 65 20 .** well as the
a9620 63 75 72 72 65 6e 74 20 6d 65 6d 6f 72 79 20 63 current memory c
a9630 65 6c 6c 20 61 72 72 61 79 20 61 6e 64 20 76 61 ell array and va
a9640 72 69 6f 75 73 20 6f 74 68 65 72 20 66 72 61 6d rious other fram
a9650 65 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 76 61 e specific.** va
a9660 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 lues stored in t
a9670 68 65 20 56 64 62 65 20 73 74 72 75 63 74 2e 20 he Vdbe struct.
a9680 57 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f When the sub-pro
a9690 67 72 61 6d 20 69 73 20 66 69 6e 69 73 68 65 64 gram is finished
a96a0 2c 20 0a 2a 2a 20 74 68 65 73 65 20 76 61 6c 75 , .** these valu
a96b0 65 73 20 61 72 65 20 63 6f 70 69 65 64 20 62 61 es are copied ba
a96c0 63 6b 20 74 6f 20 74 68 65 20 56 64 62 65 20 66 ck to the Vdbe f
a96d0 72 6f 6d 20 74 68 65 20 56 64 62 65 46 72 61 6d rom the VdbeFram
a96e0 65 20 73 74 72 75 63 74 75 72 65 2c 0a 2a 2a 20 e structure,.**
a96f0 72 65 73 74 6f 72 69 6e 67 20 74 68 65 20 73 74 restoring the st
a9700 61 74 65 20 6f 66 20 74 68 65 20 56 4d 20 74 6f ate of the VM to
a9710 20 61 73 20 69 74 20 77 61 73 20 62 65 66 6f 72 as it was befor
a9720 65 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 e the sub-progra
a9730 6d 0a 2a 2a 20 62 65 67 61 6e 20 65 78 65 63 75 m.** began execu
a9740 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 72 61 6d ting..**.** Fram
a9750 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e es are stored in
a9760 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 a linked list h
a9770 65 61 64 65 64 20 61 74 20 56 64 62 65 2e 70 50 eaded at Vdbe.pP
a9780 61 72 65 6e 74 2e 20 56 64 62 65 2e 70 50 61 72 arent. Vdbe.pPar
a9790 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70 61 ent.** is the pa
a97a0 72 65 6e 74 20 6f 66 20 74 68 65 20 63 75 72 72 rent of the curr
a97b0 65 6e 74 20 66 72 61 6d 65 2c 20 6f 72 20 7a 65 ent frame, or ze
a97c0 72 6f 20 69 66 20 74 68 65 20 63 75 72 72 65 6e ro if the curren
a97d0 74 20 66 72 61 6d 65 0a 2a 2a 20 69 73 20 74 68 t frame.** is th
a97e0 65 20 6d 61 69 6e 20 56 64 62 65 20 70 72 6f 67 e main Vdbe prog
a97f0 72 61 6d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ram..*/.typedef
a9800 73 74 72 75 63 74 20 56 64 62 65 46 72 61 6d 65 struct VdbeFrame
a9810 20 56 64 62 65 46 72 61 6d 65 3b 0a 73 74 72 75 VdbeFrame;.stru
a9820 63 74 20 56 64 62 65 46 72 61 6d 65 20 7b 0a 20 ct VdbeFrame {.
a9830 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 Vdbe *v;
a9840 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 74 /* VM t
a9850 68 69 73 20 66 72 61 6d 65 20 62 65 6c 6f 6e 67 his frame belong
a9860 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 70 63 s to */. int pc
a9870 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
a9880 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 43 6f 75 /* Program Cou
a9890 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f nter */. Op *aO
a98a0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
a98b0 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6e 73 /* Program ins
a98c0 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 tructions */. i
a98d0 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20 20 nt nOp;
a98e0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f /* Size o
a98f0 66 20 61 4f 70 20 61 72 72 61 79 20 2a 2f 0a 20 f aOp array */.
a9900 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 Mem *aMem;
a9910 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 /* Arra
a9920 79 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c y of memory cell
a9930 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b s */. int nMem;
a9940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
a9950 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
a9960 69 65 73 20 69 6e 20 61 4d 65 6d 20 2a 2f 0a 20 ies in aMem */.
a9970 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 VdbeCursor **ap
a9980 43 73 72 3b 20 20 20 20 20 2f 2a 20 45 6c 65 6d Csr; /* Elem
a9990 65 6e 74 20 6f 66 20 56 64 62 65 20 63 75 72 73 ent of Vdbe curs
a99a0 6f 72 73 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75 ors */. u16 nCu
a99b0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rsor;
a99c0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e /* Number of en
a99d0 74 72 69 65 73 20 69 6e 20 61 70 43 73 72 20 2a tries in apCsr *
a99e0 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e 3b /. void *token;
a99f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
a9a00 6f 70 79 20 6f 66 20 53 75 62 50 72 6f 67 72 61 opy of SubProgra
a9a10 6d 2e 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 m.token */. int
a9a20 20 6e 43 68 69 6c 64 4d 65 6d 3b 20 20 20 20 20 nChildMem;
a9a30 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
a9a40 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 f memory cells f
a9a50 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 20 2a or child frame *
a9a60 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 43 73 /. int nChildCs
a9a70 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e r; /* N
a9a80 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 umber of cursors
a9a90 20 66 6f 72 20 63 68 69 6c 64 20 66 72 61 6d 65 for child frame
a9aa0 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f */. i64 lastRo
a9ab0 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a wid; /*
a9ac0 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 Last insert row
a9ad0 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74 id (sqlite3.last
a9ae0 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20 Rowid) */. int
a9af0 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 nChange;
a9b00 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 /* Statement
a9b10 20 63 68 61 6e 67 65 73 20 28 56 64 62 65 2e 6e changes (Vdbe.n
a9b20 43 68 61 6e 67 65 73 29 20 20 20 20 20 2a 2f 0a Changes) */.
a9b30 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 50 61 VdbeFrame *pPa
a9b40 72 65 6e 74 3b 20 20 20 20 20 2f 2a 20 50 61 72 rent; /* Par
a9b50 65 6e 74 20 6f 66 20 74 68 69 73 20 66 72 61 6d ent of this fram
a9b60 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 e */.};..#define
a9b70 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 VdbeFrameMem(p)
a9b80 20 28 28 4d 65 6d 20 2a 29 26 28 28 75 38 20 2a ((Mem *)&((u8 *
a9b90 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65 6f )p)[ROUND8(sizeo
a9ba0 66 28 56 64 62 65 46 72 61 6d 65 29 29 5d 29 0a f(VdbeFrame))]).
a9bb0 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20 66 ./*.** A value f
a9bc0 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63 61 or VdbeCursor.ca
a9bd0 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d 65 cheValid that me
a9be0 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69 73 ans the cache is
a9bf0 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64 2e always invalid.
a9c00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43 48 .*/.#define CACH
a9c10 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a 2a E_STALE 0../*.**
a9c20 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 Internally, the
a9c30 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74 65 vdbe manipulate
a9c40 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51 4c s nearly all SQL
a9c50 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a 2a values as Mem.*
a9c60 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45 61 * structures. Ea
a9c70 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d 61 ch Mem struct ma
a9c80 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c 65 y cache multiple
a9c90 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
a9ca0 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e 74 (string,.** int
a9cb0 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74 68 eger etc.) of th
a9cc0 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 41 e same value. A
a9cd0 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65 72 value (and ther
a9ce0 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63 74 efore Mem struct
a9cf0 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65 20 ure).** has the
a9d00 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65 72 following proper
a9d10 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63 68 ties:.**.** Each
a9d20 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61 6e value has a man
a9d30 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65 20 ifest type. The
a9d40 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f 66 manifest type of
a9d50 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 the value store
a9d60 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73 74 d.** in a Mem st
a9d70 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65 64 ruct is returned
a9d80 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65 28 by the MemType(
a9d90 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68 65 Mem*) macro. The
a9da0 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 type is.** one
a9db0 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 of SQLITE_NULL,
a9dc0 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 SQLITE_INTEGER,
a9dd0 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51 4c SQLITE_REAL, SQL
a9de0 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20 53 ITE_TEXT or.** S
a9df0 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a 73 QLITE_BLOB..*/.s
a9e00 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75 6e truct Mem {. un
a9e10 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69 3b ion {. i64 i;
a9e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a9e30 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 Integer value.
a9e40 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f */. int nZero
a9e50 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 ; /* Us
a9e60 65 64 20 77 68 65 6e 20 62 69 74 20 4d 45 4d 5f ed when bit MEM_
a9e70 5a 65 72 6f 20 69 73 20 73 65 74 20 69 6e 20 66 Zero is set in f
a9e80 6c 61 67 73 20 2a 2f 0a 20 20 20 20 46 75 6e 63 lags */. Func
a9e90 44 65 66 20 2a 70 44 65 66 3b 20 20 20 20 20 20 Def *pDef;
a9ea0 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77 68 65 /* Used only whe
a9eb0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 n flags==MEM_Agg
a9ec0 20 2a 2f 0a 20 20 20 20 52 6f 77 53 65 74 20 2a */. RowSet *
a9ed0 70 52 6f 77 53 65 74 3b 20 20 20 20 2f 2a 20 55 pRowSet; /* U
a9ee0 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c sed only when fl
a9ef0 61 67 73 3d 3d 4d 45 4d 5f 52 6f 77 53 65 74 20 ags==MEM_RowSet
a9f00 2a 2f 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 */. VdbeFrame
a9f10 20 2a 70 46 72 61 6d 65 3b 20 20 2f 2a 20 55 73 *pFrame; /* Us
a9f20 65 64 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d ed when flags==M
a9f30 45 4d 5f 46 72 61 6d 65 20 2a 2f 0a 20 20 7d 20 EM_Frame */. }
a9f40 75 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 20 20 u;. double r;
a9f50 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 6c /* Real
a9f60 20 76 61 6c 75 65 20 2a 2f 0a 20 20 73 71 6c 69 value */. sqli
a9f70 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 te3 *db;
a9f80 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 /* The associate
a9f90 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 d database conne
a9fa0 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 ction */. char
a9fb0 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f *z; /
a9fc0 2a 20 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 * String or BLOB
a9fd0 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74 20 value */. int
a9fe0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
a9ff0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 /* Number of cha
aa000 72 61 63 74 65 72 73 20 69 6e 20 73 74 72 69 6e racters in strin
aa010 67 20 76 61 6c 75 65 2c 20 65 78 63 6c 75 64 69 g value, excludi
aa020 6e 67 20 27 5c 30 27 20 2a 2f 0a 20 20 75 31 36 ng '\0' */. u16
aa030 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 flags;
aa040 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69 6e 61 /* Some combina
aa050 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f 4e 75 6c 6c tion of MEM_Null
aa060 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d 45 4d 5f 44 , MEM_Str, MEM_D
aa070 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 yn, etc. */. u8
aa080 20 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 type;
aa090 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 51 4c 49 /* One of SQLI
aa0a0 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f TE_NULL, SQLITE_
aa0b0 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 49 4e 54 TEXT, SQLITE_INT
aa0c0 45 47 45 52 2c 20 65 74 63 20 2a 2f 0a 20 20 75 EGER, etc */. u
aa0d0 38 20 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 8 enc;
aa0e0 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 /* SQLITE_UTF
aa0f0 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 8, SQLITE_UTF16B
aa100 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c E, SQLITE_UTF16L
aa110 45 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 E */. void (*xD
aa120 65 6c 29 28 76 6f 69 64 20 2a 29 3b 20 20 2f 2a el)(void *); /*
aa130 20 49 66 20 6e 6f 74 20 6e 75 6c 6c 2c 20 63 61 If not null, ca
aa140 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ll this function
aa150 20 74 6f 20 64 65 6c 65 74 65 20 4d 65 6d 2e 7a to delete Mem.z
aa160 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c */. char *zMal
aa170 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 44 79 6e loc; /* Dyn
aa180 61 6d 69 63 20 62 75 66 66 65 72 20 61 6c 6c 6f amic buffer allo
aa190 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 cated by sqlite3
aa1a0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d 3b 0a _malloc() */.};.
aa1b0 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 ./* One or more
aa1c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
aa1d0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 74 flags are set t
aa1e0 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 76 o indicate the v
aa1f0 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65 70 72 65 73 alidOK.** repres
aa200 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 entations of the
aa210 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
aa220 20 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 2e the Mem struct.
aa230 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 .**.** If the ME
aa240 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20 69 73 20 73 M_Null flag is s
aa250 65 74 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c et, then the val
aa260 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c ue is an SQL NUL
aa270 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20 4e 6f 20 6f L value..** No o
aa280 74 68 65 72 20 66 6c 61 67 73 20 6d 61 79 20 62 ther flags may b
aa290 65 20 73 65 74 20 69 6e 20 74 68 69 73 20 63 61 e set in this ca
aa2a0 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 se..**.** If the
aa2b0 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 20 69 73 MEM_Str flag is
aa2c0 20 73 65 74 20 74 68 65 6e 20 4d 65 6d 2e 7a 20 set then Mem.z
aa2d0 70 6f 69 6e 74 73 20 61 74 20 61 20 73 74 72 69 points at a stri
aa2e0 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f ng representatio
aa2f0 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 74 68 n..** Usually th
aa300 69 73 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e is is encoded in
aa310 20 74 68 65 20 73 61 6d 65 20 75 6e 69 63 6f 64 the same unicod
aa320 65 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 e encoding as th
aa330 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 e main.** databa
aa340 73 65 20 28 73 65 65 20 62 65 6c 6f 77 20 66 6f se (see below fo
aa350 72 20 65 78 63 65 70 74 69 6f 6e 73 29 2e 20 49 r exceptions). I
aa360 66 20 74 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 f the MEM_Term f
aa370 6c 61 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 73 lag is also.** s
aa380 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 et, then the str
aa390 69 6e 67 20 69 73 20 6e 75 6c 20 74 65 72 6d 69 ing is nul termi
aa3a0 6e 61 74 65 64 2e 20 54 68 65 20 4d 45 4d 5f 49 nated. The MEM_I
aa3b0 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 nt and MEM_Real
aa3c0 0a 2a 2a 20 66 6c 61 67 73 20 6d 61 79 20 63 6f .** flags may co
aa3d0 65 78 69 73 74 20 77 69 74 68 20 74 68 65 20 4d exist with the M
aa3e0 45 4d 5f 53 74 72 20 66 6c 61 67 2e 0a 2a 2a 0a EM_Str flag..**.
aa3f0 2a 2a 20 4d 75 6c 74 69 70 6c 65 20 6f 66 20 74 ** Multiple of t
aa400 68 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 hese values can
aa410 61 70 70 65 61 72 20 69 6e 20 4d 65 6d 2e 66 6c appear in Mem.fl
aa420 61 67 73 2e 20 20 42 75 74 20 6f 6e 6c 79 20 6f ags. But only o
aa430 6e 65 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 ne.** at a time
aa440 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65 can appear in Me
aa450 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23 64 65 66 69 m.type..*/.#defi
aa460 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20 20 20 20 20 ne MEM_Null
aa470 20 30 78 30 30 30 31 20 20 20 2f 2a 20 56 61 6c 0x0001 /* Val
aa480 75 65 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 ue is NULL */.#d
aa490 65 66 69 6e 65 20 4d 45 4d 5f 53 74 72 20 20 20 efine MEM_Str
aa4a0 20 20 20 20 30 78 30 30 30 32 20 20 20 2f 2a 20 0x0002 /*
aa4b0 56 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e Value is a strin
aa4c0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d g */.#define MEM
aa4d0 5f 49 6e 74 20 20 20 20 20 20 20 30 78 30 30 30 _Int 0x000
aa4e0 34 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4 /* Value is
aa4f0 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 an integer */.#d
aa500 65 66 69 6e 65 20 4d 45 4d 5f 52 65 61 6c 20 20 efine MEM_Real
aa510 20 20 20 20 30 78 30 30 30 38 20 20 20 2f 2a 20 0x0008 /*
aa520 56 61 6c 75 65 20 69 73 20 61 20 72 65 61 6c 20 Value is a real
aa530 6e 75 6d 62 65 72 20 2a 2f 0a 23 64 65 66 69 6e number */.#defin
aa540 65 20 4d 45 4d 5f 42 6c 6f 62 20 20 20 20 20 20 e MEM_Blob
aa550 30 78 30 30 31 30 20 20 20 2f 2a 20 56 61 6c 75 0x0010 /* Valu
aa560 65 20 69 73 20 61 20 42 4c 4f 42 20 2a 2f 0a 23 e is a BLOB */.#
aa570 64 65 66 69 6e 65 20 4d 45 4d 5f 52 6f 77 53 65 define MEM_RowSe
aa580 74 20 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a t 0x0020 /*
aa590 20 56 61 6c 75 65 20 69 73 20 61 20 52 6f 77 53 Value is a RowS
aa5a0 65 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 et object */.#de
aa5b0 66 69 6e 65 20 4d 45 4d 5f 46 72 61 6d 65 20 20 fine MEM_Frame
aa5c0 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a 20 56 0x0040 /* V
aa5d0 61 6c 75 65 20 69 73 20 61 20 56 64 62 65 46 72 alue is a VdbeFr
aa5e0 61 6d 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 ame object */.#d
aa5f0 65 66 69 6e 65 20 4d 45 4d 5f 54 79 70 65 4d 61 efine MEM_TypeMa
aa600 73 6b 20 20 30 78 30 30 66 66 20 20 20 2f 2a 20 sk 0x00ff /*
aa610 4d 61 73 6b 20 6f 66 20 74 79 70 65 20 62 69 74 Mask of type bit
aa620 73 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 s */../* Wheneve
aa630 72 20 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20 61 r Mem contains a
aa640 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 6f 72 valid string or
aa650 20 62 6c 6f 62 20 72 65 70 72 65 73 65 6e 74 61 blob representa
aa660 74 69 6f 6e 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 tion, one of.**
aa670 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c the following fl
aa680 61 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 20 ags must be set
aa690 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 to determine the
aa6a0 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 memory manageme
aa6b0 6e 74 0a 2a 2a 20 70 6f 6c 69 63 79 20 66 6f 72 nt.** policy for
aa6c0 20 4d 65 6d 2e 7a 2e 20 20 54 68 65 20 4d 45 4d Mem.z. The MEM
aa6d0 5f 54 65 72 6d 20 66 6c 61 67 20 74 65 6c 6c 73 _Term flag tells
aa6e0 20 75 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e us whether or n
aa6f0 6f 74 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 ot the.** string
aa700 20 69 73 20 5c 30 30 30 20 6f 72 20 5c 75 30 30 is \000 or \u00
aa710 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 00 terminated.*/
aa720 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 54 65 72 .#define MEM_Ter
aa730 6d 20 20 20 20 20 20 30 78 30 32 30 30 20 20 20 m 0x0200
aa740 2f 2a 20 53 74 72 69 6e 67 20 72 65 70 20 69 73 /* String rep is
aa750 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20 nul terminated
aa760 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 44 */.#define MEM_D
aa770 79 6e 20 20 20 20 20 20 20 30 78 30 34 30 30 20 yn 0x0400
aa780 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c /* Need to cal
aa790 6c 20 73 71 6c 69 74 65 46 72 65 65 28 29 20 6f l sqliteFree() o
aa7a0 6e 20 4d 65 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 n Mem.z */.#defi
aa7b0 6e 65 20 4d 45 4d 5f 53 74 61 74 69 63 20 20 20 ne MEM_Static
aa7c0 20 30 78 30 38 30 30 20 20 20 2f 2a 20 4d 65 6d 0x0800 /* Mem
aa7d0 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 .z points to a s
aa7e0 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f 0a tatic string */.
aa7f0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 45 70 68 65 #define MEM_Ephe
aa800 6d 20 20 20 20 20 30 78 31 30 30 30 20 20 20 2f m 0x1000 /
aa810 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 * Mem.z points t
aa820 6f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 o an ephemeral s
aa830 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 tring */.#define
aa840 20 4d 45 4d 5f 41 67 67 20 20 20 20 20 20 20 30 MEM_Agg 0
aa850 78 32 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a x2000 /* Mem.z
aa860 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 67 points to an ag
aa870 67 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 g function conte
aa880 78 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 xt */.#define ME
aa890 4d 5f 5a 65 72 6f 20 20 20 20 20 20 30 78 34 30 M_Zero 0x40
aa8a0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 00 /* Mem.i co
aa8b0 6e 74 61 69 6e 73 20 63 6f 75 6e 74 20 6f 66 20 ntains count of
aa8c0 30 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 62 0s appended to b
aa8d0 6c 6f 62 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 lob */..#ifdef S
aa8e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
aa8f0 4c 4f 42 0a 20 20 23 75 6e 64 65 66 20 4d 45 4d LOB. #undef MEM
aa900 5f 5a 65 72 6f 0a 20 20 23 64 65 66 69 6e 65 20 _Zero. #define
aa910 4d 45 4d 5f 5a 65 72 6f 20 30 78 30 30 30 30 0a MEM_Zero 0x0000.
aa920 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 #endif.../*.** C
aa930 6c 65 61 72 20 61 6e 79 20 65 78 69 73 74 69 6e lear any existin
aa940 67 20 74 79 70 65 20 66 6c 61 67 73 20 66 72 6f g type flags fro
aa950 6d 20 61 20 4d 65 6d 20 61 6e 64 20 72 65 70 6c m a Mem and repl
aa960 61 63 65 20 74 68 65 6d 20 77 69 74 68 20 66 0a ace them with f.
aa970 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53 65 */.#define MemSe
aa980 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29 20 tTypeFlag(p, f)
aa990 5c 0a 20 20 20 28 28 70 29 2d 3e 66 6c 61 67 73 \. ((p)->flags
aa9a0 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e = ((p)->flags&~
aa9b0 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 7c 4d 45 (MEM_TypeMask|ME
aa9c0 4d 5f 5a 65 72 6f 29 29 7c 66 29 0a 0a 0a 2f 2a M_Zero))|f).../*
aa9d0 20 41 20 56 64 62 65 46 75 6e 63 20 69 73 20 6a A VdbeFunc is j
aa9e0 75 73 74 20 61 20 46 75 6e 63 44 65 66 20 28 64 ust a FuncDef (d
aa9f0 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 efined in sqlite
aaa00 49 6e 74 2e 68 29 20 74 68 61 74 20 63 6f 6e 74 Int.h) that cont
aaa10 61 69 6e 73 0a 2a 2a 20 61 64 64 69 74 69 6f 6e ains.** addition
aaa20 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 al information a
aaa30 62 6f 75 74 20 61 75 78 69 6c 69 61 72 79 20 69 bout auxiliary i
aaa40 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 6f 75 6e 64 nformation bound
aaa50 20 74 6f 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a to arguments.**
aaa60 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e of the function
aaa70 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 . This is used
aaa80 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
aaa90 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 sqlite3_get_aux
aaaa0 64 61 74 61 28 29 0a 2a 2a 20 61 6e 64 20 73 71 data().** and sq
aaab0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 lite3_set_auxdat
aaac0 61 28 29 20 41 50 49 73 2e 20 20 54 68 65 20 22 a() APIs. The "
aaad0 61 75 78 64 61 74 61 22 20 69 73 20 73 6f 6d 65 auxdata" is some
aaae0 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 0a auxiliary data.
aaaf0 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20 61 ** that can be a
aab00 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
aab10 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 constant argume
aab20 6e 74 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e nt to a function
aab30 2e 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 . This.** allow
aab40 73 20 66 75 6e 63 74 69 6f 6e 73 20 73 75 63 68 s functions such
aab50 20 61 73 20 22 72 65 67 65 78 70 22 20 74 6f 20 as "regexp" to
aab60 63 6f 6d 70 69 6c 65 20 74 68 65 69 72 20 63 6f compile their co
aab70 6e 73 74 61 6e 74 20 72 65 67 75 6c 61 72 0a 2a nstant regular.*
aab80 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 72 67 * expression arg
aab90 75 6d 65 6e 74 20 6f 6e 63 65 20 61 6e 64 20 72 ument once and r
aaba0 65 75 73 65 64 20 74 68 65 20 63 6f 6d 70 69 6c eused the compil
aabb0 65 64 20 63 6f 64 65 20 66 6f 72 20 6d 75 6c 74 ed code for mult
aabc0 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 iple.** invocati
aabd0 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 ons..*/.struct V
aabe0 64 62 65 46 75 6e 63 20 7b 0a 20 20 46 75 6e 63 dbeFunc {. Func
aabf0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 Def *pFunc;
aac00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
aac10 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 definition of t
aac20 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 he function */.
aac30 20 69 6e 74 20 6e 41 75 78 3b 20 20 20 20 20 20 int nAux;
aac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
aac50 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 * Number of entr
aac60 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ies allocated fo
aac70 72 20 61 70 41 75 78 5b 5d 20 2a 2f 0a 20 20 73 r apAux[] */. s
aac80 74 72 75 63 74 20 41 75 78 44 61 74 61 20 7b 0a truct AuxData {.
aac90 20 20 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20 void *pAux;
aaca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aacb0 20 20 2f 2a 20 41 75 78 20 64 61 74 61 20 66 6f /* Aux data fo
aacc0 72 20 74 68 65 20 69 2d 74 68 20 61 72 67 75 6d r the i-th argum
aacd0 65 6e 74 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 ent */. void
aace0 28 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 20 (*xDelete)(void
aacf0 2a 29 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 *); /* Dest
aad00 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 61 ructor for the a
aad10 75 78 20 64 61 74 61 20 2a 2f 0a 20 20 7d 20 61 ux data */. } a
aad20 70 41 75 78 5b 31 5d 3b 20 20 20 20 20 20 20 20 pAux[1];
aad30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e /* On
aad40 65 20 73 6c 6f 74 20 66 6f 72 20 65 61 63 68 20 e slot for each
aad50 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e function argumen
aad60 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 t */.};../*.** T
aad70 68 65 20 22 63 6f 6e 74 65 78 74 22 20 61 72 67 he "context" arg
aad80 75 6d 65 6e 74 20 66 6f 72 20 61 20 69 6e 73 74 ument for a inst
aad90 61 6c 6c 61 62 6c 65 20 66 75 6e 63 74 69 6f 6e allable function
aada0 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 . A pointer to
aadb0 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f an.** instance o
aadc0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
aadd0 20 69 73 20 74 68 65 20 66 69 72 73 74 20 61 72 is the first ar
aade0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 72 6f gument to the ro
aadf0 75 74 69 6e 65 73 20 75 73 65 64 0a 2a 2a 20 69 utines used.** i
aae00 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c mplement the SQL
aae10 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a functions..**.*
aae20 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 79 70 * There is a typ
aae30 65 64 65 66 20 66 6f 72 20 74 68 69 73 20 73 74 edef for this st
aae40 72 75 63 74 75 72 65 20 69 6e 20 73 71 6c 69 74 ructure in sqlit
aae50 65 2e 68 2e 20 20 53 6f 20 61 6c 6c 20 72 6f 75 e.h. So all rou
aae60 74 69 6e 65 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 tines,.** even t
aae70 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72 66 he public interf
aae80 61 63 65 20 74 6f 20 53 51 4c 69 74 65 2c 20 63 ace to SQLite, c
aae90 61 6e 20 75 73 65 20 61 20 70 6f 69 6e 74 65 72 an use a pointer
aaea0 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 to this structu
aaeb0 72 65 2e 0a 2a 2a 20 42 75 74 20 74 68 69 73 20 re..** But this
aaec0 66 69 6c 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 file is the only
aaed0 20 70 6c 61 63 65 20 77 68 65 72 65 20 74 68 65 place where the
aaee0 20 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c internal detail
aaef0 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73 74 72 s of this.** str
aaf00 75 63 74 75 72 65 20 61 72 65 20 6b 6e 6f 77 6e ucture are known
aaf10 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 ..**.** This str
aaf20 75 63 74 75 72 65 20 69 73 20 64 65 66 69 6e 65 ucture is define
aaf30 64 20 69 6e 73 69 64 65 20 6f 66 20 76 64 62 65 d inside of vdbe
aaf40 49 6e 74 2e 68 20 62 65 63 61 75 73 65 20 69 74 Int.h because it
aaf50 20 75 73 65 73 20 73 75 62 73 74 72 75 63 74 75 uses substructu
aaf60 72 65 73 0a 2a 2a 20 28 4d 65 6d 29 20 77 68 69 res.** (Mem) whi
aaf70 63 68 20 61 72 65 20 6f 6e 6c 79 20 64 65 66 69 ch are only defi
aaf80 6e 65 64 20 74 68 65 72 65 2e 0a 2a 2f 0a 73 74 ned there..*/.st
aaf90 72 75 63 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e ruct sqlite3_con
aafa0 74 65 78 74 20 7b 0a 20 20 46 75 6e 63 44 65 66 text {. FuncDef
aafb0 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 2f *pFunc; /
aafc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 75 6e * Pointer to fun
aafd0 63 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f ction informatio
aafe0 6e 2e 20 20 4d 55 53 54 20 42 45 20 46 49 52 53 n. MUST BE FIRS
aaff0 54 20 2a 2f 0a 20 20 56 64 62 65 46 75 6e 63 20 T */. VdbeFunc
ab000 2a 70 56 64 62 65 46 75 6e 63 3b 20 20 2f 2a 20 *pVdbeFunc; /*
ab010 41 75 78 69 6c 61 72 79 20 64 61 74 61 2c 20 69 Auxilary data, i
ab020 66 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 20 20 f created. */.
ab030 4d 65 6d 20 73 3b 20 20 20 20 20 20 20 20 20 20 Mem s;
ab040 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 /* The ret
ab050 75 72 6e 20 76 61 6c 75 65 20 69 73 20 73 74 6f urn value is sto
ab060 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20 4d 65 red here */. Me
ab070 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
ab080 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 /* Memory ce
ab090 6c 6c 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 ll used to store
ab0a0 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e 74 65 aggregate conte
ab0b0 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 xt */. int isEr
ab0c0 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ror; /*
ab0d0 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 Error code retu
ab0e0 72 6e 65 64 20 62 79 20 74 68 65 20 66 75 6e 63 rned by the func
ab0f0 74 69 6f 6e 2e 20 2a 2f 0a 20 20 43 6f 6c 6c 53 tion. */. CollS
ab100 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 eq *pColl;
ab110 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 /* Collating se
ab120 71 75 65 6e 63 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a quence */.};../*
ab130 0a 2a 2a 20 41 20 53 65 74 20 73 74 72 75 63 74 .** A Set struct
ab140 75 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 ure is used for
ab150 71 75 69 63 6b 20 74 65 73 74 69 6e 67 20 74 6f quick testing to
ab160 20 73 65 65 20 69 66 20 61 20 76 61 6c 75 65 0a see if a value.
ab170 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 ** is part of a
ab180 73 6d 61 6c 6c 20 73 65 74 2e 20 20 53 65 74 73 small set. Sets
ab190 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 are used to imp
ab1a0 6c 65 6d 65 6e 74 20 63 6f 64 65 20 6c 69 6b 65 lement code like
ab1b0 0a 2a 2a 20 74 68 69 73 3a 0a 2a 2a 20 20 20 20 .** this:.**
ab1c0 20 20 20 20 20 20 20 20 78 2e 79 20 49 4e 20 28 x.y IN (
ab1d0 27 68 69 27 2c 27 68 6f 6f 27 2c 27 68 75 6d 27 'hi','hoo','hum'
ab1e0 29 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 ).*/.typedef str
ab1f0 75 63 74 20 53 65 74 20 53 65 74 3b 0a 73 74 72 uct Set Set;.str
ab200 75 63 74 20 53 65 74 20 7b 0a 20 20 48 61 73 68 uct Set {. Hash
ab210 20 68 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 hash;
ab220 20 20 20 2f 2a 20 41 20 73 65 74 20 69 73 20 6a /* A set is j
ab230 75 73 74 20 61 20 68 61 73 68 20 74 61 62 6c 65 ust a hash table
ab240 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
ab250 70 72 65 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 prev; /*
ab260 50 72 65 76 69 6f 75 73 6c 79 20 61 63 63 65 73 Previously acces
ab270 73 65 64 20 68 61 73 68 20 65 6c 65 6d 65 6e 20 sed hash elemen
ab280 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
ab290 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
ab2a0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
ab2b0 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 This structure
ab2c0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f contains the co
ab2d0 6d 70 6c 65 74 65 0a 2a 2a 20 73 74 61 74 65 20 mplete.** state
ab2e0 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d of the virtual m
ab2f0 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 achine..**.** Th
ab300 65 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 e "sqlite3_stmt"
ab310 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 structure point
ab320 65 72 20 74 68 61 74 20 69 73 20 72 65 74 75 72 er that is retur
ab330 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 ned by sqlite3_c
ab340 6f 6d 70 69 6c 65 28 29 0a 2a 2a 20 69 73 20 72 ompile().** is r
ab350 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 eally a pointer
ab360 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f to an instance o
ab370 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 f this structure
ab380 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 ..**.** The Vdbe
ab390 2e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 76 61 .inVtabMethod va
ab3a0 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f riable is set to
ab3b0 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 74 68 non-zero for th
ab3c0 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a e duration of.**
ab3d0 20 61 6e 79 20 76 69 72 74 75 61 6c 20 74 61 62 any virtual tab
ab3e0 6c 65 20 6d 65 74 68 6f 64 20 69 6e 76 6f 63 61 le method invoca
ab3f0 74 69 6f 6e 73 20 6d 61 64 65 20 62 79 20 74 68 tions made by th
ab400 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 e vdbe program.
ab410 49 74 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 It is.** set to
ab420 32 20 66 6f 72 20 78 44 65 73 74 72 6f 79 20 6d 2 for xDestroy m
ab430 65 74 68 6f 64 20 63 61 6c 6c 73 20 61 6e 64 20 ethod calls and
ab440 31 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 1 for all other
ab450 6d 65 74 68 6f 64 73 2e 20 54 68 69 73 0a 2a 2a methods. This.**
ab460 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
ab470 64 20 66 6f 72 20 74 77 6f 20 70 75 72 70 6f 73 d for two purpos
ab480 65 73 3a 20 74 6f 20 61 6c 6c 6f 77 20 78 44 65 es: to allow xDe
ab490 73 74 72 6f 79 20 6d 65 74 68 6f 64 73 20 74 6f stroy methods to
ab4a0 20 65 78 65 63 75 74 65 0a 2a 2a 20 22 44 52 4f execute.** "DRO
ab4b0 50 20 54 41 42 4c 45 22 20 73 74 61 74 65 6d 65 P TABLE" stateme
ab4c0 6e 74 73 20 61 6e 64 20 74 6f 20 70 72 65 76 65 nts and to preve
ab4d0 6e 74 20 73 6f 6d 65 20 6e 61 73 74 79 20 73 69 nt some nasty si
ab4e0 64 65 20 65 66 66 65 63 74 73 20 6f 66 0a 2a 2a de effects of.**
ab4f0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 malloc failure
ab500 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 69 when SQLite is i
ab510 6e 76 6f 6b 65 64 20 72 65 63 75 72 73 69 76 65 nvoked recursive
ab520 6c 79 20 62 79 20 61 20 76 69 72 74 75 61 6c 20 ly by a virtual
ab530 74 61 62 6c 65 20 0a 2a 2a 20 6d 65 74 68 6f 64 table .** method
ab540 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 function..*/.st
ab550 72 75 63 74 20 56 64 62 65 20 7b 0a 20 20 73 71 ruct Vdbe {. sq
ab560 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
ab570 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
ab580 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
ab590 20 74 68 61 74 20 6f 77 6e 73 20 74 68 69 73 20 that owns this
ab5a0 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 statement */. V
ab5b0 64 62 65 20 2a 70 50 72 65 76 2c 2a 70 4e 65 78 dbe *pPrev,*pNex
ab5c0 74 3b 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 t; /* Linked
ab5d0 20 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 list of VDBEs w
ab5e0 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 ith the same Vdb
ab5f0 65 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f e.db */. int nO
ab600 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
ab610 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 /* Number of i
ab620 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20 74 nstructions in t
ab630 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 he program */.
ab640 69 6e 74 20 6e 4f 70 41 6c 6c 6f 63 3b 20 20 20 int nOpAlloc;
ab650 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ab660 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 r of slots alloc
ab670 61 74 65 64 20 66 6f 72 20 61 4f 70 5b 5d 20 2a ated for aOp[] *
ab680 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 /. Op *aOp;
ab690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
ab6a0 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 pace to hold the
ab6b0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
ab6c0 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 's program */.
ab6d0 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20 20 20 20 int nLabel;
ab6e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ab6f0 72 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 r of labels used
ab700 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c */. int nLabel
ab710 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a Alloc; /*
ab720 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 Number of slots
ab730 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 4c allocated in aL
ab740 61 62 65 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 abel[] */. int
ab750 2a 61 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 *aLabel;
ab760 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 /* Space to
ab770 68 6f 6c 64 20 74 68 65 20 6c 61 62 65 6c 73 20 hold the labels
ab780 2a 2f 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 */. Mem **apArg
ab790 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
ab7a0 41 72 67 75 6d 65 6e 74 73 20 74 6f 20 63 75 72 Arguments to cur
ab7b0 72 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 rently executing
ab7c0 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a user function *
ab7d0 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c 4e 61 6d /. Mem *aColNam
ab7e0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 e; /* C
ab7f0 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 olumn names to r
ab800 65 74 75 72 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a eturn */. Mem *
ab810 70 52 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 pResultSet;
ab820 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
ab830 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 an array of res
ab840 75 6c 74 73 20 2a 2f 0a 20 20 75 31 36 20 6e 52 ults */. u16 nR
ab850 65 73 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 esColumn;
ab860 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
ab870 6f 6c 75 6d 6e 73 20 69 6e 20 6f 6e 65 20 72 6f olumns in one ro
ab880 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 w of the result
ab890 73 65 74 20 2a 2f 0a 20 20 75 31 36 20 6e 43 75 set */. u16 nCu
ab8a0 72 73 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 rsor;
ab8b0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
ab8c0 6f 74 73 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a ots in apCsr[] *
ab8d0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
ab8e0 2a 61 70 43 73 72 3b 20 20 20 20 20 2f 2a 20 4f *apCsr; /* O
ab8f0 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 ne element of th
ab900 69 73 20 61 72 72 61 79 20 66 6f 72 20 65 61 63 is array for eac
ab910 68 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 2a 2f h open cursor */
ab920 0a 20 20 75 38 20 65 72 72 6f 72 41 63 74 69 6f . u8 errorActio
ab930 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 n; /* Re
ab940 63 6f 76 65 72 79 20 61 63 74 69 6f 6e 20 74 6f covery action to
ab950 20 64 6f 20 69 6e 20 63 61 73 65 20 6f 66 20 61 do in case of a
ab960 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75 38 20 n error */. u8
ab970 6f 6b 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 okVar;
ab980 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
ab990 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e azVar[] has been
ab9a0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
ab9b0 20 20 79 6e 56 61 72 20 6e 56 61 72 3b 20 20 20 ynVar nVar;
ab9c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
ab9d0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 ber of entries i
ab9e0 6e 20 61 56 61 72 5b 5d 20 2a 2f 0a 20 20 4d 65 n aVar[] */. Me
ab9f0 6d 20 2a 61 56 61 72 3b 20 20 20 20 20 20 20 20 m *aVar;
aba00 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 /* Values
aba10 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72 69 61 for the OP_Varia
aba20 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f 0a 20 ble opcode. */.
aba30 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b 20 20 char **azVar;
aba40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
aba50 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f of variables */
aba60 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 . u32 magic;
aba70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 /* Ma
aba80 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 gic number for s
aba90 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
abaa0 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 /. int nMem;
abab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
abac0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
abad0 6c 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e locations curren
abae0 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f tly allocated */
abaf0 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 . Mem *aMem;
abb00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
abb10 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f e memory locatio
abb20 6e 73 20 2a 2f 0a 20 20 75 33 32 20 63 61 63 68 ns */. u32 cach
abb30 65 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 20 eCtr;
abb40 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20 72 6f /* VdbeCursor ro
abb50 77 20 63 61 63 68 65 20 67 65 6e 65 72 61 74 69 w cache generati
abb60 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 on counter */.
abb70 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 int pc;
abb80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
abb90 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a rogram counter *
abba0 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
abbb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 /* V
abbc0 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a alue to return *
abbd0 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 /. char *zErrMs
abbe0 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 g; /* E
abbf0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69 rror message wri
abc00 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 75 tten here */. u
abc10 38 20 65 78 70 6c 61 69 6e 3b 20 20 20 20 20 20 8 explain;
abc20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
abc30 66 20 45 58 50 4c 41 49 4e 20 70 72 65 73 65 6e f EXPLAIN presen
abc40 74 20 6f 6e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 t on SQL command
abc50 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 */. u8 changeC
abc60 6e 74 4f 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a ntOn; /*
abc70 20 54 72 75 65 20 74 6f 20 75 70 64 61 74 65 20 True to update
abc80 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
abc90 65 72 20 2a 2f 0a 20 20 75 38 20 65 78 70 69 72 er */. u8 expir
abca0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ed;
abcb0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 56 /* True if the V
abcc0 4d 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 M needs to be re
abcd0 63 6f 6d 70 69 6c 65 64 20 2a 2f 0a 20 20 75 38 compiled */. u8
abce0 20 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 minWriteFileFor
abcf0 6d 61 74 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d mat; /* Minimum
abd00 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f 72 file format for
abd10 20 77 72 69 74 61 62 6c 65 20 64 61 74 61 62 61 writable databa
abd20 73 65 20 66 69 6c 65 73 20 2a 2f 0a 20 20 75 38 se files */. u8
abd30 20 69 6e 56 74 61 62 4d 65 74 68 6f 64 3b 20 20 inVtabMethod;
abd40 20 20 20 20 20 20 2f 2a 20 53 65 65 20 63 6f 6d /* See com
abd50 6d 65 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20 ments above */.
abd60 20 75 38 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 u8 usesStmtJour
abd70 6e 61 6c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 nal; /* True
abd80 20 69 66 20 75 73 65 73 20 61 20 73 74 61 74 65 if uses a state
abd90 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a ment journal */.
abda0 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 u8 readOnly;
abdb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
abdc0 65 20 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 e for read-only
abdd0 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 statements */.
abde0 75 38 20 69 73 50 72 65 70 61 72 65 56 32 3b 20 u8 isPrepareV2;
abdf0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
abe00 69 66 20 70 72 65 70 61 72 65 64 20 77 69 74 68 if prepared with
abe10 20 70 72 65 70 61 72 65 5f 76 32 28 29 20 2a 2f prepare_v2() */
abe20 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 . int nChange;
abe30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
abe40 6d 62 65 72 20 6f 66 20 64 62 20 63 68 61 6e 67 mber of db chang
abe50 65 73 20 6d 61 64 65 20 73 69 6e 63 65 20 6c 61 es made since la
abe60 73 74 20 72 65 73 65 74 20 2a 2f 0a 20 20 69 6e st reset */. in
abe70 74 20 62 74 72 65 65 4d 61 73 6b 3b 20 20 20 20 t btreeMask;
abe80 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b /* Bitmask
abe90 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d 20 65 6e of db->aDb[] en
abea0 74 72 69 65 73 20 72 65 66 65 72 65 6e 63 65 64 tries referenced
abeb0 20 2a 2f 0a 20 20 69 36 34 20 73 74 61 72 74 54 */. i64 startT
abec0 69 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ime; /*
abed0 20 54 69 6d 65 20 77 68 65 6e 20 71 75 65 72 79 Time when query
abee0 20 73 74 61 72 74 65 64 20 2d 20 75 73 65 64 20 started - used
abef0 66 6f 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a 2f for profiling */
abf00 0a 20 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 . BtreeMutexArr
abf10 61 79 20 61 4d 75 74 65 78 3b 20 2f 2a 20 41 6e ay aMutex; /* An
abf20 20 61 72 72 61 79 20 6f 66 20 42 74 72 65 65 20 array of Btree
abf30 75 73 65 64 20 68 65 72 65 20 61 6e 64 20 6e 65 used here and ne
abf40 65 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 eding locks */.
abf50 20 69 6e 74 20 61 43 6f 75 6e 74 65 72 5b 32 5d int aCounter[2]
abf60 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e ; /* Coun
abf70 74 65 72 73 20 75 73 65 64 20 62 79 20 73 71 6c ters used by sql
abf80 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 ite3_stmt_status
abf90 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 () */. char *zS
abfa0 71 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ql;
abfb0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 53 /* Text of the S
abfc0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 QL statement tha
abfd0 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 69 73 t generated this
abfe0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 46 72 65 */. void *pFre
abff0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
ac000 20 46 72 65 65 20 74 68 69 73 20 77 68 65 6e 20 Free this when
ac010 64 65 6c 65 74 69 6e 67 20 74 68 65 20 76 64 62 deleting the vdb
ac020 65 20 2a 2f 0a 20 20 69 36 34 20 6e 46 6b 43 6f e */. i64 nFkCo
ac030 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20 2f nstraint; /
ac040 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6d 6d 2e * Number of imm.
ac050 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 FK constraints
ac060 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 36 34 this VM */. i64
ac070 20 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 20 20 nStmtDefCons;
ac080 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
ac090 66 20 64 65 66 2e 20 63 6f 6e 73 74 72 61 69 6e f def. constrain
ac0a0 74 73 20 77 68 65 6e 20 73 74 6d 74 20 73 74 61 ts when stmt sta
ac0b0 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 53 rted */. int iS
ac0c0 74 61 74 65 6d 65 6e 74 3b 20 20 20 20 20 20 20 tatement;
ac0d0 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 6e /* Statement n
ac0e0 75 6d 62 65 72 20 28 6f 72 20 30 20 69 66 20 68 umber (or 0 if h
ac0f0 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 73 74 as not opened st
ac100 6d 74 29 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 mt) */.#ifdef SQ
ac110 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49 4c LITE_DEBUG. FIL
ac120 45 20 2a 74 72 61 63 65 3b 20 20 20 20 20 20 20 E *trace;
ac130 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e /* Write an
ac140 20 65 78 65 63 75 74 69 6f 6e 20 74 72 61 63 65 execution trace
ac150 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 here, if not NU
ac160 4c 4c 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 LL */.#endif. V
ac170 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 dbeFrame *pFrame
ac180 3b 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 ; /* Parent
ac190 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 frame */. int
ac1a0 6e 46 72 61 6d 65 3b 20 20 20 20 20 20 20 20 20 nFrame;
ac1b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
ac1c0 20 66 72 61 6d 65 73 20 69 6e 20 70 46 72 61 6d frames in pFram
ac1d0 65 20 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32 20 e list */. u32
ac1e0 65 78 70 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 expmask;
ac1f0 20 20 20 20 2f 2a 20 42 69 6e 64 69 6e 67 20 74 /* Binding t
ac200 6f 20 74 68 65 73 65 20 76 61 72 73 20 69 6e 76 o these vars inv
ac210 61 6c 69 64 61 74 65 73 20 56 4d 20 2a 2f 0a 7d alidates VM */.}
ac220 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c ;../*.** The fol
ac230 6c 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 lowing are allow
ac240 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64 ed values for Vd
ac250 62 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 be.magic.*/.#def
ac260 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 ine VDBE_MAGIC_I
ac270 4e 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61 NIT 0x26bcea
ac280 61 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e a5 /* Buildin
ac290 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d g a VDBE program
ac2a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 */.#define VDBE
ac2b0 5f 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20 _MAGIC_RUN
ac2c0 30 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a 0xbdf20da3 /*
ac2d0 20 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74 VDBE is ready t
ac2e0 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 o execute */.#de
ac2f0 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f fine VDBE_MAGIC_
ac300 48 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32 HALT 0x519c2
ac310 39 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68 973 /* VDBE h
ac320 61 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 as completed exe
ac330 63 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e cution */.#defin
ac340 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 e VDBE_MAGIC_DEA
ac350 44 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38 D 0xb606c3c8
ac360 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 /* The VDBE
ac370 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 has been dealloc
ac380 61 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 ated */../*.** F
ac390 75 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 unction prototyp
ac3a0 65 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 es.*/.SQLITE_PRI
ac3b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
ac3c0 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 3VdbeFreeCursor(
ac3d0 56 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 73 Vdbe *, VdbeCurs
ac3e0 6f 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74 or*);.void sqlit
ac3f0 65 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 64 eVdbePopStack(Vd
ac400 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 be*,int);.SQLITE
ac410 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ac420 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f ite3VdbeCursorMo
ac430 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 2a veto(VdbeCursor*
ac440 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 );.#if defined(S
ac450 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 QLITE_DEBUG) ||
ac460 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f defined(VDBE_PRO
ac470 46 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 FILE).SQLITE_PRI
ac480 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
ac490 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 3VdbePrintOp(FIL
ac4a0 45 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 E*, int, Op*);.#
ac4b0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 endif.SQLITE_PRI
ac4c0 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
ac4d0 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 VdbeSerialTypeLe
ac4e0 6e 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 n(u32);.SQLITE_P
ac4f0 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
ac500 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
ac510 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c (Mem*, int);.SQL
ac520 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
ac530 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 sqlite3VdbeSeria
ac540 6c 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68 lPut(unsigned ch
ac550 61 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 ar*, int, Mem*,
ac560 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 int);.SQLITE_PRI
ac570 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 VATE u32 sqlite3
ac580 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f VdbeSerialGet(co
ac590 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
ac5a0 72 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a r*, u32, Mem*);.
ac5b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
ac5c0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 oid sqlite3VdbeD
ac5d0 65 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62 eleteAuxData(Vdb
ac5e0 65 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 eFunc*, int);..i
ac5f0 6e 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b nt sqlite2BtreeK
ac600 65 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73 eyCompare(BtCurs
ac610 6f 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 or *, const void
ac620 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e *, int, int, in
ac630 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 t *);.SQLITE_PRI
ac640 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
ac650 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 VdbeIdxKeyCompar
ac660 65 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e e(VdbeCursor*,Un
ac670 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e packedRecord*,in
ac680 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 t*);.SQLITE_PRIV
ac690 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ac6a0 64 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69 dbeIdxRowid(sqli
ac6b0 74 65 33 2a 2c 20 42 74 43 75 72 73 6f 72 20 2a te3*, BtCursor *
ac6c0 2c 20 69 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 , i64 *);.SQLITE
ac6d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ac6e0 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 ite3MemCompare(c
ac6f0 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 onst Mem*, const
ac700 20 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 43 6f 6c Mem*, const Col
ac710 6c 53 65 71 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 lSeq*);.SQLITE_P
ac720 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
ac730 65 33 56 64 62 65 45 78 65 63 28 56 64 62 65 2a e3VdbeExec(Vdbe*
ac740 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac750 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ac760 65 4c 69 73 74 28 56 64 62 65 2a 29 3b 0a 53 51 eList(Vdbe*);.SQ
ac770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ac780 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 sqlite3VdbeHalt
ac790 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f (Vdbe*);.SQLITE_
ac7a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
ac7b0 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
ac7c0 6f 64 69 6e 67 28 4d 65 6d 20 2a 2c 20 69 6e 74 oding(Mem *, int
ac7d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
ac7e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
ac7f0 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 2a 29 eMemTooBig(Mem*)
ac800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ac810 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
ac820 4d 65 6d 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 6f MemCopy(Mem*, co
ac830 6e 73 74 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 nst Mem*);.SQLIT
ac840 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
ac850 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 qlite3VdbeMemSha
ac860 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 2a 2c 20 63 llowCopy(Mem*, c
ac870 6f 6e 73 74 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b onst Mem*, int);
ac880 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac890 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac8a0 4d 65 6d 4d 6f 76 65 28 4d 65 6d 2a 2c 20 4d 65 MemMove(Mem*, Me
ac8b0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
ac8c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ac8d0 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
ac8e0 74 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 te(Mem*);.SQLITE
ac8f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
ac900 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
ac910 72 28 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 r(Mem*, const ch
ac920 61 72 2a 2c 20 69 6e 74 2c 20 75 38 2c 20 76 6f ar*, int, u8, vo
ac930 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 id(*)(void*));.S
ac940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
ac950 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
ac960 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 2a 2c 20 mSetInt64(Mem*,
ac970 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 i64);.SQLITE_PRI
ac980 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
ac990 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 3VdbeMemSetDoubl
ac9a0 65 28 4d 65 6d 2a 2c 20 64 6f 75 62 6c 65 29 3b e(Mem*, double);
ac9b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ac9c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
ac9d0 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 2a 29 MemSetNull(Mem*)
ac9e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 ;.SQLITE_PRIVATE
ac9f0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
aca00 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 eMemSetZeroBlob(
aca10 4d 65 6d 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 Mem*,int);.SQLIT
aca20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
aca30 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
aca40 52 6f 77 53 65 74 28 4d 65 6d 2a 29 3b 0a 53 51 RowSet(Mem*);.SQ
aca50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
aca60 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d sqlite3VdbeMemM
aca70 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d 65 6d akeWriteable(Mem
aca80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
aca90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acaa0 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 4d beMemStringify(M
acab0 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 em*, int);.SQLIT
acac0 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 E_PRIVATE i64 sq
acad0 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 lite3VdbeIntValu
acae0 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f e(Mem*);.SQLITE_
acaf0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acb00 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
acb10 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 rify(Mem*);.SQLI
acb20 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 6c TE_PRIVATE doubl
acb30 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 e sqlite3VdbeRea
acb40 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 lValue(Mem*);.SQ
acb50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
acb60 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 d sqlite3VdbeInt
acb70 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d egerAffinity(Mem
acb80 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
acb90 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acba0 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 6d beMemRealify(Mem
acbb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 *);.SQLITE_PRIVA
acbc0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
acbd0 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d 65 beMemNumerify(Me
acbe0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 m*);.SQLITE_PRIV
acbf0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
acc00 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
acc10 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c 69 6e BtCursor*,int,in
acc20 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53 51 4c t,int,Mem*);.SQL
acc30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
acc40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
acc50 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 3b 0a elease(Mem *p);.
acc60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
acc70 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
acc80 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
acc90 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 l(Mem *p);.SQLIT
acca0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
accb0 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 lite3VdbeMemFina
accc0 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e 63 44 lize(Mem*, FuncD
accd0 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 ef*);.SQLITE_PRI
acce0 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 VATE const char
accf0 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 *sqlite3OpcodeNa
acd00 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f me(int);.SQLITE_
acd10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acd20 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d te3VdbeMemGrow(M
acd30 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c em *pMem, int n,
acd40 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b 0a int preserve);.
acd50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
acd60 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c nt sqlite3VdbeCl
acd70 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62 oseStatement(Vdb
acd80 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 e *, int);.SQLIT
acd90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
acda0 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 qlite3VdbeFrameD
acdb0 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 2a elete(VdbeFrame*
acdc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 );.SQLITE_PRIVAT
acdd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
acde0 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 eFrameRestore(Vd
acdf0 62 65 46 72 61 6d 65 20 2a 29 3b 0a 53 51 4c 49 beFrame *);.SQLI
ace00 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
ace10 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
ace20 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 oreType(Mem *pMe
ace30 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c m);..#ifndef SQL
ace40 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e ITE_OMIT_FOREIGN
ace50 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 _KEY.SQLITE_PRIV
ace60 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
ace70 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 dbeCheckFk(Vdbe
ace80 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 *, int);.#else.#
ace90 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 define sqlite3V
acea0 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 69 29 20 dbeCheckFk(p,i)
aceb0 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 0.#endif..#ifnde
acec0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 f SQLITE_OMIT_SH
aced0 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 ARED_CACHE.SQLIT
acee0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
acef0 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 qlite3VdbeMutexA
acf00 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a rrayEnter(Vdbe *
acf10 70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 p);.#else.# defi
acf20 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 ne sqlite3VdbeMu
acf30 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 texArrayEnter(p)
acf40 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f .#endif..SQLITE_
acf50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
acf60 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c te3VdbeMemTransl
acf70 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 ate(Mem*, u8);.#
acf80 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
acf90 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 UG.SQLITE_PRIVAT
acfa0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 E void sqlite3
acfb0 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 VdbePrintSql(Vdb
acfc0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 e*);.SQLITE_PRIV
acfd0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 ATE void sqlit
acfe0 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 e3VdbeMemPrettyP
acff0 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 rint(Mem *pMem,
ad000 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e char *zBuf);.#en
ad010 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 dif.SQLITE_PRIVA
ad020 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
ad030 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d beMemHandleBom(M
ad040 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e em *pMem);..#ifn
ad050 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
ad060 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f INCRBLOB.SQLITE_
ad070 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 PRIVATE int sq
ad080 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 lite3VdbeMemExpa
ad090 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 ndBlob(Mem *);.#
ad0a0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 else. #define s
ad0b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 qlite3VdbeMemExp
ad0c0 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 andBlob(x) SQLIT
ad0d0 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e E_OK.#endif..#en
ad0e0 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
ad0f0 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a _VDBEINT_H_) */.
ad100 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
ad110 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74 2e End of vdbeInt.
ad120 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
ad130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ad140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
ad150 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
ad160 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
ad170 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
ad180 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a utf.c *********
ad190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
ad1a0 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
ad1b0 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a _AMALGAMATION./*
ad1c0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
ad1d0 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 g constant value
ad1e0 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 is used by the
ad1f0 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e SQLITE_BIGENDIAN
ad200 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c and.** SQLITE_L
ad210 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 ITTLEENDIAN macr
ad220 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 os..*/.SQLITE_PR
ad230 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 IVATE const int
ad240 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a sqlite3one = 1;.
ad250 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
ad260 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f _AMALGAMATION */
ad270 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f ../*.** This loo
ad280 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73 65 kup table is use
ad290 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 d to help decode
ad2a0 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 the first byte
ad2b0 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 of.** a multi-by
ad2c0 74 65 20 55 54 46 38 20 63 68 61 72 61 63 74 65 te UTF8 characte
ad2d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e r..*/.static con
ad2e0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
ad2f0 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61 6e sqlite3Utf8Tran
ad300 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c s1[] = {. 0x00,
ad310 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 0x01, 0x02, 0x0
ad320 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 3, 0x04, 0x05, 0
ad330 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 x06, 0x07,. 0x0
ad340 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 8, 0x09, 0x0a, 0
ad350 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c x0b, 0x0c, 0x0d,
ad360 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 0x0e, 0x0f,. 0
ad370 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32 2c x10, 0x11, 0x12,
ad380 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78 31 0x13, 0x14, 0x1
ad390 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 5, 0x16, 0x17,.
ad3a0 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78 31 0x18, 0x19, 0x1
ad3b0 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20 30 a, 0x1b, 0x1c, 0
ad3c0 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66 2c x1d, 0x1e, 0x1f,
ad3d0 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 . 0x00, 0x01, 0
ad3e0 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c x02, 0x03, 0x04,
ad3f0 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 0x05, 0x06, 0x0
ad400 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 7,. 0x08, 0x09,
ad410 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 0x0a, 0x0b, 0x0
ad420 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 c, 0x0d, 0x0e, 0
ad430 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 x0f,. 0x00, 0x0
ad440 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 1, 0x02, 0x03, 0
ad450 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36 2c x04, 0x05, 0x06,
ad460 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 0x07,. 0x00, 0
ad470 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c x01, 0x02, 0x03,
ad480 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 0x00, 0x01, 0x0
ad490 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 0, 0x00,.};...#d
ad4a0 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46 38 efine WRITE_UTF8
ad4b0 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 (zOut, c) {
ad4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad4d0 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 \. if( c<0
ad4e0 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20 20 x00080 ){
ad4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad510 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
ad520 38 29 28 63 26 30 78 46 46 29 3b 20 20 20 20 20 8)(c&0xFF);
ad530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad540 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 \. }
ad550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad580 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c 30 \. else if( c<0
ad590 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20 20 x00800 ){
ad5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad5b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a \. *
ad5c0 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 zOut++ = 0xC0 +
ad5d0 28 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31 46 (u8)((c>>6)&0x1F
ad5e0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ad5f0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
ad600 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 = 0x80 + (u8)(c
ad610 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 & 0x3F);
ad620 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d \. }
ad630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad660 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 \. else if(
ad670 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20 20 c<0x10000 ){
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad690 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad6a0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 *zOut++ = 0xE
ad6b0 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 0 + (u8)((c>>12)
ad6c0 26 30 78 30 46 29 3b 20 20 20 20 20 20 20 20 20 &0x0F);
ad6d0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 \. *zOu
ad6e0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 t++ = 0x80 + (u8
ad6f0 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 )((c>>6) & 0x3F)
ad700 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c ; \
ad710 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 . *zOut++ = 0
ad720 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 x80 + (u8)(c & 0
ad730 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 x3F);
ad740 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 \. }els
ad750 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e{
ad760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad780 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
ad790 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63 3e 0xF0 + (u8)((c>
ad7a0 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20 20 >18) & 0x07);
ad7b0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ad7c0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b *zOut++ = 0x80 +
ad7d0 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26 20 (u8)((c>>12) &
ad7e0 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 0x3F);
ad7f0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad800 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 = 0x80 + (u8)((
ad810 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20 c>>6) & 0x3F);
ad820 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
ad830 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 *zOut++ = 0x80
ad840 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46 + (u8)(c & 0x3F
ad850 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 );
ad860 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 \. }
ad870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ad8a0 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 }..#define WRITE
ad8b0 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 _UTF16LE(zOut, c
ad8c0 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 ) {
ad8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad8e0 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 \. if( c
ad8f0 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 <=0xFFFF ){
ad900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad920 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ad930 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 *zOut++ = (u8
ad940 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 )(c&0x00FF);
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 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b \. *zOut++
ad980 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30 = (u8)((c>>8)&0
ad990 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 x00FF);
ad9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c \. }el
ad9c0 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 se{
ad9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ad9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ada00 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 . *zOut++ = (
ada10 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78 30 u8)(((c>>10)&0x0
ada20 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 03F) + (((c-0x10
ada30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 000)>>10)&0x00C0
ada40 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 )); \. *zOut
ada50 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38 ++ = (u8)(0x00D8
ada60 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29 + (((c-0x10000)
ada70 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20 >>18)&0x03));
ada80 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ada90 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 *zOut++ = (u8)(
adaa0 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 c&0x00FF);
adab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adad0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d \. *zOut++ =
adae0 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28 (u8)(0x00DC + (
adaf0 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20 (c>>8)&0x03));
adb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb10 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 \. }
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adb50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d \.}
adb60 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f ..#define WRITE_
adb70 55 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 UTF16BE(zOut, c)
adb80 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
adb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adba0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c \. if( c<
adbb0 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 =0xFFFF ){
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
adbf0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 *zOut++ = (u8)
adc00 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b ((c>>8)&0x00FF);
adc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adc30 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 \. *zOut++
adc40 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 = (u8)(c&0x00FF)
adc50 3b 20 20 20 20 20 20 20 20 20 20 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 5c 0a 20 20 7d 65 6c 73 \. }els
adc80 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e{
adc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
adcc0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 *zOut++ = (u
adcd0 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 8)(0x00D8 + (((c
adce0 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 -0x10000)>>18)&0
adcf0 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 x03));
add00 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b \. *zOut+
add10 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31 30 + = (u8)(((c>>10
add20 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28 63 )&0x003F) + (((c
add30 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26 30 -0x10000)>>10)&0
add40 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 x00C0)); \.
add50 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 *zOut++ = (u8)(0
add60 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 x00DC + ((c>>8)&
add70 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 0x03));
add80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
add90 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 \. *zOut++ =
adda0 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 (u8)(c&0x00FF);
addb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addd0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 \. }
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a \.}.
ade20 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 .#define READ_UT
ade30 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d 2c F16LE(zIn, TERM,
ade40 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 c){
ade50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade60 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 \. c = (
ade70 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 *zIn++);
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ade90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
adeb0 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b . c += ((*zIn++
adec0 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20 )<<8);
aded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adef0 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 \. if( c
adf00 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78 >=0xD800 && c<0x
adf10 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b 20 E000 && TERM ){
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 5c \
adf40 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a . int c2 = (*
adf50 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 zIn++);
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
adf80 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20 \. c2
adf90 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 += ((*zIn++)<<8)
adfa0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
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 5c \
adfd0 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30 . c = (c2&0x0
adfe0 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33 3FF) + ((c&0x003
adff0 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30 F)<<10) + (((c&0
ae000 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c x03C0)+0x0040)<<
ae010 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 10); \. }
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
ae060 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 .}..#define READ
ae070 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 54 45 _UTF16BE(zIn, TE
ae080 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 RM, c){
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 5c 0a 20 20 63 20 \. c
ae0b0 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b = ((*zIn++)<<8);
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae0f0 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e \. c += (*zIn
ae100 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ++);
ae110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae130 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
ae140 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 ( c>=0xD800 && c
ae150 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d 20 <0xE000 && TERM
ae160 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae180 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d \. int c2 =
ae190 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 ((*zIn++)<<8);
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 20 20 20 5c 0a 20 20 20 20 \.
ae1d0 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 c2 += (*zIn++);
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 20 20 20
ae210 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 \. c = (c2&
ae220 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 0x03FF) + ((c&0x
ae230 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 003F)<<10) + (((
ae240 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 c&0x03C0)+0x0040
ae250 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 )<<10); \. }
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
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 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 \.}../*.** Tra
ae2b0 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 nslate a single
ae2c0 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e UTF-8 character.
ae2d0 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e 69 Return the uni
ae2e0 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a code value..**.*
ae2f0 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 * During transla
ae300 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 tion, assume tha
ae310 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74 20 t the byte that
ae320 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 zTerm points.**
ae330 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a is a 0x00..**.**
ae340 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 Write a pointer
ae350 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e 72 to the next unr
ae360 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69 6e ead byte back in
ae370 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a to *pzNext..**.*
ae380 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c * Notes On Inval
ae390 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 id UTF-8:.**.**
ae3a0 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 * This routine
ae3b0 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 never allows a
ae3c0 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 20 7-bit character
ae3d0 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30 78 (0x00 through 0x
ae3e0 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 7f) to.** be
ae3f0 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d 75 encoded as a mu
ae400 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74 lti-byte charact
ae410 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 er. Any multi-b
ae420 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74 68 yte character th
ae430 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 at.** attemp
ae440 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 ts to encode a v
ae450 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78 30 alue between 0x0
ae460 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72 65 0 and 0x7f is re
ae470 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66 64 ndered as 0xfffd
ae480 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 ..**.** * This
ae490 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 routine never a
ae4a0 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73 75 llows a UTF16 su
ae4b0 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74 6f rrogate value to
ae4c0 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 be encoded..**
ae4d0 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 If a multi-b
ae4e0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61 74 yte character at
ae4f0 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 tempts to encode
ae500 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65 6e a value between
ae510 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20 61 .** 0xd800 a
ae520 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20 69 nd 0xe000 then i
ae530 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 t is rendered as
ae540 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 0xfffd..**.**
ae550 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65 20 * Bytes in the
ae560 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74 68 range of 0x80 th
ae570 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63 68 rough 0xbf which
ae580 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66 69 occur as the fi
ae590 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 rst.** byte
ae5a0 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20 61 of a character a
ae5b0 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 re interpreted a
ae5c0 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 s single-byte ch
ae5d0 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 aracters.**
ae5e0 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73 20 and rendered as
ae5f0 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 themselves even
ae600 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 20 though they are
ae610 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 technically.**
ae620 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72 61 invalid chara
ae630 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 cters..**.** *
ae640 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 This routine ac
ae650 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 cepts an infinit
ae660 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66 e number of diff
ae670 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 erent UTF8 encod
ae680 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 ings.** for
ae690 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20 30 unicode values 0
ae6a0 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72 2e x80 and greater.
ae6b0 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e It do not chan
ae6c0 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a ge over-length.*
ae6d0 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 * encodings
ae6e0 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f 6d to 0xfffd as som
ae6f0 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d e systems recomm
ae700 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 end..*/.#define
ae710 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a READ_UTF8(zIn, z
ae720 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20 20 Term, c)
ae730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae740 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e \. c = *(zIn
ae750 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 ++);
ae760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae780 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 \. if( c>=0xc0
ae790 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ){
ae7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
ae7c0 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 c = sqlite3Ut
ae7d0 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d f8Trans1[c-0xc0]
ae7e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ae7f0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 \.
ae800 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 while( zIn!=zTer
ae810 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 m && (*zIn & 0xc
ae820 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20 20 0)==0x80 ){
ae830 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 63 \. c
ae840 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 = (c<<6) + (0x3
ae850 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 f & *(zIn++));
ae860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae870 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 \. }
ae880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae8b0 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38 \. if( c<0x8
ae8c0 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
ae8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ae8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
ae8f0 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 || (c&0x
ae900 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38 30 FFFFF800)==0xD80
ae910 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
ae920 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
ae930 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 || (c&0xFFF
ae940 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20 29 FFFFE)==0xFFFE )
ae950 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20 7d { c = 0xFFFD; }
ae960 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 \. }.SQ
ae970 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
ae980 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 sqlite3Utf8Read
ae990 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e (. const unsign
ae9a0 65 64 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20 20 ed char *zIn,
ae9b0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 /* First byt
ae9c0 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72 61 e of UTF-8 chara
ae9d0 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 cter */. const
ae9e0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a unsigned char **
ae9f0 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72 69 pzNext /* Wri
aea00 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 te first byte pa
aea10 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68 65 st UTF-8 char he
aea20 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 re */.){. int c
aea30 3b 0a 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73 20 ;.. /* Same as
aea40 52 45 41 44 5f 55 54 46 38 28 29 20 61 62 6f 76 READ_UTF8() abov
aea50 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74 68 e but without th
aea60 65 20 7a 54 65 72 6d 20 70 61 72 61 6d 65 74 65 e zTerm paramete
aea70 72 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 r.. ** For this
aea80 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 73 73 routine, we ass
aea90 75 6d 65 20 74 68 65 20 55 54 46 38 20 73 74 72 ume the UTF8 str
aeaa0 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 7a 65 ing is always ze
aeab0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 20 ro-terminated..
aeac0 20 2a 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b */. c = *(zIn+
aead0 2b 29 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 +);. if( c>=0xc
aeae0 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 0 ){. c = sql
aeaf0 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b 63 ite3Utf8Trans1[c
aeb00 2d 30 78 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c -0xc0];. whil
aeb10 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 e( (*zIn & 0xc0)
aeb20 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20 ==0x80 ){.
aeb30 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 c = (c<<6) + (0x
aeb40 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 3f & *(zIn++));.
aeb50 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 3c }. if( c<
aeb60 30 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 0x80. ||
aeb70 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d (c&0xFFFFF800)==
aeb80 30 78 44 38 30 30 0a 20 20 20 20 20 20 20 20 7c 0xD800. |
aeb90 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45 29 | (c&0xFFFFFFFE)
aeba0 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20 3d ==0xFFFE ){ c =
aebb0 20 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 0xFFFD; }. }.
aebc0 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a *pzNext = zIn;.
aebd0 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a return c;.}...
aebe0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 ../*.** If the T
aebf0 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20 6d RANSLATE_TRACE m
aec00 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2c acro is defined,
aec10 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 61 the value of ea
aec20 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 ch Mem is.** pri
aec30 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20 6f nted on stderr o
aec40 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20 61 n the way into a
aec50 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 nd out of sqlite
aec60 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 3VdbeMemTranslat
aec70 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 e()..*/ ./* #def
aec80 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 ine TRANSLATE_TR
aec90 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 ACE 1 */..#ifnde
aeca0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
aecb0 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 F16./*.** This r
aecc0 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d outine transform
aecd0 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 s the internal t
aece0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 ext encoding use
aecf0 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 d by pMem to.**
aed00 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20 69 desiredEnc. It i
aed10 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 s an error if th
aed20 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72 65 e string is alre
aed30 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69 72 ady of the desir
aed40 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 ed.** encoding,
aed50 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 or if *pMem does
aed60 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 not contain a s
aed70 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a tring value..*/.
aed80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
aed90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
aeda0 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a mTranslate(Mem *
aedb0 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65 64 pMem, u8 desired
aedc0 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b Enc){. int len;
aedd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
aede0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c /* Maximum l
aedf0 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74 20 ength of output
aee00 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73 20 string in bytes
aee10 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
aee20 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 ar *zOut;
aee30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 /* Ou
aee40 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 tput buffer */.
aee50 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
aee60 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 zIn;
aee70 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 /* Input
aee80 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e iterator */. un
aee90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 signed char *zTe
aeea0 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rm;
aeeb0 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e /* End of in
aeec0 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 put */. unsigne
aeed0 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 d char *z;
aeee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
aeef0 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74 6f * Output iterato
aef00 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 r */. unsigned
aef10 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 int c;.. assert
aef20 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
aef30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
aef40 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
aef50 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
aef60 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 ( pMem->flags&ME
aef70 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65 72 M_Str );. asser
aef80 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 t( pMem->enc!=de
aef90 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 siredEnc );. as
aefa0 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 sert( pMem->enc!
aefb0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
aefc0 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 pMem->n>=0 );..#
aefd0 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 if defined(TRANS
aefe0 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 LATE_TRACE) && d
aeff0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 efined(SQLITE_DE
af000 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 BUG). {. cha
af010 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 r zBuf[100];.
af020 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 sqlite3VdbeMemP
af030 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c rettyPrint(pMem,
af040 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 zBuf);. fpri
af050 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e 50 ntf(stderr, "INP
af060 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 UT: %s\n", zBuf
af070 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 );. }.#endif..
af080 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e 73 /* If the trans
af090 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65 65 lation is betwee
af0a0 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 n UTF-16 little
af0b0 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 and big endian,
af0c0 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 then . ** all t
af0d0 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 20 hat is required
af0e0 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20 62 is to swap the b
af0f0 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73 20 yte order. This
af100 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 0a case is handled.
af110 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 ** differently
af120 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 from the others
af130 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 .. */. if( pMe
af140 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 m->enc!=SQLITE_U
af150 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45 6e TF8 && desiredEn
af160 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 c!=SQLITE_UTF8 )
af170 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 {. u8 temp;.
af180 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 int rc;. r
af190 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d c = sqlite3VdbeM
af1a0 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 emMakeWriteable(
af1b0 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 pMem);. if( r
af1c0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
af1d0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 assert( rc
af1e0 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
af1f0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
af200 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
af210 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 }. zIn = (u8
af220 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a *)pMem->z;. z
af230 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d Term = &zIn[pMem
af240 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 ->n&~1];. whi
af250 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b le( zIn<zTerm ){
af260 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a . temp = *z
af270 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d In;. *zIn =
af280 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 *(zIn+1);.
af290 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a zIn++;. *z
af2a0 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 In++ = temp;.
af2b0 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 }. pMem->enc
af2c0 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 = desiredEnc;.
af2d0 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 goto translat
af2e0 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a e_out;. }.. /*
af2f0 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 Set len to the
af300 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f maximum number o
af310 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 f bytes required
af320 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 in the output b
af330 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 uffer. */. if(
af340 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 desiredEnc==SQLI
af350 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 2f TE_UTF8 ){. /
af360 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e * When convertin
af370 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 g from UTF-16, t
af380 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 he maximum growt
af390 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 h results from.
af3a0 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e ** translatin
af3b0 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72 61 g a 2-byte chara
af3c0 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74 65 cter to a 4-byte
af3d0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 UTF-8 character
af3e0 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c .. ** A singl
af3f0 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69 72 e byte is requir
af400 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 ed for the outpu
af410 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 t string. **
af420 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a nul-terminator..
af430 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d */. pMem-
af440 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 >n &= ~1;. le
af450 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 n = pMem->n * 2
af460 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 + 1;. }else{.
af470 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 /* When conver
af480 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 ting from UTF-8
af490 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d 61 to UTF-16 the ma
af4a0 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73 20 ximum growth is
af4b0 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 caused. ** wh
af4c0 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 46 2d en a 1-byte UTF-
af4d0 38 20 63 68 61 72 61 63 74 65 72 20 69 73 20 74 8 character is t
af4e0 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 ranslated into a
af4f0 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 0a 20 2-byte UTF-16.
af500 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72 2e ** character.
af510 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 20 72 Two bytes are r
af520 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f equired in the o
af530 75 74 70 75 74 20 62 75 66 66 65 72 20 66 6f 72 utput buffer for
af540 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d the. ** nul-
af550 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 terminator..
af560 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 */. len = pMe
af570 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 m->n * 2 + 2;.
af580 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 }.. /* Set zIn
af590 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 to point at the
af5a0 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 6e 70 start of the inp
af5b0 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 7a 54 ut buffer and zT
af5c0 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 erm to point 1.
af5d0 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 74 68 ** byte past th
af5e0 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a e end.. **. **
af5f0 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 Variable zOut i
af600 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 s set to point a
af610 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 t the output buf
af620 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 61 69 fer, space obtai
af630 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 ned. ** from sq
af640 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a lite3_malloc()..
af650 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 */. zIn = (u8
af660 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 *)pMem->z;. zTe
af670 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e rm = &zIn[pMem->
af680 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c n];. zOut = sql
af690 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
af6a0 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a pMem->db, len);.
af6b0 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 if( !zOut ){.
af6c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
af6d0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 _NOMEM;. }. z
af6e0 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 = zOut;.. if( p
af6f0 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 Mem->enc==SQLITE
af700 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 66 28 _UTF8 ){. if(
af710 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c desiredEnc==SQL
af720 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 ITE_UTF16LE ){.
af730 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e /* UTF-8 ->
af740 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 UTF-16 Little-e
af750 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 ndian */. w
af760 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 hile( zIn<zTerm
af770 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 ){. /* c
af780 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
af790 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 d(zIn, zTerm, (c
af7a0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b onst u8**)&zIn);
af7b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 */. READ
af7c0 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d _UTF8(zIn, zTerm
af7d0 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 , c);. WR
af7e0 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 ITE_UTF16LE(z, c
af7f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
af800 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 else{. asse
af810 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d rt( desiredEnc==
af820 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 SQLITE_UTF16BE )
af830 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 ;. /* UTF-8
af840 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d 65 -> UTF-16 Big-e
af850 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 ndian */. w
af860 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 hile( zIn<zTerm
af870 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63 20 ){. /* c
af880 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 = sqlite3Utf8Rea
af890 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 d(zIn, zTerm, (c
af8a0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b onst u8**)&zIn);
af8b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41 44 */. READ
af8c0 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d _UTF8(zIn, zTerm
af8d0 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57 52 , c);. WR
af8e0 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 ITE_UTF16BE(z, c
af8f0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
af900 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 . pMem->n = (
af910 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a int)(z - zOut);.
af920 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 *z++ = 0;.
af930 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
af940 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 t( desiredEnc==S
af950 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 QLITE_UTF8 );.
af960 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d if( pMem->enc=
af970 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 =SQLITE_UTF16LE
af980 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d ){. /* UTF-
af990 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 16 Little-endian
af9a0 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 -> UTF-8 */.
af9b0 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 while( zIn<zT
af9c0 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 erm ){. R
af9d0 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c EAD_UTF16LE(zIn,
af9e0 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 zIn<zTerm, c);
af9f0 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 . WRITE_U
afa00 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 TF8(z, c);.
afa10 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
afa20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69 /* UTF-16 Bi
afa30 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d g-endian -> UTF-
afa40 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 8 */. while
afa50 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 ( zIn<zTerm ){.
afa60 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 READ_UTF1
afa70 36 42 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 65 6BE(zIn, zIn<zTe
afa80 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 rm, c); .
afa90 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 WRITE_UTF8(z, c
afaa0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
afab0 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 . pMem->n = (
afac0 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a int)(z - zOut);.
afad0 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 }. *z = 0;.
afae0 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 6e assert( (pMem->n
afaf0 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 +(desiredEnc==SQ
afb00 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 29 3c LITE_UTF8?1:2))<
afb10 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69 74 =len );.. sqlit
afb20 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
afb30 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e (pMem);. pMem->
afb40 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 53 flags &= ~(MEM_S
afb50 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 tatic|MEM_Dyn|ME
afb60 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65 6d M_Ephem);. pMem
afb70 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45 ->enc = desiredE
afb80 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 nc;. pMem->flag
afb90 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c 4d s |= (MEM_Term|M
afba0 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d 2d EM_Dyn);. pMem-
afbb0 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75 74 >z = (char*)zOut
afbc0 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f ;. pMem->zMallo
afbd0 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74 72 c = pMem->z;..tr
afbe0 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69 66 anslate_out:.#if
afbf0 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 defined(TRANSLA
afc00 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 TE_TRACE) && def
afc10 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 ined(SQLITE_DEBU
afc20 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 G). {. char
afc30 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 zBuf[100];. s
afc40 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 qlite3VdbeMemPre
afc50 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a ttyPrint(pMem, z
afc60 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 Buf);. fprint
afc70 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54 50 55 f(stderr, "OUTPU
afc80 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b T: %s\n", zBuf);
afc90 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 . }.#endif. re
afca0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
afcb0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f }../*.** This ro
afcc0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f 72 utine checks for
afcd0 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d 61 a byte-order ma
afce0 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e rk at the beginn
afcf0 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20 55 ing of the .** U
afd00 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73 74 6f TF-16 string sto
afd10 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49 66 red in *pMem. If
afd20 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e 74 2c one is present,
afd30 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64 20 61 it is removed a
afd40 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64 69 nd.** the encodi
afd50 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 61 64 ng of the Mem ad
afd60 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72 6f 75 justed. This rou
afd70 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64 6f tine does not do
afd80 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77 61 any.** byte-swa
afd90 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74 20 73 pping, it just s
afda0 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70 72 ets Mem.enc appr
afdb0 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a opriately..**.**
afdc0 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 The allocation
afdd0 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d 69 63 (static, dynamic
afde0 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f 64 etc.) and encod
afdf0 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 6d ing of the Mem m
afe00 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65 64 ay be.** changed
afe10 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
afe20 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
afe30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
afe40 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d VdbeMemHandleBom
afe50 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 (Mem *pMem){. i
afe60 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
afe70 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30 3b K;. u8 bom = 0;
afe80 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d .. assert( pMem
afe90 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 ->n>=0 );. if(
afea0 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20 20 pMem->n>1 ){.
afeb0 20 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a 29 u8 b1 = *(u8 *)
afec0 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38 20 pMem->z;. u8
afed0 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70 4d b2 = *(((u8 *)pM
afee0 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20 20 em->z) + 1);.
afef0 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26 26 if( b1==0xFE &&
aff00 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20 20 b2==0xFF ){.
aff10 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45 5f bom = SQLITE_
aff20 55 54 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a 20 UTF16BE;. }.
aff30 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 46 20 if( b1==0xFF
aff40 26 26 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a 20 && b2==0xFE ){.
aff50 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 bom = SQLIT
aff60 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 7d E_UTF16LE;. }
aff70 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 62 6f . }. . if( bo
aff80 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 m ){. rc = sq
aff90 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 lite3VdbeMemMake
affa0 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b Writeable(pMem);
affb0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
affc0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
affd0 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20 20 pMem->n -= 2;.
affe0 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65 6d memmove(pMem
afff0 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32 5d ->z, &pMem->z[2]
b0000 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 , pMem->n);.
b0010 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e pMem->z[pMem->
b0020 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 n] = '\0';.
b0030 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e pMem->z[pMem->n
b0040 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 +1] = '\0';.
b0050 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d pMem->flags |=
b0060 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 MEM_Term;.
b0070 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f 6d pMem->enc = bom
b0080 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
b0090 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
b00a0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
b00b0 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a _UTF16 */../*.**
b00c0 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20 65 pZ is a UTF-8 e
b00d0 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20 73 ncoded unicode s
b00e0 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65 20 tring. If nByte
b00f0 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 is less than zer
b0100 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 o,.** return the
b0110 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63 6f number of unico
b0120 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69 6e de characters in
b0130 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20 6e pZ up to (but n
b0140 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a 2a ot including).**
b0150 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30 20 the first 0x00
b0160 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20 69 byte. If nByte i
b0170 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e 20 s not less than
b0180 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68 65 zero, return the
b0190 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e .** number of un
b01a0 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 icode characters
b01b0 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e 42 in the first nB
b01c0 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75 70 yte of pZ (or up
b01d0 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 to .** the firs
b01e0 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76 65 t 0x00, whicheve
b01f0 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a r comes first)..
b0200 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b0210 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 E int sqlite3Utf
b0220 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20 63 8CharLen(const c
b0230 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 42 har *zIn, int nB
b0240 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 yte){. int r =
b0250 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 0;. const u8 *z
b0260 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a 49 = (const u8*)zI
b0270 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a n;. const u8 *z
b0280 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79 74 Term;. if( nByt
b0290 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65 72 e>=0 ){. zTer
b02a0 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a 20 m = &z[nByte];.
b02b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65 72 }else{. zTer
b02c0 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 28 m = (const u8*)(
b02d0 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 -1);. }. asser
b02e0 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a 20 t( z<=zTerm );.
b02f0 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26 26 while( *z!=0 &&
b0300 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 z<zTerm ){.
b0310 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 SQLITE_SKIP_UTF8
b0320 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20 20 (z);. r++;.
b0330 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a }. return r;.}.
b0340 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66 75 ./* This test fu
b0350 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 75 nction is not cu
b0360 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79 20 rrently used by
b0370 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74 65 the automated te
b0380 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48 65 st-suite. .** He
b0390 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20 61 nce it is only a
b03a0 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62 75 vailable in debu
b03b0 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 g builds..*/.#if
b03c0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f defined(SQLITE_
b03d0 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65 64 TEST) && defined
b03e0 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f (SQLITE_DEBUG)./
b03f0 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 55 *.** Translate U
b0400 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a 2a TF-8 to UTF-8..*
b0410 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 20 74 68 *.** This has th
b0420 65 20 65 66 66 65 63 74 20 6f 66 20 6d 61 6b 69 e effect of maki
b0430 6e 67 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ng sure that the
b0440 20 73 74 72 69 6e 67 20 69 73 20 77 65 6c 6c 2d string is well-
b0450 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38 2e formed.** UTF-8.
b0460 20 20 4d 69 73 63 6f 64 65 64 20 63 68 61 72 61 Miscoded chara
b0470 63 74 65 72 73 20 61 72 65 20 72 65 6d 6f 76 65 cters are remove
b0480 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72 61 d..**.** The tra
b0490 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 nslation is done
b04a0 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63 65 in-place (since
b04b0 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c it is impossibl
b04c0 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f 72 e for the.** cor
b04d0 72 65 63 74 20 55 54 46 2d 38 20 65 6e 63 6f 64 rect UTF-8 encod
b04e0 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65 72 ing to be longer
b04f0 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d 65 than a malforme
b0500 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f 0a d encoding)..*/.
b0510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
b0520 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54 6f nt sqlite3Utf8To
b0530 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 8(unsigned char
b0540 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e 65 *zIn){. unsigne
b0550 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20 7a d char *zOut = z
b0560 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 In;. unsigned c
b0570 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a 49 har *zStart = zI
b0580 6e 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20 77 n;. u32 c;.. w
b0590 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b 0a hile( zIn[0] ){.
b05a0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 c = sqlite3U
b05b0 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 28 63 6f tf8Read(zIn, (co
b05c0 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a nst u8**)&zIn);.
b05d0 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66 66 if( c!=0xfff
b05e0 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54 45 d ){. WRITE
b05f0 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a _UTF8(zOut, c);.
b0600 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 }. }. *zOu
b0610 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 t = 0;. return
b0620 28 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53 74 (int)(zOut - zSt
b0630 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a art);.}.#endif..
b0640 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
b0650 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 MIT_UTF16./*.**
b0660 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31 36 Convert a UTF-16
b0670 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 6e string in the n
b0680 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20 69 ative encoding i
b0690 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69 nto a UTF-8 stri
b06a0 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f ng..** Memory to
b06b0 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38 20 hold the UTF-8
b06c0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e string is obtain
b06d0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f ed from sqlite3_
b06e0 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 0a malloc and must.
b06f0 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20 74 ** be freed by t
b0700 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
b0710 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c 20 ion..**.** NULL
b0720 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
b0730 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f 63 here is an alloc
b0740 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a ation error..*/.
b0750 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 SQLITE_PRIVATE c
b0760 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66 31 har *sqlite3Utf1
b0770 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64 62 6to8(sqlite3 *db
b0780 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c , const void *z,
b0790 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d int nByte){. M
b07a0 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 26 em m;. memset(&
b07b0 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 m, 0, sizeof(m))
b07c0 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a 20 ;. m.db = db;.
b07d0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
b07e0 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42 79 etStr(&m, z, nBy
b07f0 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 te, SQLITE_UTF16
b0800 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 NATIVE, SQLITE_S
b0810 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 TATIC);. sqlite
b0820 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 3VdbeChangeEncod
b0830 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f 55 ing(&m, SQLITE_U
b0840 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e TF8);. if( db->
b0850 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
b0860 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
b0870 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 emRelease(&m);.
b0880 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d 0a m.z = 0;. }.
b0890 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61 assert( (m.fla
b08a0 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d gs & MEM_Term)!=
b08b0 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 0 || db->mallocF
b08c0 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72 ailed );. asser
b08d0 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 t( (m.flags & ME
b08e0 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62 2d M_Str)!=0 || db-
b08f0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
b0900 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c 61 . return (m.fla
b0910 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 gs & MEM_Dyn)!=0
b0920 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65 33 ? m.z : sqlite3
b0930 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e 7a DbStrDup(db, m.z
b0940 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 );.}../*.** Conv
b0950 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72 69 ert a UTF-8 stri
b0960 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31 36 ng to the UTF-16
b0970 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 encoding specif
b0980 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 ied by parameter
b0990 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e 74 .** enc. A point
b09a0 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 74 er to the new st
b09b0 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64 ring is returned
b09c0 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 , and the value
b09d0 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73 20 of *pnOut.** is
b09e0 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 set to the lengt
b09f0 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 h of the returne
b0a00 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 d string in byte
b0a10 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f 75 s. The call shou
b0a20 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74 6f ld.** arrange to
b0a30 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 call sqlite3DbF
b0a40 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65 74 ree() on the ret
b0a50 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77 68 urned pointer wh
b0a60 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20 6c en it is.** no l
b0a70 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a onger required..
b0a80 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c 6c ** .** If a mall
b0a90 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 oc failure occur
b0aa0 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 s, NULL is retur
b0ab0 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e 6d ned and the db.m
b0ac0 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20 66 allocFailed.** f
b0ad0 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66 64 lag set..*/.#ifd
b0ae0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
b0af0 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 52 _STAT2.SQLITE_PR
b0b00 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
b0b10 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 te3Utf8to16(sqli
b0b20 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63 2c te3 *db, u8 enc,
b0b30 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c char *z, int n,
b0b40 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 int *pnOut){.
b0b50 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28 Mem m;. memset(
b0b60 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 &m, 0, sizeof(m)
b0b70 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a );. m.db = db;.
b0b80 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
b0b90 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 2c SetStr(&m, z, n,
b0ba0 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 SQLITE_UTF8, SQ
b0bb0 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 LITE_STATIC);.
b0bc0 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d if( sqlite3VdbeM
b0bd0 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c 20 emTranslate(&m,
b0be0 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73 65 enc) ){. asse
b0bf0 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 rt( db->mallocFa
b0c00 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75 iled );. retu
b0c10 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 rn 0;. }. asse
b0c20 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c 6c rt( m.z==m.zMall
b0c30 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d oc );. *pnOut =
b0c40 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20 6d m.n;. return m
b0c50 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a .z;.}.#endif../*
b0c60 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54 46 .** zIn is a UTF
b0c70 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 -16 encoded unic
b0c80 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c 65 ode string at le
b0c90 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61 63 ast nChar charac
b0ca0 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 ters long..** Re
b0cb0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
b0cc0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 of bytes in the
b0cd0 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69 63 first nChar unic
b0ce0 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a ode characters.*
b0cf0 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72 20 * in pZ. nChar
b0d00 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 must be non-nega
b0d10 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tive..*/.SQLITE_
b0d20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b0d30 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 te3Utf16ByteLen(
b0d40 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e 2c const void *zIn,
b0d50 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20 69 int nChar){. i
b0d60 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 nt c;. unsigned
b0d70 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20 3d char const *z =
b0d80 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 zIn;. int n =
b0d90 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c 49 0;. . if( SQLI
b0da0 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d TE_UTF16NATIVE==
b0db0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 SQLITE_UTF16BE )
b0dc0 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e {. while( n<n
b0dd0 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 Char ){. RE
b0de0 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31 2c AD_UTF16BE(z, 1,
b0df0 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a c);. n++;.
b0e00 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
b0e10 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68 61 while( n<nCha
b0e20 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f r ){. READ_
b0e30 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29 UTF16LE(z, 1, c)
b0e40 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 ;. n++;.
b0e50 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
b0e60 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e 65 (int)(z-(unsigne
b0e70 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29 7a d char const *)z
b0e80 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 In);.}..#if defi
b0e90 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 ned(SQLITE_TEST)
b0ea0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
b0eb0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 72 ine is called fr
b0ec0 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74 20 om the TCL test
b0ed0 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73 6c function "transl
b0ee0 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a 2a ate_selftest"..*
b0ef0 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61 74 * It checks that
b0f00 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73 20 the primitives
b0f10 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20 for serializing
b0f20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69 6e and deserializin
b0f30 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 g.** characters
b0f40 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e 67 in each encoding
b0f50 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f 66 are inverses of
b0f60 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a each other..*/.
b0f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
b0f80 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53 65 oid sqlite3UtfSe
b0f90 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20 20 lfTest(void){.
b0fa0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c 20 unsigned int i,
b0fb0 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 t;. unsigned ch
b0fc0 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20 75 ar zBuf[20];. u
b0fd0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b nsigned char *z;
b0fe0 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 . int n;. unsi
b0ff0 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20 66 gned int c;.. f
b1000 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 31 or(i=0; i<0x0011
b1010 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 0000; i++){.
b1020 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 z = zBuf;. WR
b1030 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b 0a ITE_UTF8(z, i);.
b1040 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d n = (int)(z-
b1050 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 zBuf);. asser
b1060 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 t( n>0 && n<=4 )
b1070 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a ;. z[0] = 0;.
b1080 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 z = zBuf;.
b1090 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 c = sqlite3Utf
b10a0 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74 20 8Read(z, (const
b10b0 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74 20 u8**)&z);. t
b10c0 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e 3d = i;. if( i>=
b10d0 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78 44 0xD800 && i<=0xD
b10e0 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46 44 FFF ) t = 0xFFFD
b10f0 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78 46 ;. if( (i&0xF
b1100 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 FFFFFFE)==0xFFFE
b1110 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a 20 ) t = 0xFFFD;.
b1120 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74 20 assert( c==t
b1130 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
b1140 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 z-zBuf)==n );.
b1150 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 }. for(i=0; i<0
b1160 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b x00110000; i++){
b1170 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44 38 . if( i>=0xD8
b1180 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20 29 00 && i<0xE000 )
b1190 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a continue;. z
b11a0 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 = zBuf;. WRI
b11b0 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69 29 TE_UTF16LE(z, i)
b11c0 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 ;. n = (int)(
b11d0 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 z-zBuf);. ass
b11e0 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 ert( n>0 && n<=4
b11f0 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 );. z[0] = 0
b1200 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a ;. z = zBuf;.
b1210 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 READ_UTF16LE
b1220 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 (z, 1, c);. a
b1230 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 ssert( c==i );.
b1240 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 assert( (z-zB
b1250 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 uf)==n );. }.
b1260 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31 for(i=0; i<0x001
b1270 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 10000; i++){.
b1280 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 if( i>=0xD800 &
b1290 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e & i<0xE000 ) con
b12a0 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a tinue;. z = z
b12b0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55 Buf;. WRITE_U
b12c0 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20 TF16BE(z, i);.
b12d0 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42 n = (int)(z-zB
b12e0 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 uf);. assert(
b12f0 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a n>0 && n<=4 );.
b1300 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 z[0] = 0;.
b1310 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 z = zBuf;.
b1320 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 READ_UTF16BE(z,
b1330 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65 72 1, c);. asser
b1340 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 t( c==i );. a
b1350 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d ssert( (z-zBuf)=
b1360 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 =n );. }.}.#end
b1370 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 if /* SQLITE_TES
b1380 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 T */.#endif /* S
b1390 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
b13a0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
b13b0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66 2e **** End of utf.
b13c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
b13d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b13e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b13f0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
b1400 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
b1410 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a util.c *********
b1420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1440 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 ***/./*.** 2001
b1450 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a September 15.**.
b1460 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
b1470 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
b1480 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
b1490 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
b14a0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
b14b0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
b14c0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
b14d0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
b14e0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
b14f0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
b1500 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
b1510 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
b1520 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
b1530 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
b1540 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
b1550 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
b1560 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
b1570 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
b1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b15b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 ***********.** U
b15c0 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 tility functions
b15d0 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 used throughout
b15e0 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 sqlite..**.** T
b15f0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
b1600 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 s functions for
b1610 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 allocating memor
b1620 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 y, comparing.**
b1630 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 74 75 strings, and stu
b1640 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a 2a ff like that..**
b1650 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
b1660 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 20 69 E_HAVE_ISNAN.# i
b1670 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e 0a nclude <math.h>.
b1680 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f #endif../*.** Ro
b1690 75 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 utine needed to
b16a0 73 75 70 70 6f 72 74 20 74 68 65 20 74 65 73 74 support the test
b16b0 63 61 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f case() macro..*/
b16c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 .#ifdef SQLITE_C
b16d0 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c OVERAGE_TEST.SQL
b16e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b16f0 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 sqlite3Coverage
b1700 28 69 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 69 (int x){. stati
b1710 63 20 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b c int dummy = 0;
b1720 0a 20 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d . dummy += x;.}
b1730 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
b1740 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
b1750 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 e floating point
b1760 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61 20 value is Not a
b1770 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a 2a Number (NaN)..**
b1780 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74 68 .** Use the math
b1790 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28 29 library isnan()
b17a0 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f 6d function if com
b17b0 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 piled with SQLIT
b17c0 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a 2a E_HAVE_ISNAN..**
b17d0 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20 68 Otherwise, we h
b17e0 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70 6c ave our own impl
b17f0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 ementation that
b1800 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 79 works on most sy
b1810 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 stems..*/.SQLITE
b1820 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b1830 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 ite3IsNaN(double
b1840 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 x){. int rc;
b1850 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 /* The value re
b1860 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65 66 turn */.#if !def
b1870 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 ined(SQLITE_HAVE
b1880 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 2a _ISNAN). /*. *
b1890 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20 73 * Systems that s
b18a0 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61 6e upport the isnan
b18b0 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 () library funct
b18c0 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 ion should proba
b18d0 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 73 bly. ** make us
b18e0 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70 69 e of it by compi
b18f0 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c 49 ling with -DSQLI
b1900 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20 20 TE_HAVE_ISNAN.
b1910 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a 2a But we have. **
b1920 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e 79 found that many
b1930 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74 20 systems do not
b1940 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20 69 have a working i
b1950 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 snan() function
b1960 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d 70 so. ** this imp
b1970 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 70 lementation is p
b1980 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61 6c rovided as an al
b1990 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a 0a ternative.. **.
b19a0 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74 65 ** This NaN te
b19b0 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61 69 st sometimes fai
b19c0 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 6f ls if compiled o
b19d0 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61 73 n GCC with -ffas
b19e0 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20 t-math.. ** On
b19f0 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 the other hand,
b1a00 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61 73 the use of -ffas
b1a10 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69 74 t-math comes wit
b1a20 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a h the following.
b1a30 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 20 ** warning:.
b1a40 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 69 **. ** Thi
b1a50 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73 74 s option [-ffast
b1a60 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e 65 -math] should ne
b1a70 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f 6e ver be turned on
b1a80 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20 20 by any. **
b1a90 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e 63 -O option sinc
b1aa0 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74 20 e it can result
b1ab0 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75 74 in incorrect out
b1ac0 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d 73 put for programs
b1ad0 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63 68 . ** which
b1ae0 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65 78 depend on an ex
b1af0 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 act implementati
b1b00 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49 53 on of IEEE or IS
b1b10 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75 6c O . ** rul
b1b20 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f 6e es/specification
b1b30 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63 74 s for math funct
b1b40 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ions.. **. **
b1b50 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69 73 Under MSVC, this
b1b60 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66 61 NaN test may fa
b1b70 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 il if compiled w
b1b80 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d 0a ith a floating-.
b1b90 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63 69 ** point preci
b1ba0 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72 20 sion mode other
b1bb0 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73 65 than /fp:precise
b1bc0 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44 4e . From the MSDN
b1bd0 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 . ** documenta
b1be0 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 tion:. **. **
b1bf0 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 The compile
b1c00 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65 63 r [with /fp:prec
b1c10 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65 72 ise] will proper
b1c20 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61 72 ly handle compar
b1c30 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20 20 isons . **
b1c40 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e 20 involving NaN.
b1c50 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20 21 For example, x !
b1c60 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74 6f = x evaluates to
b1c70 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e 61 true if x is Na
b1c80 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e 2e N . ** ...
b1c90 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 46 . */.#ifdef __F
b1ca0 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 72 AST_MATH__.# err
b1cb0 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e or SQLite will n
b1cc0 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c ot work correctl
b1cd0 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61 73 y with the -ffas
b1ce0 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f 66 t-math option of
b1cf0 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20 76 GCC..#endif. v
b1d00 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 79 olatile double y
b1d10 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c 65 = x;. volatile
b1d20 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a 20 double z = y;.
b1d30 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 65 rc = (y!=z);.#e
b1d40 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69 6e lse /* if defin
b1d50 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 ed(SQLITE_HAVE_I
b1d60 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d 20 SNAN) */. rc =
b1d70 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69 66 isnan(x);.#endif
b1d80 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45 5f /* SQLITE_HAVE_
b1d90 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74 63 ISNAN */. testc
b1da0 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65 74 ase( rc );. ret
b1db0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
b1dc0 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69 6e Compute a strin
b1dd0 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69 73 g length that is
b1de0 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61 74 limited to what
b1df0 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20 69 can be stored i
b1e00 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62 69 n.** lower 30 bi
b1e10 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20 73 ts of a 32-bit s
b1e20 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a igned integer..*
b1e30 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 *.** The value r
b1e40 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65 76 eturned will nev
b1e50 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e 20 er be negative.
b1e60 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76 65 Nor will it eve
b1e70 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a 20 r be greater.**
b1e80 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c 20 than the actual
b1e90 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74 length of the st
b1ea0 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79 20 ring. For very
b1eb0 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67 72 long strings (gr
b1ec0 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31 47 eater.** than 1G
b1ed0 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72 65 iB) the value re
b1ee0 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65 20 turned might be
b1ef0 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 less than the tr
b1f00 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 ue string length
b1f10 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b1f20 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
b1f30 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63 68 trlen30(const ch
b1f40 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 20 ar *z){. const
b1f50 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 char *z2 = z;.
b1f60 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 if( z==0 ) retur
b1f70 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a n 0;. while( *z
b1f80 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 2 ){ z2++; }. r
b1f90 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66 66 eturn 0x3fffffff
b1fa0 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 & (int)(z2 - z)
b1fb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 ;.}../*.** Set t
b1fc0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 he most recent e
b1fd0 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 rror code and er
b1fe0 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 74 ror string for t
b1ff0 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e he sqlite.** han
b2000 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65 72 dle "db". The er
b2010 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74 20 ror code is set
b2020 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a to "err_code"..*
b2030 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f *.** If it is no
b2040 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a t NULL, string z
b2050 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65 73 Format specifies
b2060 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 the format of t
b2070 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 he.** error stri
b2080 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65 20 ng in the style
b2090 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66 75 of the printf fu
b20a0 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c nctions: The fol
b20b0 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 lowing.** format
b20c0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 characters are
b20d0 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 allowed:.**.**
b20e0 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 %s Inse
b20f0 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 rt a string.**
b2100 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 %z A st
b2110 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 ring that should
b2120 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20 be freed after
b2130 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 use.** %d
b2140 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e Insert an in
b2150 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 teger.** %T
b2160 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 Insert a t
b2170 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 oken.** %S
b2180 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 Insert the
b2190 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 first element of
b21a0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a a SrcList.**.**
b21b0 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 zFormat and any
b21c0 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 string tokens t
b21d0 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 hat follow it ar
b21e0 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 0a e assumed to be.
b21f0 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 ** encoded in UT
b2200 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c F-8..**.** To cl
b2210 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 ear the most rec
b2220 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73 71 ent error for sq
b2230 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62 22 lite handle "db"
b2240 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a , sqlite3Error.*
b2250 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c * should be call
b2260 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64 65 ed with err_code
b2270 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
b2280 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 K and zFormat se
b2290 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f t.** to NULL..*/
b22a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b22b0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f void sqlite3Erro
b22c0 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 r(sqlite3 *db, i
b22d0 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e nt err_code, con
b22e0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
b22f0 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 , ...){. if( db
b2300 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c && (db->pErr ||
b2310 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c (db->pErr = sql
b2320 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 ite3ValueNew(db)
b2330 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62 2d )!=0) ){. db-
b2340 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 >errCode = err_c
b2350 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f ode;. if( zFo
b2360 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63 68 rmat ){. ch
b2370 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f ar *z;. va_
b2380 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20 76 list ap;. v
b2390 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
b23a0 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 mat);. z =
b23b0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 sqlite3VMPrintf(
b23c0 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 db, zFormat, ap)
b23d0 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28 61 ;. va_end(a
b23e0 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 p);. sqlite
b23f0 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3ValueSetStr(db-
b2400 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 >pErr, -1, z, SQ
b2410 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 LITE_UTF8, SQLIT
b2420 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 E_DYNAMIC);.
b2430 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
b2440 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 ite3ValueSetStr(
b2450 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 db->pErr, 0, 0,
b2460 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
b2470 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 ITE_STATIC);.
b2480 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
b2490 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 Add an error mes
b24a0 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d 3e sage to pParse->
b24b0 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 zErrMsg and incr
b24c0 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 ement pParse->nE
b24d0 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f rr..** The follo
b24e0 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 wing formatting
b24f0 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 61 characters are a
b2500 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 llowed:.**.**
b2510 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 %s Inser
b2520 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 t a string.**
b2530 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 %z A str
b2540 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 ing that should
b2550 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20 75 be freed after u
b2560 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 se.** %d
b2570 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 Insert an int
b2580 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 eger.** %T
b2590 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f Insert a to
b25a0 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 ken.** %S
b25b0 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 Insert the f
b25c0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 irst element of
b25d0 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 a SrcList.**.**
b25e0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 This function sh
b25f0 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f 20 ould be used to
b2600 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 report any error
b2610 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 68 69 that occurs whi
b2620 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 lst.** compiling
b2630 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
b2640 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 t (i.e. within s
b2650 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
b2660 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 ). The.** last t
b2670 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 hing the sqlite3
b2680 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63 74 _prepare() funct
b2690 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70 79 ion does is copy
b26a0 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 the error.** st
b26b0 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e ored by this fun
b26c0 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 ction into the d
b26d0 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 75 atabase handle u
b26e0 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 72 6f sing sqlite3Erro
b26f0 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e r()..** Function
b2700 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 sqlite3Error()
b2710 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 64 should be used d
b2720 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 uring statement
b2730 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 execution.** (sq
b2740 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 74 63 lite3_step() etc
b2750 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 .)..*/.SQLITE_PR
b2760 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
b2770 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73 65 e3ErrorMsg(Parse
b2780 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 *pParse, const
b2790 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e char *zFormat, .
b27a0 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 ..){. va_list a
b27b0 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 p;. sqlite3 *db
b27c0 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 = pParse->db;.
b27d0 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b pParse->nErr++;
b27e0 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
b27f0 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 (db, pParse->zEr
b2800 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61 72 rMsg);. va_star
b2810 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a t(ap, zFormat);.
b2820 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 pParse->zErrMs
b2830 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 g = sqlite3VMPri
b2840 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c ntf(db, zFormat,
b2850 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 ap);. va_end(a
b2860 70 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 p);. pParse->rc
b2870 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
b2880 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
b2890 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 the error messag
b28a0 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66 20 e in pParse, if
b28b0 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 any.*/.SQLITE_PR
b28c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
b28d0 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61 72 e3ErrorClear(Par
b28e0 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 se *pParse){. s
b28f0 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61 qlite3DbFree(pPa
b2900 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d rse->db, pParse-
b2910 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50 61 >zErrMsg);. pPa
b2920 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 rse->zErrMsg = 0
b2930 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 ;. pParse->nErr
b2940 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 = 0;.}../*.** C
b2950 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 onvert an SQL-st
b2960 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69 6e yle quoted strin
b2970 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 g into a normal
b2980 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 string by removi
b2990 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 ng.** the quote
b29a0 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 65 characters. The
b29b0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 conversion is d
b29c0 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 one in-place. I
b29d0 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 f the.** input d
b29e0 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 oes not begin wi
b29f0 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72 61 th a quote chara
b2a00 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 cter, then this
b2a10 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 routine.** is a
b2a20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 no-op..**.** The
b2a30 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d 75 input string mu
b2a40 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 st be zero-termi
b2a50 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a 65 nated. A new ze
b2a60 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a 2a ro-terminator.**
b2a70 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 is added to the
b2a80 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 dequoted string
b2a90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ..**.** The retu
b2aa0 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20 69 rn value is -1 i
b2ab0 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20 6f f no dequoting o
b2ac0 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65 6e ccurs or the len
b2ad0 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 gth of the.** de
b2ae0 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20 65 quoted string, e
b2af0 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65 20 xclusive of the
b2b00 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2c zero terminator,
b2b10 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64 6f if dequoting do
b2b20 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a 0a es.** occur..**.
b2b30 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a 20 ** 2002-Feb-14:
b2b40 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
b2b50 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d 6f extended to remo
b2b60 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74 79 ve MS-Access sty
b2b70 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20 66 le.** brackets f
b2b80 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e 74 rom around ident
b2b90 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d ifers. For exam
b2ba0 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 20 ple: "[a-b-c]"
b2bb0 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d becomes.** "a-b-
b2bc0 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 c"..*/.SQLITE_PR
b2bd0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b2be0 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a 7a 3Dequote(char *z
b2bf0 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65 3b ){. char quote;
b2c00 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 . int i, j;. i
b2c10 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e f( z==0 ) return
b2c20 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a -1;. quote = z
b2c30 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 71 [0];. switch( q
b2c40 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 65 uote ){. case
b2c50 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 '\'': break;.
b2c60 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 62 case '"': b
b2c70 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 reak;. case '
b2c80 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 20 `': break;
b2c90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
b2ca0 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 or MySQL compati
b2cb0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 bility */. ca
b2cc0 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 se '[': quote
b2cd0 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 = ']'; break;
b2ce0 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 /* For MS SqlSer
b2cf0 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 ver compatibilit
b2d00 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 y */. default
b2d10 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a : return -1;.
b2d20 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a }. for(i=1, j
b2d30 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d 29 =0; ALWAYS(z[i])
b2d40 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
b2d50 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 z[i]==quote ){.
b2d60 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d if( z[i+1]=
b2d70 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 =quote ){.
b2d80 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 z[j++] = quote
b2d90 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 ;. i++;.
b2da0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
b2db0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
b2dc0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
b2dd0 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 z[j++] = z[i
b2de0 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a ];. }. }. z
b2df0 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 [j] = 0;. retur
b2e00 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 n j;.}../* Conve
b2e10 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e 64 nient short-hand
b2e20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70 65 */.#define Uppe
b2e30 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65 33 rToLower sqlite3
b2e40 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a UpperToLower../*
b2e50 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 .** Some systems
b2e60 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29 2e have stricmp().
b2e70 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73 74 Others have st
b2e80 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65 63 rcasecmp(). Bec
b2e90 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69 73 ause.** there is
b2ea0 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79 2c no consistency,
b2eb0 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65 20 we will define
b2ec0 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 our own..*/.SQLI
b2ed0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b2ee0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63 6f qlite3StrICmp(co
b2ef0 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c nst char *zLeft,
b2f00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 const char *zRi
b2f10 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65 72 ght){. register
b2f20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b2f30 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e a, *b;. a = (un
b2f40 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c signed char *)zL
b2f50 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 eft;. b = (unsi
b2f60 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67 gned char *)zRig
b2f70 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61 21 ht;. while( *a!
b2f80 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 =0 && UpperToLow
b2f90 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c er[*a]==UpperToL
b2fa0 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 ower[*b]){ a++;
b2fb0 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 b++; }. return
b2fc0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d UpperToLower[*a]
b2fd0 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b - UpperToLower[
b2fe0 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 *b];.}.SQLITE_AP
b2ff0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 I int sqlite3_st
b3000 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 rnicmp(const cha
b3010 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 r *zLeft, const
b3020 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e char *zRight, in
b3030 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65 72 t N){. register
b3040 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
b3050 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e a, *b;. a = (un
b3060 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c signed char *)zL
b3070 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 eft;. b = (unsi
b3080 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69 67 gned char *)zRig
b3090 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d ht;. while( N--
b30a0 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26 26 > 0 && *a!=0 &&
b30b0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 UpperToLower[*a
b30c0 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b ]==UpperToLower[
b30d0 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 *b]){ a++; b++;
b30e0 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f }. return N<0 ?
b30f0 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 0 : UpperToLowe
b3100 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c r[*a] - UpperToL
b3110 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a ower[*b];.}../*.
b3120 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 ** Return TRUE i
b3130 66 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e 75 f z is a pure nu
b3140 6d 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20 52 meric string. R
b3150 65 74 75 72 6e 20 46 41 4c 53 45 20 61 6e 64 20 eturn FALSE and
b3160 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e 75 leave.** *realnu
b3170 6d 20 75 6e 63 68 61 6e 67 65 64 20 69 66 20 74 m unchanged if t
b3180 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 he string contai
b3190 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65 72 ns any character
b31a0 20 77 68 69 63 68 20 69 73 20 6e 6f 74 0a 2a 2a which is not.**
b31b0 20 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 part of a numbe
b31c0 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 r..**.** If the
b31d0 73 74 72 69 6e 67 20 69 73 20 70 75 72 65 20 6e string is pure n
b31e0 75 6d 65 72 69 63 2c 20 73 65 74 20 2a 72 65 61 umeric, set *rea
b31f0 6c 6e 75 6d 20 74 6f 20 54 52 55 45 20 69 66 20 lnum to TRUE if
b3200 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f the string.** co
b3210 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20 63 ntains the '.' c
b3220 68 61 72 61 63 74 65 72 20 6f 72 20 61 6e 20 22 haracter or an "
b3230 45 2b 30 30 30 22 20 73 74 79 6c 65 20 65 78 70 E+000" style exp
b3240 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 73 75 66 66 onentiation suff
b3250 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 ix..** Otherwise
b3260 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f set *realnum to
b3270 20 46 41 4c 53 45 2e 20 20 4e 6f 74 65 20 74 68 FALSE. Note th
b3280 61 74 20 6a 75 73 74 20 62 65 63 61 75 65 20 2a at just becaue *
b3290 72 65 61 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66 61 realnum is.** fa
b32a0 6c 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 lse does not mea
b32b0 6e 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 n that the numbe
b32c0 72 20 63 61 6e 20 62 65 20 73 75 63 63 65 73 73 r can be success
b32d0 66 75 6c 6c 79 20 63 6f 6e 76 65 72 74 65 64 20 fully converted
b32e0 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 into.** an integ
b32f0 65 72 20 2d 20 69 74 20 6d 69 67 68 74 20 62 65 er - it might be
b3300 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 too big..**.**
b3310 41 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 An empty string
b3320 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f is considered no
b3330 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 n-numeric..*/.SQ
b3340 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
b3350 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 sqlite3IsNumber
b3360 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 (const char *z,
b3370 69 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 int *realnum, u8
b3380 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 enc){. int inc
b3390 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 r = (enc==SQLITE
b33a0 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 _UTF8?1:2);. if
b33b0 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 ( enc==SQLITE_UT
b33c0 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 F16BE ) z++;. i
b33d0 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a f( *z=='-' || *z
b33e0 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 =='+' ) z += inc
b33f0 72 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 r;. if( !sqlite
b3400 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 3Isdigit(*z) ){.
b3410 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b3420 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 }. z += incr;.
b3430 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 *realnum = 0;.
b3440 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 while( sqlite3I
b3450 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 sdigit(*z) ){ z
b3460 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 += incr; }. if(
b3470 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 *z=='.' ){.
b3480 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 z += incr;. i
b3490 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 f( !sqlite3Isdig
b34a0 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 it(*z) ) return
b34b0 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 0;. while( sq
b34c0 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 lite3Isdigit(*z)
b34d0 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d ){ z += incr; }
b34e0 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 . *realnum =
b34f0 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 1;. }. if( *z=
b3500 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 ='e' || *z=='E'
b3510 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 ){. z += incr
b3520 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b ;. if( *z=='+
b3530 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a ' || *z=='-' ) z
b3540 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 += incr;. if
b3550 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 ( !sqlite3Isdigi
b3560 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 30 t(*z) ) return 0
b3570 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c ;. while( sql
b3580 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 ite3Isdigit(*z)
b3590 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a ){ z += incr; }.
b35a0 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 *realnum = 1
b35b0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a ;. }. return *
b35c0 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 z==0;.}../*.** T
b35d0 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69 73 he string z[] is
b35e0 20 61 6e 20 41 53 43 49 49 20 72 65 70 72 65 73 an ASCII repres
b35f0 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 72 65 entation of a re
b3600 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f al number..** Co
b3610 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 69 6e nvert this strin
b3620 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a g to a double..*
b3630 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
b3640 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 7a e assumes that z
b3650 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76 [] really is a v
b3660 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 49 66 alid number. If
b3670 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 74 it.** is not, t
b3680 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64 he result is und
b3690 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 efined..**.** Th
b36a0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
b36b0 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 ed instead of th
b36c0 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 e library atof()
b36d0 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 function becaus
b36e0 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 e.** the library
b36f0 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 77 61 atof() might wa
b3700 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 61 73 nt to use "," as
b3710 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 the decimal poi
b3720 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 nt instead.** of
b3730 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 20 6f "." depending o
b3740 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 73 20 n how locale is
b3750 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 20 77 set. But that w
b3760 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c ould cause probl
b3770 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e 20 ems.** for SQL.
b3780 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 So this routine
b3790 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 2e 22 always uses "."
b37a0 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6c regardless of l
b37b0 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ocale..*/.SQLITE
b37c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
b37d0 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 ite3AtoF(const c
b37e0 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a har *z, double *
b37f0 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e 64 65 pResult){.#ifnde
b3800 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c f SQLITE_OMIT_FL
b3810 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 63 OATING_POINT. c
b3820 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 67 69 onst char *zBegi
b3830 6e 20 3d 20 7a 3b 0a 20 20 2f 2a 20 73 69 67 6e n = z;. /* sign
b3840 20 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a * significand *
b3850 20 28 31 30 20 5e 20 28 65 73 69 67 6e 20 2a 20 (10 ^ (esign *
b3860 65 78 70 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20 20 exponent)) */.
b3870 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 20 20 20 int sign = 1;
b3880 2f 2a 20 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 /* sign of signi
b3890 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 ficand */. i64
b38a0 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 73 s = 0; /* s
b38b0 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 ignificand */.
b38c0 69 6e 74 20 64 20 3d 20 30 3b 20 20 20 20 20 20 int d = 0;
b38d0 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 /* adjust expone
b38e0 6e 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67 20 nt for shifting
b38f0 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f decimal point */
b3900 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 31 . int esign = 1
b3910 3b 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65 78 ; /* sign of ex
b3920 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 ponent */. int
b3930 65 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 65 e = 0; /* e
b3940 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f 75 xponent */. dou
b3950 62 6c 65 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e ble result;. in
b3960 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b 0a 0a t nDigits = 0;..
b3970 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e /* skip leadin
b3980 67 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77 68 g spaces */. wh
b3990 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 ile( sqlite3Issp
b39a0 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 ace(*z) ) z++;.
b39b0 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 20 /* get sign of
b39c0 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 significand */.
b39d0 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a if( *z=='-' ){.
b39e0 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 sign = -1;.
b39f0 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 z++;. }else
b3a00 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 if( *z=='+' ){.
b3a10 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a z++;. }. /*
b3a20 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 skip leading ze
b3a30 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 roes */. while(
b3a40 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 2b 2b z[0]=='0' ) z++
b3a50 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 , nDigits++;..
b3a60 2f 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67 6e /* copy max sign
b3a70 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 74 ificant digits t
b3a80 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f o significand */
b3a90 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 . while( sqlite
b3aa0 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26 20 3Isdigit(*z) &&
b3ab0 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 s<((LARGEST_INT6
b3ac0 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20 20 4-9)/10) ){.
b3ad0 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d s = s*10 + (*z -
b3ae0 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c 20 '0');. z++,
b3af0 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 20 nDigits++;. }.
b3b00 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 /* skip non-sig
b3b10 6e 69 66 69 63 61 6e 74 20 73 69 67 6e 69 66 69 nificant signifi
b3b20 63 61 6e 64 20 64 69 67 69 74 73 0a 20 20 2a 2a cand digits. **
b3b30 20 28 69 6e 63 72 65 61 73 65 20 65 78 70 6f 6e (increase expon
b3b40 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 66 ent by d to shif
b3b50 74 20 64 65 63 69 6d 61 6c 20 6c 65 66 74 29 20 t decimal left)
b3b60 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 */. while( sqli
b3b70 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 te3Isdigit(*z) )
b3b80 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c z++, nDigits++,
b3b90 20 64 2b 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 d++;.. /* if d
b3ba0 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73 20 ecimal point is
b3bb0 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 present */. if(
b3bc0 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 *z=='.' ){.
b3bd0 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 z++;. /* copy
b3be0 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66 74 digits from aft
b3bf0 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73 69 er decimal to si
b3c00 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 20 2a 2a gnificand. **
b3c10 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f 6e (decrease expon
b3c20 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69 66 ent by d to shif
b3c30 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74 29 t decimal right)
b3c40 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 73 */. while( s
b3c50 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a qlite3Isdigit(*z
b3c60 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53 54 ) && s<((LARGEST
b3c70 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29 7b _INT64-9)/10) ){
b3c80 0a 20 20 20 20 20 20 73 20 3d 20 73 2a 31 30 20 . s = s*10
b3c90 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 + (*z - '0');.
b3ca0 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 z++, nDigits
b3cb0 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a 20 ++, d--;. }.
b3cc0 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 /* skip non-s
b3cd0 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 ignificant digit
b3ce0 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 s */. while(
b3cf0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a sqlite3Isdigit(*
b3d00 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 z) ) z++, nDigit
b3d10 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 s++;. }.. /* i
b3d20 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 70 72 f exponent is pr
b3d30 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a esent */. if( *
b3d40 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 z=='e' || *z=='E
b3d50 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 ' ){. z++;.
b3d60 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 /* get sign of
b3d70 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 exponent */.
b3d80 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a if( *z=='-' ){.
b3d90 20 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 esign = -1
b3da0 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 ;. z++;.
b3db0 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 }else if( *z=='
b3dc0 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b +' ){. z++;
b3dd0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f . }. /* co
b3de0 70 79 20 64 69 67 69 74 73 20 74 6f 20 65 78 70 py digits to exp
b3df0 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 77 68 69 onent */. whi
b3e00 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 le( sqlite3Isdig
b3e10 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 it(*z) ){.
b3e20 65 20 3d 20 65 2a 31 30 20 2b 20 28 2a 7a 20 2d e = e*10 + (*z -
b3e30 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b '0');. z++
b3e40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
b3e50 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e * adjust exponen
b3e60 74 20 62 79 20 64 2c 20 61 6e 64 20 75 70 64 61 t by d, and upda
b3e70 74 65 20 73 69 67 6e 20 2a 2f 0a 20 20 65 20 3d te sign */. e =
b3e80 20 28 65 2a 65 73 69 67 6e 29 20 2b 20 64 3b 0a (e*esign) + d;.
b3e90 20 20 69 66 28 20 65 3c 30 20 29 20 7b 0a 20 20 if( e<0 ) {.
b3ea0 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 esign = -1;.
b3eb0 20 20 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20 65 e *= -1;. } e
b3ec0 6c 73 65 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 lse {. esign
b3ed0 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 = 1;. }.. /* i
b3ee0 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64 20 f 0 significand
b3ef0 2a 2f 0a 20 20 69 66 28 20 21 73 20 29 20 7b 0a */. if( !s ) {.
b3f00 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 49 45 /* In the IE
b3f10 45 45 20 37 35 34 20 73 74 61 6e 64 61 72 64 2c EE 754 standard,
b3f20 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65 64 2e zero is signed.
b3f30 0a 20 20 20 20 2a 2a 20 41 64 64 20 74 68 65 20 . ** Add the
b3f40 73 69 67 6e 20 69 66 20 77 65 27 76 65 20 73 65 sign if we've se
b3f50 65 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 en at least one
b3f60 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 72 65 73 digit */. res
b3f70 75 6c 74 20 3d 20 28 73 69 67 6e 3c 30 20 26 26 ult = (sign<0 &&
b3f80 20 6e 44 69 67 69 74 73 29 20 3f 20 2d 28 64 6f nDigits) ? -(do
b3f90 75 62 6c 65 29 30 20 3a 20 28 64 6f 75 62 6c 65 uble)0 : (double
b3fa0 29 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 )0;. } else {.
b3fb0 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f /* attempt to
b3fc0 20 72 65 64 75 63 65 20 65 78 70 6f 6e 65 6e 74 reduce exponent
b3fd0 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 73 69 67 */. if( esig
b3fe0 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 77 68 69 n>0 ){. whi
b3ff0 6c 65 28 20 73 3c 28 4c 41 52 47 45 53 54 5f 49 le( s<(LARGEST_I
b4000 4e 54 36 34 2f 31 30 29 20 26 26 20 65 3e 30 20 NT64/10) && e>0
b4010 29 20 65 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20 20 ) e--,s*=10;.
b4020 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 }else{. wh
b4030 69 6c 65 28 20 21 28 73 25 31 30 29 20 26 26 20 ile( !(s%10) &&
b4040 65 3e 30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30 3b e>0 ) e--,s/=10;
b4050 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 61 . }.. /* a
b4060 64 6a 75 73 74 20 74 68 65 20 73 69 67 6e 20 6f djust the sign o
b4070 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f f significand */
b4080 0a 20 20 20 20 73 20 3d 20 73 69 67 6e 3c 30 20 . s = sign<0
b4090 3f 20 2d 73 20 3a 20 73 3b 0a 0a 20 20 20 20 2f ? -s : s;.. /
b40a0 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20 73 * if exponent, s
b40b0 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e 64 cale significand
b40c0 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a as appropriate.
b40d0 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65 ** and store
b40e0 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 in result. */.
b40f0 20 20 20 69 66 28 20 65 20 29 7b 0a 20 20 20 20 if( e ){.
b4100 20 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 20 3d double scale =
b4110 20 31 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20 61 1.0;. /* a
b4120 74 74 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c 65 ttempt to handle
b4130 20 65 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c 6c extremely small
b4140 2f 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20 62 /large numbers b
b4150 65 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 etter */. i
b4160 66 28 20 65 3e 33 30 37 20 26 26 20 65 3c 33 34 f( e>307 && e<34
b4170 32 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 2 ){. whi
b4180 6c 65 28 20 65 25 33 30 38 20 29 20 7b 20 73 63 le( e%308 ) { sc
b4190 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 ale *= 1.0e+1; e
b41a0 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 -= 1; }.
b41b0 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a if( esign<0 ){.
b41c0 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 result
b41d0 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 = s / scale;.
b41e0 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 2f result /
b41f0 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20 20 = 1.0e+308;.
b4200 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
b4210 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 result = s
b4220 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 * scale;.
b4230 20 20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e 30 result *= 1.0
b4240 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20 7d e+308;. }
b4250 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
b4260 20 20 20 20 20 20 2f 2a 20 31 2e 30 65 2b 32 32 /* 1.0e+22
b4270 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 is the largest
b4280 70 6f 77 65 72 20 6f 66 20 31 30 20 74 68 61 6e power of 10 than
b4290 20 63 61 6e 20 62 65 20 0a 20 20 20 20 20 20 20 can be .
b42a0 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64 20 ** represented
b42b0 65 78 61 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 exactly. */.
b42c0 20 20 20 20 77 68 69 6c 65 28 20 65 25 32 32 20 while( e%22
b42d0 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 ) { scale *= 1.0
b42e0 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 20 e+1; e -= 1; }.
b42f0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65 3e while( e>
b4300 30 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 0 ) { scale *= 1
b4310 2e 30 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32 3b .0e+22; e -= 22;
b4320 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 }. if( e
b4330 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 sign<0 ){.
b4340 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2f result = s /
b4350 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 scale;.
b4360 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
b4370 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 result = s * sc
b4380 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ale;. }.
b4390 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 }. } els
b43a0 65 20 7b 0a 20 20 20 20 20 20 72 65 73 75 6c 74 e {. result
b43b0 20 3d 20 28 64 6f 75 62 6c 65 29 73 3b 0a 20 20 = (double)s;.
b43c0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73 74 }. }.. /* st
b43d0 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 2a ore the result *
b43e0 2f 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20 72 /. *pResult = r
b43f0 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65 74 esult;.. /* ret
b4400 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 urn number of ch
b4410 61 72 61 63 74 65 72 73 20 75 73 65 64 20 2a 2f aracters used */
b4420 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 28 . return (int)(
b4430 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c z - zBegin);.#el
b4440 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 se. return sqli
b4450 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 te3Atoi64(z, pRe
b4460 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a sult);.#endif /*
b4470 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f SQLITE_OMIT_FLO
b4480 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d ATING_POINT */.}
b4490 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 ../*.** Compare
b44a0 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65 72 the 19-character
b44b0 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 string zNum aga
b44c0 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72 65 inst the text re
b44d0 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 presentation.**
b44e0 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32 32 value 2^63: 922
b44f0 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 38 3372036854775808
b4500 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74 69 . Return negati
b4510 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 ve, zero, or pos
b4520 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d itive.** if zNum
b4530 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 is less than, e
b4540 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 qual to, or grea
b4550 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 ter than the str
b4560 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b ing..**.** Unlik
b4570 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 73 20 e memcmp() this
b4580 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 72 61 routine is guara
b4590 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 nteed to return
b45a0 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a the difference.*
b45b0 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20 * in the values
b45c0 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 67 69 of the last digi
b45d0 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 t if the only di
b45e0 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e 20 74 fference is in t
b45f0 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 he.** last digit
b4600 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 . So, for examp
b4610 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 le,.**.** c
b4620 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39 32 ompare2pow63("92
b4630 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 2337203685477580
b4640 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 0").**.** will r
b4650 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 eturn -8..*/.sta
b4660 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 tic int compare2
b4670 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 61 72 pow63(const char
b4680 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 *zNum){. int c
b4690 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a ;. c = memcmp(z
b46a0 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 36 38 Num,"92233720368
b46b0 35 34 37 37 35 38 30 22 2c 31 38 29 2a 31 30 3b 5477580",18)*10;
b46c0 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 . if( c==0 ){.
b46d0 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 c = zNum[18]
b46e0 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 - '8';. }. ret
b46f0 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a urn c;.}.../*.**
b4700 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
b4710 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74 zNum is a 64-bit
b4720 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 signed integer
b4730 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 and write.** the
b4740 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
b4750 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d teger into *pNum
b4760 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f . If zNum is no
b4770 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 t an integer.**
b4780 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 or is an integer
b4790 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 that is too lar
b47a0 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73 ge to be express
b47b0 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c ed with 64 bits,
b47c0 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 .** then return
b47d0 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 false..**.** Whe
b47e0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 n this routine w
b47f0 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 as originally wr
b4800 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77 itten it dealt w
b4810 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 ith only.** 32-b
b4820 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 it numbers. At
b4830 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61 that time, it wa
b4840 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68 s much faster th
b4850 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 an the.** atoi()
b4860 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65 library routine
b4870 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a in RedHat 7.2..
b4880 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
b4890 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f E int sqlite3Ato
b48a0 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a i64(const char *
b48b0 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 zNum, i64 *pNum)
b48c0 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 {. i64 v = 0;.
b48d0 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 int neg;. int
b48e0 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 i, c;. const ch
b48f0 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 ar *zStart;. wh
b4900 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 ile( sqlite3Issp
b4910 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 ace(*zNum) ) zNu
b4920 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d m++;. if( *zNum
b4930 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 =='-' ){. neg
b4940 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b = 1;. zNum++
b4950 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a ;. }else if( *z
b4960 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 Num=='+' ){.
b4970 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 neg = 0;. zNu
b4980 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 m++;. }else{.
b4990 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 neg = 0;. }.
b49a0 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a zStart = zNum;.
b49b0 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d while( zNum[0]
b49c0 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b =='0' ){ zNum++;
b49d0 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 } /* Skip over
b49e0 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 leading zeros. T
b49f0 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 icket #2454 */.
b4a00 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 for(i=0; (c=zNu
b4a10 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c m[i])>='0' && c<
b4a20 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 ='9'; i++){.
b4a30 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 v = v*10 + c - '
b4a40 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 0';. }. *pNum
b4a50 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a = neg ? -v : v;.
b4a60 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 if( c!=0 || (i
b4a70 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a ==0 && zStart==z
b4a80 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a Num) || i>19 ){.
b4a90 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 /* zNum is e
b4aa0 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 mpty or contains
b4ab0 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 non-numeric tex
b4ac0 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 t or is longer.
b4ad0 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 ** than 19 di
b4ae0 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72 61 gits (thus guara
b4af0 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69 73 nting that it is
b4b00 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 too large) */.
b4b10 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
b4b20 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b else if( i<19 ){
b4b30 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 . /* Less tha
b4b40 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20 n 19 digits, so
b4b50 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 we know that it
b4b60 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73 20 fits in 64 bits
b4b70 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b */. return 1;
b4b80 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
b4b90 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 19-digit number
b4ba0 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 s must be no lar
b4bb0 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 ger than 9223372
b4bc0 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66 20 036854775807 if
b4bd0 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 positive. **
b4be0 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35 34 or 9223372036854
b4bf0 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74 69 775808 if negati
b4c00 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39 ve. Note that 9
b4c10 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35 38 2233720368546658
b4c20 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 08. ** is 2^6
b4c30 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3. */. return
b4c40 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a compare2pow63(z
b4c50 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a Num)<neg;. }.}.
b4c60 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e ./*.** The strin
b4c70 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 g zNum represent
b4c80 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e s an unsigned in
b4c90 74 65 67 65 72 2e 20 20 54 68 65 20 7a 4e 75 6d teger. The zNum
b4ca0 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 69 string.** consi
b4cb0 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f sts of one or mo
b4cc0 72 65 20 64 69 67 69 74 20 63 68 61 72 61 63 74 re digit charact
b4cd0 65 72 73 20 61 6e 64 20 69 73 20 74 65 72 6d 69 ers and is termi
b4ce0 6e 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 7a 65 nated by.** a ze
b4cf0 72 6f 20 63 68 61 72 61 63 74 65 72 2e 20 20 41 ro character. A
b4d00 6e 79 20 73 74 72 61 79 20 63 68 61 72 61 63 74 ny stray charact
b4d10 65 72 73 20 69 6e 20 7a 4e 75 6d 20 72 65 73 75 ers in zNum resu
b4d20 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a lt in undefined.
b4d30 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a ** behavior..**.
b4d40 2a 2a 20 49 66 20 74 68 65 20 75 6e 73 69 67 6e ** If the unsign
b4d50 65 64 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 ed integer that
b4d60 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 zNum represents
b4d70 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a will fit in a.**
b4d80 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 64-bit signed i
b4d90 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 54 nteger, return T
b4da0 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65 20 RUE. Otherwise
b4db0 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a return FALSE..**
b4dc0 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 67 46 6c .** If the negFl
b4dd0 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 ag parameter is
b4de0 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 true, that means
b4df0 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 61 6c 6c that zNum reall
b4e00 79 20 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a 20 y represents.**
b4e10 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 a negative numbe
b4e20 72 2e 20 20 28 54 68 65 20 6c 65 61 64 69 6e 67 r. (The leading
b4e30 20 22 2d 22 20 69 73 20 6f 6d 69 74 74 65 64 20 "-" is omitted
b4e40 66 72 6f 6d 20 7a 4e 75 6d 2e 29 20 20 54 68 69 from zNum.) Thi
b4e50 73 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 s.** parameter i
b4e60 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74 65 s needed to dete
b4e70 72 6d 69 6e 65 20 61 20 62 6f 75 6e 64 61 72 79 rmine a boundary
b4e80 20 63 61 73 65 2e 20 20 41 20 73 74 72 69 6e 67 case. A string
b4e90 0a 2a 2a 20 6f 66 20 22 39 32 32 33 33 37 33 30 .** of "92233730
b4ea0 33 36 38 35 34 37 37 35 38 30 38 22 20 72 65 74 36854775808" ret
b4eb0 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 6e 65 urns false if ne
b4ec0 67 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20 6f gFlag is false o
b4ed0 72 20 74 72 75 65 0a 2a 2a 20 69 66 20 6e 65 67 r true.** if neg
b4ee0 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a 2a Flag is true..**
b4ef0 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 7a 65 72 6f .** Leading zero
b4f00 73 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a s are ignored..*
b4f10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b4f20 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74 73 int sqlite3Fits
b4f30 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20 63 In64Bits(const c
b4f40 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e har *zNum, int n
b4f50 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 69 egFlag){. int i
b4f60 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b ;. int neg = 0;
b4f70 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 75 6d .. assert( zNum
b4f80 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d [0]>='0' && zNum
b4f90 5b 30 5d 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 7a [0]<='9' ); /* z
b4fa0 4e 75 6d 20 69 73 20 61 6e 20 75 6e 73 69 67 6e Num is an unsign
b4fb0 65 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 ed number */..
b4fc0 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 if( negFlag ) ne
b4fd0 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 g = 1-neg;. whi
b4fe0 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 le( *zNum=='0' )
b4ff0 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 {. zNum++;
b5000 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 /* Skip leading
b5010 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 zeros. Ticket #
b5020 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 2454 */. }. fo
b5030 72 28 69 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 r(i=0; zNum[i];
b5040 69 2b 2b 29 7b 20 61 73 73 65 72 74 28 20 7a 4e i++){ assert( zN
b5050 75 6d 5b 69 5d 3e 3d 27 30 27 20 26 26 20 7a 4e um[i]>='0' && zN
b5060 75 6d 5b 69 5d 3c 3d 27 39 27 20 29 3b 20 7d 0a um[i]<='9' ); }.
b5070 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 if( i<19 ){.
b5080 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20 /* Guaranteed
b5090 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20 74 to fit if less t
b50a0 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f han 19 digits */
b50b0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
b50c0 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20 }else if( i>19
b50d0 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e ){. /* Guaran
b50e0 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62 teed to be too b
b50f0 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74 68 ig if greater th
b5100 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a an 19 digits */.
b5110 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b5120 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f }else{. /* Co
b5130 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e mpare against 2^
b5140 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 63. */. retur
b5150 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 n compare2pow63(
b5160 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d zNum)<neg;. }.}
b5170 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 ../*.** If zNum
b5180 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e represents an in
b5190 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20 teger that will
b51a0 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20 fit in 32-bits,
b51b0 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61 then set.** *pVa
b51c0 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65 lue to that inte
b51d0 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ger and return t
b51e0 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 rue. Otherwise
b51f0 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a return false..**
b5200 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 .** Any non-nume
b5210 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20 74 ric characters t
b5220 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e hat following zN
b5230 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a um are ignored..
b5240 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66 65 ** This is diffe
b5250 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 rent from sqlite
b5260 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68 20 3Atoi64() which
b5270 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 requires the.**
b5280 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20 input number to
b5290 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 be zero-terminat
b52a0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
b52b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
b52c0 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 3GetInt32(const
b52d0 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 char *zNum, int
b52e0 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69 *pValue){. sqli
b52f0 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a te_int64 v = 0;.
b5300 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e int i, c;. in
b5310 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 t neg = 0;. if(
b5320 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b zNum[0]=='-' ){
b5330 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 . neg = 1;.
b5340 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 zNum++;. }els
b5350 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 e if( zNum[0]=='
b5360 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b +' ){. zNum++
b5370 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a ;. }. while( z
b5380 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e Num[0]=='0' ) zN
b5390 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b um++;. for(i=0;
b53a0 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e i<11 && (c = zN
b53b0 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20 um[i] - '0')>=0
b53c0 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 && c<=9; i++){.
b53d0 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b v = v*10 + c;
b53e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c . }.. /* The l
b53f0 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72 ongest decimal r
b5400 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
b5410 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67 65 a 32 bit intege
b5420 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a r is 10 digits:.
b5430 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 **. **
b5440 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39 30 1234567890
b5450 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d . ** 2^31 -
b5460 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a > 2147483648. *
b5470 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a /. if( i>10 ){.
b5480 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
b5490 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31 }. if( v-neg>21
b54a0 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20 47483647 ){.
b54b0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
b54c0 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76 if( neg ){. v
b54d0 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 = -v;. }. *pV
b54e0 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20 alue = (int)v;.
b54f0 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a return 1;.}../*
b5500 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65 .** The variable
b5510 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 -length integer
b5520 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66 encoding is as f
b5530 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 ollows:.**.** KE
b5540 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20 Y:.** A
b5550 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20 = 0xxxxxxx 7
b5560 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 bits of data and
b5570 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a one flag bit.**
b5580 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78 B = 1xx
b5590 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20 xxxxx 7 bits
b55a0 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 of data and one
b55b0 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 flag bit.**
b55c0 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78 78 C = xxxxxxxx
b55d0 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61 8 bits of da
b55e0 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 ta.**.** 7 bits
b55f0 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 - A.** 14 bits
b5600 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 - BA.** 21 bits
b5610 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73 - BBA.** 28 bits
b5620 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69 - BBBA.** 35 bi
b5630 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32 ts - BBBBA.** 42
b5640 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a bits - BBBBBA.*
b5650 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42 42 * 49 bits - BBBB
b5660 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d BBA.** 56 bits -
b5670 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20 BBBBBBBA.** 64
b5680 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42 43 bits - BBBBBBBBC
b5690 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 .*/../*.** Write
b56a0 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 a 64-bit variab
b56b0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le-length intege
b56c0 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 r to memory star
b56d0 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a ting at p[0]..**
b56e0 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 The length of d
b56f0 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62 ata write will b
b5700 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 e between 1 and
b5710 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75 9 bytes. The nu
b5720 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 mber.** of bytes
b5730 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74 75 written is retu
b5740 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 rned..**.** A va
b5750 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e riable-length in
b5760 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f teger consists o
b5770 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 f the lower 7 bi
b5780 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a ts of each byte.
b5790 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 ** for all bytes
b57a0 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20 38 that have the 8
b57b0 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f th bit set and o
b57c0 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68 65 ne byte with the
b57d0 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61 8th.** bit clea
b57e0 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 77 r. Except, if w
b57f0 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74 68 e get to the 9th
b5800 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73 byte, it stores
b5810 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 the full.** 8 b
b5820 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c its and is the l
b5830 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c ast byte..*/.SQL
b5840 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
b5850 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 sqlite3PutVarint
b5860 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
b5870 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 p, u64 v){. int
b5880 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 i, j, n;. u8 b
b5890 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20 uf[10];. if( v
b58a0 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30 30 & (((u64)0xff000
b58b0 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20 000)<<32) ){.
b58c0 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a 20 p[8] = (u8)v;.
b58d0 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 v >>= 8;.
b58e0 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 for(i=7; i>=0; i
b58f0 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 --){. p[i]
b5900 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37 66 = (u8)((v & 0x7f
b5910 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 20 ) | 0x80);.
b5920 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a v >>= 7;. }.
b5930 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20 20 return 9;.
b5940 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a 20 } . n = 0;.
b5950 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b 2b do{. buf[n++
b5960 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 ] = (u8)((v & 0x
b5970 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 7f) | 0x80);.
b5980 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 v >>= 7;. }whi
b5990 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75 le( v!=0 );. bu
b59a0 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20 f[0] &= 0x7f;.
b59b0 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a assert( n<=9 );.
b59c0 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 for(i=0, j=n-1
b59d0 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b ; j>=0; j--, i++
b59e0 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 ){. p[i] = bu
b59f0 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 f[j];. }. retu
b5a00 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 rn n;.}../*.** T
b5a10 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
b5a20 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20 faster version
b5a30 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 of sqlite3PutVar
b5a40 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a int() that only.
b5a50 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d ** works for 32-
b5a60 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74 bit positive int
b5a70 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68 20 egers and which
b5a80 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 is optimized for
b5a90 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 .** the common c
b5aa0 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 ase of small int
b5ab0 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20 egers. A MACRO
b5ac0 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69 version, putVari
b5ad0 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 nt32,.** is prov
b5ae0 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e ided which inlin
b5af0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 es the single-by
b5b00 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f te case. All co
b5b10 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a de should use.**
b5b20 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 the MACRO versi
b5b30 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63 74 on as this funct
b5b40 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 ion assumes the
b5b50 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 single-byte case
b5b60 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 has.** already
b5b70 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f been handled..*/
b5b80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b5b90 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 int sqlite3PutVa
b5ba0 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 rint32(unsigned
b5bb0 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b char *p, u32 v){
b5bc0 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72 69 .#ifndef putVari
b5bd0 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26 20 nt32. if( (v &
b5be0 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20 ~0x7f)==0 ){.
b5bf0 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72 p[0] = v;. r
b5c00 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e eturn 1;. }.#en
b5c10 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e dif. if( (v & ~
b5c20 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20 0x3fff)==0 ){.
b5c30 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28 76 p[0] = (u8)((v
b5c40 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 >>7) | 0x80);.
b5c50 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 20 p[1] = (u8)(v
b5c60 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65 74 & 0x7f);. ret
b5c70 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65 74 urn 2;. }. ret
b5c80 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56 61 urn sqlite3PutVa
b5c90 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f rint(p, v);.}../
b5ca0 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d 62 *.** Read a 64-b
b5cb0 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 it variable-leng
b5cc0 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 th integer from
b5cd0 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 memory starting
b5ce0 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 at p[0]..** Retu
b5cf0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
b5d00 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 bytes read. Th
b5d10 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 e value is store
b5d20 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 d in *v..*/.SQLI
b5d30 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 TE_PRIVATE u8 sq
b5d40 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 63 lite3GetVarint(c
b5d50 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 onst unsigned ch
b5d60 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a ar *p, u64 *v){.
b5d70 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 20 u32 a,b,s;..
b5d80 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 a = *p;. /* a:
b5d90 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f p0 (unmasked) */
b5da0 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 . if (!(a&0x80)
b5db0 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61 ). {. *v = a
b5dc0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a ;. return 1;.
b5dd0 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 }.. p++;. b
b5de0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 = *p;. /* b: p1
b5df0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b5e00 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a if (!(b&0x80)).
b5e10 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78 37 {. a &= 0x7
b5e20 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b f;. a = a<<7;
b5e30 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b5e40 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 *v = a;. ret
b5e50 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b urn 2;. }.. p+
b5e60 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a +;. a = a<<14;.
b5e70 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 a |= *p;. /*
b5e80 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 a: p0<<14 | p2 (
b5e90 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b5ea0 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
b5eb0 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 {. a &= (0x7f
b5ec0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b5ed0 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 b &= 0x7f;.
b5ee0 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 b = b<<7;. a
b5ef0 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 |= b;. *v =
b5f00 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b a;. return 3;
b5f10 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20 . }.. /* CSE1
b5f20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 from below */.
b5f30 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c a &= (0x7f<<14)|
b5f40 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 (0x7f);. p++;.
b5f50 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 b = b<<14;. b
b5f60 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 |= *p;. /* b: p
b5f70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 1<<14 | p3 (unma
b5f80 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 sked) */. if (!
b5f90 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 (b&0x80)). {.
b5fa0 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 b &= (0x7f<<14
b5fb0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a )|(0x7f);. /*
b5fc0 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a moved CSE1 up *
b5fd0 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 /. /* a &= (0
b5fe0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b5ff0 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 */. a = a<<7
b6000 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b6010 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 *v = a;. re
b6020 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f turn 4;. }.. /
b6030 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 * a: p0<<14 | p2
b6040 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f (masked) */. /
b6050 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 * b: p1<<14 | p3
b6060 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 (unmasked) */.
b6070 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20 70 /* 1:save off p
b6080 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 0<<21 | p1<<14 |
b6090 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 p2<<7 | p3 (mas
b60a0 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 ked) */. /* mov
b60b0 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 ed CSE1 up */.
b60c0 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 /* a &= (0x7f<<1
b60d0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 4)|(0x7f); */.
b60e0 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c b &= (0x7f<<14)|
b60f0 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61 3b (0x7f);. s = a;
b6100 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 20 . /* s: p0<<14
b6110 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f | p2 (masked) */
b6120 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 .. p++;. a = a
b6130 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b <<14;. a |= *p;
b6140 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 . /* a: p0<<28
b6150 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 | p2<<14 | p4 (u
b6160 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 nmasked) */. if
b6170 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b (!(a&0x80)). {
b6180 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 . /* we can s
b6190 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65 20 kip these cause
b61a0 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65 63 they were (effec
b61b0 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f tively) done abo
b61c0 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 ve in calc'ing s
b61d0 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 */. /* a &=
b61e0 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 (0x7f<<28)|(0x7f
b61f0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f <<14)|(0x7f); */
b6200 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 . /* b &= (0x
b6210 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 7f<<14)|(0x7f);
b6220 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b */. b = b<<7;
b6230 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 . a |= b;.
b6240 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 s = s>>18;.
b6250 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
b6260 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
b6270 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32 n 5;. }.. /* 2
b6280 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 :save off p0<<21
b6290 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c | p1<<14 | p2<<
b62a0 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 7 | p3 (masked)
b62b0 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 */. s = s<<7;.
b62c0 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a s |= b;. /* s:
b62d0 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 p0<<21 | p1<<14
b62e0 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d | p2<<7 | p3 (m
b62f0 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b asked) */.. p++
b6300 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 ;. b = b<<14;.
b6310 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 b |= *p;. /* b
b6320 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 : p1<<28 | p3<<1
b6330 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65 64 4 | p5 (unmasked
b6340 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 ) */. if (!(b&0
b6350 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a x80)). {. /*
b6360 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68 69 we can skip thi
b6370 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20 28 s cause it was (
b6380 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e effectively) don
b6390 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 e above in calc'
b63a0 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 ing s */. /*
b63b0 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c b &= (0x7f<<28)|
b63c0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b63d0 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 ); */. a &= (
b63e0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 0x7f<<14)|(0x7f)
b63f0 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a ;. a = a<<7;.
b6400 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 a |= b;.
b6410 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a s = s>>18;. *
b6420 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 v = ((u64)s)<<32
b6430 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e | a;. return
b6440 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 6;. }.. p++;.
b6450 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 a = a<<14;. a
b6460 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 |= *p;. /* a:
b6470 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34 20 p2<<28 | p4<<14
b6480 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p6 (unmasked)
b6490 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 */. if (!(a&0x8
b64a0 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 0)). {. a &=
b64b0 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 (0x1f<<28)|(0x7
b64c0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b64d0 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 b &= (0x7f<<1
b64e0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 4)|(0x7f);. b
b64f0 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c = b<<7;. a |
b6500 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e = b;. s = s>>
b6510 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 11;. *v = ((u
b6520 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 64)s)<<32 | a;.
b6530 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d return 7;. }
b6540 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d .. /* CSE2 from
b6550 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d below */. a &=
b6560 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 (0x7f<<14)|(0x7
b6570 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d f);. p++;. b =
b6580 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a b<<14;. b |= *
b6590 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c 32 p;. /* b: p3<<2
b65a0 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37 20 8 | p5<<14 | p7
b65b0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 (unmasked) */.
b65c0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 if (!(b&0x80)).
b65d0 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31 {. b &= (0x1
b65e0 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
b65f0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a )|(0x7f);. /*
b6600 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a moved CSE2 up *
b6610 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 /. /* a &= (0
b6620 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b6630 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 */. a = a<<7
b6640 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 ;. a |= b;.
b6650 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 20 s = s>>4;.
b6660 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 *v = ((u64)s)<<3
b6670 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 2 | a;. retur
b6680 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b n 8;. }.. p++;
b6690 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 20 . a = a<<15;.
b66a0 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a a |= *p;. /* a:
b66b0 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 35 p4<<29 | p6<<15
b66c0 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64 29 | p8 (unmasked)
b66d0 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 */.. /* moved
b66e0 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 CSE2 up */. /*
b66f0 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c a &= (0x7f<<29)|
b6700 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66 66 (0x7f<<15)|(0xff
b6710 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 ); */. b &= (0x
b6720 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 7f<<14)|(0x7f);.
b6730 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 20 b = b<<8;. a
b6740 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c |= b;.. s = s<<
b6750 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a 4;. b = p[-4];.
b6760 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 62 b &= 0x7f;. b
b6770 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20 = b>>3;. s |=
b6780 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36 34 b;.. *v = ((u64
b6790 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 20 )s)<<32 | a;..
b67a0 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a return 9;.}../*.
b67b0 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69 74 ** Read a 32-bit
b67c0 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 variable-length
b67d0 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 integer from me
b67e0 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 mory starting at
b67f0 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e p[0]..** Return
b6800 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
b6810 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 ytes read. The
b6820 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 value is stored
b6830 69 6e 20 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 in *v..**.** If
b6840 74 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72 65 the varint store
b6850 64 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61 72 d in p[0] is lar
b6860 67 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69 74 ger than can fit
b6870 20 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e 73 in a 32-bit uns
b6880 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 igned.** integer
b6890 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74 6f , then set *v to
b68a0 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a 0a 0xffffffff..**.
b68b0 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73 69 ** A MACRO versi
b68c0 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32 2c on, getVarint32,
b68d0 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 is provided whi
b68e0 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a ch inlines the .
b68f0 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 ** single-byte c
b6900 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 ase. All code s
b6910 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41 hould use the MA
b6920 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 0a CRO version as .
b6930 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e ** this function
b6940 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e assumes the sin
b6950 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61 gle-byte case ha
b6960 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 68 s already been h
b6970 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 andled..*/.SQLIT
b6980 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c E_PRIVATE u8 sql
b6990 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 ite3GetVarint32(
b69a0 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
b69b0 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b har *p, u32 *v){
b69c0 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20 2f . u32 a,b;.. /
b69d0 2a 20 54 68 65 20 31 2d 62 79 74 65 20 63 61 73 * The 1-byte cas
b69e0 65 2e 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e 67 e. Overwhelming
b69f0 6c 79 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d ly the most comm
b6a00 6f 6e 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e 6c on. Handled inl
b6a10 69 6e 65 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 ine. ** by the
b6a20 67 65 74 56 61 72 69 6e 33 32 28 29 20 6d 61 63 getVarin32() mac
b6a30 72 6f 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a ro */. a = *p;.
b6a40 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 /* a: p0 (unma
b6a50 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 sked) */.#ifndef
b6a60 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 20 69 getVarint32. i
b6a70 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 f (!(a&0x80)).
b6a80 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 {. /* Values
b6a90 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31 32 between 0 and 12
b6aa0 37 20 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61 3b 7 */. *v = a;
b6ab0 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
b6ac0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 }.#endif.. /*
b6ad0 54 68 65 20 32 2d 62 79 74 65 20 63 61 73 65 20 The 2-byte case
b6ae0 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 */. p++;. b =
b6af0 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 *p;. /* b: p1 (
b6b00 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 unmasked) */. i
b6b10 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 f (!(b&0x80)).
b6b20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 {. /* Values
b6b30 62 65 74 77 65 65 6e 20 31 32 38 20 61 6e 64 20 between 128 and
b6b40 31 36 33 38 33 20 2a 2f 0a 20 20 20 20 61 20 26 16383 */. a &
b6b50 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 = 0x7f;. a =
b6b60 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 a<<7;. *v = a
b6b70 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e | b;. return
b6b80 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 2;. }.. /* Th
b6b90 65 20 33 2d 62 79 74 65 20 63 61 73 65 20 2a 2f e 3-byte case */
b6ba0 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c . p++;. a = a<
b6bb0 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a <14;. a |= *p;.
b6bc0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c /* a: p0<<14 |
b6bd0 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a p2 (unmasked) *
b6be0 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 /. if (!(a&0x80
b6bf0 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 )). {. /* Va
b6c00 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 36 33 lues between 163
b6c10 38 34 20 61 6e 64 20 32 30 39 37 31 35 31 20 2a 84 and 2097151 *
b6c20 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 /. a &= (0x7f
b6c30 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 <<14)|(0x7f);.
b6c40 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 b &= 0x7f;.
b6c50 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a b = b<<7;. *
b6c60 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 v = a | b;. r
b6c70 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 eturn 3;. }..
b6c80 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61 72 69 /* A 32-bit vari
b6c90 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 nt is used to st
b6ca0 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d 61 ore size informa
b6cb0 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e 0a tion in btrees..
b6cc0 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72 65 ** Objects are
b6cd0 20 72 61 72 65 6c 79 20 6c 61 72 67 65 72 20 74 rarely larger t
b6ce0 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20 6f han 2MiB limit o
b6cf0 66 20 61 20 33 2d 62 79 74 65 20 76 61 72 69 6e f a 3-byte varin
b6d00 74 2e 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74 65 t.. ** A 3-byte
b6d10 20 76 61 72 69 6e 74 20 69 73 20 73 75 66 66 69 varint is suffi
b6d20 63 69 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d 70 cient, for examp
b6d30 6c 65 2c 20 74 6f 20 72 65 63 6f 72 64 20 74 68 le, to record th
b6d40 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 e size. ** of a
b6d50 20 31 30 34 38 35 36 39 2d 62 79 74 65 20 42 4c 1048569-byte BL
b6d60 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20 20 OB or string..
b6d70 2a 2a 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 **. ** We only
b6d80 75 6e 72 6f 6c 6c 20 74 68 65 20 66 69 72 73 74 unroll the first
b6d90 20 31 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d 20 1-, 2-, and 3-
b6da0 62 79 74 65 20 63 61 73 65 73 2e 20 20 54 68 65 byte cases. The
b6db0 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65 20 very. ** rare
b6dc0 6c 61 72 67 65 72 20 63 61 73 65 73 20 63 61 6e larger cases can
b6dd0 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20 74 be handled by t
b6de0 68 65 20 73 6c 6f 77 65 72 20 36 34 2d 62 69 74 he slower 64-bit
b6df0 20 76 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 varint. ** rou
b6e00 74 69 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 tine.. */.#if 1
b6e10 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 . {. u64 v64
b6e20 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 ;. u8 n;..
b6e30 20 70 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 3d p -= 2;. n =
b6e40 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e sqlite3GetVarin
b6e50 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 t(p, &v64);.
b6e60 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26 20 6e assert( n>3 && n
b6e70 3c 3d 39 20 29 3b 0a 20 20 20 20 69 66 28 20 28 <=9 );. if( (
b6e80 76 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 v64 & SQLITE_MAX
b6e90 5f 55 33 32 29 21 3d 76 36 34 20 29 7b 0a 20 20 _U32)!=v64 ){.
b6ea0 20 20 20 20 2a 76 20 3d 20 30 78 66 66 66 66 66 *v = 0xfffff
b6eb0 66 66 66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a fff;. }else{.
b6ec0 20 20 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29 *v = (u32)
b6ed0 76 36 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 v64;. }. r
b6ee0 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 eturn n;. }..#e
b6ef0 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 20 66 6f 6c lse. /* For fol
b6f00 6c 6f 77 69 6e 67 20 63 6f 64 65 20 28 6b 65 70 lowing code (kep
b6f10 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c t for historical
b6f20 20 72 65 63 6f 72 64 20 6f 6e 6c 79 29 20 73 68 record only) sh
b6f30 6f 77 73 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f ows an. ** unro
b6f40 6c 6c 69 6e 67 20 66 6f 72 20 74 68 65 20 33 2d lling for the 3-
b6f50 20 61 6e 64 20 34 2d 62 79 74 65 20 76 61 72 69 and 4-byte vari
b6f60 6e 74 20 63 61 73 65 73 2e 20 20 54 68 69 73 20 nt cases. This
b6f70 63 6f 64 65 20 69 73 0a 20 20 2a 2a 20 73 6c 69 code is. ** sli
b6f80 67 68 74 6c 79 20 66 61 73 74 65 72 2c 20 62 75 ghtly faster, bu
b6f90 74 20 69 74 20 69 73 20 61 6c 73 6f 20 6c 61 72 t it is also lar
b6fa0 67 65 72 20 61 6e 64 20 6d 75 63 68 20 68 61 72 ger and much har
b6fb0 64 65 72 20 74 6f 20 74 65 73 74 2e 0a 20 20 2a der to test.. *
b6fc0 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 /. p++;. b = b
b6fd0 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b <<14;. b |= *p;
b6fe0 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 . /* b: p1<<14
b6ff0 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 | p3 (unmasked)
b7000 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 */. if (!(b&0x8
b7010 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 0)). {. /* V
b7020 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 32 30 alues between 20
b7030 39 37 31 35 32 20 61 6e 64 20 32 36 38 34 33 35 97152 and 268435
b7040 34 35 35 20 2a 2f 0a 20 20 20 20 62 20 26 3d 20 455 */. b &=
b7050 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 (0x7f<<14)|(0x7f
b7060 29 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37 );. a &= (0x7
b7070 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 f<<14)|(0x7f);.
b7080 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 a = a<<7;.
b7090 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 *v = a | b;.
b70a0 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a return 4;. }..
b70b0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c p++;. a = a<<
b70c0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 14;. a |= *p;.
b70d0 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 /* a: p0<<28 |
b70e0 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d p2<<14 | p4 (unm
b70f0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 asked) */. if (
b7100 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 !(a&0x80)). {.
b7110 20 20 20 2f 2a 20 57 61 6c 75 65 73 20 20 62 65 /* Walues be
b7120 74 77 65 65 6e 20 32 36 38 34 33 35 34 35 36 20 tween 268435456
b7130 61 6e 64 20 33 34 33 35 39 37 33 38 33 36 37 20 and 34359738367
b7140 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 31 */. a &= (0x1
b7150 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 f<<28)|(0x7f<<14
b7160 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 )|(0x7f);. b
b7170 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 &= (0x1f<<28)|(0
b7180 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b x7f<<14)|(0x7f);
b7190 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 . b = b<<7;.
b71a0 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 *v = a | b;.
b71b0 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d return 5;. }
b71c0 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e .. /* We can on
b71d0 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 70 6f ly reach this po
b71e0 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e 67 int when reading
b71f0 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 a corrupt datab
b7200 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20 ase. ** file.
b7210 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 20 In that case we
b7220 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68 are not in any h
b7230 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 20 28 urry. Use the (
b7240 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20 relatively. **
b7250 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70 75 slow) general-pu
b7260 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65 74 rpose sqlite3Get
b7270 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e 65 Varint() routine
b7280 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65 0a to extract the.
b7290 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 ** value. */.
b72a0 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a {. u64 v64;.
b72b0 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 u8 n;.. p
b72c0 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73 -= 4;. n = s
b72d0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 qlite3GetVarint(
b72e0 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73 p, &v64);. as
b72f0 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d sert( n>5 && n<=
b7300 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75 9 );. *v = (u
b7310 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74 75 32)v64;. retu
b7320 72 6e 20 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 rn n;. }.#endif
b7330 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
b7340 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
b7350 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 62 ytes that will b
b7360 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f 72 e needed to stor
b7370 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36 e the given.** 6
b7380 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 4-bit integer..*
b7390 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
b73a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69 int sqlite3Vari
b73b0 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20 20 ntLen(u64 v){.
b73c0 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b int i = 0;. do{
b73d0 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 . i++;. v
b73e0 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 >>= 7;. }while(
b73f0 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59 53 28 v!=0 && ALWAYS(
b7400 69 3c 39 29 20 29 3b 0a 20 20 72 65 74 75 72 6e i<9) );. return
b7410 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 i;.}.../*.** Re
b7420 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66 6f ad or write a fo
b7430 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 ur-byte big-endi
b7440 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 an integer value
b7450 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b7460 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 47 ATE u32 sqlite3G
b7470 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75 38 et4byte(const u8
b7480 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 *p){. return (
b7490 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 p[0]<<24) | (p[1
b74a0 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c ]<<16) | (p[2]<<
b74b0 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 8) | p[3];.}.SQL
b74c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
b74d0 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 sqlite3Put4byte
b74e0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a (unsigned char *
b74f0 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 p, u32 v){. p[0
b7500 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 34 29 3b ] = (u8)(v>>24);
b7510 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 . p[1] = (u8)(v
b7520 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 >>16);. p[2] =
b7530 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 20 70 5b (u8)(v>>8);. p[
b7540 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 3] = (u8)v;.}...
b7550 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
b7560 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c LITE_OMIT_BLOB_L
b7570 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e ITERAL) || defin
b7580 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ed(SQLITE_HAS_CO
b7590 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 DEC)./*.** Trans
b75a0 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62 79 late a single by
b75b0 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20 61 te of Hex into a
b75c0 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 n integer..** Th
b75d0 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 is routine only
b75e0 77 6f 72 6b 73 20 69 66 20 68 20 72 65 61 6c 6c works if h reall
b75f0 79 20 69 73 20 61 20 76 61 6c 69 64 20 68 65 78 y is a valid hex
b7600 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 adecimal.** char
b7610 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 acter: 0..9a..f
b7620 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20 75 A..F.*/.static u
b7630 38 20 68 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 8 hexToInt(int h
b7640 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 68 3e ){. assert( (h>
b7650 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 29 20 ='0' && h<='9')
b7660 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 20 68 || (h>='a' && h
b7670 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27 <='f') || (h>='
b7680 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 29 3b A' && h<='F') );
b7690 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 .#ifdef SQLITE_A
b76a0 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31 SCII. h += 9*(1
b76b0 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66 &(h>>6));.#endif
b76c0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 .#ifdef SQLITE_E
b76d0 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28 BCDIC. h += 9*(
b76e0 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 1&~(h>>4));.#end
b76f0 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 38 29 if. return (u8)
b7700 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65 6e (h & 0xf);.}.#en
b7710 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f dif /* !SQLITE_O
b7720 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c MIT_BLOB_LITERAL
b7730 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 || SQLITE_HAS_C
b7740 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 ODEC */..#if !de
b7750 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
b7760 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 T_BLOB_LITERAL)
b7770 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
b7780 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a E_HAS_CODEC)./*.
b7790 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f ** Convert a BLO
b77a0 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 65 B literal of the
b77b0 20 66 6f 72 6d 20 22 78 27 68 68 68 68 68 68 27 form "x'hhhhhh'
b77c0 22 20 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 " into its binar
b77d0 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 y.** value. Ret
b77e0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
b77f0 20 69 74 73 20 62 69 6e 61 72 79 20 76 61 6c 75 its binary valu
b7800 65 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c e. Space to hol
b7810 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 d the.** binary
b7820 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20 6f value has been o
b7830 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c btained from mal
b7840 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 loc and must be
b7850 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 freed by.** the
b7860 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e calling routine.
b7870 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
b7880 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
b7890 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 HexToBlob(sqlite
b78a0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 3 *db, const cha
b78b0 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 r *z, int n){.
b78c0 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 char *zBlob;. i
b78d0 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d nt i;.. zBlob =
b78e0 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 (char *)sqlite3
b78f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
b7900 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b n/2 + 1);. n--;
b7910 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a . if( zBlob ){.
b7920 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
b7930 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a ; i+=2){. z
b7940 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 Blob[i/2] = (hex
b7950 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 ToInt(z[i])<<4)
b7960 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 | hexToInt(z[i+1
b7970 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 ]);. }. zB
b7980 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 lob[i/2] = 0;.
b7990 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 }. return zBlob
b79a0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 ;.}.#endif /* !S
b79b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f QLITE_OMIT_BLOB_
b79c0 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 LITERAL || SQLIT
b79d0 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a E_HAS_CODEC */..
b79e0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 ./*.** Change th
b79f0 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 e sqlite.magic f
b7a00 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 rom SQLITE_MAGIC
b7a10 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f _OPEN to SQLITE_
b7a20 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 MAGIC_BUSY..** R
b7a30 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 eturn an error (
b7a40 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 non-zero) if the
b7a50 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 magic was not S
b7a60 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e QLITE_MAGIC_OPEN
b7a70 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f .** when this ro
b7a80 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e utine is called.
b7a90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
b7aa0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 ine is called wh
b7ab0 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 en entering an S
b7ac0 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65 20 QLite API. The
b7ad0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 SQLITE_MAGIC_OPE
b7ae0 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 N.** value indic
b7af0 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64 61 ates that the da
b7b00 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f tabase connectio
b7b10 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 n passed into th
b7b20 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e e API is.** open
b7b30 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 6e and is not bein
b7b40 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65 g used by anothe
b7b50 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63 68 r thread. By ch
b7b60 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 anging the value
b7b70 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 .** to SQLITE_MA
b7b80 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64 69 GIC_BUSY we indi
b7b90 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63 6f cate that the co
b7ba0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 nnection is in u
b7bb0 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 se..** sqlite3Sa
b7bc0 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 fetyOff() below
b7bd0 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 20 will change the
b7be0 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 51 value back to SQ
b7bf0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a LITE_MAGIC_OPEN.
b7c00 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49 20 ** when the API
b7c10 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 exits. .**.** Th
b7c20 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
b7c30 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65 63 attempt to detec
b7c40 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64 73 t if two threads
b7c50 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 use the.** same
b7c60 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 sqlite* pointer
b7c70 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d at the same tim
b7c80 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 72 e. There is a r
b7c90 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f ace .** conditio
b7ca0 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73 69 n so it is possi
b7cb0 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72 72 ble that the err
b7cc0 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63 74 or is not detect
b7cd0 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c ed..** But usual
b7ce0 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 ly the problem w
b7cf0 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 68 ill be seen. Th
b7d00 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 e result will be
b7d10 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 an.** error whi
b7d20 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 ch can be used t
b7d30 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70 6c o debug the appl
b7d40 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 0a ication that is.
b7d50 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 ** using SQLite
b7d60 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a incorrectly..**.
b7d70 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a 20 ** Ticket #202:
b7d80 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 If db->magic is
b7d90 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 65 not a valid ope
b7da0 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 n value, take ca
b7db0 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 re not.** to mod
b7dc0 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75 63 ify the db struc
b7dd0 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 ture at all. It
b7de0 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 64 could be that d
b7df0 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 b is a stale.**
b7e00 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 pointer. In oth
b7e10 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 er words, it cou
b7e20 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72 65 ld be that there
b7e30 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69 6f has been a prio
b7e40 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c r.** call to sql
b7e50 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 ite3_close(db) a
b7e60 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20 64 nd db has been d
b7e70 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 eallocated. And
b7e80 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 we do.** not wa
b7e90 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f nt to write into
b7ea0 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d deallocated mem
b7eb0 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 ory..*/.#ifdef S
b7ec0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
b7ed0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
b7ee0 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 qlite3SafetyOn(s
b7ef0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 qlite3 *db){. i
b7f00 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 f( db->magic==SQ
b7f10 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
b7f20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 ){. db->magic
b7f30 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f = SQLITE_MAGIC_
b7f40 42 55 53 59 3b 0a 20 20 20 20 61 73 73 65 72 74 BUSY;. assert
b7f50 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
b7f60 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
b7f70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b );. return 0;
b7f80 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d . }else if( db-
b7f90 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d >magic==SQLITE_M
b7fa0 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 AGIC_BUSY ){.
b7fb0 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c db->magic = SQL
b7fc0 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b ITE_MAGIC_ERROR;
b7fd0 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e . db->u1.isIn
b7fe0 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 terrupted = 1;.
b7ff0 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d }. return 1;.}
b8000 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 .#endif../*.** C
b8010 68 61 6e 67 65 20 74 68 65 20 6d 61 67 69 63 20 hange the magic
b8020 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 from SQLITE_MAGI
b8030 43 5f 42 55 53 59 20 74 6f 20 53 51 4c 49 54 45 C_BUSY to SQLITE
b8040 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 _MAGIC_OPEN..**
b8050 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 Return an error
b8060 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 (non-zero) if th
b8070 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 e magic was not
b8080 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
b8090 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 Y.** when this r
b80a0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
b80b0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
b80c0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
b80d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
b80e0 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c te3SafetyOff(sql
b80f0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 ite3 *db){. if(
b8100 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 db->magic==SQLI
b8110 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b TE_MAGIC_BUSY ){
b8120 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d . db->magic =
b8130 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 SQLITE_MAGIC_OP
b8140 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 EN;. assert(
b8150 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
b8160 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b ld(db->mutex) );
b8170 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
b8180 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e }else{. db->
b8190 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d magic = SQLITE_M
b81a0 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 AGIC_ERROR;.
b81b0 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 db->u1.isInterru
b81c0 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 pted = 1;. re
b81d0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 turn 1;. }.}.#e
b81e0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 ndif../*.** Chec
b81f0 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 k to make sure w
b8200 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20 64 e have a valid d
b8210 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 b pointer. This
b8220 20 74 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 test is not.**
b8230 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 foolproof but it
b8240 20 64 6f 65 73 20 70 72 6f 76 69 64 65 20 73 6f does provide so
b8250 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 20 70 72 me measure of pr
b8260 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 otection against
b8270 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 68 .** misuse of th
b8280 65 20 69 6e 74 65 72 66 61 63 65 20 73 75 63 68 e interface such
b8290 20 61 73 20 70 61 73 73 69 6e 67 20 69 6e 20 64 as passing in d
b82a0 62 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 b pointers that
b82b0 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 are.** NULL or w
b82c0 68 69 63 68 20 68 61 76 65 20 62 65 65 6e 20 70 hich have been p
b82d0 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 reviously closed
b82e0 2e 20 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 . If this routi
b82f0 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 ne returns.** 1
b8300 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
b8310 65 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 e db pointer is
b8320 76 61 6c 69 64 20 61 6e 64 20 30 20 69 66 20 69 valid and 0 if i
b8330 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a t should not be.
b8340 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64 20 ** dereferenced
b8350 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 for any reason.
b8360 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e The calling fun
b8370 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 ction should inv
b8380 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 oke.** SQLITE_MI
b8390 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 SUSE immediately
b83a0 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 ..**.** sqlite3S
b83b0 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 afetyCheckOk() r
b83c0 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68 65 equires that the
b83d0 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76 db pointer be v
b83e0 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e alid for.** use.
b83f0 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 sqlite3SafetyC
b8400 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 heckSickOrOk() a
b8410 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e 74 llows a db point
b8420 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20 74 er that failed t
b8430 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 o.** open proper
b8440 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 ly and is not fi
b8450 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 t for general us
b8460 65 20 62 75 74 20 77 68 69 63 68 20 63 61 6e 20 e but which can
b8470 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 6e be.** used as an
b8480 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c argument to sql
b8490 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 ite3_errmsg() or
b84a0 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 sqlite3_close()
b84b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
b84c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 ATE int sqlite3S
b84d0 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c afetyCheckOk(sql
b84e0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 ite3 *db){. u32
b84f0 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 magic;. if( db
b8500 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
b8510 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 magic = db->ma
b8520 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 gic;. if( magic
b8530 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f !=SQLITE_MAGIC_O
b8540 50 45 4e 20 0a 23 69 66 64 65 66 20 53 51 4c 49 PEN .#ifdef SQLI
b8550 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 26 26 TE_DEBUG. &&
b8560 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d magic!=SQLITE_M
b8570 41 47 49 43 5f 42 55 53 59 0a 23 65 6e 64 69 66 AGIC_BUSY.#endif
b8580 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e . ){. return
b8590 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 0;. }else{.
b85a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d return 1;. }.}
b85b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
b85c0 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 int sqlite3Safet
b85d0 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 yCheckSickOrOk(s
b85e0 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 qlite3 *db){. u
b85f0 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69 32 magic;. magi
b8600 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 c = db->magic;.
b8610 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 if( magic!=SQLI
b8620 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 TE_MAGIC_SICK &&
b8630 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 . magic!=SQ
b8640 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 LITE_MAGIC_OPEN
b8650 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d &&. magic!=
b8660 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 SQLITE_MAGIC_BUS
b8670 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 Y ) return 0;.
b8680 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a return 1;.}../**
b8690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 ************ End
b86a0 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a of util.c *****
b86b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b86c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b86d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a ***********/./**
b86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 ************ Beg
b86f0 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 63 20 2a in file hash.c *
b8700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a ***********/./*.
b8730 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 ** 2001 Septembe
b8740 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 r 22.**.** The a
b8750 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
b8760 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
b8770 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
b8780 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
b8790 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
b87a0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
b87b0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
b87c0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
b87d0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
b87e0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
b87f0 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
b8800 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
b8810 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
b8820 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
b8830 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
b8840 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
b8850 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
b8860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
b88a0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 ***.** This is t
b88b0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
b88c0 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73 n of generic has
b88d0 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 h-tables.** used
b88e0 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a in SQLite..*/..
b88f0 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d /* Turn bulk mem
b8900 6f 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 20 ory into a hash
b8910 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 table object by
b8920 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 initializing the
b8930 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 .** fields of th
b8940 65 20 48 61 73 68 20 73 74 72 75 63 74 75 72 65 e Hash structure
b8950 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 ..**.** "pNew" i
b8960 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
b8970 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 he hash table th
b8980 61 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 74 at is to be init
b8990 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ialized..*/.SQLI
b89a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
b89b0 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 sqlite3HashInit(
b89c0 48 61 73 68 20 2a 70 4e 65 77 29 7b 0a 20 20 61 Hash *pNew){. a
b89d0 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 ssert( pNew!=0 )
b89e0 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 ;. pNew->first
b89f0 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 = 0;. pNew->cou
b8a00 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e nt = 0;. pNew->
b8a10 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e htsize = 0;. pN
b8a20 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f ew->ht = 0;.}../
b8a30 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 * Remove all ent
b8a40 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 ries from a hash
b8a50 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d table. Reclaim
b8a60 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 all memory..**
b8a70 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e Call this routin
b8a80 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 e to delete a ha
b8a90 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 sh table or to r
b8aa0 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c eset a hash tabl
b8ab0 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 e.** to the empt
b8ac0 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 y state..*/.SQLI
b8ad0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
b8ae0 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 sqlite3HashClear
b8af0 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61 (Hash *pH){. Ha
b8b00 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 shElem *elem;
b8b10 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f /* For loo
b8b20 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c ping over all el
b8b30 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 ements of the ta
b8b40 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ble */.. assert
b8b50 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65 ( pH!=0 );. ele
b8b60 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 m = pH->first;.
b8b70 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a pH->first = 0;.
b8b80 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 sqlite3_free(p
b8b90 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 H->ht);. pH->ht
b8ba0 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69 = 0;. pH->htsi
b8bb0 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 ze = 0;. while(
b8bc0 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73 elem ){. Has
b8bd0 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d hElem *next_elem
b8be0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 = elem->next;.
b8bf0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
b8c00 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d 20 elem);. elem
b8c10 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20 7d = next_elem;. }
b8c20 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 . pH->count = 0
b8c30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 ;.}../*.** The h
b8c40 61 73 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e ashing function.
b8c50 0a 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 67 .*/.static unsig
b8c60 6e 65 64 20 69 6e 74 20 73 74 72 48 61 73 68 28 ned int strHash(
b8c70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 const char *z, i
b8c80 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 nt nKey){. int
b8c90 68 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 h = 0;. assert(
b8ca0 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 77 68 nKey>=0 );. wh
b8cb0 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20 20 29 ile( nKey > 0 )
b8cc0 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 {. h = (h<<3)
b8cd0 20 5e 20 68 20 5e 20 73 71 6c 69 74 65 33 55 70 ^ h ^ sqlite3Up
b8ce0 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 perToLower[(unsi
b8cf0 67 6e 65 64 20 63 68 61 72 29 2a 7a 2b 2b 5d 3b gned char)*z++];
b8d00 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d . nKey--;. }
b8d10 0a 20 20 72 65 74 75 72 6e 20 68 3b 0a 7d 0a 0a . return h;.}..
b8d20 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65 77 20 65 6c ./* Link pNew el
b8d30 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 ement into the h
b8d40 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 49 ash table pH. I
b8d50 66 20 70 45 6e 74 72 79 21 3d 30 20 74 68 65 6e f pEntry!=0 then
b8d60 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73 65 72 74 20 also.** insert
b8d70 70 4e 65 77 20 69 6e 74 6f 20 74 68 65 20 70 45 pNew into the pE
b8d80 6e 74 72 79 20 68 61 73 68 20 62 75 63 6b 65 74 ntry hash bucket
b8d90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
b8da0 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a insertElement(.
b8db0 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 Hash *pH,
b8dc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
b8dd0 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 complete hash ta
b8de0 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 ble */. struct
b8df0 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 20 20 _ht *pEntry,
b8e00 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e 74 /* The entry int
b8e10 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73 20 o which pNew is
b8e20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 48 61 inserted */. Ha
b8e30 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20 shElem *pNew
b8e40 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d /* The elem
b8e50 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 ent to be insert
b8e60 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 ed */.){. HashE
b8e70 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 lem *pHead;
b8e80 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 /* First eleme
b8e90 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20 70 45 nt already in pE
b8ea0 6e 74 72 79 20 2a 2f 0a 20 20 69 66 28 20 70 45 ntry */. if( pE
b8eb0 6e 74 72 79 20 29 7b 0a 20 20 20 20 70 48 65 61 ntry ){. pHea
b8ec0 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e d = pEntry->coun
b8ed0 74 20 3f 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 t ? pEntry->chai
b8ee0 6e 20 3a 20 30 3b 0a 20 20 20 20 70 45 6e 74 72 n : 0;. pEntr
b8ef0 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 y->count++;.
b8f00 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 pEntry->chain =
b8f10 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 pNew;. }else{.
b8f20 20 20 20 70 48 65 61 64 20 3d 20 30 3b 0a 20 20 pHead = 0;.
b8f30 7d 0a 20 20 69 66 28 20 70 48 65 61 64 20 29 7b }. if( pHead ){
b8f40 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 . pNew->next
b8f50 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 = pHead;. pNe
b8f60 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64 2d w->prev = pHead-
b8f70 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20 70 >prev;. if( p
b8f80 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 Head->prev ){ pH
b8f90 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 ead->prev->next
b8fa0 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c = pNew; }. el
b8fb0 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 7b se {
b8fc0 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 pH->first = pNe
b8fd0 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e w; }. pHead->
b8fe0 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d prev = pNew;. }
b8ff0 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e else{. pNew->
b9000 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 next = pH->first
b9010 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66 69 ;. if( pH->fi
b9020 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 rst ){ pH->first
b9030 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d ->prev = pNew; }
b9040 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 . pNew->prev
b9050 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 = 0;. pH->fir
b9060 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d st = pNew;. }.}
b9070 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 .../* Resize the
b9080 20 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 hash table so t
b9090 68 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 hat it cantains
b90a0 22 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 "new_size" bucke
b90b0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 61 ts..**.** The ha
b90c0 73 68 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 sh table might f
b90d0 61 69 6c 20 74 6f 20 72 65 73 69 7a 65 20 69 66 ail to resize if
b90e0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
b90f0 29 20 66 61 69 6c 73 20 6f 72 0a 2a 2a 20 69 66 ) fails or.** if
b9100 20 74 68 65 20 6e 65 77 20 73 69 7a 65 20 69 73 the new size is
b9110 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
b9120 20 70 72 69 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 prior size..**
b9130 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 Return TRUE if t
b9140 68 65 20 72 65 73 69 7a 65 20 6f 63 63 75 72 73 he resize occurs
b9150 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f and false if no
b9160 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
b9170 20 72 65 68 61 73 68 28 48 61 73 68 20 2a 70 48 rehash(Hash *pH
b9180 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e , unsigned int n
b9190 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 ew_size){. stru
b91a0 63 74 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 ct _ht *new_ht;
b91b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
b91c0 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 e new hash table
b91d0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
b91e0 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d elem, *next_elem
b91f0 3b 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 ; /* For loop
b9200 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e ing over existin
b9210 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 g elements */..#
b9220 69 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 if SQLITE_MALLOC
b9230 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e 30 0a 20 20 _SOFT_LIMIT>0.
b9240 69 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a if( new_size*siz
b9250 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e eof(struct _ht)>
b9260 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f SQLITE_MALLOC_SO
b9270 46 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 FT_LIMIT ){.
b9280 6e 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 new_size = SQLIT
b9290 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 E_MALLOC_SOFT_LI
b92a0 4d 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 MIT/sizeof(struc
b92b0 74 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 t _ht);. }. if
b92c0 28 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e ( new_size==pH->
b92d0 68 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e 20 htsize ) return
b92e0 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 0;.#endif.. /*
b92f0 54 68 65 20 69 6e 61 62 69 6c 69 74 79 20 74 6f The inability to
b9300 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65 allocates space
b9310 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 68 61 for a larger ha
b9320 73 68 20 74 61 62 6c 65 20 69 73 0a 20 20 2a 2a sh table is. **
b9330 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 a performance h
b9340 69 74 20 62 75 74 20 69 74 20 69 73 20 6e 6f 74 it but it is not
b9350 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 a fatal error.
b9360 20 53 6f 20 6d 61 72 6b 20 74 68 65 0a 20 20 2a So mark the. *
b9370 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 73 20 * allocation as
b9380 61 20 62 65 6e 69 67 6e 2e 0a 20 20 2a 2f 0a 20 a benign.. */.
b9390 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
b93a0 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 6e ignMalloc();. n
b93b0 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 ew_ht = (struct
b93c0 5f 68 74 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c _ht *)sqlite3Mal
b93d0 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 loc( new_size*si
b93e0 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 zeof(struct _ht)
b93f0 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 );. sqlite3End
b9400 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
b9410 0a 20 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 . if( new_ht==0
b9420 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 ) return 0;. s
b9430 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 2d 3e qlite3_free(pH->
b9440 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 ht);. pH->ht =
b9450 6e 65 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 new_ht;. pH->ht
b9460 73 69 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 20 size = new_size
b9470 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 = sqlite3MallocS
b9480 69 7a 65 28 6e 65 77 5f 68 74 29 2f 73 69 7a 65 ize(new_ht)/size
b9490 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a of(struct _ht);.
b94a0 20 20 6d 65 6d 73 65 74 28 6e 65 77 5f 68 74 2c memset(new_ht,
b94b0 20 30 2c 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 0, new_size*siz
b94c0 65 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 29 eof(struct _ht))
b94d0 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d ;. for(elem=pH-
b94e0 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73 >first, pH->firs
b94f0 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 t=0; elem; elem
b9500 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 = next_elem){.
b9510 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
b9520 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d 2d = strHash(elem-
b9530 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 >pKey, elem->nKe
b9540 79 29 20 25 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 y) % new_size;.
b9550 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 next_elem = e
b9560 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69 lem->next;. i
b9570 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c nsertElement(pH,
b9580 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65 &new_ht[h], ele
b9590 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e m);. }. return
b95a0 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 66 1;.}../* This f
b95b0 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 69 6e 74 unction (for int
b95c0 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 29 20 ernal use only)
b95d0 6c 6f 63 61 74 65 73 20 61 6e 20 65 6c 65 6d 65 locates an eleme
b95e0 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20 68 61 73 68 nt in an.** hash
b95f0 20 74 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 table that matc
b9600 68 65 73 20 74 68 65 20 67 69 76 65 6e 20 6b 65 hes the given ke
b9610 79 2e 20 20 54 68 65 20 68 61 73 68 20 66 6f 72 y. The hash for
b9620 20 74 68 69 73 20 6b 65 79 20 68 61 73 0a 2a 2a this key has.**
b9630 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f already been co
b9640 6d 70 75 74 65 64 20 61 6e 64 20 69 73 20 70 61 mputed and is pa
b9650 73 73 65 64 20 61 73 20 74 68 65 20 34 74 68 20 ssed as the 4th
b9660 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 73 74 parameter..*/.st
b9670 61 74 69 63 20 48 61 73 68 45 6c 65 6d 20 2a 66 atic HashElem *f
b9680 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 indElementGivenH
b9690 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 48 61 73 ash(. const Has
b96a0 68 20 2a 70 48 2c 20 20 20 20 20 2f 2a 20 54 68 h *pH, /* Th
b96b0 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63 e pH to be searc
b96c0 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 hed */. const c
b96d0 68 61 72 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 har *pKey, /*
b96e0 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73 The key we are s
b96f0 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a earching for */.
b9700 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 int nKey,
b9710 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 /* Bytes i
b9720 6e 20 6b 65 79 20 28 6e 6f 74 20 63 6f 75 6e 74 n key (not count
b9730 69 6e 67 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 ing zero termina
b9740 74 6f 72 29 20 2a 2f 0a 20 20 75 6e 73 69 67 6e tor) */. unsign
b9750 65 64 20 69 6e 74 20 68 20 20 20 20 20 20 2f 2a ed int h /*
b9760 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 The hash for th
b9770 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 is key. */.){.
b9780 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 HashElem *elem;
b9790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
b97a0 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 * Used to loop t
b97b0 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 hru the element
b97c0 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f list */. int co
b97d0 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 unt;
b97e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
b97f0 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c er of elements l
b9800 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a 0a eft to test */..
b9810 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a if( pH->ht ){.
b9820 20 20 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a struct _ht *
b9830 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 pEntry = &pH->ht
b9840 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 [h];. elem =
b9850 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 pEntry->chain;.
b9860 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72 count = pEntr
b9870 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 7d 65 6c 73 y->count;. }els
b9880 65 7b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 48 e{. elem = pH
b9890 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 63 6f 75 ->first;. cou
b98a0 6e 74 20 3d 20 70 48 2d 3e 63 6f 75 6e 74 3b 0a nt = pH->count;.
b98b0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 63 6f 75 }. while( cou
b98c0 6e 74 2d 2d 20 26 26 20 41 4c 57 41 59 53 28 65 nt-- && ALWAYS(e
b98d0 6c 65 6d 29 20 29 7b 0a 20 20 20 20 69 66 28 20 lem) ){. if(
b98e0 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d 6e 4b 65 79 elem->nKey==nKey
b98f0 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 && sqlite3StrNI
b9900 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 70 Cmp(elem->pKey,p
b9910 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 Key,nKey)==0 ){
b9920 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c . return el
b9930 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c em;. }. el
b9940 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b em = elem->next;
b9950 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
b9960 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 20 .}../* Remove a
b9970 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66 72 6f single entry fro
b9980 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 m the hash table
b9990 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74 65 72 given a pointer
b99a0 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c 65 6d to that.** elem
b99b0 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68 20 6f ent and a hash o
b99c0 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27 73 20 n the element's
b99d0 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 key..*/.static v
b99e0 6f 69 64 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e oid removeElemen
b99f0 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 48 61 tGivenHash(. Ha
b9a00 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 sh *pH,
b9a10 2f 2a 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69 /* The pH contai
b9a20 6e 69 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 ning "elem" */.
b9a30 20 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c HashElem* elem,
b9a40 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e /* The elemen
b9a50 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 t to be removed
b9a60 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 from the pH */.
b9a70 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 unsigned int h
b9a80 20 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 /* Hash value
b9a90 20 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 for the element
b9aa0 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 */.){. struct
b9ab0 5f 68 74 20 2a 70 45 6e 74 72 79 3b 0a 20 20 69 _ht *pEntry;. i
b9ac0 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76 20 29 7b f( elem->prev ){
b9ad0 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72 65 76 2d . elem->prev-
b9ae0 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 >next = elem->ne
b9af0 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 xt; . }else{.
b9b00 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 65 6c pH->first = el
b9b10 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 em->next;. }.
b9b20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 if( elem->next )
b9b30 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 {. elem->next
b9b40 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 ->prev = elem->p
b9b50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 rev;. }. if( p
b9b60 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 70 45 6e H->ht ){. pEn
b9b70 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d try = &pH->ht[h]
b9b80 3b 0a 20 20 20 20 69 66 28 20 70 45 6e 74 72 79 ;. if( pEntry
b9b90 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b ->chain==elem ){
b9ba0 0a 20 20 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 . pEntry->c
b9bb0 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 hain = elem->nex
b9bc0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 6e t;. }. pEn
b9bd0 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 try->count--;.
b9be0 20 20 61 73 73 65 72 74 28 20 70 45 6e 74 72 79 assert( pEntry
b9bf0 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 ->count>=0 );.
b9c00 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 }. sqlite3_free
b9c10 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e ( elem );. pH->
b9c20 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 count--;. if( p
b9c30 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 H->count<=0 ){.
b9c40 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 assert( pH->f
b9c50 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 irst==0 );. a
b9c60 73 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 ssert( pH->count
b9c70 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 ==0 );. sqlit
b9c80 65 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b e3HashClear(pH);
b9c90 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d . }.}../* Attem
b9ca0 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20 pt to locate an
b9cb0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68 element of the h
b9cc0 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74 ash table pH wit
b9cd0 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20 h a key.** that
b9ce0 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65 matches pKey,nKe
b9cf0 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64 y. Return the d
b9d00 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65 ata for this ele
b9d10 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a ment if it is.**
b9d20 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 found, or NULL
b9d30 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d if there is no m
b9d40 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f atch..*/.SQLITE_
b9d50 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
b9d60 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63 6f lite3HashFind(co
b9d70 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 63 6f nst Hash *pH, co
b9d80 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 nst char *pKey,
b9d90 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 48 61 73 int nKey){. Has
b9da0 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 hElem *elem;
b9db0 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 /* The element t
b9dc0 68 61 74 20 6d 61 74 63 68 65 73 20 6b 65 79 20 hat matches key
b9dd0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e */. unsigned in
b9de0 74 20 68 3b 20 20 20 20 2f 2a 20 41 20 68 61 73 t h; /* A has
b9df0 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 0a 20 20 61 h on key */.. a
b9e00 73 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a ssert( pH!=0 );.
b9e10 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 21 3d assert( pKey!=
b9e20 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
b9e30 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 Key>=0 );. if(
b9e40 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 68 20 pH->ht ){. h
b9e50 3d 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 = strHash(pKey,
b9e60 6e 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 nKey) % pH->htsi
b9e70 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ze;. }else{.
b9e80 20 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c h = 0;. }. el
b9e90 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 em = findElement
b9ea0 47 69 76 65 6e 48 61 73 68 28 70 48 2c 20 70 4b GivenHash(pH, pK
b9eb0 65 79 2c 20 6e 4b 65 79 2c 20 68 29 3b 0a 20 20 ey, nKey, h);.
b9ec0 72 65 74 75 72 6e 20 65 6c 65 6d 20 3f 20 65 6c return elem ? el
b9ed0 65 6d 2d 3e 64 61 74 61 20 3a 20 30 3b 0a 7d 0a em->data : 0;.}.
b9ee0 0a 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 65 6c ./* Insert an el
b9ef0 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 ement into the h
b9f00 61 73 68 20 74 61 62 6c 65 20 70 48 2e 20 20 54 ash table pH. T
b9f10 68 65 20 6b 65 79 20 69 73 20 70 4b 65 79 2c 6e he key is pKey,n
b9f20 4b 65 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 Key.** and the d
b9f30 61 74 61 20 69 73 20 22 64 61 74 61 22 2e 0a 2a ata is "data"..*
b9f40 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65 6c 65 6d 65 *.** If no eleme
b9f50 6e 74 20 65 78 69 73 74 73 20 77 69 74 68 20 61 nt exists with a
b9f60 20 6d 61 74 63 68 69 6e 67 20 6b 65 79 2c 20 74 matching key, t
b9f70 68 65 6e 20 61 20 6e 65 77 0a 2a 2a 20 65 6c 65 hen a new.** ele
b9f80 6d 65 6e 74 20 69 73 20 63 72 65 61 74 65 64 20 ment is created
b9f90 61 6e 64 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 and NULL is retu
b9fa0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 rned..**.** If a
b9fb0 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61 nother element a
b9fc0 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77 69 lready exists wi
b9fd0 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c th the same key,
b9fe0 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 then the.** new
b9ff0 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 74 data replaces t
ba000 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20 he old data and
ba010 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20 the old data is
ba020 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 returned..** The
ba030 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69 key is not copi
ba040 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61 ed in this insta
ba050 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f nce. If a mallo
ba060 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a c fails, then.**
ba070 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73 the new data is
ba080 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 returned and th
ba090 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20 e hash table is
ba0a0 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a unchanged..**.**
ba0b0 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20 70 If the "data" p
ba0c0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 arameter to this
ba0d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c function is NUL
ba0e0 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65 L, then the.** e
ba0f0 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e lement correspon
ba100 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73 ding to "key" is
ba110 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 removed from th
ba120 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f e hash table..*/
ba130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
ba140 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 void *sqlite3Has
ba150 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a 70 48 hInsert(Hash *pH
ba160 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b , const char *pK
ba170 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f ey, int nKey, vo
ba180 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 75 6e 73 id *data){. uns
ba190 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20 igned int h;
ba1a0 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f /* the hash o
ba1b0 66 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f f the key modulo
ba1c0 20 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 hash table size
ba1d0 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a */. HashElem *
ba1e0 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 elem; /* U
ba1f0 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 sed to loop thru
ba200 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 the element lis
ba210 74 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 t */. HashElem
ba220 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 *new_elem; /*
ba230 4e 65 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 New element adde
ba240 64 20 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 0a d to the pH */..
ba250 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 assert( pH!=0
ba260 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 );. assert( pKe
ba270 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 y!=0 );. assert
ba280 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20 69 ( nKey>=0 );. i
ba290 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b f( pH->htsize ){
ba2a0 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 . h = strHash
ba2b0 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 (pKey, nKey) % p
ba2c0 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c H->htsize;. }el
ba2d0 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 se{. h = 0;.
ba2e0 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 }. elem = find
ba2f0 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 ElementGivenHash
ba300 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 (pH,pKey,nKey,h)
ba310 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a ;. if( elem ){.
ba320 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 void *old_da
ba330 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b ta = elem->data;
ba340 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 . if( data==0
ba350 20 29 7b 0a 20 20 20 20 20 20 72 65 6d 6f 76 65 ){. remove
ba360 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 ElementGivenHash
ba370 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 (pH,elem,h);.
ba380 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c }else{. el
ba390 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b em->data = data;
ba3a0 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e 70 4b 65 . elem->pKe
ba3b0 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 20 y = pKey;.
ba3c0 61 73 73 65 72 74 28 6e 4b 65 79 3d 3d 65 6c 65 assert(nKey==ele
ba3d0 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 7d 0a m->nKey);. }.
ba3e0 20 20 20 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 return old_d
ba3f0 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 ata;. }. if( d
ba400 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 ata==0 ) return
ba410 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 0;. new_elem =
ba420 28 48 61 73 68 45 6c 65 6d 2a 29 73 71 6c 69 74 (HashElem*)sqlit
ba430 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 e3Malloc( sizeof
ba440 28 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20 20 (HashElem) );.
ba450 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30 20 if( new_elem==0
ba460 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 ) return data;.
ba470 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 new_elem->pKey
ba480 3d 20 70 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c = pKey;. new_el
ba490 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b em->nKey = nKey;
ba4a0 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 . new_elem->dat
ba4b0 61 20 3d 20 64 61 74 61 3b 0a 20 20 70 48 2d 3e a = data;. pH->
ba4c0 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 count++;. if( p
ba4d0 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30 20 26 26 20 H->count>=10 &&
ba4e0 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 32 2a 70 48 pH->count > 2*pH
ba4f0 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 ->htsize ){.
ba500 69 66 28 20 72 65 68 61 73 68 28 70 48 2c 20 70 if( rehash(pH, p
ba510 48 2d 3e 63 6f 75 6e 74 2a 32 29 20 29 7b 0a 20 H->count*2) ){.
ba520 20 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d assert( pH-
ba530 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 20 >htsize>0 );.
ba540 20 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 h = strHash(p
ba550 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d Key, nKey) % pH-
ba560 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 7d 0a 20 >htsize;. }.
ba570 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 }. if( pH->ht
ba580 29 7b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 ){. insertEle
ba590 6d 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 ment(pH, &pH->ht
ba5a0 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a [h], new_elem);.
ba5b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 73 }else{. ins
ba5c0 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 30 ertElement(pH, 0
ba5d0 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 7d , new_elem);. }
ba5e0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a . return 0;.}..
ba5f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
ba600 45 6e 64 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a End of hash.c **
ba610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
ba640 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
ba650 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 Begin file opcod
ba660 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a es.c ***********
ba670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ba680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
ba690 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 /* Automatically
ba6a0 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 generated. Do
ba6b0 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 not edit */./* S
ba6c0 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 ee the mkopcodec
ba6d0 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 .awk script for
ba6e0 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 details. */.#if
ba6f0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
ba700 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c OMIT_EXPLAIN) ||
ba710 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 !defined(NDEBUG
ba720 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 ) || defined(VDB
ba730 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 E_PROFILE) || de
ba740 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
ba750 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 UG).SQLITE_PRIVA
ba760 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 TE const char *s
ba770 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 qlite3OpcodeName
ba780 28 69 6e 74 20 69 29 7b 0a 20 73 74 61 74 69 63 (int i){. static
ba790 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e const char *con
ba7a0 73 74 20 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 st azName[] = {
ba7b0 22 3f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 "?",. /* 1
ba7c0 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 */ "Goto",.
ba7d0 20 2f 2a 20 20 20 32 20 2a 2f 20 22 47 6f 73 75 /* 2 */ "Gosu
ba7e0 62 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33 20 b",. /* 3
ba7f0 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20 */ "Return",.
ba800 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 59 69 65 /* 4 */ "Yie
ba810 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 35 ld",. /* 5
ba820 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22 */ "HaltIfNull"
ba830 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36 20 2a 2f ,. /* 6 */
ba840 20 22 48 61 6c 74 22 2c 0a 20 20 20 20 20 2f 2a "Halt",. /*
ba850 20 20 20 37 20 2a 2f 20 22 49 6e 74 65 67 65 72 7 */ "Integer
ba860 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 38 20 2a ",. /* 8 *
ba870 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20 20 20 / "Int64",.
ba880 2f 2a 20 20 20 39 20 2a 2f 20 22 53 74 72 69 6e /* 9 */ "Strin
ba890 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 g",. /* 10
ba8a0 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 */ "Null",.
ba8b0 2f 2a 20 20 31 31 20 2a 2f 20 22 42 6c 6f 62 22 /* 11 */ "Blob"
ba8c0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f ,. /* 12 */
ba8d0 20 22 56 61 72 69 61 62 6c 65 22 2c 0a 20 20 20 "Variable",.
ba8e0 20 20 2f 2a 20 20 31 33 20 2a 2f 20 22 4d 6f 76 /* 13 */ "Mov
ba8f0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 34 20 e",. /* 14
ba900 2a 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 */ "Copy",.
ba910 2f 2a 20 20 31 35 20 2a 2f 20 22 53 43 6f 70 79 /* 15 */ "SCopy
ba920 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20 2a ",. /* 16 *
ba930 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a 20 / "ResultRow",.
ba940 20 20 20 20 2f 2a 20 20 31 37 20 2a 2f 20 22 43 /* 17 */ "C
ba950 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20 2f 2a ollSeq",. /*
ba960 20 20 31 38 20 2a 2f 20 22 46 75 6e 63 74 69 6f 18 */ "Functio
ba970 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20 n",. /* 19
ba980 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f */ "Not",. /
ba990 2a 20 20 32 30 20 2a 2f 20 22 41 64 64 49 6d 6d * 20 */ "AddImm
ba9a0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a ",. /* 21 *
ba9b0 2f 20 22 4d 75 73 74 42 65 49 6e 74 22 2c 0a 20 / "MustBeInt",.
ba9c0 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f 20 22 52 /* 22 */ "R
ba9d0 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 ealAffinity",.
ba9e0 20 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 50 65 /* 23 */ "Pe
ba9f0 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20 20 20 rmutation",.
baa00 20 2f 2a 20 20 32 34 20 2a 2f 20 22 43 6f 6d 70 /* 24 */ "Comp
baa10 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 are",. /* 2
baa20 35 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20 20 20 5 */ "Jump",.
baa30 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 49 66 22 /* 26 */ "If"
baa40 2c 0a 20 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f ,. /* 27 */
baa50 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f "IfNot",. /
baa60 2a 20 20 32 38 20 2a 2f 20 22 43 6f 6c 75 6d 6e * 28 */ "Column
baa70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 39 20 2a ",. /* 29 *
baa80 2f 20 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 / "Affinity",.
baa90 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20 22 4d 61 /* 30 */ "Ma
baaa0 6b 65 52 65 63 6f 72 64 22 2c 0a 20 20 20 20 20 keRecord",.
baab0 2f 2a 20 20 33 31 20 2a 2f 20 22 43 6f 75 6e 74 /* 31 */ "Count
baac0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a ",. /* 32 *
baad0 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20 / "Savepoint",.
baae0 20 20 20 20 2f 2a 20 20 33 33 20 2a 2f 20 22 41 /* 33 */ "A
baaf0 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20 utoCommit",.
bab00 20 2f 2a 20 20 33 34 20 2a 2f 20 22 54 72 61 6e /* 34 */ "Tran
bab10 73 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f saction",. /
bab20 2a 20 20 33 35 20 2a 2f 20 22 52 65 61 64 43 6f * 35 */ "ReadCo
bab30 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 okie",. /*
bab40 33 36 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 36 */ "SetCookie
bab50 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 37 20 2a ",. /* 37 *
bab60 2f 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 / "VerifyCookie"
bab70 2c 0a 20 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f ,. /* 38 */
bab80 20 22 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20 20 "OpenRead",.
bab90 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22 4f 70 65 /* 39 */ "Ope
baba0 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20 2f 2a nWrite",. /*
babb0 20 20 34 30 20 2a 2f 20 22 4f 70 65 6e 45 70 68 40 */ "OpenEph
babc0 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a emeral",. /*
babd0 20 20 34 31 20 2a 2f 20 22 4f 70 65 6e 50 73 65 41 */ "OpenPse
babe0 75 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 udo",. /* 4
babf0 32 20 2a 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 2 */ "Close",.
bac00 20 20 20 2f 2a 20 20 34 33 20 2a 2f 20 22 53 65 /* 43 */ "Se
bac10 65 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ekLt",. /*
bac20 34 34 20 2a 2f 20 22 53 65 65 6b 4c 65 22 2c 0a 44 */ "SeekLe",.
bac30 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20 22 /* 45 */ "
bac40 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a SeekGe",. /*
bac50 20 20 34 36 20 2a 2f 20 22 53 65 65 6b 47 74 22 46 */ "SeekGt"
bac60 2c 0a 20 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f ,. /* 47 */
bac70 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a "Seek",. /*
bac80 20 20 34 38 20 2a 2f 20 22 4e 6f 74 46 6f 75 6e 48 */ "NotFoun
bac90 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 d",. /* 49
baca0 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20 20 20 20 */ "Found",.
bacb0 20 2f 2a 20 20 35 30 20 2a 2f 20 22 49 73 55 6e /* 50 */ "IsUn
bacc0 69 71 75 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ique",. /*
bacd0 35 31 20 2a 2f 20 22 4e 6f 74 45 78 69 73 74 73 51 */ "NotExists
bace0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32 20 2a ",. /* 52 *
bacf0 2f 20 22 53 65 71 75 65 6e 63 65 22 2c 0a 20 20 / "Sequence",.
bad00 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 4e 65 /* 53 */ "Ne
bad10 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a wRowid",. /*
bad20 20 20 35 34 20 2a 2f 20 22 49 6e 73 65 72 74 22 54 */ "Insert"
bad30 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f ,. /* 55 */
bad40 20 22 49 6e 73 65 72 74 49 6e 74 22 2c 0a 20 20 "InsertInt",.
bad50 20 20 20 2f 2a 20 20 35 36 20 2a 2f 20 22 44 65 /* 56 */ "De
bad60 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 lete",. /*
bad70 35 37 20 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e 57 */ "ResetCoun
bad80 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 t",. /* 58
bad90 2a 2f 20 22 52 6f 77 4b 65 79 22 2c 0a 20 20 20 */ "RowKey",.
bada0 20 20 2f 2a 20 20 35 39 20 2a 2f 20 22 52 6f 77 /* 59 */ "Row
badb0 44 61 74 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Data",. /*
badc0 36 30 20 2a 2f 20 22 52 6f 77 69 64 22 2c 0a 20 60 */ "Rowid",.
badd0 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 4e /* 61 */ "N
bade0 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a ullRow",. /*
badf0 20 20 36 32 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 62 */ "Last",.
bae00 20 20 20 20 20 2f 2a 20 20 36 33 20 2a 2f 20 22 /* 63 */ "
bae10 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Sort",. /*
bae20 36 34 20 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 64 */ "Rewind",.
bae30 20 20 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 /* 65 */ "
bae40 50 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Prev",. /*
bae50 36 36 20 2a 2f 20 22 4e 65 78 74 22 2c 0a 20 20 66 */ "Next",.
bae60 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 49 64 /* 67 */ "Id
bae70 78 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f xInsert",. /
bae80 2a 20 20 36 38 20 2a 2f 20 22 4f 72 22 2c 0a 20 * 68 */ "Or",.
bae90 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22 41 /* 69 */ "A
baea0 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 30 nd",. /* 70
baeb0 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65 22 2c */ "IdxDelete",
baec0 0a 20 20 20 20 20 2f 2a 20 20 37 31 20 2a 2f 20 . /* 71 */
baed0 22 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 "IdxRowid",.
baee0 20 2f 2a 20 20 37 32 20 2a 2f 20 22 49 64 78 4c /* 72 */ "IdxL
baef0 54 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 T",. /* 73
baf00 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 */ "IsNull",.
baf10 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74 /* 74 */ "Not
baf20 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 Null",. /*
baf30 37 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 75 */ "Ne",.
baf40 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22 2c /* 76 */ "Eq",
baf50 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 . /* 77 */
baf60 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 "Gt",. /* 7
baf70 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 8 */ "Le",.
baf80 2f 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 2c 0a /* 79 */ "Lt",.
baf90 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 /* 80 */ "
bafa0 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 Ge",. /* 81
bafb0 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20 20 */ "IdxGE",.
bafc0 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22 42 69 74 /* 82 */ "Bit
bafd0 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 And",. /* 8
bafe0 33 20 2a 2f 20 22 42 69 74 4f 72 22 2c 0a 20 20 3 */ "BitOr",.
baff0 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20 22 53 68 /* 84 */ "Sh
bb000 69 66 74 4c 65 66 74 22 2c 0a 20 20 20 20 20 2f iftLeft",. /
bb010 2a 20 20 38 35 20 2a 2f 20 22 53 68 69 66 74 52 * 85 */ "ShiftR
bb020 69 67 68 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 ight",. /*
bb030 38 36 20 2a 2f 20 22 41 64 64 22 2c 0a 20 20 20 86 */ "Add",.
bb040 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22 53 75 62 /* 87 */ "Sub
bb050 74 72 61 63 74 22 2c 0a 20 20 20 20 20 2f 2a 20 tract",. /*
bb060 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69 70 6c 79 88 */ "Multiply
bb070 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 39 20 2a ",. /* 89 *
bb080 2f 20 22 44 69 76 69 64 65 22 2c 0a 20 20 20 20 / "Divide",.
bb090 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52 65 6d 61 /* 90 */ "Rema
bb0a0 69 6e 64 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 inder",. /*
bb0b0 20 39 31 20 2a 2f 20 22 43 6f 6e 63 61 74 22 2c 91 */ "Concat",
bb0c0 0a 20 20 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 . /* 92 */
bb0d0 22 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 "Destroy",.
bb0e0 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74 4e 6f /* 93 */ "BitNo
bb0f0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 t",. /* 94
bb100 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 */ "String8",.
bb110 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22 43 6c /* 95 */ "Cl
bb120 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 ear",. /* 9
bb130 36 20 2a 2f 20 22 43 72 65 61 74 65 49 6e 64 65 6 */ "CreateInde
bb140 78 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37 20 x",. /* 97
bb150 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65 22 */ "CreateTable"
bb160 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20 2a 2f ,. /* 98 */
bb170 20 22 50 61 72 73 65 53 63 68 65 6d 61 22 2c 0a "ParseSchema",.
bb180 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 /* 99 */ "
bb190 4c 6f 61 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 LoadAnalysis",.
bb1a0 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22 44 /* 100 */ "D
bb1b0 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 ropTable",.
bb1c0 2f 2a 20 31 30 31 20 2a 2f 20 22 44 72 6f 70 49 /* 101 */ "DropI
bb1d0 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20 31 ndex",. /* 1
bb1e0 30 32 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 02 */ "DropTrigg
bb1f0 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 33 er",. /* 103
bb200 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43 6b */ "IntegrityCk
bb210 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 2a ",. /* 104 *
bb220 2f 20 22 52 6f 77 53 65 74 41 64 64 22 2c 0a 20 / "RowSetAdd",.
bb230 20 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 52 /* 105 */ "R
bb240 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 20 20 20 owSetRead",.
bb250 20 2f 2a 20 31 30 36 20 2a 2f 20 22 52 6f 77 53 /* 106 */ "RowS
bb260 65 74 54 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a etTest",. /*
bb270 20 31 30 37 20 2a 2f 20 22 50 72 6f 67 72 61 6d 107 */ "Program
bb280 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 38 20 2a ",. /* 108 *
bb290 2f 20 22 50 61 72 61 6d 22 2c 0a 20 20 20 20 20 / "Param",.
bb2a0 2f 2a 20 31 30 39 20 2a 2f 20 22 46 6b 43 6f 75 /* 109 */ "FkCou
bb2b0 6e 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 nter",. /* 1
bb2c0 31 30 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22 10 */ "FkIfZero"
bb2d0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f ,. /* 111 */
bb2e0 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20 "MemMax",.
bb2f0 2f 2a 20 31 31 32 20 2a 2f 20 22 49 66 50 6f 73 /* 112 */ "IfPos
bb300 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 33 20 2a ",. /* 113 *
bb310 2f 20 22 49 66 4e 65 67 22 2c 0a 20 20 20 20 20 / "IfNeg",.
bb320 2f 2a 20 31 31 34 20 2a 2f 20 22 49 66 5a 65 72 /* 114 */ "IfZer
bb330 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 35 20 o",. /* 115
bb340 2a 2f 20 22 41 67 67 53 74 65 70 22 2c 0a 20 20 */ "AggStep",.
bb350 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 41 67 /* 116 */ "Ag
bb360 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a gFinal",. /*
bb370 20 31 31 37 20 2a 2f 20 22 56 61 63 75 75 6d 22 117 */ "Vacuum"
bb380 2c 0a 20 20 20 20 20 2f 2a 20 31 31 38 20 2a 2f ,. /* 118 */
bb390 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a 20 "IncrVacuum",.
bb3a0 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f 20 22 45 /* 119 */ "E
bb3b0 78 70 69 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 xpire",. /*
bb3c0 31 32 30 20 2a 2f 20 22 54 61 62 6c 65 4c 6f 63 120 */ "TableLoc
bb3d0 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 31 20 k",. /* 121
bb3e0 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 */ "VBegin",.
bb3f0 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 56 43 72 /* 122 */ "VCr
bb400 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 eate",. /* 1
bb410 32 33 20 2a 2f 20 22 56 44 65 73 74 72 6f 79 22 23 */ "VDestroy"
bb420 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f ,. /* 124 */
bb430 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 2f "VOpen",. /
bb440 2a 20 31 32 35 20 2a 2f 20 22 56 46 69 6c 74 65 * 125 */ "VFilte
bb450 72 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20 r",. /* 126
bb460 2a 2f 20 22 56 43 6f 6c 75 6d 6e 22 2c 0a 20 20 */ "VColumn",.
bb470 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 4e /* 127 */ "VN
bb480 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 ext",. /* 12
bb490 38 20 2a 2f 20 22 56 52 65 6e 61 6d 65 22 2c 0a 8 */ "VRename",.
bb4a0 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 /* 129 */ "
bb4b0 56 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f VUpdate",. /
bb4c0 2a 20 31 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c * 130 */ "Real",
bb4d0 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 . /* 131 */
bb4e0 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a 20 20 20 "Pagecount",.
bb4f0 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 54 72 61 /* 132 */ "Tra
bb500 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 33 ce",. /* 133
bb510 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20 */ "Noop",.
bb520 20 2f 2a 20 31 33 34 20 2a 2f 20 22 45 78 70 6c /* 134 */ "Expl
bb530 61 69 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 ain",. /* 13
bb540 35 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 5 */ "NotUsed_13
bb550 35 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 36 20 5",. /* 136
bb560 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 36 22 */ "NotUsed_136"
bb570 2c 0a 20 20 20 20 20 2f 2a 20 31 33 37 20 2a 2f ,. /* 137 */
bb580 20 22 4e 6f 74 55 73 65 64 5f 31 33 37 22 2c 0a "NotUsed_137",.
bb590 20 20 20 20 20 2f 2a 20 31 33 38 20 2a 2f 20 22 /* 138 */ "
bb5a0 4e 6f 74 55 73 65 64 5f 31 33 38 22 2c 0a 20 20 NotUsed_138",.
bb5b0 20 20 20 2f 2a 20 31 33 39 20 2a 2f 20 22 4e 6f /* 139 */ "No
bb5c0 74 55 73 65 64 5f 31 33 39 22 2c 0a 20 20 20 20 tUsed_139",.
bb5d0 20 2f 2a 20 31 34 30 20 2a 2f 20 22 4e 6f 74 55 /* 140 */ "NotU
bb5e0 73 65 64 5f 31 34 30 22 2c 0a 20 20 20 20 20 2f sed_140",. /
bb5f0 2a 20 31 34 31 20 2a 2f 20 22 54 6f 54 65 78 74 * 141 */ "ToText
bb600 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 32 20 2a ",. /* 142 *
bb610 2f 20 22 54 6f 42 6c 6f 62 22 2c 0a 20 20 20 20 / "ToBlob",.
bb620 20 2f 2a 20 31 34 33 20 2a 2f 20 22 54 6f 4e 75 /* 143 */ "ToNu
bb630 6d 65 72 69 63 22 2c 0a 20 20 20 20 20 2f 2a 20 meric",. /*
bb640 31 34 34 20 2a 2f 20 22 54 6f 49 6e 74 22 2c 0a 144 */ "ToInt",.
bb650 20 20 20 20 20 2f 2a 20 31 34 35 20 2a 2f 20 22 /* 145 */ "
bb660 54 6f 52 65 61 6c 22 2c 0a 20 20 7d 3b 0a 20 20 ToReal",. };.
bb670 72 65 74 75 72 6e 20 61 7a 4e 61 6d 65 5b 69 5d return azName[i]
bb680 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
bb690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
bb6a0 6f 66 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a of opcodes.c ***
bb6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
bb6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
bb6f0 6e 20 66 69 6c 65 20 6f 73 5f 6f 73 32 2e 63 20 n file os_os2.c
bb700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
bb730 2a 20 32 30 30 36 20 46 65 62 20 31 34 0a 2a 2a * 2006 Feb 14.**
bb740 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
bb750 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
bb760 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
bb770 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
bb780 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
bb790 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
bb7a0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
bb7b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
bb7c0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
bb7d0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
bb7e0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
bb7f0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
bb800 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
bb810 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
bb820 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
bb830 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
bb840 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
bb850 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
bb860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bb8a0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c *.**.** This fil
bb8b0 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 e contains code
bb8c0 74 68 61 74 20 69 73 20 73 70 65 63 69 66 69 63 that is specific
bb8d0 20 74 6f 20 4f 53 2f 32 2e 0a 2a 2f 0a 0a 0a 23 to OS/2..*/...#
bb8e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 if SQLITE_OS_OS2
bb8f0 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 ../*.** A Note A
bb900 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f bout Memory Allo
bb910 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 cation:.**.** Th
bb920 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d is driver uses m
bb930 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 alloc()/free() d
bb940 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 irectly rather t
bb950 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 han going throug
bb960 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d h.** the SQLite-
bb970 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 wrappers sqlite3
bb980 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 _malloc()/sqlite
bb990 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 3_free(). Those
bb9a0 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 wrappers.** are
bb9b0 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 designed for us
bb9c0 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 e on embedded sy
bb9d0 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f stems where memo
bb9e0 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 ry is scarce and
bb9f0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 .** malloc failu
bba00 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 res happen frequ
bba10 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f 65 ently. OS/2 doe
bba20 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 s not typically
bba30 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 run on.** embedd
bba40 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 ed systems, and
bba50 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 when it does the
bba60 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d developers norm
bba70 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 ally have bigger
bba80 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 .** problems to
bba90 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e worry about than
bbaa0 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 running out of
bbab0 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 memory. So ther
bbac0 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f e is not.** a co
bbad0 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f mpelling need to
bbae0 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 use the wrapper
bbaf0 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 s..**.** But the
bbb00 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 re is a good rea
bbb10 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 son to not use t
bbb20 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 he wrappers. If
bbb30 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 we use the.** w
bbb40 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 rappers then we
bbb50 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 will get simulat
bbb60 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c ed malloc() fail
bbb70 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 ures within this
bbb80 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 .** driver. And
bbb90 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c that causes all
bbba0 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 kinds of proble
bbbb0 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 ms for our tests
bbbc0 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 . We.** could e
bbbd0 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f nhance SQLite to
bbbe0 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c deal with simul
bbbf0 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c ated malloc fail
bbc00 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 ures within.** t
bbc10 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 he OS driver, bu
bbc20 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 t the code to de
bbc30 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 al with those fa
bbc40 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a ilure would not.
bbc50 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 20 ** be exercised
bbc60 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 on Linux (which
bbc70 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f does not need to
bbc80 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 malloc() in the
bbc90 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 driver).** and
bbca0 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 so we would have
bbcb0 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 difficulty writ
bbcc0 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 ing coverage tes
bbcd0 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 ts for that.** c
bbce0 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 ode. Better to
bbcf0 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f leave the code o
bbd00 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a ut, we think..**
bbd10 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 .** The point of
bbd20 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e this discussion
bbd30 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 is as follows:
bbd40 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 When creating a
bbd50 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 new.** OS layer
bbd60 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 for an embedded
bbd70 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 system, if you
bbd80 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 use this file as
bbd90 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 an example,.**
bbda0 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 avoid the use of
bbdb0 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 malloc()/free()
bbdc0 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 . Those routine
bbdd0 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 2f s work ok on OS/
bbde0 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 2.** desktops bu
bbdf0 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e t not so well in
bbe00 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
bbe10 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 s..*/../*.** Mac
bbe20 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 ros used to dete
bbe30 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 rmine whether or
bbe40 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 not to use thre
bbe50 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 ads..*/.#if defi
bbe60 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 ned(SQLITE_THREA
bbe70 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 54 45 DSAFE) && SQLITE
bbe80 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 _THREADSAFE.# de
bbe90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 5f fine SQLITE_OS2_
bbea0 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 THREADS 1.#endif
bbeb0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 ../*.** Include
bbec0 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d code that is com
bbed0 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e mon to all os_*.
bbee0 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a c files.*/./****
bbef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 ********** Inclu
bbf00 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 de os_common.h i
bbf10 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 n the middle of
bbf20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a os_os2.c *******
bbf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
bbf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
bbf50 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e file os_common.
bbf60 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
bbf70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bbf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
bbf90 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2004 May 22.**.
bbfa0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 ** The author di
bbfb0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 sclaims copyrigh
bbfc0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 t to this source
bbfd0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 code. In place
bbfe0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e of.** a legal n
bbff0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 otice, here is a
bc000 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a blessing:.**.**
bc010 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 May you do g
bc020 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c ood and not evil
bc030 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
bc040 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 find forgiveness
bc050 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e for yourself an
bc060 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 d forgive others
bc070 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 ..** May you
bc080 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 share freely, ne
bc090 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 ver taking more
bc0a0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a than you give..*
bc0b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *.**************
bc0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc100 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
bc110 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 contains macros
bc120 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 and a little bi
bc130 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 t of code that i
bc140 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 s common to.** a
bc150 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f ll of the platfo
bc160 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 rm-specific file
bc170 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 s (os_*.c) and i
bc180 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f s #included into
bc190 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e those.** files.
bc1a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
bc1b0 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c should be #incl
bc1c0 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a uded by the os_*
bc1d0 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 .c files only.
bc1e0 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 It is not a.** g
bc1f0 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 eneral purpose h
bc200 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 eader file..*/.#
bc210 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f ifndef _OS_COMMO
bc220 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 N_H_.#define _OS
bc230 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a _COMMON_H_../*.*
bc240 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f 20 62 * At least two b
bc250 75 67 73 20 68 61 76 65 20 73 6c 69 70 70 65 64 ugs have slipped
bc260 20 69 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 in because we c
bc270 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 hanged the MEMOR
bc280 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f Y_DEBUG.** macro
bc290 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42 55 47 to SQLITE_DEBUG
bc2a0 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 and some older
bc2b0 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65 20 6e makefiles have n
bc2c0 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68 65 0a ot yet made the.
bc2d0 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68 65 20 ** switch. The
bc2e0 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 following code s
bc2f0 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68 69 73 hould catch this
bc300 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 problem at comp
bc310 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 ile-time..*/.#if
bc320 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 def MEMORY_DEBUG
bc330 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20 4d 45 .# error "The ME
bc340 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63 72 6f MORY_DEBUG macro
bc350 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 is obsolete. U
bc360 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 se SQLITE_DEBUG
bc370 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 instead.".#endif
bc380 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
bc390 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 DEBUG.SQLITE_PRI
bc3a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
bc3b0 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 OSTrace = 0;.#de
bc3c0 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 fine OSTRACE1(X)
bc3d0 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
bc3e0 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
bc3f0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bc400 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 (X).#define OSTR
bc410 41 43 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 ACE2(X,Y)
bc420 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 if( sqlite3OSTra
bc430 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 ce ) sqlite3Debu
bc440 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 gPrintf(X,Y).#de
bc450 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c fine OSTRACE3(X,
bc460 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c Y,Z) if( sql
bc470 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 ite3OSTrace ) sq
bc480 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
bc490 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 (X,Y,Z).#define
bc4a0 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 OSTRACE4(X,Y,Z,A
bc4b0 29 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f ) if( sqlite3O
bc4c0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
bc4d0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
bc4e0 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 Z,A).#define OST
bc4f0 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 RACE5(X,Y,Z,A,B)
bc500 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 if( sqlite3OSTr
bc510 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 ace ) sqlite3Deb
bc520 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
bc530 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
bc540 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
bc550 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 ) \. if(sqlit
bc560 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 e3OSTrace) sqlit
bc570 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
bc580 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 Y,Z,A,B,C).#defi
bc590 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c ne OSTRACE7(X,Y,
bc5a0 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 Z,A,B,C,D) \.
bc5b0 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 if(sqlite3OSTra
bc5c0 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 ce) sqlite3Debug
bc5d0 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 Printf(X,Y,Z,A,B
bc5e0 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 ,C,D).#else.#def
bc5f0 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a ine OSTRACE1(X).
bc600 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 #define OSTRACE2
bc610 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 (X,Y).#define OS
bc620 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 TRACE3(X,Y,Z).#d
bc630 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
bc640 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 ,Y,Z,A).#define
bc650 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 OSTRACE5(X,Y,Z,A
bc660 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,B).#define OSTR
bc670 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 ACE6(X,Y,Z,A,B,C
bc680 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 ).#define OSTRAC
bc690 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 E7(X,Y,Z,A,B,C,D
bc6a0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
bc6b0 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f Macros for perfo
bc6c0 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 rmance tracing.
bc6d0 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 Normally turned
bc6e0 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b off. Only work
bc6f0 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 s.** on i486 har
bc700 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 dware..*/.#ifdef
bc710 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 SQLITE_PERFORMA
bc720 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a NCE_TRACE../* .*
bc730 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 * hwtime.h conta
bc740 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d ins inline assem
bc750 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d bler code for im
bc760 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 plementing .** h
bc770 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 igh-performance
bc780 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e timing routines.
bc790 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
bc7a0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 *** Include hwti
bc7b0 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 me.h in the midd
bc7c0 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e le of os_common.
bc7d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
bc7e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
bc7f0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 *** Begin file h
bc800 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a wtime.h ********
bc810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc830 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d **/./*.** 2008 M
bc840 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ay 27.**.** The
bc850 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 author disclaims
bc860 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 copyright to th
bc870 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 is source code.
bc880 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 In place of.**
bc890 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 a legal notice,
bc8a0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 here is a blessi
bc8b0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 ng:.**.** May
bc8c0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 you do good and
bc8d0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 not evil..**
bc8e0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f May you find fo
bc8f0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f rgiveness for yo
bc900 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 urself and forgi
bc910 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 ve others..**
bc920 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 May you share f
bc930 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b reely, never tak
bc940 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f ing more than yo
bc950 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a u give..**.*****
bc960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bc9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
bc9b0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
bc9c0 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f ns inline asm co
bc9d0 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e de for retrievin
bc9e0 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 g "high-performa
bc9f0 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 nce".** counters
bca00 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 for x86 class C
bca10 50 55 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 PUs..*/.#ifndef
bca20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 _HWTIME_H_.#defi
bca30 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f ne _HWTIME_H_../
bca40 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
bca50 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 ng routine only
bca60 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d works on pentium
bca70 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 -class (or newer
bca80 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a ) processors..**
bca90 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 It uses the RDT
bcaa0 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 SC opcode to rea
bcab0 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e d the cycle coun
bcac0 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 t value out of t
bcad0 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 he.** processor
bcae0 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 and returns that
bcaf0 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 value. This ca
bcb00 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 n be used for hi
bcb10 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c gh-res.** profil
bcb20 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 ing..*/.#if (def
bcb30 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c ined(__GNUC__) |
bcb40 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 | defined(_MSC_V
bcb50 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 ER)) && \.
bcb60 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c (defined(i386) |
bcb70 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 | defined(__i386
bcb80 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f __) || defined(_
bcb90 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 M_IX86)).. #if
bcba0 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
bcbb0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
bcbc0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
bcbd0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
bcbe0 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 ){. unsigned
bcbf0 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 int lo, hi;.
bcc00 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
bcc10 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 tile__ ("rdtsc"
bcc20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 : "=a" (lo), "=d
bcc30 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 " (hi));. re
bcc40 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e turn (sqlite_uin
bcc50 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c t64)hi << 32 | l
bcc60 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 o;. }.. #elif
bcc70 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 defined(_MSC_VER
bcc80 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 ).. __declspec(
bcc90 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 naked) __inline
bcca0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f sqlite_uint64 __
bccb0 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 cdecl sqlite3Hwt
bccc0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 ime(void){.
bccd0 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 __asm {.
bcce0 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 rdtsc. re
bccf0 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e t ; return
bcd00 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 value at EDX:EA
bcd10 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 X. }. }..
bcd20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 #endif..#elif (d
bcd30 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 efined(__GNUC__)
bcd40 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 && defined(__x8
bcd50 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6_64__)).. __in
bcd60 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 line__ sqlite_ui
bcd70 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 nt64 sqlite3Hwti
bcd80 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 me(void){.
bcd90 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 unsigned long va
bcda0 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f l;. __asm__
bcdb0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
bcdc0 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 rdtsc" : "=A" (v
bcdd0 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 al));. retu
bcde0 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 rn val;. }. .#e
bcdf0 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 lif (defined(__G
bce00 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 NUC__) && define
bce10 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f d(__ppc__)).. _
bce20 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 _inline__ sqlite
bce30 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
bce40 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 wtime(void){.
bce50 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 unsigned long
bce60 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 long retval;.
bce70 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e unsigned lon
bce80 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f g junk;. __
bce90 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
bcea0 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 __ ("\n\.
bceb0 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 1: mftbu
bcec0 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %1\n\.
bced0 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 mftb
bcee0 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 %L0\n\.
bcef0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 mftb
bcf00 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 u %0\n\.
bcf10 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 cmpw
bcf20 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 %0,%1\n\.
bcf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 b
bcf40 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 ne 1b".
bcf50 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 : "
bcf60 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d =r" (retval), "=
bcf70 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 r" (junk));.
bcf80 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b return retval;
bcf90 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 . }..#else.. #
bcfa0 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 error Need imple
bcfb0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c mentation of sql
bcfc0 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 ite3Hwtime() for
bcfd0 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a your platform..
bcfe0 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f . /*. ** To co
bcff0 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d mpile without im
bd000 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 plementing sqlit
bd010 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 e3Hwtime() for y
bd020 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 our platform,.
bd030 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 ** you can remov
bd040 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 e the above #err
bd050 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 or and use the f
bd060 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 ollowing. ** st
bd070 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f ub function. Yo
bd080 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 u will lose timi
bd090 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d ng support for m
bd0a0 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 any. ** of the
bd0b0 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 debugging and te
bd0c0 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c sting utilities,
bd0d0 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 but it should a
bd0e0 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d t. ** least com
bd0f0 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 pile and run..
bd100 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
bd110 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 E sqlite_uint6
bd120 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 4 sqlite3Hwtime(
bd130 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 void){ return ((
bd140 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 sqlite_uint64)0)
bd150 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e ; }..#endif..#en
bd160 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 dif /* !defined(
bd170 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a _HWTIME_H_) */..
bd180 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bd190 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 End of hwtime.h
bd1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bd1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bd1d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
bd1e0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 Continuing where
bd1f0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 we left off in
bd200 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a os_common.h ****
bd210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
bd220 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 .static sqlite_u
bd230 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 int64 g_start;.s
bd240 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e tatic sqlite_uin
bd250 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 t64 g_elapsed;.#
bd260 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 define TIMER_STA
bd270 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 RT g_start
bd280 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 =sqlite3Hwtime()
bd290 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
bd2a0 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 ND g_ela
bd2b0 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 psed=sqlite3Hwti
bd2c0 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 me()-g_start.#de
bd2d0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 fine TIMER_ELAPS
bd2e0 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 ED g_elapsed
bd2f0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 .#else.#define T
bd300 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 IMER_START.#defi
bd310 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 ne TIMER_END.#de
bd320 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 fine TIMER_ELAPS
bd330 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f ED ((sqlite_
bd340 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 uint64)0).#endif
bd350 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f ../*.** If we co
bd360 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 mpile with the S
bd370 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f QLITE_TEST macro
bd380 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 set, then the f
bd390 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a ollowing block.*
bd3a0 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 * of code will g
bd3b0 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 ive us the abili
bd3c0 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 ty to simulate a
bd3d0 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e disk I/O error.
bd3e0 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 This.** is use
bd3f0 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 d for testing th
bd400 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c e I/O recovery l
bd410 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 ogic..*/.#ifdef
bd420 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
bd430 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
bd440 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 e3_io_error_hit
bd450 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
bd460 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 /* Total number
bd470 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f of I/O Errors */
bd480 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
bd490 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
bd4a0 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 _hardhit = 0;
bd4b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
bd4c0 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 f non-benign err
bd4d0 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ors */.SQLITE_AP
bd4e0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
bd4f0 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d _error_pending =
bd500 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 0; /* Co
bd510 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 unt down to firs
bd520 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 t I/O error */.S
bd530 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
bd540 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
bd550 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 ersist = 0;
bd560 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f /* True if I/
bd570 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 O errors persist
bd580 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 */.SQLITE_API i
bd590 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 nt sqlite3_io_er
bd5a0 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 ror_benign = 0;
bd5b0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
bd5c0 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 if errors are be
bd5d0 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 nign */.SQLITE_A
bd5e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 PI int sqlite3_d
bd5f0 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 iskfull_pending
bd600 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 = 0;.SQLITE_API
bd610 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b int sqlite3_disk
bd620 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e full = 0;.#defin
bd630 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f e SimulateIOErro
bd640 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 rBenign(X) sqlit
bd650 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 e3_io_error_beni
bd660 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 gn=(X).#define S
bd670 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 imulateIOError(C
bd680 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 ODE) \. if( (s
bd690 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f qlite3_io_error_
bd6a0 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 persist && sqlit
bd6b0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 e3_io_error_hit)
bd6c0 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c \. || sql
bd6d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 ite3_io_error_pe
bd6e0 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 nding-- == 1 )
bd6f0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \.
bd700 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b { local_ioerr();
bd710 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 CODE; }.static
bd720 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 void local_ioerr
bd730 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 (){. IOTRACE(("
bd740 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 IOERR\n"));. sq
bd750 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 lite3_io_error_h
bd760 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c it++;. if( !sql
bd770 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 ite3_io_error_be
bd780 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 nign ) sqlite3_i
bd790 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b o_error_hardhit+
bd7a0 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d +;.}.#define Sim
bd7b0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 ulateDiskfullErr
bd7c0 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 or(CODE) \. if
bd7d0 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 ( sqlite3_diskfu
bd7e0 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a ll_pending ){ \.
bd7f0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
bd800 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e _diskfull_pendin
bd810 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 g == 1 ){ \.
bd820 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 local_ioerr()
bd830 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 ; \. sqlit
bd840 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b e3_diskfull = 1;
bd850 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
bd860 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 3_io_error_hit =
bd870 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 1; \. COD
bd880 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b E; \. }else{
bd890 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 \. sqlite
bd8a0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 3_diskfull_pendi
bd8b0 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c ng--; \. } \
bd8c0 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 . }.#else.#def
bd8d0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ine SimulateIOEr
bd8e0 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 rorBenign(X).#de
bd8f0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
bd900 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 rror(A).#define
bd910 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c SimulateDiskfull
bd920 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a Error(A).#endif.
bd930 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 ./*.** When test
bd940 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e ing, keep a coun
bd950 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 t of the number
bd960 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a of open files..*
bd970 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
bd980 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
bd990 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e int sqlite3_open
bd9a0 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b _file_count = 0;
bd9b0 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 .#define OpenCou
bd9c0 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 nter(X) sqlite3
bd9d0 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 _open_file_count
bd9e0 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 +=(X).#else.#def
bd9f0 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 ine OpenCounter(
bda00 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 X).#endif..#endi
bda10 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f f /* !defined(_O
bda20 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a S_COMMON_H_) */.
bda30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bda40 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f End of os_commo
bda50 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.h ************
bda60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bda70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bda80 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
bda90 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
bdaa0 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
bdab0 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a os_os2.c ******
bdac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
bdad0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 ../*.** The os2F
bdae0 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 ile structure is
bdaf0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c subclass of sql
bdb00 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 ite3_file specif
bdb10 69 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a ic for the OS/2.
bdb20 2a 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c ** protability l
bdb30 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 ayer..*/.typedef
bdb40 20 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 struct os2File
bdb50 6f 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 os2File;.struct
bdb60 6f 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 os2File {. cons
bdb70 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
bdb80 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 hods *pMethod;
bdb90 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 /* Always the fi
bdba0 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 rst entry */. H
bdbb0 46 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 FILE h;
bdbc0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
bdbd0 6c 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 le for accessing
bdbe0 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 the file */. c
bdbf0 68 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 har* pathToDel;
bdc00 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
bdc10 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 of file to dele
bdc20 74 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c te on close, NUL
bdc30 4c 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e L if not */. un
bdc40 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b signed char lock
bdc50 74 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 type; /* Type
bdc60 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c of lock currentl
bdc70 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 y held on this f
bdc80 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 ile */.};..#defi
bdc90 6e 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 ne LOCK_TIMEOUT
bdca0 31 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75 10L /* the defau
bdcb0 6c 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f lt locking timeo
bdcc0 75 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ut */../********
bdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd10 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 *****.** The nex
bdd20 74 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 t group of routi
bdd30 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 nes implement th
bdd40 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 e I/O methods sp
bdd50 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 ecified.** by th
bdd60 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 e sqlite3_io_met
bdd70 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a hods object..***
bdd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
bddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a ***********/../*
bddd0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 .** Close a file
bdde0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
bddf0 6f 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 os2Close( sqlite
bde00 33 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 3_file *id ){.
bde10 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
bde20 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 RROR;. os2File
bde30 2a 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 *pFile;. if( id
bde40 20 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 && (pFile = (os
bde50 32 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 2File*)id) != 0
bde60 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 ){. OSTRACE2(
bde70 20 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 "CLOSE %d\n", p
bde80 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 File->h );. r
bde90 63 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 c = DosClose( pF
bdea0 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 ile->h );. pF
bdeb0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
bdec0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 NO_LOCK;. if(
bded0 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
bdee0 6c 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 l != NULL ){.
bdef0 20 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 rc = DosForce
bdf00 44 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 Delete( (PSZ)pFi
bdf10 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b le->pathToDel );
bdf20 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 . free( pFi
bdf30 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b le->pathToDel );
bdf40 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 . pFile->pa
bdf50 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a thToDel = NULL;.
bdf60 20 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 }. id = 0
bdf70 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 ;. OpenCounte
bdf80 72 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 r( -1 );. }..
bdf90 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f return rc == NO_
bdfa0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f ERROR ? SQLITE_O
bdfb0 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 K : SQLITE_IOERR
bdfc0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 ;.}../*.** Read
bdfd0 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 data from a file
bdfe0 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 into a buffer.
bdff0 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
be000 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 K if all.** byte
be010 73 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 s were read succ
be020 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c essfully and SQL
be030 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 ITE_IOERR if any
be040 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 thing goes.** wr
be050 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ong..*/.static i
be060 6e 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 nt os2Read(. sq
be070 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 lite3_file *id,
be080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
be090 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 File to read fr
be0a0 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 om */. void *pB
be0b0 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 uf,
be0c0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 /* Write
be0d0 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 content into th
be0e0 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 is buffer */. i
be0f0 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 nt amt,
be100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
be110 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
be120 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 s to read */. s
be130 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 qlite3_int64 off
be140 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f set /
be150 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 * Begin reading
be160 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a at this offset *
be170 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c /.){. ULONG fil
be180 65 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a eLocation = 0L;.
be190 20 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f ULONG got;. o
be1a0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 s2File *pFile =
be1b0 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 (os2File*)id;.
be1c0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b assert( id!=0 );
be1d0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
be1e0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 or( return SQLIT
be1f0 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a E_IOERR_READ );.
be200 20 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 OSTRACE3( "REA
be210 44 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c D %d lock=%d\n",
be220 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 pFile->h, pFile
be230 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 ->locktype );.
be240 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 if( DosSetFilePt
be250 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 r(pFile->h, offs
be260 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 et, FILE_BEGIN,
be270 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 &fileLocation) !
be280 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 = NO_ERROR ){.
be290 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
be2a0 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 IOERR;. }. if(
be2b0 20 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d DosRead( pFile-
be2c0 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 >h, pBuf, amt, &
be2d0 67 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f got ) != NO_ERRO
be2e0 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 R ){. return
be2f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 SQLITE_IOERR_REA
be300 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 D;. }. if( got
be310 20 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 == (ULONG)amt )
be320 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
be330 54 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a TE_OK;. else {.
be340 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f /* Unread po
be350 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e rtions of the in
be360 70 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20 put buffer must
be370 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a be zero-filled *
be380 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 /. memset(&((
be390 63 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d char*)pBuf)[got]
be3a0 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 , 0, amt-got);.
be3b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
be3c0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 _IOERR_SHORT_REA
be3d0 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 D;. }.}../*.**
be3e0 57 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 Write data from
be3f0 61 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 a buffer into a
be400 66 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 file. Return SQ
be410 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
be420 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 ss.** or some ot
be430 68 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f her error code o
be440 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 n failure..*/.st
be450 61 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 atic int os2Writ
be460 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c e(. sqlite3_fil
be470 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 e *id,
be480 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 /* File to
be490 77 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 write into */.
be4a0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 const void *pBuf
be4b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
be4c0 2f 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 /* The bytes to
be4d0 62 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 be written */.
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 77 72 69 74 65 20 2a 2f 0a 20 es to write */.
be520 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f sqlite3_int64 o
be530 66 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 ffset
be540 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 /* Offset into
be550 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 the file to begi
be560 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a n writing at */.
be570 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c ){. ULONG fileL
be580 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 ocation = 0L;.
be590 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 APIRET rc = NO_E
be5a0 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 RROR;. ULONG wr
be5b0 6f 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a ote;. os2File *
be5c0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
be5d0 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 *)id;. assert(
be5e0 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c id!=0 );. Simul
be5f0 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
be600 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
be610 57 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c WRITE );. Simul
be620 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 ateDiskfullError
be630 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
be640 46 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 FULL );. OSTRAC
be650 45 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f E3( "WRITE %d lo
be660 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
be670 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
be680 79 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 ype );. if( Dos
be690 53 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 SetFilePtr(pFile
be6a0 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c ->h, offset, FIL
be6b0 45 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f E_BEGIN, &fileLo
be6c0 63 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 cation) != NO_ER
be6d0 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ROR ){. retur
be6e0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a n SQLITE_IOERR;.
be6f0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d }. assert( am
be700 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 t>0 );. while(
be710 61 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20 amt > 0 &&.
be720 20 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72 ( rc = DosWr
be730 69 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 ite( pFile->h, (
be740 50 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c PVOID)pBuf, amt,
be750 20 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e &wrote ) ) == N
be760 4f 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20 O_ERROR &&.
be770 20 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 wrote > 0.
be780 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 ){. amt -= wr
be790 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 ote;. pBuf =
be7a0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 &((char*)pBuf)[w
be7b0 72 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 rote];. }.. re
be7c0 74 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f turn ( rc != NO_
be7d0 45 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 ERROR || amt > (
be7e0 69 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51 int)wrote ) ? SQ
be7f0 4c 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 LITE_FULL : SQLI
be800 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
be810 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e Truncate an open
be820 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 file to a speci
be830 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 fied size.*/.sta
be840 74 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 tic int os2Trunc
be850 61 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c ate( sqlite3_fil
be860 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 e *id, i64 nByte
be870 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 ){. APIRET rc
be880 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 = NO_ERROR;. os
be890 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
be8a0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f os2File*)id;. O
be8b0 53 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 STRACE3( "TRUNCA
be8c0 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 TE %d %lld\n", p
be8d0 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 File->h, nByte )
be8e0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 ;. SimulateIOEr
be8f0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
be900 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 TE_IOERR_TRUNCAT
be910 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 E );. rc = DosS
be920 65 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c etFileSize( pFil
be930 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 e->h, nByte );.
be940 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f return rc == NO
be950 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f _ERROR ? SQLITE_
be960 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 OK : SQLITE_IOER
be970 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 R_TRUNCATE;.}..#
be980 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
be990 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 T./*.** Count th
be9a0 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c e number of full
be9b0 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c syncs and normal
be9c0 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 syncs. This is
be9d0 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a used to test.**
be9e0 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 that syncs and
be9f0 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 fullsyncs are oc
bea00 63 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 curing at the ri
bea10 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 ght times..*/.SQ
bea20 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
bea30 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 ite3_sync_count
bea40 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 = 0;.SQLITE_API
bea50 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c int sqlite3_full
bea60 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a sync_count = 0;.
bea70 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 #endif../*.** Ma
bea80 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 ke sure all writ
bea90 65 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c es to a particul
beaa0 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d ar file are comm
beab0 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a itted to disk..*
beac0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
bead0 53 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 Sync( sqlite3_fi
beae0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 le *id, int flag
beaf0 73 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a s ){. os2File *
beb00 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
beb10 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 *)id;. OSTRACE3
beb20 28 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d ( "SYNC %d lock=
beb30 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
beb40 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
beb50 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );.#ifdef SQLIT
beb60 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 E_TEST. if( fla
beb70 67 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 gs & SQLITE_SYNC
beb80 5f 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 _FULL){. sqli
beb90 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
beba0 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 nt++;. }. sqli
bebb0 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b te3_sync_count++
bebc0 3b 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 ;.#endif. /* If
bebd0 20 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 we compiled wit
bebe0 68 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f h the SQLITE_NO_
bebf0 53 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 SYNC flag, then
bec00 73 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a syncing is a. *
bec10 2a 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 * no-op. */.#if
bec20 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 def SQLITE_NO_SY
bec30 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 NC. UNUSED_PARA
bec40 4d 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 METER(pFile);.
bec50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
bec60 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e ;.#else. return
bec70 20 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 DosResetBuffer(
bec80 20 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e pFile->h ) == N
bec90 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 O_ERROR ? SQLITE
beca0 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 _OK : SQLITE_IOE
becb0 52 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a RR;.#endif.}../*
becc0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 .** Determine th
becd0 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f e current size o
bece0 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 f a file in byte
becf0 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 s.*/.static int
bed00 6f 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c os2FileSize( sql
bed10 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 ite3_file *id, s
bed20 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 qlite3_int64 *pS
bed30 69 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 ize ){. APIRET
bed40 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 rc = NO_ERROR;.
bed50 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 FILESTATUS3 fst
bed60 73 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 s3FileInfo;. me
bed70 6d 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49 mset(&fsts3FileI
bed80 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 nfo, 0, sizeof(f
bed90 73 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a sts3FileInfo));.
beda0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
bedb0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
bedc0 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
bedd0 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 ITE_IOERR_FSTAT
bede0 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 );. rc = DosQue
bedf0 72 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 ryFileInfo( ((os
bee00 32 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 2File*)id)->h, F
bee10 49 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 IL_STANDARD, &fs
bee20 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a ts3FileInfo, siz
bee30 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 eof(FILESTATUS3)
bee40 20 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 );. if( rc ==
bee50 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
bee60 2a 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69 *pSize = fsts3Fi
bee70 6c 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 leInfo.cbFile;.
bee80 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
bee90 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
beea0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
beeb0 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d IOERR_FSTAT;. }
beec0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 .}../*.** Acquir
beed0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e e a reader lock.
beee0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
beef0 65 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 etReadLock( os2F
bef00 69 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 ile *pFile ){.
bef10 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 FILELOCK LockAr
bef20 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ea,.
bef30 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 UnlockArea;. AP
bef40 49 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 IRET res;. mems
bef50 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c et(&LockArea, 0,
bef60 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 sizeof(LockArea
bef70 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e ));. memset(&Un
bef80 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a lockArea, 0, siz
bef90 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 eof(UnlockArea))
befa0 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 ;. LockArea.lOf
befb0 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 fset = SHARED_FI
befc0 52 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e RST;. LockArea.
befd0 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f lRange = SHARED_
befe0 53 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 SIZE;. UnlockAr
beff0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b ea.lOffset = 0L;
bf000 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 . UnlockArea.lR
bf010 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 ange = 0L;. res
bf020 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 = DosSetFileLoc
bf030 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 ks( pFile->h, &U
bf040 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b nlockArea, &Lock
bf050 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f Area, LOCK_TIMEO
bf060 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 UT, 1L );. OSTR
bf070 41 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f ACE3( "GETREADLO
bf080 43 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c CK %d res=%d\n",
bf090 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 pFile->h, res )
bf0a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a ;. return res;.
bf0b0 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 }../*.** Undo a
bf0c0 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 readlock.*/.stat
bf0d0 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 ic int unlockRea
bf0e0 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a dLock( os2File *
bf0f0 69 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b id ){. FILELOCK
bf100 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 LockArea,.
bf110 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 UnlockAr
bf120 65 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 ea;. APIRET res
bf130 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b ;. memset(&Lock
bf140 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
bf150 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 LockArea));. me
bf160 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
bf170 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
bf180 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b ckArea));. Lock
bf190 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 Area.lOffset = 0
bf1a0 4c 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 L;. LockArea.lR
bf1b0 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c ange = 0L;. Unl
bf1c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
bf1d0 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a = SHARED_FIRST;.
bf1e0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
bf1f0 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a nge = SHARED_SIZ
bf200 45 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 E;. res = DosSe
bf210 74 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e tFileLocks( id->
bf220 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 h, &UnlockArea,
bf230 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f &LockArea, LOCK_
bf240 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 TIMEOUT, 1L );.
bf250 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f OSTRACE3( "UNLO
bf260 43 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 CK-READLOCK file
bf270 20 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 handle=%d res=%
bf280 64 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 d?\n", id->h, re
bf290 73 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 s );. return re
bf2a0 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b s;.}../*.** Lock
bf2b0 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 the file with t
bf2c0 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 he lock specifie
bf2d0 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c d by parameter l
bf2e0 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a ocktype - one.**
bf2f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
bf300 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 g:.**.** (1)
bf310 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 SHARED_LOCK.**
bf320 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 (2) RESERVED
bf330 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 _LOCK.** (3)
bf340 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a PENDING_LOCK.**
bf350 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 (4) EXCLUSI
bf360 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f VE_LOCK.**.** So
bf370 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 metimes when req
bf380 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b uesting one lock
bf390 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e state, addition
bf3a0 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a al lock states.*
bf3b0 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 * are inserted i
bf3c0 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 n between. The
bf3d0 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 locking might fa
bf3e0 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 il on one of the
bf3f0 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 later.** transi
bf400 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 tions leaving th
bf410 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 e lock state dif
bf420 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 ferent from what
bf430 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a it started but.
bf440 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f ** still short o
bf450 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 f its goal. The
bf460 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 following chart
bf470 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 shows the allow
bf480 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e ed.** transition
bf490 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 s and the insert
bf4a0 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 ed intermediate
bf4b0 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 states:.**.**
bf4c0 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 UNLOCKED -> SHA
bf4d0 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 RED.** SHARED
bf4e0 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 -> RESERVED.**
bf4f0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 SHARED -> (PE
bf500 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
bf510 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 IVE.** RESERV
bf520 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 ED -> (PENDING)
bf530 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 -> EXCLUSIVE.**
bf540 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 PENDING -> EX
bf550 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 CLUSIVE.**.** Th
bf560 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 is routine will
bf570 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 only increase a
bf580 6c 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e lock. The os2Un
bf590 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a lock() routine.*
bf5a0 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 * erases all loc
bf5b0 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 ks at once and r
bf5c0 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 eturns us immedi
bf5d0 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 ately to locking
bf5e0 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 level 0..** It
bf5f0 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 is not possible
bf600 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 to lower the loc
bf610 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 king level one s
bf620 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 tep at a time.
bf630 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 You.** must go s
bf640 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 traight to locki
bf650 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 ng level 0..*/.s
bf660 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 tatic int os2Loc
bf670 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 k( sqlite3_file
bf680 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
bf690 65 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 e ){. int rc =
bf6a0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
bf6b0 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
bf6c0 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 from subroutines
bf6d0 20 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 */. APIRET res
bf6e0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 = NO_ERROR;
bf6f0 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 /* Result of an
bf700 4f 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a OS/2 lock call *
bf710 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 /. int newLockt
bf720 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 ype; /* Se
bf730 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 t pFile->locktyp
bf740 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 e to this value
bf750 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a before exiting *
bf760 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 /. int gotPendi
bf770 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 ngLock = 0;/* Tr
bf780 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 ue if we acquire
bf790 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b d a PENDING lock
bf7a0 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 this time */.
bf7b0 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 FILELOCK LockAr
bf7c0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ea,.
bf7d0 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 UnlockArea;. os
bf7e0 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 2File *pFile = (
bf7f0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d os2File*)id;. m
bf800 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c emset(&LockArea,
bf810 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 0, sizeof(LockA
bf820 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 rea));. memset(
bf830 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 &UnlockArea, 0,
bf840 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 sizeof(UnlockAre
bf850 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 a));. assert( p
bf860 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 File!=0 );. OST
bf870 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 RACE4( "LOCK %d
bf880 25 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 %d was %d\n", pF
bf890 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 ile->h, locktype
bf8a0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
bf8b0 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 e );.. /* If th
bf8c0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 ere is already a
bf8d0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 lock of this ty
bf8e0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 pe or more restr
bf8f0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 ictive on the.
bf900 2a 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e ** os2File, do n
bf910 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 othing. Don't us
bf920 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 e the end_lock:
bf930 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 exit path, as.
bf940 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 ** sqlite3_mutex
bf950 5f 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 _enter() hasn't
bf960 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e been called yet.
bf970 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c . */. if( pFil
bf980 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 e->locktype>=loc
bf990 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 ktype ){. OST
bf9a0 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 RACE3( "LOCK %d
bf9b0 25 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 %d ok (already h
bf9c0 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e eld)\n", pFile->
bf9d0 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 h, locktype );.
bf9e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
bf9f0 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
bfa00 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
bfa10 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
bfa20 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
bfa30 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
bfa40 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
bfa50 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
bfa60 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
bfa70 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
bfa80 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
bfa90 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
bfaa0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
bfab0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
bfac0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
bfad0 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b CK );.. /* Lock
bfae0 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 the PENDING_LOC
bfaf0 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 K byte if we nee
bfb00 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 d to acquire a P
bfb10 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 ENDING lock or.
bfb20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ** a SHARED loc
bfb30 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 k. If we are ac
bfb40 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 quiring a SHARED
bfb50 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 lock, the acqui
bfb60 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 sition of. ** t
bfb70 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 he PENDING_LOCK
bfb80 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 byte is temporar
bfb90 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 y.. */. newLoc
bfba0 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c ktype = pFile->l
bfbb0 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 ocktype;. if( p
bfbc0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d File->locktype==
bfbd0 4e 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c NO_LOCK. ||
bfbe0 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c (locktype==EXCL
bfbf0 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 USIVE_LOCK && pF
bfc00 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 ile->locktype==R
bfc10 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 ESERVED_LOCK).
bfc20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e ){. LockArea.
bfc30 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e lOffset = PENDIN
bfc40 47 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b G_BYTE;. Lock
bfc50 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c Area.lRange = 1L
bfc60 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 ;. UnlockArea
bfc70 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 .lOffset = 0L;.
bfc80 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 UnlockArea.lR
bfc90 61 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 ange = 0L;..
bfca0 2f 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 /* wait longer t
bfcb0 68 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 han LOCK_TIMEOUT
bfcc0 20 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 here not to hav
bfcd0 65 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c e to try multipl
bfce0 65 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 e times */. r
bfcf0 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c es = DosSetFileL
bfd00 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
bfd10 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
bfd20 63 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c ckArea, 100L, 0L
bfd30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 );. if( res
bfd40 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
bfd50 20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c gotPendingL
bfd60 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f ock = 1;. O
bfd70 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
bfd80 64 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 d pending lock b
bfd90 6f 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 oolean set. res
bfda0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 =%d\n", pFile->h
bfdb0 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 , res );. }.
bfdc0 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 }.. /* Acquire
bfdd0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 a shared lock.
bfde0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 */. if( lockty
bfdf0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe==SHARED_LOCK
bfe00 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 && res == NO_ERR
bfe10 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 OR ){. assert
bfe20 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
bfe30 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 e==NO_LOCK );.
bfe40 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c res = getReadL
bfe50 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
bfe60 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 if( res == NO_ER
bfe70 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 ROR ){. new
bfe80 4c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 Locktype = SHARE
bfe90 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 D_LOCK;. }.
bfea0 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 OSTRACE3( "LOC
bfeb0 4b 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61 K %d acquire sha
bfec0 72 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 red lock. res=%d
bfed0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
bfee0 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 es );. }.. /*
bfef0 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 Acquire a RESERV
bff00 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 ED lock. */. i
bff10 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 f( locktype==RES
bff20 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 ERVED_LOCK && re
bff30 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b s == NO_ERROR ){
bff40 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
bff50 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 le->locktype==SH
bff60 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 ARED_LOCK );.
bff70 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
bff80 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
bff90 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e E;. LockArea.
bffa0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 lRange = 1L;.
bffb0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
bffc0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
bffd0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
bffe0 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 0L;. res =
bfff0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c0000 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c0010 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c0020 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c0030 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 0L );. if( r
c0040 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 es == NO_ERROR )
c0050 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 {. newLockt
c0060 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c ype = RESERVED_L
c0070 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f OCK;. }. O
c0080 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 STRACE3( "LOCK %
c0090 64 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76 d acquire reserv
c00a0 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c ed lock. res=%d\
c00b0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 n", pFile->h, re
c00c0 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 s );. }.. /* A
c00d0 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 cquire a PENDING
c00e0 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
c00f0 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
c0100 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
c0110 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a == NO_ERROR ){.
c0120 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 newLocktype
c0130 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a = PENDING_LOCK;.
c0140 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f gotPendingLo
c0150 63 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 ck = 0;. OSTR
c0160 41 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 ACE2( "LOCK %d a
c0170 63 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c cquire pending l
c0180 6f 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 ock. pending loc
c0190 6b 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e k boolean unset.
c01a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b \n", pFile->h );
c01b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 . }.. /* Acqui
c01c0 72 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 re an EXCLUSIVE
c01d0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
c01e0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 locktype==EXCLUS
c01f0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 IVE_LOCK && res
c0200 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 == NO_ERROR ){.
c0210 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 assert( pFile
c0220 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 ->locktype>=SHAR
c0230 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 ED_LOCK );. r
c0240 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c es = unlockReadL
c0250 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 ock(pFile);.
c0260 4f 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 OSTRACE2( "unrea
c0270 64 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 dlock = %d\n", r
c0280 65 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 es );. LockAr
c0290 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 ea.lOffset = SHA
c02a0 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c RED_FIRST;. L
c02b0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c02c0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 SHARED_SIZE;.
c02d0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 UnlockArea.lOf
c02e0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 fset = 0L;. U
c02f0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c0300 20 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 0L;. res =
c0310 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c0320 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c0330 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c0340 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c0350 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 , 0L );. if(
c0360 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 res == NO_ERROR
c0370 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
c0380 74 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 type = EXCLUSIVE
c0390 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
c03a0 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 {. OSTRACE2
c03b0 28 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f ( "OS/2 error-co
c03c0 64 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 de = %d\n", res
c03d0 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 );. getRead
c03e0 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 Lock(pFile);.
c03f0 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 }. OSTRACE3(
c0400 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 "LOCK %d acquir
c0410 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
c0420 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 . res=%d\n", pF
c0430 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 ile->h, res );.
c0440 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 }.. /* If we a
c0450 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e re holding a PEN
c0460 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f DING lock that o
c0470 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 ught to be relea
c0480 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 sed, then. ** r
c0490 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 elease it now..
c04a0 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e */. if( gotPen
c04b0 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b dingLock && lock
c04c0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
c04d0 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a K ){. int r;.
c04e0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 LockArea.lOf
c04f0 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c fset = 0L;. L
c0500 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d ockArea.lRange =
c0510 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 0L;. UnlockA
c0520 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 rea.lOffset = PE
c0530 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 NDING_BYTE;.
c0540 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 UnlockArea.lRang
c0550 65 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 e = 1L;. r =
c0560 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c0570 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c0580 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c0590 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c05a0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c05b0 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e CE3( "LOCK %d un
c05c0 6c 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f locking pending/
c05d0 69 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c is shared. r=%d\
c05e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 n", pFile->h, r
c05f0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 );. }.. /* Upd
c0600 61 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ate the state of
c0610 20 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 the lock has he
c0620 6c 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 ld in the file d
c0630 65 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 escriptor then.
c0640 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 ** return the a
c0650 70 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c ppropriate resul
c0660 74 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 t code.. */. i
c0670 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 f( res == NO_ERR
c0680 4f 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 OR ){. rc = S
c0690 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
c06a0 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 e{. OSTRACE4(
c06b0 20 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 "LOCK FAILED %d
c06c0 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 trying for %d b
c06d0 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 ut got %d\n", pF
c06e0 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 ile->h,.
c06f0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 locktype,
c0700 6e 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 newLocktype );.
c0710 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
c0720 55 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 USY;. }. pFile
c0730 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 ->locktype = new
c0740 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 Locktype;. OSTR
c0750 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e ACE3( "LOCK %d n
c0760 6f 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ow %d\n", pFile-
c0770 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
c0780 79 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 ype );. return
c0790 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 rc;.}../*.** Thi
c07a0 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 s routine checks
c07b0 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 if there is a R
c07c0 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c ESERVED lock hel
c07d0 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 d on the specifi
c07e0 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 ed.** file by th
c07f0 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 is or any other
c0800 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 process. If such
c0810 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c a lock is held,
c0820 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a return.** non-z
c0830 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a ero, otherwise z
c0840 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ero..*/.static i
c0850 6e 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 nt os2CheckReser
c0860 76 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 vedLock( sqlite3
c0870 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a _file *id, int *
c0880 70 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 pOut ){. int r
c0890 3d 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a = 0;. os2File *
c08a0 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 pFile = (os2File
c08b0 2a 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 *)id;. assert(
c08c0 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 pFile!=0 );. if
c08d0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
c08e0 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b e>=RESERVED_LOCK
c08f0 20 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 ){. r = 1;.
c0900 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
c0910 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 ST WR-LOCK %d %d
c0920 20 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 (local)\n", pFi
c0930 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 le->h, r );. }e
c0940 6c 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 lse{. FILELOC
c0950 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 K LockArea,.
c0960 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 Unloc
c0970 6b 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45 kArea;. APIRE
c0980 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b T rc = NO_ERROR;
c0990 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 . memset(&Loc
c09a0 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 kArea, 0, sizeof
c09b0 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 (LockArea));.
c09c0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 memset(&UnlockA
c09d0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 rea, 0, sizeof(U
c09e0 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 nlockArea));.
c09f0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 LockArea.lOffse
c0a00 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 t = RESERVED_BYT
c0a10 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e E;. LockArea.
c0a20 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 lRange = 1L;.
c0a30 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 UnlockArea.lOff
c0a40 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e set = 0L;. Un
c0a50 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c0a60 3d 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 = 0L;. rc = D
c0a70 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 osSetFileLocks(
c0a80 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 pFile->h, &Unloc
c0a90 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 kArea, &LockArea
c0aa0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 , LOCK_TIMEOUT,
c0ab0 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 0L );. OSTRAC
c0ac0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 E3( "TEST WR-LOC
c0ad0 4b 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 K %d lock reserv
c0ae0 65 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 ed byte rc=%d\n"
c0af0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 , pFile->h, rc )
c0b00 3b 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 ;. if( rc ==
c0b10 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 NO_ERROR ){.
c0b20 20 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e APIRET rcu = N
c0b30 4f 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 O_ERROR; /* retu
c0b40 72 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f rn code for unlo
c0b50 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c cking */. L
c0b60 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 ockArea.lOffset
c0b70 3d 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b = 0L;. Lock
c0b80 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c Area.lRange = 0L
c0b90 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 ;. UnlockAr
c0ba0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 ea.lOffset = RES
c0bb0 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
c0bc0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 UnlockArea.lRa
c0bd0 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 nge = 1L;.
c0be0 72 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 rcu = DosSetFile
c0bf0 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c Locks( pFile->h,
c0c00 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c &UnlockArea, &L
c0c10 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 ockArea, LOCK_TI
c0c20 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 MEOUT, 0L );.
c0c30 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 OSTRACE3( "TE
c0c40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e ST WR-LOCK %d un
c0c50 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 lock reserved by
c0c60 74 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c te r=%d\n", pFil
c0c70 65 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 e->h, rcu );.
c0c80 20 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20 }. r = !(rc
c0c90 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 == NO_ERROR);.
c0ca0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 OSTRACE3( "TES
c0cb0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 T WR-LOCK %d %d
c0cc0 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 (remote)\n", pFi
c0cd0 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a le->h, r );. }.
c0ce0 20 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 *pOut = r;. r
c0cf0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
c0d00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 .}../*.** Lower
c0d10 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 the locking leve
c0d20 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 l on file descri
c0d30 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 ptor id to lockt
c0d40 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a ype. locktype.*
c0d50 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 * must be either
c0d60 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 NO_LOCK or SHAR
c0d70 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 ED_LOCK..**.** I
c0d80 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 f the locking le
c0d90 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 vel of the file
c0da0 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c descriptor is al
c0db0 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f ready at or belo
c0dc0 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 w.** the request
c0dd0 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c ed locking level
c0de0 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 , this routine i
c0df0 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a s a no-op..**.**
c0e00 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 It is not possi
c0e10 62 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 ble for this rou
c0e20 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 tine to fail if
c0e30 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
c0e40 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 ent.** is NO_LOC
c0e50 4b 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e K. If the secon
c0e60 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 d argument is SH
c0e70 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 ARED_LOCK then t
c0e80 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d his routine.** m
c0e90 69 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 ight return SQLI
c0ea0 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 TE_IOERR;.*/.sta
c0eb0 74 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 tic int os2Unloc
c0ec0 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 k( sqlite3_file
c0ed0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 *id, int locktyp
c0ee0 65 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b e ){. int type;
c0ef0 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c . os2File *pFil
c0f00 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 e = (os2File*)id
c0f10 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 ;. APIRET rc =
c0f20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 SQLITE_OK;. API
c0f30 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 RET res = NO_ERR
c0f40 4f 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 OR;. FILELOCK
c0f50 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 LockArea,.
c0f60 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 UnlockArea
c0f70 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b ;. memset(&Lock
c0f80 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Area, 0, sizeof(
c0f90 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 LockArea));. me
c0fa0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 mset(&UnlockArea
c0fb0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f , 0, sizeof(Unlo
c0fc0 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 ckArea));. asse
c0fd0 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a rt( pFile!=0 );.
c0fe0 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 assert( lockty
c0ff0 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 pe<=SHARED_LOCK
c1000 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 );. OSTRACE4( "
c1010 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 UNLOCK %d to %d
c1020 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 was %d\n", pFile
c1030 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 ->h, locktype, p
c1040 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 File->locktype )
c1050 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 ;. type = pFile
c1060 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 ->locktype;. if
c1070 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 ( type>=EXCLUSIV
c1080 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f E_LOCK ){. Lo
c1090 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d ckArea.lOffset =
c10a0 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 0L;. LockAre
c10b0 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 a.lRange = 0L;.
c10c0 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f UnlockArea.lO
c10d0 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 ffset = SHARED_F
c10e0 49 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b IRST;. Unlock
c10f0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 Area.lRange = SH
c1100 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 ARED_SIZE;. r
c1110 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c es = DosSetFileL
c1120 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 ocks( pFile->h,
c1130 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f &UnlockArea, &Lo
c1140 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d ckArea, LOCK_TIM
c1150 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 EOUT, 0L );.
c1160 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 OSTRACE3( "UNLOC
c1170 4b 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c K %d exclusive l
c1180 6f 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 ock res=%d\n", p
c1190 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a File->h, res );.
c11a0 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 if( locktype
c11b0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 ==SHARED_LOCK &&
c11c0 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 getReadLock(pFi
c11d0 6c 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 le) != NO_ERROR
c11e0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 ){. /* This
c11f0 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 should never ha
c1200 70 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 ppen. We should
c1210 20 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 always be able
c1220 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 to. ** reac
c1230 71 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c quire the read l
c1240 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 ock */. OST
c1250 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 RACE3( "UNLOCK %
c1260 64 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c d to %d getReadL
c1270 6f 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c ock() failed\n",
c1280 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 pFile->h, lockt
c1290 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 ype );. rc
c12a0 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 = SQLITE_IOERR_U
c12b0 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d NLOCK;. }. }
c12c0 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 . if( type>=RES
c12d0 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 ERVED_LOCK ){.
c12e0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c12f0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 et = 0L;. Loc
c1300 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c1310 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
c1320 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 a.lOffset = RESE
c1330 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 RVED_BYTE;. U
c1340 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 nlockArea.lRange
c1350 20 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d = 1L;. res =
c1360 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 DosSetFileLocks
c1370 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c ( pFile->h, &Unl
c1380 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 ockArea, &LockAr
c1390 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 ea, LOCK_TIMEOUT
c13a0 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 , 0L );. OSTR
c13b0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 ACE3( "UNLOCK %d
c13c0 20 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64 reserved res=%d
c13d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 \n", pFile->h, r
c13e0 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 es );. }. if(
c13f0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 locktype==NO_LOC
c1400 4b 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 K && type>=SHARE
c1410 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 D_LOCK ){. re
c1420 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f s = unlockReadLo
c1430 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f ck(pFile);. O
c1440 53 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b STRACE5( "UNLOCK
c1450 20 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25 %d is %d want %
c1460 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 d res=%d\n", pFi
c1470 6c 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 le->h, type, loc
c1480 6b 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 ktype, res );.
c1490 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 }. if( type>=PE
c14a0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 NDING_LOCK ){.
c14b0 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 LockArea.lOffs
c14c0 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 et = 0L;. Loc
c14d0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 kArea.lRange = 0
c14e0 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 L;. UnlockAre
c14f0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 a.lOffset = PEND
c1500 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e ING_BYTE;. Un
c1510 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 lockArea.lRange
c1520 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 = 1L;. res =
c1530 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 DosSetFileLocks(
c1540 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f pFile->h, &Unlo
c1550 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 ckArea, &LockAre
c1560 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c a, LOCK_TIMEOUT,
c1570 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 0L );. OSTRA
c1580 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 CE3( "UNLOCK %d
c1590 70 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e pending res=%d\n
c15a0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 ", pFile->h, res
c15b0 20 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d );. }. pFile-
c15c0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b >locktype = lock
c15d0 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 type;. OSTRACE3
c15e0 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 ( "UNLOCK %d now
c15f0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 %d\n", pFile->h
c1600 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 , pFile->locktyp
c1610 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 e );. return rc
c1620 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 ;.}../*.** Contr
c1630 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 ol and query of
c1640 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 the open file ha
c1650 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ndle..*/.static
c1660 69 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 int os2FileContr
c1670 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ol(sqlite3_file
c1680 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 *id, int op, voi
c1690 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 d *pArg){. swit
c16a0 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 ch( op ){. ca
c16b0 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f se SQLITE_FCNTL_
c16c0 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 LOCKSTATE: {.
c16d0 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d *(int*)pArg =
c16e0 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d ((os2File*)id)-
c16f0 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 >locktype;.
c1700 20 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54 OSTRACE3( "FCNT
c1710 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c L_LOCKSTATE %d l
c1720 6f 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 ock=%d\n", ((os2
c1730 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 File*)id)->h, ((
c1740 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f os2File*)id)->lo
c1750 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 cktype );.
c1760 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c1770 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 ;. }. }. re
c1780 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
c1790 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 R;.}../*.** Retu
c17a0 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 rn the sector si
c17b0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 ze in bytes of t
c17c0 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c he underlying bl
c17d0 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a ock device for.*
c17e0 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 * the specified
c17f0 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c file. This is al
c1800 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 most always 512
c1810 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 bytes, but may b
c1820 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 e.** larger for
c1830 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a some devices..**
c1840 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 .** SQLite code
c1850 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e assumes this fun
c1860 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 ction cannot fai
c1870 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d l. It also assum
c1880 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 es that.** if tw
c1890 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 o files are crea
c18a0 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ted in the same
c18b0 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 file-system dire
c18c0 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 ctory (i.e..** a
c18d0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 database and it
c18e0 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 s journal file)
c18f0 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 that the sector
c1900 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 size will be the
c1910 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 .** same for bot
c1920 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 h..*/.static int
c1930 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73 os2SectorSize(s
c1940 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 qlite3_file *id)
c1950 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 {. return SQLIT
c1960 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 E_DEFAULT_SECTOR
c1970 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 _SIZE;.}../*.**
c1980 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 Return a vector
c1990 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 of device charac
c19a0 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 teristics..*/.st
c19b0 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69 atic int os2Devi
c19c0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
c19d0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
c19e0 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b id){. return 0;
c19f0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 .}.../*.** Chara
c1a00 63 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73 cter set convers
c1a10 69 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64 ion objects used
c1a20 20 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 by conversion r
c1a30 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 outines..*/.stat
c1a40 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 ic UconvObject u
c1a50 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a cUtf8 = NULL; /*
c1a60 20 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e convert between
c1a70 20 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 UTF-8 and UCS-2
c1a80 20 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 */.static Uconv
c1a90 4f 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e Object uclCp = N
c1aa0 55 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 ULL; /* convert
c1ab0 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 between local c
c1ac0 6f 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d odepage and UCS-
c1ad0 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 2 */../*.** Help
c1ae0 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 er function to i
c1af0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f nitialize the co
c1b00 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 nversion objects
c1b10 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 from and to UTF
c1b20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f -8..*/.static vo
c1b30 69 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 id initUconvObje
c1b40 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 cts( void ){. i
c1b50 66 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e f( UniCreateUcon
c1b60 76 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 vObject( UTF_8,
c1b70 26 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53 &ucUtf8 ) != ULS
c1b80 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 _SUCCESS ). u
c1b90 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 cUtf8 = NULL;.
c1ba0 69 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63 if ( UniCreateUc
c1bb0 6f 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 onvObject( (UniC
c1bc0 68 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 har *)L"@path=ye
c1bd0 73 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20 s", &uclCp ) !=
c1be0 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 ULS_SUCCESS ).
c1bf0 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a uclCp = NULL;.
c1c00 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 }../*.** Helper
c1c10 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 function to free
c1c20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 the conversion
c1c30 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 objects from and
c1c40 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 to UTF-8..*/.st
c1c50 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63 atic void freeUc
c1c60 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 onvObjects( void
c1c70 20 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66 ){. if ( ucUtf
c1c80 38 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 8 ). UniFreeU
c1c90 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74 convObject( ucUt
c1ca0 66 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c f8 );. if ( ucl
c1cb0 43 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 Cp ). UniFree
c1cc0 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c UconvObject( ucl
c1cd0 43 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d Cp );. ucUtf8 =
c1ce0 20 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d NULL;. uclCp =
c1cf0 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 NULL;.}../*.**
c1d00 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 Helper function
c1d10 74 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 to convert UTF-8
c1d20 20 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f filenames to lo
c1d30 63 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 cal OS/2 codepag
c1d40 65 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 e..** The two-st
c1d50 65 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 ep process: firs
c1d60 74 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e t convert the in
c1d70 63 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 coming UTF-8 str
c1d80 69 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d ing.** into UCS-
c1d90 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 2 and then from
c1da0 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72 UCS-2 to the cur
c1db0 72 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a rent codepage..*
c1dc0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 * The returned c
c1dd0 68 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 har pointer has
c1de0 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a to be freed..*/.
c1df0 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e static char *con
c1e00 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 vertUtf8PathToCp
c1e10 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e ( const char *in
c1e20 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 ){. UniChar te
c1e30 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 mpPath[CCHMAXPAT
c1e40 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 H];. char *out
c1e50 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 = (char *)calloc
c1e60 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 ( CCHMAXPATH, 1
c1e70 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 );.. if( !out )
c1e80 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c . return NULL
c1e90 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 ;.. if( !ucUtf8
c1ea0 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 || !uclCp ).
c1eb0 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 initUconvObject
c1ec0 73 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 s();.. /* deter
c1ed0 6d 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 mine string for
c1ee0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f the conversion o
c1ef0 66 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 f UTF-8 which is
c1f00 20 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 CP1208 */. if(
c1f10 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 UniStrToUcs( uc
c1f20 55 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 Utf8, tempPath,
c1f30 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d (char *)in, CCHM
c1f40 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f AXPATH ) != ULS_
c1f50 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 SUCCESS ). re
c1f60 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 turn out; /* if
c1f70 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 conversion fails
c1f80 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 , return the emp
c1f90 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 ty string */..
c1fa0 2f 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f /* conversion fo
c1fb0 72 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 r current codepa
c1fc0 67 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 ge which can be
c1fd0 75 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a used for paths *
c1fe0 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 /. UniStrFromUc
c1ff0 73 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 s( uclCp, out, t
c2000 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 empPath, CCHMAXP
c2010 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e ATH );.. return
c2020 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 out;.}../*.** H
c2030 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 elper function t
c2040 6f 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 o convert filena
c2050 6d 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 mes from local c
c2060 6f 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 odepage to UTF-8
c2070 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 ..** The two-ste
c2080 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 p process: first
c2090 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 convert the inc
c20a0 6f 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 oming codepage-s
c20b0 70 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e pecific.** strin
c20c0 67 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 g into UCS-2 and
c20d0 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 then from UCS-2
c20e0 20 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 to the codepage
c20f0 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 of UTF-8..** Th
c2100 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 e returned char
c2110 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 pointer has to b
c2120 65 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 e freed..**.** T
c2130 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
c2140 6e 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65 non-static to be
c2150 20 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69 able to use thi
c2160 73 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 s in shell.c and
c2170 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c .** similar appl
c2180 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 ications that ta
c2190 6b 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 ke command line
c21a0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 arguments..*/.ch
c21b0 61 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 ar *convertCpPat
c21c0 68 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63 hToUtf8( const c
c21d0 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 har *in ){. Uni
c21e0 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 Char tempPath[CC
c21f0 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 HMAXPATH];. cha
c2200 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a r *out = (char *
c2210 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 )calloc( CCHMAXP
c2220 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 ATH, 1 );.. if(
c2230 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 !out ). retu
c2240 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 rn NULL;.. if(
c2250 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 !ucUtf8 || !uclC
c2260 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e p ). initUcon
c2270 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f vObjects();.. /
c2280 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 * conversion for
c2290 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 current codepag
c22a0 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 e which can be u
c22b0 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f sed for paths */
c22c0 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 . if( UniStrToU
c22d0 63 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 cs( uclCp, tempP
c22e0 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c ath, (char *)in,
c22f0 20 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d CCHMAXPATH ) !=
c2300 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 ULS_SUCCESS ).
c2310 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f return out; /
c2320 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 * if conversion
c2330 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 fails, return th
c2340 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a e empty string *
c2350 2f 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e /.. /* determin
c2360 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 e string for the
c2370 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 conversion of U
c2380 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 TF-8 which is CP
c2390 31 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 1208 */. UniStr
c23a0 46 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c FromUcs( ucUtf8,
c23b0 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 out, tempPath,
c23c0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 CCHMAXPATH );..
c23d0 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a return out;.}..
c23e0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f /*.** This vecto
c23f0 72 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 r defines all th
c2400 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 e methods that c
c2410 61 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e an operate on an
c2420 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 .** sqlite3_file
c2430 20 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 for os2..*/.sta
c2440 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 tic const sqlite
c2450 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 3_io_methods os2
c2460 49 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 IoMethod = {. 1
c2470 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
c2480 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
c2490 73 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f sion */. os2Clo
c24a0 73 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 se,. os2Read,.
c24b0 20 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 os2Write,. os2
c24c0 54 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 Truncate,. os2S
c24d0 79 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 ync,. os2FileSi
c24e0 7a 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 ze,. os2Lock,.
c24f0 20 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 os2Unlock,. os
c2500 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 2CheckReservedLo
c2510 63 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e ck,. os2FileCon
c2520 74 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f trol,. os2Secto
c2530 72 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 rSize,. os2Devi
c2540 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
c2550 73 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a s.};../*********
c2560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c25a0 2a 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 **.** Here ends
c25b0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 the I/O methods
c25c0 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 that form the sq
c25d0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
c25e0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 object..**.** T
c25f0 68 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 he next block of
c2600 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 code implements
c2610 20 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 the VFS methods
c2620 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**************
c2630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
c2670 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
c2680 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
c2690 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 ame in zBuf. zB
c26a0 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 uf must be big e
c26b0 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 nough to.** hold
c26c0 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 at pVfs->mxPath
c26d0 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e name characters.
c26e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 .*/.static int g
c26f0 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e etTempname(int n
c2700 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 Buf, char *zBuf
c2710 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 ){. static cons
c2720 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 t unsigned char
c2730 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 zChars[] =. "
c2740 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
c2750 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 qrstuvwxyz".
c2760 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f "ABCDEFGHIJKLMNO
c2770 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 PQRSTUVWXYZ".
c2780 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 "0123456789";.
c2790 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 int i, j;. cha
c27a0 72 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33 r zTempPathBuf[3
c27b0 5d 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 ];. PSZ zTempPa
c27c0 74 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 th = (PSZ)&zTemp
c27d0 50 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73 PathBuf;. if( s
c27e0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 qlite3_temp_dire
c27f0 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 ctory ){. zTe
c2800 6d 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 mpPath = sqlite3
c2810 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b _temp_directory;
c2820 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
c2830 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 ( DosScanEnv( (P
c2840 53 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d SZ)"TEMP", &zTem
c2850 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 pPath ) ){.
c2860 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 if( DosScanEnv(
c2870 20 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 (PSZ)"TMP", &zT
c2880 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 empPath ) ){.
c2890 20 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e if( DosScan
c28a0 45 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49 Env( (PSZ)"TMPDI
c28b0 52 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 R", &zTempPath )
c28c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55 ){. U
c28d0 4c 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 LONG ulDriveNum
c28e0 3d 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 = 0, ulDriveMap
c28f0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
c2900 44 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44 DosQueryCurrentD
c2910 69 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d isk( &ulDriveNum
c2920 2c 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b , &ulDriveMap );
c2930 0a 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69 . spri
c2940 6e 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d ntf( (char*)zTem
c2950 70 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 pPath, "%c:", (c
c2960 68 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72 har)( 'A' + ulDr
c2970 69 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a iveNum - 1 ) );.
c2980 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
c2990 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a }. }. }. /*
c29a0 20 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61 Strip off a tra
c29b0 69 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 iling slashes or
c29c0 20 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 backslashes, ot
c29d0 68 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64 herwise we would
c29e0 20 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 get *. * mult
c29f0 69 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68 iple (back)slash
c2a00 65 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20 es which causes
c2a10 44 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 DosOpen() to fai
c2a20 6c 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 l.
c2a30 2a 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 *. * Trailing
c2a40 73 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61 spaces are not a
c2a50 6c 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20 llowed, either.
c2a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2a70 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 */.
c2a80 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 j = sqlite3Strle
c2a90 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a n30(zTempPath);.
c2aa0 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 while( j > 0 &
c2ab0 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d & ( zTempPath[j-
c2ac0 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 1] == '\\' || zT
c2ad0 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 empPath[j-1] ==
c2ae0 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20 '/'.
c2af0 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 || zTemp
c2b00 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 Path[j-1] == ' '
c2b10 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 ) ){. j--;.
c2b20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a }. zTempPath[j
c2b30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 ] = '\0';. if(
c2b40 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 !sqlite3_temp_di
c2b50 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 rectory ){. c
c2b60 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 har *zTempPathUT
c2b70 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 F = convertCpPat
c2b80 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61 hToUtf8( zTempPa
c2b90 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 th );. sqlite
c2ba0 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 3_snprintf( nBuf
c2bb0 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 -30, zBuf,.
c2bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c2bd0 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 "%s\\"SQLITE_TE
c2be0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 MP_FILE_PREFIX,
c2bf0 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a zTempPathUTF );.
c2c00 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50 free( zTempP
c2c10 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 athUTF );. }els
c2c20 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 e{. sqlite3_s
c2c30 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 nprintf( nBuf-30
c2c40 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 , zBuf,.
c2c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 "%
c2c60 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f s\\"SQLITE_TEMP_
c2c70 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 FILE_PREFIX, zTe
c2c80 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 mpPath );. }.
c2c90 6a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 j = sqlite3Strle
c2ca0 6e 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 n30( zBuf );. s
c2cb0 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 qlite3_randomnes
c2cc0 73 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 s( 20, &zBuf[j]
c2cd0 29 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b );. for( i = 0;
c2ce0 20 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b i < 20; i++, j+
c2cf0 2b 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d + ){. zBuf[j]
c2d00 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b = (char)zChars[
c2d10 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 ((unsigned char
c2d20 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f )zBuf[j])%(sizeo
c2d30 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a f(zChars)-1) ];.
c2d40 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 }. zBuf[j] =
c2d50 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 0;. OSTRACE2( "
c2d60 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 TEMP FILENAME: %
c2d70 73 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 s\n", zBuf );.
c2d80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
c2d90 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e ;.}.../*.** Turn
c2da0 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 a relative path
c2db0 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c name into a full
c2dc0 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 pathname. Writ
c2dd0 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 e the full.** pa
c2de0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c thname into zFul
c2df0 6c 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 l[]. zFull[] wi
c2e00 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 ll be at least p
c2e10 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a Vfs->mxPathname.
c2e20 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ** bytes in size
c2e30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
c2e40 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 os2FullPathname(
c2e50 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
c2e60 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f pVfs, /
c2e70 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 * Pointer to vfs
c2e80 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e object */. con
c2e90 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 st char *zRelati
c2ea0 76 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 ve, /* Poss
c2eb0 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e ibly relative in
c2ec0 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e put path */. in
c2ed0 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 t nFull,
c2ee0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a /* Siz
c2ef0 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 e of output buff
c2f00 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 er in bytes */.
c2f10 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 char *zFull
c2f20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c2f30 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f Output buffer */
c2f40 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c .){. char *zRel
c2f50 61 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 ativeCp = conver
c2f60 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a tUtf8PathToCp( z
c2f70 52 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68 Relative );. ch
c2f80 61 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 ar zFullCp[CCHMA
c2f90 58 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 XPATH] = "\0";.
c2fa0 20 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b char *zFullUTF;
c2fb0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 . APIRET rc = D
c2fc0 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 osQueryPathInfo(
c2fd0 20 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49 zRelativeCp, FI
c2fe0 4c 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c L_QUERYFULLNAME,
c2ff0 20 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 zFullCp,.
c3000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3010 20 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58 CCHMAX
c3020 50 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20 PATH );. free(
c3030 7a 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 zRelativeCp );.
c3040 20 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 zFullUTF = conv
c3050 65 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 ertCpPathToUtf8(
c3060 20 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 zFullCp );. sq
c3070 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 lite3_snprintf(
c3080 6e 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 nFull, zFull, zF
c3090 75 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65 ullUTF );. free
c30a0 28 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 ( zFullUTF );.
c30b0 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f return rc == NO_
c30c0 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f ERROR ? SQLITE_O
c30d0 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 K : SQLITE_IOERR
c30e0 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e ;.}.../*.** Open
c30f0 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 a file..*/.stat
c3100 69 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a ic int os2Open(.
c3110 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
c3120 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 Vfs,
c3130 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 /* Not used */.
c3140 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 const char *zNa
c3150 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f me, /
c3160 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 * Name of the fi
c3170 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f le */. sqlite3_
c3180 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 file *id,
c3190 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
c31a0 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 he SQLite file h
c31b0 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 andle here */.
c31c0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
c31d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c31e0 20 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 Open mode flags
c31f0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 */. int *pOutF
c3200 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 lags
c3210 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 /* Status re
c3220 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b turn flags */.){
c3230 0a 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c . HFILE h;. UL
c3240 4f 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62 ONG ulFileAttrib
c3250 75 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 ute = FILE_NORMA
c3260 4c 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 L;. ULONG ulOpe
c3270 6e 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c nFlags = 0;. UL
c3280 4f 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d ONG ulOpenMode =
c3290 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 0;. os2File *p
c32a0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a File = (os2File*
c32b0 29 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 )id;. APIRET rc
c32c0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 = NO_ERROR;. U
c32d0 4c 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 LONG ulAction;.
c32e0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a char *zNameCp;.
c32f0 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b char zTmpname[
c3300 43 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 CCHMAXPATH+1];
c3310 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68 /* Buffer to h
c3320 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 old name of temp
c3330 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 file */.. /* I
c3340 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 f the second arg
c3350 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 ument to this fu
c3360 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 nction is NULL,
c3370 67 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a generate a . **
c3380 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 temporary file
c3390 6e 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a name to use . *
c33a0 2f 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 /. if( !zName )
c33b0 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 {. int rc = g
c33c0 65 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 etTempname(CCHMA
c33d0 58 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d XPATH+1, zTmpnam
c33e0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
c33f0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
c3400 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
c3410 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 }. zName =
c3420 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a zTmpname;. }...
c3430 20 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c memset( pFile,
c3440 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 0, sizeof(*pFil
c3450 65 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 e) );.. OSTRACE
c3460 32 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 2( "OPEN want %d
c3470 5c 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 \n", flags );..
c3480 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c if( flags & SQL
c3490 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
c34a0 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e TE ){. ulOpen
c34b0 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 Mode |= OPEN_ACC
c34c0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 ESS_READWRITE;.
c34d0 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 OSTRACE1( "OP
c34e0 45 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 EN read/write\n"
c34f0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 );. }else{.
c3500 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f ulOpenMode |= O
c3510 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f PEN_ACCESS_READO
c3520 4e 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 NLY;. OSTRACE
c3530 31 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 1( "OPEN read on
c3540 6c 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 ly\n" );. }..
c3550 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 if( flags & SQLI
c3560 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 TE_OPEN_CREATE )
c3570 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 {. ulOpenFlag
c3580 73 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e s |= OPEN_ACTION
c3590 5f 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 _OPEN_IF_EXISTS
c35a0 7c 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 | OPEN_ACTION_CR
c35b0 45 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 EATE_IF_NEW;.
c35c0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e OSTRACE1( "OPEN
c35d0 20 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 open new/create
c35e0 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a \n" );. }else{.
c35f0 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 ulOpenFlags
c3600 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f |= OPEN_ACTION_O
c3610 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 PEN_IF_EXISTS |
c3620 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c OPEN_ACTION_FAIL
c3630 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 _IF_NEW;. OST
c3640 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 RACE1( "OPEN ope
c3650 6e 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b n existing\n" );
c3660 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 . }.. if( flag
c3670 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f s & SQLITE_OPEN_
c3680 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 MAIN_DB ){. u
c3690 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 lOpenMode |= OPE
c36a0 4e 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 N_SHARE_DENYNONE
c36b0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 ;. OSTRACE1(
c36c0 22 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 "OPEN share read
c36d0 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d /write\n" );. }
c36e0 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e else{. ulOpen
c36f0 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 Mode |= OPEN_SHA
c3700 52 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 RE_DENYWRITE;.
c3710 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 OSTRACE1( "OPE
c3720 4e 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c N share read onl
c3730 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 y\n" );. }.. i
c3740 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
c3750 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 E_OPEN_DELETEONC
c3760 4c 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 LOSE ){. char
c3770 20 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58 pathUtf8[CCHMAX
c3780 50 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 PATH];.#ifdef ND
c3790 45 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 EBUG /* when deb
c37a0 75 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74 ugging we want t
c37b0 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 o make sure it i
c37c0 73 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 s deleted */.
c37d0 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 ulFileAttribute
c37e0 20 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a = FILE_HIDDEN;.
c37f0 23 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75 #endif. os2Fu
c3800 6c 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73 llPathname( pVfs
c3810 2c 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 , zName, CCHMAXP
c3820 41 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b ATH, pathUtf8 );
c3830 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 . pFile->path
c3840 54 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 ToDel = convertU
c3850 74 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74 tf8PathToCp( pat
c3860 68 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 hUtf8 );. OST
c3870 52 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64 RACE1( "OPEN hid
c3880 64 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c den/delete on cl
c3890 6f 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75 ose file attribu
c38a0 74 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 tes\n" );. }els
c38b0 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 e{. pFile->pa
c38c0 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a thToDel = NULL;.
c38d0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f OSTRACE1( "O
c38e0 50 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 PEN normal file
c38f0 61 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a attribute\n" );.
c3900 20 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 }.. /* always
c3910 20 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 open in random
c3920 61 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 access mode for
c3930 70 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 possibly better
c3940 73 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 speed */. ulOpe
c3950 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c nMode |= OPEN_FL
c3960 41 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c AGS_RANDOM;. ul
c3970 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e OpenMode |= OPEN
c3980 5f 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 _FLAGS_FAIL_ON_E
c3990 52 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f RROR;. ulOpenMo
c39a0 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 de |= OPEN_FLAGS
c39b0 5f 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a _NOINHERIT;.. z
c39c0 4e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 NameCp = convert
c39d0 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e Utf8PathToCp( zN
c39e0 61 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f ame );. rc = Do
c39f0 73 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d sOpen( (PSZ)zNam
c3a00 65 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 eCp,.
c3a10 20 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20 &h,.
c3a20 20 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69 &ulActi
c3a30 6f 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 on,.
c3a40 20 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 0L,.
c3a50 20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74 ulFileAt
c3a60 74 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20 tribute,.
c3a70 20 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 ulOpenF
c3a80 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 lags,.
c3a90 20 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 ulOpenMode
c3aa0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
c3ab0 20 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 (PEAOP2)NULL )
c3ac0 3b 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 ;. free( zNameC
c3ad0 70 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d p );. if( rc !=
c3ae0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 NO_ERROR ){.
c3af0 20 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e OSTRACE7( "OPEN
c3b00 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 Invalid handle
c3b10 72 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c rc=%d: zName=%s,
c3b20 20 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 ulAction=%#lx,
c3b30 75 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 ulAttr=%#lx, ulF
c3b40 6c 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 lags=%#lx, ulMod
c3b50 65 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 e=%#lx\n",.
c3b60 20 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 rc, zNa
c3b70 6d 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c me, ulAction, ul
c3b80 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75 FileAttribute, u
c3b90 6c 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 lOpenFlags, ulOp
c3ba0 65 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 enMode );. if
c3bb0 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 ( pFile->pathToD
c3bc0 65 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28 el ). free(
c3bd0 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 pFile->pathToDe
c3be0 6c 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e l );. pFile->
c3bf0 70 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c pathToDel = NULL
c3c00 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 ;. if( flags
c3c10 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 & SQLITE_OPEN_RE
c3c20 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 ADWRITE ){.
c3c30 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e OSTRACE2( "OPEN
c3c40 20 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 %d Invalid hand
c3c50 6c 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c le\n", ((flags |
c3c60 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
c3c70 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 DONLY) & ~SQLITE
c3c80 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 _OPEN_READWRITE)
c3c90 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e );. return
c3ca0 20 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 os2Open( pVfs,
c3cb0 7a 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 zName, id,.
c3cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3cd0 20 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 ((flags | SQLIT
c3ce0 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 E_OPEN_READONLY)
c3cf0 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f & ~SQLITE_OPEN_
c3d00 52 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20 READWRITE),.
c3d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3d20 20 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 pOutFlags );.
c3d30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
c3d40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
c3d50 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
c3d60 7d 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 }.. if( pOutFla
c3d70 67 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 gs ){. *pOutF
c3d80 6c 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53 lags = flags & S
c3d90 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
c3da0 52 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 RITE ? SQLITE_OP
c3db0 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53 EN_READWRITE : S
c3dc0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f QLITE_OPEN_READO
c3dd0 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c NLY;. }.. pFil
c3de0 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 e->pMethod = &os
c3df0 32 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 2IoMethod;. pFi
c3e00 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 le->h = h;. Ope
c3e10 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 nCounter(+1);.
c3e20 4f 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20 OSTRACE3( "OPEN
c3e30 25 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c %d pOutFlags=%d\
c3e40 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f n", pFile->h, pO
c3e50 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 utFlags );. ret
c3e60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
c3e70 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 ../*.** Delete t
c3e80 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a he named file..*
c3e90 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 /.static int os2
c3ea0 44 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 Delete(. sqlite
c3eb0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 3_vfs *pVfs,
c3ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3ed0 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 /* Not used on
c3ee0 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 os2 */. const c
c3ef0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 har *zFilename,
c3f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3f10 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 /* Name of file
c3f20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 to delete */. i
c3f30 6e 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 nt syncDir
c3f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c3f50 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 /* Not use
c3f60 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 d on os2 */.){.
c3f70 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f APIRET rc = NO_
c3f80 45 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a ERROR;. char *z
c3f90 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e FilenameCp = con
c3fa0 76 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 vertUtf8PathToCp
c3fb0 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 ( zFilename );.
c3fc0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
c3fd0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
c3fe0 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a IOERR_DELETE );.
c3ff0 20 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 rc = DosDelete
c4000 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 ( (PSZ)zFilename
c4010 43 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 Cp );. free( zF
c4020 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f ilenameCp );. O
c4030 53 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45 STRACE2( "DELETE
c4040 20 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c \"%s\"\n", zFil
c4050 65 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 ename );. retur
c4060 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 n rc == NO_ERROR
c4070 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 ? SQLITE_OK : S
c4080 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 QLITE_IOERR_DELE
c4090 54 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 TE;.}../*.** Che
c40a0 63 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 ck the existance
c40b0 20 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 and status of a
c40c0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
c40d0 20 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a int os2Access(.
c40e0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 sqlite3_vfs *p
c40f0 56 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e Vfs, /* N
c4100 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a ot used on os2 *
c4110 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
c4120 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a zFilename, /*
c4130 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
c4140 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 check */. int
c4150 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
c4160 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 /* Type of
c4170 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e test to make on
c4180 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 this file */.
c4190 69 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20 int *pOut
c41a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
c41b0 74 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 te results here
c41c0 2a 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 */.){. FILESTAT
c41d0 55 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 US3 fsts3ConfigI
c41e0 6e 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 nfo;. APIRET rc
c41f0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 = NO_ERROR;. c
c4200 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 har *zFilenameCp
c4210 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 = convertUtf8Pa
c4220 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d thToCp( zFilenam
c4230 65 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 e );.. memset(
c4240 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f &fsts3ConfigInfo
c4250 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 , 0, sizeof(fsts
c4260 33 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 3ConfigInfo) );.
c4270 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 rc = DosQueryP
c4280 61 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 athInfo( (PSZ)zF
c4290 69 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 ilenameCp, FIL_S
c42a0 54 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 TANDARD,.
c42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c42c0 20 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e &fsts3ConfigIn
c42d0 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 fo, sizeof(FILES
c42e0 54 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 TATUS3) );. fre
c42f0 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 e( zFilenameCp )
c4300 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 ;. OSTRACE4( "A
c4310 43 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 CCESS fsts3Confi
c4320 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 gInfo.attrFile=%
c4330 64 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 d flags=%d rc=%d
c4340 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
c4350 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f fsts3ConfigInfo
c4360 2e 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 .attrFile, flags
c4370 2c 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 , rc );. switch
c4380 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 ( flags ){. c
c4390 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 ase SQLITE_ACCES
c43a0 53 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 S_READ:. case
c43b0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 SQLITE_ACCESS_E
c43c0 58 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 XISTS:. rc
c43d0 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f = (rc == NO_ERRO
c43e0 52 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 R);. OSTRAC
c43f0 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 E3( "ACCESS %s a
c4400 63 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e ccess of read an
c4410 64 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c d exists rc=%d\
c4420 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 n", zFilename, r
c4430 63 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b c );. break
c4440 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 ;. case SQLIT
c4450 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 E_ACCESS_READWRI
c4460 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 TE:. rc = (
c4470 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 rc == NO_ERROR)
c4480 26 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69 && ( (fsts3Confi
c4490 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 gInfo.attrFile &
c44a0 20 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 FILE_READONLY)
c44b0 3d 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 == 0 );. OS
c44c0 54 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 TRACE3( "ACCESS
c44d0 25 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 %s access of rea
c44e0 64 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e d/write rc=%d\n
c44f0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 ", zFilename, rc
c4500 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b );. break;
c4510 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 . default:.
c4520 20 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e assert( !"In
c4530 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 valid flags argu
c4540 6d 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a ment" );. }. *
c4550 70 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 pOut = rc;. ret
c4560 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
c4570 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
c4580 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
c4590 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 NSION./*.** Inte
c45a0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
c45b0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
c45c0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
c45d0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
c45e0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
c45f0 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
c4600 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
c4610 69 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a ibrary..*/./*.**
c4620 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 Interfaces for
c4630 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 opening a shared
c4640 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e library, findin
c4650 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a g entry points.*
c4660 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 * within the sha
c4670 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 red library, and
c4680 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 closing the sha
c4690 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a red library..*/.
c46a0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 static void *os2
c46b0 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 DlOpen(sqlite3_v
c46c0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 fs *pVfs, const
c46d0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
c46e0 7b 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 {. UCHAR loadEr
c46f0 72 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c r[256];. HMODUL
c4700 45 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 E hmod;. APIRET
c4710 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 rc;. char *zFi
c4720 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 lenameCp = conve
c4730 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a rtUtf8PathToCp(z
c4740 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 Filename);. rc
c4750 3d 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 = DosLoadModule(
c4760 28 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 (PSZ)loadErr, si
c4770 7a 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a zeof(loadErr), z
c4780 46 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f FilenameCp, &hmo
c4790 64 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 d);. free(zFile
c47a0 6e 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 nameCp);. retur
c47b0 6e 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 n rc != NO_ERROR
c47c0 20 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d ? 0 : (void*)hm
c47d0 6f 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f od;.}./*.** A no
c47e0 2d 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 -op since the er
c47f0 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
c4800 72 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c rned on the DosL
c4810 6f 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a oadModule call..
c4820 2a 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 ** os2Dlopen ret
c4830 75 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 urns zero if Dos
c4840 4c 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f LoadModule is no
c4850 74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f t successful..*/
c4860 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 .static void os2
c4870 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f DlError(sqlite3_
c4880 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e vfs *pVfs, int n
c4890 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f Buf, char *zBufO
c48a0 75 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f ut){./* no-op */
c48b0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a .}.static void *
c48c0 6f 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 os2DlSym(sqlite3
c48d0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 _vfs *pVfs, void
c48e0 20 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 *pHandle, const
c48f0 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b char *zSymbol){
c4900 0a 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 . PFN pfn;. AP
c4910 49 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 IRET rc;. rc =
c4920 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 DosQueryProcAddr
c4930 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c ((HMODULE)pHandl
c4940 65 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 e, 0L, zSymbol,
c4950 26 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 &pfn);. if( rc
c4960 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 != NO_ERROR ){.
c4970 20 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d /* if the sym
c4980 62 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e bol itself was n
c4990 6f 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 ot found, search
c49a0 20 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73 again for the s
c49b0 61 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f ame. * symbo
c49c0 6c 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20 l with an extra
c49d0 75 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74 underscore, that
c49e0 20 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64 might be needed
c49f0 20 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 depending.
c4a00 2a 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 * on the calling
c4a10 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 convention */.
c4a20 20 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c char _zSymbol
c4a30 5b 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 [256] = "_";.
c4a40 20 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f strncat(_zSymbo
c4a50 6c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 l, zSymbol, 255)
c4a60 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75 ;. rc = DosQu
c4a70 65 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f eryProcAddr((HMO
c4a80 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c DULE)pHandle, 0L
c4a90 2c 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e , _zSymbol, &pfn
c4aa0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
c4ab0 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f rc != NO_ERROR ?
c4ac0 20 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0 : (void*)pfn;
c4ad0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f .}.static void o
c4ae0 73 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 s2DlClose(sqlite
c4af0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 3_vfs *pVfs, voi
c4b00 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 d *pHandle){. D
c4b10 6f 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d osFreeModule((HM
c4b20 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a ODULE)pHandle);.
c4b30 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 }.#else /* if SQ
c4b40 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 LITE_OMIT_LOAD_E
c4b50 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 XTENSION is defi
c4b60 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e ned: */. #defin
c4b70 65 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 e os2DlOpen 0.
c4b80 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 #define os2DlErr
c4b90 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f or 0. #define o
c4ba0 73 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 s2DlSym 0. #def
c4bb0 69 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 ine os2DlClose 0
c4bc0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
c4bd0 57 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 Write up to nBuf
c4be0 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d bytes of random
c4bf0 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a ness into zBuf..
c4c00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 */.static int os
c4c10 32 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 2Randomness(sqli
c4c20 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 te3_vfs *pVfs, i
c4c30 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a nt nBuf, char *z
c4c40 42 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d Buf ){. int n =
c4c50 20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 0;.#if defined(
c4c60 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e SQLITE_TEST). n
c4c70 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 = nBuf;. memse
c4c80 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 t(zBuf, 0, nBuf)
c4c90 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 ;.#else. int si
c4ca0 7a 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 zeofULong = size
c4cb0 6f 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 of(ULONG);. if(
c4cc0 20 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 (int)sizeof(DAT
c4cd0 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d ETIME) <= nBuf -
c4ce0 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49 n ){. DATETI
c4cf0 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 ME x;. DosGet
c4d00 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 DateTime(&x);.
c4d10 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e memcpy(&zBuf[n
c4d20 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 ], &x, sizeof(x)
c4d30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
c4d40 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 of(x);. }.. if
c4d50 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d ( sizeofULong <=
c4d60 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 nBuf - n ){.
c4d70 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 PPIB ppib;.
c4d80 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 DosGetInfoBlocks
c4d90 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 (NULL, &ppib);.
c4da0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b memcpy(&zBuf[
c4db0 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 n], &ppib->pib_u
c4dc0 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e lpid, sizeofULon
c4dd0 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a g);. n += siz
c4de0 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 eofULong;. }..
c4df0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 if( sizeofULong
c4e00 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a <= nBuf - n ){.
c4e10 20 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 PTIB ptib;.
c4e20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f DosGetInfoBlo
c4e30 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 cks(&ptib, NULL)
c4e40 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 ;. memcpy(&zB
c4e50 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 uf[n], &ptib->ti
c4e60 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c b_ptib2->tib2_ul
c4e70 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 tid, sizeofULong
c4e80 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 );. n += size
c4e90 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 ofULong;. }..
c4ea0 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68 /* if we still h
c4eb0 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 aven't filled th
c4ec0 65 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65 e buffer yet the
c4ed0 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 following will
c4ee0 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65 */. /* grab eve
c4ef0 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 rything once ins
c4f00 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 tead of making s
c4f10 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 everal calls for
c4f20 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a a single item *
c4f30 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c /. if( sizeofUL
c4f40 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 ong <= nBuf - n
c4f50 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 ){. ULONG ulS
c4f60 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b ysInfo[QSV_MAX];
c4f70 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 . DosQuerySys
c4f80 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 Info(1L, QSV_MAX
c4f90 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a , ulSysInfo, siz
c4fa0 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d eofULong * QSV_M
c4fb0 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 AX);.. memcpy
c4fc0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 (&zBuf[n], &ulSy
c4fd0 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 sInfo[QSV_MS_COU
c4fe0 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 NT - 1], sizeofU
c4ff0 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 Long);. n +=
c5000 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 sizeofULong;..
c5010 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e if( sizeofULon
c5020 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b g <= nBuf - n ){
c5030 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a . memcpy(&z
c5040 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e Buf[n], &ulSysIn
c5050 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 fo[QSV_TIMER_INT
c5060 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 ERVAL - 1], size
c5070 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 ofULong);.
c5080 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
c5090 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
c50a0 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e sizeofULong <= n
c50b0 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 Buf - n ){.
c50c0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d memcpy(&zBuf[n]
c50d0 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 , &ulSysInfo[QSV
c50e0 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 _TIME_LOW - 1],
c50f0 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 sizeofULong);.
c5100 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 n += sizeofU
c5110 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Long;. }.
c5120 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 if( sizeofULong
c5130 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 <= nBuf - n ){.
c5140 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 memcpy(&zBu
c5150 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f f[n], &ulSysInfo
c5160 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d [QSV_TIME_HIGH -
c5170 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 1], sizeofULong
c5180 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 );. n += si
c5190 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d zeofULong;. }
c51a0 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 . if( sizeofU
c51b0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e Long <= nBuf - n
c51c0 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 ){. memcpy
c51d0 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 (&zBuf[n], &ulSy
c51e0 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 sInfo[QSV_TOTAVA
c51f0 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 ILMEM - 1], size
c5200 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 ofULong);.
c5210 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 n += sizeofULong
c5220 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 ;. }. }.#end
c5230 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a if.. return n;.
c5240 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 }../*.** Sleep f
c5250 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c or a little whil
c5260 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 e. Return the a
c5270 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c mount of time sl
c5280 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 ept..** The argu
c5290 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 ment is the numb
c52a0 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e er of microsecon
c52b0 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c ds we want to sl
c52c0 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 eep..** The retu
c52d0 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 rn value is the
c52e0 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 number of micros
c52f0 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 econds of sleep
c5300 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 actually.** requ
c5310 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 ested from the u
c5320 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 nderlying operat
c5330 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 ing system, a nu
c5340 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 mber which.** mi
c5350 67 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 ght be greater t
c5360 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
c5370 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 the argument, bu
c5380 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 t not less.** th
c5390 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e an the argument.
c53a0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f .*/.static int o
c53b0 73 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33 s2Sleep( sqlite3
c53c0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 _vfs *pVfs, int
c53d0 6d 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f microsec ){. Do
c53e0 73 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65 sSleep( (microse
c53f0 63 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74 c/1000) );. ret
c5400 75 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a urn microsec;.}.
c5410 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
c5420 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 wing variable, i
c5430 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a f set to a non-z
c5440 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d ero value, becom
c5450 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a es the result.**
c5460 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 returned from s
c5470 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 qlite3OsCurrentT
c5480 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 ime(). This is
c5490 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
c54a0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
c54b0 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
c54c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 PI int sqlite3_c
c54d0 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b urrent_time = 0;
c54e0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 .#endif../*.** F
c54f0 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 ind the current
c5500 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 time (in Univers
c5510 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 al Coordinated T
c5520 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 ime). Write the
c5530 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 .** current time
c5540 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a and date as a J
c5550 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 ulian Day number
c5560 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 into *prNow and
c5570 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 .** return 0. R
c5580 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 eturn 1 if the t
c5590 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e ime and date can
c55a0 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f not be found..*/
c55b0 0a 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54 .int os2CurrentT
c55c0 69 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 ime( sqlite3_vfs
c55d0 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a *pVfs, double *
c55e0 70 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c prNow ){. doubl
c55f0 65 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d e now;. SHORT m
c5600 69 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 inute; /* needs
c5610 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f to be able to co
c5620 70 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65 pe with negative
c5630 20 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 timezone offset
c5640 20 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63 */. USHORT sec
c5650 6f 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 ond, hour,.
c5660 20 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 day, month,
c5670 79 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 year;. DATETIME
c5680 20 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 dt;. DosGetDat
c5690 65 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 eTime( &dt );.
c56a0 73 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 second = (USHORT
c56b0 29 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d )dt.seconds;. m
c56c0 69 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64 inute = (SHORT)d
c56d0 74 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 t.minutes + dt.t
c56e0 69 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 imezone;. hour
c56f0 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 = (USHORT)dt.hou
c5700 72 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48 rs;. day = (USH
c5710 4f 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f ORT)dt.day;. mo
c5720 6e 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74 nth = (USHORT)dt
c5730 2e 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d .month;. year =
c5740 20 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72 (USHORT)dt.year
c5750 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 ;.. /* Calculat
c5760 69 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f ions from http:/
c5770 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 /www.astro.keele
c5780 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 .ac.uk/~rno/Astr
c5790 6f 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 onomy/hjd.html.
c57a0 20 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 http://www.a
c57b0 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b stro.keele.ac.uk
c57c0 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f /~rno/Astronomy/
c57d0 68 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f hjd-0.1.c */. /
c57e0 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 * Calculate the
c57f0 4a 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 Julian days */.
c5800 20 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30 now = day - 320
c5810 37 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 76 +. 1461*(y
c5820 65 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f ear + 4800 + (mo
c5830 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 nth - 14)/12)/4
c5840 2b 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 +. 367*(month
c5850 20 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 - 2 - (month -
c5860 31 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 14)/12*12)/12 -.
c5870 20 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34 3*((year + 4
c5880 39 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 900 + (month - 1
c5890 34 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 4)/12)/100)/4;..
c58a0 20 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61 /* Add the fra
c58b0 63 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d ctional hours, m
c58c0 69 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 ins and seconds
c58d0 2a 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 */. now += (hou
c58e0 72 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a r + 12.0)/24.0;.
c58f0 20 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f now += minute/
c5900 31 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 1440.0;. now +=
c5910 20 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b second/86400.0;
c5920 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b . *prNow = now;
c5930 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 .#ifdef SQLITE_T
c5940 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 EST. if( sqlite
c5950 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 3_current_time )
c5960 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 {. *prNow = s
c5970 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 qlite3_current_t
c5980 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 ime/86400.0 + 24
c5990 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 40587.5;. }.#en
c59a0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a dif. return 0;.
c59b0 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 }..static int os
c59c0 32 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 2GetLastError(sq
c59d0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
c59e0 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 int nBuf, char
c59f0 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e *zBuf){. return
c5a00 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 0;.}../*.** Ini
c5a10 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e tialize and dein
c5a20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 itialize the ope
c5a30 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e rating system in
c5a40 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 terface..*/.SQLI
c5a50 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
c5a60 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 e3_os_init(void)
c5a70 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 {. static sqlit
c5a80 65 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20 e3_vfs os2Vfs =
c5a90 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 {. 1,
c5aa0 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 /* iVer
c5ab0 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 sion */. size
c5ac0 6f 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f of(os2File), /
c5ad0 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 * szOsFile */.
c5ae0 20 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20 CCHMAXPATH,
c5af0 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 /* mxPathna
c5b00 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 me */. 0,
c5b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c5b20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 pNext */. "os
c5b30 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2",
c5b40 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 /* zName */.
c5b50 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
c5b60 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a /* pAppData *
c5b70 2f 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 /.. os2Open,
c5b80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 /* xOp
c5b90 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c en */. os2Del
c5ba0 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 ete, /*
c5bb0 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f xDelete */. o
c5bc0 73 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 s2Access,
c5bd0 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a /* xAccess */.
c5be0 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e os2FullPathn
c5bf0 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 ame, /* xFullP
c5c00 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f athname */. o
c5c10 73 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 s2DlOpen,
c5c20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a /* xDlOpen */.
c5c30 20 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 os2DlError,
c5c40 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 /* xDlErr
c5c50 6f 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 or */. os2DlS
c5c60 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ym, /*
c5c70 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 xDlSym */. os
c5c80 32 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 2DlClose,
c5c90 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a /* xDlClose */.
c5ca0 20 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 os2Randomnes
c5cb0 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f s, /* xRando
c5cc0 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 mness */. os2
c5cd0 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 Sleep,
c5ce0 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 /* xSleep */.
c5cf0 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c os2CurrentTime,
c5d00 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 /* xCurrentT
c5d10 69 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 ime */. os2Ge
c5d20 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a tLastError /*
c5d30 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a xGetLastError *
c5d40 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 /. };. sqlite3
c5d50 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f _vfs_register(&o
c5d60 73 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 s2Vfs, 1);. ini
c5d70 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b tUconvObjects();
c5d80 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
c5d90 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 _OK;.}.SQLITE_AP
c5da0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 I int sqlite3_os
c5db0 5f 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 _end(void){. fr
c5dc0 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 eeUconvObjects()
c5dd0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
c5de0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 E_OK;.}..#endif
c5df0 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 /* SQLITE_OS_OS2
c5e00 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
c5e10 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f **** End of os_o
c5e20 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a s2.c ***********
c5e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e50 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
c5e60 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
c5e70 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a os_unix.c ******
c5e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5ea0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 ***/./*.** 2004
c5eb0 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 May 22.**.** The
c5ec0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
c5ed0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
c5ee0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
c5ef0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
c5f00 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
c5f10 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
c5f20 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
c5f30 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
c5f40 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
c5f50 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
c5f60 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
c5f70 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
c5f80 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
c5f90 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
c5fa0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
c5fb0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
c5fc0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
c5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c5ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c6010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
c6020 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
c6030 69 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c ins the VFS impl
c6040 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 ementation for u
c6050 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 nix-like operati
c6060 6e 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e ng systems.** in
c6070 63 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 clude Linux, Mac
c6080 4f 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 OSX, *BSD, QNX,
c6090 56 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 VxWorks, AIX, HP
c60a0 55 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a UX, and others..
c60b0 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 **.** There are
c60c0 61 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c actually several
c60d0 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69 different VFS i
c60e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 mplementations i
c60f0 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 n this file..**
c6100 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 The differences
c6110 61 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 are in the way t
c6120 68 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 hat file locking
c6130 20 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 is done. The d
c6140 65 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d efault.** implem
c6150 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f entation uses Po
c6160 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 six Advisory Loc
c6170 6b 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 ks. Alternative
c6180 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 implementations
c6190 0a 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c .** use flock(),
c61a0 20 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 dot-files, vari
c61b0 6f 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20 ous proprietary
c61c0 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c locking schemas,
c61d0 20 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b or simply.** sk
c61e0 69 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 ip locking all t
c61f0 6f 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 ogether..**.** T
c6200 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 his source file
c6210 69 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 is organized int
c6220 6f 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72 o divisions wher
c6230 65 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 e the logic for
c6240 76 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 various.** subfu
c6250 6e 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 nctions is conta
c6260 69 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 ined within the
c6270 61 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69 appropriate divi
c6280 73 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a sion. PLEASE.**
c6290 20 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54 KEEP THE STRUCT
c62a0 55 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45 URE OF THIS FILE
c62b0 20 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f INTACT. New co
c62c0 64 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 de should be pla
c62d0 63 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f ced.** in the co
c62e0 72 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61 rrect division a
c62f0 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 nd should be cle
c6300 61 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a arly labeled..**
c6310 0a 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f .** The layout o
c6320 66 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61 f divisions is a
c6330 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
c6340 20 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 * General-pu
c6350 72 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f rpose declaratio
c6360 6e 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66 ns and utility f
c6370 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a unctions..** *
c6380 20 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 Unique file ID
c6390 20 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56 logic used by V
c63a0 78 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 xWorks..** *
c63b0 56 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 Various locking
c63c0 70 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d primitive implem
c63d0 65 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 entations (all e
c63e0 78 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b xcept proxy lock
c63f0 69 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 ing):.** +
c6400 66 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f for Posix Adviso
c6410 72 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 ry Locks.**
c6420 20 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 + for no-op loc
c6430 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 ks.** + for
c6440 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a dot-file locks.
c6450 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c ** + for fl
c6460 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a ock() locking.**
c6470 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 + for name
c6480 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b d semaphore lock
c6490 73 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 s (VxWorks only)
c64a0 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41 .** + for A
c64b0 46 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f FP filesystem lo
c64c0 63 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 cks (MacOSX only
c64d0 29 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 ).** * sqlite
c64e0 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 3_file methods n
c64f0 6f 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ot associated wi
c6500 74 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 th locking..**
c6510 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 * Definitions
c6520 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 of sqlite3_io_me
c6530 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f thods objects fo
c6540 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a r all locking.**
c6550 20 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c methods pl
c6560 75 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 us "finder" func
c6570 74 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c tions for each l
c6580 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a ocking method..*
c6590 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 * * sqlite3_v
c65a0 66 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d fs method implem
c65b0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 entations..**
c65c0 2a 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 * Locking primi
c65d0 74 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72 tives for the pr
c65e0 6f 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 oxy uber-locking
c65f0 2d 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 -method. (MacOSX
c6600 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 only).** * D
c6610 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 efinitions of sq
c6620 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 lite3_vfs object
c6630 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e s for all lockin
c6640 67 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 g methods.**
c6650 20 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 plus implement
c6660 61 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 ations of sqlite
c6670 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 3_os_init() and
c6680 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 sqlite3_os_end()
c6690 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f ..*/.#if SQLITE_
c66a0 4f 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20 OS_UNIX
c66b0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c /* This fil
c66c0 65 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 e is used on uni
c66d0 78 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a x only */../*.**
c66e0 20 54 68 65 72 65 20 61 72 65 20 76 61 72 69 6f There are vario
c66f0 75 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 us methods for f
c6700 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 ile locking used
c6710 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 for concurrency
c6720 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a .** control:.**.
c6730 2a 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f ** 1. POSIX lo
c6740 63 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 75 cking (the defau
c6750 6c 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 lt),.** 2. No
c6760 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e locking,.** 3.
c6770 20 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e Dot-file lockin
c6780 67 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b g,.** 4. flock
c6790 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 () locking,.**
c67a0 20 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 5. AFP locking
c67b0 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 (OSX only),.**
c67c0 20 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 6. Named POSIX
c67d0 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 6f semaphores (VXWo
c67e0 72 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 rks only),.**
c67f0 37 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 7. proxy locking
c6800 2e 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a . (OSX only).**.
c6810 2a 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c 20 ** Styles 4, 5,
c6820 61 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 61 and 7 are only a
c6830 76 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49 vailable of SQLI
c6840 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
c6850 47 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65 G_STYLE.** is de
c6860 66 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 65 fined to 1. The
c6870 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c SQLITE_ENABLE_L
c6880 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73 OCKING_STYLE als
c6890 6f 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61 o enables automa
c68a0 74 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e tic.** selection
c68b0 20 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 69 of the appropri
c68c0 61 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c ate locking styl
c68d0 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 e based on the f
c68e0 69 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65 ilesystem.** whe
c68f0 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 re the database
c6900 69 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f is located. .*/
c6910 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
c6920 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
c6930 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 ING_STYLE).# if
c6940 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
c6950 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 __).# define
c6960 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f SQLITE_ENABLE_LO
c6970 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 CKING_STYLE 1.#
c6980 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e else.# defin
c6990 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f e SQLITE_ENABLE_
c69a0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a LOCKING_STYLE 0.
c69b0 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a # endif.#endif.
c69c0 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 ./*.** Define th
c69d0 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 65 e OS_VXWORKS pre
c69e0 2d 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f -processor macro
c69f0 20 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 6e to 1 if buildin
c6a00 67 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 g on .** vxworks
c6a10 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65 , or 0 otherwise
c6a20 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f ..*/.#ifndef OS_
c6a30 56 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 65 VXWORKS.# if de
c6a40 66 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c fined(__RTP__) |
c6a50 7c 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b | defined(_WRS_K
c6a60 45 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 69 ERNEL).# defi
c6a70 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a ne OS_VXWORKS 1.
c6a80 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 # else.# def
c6a90 69 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 ine OS_VXWORKS 0
c6aa0 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 .# endif.#endif
c6ab0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 ../*.** These #d
c6ac0 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e efines should en
c6ad0 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 able >2GB file s
c6ae0 75 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 upport on Posix
c6af0 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c if the.** underl
c6b00 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 ying operating s
c6b10 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 ystem supports i
c6b20 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 t. If the OS la
c6b30 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c cks.** large fil
c6b40 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65 e support, these
c6b50 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 should be no-op
c6b60 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 s..**.** Large f
c6b70 69 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20 ile support can
c6b80 62 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e be disabled usin
c6b90 67 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 g the -DSQLITE_D
c6ba0 49 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 ISABLE_LFS switc
c6bb0 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 h.** on the comp
c6bc0 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e iler command lin
c6bd0 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 e. This is nece
c6be0 73 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65 ssary if you are
c6bf0 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e compiling.** on
c6c00 20 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e a recent machin
c6c10 65 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e e (ex: RedHat 7.
c6c20 32 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20 2) but you want
c6c30 79 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 your code to wor
c6c40 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 k.** on an older
c6c50 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 machine (ex: Re
c6c60 64 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79 dHat 6.0). If y
c6c70 6f 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 ou compile on Re
c6c80 64 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 dHat 7.2.** with
c6c90 6f 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c out this option,
c6ca0 20 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 LFS is enable.
c6cb0 20 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f But LFS does no
c6cc0 74 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b t exist in the k
c6cd0 65 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 ernel.** in RedH
c6ce0 61 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 at 6.0, so the c
c6cf0 6f 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 ode won't work.
c6d00 20 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 Hence, for maxi
c6d10 6d 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f mum binary.** po
c6d20 72 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 rtability you sh
c6d30 6f 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a ould omit LFS..*
c6d40 2a 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75 *.** The previou
c6d50 73 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20 s paragraph was
c6d60 77 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e written in 2005.
c6d70 20 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70 (This paragrap
c6d80 68 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 h is written.**
c6d90 6f 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20 on 2008-11-28.)
c6da0 54 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20 These days, all
c6db0 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75 Linux kernels su
c6dc0 70 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65 pport large file
c6dd0 73 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f s, so.** you sho
c6de0 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 61 uld probably lea
c6df0 76 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20 ve LFS enabled.
c6e00 20 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 64 But some embedd
c6e10 65 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 ed platforms mig
c6e20 68 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69 ht.** lack LFS i
c6e30 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 n which case the
c6e40 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f SQLITE_DISABLE_
c6e50 4c 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 20 LFS macro might
c6e60 73 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e still be useful.
c6e70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
c6e80 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 TE_DISABLE_LFS.#
c6e90 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 define _LARGE_F
c6ea0 49 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 ILE 1.# if
c6eb0 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 ndef _FILE_OFFSE
c6ec0 54 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e T_BITS.# defin
c6ed0 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 e _FILE_OFFSET_B
c6ee0 49 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 ITS 64.# endif.#
c6ef0 20 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 define _LARGEFI
c6f00 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 LE_SOURCE 1.#end
c6f10 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 if../*.** standa
c6f20 72 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 rd include files
c6f30 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 ..*/.#include <s
c6f40 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 ys/types.h>.#inc
c6f50 6c 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 lude <sys/stat.h
c6f60 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 >.#include <fcnt
c6f70 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 l.h>.#include <u
c6f80 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 nistd.h>.#includ
c6f90 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 e <sys/time.h>.#
c6fa0 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 include <errno.h
c6fb0 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e >..#if SQLITE_EN
c6fc0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
c6fd0 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 LE.# include <sy
c6fe0 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20 s/ioctl.h>.# if
c6ff0 4f 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e OS_VXWORKS.# in
c7000 63 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 65 clude <semaphore
c7010 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c .h>.# include <
c7020 6c 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65 limits.h>.# else
c7030 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 .# include <sys
c7040 2f 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c /file.h>.# incl
c7050 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 ude <sys/param.h
c7060 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 >.# include <sy
c7070 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 s/mount.h>.# end
c7080 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c if.#endif /* SQL
c7090 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
c70a0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a NG_STYLE */../*.
c70b0 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20 ** If we are to
c70c0 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20 be thread-safe,
c70d0 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72 include the pthr
c70e0 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20 eads header and
c70f0 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 define.** the SQ
c7100 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 LITE_UNIX_THREAD
c7110 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 S macro..*/.#if
c7120 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
c7130 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 E.# define SQLIT
c7140 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31 E_UNIX_THREADS 1
c7150 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 .#endif../*.** D
c7160 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f efault permissio
c7170 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 ns when creating
c7180 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 a new file.*/.#
c7190 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 ifndef SQLITE_DE
c71a0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 FAULT_FILE_PERMI
c71b0 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 SSIONS.# define
c71c0 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 SQLITE_DEFAULT_F
c71d0 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 ILE_PERMISSIONS
c71e0 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 0644.#endif../*.
c71f0 20 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d ** Default perm
c7200 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 issions when cre
c7210 61 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 ating auto proxy
c7220 20 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 dir. */.#ifndef
c7230 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
c7240 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 PROXYDIR_PERMISS
c7250 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 IONS.# define SQ
c7260 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f LITE_DEFAULT_PRO
c7270 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e XYDIR_PERMISSION
c7280 53 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f S 0755.#endif../
c7290 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 *.** Maximum sup
c72a0 70 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 ported path-leng
c72b0 74 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d th..*/.#define M
c72c0 41 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a AX_PATHNAME 512.
c72d0 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20 ./*.** Only set
c72e0 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 66 the lastErrno if
c72f0 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 the error code
c7300 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 20 is a real error
c7310 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f and not .** a no
c7320 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 65 rmal expected re
c7330 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c turn code of SQL
c7340 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 ITE_BUSY or SQLI
c7350 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65 TE_OK.*/.#define
c7360 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 IS_LOCK_ERROR(x
c7370 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45 ) ((x != SQLITE
c7380 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51 _OK) && (x != SQ
c7390 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a LITE_BUSY)).../*
c73a0 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61 .** Sometimes, a
c73b0 66 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e 64 fter a file hand
c73c0 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20 le is closed by
c73d0 53 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c 65 SQLite, the file
c73e0 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63 descriptor.** c
c73f0 61 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 annot be closed
c7400 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 20 immediately. In
c7410 74 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e 73 these cases, ins
c7420 74 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 6f tances of the fo
c7430 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 llowing.** struc
c7440 74 75 72 65 20 61 72 65 20 75 73 65 64 20 74 6f ture are used to
c7450 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20 store the file
c7460 64 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c 65 descriptor while
c7470 20 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a waiting for an.
c7480 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 ** opportunity t
c7490 6f 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 6f o either close o
c74a0 72 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74 r reuse it..*/.t
c74b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e ypedef struct Un
c74c0 69 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 55 ixUnusedFd UnixU
c74d0 6e 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 20 nusedFd;.struct
c74e0 55 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 UnixUnusedFd {.
c74f0 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 int fd;
c7500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
c7510 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
c7520 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 close */. int
c7530 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 flags;
c7540 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 /* Flags t
c7550 68 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 70 his file descrip
c7560 74 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 77 tor was opened w
c7570 69 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 ith */. UnixUnu
c7580 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 sedFd *pNext;
c7590 20 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 /* Next unuse
c75a0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
c75b0 72 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a r on same file *
c75c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 /.};../*.** The
c75d0 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 unixFile structu
c75e0 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f re is subclass o
c75f0 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 f sqlite3_file s
c7600 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75 pecific to the u
c7610 6e 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 nix.** VFS imple
c7620 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 mentations..*/.t
c7630 79 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e ypedef struct un
c7640 69 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b ixFile unixFile;
c7650 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 .struct unixFile
c7660 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f {. sqlite3_io_
c7670 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 methods const *p
c7680 4d 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 Method; /* Alwa
c7690 79 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 ys the first ent
c76a0 72 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 ry */. struct u
c76b0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
c76c0 6e 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f n; /* Info
c76d0 20 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 about all open
c76e0 66 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f fd's on this ino
c76f0 64 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 de */. struct u
c7700 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f nixLockInfo *pLo
c7710 63 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f ck; /* Info
c7720 20 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 about locks on
c7730 74 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 this inode */.
c7740 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 int h;
c7750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7760 20 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 /* The file des
c7770 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 criptor */. int
c7780 20 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 dirfd;
c7790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
c77a0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 File descriptor
c77b0 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f for the directo
c77c0 72 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 ry */. unsigned
c77d0 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 char locktype;
c77e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
c77f0 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c type of lock hel
c7800 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a d on this fd */.
c7810 20 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b int lastErrno;
c7820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c7830 20 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 /* The unix e
c7840 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 rrno from the la
c7850 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a st I/O error */.
c7860 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 void *lockingC
c7870 6f 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 ontext;
c7880 20 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 /* Locking st
c7890 79 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61 yle specific sta
c78a0 74 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 te */. UnixUnus
c78b0 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 edFd *pUnused;
c78c0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d /* Pre-
c78d0 61 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e allocated UnixUn
c78e0 75 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20 usedFd */. int
c78f0 66 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 20 fileFlags;
c7900 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
c7910 4d 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 Miscellanous fla
c7920 67 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 gs */.#if SQLITE
c7930 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
c7940 53 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e STYLE. int open
c7950 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 Flags;
c7960 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
c7970 66 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20 flags specified
c7980 61 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e at open() */.#en
c7990 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 dif.#if SQLITE_T
c79a0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
c79b0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
c79c0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b pthread_t tid;
c79d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c79e0 20 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 /* The thread
c79f0 20 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 that "owns" thi
c7a00 73 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 s unixFile */.#e
c7a10 6e 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f ndif.#if OS_VXWO
c7a20 52 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 RKS. int isDele
c7a30 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 te;
c7a40 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 /* Delete
c7a50 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75 on close if tru
c7a60 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 e */. struct vx
c7a70 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 worksFileId *pId
c7a80 3b 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 ; /* Uniqu
c7a90 65 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e e file ID */.#en
c7aa0 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 dif.#ifndef NDEB
c7ab0 55 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 UG. /* The next
c7ac0 20 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 group of variab
c7ad0 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 les are used to
c7ae0 74 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 track whether or
c7af0 20 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 not the. ** tr
c7b00 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
c7b10 72 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 r in bytes 24-27
c7b20 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c of database fil
c7b30 65 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 es are updated.
c7b40 20 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 ** whenever any
c7b50 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 part of the dat
c7b60 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 abase changes.
c7b70 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 An assertion fau
c7b80 6c 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 lt will. ** occ
c7b90 75 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 ur if a file is
c7ba0 75 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 updated without
c7bb0 61 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 also updating th
c7bc0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 e transaction.
c7bd0 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 ** counter. Thi
c7be0 73 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 s test is made t
c7bf0 6f 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 o avoid new prob
c7c00 6c 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 lems similar to
c7c10 74 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 the. ** one des
c7c20 63 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 cribed by ticket
c7c30 20 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 #3584. . */.
c7c40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 unsigned char tr
c7c50 61 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f ansCntrChng; /
c7c60 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72 * True if the tr
c7c70 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
c7c80 72 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 r changed */. u
c7c90 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 nsigned char dbU
c7ca0 70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a pdate; /*
c7cb0 20 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 True if any par
c7cc0 74 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 t of database fi
c7cd0 6c 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 le changed */.
c7ce0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e unsigned char in
c7cf0 4e 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f NormalWrite; /
c7d00 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e * True if in a n
c7d10 6f 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 ormal write oper
c7d20 61 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a ation */.#endif.
c7d30 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
c7d40 53 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 ST. /* In test
c7d50 6d 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 mode, increase t
c7d60 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 he size of this
c7d70 73 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 structure a bit
c7d80 73 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 so that . ** it
c7d90 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
c7da0 74 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 the struct Crash
c7db0 46 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 File defined in
c7dc0 74 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 test6.c.. */.
c7dd0 63 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 char aPadding[32
c7de0 5d 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a ];.#endif.};../*
c7df0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
c7e00 67 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 g macros define
c7e10 62 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c 65 bits in unixFile
c7e20 2e 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64 .fileFlags.*/.#d
c7e30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f efine SQLITE_WHO
c7e40 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 LE_FILE_LOCKING
c7e50 20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 65 0x0001 /* Use
c7e60 20 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b whole-file lock
c7e70 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e ing */../*.** In
c7e80 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 clude code that
c7e90 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c is common to all
c7ea0 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f os_*.c files.*/
c7eb0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c7ec0 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d Include os_comm
c7ed0 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 on.h in the midd
c7ee0 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 le of os_unix.c
c7ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c7f00 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
c7f10 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 Begin file os_c
c7f20 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
c7f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c7f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
c7f50 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 ./*.** 2004 May
c7f60 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 22.**.** The aut
c7f70 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
c7f80 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
c7f90 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
c7fa0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
c7fb0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
c7fc0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
c7fd0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
c7fe0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
c7ff0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
c8000 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
c8010 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
c8020 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
c8030 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
c8040 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
c8050 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
c8060 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
c8070 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
c8080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c80a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c80b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c80c0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
c80d0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
c80e0 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 macros and a lit
c80f0 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 tle bit of code
c8100 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
c8110 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 o.** all of the
c8120 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 platform-specifi
c8130 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 c files (os_*.c)
c8140 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 and is #include
c8150 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 d into those.**
c8160 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 files..**.** Thi
c8170 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 s file should be
c8180 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 #included by th
c8190 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f e os_*.c files o
c81a0 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 nly. It is not
c81b0 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 a.** general pur
c81c0 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 pose header file
c81d0 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 ..*/.#ifndef _OS
c81e0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 _COMMON_H_.#defi
c81f0 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f ne _OS_COMMON_H_
c8200 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 ../*.** At least
c8210 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 two bugs have s
c8220 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 lipped in becaus
c8230 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 e we changed the
c8240 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a MEMORY_DEBUG.**
c8250 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 macro to SQLITE
c8260 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 _DEBUG and some
c8270 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 older makefiles
c8280 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 have not yet mad
c8290 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e e the.** switch.
c82a0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 The following
c82b0 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 code should catc
c82c0 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 h this problem a
c82d0 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a t compile-time..
c82e0 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 */.#ifdef MEMORY
c82f0 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 _DEBUG.# error "
c8300 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 The MEMORY_DEBUG
c8310 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 macro is obsole
c8320 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f te. Use SQLITE_
c8330 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a DEBUG instead.".
c8340 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 #endif..#ifdef S
c8350 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 QLITE_DEBUG.SQLI
c8360 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
c8370 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 qlite3OSTrace =
c8380 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 0;.#define OSTRA
c8390 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 CE1(X) i
c83a0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c83b0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c83c0 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e Printf(X).#defin
c83d0 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 e OSTRACE2(X,Y)
c83e0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
c83f0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 3OSTrace ) sqlit
c8400 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c e3DebugPrintf(X,
c8410 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Y).#define OSTRA
c8420 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 CE3(X,Y,Z) i
c8430 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
c8440 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
c8450 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 Printf(X,Y,Z).#d
c8460 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 efine OSTRACE4(X
c8470 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 ,Y,Z,A) if( sq
c8480 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 lite3OSTrace ) s
c8490 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
c84a0 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 f(X,Y,Z,A).#defi
c84b0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c ne OSTRACE5(X,Y,
c84c0 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 Z,A,B) if( sqlit
c84d0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 e3OSTrace ) sqli
c84e0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 te3DebugPrintf(X
c84f0 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e ,Y,Z,A,B).#defin
c8500 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a e OSTRACE6(X,Y,Z
c8510 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 ,A,B,C) \. if
c8520 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 (sqlite3OSTrace)
c8530 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
c8540 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 ntf(X,Y,Z,A,B,C)
c8550 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
c8560 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 7(X,Y,Z,A,B,C,D)
c8570 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 \. if(sqlite
c8580 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 3OSTrace) sqlite
c8590 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 3DebugPrintf(X,Y
c85a0 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 ,Z,A,B,C,D).#els
c85b0 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 e.#define OSTRAC
c85c0 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 E1(X).#define OS
c85d0 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 TRACE2(X,Y).#def
c85e0 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 ine OSTRACE3(X,Y
c85f0 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 ,Z).#define OSTR
c8600 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 ACE4(X,Y,Z,A).#d
c8610 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 efine OSTRACE5(X
c8620 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e ,Y,Z,A,B).#defin
c8630 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a e OSTRACE6(X,Y,Z
c8640 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 ,A,B,C).#define
c8650 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 OSTRACE7(X,Y,Z,A
c8660 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a ,B,C,D).#endif..
c8670 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 /*.** Macros for
c8680 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 performance tra
c8690 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 cing. Normally
c86a0 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c turned off. Onl
c86b0 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 y works.** on i4
c86c0 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 86 hardware..*/.
c86d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 #ifdef SQLITE_PE
c86e0 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a RFORMANCE_TRACE.
c86f0 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 ./* .** hwtime.h
c8700 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 contains inline
c8710 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 assembler code
c8720 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 for implementing
c8730 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 .** high-perfor
c8740 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 mance timing rou
c8750 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a tines..*/./*****
c8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 ********* Includ
c8770 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 e hwtime.h in th
c8780 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 e middle of os_c
c8790 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a ommon.h ********
c87a0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
c87b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
c87c0 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a file hwtime.h **
c87d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c87e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c87f0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
c8800 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2008 May 27.**.*
c8810 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 * The author dis
c8820 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 claims copyright
c8830 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 to this source
c8840 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 code. In place
c8850 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f of.** a legal no
c8860 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 tice, here is a
c8870 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 blessing:.**.**
c8880 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f May you do go
c8890 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e od and not evil.
c88a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 .** May you f
c88b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 ind forgiveness
c88c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 for yourself and
c88d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e forgive others.
c88e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 .** May you s
c88f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 hare freely, nev
c8900 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 er taking more t
c8910 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a han you give..**
c8920 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
c8930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
c8970 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 **.** This file
c8980 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 contains inline
c8990 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 asm code for ret
c89a0 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 rieving "high-pe
c89b0 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f rformance".** co
c89c0 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 unters for x86 c
c89d0 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23 69 lass CPUs..*/.#i
c89e0 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f fndef _HWTIME_H_
c89f0 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 .#define _HWTIME
c8a00 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 _H_../*.** The f
c8a10 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 ollowing routine
c8a20 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 only works on p
c8a30 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 entium-class (or
c8a40 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f newer) processo
c8a50 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 rs..** It uses t
c8a60 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 he RDTSC opcode
c8a70 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c to read the cycl
c8a80 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 e count value ou
c8a90 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 t of the.** proc
c8aa0 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e essor and return
c8ab0 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 s that value. T
c8ac0 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 his can be used
c8ad0 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 for high-res.**
c8ae0 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 profiling..*/.#i
c8af0 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 f (defined(__GNU
c8b00 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 C__) || defined(
c8b10 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a _MSC_VER)) && \.
c8b20 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 (defined(i
c8b30 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 386) || defined(
c8b40 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 __i386__) || def
c8b50 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a ined(_M_IX86))..
c8b60 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f #if defined(__
c8b70 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c GNUC__).. __inl
c8b80 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
c8b90 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
c8ba0 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e e(void){. un
c8bb0 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 signed int lo, h
c8bc0 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 i;. __asm__
c8bd0 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 __volatile__ ("r
c8be0 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f dtsc" : "=a" (lo
c8bf0 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 ), "=d" (hi));.
c8c00 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 return (sqli
c8c10 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 te_uint64)hi <<
c8c20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 32 | lo;. }..
c8c30 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d #elif defined(_M
c8c40 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 SC_VER).. __dec
c8c50 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 lspec(naked) __i
c8c60 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e nline sqlite_uin
c8c70 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 t64 __cdecl sqli
c8c80 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b te3Hwtime(void){
c8c90 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 . __asm {.
c8ca0 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 rdtsc.
c8cb0 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 ret ;
c8cc0 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 return value at
c8cd0 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 EDX:EAX. }.
c8ce0 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 }.. #endif..#e
c8cf0 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 lif (defined(__G
c8d00 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 NUC__) && define
c8d10 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a d(__x86_64__))..
c8d20 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c __inline__ sql
c8d30 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 ite_uint64 sqlit
c8d40 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a e3Hwtime(void){.
c8d50 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c unsigned l
c8d60 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f ong val;. _
c8d70 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c _asm__ __volatil
c8d80 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 e__ ("rdtsc" : "
c8d90 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 =A" (val));.
c8da0 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 return val;.
c8db0 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e }. .#elif (defin
c8dc0 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
c8dd0 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 defined(__ppc__)
c8de0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 ).. __inline__
c8df0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
c8e00 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
c8e10 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 ){. unsigne
c8e20 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 d long long retv
c8e30 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e al;. unsign
c8e40 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 ed long junk;.
c8e50 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
c8e60 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 latile__ ("\n\.
c8e70 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 1:
c8e80 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 mftbu %1\n\.
c8e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8ea0 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a mftb %L0\n\.
c8eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8ec0 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a mftbu %0\n\.
c8ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
c8ee0 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c cmpw %0,%1\
c8ef0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 n\.
c8f00 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 bne 1b"
c8f10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
c8f20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 : "=r" (retva
c8f30 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 l), "=r" (junk))
c8f40 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 ;. return r
c8f50 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 etval;. }..#els
c8f60 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 e.. #error Need
c8f70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
c8f80 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 of sqlite3Hwtime
c8f90 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 () for your plat
c8fa0 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a form... /*. **
c8fb0 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 To compile with
c8fc0 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 out implementing
c8fd0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 sqlite3Hwtime()
c8fe0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f for your platfo
c8ff0 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e rm,. ** you can
c9000 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 remove the abov
c9010 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 e #error and use
c9020 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 the following.
c9030 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f ** stub functio
c9040 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 n. You will los
c9050 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 e timing support
c9060 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f for many. ** o
c9070 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 f the debugging
c9080 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c and testing util
c9090 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 ities, but it sh
c90a0 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 ould at. ** lea
c90b0 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 st compile and r
c90c0 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f un.. */.SQLITE_
c90d0 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 PRIVATE sqlite
c90e0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 _uint64 sqlite3H
c90f0 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 wtime(void){ ret
c9100 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e urn ((sqlite_uin
c9110 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 t64)0); }..#endi
c9120 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 f..#endif /* !de
c9130 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f fined(_HWTIME_H_
c9140 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ) */../*********
c9150 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 ***** End of hwt
c9160 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ime.h **********
c9170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9190 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
c91a0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 ***** Continuing
c91b0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f where we left o
c91c0 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e ff in os_common.
c91d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a h **************
c91e0 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 ****/..static sq
c91f0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 lite_uint64 g_st
c9200 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 art;.static sqli
c9210 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 te_uint64 g_elap
c9220 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d sed;.#define TIM
c9230 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 ER_START g
c9240 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 _start=sqlite3Hw
c9250 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 time().#define T
c9260 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 IMER_END
c9270 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 g_elapsed=sqlit
c9280 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 e3Hwtime()-g_sta
c9290 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 rt.#define TIMER
c92a0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 _ELAPSED g_e
c92b0 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 lapsed.#else.#de
c92c0 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 fine TIMER_START
c92d0 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 .#define TIMER_E
c92e0 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 ND.#define TIMER
c92f0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 _ELAPSED ((s
c9300 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a qlite_uint64)0).
c9310 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 #endif../*.** If
c9320 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 we compile with
c9330 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 the SQLITE_TEST
c9340 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e macro set, then
c9350 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 the following b
c9360 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 lock.** of code
c9370 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 will give us the
c9380 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 ability to simu
c9390 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 late a disk I/O
c93a0 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 error. This.**
c93b0 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 is used for test
c93c0 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f ing the I/O reco
c93d0 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 very logic..*/.#
c93e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 ifdef SQLITE_TES
c93f0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 T.SQLITE_API int
c9400 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c9410 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 r_hit = 0;
c9420 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e /* Total n
c9430 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 umber of I/O Err
c9440 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ors */.SQLITE_AP
c9450 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
c9460 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d _error_hardhit =
c9470 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 0; /* Nu
c9480 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 mber of non-beni
c9490 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c gn errors */.SQL
c94a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
c94b0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e te3_io_error_pen
c94c0 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 ding = 0;
c94d0 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 /* Count down t
c94e0 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f o first I/O erro
c94f0 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 r */.SQLITE_API
c9500 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 int sqlite3_io_e
c9510 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 rror_persist = 0
c9520 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 ; /* True
c9530 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 if I/O errors p
c9540 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 ersist */.SQLITE
c9550 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
c9560 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e _io_error_benign
c9570 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
c9580 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 True if errors
c9590 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 are benign */.SQ
c95a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
c95b0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
c95c0 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 nding = 0;.SQLIT
c95d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c95e0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 3_diskfull = 0;.
c95f0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
c9600 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 IOErrorBenign(X)
c9610 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c9620 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 r_benign=(X).#de
c9630 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 fine SimulateIOE
c9640 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 rror(CODE) \.
c9650 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f if( (sqlite3_io_
c9660 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 error_persist &&
c9670 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
c9680 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 r_hit) \.
c9690 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 || sqlite3_io_er
c96a0 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d ror_pending-- ==
c96b0 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 1 ) \.
c96c0 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f { local_io
c96d0 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 err(); CODE; }.s
c96e0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c tatic void local
c96f0 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 _ioerr(){. IOTR
c9700 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 ACE(("IOERR\n"))
c9710 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 ;. sqlite3_io_e
c9720 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 rror_hit++;. if
c9730 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 ( !sqlite3_io_er
c9740 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c ror_benign ) sql
c9750 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 ite3_io_error_ha
c9760 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 rdhit++;.}.#defi
c9770 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 ne SimulateDiskf
c9780 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c ullError(CODE) \
c9790 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f . if( sqlite3_
c97a0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 diskfull_pending
c97b0 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 ){ \. if( s
c97c0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f qlite3_diskfull_
c97d0 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 pending == 1 ){
c97e0 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 \. local_i
c97f0 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 oerr(); \.
c9800 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c sqlite3_diskful
c9810 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 l = 1; \.
c9820 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 sqlite3_io_error
c9830 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 _hit = 1; \.
c9840 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 CODE; \.
c9850 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 }else{ \.
c9860 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c sqlite3_diskfull
c9870 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 _pending--; \.
c9880 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 } \. }.#els
c9890 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 e.#define Simula
c98a0 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
c98b0 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c X).#define Simul
c98c0 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 ateIOError(A).#d
c98d0 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 efine SimulateDi
c98e0 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 skfullError(A).#
c98f0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 endif../*.** Whe
c9900 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 n testing, keep
c9910 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e a count of the n
c9920 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 umber of open fi
c9930 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 les..*/.#ifdef S
c9940 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 QLITE_TEST.SQLIT
c9950 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
c9960 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 3_open_file_coun
c9970 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f t = 0;.#define O
c9980 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 penCounter(X) s
c9990 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 qlite3_open_file
c99a0 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 _count+=(X).#els
c99b0 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f e.#define OpenCo
c99c0 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a unter(X).#endif.
c99d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 .#endif /* !defi
c99e0 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 ned(_OS_COMMON_H
c99f0 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a _) */../********
c9a00 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 ****** End of os
c9a10 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a _common.h ******
c9a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
c9a40 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
c9a50 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e ****** Continuin
c9a60 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 g where we left
c9a70 6f 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 off in os_unix.c
c9a80 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
c9a90 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 *****/../*.** De
c9aa0 66 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 fine various mac
c9ab0 72 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 ros that are mis
c9ac0 73 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 sing from some s
c9ad0 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 ystems..*/.#ifnd
c9ae0 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 ef O_LARGEFILE.#
c9af0 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 define O_LARGEF
c9b00 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 ILE 0.#endif.#if
c9b10 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 def SQLITE_DISAB
c9b20 4c 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f LE_LFS.# undef O
c9b30 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 _LARGEFILE.# def
c9b40 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 ine O_LARGEFILE
c9b50 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 0.#endif.#ifndef
c9b60 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 O_NOFOLLOW.# de
c9b70 66 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 fine O_NOFOLLOW
c9b80 30 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 0.#endif.#ifndef
c9b90 20 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 O_BINARY.# defi
c9ba0 6e 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 ne O_BINARY 0.#e
c9bb0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
c9bc0 44 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 DJGPP compiler e
c9bd0 6e 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 nvironment looks
c9be0 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 mostly like Uni
c9bf0 78 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 x, but it.** lac
c9c00 6b 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 ks the fcntl() s
c9c10 79 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 ystem call. So
c9c20 72 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 redefine fcntl()
c9c30 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 to be something
c9c40 0a 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 .** that always
c9c50 73 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 succeeds. This
c9c60 6d 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 means that locki
c9c70 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 ng does not occu
c9c80 72 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 r under.** DJGPP
c9c90 2e 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 . But it is DOS
c9ca0 20 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 - what did you
c9cb0 65 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 expect?.*/.#ifde
c9cc0 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 f __DJGPP__.# de
c9cd0 66 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 fine fcntl(A,B,C
c9ce0 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ) 0.#endif../*.*
c9cf0 2a 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d * The threadid m
c9d00 61 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f acro resolves to
c9d10 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f the thread-id o
c9d20 72 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f r to 0. Used fo
c9d30 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 r.** testing and
c9d40 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e debugging only.
c9d50 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
c9d60 48 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e HREADSAFE.#defin
c9d70 65 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 e threadid pthre
c9d80 61 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a ad_self().#else.
c9d90 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 #define threadid
c9da0 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 0.#endif.../*.*
c9db0 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f * Helper functio
c9dc0 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 ns to obtain and
c9dd0 20 72 65 6c 69 6e 71 75 69 73 68 20 74 68 65 20 relinquish the
c9de0 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 global mutex. Th
c9df0 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 e.** global mute
c9e00 78 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f x is used to pro
c9e10 74 65 63 74 20 74 68 65 20 75 6e 69 78 4f 70 65 tect the unixOpe
c9e20 6e 43 6e 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e nCnt, unixLockIn
c9e30 66 6f 20 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b fo and.** vxwork
c9e40 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 sFileId objects
c9e50 75 73 65 64 20 62 79 20 74 68 69 73 20 66 69 6c used by this fil
c9e60 65 2c 20 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 e, all of which
c9e70 6d 61 79 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 may be .** share
c9e80 64 20 62 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 d by multiple th
c9e90 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e reads..**.** Fun
c9ea0 63 74 69 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 ction unixMutexH
c9eb0 65 6c 64 28 29 20 69 73 20 75 73 65 64 20 74 6f eld() is used to
c9ec0 20 61 73 73 65 72 74 28 29 20 74 68 61 74 20 74 assert() that t
c9ed0 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 he global mutex
c9ee0 0a 2a 2a 20 69 73 20 68 65 6c 64 20 77 68 65 6e .** is held when
c9ef0 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 required. This
c9f00 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 function is only
c9f10 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 used as part of
c9f20 20 61 73 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 assert() .** st
c9f30 61 74 65 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a atements. e.g..*
c9f40 2a 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 *.** unixEnter
c9f50 4d 75 74 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 Mutex().** a
c9f60 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 ssert( unixMutex
c9f70 48 65 6c 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 Held() );.** u
c9f80 6e 69 78 45 6e 74 65 72 4c 65 61 76 65 28 29 0a nixEnterLeave().
c9f90 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 */.static void u
c9fa0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 76 6f nixEnterMutex(vo
c9fb0 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d id){. sqlite3_m
c9fc0 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 utex_enter(sqlit
c9fd0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
c9fe0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
c9ff0 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 _MASTER));.}.sta
ca000 74 69 63 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 tic void unixLea
ca010 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 veMutex(void){.
ca020 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
ca030 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 eave(sqlite3Mute
ca040 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 xAlloc(SQLITE_MU
ca050 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
ca060 52 29 29 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 R));.}.#ifdef SQ
ca070 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 LITE_DEBUG.stati
ca080 63 20 69 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 c int unixMutexH
ca090 65 6c 64 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 eld(void) {. re
ca0a0 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 turn sqlite3_mut
ca0b0 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d ex_held(sqlite3M
ca0c0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 utexAlloc(SQLITE
ca0d0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 _MUTEX_STATIC_MA
ca0e0 53 54 45 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 STER));.}.#endif
ca0f0 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 ...#ifdef SQLITE
ca100 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c _DEBUG./*.** Hel
ca110 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 per function for
ca120 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 printing out tr
ca130 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ace information
ca140 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a from debugging.*
ca150 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68 69 73 * binaries. This
ca160 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 74 72 returns the str
ca170 69 6e 67 20 72 65 70 72 65 73 65 74 61 74 69 6f ing represetatio
ca180 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 n of the supplie
ca190 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 d.** integer loc
ca1a0 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 k-type..*/.stati
ca1b0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f c const char *lo
ca1c0 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c cktypeName(int l
ca1d0 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77 69 74 ocktype){. swit
ca1e0 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a ch( locktype ){.
ca1f0 20 20 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b case NO_LOCK
ca200 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b : return "NONE";
ca210 0a 20 20 20 20 63 61 73 65 20 53 48 41 52 45 44 . case SHARED
ca220 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 _LOCK: return "S
ca230 48 41 52 45 44 22 3b 0a 20 20 20 20 63 61 73 65 HARED";. case
ca240 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 RESERVED_LOCK:
ca250 72 65 74 75 72 6e 20 22 52 45 53 45 52 56 45 44 return "RESERVED
ca260 22 3b 0a 20 20 20 20 63 61 73 65 20 50 45 4e 44 ";. case PEND
ca270 49 4e 47 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e ING_LOCK: return
ca280 20 22 50 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 "PENDING";.
ca290 63 61 73 65 20 45 58 43 4c 55 53 49 56 45 5f 4c case EXCLUSIVE_L
ca2a0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 45 58 43 OCK: return "EXC
ca2b0 4c 55 53 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 LUSIVE";. }. r
ca2c0 65 74 75 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d eturn "ERROR";.}
ca2d0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 .#endif..#ifdef
ca2e0 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 SQLITE_LOCK_TRAC
ca2f0 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 E./*.** Print ou
ca300 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 t information ab
ca310 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 out all locking
ca320 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a operations..**.*
ca330 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
ca340 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62 s used for troub
ca350 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 leshooting locks
ca360 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65 on multithreade
ca370 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 d.** platforms.
ca380 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 Enable by compi
ca390 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44 ling with the -D
ca3a0 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 SQLITE_LOCK_TRAC
ca3b0 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e E.** command-lin
ca3c0 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 e option on the
ca3d0 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20 compiler. This
ca3e0 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 code is normally
ca3f0 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a .** turned off..
ca400 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f */.static int lo
ca410 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20 ckTrace(int fd,
ca420 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66 int op, struct f
ca430 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 lock *p){. char
ca440 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 *zOpName, *zTyp
ca450 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e e;. int s;. in
ca460 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 t savedErrno;.
ca470 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 if( op==F_GETLK
ca480 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d ){. zOpName =
ca490 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 "GETLK";. }els
ca4a0 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c e if( op==F_SETL
ca4b0 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 K ){. zOpName
ca4c0 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 = "SETLK";. }e
ca4d0 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e lse{. s = fcn
ca4e0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 tl(fd, op, p);.
ca4f0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 sqlite3DebugP
ca500 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b rintf("fcntl unk
ca510 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 nown %d %d %d\n"
ca520 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 , fd, op, s);.
ca530 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a return s;. }.
ca540 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d if( p->l_type=
ca550 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_RDLCK ){.
ca560 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b zType = "RDLCK";
ca570 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
ca580 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 l_type==F_WRLCK
ca590 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 ){. zType = "
ca5a0 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 WRLCK";. }else
ca5b0 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 if( p->l_type==F
ca5c0 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 _UNLCK ){. zT
ca5d0 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 ype = "UNLCK";.
ca5e0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
ca5f0 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 rt( 0 );. }. a
ca600 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e ssert( p->l_when
ca610 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a ce==SEEK_SET );.
ca620 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 s = fcntl(fd,
ca630 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45 op, p);. savedE
ca640 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
ca650 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
ca660 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20 tf("fcntl %d %d
ca670 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25 %s %s %d %d %d %
ca680 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61 d\n",. threa
ca690 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 did, fd, zOpName
ca6a0 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d , zType, (int)p-
ca6b0 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70 >l_start, (int)p
ca6c0 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 ->l_len,. (i
ca6d0 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b nt)p->l_pid, s);
ca6e0 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 . if( s==(-1) &
ca6f0 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 & op==F_SETLK &&
ca700 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 (p->l_type==F_R
ca710 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 DLCK || p->l_typ
ca720 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 e==F_WRLCK) ){.
ca730 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 struct flock
ca740 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b l2;. l2 = *p;
ca750 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 . fcntl(fd, F
ca760 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 _GETLK, &l2);.
ca770 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d if( l2.l_type=
ca780 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_RDLCK ){.
ca790 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b zType = "RDLCK
ca7a0 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 ";. }else if(
ca7b0 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 l2.l_type==F_WR
ca7c0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 LCK ){. zTy
ca7d0 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 pe = "WRLCK";.
ca7e0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c }else if( l2.l
ca7f0 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 _type==F_UNLCK )
ca800 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 {. zType =
ca810 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c "UNLCK";. }el
ca820 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
ca830 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 ( 0 );. }.
ca840 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
ca850 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 ntf("fcntl-failu
ca860 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 re-reason: %s %d
ca870 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 %d %d\n",.
ca880 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 zType, (int)l2
ca890 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c .l_start, (int)l
ca8a0 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2.l_len, (int)l2
ca8b0 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 .l_pid);. }. e
ca8c0 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e rrno = savedErrn
ca8d0 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d o;. return s;.}
ca8e0 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c .#define fcntl l
ca8f0 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20 ockTrace.#endif
ca900 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 /* SQLITE_LOCK_T
ca910 52 41 43 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a RACE */..../*.**
ca920 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 This routine tr
ca930 61 6e 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 anslates a stand
ca940 61 72 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 ard POSIX errno
ca950 63 6f 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 code into someth
ca960 69 6e 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f ing.** useful to
ca970 20 74 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 the clients of
ca980 74 68 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 the sqlite3 func
ca990 74 69 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 tions. Specific
ca9a0 61 6c 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 ally, it is.** i
ca9b0 6e 74 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 ntended to trans
ca9c0 6c 61 74 65 20 61 20 76 61 72 69 65 74 79 20 6f late a variety o
ca9d0 66 20 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 f "try again" er
ca9e0 72 6f 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 rors into SQLITE
ca9f0 5f 42 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 _BUSY.** and a v
caa00 61 72 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 ariety of "pleas
caa10 65 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 e close the file
caa20 20 64 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 descriptor NOW"
caa30 20 65 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a errors into .**
caa40 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a SQLITE_IOERR.**
caa50 20 0a 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 .** Errors duri
caa60 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f ng initializatio
caa70 6e 20 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 n of locks, or f
caa80 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f ile system suppo
caa90 72 74 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a rt for locks,.**
caaa0 20 73 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 should handle E
caab0 4e 4f 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 NOLCK, ENOTSUP,
caac0 45 4f 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 EOPNOTSUPP separ
caad0 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 ately..*/.static
caae0 20 69 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 int sqliteError
caaf0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 FromPosixError(i
cab00 6e 74 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 nt posixError, i
cab10 6e 74 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 nt sqliteIOErr)
cab20 7b 0a 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 {. switch (posi
cab30 78 45 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 xError) {. case
cab40 20 30 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 0: . return
cab50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a SQLITE_OK;. .
cab60 20 20 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 case EAGAIN:.
cab70 20 63 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a case ETIMEDOUT:
cab80 0a 20 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 . case EBUSY:.
cab90 20 63 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 case EINTR:. c
caba0 61 73 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 ase ENOLCK: .
cabb0 20 20 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 /* random NFS
cabc0 72 65 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c retry error, unl
cabd0 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 ess during file
cabe0 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a system support .
cabf0 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 * introspec
cac00 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 tion, in which i
cac10 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 t actually means
cac20 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f what it says */
cac30 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
cac40 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 TE_BUSY;. .
cac50 63 61 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 case EACCES: .
cac60 20 20 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c /* EACCES is l
cac70 69 6b 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e ike EAGAIN durin
cac80 67 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 g locking operat
cac90 69 6f 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e ions, but not an
caca0 79 20 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 y other time*/.
cacb0 20 20 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f if( (sqliteIO
cacc0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f Err == SQLITE_IO
cacd0 45 52 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 ERR_LOCK) || ..(
cace0 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 sqliteIOErr == S
cacf0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f QLITE_IOERR_UNLO
cad00 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 CK) || ..(sqlite
cad10 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f IOErr == SQLITE_
cad20 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c IOERR_RDLOCK) ||
cad30 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d ..(sqliteIOErr =
cad40 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 = SQLITE_IOERR_C
cad50 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
cad60 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
cad70 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 n SQLITE_BUSY;.
cad80 20 20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 }. /* else
cad90 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f fall through */
cada0 0a 20 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a . case EPERM: .
cadb0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
cadc0 45 5f 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 E_PERM;. . c
cadd0 61 73 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 ase EDEADLK:.
cade0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
cadf0 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 OERR_BLOCKED;.
cae00 20 20 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 .#if EOPNOTSUP
cae10 50 21 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 P!=ENOTSUP. cas
cae20 65 20 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 e EOPNOTSUPP: .
cae30 20 20 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 /* something
cae40 77 65 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 went terribly aw
cae50 72 79 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e ry, unless durin
cae60 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 g file system su
cae70 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e pport . * in
cae80 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 trospection, in
cae90 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c which it actuall
caea0 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 y means what it
caeb0 73 61 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 says */.#endif.#
caec0 69 66 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 ifdef ENOTSUP.
caed0 63 61 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 case ENOTSUP: .
caee0 20 20 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 /* invalid fd
caef0 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 , unless during
caf00 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 file system supp
caf10 6f 72 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f ort introspectio
caf20 6e 2c 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 n, in which .
caf30 20 20 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 * it actually
caf40 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 means what it sa
caf50 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 ys */.#endif. c
caf60 61 73 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 ase EIO:. case
caf70 45 42 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 EBADF:. case EI
caf80 4e 56 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f NVAL:. case ENO
caf90 54 43 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e TCONN:. case EN
cafa0 4f 44 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 ODEV:. case ENX
cafb0 49 4f 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e IO:. case ENOEN
cafc0 54 3a 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 T:. case ESTALE
cafd0 3a 0a 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a :. case ENOSYS:
cafe0 0a 20 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 . /* these sh
caff0 6f 75 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 ould force the c
cb000 6c 69 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 lient to close t
cb010 68 65 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f he file and reco
cb020 6e 6e 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 nnect */. .
cb030 64 65 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 default: . re
cb040 74 75 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 turn sqliteIOErr
cb050 3b 0a 20 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a ;. }.}..../****
cb060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cb0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
cb0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
cb0c0 67 69 6e 20 55 6e 69 71 75 65 20 46 69 6c 65 20 gin Unique File
cb0d0 49 44 20 55 74 69 6c 69 74 79 20 55 73 65 64 20 ID Utility Used
cb0e0 42 79 20 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a By VxWorks *****
cb0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
cb100 20 4f 6e 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e On most version
cb110 73 20 6f 66 20 75 6e 69 78 2c 20 77 65 20 63 61 s of unix, we ca
cb120 6e 20 67 65 74 20 61 20 75 6e 69 71 75 65 20 49 n get a unique I
cb130 44 20 66 6f 72 20 61 20 66 69 6c 65 20 62 79 20 D for a file by
cb140 63 6f 6e 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a concatenating.**
cb150 20 74 68 65 20 64 65 76 69 63 65 20 6e 75 6d 62 the device numb
cb160 65 72 20 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 er and the inode
cb170 20 6e 75 6d 62 65 72 2e 20 20 42 75 74 20 74 68 number. But th
cb180 69 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b is does not work
cb190 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 on VxWorks..**
cb1a0 4f 6e 20 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e On VxWorks, a un
cb1b0 69 71 75 65 20 66 69 6c 65 20 69 64 20 6d 75 73 ique file id mus
cb1c0 74 20 62 65 20 62 61 73 65 64 20 6f 6e 20 74 68 t be based on th
cb1d0 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 e canonical file
cb1e0 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f name..**.** A po
cb1f0 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 inter to an inst
cb200 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c ance of the foll
cb210 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 owing structure
cb220 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 61 can be used as a
cb230 0a 2a 2a 20 75 6e 69 71 75 65 20 66 69 6c 65 20 .** unique file
cb240 49 44 20 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 ID in VxWorks.
cb250 45 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 Each instance of
cb260 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
cb270 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f contains.** a co
cb280 70 79 20 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 py of the canoni
cb290 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 cal filename. T
cb2a0 68 65 72 65 20 69 73 20 61 6c 73 6f 20 61 20 72 here is also a r
cb2b0 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 eference count.
cb2c0 20 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75 .** The structu
cb2d0 72 65 20 69 73 20 72 65 63 6c 61 69 6d 65 64 20 re is reclaimed
cb2e0 77 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 when the number
cb2f0 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 of pointers to i
cb300 74 20 64 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 t drops to.** ze
cb310 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 ro..**.** There
cb320 61 72 65 20 6e 65 76 65 72 20 76 65 72 79 20 6d are never very m
cb330 61 6e 79 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 any files open a
cb340 74 20 6f 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c t one time and l
cb350 6f 6f 6b 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a ookups are not.*
cb360 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d * a performance-
cb370 63 72 69 74 69 63 61 6c 20 70 61 74 68 2c 20 73 critical path, s
cb380 6f 20 69 74 20 69 73 20 73 75 66 66 69 63 69 65 o it is sufficie
cb390 6e 74 20 74 6f 20 70 75 74 20 74 68 65 73 65 0a nt to put these.
cb3a0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 6f 6e ** structures on
cb3b0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a a linked list..
cb3c0 2a 2f 0a 73 74 72 75 63 74 20 76 78 77 6f 72 6b */.struct vxwork
cb3d0 73 46 69 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 sFileId {. stru
cb3e0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cb3f0 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 *pNext; /* Nex
cb400 74 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 t in a list of t
cb410 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 hem all */. int
cb420 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 nRef;
cb430 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
cb440 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 mber of referenc
cb450 65 73 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a es to this one *
cb460 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 /. int nName;
cb470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cb480 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 /* Length of t
cb490 68 65 20 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d he zCanonicalNam
cb4a0 65 5b 5d 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 e[] string */.
cb4b0 63 68 61 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c char *zCanonical
cb4c0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f 2a Name; /*
cb4d0 20 43 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e Canonical filen
cb4e0 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f ame */.};..#if O
cb4f0 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a S_VXWORKS./* .**
cb500 20 41 6c 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 All unique file
cb510 6e 61 6d 65 73 20 61 72 65 20 68 65 6c 64 20 6f names are held o
cb520 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 n a linked list
cb530 68 65 61 64 65 64 20 62 79 20 74 68 69 73 0a 2a headed by this.*
cb540 2a 20 76 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 * variable:.*/.s
cb550 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77 tatic struct vxw
cb560 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f orksFileId *vxwo
cb570 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b rksFileList = 0;
cb580 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 ../*.** Simplify
cb590 20 61 20 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f a filename into
cb5a0 20 69 74 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 its canonical f
cb5b0 6f 72 6d 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 orm.** by making
cb5c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 the following c
cb5d0 68 61 6e 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a hanges:.**.** *
cb5e0 20 72 65 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 removing any tr
cb5f0 61 69 6c 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 ailing and dupli
cb600 63 61 74 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e cate /.** * con
cb610 76 65 72 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 vert /./ into ju
cb620 73 74 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 st /.** * conve
cb630 72 74 20 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 rt /A/../ where
cb640 41 20 69 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 A is any simple
cb650 6e 61 6d 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f name into just /
cb660 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 .**.** Changes a
cb670 72 65 20 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 re made in-place
cb680 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 65 . Return the ne
cb690 77 20 6e 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a w name length..*
cb6a0 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 *.** The origina
cb6b0 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e l filename is in
cb6c0 20 7a 5b 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 z[0..n-1]. Ret
cb6d0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
cb6e0 66 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 f.** characters
cb6f0 69 6e 20 74 68 65 20 73 69 6d 70 6c 69 66 69 65 in the simplifie
cb700 64 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 d name..*/.stati
cb710 63 20 69 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d c int vxworksSim
cb720 70 6c 69 66 79 4e 61 6d 65 28 63 68 61 72 20 2a plifyName(char *
cb730 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 z, int n){. int
cb740 20 69 2c 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 i, j;. while(
cb750 6e 3e 31 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 n>1 && z[n-1]=='
cb760 2f 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 /' ){ n--; }. f
cb770 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 or(i=j=0; i<n; i
cb780 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 ++){. if( z[i
cb790 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 ]=='/' ){.
cb7a0 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 if( z[i+1]=='/'
cb7b0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
cb7c0 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e if( z[i+1]=='.
cb7d0 27 20 26 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b ' && i+2<n && z[
cb7e0 69 2b 32 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 i+2]=='/' ){.
cb7f0 20 20 20 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 i += 1;.
cb800 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
cb810 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
cb820 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 z[i+1]=='.' &&
cb830 69 2b 33 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d i+3<n && z[i+2]=
cb840 3d 27 2e 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d ='.' && z[i+3]==
cb850 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20 77 '/' ){. w
cb860 68 69 6c 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a hile( j>0 && z[j
cb870 2d 31 5d 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b -1]!='/' ){ j--;
cb880 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a }. if( j
cb890 3e 30 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 >0 ){ j--; }.
cb8a0 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 i += 2;.
cb8b0 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
cb8c0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
cb8d0 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a z[j++] = z[i];.
cb8e0 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a }. z[j] = 0;.
cb8f0 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f return j;.}../
cb900 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 *.** Find a uniq
cb910 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 74 ue file ID for t
cb920 68 65 20 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 he given absolut
cb930 65 20 70 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 e pathname. Ret
cb940 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 urn.** a pointer
cb950 20 74 6f 20 74 68 65 20 76 78 77 6f 72 6b 73 46 to the vxworksF
cb960 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 ileId object. T
cb970 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73 20 74 his pointer is t
cb980 68 65 20 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c he unique.** fil
cb990 65 20 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 e ID..**.** The
cb9a0 6e 52 65 66 20 66 69 65 6c 64 20 6f 66 20 74 68 nRef field of th
cb9b0 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 e vxworksFileId
cb9c0 6f 62 6a 65 63 74 20 69 73 20 69 6e 63 72 65 6d object is increm
cb9d0 65 6e 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 ented before.**
cb9e0 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 72 65 the object is re
cb9f0 74 75 72 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 turned. A new v
cba00 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a xworksFileId obj
cba10 65 63 74 20 69 73 20 63 72 65 61 74 65 64 0a 2a ect is created.*
cba20 2a 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74 * and added to t
cba30 68 65 20 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 he global list i
cba40 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a f necessary..**.
cba50 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 ** If a memory a
cba60 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 llocation error
cba70 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e occurs, return N
cba80 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 ULL..*/.static s
cba90 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
cbaa0 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 eId *vxworksFind
cbab0 46 69 6c 65 49 64 28 63 6f 6e 73 74 20 63 68 61 FileId(const cha
cbac0 72 20 2a 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 r *zAbsoluteName
cbad0 29 7b 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f ){. struct vxwo
cbae0 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b rksFileId *pNew;
cbaf0 20 20 20 20 20 20 20 20 20 2f 2a 20 73 65 61 72 /* sear
cbb00 63 68 20 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 ch key and new f
cbb10 69 6c 65 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 ile ID */. stru
cbb20 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 ct vxworksFileId
cbb30 20 2a 70 43 61 6e 64 69 64 61 74 65 3b 20 20 20 *pCandidate;
cbb40 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f /* For looping o
cbb50 76 65 72 20 65 78 69 73 74 69 6e 67 20 66 69 6c ver existing fil
cbb60 65 20 49 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e e IDs */. int n
cbb70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cbb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
cbb90 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 * Length of zAbs
cbba0 6f 6c 75 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 oluteName string
cbbb0 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 7a */.. assert( z
cbbc0 41 62 73 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d AbsoluteName[0]=
cbbd0 3d 27 2f 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 ='/' );. n = (i
cbbe0 6e 74 29 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c nt)strlen(zAbsol
cbbf0 75 74 65 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 uteName);. pNew
cbc00 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f = sqlite3_mallo
cbc10 63 28 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 c( sizeof(*pNew)
cbc20 20 2b 20 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 + (n+1) );. if
cbc30 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 ( pNew==0 ) retu
cbc40 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 rn 0;. pNew->zC
cbc50 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 anonicalName = (
cbc60 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a char*)&pNew[1];.
cbc70 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a memcpy(pNew->z
cbc80 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a CanonicalName, z
cbc90 41 62 73 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b AbsoluteName, n+
cbca0 31 29 3b 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 1);. n = vxwork
cbcb0 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e sSimplifyName(pN
cbcc0 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 ew->zCanonicalNa
cbcd0 6d 65 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 me, n);.. /* Se
cbce0 61 72 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73 arch for an exis
cbcf0 74 69 6e 67 20 65 6e 74 72 79 20 74 68 61 74 20 ting entry that
cbd00 6d 61 74 63 68 69 6e 67 20 74 68 65 20 63 61 6e matching the can
cbd10 6f 6e 69 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a onical name.. *
cbd20 2a 20 49 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 * If found, incr
cbd30 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 ement the refere
cbd40 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 nce count and re
cbd50 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
cbd60 6f 0a 20 20 2a 2a 20 74 68 65 20 65 78 69 73 74 o. ** the exist
cbd70 69 6e 67 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a ing file ID.. *
cbd80 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 /. unixEnterMut
cbd90 65 78 28 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e ex();. for(pCan
cbda0 64 69 64 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 didate=vxworksFi
cbdb0 6c 65 4c 69 73 74 3b 20 70 43 61 6e 64 69 64 61 leList; pCandida
cbdc0 74 65 3b 20 70 43 61 6e 64 69 64 61 74 65 3d 70 te; pCandidate=p
cbdd0 43 61 6e 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 Candidate->pNext
cbde0 29 7b 0a 20 20 20 20 69 66 28 20 70 43 61 6e 64 ){. if( pCand
cbdf0 69 64 61 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 idate->nName==n
cbe00 0a 20 20 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 . && memcmp(
cbe10 70 43 61 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e pCandidate->zCan
cbe20 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 onicalName, pNew
cbe30 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 ->zCanonicalName
cbe40 2c 20 6e 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 , n)==0. ){.
cbe50 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
cbe60 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 ee(pNew);.
cbe70 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 pCandidate->nRe
cbe80 66 2b 2b 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 f++;. unix
cbe90 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
cbea0 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 61 6e return pCan
cbeb0 64 69 64 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 didate;. }.
cbec0 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 }.. /* No match
cbed0 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 was found. We
cbee0 77 69 6c 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 will make a new
cbef0 66 69 6c 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 file ID */. pNe
cbf00 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 w->nRef = 1;. p
cbf10 4e 65 77 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a New->nName = n;.
cbf20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 pNew->pNext =
cbf30 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b vxworksFileList;
cbf40 0a 20 20 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 . vxworksFileLi
cbf50 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 st = pNew;. uni
cbf60 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
cbf70 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
cbf80 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 ./*.** Decrement
cbf90 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 the reference c
cbfa0 6f 75 6e 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b ount on a vxwork
cbfb0 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 sFileId object.
cbfc0 20 46 72 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a Free.** the obj
cbfd0 65 63 74 20 77 68 65 6e 20 74 68 65 20 72 65 66 ect when the ref
cbfe0 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 erence count rea
cbff0 63 68 65 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 ches zero..*/.st
cc000 61 74 69 63 20 76 6f 69 64 20 76 78 77 6f 72 6b atic void vxwork
cc010 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 73 sReleaseFileId(s
cc020 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c truct vxworksFil
cc030 65 49 64 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 eId *pId){. uni
cc040 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
cc050 20 61 73 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 assert( pId->nR
cc060 65 66 3e 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e ef>0 );. pId->n
cc070 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 Ref--;. if( pId
cc080 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 ->nRef==0 ){.
cc090 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 struct vxworksF
cc0a0 69 6c 65 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 ileId **pp;.
cc0b0 66 6f 72 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 for(pp=&vxworksF
cc0c0 69 6c 65 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 ileList; *pp &&
cc0d0 2a 70 70 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 *pp!=pId; pp = &
cc0e0 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b ((*pp)->pNext)){
cc0f0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 }. assert( *p
cc100 70 3d 3d 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 p==pId );. *p
cc110 70 20 3d 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a p = pId->pNext;.
cc120 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
cc130 28 70 49 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 (pId);. }. uni
cc140 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d xLeaveMutex();.}
cc150 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 .#endif /* OS_VX
cc160 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a WORKS */./******
cc170 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
cc180 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 Unique File ID
cc190 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 Utility Used By
cc1a0 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a VxWorks ********
cc1b0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
cc1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc200 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a *******/.../****
cc210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
cc260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc270 2a 2a 2a 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 ****** Posix Adv
cc280 69 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a isory Locking **
cc290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
cc2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a **********.**.**
cc2b0 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 POSIX advisory
cc2c0 6c 6f 63 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e locks are broken
cc2d0 20 62 79 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 by design. ANS
cc2e0 49 20 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 I STD 1003.1 (19
cc2f0 39 36 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 96).** section 6
cc300 2e 35 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 .5.2.2 lines 483
cc310 20 74 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 through 490 spe
cc320 63 69 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 cify that when a
cc330 20 70 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 process.** sets
cc340 20 6f 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 or clears a loc
cc350 6b 2c 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f k, that operatio
cc360 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 n overrides any
cc370 70 72 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a prior locks set.
cc380 2a 2a 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 ** by the same p
cc390 72 6f 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 rocess. It does
cc3a0 20 6e 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 not explicitly
cc3b0 73 61 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 say so, but this
cc3c0 20 69 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 implies.** that
cc3d0 20 69 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f it overrides lo
cc3e0 63 6b 73 20 73 65 74 20 62 79 20 74 68 65 20 73 cks set by the s
cc3f0 61 6d 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e ame process usin
cc400 67 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a g a different.**
cc410 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
cc420 2e 20 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 . Consider this
cc430 20 74 65 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a test case:.**.*
cc440 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 31 20 * int fd1
cc450 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 = open("./file1"
cc460 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 , O_RDWR|O_CREAT
cc470 2c 20 30 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 , 0644);.**
cc480 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e int fd2 = open
cc490 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 ("./file2", O_RD
cc4a0 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 WR|O_CREAT, 0644
cc4b0 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 );.**.** Suppose
cc4c0 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 ./file1 and ./f
cc4d0 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79 20 ile2 are really
cc4e0 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28 62 the same file (b
cc4f0 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 ecause.** one is
cc500 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62 6f a hard or symbo
cc510 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 lic link to the
cc520 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 20 79 other) then if y
cc530 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 ou set.** an exc
cc540 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 lusive lock on f
cc550 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f 20 d1, then try to
cc560 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76 65 get an exclusive
cc570 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c lock.** on fd2,
cc580 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f it works. I wo
cc590 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 74 65 uld have expecte
cc5a0 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 d the second loc
cc5b0 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e k to.** fail sin
cc5c0 63 65 20 74 68 65 72 65 20 77 61 73 20 61 6c 72 ce there was alr
cc5d0 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 eady a lock on t
cc5e0 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20 66 he file due to f
cc5f0 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 d1..** But not s
cc600 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20 6c o. Since both l
cc610 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 ocks came from t
cc620 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2c he same process,
cc630 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f the.** second o
cc640 76 65 72 72 69 64 65 73 20 74 68 65 20 66 69 72 verrides the fir
cc650 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 st, even though
cc660 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69 66 they were on dif
cc670 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 ferent.** file d
cc680 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 escriptors opene
cc690 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20 66 d on different f
cc6a0 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a ile names..**.**
cc6b0 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 This means that
cc6c0 20 77 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 we cannot use P
cc6d0 4f 53 49 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 OSIX locks to sy
cc6e0 6e 63 68 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 nchronize file a
cc6f0 63 63 65 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 ccess.** among c
cc700 6f 6d 70 65 74 69 6e 67 20 74 68 72 65 61 64 73 ompeting threads
cc710 20 6f 66 20 74 68 65 20 73 61 6d 65 20 70 72 6f of the same pro
cc720 63 65 73 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 cess. POSIX loc
cc730 6b 73 20 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e ks will work fin
cc740 65 0a 2a 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e e.** to synchron
cc750 69 7a 65 20 61 63 63 65 73 73 20 66 6f 72 20 74 ize access for t
cc760 68 72 65 61 64 73 20 69 6e 20 73 65 70 61 72 61 hreads in separa
cc770 74 65 20 70 72 6f 63 65 73 73 65 73 2c 20 62 75 te processes, bu
cc780 74 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 t not.** threads
cc790 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 within the same
cc7a0 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 process..**.**
cc7b0 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 To work around t
cc7c0 68 65 20 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 he problem, SQLi
cc7d0 74 65 20 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 te has to manage
cc7e0 20 66 69 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 file locks inte
cc7f0 72 6e 61 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 rnally.** on its
cc800 20 6f 77 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 own. Whenever
cc810 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 69 a new database i
cc820 73 20 6f 70 65 6e 65 64 2c 20 77 65 20 68 61 76 s opened, we hav
cc830 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a e to find the.**
cc840 20 73 70 65 63 69 66 69 63 20 69 6e 6f 64 65 20 specific inode
cc850 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
cc860 66 69 6c 65 20 28 74 68 65 20 69 6e 6f 64 65 20 file (the inode
cc870 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 is determined by
cc880 20 74 68 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 the.** st_dev a
cc890 6e 64 20 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 nd st_ino fields
cc8a0 20 6f 66 20 74 68 65 20 73 74 61 74 20 73 74 72 of the stat str
cc8b0 75 63 74 75 72 65 20 74 68 61 74 20 66 73 74 61 ucture that fsta
cc8c0 74 28 29 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a t() fills in).**
cc8d0 20 61 6e 64 20 63 68 65 63 6b 20 66 6f 72 20 6c and check for l
cc8e0 6f 63 6b 73 20 61 6c 72 65 61 64 79 20 65 78 69 ocks already exi
cc8f0 73 74 69 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e sting on that in
cc900 6f 64 65 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 ode. When locks
cc910 20 61 72 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 are.** created
cc920 6f 72 20 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 or removed, we h
cc930 61 76 65 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f ave to look at o
cc940 75 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 ur own internal
cc950 72 65 63 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a record of the.**
cc960 20 6c 6f 63 6b 73 20 74 6f 20 73 65 65 20 69 66 locks to see if
cc970 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 another thread
cc980 68 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 has previously s
cc990 65 74 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 et a lock on tha
cc9a0 74 20 73 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e t same.** inode.
cc9b0 0a 2a 2a 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 .**.** (Aside: T
cc9c0 68 65 20 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 he use of inode
cc9d0 6e 75 6d 62 65 72 73 20 61 73 20 75 6e 69 71 75 numbers as uniqu
cc9e0 65 20 49 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 e IDs does not w
cc9f0 6f 72 6b 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a ork on VxWorks..
cca00 2a 2a 20 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 ** For VxWorks,
cca10 77 65 20 68 61 76 65 20 74 6f 20 75 73 65 20 74 we have to use t
cca20 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 75 he alternative u
cca30 6e 69 71 75 65 20 49 44 20 73 79 73 74 65 6d 20 nique ID system
cca40 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f based on.** cano
cca50 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 nical filename a
cca60 6e 64 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 nd implemented i
cca70 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64 n the previous d
cca80 69 76 69 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 ivision.).**.**
cca90 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 The sqlite3_file
ccaa0 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 50 structure for P
ccab0 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 OSIX is no longe
ccac0 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67 65 r just an intege
ccad0 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 r file.** descri
ccae0 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 ptor. It is now
ccaf0 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 a structure tha
ccb00 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 t holds the inte
ccb10 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 ger file.** desc
ccb20 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 riptor and a poi
ccb30 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63 74 nter to a struct
ccb40 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 ure that describ
ccb50 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a es the internal.
ccb60 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 ** locks on the
ccb70 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e corresponding in
ccb80 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 20 6f ode. There is o
ccb90 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 ne locking struc
ccba0 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 ture.** per inod
ccbb0 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61 6d e, so if the sam
ccbc0 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 e inode is opene
ccbd0 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75 6e d twice, both un
ccbe0 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 ixFile structure
ccbf0 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 s.** point to th
ccc00 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 e same locking s
ccc10 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 6c tructure. The l
ccc20 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 ocking structure
ccc30 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 keeps.** a refe
ccc40 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 rence count (so
ccc50 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 we will know whe
ccc60 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29 20 n to delete it)
ccc70 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 and a "cnt".** f
ccc80 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73 20 ield that tells
ccc90 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 us its internal
ccca0 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63 6e lock status. cn
cccb0 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a t==0 means the.*
cccc0 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b * file is unlock
cccd0 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 ed. cnt==-1 mea
ccce0 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 ns the file has
cccf0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
ccd00 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e k..** cnt>0 mean
ccd10 73 20 74 68 65 72 65 20 61 72 65 20 63 6e 74 20 s there are cnt
ccd20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 shared locks on
ccd30 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 the file..**.**
ccd40 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 6c Any attempt to l
ccd50 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 ock or unlock a
ccd60 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 63 6b file first check
ccd70 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a s the locking.**
ccd80 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 structure. The
ccd90 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 fcntl() system
ccda0 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 call is only inv
ccdb0 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a 2a oked to set a .*
ccdc0 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 * POSIX lock if
ccdd0 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 the internal loc
ccde0 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 61 6e k structure tran
ccdf0 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a sitions between.
cce00 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 ** a locked and
cce10 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 an unlocked stat
cce20 65 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 e..**.** But wai
cce30 74 3a 20 20 74 68 65 72 65 20 61 72 65 20 79 65 t: there are ye
cce40 74 20 6d 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 t more problems
cce50 77 69 74 68 20 50 4f 53 49 58 20 61 64 76 69 73 with POSIX advis
cce60 6f 72 79 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a ory locks..**.**
cce70 20 49 66 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 If you close a
cce80 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
cce90 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 that points to a
ccea0 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 6c file that has l
cceb0 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 ocks,.** all loc
ccec0 6b 73 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 ks on that file
cced0 74 68 61 74 20 61 72 65 20 6f 77 6e 65 64 20 62 that are owned b
ccee0 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 y the current pr
ccef0 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c ocess are.** rel
ccf00 65 61 73 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 eased. To work
ccf10 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62 around this prob
ccf20 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69 78 46 69 lem, each unixFi
ccf30 6c 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e le structure con
ccf40 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 tains.** a point
ccf50 65 72 20 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 er to an unixOpe
ccf60 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 nCnt structure.
ccf70 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 75 6e There is one un
ccf80 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 ixOpenCnt struct
ccf90 75 72 65 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 ure.** per open
ccfa0 69 6e 6f 64 65 2c 20 77 68 69 63 68 20 6d 65 61 inode, which mea
ccfb0 6e 73 20 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 ns that multiple
ccfc0 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 70 6f unixFile can po
ccfd0 69 6e 74 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a int to a single.
ccfe0 2a 2a 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 ** unixOpenCnt.
ccff0 20 57 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 When an attempt
cd000 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 is made to clos
cd010 65 20 61 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 e an unixFile, i
cd020 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f f there are.** o
cd030 74 68 65 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 ther unixFile op
cd040 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 en on the same i
cd050 6e 6f 64 65 20 74 68 61 74 20 61 72 65 20 68 6f node that are ho
cd060 6c 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 lding locks, the
cd070 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 call.** to clos
cd080 65 28 29 20 74 68 65 20 66 69 6c 65 20 64 65 73 e() the file des
cd090 63 72 69 70 74 6f 72 20 69 73 20 64 65 66 65 72 criptor is defer
cd0a0 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 red until all of
cd0b0 20 74 68 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 the locks clear
cd0c0 2e 0a 2a 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 ..** The unixOpe
cd0d0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 6b nCnt structure k
cd0e0 65 65 70 73 20 61 20 6c 69 73 74 20 6f 66 20 66 eeps a list of f
cd0f0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
cd100 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 that need to.**
cd110 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 be closed and th
cd120 61 74 20 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 at list is walke
cd130 64 20 28 61 6e 64 20 63 6c 65 61 72 65 64 29 20 d (and cleared)
cd140 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f when the last lo
cd150 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a ck.** clears..**
cd160 0a 2a 2a 20 59 65 74 20 61 6e 6f 74 68 65 72 20 .** Yet another
cd170 70 72 6f 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 problem: LinuxT
cd180 68 72 65 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c hreads do not pl
cd190 61 79 20 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 ay well with pos
cd1a0 69 78 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 ix locks..**.**
cd1b0 4d 61 6e 79 20 6f 6c 64 65 72 20 76 65 72 73 69 Many older versi
cd1c0 6f 6e 73 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 ons of linux use
cd1d0 20 74 68 65 20 4c 69 6e 75 78 54 68 72 65 61 64 the LinuxThread
cd1e0 73 20 6c 69 62 72 61 72 79 20 77 68 69 63 68 20 s library which
cd1f0 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 is.** not posix
cd200 63 6f 6d 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 compliant. Unde
cd210 72 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c 20 r LinuxThreads,
cd220 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20 62 a lock created b
cd230 79 20 74 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 y thread.** A ca
cd240 6e 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 nnot be modified
cd250 20 6f 72 20 6f 76 65 72 72 69 64 64 65 6e 20 62 or overridden b
cd260 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74 68 y a different th
cd270 72 65 61 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 read B..** Only
cd280 74 68 72 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 thread A can mod
cd290 69 66 79 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c ify the lock. L
cd2a0 6f 63 6b 69 6e 67 20 62 65 68 61 76 69 6f 72 20 ocking behavior
cd2b0 69 73 20 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 is correct.** if
cd2c0 20 74 68 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 the appliation
cd2d0 75 73 65 73 20 74 68 65 20 6e 65 77 65 72 20 4e uses the newer N
cd2e0 61 74 69 76 65 20 50 6f 73 69 78 20 54 68 72 65 ative Posix Thre
cd2f0 61 64 20 4c 69 62 72 61 72 79 20 28 4e 50 54 4c ad Library (NPTL
cd300 29 0a 2a 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 ).** on linux -
cd310 77 69 74 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b with NPTL a lock
cd320 20 63 72 65 61 74 65 64 20 62 79 20 74 68 72 65 created by thre
cd330 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 ad A can overrid
cd340 65 20 6c 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 e locks.** in th
cd350 72 65 61 64 20 42 2e 20 20 42 75 74 20 74 68 65 read B. But the
cd360 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 re is no way to
cd370 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d know at compile-
cd380 74 69 6d 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 time which.** th
cd390 72 65 61 64 69 6e 67 20 6c 69 62 72 61 72 79 20 reading library
cd3a0 69 73 20 62 65 69 6e 67 20 75 73 65 64 2e 20 20 is being used.
cd3b0 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 So there is no w
cd3c0 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a ay to know at.**
cd3d0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 compile-time wh
cd3e0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 ether or not thr
cd3f0 65 61 64 20 41 20 63 61 6e 20 6f 76 65 72 72 69 ead A can overri
cd400 64 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 de locks on thre
cd410 61 64 20 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 ad B..** We have
cd420 20 74 6f 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d to do a run-tim
cd430 65 20 63 68 65 63 6b 20 74 6f 20 64 69 73 63 6f e check to disco
cd440 76 65 72 20 74 68 65 20 62 65 68 61 76 69 6f 72 ver the behavior
cd450 20 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 of the.** curre
cd460 6e 74 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a nt process..**.*
cd470 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 68 65 * On systems whe
cd480 72 65 20 74 68 72 65 61 64 20 41 20 69 73 20 75 re thread A is u
cd490 6e 61 62 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 nable to modify
cd4a0 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20 62 79 locks created by
cd4b0 0a 2a 2a 20 74 68 72 65 61 64 20 42 2c 20 77 65 .** thread B, we
cd4c0 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20 74 72 have to keep tr
cd4d0 61 63 6b 20 6f 66 20 77 68 69 63 68 20 74 68 72 ack of which thr
cd4e0 65 61 64 20 63 72 65 61 74 65 64 20 65 61 63 68 ead created each
cd4f0 0a 2a 2a 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 .** lock. Hence
cd500 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 78 74 there is an ext
cd510 72 61 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 ra field in the
cd520 6b 65 79 20 74 6f 20 74 68 65 20 75 6e 69 78 4c key to the unixL
cd530 6f 63 6b 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 ockInfo.** struc
cd540 74 75 72 65 20 74 6f 20 72 65 63 6f 72 64 20 74 ture to record t
cd550 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e his information.
cd560 20 20 41 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 And on those s
cd570 79 73 74 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 ystems it.** is
cd580 69 6c 6c 65 67 61 6c 20 74 6f 20 62 65 67 69 6e illegal to begin
cd590 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 a transaction i
cd5a0 6e 20 6f 6e 65 20 74 68 72 65 61 64 20 61 6e 64 n one thread and
cd5b0 20 66 69 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e finish it.** in
cd5c0 20 61 6e 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 another. For t
cd5d0 68 69 73 20 6c 61 74 74 65 72 20 72 65 73 74 72 his latter restr
cd5e0 69 63 74 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 iction, there is
cd5f0 20 6e 6f 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e no work-around.
cd600 0a 2a 2a 20 49 74 20 69 73 20 61 20 6c 69 6d 69 .** It is a limi
cd610 74 61 74 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 tation of LinuxT
cd620 68 72 65 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a hreads..*/../*.*
cd630 2a 20 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 * Set or check t
cd640 68 65 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 he unixFile.tid
cd650 66 69 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 field. This fie
cd660 6c 64 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 ld is set when a
cd670 6e 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 n unixFile.** is
cd680 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 first opened.
cd690 41 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 All subsequent u
cd6a0 73 65 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 ses of the unixF
cd6b0 69 6c 65 20 76 65 72 69 66 79 20 74 68 61 74 20 ile verify that
cd6c0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 the.** same thre
cd6d0 61 64 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 ad is operating
cd6e0 6f 6e 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e on the unixFile.
cd6f0 20 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 Some operating
cd700 20 73 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e systems do.** n
cd710 6f 74 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 ot allow locks t
cd720 6f 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 o be overridden
cd730 62 79 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 by other threads
cd740 20 61 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 and that restri
cd750 63 74 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 ction.** means t
cd760 68 61 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 hat sqlite3* dat
cd770 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 abase handles ca
cd780 6e 6e 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 nnot be moved fr
cd790 6f 6d 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a om one thread.**
cd7a0 20 74 6f 20 61 6e 6f 74 68 65 72 20 77 68 69 6c to another whil
cd7b0 65 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 e locks are held
cd7c0 2e 0a 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 ..**.** Version
cd7d0 33 2e 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 3.3.1 (2006-01-1
cd7e0 35 29 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 5): unixFile ca
cd7f0 6e 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 n be moved from
cd800 6f 6e 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a one thread to.**
cd810 20 61 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 another as long
cd820 20 61 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 as we are runni
cd830 6e 67 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 ng on a system t
cd840 68 61 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 hat supports thr
cd850 65 61 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 eads.** overridi
cd860 6e 67 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c ng each others l
cd870 6f 63 6b 73 20 28 77 68 69 63 68 20 69 73 20 6e ocks (which is n
cd880 6f 77 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d ow the most comm
cd890 6f 6e 20 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 on behavior).**
cd8a0 6f 72 20 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 or if no locks a
cd8b0 72 65 20 68 65 6c 64 2e 20 20 42 75 74 20 74 68 re held. But th
cd8c0 65 20 75 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b e unixFile.pLock
cd8d0 20 66 69 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 field needs to
cd8e0 62 65 0a 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 be.** recomputed
cd8f0 20 62 65 63 61 75 73 65 20 69 74 73 20 6b 65 79 because its key
cd900 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 74 68 includes the th
cd910 72 65 61 64 2d 69 64 2e 20 20 53 65 65 20 74 68 read-id. See th
cd920 65 20 0a 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 e .** transferOw
cd930 6e 65 72 73 68 69 70 28 29 20 66 75 6e 63 74 69 nership() functi
cd940 6f 6e 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 on below for add
cd950 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
cd960 69 6f 6e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ion.*/.#if SQLIT
cd970 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cd980 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cd990 5f 29 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f _).# define SET_
cd9a0 54 48 52 45 41 44 49 44 28 58 29 20 20 20 28 58 THREADID(X) (X
cd9b0 29 2d 3e 74 69 64 20 3d 20 70 74 68 72 65 61 64 )->tid = pthread
cd9c0 5f 73 65 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 _self().# define
cd9d0 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 CHECK_THREADID(
cd9e0 58 29 20 28 74 68 72 65 61 64 73 4f 76 65 72 72 X) (threadsOverr
cd9f0 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 ideEachOthersLoc
cda00 6b 73 3d 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 ks==0 && \.
cda10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
cda20 20 20 20 20 20 20 20 21 70 74 68 72 65 61 64 5f !pthread_
cda30 65 71 75 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 equal((X)->tid,
cda40 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 29 29 pthread_self()))
cda50 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
cda60 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29 0a SET_THREADID(X).
cda70 23 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 # define CHECK_T
cda80 48 52 45 41 44 49 44 28 58 29 20 30 0a 23 65 6e HREADID(X) 0.#en
cda90 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e dif../*.** An in
cdaa0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f stance of the fo
cdab0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 llowing structur
cdac0 65 20 73 65 72 76 65 73 20 61 73 20 74 68 65 20 e serves as the
cdad0 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c key used.** to l
cdae0 6f 63 61 74 65 20 61 20 70 61 72 74 69 63 75 6c ocate a particul
cdaf0 61 72 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 ar unixOpenCnt s
cdb00 74 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 tructure given i
cdb10 74 73 20 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a ts inode. This.
cdb20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ** is the same a
cdb30 73 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 s the unixLockKe
cdb40 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 y except that th
cdb50 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20 6f e thread ID is o
cdb60 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 mitted..*/.struc
cdb70 74 20 75 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 t unixFileId {.
cdb80 20 64 65 76 5f 74 20 64 65 76 3b 20 20 20 20 20 dev_t dev;
cdb90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cdba0 44 65 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f Device number */
cdbb0 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a .#if OS_VXWORKS.
cdbc0 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 struct vxworks
cdbd0 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a FileId *pId; /*
cdbe0 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 Unique file ID
cdbf0 66 6f 72 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a for vxworks. */.
cdc00 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e #else. ino_t in
cdc10 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o;
cdc20 20 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d /* Inode num
cdc30 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b ber */.#endif.};
cdc40 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
cdc50 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
cdc60 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 wing structure s
cdc70 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 erves as the key
cdc80 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 used.** to loca
cdc90 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 te a particular
cdca0 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 unixLockInfo str
cdcb0 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 ucture given its
cdcc0 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 inode..**.** If
cdcd0 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 threads cannot
cdce0 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 override each ot
cdcf0 68 65 72 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 hers locks (Linu
cdd00 78 54 68 72 65 61 64 73 29 2c 20 74 68 65 6e 20 xThreads), then
cdd10 77 65 0a 2a 2a 20 73 65 74 20 74 68 65 20 75 6e we.** set the un
cdd20 69 78 4c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 ixLockKey.tid fi
cdd30 65 6c 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 eld to the threa
cdd40 64 20 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 d ID. If thread
cdd50 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a s can override.*
cdd60 2a 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f * each others lo
cdd70 63 6b 73 20 28 50 6f 73 69 78 20 61 6e 64 20 4e cks (Posix and N
cdd80 50 54 4c 29 20 74 68 65 6e 20 74 69 64 20 69 73 PTL) then tid is
cdd90 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a always set to z
cdda0 65 72 6f 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f ero..** tid is o
cddb0 6d 69 74 74 65 64 20 69 66 20 77 65 20 63 6f 6d mitted if we com
cddc0 70 69 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 pile without thr
cddd0 65 61 64 69 6e 67 20 73 75 70 70 6f 72 74 20 6f eading support o
cdde0 72 20 6f 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 r on an OS.** ot
cddf0 68 65 72 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a her than linux..
cde00 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f */.struct unixLo
cde10 63 6b 4b 65 79 20 7b 0a 20 20 73 74 72 75 63 74 ckKey {. struct
cde20 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 64 3b unixFileId fid;
cde30 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 64 65 6e /* Unique iden
cde40 74 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 66 tifier for the f
cde50 69 6c 65 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 ile */.#if SQLIT
cde60 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cde70 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cde80 5f 29 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74 _). pthread_t t
cde90 69 64 3b 20 20 2f 2a 20 54 68 72 65 61 64 20 49 id; /* Thread I
cdea0 44 20 6f 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e D of lock owner.
cdeb0 20 5a 65 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 Zero if not usi
cdec0 6e 67 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 ng LinuxThreads
cded0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a */.#endif.};../*
cdee0 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
cdef0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
cdf00 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
cdf10 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 located for each
cdf20 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 open.** inode.
cdf30 20 4f 72 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 Or, on LinuxThr
cdf40 65 61 64 73 2c 20 74 68 65 72 65 20 69 73 20 6f eads, there is o
cdf50 6e 65 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 ne of these stru
cdf60 63 74 75 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 ctures for.** ea
cdf70 63 68 20 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 ch inode opened
cdf80 62 79 20 65 61 63 68 20 74 68 72 65 61 64 2e 0a by each thread..
cdf90 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 **.** A single i
cdfa0 6e 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 node can have mu
cdfb0 6c 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 ltiple file desc
cdfc0 72 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 riptors, so each
cdfd0 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 unixFile.** str
cdfe0 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 ucture contains
cdff0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 a pointer to an
ce000 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 instance of this
ce010 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 object and this
ce020 0a 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 .** object keeps
ce030 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 a count of the
ce040 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 number of unixFi
ce050 6c 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 le pointing to i
ce060 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 t..*/.struct uni
ce070 78 4c 6f 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 xLockInfo {. st
ce080 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 ruct unixLockKey
ce090 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a lockKey; /*
ce0a0 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 The lookup key
ce0b0 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 */. int cnt;
ce0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce0d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
ce0e0 66 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 f SHARED locks h
ce0f0 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 eld */. int loc
ce100 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 ktype;
ce110 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 /* One
ce120 6f 66 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 of SHARED_LOCK,
ce130 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 RESERVED_LOCK et
ce140 63 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 c. */. int nRef
ce150 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
ce160 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ce170 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f r of pointers to
ce180 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 this structure
ce190 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 */. struct unix
ce1a0 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b LockInfo *pNext;
ce1b0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 /* List of
ce1c0 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f all unixLockInfo
ce1d0 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 objects */. st
ce1e0 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 ruct unixLockInf
ce1f0 6f 20 2a 70 50 72 65 76 3b 20 20 20 20 20 2f 2a o *pPrev; /*
ce200 20 20 20 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 .... doubly
ce210 6c 69 6e 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a linked */.};../*
ce220 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 .** An instance
ce230 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 of the following
ce240 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c structure is al
ce250 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 located for each
ce260 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 open.** inode.
ce270 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 This structure
ce280 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20 74 keeps track of t
ce290 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 he number of loc
ce2a0 6b 73 20 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e ks on that.** in
ce2b0 6f 64 65 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 ode. If a close
ce2c0 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61 67 is attempted ag
ce2d0 61 69 6e 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 ainst an inode t
ce2e0 68 61 74 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a hat is holding.*
ce2f0 2a 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f * locks, the clo
ce300 73 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75 se is deferred u
ce310 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 ntil all locks c
ce320 6c 65 61 72 20 62 79 20 61 64 64 69 6e 67 20 74 lear by adding t
ce330 68 65 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 he.** file descr
ce340 69 70 74 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 iptor to be clos
ce350 65 64 20 74 6f 20 74 68 65 20 70 65 6e 64 69 6e ed to the pendin
ce360 67 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f g list..**.** TO
ce370 44 4f 3a 20 20 43 6f 6e 73 69 64 65 72 20 63 68 DO: Consider ch
ce380 61 6e 67 69 6e 67 20 74 68 69 73 20 73 6f 20 74 anging this so t
ce390 68 61 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c hat there is onl
ce3a0 79 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a y a single file.
ce3b0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f ** descriptor fo
ce3c0 72 20 65 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 r each open file
ce3d0 2c 20 65 76 65 6e 20 77 68 65 6e 20 69 74 20 69 , even when it i
ce3e0 73 20 6f 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c s opened multipl
ce3f0 65 20 74 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 e times..** The
ce400 63 6c 6f 73 65 28 29 20 73 79 73 74 65 6d 20 63 close() system c
ce410 61 6c 6c 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f all would only o
ce420 63 63 75 72 20 77 68 65 6e 20 74 68 65 20 6c 61 ccur when the la
ce430 73 74 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 st database.** u
ce440 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 63 6c sing the file cl
ce450 6f 73 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 oses..*/.struct
ce460 75 6e 69 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 unixOpenCnt {.
ce470 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 struct unixFileI
ce480 64 20 66 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 d fileId; /* T
ce490 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f he lookup key */
ce4a0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 . int nRef;
ce4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
ce4c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e * Number of poin
ce4d0 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 ters to this str
ce4e0 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 ucture */. int
ce4f0 6e 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 nLock;
ce500 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
ce510 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 r of outstanding
ce520 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 locks */. Unix
ce530 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 UnusedFd *pUnuse
ce540 64 3b 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 d; /* Unuse
ce550 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
ce560 72 73 20 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 rs to close */.#
ce570 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 if OS_VXWORKS.
ce580 73 65 6d 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 sem_t *pSem;
ce590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ce5a0 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 /* Named POSIX
ce5b0 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 semaphore */. c
ce5c0 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 har aSemName[MAX
ce5d0 5f 50 41 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 _PATHNAME+2];
ce5e0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 /* Name of that
ce5f0 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e semaphore */.#en
ce600 64 69 66 0a 20 20 73 74 72 75 63 74 20 75 6e 69 dif. struct uni
ce610 78 4f 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c xOpenCnt *pNext,
ce620 20 2a 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 *pPrev; /* Li
ce630 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 st of all unixOp
ce640 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f enCnt objects */
ce650 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 .};../*.** Lists
ce660 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b of all unixLock
ce670 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 Info and unixOpe
ce680 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 nCnt objects. T
ce690 68 65 73 65 20 75 73 65 64 20 74 6f 20 62 65 20 hese used to be
ce6a0 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 hash.** tables.
ce6b0 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 But the number
ce6c0 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72 61 of objects is ra
ce6d0 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 rely more than a
ce6e0 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 dozen and.** ne
ce6f0 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66 65 ver exceeds a fe
ce700 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 w thousand. And
ce710 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f lookup is not o
ce720 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 n a critical.**
ce730 70 61 74 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 path so a simple
ce740 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c linked list wil
ce750 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 l suffice..*/.st
ce760 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69 78 atic struct unix
ce770 4c 6f 63 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 LockInfo *lockLi
ce780 73 74 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 st = 0;.static s
ce790 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e truct unixOpenCn
ce7a0 74 20 2a 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b t *openList = 0;
ce7b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 ../*.** This var
ce7c0 69 61 62 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 iable remembers
ce7d0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 whether or not t
ce7e0 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 hreads can overr
ce7f0 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 0a ide each others.
ce800 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 ** locks..**.**
ce810 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 0: No. Thre
ce820 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 ads cannot overr
ce830 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 ide each others
ce840 6c 6f 63 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 locks. (LinuxTh
ce850 72 65 61 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 reads).** 1:
ce860 20 59 65 73 2e 20 20 54 68 72 65 61 64 73 20 63 Yes. Threads c
ce870 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 an override each
ce880 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 others locks.
ce890 28 50 6f 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a (Posix & NLPT).*
ce8a0 2a 20 20 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 * -1: We don'
ce8b0 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a t know yet..**.*
ce8c0 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d * On some system
ce8d0 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f s, we know at co
ce8e0 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 mpile-time if th
ce8f0 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 reads can overri
ce900 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 de each.** other
ce910 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f s locks. On tho
ce920 73 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 20 se systems, the
ce930 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 SQLITE_THREAD_OV
ce940 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 ERRIDE_LOCK macr
ce950 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 o.** will be set
ce960 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 appropriately.
ce970 20 4f 6e 20 6f 74 68 65 72 20 73 79 73 74 65 6d On other system
ce980 73 2c 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 s, we have to ch
ce990 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d eck at.** runtim
ce9a0 65 2e 20 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 e. On these lat
ce9b0 74 65 72 20 73 79 73 74 65 6d 73 2c 20 53 51 4c ter systems, SQL
ce9c0 54 49 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 TIE_THREAD_OVERR
ce9d0 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 IDE_LOCK is.** u
ce9e0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 ndefined..**.**
ce9f0 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6e 6f This variable no
cea00 72 6d 61 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 rmally has file
cea10 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 scope only. But
cea20 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e 67 2c during testing,
cea30 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 we make.** it a
cea40 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 global so that
cea50 74 68 65 20 74 65 73 74 20 63 6f 64 65 20 63 61 the test code ca
cea60 6e 20 63 68 61 6e 67 65 20 69 74 73 20 76 61 6c n change its val
cea70 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 ue in order to v
cea80 65 72 69 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 erify.** that th
cea90 65 20 72 69 67 68 74 20 73 74 75 66 66 20 68 61 e right stuff ha
ceaa0 70 70 65 6e 73 20 69 6e 20 65 69 74 68 65 72 20 ppens in either
ceab0 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c case..*/.#if SQL
ceac0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 ITE_THREADSAFE &
cead0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 & defined(__linu
ceae0 78 5f 5f 29 0a 23 20 20 69 66 6e 64 65 66 20 53 x__).# ifndef S
ceaf0 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 QLITE_THREAD_OVE
ceb00 52 52 49 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 RRIDE_LOCK.#
ceb10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 define SQLITE_TH
ceb20 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f READ_OVERRIDE_LO
ceb30 43 4b 20 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 CK -1.# endif.#
ceb40 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 ifdef SQLITE_T
ceb50 45 53 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f EST.int threadsO
ceb60 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 verrideEachOther
ceb70 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f sLocks = SQLITE_
ceb80 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f THREAD_OVERRIDE_
ceb90 4c 4f 43 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 LOCK;.# else.st
ceba0 61 74 69 63 20 69 6e 74 20 74 68 72 65 61 64 73 atic int threads
cebb0 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cebc0 72 73 4c 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 rsLocks = SQLITE
cebd0 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 _THREAD_OVERRIDE
cebe0 5f 4c 4f 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a _LOCK;.# endif.
cebf0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 #endif../*.** Th
cec00 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c is structure hol
cec10 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 ds information p
cec20 61 73 73 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 assed into indiv
cec30 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 idual test.** th
cec40 72 65 61 64 73 20 62 79 20 74 68 65 20 74 65 73 reads by the tes
cec50 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 tThreadLockingBe
cec60 68 61 76 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 havior() routine
cec70 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74 68 72 65 ..*/.struct thre
cec80 61 64 54 65 73 74 44 61 74 61 20 7b 0a 20 20 69 adTestData {. i
cec90 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 nt fd;
ceca0 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f /* File to
cecb0 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 be locked */.
cecc0 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 struct flock loc
cecd0 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f k; /* The lo
cece0 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 cking operation
cecf0 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75 6c 74 3b */. int result;
ced00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
ced10 65 73 75 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 esult of the loc
ced20 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a king operation *
ced30 2f 0a 7d 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 /.};..#if SQLITE
ced40 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
ced50 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
ced60 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e )./*.** This fun
ced70 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73 ction is used as
ced80 20 74 68 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e the main routin
ced90 65 20 66 6f 72 20 61 20 74 68 72 65 61 64 20 6c e for a thread l
ceda0 61 75 6e 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 aunched by.** te
cedb0 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 stThreadLockingB
cedc0 65 68 61 76 69 6f 72 28 29 2e 20 49 74 20 74 65 ehavior(). It te
cedd0 73 74 73 20 77 68 65 74 68 65 72 20 74 68 65 20 sts whether the
cede0 73 68 61 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 shared-lock obta
cedf0 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d ined.** by the m
cee00 61 69 6e 20 74 68 72 65 61 64 20 69 6e 20 74 65 ain thread in te
cee10 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 stThreadLockingB
cee20 65 68 61 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 ehavior() confli
cee30 63 74 73 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 cts with a.** hy
cee40 70 6f 74 68 65 74 69 63 61 6c 20 77 72 69 74 65 pothetical write
cee50 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 -lock obtained b
cee60 79 20 74 68 69 73 20 74 68 72 65 61 64 20 6f 6e y this thread on
cee70 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a the same file..
cee80 2a 2a 0a 2a 2a 20 54 68 65 20 77 72 69 74 65 2d **.** The write-
cee90 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 lock is not actu
ceea0 61 6c 6c 79 20 61 63 71 75 69 72 65 64 2c 20 61 ally acquired, a
ceeb0 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 70 6f s this is not po
ceec0 73 73 69 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 ssible if .** th
ceed0 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 e file is open i
ceee0 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 n read-only mode
ceef0 20 28 73 65 65 20 74 69 63 6b 65 74 20 23 33 34 (see ticket #34
cef00 37 32 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 72)..*/ .static
cef10 76 6f 69 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b void *threadLock
cef20 69 6e 67 54 65 73 74 28 76 6f 69 64 20 2a 70 41 ingTest(void *pA
cef30 72 67 29 7b 0a 20 20 73 74 72 75 63 74 20 74 68 rg){. struct th
cef40 72 65 61 64 54 65 73 74 44 61 74 61 20 2a 70 44 readTestData *pD
cef50 61 74 61 20 3d 20 28 73 74 72 75 63 74 20 74 68 ata = (struct th
cef60 72 65 61 64 54 65 73 74 44 61 74 61 2a 29 70 41 readTestData*)pA
cef70 72 67 3b 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 rg;. pData->res
cef80 75 6c 74 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 ult = fcntl(pDat
cef90 61 2d 3e 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 a->fd, F_GETLK,
cefa0 26 70 44 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 &pData->lock);.
cefb0 20 72 65 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a return pArg;.}.
cefc0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
cefd0 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 _THREADSAFE && d
cefe0 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f efined(__linux__
ceff0 29 20 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 ) */...#if SQLIT
cf000 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 E_THREADSAFE &&
cf010 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f defined(__linux_
cf020 5f 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 _)./*.** This pr
cf030 6f 63 65 64 75 72 65 20 61 74 74 65 6d 70 74 73 ocedure attempts
cf040 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 to determine wh
cf050 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 ether or not thr
cf060 65 61 64 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 eads.** can over
cf070 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 ride each others
cf080 20 6c 6f 63 6b 73 20 74 68 65 6e 20 73 65 74 73 locks then sets
cf090 20 74 68 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 the .** threads
cf0a0 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cf0b0 72 73 4c 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 rsLocks variable
cf0c0 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a appropriately..
cf0d0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 */.static void t
cf0e0 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 estThreadLocking
cf0f0 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66 64 5f Behavior(int fd_
cf100 6f 72 69 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b orig){. int fd;
cf110 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 . int rc;. str
cf120 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44 61 uct threadTestDa
cf130 74 61 20 64 3b 0a 20 20 73 74 72 75 63 74 20 66 ta d;. struct f
cf140 6c 6f 63 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 lock l;. pthrea
cf150 64 5f 74 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 d_t t;.. fd = d
cf160 75 70 28 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 up(fd_orig);. i
cf170 66 28 20 66 64 3c 30 20 29 20 72 65 74 75 72 6e f( fd<0 ) return
cf180 3b 0a 20 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 ;. memset(&l, 0
cf190 2c 20 73 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 , sizeof(l));.
cf1a0 6c 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c l.l_type = F_RDL
cf1b0 43 4b 3b 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 CK;. l.l_len =
cf1c0 31 3b 0a 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 1;. l.l_start =
cf1d0 20 30 3b 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 0;. l.l_whence
cf1e0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 = SEEK_SET;. r
cf1f0 63 20 3d 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 c = fcntl(fd_ori
cf200 67 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b g, F_SETLK, &l);
cf210 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 . if( rc!=0 ) r
cf220 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 eturn;. memset(
cf230 26 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 &d, 0, sizeof(d)
cf240 29 3b 0a 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a );. d.fd = fd;.
cf250 20 20 64 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 d.lock = l;.
cf260 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 d.lock.l_type =
cf270 46 5f 57 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 F_WRLCK;. if( p
cf280 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 thread_create(&t
cf290 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 , 0, threadLocki
cf2a0 6e 67 54 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 ngTest, &d)==0 )
cf2b0 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6a 6f {. pthread_jo
cf2c0 69 6e 28 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 in(t, 0);. }.
cf2d0 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 69 66 28 close(fd);. if(
cf2e0 20 64 2e 72 65 73 75 6c 74 21 3d 30 20 29 20 72 d.result!=0 ) r
cf2f0 65 74 75 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 eturn;. threads
cf300 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 OverrideEachOthe
cf310 72 73 4c 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 rsLocks = (d.loc
cf320 6b 2e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 k.l_type==F_UNLC
cf330 4b 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 K);.}.#endif /*
cf340 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 SQLITE_THREADSAF
cf350 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c E && defined(__l
cf360 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a inux__) */../*.*
cf370 2a 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 * Release a unix
cf380 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 LockInfo structu
cf390 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c re previously al
cf3a0 6c 6f 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c located by findL
cf3b0 6f 63 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a ockInfo()..**.**
cf3c0 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 The mutex enter
cf3d0 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 ed using the uni
cf3e0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 xEnterMutex() fu
cf3f0 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 nction must be h
cf400 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 eld.** when this
cf410 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
cf420 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 led..*/.static v
cf430 6f 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 oid releaseLockI
cf440 6e 66 6f 28 73 74 72 75 63 74 20 75 6e 69 78 4c nfo(struct unixL
cf450 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b ockInfo *pLock){
cf460 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78 4d . assert( unixM
cf470 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 utexHeld() );.
cf480 69 66 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 if( pLock ){.
cf490 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a pLock->nRef--;.
cf4a0 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e if( pLock->n
cf4b0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 Ref==0 ){.
cf4c0 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 if( pLock->pPrev
cf4d0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ){. asse
cf4e0 72 74 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 rt( pLock->pPrev
cf4f0 2d 3e 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 ->pNext==pLock )
cf500 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d ;. pLock-
cf510 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 >pPrev->pNext =
cf520 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 pLock->pNext;.
cf530 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
cf540 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c assert( lockL
cf550 69 73 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 ist==pLock );.
cf560 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d lockList =
cf570 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 pLock->pNext;.
cf580 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
cf590 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b pLock->pNext ){
cf5a0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
cf5b0 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 pLock->pNext->p
cf5c0 50 72 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 Prev==pLock );.
cf5d0 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e pLock->pN
cf5e0 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f ext->pPrev = pLo
cf5f0 63 6b 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 ck->pPrev;.
cf600 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
cf610 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 _free(pLock);.
cf620 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
cf630 20 52 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4f Release a unixO
cf640 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 penCnt structure
cf650 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f previously allo
cf660 63 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 cated by findLoc
cf670 6b 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 kInfo()..**.** T
cf680 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 he mutex entered
cf690 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 using the unixE
cf6a0 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 nterMutex() func
cf6b0 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c tion must be hel
cf6c0 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 d.** when this f
cf6d0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
cf6e0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
cf6f0 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 d releaseOpenCnt
cf700 28 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e (struct unixOpen
cf710 43 6e 74 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 Cnt *pOpen){. a
cf720 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 ssert( unixMutex
cf730 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 Held() );. if(
cf740 70 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 pOpen ){. pOp
cf750 65 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 en->nRef--;.
cf760 69 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d if( pOpen->nRef=
cf770 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =0 ){. if(
cf780 70 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a pOpen->pPrev ){.
cf790 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
cf7a0 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e pOpen->pPrev->pN
cf7b0 65 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 ext==pOpen );.
cf7c0 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 pOpen->pPr
cf7d0 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 ev->pNext = pOpe
cf7e0 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 n->pNext;.
cf7f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 }else{. a
cf800 73 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d ssert( openList=
cf810 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 =pOpen );.
cf820 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 openList = pOp
cf830 65 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 en->pNext;.
cf840 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 }. if( pOp
cf850 65 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 en->pNext ){.
cf860 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
cf870 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 en->pNext->pPrev
cf880 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 ==pOpen );.
cf890 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d pOpen->pNext-
cf8a0 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e >pPrev = pOpen->
cf8b0 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 23 pPrev;. }.#
cf8c0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 if SQLITE_THREAD
cf8d0 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 SAFE && defined(
cf8e0 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 20 20 20 __linux__).
cf8f0 20 61 73 73 65 72 74 28 20 21 70 4f 70 65 6e 2d assert( !pOpen-
cf900 3e 70 55 6e 75 73 65 64 20 7c 7c 20 74 68 72 65 >pUnused || thre
cf910 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f adsOverrideEachO
cf920 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 3b thersLocks==0 );
cf930 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f .#endif.. /
cf940 2a 20 49 66 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 * If pOpen->pUnu
cf950 73 65 64 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c sed is not null,
cf960 20 74 68 65 6e 20 6d 65 6d 6f 72 79 20 61 6e 64 then memory and
cf970 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 file-descriptor
cf980 73 0a 20 20 20 20 20 20 2a 2a 20 61 72 65 20 6c s. ** are l
cf990 65 61 6b 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a eaked.. **.
cf9a0 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 77 69 ** This wi
cf9b0 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 ll only happen i
cf9c0 66 2c 20 75 6e 64 65 72 20 4c 69 6e 75 78 74 68 f, under Linuxth
cf9d0 72 65 61 64 73 2c 20 74 68 65 20 75 73 65 72 20 reads, the user
cf9e0 68 61 73 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 has opened.
cf9f0 20 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f ** a transactio
cfa00 6e 20 69 6e 20 6f 6e 65 20 74 68 72 65 61 64 2c n in one thread,
cfa10 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 then attempts t
cfa20 6f 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61 o close the data
cfa30 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 base. ** ha
cfa40 6e 64 6c 65 20 66 72 6f 6d 20 61 6e 6f 74 68 65 ndle from anothe
cfa50 72 20 74 68 72 65 61 64 20 28 77 69 74 68 6f 75 r thread (withou
cfa60 74 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 69 6e t first unlockin
cfa70 67 20 74 68 65 20 64 62 20 66 69 6c 65 29 2e 0a g the db file)..
cfa80 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 ** This is
cfa90 20 61 20 6d 69 73 75 73 65 2e 20 20 2a 2f 0a 20 a misuse. */.
cfaa0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
cfab0 65 28 70 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a e(pOpen);. }.
cfac0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 }.}../*.** Giv
cfad0 65 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 en a file descri
cfae0 70 74 6f 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 ptor, locate uni
cfaf0 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e xLockInfo and un
cfb00 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 ixOpenCnt struct
cfb10 75 72 65 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 ures that.** des
cfb20 63 72 69 62 65 73 20 74 68 61 74 20 66 69 6c 65 cribes that file
cfb30 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 72 descriptor. Cr
cfb40 65 61 74 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 eate new ones if
cfb50 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65 necessary. The
cfb60 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 .** return value
cfb70 73 20 6d 69 67 68 74 20 62 65 20 75 6e 69 6e 69 s might be unini
cfb80 74 69 61 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 tialized if an e
cfb90 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a rror occurs..**.
cfba0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 65 6e 74 ** The mutex ent
cfbb0 65 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 75 ered using the u
cfbc0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 nixEnterMutex()
cfbd0 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 function must be
cfbe0 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 held.** when th
cfbf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
cfc00 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 alled..**.** Ret
cfc10 75 72 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 urn an appropria
cfc20 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a te error code..*
cfc30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e /.static int fin
cfc40 64 4c 6f 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 dLockInfo(. uni
cfc50 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 xFile *pFile,
cfc60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
cfc70 6e 69 78 20 66 69 6c 65 20 77 69 74 68 20 66 69 nix file with fi
cfc80 6c 65 20 64 65 73 63 20 75 73 65 64 20 69 6e 20 le desc used in
cfc90 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 the key */. str
cfca0 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
cfcb0 20 2a 2a 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 **ppLock, /* R
cfcc0 65 74 75 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f eturn the unixLo
cfcd0 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 ckInfo structure
cfce0 20 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 here */. struc
cfcf0 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a t unixOpenCnt **
cfd00 70 70 4f 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 ppOpen /* Ret
cfd10 75 72 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e urn the unixOpen
cfd20 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 68 65 Cnt structure he
cfd30 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 re */.){. int r
cfd40 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
cfd50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 73 /* Sys
cfd60 74 65 6d 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 tem call return
cfd70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 code */. int fd
cfd80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
cfd90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
cfda0 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
cfdb0 66 6f 72 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 for pFile */. s
cfdc0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 truct unixLockKe
cfdd0 79 20 6c 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a y lockKey; /*
cfde0 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 Lookup key for
cfdf0 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f the unixLockInfo
cfe00 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 structure */.
cfe10 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 struct unixFileI
cfe20 64 20 66 69 6c 65 49 64 3b 20 20 20 20 20 20 2f d fileId; /
cfe30 2a 20 4c 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 * Lookup key for
cfe40 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 the unixOpenCnt
cfe50 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 struct */. str
cfe60 75 63 74 20 73 74 61 74 20 73 74 61 74 62 75 66 uct stat statbuf
cfe70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c ; /* L
cfe80 6f 77 2d 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e ow-level file in
cfe90 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 formation */. s
cfea0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e truct unixLockIn
cfeb0 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a fo *pLock = 0;/*
cfec0 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4c Candidate unixL
cfed0 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a ockInfo object *
cfee0 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f /. struct unixO
cfef0 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 penCnt *pOpen;
cff00 20 20 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 /* Candidate
cff10 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 unixOpenCnt obje
cff20 63 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 ct */.. assert(
cff30 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29 unixMutexHeld()
cff40 20 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f );.. /* Get lo
cff50 77 2d 6c 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 w-level informat
cff60 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66 69 ion about the fi
cff70 6c 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 75 le that we can u
cff80 73 65 64 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 sed to. ** crea
cff90 74 65 20 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 te a unique name
cffa0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 for the file..
cffb0 20 2a 2f 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 */. fd = pFile
cffc0 2d 3e 68 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 ->h;. rc = fsta
cffd0 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b t(fd, &statbuf);
cffe0 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a . if( rc!=0 ){.
cfff0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
d0000 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 rrno = errno;.#i
d0010 66 64 65 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 fdef EOVERFLOW.
d0020 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 if( pFile->la
d0030 73 74 45 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c stErrno==EOVERFL
d0040 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 OW ) return SQLI
d0050 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 TE_NOLFS;.#endif
d0060 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d0070 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 TE_IOERR;. }..#
d0080 69 66 64 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a ifdef __APPLE__.
d0090 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 /* On OS X on
d00a0 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 an msdos filesys
d00b0 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e tem, the inode n
d00c0 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65 umber is reporte
d00d0 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 d. ** incorrect
d00e0 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 ly for zero-size
d00f0 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63 files. See tic
d0100 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77 ket #3260. To w
d0110 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 ork. ** around
d0120 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 this problem (we
d0130 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62 consider it a b
d0140 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 ug in OS X, not
d0150 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 SQLite). ** we
d0160 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20 always increase
d0170 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f the file size to
d0180 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20 1 by writing a
d0190 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a single byte. **
d01a0 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73 prior to access
d01b0 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 ing the inode nu
d01c0 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 mber. The one b
d01d0 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20 yte written is.
d01e0 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27 ** an ASCII 'S'
d01f0 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68 character which
d0200 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f also happens to
d0210 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79 be the first by
d0220 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 te. ** in the h
d0230 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53 eader of every S
d0240 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20 QLite database.
d0250 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66 In this way, if
d0260 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 there. ** is a
d0270 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 race condition
d0280 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65 such that anothe
d0290 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72 r thread has alr
d02a0 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 eady populated.
d02b0 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61 ** the first pa
d02c0 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
d02d0 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 se, no damage is
d02e0 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 done.. */. if
d02f0 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a ( statbuf.st_siz
d0300 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d e==0 ){. rc =
d0310 20 77 72 69 74 65 28 66 64 2c 20 22 53 22 2c 20 write(fd, "S",
d0320 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 1);. if( rc!=
d0330 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 1 ){. retur
d0340 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a n SQLITE_IOERR;.
d0350 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 }. rc = f
d0360 73 74 61 74 28 66 64 2c 20 26 73 74 61 74 62 75 stat(fd, &statbu
d0370 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d f);. if( rc!=
d0380 30 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 0 ){. pFile
d0390 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
d03a0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 rno;. retur
d03b0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a n SQLITE_IOERR;.
d03c0 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
d03d0 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b .. memset(&lock
d03e0 4b 65 79 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c Key, 0, sizeof(l
d03f0 6f 63 6b 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b ockKey));. lock
d0400 4b 65 79 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 Key.fid.dev = st
d0410 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 atbuf.st_dev;.#i
d0420 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c f OS_VXWORKS. l
d0430 6f 63 6b 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d ockKey.fid.pId =
d0440 20 70 46 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c pFile->pId;.#el
d0450 73 65 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 se. lockKey.fid
d0460 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 .ino = statbuf.s
d0470 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 t_ino;.#endif.#i
d0480 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 f SQLITE_THREADS
d0490 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f AFE && defined(_
d04a0 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 _linux__). if(
d04b0 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 threadsOverrideE
d04c0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 achOthersLocks<0
d04d0 20 29 7b 0a 20 20 20 20 74 65 73 74 54 68 72 65 ){. testThre
d04e0 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f adLockingBehavio
d04f0 72 28 66 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 r(fd);. }. loc
d0500 6b 4b 65 79 2e 74 69 64 20 3d 20 74 68 72 65 61 kKey.tid = threa
d0510 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 dsOverrideEachOt
d0520 68 65 72 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 hersLocks ? 0 :
d0530 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a pthread_self();.
d0540 23 65 6e 64 69 66 0a 20 20 66 69 6c 65 49 64 20 #endif. fileId
d0550 3d 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 = lockKey.fid;.
d0560 20 69 66 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 if( ppLock!=0 )
d0570 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f {. pLock = lo
d0580 63 6b 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c ckList;. whil
d0590 65 28 20 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 e( pLock && memc
d05a0 6d 70 28 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c mp(&lockKey, &pL
d05b0 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 ock->lockKey, si
d05c0 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 zeof(lockKey)) )
d05d0 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 {. pLock =
d05e0 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 pLock->pNext;.
d05f0 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 }. if( pLoc
d0600 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4c k==0 ){. pL
d0610 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 ock = sqlite3_ma
d0620 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c lloc( sizeof(*pL
d0630 6f 63 6b 29 20 29 3b 0a 20 20 20 20 20 20 69 66 ock) );. if
d0640 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 ( pLock==0 ){.
d0650 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
d0660 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 E_NOMEM;.
d0670 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c goto exit_findl
d0680 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d ockinfo;. }
d0690 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 . memcpy(&p
d06a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 26 6c Lock->lockKey,&l
d06b0 6f 63 6b 4b 65 79 2c 73 69 7a 65 6f 66 28 6c 6f ockKey,sizeof(lo
d06c0 63 6b 4b 65 79 29 29 3b 0a 20 20 20 20 20 20 70 ckKey));. p
d06d0 4c 6f 63 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a Lock->nRef = 1;.
d06e0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 pLock->cnt
d06f0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 = 0;. pLoc
d0700 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 30 3b k->locktype = 0;
d0710 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e . pLock->pN
d0720 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a ext = lockList;.
d0730 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 pLock->pPr
d0740 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 ev = 0;. if
d0750 28 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63 ( lockList ) loc
d0760 6b 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 kList->pPrev = p
d0770 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b Lock;. lock
d0780 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 List = pLock;.
d0790 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
d07a0 4c 6f 63 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 Lock->nRef++;.
d07b0 20 20 7d 0a 20 20 20 20 2a 70 70 4c 6f 63 6b 20 }. *ppLock
d07c0 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 69 = pLock;. }. i
d07d0 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b 0a f( ppOpen!=0 ){.
d07e0 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 65 6e pOpen = open
d07f0 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 List;. while(
d0800 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 6d 70 pOpen && memcmp
d0810 28 26 66 69 6c 65 49 64 2c 20 26 70 4f 70 65 6e (&fileId, &pOpen
d0820 2d 3e 66 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 ->fileId, sizeof
d0830 28 66 69 6c 65 49 64 29 29 20 29 7b 0a 20 20 20 (fileId)) ){.
d0840 20 20 20 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e pOpen = pOpen
d0850 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 ->pNext;. }.
d0860 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 if( pOpen==0
d0870 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d ){. pOpen =
d0880 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
d0890 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 20 sizeof(*pOpen)
d08a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 );. if( pOp
d08b0 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 en==0 ){.
d08c0 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f releaseLockInfo
d08d0 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 (pLock);.
d08e0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
d08f0 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f EM;. goto
d0900 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e exit_findlockin
d0910 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 fo;. }.
d0920 20 20 6d 65 6d 73 65 74 28 70 4f 70 65 6e 2c 20 memset(pOpen,
d0930 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 0, sizeof(*pOpen
d0940 29 29 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d ));. pOpen-
d0950 3e 66 69 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 >fileId = fileId
d0960 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e ;. pOpen->n
d0970 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 Ref = 1;. p
d0980 4f 70 65 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 Open->pNext = op
d0990 65 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 69 66 enList;. if
d09a0 28 20 6f 70 65 6e 4c 69 73 74 20 29 20 6f 70 65 ( openList ) ope
d09b0 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 nList->pPrev = p
d09c0 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f 70 65 6e Open;. open
d09d0 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 List = pOpen;.
d09e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
d09f0 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 Open->nRef++;.
d0a00 20 20 7d 0a 20 20 20 20 2a 70 70 4f 70 65 6e 20 }. *ppOpen
d0a10 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65 78 = pOpen;. }..ex
d0a20 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3a it_findlockinfo:
d0a30 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
d0a40 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 ./*.** If we are
d0a50 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 currently in a
d0a60 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 different thread
d0a70 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 64 than the thread
d0a80 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69 that the.** uni
d0a90 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 62 xFile argument b
d0aa0 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20 elongs to, then
d0ab0 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 transfer ownersh
d0ac0 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 ip of the unixFi
d0ad0 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68 le.** over to th
d0ae0 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 e current thread
d0af0 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69 ..**.** A unixFi
d0b00 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64 le is only owned
d0b10 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e 20 by a thread on
d0b20 73 79 73 74 65 6d 73 20 74 68 61 74 20 75 73 65 systems that use
d0b30 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a LinuxThreads..*
d0b40 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 *.** Ownership t
d0b50 72 61 6e 73 66 65 72 20 69 73 20 6f 6e 6c 79 20 ransfer is only
d0b60 61 6c 6c 6f 77 65 64 20 69 66 20 74 68 65 20 75 allowed if the u
d0b70 6e 69 78 46 69 6c 65 20 69 73 20 63 75 72 72 65 nixFile is curre
d0b80 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a ntly unlocked..*
d0b90 2a 20 49 66 20 74 68 65 20 75 6e 69 78 46 69 6c * If the unixFil
d0ba0 65 20 69 73 20 6c 6f 63 6b 65 64 20 61 6e 64 20 e is locked and
d0bb0 61 6e 20 6f 77 6e 65 72 73 68 69 70 20 69 73 20 an ownership is
d0bc0 77 72 6f 6e 67 2c 20 74 68 65 6e 20 72 65 74 75 wrong, then retu
d0bd0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 rn.** SQLITE_MIS
d0be0 55 53 45 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 20 USE. SQLITE_OK
d0bf0 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 65 is returned if e
d0c00 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2e verything works.
d0c10 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 .*/.#if SQLITE_T
d0c20 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 HREADSAFE && def
d0c30 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a ined(__linux__).
d0c40 73 74 61 74 69 63 20 69 6e 74 20 74 72 61 6e 73 static int trans
d0c50 66 65 72 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 ferOwnership(uni
d0c60 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 xFile *pFile){.
d0c70 20 69 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 65 int rc;. pthre
d0c80 61 64 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 ad_t hSelf;. if
d0c90 28 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 ( threadsOverrid
d0ca0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 eEachOthersLocks
d0cb0 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 ){. /* Owner
d0cc0 73 68 69 70 20 74 72 61 6e 73 66 65 72 73 20 6e ship transfers n
d0cd0 6f 74 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 ot needed on thi
d0ce0 73 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 s system */.
d0cf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d0d00 3b 0a 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 ;. }. hSelf =
d0d10 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a pthread_self();.
d0d20 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 65 71 if( pthread_eq
d0d30 75 61 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 ual(pFile->tid,
d0d40 68 53 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a hSelf) ){. /*
d0d50 20 57 65 20 61 72 65 20 73 74 69 6c 6c 20 69 6e We are still in
d0d60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 the same thread
d0d70 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 31 */. OSTRACE1
d0d80 28 22 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 ("No-transfer, s
d0d90 61 6d 65 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a ame thread\n");.
d0da0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d0db0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 E_OK;. }. if(
d0dc0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 pFile->locktype!
d0dd0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 =NO_LOCK ){.
d0de0 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 /* We cannot cha
d0df0 6e 67 65 20 6f 77 6e 65 72 73 68 69 70 20 77 68 nge ownership wh
d0e00 69 6c 65 20 77 65 20 61 72 65 20 68 6f 6c 64 69 ile we are holdi
d0e10 6e 67 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 ng a lock! */.
d0e20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d0e30 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 MISUSE;. }. OS
d0e40 54 52 41 43 45 34 28 22 54 72 61 6e 73 66 65 72 TRACE4("Transfer
d0e50 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 25 64 ownership of %d
d0e60 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e from %d to %d\n
d0e70 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ",. p
d0e80 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e File->h, pFile->
d0e90 74 69 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 tid, hSelf);. p
d0ea0 46 69 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c File->tid = hSel
d0eb0 66 3b 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e f;. if (pFile->
d0ec0 70 4c 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b pLock != NULL) {
d0ed0 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b . releaseLock
d0ee0 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 Info(pFile->pLoc
d0ef0 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e k);. rc = fin
d0f00 64 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2c dLockInfo(pFile,
d0f10 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 &pFile->pLock,
d0f20 30 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 35 0);. OSTRACE5
d0f30 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69 73 20 ("LOCK %d is
d0f40 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c 6e 22 now %s(%s,%d)\n"
d0f50 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
d0f60 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e locktypeN
d0f70 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 ame(pFile->lockt
d0f80 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 ype),.
d0f90 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 locktypeName(pF
d0fa0 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b ile->pLock->lock
d0fb0 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e 70 4c type), pFile->pL
d0fc0 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20 20 72 ock->cnt);. r
d0fd0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c eturn rc;. } el
d0fe0 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 se {. return
d0ff0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d SQLITE_OK;. }.}
d1000 0a 23 65 6c 73 65 20 20 2f 2a 20 69 66 20 6e 6f .#else /* if no
d1010 74 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 t SQLITE_THREADS
d1020 41 46 45 20 2a 2f 0a 20 20 2f 2a 20 4f 6e 20 73 AFE */. /* On s
d1030 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 62 ingle-threaded b
d1040 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68 69 70 uilds, ownership
d1050 20 74 72 61 6e 73 66 65 72 20 69 73 20 61 20 6e transfer is a n
d1060 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69 6e 65 o-op */.# define
d1070 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 transferOwnersh
d1080 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f 4b 0a ip(X) SQLITE_OK.
d1090 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
d10a0 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 0a _THREADSAFE */..
d10b0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
d10c0 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 ine checks if th
d10d0 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 ere is a RESERVE
d10e0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 D lock held on t
d10f0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 he specified.**
d1100 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 file by this or
d1110 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 any other proces
d1120 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 s. If such a loc
d1130 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a k is held, set *
d1140 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 pResOut.** to a
d1150 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f non-zero value o
d1160 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 therwise *pResOu
d1170 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f t is set to zero
d1180 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 . The return va
d1190 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f lue.** is set to
d11a0 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 SQLITE_OK unles
d11b0 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f s an I/O error o
d11c0 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 ccurs during loc
d11d0 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 k checking..*/.s
d11e0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 68 tatic int unixCh
d11f0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d1200 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d1210 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b , int *pResOut){
d1220 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
d1230 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 TE_OK;. int res
d1240 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 erved = 0;. uni
d1250 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
d1260 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 unixFile*)id;..
d1270 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d1280 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ( return SQLITE_
d1290 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 IOERR_CHECKRESER
d12a0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 20 61 VEDLOCK; );.. a
d12b0 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a ssert( pFile );.
d12c0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 unixEnterMutex
d12d0 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 70 (); /* Because p
d12e0 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 File->pLock is s
d12f0 68 61 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 hared across thr
d1300 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 eads */.. /* Ch
d1310 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 eck if a thread
d1320 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 in this process
d1330 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 holds such a loc
d1340 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 k */. if( pFile
d1350 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 ->pLock->locktyp
d1360 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b e>SHARED_LOCK ){
d1370 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 . reserved =
d1380 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68 1;. }.. /* Oth
d1390 65 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f erwise see if so
d13a0 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 me other process
d13b0 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a holds it.. */.
d13c0 23 69 66 6e 64 65 66 20 5f 5f 44 4a 47 50 50 5f #ifndef __DJGPP_
d13d0 5f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 _. if( !reserve
d13e0 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 d ){. struct
d13f0 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20 flock lock;.
d1400 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 lock.l_whence =
d1410 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f SEEK_SET;. lo
d1420 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 ck.l_start = RES
d1430 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 ERVED_BYTE;.
d1440 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a lock.l_len = 1;.
d1450 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 lock.l_type
d1460 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69 = F_WRLCK;. i
d1470 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70 f (-1 == fcntl(p
d1480 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b File->h, F_GETLK
d1490 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20 , &lock)) {.
d14a0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 int tErrno = e
d14b0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d rrno;. rc =
d14c0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d14d0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d14e0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d14f0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
d1500 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d K);. pFile-
d1510 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d1520 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 rno;. } else
d1530 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 if( lock.l_type!
d1540 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 =F_UNLCK ){.
d1550 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a reserved = 1;.
d1560 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
d1570 0a 20 20 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d . . unixLeaveM
d1580 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 utex();. OSTRAC
d1590 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b E4("TEST WR-LOCK
d15a0 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 %d %d %d\n", pF
d15b0 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 ile->h, rc, rese
d15c0 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f rved);.. *pResO
d15d0 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 ut = reserved;.
d15e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
d15f0 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 66 *.** Perform a f
d1600 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 ile locking oper
d1610 61 74 69 6f 6e 20 6f 6e 20 61 20 72 61 6e 67 65 ation on a range
d1620 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 66 of bytes in a f
d1630 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 22 6f 70 22 ile..** The "op"
d1640 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c parameter shoul
d1650 64 20 62 65 20 6f 6e 65 20 6f 66 20 46 5f 52 44 d be one of F_RD
d1660 4c 43 4b 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 72 LCK, F_WRLCK, or
d1670 20 46 5f 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 65 74 F_UNLCK..** Ret
d1680 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 urn 0 on success
d1690 20 6f 72 20 2d 31 20 66 6f 72 20 66 61 69 6c 75 or -1 for failu
d16a0 72 65 2e 20 20 4f 6e 20 66 61 69 6c 75 72 65 2c re. On failure,
d16b0 20 77 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 write the error
d16c0 0a 2a 2a 20 63 6f 64 65 20 69 6e 74 6f 20 2a 70 .** code into *p
d16d0 45 72 72 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 Errcode..**.** I
d16e0 66 20 74 68 65 20 53 51 4c 49 54 45 5f 57 48 4f f the SQLITE_WHO
d16f0 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 LE_FILE_LOCKING
d1700 62 69 74 20 69 73 20 63 6c 65 61 72 2c 20 74 68 bit is clear, th
d1710 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 en only lock.**
d1720 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 the range of byt
d1730 65 73 20 6f 6e 20 74 68 65 20 6c 6f 63 6b 69 6e es on the lockin
d1740 67 20 70 61 67 65 20 62 65 74 77 65 65 6e 20 53 g page between S
d1750 48 41 52 45 44 5f 46 49 52 53 54 20 61 6e 64 0a HARED_FIRST and.
d1760 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 2e 20 ** SHARED_SIZE.
d1770 20 49 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 If SQLITE_WHOLE
d1780 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 _FILE_LOCKING is
d1790 20 73 65 74 2c 20 74 68 65 6e 20 6c 6f 63 6b 20 set, then lock
d17a0 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 66 72 6f all.** bytes fro
d17b0 6d 20 30 20 75 70 20 74 6f 20 62 75 74 20 6e 6f m 0 up to but no
d17c0 74 20 69 6e 63 6c 75 64 69 6e 67 20 50 45 4e 44 t including PEND
d17d0 49 4e 47 5f 42 59 54 45 2c 20 61 6e 64 20 61 6c ING_BYTE, and al
d17e0 6c 20 62 79 74 65 73 0a 2a 2a 20 74 68 61 74 20 l bytes.** that
d17f0 66 6f 6c 6c 6f 77 20 53 48 41 52 45 44 5f 46 49 follow SHARED_FI
d1800 52 53 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 RST..**.** In ot
d1810 68 65 72 20 77 6f 72 64 73 2c 20 6f 66 20 53 51 her words, of SQ
d1820 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f LITE_WHOLE_FILE_
d1830 4c 4f 43 4b 49 4e 47 20 69 66 20 66 61 6c 73 65 LOCKING if false
d1840 20 28 74 68 65 20 68 69 73 74 6f 72 69 63 61 6c (the historical
d1850 0a 2a 2a 20 64 65 66 61 75 6c 74 20 63 61 73 65 .** default case
d1860 29 20 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b ) then only lock
d1870 20 61 20 73 6d 61 6c 6c 20 72 61 6e 67 65 20 6f a small range o
d1880 66 20 62 79 74 65 73 20 66 72 6f 6d 20 53 48 41 f bytes from SHA
d1890 52 45 44 5f 46 49 52 53 54 0a 2a 2a 20 74 68 72 RED_FIRST.** thr
d18a0 6f 75 67 68 20 53 48 41 52 45 44 5f 46 49 52 53 ough SHARED_FIRS
d18b0 54 2b 53 48 41 52 45 44 5f 53 49 5a 45 2d 31 2e T+SHARED_SIZE-1.
d18c0 20 20 42 75 74 20 69 66 20 53 51 4c 49 54 45 5f But if SQLITE_
d18d0 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 WHOLE_FILE_LOCKI
d18e0 4e 47 20 69 73 0a 2a 2a 20 74 72 75 65 20 74 68 NG is.** true th
d18f0 65 6e 20 6c 6f 63 6b 20 65 76 65 72 79 20 62 79 en lock every by
d1900 74 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 65 te in the file e
d1910 78 63 65 70 74 20 66 6f 72 20 50 45 4e 44 49 4e xcept for PENDIN
d1920 47 5f 42 59 54 45 20 61 6e 64 0a 2a 2a 20 52 45 G_BYTE and.** RE
d1930 53 45 52 56 45 44 5f 42 59 54 45 2e 0a 2a 2a 0a SERVED_BYTE..**.
d1940 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f ** SQLITE_WHOLE_
d1950 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 FILE_LOCKING=tru
d1960 65 20 6f 76 65 72 6c 61 70 73 20 53 51 4c 49 54 e overlaps SQLIT
d1970 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 E_WHOLE_FILE_LOC
d1980 4b 49 4e 47 3d 66 61 6c 73 65 0a 2a 2a 20 61 6e KING=false.** an
d1990 64 20 73 6f 20 74 68 65 20 6c 6f 63 6b 69 6e 67 d so the locking
d19a0 20 73 63 68 65 6d 65 73 20 61 72 65 20 63 6f 6d schemes are com
d19b0 70 61 74 69 62 6c 65 2e 20 20 4f 6e 65 20 74 79 patible. One ty
d19c0 70 65 20 6f 66 20 6c 6f 63 6b 20 77 69 6c 6c 0a pe of lock will.
d19d0 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20 65 ** effectively e
d19e0 78 63 6c 75 64 65 20 74 68 65 20 6f 74 68 65 72 xclude the other
d19f0 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 61 73 type. The reas
d1a00 6f 6e 20 66 6f 72 20 75 73 69 6e 67 20 74 68 65 on for using the
d1a10 0a 2a 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 .** SQLITE_WHOLE
d1a20 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 _FILE_LOCKING=tr
d1a30 75 65 20 69 73 20 74 68 61 74 20 62 79 20 69 6e ue is that by in
d1a40 64 69 63 61 74 69 6e 67 20 74 68 65 20 66 75 6c dicating the ful
d1a50 6c 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 62 79 l range.** of by
d1a60 74 65 73 20 74 6f 20 62 65 20 72 65 61 64 20 6f tes to be read o
d1a70 72 20 77 72 69 74 74 65 6e 2c 20 77 65 20 67 69 r written, we gi
d1a80 76 65 20 68 69 6e 74 73 20 74 6f 20 4e 46 53 20 ve hints to NFS
d1a90 74 6f 20 68 65 6c 70 20 69 74 0a 2a 2a 20 6d 61 to help it.** ma
d1aa0 69 6e 74 61 69 6e 20 63 61 63 68 65 20 63 6f 68 intain cache coh
d1ab0 65 72 65 6e 63 79 2e 20 20 4f 6e 20 74 68 65 20 erency. On the
d1ac0 6f 74 68 65 72 20 68 61 6e 64 2c 20 77 68 6f 6c other hand, whol
d1ad0 65 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a e file locking.*
d1ae0 2a 20 69 73 20 73 6c 6f 77 65 72 2c 20 73 6f 20 * is slower, so
d1af0 77 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f we don't want to
d1b00 20 75 73 65 20 69 74 20 65 78 63 65 70 74 20 66 use it except f
d1b10 6f 72 20 4e 46 53 2e 0a 2a 2f 0a 73 74 61 74 69 or NFS..*/.stati
d1b20 63 20 69 6e 74 20 72 61 6e 67 65 4c 6f 63 6b 28 c int rangeLock(
d1b30 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c unixFile *pFile,
d1b40 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 45 int op, int *pE
d1b50 72 72 63 6f 64 65 29 7b 0a 20 20 73 74 72 75 63 rrcode){. struc
d1b60 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 t flock lock;.
d1b70 69 6e 74 20 72 63 3b 0a 20 20 6c 6f 63 6b 2e 6c int rc;. lock.l
d1b80 5f 74 79 70 65 20 3d 20 6f 70 3b 0a 20 20 6c 6f _type = op;. lo
d1b90 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 ck.l_start = SHA
d1ba0 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 6c 6f 63 RED_FIRST;. loc
d1bb0 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 k.l_whence = SEE
d1bc0 4b 5f 53 45 54 3b 0a 20 20 69 66 28 20 28 70 46 K_SET;. if( (pF
d1bd0 69 6c 65 2d 3e 66 69 6c 65 46 6c 61 67 73 20 26 ile->fileFlags &
d1be0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
d1bf0 4c 45 5f 4c 4f 43 4b 49 4e 47 29 3d 3d 30 20 29 LE_LOCKING)==0 )
d1c00 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e {. lock.l_len
d1c10 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a = SHARED_SIZE;.
d1c20 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 rc = fcntl(p
d1c30 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b File->h, F_SETLK
d1c40 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 , &lock);. *p
d1c50 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b Errcode = errno;
d1c60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f . }else{. lo
d1c70 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 ck.l_len = 0;.
d1c80 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 rc = fcntl(pFi
d1c90 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 le->h, F_SETLK,
d1ca0 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 &lock);. *pEr
d1cb0 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 rcode = errno;.
d1cc0 20 20 20 69 66 28 20 4e 45 56 45 52 28 6f 70 3d if( NEVER(op=
d1cd0 3d 46 5f 55 4e 4c 43 4b 29 20 7c 7c 20 72 63 21 =F_UNLCK) || rc!
d1ce0 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 6c =(-1) ){. l
d1cf0 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b ock.l_start = 0;
d1d00 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 . lock.l_le
d1d10 6e 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 n = PENDING_BYTE
d1d20 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 63 6e ;. rc = fcn
d1d30 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 tl(pFile->h, F_S
d1d40 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 ETLK, &lock);.
d1d50 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 6f if( ALWAYS(o
d1d60 70 21 3d 46 5f 55 4e 4c 43 4b 29 20 26 26 20 72 p!=F_UNLCK) && r
d1d70 63 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 c==(-1) ){.
d1d80 20 20 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 *pErrcode = e
d1d90 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 6c 6f rrno;. lo
d1da0 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e ck.l_type = F_UN
d1db0 4c 43 4b 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 LCK;. loc
d1dc0 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 k.l_start = SHAR
d1dd0 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 ED_FIRST;.
d1de0 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 lock.l_len = 0
d1df0 3b 0a 20 20 20 20 20 20 20 20 66 63 6e 74 6c 28 ;. fcntl(
d1e00 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c pFile->h, F_SETL
d1e10 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 K, &lock);.
d1e20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
d1e30 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d1e40 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
d1e50 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
d1e60 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
d1e70 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
d1e80 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
d1e90 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
d1ea0 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
d1eb0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
d1ec0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
d1ed0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
d1ee0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
d1ef0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
d1f00 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
d1f10 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
d1f20 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
d1f30 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
d1f40 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
d1f50 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
d1f60 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
d1f70 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
d1f80 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
d1f90 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
d1fa0 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
d1fb0 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
d1fc0 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
d1fd0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
d1fe0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
d1ff0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
d2000 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
d2010 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
d2020 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
d2030 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
d2040 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
d2050 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
d2060 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
d2070 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
d2080 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
d2090 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d20a0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d20b0 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
d20c0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
d20d0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
d20e0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
d20f0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
d2100 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 e will only incr
d2110 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 ease a lock. Us
d2120 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 e the sqlite3OsU
d2130 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 nlock().** routi
d2140 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f ne to lower a lo
d2150 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a cking level..*/.
d2160 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4c static int unixL
d2170 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
d2180 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
d2190 70 65 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 6f pe){. /* The fo
d21a0 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65 llowing describe
d21b0 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 s the implementa
d21c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 72 69 tion of the vari
d21d0 6f 75 73 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 ous locks and.
d21e0 2a 2a 20 6c 6f 63 6b 20 74 72 61 6e 73 69 74 69 ** lock transiti
d21f0 6f 6e 73 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 ons in terms of
d2200 74 68 65 20 50 4f 53 49 58 20 61 64 76 69 73 6f the POSIX adviso
d2210 72 79 20 73 68 61 72 65 64 20 61 6e 64 20 65 78 ry shared and ex
d2220 63 6c 75 73 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 clusive. ** loc
d2230 6b 20 70 72 69 6d 69 74 69 76 65 73 20 28 63 61 k primitives (ca
d2240 6c 6c 65 64 20 72 65 61 64 2d 6c 6f 63 6b 73 20 lled read-locks
d2250 61 6e 64 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20 and write-locks
d2260 62 65 6c 6f 77 2c 20 74 6f 20 61 76 6f 69 64 0a below, to avoid.
d2270 20 20 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 ** confusion w
d2280 69 74 68 20 53 51 4c 69 74 65 20 6c 6f 63 6b 20 ith SQLite lock
d2290 6e 61 6d 65 73 29 2e 20 54 68 65 20 61 6c 67 6f names). The algo
d22a0 72 69 74 68 6d 73 20 61 72 65 20 63 6f 6d 70 6c rithms are compl
d22b0 69 63 61 74 65 64 0a 20 20 2a 2a 20 73 6c 69 67 icated. ** slig
d22c0 68 74 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f htly in order to
d22d0 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 be compatible w
d22e0 69 74 68 20 77 69 6e 64 6f 77 73 20 73 79 73 74 ith windows syst
d22f0 65 6d 73 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 ems simultaneous
d2300 6c 79 0a 20 20 2a 2a 20 61 63 63 65 73 73 69 6e ly. ** accessin
d2310 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 g the same datab
d2320 61 73 65 20 66 69 6c 65 2c 20 69 6e 20 63 61 73 ase file, in cas
d2330 65 20 74 68 61 74 20 69 73 20 65 76 65 72 20 72 e that is ever r
d2340 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 equired.. **.
d2350 2a 2a 20 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e ** Symbols defin
d2360 65 64 20 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e ed in os.h inden
d2370 74 69 66 79 20 74 68 65 20 27 70 65 6e 64 69 6e tify the 'pendin
d2380 67 20 62 79 74 65 27 20 61 6e 64 20 74 68 65 20 g byte' and the
d2390 27 72 65 73 65 72 76 65 64 0a 20 20 2a 2a 20 62 'reserved. ** b
d23a0 79 74 65 27 2c 20 65 61 63 68 20 73 69 6e 67 6c yte', each singl
d23b0 65 20 62 79 74 65 73 20 61 74 20 77 65 6c 6c 20 e bytes at well
d23c0 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 73 2c 20 61 known offsets, a
d23d0 6e 64 20 74 68 65 20 27 73 68 61 72 65 64 20 62 nd the 'shared b
d23e0 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c yte. ** range',
d23f0 20 61 20 72 61 6e 67 65 20 6f 66 20 35 31 30 20 a range of 510
d2400 62 79 74 65 73 20 61 74 20 61 20 77 65 6c 6c 20 bytes at a well
d2410 6b 6e 6f 77 6e 20 6f 66 66 73 65 74 2e 0a 20 20 known offset..
d2420 2a 2a 0a 20 20 2a 2a 20 54 6f 20 6f 62 74 61 69 **. ** To obtai
d2430 6e 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c n a SHARED lock,
d2440 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 a read-lock is
d2450 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 obtained on the
d2460 27 70 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 62 79 'pending. ** by
d2470 74 65 27 2e 20 20 49 66 20 74 68 69 73 20 69 73 te'. If this is
d2480 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 72 successful, a r
d2490 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 andom byte from
d24a0 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65 the 'shared byte
d24b0 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 . ** range' is
d24c0 72 65 61 64 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 read-locked and
d24d0 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 the lock on the
d24e0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 72 'pending byte' r
d24f0 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 eleased.. **.
d2500 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79 ** A process may
d2510 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 52 only obtain a R
d2520 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 61 66 74 ESERVED lock aft
d2530 65 72 20 69 74 20 68 61 73 20 61 20 53 48 41 52 er it has a SHAR
d2540 45 44 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 ED lock.. ** A
d2550 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 RESERVED lock is
d2560 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 implemented by
d2570 67 72 61 62 62 69 6e 67 20 61 20 77 72 69 74 65 grabbing a write
d2580 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a -lock on the. *
d2590 2a 20 27 72 65 73 65 72 76 65 64 20 62 79 74 65 * 'reserved byte
d25a0 27 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 '. . **. ** A
d25b0 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 process may only
d25c0 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e obtain a PENDIN
d25d0 47 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 G lock after it
d25e0 68 61 73 20 6f 62 74 61 69 6e 65 64 20 61 0a 20 has obtained a.
d25f0 20 2a 2a 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e ** SHARED lock.
d2600 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 A PENDING lock
d2610 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 is implemented b
d2620 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 y obtaining a wr
d2630 69 74 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e ite-lock. ** on
d2640 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 the 'pending by
d2650 74 65 27 2e 20 54 68 69 73 20 65 6e 73 75 72 65 te'. This ensure
d2660 73 20 74 68 61 74 20 6e 6f 20 6e 65 77 20 53 48 s that no new SH
d2670 41 52 45 44 20 6c 6f 63 6b 73 20 63 61 6e 20 62 ARED locks can b
d2680 65 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c e. ** obtained,
d2690 20 62 75 74 20 65 78 69 73 74 69 6e 67 20 53 48 but existing SH
d26a0 41 52 45 44 20 6c 6f 63 6b 73 20 61 72 65 20 61 ARED locks are a
d26b0 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 73 69 73 llowed to persis
d26c0 74 2e 20 41 20 70 72 6f 63 65 73 73 0a 20 20 2a t. A process. *
d26d0 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 * does not have
d26e0 74 6f 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 to obtain a RESE
d26f0 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 RVED lock on the
d2700 20 77 61 79 20 74 6f 20 61 20 50 45 4e 44 49 4e way to a PENDIN
d2710 47 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 G lock.. ** Thi
d2720 73 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 s property is us
d2730 65 64 20 62 79 20 74 68 65 20 61 6c 67 6f 72 69 ed by the algori
d2740 74 68 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 thm for rolling
d2750 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 back a journal f
d2760 69 6c 65 0a 20 20 2a 2a 20 61 66 74 65 72 20 61 ile. ** after a
d2770 20 63 72 61 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a crash.. **. *
d2780 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c * An EXCLUSIVE l
d2790 6f 63 6b 2c 20 6f 62 74 61 69 6e 65 64 20 61 66 ock, obtained af
d27a0 74 65 72 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f ter a PENDING lo
d27b0 63 6b 20 69 73 20 68 65 6c 64 2c 20 69 73 0a 20 ck is held, is.
d27c0 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 ** implemented
d27d0 62 79 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 by obtaining a w
d27e0 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 rite-lock on the
d27f0 20 65 6e 74 69 72 65 20 27 73 68 61 72 65 64 20 entire 'shared
d2800 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 byte. ** range'
d2810 2e 20 53 69 6e 63 65 20 61 6c 6c 20 6f 74 68 65 . Since all othe
d2820 72 20 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 20 r locks require
d2830 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f a read-lock on o
d2840 6e 65 20 6f 66 20 74 68 65 20 62 79 74 65 73 0a ne of the bytes.
d2850 20 20 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 ** within this
d2860 20 72 61 6e 67 65 2c 20 74 68 69 73 20 65 6e 73 range, this ens
d2870 75 72 65 73 20 74 68 61 74 20 6e 6f 20 6f 74 68 ures that no oth
d2880 65 72 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c er locks are hel
d2890 64 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 d on the. ** da
d28a0 74 61 62 61 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 tabase. . **.
d28b0 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 61 20 ** The reason a
d28c0 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61 6e 6e single byte cann
d28d0 6f 74 20 62 65 20 75 73 65 64 20 69 6e 73 74 65 ot be used inste
d28e0 61 64 20 6f 66 20 74 68 65 20 27 73 68 61 72 65 ad of the 'share
d28f0 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 d byte. ** rang
d2900 65 27 20 69 73 20 74 68 61 74 20 73 6f 6d 65 20 e' is that some
d2910 76 65 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 versions of wind
d2920 6f 77 73 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f ows do not suppo
d2930 72 74 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 rt read-locks. B
d2940 79 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 y. ** locking a
d2950 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f random byte fro
d2960 6d 20 61 20 72 61 6e 67 65 2c 20 63 6f 6e 63 75 m a range, concu
d2970 72 72 65 6e 74 20 53 48 41 52 45 44 20 6c 6f 63 rrent SHARED loc
d2980 6b 73 20 6d 61 79 20 65 78 69 73 74 0a 20 20 2a ks may exist. *
d2990 2a 20 65 76 65 6e 20 69 66 20 74 68 65 20 6c 6f * even if the lo
d29a0 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 20 cking primitive
d29b0 75 73 65 64 20 69 73 20 61 6c 77 61 79 73 20 61 used is always a
d29c0 20 77 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a write-lock.. *
d29d0 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
d29e0 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 ITE_OK;. unixFi
d29f0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d2a00 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 73 74 72 xFile*)id;. str
d2a10 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f uct unixLockInfo
d2a20 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d *pLock = pFile-
d2a30 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 >pLock;. struct
d2a40 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 flock lock;. i
d2a50 6e 74 20 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 nt s = 0;. int
d2a60 74 45 72 72 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 tErrno;.. asser
d2a70 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 t( pFile );. OS
d2a80 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 TRACE7("LOCK
d2a90 25 64 20 25 73 20 77 61 73 20 25 73 28 25 73 2c %d %s was %s(%s,
d2aa0 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %d) pid=%d\n", p
d2ab0 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c File->h,. l
d2ac0 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b ocktypeName(lock
d2ad0 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e type), locktypeN
d2ae0 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 ame(pFile->lockt
d2af0 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b ype),. lock
d2b00 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e typeName(pLock->
d2b10 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b locktype), pLock
d2b20 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28 29 ->cnt , getpid()
d2b30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 );.. /* If ther
d2b40 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c e is already a l
d2b50 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 ock of this type
d2b60 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 or more restric
d2b70 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a tive on the. **
d2b80 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f unixFile, do no
d2b90 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 thing. Don't use
d2ba0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 the end_lock: e
d2bb0 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a xit path, as. *
d2bc0 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 * unixEnterMutex
d2bd0 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 () hasn't been c
d2be0 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a alled yet.. */.
d2bf0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 if( pFile->loc
d2c00 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 ktype>=locktype
d2c10 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 ){. OSTRACE3(
d2c20 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 6f "LOCK %d %s o
d2c30 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 k (already held)
d2c40 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 \n", pFile->h,.
d2c50 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 lockt
d2c60 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 ypeName(locktype
d2c70 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ));. return S
d2c80 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
d2c90 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 /* Make sure th
d2ca0 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e e locking sequen
d2cb0 63 65 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 ce is correct..
d2cc0 20 2a 2a 20 20 28 31 29 20 57 65 20 6e 65 76 65 ** (1) We neve
d2cd0 72 20 6d 6f 76 65 20 66 72 6f 6d 20 75 6e 6c 6f r move from unlo
d2ce0 63 6b 65 64 20 74 6f 20 61 6e 79 74 68 69 6e 67 cked to anything
d2cf0 20 68 69 67 68 65 72 20 74 68 61 6e 20 73 68 61 higher than sha
d2d00 72 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 red lock.. **
d2d10 28 32 29 20 53 51 4c 69 74 65 20 6e 65 76 65 72 (2) SQLite never
d2d20 20 65 78 70 6c 69 63 69 74 6c 79 20 72 65 71 75 explicitly requ
d2d30 65 73 74 73 20 61 20 70 65 6e 64 69 67 20 6c 6f ests a pendig lo
d2d40 63 6b 2e 0a 20 20 2a 2a 20 20 28 33 29 20 41 20 ck.. ** (3) A
d2d50 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 73 20 61 shared lock is a
d2d60 6c 77 61 79 73 20 68 65 6c 64 20 77 68 65 6e 20 lways held when
d2d70 61 20 72 65 73 65 72 76 65 20 6c 6f 63 6b 20 69 a reserve lock i
d2d80 73 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 2a s requested.. *
d2d90 2f 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c /. assert( pFil
d2da0 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f e->locktype!=NO_
d2db0 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 LOCK || locktype
d2dc0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b ==SHARED_LOCK );
d2dd0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
d2de0 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 ype!=PENDING_LOC
d2df0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c K );. assert( l
d2e00 6f 63 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 ocktype!=RESERVE
d2e10 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d D_LOCK || pFile-
d2e20 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 >locktype==SHARE
d2e30 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 D_LOCK );.. /*
d2e40 54 68 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 This mutex is ne
d2e50 65 64 65 64 20 62 65 63 61 75 73 65 20 70 46 69 eded because pFi
d2e60 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 le->pLock is sha
d2e70 72 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 red across threa
d2e80 64 73 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e ds. */. unixEn
d2e90 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f terMutex();.. /
d2ea0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
d2eb0 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f current thread o
d2ec0 77 6e 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 wns the pFile..
d2ed0 20 2a 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 */. rc = trans
d2ee0 66 65 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 ferOwnership(pFi
d2ef0 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 le);. if( rc!=S
d2f00 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
d2f10 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 unixLeaveMutex()
d2f20 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b ;. return rc;
d2f30 0a 20 20 7d 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 . }. pLock = p
d2f40 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 File->pLock;..
d2f50 2f 2a 20 49 66 20 73 6f 6d 65 20 74 68 72 65 61 /* If some threa
d2f60 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49 44 d using this PID
d2f70 20 68 61 73 20 61 20 6c 6f 63 6b 20 76 69 61 20 has a lock via
d2f80 61 20 64 69 66 66 65 72 65 6e 74 20 75 6e 69 78 a different unix
d2f90 46 69 6c 65 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c File*. ** handl
d2fa0 65 20 74 68 61 74 20 70 72 65 63 6c 75 64 65 73 e that precludes
d2fb0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c the requested l
d2fc0 6f 63 6b 2c 20 72 65 74 75 72 6e 20 42 55 53 59 ock, return BUSY
d2fd0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 46 .. */. if( (pF
d2fe0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 70 ile->locktype!=p
d2ff0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 26 Lock->locktype &
d3000 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 4c & . (pL
d3010 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 ock->locktype>=P
d3020 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c ENDING_LOCK || l
d3030 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d3040 4f 43 4b 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 OCK)). ){. r
d3050 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b c = SQLITE_BUSY;
d3060 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f . goto end_lo
d3070 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 ck;. }.. /* If
d3080 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 a SHARED lock i
d3090 73 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e 64 s requested, and
d30a0 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69 some thread usi
d30b0 6e 67 20 74 68 69 73 20 50 49 44 20 61 6c 72 65 ng this PID alre
d30c0 61 64 79 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 ady. ** has a S
d30d0 48 41 52 45 44 20 6f 72 20 52 45 53 45 52 56 45 HARED or RESERVE
d30e0 44 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 D lock, then inc
d30f0 72 65 6d 65 6e 74 20 72 65 66 65 72 65 6e 63 65 rement reference
d3100 20 63 6f 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a counts and. **
d3110 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
d3120 4b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f K.. */. if( lo
d3130 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d3140 4f 43 4b 20 26 26 20 0a 20 20 20 20 20 20 28 70 OCK && . (p
d3150 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d Lock->locktype==
d3160 53 48 41 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 SHARED_LOCK || p
d3170 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d Lock->locktype==
d3180 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 29 RESERVED_LOCK) )
d3190 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 6f {. assert( lo
d31a0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d31b0 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 OCK );. asser
d31c0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 t( pFile->lockty
d31d0 70 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 pe==0 );. ass
d31e0 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e ert( pLock->cnt>
d31f0 30 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 0 );. pFile->
d3200 6c 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 locktype = SHARE
d3210 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 D_LOCK;. pLoc
d3220 6b 2d 3e 63 6e 74 2b 2b 3b 0a 20 20 20 20 70 46 k->cnt++;. pF
d3230 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 ile->pOpen->nLoc
d3240 6b 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e k++;. goto en
d3250 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 d_lock;. }...
d3260 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 /* A PENDING loc
d3270 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f k is needed befo
d3280 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53 re acquiring a S
d3290 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 HARED lock and b
d32a0 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 efore. ** acqui
d32b0 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56 ring an EXCLUSIV
d32c0 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 E lock. For the
d32d0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 SHARED lock, th
d32e0 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 e PENDING will.
d32f0 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e ** be released.
d3300 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c . */. lock.l_l
d3310 65 6e 20 3d 20 31 4c 3b 0a 20 20 6c 6f 63 6b 2e en = 1L;. lock.
d3320 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f l_whence = SEEK_
d3330 53 45 54 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 74 SET;. if( lockt
d3340 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d3350 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b . || (lock
d3360 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f type==EXCLUSIVE_
d3370 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c LOCK && pFile->l
d3380 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f ocktype<PENDING_
d3390 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 6c LOCK). ){. l
d33a0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 28 6c 6f ock.l_type = (lo
d33b0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
d33c0 4f 43 4b 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 OCK?F_RDLCK:F_WR
d33d0 4c 43 4b 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c LCK);. lock.l
d33e0 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 _start = PENDING
d33f0 5f 42 59 54 45 3b 0a 20 20 20 20 73 20 3d 20 66 _BYTE;. s = f
d3400 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 cntl(pFile->h, F
d3410 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a _SETLK, &lock);.
d3420 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 if( s==(-1)
d3430 29 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20 ){. tErrno
d3440 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 = errno;. r
d3450 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 c = sqliteErrorF
d3460 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 romPosixError(tE
d3470 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 rrno, SQLITE_IOE
d3480 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 RR_LOCK);.
d3490 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d34a0 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 R(rc) ){.
d34b0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
d34c0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 o = tErrno;.
d34d0 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 }. goto e
d34e0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 nd_lock;. }.
d34f0 20 7d 0a 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e }... /* If con
d3500 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68 69 trol gets to thi
d3510 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 s point, then ac
d3520 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 tually go ahead
d3530 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 and make. ** op
d3540 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 erating system c
d3550 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70 65 alls for the spe
d3560 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a cified lock.. *
d3570 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 /. if( locktype
d3580 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b ==SHARED_LOCK ){
d3590 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f . assert( pLo
d35a0 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 ck->cnt==0 );.
d35b0 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d assert( pLock-
d35c0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a >locktype==0 );.
d35d0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 . /* Now get
d35e0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f the read-lock */
d35f0 0a 20 20 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f . s = rangeLo
d3600 63 6b 28 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43 ck(pFile, F_RDLC
d3610 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 0a 20 20 K, &tErrno);..
d3620 20 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 74 65 /* Drop the te
d3630 6d 70 6f 72 61 72 79 20 50 45 4e 44 49 4e 47 20 mporary PENDING
d3640 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b lock */. lock
d3650 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 .l_start = PENDI
d3660 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 NG_BYTE;. loc
d3670 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 k.l_len = 1L;.
d3680 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 lock.l_type =
d3690 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28 F_UNLCK;. if(
d36a0 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c fcntl(pFile->h,
d36b0 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 F_SETLK, &lock)
d36c0 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 !=0 ){. if(
d36d0 20 73 20 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 s != -1 ){.
d36e0 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f 75 6c /* This coul
d36f0 64 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 20 d happen with a
d3700 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f network mount */
d3710 0a 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20 . tErrno
d3720 3d 20 65 72 72 6e 6f 3b 20 0a 20 20 20 20 20 20 = errno; .
d3730 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 rc = sqliteErr
d3740 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 orFromPosixError
d3750 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f (tErrno, SQLITE_
d3760 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a IOERR_UNLOCK); .
d3770 20 20 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c if( IS_L
d3780 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b OCK_ERROR(rc) ){
d3790 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 . pFile
d37a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d37b0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a rrno;. }.
d37c0 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
d37d0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 _lock;. }.
d37e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d }. if( s==
d37f0 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 63 (-1) ){. rc
d3800 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
d3810 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
d3820 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
d3830 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 R_LOCK);. i
d3840 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 f( IS_LOCK_ERROR
d3850 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 (rc) ){.
d3860 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d3870 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 = tErrno;.
d3880 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
d3890 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 pFile->lockt
d38a0 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 ype = SHARED_LOC
d38b0 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e K;. pFile->
d38c0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a pOpen->nLock++;.
d38d0 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 pLock->cnt
d38e0 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 = 1;. }. }e
d38f0 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 lse if( locktype
d3900 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
d3910 20 26 26 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 && pLock->cnt>1
d3920 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 ){. /* We ar
d3930 65 20 74 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 e trying for an
d3940 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 exclusive lock b
d3950 75 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 ut another threa
d3960 64 20 69 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a d in this. **
d3970 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 69 73 same process is
d3980 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 still holding a
d3990 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f shared lock. */
d39a0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d39b0 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a _BUSY;. }else{.
d39c0 20 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 /* The reque
d39d0 73 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 st was for a RES
d39e0 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 ERVED or EXCLUSI
d39f0 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a VE lock. It is.
d3a00 20 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 ** assumed t
d3a10 68 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53 hat there is a S
d3a20 48 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 HARED or greater
d3a30 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c lock on the fil
d3a40 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 e. ** already
d3a50 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 .. */. ass
d3a60 65 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c ert( 0!=pFile->l
d3a70 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 6c ocktype );. l
d3a80 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 ock.l_type = F_W
d3a90 52 4c 43 4b 3b 0a 20 20 20 20 73 77 69 74 63 68 RLCK;. switch
d3aa0 28 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 ( locktype ){.
d3ab0 20 20 20 20 63 61 73 65 20 52 45 53 45 52 56 45 case RESERVE
d3ac0 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20 D_LOCK:.
d3ad0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 lock.l_start = R
d3ae0 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 ESERVED_BYTE;.
d3af0 20 20 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 s = fcntl(
d3b00 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c pFile->h, F_SETL
d3b10 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 K, &lock);.
d3b20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e tErrno = errn
d3b30 6f 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b o;. break
d3b40 3b 0a 20 20 20 20 20 20 63 61 73 65 20 45 58 43 ;. case EXC
d3b50 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 LUSIVE_LOCK:.
d3b60 20 20 20 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f s = rangeLo
d3b70 63 6b 28 70 46 69 6c 65 2c 20 46 5f 57 52 4c 43 ck(pFile, F_WRLC
d3b80 4b 2c 20 26 74 45 72 72 6e 6f 29 3b 0a 20 20 20 K, &tErrno);.
d3b90 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
d3ba0 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 default:.
d3bb0 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20 assert(0);.
d3bc0 20 20 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 }. if( s==(
d3bd0 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 -1) ){. rc
d3be0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d3bf0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d3c00 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d3c10 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 _LOCK);. if
d3c20 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 ( IS_LOCK_ERROR(
d3c30 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 rc) ){. p
d3c40 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
d3c50 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = tErrno;.
d3c60 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a }. }. }. ..
d3c70 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 #ifndef NDEBUG.
d3c80 20 2f 2a 20 53 65 74 20 75 70 20 74 68 65 20 74 /* Set up the t
d3c90 72 61 6e 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 ransaction-count
d3ca0 65 72 20 63 68 61 6e 67 65 20 63 68 65 63 6b 69 er change checki
d3cb0 6e 67 20 66 6c 61 67 73 20 77 68 65 6e 0a 20 20 ng flags when.
d3cc0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 ** transitioning
d3cd0 20 66 72 6f 6d 20 61 20 53 48 41 52 45 44 20 74 from a SHARED t
d3ce0 6f 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 o a RESERVED loc
d3cf0 6b 2e 20 20 54 68 65 20 63 68 61 6e 67 65 0a 20 k. The change.
d3d00 20 2a 2a 20 66 72 6f 6d 20 53 48 41 52 45 44 20 ** from SHARED
d3d10 74 6f 20 52 45 53 45 52 56 45 44 20 6d 61 72 6b to RESERVED mark
d3d20 73 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 s the beginning
d3d30 6f 66 20 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a of a normal. **
d3d40 20 77 72 69 74 65 20 6f 70 65 72 61 74 69 6f 6e write operation
d3d50 20 28 6e 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72 (not a hot jour
d3d60 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 nal rollback)..
d3d70 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
d3d80 4c 49 54 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46 LITE_OK. && pF
d3d90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 ile->locktype<=S
d3da0 48 41 52 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 HARED_LOCK. &&
d3db0 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 locktype==RESER
d3dc0 56 45 44 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 VED_LOCK. ){.
d3dd0 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e pFile->transCn
d3de0 74 72 43 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 trChng = 0;.
d3df0 70 46 69 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 pFile->dbUpdate
d3e00 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e = 0;. pFile->
d3e10 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 inNormalWrite =
d3e20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 1;. }.#endif...
d3e30 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
d3e40 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 _OK ){. pFile
d3e50 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
d3e60 6b 74 79 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b ktype;. pLock
d3e70 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
d3e80 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 ktype;. }else i
d3e90 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 f( locktype==EXC
d3ea0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 LUSIVE_LOCK ){.
d3eb0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 pFile->lockty
d3ec0 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 pe = PENDING_LOC
d3ed0 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f K;. pLock->lo
d3ee0 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 cktype = PENDING
d3ef0 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f _LOCK;. }..end_
d3f00 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 lock:. unixLeav
d3f10 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 eMutex();. OSTR
d3f20 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 ACE4("LOCK %d
d3f30 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 %s %s\n", pFile
d3f40 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d ->h, locktypeNam
d3f50 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 e(locktype), .
d3f60 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rc==SQLITE_O
d3f70 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c K ? "ok" : "fail
d3f80 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 ed");. return r
d3f90 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 c;.}../*.** Clos
d3fa0 65 20 61 6c 6c 20 66 69 6c 65 20 64 65 73 63 72 e all file descr
d3fb0 69 70 74 6f 72 73 20 61 63 63 75 6d 75 61 74 65 iptors accumuate
d3fc0 64 20 69 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 d in the unixOpe
d3fd0 6e 43 6e 74 2d 3e 70 55 6e 75 73 65 64 20 6c 69 nCnt->pUnused li
d3fe0 73 74 2e 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 st..** If all su
d3ff0 63 68 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ch file descript
d4000 6f 72 73 20 61 72 65 20 63 6c 6f 73 65 64 20 77 ors are closed w
d4010 69 74 68 6f 75 74 20 65 72 72 6f 72 2c 20 74 68 ithout error, th
d4020 65 20 6c 69 73 74 20 69 73 0a 2a 2a 20 63 6c 65 e list is.** cle
d4030 61 72 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f ared and SQLITE_
d4040 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a OK returned..**.
d4050 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 ** Otherwise, if
d4060 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
d4070 2c 20 74 68 65 6e 20 73 75 63 63 65 73 73 66 75 , then successfu
d4080 6c 6c 79 20 63 6c 6f 73 65 64 20 66 69 6c 65 20 lly closed file
d4090 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 65 6e descriptor.** en
d40a0 74 72 69 65 73 20 61 72 65 20 72 65 6d 6f 76 65 tries are remove
d40b0 64 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 2c d from the list,
d40c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 and SQLITE_IOER
d40d0 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 R_CLOSE returned
d40e0 2e 20 0a 2a 2a 20 6e 6f 74 20 64 65 6c 65 74 65 . .** not delete
d40f0 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 d and SQLITE_IOE
d4100 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 RR_CLOSE returne
d4110 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 69 6e d..*/ .static in
d4120 74 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 t closePendingFd
d4130 73 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c s(unixFile *pFil
d4140 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 e){. int rc = S
d4150 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 74 72 75 QLITE_OK;. stru
d4160 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a ct unixOpenCnt *
d4170 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 pOpen = pFile->p
d4180 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 Open;. UnixUnus
d4190 65 64 46 64 20 2a 70 45 72 72 6f 72 20 3d 20 30 edFd *pError = 0
d41a0 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 ;. UnixUnusedFd
d41b0 20 2a 70 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 *p;. UnixUnuse
d41c0 64 46 64 20 2a 70 4e 65 78 74 3b 0a 20 20 66 6f dFd *pNext;. fo
d41d0 72 28 70 3d 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 r(p=pOpen->pUnus
d41e0 65 64 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b ed; p; p=pNext){
d41f0 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e . pNext = p->
d4200 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 63 pNext;. if( c
d4210 6c 6f 73 65 28 70 2d 3e 66 64 29 20 29 7b 0a 20 lose(p->fd) ){.
d4220 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 pFile->last
d4230 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d4240 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
d4250 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 _IOERR_CLOSE;.
d4260 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 p->pNext = p
d4270 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 70 45 72 Error;. pEr
d4280 72 6f 72 20 3d 20 70 3b 0a 20 20 20 20 7d 65 6c ror = p;. }el
d4290 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
d42a0 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 7d 3_free(p);. }
d42b0 0a 20 20 7d 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 . }. pOpen->pU
d42c0 6e 75 73 65 64 20 3d 20 70 45 72 72 6f 72 3b 0a nused = pError;.
d42d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
d42e0 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 66 69 /*.** Add the fi
d42f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 75 73 le descriptor us
d4300 65 64 20 62 79 20 66 69 6c 65 20 68 61 6e 64 6c ed by file handl
d4310 65 20 70 46 69 6c 65 20 74 6f 20 74 68 65 20 63 e pFile to the c
d4320 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 orresponding.**
d4330 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f pUnused list..*/
d4340 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
d4350 50 65 6e 64 69 6e 67 46 64 28 75 6e 69 78 46 69 PendingFd(unixFi
d4360 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 73 74 le *pFile){. st
d4370 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
d4380 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d *pOpen = pFile-
d4390 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e >pOpen;. UnixUn
d43a0 75 73 65 64 46 64 20 2a 70 20 3d 20 70 46 69 6c usedFd *p = pFil
d43b0 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d e->pUnused;. p-
d43c0 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e >pNext = pOpen->
d43d0 70 55 6e 75 73 65 64 3b 0a 20 20 70 4f 70 65 6e pUnused;. pOpen
d43e0 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 ->pUnused = p;.
d43f0 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a pFile->h = -1;.
d4400 20 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 pFile->pUnused
d4410 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c = 0;.}../*.** L
d4420 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
d4430 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
d4440 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
d4450 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
d4460 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
d4470 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
d4480 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
d4490 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
d44a0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
d44b0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
d44c0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
d44d0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
d44e0 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
d44f0 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
d4500 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
d4510 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
d4520 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 unixUnlock(sqli
d4530 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e te3_file *id, in
d4540 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 75 t locktype){. u
d4550 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d4560 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 20 (unixFile*)id;
d4570 2f 2a 20 54 68 65 20 6f 70 65 6e 20 66 69 6c 65 /* The open file
d4580 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 */. struct uni
d4590 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b xLockInfo *pLock
d45a0 3b 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63 74 ; /* Struct
d45b0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 63 ure describing c
d45c0 75 72 72 65 6e 74 20 6c 6f 63 6b 20 73 74 61 74 urrent lock stat
d45d0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 66 6c e */. struct fl
d45e0 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 ock lock;
d45f0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 /* Infor
d4600 6d 61 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e mation passed in
d4610 74 6f 20 66 63 6e 74 6c 28 29 20 2a 2f 0a 20 20 to fcntl() */.
d4620 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
d4630 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
d4640 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 /* Return code
d4650 66 72 6f 6d 20 74 68 69 73 20 69 6e 74 65 72 66 from this interf
d4660 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 ace */. int h;
d4670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d4680 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
d4690 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 underlying file
d46a0 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 descriptor */.
d46b0 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20 20 20 20 int tErrno;
d46c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d46d0 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 /* Error code
d46e0 66 72 6f 6d 20 73 79 73 74 65 6d 20 63 61 6c 6c from system call
d46f0 20 65 72 72 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 errors */.. as
d4700 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 sert( pFile );.
d4710 20 4f 53 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 OSTRACE7("UNLOC
d4720 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 28 K %d %d was %d(
d4730 25 64 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 %d,%d) pid=%d\n"
d4740 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b , pFile->h, lock
d4750 74 79 70 65 2c 0a 20 20 20 20 20 20 70 46 69 6c type,. pFil
d4760 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 e->locktype, pFi
d4770 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 le->pLock->lockt
d4780 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 ype, pFile->pLoc
d4790 6b 2d 3e 63 6e 74 2c 20 67 65 74 70 69 64 28 29 k->cnt, getpid()
d47a0 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6c 6f );.. assert( lo
d47b0 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c cktype<=SHARED_L
d47c0 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 46 69 OCK );. if( pFi
d47d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f le->locktype<=lo
d47e0 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 cktype ){. re
d47f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d4800 20 20 7d 0a 20 20 69 66 28 20 43 48 45 43 4b 5f }. if( CHECK_
d4810 54 48 52 45 41 44 49 44 28 70 46 69 6c 65 29 20 THREADID(pFile)
d4820 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
d4830 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d LITE_MISUSE;. }
d4840 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 . unixEnterMute
d4850 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 x();. h = pFile
d4860 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 ->h;. pLock = p
d4870 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 File->pLock;. a
d4880 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e ssert( pLock->cn
d4890 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 t!=0 );. if( pF
d48a0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
d48b0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d48c0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e assert( pLock->
d48d0 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d locktype==pFile-
d48e0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 >locktype );.
d48f0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
d4900 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 Benign(1);. S
d4910 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
d4920 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d h=(-1) ). Sim
d4930 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
d4940 67 6e 28 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20 gn(0);..#ifndef
d4950 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 57 68 NDEBUG. /* Wh
d4960 65 6e 20 72 65 64 75 63 69 6e 67 20 61 20 6c 6f en reducing a lo
d4970 63 6b 20 73 75 63 68 20 74 68 61 74 20 6f 74 68 ck such that oth
d4980 65 72 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e er processes can
d4990 20 73 74 61 72 74 0a 20 20 20 20 2a 2a 20 72 65 start. ** re
d49a0 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
d49b0 73 65 20 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d se file again, m
d49c0 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 ake sure that th
d49d0 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 e. ** transac
d49e0 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 tion counter was
d49f0 20 75 70 64 61 74 65 64 20 69 66 20 61 6e 79 20 updated if any
d4a00 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 part of the data
d4a10 62 61 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 base. ** file
d4a20 20 63 68 61 6e 67 65 64 2e 20 20 49 66 20 74 68 changed. If th
d4a30 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f e transaction co
d4a40 75 6e 74 65 72 20 69 73 20 6e 6f 74 20 75 70 64 unter is not upd
d4a50 61 74 65 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 ated,. ** oth
d4a60 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 er connections t
d4a70 6f 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 o the same file
d4a80 6d 69 67 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a might not realiz
d4a90 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 e that. ** th
d4aa0 65 20 66 69 6c 65 20 68 61 73 20 63 68 61 6e 67 e file has chang
d4ab0 65 64 20 61 6e 64 20 68 65 6e 63 65 20 6d 69 67 ed and hence mig
d4ac0 68 74 20 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 ht not know to f
d4ad0 6c 75 73 68 20 74 68 65 69 72 0a 20 20 20 20 2a lush their. *
d4ae0 2a 20 63 61 63 68 65 2e 20 20 54 68 65 20 75 73 * cache. The us
d4af0 65 20 6f 66 20 61 20 73 74 61 6c 65 20 63 61 63 e of a stale cac
d4b00 68 65 20 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 he can lead to d
d4b10 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
d4b20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 on.. */. a
d4b30 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e ssert( pFile->in
d4b40 4e 6f 72 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20 NormalWrite==0.
d4b50 20 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 || pFile
d4b60 2d 3e 64 62 55 70 64 61 74 65 3d 3d 30 0a 20 20 ->dbUpdate==0.
d4b70 20 20 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d || pFile-
d4b80 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d >transCntrChng==
d4b90 31 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 1 );. pFile->
d4ba0 69 6e 4e 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 inNormalWrite =
d4bb0 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 0;.#endif...
d4bc0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 if( locktype==SH
d4bd0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d4be0 20 20 20 69 66 28 20 72 61 6e 67 65 4c 6f 63 6b if( rangeLock
d4bf0 28 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c (pFile, F_RDLCK,
d4c00 20 26 74 45 72 72 6e 6f 29 3d 3d 28 2d 31 29 20 &tErrno)==(-1)
d4c10 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
d4c20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
d4c30 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
d4c40 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 , SQLITE_IOERR_R
d4c50 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 DLOCK);.
d4c60 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f if( IS_LOCK_ERRO
d4c70 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 R(rc) ){.
d4c80 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d4c90 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d4ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
d4cb0 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b goto end_unlock;
d4cc0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
d4cd0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d lock.l_type =
d4ce0 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f F_UNLCK;. lo
d4cf0 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 ck.l_whence = SE
d4d00 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b EK_SET;. lock
d4d10 2e 6c 5f 73 74 61 72 74 20 3d 20 50 45 4e 44 49 .l_start = PENDI
d4d20 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 NG_BYTE;. loc
d4d30 6b 2e 6c 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 k.l_len = 2L; a
d4d40 73 73 65 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 ssert( PENDING_B
d4d50 59 54 45 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f YTE+1==RESERVED_
d4d60 42 59 54 45 20 29 3b 0a 20 20 20 20 69 66 28 20 BYTE );. if(
d4d70 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b fcntl(h, F_SETLK
d4d80 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 , &lock)!=(-1) )
d4d90 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c {. pLock->l
d4da0 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 ocktype = SHARED
d4db0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 _LOCK;. }else
d4dc0 7b 0a 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d {. tErrno =
d4dd0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 errno;. rc
d4de0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 = sqliteErrorFr
d4df0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 omPosixError(tEr
d4e00 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 rno, SQLITE_IOER
d4e10 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 R_UNLOCK);.
d4e20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 if( IS_LOCK_ERR
d4e30 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 OR(rc) ){.
d4e40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d4e50 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d4e60 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
d4e70 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 end_unlock;.
d4e80 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b }. }. if( lock
d4e90 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type==NO_LOCK ){
d4ea0 0a 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 . struct unix
d4eb0 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a OpenCnt *pOpen;.
d4ec0 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e . /* Decremen
d4ed0 74 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 t the shared loc
d4ee0 6b 20 63 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65 k counter. Rele
d4ef0 61 73 65 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 ase the lock usi
d4f00 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 ng an. ** OS
d4f10 63 61 6c 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 call only when a
d4f20 6c 6c 20 74 68 72 65 61 64 73 20 69 6e 20 74 68 ll threads in th
d4f30 69 73 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 is same process
d4f40 68 61 76 65 20 72 65 6c 65 61 73 65 64 0a 20 20 have released.
d4f50 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 ** the lock..
d4f60 20 20 20 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d */. pLock-
d4f70 3e 63 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 >cnt--;. if(
d4f80 70 4c 6f 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b pLock->cnt==0 ){
d4f90 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 . lock.l_ty
d4fa0 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 pe = F_UNLCK;.
d4fb0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 lock.l_whenc
d4fc0 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 e = SEEK_SET;.
d4fd0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 lock.l_start
d4fe0 20 3d 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 = lock.l_len =
d4ff0 30 4c 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 0L;. Simula
d5000 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
d5010 31 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 1);. Simula
d5020 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 teIOError( h=(-1
d5030 29 20 29 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 ) ). Simula
d5040 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
d5050 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66 63 0);. if( fc
d5060 6e 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 ntl(h, F_SETLK,
d5070 26 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a &lock)!=(-1) ){.
d5080 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c pLock->l
d5090 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
d50a0 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a K;. }else{.
d50b0 20 20 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d tErrno =
d50c0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
d50d0 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 rc = sqliteError
d50e0 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 FromPosixError(t
d50f0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f Errno, SQLITE_IO
d5100 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 ERR_UNLOCK);.
d5110 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b if( IS_LOCK
d5120 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 _ERROR(rc) ){.
d5130 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d5140 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e astErrno = tErrn
d5150 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 o;. }.
d5160 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b pLock->lock
d5170 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a type = NO_LOCK;.
d5180 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
d5190 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 ocktype = NO_LOC
d51a0 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d K;. }. }
d51b0 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 .. /* Decreme
d51c0 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 nt the count of
d51d0 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74 68 locks against th
d51e0 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 is same file. W
d51f0 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 hen the. ** c
d5200 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 ount reaches zer
d5210 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 o, close any oth
d5220 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 er file descript
d5230 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a ors whose close.
d5240 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65 72 ** was defer
d5250 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 6f red because of o
d5260 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 utstanding locks
d5270 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70 .. */. pOp
d5280 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 en = pFile->pOpe
d5290 6e 3b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c n;. pOpen->nL
d52a0 6f 63 6b 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 ock--;. asser
d52b0 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e t( pOpen->nLock>
d52c0 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f =0 );. if( pO
d52d0 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b pen->nLock==0 ){
d52e0 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 20 3d . int rc2 =
d52f0 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 closePendingFds
d5300 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 (pFile);. i
d5310 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
d5320 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
d5330 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rc2;. }.
d5340 20 20 7d 0a 20 20 7d 0a 09 0a 65 6e 64 5f 75 6e }. }...end_un
d5350 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 lock:. unixLeav
d5360 65 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 eMutex();. if(
d5370 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc==SQLITE_OK )
d5380 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
d5390 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 = locktype;. re
d53a0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
d53b0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
d53c0 70 65 72 66 6f 72 6d 73 20 74 68 65 20 70 61 72 performs the par
d53d0 74 73 20 6f 66 20 74 68 65 20 22 63 6c 6f 73 65 ts of the "close
d53e0 20 66 69 6c 65 22 20 6f 70 65 72 61 74 69 6f 6e file" operation
d53f0 20 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 .** common to a
d5400 6c 6c 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d ll locking schem
d5410 65 73 2e 20 49 74 20 63 6c 6f 73 65 73 20 74 68 es. It closes th
d5420 65 20 64 69 72 65 63 74 6f 72 79 20 61 6e 64 20 e directory and
d5430 66 69 6c 65 0a 2a 2a 20 68 61 6e 64 6c 65 73 2c file.** handles,
d5440 20 69 66 20 74 68 65 79 20 61 72 65 20 76 61 6c if they are val
d5450 69 64 2c 20 61 6e 64 20 73 65 74 73 20 61 6c 6c id, and sets all
d5460 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 75 fields of the u
d5470 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 nixFile.** struc
d5480 74 75 72 65 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a ture to 0..**.**
d5490 20 49 74 20 69 73 20 2a 6e 6f 74 2a 20 6e 65 63 It is *not* nec
d54a0 65 73 73 61 72 79 20 74 6f 20 68 6f 6c 64 20 74 essary to hold t
d54b0 68 65 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 he mutex when th
d54c0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
d54d0 6c 6c 65 64 2c 0a 2a 2a 20 65 76 65 6e 20 6f 6e lled,.** even on
d54e0 20 56 78 57 6f 72 6b 73 2e 20 20 41 20 6d 75 74 VxWorks. A mut
d54f0 65 78 20 77 69 6c 6c 20 62 65 20 61 63 71 75 69 ex will be acqui
d5500 72 65 64 20 6f 6e 20 56 78 57 6f 72 6b 73 20 62 red on VxWorks b
d5510 79 20 74 68 65 0a 2a 2a 20 76 78 77 6f 72 6b 73 y the.** vxworks
d5520 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 29 20 ReleaseFileId()
d5530 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 routine..*/.stat
d5540 69 63 20 69 6e 74 20 63 6c 6f 73 65 55 6e 69 78 ic int closeUnix
d5550 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c File(sqlite3_fil
d5560 65 20 2a 69 64 29 7b 0a 20 20 75 6e 69 78 46 69 e *id){. unixFi
d5570 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
d5580 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 66 28 xFile*)id;. if(
d5590 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 69 66 pFile ){. if
d55a0 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d ( pFile->dirfd>=
d55b0 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 0 ){. int e
d55c0 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 rr = close(pFile
d55d0 2d 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 20 20 ->dirfd);.
d55e0 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20 if( err ){.
d55f0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d5600 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d5610 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
d5620 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f TE_IOERR_DIR_CLO
d5630 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b SE;. }else{
d5640 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d5650 64 69 72 66 64 3d 2d 31 3b 0a 20 20 20 20 20 20 dirfd=-1;.
d5660 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 }. }. if(
d5670 70 46 69 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 pFile->h>=0 ){.
d5680 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63 int err = c
d5690 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a lose(pFile->h);.
d56a0 20 20 20 20 20 20 69 66 28 20 65 72 72 20 29 7b if( err ){
d56b0 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
d56c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e lastErrno = errn
d56d0 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 o;. retur
d56e0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
d56f0 4c 4f 53 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 LOSE;. }.
d5700 20 20 7d 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 }.#if OS_VXWOR
d5710 4b 53 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 KS. if( pFile
d5720 2d 3e 70 49 64 20 29 7b 0a 20 20 20 20 20 20 69 ->pId ){. i
d5730 66 28 20 70 46 69 6c 65 2d 3e 69 73 44 65 6c 65 f( pFile->isDele
d5740 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e te ){. un
d5750 6c 69 6e 6b 28 70 46 69 6c 65 2d 3e 70 49 64 2d link(pFile->pId-
d5760 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 >zCanonicalName)
d5770 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
d5780 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 vxworksReleaseFi
d5790 6c 65 49 64 28 70 46 69 6c 65 2d 3e 70 49 64 29 leId(pFile->pId)
d57a0 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 ;. pFile->p
d57b0 49 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 Id = 0;. }.#e
d57c0 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 43 45 ndif. OSTRACE
d57d0 32 28 22 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 2("CLOSE %-3d\
d57e0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 n", pFile->h);.
d57f0 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d OpenCounter(-
d5800 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 1);. sqlite3_
d5810 66 72 65 65 28 70 46 69 6c 65 2d 3e 70 55 6e 75 free(pFile->pUnu
d5820 73 65 64 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 sed);. memset
d5830 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f (pFile, 0, sizeo
d5840 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 f(unixFile));.
d5850 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
d5860 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 E_OK;.}../*.** C
d5870 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a lose a file..*/.
d5880 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43 static int unixC
d5890 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c lose(sqlite3_fil
d58a0 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 e *id){. int rc
d58b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
d58c0 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e if( id ){. un
d58d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d58e0 28 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a (unixFile *)id;.
d58f0 20 20 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 unixUnlock(i
d5900 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 d, NO_LOCK);.
d5910 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 unixEnterMutex(
d5920 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 );. if( pFile
d5930 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 ->pOpen && pFile
d5940 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 ->pOpen->nLock )
d5950 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 {. /* If th
d5960 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 ere are outstand
d5970 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f ing locks, do no
d5980 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 t actually close
d5990 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 the file just.
d59a0 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61 ** yet beca
d59b0 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63 use that would c
d59c0 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 lear those locks
d59d0 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 . Instead, add
d59e0 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a the file. *
d59f0 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 * descriptor to
d5a00 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 6c pOpen->pUnused l
d5a10 69 73 74 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 ist. It will be
d5a20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 automatically c
d5a30 6c 6f 73 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 losed . **
d5a40 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f when the last lo
d5a50 63 6b 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 ck is cleared..
d5a60 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 */. se
d5a70 74 50 65 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 tPendingFd(pFile
d5a80 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6c );. }. rel
d5a90 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 easeLockInfo(pFi
d5aa0 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 le->pLock);.
d5ab0 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 releaseOpenCnt(p
d5ac0 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 File->pOpen);.
d5ad0 20 20 72 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 rc = closeUnix
d5ae0 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e File(id);. un
d5af0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a ixLeaveMutex();.
d5b00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
d5b10 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .}../***********
d5b20 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 70 *** End of the p
d5b30 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f osix advisory lo
d5b40 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
d5b50 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
d5b60 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***.************
d5b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5bb0 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/../**********
d5bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c00 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****.***********
d5c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c20 2a 2a 2a 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e *** No-op Lockin
d5c30 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a g **************
d5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d5c50 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68 ****.**.** Of th
d5c60 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e e various lockin
d5c70 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e g implementation
d5c80 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 s available, thi
d5c90 73 20 69 73 20 62 79 20 66 61 72 20 74 68 65 0a s is by far the.
d5ca0 2a 2a 20 73 69 6d 70 6c 65 73 74 3a 20 20 6c 6f ** simplest: lo
d5cb0 63 6b 69 6e 67 20 69 73 20 69 67 6e 6f 72 65 64 cking is ignored
d5cc0 2e 20 20 4e 6f 20 61 74 74 65 6d 70 74 20 69 73 . No attempt is
d5cd0 20 6d 61 64 65 20 74 6f 20 6c 6f 63 6b 20 74 68 made to lock th
d5ce0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 e database.** fi
d5cf0 6c 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 6f le for reading o
d5d00 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a r writing..**.**
d5d10 20 54 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f This locking mo
d5d20 64 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 de is appropriat
d5d30 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 72 65 61 e for use on rea
d5d40 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 73 d-only databases
d5d50 0a 2a 2a 20 28 65 78 3a 20 64 61 74 61 62 61 73 .** (ex: databas
d5d60 65 73 20 74 68 61 74 20 61 72 65 20 62 75 72 6e es that are burn
d5d70 65 64 20 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 ed into CD-ROM,
d5d80 66 6f 72 20 65 78 61 6d 70 6c 65 2e 29 20 20 49 for example.) I
d5d90 74 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 t can.** also be
d5da0 20 75 73 65 64 20 69 66 20 74 68 65 20 61 70 70 used if the app
d5db0 6c 69 63 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 73 lication employs
d5dc0 20 73 6f 6d 65 20 65 78 74 65 72 6e 61 6c 20 6d some external m
d5dd0 65 63 68 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70 echanism to.** p
d5de0 72 65 76 65 6e 74 20 73 69 6d 75 6c 74 61 6e 65 revent simultane
d5df0 6f 75 73 20 61 63 63 65 73 73 20 6f 66 20 74 68 ous access of th
d5e00 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 e same database
d5e10 62 79 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a by two or more.*
d5e20 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * database conne
d5e30 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 74 68 65 ctions. But the
d5e40 72 65 20 69 73 20 61 20 73 65 72 69 6f 75 73 20 re is a serious
d5e50 72 69 73 6b 20 6f 66 20 64 61 74 61 62 61 73 65 risk of database
d5e60 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 .** corruption i
d5e70 66 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d f this locking m
d5e80 6f 64 65 20 69 73 20 75 73 65 64 20 69 6e 20 73 ode is used in s
d5e90 69 74 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 ituations where
d5ea0 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 64 61 74 61 multiple.** data
d5eb0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
d5ec0 20 61 72 65 20 61 63 63 65 73 73 69 6e 67 20 74 are accessing t
d5ed0 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 he same database
d5ee0 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d file at the sam
d5ef0 65 0a 2a 2a 20 74 69 6d 65 20 61 6e 64 20 6f 6e e.** time and on
d5f00 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 6f e or more of tho
d5f10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 se connections a
d5f20 72 65 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a re writing..*/..
d5f30 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 static int noloc
d5f40 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f kCheckReservedLo
d5f50 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d5f60 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 2a 70 *NotUsed, int *p
d5f70 52 65 73 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 ResOut){. UNUSE
d5f80 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
d5f90 73 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 sed);. *pResOut
d5fa0 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 = 0;. return S
d5fb0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 QLITE_OK;.}.stat
d5fc0 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 ic int nolockLoc
d5fd0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d5fe0 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 NotUsed, int Not
d5ff0 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 Used2){. UNUSED
d6000 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 _PARAMETER2(NotU
d6010 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a sed, NotUsed2);.
d6020 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
d6030 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 OK;.}.static int
d6040 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 nolockUnlock(sq
d6050 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 lite3_file *NotU
d6060 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 sed, int NotUsed
d6070 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 2){. UNUSED_PAR
d6080 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c AMETER2(NotUsed,
d6090 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 NotUsed2);. re
d60a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d60b0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 }../*.** Close t
d60c0 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
d60d0 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f ic int nolockClo
d60e0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 se(sqlite3_file
d60f0 2a 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 *id) {. return
d6100 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 closeUnixFile(id
d6110 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a );.}../*********
d6120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
d6130 66 20 74 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b f the no-op lock
d6140 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d6150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6160 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d6170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61b0 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
d61c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6200 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d6210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6220 20 42 65 67 69 6e 20 64 6f 74 2d 66 69 6c 65 20 Begin dot-file
d6230 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a Locking ********
d6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d6250 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 ******.**.** The
d6260 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 dotfile locking
d6270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
d6280 75 73 65 73 20 74 68 65 20 65 78 69 73 74 61 6e uses the existan
d6290 63 65 20 6f 66 20 73 65 70 61 72 61 74 65 20 6c ce of separate l
d62a0 6f 63 6b 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20 ock.** files in
d62b0 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 72 6f 6c order to control
d62c0 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 64 access to the d
d62d0 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 77 atabase. This w
d62e0 6f 72 6b 73 20 6f 6e 20 6a 75 73 74 0a 2a 2a 20 orks on just.**
d62f0 61 62 6f 75 74 20 65 76 65 72 79 20 66 69 6c 65 about every file
d6300 73 79 73 74 65 6d 20 69 6d 61 67 69 6e 61 62 6c system imaginabl
d6310 65 2e 20 20 42 75 74 20 74 68 65 72 65 20 61 72 e. But there ar
d6320 65 20 73 65 72 69 6f 75 73 20 64 6f 77 6e 73 69 e serious downsi
d6330 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 des:.**.** (1
d6340 29 20 20 54 68 65 72 65 20 69 73 20 7a 65 72 6f ) There is zero
d6350 20 63 6f 6e 63 75 72 72 65 6e 63 79 2e 20 20 41 concurrency. A
d6360 20 73 69 6e 67 6c 65 20 72 65 61 64 65 72 20 62 single reader b
d6370 6c 6f 63 6b 73 20 61 6c 6c 20 6f 74 68 65 72 0a locks all other.
d6380 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e 6e 65 ** conne
d6390 63 74 69 6f 6e 73 20 66 72 6f 6d 20 72 65 61 64 ctions from read
d63a0 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 ing or writing t
d63b0 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a he database..**.
d63c0 2a 2a 20 20 20 20 28 32 29 20 20 41 6e 20 61 70 ** (2) An ap
d63d0 70 6c 69 63 61 74 69 6f 6e 20 63 72 61 73 68 20 plication crash
d63e0 6f 72 20 70 6f 77 65 72 20 6c 6f 73 73 20 63 61 or power loss ca
d63f0 6e 20 6c 65 61 76 65 20 73 74 61 6c 65 20 6c 6f n leave stale lo
d6400 63 6b 20 66 69 6c 65 73 0a 2a 2a 20 20 20 20 20 ck files.**
d6410 20 20 20 20 73 69 74 74 69 6e 67 20 61 72 6f 75 sitting arou
d6420 6e 64 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 nd that need to
d6430 62 65 20 63 6c 65 61 72 65 64 20 6d 61 6e 75 61 be cleared manua
d6440 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72 lly..**.** Never
d6450 74 68 65 6c 65 73 73 2c 20 61 20 64 6f 74 6c 6f theless, a dotlo
d6460 63 6b 20 69 73 20 61 6e 20 61 70 70 72 6f 70 72 ck is an appropr
d6470 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 iate locking mod
d6480 65 20 66 6f 72 20 75 73 65 20 69 66 20 6e 6f 0a e for use if no.
d6490 2a 2a 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 ** other locking
d64a0 20 73 74 72 61 74 65 67 79 20 69 73 20 61 76 61 strategy is ava
d64b0 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f ilable..**.** Do
d64c0 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 77 6f tfile locking wo
d64d0 72 6b 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 rks by creating
d64e0 61 20 66 69 6c 65 20 69 6e 20 74 68 65 20 73 61 a file in the sa
d64f0 6d 65 20 64 69 72 65 63 74 6f 72 79 20 61 73 20 me directory as
d6500 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 the.** database
d6510 61 6e 64 20 77 69 74 68 20 74 68 65 20 73 61 6d and with the sam
d6520 65 20 6e 61 6d 65 20 62 75 74 20 77 69 74 68 20 e name but with
d6530 61 20 22 2e 6c 6f 63 6b 22 20 65 78 74 65 6e 73 a ".lock" extens
d6540 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2a 20 54 68 ion added..** Th
d6550 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 61 e existance of a
d6560 20 6c 6f 63 6b 20 66 69 6c 65 20 69 6d 70 6c 69 lock file impli
d6570 65 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 es an EXCLUSIVE
d6580 6c 6f 63 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72 lock. All other
d6590 20 6c 6f 63 6b 0a 2a 2a 20 74 79 70 65 73 20 28 lock.** types (
d65a0 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 SHARED, RESERVED
d65b0 2c 20 50 45 4e 44 49 4e 47 29 20 61 72 65 20 6d , PENDING) are m
d65c0 61 70 70 65 64 20 69 6e 74 6f 20 45 58 43 4c 55 apped into EXCLU
d65d0 53 49 56 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 SIVE..*/../*.**
d65e0 54 68 65 20 66 69 6c 65 20 73 75 66 66 69 78 20 The file suffix
d65f0 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 added to the dat
d6600 61 20 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 a base filename
d6610 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 in order to crea
d6620 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 66 te the.** lock f
d6630 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 ile..*/.#define
d6640 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22 DOTLOCK_SUFFIX "
d6650 2e 6c 6f 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 .lock"../*.** Th
d6660 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b is routine check
d6670 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 s if there is a
d6680 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 RESERVED lock he
d6690 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 ld on the specif
d66a0 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 ied.** file by t
d66b0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 his or any other
d66c0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 process. If suc
d66d0 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 h a lock is held
d66e0 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a , set *pResOut.*
d66f0 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 * to a non-zero
d6700 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 value otherwise
d6710 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 *pResOut is set
d6720 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 to zero. The re
d6730 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 turn value.** is
d6740 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f set to SQLITE_O
d6750 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 K unless an I/O
d6760 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 error occurs dur
d6770 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e ing lock checkin
d6780 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66 g..**.** In dotf
d6790 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74 ile locking, eit
d67a0 68 65 72 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 her a lock exist
d67b0 73 20 6f 72 20 69 74 20 64 6f 65 73 20 6e 6f 74 s or it does not
d67c0 2e 20 20 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a . So in this.**
d67d0 20 76 61 72 69 61 74 69 6f 6e 20 6f 66 20 43 68 variation of Ch
d67e0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 eckReservedLock(
d67f0 29 2c 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 ), *pResOut is s
d6800 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 61 6e et to true if an
d6810 79 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c y lock.** is hel
d6820 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 61 6e d on the file an
d6830 64 20 66 61 6c 73 65 20 69 66 20 74 68 65 20 66 d false if the f
d6840 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e ile is unlocked.
d6850 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 .*/.static int d
d6860 6f 74 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 otlockCheckReser
d6870 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
d6880 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
d6890 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 ResOut) {. int
d68a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d68b0 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d int reserved =
d68c0 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 0;. unixFile *
d68d0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c pFile = (unixFil
d68e0 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 e*)id;.. Simula
d68f0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 teIOError( retur
d6900 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
d6910 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b HECKRESERVEDLOCK
d6920 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 ; );. . assert
d6930 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a ( pFile );.. /*
d6940 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 Check if a thre
d6950 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 ad in this proce
d6960 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 ss holds such a
d6970 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 lock */. if( pF
d6980 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 ile->locktype>SH
d6990 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 ARED_LOCK ){.
d69a0 20 2f 2a 20 45 69 74 68 65 72 20 74 68 69 73 20 /* Either this
d69b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f connection or so
d69c0 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 me other connect
d69d0 69 6f 6e 20 69 6e 20 74 68 65 20 73 61 6d 65 20 ion in the same
d69e0 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20 68 process. ** h
d69f0 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 olds a lock on t
d6a00 68 65 20 66 69 6c 65 2e 20 20 4e 6f 20 6e 65 65 he file. No nee
d6a10 64 20 74 6f 20 63 68 65 63 6b 20 66 75 72 74 68 d to check furth
d6a20 65 72 2e 20 2a 2f 0a 20 20 20 20 72 65 73 65 72 er. */. reser
d6a30 76 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 ved = 1;. }else
d6a40 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 {. /* The loc
d6a50 6b 20 69 73 20 68 65 6c 64 20 69 66 20 61 6e 64 k is held if and
d6a60 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c 6f 63 only if the loc
d6a70 6b 66 69 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a kfile exists */.
d6a80 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
d6a90 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 6f 6e zLockFile = (con
d6aa0 73 74 20 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e st char*)pFile->
d6ab0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
d6ac0 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 61 reserved = a
d6ad0 63 63 65 73 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c ccess(zLockFile,
d6ae0 20 30 29 3d 3d 30 3b 0a 20 20 7d 0a 20 20 4f 53 0)==0;. }. OS
d6af0 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d TRACE4("TEST WR-
d6b00 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 LOCK %d %d %d\n"
d6b10 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 , pFile->h, rc,
d6b20 72 65 73 65 72 76 65 64 29 3b 0a 20 20 2a 70 52 reserved);. *pR
d6b30 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 esOut = reserved
d6b40 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
d6b50 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
d6b60 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
d6b70 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
d6b80 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
d6b90 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
d6ba0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
d6bb0 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
d6bc0 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
d6bd0 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
d6be0 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
d6bf0 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
d6c00 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
d6c10 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
d6c20 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
d6c30 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
d6c40 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
d6c50 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
d6c60 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
d6c70 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
d6c80 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
d6c90 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
d6ca0 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
d6cb0 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
d6cc0 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
d6cd0 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
d6ce0 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
d6cf0 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
d6d00 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
d6d10 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
d6d20 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
d6d30 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
d6d40 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
d6d50 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
d6d60 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
d6d70 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
d6d80 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
d6d90 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
d6da0 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
d6db0 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
d6dc0 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
d6dd0 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
d6de0 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
d6df0 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
d6e00 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 IVE.**.** This r
d6e10 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 outine will only
d6e20 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b increase a lock
d6e30 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 . Use the sqlit
d6e40 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 e3OsUnlock().**
d6e50 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 routine to lower
d6e60 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c a locking level
d6e70 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74 ..**.** With dot
d6e80 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65 file locking, we
d6e90 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70 really only sup
d6ea0 70 6f 72 74 20 73 74 61 74 65 20 28 34 29 3a 20 port state (4):
d6eb0 45 58 43 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75 EXCLUSIVE..** Bu
d6ec0 74 20 77 65 20 74 72 61 63 6b 20 74 68 65 20 6f t we track the o
d6ed0 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 ther locking lev
d6ee0 65 6c 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a els internally..
d6ef0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f */.static int do
d6f00 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 tlockLock(sqlite
d6f10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
d6f20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
d6f30 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
d6f40 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
d6f50 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 int fd;. char
d6f60 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 *zLockFile = (ch
d6f70 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ar *)pFile->lock
d6f80 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e ingContext;. in
d6f90 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
d6fa0 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 ;... /* If we h
d6fb0 61 76 65 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 ave any lock, th
d6fc0 65 6e 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 en the lock file
d6fd0 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e already exists.
d6fe0 20 20 41 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 All we have.
d6ff0 2a 2a 20 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 ** to do is adju
d7000 73 74 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 st our internal
d7010 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f record of the lo
d7020 63 6b 20 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 ck level.. */.
d7030 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
d7040 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 type > NO_LOCK )
d7050 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
d7060 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d7070 3b 0a 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b ;.#if !OS_VXWORK
d7080 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 S. /* Always
d7090 75 70 64 61 74 65 20 74 68 65 20 74 69 6d 65 73 update the times
d70a0 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 tamp on the old
d70b0 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d file */. utim
d70c0 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 es(zLockFile, NU
d70d0 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 LL);.#endif.
d70e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
d70f0 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 ;. }. . /* gr
d7100 61 62 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 ab an exclusive
d7110 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f lock */. fd = o
d7120 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f pen(zLockFile,O_
d7130 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f RDONLY|O_CREAT|O
d7140 5f 45 58 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 _EXCL,0600);. i
d7150 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f f( fd<0 ){. /
d7160 2a 20 66 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e * failed to open
d7170 2f 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 /create the file
d7180 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d , someone else m
d7190 61 79 20 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 ay have stolen t
d71a0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 he lock */. i
d71b0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e nt tErrno = errn
d71c0 6f 3b 0a 20 20 20 20 69 66 28 20 45 45 58 49 53 o;. if( EEXIS
d71d0 54 20 3d 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 T == tErrno ){.
d71e0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
d71f0 5f 42 55 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 _BUSY;. } els
d7200 65 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 e {. rc = s
d7210 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d7220 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d7230 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d7240 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 CK);. if( I
d7250 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 S_LOCK_ERROR(rc)
d7260 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c ){. pFil
d7270 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 e->lastErrno = t
d7280 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 Errno;. }.
d7290 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 }. return
d72a0 72 63 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 63 rc;. } . if( c
d72b0 6c 6f 73 65 28 66 64 29 20 29 7b 0a 20 20 20 20 lose(fd) ){.
d72c0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
d72d0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 = errno;. rc
d72e0 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f = SQLITE_IOERR_
d72f0 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 CLOSE;. }. .
d7300 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 /* got it, set t
d7310 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 he type and retu
d7320 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 rn ok */. pFile
d7330 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 ->locktype = loc
d7340 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 ktype;. return
d7350 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 rc;.}../*.** Low
d7360 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
d7370 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
d7380 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f criptor pFile to
d7390 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b locktype. lock
d73a0 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 type.** must be
d73b0 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f either NO_LOCK o
d73c0 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a r SHARED_LOCK..*
d73d0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b *.** If the lock
d73e0 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 ing level of the
d73f0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
d7400 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f is already at o
d7410 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 r below.** the r
d7420 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 equested locking
d7430 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 level, this rou
d7440 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tine is a no-op.
d7450 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 .**.** When the
d7460 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65 locking level re
d7470 61 63 68 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 aches NO_LOCK, d
d7480 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 elete the lock f
d7490 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ile..*/.static i
d74a0 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b nt dotlockUnlock
d74b0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d74c0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 d, int locktype)
d74d0 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 {. unixFile *p
d74e0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
d74f0 2a 29 69 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c *)id;. char *zL
d7500 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 ockFile = (char
d7510 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
d7520 43 6f 6e 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 Context;.. asse
d7530 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f rt( pFile );. O
d7540 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 STRACE5("UNLOCK
d7550 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70 69 %d %d was %d pi
d7560 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e d=%d\n", pFile->
d7570 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 h, locktype,..
d7580 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
d7590 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61 , getpid());. a
d75a0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c ssert( locktype<
d75b0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a =SHARED_LOCK );.
d75c0 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 . /* no-op if
d75d0 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 possible */. i
d75e0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 f( pFile->lockty
d75f0 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a pe==locktype ){.
d7600 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
d7610 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 E_OK;. }.. /*
d7620 54 6f 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 To downgrade to
d7630 73 68 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 shared, simply u
d7640 70 64 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e pdate our intern
d7650 61 6c 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 al notion of the
d7660 0a 20 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 . ** lock state
d7670 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 . No need to me
d7680 73 73 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 ss with the file
d7690 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 on disk.. */.
d76a0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
d76b0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
d76c0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
d76d0 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b e = SHARED_LOCK;
d76e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
d76f0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 TE_OK;. }. .
d7700 2f 2a 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f /* To fully unlo
d7710 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 2c ck the database,
d7720 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b delete the lock
d7730 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 file */. asser
d7740 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f t( locktype==NO_
d7750 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e LOCK );. if( un
d7760 6c 69 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20 link(zLockFile)
d7770 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 ){. int rc =
d7780 30 3b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 0;. int tErrn
d7790 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 o = errno;. i
d77a0 66 28 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 f( ENOENT != tEr
d77b0 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 rno ){. rc
d77c0 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d77d0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d77e0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d77f0 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a _UNLOCK);. }.
d7800 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f if( IS_LOCK_
d7810 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 ERROR(rc) ){.
d7820 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
d7830 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 rno = tErrno;.
d7840 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 }. return r
d7850 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d c; . }. pFile-
d7860 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c >locktype = NO_L
d7870 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 OCK;. return SQ
d7880 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
d7890 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 * Close a file.
d78a0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c Make sure the l
d78b0 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c ock has been rel
d78c0 65 61 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f eased before clo
d78d0 73 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 sing..*/.static
d78e0 69 6e 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 int dotlockClose
d78f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
d7900 64 29 20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 d) {. int rc;.
d7910 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 if( id ){. u
d7920 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d nixFile *pFile =
d7930 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a (unixFile*)id;.
d7940 20 20 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 dotlockUnloc
d7950 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
d7960 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
d7970 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 (pFile->lockingC
d7980 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 ontext);. }. r
d7990 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c c = closeUnixFil
d79a0 65 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 e(id);. return
d79b0 72 63 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a rc;.}./*********
d79c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
d79d0 20 74 68 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f the dot-file lo
d79e0 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ck implementatio
d79f0 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n **************
d7a00 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
d7a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a50 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a ****/../********
d7a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7aa0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ******.*********
d7ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7ac0 2a 20 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f * Begin flock Lo
d7ad0 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a cking **********
d7ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d7af0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 ******.**.** Use
d7b00 20 74 68 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 the flock() sys
d7b10 74 65 6d 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 tem call to do f
d7b20 69 6c 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a ile locking..**.
d7b30 2a 2a 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 ** flock() locki
d7b40 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 ng is like dot-f
d7b50 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 ile locking in t
d7b60 68 61 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a hat the various.
d7b70 2a 2a 20 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f ** fine-grain lo
d7b80 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 cking levels sup
d7b90 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 ported by SQLite
d7ba0 20 61 72 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 are collapsed i
d7bb0 6e 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 nto.** a single
d7bc0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 exclusive lock.
d7bd0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
d7be0 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 SHARED, RESERVE
d7bf0 44 2c 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e D, and.** PENDIN
d7c00 47 20 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 G locks are the
d7c10 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 61 6e same thing as an
d7c20 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e EXCLUSIVE lock.
d7c30 20 20 53 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c SQLite.** stil
d7c40 6c 20 77 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 l works when you
d7c50 20 64 6f 20 74 68 69 73 2c 20 62 75 74 20 63 6f do this, but co
d7c60 6e 63 75 72 72 65 6e 63 79 20 69 73 20 72 65 64 ncurrency is red
d7c70 75 63 65 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e uced since.** on
d7c80 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 ly a single proc
d7c90 65 73 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 ess can be readi
d7ca0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ng the database
d7cb0 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a at a time..**.**
d7cc0 20 4f 6d 69 74 20 74 68 69 73 20 73 65 63 74 69 Omit this secti
d7cd0 6f 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 on if SQLITE_ENA
d7ce0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
d7cf0 45 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20 E is turned off
d7d00 6f 72 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 or if.** compili
d7d10 6e 67 20 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a ng for VXWORKS..
d7d20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e */.#if SQLITE_EN
d7d30 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
d7d40 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b LE && !OS_VXWORK
d7d50 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f S../*.** This ro
d7d60 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 utine checks if
d7d70 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 there is a RESER
d7d80 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e VED lock held on
d7d90 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a the specified.*
d7da0 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f * file by this o
d7db0 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 r any other proc
d7dc0 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c ess. If such a l
d7dd0 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 ock is held, set
d7de0 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 *pResOut.** to
d7df0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
d7e00 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 otherwise *pRes
d7e10 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 Out is set to ze
d7e20 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 ro. The return
d7e30 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 value.** is set
d7e40 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c to SQLITE_OK unl
d7e50 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 ess an I/O error
d7e60 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c occurs during l
d7e70 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f ock checking..*/
d7e80 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 .static int floc
d7e90 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f kCheckReservedLo
d7ea0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ck(sqlite3_file
d7eb0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 *id, int *pResOu
d7ec0 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
d7ed0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d7ee0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d7ef0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d7f00 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d7f10 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f . . SimulateIO
d7f20 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
d7f30 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b LITE_IOERR_CHECK
d7f40 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b RESERVEDLOCK; );
d7f50 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 . . assert( pF
d7f60 69 6c 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 ile );. . /* C
d7f70 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 heck if a thread
d7f80 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 in this process
d7f90 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f holds such a lo
d7fa0 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c ck */. if( pFil
d7fb0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 e->locktype>SHAR
d7fc0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 ED_LOCK ){. r
d7fd0 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d eserved = 1;. }
d7fe0 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 . . /* Otherwi
d7ff0 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f se see if some o
d8000 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c ther process hol
d8010 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 ds it. */. if(
d8020 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 !reserved ){.
d8030 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 /* attempt to g
d8040 65 74 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 et the lock */.
d8050 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f int lrc = flo
d8060 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 ck(pFile->h, LOC
d8070 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b K_EX | LOCK_NB);
d8080 0a 20 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b . if( !lrc ){
d8090 0a 20 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 . /* got th
d80a0 65 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 e lock, unlock i
d80b0 74 20 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d t */. lrc =
d80c0 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c flock(pFile->h,
d80d0 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 LOCK_UN);.
d80e0 20 69 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 if ( lrc ) {.
d80f0 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f int tErrno
d8100 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 = errno;.
d8110 20 20 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c /* unlock fail
d8120 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ed with an error
d8130 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 */. lrc
d8140 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f = sqliteErrorFro
d8150 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 mPosixError(tErr
d8160 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 no, SQLITE_IOERR
d8170 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 _UNLOCK); .
d8180 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d8190 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 RROR(lrc) ){.
d81a0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
d81b0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f stErrno = tErrno
d81c0 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d ;. rc =
d81d0 20 6c 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a lrc;. }.
d81e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c }. } el
d81f0 73 65 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 se {. int t
d8200 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
d8210 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 reserved =
d8220 31 3b 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 1;. /* some
d8230 6f 6e 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 one else might h
d8240 61 76 65 20 69 74 20 72 65 73 65 72 76 65 64 20 ave it reserved
d8250 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 */. lrc = s
d8260 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d8270 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d8280 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f SQLITE_IOERR_LO
d8290 43 4b 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 CK); . if(
d82a0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 IS_LOCK_ERROR(lr
d82b0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 c) ){. pF
d82c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
d82d0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 tErrno;.
d82e0 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 rc = lrc;.
d82f0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f }. }. }. O
d8300 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52 STRACE4("TEST WR
d8310 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e -LOCK %d %d %d\n
d8320 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c ", pFile->h, rc,
d8330 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 23 69 66 reserved);..#if
d8340 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 def SQLITE_IGNOR
d8350 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 E_FLOCK_LOCK_ERR
d8360 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 ORS. if( (rc &
d8370 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d SQLITE_IOERR) ==
d8380 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b SQLITE_IOERR ){
d8390 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
d83a0 5f 4f 4b 3b 0a 20 20 20 20 72 65 73 65 72 76 65 _OK;. reserve
d83b0 64 3d 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 d=1;. }.#endif
d83c0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 /* SQLITE_IGNORE
d83d0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d83e0 52 53 20 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 RS */. *pResOut
d83f0 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 = reserved;. r
d8400 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
d8410 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 ** Lock the file
d8420 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 with the lock s
d8430 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 pecified by para
d8440 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d meter locktype -
d8450 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 one.** of the f
d8460 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 ollowing:.**.**
d8470 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c (1) SHARED_L
d8480 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 OCK.** (2) R
d8490 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 ESERVED_LOCK.**
d84a0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f (3) PENDING_
d84b0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 LOCK.** (4)
d84c0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a EXCLUSIVE_LOCK.*
d84d0 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 *.** Sometimes w
d84e0 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f hen requesting o
d84f0 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 ne lock state, a
d8500 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 dditional lock s
d8510 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 tates.** are ins
d8520 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e erted in between
d8530 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d . The locking m
d8540 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 ight fail on one
d8550 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a of the later.**
d8560 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 transitions lea
d8570 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 ving the lock st
d8580 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 ate different fr
d8590 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 om what it start
d85a0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 ed but.** still
d85b0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 short of its goa
d85c0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e l. The followin
d85d0 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 g chart shows th
d85e0 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 e allowed.** tra
d85f0 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 nsitions and the
d8600 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d inserted interm
d8610 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a ediate states:.*
d8620 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 *.** UNLOCKED
d8630 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 -> SHARED.**
d8640 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 SHARED -> RESER
d8650 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 VED.** SHARED
d8660 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
d8670 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
d8680 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 RESERVED -> (PE
d8690 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 NDING) -> EXCLUS
d86a0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e IVE.** PENDIN
d86b0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a G -> EXCLUSIVE.*
d86c0 2a 0a 2a 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c *.** flock() onl
d86d0 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 y really support
d86e0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 EXCLUSIVE locks
d86f0 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e 74 65 . We track inte
d8700 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b rmediate.** lock
d8710 20 73 74 61 74 65 73 20 69 6e 20 74 68 65 20 73 states in the s
d8720 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75 qlite3_file stru
d8730 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c cture, but all l
d8740 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72 0a 2a ocks SHARED or.*
d8750 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65 61 6c * above are real
d8760 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 ly EXCLUSIVE loc
d8770 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65 20 61 ks and exclude a
d8780 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ll other process
d8790 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 es from.** acces
d87a0 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a s the file..**.*
d87b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 * This routine w
d87c0 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 ill only increas
d87d0 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 e a lock. Use t
d87e0 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f he sqlite3OsUnlo
d87f0 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ck().** routine
d8800 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 to lower a locki
d8810 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 ng level..*/.sta
d8820 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 tic int flockLoc
d8830 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d8840 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 id, int locktype
d8850 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
d8860 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 QLITE_OK;. unix
d8870 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 File *pFile = (u
d8880 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 nixFile*)id;..
d8890 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
d88a0 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 .. /* if we alr
d88b0 65 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b eady have a lock
d88c0 2c 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 , it is exclusiv
d88d0 65 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 e. . ** Just a
d88e0 64 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 djust level and
d88f0 70 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 punt on outta he
d8900 72 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 re. */. if (pFi
d8910 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e le->locktype > N
d8920 4f 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 O_LOCK) {. pF
d8930 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
d8940 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 locktype;. re
d8950 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
d8960 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 }. . /* grab
d8970 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f an exclusive lo
d8980 63 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 ck */. . if (f
d8990 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c lock(pFile->h, L
d89a0 4f 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 OCK_EX | LOCK_NB
d89b0 29 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 )) {. int tEr
d89c0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
d89d0 20 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 /* didn't get,
d89e0 6d 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a must be busy */.
d89f0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 rc = sqliteE
d8a00 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 rrorFromPosixErr
d8a10 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 or(tErrno, SQLIT
d8a20 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 E_IOERR_LOCK);.
d8a30 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
d8a40 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
d8a50 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
d8a60 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
d8a70 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 }. } else {.
d8a80 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 /* got it, set
d8a90 20 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 the type and re
d8aa0 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 turn ok */. p
d8ab0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d File->locktype =
d8ac0 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 locktype;. }.
d8ad0 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 OSTRACE4("LOCK
d8ae0 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 %d %s %s\n",
d8af0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
d8b00 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 peName(locktype)
d8b10 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 , . rc
d8b20 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f ==SQLITE_OK ? "o
d8b30 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a k" : "failed");.
d8b40 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 #ifdef SQLITE_IG
d8b50 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f NORE_FLOCK_LOCK_
d8b60 45 52 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 ERRORS. if( (rc
d8b70 20 26 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 & SQLITE_IOERR)
d8b80 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 == SQLITE_IOERR
d8b90 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
d8ba0 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 ITE_BUSY;. }.#e
d8bb0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 ndif /* SQLITE_I
d8bc0 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b GNORE_FLOCK_LOCK
d8bd0 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 _ERRORS */. ret
d8be0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
d8bf0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b * Lower the lock
d8c00 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c ing level on fil
d8c10 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 e descriptor pFi
d8c20 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 le to locktype.
d8c30 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 locktype.** mus
d8c40 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c t be either NO_L
d8c50 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f OCK or SHARED_LO
d8c60 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 CK..**.** If the
d8c70 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f locking level o
d8c80 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 f the file descr
d8c90 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 iptor is already
d8ca0 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 at or below.**
d8cb0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f the requested lo
d8cc0 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 cking level, thi
d8cd0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e s routine is a n
d8ce0 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 o-op..*/.static
d8cf0 69 6e 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 int flockUnlock(
d8d00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
d8d10 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
d8d20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 {. unixFile *pF
d8d30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
d8d40 29 69 64 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 )id;. . assert
d8d50 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 ( pFile );. OST
d8d60 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 RACE5("UNLOCK %
d8d70 64 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d d %d was %d pid=
d8d80 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c %d\n", pFile->h,
d8d90 20 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 locktype,.
d8da0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 pFile->loc
d8db0 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 ktype, getpid())
d8dc0 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b ;. assert( lock
d8dd0 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 type<=SHARED_LOC
d8de0 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d K );. . /* no-
d8df0 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a op if possible *
d8e00 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d8e10 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 ocktype==locktyp
d8e20 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
d8e30 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
d8e40 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 . /* shared ca
d8e50 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 n just be set be
d8e60 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 cause we always
d8e70 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 have an exclusiv
d8e80 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 e */. if (lockt
d8e90 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b ype==SHARED_LOCK
d8ea0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ) {. pFile->l
d8eb0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 ocktype = lockty
d8ec0 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 pe;. return S
d8ed0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
d8ee0 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 . /* no, really
d8ef0 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 , unlock. */. i
d8f00 6e 74 20 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 nt rc = flock(pF
d8f10 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 ile->h, LOCK_UN)
d8f20 3b 0a 20 20 69 66 20 28 72 63 29 20 7b 0a 20 20 ;. if (rc) {.
d8f30 20 20 69 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 int r, tErrno
d8f40 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d = errno;. r =
d8f50 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d sqliteErrorFrom
d8f60 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e PosixError(tErrn
d8f70 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f o, SQLITE_IOERR_
d8f80 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 UNLOCK);. if(
d8f90 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 IS_LOCK_ERROR(r
d8fa0 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ) ){. pFile
d8fb0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
d8fc0 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64 rrno;. }.#ifd
d8fd0 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ef SQLITE_IGNORE
d8fe0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f _FLOCK_LOCK_ERRO
d8ff0 52 53 0a 20 20 20 20 69 66 28 20 28 72 20 26 20 RS. if( (r &
d9000 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d SQLITE_IOERR) ==
d9010 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b SQLITE_IOERR ){
d9020 0a 20 20 20 20 20 20 72 20 3d 20 53 51 4c 49 54 . r = SQLIT
d9030 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65 E_BUSY;. }.#e
d9040 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 ndif /* SQLITE_I
d9050 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b GNORE_FLOCK_LOCK
d9060 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a _ERRORS */. .
d9070 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a 20 20 return r;.
d9080 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 } else {. pFi
d9090 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e le->locktype = N
d90a0 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 O_LOCK;. retu
d90b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
d90c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 }.}../*.** Close
d90d0 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 a file..*/.stat
d90e0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 ic int flockClos
d90f0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a e(sqlite3_file *
d9100 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 id) {. if( id )
d9110 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 {. flockUnloc
d9120 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a k(id, NO_LOCK);.
d9130 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f }. return clo
d9140 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a seUnixFile(id);.
d9150 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c }..#endif /* SQL
d9160 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 ITE_ENABLE_LOCKI
d9170 4e 47 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f NG_STYLE && !OS_
d9180 56 58 57 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a VXWORK */../****
d9190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
d91a0 45 6e 64 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b End of the flock
d91b0 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 lock implementa
d91c0 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a tion ***********
d91d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a **********.*****
d91e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d91f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a *********/../***
d9230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
d9280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
d9290 2a 2a 2a 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 **** Begin Named
d92a0 20 53 65 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 Semaphore Locki
d92b0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ng *************
d92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
d92d0 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 * Named semaphor
d92e0 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c e locking is onl
d92f0 79 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 y supported on V
d9300 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 xWorks..**.** Se
d9310 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 maphore locking
d9320 69 73 20 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b is like dot-lock
d9330 20 61 6e 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 and flock in th
d9340 61 74 20 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c at it really onl
d9350 79 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 45 58 y.** supports EX
d9360 43 4c 55 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e CLUSIVE locking.
d9370 20 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 Only a single
d9380 70 72 6f 63 65 73 73 20 63 61 6e 20 72 65 61 64 process can read
d9390 20 6f 72 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 or write.** the
d93a0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
d93b0 74 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20 t a time. This
d93c0 72 65 64 75 63 65 73 20 70 6f 74 65 6e 74 69 61 reduces potentia
d93d0 6c 20 63 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 l concurrency, b
d93e0 75 74 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 ut.** makes the
d93f0 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 lock implementat
d9400 69 6f 6e 20 6d 75 63 68 20 65 61 73 69 65 72 2e ion much easier.
d9410 0a 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 .*/.#if OS_VXWOR
d9420 4b 53 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 KS../*.** This r
d9430 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 outine checks if
d9440 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 there is a RESE
d9450 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f RVED lock held o
d9460 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a n the specified.
d9470 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 ** file by this
d9480 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f or any other pro
d9490 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 cess. If such a
d94a0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 lock is held, se
d94b0 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f t *pResOut.** to
d94c0 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 a non-zero valu
d94d0 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 e otherwise *pRe
d94e0 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a sOut is set to z
d94f0 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e ero. The return
d9500 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 value.** is set
d9510 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e to SQLITE_OK un
d9520 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f less an I/O erro
d9530 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 r occurs during
d9540 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a lock checking..*
d9550 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d /.static int sem
d9560 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
d9570 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a k(sqlite3_file *
d9580 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 id, int *pResOut
d9590 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ) {. int rc = S
d95a0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 QLITE_OK;. int
d95b0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 reserved = 0;.
d95c0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 unixFile *pFile
d95d0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b = (unixFile*)id;
d95e0 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 .. SimulateIOEr
d95f0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 ror( return SQLI
d9600 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 TE_IOERR_CHECKRE
d9610 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 SERVEDLOCK; );.
d9620 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c . assert( pFil
d9630 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b e );.. /* Check
d9640 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 if a thread in
d9650 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c this process hol
d9660 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a ds such a lock *
d9670 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c /. if( pFile->l
d9680 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c ocktype>SHARED_L
d9690 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 OCK ){. reser
d96a0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a ved = 1;. }. .
d96b0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 /* Otherwise s
d96c0 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ee if some other
d96d0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 process holds i
d96e0 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 t. */. if( !res
d96f0 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65 6d erved ){. sem
d9700 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 _t *pSem = pFile
d9710 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 ->pOpen->pSem;.
d9720 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 struct stat s
d9730 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 tatBuf;.. if(
d9740 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53 65 sem_trywait(pSe
d9750 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 m)==-1 ){.
d9760 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
d9770 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 41 no;. if( EA
d9780 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 GAIN != tErrno )
d9790 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 {. rc = s
d97a0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f qliteErrorFromPo
d97b0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c sixError(tErrno,
d97c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 SQLITE_IOERR_CH
d97d0 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 ECKRESERVEDLOCK)
d97e0 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d ;. pFile-
d97f0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 >lastErrno = tEr
d9800 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 rno;. } els
d9810 65 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 e {. /* s
d9820 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 omeone else has
d9830 74 68 65 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 the lock when we
d9840 20 61 72 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 are in NO_LOCK
d9850 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 72 */. reser
d9860 76 65 64 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f ved = (pFile->lo
d9870 63 6b 74 79 70 65 20 3c 20 53 48 41 52 45 44 5f cktype < SHARED_
d9880 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 LOCK);. }.
d9890 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
d98a0 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65 /* we could have
d98b0 20 69 74 20 69 66 20 77 65 20 77 61 6e 74 20 69 it if we want i
d98c0 74 20 2a 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 t */. sem_p
d98d0 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d ost(pSem);. }
d98e0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 . }. OSTRACE4(
d98f0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 "TEST WR-LOCK %d
d9900 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 %d %d\n", pFile
d9910 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 ->h, rc, reserve
d9920 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 d);.. *pResOut
d9930 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 = reserved;. re
d9940 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
d9950 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 * Lock the file
d9960 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 with the lock sp
d9970 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d ecified by param
d9980 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 eter locktype -
d9990 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f one.** of the fo
d99a0 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
d99b0 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f (1) SHARED_LO
d99c0 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 CK.** (2) RE
d99d0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 SERVED_LOCK.**
d99e0 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c (3) PENDING_L
d99f0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 OCK.** (4) E
d9a00 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a XCLUSIVE_LOCK.**
d9a10 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 .** Sometimes wh
d9a20 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e en requesting on
d9a30 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 e lock state, ad
d9a40 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 ditional lock st
d9a50 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 ates.** are inse
d9a60 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e rted in between.
d9a70 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 The locking mi
d9a80 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 ght fail on one
d9a90 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 of the later.**
d9aa0 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 transitions leav
d9ab0 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 ing the lock sta
d9ac0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f te different fro
d9ad0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 m what it starte
d9ae0 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 d but.** still s
d9af0 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c hort of its goal
d9b00 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 . The following
d9b10 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 chart shows the
d9b20 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e allowed.** tran
d9b30 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 sitions and the
d9b40 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 inserted interme
d9b50 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a diate states:.**
d9b60 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 .** UNLOCKED
d9b70 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 -> SHARED.**
d9b80 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 SHARED -> RESERV
d9b90 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 ED.** SHARED
d9ba0 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 -> (PENDING) ->
d9bb0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 EXCLUSIVE.**
d9bc0 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e RESERVED -> (PEN
d9bd0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 DING) -> EXCLUSI
d9be0 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 VE.** PENDING
d9bf0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a -> EXCLUSIVE.**
d9c00 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f .** Semaphore lo
d9c10 63 6b 73 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 cks only really
d9c20 73 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 support EXCLUSIV
d9c30 45 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 E locks. We tra
d9c40 63 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a ck intermediate.
d9c50 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 ** lock states i
d9c60 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 n the sqlite3_fi
d9c70 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 le structure, bu
d9c80 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 t all locks SHAR
d9c90 45 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 ED or.** above a
d9ca0 72 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 re really EXCLUS
d9cb0 49 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 IVE locks and ex
d9cc0 63 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 clude all other
d9cd0 70 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a processes from.*
d9ce0 2a 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c * access the fil
d9cf0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f e..**.** This ro
d9d00 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 utine will only
d9d10 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e increase a lock.
d9d20 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 Use the sqlite
d9d30 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 3OsUnlock().** r
d9d40 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 outine to lower
d9d50 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e a locking level.
d9d60 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 .*/.static int s
d9d70 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 emLock(sqlite3_f
d9d80 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 ile *id, int loc
d9d90 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 ktype) {. unixF
d9da0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
d9db0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e ixFile*)id;. in
d9dc0 74 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 t fd;. sem_t *p
d9dd0 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 Sem = pFile->pOp
d9de0 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20 en->pSem;. int
d9df0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
d9e00 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 . /* if we alre
d9e10 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c ady have a lock,
d9e20 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 it is exclusive
d9e30 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 . . ** Just ad
d9e40 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 just level and p
d9e50 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 unt on outta her
d9e60 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c e. */. if (pFil
d9e70 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f e->locktype > NO
d9e80 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 _LOCK) {. pFi
d9e90 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c le->locktype = l
d9ea0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 63 20 ocktype;. rc
d9eb0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
d9ec0 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f goto sem_end_lo
d9ed0 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 ck;. }. . /*
d9ee0 6c 6f 63 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e lock semaphore n
d9ef0 6f 77 20 62 75 74 20 62 61 69 6c 20 6f 75 74 20 ow but bail out
d9f00 77 68 65 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 when already loc
d9f10 6b 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 ked. */. if( se
d9f20 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d m_trywait(pSem)=
d9f30 3d 2d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 =-1 ){. rc =
d9f40 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
d9f50 20 67 6f 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f goto sem_end_lo
d9f60 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f ck;. }.. /* go
d9f70 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79 t it, set the ty
d9f80 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b pe and return ok
d9f90 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 */. pFile->loc
d9fa0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 ktype = locktype
d9fb0 3b 0a 0a 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b ;.. sem_end_lock
d9fc0 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d :. return rc;.}
d9fd0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 ../*.** Lower th
d9fe0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 e locking level
d9ff0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 on file descript
da000 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b or pFile to lock
da010 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a type. locktype.
da020 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 ** must be eithe
da030 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 r NO_LOCK or SHA
da040 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 RED_LOCK..**.**
da050 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c If the locking l
da060 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 evel of the file
da070 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 descriptor is a
da080 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c lready at or bel
da090 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 ow.** the reques
da0a0 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 ted locking leve
da0b0 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 l, this routine
da0c0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 is a no-op..*/.s
da0d0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 55 6e 6c tatic int semUnl
da0e0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ock(sqlite3_file
da0f0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 *id, int lockty
da100 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 pe) {. unixFile
da110 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
da120 69 6c 65 2a 29 69 64 3b 0a 20 20 73 65 6d 5f 74 ile*)id;. sem_t
da130 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e *pSem = pFile->
da140 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 pOpen->pSem;..
da150 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b assert( pFile );
da160 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6d 20 . assert( pSem
da170 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 );. OSTRACE5("U
da180 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 NLOCK %d %d was
da190 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 %d pid=%d\n", p
da1a0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
da1b0 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e 6c 6f e,.. pFile->lo
da1c0 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64 28 29 cktype, getpid()
da1d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 );. assert( loc
da1e0 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f ktype<=SHARED_LO
da1f0 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f CK );. . /* no
da200 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c 65 20 -op if possible
da210 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e */. if( pFile->
da220 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 locktype==lockty
da230 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e pe ){. return
da240 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
da250 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64 20 63 . /* shared c
da260 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74 20 62 an just be set b
da270 65 63 61 75 73 65 20 77 65 20 61 6c 77 61 79 73 ecause we always
da280 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 have an exclusi
da290 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b ve */. if (lock
da2a0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 type==SHARED_LOC
da2b0 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e K) {. pFile->
da2c0 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 locktype = lockt
da2d0 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ype;. return
da2e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
da2f0 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c . /* no, reall
da300 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 y unlock. */. i
da310 66 20 28 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 f ( sem_post(pSe
da320 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 m)==-1 ) {. i
da330 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 nt rc, tErrno =
da340 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 errno;. rc =
da350 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 sqliteErrorFromP
da360 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f osixError(tErrno
da370 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 , SQLITE_IOERR_U
da380 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 NLOCK);. if(
da390 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 IS_LOCK_ERROR(rc
da3a0 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ) ){. pFile
da3b0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 ->lastErrno = tE
da3c0 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rrno;. }.
da3d0 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a return rc; . }.
da3e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
da3f0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 e = NO_LOCK;. r
da400 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
da410 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 .}../*. ** Close
da420 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 a file.. */.sta
da430 74 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f 73 65 tic int semClose
da440 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
da450 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b d) {. if( id ){
da460 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 . unixFile *p
da470 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
da480 2a 29 69 64 3b 0a 20 20 20 20 73 65 6d 55 6e 6c *)id;. semUnl
da490 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 ock(id, NO_LOCK)
da4a0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 ;. assert( pF
da4b0 69 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69 78 45 ile );. unixE
da4c0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
da4d0 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f releaseLockInfo
da4e0 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a (pFile->pLock);.
da4f0 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 releaseOpenC
da500 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 nt(pFile->pOpen)
da510 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d ;. unixLeaveM
da520 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73 utex();. clos
da530 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 eUnixFile(id);.
da540 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 }. return SQLI
da550 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 TE_OK;.}..#endif
da560 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a /* OS_VXWORKS *
da570 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 /./*.** Named se
da580 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 maphore locking
da590 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
da5a0 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a e on VxWorks..**
da5b0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
da5c0 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61 6d 65 End of the name
da5d0 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b d semaphore lock
da5e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
da5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da600 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
da610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
da650 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .../************
da660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6a0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
da6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
da6c0 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67 egin AFP Locking
da6d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
da6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
da6f0 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20 **.**.** AFP is
da700 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69 6e 67 the Apple Filing
da710 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20 Protocol. AFP
da720 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c is a network fil
da730 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a esystem found.**
da740 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69 6e 74 on Apple Macint
da750 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20 2d 20 osh computers -
da760 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f 53 58 both OS9 and OSX
da770 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61 ..**.** Third-pa
da780 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 rty implementati
da790 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65 20 61 ons of AFP are a
da7a0 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74 20 74 vailable. But t
da7b0 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a his code here.**
da7c0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f only works on O
da7d0 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 SX..*/..#if defi
da7e0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
da7f0 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
da800 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a LOCKING_STYLE./*
da810 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 .** The afpLocki
da820 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75 63 74 ngContext struct
da830 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c ure contains all
da840 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 afp lock specif
da850 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65 ic state.*/.type
da860 64 65 66 20 73 74 72 75 63 74 20 61 66 70 4c 6f def struct afpLo
da870 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 ckingContext afp
da880 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a LockingContext;.
da890 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e struct afpLockin
da8a0 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 gContext {. uns
da8b0 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 igned long long
da8c0 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20 63 6f sharedByte;. co
da8d0 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68 nst char *dbPath
da8e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
da8f0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 Name of the ope
da900 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 n file */.};..st
da910 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f ruct ByteRangeLo
da920 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e ckPB2.{. unsign
da930 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 ed long long off
da940 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f set; /* o
da950 66 66 73 65 74 20 74 6f 20 66 69 72 73 74 20 62 ffset to first b
da960 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 yte to lock */.
da970 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c unsigned long l
da980 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 ong length;
da990 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 /* nbr of byt
da9a0 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 es to lock */.
da9b0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
da9c0 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61 72 74 ng retRangeStart
da9d0 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 ; /* nbr of 1st
da9e0 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 byte locked if s
da9f0 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 uccessful */. u
daa00 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75 6e 4c nsigned char unL
daa10 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20 20 20 ockFlag;
daa20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 /* 1 = unlock,
daa30 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 0 = lock */. un
daa40 73 69 67 6e 65 64 20 63 68 61 72 20 73 74 61 72 signed char star
daa50 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20 20 20 tEndFlag;
daa60 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 /* 1=rel to end
daa70 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 of fork, 0=rel t
daa80 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 o start */. int
daa90 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 fd;
daaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
daab0 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f 20 61 * file desc to a
daac0 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 ssoc this lock w
daad0 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 ith */.};..#defi
daae0 6e 65 20 61 66 70 66 73 42 79 74 65 52 61 6e 67 ne afpfsByteRang
daaf0 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20 20 20 eLock2FSCTL
dab00 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 _IOWR('z', 23
dab10 2c 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e , struct ByteRan
dab20 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a geLockPB2)../*.*
dab30 2a 20 54 68 69 73 20 69 73 20 61 20 75 74 69 6c * This is a util
dab40 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e 67 20 ity for setting
dab50 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20 62 69 or clearing a bi
dab60 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 t-range lock on
dab70 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65 73 79 an.** AFP filesy
dab80 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 stem..** .** Ret
dab90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
daba0 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 success, SQLITE
dabb0 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 _BUSY on failure
dabc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
dabd0 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f afpSetLock(. co
dabe0 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 nst char *path,
dabf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
dac00 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 Name of the file
dac10 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 6f 72 to be locked or
dac20 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 unlocked */. u
dac30 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 nixFile *pFile,
dac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
dac50 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 Open file descr
dac60 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20 2a 2f iptor on path */
dac70 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 . unsigned long
dac80 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20 20 20 long offset,
dac90 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
daca0 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a to be locked */.
dacb0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 unsigned long
dacc0 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20 20 20 long length,
dacd0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
dace0 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 tes to lock */.
dacf0 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 int setLockFlag
dad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dad10 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 6c /* True to set l
dad20 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f 20 63 ock. False to c
dad30 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a lear lock */.){.
dad40 20 20 73 74 72 75 63 74 20 42 79 74 65 52 61 6e struct ByteRan
dad50 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a 20 20 geLockPB2 pb;.
dad60 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20 70 62 int err;. . pb
dad70 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65 .unLockFlag = se
dad80 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20 tLockFlag ? 0 :
dad90 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e 64 1;. pb.startEnd
dada0 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f Flag = 0;. pb.o
dadb0 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a ffset = offset;.
dadc0 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 pb.length = le
dadd0 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d ngth; . pb.fd =
dade0 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 pFile->h;. .
dadf0 4f 53 54 52 41 43 45 36 28 22 41 46 50 53 45 54 OSTRACE6("AFPSET
dae00 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20 25 64 LOCK [%s] for %d
dae10 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c 6c 78 %s in range %llx
dae20 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 28 :%llx\n", . (
dae30 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 setLockFlag?"ON"
dae40 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65 2d 3e :"OFF"), pFile->
dae50 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b h, (pb.fd==-1?"[
dae60 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22 29 2c testval-1]":""),
dae70 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c 65 6e . offset, len
dae80 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20 66 73 gth);. err = fs
dae90 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66 73 42 ctl(path, afpfsB
daea0 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 yteRangeLock2FSC
daeb0 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20 20 69 TL, &pb, 0);. i
daec0 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a f ( err==-1 ) {.
daed0 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 int rc;.
daee0 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 int tErrno = err
daef0 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 no;. OSTRACE4
daf00 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66 61 69 ("AFPSETLOCK fai
daf10 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29 20 27 led to fsctl() '
daf20 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 %s' %d %s\n",.
daf30 20 20 20 20 20 20 20 20 20 20 20 70 61 74 68 2c path,
daf40 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f tErrno, strerro
daf50 72 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69 66 64 r(tErrno));.#ifd
daf60 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 ef SQLITE_IGNORE
daf70 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 _AFP_LOCK_ERRORS
daf80 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
daf90 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20 _BUSY;.#else.
dafa0 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f rc = sqliteErro
dafb0 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 rFromPosixError(
dafc0 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 tErrno,.
dafd0 20 20 20 20 20 20 20 20 20 20 20 20 73 65 74 4c setL
dafe0 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 ockFlag ? SQLITE
daff0 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 _IOERR_LOCK : SQ
db000 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
db010 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 K);.#endif /* SQ
db020 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f LITE_IGNORE_AFP_
db030 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 LOCK_ERRORS */.
db040 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 if( IS_LOCK_E
db050 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 RROR(rc) ){.
db060 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
db070 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 no = tErrno;.
db080 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 }. return rc
db090 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 ;. } else {.
db0a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
db0b0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 K;. }.}../*.**
db0c0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 This routine che
db0d0 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 cks if there is
db0e0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 a RESERVED lock
db0f0 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 held on the spec
db100 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 ified.** file by
db110 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 this or any oth
db120 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 er process. If s
db130 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 uch a lock is he
db140 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 ld, set *pResOut
db150 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 .** to a non-zer
db160 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 o value otherwis
db170 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 e *pResOut is se
db180 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 t to zero. The
db190 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 return value.**
db1a0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 is set to SQLITE
db1b0 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f _OK unless an I/
db1c0 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 O error occurs d
db1d0 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b uring lock check
db1e0 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ing..*/.static i
db1f0 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 nt afpCheckReser
db200 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
db210 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 file *id, int *p
db220 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 ResOut){. int r
db230 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
db240 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 int reserved =
db250 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 0;. unixFile *p
db260 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
db270 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c *)id;. . Simul
db280 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 ateIOError( retu
db290 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
db2a0 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 CHECKRESERVEDLOC
db2b0 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 K; );. . asser
db2c0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 66 t( pFile );. af
db2d0 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 pLockingContext
db2e0 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c *context = (afpL
db2f0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 ockingContext *)
db300 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 pFile->lockingC
db310 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 ontext;. . /*
db320 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 Check if a threa
db330 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 d in this proces
db340 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c s holds such a l
db350 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 ock */. if( pFi
db360 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 le->locktype>SHA
db370 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 RED_LOCK ){.
db380 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 reserved = 1;.
db390 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 }. . /* Otherw
db3a0 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 ise see if some
db3b0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f other process ho
db3c0 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 lds it.. */.
db3d0 69 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b if( !reserved ){
db3e0 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 . /* lock the
db3f0 20 52 45 53 45 52 56 45 44 20 62 79 74 65 20 2a RESERVED byte *
db400 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 /. int lrc =
db410 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 afpSetLock(conte
db420 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c xt->dbPath, pFil
db430 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 e, RESERVED_BYTE
db440 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69 66 , 1,1); . if
db450 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 ( SQLITE_OK==lrc
db460 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 ){. /* if
db470 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e 20 we succeeded in
db480 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73 65 72 taking the reser
db490 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b ved lock, unlock
db4a0 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 0a 20 it to restore.
db4b0 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 69 67 ** the orig
db4c0 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a 20 20 inal state */.
db4d0 20 20 20 20 6c 72 63 20 3d 20 61 66 70 53 65 74 lrc = afpSet
db4e0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 Lock(context->db
db4f0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 Path, pFile, RES
db500 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 ERVED_BYTE, 1, 0
db510 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a );. } else {.
db520 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 66 /* if we f
db530 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74 68 65 ailed to get the
db540 20 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f lock then someo
db550 6e 65 20 65 6c 73 65 20 6d 75 73 74 20 68 61 76 ne else must hav
db560 65 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 e it */. re
db570 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 served = 1;.
db580 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 }. if( IS_LOC
db590 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a K_ERROR(lrc) ){.
db5a0 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 rc=lrc;.
db5b0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 }. }. . OST
db5c0 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c RACE4("TEST WR-L
db5d0 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c OCK %d %d %d\n",
db5e0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 pFile->h, rc, r
db5f0 65 73 65 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a eserved);. . *
db600 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 pResOut = reserv
db610 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b ed;. return rc;
db620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 .}../*.** Lock t
db630 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 he file with the
db640 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 lock specified
db650 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 by parameter loc
db660 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f ktype - one.** o
db670 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a f the following:
db680 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 .**.** (1) S
db690 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 HARED_LOCK.**
db6a0 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c (2) RESERVED_L
db6b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 OCK.** (3) P
db6c0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 ENDING_LOCK.**
db6d0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 (4) EXCLUSIVE
db6e0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 _LOCK.**.** Some
db6f0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 times when reque
db700 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 sting one lock s
db710 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c tate, additional
db720 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 lock states.**
db730 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 are inserted in
db740 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f between. The lo
db750 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c cking might fail
db760 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c on one of the l
db770 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 ater.** transiti
db780 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 ons leaving the
db790 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 lock state diffe
db7a0 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 rent from what i
db7b0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a t started but.**
db7c0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 still short of
db7d0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 its goal. The f
db7e0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 ollowing chart s
db7f0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 hows the allowed
db800 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 .** transitions
db810 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 and the inserted
db820 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 intermediate st
db830 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 ates:.**.** U
db840 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 NLOCKED -> SHARE
db850 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d D.** SHARED -
db860 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 > RESERVED.**
db870 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 SHARED -> (PEND
db880 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 ING) -> EXCLUSIV
db890 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 E.** RESERVED
db8a0 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e -> (PENDING) ->
db8b0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 EXCLUSIVE.**
db8c0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c PENDING -> EXCL
db8d0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 USIVE.**.** This
db8e0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e routine will on
db8f0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f ly increase a lo
db900 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c ck. Use the sql
db910 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a ite3OsUnlock().*
db920 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 * routine to low
db930 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 er a locking lev
db940 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e el..*/.static in
db950 74 20 61 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 t afpLock(sqlite
db960 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
db970 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 locktype){. int
db980 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
db990 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 . unixFile *pFi
db9a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 le = (unixFile*)
db9b0 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 id;. afpLocking
db9c0 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 Context *context
db9d0 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f = (afpLockingCo
db9e0 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e ntext *) pFile->
db9f0 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a lockingContext;.
dba00 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 . assert( pFi
dba10 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 le );. OSTRACE5
dba20 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 ("LOCK %d %s
dba30 77 61 73 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 was %s pid=%d\n"
dba40 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 , pFile->h,.
dba50 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d locktypeNam
dba60 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 e(locktype), loc
dba70 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d ktypeName(pFile-
dba80 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70 >locktype), getp
dba90 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 id());.. /* If
dbaa0 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 there is already
dbab0 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 a lock of this
dbac0 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 type or more res
dbad0 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a trictive on the.
dbae0 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 ** unixFile, d
dbaf0 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 o nothing. Don't
dbb00 20 75 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64 use the afp_end
dbb10 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 _lock: exit path
dbb20 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e , as. ** unixEn
dbb30 74 65 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 terMutex() hasn'
dbb40 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 t been called ye
dbb50 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 t.. */. if( pF
dbb60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c ile->locktype>=l
dbb70 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f ocktype ){. O
dbb80 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 STRACE3("LOCK
dbb90 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 %d %s ok (alrea
dbba0 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 dy held)\n", pFi
dbbb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
dbbc0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c locktypeName(l
dbbd0 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20 20 72 ocktype));. r
dbbe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
dbbf0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 . }.. /* Make
dbc00 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 sure the locking
dbc10 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 sequence is cor
dbc20 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 rect. */. asse
dbc30 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 rt( pFile->lockt
dbc40 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 ype!=NO_LOCK ||
dbc50 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 locktype==SHARED
dbc60 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 _LOCK );. asser
dbc70 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e t( locktype!=PEN
dbc80 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 DING_LOCK );. a
dbc90 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
dbca0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c =RESERVED_LOCK |
dbcb0 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 | pFile->locktyp
dbcc0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
dbcd0 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d ;. . /* This m
dbce0 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 20 62 utex is needed b
dbcf0 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c ecause pFile->pL
dbd00 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 ock is shared ac
dbd10 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20 2a ross threads. *
dbd20 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 /. unixEnterMut
dbd30 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 ex();.. /* Make
dbd40 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e sure the curren
dbd50 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 t thread owns th
dbd60 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 e pFile.. */.
dbd70 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e rc = transferOwn
dbd80 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 ership(pFile);.
dbd90 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
dbda0 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 OK ){. unixLe
dbdb0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 aveMutex();.
dbdc0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 return rc;. }.
dbdd0 20 20 20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 . /* A PENDI
dbde0 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 NG lock is neede
dbdf0 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69 d before acquiri
dbe00 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b ng a SHARED lock
dbe10 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a and before. **
dbe20 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58 acquiring an EX
dbe30 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 CLUSIVE lock. F
dbe40 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f or the SHARED lo
dbe50 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20 ck, the PENDING
dbe60 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c will. ** be rel
dbe70 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 eased.. */. if
dbe80 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 ( locktype==SHAR
dbe90 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c ED_LOCK . |
dbea0 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 | (locktype==EXC
dbeb0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 LUSIVE_LOCK && p
dbec0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 File->locktype<P
dbed0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 ENDING_LOCK). )
dbee0 7b 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 {. int failed
dbef0 3b 0a 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 ;. failed = a
dbf00 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 fpSetLock(contex
dbf10 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 t->dbPath, pFile
dbf20 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
dbf30 31 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 1, 1);. if (f
dbf40 61 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 ailed) {. r
dbf50 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 c = failed;.
dbf60 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c goto afp_end_l
dbf70 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ock;. }. }.
dbf80 20 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f . /* If contro
dbf90 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 l gets to this p
dbfa0 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 oint, then actua
dbfb0 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 lly go ahead and
dbfc0 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 make. ** opera
dbfd0 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c ting system call
dbfe0 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 s for the specif
dbff0 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 ied lock.. */.
dc000 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 if( locktype==S
dc010 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 HARED_LOCK ){.
dc020 20 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 int lk, lrc1,
dc030 6c 72 63 32 3b 0a 20 20 20 20 69 6e 74 20 6c 72 lrc2;. int lr
dc040 63 31 45 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 20 c1Errno = 0;.
dc050 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 . /* Now get
dc060 20 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 the read-lock S
dc070 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 HARED_LOCK */.
dc080 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 /* note that t
dc090 68 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 he quality of th
dc0a0 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 e randomness doe
dc0b0 73 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 sn't matter that
dc0c0 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 much */. lk
dc0d0 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 = random(); .
dc0e0 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 context->shared
dc0f0 42 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 Byte = (lk & 0x7
dc100 66 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 fffffff)%(SHARED
dc110 5f 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 _SIZE - 1);.
dc120 6c 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 lrc1 = afpSetLoc
dc130 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 k(context->dbPat
dc140 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 h, pFile, .
dc150 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 SHARED_FIRS
dc160 54 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 T+context->share
dc170 64 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 dByte, 1, 1);.
dc180 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 if( IS_LOCK_ER
dc190 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 ROR(lrc1) ){.
dc1a0 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 lrc1Errno = p
dc1b0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b File->lastErrno;
dc1c0 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 . }. /* Dr
dc1d0 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 op the temporary
dc1e0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f PENDING lock */
dc1f0 0a 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 . lrc2 = afpS
dc200 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
dc210 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 dbPath, pFile, P
dc220 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 ENDING_BYTE, 1,
dc230 30 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 0);. . if(
dc240 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c IS_LOCK_ERROR(l
dc250 72 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 rc1) ) {. p
dc260 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 File->lastErrno
dc270 3d 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 = lrc1Errno;.
dc280 20 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 rc = lrc1;.
dc290 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 goto afp_end
dc2a0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 _lock;. } els
dc2b0 65 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 e if( IS_LOCK_ER
dc2c0 52 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 ROR(lrc2) ){.
dc2d0 20 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 rc = lrc2;.
dc2e0 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 goto afp_end
dc2f0 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 _lock;. } els
dc300 65 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 e if( lrc1 != SQ
dc310 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 LITE_OK ) {.
dc320 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 rc = lrc1;.
dc330 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
dc340 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 pFile->locktype
dc350 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 = SHARED_LOCK;.
dc360 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 pFile->pOpe
dc370 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 n->nLock++;.
dc380 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f }. }else{. /
dc390 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 * The request wa
dc3a0 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 s for a RESERVED
dc3b0 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
dc3c0 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a ck. It is. *
dc3d0 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 * assumed that t
dc3e0 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 here is a SHARED
dc3f0 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b or greater lock
dc400 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 on the file.
dc410 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 ** already..
dc420 20 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c */. int fail
dc430 65 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 ed = 0;. asse
dc440 72 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f rt( 0!=pFile->lo
dc450 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 cktype );. if
dc460 20 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 (locktype >= RE
dc470 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 SERVED_LOCK && p
dc480 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c File->locktype <
dc490 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 RESERVED_LOCK)
dc4a0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 {. /* Acq
dc4b0 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 uire a RESERVED
dc4c0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 lock */.
dc4d0 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c failed = afpSetL
dc4e0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
dc4f0 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 ath, pFile, RESE
dc500 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b RVED_BYTE, 1,1);
dc510 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 . }. if (!
dc520 66 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 failed && lockty
dc530 70 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f pe == EXCLUSIVE_
dc540 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a LOCK) {. /*
dc550 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c Acquire an EXCL
dc560 55 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 USIVE lock */.
dc570 20 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 . /*
dc580 52 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 Remove the share
dc590 64 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 d lock before tr
dc5a0 79 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 ying the range.
dc5b0 20 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a we'll need to .
dc5c0 20 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 ** reestab
dc5d0 6c 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 lish the shared
dc5e0 6c 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 lock if we can't
dc5f0 20 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c get the afpUnl
dc600 6f 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ock. */.
dc610 20 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 if( !(failed
dc620 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e = afpSetLock(con
dc630 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 text->dbPath, pF
dc640 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ile, SHARED_FIRS
dc650 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 T +.
dc660 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e con
dc670 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 text->sharedByte
dc680 2c 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 , 1, 0)) ){.
dc690 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 int failed2
dc6a0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
dc6b0 20 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 /* now atte
dc6c0 6d 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 mmpt to get the
dc6d0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 exclusive lock r
dc6e0 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 ange */.
dc6f0 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c failed = afpSetL
dc700 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 ock(context->dbP
dc710 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 ath, pFile, SHAR
dc720 45 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 ED_FIRST, .
dc730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dc740 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 SHARED
dc750 5f 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 _SIZE, 1);.
dc760 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 if( failed &&
dc770 20 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 (failed2 = afpS
dc780 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e etLock(context->
dc790 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a dbPath, pFile, .
dc7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
dc7b0 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 SHARED_FI
dc7c0 52 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 RST + context->s
dc7d0 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 haredByte, 1, 1)
dc7e0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f ) ){. /
dc7f0 2a 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c * Can't reestabl
dc800 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c ish the shared l
dc810 6f 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e ock. Sqlite can
dc820 27 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 't deal, this is
dc830 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 . ** a
dc840 63 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 critical I/O err
dc850 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a or. */.
dc860 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 rc = (
dc870 28 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 (failed & SQLITE
dc880 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 _IOERR) == SQLIT
dc890 45 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 E_IOERR) ? faile
dc8a0 64 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 d2 : .
dc8b0 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 SQLITE_IOER
dc8c0 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 R_LOCK;.
dc8d0 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c goto afp_end_l
dc8e0 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a ock;. } .
dc8f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
dc900 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 rc = failed
dc910 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d ; . }. }
dc920 0a 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 . if( failed
dc930 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 ){. rc = fa
dc940 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a iled;. }. }.
dc950 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
dc960 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 ITE_OK ){. pF
dc970 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 ile->locktype =
dc980 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 locktype;. }els
dc990 65 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d e if( locktype==
dc9a0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 EXCLUSIVE_LOCK )
dc9b0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 {. pFile->loc
dc9c0 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f ktype = PENDING_
dc9d0 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 LOCK;. }. .afp
dc9e0 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 _end_lock:. uni
dc9f0 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 xLeaveMutex();.
dca00 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 OSTRACE4("LOCK
dca10 20 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 %d %s %s\n",
dca20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 pFile->h, lockty
dca30 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 peName(locktype)
dca40 2c 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d , . rc==
dca50 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 SQLITE_OK ? "ok"
dca60 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 : "failed");.
dca70 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
dca80 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f .** Lower the lo
dca90 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 cking level on f
dcaa0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 ile descriptor p
dcab0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 File to locktype
dcac0 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d . locktype.** m
dcad0 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f ust be either NO
dcae0 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f _LOCK or SHARED_
dcaf0 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 LOCK..**.** If t
dcb00 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c he locking level
dcb10 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 of the file des
dcb20 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 criptor is alrea
dcb30 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a dy at or below.*
dcb40 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 * the requested
dcb50 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 locking level, t
dcb60 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
dcb70 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 no-op..*/.stati
dcb80 63 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 c int afpUnlock(
dcb90 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
dcba0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 , int locktype)
dcbb0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
dcbc0 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 ITE_OK;. unixFi
dcbd0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 le *pFile = (uni
dcbe0 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 xFile*)id;. afp
dcbf0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
dcc00 70 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 pCtx = (afpLocki
dcc10 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 ngContext *) pFi
dcc20 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
dcc30 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 xt;.. assert( p
dcc40 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 File );. OSTRAC
dcc50 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 E5("UNLOCK %d %
dcc60 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c d was %d pid=%d\
dcc70 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f n", pFile->h, lo
dcc80 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 cktype,.
dcc90 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dcca0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 , getpid());..
dccb0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 assert( locktype
dccc0 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b <=SHARED_LOCK );
dccd0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f . if( pFile->lo
dcce0 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 cktype<=locktype
dccf0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
dcd00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
dcd10 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 if( CHECK_THREAD
dcd20 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 ID(pFile) ){.
dcd30 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d return SQLITE_M
dcd40 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 ISUSE;. }. uni
dcd50 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 xEnterMutex();.
dcd60 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b if( pFile->lock
dcd70 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b type>SHARED_LOCK
dcd80 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 ){. . if(
dcd90 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
dcda0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b ==EXCLUSIVE_LOCK
dcdb0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 ){. rc = a
dcdc0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e fpSetLock(pCtx->
dcdd0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 dbPath, pFile, S
dcde0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 HARED_FIRST, SHA
dcdf0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 RED_SIZE, 0);.
dce00 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
dce10 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 TE_OK && locktyp
dce20 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e==SHARED_LOCK )
dce30 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c {. /* onl
dce40 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 y re-establish t
dce50 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 he shared lock i
dce60 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 f necessary */.
dce70 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 int share
dce80 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 dLockByte = SHAR
dce90 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 ED_FIRST+pCtx->s
dcea0 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 haredByte;.
dceb0 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f rc = afpSetLo
dcec0 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c ck(pCtx->dbPath,
dced0 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f pFile, sharedLo
dcee0 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 ckByte, 1, 1);.
dcef0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
dcf00 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
dcf10 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 OK && pFile->loc
dcf20 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c ktype>=PENDING_L
dcf30 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 OCK ){. rc
dcf40 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
dcf50 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
dcf60 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 , PENDING_BYTE,
dcf70 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 1, 0);. } .
dcf80 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
dcf90 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f _OK && pFile->lo
dcfa0 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 cktype>=RESERVED
dcfb0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 _LOCK ){. r
dcfc0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 c = afpSetLock(p
dcfd0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 Ctx->dbPath, pFi
dcfe0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 le, RESERVED_BYT
dcff0 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a E, 1, 0);. }.
dd000 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b }else if( lock
dd010 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b type==NO_LOCK ){
dd020 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 . /* clear th
dd030 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f e shared lock */
dd040 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c . int sharedL
dd050 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 ockByte = SHARED
dd060 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 _FIRST+pCtx->sha
dd070 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 redByte;. rc
dd080 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 = afpSetLock(pCt
dd090 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 x->dbPath, pFile
dd0a0 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 , sharedLockByte
dd0b0 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 , 1, 0);. }..
dd0c0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
dd0d0 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 K ){. if( loc
dd0e0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 ktype==NO_LOCK )
dd0f0 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 {. struct u
dd100 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 nixOpenCnt *pOpe
dd110 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e n = pFile->pOpen
dd120 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e ;. pOpen->n
dd130 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 Lock--;. as
dd140 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f sert( pOpen->nLo
dd150 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 ck>=0 );. i
dd160 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d f( pOpen->nLock=
dd170 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 =0 ){. rc
dd180 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 = closePendingF
dd190 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 ds(pFile);.
dd1a0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 }. }. }. u
dd1b0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
dd1c0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
dd1d0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c E_OK ){. pFil
dd1e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f e->locktype = lo
dd1f0 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 cktype;. }. re
dd200 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
dd210 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 * Close a file &
dd220 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 cleanup AFP spe
dd230 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f cific locking co
dd240 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 ntext .*/.static
dd250 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 int afpClose(sq
dd260 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 lite3_file *id)
dd270 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 {. if( id ){.
dd280 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c unixFile *pFil
dd290 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 e = (unixFile*)i
dd2a0 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b d;. afpUnlock
dd2b0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 (id, NO_LOCK);.
dd2c0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 unixEnterMute
dd2d0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 x();. if( pFi
dd2e0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 le->pOpen && pFi
dd2f0 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b le->pOpen->nLock
dd300 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
dd310 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 there are outsta
dd320 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 nding locks, do
dd330 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f not actually clo
dd340 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 se the file just
dd350 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 . ** yet be
dd360 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 cause that would
dd370 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 clear those loc
dd380 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 ks. Instead, ad
dd390 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 d the file.
dd3a0 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 ** descriptor t
dd3b0 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e o pOpen->aPendin
dd3c0 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 g. It will be a
dd3d0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f utomatically clo
dd3e0 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a sed when. *
dd3f0 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 * the last lock
dd400 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 is cleared..
dd410 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 */. setPe
dd420 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a ndingFd(pFile);.
dd430 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 }. releas
dd440 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e eOpenCnt(pFile->
dd450 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 pOpen);. sqli
dd460 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e te3_free(pFile->
dd470 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b lockingContext);
dd480 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 . closeUnixFi
dd490 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 le(id);. unix
dd4a0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
dd4b0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
dd4c0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 E_OK;.}..#endif
dd4d0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 /* defined(__APP
dd4e0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f LE__) && SQLITE_
dd4f0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 ENABLE_LOCKING_S
dd500 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 TYLE */./*.** Th
dd510 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 e code above is
dd520 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 the AFP lock imp
dd530 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 lementation. Th
dd540 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 e code is specif
dd550 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 ic.** to MacOSX
dd560 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 and does not wor
dd570 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 k on other unix
dd580 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 platforms. No a
dd590 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 lternative.** is
dd5a0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 available. If
dd5b0 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c you don't compil
dd5c0 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 e for a mac, the
dd5d0 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22 n the "unix-afp"
dd5e0 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61 .** VFS is not a
dd5f0 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a vailable..**.***
dd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd610 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46 ** End of the AF
dd620 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 P lock implement
dd630 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ation **********
dd640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
dd650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f ***********/.../
dd6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
dd6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
dd6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
dd700 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 Non-locking sqli
dd710 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 te3_file methods
dd720 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
dd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
dd740 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69 *.** The next di
dd750 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 vision contains
dd760 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 implementations
dd770 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 for all methods
dd780 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 of the .** sqlit
dd790 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f e3_file object o
dd7a0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f ther than the lo
dd7b0 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 cking methods.
dd7c0 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d The locking.** m
dd7d0 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69 ethods were defi
dd7e0 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 ned in divisions
dd7f0 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b above (one lock
dd800 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a ing method per.*
dd810 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 * division). Th
dd820 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 ose methods that
dd830 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 are common to a
dd840 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 ll locking modes
dd850 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74 .** are gather t
dd860 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69 ogether into thi
dd870 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a s division..*/..
dd880 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 /*.** Seek to th
dd890 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20 e offset passed
dd8a0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 as the second ar
dd8b0 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 gument, then rea
dd8c0 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 d cnt .** bytes
dd8d0 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72 into pBuf. Retur
dd8e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
dd8f0 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 bytes actually r
dd900 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 ead..**.** NB:
dd910 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53 If you define US
dd920 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50 E_PREAD or USE_P
dd930 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20 READ64, then it
dd940 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 might also.** be
dd950 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65 necessary to de
dd960 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 fine _XOPEN_SOUR
dd970 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54 CE to be 500. T
dd980 68 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a his varies from.
dd990 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f ** one system to
dd9a0 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 another. Since
dd9b0 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 SQLite does not
dd9c0 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 define USE_PREA
dd9d0 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 D.** any any for
dd9e0 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65 m by default, we
dd9f0 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 will not attemp
dda00 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 t to define _XOP
dda10 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 EN_SOURCE..** Se
dda20 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20 e tickets #2741
dda30 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a and #2681..**.**
dda40 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 To avoid stompi
dda50 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c ng the errno val
dda60 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 ue on a failed r
dda70 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e ead the lastErrn
dda80 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 o value.** is se
dda90 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 t before returni
ddaa0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ng..*/.static in
ddab0 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e t seekAndRead(un
ddac0 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 ixFile *id, sqli
ddad0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
ddae0 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e , void *pBuf, in
ddaf0 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f t cnt){. int go
ddb00 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 t;. i64 newOffs
ddb10 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 et;. TIMER_STAR
ddb20 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 T;.#if defined(U
ddb30 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 SE_PREAD). got
ddb40 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 = pread(id->h, p
ddb50 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 Buf, cnt, offset
ddb60 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
ddb70 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 rror( got = -1 )
ddb80 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 ;.#elif defined(
ddb90 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 USE_PREAD64). g
ddba0 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d ot = pread64(id-
ddbb0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f >h, pBuf, cnt, o
ddbc0 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 ffset);. Simula
ddbd0 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d teIOError( got =
ddbe0 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e -1 );.#else. n
ddbf0 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b ewOffset = lseek
ddc00 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 (id->h, offset,
ddc10 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d SEEK_SET);. Sim
ddc20 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 ulateIOError( ne
ddc30 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 wOffset-- );. i
ddc40 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 f( newOffset!=of
ddc50 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 fset ){. if(
ddc60 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 newOffset == -1
ddc70 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 ){. ((unixF
ddc80 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 ile*)id)->lastEr
ddc90 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
ddca0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 }else{. ((
ddcb0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c unixFile*)id)->l
ddcc0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 astErrno = 0;...
ddcd0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
ddce0 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 n -1;. }. got
ddcf0 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 = read(id->h, pB
ddd00 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 uf, cnt);.#endif
ddd10 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 . TIMER_END;.
ddd20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 if( got<0 ){.
ddd30 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
ddd40 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
ddd50 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 rno;. }. OSTRA
ddd60 43 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33 CE5("READ %-3
ddd70 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 d %5d %7lld %llu
ddd80 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c \n", id->h, got,
ddd90 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 offset, TIMER_E
ddda0 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72 LAPSED);. retur
dddb0 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 n got;.}../*.**
dddc0 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 Read data from a
dddd0 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 file into a buf
ddde0 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c fer. Return SQL
dddf0 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a ITE_OK if all.**
dde00 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 bytes were read
dde10 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e successfully an
dde20 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 d SQLITE_IOERR i
dde30 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a f anything goes.
dde40 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 ** wrong..*/.sta
dde50 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64 tic int unixRead
dde60 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
dde70 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 *id, . void *p
dde80 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c Buf, . int amt,
dde90 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 . sqlite3_int64
ddea0 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69 offset.){. uni
ddeb0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
ddec0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 unixFile *)id;.
dded0 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 73 65 int got;. asse
ddee0 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20 rt( id );.. /*
ddef0 49 66 20 74 68 69 73 20 69 73 20 61 20 64 61 74 If this is a dat
ddf00 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 abase file (not
ddf10 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65 a journal, maste
ddf20 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d r-journal or tem
ddf30 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68 p. ** file), th
ddf40 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c e bytes in the l
ddf50 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 68 6f ocking range sho
ddf60 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 65 61 uld never be rea
ddf70 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f d or written. */
ddf80 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 . assert( pFile
ddf90 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20 ->pUnused==0.
ddfa0 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 || offset>=P
ddfb0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a ENDING_BYTE+512.
ddfc0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 || offset
ddfd0 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 +amt<=PENDING_BY
ddfe0 54 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20 TE . );.. got
ddff0 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 = seekAndRead(pF
de000 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 ile, offset, pBu
de010 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 f, amt);. if( g
de020 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 ot==amt ){. r
de030 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
de040 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 . }else if( got
de050 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73 <0 ){. /* las
de060 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 tErrno set by se
de070 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 ekAndRead */.
de080 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
de090 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c OERR_READ;. }el
de0a0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c se{. pFile->l
de0b0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a astErrno = 0; /*
de0c0 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 not a system er
de0d0 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e ror */. /* Un
de0e0 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 read parts of th
de0f0 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 e buffer must be
de100 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a zero-filled */.
de110 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 memset(&((ch
de120 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 ar*)pBuf)[got],
de130 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 0, amt-got);.
de140 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
de150 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b OERR_SHORT_READ;
de160 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 . }.}../*.** Se
de170 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 ek to the offset
de180 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 in id->offset t
de190 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 hen read cnt byt
de1a0 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a es into pBuf..**
de1b0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
de1c0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 er of bytes actu
de1d0 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 ally read. Upda
de1e0 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a te the offset..*
de1f0 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 *.** To avoid st
de200 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f omping the errno
de210 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c value on a fail
de220 65 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 ed write the las
de230 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 tErrno value.**
de240 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 is set before re
de250 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 turning..*/.stat
de260 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 ic int seekAndWr
de270 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 ite(unixFile *id
de280 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f , i64 offset, co
de290 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 nst void *pBuf,
de2a0 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 int cnt){. int
de2b0 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 got;. i64 newOf
de2c0 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 fset;. TIMER_ST
de2d0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 ART;.#if defined
de2e0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f (USE_PREAD). go
de2f0 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 t = pwrite(id->h
de300 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 , pBuf, cnt, off
de310 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 set);.#elif defi
de320 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 ned(USE_PREAD64)
de330 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 . got = pwrite6
de340 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 4(id->h, pBuf, c
de350 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c nt, offset);.#el
de360 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d se. newOffset =
de370 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 lseek(id->h, of
de380 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b fset, SEEK_SET);
de390 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 . if( newOffset
de3a0 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 !=offset ){.
de3b0 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d if( newOffset ==
de3c0 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 -1 ){. ((u
de3d0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 nixFile*)id)->la
de3e0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
de3f0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
de400 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
de410 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 )->lastErrno = 0
de420 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 ;.... }. r
de430 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 eturn -1;. }.
de440 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e got = write(id->
de450 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 h, pBuf, cnt);.#
de460 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e endif. TIMER_EN
de470 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29 D;. if( got<0 )
de480 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 {. ((unixFile
de490 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f *)id)->lastErrno
de4a0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 = errno;. }..
de4b0 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45 OSTRACE5("WRITE
de4c0 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c %-3d %5d %7ll
de4d0 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 d %llu\n", id->h
de4e0 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 , got, offset, T
de4f0 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 IMER_ELAPSED);.
de500 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a return got;.}..
de510 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 ./*.** Write dat
de520 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 a from a buffer
de530 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 into a file. Re
de540 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
de550 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 n success.** or
de560 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 some other error
de570 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 code on failure
de580 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
de590 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c unixWrite(. sql
de5a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a ite3_file *id, .
de5b0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 const void *pB
de5c0 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a uf, . int amt,.
de5d0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
de5e0 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69 offset .){. uni
de5f0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 xFile *pFile = (
de600 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 unixFile*)id;.
de610 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 int wrote = 0;.
de620 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 assert( id );.
de630 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 assert( amt>0 )
de640 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 ;.. /* If this
de650 69 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69 is a database fi
de660 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 le (not a journa
de670 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 l, master-journa
de680 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 l or temp. ** f
de690 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20 ile), the bytes
de6a0 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 in the locking r
de6b0 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 ange should neve
de6c0 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 r be read or wri
de6d0 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 tten. */. asser
de6e0 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 t( pFile->pUnuse
de6f0 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f d==0. || o
de700 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 ffset>=PENDING_B
de710 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c YTE+512. |
de720 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 | offset+amt<=PE
de730 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b NDING_BYTE . );
de740 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 ..#ifndef NDEBUG
de750 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 . /* If we are
de760 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 doing a normal w
de770 72 69 74 65 20 74 6f 20 61 20 64 61 74 61 62 61 rite to a databa
de780 73 65 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f se file (as oppo
de790 73 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e sed to. ** doin
de7a0 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 g a hot-journal
de7b0 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 rollback or a wr
de7c0 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 ite to some file
de7d0 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 other than a.
de7e0 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 ** normal databa
de7f0 73 65 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65 se file) then re
de800 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 cord the fact th
de810 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 0a at the database.
de820 20 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64 ** has changed
de830 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 . If the transa
de840 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 ction counter is
de850 20 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72 modified, recor
de860 64 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 d that. ** fact
de870 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 too.. */. if(
de880 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c pFile->inNormal
de890 57 72 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69 Write ){. pFi
de8a0 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 31 le->dbUpdate = 1
de8b0 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 ; /* The databa
de8c0 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 se has been modi
de8d0 66 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 fied */. if(
de8e0 6f 66 66 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 offset<=24 && of
de8f0 66 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a fset+amt>=27 ){.
de900 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 int rc;.
de910 20 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 char oldCntr
de920 5b 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c [4];. Simul
de930 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e ateIOErrorBenign
de940 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 (1);. rc =
de950 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c seekAndRead(pFil
de960 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 e, 24, oldCntr,
de970 34 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 4);. Simula
de980 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 teIOErrorBenign(
de990 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 0);. if( rc
de9a0 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c !=4 || memcmp(ol
de9b0 64 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29 dCntr, &((char*)
de9c0 70 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d pBuf)[24-offset]
de9d0 2c 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 , 4)!=0 ){.
de9e0 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 pFile->transC
de9f0 6e 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a ntrChng = 1; /*
dea00 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e The transaction
dea10 20 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61 counter has cha
dea20 6e 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a nged */. }.
dea30 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
dea40 0a 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 .. while( amt>0
dea50 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 && (wrote = see
dea60 6b 41 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c kAndWrite(pFile,
dea70 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 offset, pBuf, a
dea80 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d mt))>0 ){. am
dea90 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 t -= wrote;.
deaa0 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b offset += wrote;
deab0 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 . pBuf = &((c
deac0 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 har*)pBuf)[wrote
dead0 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 ];. }. Simulat
deae0 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65 eIOError(( wrote
deaf0 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b =(-1), amt=1 ));
deb00 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 . SimulateDiskf
deb10 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65 ullError(( wrote
deb20 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 =0, amt=1 ));.
deb30 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 if( amt>0 ){.
deb40 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a if( wrote<0 ){.
deb50 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 /* lastErr
deb60 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e no set by seekAn
deb70 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 dWrite */.
deb80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
deb90 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d ERR_WRITE;. }
deba0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c else{. pFil
debb0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 e->lastErrno = 0
debc0 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 ; /* not a syste
debd0 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 m error */.
debe0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 return SQLITE_F
debf0 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ULL;. }. }.
dec00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
dec10 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c K;.}..#ifdef SQL
dec20 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 ITE_TEST./*.** C
dec30 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 ount the number
dec40 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 of fullsyncs and
dec50 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 normal syncs.
dec60 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
dec70 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e test.** that syn
dec80 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 cs and fullsyncs
dec90 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61 are occurring a
deca0 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 t the right time
decb0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 s..*/.SQLITE_API
decc0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e int sqlite3_syn
decd0 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c c_count = 0;.SQL
dece0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
decf0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 te3_fullsync_cou
ded00 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a nt = 0;.#endif..
ded10 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 /*.** We do not
ded20 74 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f trust systems to
ded30 20 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 provide a worki
ded40 6e 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20 ng fdatasync().
ded50 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 Some do..** Oth
ded60 65 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 ers do no. To b
ded70 65 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 e safe, we will
ded80 73 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28 stick with the (
ded90 73 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e slower) fsync().
deda0 0a 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 .** If you know
dedb0 74 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d that your system
dedc0 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64 does support fd
dedd0 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63 atasync() correc
dede0 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d tly,.** then sim
dedf0 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 ply compile with
dee00 20 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61 -Dfdatasync=fda
dee10 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 tasync.*/.#if !d
dee20 65 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63 efined(fdatasync
dee30 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f ) && !defined(__
dee40 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e linux__).# defin
dee50 65 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e e fdatasync fsyn
dee60 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 c.#endif../*.**
dee70 44 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c Define HAVE_FULL
dee80 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 FSYNC to 0 or 1
dee90 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 depending on whe
deea0 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 ther or not.** t
deeb0 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d he F_FULLFSYNC m
deec0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e acro is defined.
deed0 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 F_FULLFSYNC is
deee0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e currently.** on
deef0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 ly available on
def00 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74 Mac OS X. But t
def10 68 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 hat could change
def20 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 ..*/.#ifdef F_FU
def30 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 LLFSYNC.# define
def40 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 HAVE_FULLFSYNC
def50 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 1.#else.# define
def60 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 HAVE_FULLFSYNC
def70 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 0.#endif.../*.**
def80 20 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 The fsync() sys
def90 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f tem call does no
defa0 74 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 t work as advert
defb0 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 ised on many.**
defc0 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54 unix systems. T
defd0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f he following pro
defe0 63 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74 cedure is an att
deff0 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 empt to make.**
df000 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a it work better..
df010 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 **.** The SQLITE
df020 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 _NO_SYNC macro d
df030 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e isables all fsyn
df040 63 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75 c()s. This is u
df050 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 seful.** for tes
df060 74 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e ting when we wan
df070 74 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 t to run through
df080 20 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20 the test suite
df090 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 quickly..** You
df0a0 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 are strongly adv
df0b0 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 ised *not* to de
df0c0 70 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45 ploy with SQLITE
df0d0 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 _NO_SYNC.** enab
df0e0 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 led, however, si
df0f0 6e 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f nce with SQLITE_
df100 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c NO_SYNC enabled,
df110 20 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 an OS crash.**
df120 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 or power failure
df130 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 will likely cor
df140 72 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73 rupt the databas
df150 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 e file..**.** SQ
df160 4c 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61 Lite sets the da
df170 74 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 taOnly flag if t
df180 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 he size of the f
df190 69 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 ile is unchanged
df1a0 2e 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65 ..** The idea be
df1b0 68 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 hind dataOnly is
df1c0 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 that it should
df1d0 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66 only write the f
df1e0 69 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 ile content.** t
df1f0 6f 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 o disk, not the
df200 69 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 inode. We only
df210 73 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 set dataOnly if
df220 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 the file size is
df230 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 .** unchanged s
df240 69 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69 ince the file si
df250 7a 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 ze is part of th
df260 65 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 e inode. Howeve
df270 72 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 r, .** Ted Ts'o
df280 74 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64 tells us that fd
df290 61 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 atasync() will a
df2a0 6c 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e lso write the in
df2b0 6f 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 ode if the.** fi
df2c0 6c 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e le size has chan
df2d0 67 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 ged. The only r
df2e0 65 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 eal difference b
df2f0 65 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 etween fdatasync
df300 28 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 ().** and fsync(
df310 29 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c ), Ted tells us,
df320 20 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 is that fdatasy
df330 6e 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c nc() will not fl
df340 75 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 ush the.** inode
df350 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 if the mtime or
df360 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 owner or other
df370 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73 inode attributes
df380 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a have changed..*
df390 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 * We only care a
df3a0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69 bout the file si
df3b0 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 ze, not the othe
df3c0 72 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 r file attribute
df3d0 73 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 s, so.** as far
df3e0 61 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e as SQLite is con
df3f0 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 cerned, an fdata
df400 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73 sync() is always
df410 20 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f adequate..** So
df420 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 , we always use
df430 66 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69 fdatasync() if i
df440 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 t is available,
df450 72 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a regardless of.**
df460 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
df470 65 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e e dataOnly flag.
df480 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
df490 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 ull_fsync(int fd
df4a0 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 , int fullSync,
df4b0 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 int dataOnly){.
df4c0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 int rc;.. /* T
df4d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 he following "if
df4e0 64 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 def/elif/else/"
df4f0 62 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61 block has the sa
df500 6d 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a me structure as.
df510 20 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c ** the one bel
df520 6f 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63 ow. It is replic
df530 61 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 ated here solely
df540 20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65 to avoid clutte
df550 72 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 ring . ** up th
df560 65 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68 e real code with
df570 20 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41 the UNUSED_PARA
df580 4d 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a METER() macros..
df590 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
df5a0 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 TE_NO_SYNC. UNU
df5b0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 SED_PARAMETER(fd
df5c0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 );. UNUSED_PARA
df5d0 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b METER(fullSync);
df5e0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
df5f0 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 TER(dataOnly);.#
df600 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 elif HAVE_FULLFS
df610 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 YNC. UNUSED_PAR
df620 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 AMETER(dataOnly)
df630 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 ;.#else. UNUSED
df640 5f 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 _PARAMETER(fullS
df650 79 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 ync);. UNUSED_P
df660 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c ARAMETER(dataOnl
df670 79 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a y);.#endif.. /*
df680 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 Record the numb
df690 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 er of times that
df6a0 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 we do a normal
df6b0 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a fsync() and . *
df6c0 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 * FULLSYNC. Thi
df6d0 73 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 s is used during
df6e0 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 testing to veri
df6f0 66 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f fy that this pro
df700 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 cedure. ** gets
df710 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 called with the
df720 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e correct argumen
df730 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 ts.. */.#ifdef
df740 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 SQLITE_TEST. if
df750 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c ( fullSync ) sql
df760 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f ite3_fullsync_co
df770 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 unt++;. sqlite3
df780 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 _sync_count++;.#
df790 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 endif.. /* If w
df7a0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 e compiled with
df7b0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 the SQLITE_NO_SY
df7c0 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 NC flag, then sy
df7d0 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 ncing is a. **
df7e0 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 no-op. */.#ifde
df7f0 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 f SQLITE_NO_SYNC
df800 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f . rc = SQLITE_O
df810 4b 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 K;.#elif HAVE_FU
df820 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 LLFSYNC. if( fu
df830 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 llSync ){. rc
df840 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 = fcntl(fd, F_F
df850 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 ULLFSYNC, 0);.
df860 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
df870 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 1;. }. /* If t
df880 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 he FULLFSYNC fai
df890 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 led, fall back t
df8a0 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 o attempting an
df8b0 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 fsync().. ** It
df8c0 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f shouldn't be po
df8d0 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 ssible for fullf
df8e0 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 sync to fail on
df8f0 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 the local . **
df900 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 file system (on
df910 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 OSX), so failure
df920 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 indicates that
df930 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 FULLFSYNC. ** i
df940 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 sn't supported f
df950 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 or this file sys
df960 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 tem. So, attempt
df970 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 an fsync . **
df980 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 and (for now) ig
df990 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 nore the overhea
df9a0 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f d of a superfluo
df9b0 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 us fcntl call.
df9c0 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 . ** It'd be be
df9d0 74 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 tter to detect f
df9e0 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 ullfsync support
df9f0 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 once and avoid
dfa00 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 . ** the fcntl
dfa10 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 call every time
dfa20 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a sync is called..
dfa30 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 */. if( rc )
dfa40 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a rc = fsync(fd);.
dfa50 0a 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 .#else . rc = f
dfa60 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 datasync(fd);.#i
dfa70 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 f OS_VXWORKS. i
dfa80 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 f( rc==-1 && err
dfa90 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 no==ENOTSUP ){.
dfaa0 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 rc = fsync(fd
dfab0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a );. }.#endif /*
dfac0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 OS_VXWORKS */.#
dfad0 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 endif /* ifdef S
dfae0 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c QLITE_NO_SYNC el
dfaf0 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e if HAVE_FULLFSYN
dfb00 43 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 C */.. if( OS_V
dfb10 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d XWORKS && rc!= -
dfb20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 1 ){. rc = 0;
dfb30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
dfb40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 ;.}../*.** Make
dfb50 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 sure all writes
dfb60 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 to a particular
dfb70 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 file are committ
dfb80 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a ed to disk..**.*
dfb90 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 * If dataOnly==0
dfba0 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 then both the f
dfbb0 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 ile itself and i
dfbc0 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c ts metadata (fil
dfbd0 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 e.** size, acces
dfbe0 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 s time, etc) are
dfbf0 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 synced. If dat
dfc00 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e aOnly!=0 then on
dfc10 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 ly the.** file d
dfc20 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a ata is synced..*
dfc30 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c *.** Under Unix,
dfc40 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 also make sure
dfc50 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f that the directo
dfc60 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 ry entry for the
dfc70 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 file.** has bee
dfc80 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 n created by fsy
dfc90 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 nc-ing the direc
dfca0 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 tory that contai
dfcb0 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 ns the file..**
dfcc0 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 If we do not do
dfcd0 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f this and we enco
dfce0 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 unter a power fa
dfcf0 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 ilure, the direc
dfd00 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f tory.** entry fo
dfd10 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 r the journal mi
dfd20 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 ght not exist af
dfd30 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 ter we reboot.
dfd40 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 The next.** SQLi
dfd50 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 te to access the
dfd60 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b file will not k
dfd70 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 now that the jou
dfd80 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 rnal exists (bec
dfd90 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 ause.** the dire
dfda0 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 ctory entry for
dfdb0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 the journal was
dfdc0 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 never created) a
dfdd0 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 nd the transacti
dfde0 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 on.** will not r
dfdf0 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 oll back - possi
dfe00 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 bly leading to d
dfe10 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 atabase corrupti
dfe20 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e on..*/.static in
dfe30 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 t unixSync(sqlit
dfe40 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 e3_file *id, int
dfe50 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 flags){. int r
dfe60 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 c;. unixFile *p
dfe70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 File = (unixFile
dfe80 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 *)id;.. int isD
dfe90 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 ataOnly = (flags
dfea0 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 &SQLITE_SYNC_DAT
dfeb0 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 AONLY);. int is
dfec0 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 Fullsync = (flag
dfed0 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f s&0x0F)==SQLITE_
dfee0 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a SYNC_FULL;.. /*
dfef0 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 Check that one
dff00 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e of SQLITE_SYNC_N
dff10 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 ORMAL or FULL wa
dff20 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 s passed */. as
dff30 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 sert((flags&0x0F
dff40 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e )==SQLITE_SYNC_N
dff50 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 ORMAL. || (
dff60 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c flags&0x0F)==SQL
dff70 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 ITE_SYNC_FULL.
dff80 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 );.. /* Unix ca
dff90 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 nnot, but some s
dffa0 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 ystems may retur
dffb0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 n SQLITE_FULL fr
dffc0 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 om here. This.
dffd0 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 ** line is to te
dffe0 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f st that doing so
dfff0 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 does not cause
e0000 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 any problems..
e0010 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 */. SimulateDis
e0020 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 kfullError( retu
e0030 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 rn SQLITE_FULL )
e0040 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 ;.. assert( pFi
e0050 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 le );. OSTRACE2
e0060 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e ("SYNC %-3d\n
e0070 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 ", pFile->h);.
e0080 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 rc = full_fsync(
e0090 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c pFile->h, isFull
e00a0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 sync, isDataOnly
e00b0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 );. SimulateIOE
e00c0 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 rror( rc=1 );.
e00d0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 if( rc ){. pF
e00e0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
e00f0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 errno;. retu
e0100 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e0110 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 FSYNC;. }. if(
e0120 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 pFile->dirfd>=0
e0130 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b ){. int err;
e0140 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44 . OSTRACE4("D
e0150 49 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76 IRSYNC %-3d (hav
e0160 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 e_fullfsync=%d f
e0170 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 ullsync=%d)\n",
e0180 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 pFile->dirfd,.
e0190 20 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 HAVE_F
e01a0 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c ULLFSYNC, isFull
e01b0 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 sync);.#ifndef S
e01c0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 QLITE_DISABLE_DI
e01d0 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 RSYNC. /* The
e01e0 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 directory sync
e01f0 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 is only attempte
e0200 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 d if full_fsync
e0210 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 is. ** turned
e0220 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 off or unavaila
e0230 62 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f ble. If a full_
e0240 66 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61 fsync occurred a
e0250 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 bove,. ** the
e0260 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 n the directory
e0270 73 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75 sync is superflu
e0280 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ous.. */.
e0290 69 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 if( (!HAVE_FULLF
e02a0 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 SYNC || !isFulls
e02b0 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 ync) && full_fsy
e02c0 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c nc(pFile->dirfd,
e02d0 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 0,0) ){. /
e02e0 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68 *. ** We h
e02f0 61 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c ave received mul
e0300 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 tiple reports of
e0310 20 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69 fsync() returni
e0320 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72 ng. ** err
e0330 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64 ors when applied
e0340 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20 to directories
e0350 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20 on certain file
e0360 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 systems..
e0370 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65 ** A failed dire
e0380 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f ctory sync is no
e0390 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53 t a big deal. S
e03a0 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20 o it seems.
e03b0 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69 ** better to i
e03c0 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e gnore the error.
e03d0 20 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20 Ticket #1657.
e03e0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
e03f0 2f 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 /* pFile->lastEr
e0400 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a rno = errno; */.
e0410 20 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e /* return
e0420 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a SQLITE_IOERR; *
e0430 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 /. }.#endif.
e0440 20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 err = close(p
e0450 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a File->dirfd); /*
e0460 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 Only need to sy
e0470 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 nc once, so clos
e0480 65 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 e the */. if(
e0490 20 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 err==0 ){
e04a0 20 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63 /* direc
e04b0 74 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65 tory when we are
e04c0 20 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 done */. p
e04d0 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 File->dirfd = -1
e04e0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
e04f0 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
e0500 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 rno = errno;.
e0510 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
e0520 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a OERR_DIR_CLOSE;.
e0530 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
e0540 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
e0550 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e Truncate an open
e0560 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 file to a speci
e0570 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 fied size.*/.sta
e0580 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e tic int unixTrun
e0590 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c cate(sqlite3_fil
e05a0 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 e *id, i64 nByte
e05b0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 ){. int rc;. a
e05c0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 ssert( id );. S
e05d0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
e05e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
e05f0 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a ERR_TRUNCATE );.
e0600 20 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 rc = ftruncate
e0610 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 (((unixFile*)id)
e0620 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 ->h, (off_t)nByt
e0630 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a e);. if( rc ){.
e0640 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 ((unixFile*)
e0650 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d id)->lastErrno =
e0660 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 errno;. retu
e0670 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
e0680 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 TRUNCATE;. }els
e0690 65 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 e{.#ifndef NDEBU
e06a0 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 G. /* If we a
e06b0 72 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 re doing a norma
e06c0 6c 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74 l write to a dat
e06d0 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 6f abase file (as o
e06e0 70 70 6f 73 65 64 20 74 6f 0a 20 20 20 20 2a 2a pposed to. **
e06f0 20 64 6f 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75 doing a hot-jou
e0700 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 rnal rollback or
e0710 20 61 20 77 72 69 74 65 20 74 6f 20 73 6f 6d 65 a write to some
e0720 20 66 69 6c 65 20 6f 74 68 65 72 20 74 68 61 6e file other than
e0730 20 61 0a 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c a. ** normal
e0740 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20 database file)
e0750 61 6e 64 20 77 65 20 74 72 75 6e 63 61 74 65 20 and we truncate
e0760 74 68 65 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f the file to zero
e0770 20 6c 65 6e 67 74 68 2c 0a 20 20 20 20 2a 2a 20 length,. **
e0780 74 68 61 74 20 65 66 66 65 63 74 69 76 65 6c 79 that effectively
e0790 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68 61 updates the cha
e07a0 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 nge counter. Th
e07b0 69 73 20 6d 69 67 68 74 20 68 61 70 70 65 6e 0a is might happen.
e07c0 20 20 20 20 2a 2a 20 77 68 65 6e 20 72 65 73 74 ** when rest
e07d0 6f 72 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 oring a database
e07e0 20 75 73 69 6e 67 20 74 68 65 20 62 61 63 6b 75 using the backu
e07f0 70 20 41 50 49 20 66 72 6f 6d 20 61 20 7a 65 72 p API from a zer
e0800 6f 2d 6c 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20 o-length. **
e0810 73 6f 75 72 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 source.. */.
e0820 20 20 20 69 66 28 20 28 28 75 6e 69 78 46 69 6c if( ((unixFil
e0830 65 2a 29 69 64 29 2d 3e 69 6e 4e 6f 72 6d 61 6c e*)id)->inNormal
e0840 57 72 69 74 65 20 26 26 20 6e 42 79 74 65 3d 3d Write && nByte==
e0850 30 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 0 ){. ((uni
e0860 78 46 69 6c 65 2a 29 69 64 29 2d 3e 74 72 61 6e xFile*)id)->tran
e0870 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b 0a 20 sCntrChng = 1;.
e0880 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 }.#endif..
e0890 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
e08a0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 K;. }.}../*.**
e08b0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 Determine the cu
e08c0 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 rrent size of a
e08d0 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f file in bytes.*/
e08e0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e08f0 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 FileSize(sqlite3
e0900 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a _file *id, i64 *
e0910 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 pSize){. int rc
e0920 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 ;. struct stat
e0930 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 69 buf;. assert( i
e0940 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 d );. rc = fsta
e0950 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 t(((unixFile*)id
e0960 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 53 )->h, &buf);. S
e0970 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 imulateIOError(
e0980 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63 rc=1 );. if( rc
e0990 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69 !=0 ){. ((uni
e09a0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 xFile*)id)->last
e09b0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 Errno = errno;.
e09c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e09d0 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 _IOERR_FSTAT;.
e09e0 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 66 }. *pSize = buf
e09f0 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 .st_size;.. /*
e0a00 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 7a When opening a z
e0a10 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 73 ero-size databas
e0a20 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b 49 e, the findLockI
e0a30 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 0a nfo() procedure.
e0a40 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 69 ** writes a si
e0a50 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 74 ngle byte into t
e0a60 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 hat file in orde
e0a70 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 r to work around
e0a80 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74 a bug. ** in t
e0a90 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69 he OS-X msdos fi
e0aa0 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72 lesystem. In or
e0ab0 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f der to avoid pro
e0ac0 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 72 blems with upper
e0ad0 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65 . ** layers, we
e0ae0 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 20 need to report
e0af0 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 61 this file size a
e0b00 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f 75 s zero even thou
e0b10 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 65 gh it is. ** re
e0b20 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 74 ally 1. Ticket
e0b30 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69 #3260.. */. i
e0b40 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a f( *pSize==1 ) *
e0b50 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72 pSize = 0;... r
e0b60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e0b70 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 .}..#if SQLITE_E
e0b80 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e0b90 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f YLE && defined(_
e0ba0 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 _APPLE__)./*.**
e0bb0 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f 78 Handler for prox
e0bc0 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63 y-locking file-c
e0bd0 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 44 ontrol verbs. D
e0be0 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20 efined below in
e0bf0 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20 the.** proxying
e0c00 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e locking division
e0c10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e0c20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c proxyFileControl
e0c30 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 (sqlite3_file*,i
e0c40 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 nt,void*);.#endi
e0c50 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d f.../*.** Inform
e0c60 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f ation and contro
e0c70 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c l of an open fil
e0c80 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 e handle..*/.sta
e0c90 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 tic int unixFile
e0ca0 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f Control(sqlite3_
e0cb0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 file *id, int op
e0cc0 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 , void *pArg){.
e0cd0 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 switch( op ){.
e0ce0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 case SQLITE_F
e0cf0 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 CNTL_LOCKSTATE:
e0d00 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 {. *(int*)p
e0d10 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 Arg = ((unixFile
e0d20 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b *)id)->locktype;
e0d30 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e0d40 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 LITE_OK;. }.
e0d50 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c case SQLITE_L
e0d60 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20 AST_ERRNO: {.
e0d70 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d *(int*)pArg =
e0d80 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 ((unixFile*)id)
e0d90 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20 ->lastErrno;.
e0da0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
e0db0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 _OK;. }.#ifnd
e0dc0 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a ef NDEBUG. /*
e0dd0 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c 73 The pager calls
e0de0 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f 20 this method to
e0df0 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 68 signal that it h
e0e00 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 as done. ** a
e0e10 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68 rollback and th
e0e20 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 at the database
e0e30 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e 63 is therefore unc
e0e40 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 2a hanged and. *
e0e50 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 73 * it hence it is
e0e60 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 6e OK for the tran
e0e70 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 63 saction change c
e0e80 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 20 ounter to be.
e0e90 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 ** unchanged..
e0ea0 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 */. case S
e0eb0 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 QLITE_FCNTL_DB_U
e0ec0 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20 NCHANGED: {.
e0ed0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 ((unixFile*)id
e0ee0 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b )->dbUpdate = 0;
e0ef0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
e0f00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 LITE_OK;. }.#
e0f10 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 endif.#if SQLITE
e0f20 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
e0f30 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 STYLE && defined
e0f40 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 (__APPLE__).
e0f50 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f case SQLITE_SET_
e0f60 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a 20 LOCKPROXYFILE:.
e0f70 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47 case SQLITE_G
e0f80 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
e0f90 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
e0fa0 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f proxyFileContro
e0fb0 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a 20 l(id,op,pArg);.
e0fc0 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.#endif /* S
e0fd0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 QLITE_ENABLE_LOC
e0fe0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 KING_STYLE && de
e0ff0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 fined(__APPLE__)
e1000 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e */. }. return
e1010 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d SQLITE_ERROR;.}
e1020 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
e1030 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 he sector size i
e1040 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 n bytes of the u
e1050 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 nderlying block
e1060 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 device for.** th
e1070 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 e specified file
e1080 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 . This is almost
e1090 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 always 512 byte
e10a0 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a s, but may be.**
e10b0 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 larger for some
e10c0 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 devices..**.**
e10d0 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 SQLite code assu
e10e0 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f mes this functio
e10f0 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 n cannot fail. I
e1100 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 t also assumes t
e1110 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 hat.** if two fi
e1120 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 les are created
e1130 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 in the same file
e1140 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 -system director
e1150 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 y (i.e..** a dat
e1160 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f abase and its jo
e1170 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 urnal file) that
e1180 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
e1190 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 will be the.**
e11a0 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a same for both..*
e11b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 /.static int uni
e11c0 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 xSectorSize(sqli
e11d0 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 te3_file *NotUse
e11e0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
e11f0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e1200 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
e1210 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f _DEFAULT_SECTOR_
e1220 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 SIZE;.}../*.** R
e1230 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 65 eturn the device
e1240 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 characteristics
e1250 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 54 for the file. T
e1260 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 20 his is always 0
e1270 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61 for unix..*/.sta
e1280 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 69 tic int unixDevi
e1290 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
e12a0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a s(sqlite3_file *
e12b0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 NotUsed){. UNUS
e12c0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 ED_PARAMETER(Not
e12d0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 Used);. return
e12e0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65 0;.}../*.** Here
e12f0 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 6d ends the implem
e1300 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 entation of all
e1310 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74 sqlite3_file met
e1320 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a hods..**.*******
e1330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
e1340 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 End sqlite3_file
e1350 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a Methods *******
e1360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1370 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
e1380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e1390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e13a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e13b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e13c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 *******/../*.**
e13d0 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f This division co
e13e0 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f ntains definitio
e13f0 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f ns of sqlite3_io
e1400 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 _methods objects
e1410 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 that.** impleme
e1420 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 20 nt various file
e1430 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 69 locking strategi
e1440 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f 6e es. It also con
e1450 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e tains definition
e1460 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 22 s.** of "finder"
e1470 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 66 functions. A f
e1480 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 inder-function i
e1490 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 s used to locate
e14a0 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 the appropriate
e14b0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d .** sqlite3_io_m
e14c0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 6f ethods object fo
e14d0 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 r a particular d
e14e0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 atabase file. T
e14f0 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 66 he pAppData.** f
e1500 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69 ield of the sqli
e1510 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a 65 te3_vfs VFS obje
e1520 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69 cts are initiali
e1530 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 65 zed to be pointe
e1540 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f 72 rs to.** the cor
e1550 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e 63 rect finder-func
e1560 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 46 tion for that VF
e1570 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69 S..**.** Most fi
e1580 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72 nder functions r
e1590 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
e15a0 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 to a fixed sqlit
e15b0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a e3_io_methods.**
e15c0 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f 6e object. The on
e15d0 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 66 ly interesting f
e15e0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69 inder-function i
e15f0 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 s autolockIoFind
e1600 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f er, which.** loo
e1610 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 79 ks at the filesy
e1620 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 72 stem type and tr
e1630 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 65 ies to guess the
e1640 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a best locking.**
e1650 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 74 strategy from t
e1660 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 hat..**.** For f
e1670 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c inder-funtion F,
e1680 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 65 two objects are
e1690 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 created:.**.**
e16a0 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c 20 (1) The real
e16b0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 finder-function
e16c0 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 2e named "FImpt()".
e16d0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 20 .**.** (2) A
e16e0 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 72 constant pointer
e16f0 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
e1700 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 22 n named just "F"
e1710 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 ..**.**.** A poi
e1720 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 6f nter to the F po
e1730 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
e1740 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 61 the pAppData va
e1750 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 6f lue for VFS.** o
e1760 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 65 bjects. We have
e1770 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 74 to do this inst
e1780 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 70 ead of letting p
e1790 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a 2a AppData point.**
e17a0 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 65 directly at the
e17b0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
e17c0 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 73 since C90 rules
e17d0 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 2a prevent a void*
e17e0 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 74 .** from be cast
e17f0 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e into a function
e1800 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a pointer..**.**.
e1810 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 ** Each instance
e1820 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 67 of this macro g
e1830 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 6a enerates two obj
e1840 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 ects:.**.** *
e1850 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69 A constant sqli
e1860 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f te3_io_methods o
e1870 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f bject call METHO
e1880 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 69 D that has locki
e1890 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f ng.** metho
e18a0 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 ds CLOSE, LOCK,
e18b0 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43 UNLOCK, CKRESLOC
e18c0 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e K..**.** * An
e18d0 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 I/O method find
e18e0 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c er function call
e18f0 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72 ed FINDER that r
e1900 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
e1910 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 .** to the
e1920 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 6e METHOD object in
e1930 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 75 the previous bu
e1940 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 llet..*/.#define
e1950 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 45 IOMETHODS(FINDE
e1960 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 45 R, METHOD, CLOSE
e1970 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 , LOCK, UNLOCK,
e1980 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 20 CKLOCK)
e1990 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63 \.static c
e19a0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f onst sqlite3_io_
e19b0 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 3d methods METHOD =
e19c0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
e19d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e19e0 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 20 \. 1,
e19f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a00 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f /* iVersio
e1a10 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 n */
e1a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a30 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c 20 \. CLOSE,
e1a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a50 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a /* xClose *
e1a60 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e1a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1a80 20 20 20 5c 0a 20 20 20 75 6e 69 78 52 65 61 64 \. unixRead
e1a90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1aa0 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20 /* xRead */
e1ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ad0 20 20 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 65 \. unixWrite
e1ae0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1af0 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 20 /* xWrite */
e1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 61 \. unixTrunca
e1b30 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 te,
e1b40 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a /* xTruncate *
e1b50 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
e1b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b70 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20 \. unixSync,
e1b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1b90 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20 /* xSync */
e1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
e1bc0 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 . unixFileSize
e1bd0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e1be0 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20 /* xFileSize */
e1bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a \.
e1c10 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 LOCK,
e1c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
e1c30 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 * xLock */
e1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
e1c60 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20 UNLOCK,
e1c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e1c80 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20 xUnlock */
e1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ca0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
e1cb0 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20 CKLOCK,
e1cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e1cd0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e1ce0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 ck */
e1cf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
e1d00 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c unixFileControl,
e1d10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e1d20 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20 FileControl */
e1d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d40 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 \. u
e1d50 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20 nixSectorSize,
e1d60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 /* xS
e1d70 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20 ectorSize */
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1d90 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e \. un
e1da0 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 ixDeviceCharacte
e1db0 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 65 ristics /* xDe
e1dc0 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73 viceCapabilities
e1dd0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 */
e1de0 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 20 \.};
e1df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1e30 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 \.static
e1e40 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e1e50 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 52 _methods *FINDER
e1e60 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 ##Impl(const cha
e1e70 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20 2a r *z, unixFile *
e1e80 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45 44 p){ \. UNUSED
e1e90 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 55 _PARAMETER(z); U
e1ea0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
e1eb0 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 p);
e1ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1ed0 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e 20 \. return
e1ee0 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 20 &METHOD;
e1ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 20 \.}
e1f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e1f70 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73 \.static cons
e1f80 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e1f90 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 49 hods *(*const FI
e1fa0 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 72 NDER)(const char
e1fb0 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20 20 *,unixFile *p)
e1fc0 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 52 \. = FINDER
e1fd0 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48 ##Impl;../*.** H
e1fe0 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 74 ere are all of t
e1ff0 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 he sqlite3_io_me
e2000 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f thods objects fo
e2010 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a r each of the.**
e2020 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 locking strateg
e2030 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 20 ies. Functions
e2040 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 6e that return poin
e2050 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d 65 ters to these me
e2060 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c 73 thods.** are als
e2070 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f o created..*/.IO
e2080 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 78 METHODS(. posix
e2090 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e20a0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
e20b0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
e20c0 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 posixIoMethods
e20d0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 , /* s
e20e0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e20f0 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
e2100 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20 . unixClose,
e2110 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2120 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
e2130 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20 . unixLock,
e2140 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2150 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
e2160 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20 unixUnlock,
e2170 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2180 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
e2190 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 . unixCheckRese
e21a0 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a 20 rvedLock /*
e21b0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e21c0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49 ck method */.).I
e21d0 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f OMETHODS(. nolo
e21e0 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 ckIoFinder,
e21f0 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 /* Finder
e2200 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
e2210 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f . nolockIoMetho
e2220 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ds, /*
e2230 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
e2240 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a ds object name *
e2250 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c /. nolockClose,
e2260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e2270 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a xClose method *
e2280 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20 /. nolockLock,
e2290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e22a0 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f xLock method */
e22b0 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c . nolockUnlock,
e22c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e22d0 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a xUnlock method *
e22e0 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 /. nolockCheckR
e22f0 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a eservedLock /*
e2300 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
e2310 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a ock method */.).
e2320 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74 IOMETHODS(. dot
e2330 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 lockIoFinder,
e2340 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 /* Finder
e2350 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a function name *
e2360 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 /. dotlockIoMet
e2370 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a hods, /*
e2380 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e2390 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 ods object name
e23a0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 */. dotlockClos
e23b0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f e, /
e23c0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 * xClose method
e23d0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b */. dotlockLock
e23e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e23f0 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a * xLock method *
e2400 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 /. dotlockUnloc
e2410 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a k, /*
e2420 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 xUnlock method
e2430 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 63 */. dotlockChec
e2440 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 2f kReservedLock /
e2450 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 * xCheckReserved
e2460 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 Lock method */.)
e2470 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 ..#if SQLITE_ENA
e2480 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
e2490 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 E && !OS_VXWORKS
e24a0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c .IOMETHODS(. fl
e24b0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 ockIoFinder,
e24c0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 /* Finde
e24d0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 r function name
e24e0 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68 */. flockIoMeth
e24f0 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f ods, /
e2500 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 * sqlite3_io_met
e2510 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 hods object name
e2520 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65 */. flockClose
e2530 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
e2540 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 /* xClose method
e2550 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c */. flockLock,
e2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e2570 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 /* xLock method
e2580 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b */. flockUnlock
e2590 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e25a0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 * xUnlock method
e25b0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b */. flockCheck
e25c0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 ReservedLock
e25d0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 /* xCheckReserve
e25e0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a dLock method */.
e25f0 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53 ).#endif..#if OS
e2600 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f _VXWORKS.IOMETHO
e2610 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65 DS(. semIoFinde
e2620 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r,
e2630 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 /* Finder functi
e2640 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d on name */. sem
e2650 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 IoMethods,
e2660 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 /* sqlite
e2670 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 3_io_methods obj
e2680 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 ect name */. se
e2690 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 mClose,
e26a0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 /* xClos
e26b0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 e method */. se
e26c0 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 mLock,
e26d0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b /* xLock
e26e0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d method */. sem
e26f0 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 Unlock,
e2700 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 /* xUnloc
e2710 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 k method */. se
e2720 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f mCheckReservedLo
e2730 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 ck /* xChec
e2740 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 kReservedLock me
e2750 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 thod */.).#endif
e2760 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f ..#if defined(__
e2770 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 APPLE__) && SQLI
e2780 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
e2790 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44 G_STYLE.IOMETHOD
e27a0 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72 S(. afpIoFinder
e27b0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
e27c0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f * Finder functio
e27d0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49 n name */. afpI
e27e0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 oMethods,
e27f0 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 /* sqlite3
e2800 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 _io_methods obje
e2810 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 ct name */. afp
e2820 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 Close,
e2830 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 /* xClose
e2840 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
e2850 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 Lock,
e2860 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 /* xLock
e2870 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55 method */. afpU
e2880 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 nlock,
e2890 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b /* xUnlock
e28a0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 method */. afp
e28b0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 CheckReservedLoc
e28c0 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b k /* xCheck
e28d0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 ReservedLock met
e28e0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a hod */.).#endif.
e28f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f 6c ./*.** The "Whol
e2900 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22 20 e File Locking"
e2910 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20 74 finder returns t
e2920 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 6d he same set of m
e2930 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68 65 ethods as.** the
e2940 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20 66 posix locking f
e2950 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20 61 inder. But it a
e2960 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51 4c lso sets the SQL
e2970 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c ITE_WHOLE_FILE_L
e2980 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20 74 OCKING.** flag t
e2990 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73 69 o force the posi
e29a0 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 x advisory locks
e29b0 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77 68 to cover the wh
e29c0 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64 ole file instead
e29d0 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73 6d .** of just a sm
e29e0 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74 65 all span of byte
e29f0 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42 20 s near the 1GiB
e2a00 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c 65 boundary. Whole
e2a10 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a 2a File Locking.**
e2a20 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e 46 is useful on NF
e2a30 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73 20 S-mounted files
e2a40 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20 4e since it helps N
e2a50 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 63 FS to maintain c
e2a60 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e 63 ache.** coherenc
e2a70 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61 20 y. But it is a
e2a80 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74 68 detriment to oth
e2a90 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20 73 er filesystems s
e2aa0 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a 20 ince it runs.**
e2ab0 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 slower..*/.stati
e2ac0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
e2ad0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73 69 io_methods *posi
e2ae0 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c xWflIoFinderImpl
e2af0 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20 75 (const char*z, u
e2b00 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55 4e nixFile*p){. UN
e2b10 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a USED_PARAMETER(z
e2b20 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61 67 );. p->fileFlag
e2b30 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 s = SQLITE_WHOLE
e2b40 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 _FILE_LOCKING;.
e2b50 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f return &posixIo
e2b60 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74 69 Methods;.}.stati
e2b70 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f c const sqlite3_
e2b80 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 io_methods . *(
e2b90 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c 49 *const posixWflI
e2ba0 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 oFinder)(const c
e2bb0 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 har*,unixFile *p
e2bc0 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 ) = posixWflIoFi
e2bd0 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a nderImpl;../*.**
e2be0 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 The proxy locki
e2bf0 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20 22 ng method is a "
e2c00 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69 6e super-method" in
e2c10 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74 20 the sense that
e2c20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 6f it.** opens seco
e2c30 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63 72 ndary file descr
e2c40 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63 iptors for the c
e2c50 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 69 onch and lock fi
e2c60 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75 73 les and.** it us
e2c70 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 69 es proxy, dot-fi
e2c80 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c 6f le, AFP, and flo
e2c90 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 ck() locking met
e2ca0 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a 2a hods on those.**
e2cb0 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 73 secondary files
e2cc0 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 . For this reas
e2cd0 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e on, the division
e2ce0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 that implements
e2cf0 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e .** proxy lockin
e2d00 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75 63 g is located muc
e2d10 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69 h further down i
e2d20 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75 74 n the file. But
e2d30 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 67 we need.** to g
e2d40 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66 69 o ahead and defi
e2d50 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 ne the sqlite3_i
e2d60 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66 69 o_methods and fi
e2d70 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a nder function.**
e2d80 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 for proxy locki
e2d90 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20 ng here. So we
e2da0 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65 20 forward declare
e2db0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 2e the I/O methods.
e2dc0 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .*/.#if defined(
e2dd0 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 __APPLE__) && SQ
e2de0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b LITE_ENABLE_LOCK
e2df0 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 63 ING_STYLE.static
e2e00 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 int proxyClose(
e2e10 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a sqlite3_file*);.
e2e20 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 static int proxy
e2e30 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c Lock(sqlite3_fil
e2e40 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 e*, int);.static
e2e50 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b int proxyUnlock
e2e60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 (sqlite3_file*,
e2e70 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 int);.static int
e2e80 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72 proxyCheckReser
e2e90 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f vedLock(sqlite3_
e2ea0 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49 4f file*, int*);.IO
e2eb0 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78 79 METHODS(. proxy
e2ec0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 IoFinder,
e2ed0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 /* Finder f
e2ee0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a unction name */.
e2ef0 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 proxyIoMethods
e2f00 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 , /* s
e2f10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e2f20 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f s object name */
e2f30 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 20 . proxyClose,
e2f40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2f50 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f xClose method */
e2f60 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 20 . proxyLock,
e2f70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e2f80 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a xLock method */.
e2f90 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 20 proxyUnlock,
e2fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
e2fb0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f Unlock method */
e2fc0 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 . proxyCheckRes
e2fd0 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20 ervedLock /*
e2fe0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f xCheckReservedLo
e2ff0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 ck method */.).#
e3000 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66 69 endif...#if defi
e3010 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 ned(__APPLE__) &
e3020 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f & SQLITE_ENABLE_
e3030 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a LOCKING_STYLE./*
e3040 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 65 .** This "finde
e3050 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 r" function atte
e3060 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e mpts to determin
e3070 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 e the best locki
e3080 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a 20 ng strategy .**
e3090 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 for the database
e30a0 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 22 file "filePath"
e30b0 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 72 . It then retur
e30c0 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 ns the sqlite3_i
e30d0 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a o_methods.** obj
e30e0 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 ect that impleme
e30f0 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 67 nts that strateg
e3100 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 y..**.** This is
e3110 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c 79 for MacOSX only
e3120 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 ..*/.static cons
e3130 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e3140 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f hods *autolockIo
e3150 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 6f FinderImpl(. co
e3160 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61 nst char *filePa
e3170 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 6f th, /* name o
e3180 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
e3190 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c ile */. unixFil
e31a0 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20 e *pNew
e31b0 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 6f /* open file o
e31c0 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 61 bject for the da
e31d0 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29 tabase file */.)
e31e0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 {. static const
e31f0 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 20 struct Mapping
e3200 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 {. const char
e3210 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 20 *zFilesystem;
e3220 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
e3230 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 6e ilesystem type n
e3240 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 ame */. const
e3250 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 sqlite3_io_meth
e3260 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20 ods *pMethods;
e3270 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 20 /* Appropriate
e3280 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 2a locking method *
e3290 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b /. } aMap[] = {
e32a0 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 20 . { "hfs",
e32b0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 &posixIoMethods
e32c0 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 2c },. { "ufs",
e32d0 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 &posixIoMeth
e32e0 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66 ods },. { "af
e32f0 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74 pfs", &afpIoMet
e3300 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20 53 hods },.#ifdef S
e3310 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50 QLITE_ENABLE_AFP
e3320 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20 _LOCKING_SMB.
e3330 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61 66 { "smbfs", &af
e3340 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 pIoMethods },.#e
e3350 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 lse. { "smbfs
e3360 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 68 ", &flockIoMeth
e3370 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 ods },.#endif.
e3380 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26 6e { "webdav", &n
e3390 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d olockIoMethods }
e33a0 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20 ,. { 0, 0 }.
e33b0 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 };. int i;. s
e33c0 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49 truct statfs fsI
e33d0 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 6c nfo;. struct fl
e33e0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 ock lockInfo;..
e33f0 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 if( !filePath )
e3400 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 {. /* If file
e3410 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 Path==NULL that
e3420 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 means we are dea
e3430 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e ling with a tran
e3440 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a sient file. *
e3450 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 * that does not
e3460 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 need to be locke
e3470 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e d. */. return
e3480 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 &nolockIoMethod
e3490 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 61 s;. }. if( sta
e34a0 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26 66 tfs(filePath, &f
e34b0 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a sInfo) != -1 ){.
e34c0 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e 66 if( fsInfo.f
e34d0 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f _flags & MNT_RDO
e34e0 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65 74 NLY ){. ret
e34f0 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 urn &nolockIoMet
e3500 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 hods;. }.
e3510 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d for(i=0; aMap[i]
e3520 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b .zFilesystem; i+
e3530 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 +){. if( st
e3540 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73 rcmp(fsInfo.f_fs
e3550 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69 typename, aMap[i
e3560 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d ].zFilesystem)==
e3570 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 0 ){. ret
e3580 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74 urn aMap[i].pMet
e3590 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20 hods;. }.
e35a0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 }. }.. /* De
e35b0 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e 64 fault case. Hand
e35c0 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68 les, amongst oth
e35d0 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a ers, "nfs".. **
e35e0 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65 Test byte-range
e35f0 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 lock using fcnt
e3600 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c l(). If the call
e3610 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a succeeds, . **
e3620 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 assume that the
e3630 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 file-system sup
e3640 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c ports POSIX styl
e3650 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 e locks. . */.
e3660 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 lockInfo.l_len
e3670 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e = 1;. lockInfo.
e3680 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c l_start = 0;. l
e3690 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 ockInfo.l_whence
e36a0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c = SEEK_SET;. l
e36b0 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d ockInfo.l_type =
e36c0 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 F_RDLCK;. if(
e36d0 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46 fcntl(pNew->h, F
e36e0 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66 _GETLK, &lockInf
e36f0 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 70 o)!=-1 ) {. p
e3700 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d New->fileFlags =
e3710 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 SQLITE_WHOLE_FI
e3720 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20 20 LE_LOCKING;.
e3730 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d return &posixIoM
e3740 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b ethods;. }else{
e3750 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74 . return &dot
e3760 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 lockIoMethods;.
e3770 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 }.}.static cons
e3780 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e3790 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 hods . *(*const
e37a0 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 autolockIoFinde
e37b0 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 r)(const char*,u
e37c0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f nixFile*) = auto
e37d0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c lockIoFinderImpl
e37e0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 ;..#endif /* def
e37f0 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 ined(__APPLE__)
e3800 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
e3810 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a _LOCKING_STYLE *
e3820 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b /..#if OS_VXWORK
e3830 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 S && SQLITE_ENAB
e3840 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e3850 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 ./* .** This "fi
e3860 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 nder" function a
e3870 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 ttempts to deter
e3880 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f mine the best lo
e3890 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a cking strategy .
e38a0 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 ** for the datab
e38b0 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61 ase file "filePa
e38c0 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65 th". It then re
e38d0 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65 turns the sqlite
e38e0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 3_io_methods.**
e38f0 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c object that impl
e3900 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61 ements that stra
e3910 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 tegy..**.** This
e3920 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 20 is for VXWorks
e3930 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 only..*/.static
e3940 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f const sqlite3_io
e3950 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f _methods *autolo
e3960 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a ckIoFinderImpl(.
e3970 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 const char *fi
e3980 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 lePath, /* na
e3990 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
e39a0 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 se file */. uni
e39b0 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 xFile *pNew
e39c0 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 65 /* the ope
e39d0 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a 2f n file object */
e39e0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f .){. struct flo
e39f0 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 ck lockInfo;..
e3a00 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b if( !filePath ){
e3a10 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50 . /* If fileP
e3a20 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d ath==NULL that m
e3a30 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c eans we are deal
e3a40 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73 ing with a trans
e3a50 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a ient file. **
e3a60 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e that does not n
e3a70 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 eed to be locked
e3a80 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 . */. return
e3a90 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 &nolockIoMethods
e3aa0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 ;. }.. /* Test
e3ab0 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 73 if fcntl() is s
e3ac0 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 65 upported and use
e3ad0 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63 POSIX style loc
e3ae0 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 ks.. ** Otherwi
e3af0 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 se fall back to
e3b00 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 the named semaph
e3b10 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2f ore method.. */
e3b20 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 . lockInfo.l_le
e3b30 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 n = 1;. lockInf
e3b40 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 o.l_start = 0;.
e3b50 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e lockInfo.l_when
e3b60 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 ce = SEEK_SET;.
e3b70 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 lockInfo.l_type
e3b80 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 = F_RDLCK;. if
e3b90 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c ( fcntl(pNew->h,
e3ba0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 F_GETLK, &lockI
e3bb0 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 nfo)!=-1 ) {.
e3bc0 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f return &posixIo
e3bd0 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 Methods;. }else
e3be0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 65 {. return &se
e3bf0 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a mIoMethods;. }.
e3c00 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 }.static const s
e3c10 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
e3c20 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75 s . *(*const au
e3c30 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28 tolockIoFinder)(
e3c40 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 const char*,unix
e3c50 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63 File*) = autoloc
e3c60 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a kIoFinderImpl;..
e3c70 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 #endif /* OS_VXW
e3c80 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45 ORKS && SQLITE_E
e3c90 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e3ca0 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e YLE */../*.** An
e3cb0 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 abstract type f
e3cc0 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 or a pointer to
e3cd0 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64 a IO method find
e3ce0 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f 0a er function:.*/.
e3cf0 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 71 typedef const sq
e3d00 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 lite3_io_methods
e3d10 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 29 *(*finder_type)
e3d20 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 (const char*,uni
e3d30 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a 2a xFile*);.../****
e3d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3d80 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a ********.*******
e3d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3da0 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66 ***** sqlite3_vf
e3db0 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a s methods ******
e3dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
e3dd0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
e3de0 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61 s division conta
e3df0 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e ins the implemen
e3e00 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64 tation of method
e3e10 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69 s on the.** sqli
e3e20 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a te3_vfs object..
e3e30 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 */../*.** Initia
e3e40 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 lize the content
e3e50 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c s of the unixFil
e3e60 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e e structure poin
e3e70 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a 2a ted to by pId..*
e3e80 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c /.static int fil
e3e90 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 73 lInUnixFile(. s
e3ea0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
e3eb0 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 , /* Pointe
e3ec0 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 r to vfs object
e3ed0 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20 */. int h,
e3ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e3ef0 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 Open file descri
e3f00 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 69 ptor of file bei
e3f10 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 ng opened */. i
e3f20 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 20 nt dirfd,
e3f30 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 /* Direct
e3f40 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 ory file descrip
e3f50 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 tor */. sqlite3
e3f60 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20 _file *pId,
e3f70 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65 /* Write to the
e3f80 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 unixFile struct
e3f90 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f ure here */. co
e3fa0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e nst char *zFilen
e3fb0 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 ame, /* Name of
e3fc0 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20 the file being
e3fd0 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 opened */. int
e3fe0 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 noLock,
e3ff0 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 6b /* Omit lock
e4000 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 ing if true */.
e4010 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 int isDelete
e4020 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 /* Dele
e4030 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 te on close if t
e4040 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 rue */.){. cons
e4050 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
e4060 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 74 hods *pLockingSt
e4070 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 yle;. unixFile
e4080 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c *pNew = (unixFil
e4090 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72 e *)pId;. int r
e40a0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a c = SQLITE_OK;..
e40b0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
e40c0 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 pLock==NULL );.
e40d0 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 assert( pNew->p
e40e0 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 Open==NULL );..
e40f0 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73 /* Parameter is
e4100 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 75 Delete is only u
e4110 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 sed on vxworks.
e4120 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 70 Express this exp
e4130 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68 65 licitly . ** he
e4140 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f re to prevent co
e4150 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 mpiler warnings
e4160 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 72 about unused par
e4170 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 ameters.. */.
e4180 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
e4190 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20 4f (isDelete);.. O
e41a0 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 20 STRACE3("OPEN
e41b0 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20 %-3d %s\n", h,
e41c0 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 0a zFilename); .
e41d0 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a 20 pNew->h = h;.
e41e0 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 64 pNew->dirfd = d
e41f0 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 45 irfd;. SET_THRE
e4200 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70 4e ADID(pNew);. pN
e4210 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d 20 ew->fileFlags =
e4220 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 0;..#if OS_VXWOR
e4230 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d KS. pNew->pId =
e4240 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 vxworksFindFile
e4250 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 Id(zFilename);.
e4260 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d if( pNew->pId==
e4270 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20 0 ){. noLock
e4280 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 = 1;. rc = SQ
e4290 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
e42a0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f #endif.. if( no
e42b0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 Lock ){. pLoc
e42c0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c kingStyle = &nol
e42d0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 ockIoMethods;.
e42e0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b }else{. pLock
e42f0 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66 ingStyle = (**(f
e4300 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 73 inder_type*)pVfs
e4310 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 6c ->pAppData)(zFil
e4320 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23 69 ename, pNew);.#i
e4330 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
e4340 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 LOCKING_STYLE.
e4350 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c 65 /* Cache zFile
e4360 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b name in the lock
e4370 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 50 ing context (AFP
e4380 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 65 and dotlock ove
e4390 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 2a rride) for. *
e43a0 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 69 * proxyLock acti
e43b0 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 vation is possib
e43c0 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 79 le (remote proxy
e43d0 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 20 is based on db
e43e0 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 69 name). ** zFi
e43f0 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 76 lename remains v
e4400 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 20 alid until file
e4410 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 75 is closed, to su
e4420 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e 65 pport */. pNe
e4430 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 w->lockingContex
e4440 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c 65 t = (void*)zFile
e4450 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 7d name;.#endif. }
e4460 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 .. if( pLocking
e4470 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49 Style == &posixI
e4480 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
e4490 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e44a0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c ;. rc = findL
e44b0 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 ockInfo(pNew, &p
e44c0 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 New->pLock, &pNe
e44d0 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 w->pOpen);. i
e44e0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
e44f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 ){. /* If
e4500 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 an error occured
e4510 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f in findLockInfo
e4520 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69 (), close the fi
e4530 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 20 le descriptor.
e4540 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 ** immediate
e4550 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 61 ly, before relea
e4560 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20 sing the mutex.
e4570 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 6d findLockInfo() m
e4580 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a 2a ay fail. **
e4590 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 6f in two scenario
e45a0 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 s:. **.
e45b0 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 6c ** (a) A cal
e45c0 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 69 l to fstat() fai
e45d0 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 20 led.. **
e45e0 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 69 (b) A malloc fai
e45f0 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 led.. **.
e4600 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f 20 ** Scenario
e4610 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 63 (b) may only occ
e4620 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 73 ur if the proces
e4630 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f 20 s is holding no
e4640 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 66 other. ** f
e4650 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 ile descriptors
e4660 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 open on the same
e4670 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 file. If there
e4680 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 0a were other file.
e4690 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70 ** descrip
e46a0 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 6c tors on this fil
e46b0 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c 6f e, then no mallo
e46c0 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 69 c would be requi
e46d0 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20 red by. **
e46e0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 20 findLockInfo().
e46f0 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 If this is the c
e4700 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74 65 ase, it is quite
e4710 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a 20 safe to close.
e4720 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 68 ** handle h
e4730 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61 72 - as it is guar
e4740 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20 70 anteed that no p
e4750 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 osix locks will
e4760 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 be released.
e4770 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73 6f ** by doing so
e4780 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 .. **.
e4790 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f 20 ** If scenario
e47a0 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20 65 (a) caused the e
e47b0 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 73 rror then things
e47c0 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 65 are not so safe
e47d0 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 69 . The. ** i
e47e0 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 69 mplicit assumpti
e47f0 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20 on here is that
e4800 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c 73 if fstat() fails
e4810 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e 0a , things are in.
e4820 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62 61 ** such ba
e4830 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72 6f d shape that dro
e4840 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72 20 pping a lock or
e4850 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 two doesn't matt
e4860 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20 2a er much.. *
e4870 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68 29 /. close(h)
e4880 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0a ;. h = -1;.
e4890 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 }. unixLe
e48a0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
e48b0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 .#if SQLITE_ENAB
e48c0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 LE_LOCKING_STYLE
e48d0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 && defined(__AP
e48e0 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66 PLE__). else if
e48f0 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 ( pLockingStyle
e4900 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 == &afpIoMethods
e4910 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c ){. /* AFP l
e4920 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 ocking uses the
e4930 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 file path so it
e4940 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c needs to be incl
e4950 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 uded in. ** t
e4960 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e he afpLockingCon
e4970 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 text.. */.
e4980 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 afpLockingConte
e4990 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e xt *pCtx;. pN
e49a0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 ew->lockingConte
e49b0 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69 xt = pCtx = sqli
e49c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 te3_malloc( size
e49d0 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20 of(*pCtx) );.
e49e0 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a if( pCtx==0 ){.
e49f0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e4a00 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c E_NOMEM;. }el
e4a10 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a se{. /* NB:
e4a20 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74 zFilename exist
e4a30 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61 s and remains va
e4a40 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69 lid until the fi
e4a50 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20 le is closed.
e4a60 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20 ** according
e4a70 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46 to requirement F
e4a80 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f 11141. So we do
e4a90 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b not need to mak
e4aa0 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70 e a. ** cop
e4ab0 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d y of the filenam
e4ac0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78 e. */. pCtx
e4ad0 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65 ->dbPath = zFile
e4ae0 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 6e name;. sran
e4af0 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20 domdev();.
e4b00 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 unixEnterMutex()
e4b10 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e ;. rc = fin
e4b20 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 dLockInfo(pNew,
e4b30 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f 70 NULL, &pNew->pOp
e4b40 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 4c en);. unixL
e4b50 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20 eaveMutex();
e4b60 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 . }. }.#
e4b70 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66 endif.. else if
e4b80 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 ( pLockingStyle
e4b90 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 == &dotlockIoMet
e4ba0 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44 hods ){. /* D
e4bb0 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 otfile locking u
e4bc0 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74 ses the file pat
e4bd0 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f h so it needs to
e4be0 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a be included in.
e4bf0 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f ** the dotlo
e4c00 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ckLockingContext
e4c10 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61 . */. cha
e4c20 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 r *zLockFile;.
e4c30 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b int nFilename;
e4c40 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d . nFilename =
e4c50 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69 (int)strlen(zFi
e4c60 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20 lename) + 6;.
e4c70 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 zLockFile = (ch
e4c80 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c ar *)sqlite3_mal
e4c90 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a loc(nFilename);.
e4ca0 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c if( zLockFil
e4cb0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 e==0 ){. rc
e4cc0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
e4cd0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
e4ce0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
e4cf0 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a 4c tf(nFilename, zL
e4d00 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 4f ockFile, "%s" DO
e4d10 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a 46 TLOCK_SUFFIX, zF
e4d20 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a ilename);. }.
e4d30 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e pNew->lockin
e4d40 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 6b gContext = zLock
e4d50 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f File;. }..#if O
e4d60 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 65 S_VXWORKS. else
e4d70 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 if( pLockingSty
e4d80 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 68 le == &semIoMeth
e4d90 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 61 ods ){. /* Na
e4da0 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f med semaphore lo
e4db0 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 cking uses the f
e4dc0 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e ile path so it n
e4dd0 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a eeds to be. *
e4de0 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 * included in th
e4df0 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e 74 e semLockingCont
e4e00 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75 ext. */. u
e4e10 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b nixEnterMutex();
e4e20 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f . rc = findLo
e4e30 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e ckInfo(pNew, &pN
e4e40 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77 ew->pLock, &pNew
e4e50 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 66 ->pOpen);. if
e4e60 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b ( (rc==SQLITE_OK
e4e70 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 65 ) && (pNew->pOpe
e4e80 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29 n->pSem==NULL) )
e4e90 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53 {. char *zS
e4ea0 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70 emName = pNew->p
e4eb0 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a Open->aSemName;.
e4ec0 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 int n;.
e4ed0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 sqlite3_snpri
e4ee0 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 ntf(MAX_PATHNAME
e4ef0 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 73 , zSemName, "/%s
e4f00 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20 .sem",.
e4f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e pN
e4f20 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69 ew->pId->zCanoni
e4f30 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 calName);.
e4f40 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e 61 for( n=1; zSemNa
e4f50 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20 me[n]; n++ ).
e4f60 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d if( zSemNam
e4f70 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d e[n]=='/' ) zSem
e4f80 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 Name[n] = '_';.
e4f90 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e pNew->pOpen
e4fa0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 ->pSem = sem_ope
e4fb0 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 n(zSemName, O_CR
e4fc0 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 EAT, 0666, 1);.
e4fd0 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 if( pNew->p
e4fe0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45 Open->pSem == SE
e4ff0 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20 M_FAILED ){.
e5000 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
e5010 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 NOMEM;. p
e5020 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d New->pOpen->aSem
e5030 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a Name[0] = '\0';.
e5040 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
e5050 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 unixLeaveMutex
e5060 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ();. }.#endif.
e5070 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72 . pNew->lastEr
e5080 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f rno = 0;.#if OS_
e5090 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63 VXWORKS. if( rc
e50a0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
e50b0 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e unlink(zFilen
e50c0 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65 ame);. isDele
e50d0 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e te = 0;. }. pN
e50e0 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69 ew->isDelete = i
e50f0 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a sDelete;.#endif.
e5100 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
e5110 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64 _OK ){. if( d
e5120 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28 irfd>=0 ) close(
e5130 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e dirfd); /* silen
e5140 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 t leak if fail,
e5150 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 already in error
e5160 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d 30 */. if( h>=0
e5170 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 7d ) close(h);. }
e5180 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e else{. pNew->
e5190 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b 69 pMethod = pLocki
e51a0 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 65 ngStyle;. Ope
e51b0 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 nCounter(+1);.
e51c0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
e51d0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 ../*.** Open a f
e51e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
e51f0 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 o the directory
e5200 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20 containing file
e5210 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66 zFilename..** If
e5220 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46 successful, *pF
e5230 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 d is set to the
e5240 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63 opened file desc
e5250 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51 riptor and.** SQ
e5260 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
e5270 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 ned. If an error
e5280 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20 occurs, either
e5290 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 SQLITE_NOMEM.**
e52a0 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 or SQLITE_CANTOP
e52b0 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 EN is returned a
e52c0 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 nd *pFd is set t
e52d0 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a o an undefined.*
e52e0 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 * value..**.** I
e52f0 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 f SQLITE_OK is r
e5300 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c eturned, the cal
e5310 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 ler is responsib
e5320 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a le for closing.*
e5330 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 * the file descr
e5340 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67 iptor *pFd using
e5350 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61 close()..*/.sta
e5360 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65 tic int openDire
e5370 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72 ctory(const char
e5380 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74 *zFilename, int
e5390 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69 *pFd){. int ii
e53a0 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b ;. int fd = -1;
e53b0 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65 . char zDirname
e53c0 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d [MAX_PATHNAME+1]
e53d0 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 ;.. sqlite3_snp
e53e0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 rintf(MAX_PATHNA
e53f0 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25 ME, zDirname, "%
e5400 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a s", zFilename);.
e5410 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74 for(ii=(int)st
e5420 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 rlen(zDirname);
e5430 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65 ii>1 && zDirname
e5440 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 [ii]!='/'; ii--)
e5450 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a ;. if( ii>0 ){.
e5460 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d zDirname[ii]
e5470 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 = '\0';. fd
e5480 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c = open(zDirname,
e5490 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 O_RDONLY|O_BINA
e54a0 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 RY, 0);. if(
e54b0 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 fd>=0 ){.#ifdef
e54c0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 FD_CLOEXEC.
e54d0 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 fcntl(fd, F_SET
e54e0 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f FD, fcntl(fd, F_
e54f0 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 GETFD, 0) | FD_C
e5500 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a LOEXEC);.#endif.
e5510 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 OSTRACE3("
e5520 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c OPENDIR %-3d %s\
e5530 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 n", fd, zDirname
e5540 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a );. }. }. *
e5550 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 pFd = fd;. retu
e5560 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 rn (fd>=0?SQLITE
e5570 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f _OK:SQLITE_CANTO
e5580 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 PEN);.}../*.** C
e5590 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 reate a temporar
e55a0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a y file name in z
e55b0 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 Buf. zBuf must
e55c0 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 be allocated.**
e55d0 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 by the calling p
e55e0 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 rocess and must
e55f0 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f be big enough to
e5600 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a hold at least.*
e5610 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 * pVfs->mxPathna
e5620 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 me bytes..*/.sta
e5630 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e tic int getTempn
e5640 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 ame(int nBuf, ch
e5650 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 ar *zBuf){. sta
e5660 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a tic const char *
e5670 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 azDirs[] = {.
e5680 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 20 0,. 0,.
e5690 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 20 "/var/tmp",.
e56a0 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a 20 "/usr/tmp",.
e56b0 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 20 "/tmp",.
e56c0 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 ".",. };. sta
e56d0 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
e56e0 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d ed char zChars[]
e56f0 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 =. "abcdefgh
e5700 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 ijklmnopqrstuvwx
e5710 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 yz". "ABCDEFG
e5720 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 HIJKLMNOPQRSTUVW
e5730 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 XYZ". "012345
e5740 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65 6789";. unsigne
e5750 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 d int i, j;. st
e5760 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 ruct stat buf;.
e5770 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 const char *zDi
e5780 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 r = ".";.. /* I
e5790 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c t's odd to simul
e57a0 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 ate an io-error
e57b0 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 here, but really
e57c0 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 this is just.
e57d0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d ** using the io-
e57e0 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 error infrastruc
e57f0 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 ture to test tha
e5800 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 t SQLite handles
e5810 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 this. ** funct
e5820 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 ion failing. .
e5830 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 */. SimulateIOE
e5840 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c rror( return SQL
e5850 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 ITE_IOERR );..
e5860 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 azDirs[0] = sqli
e5870 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f te3_temp_directo
e5880 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 3d ry;. if (NULL =
e5890 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a 20 = azDirs[1]) {.
e58a0 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 67 azDirs[1] = g
e58b0 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29 3b etenv("TMPDIR");
e58c0 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 3d . }. . for(i=
e58d0 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69 0; i<sizeof(azDi
e58e0 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72 rs)/sizeof(azDir
e58f0 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 s[0]); i++){.
e5900 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d if( azDirs[i]==
e5910 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 0 ) continue;.
e5920 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69 72 if( stat(azDir
e5930 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f s[i], &buf) ) co
e5940 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 ntinue;. if(
e5950 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f !S_ISDIR(buf.st_
e5960 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 mode) ) continue
e5970 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73 73 ;. if( access
e5980 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20 (azDirs[i], 07)
e5990 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
e59a0 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d zDir = azDirs[i]
e59b0 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d ;. break;. }
e59c0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 .. /* Check tha
e59d0 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 t the output buf
e59e0 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f fer is large eno
e59f0 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70 ugh for the temp
e5a00 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a orary file . **
e5a10 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20 name. If it is
e5a20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 not, return SQLI
e5a30 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20 TE_ERROR.. */.
e5a40 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 69 if( (strlen(zDi
e5a50 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c 49 r) + strlen(SQLI
e5a60 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 TE_TEMP_FILE_PRE
e5a70 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 73 FIX) + 17) >= (s
e5a80 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 20 ize_t)nBuf ){.
e5a90 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
e5aa0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f ERROR;. }.. do
e5ab0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e {. sqlite3_sn
e5ac0 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20 printf(nBuf-17,
e5ad0 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54 zBuf, "%s/"SQLIT
e5ae0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 E_TEMP_FILE_PREF
e5af0 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a IX, zDir);. j
e5b00 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a = (int)strlen(z
e5b10 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Buf);. sqlite
e5b20 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c 3_randomness(15,
e5b30 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 20 &zBuf[j]);.
e5b40 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 69 for(i=0; i<15; i
e5b50 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ++, j++){.
e5b60 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 zBuf[j] = (char)
e5b70 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e zChars[ ((unsign
e5b80 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 ed char)zBuf[j])
e5b90 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 %(sizeof(zChars)
e5ba0 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 -1) ];. }.
e5bb0 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 zBuf[j] = 0;.
e5bc0 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28 7a }while( access(z
e5bd0 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72 Buf,0)==0 );. r
e5be0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
e5bf0 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 .}..#if SQLITE_E
e5c00 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
e5c10 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f YLE && defined(_
e5c20 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20 _APPLE__)./*.**
e5c30 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73 Routine to trans
e5c40 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20 form a unixFile
e5c50 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63 into a proxy-loc
e5c60 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a king unixFile..*
e5c70 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e * Implementation
e5c80 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f in the proxy-lo
e5c90 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74 ck division, but
e5ca0 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65 used by unixOpe
e5cb0 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 n().** if SQLITE
e5cc0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f _PREFER_PROXY_LO
e5cd0 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64 CKING is defined
e5ce0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
e5cf0 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e proxyTransformUn
e5d00 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a ixFile(unixFile*
e5d10 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a , const char*);.
e5d20 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 #endif../*.** Se
e5d30 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 arch for an unus
e5d40 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ed file descript
e5d50 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e or that was open
e5d60 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ed on the databa
e5d70 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f 74 se .** file (not
e5d80 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d 61 a journal or ma
e5d90 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c ster-journal fil
e5da0 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 e) identified by
e5db0 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 61 pathname.** zPa
e5dc0 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f th with SQLITE_O
e5dd0 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d 61 PEN_XXX flags ma
e5de0 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 73 tching those pas
e5df0 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e sed as the secon
e5e00 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f d.** argument to
e5e10 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
e5e20 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c **.** Such a fil
e5e30 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79 e descriptor may
e5e40 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74 61 exist if a data
e5e50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
e5e60 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 75 was closed.** bu
e5e70 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 t the associated
e5e80 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e5e90 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c could not be cl
e5ea0 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d osed because som
e5eb0 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20 e.** other file
e5ec0 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20 descriptor open
e5ed0 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 on the same file
e5ee0 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69 is holding a fi
e5ef0 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 65 le-lock..** Refe
e5f00 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 6e r to comments in
e5f10 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 29 the unixClose()
e5f20 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 68 function and th
e5f30 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 6e e lengthy commen
e5f40 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 t.** describing
e5f50 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20 "Posix Advisory
e5f60 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 20 Locking" at the
e5f70 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 69 start of this fi
e5f80 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 68 le for .** furth
e5f90 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 6f er details. Also
e5fa0 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e 0a , ticket #4018..
e5fb0 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 61 **.** If a suita
e5fc0 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 ble file descrip
e5fd0 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 tor is found, th
e5fe0 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 en it is returne
e5ff0 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 68 d. If no.** such
e6000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
e6010 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 20 is located, -1
e6020 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a is returned..*/.
e6030 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 65 static UnixUnuse
e6040 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 6c dFd *findReusabl
e6050 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a eFd(const char *
e6060 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73 zPath, int flags
e6070 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 ){. UnixUnusedF
e6080 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a d *pUnused = 0;.
e6090 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 61 . /* Do not sea
e60a0 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65 rch for an unuse
e60b0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f d file descripto
e60c0 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e 6f r on vxworks. No
e60d0 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 76 t because. ** v
e60e0 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f 74 xworks would not
e60f0 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 68 benefit from th
e6100 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 67 e change (it mig
e6110 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 75 ht, we're not su
e6120 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 65 re),. ** but be
e6130 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f 20 cause no way to
e6140 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72 65 test it is curre
e6150 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e 20 ntly available.
e6160 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 20 It is better .
e6170 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62 ** not to risk b
e6180 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20 reaking vxworks
e6190 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20 support for the
e61a0 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20 sake of such an
e61b0 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 65 obscure . ** fe
e61c0 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 21 ature. */.#if !
e61d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 OS_VXWORKS. str
e61e0 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b 20 uct stat sStat;
e61f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6200 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 /* Results of
e6210 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a 0a stat() call */..
e6220 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 61 /* A stat() ca
e6230 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 20 ll may fail for
e6240 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 2e various reasons.
e6250 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 If this happens
e6260 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c 6d , it is. ** alm
e6270 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 74 ost certain that
e6280 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20 an open() call
e6290 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68 on the same path
e62a0 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c 2e will also fail.
e62b0 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 . ** For this r
e62c0 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 72 eason, if an err
e62d0 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 or occurs in the
e62e0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 72 stat() call her
e62f0 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 67 e, it is. ** ig
e6300 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 20 nored and -1 is
e6310 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61 returned. The ca
e6320 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 6f ller will try to
e6330 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65 open a new file
e6340 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 . ** descriptor
e6350 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 on the same pat
e6360 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 74 h, fail, and ret
e6370 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f 20 urn an error to
e6380 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a SQLite.. **. *
e6390 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 73 * Even if a subs
e63a0 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 61 equent open() ca
e63b0 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 2c ll does succeed,
e63c0 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65 the consequence
e63d0 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 s of. ** not se
e63e0 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 65 arching for a re
e63f0 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 73 susable file des
e6400 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 20 criptor are not
e6410 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 dire. */. if(
e6420 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20 26 0==stat(zPath, &
e6430 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73 74 sStat) ){. st
e6440 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 ruct unixOpenCnt
e6450 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63 74 *pO;. struct
e6460 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b 0a unixFileId id;.
e6470 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53 74 id.dev = sSt
e6480 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20 69 at.st_dev;. i
e6490 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73 74 d.ino = sStat.st
e64a0 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78 45 _ino;.. unixE
e64b0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 nterMutex();.
e64c0 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73 74 for(pO=openList
e64d0 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28 26 ; pO && memcmp(&
e64e0 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64 2c id, &pO->fileId,
e64f0 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70 4f sizeof(id)); pO
e6500 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 20 =pO->pNext);.
e6510 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20 20 if( pO ){.
e6520 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2a UnixUnusedFd **
e6530 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 70 pp;. for(pp
e6540 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20 2a =&pO->pUnused; *
e6550 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c 61 pp && (*pp)->fla
e6560 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 28 gs!=flags; pp=&(
e6570 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a (*pp)->pNext));.
e6580 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20 pUnused =
e6590 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 *pp;. if( p
e65a0 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 20 Unused ){.
e65b0 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64 2d *pp = pUnused-
e65c0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a >pNext;. }.
e65d0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 }. unixLe
e65e0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
e65f0 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66 20 #endif /* if
e6600 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 20 !OS_VXWORKS */.
e6610 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64 3b return pUnused;
e6620 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 .}../*.** Open t
e6630 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a he file zPath..*
e6640 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79 * .** Previously
e6650 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 20 , the SQLite OS
e6660 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 65 layer used three
e6670 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c functions in pl
e6680 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f ace of this.** o
e6690 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 ne:.**.** sq
e66a0 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57 lite3OsOpenReadW
e66b0 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73 rite();.** s
e66c0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 qlite3OsOpenRead
e66d0 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73 Only();.** s
e66e0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c qlite3OsOpenExcl
e66f0 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54 usive();.**.** T
e6700 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65 hese calls corre
e6710 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c spond to the fol
e6720 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 lowing combinati
e6730 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a ons of flags:.**
e6740 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 74 .** ReadWrit
e6750 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44 e() -> (READ
e6760 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 0a WRITE | CREATE).
e6770 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 28 ** ReadOnly(
e6780 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 4f ) -> (READO
e6790 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65 NLY) .** Ope
e67a0 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e 20 nExclusive() ->
e67b0 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 (READWRITE | CRE
e67c0 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 29 ATE | EXCLUSIVE)
e67d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f .**.** The old O
e67e0 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 61 penExclusive() a
e67f0 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 61 ccepted a boolea
e6800 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 65 n argument - "de
e6810 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72 lFlag". If.** tr
e6820 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 73 ue, the file was
e6830 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 62 configured to b
e6840 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 e automatically
e6850 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 deleted when the
e6860 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 .** file handle
e6870 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 65 closed. To achie
e6880 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 ve the same effe
e6890 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e 65 ct using this ne
e68a0 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c w .** interface,
e68b0 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 4f add the DELETEO
e68c0 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 74 NCLOSE flag to t
e68d0 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 61 hose specified a
e68e0 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65 bove for .** Ope
e68f0 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f nExclusive()..*/
e6900 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 .static int unix
e6910 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f Open(. sqlite3_
e6920 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
e6930 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 /* The VFS
e6940 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 69 for which this i
e6950 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 s the xOpen meth
e6960 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 od */. const ch
e6970 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 ar *zPath,
e6980 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d 65 /* Pathname
e6990 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 6f of file to be o
e69a0 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 pened */. sqlit
e69b0 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 e3_file *pFile,
e69c0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 /* The f
e69d0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 ile descriptor t
e69e0 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a o be filled in *
e69f0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 /. int flags,
e6a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6a10 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20 /* Input flags
e6a20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f to control the o
e6a30 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 pening */. int
e6a40 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 *pOutFlags
e6a50 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 /* Outp
e6a60 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 ut flags returne
e6a70 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65 d to SQLite core
e6a80 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c */.){. unixFil
e6a90 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c 65 e *p = (unixFile
e6aa0 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 20 *)pFile;. int
e6ab0 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 fd = -1;
e6ac0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
e6ad0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 65 le descriptor re
e6ae0 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 29 turned by open()
e6af0 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 20 */. int dirfd
e6b00 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 = -1;
e6b10 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72 /* Director
e6b20 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f y file descripto
e6b30 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46 r */. int openF
e6b40 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20 lags = 0;
e6b50 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 /* Flags t
e6b60 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29 o pass to open()
e6b70 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20 */. int eType
e6b80 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46 = flags&0xFFFFFF
e6b90 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 00; /* Type of
e6ba0 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a file to open */.
e6bb0 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20 int noLock;
e6bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e6bd0 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 /* True to omit
e6be0 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 locking primiti
e6bf0 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 ves */. int rc
e6c00 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
e6c10 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 /* Funct
e6c20 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20 ion Return Code
e6c30 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c */.. int isExcl
e6c40 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20 usive = (flags
e6c50 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 & SQLITE_OPEN_EX
e6c60 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20 CLUSIVE);. int
e6c70 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28 isDelete = (
e6c80 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f flags & SQLITE_O
e6c90 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 PEN_DELETEONCLOS
e6ca0 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61 E);. int isCrea
e6cb0 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20 te = (flags
e6cc0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 & SQLITE_OPEN_CR
e6cd0 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52 EATE);. int isR
e6ce0 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61 eadonly = (fla
e6cf0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
e6d00 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e _READONLY);. in
e6d10 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d t isReadWrite =
e6d20 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
e6d30 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 _OPEN_READWRITE)
e6d40 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74 ;.. /* If creat
e6d50 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20 ing a master or
e6d60 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61 main-file journa
e6d70 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e l, this function
e6d80 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20 will open. **
e6d90 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f a file-descripto
e6da0 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f r on the directo
e6db0 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73 ry too. The firs
e6dc0 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28 t time unixSync(
e6dd0 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 ). ** is called
e6de0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66 the directory f
e6df0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 ile descriptor w
e6e00 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64 ill be fsync()ed
e6e10 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20 and close()d..
e6e20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e */. int isOpen
e6e30 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43 Directory = (isC
e6e40 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 20 reate && .
e6e50 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f (eType==SQLITE_O
e6e60 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e PEN_MASTER_JOURN
e6e70 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c AL || eType==SQL
e6e80 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f ITE_OPEN_MAIN_JO
e6e90 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f URNAL). );.. /
e6ea0 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50 * If argument zP
e6eb0 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f ath is a NULL po
e6ec0 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 inter, this func
e6ed0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 tion is required
e6ee0 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 to open. ** a
e6ef0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 temporary file.
e6f00 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20 Use this buffer
e6f10 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c to store the fil
e6f20 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a e name in.. */.
e6f30 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b char zTmpname[
e6f40 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b MAX_PATHNAME+1];
e6f50 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
e6f60 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20 Name = zPath;..
e6f70 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f /* Check the fo
e6f80 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e llowing statemen
e6f90 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20 ts are true: .
e6fa0 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78 **. ** (a) Ex
e6fb0 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65 actly one of the
e6fc0 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52 READWRITE and R
e6fd0 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75 EADONLY flags mu
e6fe0 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a st be set, and .
e6ff0 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52 ** (b) if CR
e7000 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 65 EATE is set, the
e7010 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 74 n READWRITE must
e7020 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e also be set, an
e7030 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20 d. ** (c) if
e7040 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 74 EXCLUSIVE is set
e7050 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75 , then CREATE mu
e7060 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a st also be set..
e7070 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45 ** (d) if DE
e7080 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73 LETEONCLOSE is s
e7090 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 et, then CREATE
e70a0 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 must also be set
e70b0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
e70c0 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c (isReadonly==0 |
e70d0 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30 | isReadWrite==0
e70e0 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 74 ) && (isReadWrit
e70f0 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29 e || isReadonly)
e7100 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 72 );. assert(isCr
e7110 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61 eate==0 || isRea
e7120 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72 dWrite);. asser
e7130 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 t(isExclusive==0
e7140 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20 || isCreate);.
e7150 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 assert(isDelete
e7160 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29 ==0 || isCreate)
e7170 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e ;.. /* The main
e7180 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 DB, main journa
e7190 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f l, and master jo
e71a0 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20 urnal are never
e71b0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20 automatically.
e71c0 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 20 ** deleted. Nor
e71d0 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74 65 are they ever te
e71e0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 20 mporary files.
e71f0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 */. assert( (!i
e7200 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 sDelete && zName
e7210 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 ) || eType!=SQLI
e7220 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 TE_OPEN_MAIN_DB
e7230 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69 );. assert( (!i
e7240 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65 sDelete && zName
e7250 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49 ) || eType!=SQLI
e7260 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 TE_OPEN_MAIN_JOU
e7270 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 RNAL );. assert
e7280 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 ( (!isDelete &&
e7290 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 zName) || eType!
e72a0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 =SQLITE_OPEN_MAS
e72b0 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 0a TER_JOURNAL );..
e72c0 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 /* Assert that
e72d0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 the upper layer
e72e0 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20 has set one of
e72f0 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20 the "file-type"
e7300 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65 flags. */. asse
e7310 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 rt( eType==SQLIT
e7320 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20 E_OPEN_MAIN_DB
e7330 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 || eType==SQ
e7340 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 LITE_OPEN_TEMP_D
e7350 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 B . || eTy
e7360 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f pe==SQLITE_OPEN_
e7370 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 MAIN_JOURNAL ||
e7380 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 eType==SQLITE_OP
e7390 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 EN_TEMP_JOURNAL
e73a0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
e73b0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 ==SQLITE_OPEN_SU
e73c0 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54 BJOURNAL || eT
e73d0 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e ype==SQLITE_OPEN
e73e0 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 _MASTER_JOURNAL
e73f0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 . || eType
e7400 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 ==SQLITE_OPEN_TR
e7410 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a ANSIENT_DB. );.
e7420 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
e7430 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 sizeof(unixFile)
e7440 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d );.. if( eType=
e7450 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 =SQLITE_OPEN_MAI
e7460 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69 78 N_DB ){. Unix
e7470 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 UnusedFd *pUnuse
e7480 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 3d d;. pUnused =
e7490 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28 findReusableFd(
e74a0 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a 20 zName, flags);.
e74b0 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 29 if( pUnused )
e74c0 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55 6e {. fd = pUn
e74d0 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d 65 used->fd;. }e
e74e0 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75 73 lse{. pUnus
e74f0 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ed = sqlite3_mal
e7500 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e 75 loc(sizeof(*pUnu
e7510 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66 28 sed));. if(
e7520 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 !pUnused ){.
e7530 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
e7540 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 TE_NOMEM;.
e7550 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 }. }. p->p
e7560 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 64 Unused = pUnused
e7570 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a ;. }else if( !z
e7580 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Name ){. /* I
e7590 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c f zName is NULL,
e75a0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72 the upper layer
e75b0 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61 is requesting a
e75c0 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20 temp file. */.
e75d0 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 assert(isDele
e75e0 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 72 te && !isOpenDir
e75f0 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 20 ectory);. rc
e7600 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 = getTempname(MA
e7610 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a 54 X_PATHNAME+1, zT
e7620 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 mpname);. if(
e7630 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
e7640 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 {. return r
e7650 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61 c;. }. zNa
e7660 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 me = zTmpname;.
e7670 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 }.. /* Determi
e7680 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ne the value of
e7690 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 the flags parame
e76a0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50 4f ter passed to PO
e76b0 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a SIX function. *
e76c0 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65 20 * open(). These
e76d0 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61 74 must be calculat
e76e0 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e 28 ed even if open(
e76f0 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2c ) is not called,
e7700 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d 61 as. ** they ma
e7710 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 70 y be stored as p
e7720 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 art of the file
e7730 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64 20 handle and used
e7740 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63 6f by the . ** 'co
e7750 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69 6e nch file' lockin
e7760 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74 65 g functions late
e7770 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28 20 r on. */. if(
e7780 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70 isReadonly ) op
e7790 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f enFlags |= O_RDO
e77a0 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 61 NLY;. if( isRea
e77b0 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61 dWrite ) openFla
e77c0 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20 gs |= O_RDWR;.
e77d0 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20 if( isCreate )
e77e0 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f openFlags |= O
e77f0 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 73 _CREAT;. if( is
e7800 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e Exclusive ) open
e7810 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c Flags |= (O_EXCL
e7820 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20 |O_NOFOLLOW);.
e7830 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f openFlags |= (O_
e7840 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41 LARGEFILE|O_BINA
e7850 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c 30 RY);.. if( fd<0
e7860 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20 6f ){. mode_t o
e7870 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65 6c penMode = (isDel
e7880 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f ete?0600:SQLITE_
e7890 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 DEFAULT_FILE_PER
e78a0 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 66 MISSIONS);. f
e78b0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 d = open(zName,
e78c0 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d openFlags, openM
e78d0 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 ode);. OSTRAC
e78e0 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 64 E4("OPENX %-3d
e78f0 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20 %s 0%o\n", fd,
e7900 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 zName, openFlags
e7910 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 );. if( fd<0
e7920 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52 && errno!=EISDIR
e7930 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65 20 && isReadWrite
e7940 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 20 && !isExclusive
e7950 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 6c ){. /* Fail
e7960 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 66 ed to open the f
e7970 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 69 ile for read/wri
e7980 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 72 te access. Try r
e7990 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20 ead-only. */.
e79a0 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51 flags &= ~(SQ
e79b0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 LITE_OPEN_READWR
e79c0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ITE|SQLITE_OPEN_
e79d0 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20 6f CREATE);. o
e79e0 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f 5f penFlags &= ~(O_
e79f0 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a 20 RDWR|O_CREAT);.
e7a00 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 flags |= SQ
e7a10 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e LITE_OPEN_READON
e7a20 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c LY;. openFl
e7a30 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b ags |= O_RDONLY;
e7a40 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65 6e . fd = open
e7a50 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 (zName, openFlag
e7a60 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 s, openMode);.
e7a70 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c 30 }. if( fd<0
e7a80 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
e7a90 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a QLITE_CANTOPEN;.
e7aa0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f goto open_
e7ab0 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a finished;. }.
e7ac0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66 64 }. assert( fd
e7ad0 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 75 >=0 );. if( pOu
e7ae0 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70 tFlags ){. *p
e7af0 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 OutFlags = flags
e7b00 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e ;. }.. if( p->
e7b10 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 70 pUnused ){. p
e7b20 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d 20 ->pUnused->fd =
e7b30 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73 fd;. p->pUnus
e7b40 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 ed->flags = flag
e7b50 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 s;. }.. if( is
e7b60 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f 53 Delete ){.#if OS
e7b70 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 61 _VXWORKS. zPa
e7b80 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73 th = zName;.#els
e7b90 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61 e. unlink(zNa
e7ba0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a me);.#endif. }.
e7bb0 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c #if SQLITE_ENABL
e7bc0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a E_LOCKING_STYLE.
e7bd0 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6f else{. p->o
e7be0 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46 penFlags = openF
e7bf0 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 lags;. }.#endif
e7c00 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 69 .. if( isOpenDi
e7c10 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 72 rectory ){. r
e7c20 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f 72 c = openDirector
e7c30 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 29 y(zPath, &dirfd)
e7c40 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 ;. if( rc!=SQ
e7c50 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
e7c60 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20 74 /* It is safe t
e7c70 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74 68 o close fd at th
e7c80 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75 73 is point, becaus
e7c90 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 e it is guarante
e7ca0 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 ed not. **
e7cb0 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61 20 to be open on a
e7cc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
e7cd0 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20 6f f it were open o
e7ce0 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c n a database fil
e7cf0 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 77 e,. ** it w
e7d00 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 65 ould not be safe
e7d10 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68 69 to close as thi
e7d20 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65 20 s would release
e7d30 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a 20 any locks held.
e7d40 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 66 ** on the f
e7d50 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f 63 ile by this proc
e7d60 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61 ess. */. a
e7d70 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 ssert( eType!=SQ
e7d80 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 LITE_OPEN_MAIN_D
e7d90 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 B );. close
e7da0 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20 20 (fd);
e7db0 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 /* silently le
e7dc0 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 65 ak if fail, alre
e7dd0 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a ady in error */.
e7de0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f goto open_
e7df0 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a finished;. }.
e7e00 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 }..#ifdef FD_C
e7e10 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 LOEXEC. fcntl(f
e7e20 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 d, F_SETFD, fcnt
e7e30 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 l(fd, F_GETFD, 0
e7e40 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b ) | FD_CLOEXEC);
e7e50 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 .#endif.. noLoc
e7e60 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54 k = eType!=SQLIT
e7e70 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a E_OPEN_MAIN_DB;.
e7e80 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46 .#if SQLITE_PREF
e7e90 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 ER_PROXY_LOCKING
e7ea0 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e 55 . if( zPath!=NU
e7eb0 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26 26 LL && !noLock &&
e7ec0 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b 0a pVfs->xOpen ){.
e7ed0 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72 char *envfor
e7ee0 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c ce = getenv("SQL
e7ef0 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f ITE_FORCE_PROXY_
e7f00 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 69 LOCKING");. i
e7f10 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b nt useProxy = 0;
e7f20 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f .. /* SQLITE_
e7f30 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b FORCE_PROXY_LOCK
e7f40 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f 72 ING==1 means for
e7f50 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70 72 ce always use pr
e7f60 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20 20 oxy, 0 means .
e7f70 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20 70 ** never use p
e7f80 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e 73 roxy, NULL means
e7f90 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20 6e use proxy for n
e7fa0 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 6f on-local files o
e7fb0 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 nly. */. if(
e7fc0 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c 20 envforce!=NULL
e7fd0 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 ){. useProx
e7fe0 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 63 y = atoi(envforc
e7ff0 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b e)>0;. }else{
e8000 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74 . struct st
e8010 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 20 atfs fsInfo;.
e8020 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a 50 if( statfs(zP
e8030 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d ath, &fsInfo) ==
e8040 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f -1 ){. /
e8050 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65 * In theory, the
e8060 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c 20 close(fd) call
e8070 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e 20 is sub-optimal.
e8080 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e If the file open
e8090 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69 ed. ** wi
e80a0 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61 62 th fd is a datab
e80b0 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 68 ase file, and th
e80c0 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 6f ere are other co
e80d0 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a 20 nnections open.
e80e0 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61 ** on tha
e80f0 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20 t file that are
e8100 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e currently holdin
e8110 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73 g advisory locks
e8120 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 20 on it,.
e8130 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c ** then the call
e8140 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c to close() will
e8150 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f cancel those lo
e8160 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65 cks. In practice
e8170 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27 ,. ** we'
e8180 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74 re assuming that
e8190 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27 statfs() doesn'
e81a0 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65 t fail very ofte
e81b0 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 20 n. At least.
e81c0 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65 ** not while
e81d0 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63 other file desc
e81e0 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62 riptors opened b
e81f0 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 y the same proce
e8200 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a ss on. **
e8210 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 61 the same file a
e8220 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f 0a re working. */.
e8230 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 45 p->lastE
e8240 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 rrno = errno;.
e8250 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 3e if( dirfd>
e8260 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
e8270 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a close(dirfd); /*
e8280 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69 silently leak i
e8290 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72 f fail, in error
e82a0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 */. }.
e82b0 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b close(fd);
e82c0 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
e82d0 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 k if fail, in er
e82e0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 ror */. r
e82f0 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
e8300 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20 20 _ACCESS;.
e8310 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 goto open_finis
e8320 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 hed;. }.
e8330 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21 28 useProxy = !(
e8340 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d fsInfo.f_flags&M
e8350 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d NT_LOCAL);. }
e8360 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f 78 . if( useProx
e8370 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 y ){. rc =
e8380 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 fillInUnixFile(p
e8390 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 Vfs, fd, dirfd,
e83a0 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f pFile, zPath, no
e83b0 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b Lock, isDelete);
e83c0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
e83d0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
e83e0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72 rc = proxyTr
e83f0 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28 ansformUnixFile(
e8400 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c 65 (unixFile*)pFile
e8410 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20 , ":auto:");.
e8420 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 }. goto
e8430 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 open_finished;.
e8440 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
e8450 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e . rc = fillIn
e8460 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66 UnixFile(pVfs, f
e8470 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c d, dirfd, pFile,
e8480 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 zPath, noLock,
e8490 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e 5f isDelete);.open_
e84a0 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20 finished:. if(
e84b0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
e84c0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 . sqlite3_fre
e84d0 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20 e(p->pUnused);.
e84e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
e84f0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 }.../*.** Delete
e8500 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61 the file at zPa
e8510 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79 th. If the dirSy
e8520 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 nc argument is t
e8530 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20 rue, fsync().**
e8540 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 66 the directory af
e8550 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 ter deleting the
e8560 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
e8570 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28 int unixDelete(
e8580 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
e8590 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20 NotUsed, /*
e85a0 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 VFS containing t
e85b0 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c 65 his as the xDele
e85c0 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 te method */. c
e85d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 onst char *zPath
e85e0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 , /* Name
e85f0 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64 of file to be d
e8600 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 eleted */. int
e8610 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 20 dirSync
e8620 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 /* If true
e8630 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 74 , fsync() direct
e8640 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 ory after deleti
e8650 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 ng file */.){.
e8660 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
e8670 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 OK;. UNUSED_PAR
e8680 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e8690 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 . SimulateIOErr
e86a0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 or(return SQLITE
e86b0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a _IOERR_DELETE);.
e86c0 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b unlink(zPath);
e86d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
e86e0 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a DISABLE_DIRSYNC.
e86f0 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b if( dirSync ){
e8700 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20 . int fd;.
e8710 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 rc = openDirect
e8720 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b ory(zPath, &fd);
e8730 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
e8740 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53 ITE_OK ){.#if OS
e8750 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69 _VXWORKS. i
e8760 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31 f( fsync(fd)==-1
e8770 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69 ).#else. i
e8780 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23 f( fsync(fd) ).#
e8790 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 endif. {.
e87a0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
e87b0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e E_IOERR_DIR_FSYN
e87c0 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 C;. }.
e87d0 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26 if( close(fd)&&
e87e0 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 !rc ){. r
e87f0 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 c = SQLITE_IOERR
e8800 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 _DIR_CLOSE;.
e8810 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 }. }. }.#e
e8820 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 ndif. return rc
e8830 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 ;.}../*.** Test
e8840 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 the existance of
e8850 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69 or access permi
e8860 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a ssions of file z
e8870 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73 Path. The.** tes
e8880 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65 t performed depe
e8890 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 nds on the value
e88a0 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a of flags:.**.**
e88b0 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 SQLITE_ACCE
e88c0 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72 SS_EXISTS: Retur
e88d0 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 n 1 if the file
e88e0 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51 exists.** SQ
e88f0 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 LITE_ACCESS_READ
e8900 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20 WRITE: Return 1
e8910 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 if the file is r
e8920 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65 ead and writable
e8930 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f ..** SQLITE_
e8940 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a ACCESS_READONLY:
e8950 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 Return 1 if the
e8960 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c file is readabl
e8970 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 e..**.** Otherwi
e8980 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a se return 0..*/.
e8990 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41 static int unixA
e89a0 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 ccess(. sqlite3
e89b0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 _vfs *NotUsed,
e89c0 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74 /* The VFS cont
e89d0 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63 aining this xAcc
e89e0 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 ess method */.
e89f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 const char *zPat
e8a00 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20 h, /* Path
e8a10 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65 of the file to e
e8a20 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 xamine */. int
e8a30 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
e8a40 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77 /* What do w
e8a50 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20 e want to learn
e8a60 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20 about the zPath
e8a70 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a file? */. int *
e8a80 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20 pResOut
e8a90 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 /* Write resu
e8aa0 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20 lt boolean here
e8ab0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64 */.){. int amod
e8ac0 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f e = 0;. UNUSED_
e8ad0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e8ae0 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f d);. SimulateIO
e8af0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 Error( return SQ
e8b00 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 LITE_IOERR_ACCES
e8b10 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 S; );. switch(
e8b20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 flags ){. cas
e8b30 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e8b40 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d EXISTS:. am
e8b50 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20 ode = F_OK;.
e8b60 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 break;. cas
e8b70 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f e SQLITE_ACCESS_
e8b80 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20 READWRITE:.
e8b90 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f amode = W_OK|R_
e8ba0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b OK;. break;
e8bb0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
e8bc0 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 _ACCESS_READ:.
e8bd0 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b amode = R_OK
e8be0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a ;. break;..
e8bf0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 default:.
e8c00 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61 assert(!"Inva
e8c10 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 lid flags argume
e8c20 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 nt");. }. *pRe
e8c30 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a sOut = (access(z
e8c40 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29 Path, amode)==0)
e8c50 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
e8c60 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 E_OK;.}.../*.**
e8c70 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 Turn a relative
e8c80 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 pathname into a
e8c90 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54 full pathname. T
e8ca0 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 he relative path
e8cb0 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73 .** is stored as
e8cc0 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 a nul-terminate
e8cd0 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 d string in the
e8ce0 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 buffer pointed t
e8cf0 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a o by.** zPath. .
e8d00 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74 **.** zOut point
e8d10 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 s to a buffer of
e8d20 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65 at least sqlite
e8d30 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 3_vfs.mxPathname
e8d40 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74 bytes .** (in t
e8d50 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41 his case, MAX_PA
e8d60 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54 THNAME bytes). T
e8d70 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20 he full-path is
e8d80 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68 written to.** th
e8d90 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65 is buffer before
e8da0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 returning..*/.s
e8db0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75 tatic int unixFu
e8dc0 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 llPathname(. sq
e8dd0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
e8de0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
e8df0 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 ointer to vfs ob
e8e00 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 ject */. const
e8e10 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 char *zPath,
e8e20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 /* Possi
e8e30 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 bly relative inp
e8e40 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 ut path */. int
e8e50 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 nOut,
e8e60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
e8e70 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 ze of output buf
e8e80 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a fer in bytes */.
e8e90 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20 char *zOut
e8ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
e8eb0 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72 /* Output buffer
e8ec0 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27 */.){.. /* It'
e8ed0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74 s odd to simulat
e8ee0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65 e an io-error he
e8ef0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74 re, but really t
e8f00 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a his is just. **
e8f10 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72 using the io-er
e8f20 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75 ror infrastructu
e8f30 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20 re to test that
e8f40 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74 SQLite handles t
e8f50 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f his. ** functio
e8f60 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20 n failing. This
e8f70 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66 function could f
e8f80 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d ail if, for exam
e8f90 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75 ple, the. ** cu
e8fa0 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69 rrent working di
e8fb0 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e rectory has been
e8fc0 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a unlinked.. */.
e8fd0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
e8fe0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 r( return SQLITE
e8ff0 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73 _ERROR );.. ass
e9000 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74 ert( pVfs->mxPat
e9010 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e hname==MAX_PATHN
e9020 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f AME );. UNUSED_
e9030 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
e9040 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d .. zOut[nOut-1]
e9050 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a = '\0';. if( z
e9060 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a Path[0]=='/' ){.
e9070 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
e9080 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c intf(nOut, zOut,
e9090 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 "%s", zPath);.
e90a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 }else{. int
e90b0 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65 nCwd;. if( ge
e90c0 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d tcwd(zOut, nOut-
e90d0 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 1)==0 ){. r
e90e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e eturn SQLITE_CAN
e90f0 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 TOPEN;. }.
e9100 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72 nCwd = (int)str
e9110 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 len(zOut);. s
e9120 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
e9130 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 nOut-nCwd, &zOut
e9140 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a [nCwd], "/%s", z
e9150 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Path);. }. ret
e9160 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
e9170 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ...#ifndef SQLIT
e9180 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 E_OMIT_LOAD_EXTE
e9190 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 NSION./*.** Inte
e91a0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 rfaces for openi
e91b0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 ng a shared libr
e91c0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 ary, finding ent
e91d0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 ry points.** wit
e91e0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c hin the shared l
e91f0 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 ibrary, and clos
e9200 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c ing the shared l
e9210 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c ibrary..*/.#incl
e9220 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74 ude <dlfcn.h>.st
e9230 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44 atic void *unixD
e9240 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 lOpen(sqlite3_vf
e9250 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73 s *NotUsed, cons
e9260 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d t char *zFilenam
e9270 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e){. UNUSED_PAR
e9280 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
e9290 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e . return dlopen
e92a0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44 (zFilename, RTLD
e92b0 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42 _NOW | RTLD_GLOB
e92c0 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 AL);.}../*.** SQ
e92d0 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 Lite calls this
e92e0 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61 function immedia
e92f0 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c tely after a cal
e9300 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29 l to unixDlSym()
e9310 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65 or.** unixDlOpe
e9320 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 72 n() fails (retur
e9330 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 ns a null pointe
e9340 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65 r). If a more de
e9350 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20 tailed error.**
e9360 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 6c message is avail
e9370 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74 able, it is writ
e9380 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20 ten to zBufOut.
e9390 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 If no error mess
e93a0 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61 age.** is availa
e93b0 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20 ble, zBufOut is
e93c0 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20 left unmodified
e93d0 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 20 and SQLite uses
e93e0 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72 a default.** err
e93f0 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 or message..*/.s
e9400 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 tatic void unixD
e9410 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 lError(sqlite3_v
e9420 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 fs *NotUsed, int
e9430 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 nBuf, char *zBu
e9440 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a fOut){. char *z
e9450 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 Err;. UNUSED_PA
e9460 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
e9470 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 ;. unixEnterMut
e9480 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64 ex();. zErr = d
e9490 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20 lerror();. if(
e94a0 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 zErr ){. sqli
e94b0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 te3_snprintf(nBu
e94c0 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22 f, zBufOut, "%s"
e94d0 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75 , zErr);. }. u
e94e0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b nixLeaveMutex();
e94f0 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28 .}.static void (
e9500 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74 *unixDlSym(sqlit
e9510 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c e3_vfs *NotUsed,
e9520 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20 void *p, const
e9530 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64 char*zSym))(void
e9540 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43 ){. /* . ** GC
e9550 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 C with -pedantic
e9560 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61 -errors says tha
e9570 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61 t C90 does not a
e9580 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20 llow a void* to
e9590 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74 be. ** cast int
e95a0 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 o a pointer to a
e95b0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20 function. And
e95c0 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20 yet the library
e95d0 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a dlsym() routine.
e95e0 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 ** returns a v
e95f0 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65 oid* which is re
e9600 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 ally a pointer t
e9610 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 o a function. S
e9620 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a o how do we. **
e9630 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74 use dlsym() wit
e9640 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f h -pedantic-erro
e9650 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 rs?. **. ** Va
e9660 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69 riable x below i
e9670 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 s defined to be
e9680 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 a pointer to a f
e9690 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20 unction taking.
e96a0 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76 ** parameters v
e96b0 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63 oid* and const c
e96c0 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69 har* and returni
e96d0 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ng a pointer to
e96e0 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a a function.. **
e96f0 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78 We initialize x
e9700 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74 by assigning it
e9710 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
e9720 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69 e dlsym() functi
e9730 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61 on.. ** (That a
e9740 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 ssignment requir
e9750 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65 es a cast.) The
e9760 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75 n we call the fu
e9770 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a nction that. **
e9780 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a x points to. .
e9790 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77 **. ** This w
e97a0 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e ork-around is un
e97b0 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63 likely to work c
e97c0 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20 orrectly on any
e97d0 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a system where. *
e97e0 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e * you really can
e97f0 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74 not cast a funct
e9800 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f ion pointer into
e9810 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65 void*. But the
e9820 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f n, on the. ** o
e9830 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d ther hand, dlsym
e9840 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b () will not work
e9850 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65 on such a syste
e9860 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20 m either, so we
e9870 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 have. ** not re
e9880 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69 ally lost anythi
e9890 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20 ng.. */. void
e98a0 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e (*(*x)(void*,con
e98b0 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 st char*))(void)
e98c0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
e98d0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 ETER(NotUsed);.
e98e0 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28 x = (void(*(*)(
e98f0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 void*,const char
e9900 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b *))(void))dlsym;
e9910 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70 . return (*x)(p
e9920 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69 , zSym);.}.stati
e9930 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f c void unixDlClo
e9940 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a se(sqlite3_vfs *
e9950 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 NotUsed, void *p
e9960 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 Handle){. UNUSE
e9970 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 D_PARAMETER(NotU
e9980 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 sed);. dlclose(
e9990 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 pHandle);.}.#els
e99a0 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f e /* if SQLITE_O
e99b0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
e99c0 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a ON is defined: *
e99d0 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 /. #define unix
e99e0 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66 DlOpen 0. #def
e99f0 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 ine unixDlError
e9a00 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78 0. #define unix
e9a10 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66 DlSym 0. #def
e9a20 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 ine unixDlClose
e9a30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 0.#endif../*.**
e9a40 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73 Write nBuf bytes
e9a50 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 of random data
e9a60 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 to the supplied
e9a70 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a buffer zBuf..*/.
e9a80 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52 static int unixR
e9a90 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 andomness(sqlite
e9aa0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 3_vfs *NotUsed,
e9ab0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
e9ac0 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f zBuf){. UNUSED_
e9ad0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 PARAMETER(NotUse
e9ae0 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69 d);. assert((si
e9af0 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 ze_t)nBuf>=(size
e9b00 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f of(time_t)+sizeo
e9b10 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 f(int)));.. /*
e9b20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69 We have to initi
e9b30 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 alize zBuf to pr
e9b40 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 event valgrind f
e9b50 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 rom reporting.
e9b60 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20 ** errors. The
e9b70 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62 reports issued b
e9b80 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69 y valgrind are i
e9b90 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f ncorrect - we wo
e9ba0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 uld. ** prefer
e9bb0 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e that the randomn
e9bc0 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64 ess be increased
e9bd0 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f by making use o
e9be0 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 f the. ** unini
e9bf0 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69 tialized space i
e9c00 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c n zBuf - but val
e9c10 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e grind errors ten
e9c20 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 d to worry. **
e9c30 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74 some users. Rat
e9c40 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20 her than argue,
e9c50 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20 it seems easier
e9c60 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 just to initiali
e9c70 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c ze. ** the whol
e9c80 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65 e array and sile
e9c90 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 nce valgrind, ev
e9ca0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 en if that means
e9cb0 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 less randomness
e9cc0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e . ** in the ran
e9cd0 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 dom seed.. **.
e9ce0 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 ** When testing
e9cf0 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a , initializing z
e9d00 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 Buf[] to zero is
e9d10 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 all we do. Tha
e9d20 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 t means. ** tha
e9d30 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 t we always use
e9d40 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 the same random
e9d50 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e number sequence.
e9d60 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 This makes the
e9d70 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65 . ** tests repe
e9d80 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d atable.. */. m
e9d90 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e emset(zBuf, 0, n
e9da0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e Buf);.#if !defin
e9db0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a ed(SQLITE_TEST).
e9dc0 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c {. int pid,
e9dd0 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 fd;. fd = op
e9de0 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d en("/dev/urandom
e9df0 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 ", O_RDONLY);.
e9e00 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 if( fd<0 ){.
e9e10 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 time_t t;.
e9e20 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 time(&t);.
e9e30 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c memcpy(zBuf,
e9e40 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b &t, sizeof(t));
e9e50 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74 . pid = get
e9e60 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d pid();. mem
e9e70 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 cpy(&zBuf[sizeof
e9e80 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 (t)], &pid, size
e9e90 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20 of(pid));.
e9ea0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74 assert( sizeof(t
e9eb0 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 )+sizeof(pid)<=(
e9ec0 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 size_t)nBuf );.
e9ed0 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 nBuf = size
e9ee0 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 of(t) + sizeof(p
e9ef0 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a id);. }else{.
e9f00 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61 nBuf = rea
e9f10 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 d(fd, zBuf, nBuf
e9f20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66 );. close(f
e9f30 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 d);. }. }.#e
e9f40 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 ndif. return nB
e9f50 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c uf;.}.../*.** Sl
e9f60 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 eep for a little
e9f70 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 while. Return
e9f80 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 the amount of ti
e9f90 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 me slept..** The
e9fa0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 argument is the
e9fb0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f number of micro
e9fc0 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 seconds we want
e9fd0 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 to sleep..** The
e9fe0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 return value is
e9ff0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d the number of m
ea000 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 icroseconds of s
ea010 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a leep actually.**
ea020 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 requested from
ea030 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f the underlying o
ea040 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c perating system,
ea050 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a a number which.
ea060 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61 ** might be grea
ea070 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 ter than or equa
ea080 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e l to the argumen
ea090 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a t, but not less.
ea0a0 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75 ** than the argu
ea0b0 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ment..*/.static
ea0c0 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71 int unixSleep(sq
ea0d0 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 lite3_vfs *NotUs
ea0e0 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 ed, int microsec
ea0f0 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58 onds){.#if OS_VX
ea100 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 WORKS. struct t
ea110 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 imespec sp;.. s
ea120 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f p.tv_sec = micro
ea130 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 seconds / 100000
ea140 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 0;. sp.tv_nsec
ea150 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 = (microseconds
ea160 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 30 % 1000000) * 100
ea170 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 0;. nanosleep(&
ea180 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e 55 sp, NULL);. UNU
ea190 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f SED_PARAMETER(No
ea1a0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e tUsed);. return
ea1b0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23 microseconds;.#
ea1c0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 elif defined(HAV
ea1d0 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56 E_USLEEP) && HAV
ea1e0 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65 E_USLEEP. uslee
ea1f0 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b p(microseconds);
ea200 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea210 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
ea220 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f return microseco
ea230 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 nds;.#else. int
ea240 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72 seconds = (micr
ea250 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29 oseconds+999999)
ea260 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65 /1000000;. slee
ea270 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e p(seconds);. UN
ea280 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
ea290 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 otUsed);. retur
ea2a0 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30 n seconds*100000
ea2b0 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 0;.#endif.}../*.
ea2c0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 ** The following
ea2d0 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 variable, if se
ea2e0 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 t to a non-zero
ea2f0 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70 value, is interp
ea300 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 reted as.** the
ea310 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64 number of second
ea320 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64 s since 1970 and
ea330 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 is used to set
ea340 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a the result of.**
ea350 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e sqlite3OsCurren
ea360 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74 tTime() during t
ea370 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 esting..*/.#ifde
ea380 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 f SQLITE_TEST.SQ
ea390 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
ea3a0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d ite3_current_tim
ea3b0 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20 e = 0; /* Fake
ea3c0 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73 system time in s
ea3d0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 econds since 197
ea3e0 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0. */.#endif../*
ea3f0 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 .** Find the cur
ea400 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e rent time (in Un
ea410 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 iversal Coordina
ea420 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 ted Time). Writ
ea430 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 e the.** current
ea440 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 time and date a
ea450 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e s a Julian Day n
ea460 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f umber into *prNo
ea470 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 w and.** return
ea480 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 0. Return 1 if
ea490 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 the time and dat
ea4a0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e e cannot be foun
ea4b0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
ea4c0 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65 unixCurrentTime
ea4d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f (sqlite3_vfs *No
ea4e0 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a 70 tUsed, double *p
ea4f0 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 6e rNow){.#if defin
ea500 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ed(SQLITE_OMIT_F
ea510 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 LOATING_POINT).
ea520 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d time_t t;. tim
ea530 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 e(&t);. *prNow
ea540 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 = (((sqlite3_int
ea550 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34 34 64)t)/8640 + 244
ea560 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69 66 05875)/10;.#elif
ea570 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54 defined(NO_GETT
ea580 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a OD). time_t t;.
ea590 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 time(&t);. *p
ea5a0 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30 rNow = t/86400.0
ea5b0 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65 + 2440587.5;.#e
ea5c0 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 lif OS_VXWORKS.
ea5d0 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63 struct timespec
ea5e0 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67 sNow;. clock_g
ea5f0 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41 ettime(CLOCK_REA
ea600 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20 LTIME, &sNow);.
ea610 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38 *prNow = 244058
ea620 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65 7.5 + sNow.tv_se
ea630 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77 c/86400.0 + sNow
ea640 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30 .tv_nsec/8640000
ea650 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65 0000000.0;.#else
ea660 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61 . struct timeva
ea670 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d l sNow;. gettim
ea680 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29 eofday(&sNow, 0)
ea690 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 ;. *prNow = 244
ea6a0 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 0587.5 + sNow.tv
ea6b0 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 _sec/86400.0 + s
ea6c0 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30 Now.tv_usec/8640
ea6d0 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69 0000000.0;.#endi
ea6e0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 f..#ifdef SQLITE
ea6f0 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 _TEST. if( sqli
ea700 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 te3_current_time
ea710 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d ){. *prNow =
ea720 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 sqlite3_current
ea730 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 _time/86400.0 +
ea740 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 2440587.5;. }.#
ea750 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 endif. UNUSED_P
ea760 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 ARAMETER(NotUsed
ea770 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
ea780 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64 ../*.** We added
ea790 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 72 the xGetLastErr
ea7a0 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 or() method with
ea7b0 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f the intention o
ea7c0 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62 f providing.** b
ea7d0 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20 etter low-level
ea7e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77 error messages w
ea7f0 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 hen operating-sy
ea800 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f stem problems co
ea810 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20 me up.** during
ea820 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e SQLite operation
ea830 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e . But so far, n
ea840 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 20 one of that has
ea850 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 been implemented
ea860 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e .** in the core.
ea870 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e So this routin
ea880 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 e is never calle
ea890 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20 d. For now, it
ea8a0 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70 is merely.** a p
ea8b0 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a lace-holder..*/.
ea8c0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47 static int unixG
ea8d0 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 etLastError(sqli
ea8e0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 te3_vfs *NotUsed
ea8f0 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20 , int NotUsed2,
ea900 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b char *NotUsed3){
ea910 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
ea920 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 TER(NotUsed);.
ea930 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
ea940 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e (NotUsed2);. UN
ea950 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e USED_PARAMETER(N
ea960 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75 otUsed3);. retu
ea970 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a rn 0;.}../*.****
ea980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea990 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 **** End of sqli
ea9a0 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 te3_vfs methods
ea9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a ***********.****
ea9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
ea9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a **********/../**
eaa20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a ************.***
eaa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaa80 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72 ******* Begin Pr
eaa90 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a oxy Locking ****
eaaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
eaab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a ************.**.
eaac0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ** Proxy locking
eaad0 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b is a "uber-lock
eaae0 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74 ing-method" in t
eaaf0 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75 his sense: It u
eab00 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72 ses the.** other
eab10 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 locking methods
eab20 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f on secondary lo
eab30 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79 ck files. Proxy
eab40 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a locking is a.**
eab50 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72 meta-layer over
eab60 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d top of the prim
eab70 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d itive locking im
eab80 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e plemented above.
eab90 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65 For.** this re
eaba0 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 ason, the divisi
eabb0 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e on that implemen
eabc0 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b ts of proxy lock
eabd0 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 0a ing is deferred.
eabe0 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e ** until late in
eabf0 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 29 the file (here)
eac00 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 after all of th
eac10 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68 e other I/O meth
eac20 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e ods have.** been
eac30 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68 defined - so th
eac40 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 at the primitive
eac50 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 locking methods
eac60 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a are available.*
eac70 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f * as services to
eac80 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69 help with the i
eac90 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
eaca0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a proxy locking..
eacb0 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 **.****.**.** Th
eacc0 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e e default lockin
eacd0 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c g schemes in SQL
eace0 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e ite use byte-ran
eacf0 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a ge locks on the.
ead00 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 ** database file
ead10 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73 to coordinate s
ead20 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 afe, concurrent
ead30 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 access by multip
ead40 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e le readers.** an
ead50 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a d writers [http:
ead60 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63 //sqlite.org/loc
ead70 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54 kingv3.html]. T
ead80 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63 he five file loc
ead90 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28 king.** states (
eada0 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e UNLOCKED, PENDIN
eadb0 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52 G, SHARED, RESER
eadc0 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20 VED, EXCLUSIVE)
eadd0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a are implemented.
eade0 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 64 ** as POSIX read
eadf0 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f & write locks o
eae00 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f 66 ver fixed set of
eae10 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20 locations (via
eae20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46 fsctl),.** on AF
eae30 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65 P and SMB only e
eae40 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61 xclusive byte-ra
eae50 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76 nge locks are av
eae60 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74 ailable via fsct
eae70 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28 l.** with _IOWR(
eae80 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 'z', 23, struct
eae90 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 ByteRangeLockPB2
eaea0 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73 ) to track the s
eaeb0 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a ame 5 states..**
eaec0 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46 To simulate a F
eaed0 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68 _RDLCK on the sh
eaee0 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41 ared range, on A
eaef0 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65 FP a randomly se
eaf00 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73 lected.** addres
eaf10 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 s in the shared
eaf20 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66 range is taken f
eaf30 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b or a SHARED lock
eaf40 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20 , the entire.**
eaf50 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 shared range is
eaf60 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43 taken for an EXC
eaf70 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a LUSIVE lock):.**
eaf80 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47 .** PENDING
eaf90 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34 _BYTE 0x4
eafa0 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a 0000000.. ..**
eafb0 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42 RESERVED_B
eafc0 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30 YTE 0x4000
eafd0 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41 0001.** SHA
eafe0 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20 RED_RANGE
eaff0 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30 0x40000002 -> 0
eb000 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20 x40000200.**.**
eb010 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20 This works well
eb020 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c on the local fil
eb030 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68 e system, but sh
eb040 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30 ows a nearly 100
eb050 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e x.** slowdown in
eb060 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63 read performanc
eb070 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65 e on AFP because
eb080 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20 the AFP client
eb090 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20 disables.** the
eb0a0 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e 20 read cache when
eb0b0 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 byte-range locks
eb0c0 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45 are present. E
eb0d0 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64 nabling the read
eb0e0 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65 .** cache expose
eb0f0 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 65 s a cache cohere
eb100 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74 ncy problem that
eb110 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61 is present on a
eb120 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f ll OS X.** suppo
eb130 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c rted network fil
eb140 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20 e systems. NFS
eb150 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73 and AFP both obs
eb160 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73 erve the.** clos
eb170 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74 e-to-open semant
eb180 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 ics for ensuring
eb190 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79 cache coherency
eb1a0 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e .** [http://nfs.
eb1b0 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f sourceforge.net/
eb1c0 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20 #faq_a8], which
eb1d0 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69 does not effecti
eb1e0 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20 vely.** address
eb1f0 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 the requirements
eb200 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20 for concurrent
eb210 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 database access
eb220 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72 by multiple.** r
eb230 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65 eaders and write
eb240 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77 rs.** [http://ww
eb250 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c w.nabble.com/SQL
eb260 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65 ite-on-NFS-cache
eb270 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36 -coherency-td156
eb280 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a 55701.html]..**.
eb290 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 68 ** To address th
eb2a0 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e e performance an
eb2b0 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 d cache coherenc
eb2c0 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20 y issues, proxy
eb2d0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 file locking.**
eb2e0 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20 changes the way
eb2f0 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20 database access
eb300 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 is controlled by
eb310 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73 limiting access
eb320 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 to a.** single
eb330 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61 host at a time a
eb340 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c nd moving file l
eb350 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20 ocks off of the
eb360 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a database file.**
eb370 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78 and onto a prox
eb380 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f y file on the lo
eb390 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e cal file system.
eb3a0 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e .**.**.** Usin
eb3b0 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a g proxy locks.**
eb3c0 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ---------------
eb3d0 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a --.**.** C APIs.
eb3e0 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 **.** sqlite3_f
eb3f0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 ile_control(db,
eb400 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53 dbname, SQLITE_S
eb410 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 ET_LOCKPROXYFILE
eb420 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 ,.**
eb430 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78 <prox
eb440 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f y_path> | ":auto
eb450 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 :");.** sqlite3
eb460 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 _file_control(db
eb470 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 , dbname, SQLITE
eb480 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _GET_LOCKPROXYFI
eb490 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68 LE, &<proxy_path
eb4a0 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c >);.**.**.** SQL
eb4b0 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20 pragmas.**.**
eb4c0 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 PRAGMA [database
eb4d0 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c .]lock_proxy_fil
eb4e0 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c e=<proxy_path> |
eb4f0 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47 :auto:.** PRAG
eb500 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f MA [database.]lo
eb510 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a ck_proxy_file.**
eb520 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22 .** Specifying "
eb530 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68 :auto:" means th
eb540 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61 at if there is a
eb550 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68 conch file with
eb560 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68 a matching.** h
eb570 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68 ost ID in it, th
eb580 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20 e proxy path in
eb590 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 the conch file w
eb5a0 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68 ill be used, oth
eb5b0 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78 erwise.** a prox
eb5c0 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20 y path based on
eb5d0 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 20 the user's temp
eb5e0 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66 dir.** (via conf
eb5f0 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55 str(_CS_DARWIN_U
eb600 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e SER_TEMP_DIR,...
eb610 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 )) will be used
eb620 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 and the.** actua
eb630 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d l proxy file nam
eb640 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 e is generated f
eb650 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64 rom the name and
eb660 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 path of the.**
eb670 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
eb680 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a For example:.**.
eb690 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 74 ** For dat
eb6a0 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 65 abase path "/Use
eb6b0 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a rs/me/foo.db" .*
eb6c0 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b * The lock
eb6d0 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c path will be "<
eb6e0 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c tmpdir>/sqlitepl
eb6f0 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66 ocks/_Users_me_f
eb700 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a oo.db:auto:").**
eb710 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20 .** Once a lock
eb720 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75 proxy is configu
eb730 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 61 red for a databa
eb740 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 se connection, i
eb750 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20 t can not.** be
eb760 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72 removed, however
eb770 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 63 it may be switc
eb780 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 hed to a differe
eb790 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 69 nt proxy path vi
eb7a0 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41 a.** the above A
eb7b0 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68 PIs (assuming th
eb7c0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20 e conch file is
eb7d0 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62 not being held b
eb7e0 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e y another.** con
eb7f0 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65 nection or proce
eb800 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48 ss). .**.**.** H
eb810 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 ow proxy locking
eb820 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d works.** ------
eb830 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
eb840 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69 -.**.** Proxy fi
eb850 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65 le locking relie
eb860 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74 s primarily on t
eb870 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e wo new supportin
eb880 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20 g files: .**.**
eb890 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20 * conch file
eb8a0 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20 to limit access
eb8b0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
eb8c0 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 file to a single
eb8d0 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74 host.** at
eb8e0 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20 a time.**.**
eb8f0 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f * proxy file to
eb900 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 20 act as a proxy
eb910 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 79 for the advisory
eb920 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a locks normally.
eb930 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e ** taken on
eb940 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a the database.**
eb950 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69 .** The conch fi
eb960 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72 le - to use a pr
eb970 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65 oxy file, sqlite
eb980 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c must first "hol
eb990 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20 d the conch".**
eb9a0 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c by taking an sql
eb9b0 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64 ite-style shared
eb9c0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e lock on the con
eb9d0 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67 ch file, reading
eb9e0 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 the.** contents
eb9f0 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74 and comparing t
eba00 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65 he host's unique
eba10 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65 host ID (see be
eba20 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a low) and lock.**
eba30 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 69 proxy path agai
eba40 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 73 nst the values s
eba50 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e tored in the con
eba60 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66 ch. The conch f
eba70 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 ile is.** stored
eba80 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72 in the same dir
eba90 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 61 ectory as the da
ebaa0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 tabase file and
ebab0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a the file name.**
ebac0 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 66 is patterned af
ebad0 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65 ter the database
ebae0 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e file name as ".
ebaf0 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63 <databasename>-c
ebb00 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65 onch"..** If the
ebb10 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73 conch file does
ebb20 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69 not exist, or i
ebb30 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20 t's contents do
ebb40 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a not match the.**
ebb50 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20 host ID and/or
ebb60 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e proxy path, then
ebb70 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63 the lock is esc
ebb80 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63 alated to an exc
ebb90 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61 lusive.** lock a
ebba0 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c nd the conch fil
ebbb0 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70 e contents is up
ebbc0 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 68 dated with the h
ebbd0 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79 ost ID and proxy
ebbe0 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65 .** path and the
ebbf0 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61 lock is downgra
ebc00 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 20 ded to a shared
ebc10 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20 lock again. If
ebc20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20 the conch.** is
ebc30 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20 held by another
ebc40 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 20 process (with a
ebc50 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68 shared lock), th
ebc60 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
ebc70 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e .** will fail an
ebc80 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 d SQLITE_BUSY is
ebc90 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
ebca0 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 The proxy file
ebcb0 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 - a single-byte
ebcc0 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c file used for al
ebcd0 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20 l advisory file
ebce0 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c locks.** normall
ebcf0 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64 y taken on the d
ebd00 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20 atabase file.
ebd10 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20 This allows for
ebd20 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20 safe sharing.**
ebd30 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
ebd40 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c file for multipl
ebd50 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72 e readers and wr
ebd60 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d iters on the sam
ebd70 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63 e.** host (the c
ebd80 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 61 onch ensures tha
ebd90 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74 t they all use t
ebda0 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f he same local lo
ebdb0 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20 ck file)..**.**
ebdc0 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72 64 There is a third
ebdd0 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74 file - the host
ebde0 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20 ID file - used
ebdf0 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 20 as a persistent
ebe00 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75 record.** of a u
ebe10 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72 nique identifier
ebe20 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61 for the host, a
ebe30 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65 128-byte unique
ebe40 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a host id file.**
ebe50 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 66 in the path def
ebe60 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 54 ined by the HOST
ebe70 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65 IDPATH macro (de
ebe80 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a fault value is.*
ebe90 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 * /Library/Cache
ebea0 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c s/.com.apple.sql
ebeb0 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e iteConchHostId).
ebec0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e .**.** Requestin
ebed0 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79 g the lock proxy
ebee0 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69 does not immedi
ebef0 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63 ately take the c
ebf00 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f onch, it is.** o
ebf10 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74 nly taken when t
ebf20 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73 74 he first request
ebf30 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73 to lock databas
ebf40 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20 e file is made.
ebf50 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65 .** This matche
ebf60 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20 s the semantics
ebf70 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e of the tradition
ebf80 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76 al locking behav
ebf90 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70 ior, where.** op
ebfa0 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69 ening a connecti
ebfb0 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 on to a database
ebfc0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74 file does not t
ebfd0 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74 ake a lock on it
ebfe0 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20 ..** The shared
ebff0 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e lock and an open
ec000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
ec010 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20 are maintained
ec020 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f until .** the co
ec030 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 nnection to the
ec040 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73 database is clos
ec050 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ed. .**.** The p
ec060 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68 roxy file and th
ec070 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20 e lock file are
ec080 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f never deleted so
ec090 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a they only need.
ec0a0 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 ** to be created
ec0b0 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 the first time
ec0c0 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a they are used..*
ec0d0 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 *.** Configurati
ec0e0 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d on options.** --
ec0f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
ec100 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54 ---.**.** SQLIT
ec110 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c E_PREFER_PROXY_L
ec120 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20 OCKING.**.**
ec130 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 Database file
ec140 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f s accessed on no
ec150 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 n-local file sys
ec160 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20 tems are.**
ec170 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 automatically
ec180 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70 configured for p
ec190 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f roxy locking, lo
ec1a0 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20 ck files are.**
ec1b0 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f named auto
ec1c0 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20 matically using
ec1d0 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61 the same logic a
ec1e0 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d s.** PRAGM
ec1f0 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c A lock_proxy_fil
ec200 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20 e=":auto:".**
ec210 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f .** SQLITE_PRO
ec220 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20 XY_DEBUG.**.**
ec230 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65 Enables the
ec240 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f logging of erro
ec250 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e r messages durin
ec260 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a g host id file.*
ec270 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61 * retrieva
ec280 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a l and creation.*
ec290 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48 *.** HOSTIDPATH
ec2a0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65 .**.** Ove
ec2b0 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 rrides the defau
ec2c0 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 lt host ID file
ec2d0 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a path location.**
ec2e0 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49 .** LOCKPROXYDI
ec2f0 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 R.**.** Ov
ec300 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 errides the defa
ec310 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 73 ult directory us
ec320 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78 ed for lock prox
ec330 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20 y files that.**
ec340 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20 are named
ec350 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69 automatically vi
ec360 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73 a the ":auto:" s
ec370 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51 etting.**.** SQ
ec380 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f LITE_DEFAULT_PRO
ec390 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e XYDIR_PERMISSION
ec3a0 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65 S.**.** Pe
ec3b0 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65 rmissions to use
ec3c0 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 when creating a
ec3d0 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73 directory for s
ec3e0 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20 toring the.**
ec3f0 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 lock proxy f
ec400 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20 iles, only used
ec410 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49 when LOCKPROXYDI
ec420 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a R is not set..**
ec430 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41 .** .** A
ec440 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76 s mentioned abov
ec450 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64 e, when compiled
ec460 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45 with SQLITE_PRE
ec470 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e FER_PROXY_LOCKIN
ec480 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68 G,.** setting th
ec490 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61 e environment va
ec4a0 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f riable SQLITE_FO
ec4b0 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e RCE_PROXY_LOCKIN
ec4c0 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66 G to 1 will.** f
ec4d0 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 orce proxy locki
ec4e0 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f ng to be used fo
ec4f0 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 r every database
ec500 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e file opened, an
ec510 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63 d 0.** will forc
ec520 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78 e automatic prox
ec530 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 y locking to be
ec540 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c disabled for all
ec550 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c database.** fil
ec560 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 61 es (explicity ca
ec570 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 lling the SQLITE
ec580 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 _SET_LOCKPROXYFI
ec590 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20 LE pragma or.**
ec5a0 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74 sqlite_file_cont
ec5b0 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61 rol API is not a
ec5c0 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 ffected by SQLIT
ec5d0 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f E_FORCE_PROXY_LO
ec5e0 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a CKING)..*/../*.*
ec5f0 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 * Proxy locking
ec600 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c is only availabl
ec610 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a e on MacOSX .*/.
ec620 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 #if defined(__AP
ec630 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 PLE__) && SQLITE
ec640 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f _ENABLE_LOCKING_
ec650 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51 STYLE..#ifdef SQ
ec660 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d LITE_TEST./* sim
ec670 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68 ulate multiple h
ec680 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67 osts by creating
ec690 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66 unique hostid f
ec6a0 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c ile paths */.SQL
ec6b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
ec6c0 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d te3_hostid_num =
ec6d0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 0;.#endif../*.*
ec6e0 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69 * The proxyLocki
ec6f0 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68 ngContext has th
ec700 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20 e path and file
ec710 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 74 structures for t
ec720 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e he remote .** an
ec730 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69 d local proxy fi
ec740 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70 les in it.*/.typ
ec750 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f 78 edef struct prox
ec760 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
ec770 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
ec780 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78 ext;.struct prox
ec790 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
ec7a0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f {. unixFile *co
ec7b0 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20 nchFile;
ec7c0 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66 /* Open conch f
ec7d0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63 ile */. char *c
ec7e0 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20 onchFilePath;
ec7f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
ec800 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ec810 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c */. unixFile *l
ec820 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20 ockProxy;
ec830 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20 /* Open proxy
ec840 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63 lock file */. c
ec850 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61 har *lockProxyPa
ec860 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e th; /* N
ec870 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79 ame of the proxy
ec880 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 lock file */.
ec890 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 char *dbPath;
ec8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
ec8b0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e Name of the open
ec8c0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63 file */. int c
ec8d0 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20 onchHeld;
ec8e0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
ec8f0 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20 if the conch is
ec900 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a currently held *
ec910 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63 /. void *oldLoc
ec920 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 kingContext;
ec930 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63 /* Original loc
ec940 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72 kingcontext to r
ec950 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20 estore on close
ec960 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f */. sqlite3_io_
ec970 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 methods const *p
ec980 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f OldMethod; /
ec990 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d * Original I/O m
ec9a0 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65 ethods for close
ec9b0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49 */.};../* HOSTI
ec9c0 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45 DLEN and CONCHLE
ec9d0 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73 N both include s
ec9e0 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 72 pace for the str
ec9f0 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74 ing .** terminat
eca00 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66 ing nul .*/.#def
eca10 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20 ine HOSTIDLEN
eca20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e 128.#defin
eca30 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20 e CONCHLEN
eca40 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b (MAXPATHLEN+
eca50 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66 HOSTIDLEN+1).#if
eca60 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a ndef HOSTIDPATH.
eca70 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50 # define HOSTIDP
eca80 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72 ATH "/Libr
eca90 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e ary/Caches/.com.
ecaa0 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63 apple.sqliteConc
ecab0 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a hHostId".#endif.
ecac0 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20 ./* basically a
ecad0 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64 copy of unixRand
ecae0 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 66 omness with diff
ecaf0 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65 erent.** test be
ecb00 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20 havior built in
ecb10 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
ecb20 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 oxyGenerateHostI
ecb30 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29 D(char *pHostID)
ecb40 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c {. int pid, fd,
ecb50 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 len;. unsigned
ecb60 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e char *key = (un
ecb70 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48 signed char *)pH
ecb80 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73 ostID;. . mems
ecb90 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49 et(key, 0, HOSTI
ecba0 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30 DLEN);. len = 0
ecbb0 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f ;. fd = open("/
ecbc0 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f dev/urandom", O_
ecbd0 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66 RDONLY);. if( f
ecbe0 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20 d>=0 ){. len
ecbf0 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20 = read(fd, key,
ecc00 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20 HOSTIDLEN);.
ecc10 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69 close(fd); /* si
ecc20 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20 lently leak the
ecc30 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a fd if it fails *
ecc40 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20 /. }. if( len
ecc50 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 < HOSTIDLEN ){.
ecc60 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 time_t t;.
ecc70 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d time(&t);. m
ecc80 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73 emcpy(key, &t, s
ecc90 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70 izeof(t));. p
ecca0 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 id = getpid();.
eccb0 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73 memcpy(&key[s
eccc0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c izeof(t)], &pid,
eccd0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 sizeof(pid));.
ecce0 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b }. .#ifdef MAK
eccf0 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a E_PRETTY_HOSTID.
ecd00 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
ecd10 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65 /* filter the
ecd20 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e bytes into prin
ecd30 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 72 table ascii char
ecd40 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74 acters and NUL t
ecd50 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20 erminate */.
ecd60 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31 key[(HOSTIDLEN-1
ecd70 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66 )] = 0x00;. f
ecd80 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54 or( i=0; i<(HOST
ecd90 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b IDLEN-1); i++ ){
ecda0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 . unsigned
ecdb0 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d char pa = key[i]
ecdc0 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28 &0x7F;. if(
ecdd0 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20 pa<0x20 ){.
ecde0 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 key[i] = (ke
ecdf0 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 y[i]&0x80 == 0x8
ece00 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70 0) ? pa+0x40 : p
ece10 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65 a+0x20;. }e
ece20 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46 lse if( pa==0x7F
ece30 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b ){. key[
ece40 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 i] = (key[i]&0x8
ece50 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d 0 == 0x80) ? pa=
ece60 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a 0x20 : pa+0x7E;.
ece70 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
ece80 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 }.#endif. retur
ece90 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
ecea0 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 6f /* writes the ho
eceb0 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 61 st id path to pa
ecec0 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20 th, path should
eced0 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61 be an pre-alloca
ecee0 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69 ted buffer.** wi
ecef0 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20 th enough space
ecf00 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73 for a path .*/.s
ecf10 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79 tatic void proxy
ecf20 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 68 GetHostIDPath(ch
ecf30 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74 ar *path, size_t
ecf40 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79 len){. strlcpy
ecf50 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54 (path, HOSTIDPAT
ecf60 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20 H, len);.#ifdef
ecf70 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 SQLITE_TEST. if
ecf80 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 ( sqlite3_hostid
ecf90 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68 _num>0 ){. ch
ecfa0 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22 ar suffix[2] = "
ecfb0 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30 1";. suffix[0
ecfc0 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20 ] = suffix[0] +
ecfd0 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e sqlite3_hostid_n
ecfe0 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28 um;. strlcat(
ecff0 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65 path, suffix, le
ed000 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 n);. }.#endif.
ed010 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f OSTRACE3("GETHO
ed020 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69 64 STIDPATH %s pid
ed030 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65 =%d\n", path, ge
ed040 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67 tpid());.}../* g
ed050 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 et the host ID f
ed060 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73 rom a sqlite hos
ed070 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20 tid file stored
ed080 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d in the .** user-
ed090 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 72 specific tmp dir
ed0a0 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74 ectory, create t
ed0b0 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f he ID if it's no
ed0c0 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 20 t there already
ed0d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
ed0e0 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 68 roxyGetHostID(ch
ed0f0 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74 ar *pHostID, int
ed100 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74 *pError){. int
ed110 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68 fd;. char path
ed120 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20 [MAXPATHLEN]; .
ed130 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69 size_t len;. i
ed140 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b nt rc=SQLITE_OK;
ed150 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74 .. proxyGetHost
ed160 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58 IDPath(path, MAX
ed170 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74 PATHLEN);. /* t
ed180 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 ry to create the
ed190 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69 host ID file, i
ed1a0 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 f it already exi
ed1b0 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e sts read the con
ed1c0 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20 tents */. fd =
ed1d0 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45 open(path, O_CRE
ed1e0 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58 AT|O_WRONLY|O_EX
ed1f0 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28 CL, 0644);. if(
ed200 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74 fd<0 ){. int
ed210 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20 err=errno;....
ed220 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49 if( err!=EEXI
ed230 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c ST ){.#ifdef SQL
ed240 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 ITE_PROXY_DEBUG
ed250 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 /* set the sqlit
ed260 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 e error message
ed270 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 instead */.
ed280 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c fprintf(stderr,
ed290 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63 "sqlite error c
ed2a0 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20 reating host ID
ed2b0 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a file %s: %s\n",.
ed2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 pa
ed2d0 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72 th, strerror(err
ed2e0 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 ));.#endif.
ed2f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 return SQLITE_P
ed300 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f ERM;. }. /
ed310 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74 * couldn't creat
ed320 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64 e the file, read
ed330 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 it instead */.
ed340 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 fd = open(pat
ed350 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58 h, O_RDONLY|O_EX
ed360 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c CL);. if( fd<
ed370 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 0 ){.#ifdef SQLI
ed380 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f TE_PROXY_DEBUG /
ed390 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 * set the sqlite
ed3a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 error message i
ed3b0 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20 nstead */.
ed3c0 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b int err = errno;
ed3d0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 . fprintf(s
ed3e0 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 tderr, "sqlite e
ed3f0 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73 rror opening hos
ed400 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 t ID file %s: %s
ed410 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
ed420 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f path, strerro
ed430 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a r(err));.#endif.
ed440 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
ed450 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a ITE_PERM;. }.
ed460 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28 len = pread(
ed470 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 fd, pHostID, HOS
ed480 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 TIDLEN, 0);.
ed490 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 if( len<0 ){.
ed4a0 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 *pError = err
ed4b0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 no;. rc = S
ed4c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 QLITE_IOERR_READ
ed4d0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
ed4e0 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b len<HOSTIDLEN ){
ed4f0 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d . *pError =
ed500 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 0;. rc = S
ed510 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 QLITE_IOERR_SHOR
ed520 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20 T_READ;. }.
ed530 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 close(fd); /*
ed540 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 silently leak th
ed550 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 e fd if it fails
ed560 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33 */. OSTRACE3
ed570 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 61 ("GETHOSTID rea
ed580 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 d %s pid=%d\n",
ed590 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28 pHostID, getpid(
ed5a0 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ));. return r
ed5b0 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 c;. }else{.
ed5c0 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 6e /* we're creatin
ed5d0 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 g the host ID fi
ed5e0 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d le (use a random
ed5f0 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73 string of bytes
ed600 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65 ) */. proxyGe
ed610 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f nerateHostID(pHo
ed620 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d stID);. len =
ed630 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73 pwrite(fd, pHos
ed640 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 tID, HOSTIDLEN,
ed650 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 0);. if( len<
ed660 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 0 ){. *pErr
ed670 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 or = errno;.
ed680 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f rc = SQLITE_IO
ed690 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d ERR_WRITE;. }
ed6a0 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53 else if( len<HOS
ed6b0 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20 TIDLEN ){.
ed6c0 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 *pError = 0;.
ed6d0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 rc = SQLITE_I
ed6e0 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 OERR_WRITE;.
ed6f0 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b }. close(fd);
ed700 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 /* silently lea
ed710 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 k the fd if it f
ed720 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 ails */. OSTR
ed730 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 ACE3("GETHOSTID
ed740 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64 wrote %s pid=%d
ed750 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 \n", pHostID, ge
ed760 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 tpid());. ret
ed770 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73 urn rc;. }.}..s
ed780 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47 tatic int proxyG
ed790 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74 etLockPath(const
ed7a0 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 char *dbPath, c
ed7b0 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65 har *lPath, size
ed7c0 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e _t maxLen){. in
ed7d0 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c t len;. int dbL
ed7e0 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69 en;. int i;..#i
ed7f0 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49 fdef LOCKPROXYDI
ed800 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70 R. len = strlcp
ed810 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f y(lPath, LOCKPRO
ed820 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a XYDIR, maxLen);.
ed830 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43 #else.# ifdef _C
ed840 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 S_DARWIN_USER_TE
ed850 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63 MP_DIR. {. c
ed860 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49 onfstr(_CS_DARWI
ed870 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c N_USER_TEMP_DIR,
ed880 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b lPath, maxLen);
ed890 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 . len = strlc
ed8a0 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74 at(lPath, "sqlit
ed8b0 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e eplocks", maxLen
ed8c0 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72 );. if( mkdir
ed8d0 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44 (lPath, SQLITE_D
ed8e0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f EFAULT_PROXYDIR_
ed8f0 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a PERMISSIONS) ){.
ed900 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69 /* if mkdi
ed910 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20 r fails, handle
ed920 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65 as lock file cre
ed930 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f ation failure */
ed940 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45 .# ifdef SQLITE
ed950 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e 74 _DEBUG. int
ed960 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 err = errno;.
ed970 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 if( err!=EEX
ed980 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66 IST ){. f
ed990 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 printf(stderr, "
ed9a0 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 proxyGetLockPath
ed9b0 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20 : mkdir(%s,0%o)
ed9c0 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20 error %d %s\n",
ed9d0 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20 lPath,.
ed9e0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45 SQLITE_DE
ed9f0 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 FAULT_PROXYDIR_P
eda00 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c ERMISSIONS, err,
eda10 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b strerror(err));
eda20 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69 . }.# endi
eda30 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 f. }else{.
eda40 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 OSTRACE3("GET
eda50 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20 LOCKPATH mkdir
eda60 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50 %s pid=%d\n", lP
eda70 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a ath, getpid());.
eda80 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23 }. . }.#
eda90 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74 else. len = st
edaa0 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74 rlcpy(lPath, "/t
edab0 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23 mp/", maxLen);.#
edac0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20 endif.#endif..
edad0 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31 if( lPath[len-1
edae0 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65 ]!='/' ){. le
edaf0 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74 n = strlcat(lPat
edb00 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b h, "/", maxLen);
edb10 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61 . }. . /* tra
edb20 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61 nsform the db pa
edb30 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63 th to a unique c
edb40 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64 ache name */. d
edb50 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c bLen = (int)strl
edb60 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f en(dbPath);. fo
edb70 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20 r( i=0; i<dbLen
edb80 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78 && (i+len+7)<max
edb90 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 Len; i++){. c
edba0 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69 har c = dbPath[i
edbb0 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c ];. lPath[i+l
edbc0 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27 en] = (c=='/')?'
edbd0 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74 _':c;. }. lPat
edbe0 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20 h[i+len]='\0';.
edbf0 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 strlcat(lPath,
edc00 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e ":auto:", maxLen
edc10 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 );. return SQLI
edc20 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
edc30 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 53 Create a new VFS
edc40 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
edc50 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f (stored in memo
edc60 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
edc70 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c .** sqlite3_mall
edc80 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65 oc) and open the
edc90 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74 file named "pat
edca0 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 h" in the file d
edcb0 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a escriptor..**.**
edcc0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 The caller is r
edcd0 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f esponsible not o
edce0 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20 nly for closing
edcf0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 the file descrip
edd00 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20 tor.** but also
edd10 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 for freeing the
edd20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
edd30 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 d with the file
edd40 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73 descriptor..*/.s
edd50 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 tatic int proxyC
edd60 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f reateUnixFile(co
edd70 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20 nst char *path,
edd80 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c unixFile **ppFil
edd90 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 e) {. unixFile
edda0 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c 61 *pNew;. int fla
eddb0 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs = SQLITE_OPEN
eddc0 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45 5f _MAIN_DB|SQLITE_
eddd0 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49 OPEN_CREATE|SQLI
edde0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 TE_OPEN_READWRIT
eddf0 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 E;. int rc = SQ
ede00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74 LITE_OK;. sqlit
ede10 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b e3_vfs dummyVfs;
ede20 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 78 .. pNew = (unix
ede30 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d File *)sqlite3_m
ede40 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e 69 alloc(sizeof(uni
ede50 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 xFile));. if( !
ede60 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 75 pNew ){. retu
ede70 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
ede80 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e . }. memset(pN
ede90 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e ew, 0, sizeof(un
edea0 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a 20 ixFile));.. /*
edeb0 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29 20 Call unixOpen()
edec0 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f 78 to open the prox
eded0 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61 67 y file. The flag
edee0 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69 78 s passed to unix
edef0 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67 67 Open(). ** sugg
edf00 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69 6c est that the fil
edf10 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 69 e being opened i
edf20 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62 61 s a "main databa
edf30 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20 2a se". This is. *
edf40 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 6f * necessary as o
edf50 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73 20 ther file types
edf60 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 do not necessari
edf70 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b 69 ly support locki
edf80 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 62 ng. It. ** is b
edf90 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e 69 etter to use uni
edfa0 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64 20 xOpen() instead
edfb0 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 of opening the f
edfc0 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69 74 ile directly wit
edfd0 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20 61 h. ** open(), a
edfe0 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65 74 s unixOpen() set
edff0 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75 73 s up the various
ee000 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71 75 mechanisms requ
ee010 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61 6b ired to. ** mak
ee020 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74 6f e sure a call to
ee030 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e 6f close() does no
ee040 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73 74 t cause the syst
ee050 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20 20 em to discard.
ee060 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 70 ** POSIX locks p
ee070 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a 2a rematurely.. **
ee080 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f . ** It is impo
ee090 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20 78 rtant that the x
ee0a0 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20 74 Open member of t
ee0b0 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70 61 he VFS object pa
ee0c0 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75 6e ssed to . ** un
ee0d0 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c 4c ixOpen() is NULL
ee0e0 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e 69 . This tells uni
ee0f0 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79 20 xOpen() may try
ee100 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79 2d to open a proxy-
ee110 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20 74 file . ** for t
ee120 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28 63 he proxy-file (c
ee130 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e 74 reating a potent
ee140 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f ial infinite loo
ee150 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d 79 p).. */. dummy
ee160 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 28 Vfs.pAppData = (
ee170 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b 49 void*)&autolockI
ee180 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d 79 oFinder;. dummy
ee190 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a 20 Vfs.xOpen = 0;.
ee1a0 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28 26 rc = unixOpen(&
ee1b0 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c 20 dummyVfs, path,
ee1c0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 (sqlite3_file *)
ee1d0 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66 6c pNew, flags, &fl
ee1e0 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d ags);. if( rc==
ee1f0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66 6c SQLITE_OK && (fl
ee200 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ags&SQLITE_OPEN_
ee210 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20 20 READONLY) ){.
ee220 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d 3e pNew->pMethod->
ee230 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f xClose((sqlite3_
ee240 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20 20 file *)pNew);.
ee250 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 rc = SQLITE_CA
ee260 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 69 NTOPEN;. }.. i
ee270 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
ee280 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
ee290 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 free(pNew);.
ee2a0 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 pNew = 0;. }..
ee2b0 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77 3b *ppFile = pNew;
ee2c0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
ee2d0 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 6f ./* takes the co
ee2e0 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 20 nch by taking a
ee2f0 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 shared lock and
ee300 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 read the content
ee310 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a 20 s conch, if .**
ee320 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e 2d lockPath is non-
ee330 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 49 NULL, the host I
ee340 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20 D and lock file
ee350 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 2e path must match.
ee360 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f 63 A NULL .** loc
ee370 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 74 kPath means that
ee380 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 6e the lockPath in
ee390 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ee3a0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 20 will be used if
ee3b0 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 73 the .** host IDs
ee3c0 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 77 match, or a new
ee3d0 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 lock path will
ee3e0 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 74 be generated aut
ee3f0 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 61 omatically .** a
ee400 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 nd written to th
ee410 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2f e conch file..*/
ee420 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 .static int prox
ee430 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 46 yTakeConch(unixF
ee440 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 70 ile *pFile){. p
ee450 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
ee460 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
ee470 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
ee480 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
ee490 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 69 Context; . . i
ee4a0 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 f( pCtx->conchHe
ee4b0 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 ld>0 ){. retu
ee4c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
ee4d0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 }else{. unixF
ee4e0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d ile *conchFile =
ee4f0 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 pCtx->conchFile
ee500 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 56 ;. char testV
ee510 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a alue[CONCHLEN];.
ee520 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 61 char conchVa
ee530 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20 lue[CONCHLEN];.
ee540 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 68 char lockPath
ee550 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20 [MAXPATHLEN];.
ee560 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 74 char *tLockPat
ee570 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e h = NULL;. in
ee580 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
ee590 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 63 ;. int readRc
ee5a0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
ee5b0 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 20 int syncPerms
ee5c0 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 43 = 0;.. OSTRAC
ee5d0 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 E4("TAKECONCH %
ee5e0 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c d for %s pid=%d\
ee5f0 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 n", conchFile->h
ee600 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 ,. (
ee610 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 pCtx->lockProxyP
ee620 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b ath ? pCtx->lock
ee630 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75 ProxyPath : ":au
ee640 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29 to:"), getpid())
ee650 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 ;.. rc = conc
ee660 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e hFile->pMethod->
ee670 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 xLock((sqlite3_f
ee680 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 ile*)conchFile,
ee690 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
ee6a0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
ee6b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 _OK ){. int
ee6c0 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 pError = 0;.
ee6d0 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 61 memset(testVa
ee6e0 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 4e lue, 0, CONCHLEN
ee6f0 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 66 ); /* conch is f
ee700 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 20 ixed size */.
ee710 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 74 rc = proxyGet
ee720 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 65 HostID(testValue
ee730 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 20 , &pError);.
ee740 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d if( (rc&0xff)=
ee750 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b =SQLITE_IOERR ){
ee760 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e . pFile->
ee770 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 72 lastErrno = pErr
ee780 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 or;. }.
ee790 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b if( pCtx->lock
ee7a0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 ProxyPath ){.
ee7b0 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 65 strlcpy(&te
ee7c0 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 stValue[HOSTIDLE
ee7d0 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 N], pCtx->lockPr
ee7e0 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 oxyPath, MAXPATH
ee7f0 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 LEN);. }.
ee800 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
ee810 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
ee820 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 goto end_take
ee830 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20 conch;. }.
ee840 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 75 . readRc = u
ee850 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 33 nixRead((sqlite3
ee860 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c _file *)conchFil
ee870 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 e, conchValue, C
ee880 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 ONCHLEN, 0);.
ee890 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c if( readRc!=SQL
ee8a0 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f ITE_IOERR_SHORT_
ee8b0 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 66 READ ){. if
ee8c0 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 45 ( readRc!=SQLITE
ee8d0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 _OK ){. i
ee8e0 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 f( (rc&0xff)==SQ
ee8f0 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 LITE_IOERR ){.
ee900 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c pFile->l
ee910 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 68 astErrno = conch
ee920 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b File->lastErrno;
ee930 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
ee940 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b 0a rc = readRc;.
ee950 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
ee960 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 _takeconch;.
ee970 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 }. /* if
ee980 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 61 the conch has da
ee990 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63 ta compare the c
ee9a0 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 ontents */.
ee9b0 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b if( !pCtx->lock
ee9c0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20 ProxyPath ){.
ee9d0 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f /* for auto
ee9e0 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63 -named local loc
ee9f0 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 65 k file, just che
eea00 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 61 ck the host ID a
eea10 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 20 nd we'll.
eea20 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f 63 ** use the loc
eea30 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 al lock file pat
eea40 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 79 h that's already
eea50 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 20 in there */.
eea60 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d 70 if( !memcmp
eea70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63 (testValue, conc
eea80 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c 45 hValue, HOSTIDLE
eea90 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 N) ){.
eeaa0 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 61 tLockPath = (cha
eeab0 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 5b r *)&conchValue[
eeac0 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 20 HOSTIDLEN];.
eead0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 goto end_t
eeae0 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 akeconch;.
eeaf0 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
eeb00 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 76 . /* we'v
eeb10 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 20 e got the conch
eeb20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d 61 if conchValue ma
eeb30 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 61 tches our path a
eeb40 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 20 nd host ID */.
eeb50 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d if( !memcm
eeb60 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e p(testValue, con
eeb70 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45 chValue, CONCHLE
eeb80 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 N) ){.
eeb90 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e goto end_takecon
eeba0 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 ch;. }.
eebb0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b }. }else{
eebc0 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f 72 . /* a shor
eebd0 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 27 t read means we'
eebe0 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 68 re "creating" th
eebf0 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 68 e conch (even th
eec00 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a 20 ough it could .
eec10 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 65 ** have bee
eec20 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e 74 n user-intervent
eec30 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 75 ion), if we acqu
eec40 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 76 ire the exclusiv
eec50 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a 2a e lock,. **
eec60 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d 61 we'll try to ma
eec70 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 tch the current
eec80 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 69 on-disk permissi
eec90 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 ons of the datab
eeca0 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ase. */.
eecb0 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 31 syncPerms = 1
eecc0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 ;. }. .
eecd0 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 68 /* either conch
eece0 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 69 was emtpy or di
eecf0 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 dn't match */.
eed00 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 if( !pCtx->loc
eed10 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
eed20 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b proxyGetLock
eed30 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 74 Path(pCtx->dbPat
eed40 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 h, lockPath, MAX
eed50 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 PATHLEN);.
eed60 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 6b tLockPath = lock
eed70 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 6c Path;. strl
eed80 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48 cpy(&testValue[H
eed90 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b 50 OSTIDLEN], lockP
eeda0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
eedb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 ;. }. .
eedc0 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 68 /* update conch
eedd0 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 70 with host and p
eede0 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 66 ath (this will f
eedf0 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 6f ail if other pro
eee00 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 73 cess. ** has
eee10 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 a shared lock a
eee20 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 72 lready) */. r
eee30 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 c = conchFile->p
eee40 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 Method->xLock((s
eee50 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
eee60 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 56 chFile, EXCLUSIV
eee70 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 E_LOCK);. if(
eee80 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
eee90 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69 {. rc = uni
eeea0 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 5f xWrite((sqlite3_
eeeb0 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65 file *)conchFile
eeec0 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f 4e , testValue, CON
eeed0 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20 CHLEN, 0);.
eeee0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
eeef0 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 20 OK && syncPerms
eef00 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 ){. struc
eef10 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20 t stat buf;.
eef20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 73 int err = fs
eef30 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62 tat(pFile->h, &b
eef40 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 uf);. if(
eef50 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 err==0 ){.
eef60 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 6d /* try to m
eef70 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 73 atch the databas
eef80 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 6f e file permissio
eef90 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c 75 ns, ignore failu
eefa0 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 re */.#ifndef SQ
eefb0 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 LITE_PROXY_DEBUG
eefc0 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d 6f . fchmo
eefd0 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 d(conchFile->h,
eefe0 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 65 buf.st_mode);.#e
eeff0 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 66 lse. if
ef000 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 ( fchmod(conchFi
ef010 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f le->h, buf.st_mo
ef020 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 de)!=0 ){.
ef030 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d int code =
ef040 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 errno;.
ef050 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 fprintf(stde
ef060 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 46 rr, "fchmod %o F
ef070 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 73 AILED with %d %s
ef080 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
ef090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef0a0 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 63 buf.st_mode, c
ef0b0 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f ode, strerror(co
ef0c0 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 de));.
ef0d0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 } else {.
ef0e0 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 fprintf(std
ef0f0 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 err, "fchmod %o
ef100 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 2e SUCCEDED\n",buf.
ef110 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 20 st_mode);.
ef120 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 }. }e
ef130 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 lse{. i
ef140 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b nt code = errno;
ef150 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e . fprin
ef160 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 54 tf(stderr, "STAT
ef170 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 68 FAILED[%d] with
ef180 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20 %d %s\n", .
ef190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef1a0 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 2c err, code,
ef1b0 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 29 strerror(code))
ef1c0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 ;.#endif.
ef1d0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
ef1e0 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e . conchFile->
ef1f0 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b pMethod->xUnlock
ef200 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
ef210 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45 conchFile, SHARE
ef220 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 5f D_LOCK);. .end_
ef230 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 4f takeconch:. O
ef240 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 52 STRACE2("TRANSPR
ef250 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e OXY: CLOSE %d\n
ef260 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 ", pFile->h);.
ef270 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
ef280 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f 70 _OK && pFile->op
ef290 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 enFlags ){.
ef2a0 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30 if( pFile->h>=0
ef2b0 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 43 ){.#ifdef STRIC
ef2c0 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 20 T_CLOSE_ERROR.
ef2d0 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 if( close(
ef2e0 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 pFile->h) ){.
ef2f0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 pFile->la
ef300 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b stErrno = errno;
ef310 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 . retur
ef320 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 n SQLITE_IOERR_C
ef330 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a LOSE;. }.
ef340 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 6c #else. cl
ef350 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 2f ose(pFile->h); /
ef360 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 * silently leak
ef370 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 65 fd if fail */.#e
ef380 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 ndif. }.
ef390 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 pFile->h = -1
ef3a0 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 3d ;. int fd =
ef3b0 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 61 open(pCtx->dbPa
ef3c0 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46 th, pFile->openF
ef3d0 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 lags,.
ef3e0 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 SQLITE
ef3f0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 _DEFAULT_FILE_PE
ef400 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 RMISSIONS);.
ef410 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e OSTRACE2("TRAN
ef420 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64 SPROXY: OPEN %d
ef430 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 20 \n", fd);.
ef440 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 if( fd>=0 ){.
ef450 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 pFile->h =
ef460 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b fd;. }else{
ef470 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c 49 . rc=SQLI
ef480 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a 20 TE_CANTOPEN; /*
ef490 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 6f SQLITE_BUSY? pro
ef4a0 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c xyTakeConch call
ef4b0 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ed.
ef4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef4d0 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67 during locking
ef4e0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 */. }.
ef4f0 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 }. if( rc==SQ
ef500 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 78 LITE_OK && !pCtx
ef510 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 ->lockProxy ){.
ef520 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 20 char *path
ef530 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 4c = tLockPath ? tL
ef540 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e ockPath : pCtx->
ef550 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 lockProxyPath;.
ef560 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 65 /* ACS: Nee
ef570 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 d to make a copy
ef580 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 6d of path sometim
ef590 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d es */. rc =
ef5a0 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 proxyCreateUnix
ef5b0 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 78 File(path, &pCtx
ef5c0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 ->lockProxy);.
ef5d0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d }. if( rc==
ef5e0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
ef5f0 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 pCtx->conchHe
ef600 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69 ld = 1;.. i
ef610 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b 0a f( tLockPath ){.
ef620 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f pCtx->lo
ef630 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71 ckProxyPath = sq
ef640 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c lite3DbStrDup(0,
ef650 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 20 tLockPath);.
ef660 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c if( pCtx->l
ef670 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f ockProxy->pMetho
ef680 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f d == &afpIoMetho
ef690 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ds ){.
ef6a0 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 ((afpLockingCont
ef6b0 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b ext *)pCtx->lock
ef6c0 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f Proxy->lockingCo
ef6d0 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 3d ntext)->dbPath =
ef6e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
ef6f0 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b pCtx->lock
ef700 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 ProxyPath;.
ef710 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
ef720 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 } else {.
ef730 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 conchFile->pMeth
ef740 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c od->xUnlock((sql
ef750 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 ite3_file*)conch
ef760 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a File, NO_LOCK);.
ef770 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 }. OSTRAC
ef780 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25 E3("TAKECONCH %
ef790 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 d %s\n", conchFi
ef7a0 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 54 le->h, rc==SQLIT
ef7b0 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c 65 E_OK?"ok":"faile
ef7c0 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 d");. return
ef7d0 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a rc;. }.}../*.**
ef7e0 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 20 If pFile holds
ef7f0 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e 63 a lock on a conc
ef800 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 6c h file, then rel
ef810 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e 0a ease that lock..
ef820 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 */.static int pr
ef830 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 oxyReleaseConch(
ef840 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 unixFile *pFile)
ef850 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
ef860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ef870 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 65 /* Subroutine re
ef880 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 70 turn code */. p
ef890 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
ef8a0 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 68 xt *pCtx; /* Th
ef8b0 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 e locking contex
ef8c0 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20 t for the proxy
ef8d0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 69 lock */. unixFi
ef8e0 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 le *conchFile;
ef8f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
ef900 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 the conch file
ef910 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 72 */.. pCtx = (pr
ef920 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 oxyLockingContex
ef930 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 t *)pFile->locki
ef940 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f 6e ngContext;. con
ef950 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 chFile = pCtx->c
ef960 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 52 onchFile;. OSTR
ef970 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f 4e ACE4("RELEASECON
ef980 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 CH %d for %s pi
ef990 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 d=%d\n", conchFi
ef9a0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 le->h,.
ef9b0 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f (pCtx->lockPro
ef9c0 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c xyPath ? pCtx->l
ef9d0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22 ockProxyPath : "
ef9e0 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 20 :auto:"), .
ef9f0 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 3b getpid());
efa00 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 . pCtx->conchHe
efa10 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 63 ld = 0;. rc = c
efa20 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f onchFile->pMetho
efa30 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 d->xUnlock((sqli
efa40 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 te3_file*)conchF
efa50 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 ile, NO_LOCK);.
efa60 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 41 OSTRACE3("RELEA
efa70 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e SECONCH %d %s\n
efa80 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c ", conchFile->h,
efa90 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d . (rc=
efaa0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b =SQLITE_OK ? "ok
efab0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a " : "failed"));.
efac0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
efad0 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 /*.** Given the
efae0 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 name of a databa
efaf0 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 65 se file, compute
efb00 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 73 the name of its
efb10 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20 conch file..**
efb20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 20 Store the conch
efb30 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f filename in memo
efb40 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d ry obtained from
efb50 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 sqlite3_malloc(
efb60 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e )..** Make *pCon
efb70 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f 20 chPath point to
efb80 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 52 the new name. R
efb90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 eturn SQLITE_OK
efba0 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 on success.** or
efbb0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
efbc0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 unable to obtai
efbd0 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 n memory..**.**
efbe0 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 The caller is re
efbf0 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e sponsible for en
efc00 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 20 suring that the
efc10 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 allocated memory
efc20 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 65 .** space is eve
efc30 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a ntually freed..*
efc40 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 68 *.** *pConchPath
efc50 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 is set to NULL
efc60 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f if a memory allo
efc70 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 cation error occ
efc80 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 urs..*/.static i
efc90 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f nt proxyCreateCo
efca0 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 72 nchPathname(char
efcb0 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a *dbPath, char *
efcc0 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 20 *pConchPath){.
efcd0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 int i;
efce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
efcf0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
efd00 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 6e . int len = (in
efd10 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 29 t)strlen(dbPath)
efd20 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 64 ; /* Length of d
efd30 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 atabase filename
efd40 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 63 - dbPath */. c
efd50 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b 20 har *conchPath;
efd60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
efd70 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 20 buffer in which
efd80 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f 6e to construct con
efd90 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a ch name */.. /*
efda0 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 Allocate space
efdb0 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 69 for the conch fi
efdc0 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 69 lename and initi
efdd0 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 74 alize the name t
efde0 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 o. ** the name
efdf0 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 of the original
efe00 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a database file. *
efe10 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 74 / . *pConchPat
efe20 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d 20 h = conchPath =
efe30 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f (char *)sqlite3_
efe40 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 3b malloc(len + 8);
efe50 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68 . if( conchPath
efe60 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
efe70 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
efe80 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e }. memcpy(con
efe90 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c 20 chPath, dbPath,
efea0 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a 20 len+1);. . /*
efeb0 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e 22 now insert a "."
efec0 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74 before the last
efed0 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a / character */.
efee0 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 29 for( i=(len-1)
efef0 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20 ; i>=0; i-- ){.
eff00 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68 if( conchPath
eff10 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 [i]=='/' ){.
eff20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65 i++;. bre
eff30 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
eff40 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e 27 conchPath[i]='.'
eff50 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c 65 ;. while ( i<le
eff60 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 61 n ){. conchPa
eff70 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b 69 th[i+1]=dbPath[i
eff80 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a ];. i++;. }.
eff90 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 65 . /* append the
effa0 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 78 "-conch" suffix
effb0 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a to the file */.
effc0 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 50 memcpy(&conchP
effd0 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63 ath[i+1], "-conc
effe0 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 74 h", 7);. assert
efff0 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f ( (int)strlen(co
f0000 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b nchPath) == len+
f0010 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 7 );.. return S
f0020 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a QLITE_OK;.}.../*
f0030 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 63 Takes a fully c
f0040 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 20 onfigured proxy
f0050 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 6e locking-style un
f0060 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 74 ix file and swit
f0070 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 61 ches.** the loca
f0080 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68 l lock file path
f0090 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 .*/.static int
f00a0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 switchLockProxyP
f00b0 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 ath(unixFile *pF
f00c0 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 ile, const char
f00d0 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 *path) {. proxy
f00e0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a LockingContext *
f00f0 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 pCtx = (proxyLoc
f0100 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 kingContext*)pFi
f0110 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f0120 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 50 xt;. char *oldP
f0130 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b ath = pCtx->lock
f0140 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e 74 ProxyPath;. int
f0150 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
f0160 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c .. if( pFile->l
f0170 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
f0180 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
f0190 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20 QLITE_BUSY;. }
f01a0 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 .. /* nothing
f01b0 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 74 to do if the pat
f01c0 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f h is NULL, :auto
f01d0 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 65 : or matches the
f01e0 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 2a existing path *
f01f0 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c /. if( !path ||
f0200 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c path[0]=='\0' |
f0210 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 | !strcmp(path,
f0220 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20 ":auto:") ||.
f0230 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 74 (oldPath && !st
f0240 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70 rncmp(oldPath, p
f0250 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 ath, MAXPATHLEN)
f0260 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
f0270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c SQLITE_OK;. }el
f0280 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 se{. unixFile
f0290 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 *lockProxy = pC
f02a0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 tx->lockProxy;.
f02b0 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f pCtx->lockPro
f02c0 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74 xy=NULL;. pCt
f02d0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30 x->conchHeld = 0
f02e0 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 72 ;. if( lockPr
f02f0 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 oxy!=NULL ){.
f0300 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d rc=lockProxy-
f0310 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 >pMethod->xClose
f0320 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a ((sqlite3_file *
f0330 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 )lockProxy);.
f0340 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 if( rc ) retu
f0350 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c rn rc;. sql
f0360 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72 ite3_free(lockPr
f0370 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 oxy);. }.
f0380 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c 64 sqlite3_free(old
f0390 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d Path);. pCtx-
f03a0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d >lockProxyPath =
f03b0 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 sqlite3DbStrDup
f03c0 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20 (0, path);. }.
f03d0 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d . return rc;.}
f03e0 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73 ../*.** pFile is
f03f0 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 a file that has
f0400 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 been opened by
f0410 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 61 a prior xOpen ca
f0420 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 69 ll. dbPath.** i
f0430 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65 s a string buffe
f0440 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 41 r at least MAXPA
f0450 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 65 THLEN+1 characte
f0460 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a rs in size..**.*
f0470 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66 * This routine f
f0480 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 ind the filename
f0490 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
f04a0 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 65 pFile and write
f04b0 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61 s it.** int dbPa
f04c0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e th..*/.static in
f04d0 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 68 t proxyGetDbPath
f04e0 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 ForUnixFile(unix
f04f0 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61 File *pFile, cha
f0500 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 20 r *dbPath){.#if
f0510 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f defined(__APPLE_
f0520 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e _). if( pFile->
f0530 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 pMethod == &afpI
f0540 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 oMethods ){.
f0550 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 65 /* afp style kee
f0560 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 ps a reference t
f0570 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 6e o the db path in
f0580 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 69 the filePath fi
f0590 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74 eld . ** of t
f05a0 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 20 he struct */.
f05b0 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 74 assert( (int)st
f05c0 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c rlen((char*)pFil
f05d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f05e0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 t)<=MAXPATHLEN )
f05f0 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 ;. strcpy(dbP
f0600 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e ath, ((afpLockin
f0610 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 gContext *)pFile
f0620 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f0630 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d 65 )->dbPath);. }e
f0640 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 lse.#endif. if(
f0650 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 pFile->pMethod
f0660 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 == &dotlockIoMet
f0670 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 64 hods ){. /* d
f0680 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 73 ot lock style us
f0690 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 es the locking c
f06a0 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 ontext to store
f06b0 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 20 the dot lock.
f06c0 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a 2f ** file path */
f06d0 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 73 . int len = s
f06e0 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 46 trlen((char *)pF
f06f0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 ile->lockingCont
f0700 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 4f ext) - strlen(DO
f0710 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a 20 TLOCK_SUFFIX);.
f0720 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 68 memcpy(dbPath
f0730 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d , (char *)pFile-
f0740 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c >lockingContext,
f0750 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 6c len + 1);. }el
f0760 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6f se{. /* all o
f0770 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 20 ther styles use
f0780 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 the locking cont
f0790 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 ext to store the
f07a0 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a 2f db file path */
f07b0 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72 . assert( str
f07c0 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65 len((char*)pFile
f07d0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f07e0 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b )<=MAXPATHLEN );
f07f0 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 61 . strcpy(dbPa
f0800 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c th, (char *)pFil
f0810 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f0820 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e t);. }. return
f0830 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
f0840 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c *.** Takes an al
f0850 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e 20 ready filled in
f0860 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 6c unix file and al
f0870 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 66 ters it so all f
f0880 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 ile locking .**
f0890 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d 65 will be performe
f08a0 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 d on the local p
f08b0 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e 20 roxy lock file.
f08c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 The following f
f08d0 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 65 ields.** are pre
f08e0 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c 6f served in the lo
f08f0 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 6f cking context so
f0900 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 62 that they can b
f0910 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 0a e restored and .
f0920 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 75 ** the unix stru
f0930 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 63 cture properly c
f0940 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c 6f leaned up at clo
f0950 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e 6c se time:.** ->l
f0960 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a 2a ockingContext.**
f0970 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a 73 ->pMethod.*/.s
f0980 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54 tatic int proxyT
f0990 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 ransformUnixFile
f09a0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 (unixFile *pFile
f09b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 , const char *pa
f09c0 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 th) {. proxyLoc
f09d0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 kingContext *pCt
f09e0 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 68 x;. char dbPath
f09f0 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b 20 [MAXPATHLEN+1];
f0a00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 /* Name of
f0a10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
f0a20 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f le */. char *lo
f0a30 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 69 ckPath=NULL;. i
f0a40 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
f0a50 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 6c K;. . if( pFil
f0a60 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f e->locktype!=NO_
f0a70 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 LOCK ){. retu
f0a80 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a rn SQLITE_BUSY;.
f0a90 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 62 }. proxyGetDb
f0aa0 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 PathForUnixFile(
f0ab0 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b 0a pFile, dbPath);.
f0ac0 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70 if( !path || p
f0ad0 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20 ath[0]=='\0' ||
f0ae0 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a !strcmp(path, ":
f0af0 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 6c auto:") ){. l
f0b00 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 ockPath=NULL;.
f0b10 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 50 }else{. lockP
f0b20 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 68 ath=(char *)path
f0b30 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 ;. }. . OSTRA
f0b40 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 20 CE4("TRANSPROXY
f0b50 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 %d for %s pid=%
f0b60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a d\n", pFile->h,.
f0b70 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 6b (lock
f0b80 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 20 Path ? lockPath
f0b90 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 : ":auto:"), get
f0ba0 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 20 pid());.. pCtx
f0bb0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
f0bc0 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 ( sizeof(*pCtx)
f0bd0 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d 30 );. if( pCtx==0
f0be0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
f0bf0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
f0c00 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c 20 . memset(pCtx,
f0c10 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 0, sizeof(*pCtx)
f0c20 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 79 );.. rc = proxy
f0c30 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e CreateConchPathn
f0c40 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 74 ame(dbPath, &pCt
f0c50 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 x->conchFilePath
f0c60 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
f0c70 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 ITE_OK ){. rc
f0c80 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e = proxyCreateUn
f0c90 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f 6e ixFile(pCtx->con
f0ca0 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 74 chFilePath, &pCt
f0cb0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 x->conchFile);.
f0cc0 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 } . if( rc==S
f0cd0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b QLITE_OK && lock
f0ce0 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 78 Path ){. pCtx
f0cf0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 ->lockProxyPath
f0d00 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
f0d10 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b 0a p(0, lockPath);.
f0d20 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 }.. if( rc==S
f0d30 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
f0d40 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73 /* all memory is
f0d50 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78 allocated, prox
f0d60 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 61 ys are created a
f0d70 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 20 nd assigned, .
f0d80 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 20 ** switch the
f0d90 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 locking context
f0da0 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e and pMethod then
f0db0 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a return.. */.
f0dc0 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 68 pCtx->dbPath
f0dd0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
f0de0 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a 20 up(0, dbPath);.
f0df0 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b pCtx->oldLock
f0e00 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 69 ingContext = pFi
f0e10 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 le->lockingConte
f0e20 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c xt;. pFile->l
f0e30 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 ockingContext =
f0e40 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d 3e pCtx;. pCtx->
f0e50 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 69 pOldMethod = pFi
f0e60 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 20 le->pMethod;.
f0e70 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 pFile->pMethod
f0e80 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 = &proxyIoMethod
f0e90 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 s;. }else{.
f0ea0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 if( pCtx->conchF
f0eb0 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 63 ile ){ . rc
f0ec0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 = pCtx->conchFi
f0ed0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c le->pMethod->xCl
f0ee0 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c ose((sqlite3_fil
f0ef0 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 46 e *)pCtx->conchF
f0f00 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 ile);. if(
f0f10 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f0f20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 sqlite3_fr
f0f30 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 ee(pCtx->conchFi
f0f40 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 le);. }. s
f0f50 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f0f60 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29 ->conchFilePath)
f0f70 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 ; . sqlite3_f
f0f80 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 ree(pCtx);. }.
f0f90 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e 53 OSTRACE3("TRANS
f0fa0 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 2c PROXY %d %s\n",
f0fb0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 pFile->h,.
f0fc0 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54 (rc==SQLIT
f0fd0 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 E_OK ? "ok" : "f
f0fe0 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75 ailed"));. retu
f0ff0 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a rn rc;.}.../*.**
f1000 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 This routine ha
f1010 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 69 ndles sqlite3_fi
f1020 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c le_control() cal
f1030 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 63 ls that are spec
f1040 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79 ific.** to proxy
f1050 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 locking..*/.sta
f1060 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c tic int proxyFil
f1070 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 eControl(sqlite3
f1080 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f _file *id, int o
f1090 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a p, void *pArg){.
f10a0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a switch( op ){.
f10b0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
f10c0 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c GET_LOCKPROXYFIL
f10d0 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 E: {. unixF
f10e0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e ile *pFile = (un
f10f0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 ixFile*)id;.
f1100 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 if( pFile->pMe
f1110 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f thod == &proxyIo
f1120 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 Methods ){.
f1130 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 proxyLockingC
f1140 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 ontext *pCtx = (
f1150 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 proxyLockingCont
f1160 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b ext*)pFile->lock
f1170 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 ingContext;.
f1180 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e proxyTakeCon
f1190 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 ch(pFile);.
f11a0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 if( pCtx->loc
f11b0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 kProxyPath ){.
f11c0 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20 *(const
f11d0 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 70 char **)pArg = p
f11e0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 Ctx->lockProxyPa
f11f0 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 th;. }els
f1200 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 e{. *(c
f1210 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 onst char **)pAr
f1220 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74 g = ":auto: (not
f1230 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 20 held)";.
f1240 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 }. } else
f1250 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 {. *(cons
f1260 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d t char **)pArg =
f1270 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 NULL;. }.
f1280 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
f1290 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 TE_OK;. }.
f12a0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 case SQLITE_SET
f12b0 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20 _LOCKPROXYFILE:
f12c0 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65 {. unixFile
f12d0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 *pFile = (unixF
f12e0 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69 ile*)id;. i
f12f0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
f1300 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50 K;. int isP
f1310 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 69 roxyStyle = (pFi
f1320 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 le->pMethod == &
f1330 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b proxyIoMethods);
f1340 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 3d . if( pArg=
f1350 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20 =NULL || (const
f1360 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 29 char *)pArg==0 )
f1370 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73 {. if( is
f1380 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20 ProxyStyle ){.
f1390 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20 /* turn
f13a0 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e off proxy lockin
f13b0 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 65 g - not supporte
f13c0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 d */. r
f13d0 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
f13e0 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 /*SQLITE_PROTOC
f13f0 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 OL? SQLITE_MISUS
f1400 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65 E?*/;. }e
f1410 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f lse{. /
f1420 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 * turn off proxy
f1430 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61 locking - alrea
f1440 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f dy off - NOOP */
f1450 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
f1460 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 SQLITE_OK;.
f1470 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
f1480 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
f1490 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 20 char *proxyPath
f14a0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 = (const char *)
f14b0 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 66 pArg;. if
f14c0 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 ( isProxyStyle )
f14d0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f 78 {. prox
f14e0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f14f0 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 20 *pCtx = .
f1500 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69 (proxyLocki
f1510 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 ngContext*)pFile
f1520 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 ->lockingContext
f1530 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
f1540 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 3a !strcmp(pArg, ":
f1550 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 20 auto:") .
f1560 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f || (pCtx->lo
f1570 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a 20 ckProxyPath &&.
f1580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 73 !s
f1590 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63 trncmp(pCtx->loc
f15a0 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f 78 kProxyPath, prox
f15b0 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 yPath, MAXPATHLE
f15c0 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b N)). ){
f15d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
f15e0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
f15f0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
f1600 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
f1610 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61 witchLockProxyPa
f1620 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 th(pFile, proxyP
f1630 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ath);.
f1640 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
f1650 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75 . /* tu
f1660 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65 rn on proxy file
f1670 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 locking */.
f1680 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 rc = proxy
f1690 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c TransformUnixFil
f16a0 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61 e(pFile, proxyPa
f16b0 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 th);. }.
f16c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 }. ret
f16d0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
f16e0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 default: {.
f16f0 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 20 assert( 0 );
f1700 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 73 /* The call ass
f1710 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 76 ures that only v
f1720 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 65 alid opcodes are
f1730 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 sent */. }.
f1740 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 }. /*NOTREACHE
f1750 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c D*/. return SQL
f1760 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a ITE_ERROR;.}../*
f1770 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20 .** Within this
f1780 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72 division (the pr
f1790 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69 oxying locking i
f17a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74 mplementation) t
f17b0 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a he procedures.**
f17c0 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e above this poin
f17d0 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74 t are all utilit
f17e0 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72 ies. The lock-r
f17f0 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f elated methods o
f1800 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c f the.** proxy-l
f1810 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 ocking sqlite3_i
f1820 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20 o_method object
f1830 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a follow..*/.../*.
f1840 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
f1850 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 checks if there
f1860 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f is a RESERVED lo
f1870 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 ck held on the s
f1880 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 pecified.** file
f1890 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 by this or any
f18a0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 other process. I
f18b0 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 f such a lock is
f18c0 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 held, set *pRes
f18d0 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d Out.** to a non-
f18e0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 zero value other
f18f0 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 wise *pResOut is
f1900 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 set to zero. T
f1910 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
f1920 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c ** is set to SQL
f1930 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e ITE_OK unless an
f1940 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 I/O error occur
f1950 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 s during lock ch
f1960 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 ecking..*/.stati
f1970 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b c int proxyCheck
f1980 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c ReservedLock(sql
f1990 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f19a0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20 nt *pResOut) {.
f19b0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
f19c0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
f19d0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f ;. int rc = pro
f19e0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c xyTakeConch(pFil
f19f0 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
f1a00 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
f1a10 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f1a20 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
f1a30 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f1a40 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
f1a50 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 Context;. uni
f1a60 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 xFile *proxy = p
f1a70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
f1a80 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79 return proxy
f1a90 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63 ->pMethod->xChec
f1aa0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 73 kReservedLock((s
f1ab0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f qlite3_file*)pro
f1ac0 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 20 xy, pResOut);.
f1ad0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
f1ae0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 ../*.** Lock the
f1af0 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c file with the l
f1b00 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 ock specified by
f1b10 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 parameter lockt
f1b20 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 ype - one.** of
f1b30 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a the following:.*
f1b40 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 *.** (1) SHA
f1b50 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 RED_LOCK.**
f1b60 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 (2) RESERVED_LOC
f1b70 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e K.** (3) PEN
f1b80 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 DING_LOCK.**
f1b90 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c (4) EXCLUSIVE_L
f1ba0 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 OCK.**.** Someti
f1bb0 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 mes when request
f1bc0 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 ing one lock sta
f1bd0 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c te, additional l
f1be0 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 ock states.** ar
f1bf0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 e inserted in be
f1c00 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b tween. The lock
f1c10 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f ing might fail o
f1c20 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 n one of the lat
f1c30 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e er.** transition
f1c40 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f s leaving the lo
f1c50 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 ck state differe
f1c60 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 nt from what it
f1c70 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 started but.** s
f1c80 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 till short of it
f1c90 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c s goal. The fol
f1ca0 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f lowing chart sho
f1cb0 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a ws the allowed.*
f1cc0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e * transitions an
f1cd0 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 d the inserted i
f1ce0 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 ntermediate stat
f1cf0 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c es:.**.** UNL
f1d00 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a OCKED -> SHARED.
f1d10 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 ** SHARED ->
f1d20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 RESERVED.** S
f1d30 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e HARED -> (PENDIN
f1d40 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a G) -> EXCLUSIVE.
f1d50 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d ** RESERVED -
f1d60 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 > (PENDING) -> E
f1d70 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 XCLUSIVE.** P
f1d80 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 ENDING -> EXCLUS
f1d90 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 IVE.**.** This r
f1da0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 outine will only
f1db0 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b increase a lock
f1dc0 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 . Use the sqlit
f1dd0 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 e3OsUnlock().**
f1de0 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 routine to lower
f1df0 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c a locking level
f1e00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
f1e10 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 proxyLock(sqlite
f1e20 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
f1e30 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e locktype) {. un
f1e40 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 ixFile *pFile =
f1e50 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 (unixFile*)id;.
f1e60 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54 int rc = proxyT
f1e70 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b akeConch(pFile);
f1e80 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
f1e90 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78 E_OK ){. prox
f1ea0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f1eb0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f *pCtx = (proxyLo
f1ec0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 ckingContext *)p
f1ed0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e File->lockingCon
f1ee0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 text;. unixFi
f1ef0 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78 le *proxy = pCtx
f1f00 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20 ->lockProxy;.
f1f10 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65 rc = proxy->pMe
f1f20 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c thod->xLock((sql
f1f30 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79 ite3_file*)proxy
f1f40 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 , locktype);.
f1f50 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 pFile->locktype
f1f60 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79 = proxy->lockty
f1f70 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e pe;. }. return
f1f80 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c rc;.}.../*.** L
f1f90 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 ower the locking
f1fa0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 level on file d
f1fb0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 escriptor pFile
f1fc0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f to locktype. lo
f1fd0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 cktype.** must b
f1fe0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b e either NO_LOCK
f1ff0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e or SHARED_LOCK.
f2000 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f .**.** If the lo
f2010 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 cking level of t
f2020 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 he file descript
f2030 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 or is already at
f2040 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 or below.** the
f2050 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 requested locki
f2060 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 ng level, this r
f2070 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f outine is a no-o
f2080 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 p..*/.static int
f2090 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c proxyUnlock(sql
f20a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 ite3_file *id, i
f20b0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 nt locktype) {.
f20c0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 unixFile *pFile
f20d0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 = (unixFile*)id
f20e0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f ;. int rc = pro
f20f0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c xyTakeConch(pFil
f2100 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 e);. if( rc==SQ
f2110 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 LITE_OK ){. p
f2120 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 roxyLockingConte
f2130 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 xt *pCtx = (prox
f2140 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 yLockingContext
f2150 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 *)pFile->locking
f2160 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 Context;. uni
f2170 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 xFile *proxy = p
f2180 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a Ctx->lockProxy;.
f2190 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e rc = proxy->
f21a0 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b pMethod->xUnlock
f21b0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 ((sqlite3_file*)
f21c0 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 proxy, locktype)
f21d0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 ;. pFile->loc
f21e0 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c ktype = proxy->l
f21f0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 ocktype;. }. r
f2200 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
f2210 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 ** Close a file
f2220 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 20 that uses proxy
f2230 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 locks..*/.static
f2240 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28 int proxyClose(
f2250 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
f2260 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a ) {. if( id ){.
f2270 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 unixFile *pF
f2280 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a ile = (unixFile*
f2290 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f )id;. proxyLo
f22a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 ckingContext *pC
f22b0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 tx = (proxyLocki
f22c0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c ngContext *)pFil
f22d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 e->lockingContex
f22e0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 t;. unixFile
f22f0 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74 *lockProxy = pCt
f2300 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 x->lockProxy;.
f2310 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 unixFile *conc
f2320 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f hFile = pCtx->co
f2330 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74 nchFile;. int
f2340 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
f2350 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c 6f . . if( lo
f2360 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 ckProxy ){.
f2370 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d rc = lockProxy-
f2380 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 >pMethod->xUnloc
f2390 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a k((sqlite3_file*
f23a0 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c )lockProxy, NO_L
f23b0 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 OCK);. if(
f23c0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f23d0 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 rc = lockP
f23e0 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 roxy->pMethod->x
f23f0 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 Close((sqlite3_f
f2400 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b ile*)lockProxy);
f2410 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
f2420 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
f2430 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f sqlite3_free(lo
f2440 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20 ckProxy);.
f2450 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 pCtx->lockProxy
f2460 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 = 0;. }. i
f2470 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a f( conchFile ){.
f2480 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e if( pCtx->
f2490 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 20 conchHeld ){.
f24a0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52 rc = proxyR
f24b0 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c eleaseConch(pFil
f24c0 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
f24d0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
f24e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 }. rc
f24f0 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d = conchFile->pM
f2500 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 ethod->xClose((s
f2510 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e qlite3_file*)con
f2520 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 chFile);. i
f2530 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
f2540 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
f2550 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 29 _free(conchFile)
f2560 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
f2570 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 6c te3_free(pCtx->l
f2580 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a 20 ockProxyPath);.
f2590 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
f25a0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 pCtx->conchFileP
f25b0 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 ath);. sqlite
f25c0 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 50 3_free(pCtx->dbP
f25d0 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 73 ath);. /* res
f25e0 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 tore the origina
f25f0 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 l locking contex
f2600 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 t and pMethod th
f2610 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a 20 en close it */.
f2620 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e pFile->lockin
f2630 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 2d gContext = pCtx-
f2640 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 >oldLockingConte
f2650 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 xt;. pFile->p
f2660 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e 70 Method = pCtx->p
f2670 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 73 OldMethod;. s
f2680 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 qlite3_free(pCtx
f2690 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 46 );. return pF
f26a0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 ile->pMethod->xC
f26b0 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20 lose(id);. }.
f26c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
f26d0 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a ;.}....#endif /*
f26e0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 defined(__APPLE
f26f0 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e __) && SQLITE_EN
f2700 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 ABLE_LOCKING_STY
f2710 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 LE */./*.** The
f2720 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74 proxy locking st
f2730 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 yle is intended
f2740 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 50 for use with AFP
f2750 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a 2a filesystems..**
f2760 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 69 And since AFP i
f2770 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64 s only supported
f2780 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 20 on MacOSX, the
f2790 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 proxy locking is
f27a0 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 63 also.** restric
f27b0 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a 2a ted to MacOSX..*
f27c0 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a * .**.**********
f27d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
f27e0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20 the proxy lock
f27f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a implementation *
f2800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2810 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a *****.**********
f2820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f2860 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 ****/../*.** Ini
f2870 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 tialize the oper
f2880 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 ating system int
f2890 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 erface..**.** Th
f28a0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73 is routine regis
f28b0 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d 70 ters all VFS imp
f28c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 lementations for
f28d0 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 unix-like opera
f28e0 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e ting.** systems.
f28f0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 This routine,
f2900 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f and the sqlite3_
f2910 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 os_end() routine
f2920 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a 2a that follows,.*
f2930 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 * should be the
f2940 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 6e only routines in
f2950 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 20 this file that
f2960 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f 6d are visible from
f2970 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 2e other.** files.
f2980 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
f2990 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e ine is called on
f29a0 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65 ce during SQLite
f29b0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
f29c0 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e 67 and by a.** sing
f29d0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20 le thread. The
f29e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
f29f0 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 73 n and mutex subs
f2a00 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 0a ystems have not.
f2a10 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 62 ** necessarily b
f2a20 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 een initialized
f2a30 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e when this routin
f2a40 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e 64 e is called, and
f2a50 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75 so they.** shou
f2a60 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 0a ld not be used..
f2a70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f2a80 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 t sqlite3_os_ini
f2a90 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 0a t(void){ . /* .
f2aa0 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
f2ab0 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 73 ng macro defines
f2ac0 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 an initializer
f2ad0 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 for an sqlite3_v
f2ae0 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20 fs object.. **
f2af0 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 The name of the
f2b00 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 68 VFS is NAME. Th
f2b10 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 20 e pAppData is a
f2b20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f 69 pointer to a poi
f2b30 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 65 nter. ** to the
f2b40 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 "finder" functi
f2b50 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 69 on. (pAppData i
f2b60 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
f2b70 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 65 pointer because
f2b80 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 20 . ** silly C90
f2b90 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 61 rules prohibit a
f2ba0 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 6e void* from bein
f2bb0 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e 63 g cast to a func
f2bc0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 2a tion pointer. *
f2bd0 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 65 * and so we have
f2be0 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74 to go through t
f2bf0 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 he intermediate
f2c00 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 64 pointer to avoid
f2c10 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 77 problems. ** w
f2c20 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 hen compiling wi
f2c30 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 th -pedantic-err
f2c40 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 2a ors on GCC.). *
f2c50 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 45 *. ** The FINDE
f2c60 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 R parameter to t
f2c70 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 65 his macro is the
f2c80 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f 69 name of the poi
f2c90 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a nter to the. **
f2ca0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e finder-function
f2cb0 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 75 . The finder-fu
f2cc0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 nction returns a
f2cd0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a pointer to the.
f2ce0 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d ** sqlite_io_m
f2cf0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 68 ethods object th
f2d00 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 at implements th
f2d10 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e e desired lockin
f2d20 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 73 g. ** behaviors
f2d30 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 73 . See the divis
f2d40 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 63 ion above that c
f2d50 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d 45 ontains the IOME
f2d60 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 6f THODS. ** macro
f2d70 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 6e for addition in
f2d80 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 6e formation on fin
f2d90 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 der-functions..
f2da0 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 69 **. ** Most fi
f2db0 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 74 nders simply ret
f2dc0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f urn a pointer to
f2dd0 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 33 a fixed sqlite3
f2de0 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 2a _io_methods. **
f2df0 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 68 object. But th
f2e00 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e e "autolockIoFin
f2e10 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 6f der" available o
f2e20 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 20 n MacOSX does a
f2e30 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 65 little. ** more
f2e40 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 6c than that; it l
f2e50 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 ooks at the file
f2e60 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 74 system type that
f2e70 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a 2a hosts the . **
f2e80 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
f2e90 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f 6f nd tries to choo
f2ea0 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d 65 se an locking me
f2eb0 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 65 thod appropriate
f2ec0 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 66 for. ** that f
f2ed0 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e 0a ilesystem time..
f2ee0 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 55 */. #define U
f2ef0 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c 20 NIXVFS(VFSNAME,
f2f00 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 20 FINDER) {
f2f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f2f20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 \. 1,
f2f30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
f2f40 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 iVersion */
f2f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f2f60 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 78 . sizeof(unix
f2f70 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a File), /* sz
f2f80 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 20 OsFile */
f2f90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f2fa0 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c MAX_PATHNAME,
f2fb0 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 /* mxPa
f2fc0 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 thname */
f2fd0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f2fe0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
f2ff0 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 /* pNext
f3000 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f3010 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 56 \. V
f3020 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 FSNAME,
f3030 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f /* zName */
f3040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3050 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 6f \. (vo
f3060 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 20 id*)&FINDER,
f3070 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a /* pAppData *
f3080 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3090 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f \. unixO
f30a0 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 pen,
f30b0 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20 /* xOpen */
f30c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f30d0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c \. unixDel
f30e0 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f ete, /
f30f0 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 20 * xDelete */
f3100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3110 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 73 \. unixAcces
f3120 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 s, /*
f3130 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 20 xAccess */
f3140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f3150 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 74 . unixFullPat
f3160 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46 hname, /* xF
f3170 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 ullPathname */
f3180 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3190 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20 unixDlOpen,
f31a0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f /* xDlO
f31b0 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 pen */
f31c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
f31d0 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 20 unixDlError,
f31e0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 /* xDlErr
f31f0 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 or */
f3200 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 \. u
f3210 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 20 nixDlSym,
f3220 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a /* xDlSym *
f3230 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3240 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 \. uni
f3250 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 xDlClose,
f3260 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a /* xDlClose *
f3270 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /
f3280 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 52 \. unixR
f3290 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20 andomness,
f32a0 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 /* xRandomness
f32b0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 */
f32c0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65 \. unixSle
f32d0 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f ep, /
f32e0 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 20 * xSleep */
f32f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f3300 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 65 \. unixCurre
f3310 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20 ntTime, /*
f3320 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20 xCurrentTime */
f3330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c \
f3340 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 74 . unixGetLast
f3350 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 47 Error /* xG
f3360 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 20 etLastError */
f3370 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 \.
f3380 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c }.. /*. ** Al
f3390 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 20 l default VFSes
f33a0 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f 6e for unix are con
f33b0 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 6f tained in the fo
f33c0 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a 20 llowing array..
f33d0 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 **. ** Note th
f33e0 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 at the sqlite3_v
f33f0 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 6f fs.pNext field o
f3400 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74 f the VFS object
f3410 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a is modified. *
f3420 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 * by the SQLite
f3430 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 46 core when the VF
f3440 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e S is registered.
f3450 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 So the followi
f3460 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61 ng. ** array ca
f3470 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20 nnot be const..
f3480 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 6c */. static sql
f3490 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d 20 ite3_vfs aVfs[]
f34a0 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 = {.#if SQLITE_E
f34b0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 NABLE_LOCKING_ST
f34c0 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f 52 YLE && (OS_VXWOR
f34d0 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f KS || defined(__
f34e0 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 4e APPLE__)). UN
f34f0 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20 IXVFS("unix",
f3500 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49 autolockI
f3510 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65 oFinder ),.#else
f3520 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e . UNIXVFS("un
f3530 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 6f ix", po
f3540 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 sixIoFinder ),.#
f3550 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 46 endif. UNIXVF
f3560 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 S("unix-none",
f3570 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 nolockIoFinde
f3580 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
f3590 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c ("unix-dotfile",
f35a0 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 dotlockIoFinde
f35b0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 r ),. UNIXVFS
f35c0 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20 20 ("unix-wfl",
f35d0 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 posixWflIoFind
f35e0 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57 er ),.#if OS_VXW
f35f0 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53 ORKS. UNIXVFS
f3600 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 ("unix-namedsem"
f3610 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c , semIoFinder ),
f3620 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 .#endif.#if SQLI
f3630 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e TE_ENABLE_LOCKIN
f3640 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 58 G_STYLE. UNIX
f3650 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 VFS("unix-posix"
f3660 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 , posixIoFind
f3670 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 58 er ),.#if !OS_VX
f3680 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 WORKS. UNIXVF
f3690 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20 S("unix-flock",
f36a0 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 flockIoFinder
f36b0 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 ),.#endif.#endi
f36c0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 f.#if SQLITE_ENA
f36d0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c BLE_LOCKING_STYL
f36e0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 E && defined(__A
f36f0 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58 PPLE__). UNIX
f3700 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c 20 VFS("unix-afp",
f3710 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 72 afpIoFinder
f3720 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28 ),. UNIXVFS(
f3730 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 20 "unix-proxy",
f3740 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 29 proxyIoFinder )
f3750 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 ,.#endif. };.
f3760 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 20 unsigned int i;
f3770 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
f3780 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f counter */.. /
f3790 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 56 * Register all V
f37a0 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e 20 FSes defined in
f37b0 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 79 the aVfs[] array
f37c0 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 */. for(i=0; i
f37d0 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f 73 <(sizeof(aVfs)/s
f37e0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 66 izeof(sqlite3_vf
f37f0 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 s)); i++){. s
f3800 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 qlite3_vfs_regis
f3810 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 3d ter(&aVfs[i], i=
f3820 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 =0);. }. retur
f3830 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
f3840 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 ./*.** Shutdown
f3850 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 the operating sy
f3860 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a stem interface..
f3870 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 61 **.** Some opera
f3880 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 67 ting systems mig
f3890 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 6f ht need to do so
f38a0 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 68 me cleanup in th
f38b0 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 74 is routine,.** t
f38c0 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d 69 o release dynami
f38d0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
f38e0 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e 6f objects. But no
f38f0 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 68 t on unix..** Th
f3900 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 is routine is a
f3910 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0a no-op for unix..
f3920 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f3930 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 t sqlite3_os_end
f3940 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72 (void){ . retur
f3950 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a n SQLITE_OK; .}.
f3960 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 .#endif /* SQLI
f3970 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a 2f TE_OS_UNIX */../
f3980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
f3990 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 nd of os_unix.c
f39a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f39b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f39c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f39d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
f39e0 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 6e egin file os_win
f39f0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
f3a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f3a20 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 *.** 2004 May 22
f3a30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
f3a40 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
f3a50 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
f3a60 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
f3a70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
f3a80 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
f3a90 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
f3aa0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
f3ab0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
f3ac0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
f3ad0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
f3ae0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
f3af0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
f3b00 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
f3b10 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
f3b20 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
f3b30 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
f3b40 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
f3b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f3b90 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
f3ba0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f file contains co
f3bb0 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 69 de that is speci
f3bc0 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e 0a fic to windows..
f3bd0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 */.#if SQLITE_OS
f3be0 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 _WIN
f3bf0 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 /* This file
f3c00 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64 is used for wind
f3c10 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a ows only */.../*
f3c20 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 .** A Note About
f3c30 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 Memory Allocati
f3c40 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 on:.**.** This d
f3c50 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f river uses mallo
f3c60 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 63 c()/free() direc
f3c70 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e 20 tly rather than
f3c80 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a going through.**
f3c90 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70 the SQLite-wrap
f3ca0 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c pers sqlite3_mal
f3cb0 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 loc()/sqlite3_fr
f3cc0 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61 ee(). Those wra
f3cd0 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 ppers.** are des
f3ce0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e igned for use on
f3cf0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
f3d00 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 s where memory i
f3d10 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 s scarce and.**
f3d20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 malloc failures
f3d30 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c happen frequentl
f3d40 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 6e y. Win32 does n
f3d50 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 6e ot typically run
f3d60 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 on.** embedded
f3d70 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 65 systems, and whe
f3d80 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 65 n it does the de
f3d90 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c velopers normall
f3da0 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a 2a y have bigger.**
f3db0 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72 problems to wor
f3dc0 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 75 ry about than ru
f3dd0 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d nning out of mem
f3de0 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 69 ory. So there i
f3df0 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 s not.** a compe
f3e00 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 73 lling need to us
f3e10 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 0a e the wrappers..
f3e20 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20 **.** But there
f3e30 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e is a good reason
f3e40 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 to not use the
f3e50 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 65 wrappers. If we
f3e60 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 70 use the.** wrap
f3e70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 6c pers then we wil
f3e80 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 20 l get simulated
f3e90 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 malloc() failure
f3ea0 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a s within this.**
f3eb0 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 68 driver. And th
f3ec0 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b 69 at causes all ki
f3ed0 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 nds of problems
f3ee0 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 20 for our tests.
f3ef0 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61 We.** could enha
f3f00 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 65 nce SQLite to de
f3f10 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 65 al with simulate
f3f20 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 d malloc failure
f3f30 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 s within.** the
f3f40 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 74 OS driver, but t
f3f50 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20 he code to deal
f3f60 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75 with those failu
f3f70 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 re would not.**
f3f80 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20 be exercised on
f3f90 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65 Linux (which doe
f3fa0 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 s not need to ma
f3fb0 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72 lloc() in the dr
f3fc0 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 iver).** and so
f3fd0 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69 we would have di
f3fe0 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67 fficulty writing
f3ff0 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20 coverage tests
f4000 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 for that.** code
f4010 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 61 . Better to lea
f4020 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 2c ve the code out,
f4030 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a we think..**.**
f4040 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68 The point of th
f4050 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 73 is discussion is
f4060 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 as follows: Wh
f4070 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 en creating a ne
f4080 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f w.** OS layer fo
f4090 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 79 r an embedded sy
f40a0 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 65 stem, if you use
f40b0 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 6e this file as an
f40c0 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f example,.** avo
f40d0 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d 61 id the use of ma
f40e0 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 20 lloc()/free().
f40f0 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 77 Those routines w
f4100 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77 ork ok on window
f4110 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 s.** desktops bu
f4120 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e t not so well in
f4130 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d embedded system
f4140 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 s..*/..#include
f4150 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66 <winbase.h>..#if
f4160 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23 def __CYGWIN__.#
f4170 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 79 include <sys/cy
f4180 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a gwin.h>.#endif..
f4190 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 /*.** Macros use
f41a0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 d to determine w
f41b0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f hether or not to
f41c0 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f use threads..*/
f41d0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52 .#if defined(THR
f41e0 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 45 EADSAFE) && THRE
f41f0 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 ADSAFE.# define
f4200 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41 SQLITE_W32_THREA
f4210 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a DS 1.#endif../*.
f4220 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 ** Include code
f4230 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 that is common t
f4240 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c o all os_*.c fil
f4250 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a es.*/./*********
f4260 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 ***** Include os
f4270 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 _common.h in the
f4280 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69 middle of os_wi
f4290 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a n.c ************
f42a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
f42b0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
f42c0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
f42d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f42e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f42f0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 ****/./*.** 2004
f4300 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 May 22.**.** Th
f4310 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
f4320 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
f4330 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
f4340 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
f4350 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
f4360 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
f4370 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
f4380 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
f4390 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
f43a0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
f43b0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
f43c0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
f43d0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
f43e0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
f43f0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
f4400 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
f4410 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
f4420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a ***********.**.*
f4470 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 * This file cont
f4480 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 ains macros and
f4490 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 a little bit of
f44a0 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d code that is com
f44b0 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 mon to.** all of
f44c0 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 the platform-sp
f44d0 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 ecific files (os
f44e0 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e _*.c) and is #in
f44f0 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 cluded into thos
f4500 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a e.** files..**.*
f4510 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 * This file shou
f4520 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 ld be #included
f4530 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 by the os_*.c fi
f4540 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 les only. It is
f4550 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 not a.** genera
f4560 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 l purpose header
f4570 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 file..*/.#ifnde
f4580 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a f _OS_COMMON_H_.
f4590 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d #define _OS_COMM
f45a0 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 ON_H_../*.** At
f45b0 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 least two bugs h
f45c0 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 ave slipped in b
f45d0 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 ecause we change
f45e0 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 d the MEMORY_DEB
f45f0 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 UG.** macro to S
f4600 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 QLITE_DEBUG and
f4610 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 some older makef
f4620 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 iles have not ye
f4630 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 t made the.** sw
f4640 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f itch. The follo
f4650 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 wing code should
f4660 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 catch this prob
f4670 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 lem at compile-t
f4680 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d ime..*/.#ifdef M
f4690 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 EMORY_DEBUG.# er
f46a0 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f ror "The MEMORY_
f46b0 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f DEBUG macro is o
f46c0 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 bsolete. Use SQ
f46d0 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 LITE_DEBUG inste
f46e0 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 ad.".#endif..#if
f46f0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
f4700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
f4710 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 int sqlite3OSTra
f4720 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 ce = 0;.#define
f4730 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20 OSTRACE1(X)
f4740 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
f4750 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f4760 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 DebugPrintf(X).#
f4770 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 define OSTRACE2(
f4780 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73 X,Y) if( s
f4790 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 qlite3OSTrace )
f47a0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
f47b0 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 tf(X,Y).#define
f47c0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 OSTRACE3(X,Y,Z)
f47d0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f if( sqlite3O
f47e0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 STrace ) sqlite3
f47f0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c DebugPrintf(X,Y,
f4800 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 Z).#define OSTRA
f4810 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 CE4(X,Y,Z,A) i
f4820 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 f( sqlite3OSTrac
f4830 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 e ) sqlite3Debug
f4840 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a Printf(X,Y,Z,A).
f4850 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 #define OSTRACE5
f4860 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 (X,Y,Z,A,B) if(
f4870 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 sqlite3OSTrace )
f4880 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
f4890 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 ntf(X,Y,Z,A,B).#
f48a0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
f48b0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 X,Y,Z,A,B,C) \.
f48c0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 if(sqlite3OST
f48d0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 race) sqlite3Deb
f48e0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 ugPrintf(X,Y,Z,A
f48f0 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 ,B,C).#define OS
f4900 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 TRACE7(X,Y,Z,A,B
f4910 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 ,C,D) \. if(s
f4920 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 qlite3OSTrace) s
f4930 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
f4940 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 f(X,Y,Z,A,B,C,D)
f4950 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
f4960 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 STRACE1(X).#defi
f4970 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 ne OSTRACE2(X,Y)
f4980 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 .#define OSTRACE
f4990 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 3(X,Y,Z).#define
f49a0 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c OSTRACE4(X,Y,Z,
f49b0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 A).#define OSTRA
f49c0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 CE5(X,Y,Z,A,B).#
f49d0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 define OSTRACE6(
f49e0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 X,Y,Z,A,B,C).#de
f49f0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c fine OSTRACE7(X,
f4a00 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e Y,Z,A,B,C,D).#en
f4a10 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f dif../*.** Macro
f4a20 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 s for performanc
f4a30 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d e tracing. Norm
f4a40 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e ally turned off.
f4a50 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 Only works.**
f4a60 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65 on i486 hardware
f4a70 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
f4a80 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 TE_PERFORMANCE_T
f4a90 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 RACE../* .** hwt
f4aa0 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 ime.h contains i
f4ab0 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 nline assembler
f4ac0 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 code for impleme
f4ad0 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 nting .** high-p
f4ae0 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e erformance timin
f4af0 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f g routines..*/./
f4b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 ************** I
f4b10 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 nclude hwtime.h
f4b20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 in the middle of
f4b30 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a os_common.h ***
f4b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f4b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
f4b60 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 egin file hwtime
f4b70 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
f4b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
f4ba0 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 *.** 2008 May 27
f4bb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f .**.** The autho
f4bc0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 r disclaims copy
f4bd0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f right to this so
f4be0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 urce code. In p
f4bf0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 lace of.** a leg
f4c00 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 al notice, here
f4c10 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a is a blessing:.*
f4c20 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 *.** May you
f4c30 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 do good and not
f4c40 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 evil..** May
f4c50 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 you find forgive
f4c60 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c ness for yoursel
f4c70 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 f and forgive ot
f4c80 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 hers..** May
f4c90 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 you share freely
f4ca0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d , never taking m
f4cb0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 ore than you giv
f4cc0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e..**.**********
f4cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f4d10 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ****.**.** This
f4d20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e file contains in
f4d30 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f line asm code fo
f4d40 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 r retrieving "hi
f4d50 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a gh-performance".
f4d60 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 ** counters for
f4d70 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a x86 class CPUs..
f4d80 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 */.#ifndef _HWTI
f4d90 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 ME_H_.#define _H
f4da0 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 WTIME_H_../*.**
f4db0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
f4dc0 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 utine only works
f4dd0 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 on pentium-clas
f4de0 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f s (or newer) pro
f4df0 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 cessors..** It u
f4e00 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 ses the RDTSC op
f4e10 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 code to read the
f4e20 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c cycle count val
f4e30 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a ue out of the.**
f4e40 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 processor and r
f4e50 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 eturns that valu
f4e60 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 e. This can be
f4e70 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 used for high-re
f4e80 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a s.** profiling..
f4e90 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 */.#if (defined(
f4ea0 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 __GNUC__) || def
f4eb0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 ined(_MSC_VER))
f4ec0 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 && \. (defi
f4ed0 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 ned(i386) || def
f4ee0 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c ined(__i386__) |
f4ef0 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 | defined(_M_IX8
f4f00 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 6)).. #if defin
f4f10 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 ed(__GNUC__)..
f4f20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 __inline__ sqlit
f4f30 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 e_uint64 sqlite3
f4f40 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 Hwtime(void){.
f4f50 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
f4f60 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 lo, hi;. __a
f4f70 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f sm__ __volatile_
f4f80 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 _ ("rdtsc" : "=a
f4f90 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 " (lo), "=d" (hi
f4fa0 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 ));. return
f4fb0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 (sqlite_uint64)h
f4fc0 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 i << 32 | lo;.
f4fd0 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e }.. #elif defin
f4fe0 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 ed(_MSC_VER)..
f4ff0 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 __declspec(naked
f5000 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 ) __inline sqlit
f5010 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c e_uint64 __cdecl
f5020 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
f5030 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d oid){. __asm
f5040 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 {. rdtsc
f5050 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20 . ret
f5060 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 ; return valu
f5070 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 e at EDX:EAX.
f5080 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 }. }.. #endi
f5090 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 f..#elif (define
f50a0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 d(__GNUC__) && d
f50b0 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f efined(__x86_64_
f50c0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f _)).. __inline_
f50d0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 _ sqlite_uint64
f50e0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f sqlite3Hwtime(vo
f50f0 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 id){. unsig
f5100 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 ned long val;.
f5110 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f __asm__ __vo
f5120 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 latile__ ("rdtsc
f5130 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b " : "=A" (val));
f5140 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 . return va
f5150 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 l;. }. .#elif (
f5160 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f defined(__GNUC__
f5170 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 ) && defined(__p
f5180 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 pc__)).. __inli
f5190 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 ne__ sqlite_uint
f51a0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 64 sqlite3Hwtime
f51b0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e (void){. un
f51c0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
f51d0 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 retval;. u
f51e0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e nsigned long jun
f51f0 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f k;. __asm__
f5200 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 __volatile__ ("
f5210 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a \n\. 1:
f5220 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 mftbu %1
f5230 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 \n\.
f5240 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c mftb %L
f5250 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 0\n\.
f5260 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 mftbu %
f5270 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 0\n\.
f5280 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 cmpw %
f5290 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 0,%1\n\.
f52a0 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 bne
f52b0 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 1b".
f52c0 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 : "=r" (
f52d0 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a retval), "=r" (j
f52e0 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 unk));. ret
f52f0 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a urn retval;. }.
f5300 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 .#else.. #error
f5310 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 Need implementa
f5320 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 tion of sqlite3H
f5330 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 wtime() for your
f5340 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a platform... /*
f5350 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 . ** To compile
f5360 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 without impleme
f5370 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 nting sqlite3Hwt
f5380 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 ime() for your p
f5390 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f latform,. ** yo
f53a0 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 u can remove the
f53b0 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e above #error an
f53c0 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 d use the follow
f53d0 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 ing. ** stub fu
f53e0 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c nction. You wil
f53f0 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 l lose timing su
f5400 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 pport for many.
f5410 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 ** of the debug
f5420 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 ging and testing
f5430 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 utilities, but
f5440 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a it should at. *
f5450 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 * least compile
f5460 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 and run.. */.SQ
f5470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 LITE_PRIVATE s
f5480 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c qlite_uint64 sql
f5490 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 ite3Hwtime(void)
f54a0 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 { return ((sqlit
f54b0 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a e_uint64)0); }..
f54c0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f #endif..#endif /
f54d0 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 * !defined(_HWTI
f54e0 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a ME_H_) */../****
f54f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
f5500 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a f hwtime.h *****
f5510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
f5540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 ********** Conti
f5550 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c nuing where we l
f5560 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f eft off in os_co
f5570 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a mmon.h *********
f5580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 *********/..stat
f5590 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 ic sqlite_uint64
f55a0 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63 g_start;.static
f55b0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 sqlite_uint64 g
f55c0 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e _elapsed;.#defin
f55d0 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20 e TIMER_START
f55e0 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 g_start=sqli
f55f0 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66 te3Hwtime().#def
f5600 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 ine TIMER_END
f5610 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d g_elapsed=
f5620 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d sqlite3Hwtime()-
f5630 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20 g_start.#define
f5640 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 TIMER_ELAPSED
f5650 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 g_elapsed.#els
f5660 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f e.#define TIMER_
f5670 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49 START.#define TI
f5680 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 MER_END.#define
f5690 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20 TIMER_ELAPSED
f56a0 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 ((sqlite_uint6
f56b0 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 4)0).#endif../*.
f56c0 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 ** If we compile
f56d0 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45 with the SQLITE
f56e0 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c _TEST macro set,
f56f0 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 then the follow
f5700 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 ing block.** of
f5710 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75 code will give u
f5720 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f s the ability to
f5730 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b simulate a disk
f5740 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 I/O error. Thi
f5750 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72 s.** is used for
f5760 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f testing the I/O
f5770 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e recovery logic.
f5780 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
f5790 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 E_TEST.SQLITE_AP
f57a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f I int sqlite3_io
f57b0 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 _error_hit = 0;
f57c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f /* To
f57d0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f tal number of I/
f57e0 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 O Errors */.SQLI
f57f0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
f5800 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 e3_io_error_hard
f5810 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 hit = 0;
f5820 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e /* Number of non
f5830 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a -benign errors *
f5840 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 /.SQLITE_API int
f5850 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
f5860 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 r_pending = 0;
f5870 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 /* Count d
f5880 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f own to first I/O
f5890 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 error */.SQLITE
f58a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
f58b0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 _io_error_persis
f58c0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a t = 0; /*
f58d0 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72 True if I/O err
f58e0 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 ors persist */.S
f58f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
f5900 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 lite3_io_error_b
f5910 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 enign = 0;
f5920 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72 /* True if er
f5930 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20 rors are benign
f5940 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e */.SQLITE_API in
f5950 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 t sqlite3_diskfu
f5960 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a ll_pending = 0;.
f5970 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f5980 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 qlite3_diskfull
f5990 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d = 0;.#define Sim
f59a0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 ulateIOErrorBeni
f59b0 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f gn(X) sqlite3_io
f59c0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 _error_benign=(X
f59d0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 ).#define Simula
f59e0 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 teIOError(CODE)
f59f0 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 \. if( (sqlite
f5a00 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 3_io_error_persi
f5a10 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f st && sqlite3_io
f5a20 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 _error_hit) \.
f5a30 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f || sqlite3_
f5a40 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
f5a50 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 -- == 1 ) \.
f5a60 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 { loc
f5a70 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 al_ioerr(); CODE
f5a80 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ; }.static void
f5a90 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 local_ioerr(){.
f5aa0 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52 IOTRACE(("IOERR
f5ab0 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 \n"));. sqlite3
f5ac0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b _io_error_hit++;
f5ad0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f . if( !sqlite3_
f5ae0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 io_error_benign
f5af0 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 ) sqlite3_io_err
f5b00 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a or_hardhit++;.}.
f5b10 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 #define Simulate
f5b20 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f DiskfullError(CO
f5b30 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c DE) \. if( sql
f5b40 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 ite3_diskfull_pe
f5b50 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 nding ){ \.
f5b60 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b if( sqlite3_disk
f5b70 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 full_pending ==
f5b80 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 1 ){ \. lo
f5b90 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 cal_ioerr(); \.
f5ba0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 sqlite3_di
f5bb0 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 skfull = 1; \.
f5bc0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f sqlite3_io_
f5bd0 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c error_hit = 1; \
f5be0 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a . CODE; \.
f5bf0 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 }else{ \.
f5c00 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 sqlite3_dis
f5c10 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b kfull_pending--;
f5c20 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d \. } \. }
f5c30 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 .#else.#define S
f5c40 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 imulateIOErrorBe
f5c50 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 nign(X).#define
f5c60 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
f5c70 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c A).#define Simul
f5c80 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 ateDiskfullError
f5c90 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (A).#endif../*.*
f5ca0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 * When testing,
f5cb0 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 keep a count of
f5cc0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 the number of op
f5cd0 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 en files..*/.#if
f5ce0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
f5cf0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
f5d00 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 qlite3_open_file
f5d10 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 _count = 0;.#def
f5d20 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 ine OpenCounter(
f5d30 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e X) sqlite3_open
f5d40 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 _file_count+=(X)
f5d50 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f .#else.#define O
f5d60 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 penCounter(X).#e
f5d70 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 ndif..#endif /*
f5d80 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d !defined(_OS_COM
f5d90 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a MON_H_) */../***
f5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
f5db0 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a of os_common.h *
f5dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f5de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
f5df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
f5e00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
f5e10 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 left off in os_w
f5e20 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a in.c ***********
f5e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a **********/../*.
f5e40 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 ** Some microsof
f5e50 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b t compilers lack
f5e60 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e this definition
f5e70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 ..*/.#ifndef INV
f5e80 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 ALID_FILE_ATTRIB
f5e90 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e UTES.# define IN
f5ea0 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 VALID_FILE_ATTRI
f5eb0 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31 BUTES ((DWORD)-1
f5ec0 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a ) .#endif../*.**
f5ed0 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65 Determine if we
f5ee0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 are dealing wit
f5ef0 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 h WindowsCE - wh
f5f00 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a ich has a much.*
f5f10 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a * reduced API..*
f5f20 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
f5f30 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41 WINCE.# define A
f5f40 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29 reFileApisANSI()
f5f50 20 31 0a 23 20 64 65 66 69 6e 65 20 46 6f 72 6d 1.# define Form
f5f60 61 74 4d 65 73 73 61 67 65 57 28 61 2c 62 2c 63 atMessageW(a,b,c
f5f70 2c 64 2c 65 2c 66 2c 67 29 20 30 0a 23 65 6e 64 ,d,e,f,g) 0.#end
f5f80 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 20 if../*.** WinCE
f5f90 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70 lacks native sup
f5fa0 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f port for file lo
f5fb0 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65 cking so we have
f5fc0 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 77 to fake it.** w
f5fd0 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f 66 ith some code of
f5fe0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 our own..*/.#if
f5ff0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f6000 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
f6010 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e winceLock {. in
f6020 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20 t nReaders;
f6030 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
f6040 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61 eader locks obta
f6050 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 ined */. BOOL b
f6060 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a Pending; /*
f6070 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e Indicates a pen
f6080 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 ding lock has be
f6090 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 en obtained */.
f60a0 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b BOOL bReserved;
f60b0 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 /* Indicate
f60c0 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 s a reserved loc
f60d0 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 k has been obtai
f60e0 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45 ned */. BOOL bE
f60f0 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 xclusive; /*
f6100 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63 Indicates an exc
f6110 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20 lusive lock has
f6120 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f been obtained */
f6130 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65 .} winceLock;.#e
f6140 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
f6150 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72 winFile structur
f6160 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 e is a subclass
f6170 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a of sqlite3_file*
f6180 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65 specific to the
f6190 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62 win32.** portab
f61a0 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a ility layer..*/.
f61b0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77 typedef struct w
f61c0 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a inFile winFile;.
f61d0 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b struct winFile {
f61e0 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 . const sqlite3
f61f0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 _io_methods *pMe
f6200 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 20 thod;/* Must be
f6210 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 4c first */. HANDL
f6220 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 E h;
f6230 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 /* Handle for
f6240 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 66 accessing the f
f6250 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 ile */. unsigne
f6260 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b d char locktype;
f6270 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b /* Type of lock
f6280 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 currently held
f6290 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a on this file */.
f62a0 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c 6f short sharedLo
f62b0 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 6e ckByte; /* Ran
f62c0 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 74 domly chosen byt
f62d0 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61 72 e used as a shar
f62e0 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57 4f ed lock */. DWO
f62f0 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 RD lastErrno;
f6300 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64 /* The Wind
f6310 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 ows errno from t
f6320 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f he last I/O erro
f6330 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65 63 r */. DWORD sec
f6340 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 2f torSize; /
f6350 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 * Sector size of
f6360 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c 65 the device file
f6370 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53 51 is on */.#if SQ
f6380 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
f6390 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f 6e WCHAR *zDeleteOn
f63a0 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 Close; /* Name
f63b0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 of file to delet
f63c0 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 2a e when closing *
f63d0 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74 65 /. HANDLE hMute
f63e0 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d x; /* M
f63f0 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f 6e utex used to con
f6400 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 73 trol access to s
f6410 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20 0a hared lock */ .
f6420 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65 64 HANDLE hShared
f6430 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 ; /* Sha
f6440 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d 65 red memory segme
f6450 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b nt used for lock
f6460 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f ing */. winceLo
f6470 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20 20 ck local;
f6480 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69 6e /* Locks obtain
f6490 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 61 ed by this insta
f64a0 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20 2a nce of winFile *
f64b0 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a 73 /. winceLock *s
f64c0 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20 47 hared; /* G
f64d0 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f 63 lobal shared loc
f64e0 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 k memory for the
f64f0 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69 66 file */.#endif
f6500 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 .};../*.** Forwa
f6510 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a 2a rd prototypes..*
f6520 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 /.static int get
f6530 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20 SectorSize(.
f6540 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 sqlite3_vfs *pVf
f6550 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 s,. const cha
f6560 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20 r *zRelative
f6570 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e /* UTF-8 file n
f6580 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a ame */.);../*.**
f6590 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 The following v
f65a0 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d ariable is (norm
f65b0 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 61 ally) set once a
f65c0 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 73 nd never changes
f65d0 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20 .** thereafter.
f65e0 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 It records whet
f65f0 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e her the operatin
f6600 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39 g system is Win9
f6610 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 5.** or WinNT..*
f6620 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 74 *.** 0: Operat
f6630 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e 6f ing system unkno
f6640 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 72 wn..** 1: Oper
f6650 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 ating system is
f6660 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 4f Win95..** 2: O
f6670 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 perating system
f6680 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 is WinNT..**.**
f6690 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 69 In order to faci
f66a0 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 6f litate testing o
f66b0 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 6d n a WinNT system
f66c0 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 75 , the test fixtu
f66d0 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 6c re.** can manual
f66e0 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c 75 ly set this valu
f66f0 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 74 e to 1 to emulat
f6700 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f 72 e Win98 behavior
f6710 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
f6720 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 TE_TEST.SQLITE_A
f6730 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f PI int sqlite3_o
f6740 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c 73 s_type = 0;.#els
f6750 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c e.static int sql
f6760 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30 ite3_os_type = 0
f6770 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ;.#endif../*.**
f6780 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e Return true (non
f6790 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 -zero) if we are
f67a0 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 running under W
f67b0 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e inNT, Win2K, Win
f67c0 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e XP,.** or WinCE.
f67d0 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 Return false (
f67e0 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c zero) for Win95,
f67f0 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 Win98, or WinME
f6800 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 ..**.** Here is
f6810 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f an interesting o
f6820 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e bservation: Win
f6830 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 95, Win98, and W
f6840 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 inME lack.** the
f6850 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 LockFileEx() AP
f6860 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 I. But we can s
f6870 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 till statically
f6880 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 link against tha
f6890 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 t.** API as long
f68a0 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c as we don't cal
f68b0 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e l it when runnin
f68c0 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 g Win95/98/ME.
f68d0 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 A call to.** thi
f68e0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 s routine is use
f68f0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 d to determine i
f6900 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 f the host is Wi
f6910 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 n95/98/ME or.**
f6920 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 WinNT/2K/XP so t
f6930 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 hat we will know
f6940 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
f6950 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 we can safely ca
f6960 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 ll.** the LockFi
f6970 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 leEx() API..*/.#
f6980 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
f6990 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54 CE.# define isNT
f69a0 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 () (1).#else.
f69b0 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28 static int isNT(
f69c0 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73 void){. if( s
f69d0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d qlite3_os_type==
f69e0 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 0 ){. OSVER
f69f0 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a SIONINFO sInfo;.
f6a00 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 sInfo.dwOS
f6a10 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 VersionInfoSize
f6a20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b = sizeof(sInfo);
f6a30 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 6f . GetVersio
f6a40 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 nEx(&sInfo);.
f6a50 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 sqlite3_os_ty
f6a60 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 pe = sInfo.dwPla
f6a70 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 tformId==VER_PLA
f6a80 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f TFORM_WIN32_NT ?
f6a90 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 2 : 1;. }.
f6aa0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
f6ab0 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d _os_type==2;. }
f6ac0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
f6ad0 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f E_OS_WINCE */../
f6ae0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 *.** Convert a U
f6af0 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d TF-8 string to m
f6b00 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 icrosoft unicode
f6b10 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a (UTF-16?). .**.
f6b20 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 ** Space to hold
f6b30 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 the returned st
f6b40 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 ring is obtained
f6b50 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f from malloc..*/
f6b60 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 75 .static WCHAR *u
f6b70 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e tf8ToUnicode(con
f6b80 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 st char *zFilena
f6b90 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72 me){. int nChar
f6ba0 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 65 ;. WCHAR *zWide
f6bb0 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68 Filename;.. nCh
f6bc0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f ar = MultiByteTo
f6bd0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 WideChar(CP_UTF8
f6be0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 , 0, zFilename,
f6bf0 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20 -1, NULL, 0);.
f6c00 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 zWideFilename =
f6c10 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 69 malloc( nChar*si
f6c20 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e 61 zeof(zWideFilena
f6c30 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 me[0]) );. if(
f6c40 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d 30 zWideFilename==0
f6c50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 ){. return 0
f6c60 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 ;. }. nChar =
f6c70 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 MultiByteToWideC
f6c80 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 har(CP_UTF8, 0,
f6c90 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a zFilename, -1, z
f6ca0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e 43 WideFilename, nC
f6cb0 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 61 har);. if( nCha
f6cc0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 r==0 ){. free
f6cd0 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 3b (zWideFilename);
f6ce0 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 . zWideFilena
f6cf0 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 me = 0;. }. re
f6d00 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61 turn zWideFilena
f6d10 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e me;.}../*.** Con
f6d20 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 vert microsoft u
f6d30 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 2e nicode to UTF-8.
f6d40 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 Space to hold
f6d50 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 the returned str
f6d60 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e ing is.** obtain
f6d70 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 ed from malloc()
f6d80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 ..*/.static char
f6d90 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 *unicodeToUtf8(
f6da0 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 const WCHAR *zWi
f6db0 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 deFilename){. i
f6dc0 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 nt nByte;. char
f6dd0 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 *zFilename;..
f6de0 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 nByte = WideChar
f6df0 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 ToMultiByte(CP_U
f6e00 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c TF8, 0, zWideFil
f6e10 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c ename, -1, 0, 0,
f6e20 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 0, 0);. zFilen
f6e30 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 ame = malloc( nB
f6e40 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 yte );. if( zFi
f6e50 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 lename==0 ){.
f6e60 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
f6e70 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 nByte = WideCha
f6e80 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f rToMultiByte(CP_
f6e90 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 UTF8, 0, zWideFi
f6ea0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c lename, -1, zFil
f6eb0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 ename, nByte,.
f6ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f6ed0 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 0, 0
f6ee0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d );. if( nByte =
f6ef0 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 = 0 ){. free(
f6f00 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 zFilename);.
f6f10 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 zFilename = 0;.
f6f20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c }. return zFil
f6f30 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 ename;.}../*.**
f6f40 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 20 Convert an ansi
f6f50 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 string to micros
f6f60 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 73 oft unicode, bas
f6f70 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 ed on the.** cur
f6f80 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 65 rent codepage se
f6f90 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 ttings for file
f6fa0 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 apis..** .** Spa
f6fb0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 ce to hold the r
f6fc0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 eturned string i
f6fd0 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 s obtained.** fr
f6fe0 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 om malloc..*/.st
f6ff0 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 73 atic WCHAR *mbcs
f7000 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 ToUnicode(const
f7010 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 char *zFilename)
f7020 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 {. int nByte;.
f7030 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c WCHAR *zMbcsFil
f7040 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 ename;. int cod
f7050 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 epage = AreFileA
f7060 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 pisANSI() ? CP_A
f7070 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a CP : CP_OEMCP;..
f7080 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 nByte = MultiB
f7090 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f yteToWideChar(co
f70a0 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 depage, 0, zFile
f70b0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30 name, -1, NULL,0
f70c0 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b )*sizeof(WCHAR);
f70d0 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 . zMbcsFilename
f70e0 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 = malloc( nByte
f70f0 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c *sizeof(zMbcsFil
f7100 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 ename[0]) );. i
f7110 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 f( zMbcsFilename
f7120 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
f7130 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 n 0;. }. nByte
f7140 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 = MultiByteToWi
f7150 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c deChar(codepage,
f7160 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 0, zFilename, -
f7170 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 1, zMbcsFilename
f7180 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 , nByte);. if(
f7190 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 nByte==0 ){.
f71a0 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61 free(zMbcsFilena
f71b0 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69 me);. zMbcsFi
f71c0 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a lename = 0;. }.
f71d0 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69 return zMbcsFi
f71e0 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a lename;.}../*.**
f71f0 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f Convert microso
f7200 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d 75 ft unicode to mu
f7210 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 ltibyte characte
f7220 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 20 r string, based
f7230 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 73 on the.** user's
f7240 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e 0a Ansi codepage..
f7250 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 **.** Space to h
f7260 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 old the returned
f7270 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 string is obtai
f7280 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c ned from.** mall
f7290 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 oc()..*/.static
f72a0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 4d char *unicodeToM
f72b0 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 20 bcs(const WCHAR
f72c0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b *zWideFilename){
f72d0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 . int nByte;.
f72e0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b char *zFilename;
f72f0 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 . int codepage
f7300 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 = AreFileApisANS
f7310 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 I() ? CP_ACP : C
f7320 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 P_OEMCP;.. nByt
f7330 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 e = WideCharToMu
f7340 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65 ltiByte(codepage
f7350 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 , 0, zWideFilena
f7360 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c me, -1, 0, 0, 0,
f7370 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 0);. zFilename
f7380 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 = malloc( nByte
f7390 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e );. if( zFilen
f73a0 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ame==0 ){. re
f73b0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 turn 0;. }. nB
f73c0 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f yte = WideCharTo
f73d0 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 MultiByte(codepa
f73e0 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 ge, 0, zWideFile
f73f0 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e name, -1, zFilen
f7400 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20 ame, nByte,.
f7410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f7420 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b 0, 0);
f7430 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 . if( nByte ==
f7440 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46 0 ){. free(zF
f7450 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46 ilename);. zF
f7460 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d ilename = 0;. }
f7470 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e . return zFilen
f7480 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f ame;.}../*.** Co
f7490 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 20 nvert multibyte
f74a0 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 character string
f74b0 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 to UTF-8. Spac
f74c0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a e to hold the.**
f74d0 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 returned string
f74e0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f is obtained fro
f74f0 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 m malloc()..*/.S
f7500 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a QLITE_API char *
f7510 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 sqlite3_win32_mb
f7520 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74 cs_to_utf8(const
f7530 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f7540 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 ){. char *zFile
f7550 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41 nameUtf8;. WCHA
f7560 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 R *zTmpWide;..
f7570 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54 zTmpWide = mbcsT
f7580 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 oUnicode(zFilena
f7590 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 me);. if( zTmpW
f75a0 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 ide==0 ){. re
f75b0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 turn 0;. }. zF
f75c0 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e ilenameUtf8 = un
f75d0 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70 icodeToUtf8(zTmp
f75e0 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 Wide);. free(zT
f75f0 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 mpWide);. retur
f7600 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b n zFilenameUtf8;
f7610 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
f7620 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69 t UTF-8 to multi
f7630 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 byte character s
f7640 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f tring. Space to
f7650 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65 hold the .** re
f7660 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 turned string is
f7670 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d obtained from m
f7680 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
f7690 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d ic char *utf8ToM
f76a0 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a bcs(const char *
f76b0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 zFilename){. ch
f76c0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 ar *zFilenameMbc
f76d0 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 s;. WCHAR *zTmp
f76e0 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 Wide;.. zTmpWid
f76f0 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 e = utf8ToUnicod
f7700 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 e(zFilename);.
f7710 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 if( zTmpWide==0
f7720 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
f7730 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 . }. zFilename
f7740 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f Mbcs = unicodeTo
f7750 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a Mbcs(zTmpWide);.
f7760 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 free(zTmpWide)
f7770 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 ;. return zFile
f7780 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66 nameMbcs;.}..#if
f7790 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
f77a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
f77b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f77c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f77d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f77e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
f77f0 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e 74 his section cont
f7800 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 69 ains code for Wi
f7810 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a nCE only..*/./*.
f7820 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f 65 ** WindowsCE doe
f7830 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63 s not have a loc
f7840 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f altime() functio
f7850 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0a n. So create a.
f7860 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a ** substitute..*
f7870 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f 63 /.struct tm *__c
f7880 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 63 decl localtime(c
f7890 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 0a onst time_t *t).
f78a0 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63 {. static struc
f78b0 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 49 t tm y;. FILETI
f78c0 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 ME uTm, lTm;. S
f78d0 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 YSTEMTIME pTm;.
f78e0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 sqlite3_int64 t
f78f0 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a 64;. t64 = *t;.
f7900 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31 t64 = (t64 + 1
f7910 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30 1644473600)*1000
f7920 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 0000;. uTm.dwLo
f7930 77 44 61 74 65 54 69 6d 65 20 3d 20 28 44 57 4f wDateTime = (DWO
f7940 52 44 29 28 74 36 34 20 26 20 30 78 46 46 46 46 RD)(t64 & 0xFFFF
f7950 46 46 46 46 29 3b 0a 20 20 75 54 6d 2e 64 77 48 FFFF);. uTm.dwH
f7960 69 67 68 44 61 74 65 54 69 6d 65 3d 20 28 44 57 ighDateTime= (DW
f7970 4f 52 44 29 28 74 36 34 20 3e 3e 20 33 32 29 3b ORD)(t64 >> 32);
f7980 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 . FileTimeToLoc
f7990 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c alFileTime(&uTm,
f79a0 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d &lTm);. FileTim
f79b0 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c eToSystemTime(&l
f79c0 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d Tm,&pTm);. y.tm
f79d0 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 _year = pTm.wYea
f79e0 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d r - 1900;. y.tm
f79f0 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 _mon = pTm.wMont
f7a00 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 h - 1;. y.tm_wd
f7a10 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 ay = pTm.wDayOfW
f7a20 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 eek;. y.tm_mday
f7a30 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 = pTm.wDay;. y
f7a40 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 .tm_hour = pTm.w
f7a50 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e Hour;. y.tm_min
f7a60 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a = pTm.wMinute;.
f7a70 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d y.tm_sec = pTm
f7a80 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 .wSecond;. retu
f7a90 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 rn &y;.}../* Thi
f7aa0 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 s will never be
f7ab0 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69 called, but defi
f7ac0 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 ned to make the
f7ad0 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a code compile */.
f7ae0 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50 #define GetTempP
f7af0 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 athA(a,b)..#defi
f7b00 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c ne LockFile(a,b,
f7b10 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e c,d,e) win
f7b20 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 ceLockFile(&a, b
f7b30 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 , c, d, e).#defi
f7b40 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c ne UnlockFile(a,
f7b50 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e b,c,d,e) win
f7b60 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c ceUnlockFile(&a,
f7b70 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 b, c, d, e).#de
f7b80 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 fine LockFileEx(
f7b90 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 a,b,c,d,e,f) w
f7ba0 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 inceLockFileEx(&
f7bb0 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 a, b, c, d, e, f
f7bc0 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c )..#define HANDL
f7bd0 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 E_TO_WINFILE(a)
f7be0 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 (winFile*)&((cha
f7bf0 72 2a 29 61 29 5b 2d 28 69 6e 74 29 6f 66 66 73 r*)a)[-(int)offs
f7c00 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d etof(winFile,h)]
f7c10 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 ../*.** Acquire
f7c20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61 a lock on the ha
f7c30 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63 ndle h.*/.static
f7c40 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78 void winceMutex
f7c50 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68 Acquire(HANDLE h
f7c60 29 7b 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72 ){. DWORD dwEr
f7c70 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 r;. do {.
f7c80 64 77 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53 dwErr = WaitForS
f7c90 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49 ingleObject(h, I
f7ca0 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77 NFINITE);. } w
f7cb0 68 69 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57 hile (dwErr != W
f7cc0 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20 AIT_OBJECT_0 &&
f7cd0 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42 dwErr != WAIT_AB
f7ce0 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a ANDONED);.}./*.*
f7cf0 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b * Release a lock
f7d00 20 61 63 71 75 69 72 65 64 20 62 79 20 77 69 6e acquired by win
f7d10 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 29 ceMutexAcquire()
f7d20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63 .*/.#define winc
f7d30 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 68 29 eMutexRelease(h)
f7d40 20 52 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29 ReleaseMutex(h)
f7d50 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 ../*.** Create t
f7d60 68 65 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61 he mutex and sha
f7d70 72 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 red memory used
f7d80 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 for locking in t
f7d90 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 he file.** descr
f7da0 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73 iptor pFile.*/.s
f7db0 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 tatic BOOL wince
f7dc0 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74 CreateLock(const
f7dd0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 char *zFilename
f7de0 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 , winFile *pFile
f7df0 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b ){. WCHAR *zTok
f7e00 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65 ;. WCHAR *zName
f7e10 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 = utf8ToUnicode
f7e20 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42 (zFilename);. B
f7e30 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45 OOL bInit = TRUE
f7e40 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 ;.. /* Initiali
f7e50 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63 ze the local loc
f7e60 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d kdata */. ZeroM
f7e70 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f emory(&pFile->lo
f7e80 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c cal, sizeof(pFil
f7e90 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f e->local));.. /
f7ea0 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 62 61 * Replace the ba
f7eb0 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74 ckslashes from t
f7ec0 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 he filename and
f7ed0 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a lowercase it. *
f7ee0 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75 * to derive a mu
f7ef0 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a tex name. */. z
f7f00 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57 Tok = CharLowerW
f7f10 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28 (zName);. for (
f7f20 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a ;*zTok;zTok++){.
f7f30 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d if (*zTok ==
f7f40 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27 '\\') *zTok = '
f7f50 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 _';. }.. /* Cr
f7f60 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61 eate/open the na
f7f70 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70 med mutex */. p
f7f80 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43 File->hMutex = C
f7f90 72 65 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c reateMutexW(NULL
f7fa0 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b , FALSE, zName);
f7fb0 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 . if (!pFile->h
f7fc0 4d 75 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c Mutex){. pFil
f7fd0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
f7fe0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
f7ff0 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a free(zName);.
f8000 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45 return FALSE
f8010 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 ;. }.. /* Acqu
f8020 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 65 ire the mutex be
f8030 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20 fore continuing
f8040 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 */. winceMutexA
f8050 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d cquire(pFile->hM
f8060 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53 utex);. . /* S
f8070 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f ince the names o
f8080 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c f named mutexes,
f8090 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c semaphores, fil
f80a0 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61 e mappings etc a
f80b0 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65 re . ** case-se
f80c0 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64 nsitive, take ad
f80d0 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20 vantage of that
f80e0 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 74 by uppercasing t
f80f0 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20 he mutex name.
f8100 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61 ** and using tha
f8110 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 20 t as the shared
f8120 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65 filemapping name
f8130 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70 .. */. CharUpp
f8140 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46 erW(zName);. pF
f8150 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43 ile->hShared = C
f8160 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67 reateFileMapping
f8170 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 W(INVALID_HANDLE
f8180 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20 _VALUE, NULL,.
f8190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f81a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f81b0 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57 52 PAGE_READWR
f81c0 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77 ITE, 0, sizeof(w
f81d0 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20 inceLock),.
f81e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f81f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f8200 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f zName); .. /
f8210 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68 61 * Set a flag tha
f8220 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27 72 t indicates we'r
f8230 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20 63 e the first to c
f8240 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 reate the memory
f8250 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73 so it . ** mus
f8260 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61 t be zero-initia
f8270 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47 lized */. if (G
f8280 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d etLastError() ==
f8290 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45 ERROR_ALREADY_E
f82a0 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69 XISTS){. bIni
f82b0 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a t = FALSE;. }..
f82c0 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a free(zName);..
f82d0 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65 /* If we succe
f82e0 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74 eded in making t
f82f0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
f8300 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e handle, map it.
f8310 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d */. if (pFile-
f8320 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70 >hShared){. p
f8330 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28 File->shared = (
f8340 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69 winceLock*)MapVi
f8350 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e ewOfFile(pFile->
f8360 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20 hShared, .
f8370 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f FILE_MAP_
f8380 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52 READ|FILE_MAP_WR
f8390 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f ITE, 0, 0, sizeo
f83a0 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 f(winceLock));.
f83b0 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67 /* If mapping
f83c0 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74 failed, close t
f83d0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 he shared memory
f83e0 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73 handle and eras
f83f0 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 e it */. if (
f8400 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b !pFile->shared){
f8410 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 . pFile->la
f8420 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 stErrno = GetLas
f8430 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 tError();.
f8440 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f8450 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20 e->hShared);.
f8460 20 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 pFile->hShare
f8470 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a d = NULL;. }.
f8480 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61 }.. /* If sha
f8490 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64 red memory could
f84a0 20 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c not be created,
f84b0 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20 then close the
f84c0 6d 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a mutex and fail *
f84d0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 /. if (pFile->h
f84e0 53 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b Shared == NULL){
f84f0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52 . winceMutexR
f8500 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d elease(pFile->hM
f8510 75 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65 utex);. Close
f8520 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d Handle(pFile->hM
f8530 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 utex);. pFile
f8540 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b ->hMutex = NULL;
f8550 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 . return FALS
f8560 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 E;. }. . /* I
f8570 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68 nitialize the sh
f8580 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77 ared memory if w
f8590 65 27 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f e're supposed to
f85a0 20 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29 */. if (bInit)
f85b0 20 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72 {. ZeroMemor
f85c0 79 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c y(pFile->shared,
f85d0 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63 sizeof(winceLoc
f85e0 6b 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 k));. }.. winc
f85f0 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 eMutexRelease(pF
f8600 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 ile->hMutex);.
f8610 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a return TRUE;.}..
f8620 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68 /*.** Destroy th
f8630 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c e part of winFil
f8640 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69 74 e that deals wit
f8650 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f h wince locks.*/
f8660 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e .static void win
f8670 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69 ceDestroyLock(wi
f8680 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 nFile *pFile){.
f8690 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74 if (pFile->hMut
f86a0 65 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75 ex){. /* Acqu
f86b0 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f ire the mutex */
f86c0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41 . winceMutexA
f86d0 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d cquire(pFile->hM
f86e0 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 utex);.. /* T
f86f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f he following blo
f8700 63 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 cks should proba
f8710 62 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64 65 bly assert in de
f8720 62 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68 bug mode, but th
f8730 65 79 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f ey. are to
f8740 20 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65 cleanup in case
f8750 20 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69 any locks remai
f8760 6e 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 ned open */.
f8770 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c if (pFile->local
f8780 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20 .nReaders){.
f8790 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f87a0 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20 >nReaders --;.
f87b0 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c }. if (pFil
f87c0 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76 e->local.bReserv
f87d0 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 ed){. pFile
f87e0 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72 ->shared->bReser
f87f0 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ved = FALSE;.
f8800 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 }. if (pFile
f8810 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 ->local.bPending
f8820 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f8830 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 shared->bPending
f8840 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a = FALSE;. }.
f8850 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c if (pFile->l
f8860 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29 ocal.bExclusive)
f8870 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 {. pFile->s
f8880 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 hared->bExclusiv
f8890 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d e = FALSE;. }
f88a0 0a 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65 .. /* De-refe
f88b0 72 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20 rence and close
f88c0 6f 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20 our copy of the
f88d0 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61 shared memory ha
f88e0 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61 ndle */. Unma
f88f0 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c pViewOfFile(pFil
f8900 65 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20 e->shared);.
f8910 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c CloseHandle(pFil
f8920 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20 e->hShared);..
f8930 20 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74 /* Done with t
f8940 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 he mutex */.
f8950 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 winceMutexReleas
f8960 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 e(pFile->hMutex)
f8970 3b 20 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48 ; . CloseH
f8980 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75 andle(pFile->hMu
f8990 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d tex);. pFile-
f89a0 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a >hMutex = NULL;.
f89b0 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e }.}../* .** An
f89c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
f89d0 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28 of the LockFile(
f89e0 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 ) API of windows
f89f0 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 for wince.*/.st
f8a00 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c atic BOOL winceL
f8a10 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c ockFile(. HANDL
f8a20 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f E *phFile,. DWO
f8a30 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c RD dwFileOffsetL
f8a40 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 ow,. DWORD dwFi
f8a50 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20 leOffsetHigh,.
f8a60 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 DWORD nNumberOfB
f8a70 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 ytesToLockLow,.
f8a80 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 DWORD nNumberOf
f8a90 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a BytesToLockHigh.
f8aa0 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 ){. winFile *pF
f8ab0 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f ile = HANDLE_TO_
f8ac0 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b WINFILE(phFile);
f8ad0 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20 . BOOL bReturn
f8ae0 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 = FALSE;.. UNUS
f8af0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 46 ED_PARAMETER(dwF
f8b00 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b 0a ileOffsetHigh);.
f8b10 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
f8b20 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 ER(nNumberOfByte
f8b30 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 sToLockHigh);..
f8b40 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 if (!pFile->hMu
f8b50 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 tex) return TRUE
f8b60 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 ;. winceMutexAc
f8b70 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 quire(pFile->hMu
f8b80 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74 tex);.. /* Want
f8b90 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ing an exclusive
f8ba0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20 28 lock? */. if (
f8bb0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 dwFileOffsetLow
f8bc0 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f8bd0 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26 _FIRST. &&
f8be0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 nNumberOfBytesT
f8bf0 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f oLockLow == (DWO
f8c00 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b RD)SHARED_SIZE){
f8c10 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f8c20 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 shared->nReaders
f8c30 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65 2d 3e == 0 && pFile->
f8c40 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 shared->bExclusi
f8c50 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20 ve == 0){.
f8c60 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e pFile->shared->
f8c70 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55 bExclusive = TRU
f8c80 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d E;. pFile-
f8c90 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 >local.bExclusiv
f8ca0 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 e = TRUE;.
f8cb0 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b bReturn = TRUE;
f8cc0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
f8cd0 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f 6e 6c Want a read-onl
f8ce0 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 y lock? */. els
f8cf0 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 e if (dwFileOffs
f8d00 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 etLow == (DWORD)
f8d10 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a SHARED_FIRST &&.
f8d20 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 nNumb
f8d30 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c erOfBytesToLockL
f8d40 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 ow == 1){. if
f8d50 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d (pFile->shared-
f8d60 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 >bExclusive == 0
f8d70 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e ){. pFile->
f8d80 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b local.nReaders +
f8d90 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69 +;. if (pFi
f8da0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 le->local.nReade
f8db0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 rs == 1){.
f8dc0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d pFile->shared-
f8dd0 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 >nReaders ++;.
f8de0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74 }. bRet
f8df0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f8e00 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 }. }.. /* Want
f8e10 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f a pending lock?
f8e20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 */. else if (d
f8e30 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f8e40 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47 = (DWORD)PENDING
f8e50 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 _BYTE && nNumber
f8e60 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 OfBytesToLockLow
f8e70 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f 2a 20 49 == 1){. /* I
f8e80 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f 63 f no pending loc
f8e90 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75 69 k has been acqui
f8ea0 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69 72 red, then acquir
f8eb0 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28 e it */. if (
f8ec0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 pFile->shared->b
f8ed0 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b 0a Pending == 0) {.
f8ee0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 pFile->sha
f8ef0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 red->bPending =
f8f00 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69 6c TRUE;. pFil
f8f10 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e e->local.bPendin
f8f20 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 g = TRUE;.
f8f30 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a bReturn = TRUE;.
f8f40 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
f8f50 57 61 6e 74 20 61 20 72 65 73 65 72 76 65 64 20 Want a reserved
f8f60 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 lock? */. else
f8f70 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 if (dwFileOffset
f8f80 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45 Low == (DWORD)RE
f8f90 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e SERVED_BYTE && n
f8fa0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c NumberOfBytesToL
f8fb0 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 ockLow == 1){.
f8fc0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 if (pFile->sha
f8fd0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d red->bReserved =
f8fe0 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 = 0) {. pFi
f8ff0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 le->shared->bRes
f9000 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 erved = TRUE;.
f9010 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c pFile->local
f9020 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 .bReserved = TRU
f9030 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e E;. bReturn
f9040 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 = TRUE;. }.
f9050 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 }.. winceMutex
f9060 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 Release(pFile->h
f9070 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e Mutex);. return
f9080 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a bReturn;.}../*.
f9090 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 ** An implementa
f90a0 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f tion of the Unlo
f90b0 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69 ckFile API of wi
f90c0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a ndows for wince.
f90d0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 */.static BOOL w
f90e0 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a inceUnlockFile(.
f90f0 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 HANDLE *phFile
f9100 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 ,. DWORD dwFile
f9110 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f OffsetLow,. DWO
f9120 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 RD dwFileOffsetH
f9130 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 igh,. DWORD nNu
f9140 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c mberOfBytesToUnl
f9150 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 ockLow,. DWORD
f9160 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9170 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 UnlockHigh.){.
f9180 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d winFile *pFile =
f9190 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 HANDLE_TO_WINFI
f91a0 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f LE(phFile);. BO
f91b0 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c OL bReturn = FAL
f91c0 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 SE;.. UNUSED_PA
f91d0 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 RAMETER(dwFileOf
f91e0 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55 fsetHigh);. UNU
f91f0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e SED_PARAMETER(nN
f9200 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
f9210 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66 lockHigh);.. if
f9220 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 (!pFile->hMutex
f9230 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20 ) return TRUE;.
f9240 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 winceMutexAcqui
f9250 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 re(pFile->hMutex
f9260 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 );.. /* Releasi
f9270 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b ng a reader lock
f9280 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 or an exclusive
f9290 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64 lock */. if (d
f92a0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d wFileOffsetLow =
f92b0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f = (DWORD)SHARED_
f92c0 46 49 52 53 54 29 7b 0a 20 20 20 20 2f 2a 20 44 FIRST){. /* D
f92d0 69 64 20 77 65 20 68 61 76 65 20 61 6e 20 65 78 id we have an ex
f92e0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f clusive lock? */
f92f0 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f9300 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 local.bExclusive
f9310 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
f9320 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9330 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 UnlockLow == (DW
f9340 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 ORD)SHARED_SIZE)
f9350 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c ;. pFile->l
f9360 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20 ocal.bExclusive
f9370 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 = FALSE;. p
f9380 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 File->shared->bE
f9390 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 xclusive = FALSE
f93a0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 ;. bReturn
f93b0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20 = TRUE;. }..
f93c0 20 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73 /* Did we jus
f93d0 74 20 68 61 76 65 20 61 20 72 65 61 64 65 72 20 t have a reader
f93e0 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73 lock? */. els
f93f0 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 e if (pFile->loc
f9400 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 al.nReaders){.
f9410 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 assert(nNumb
f9420 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 erOfBytesToUnloc
f9430 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 kLow == (DWORD)S
f9440 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e 4e HARED_SIZE || nN
f9450 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e umberOfBytesToUn
f9460 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a 20 lockLow == 1);.
f9470 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 pFile->loca
f9480 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 l.nReaders --;.
f9490 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e if (pFile->
f94a0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d local.nReaders =
f94b0 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20 20 = 0). {.
f94c0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 pFile->shar
f94d0 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b ed->nReaders --;
f94e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
f94f0 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 Return = TRUE;.
f9500 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 }. }.. /* R
f9510 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64 69 eleasing a pendi
f9520 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 ng lock */. els
f9530 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 e if (dwFileOffs
f9540 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 etLow == (DWORD)
f9550 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 PENDING_BYTE &&
f9560 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f nNumberOfBytesTo
f9570 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b UnlockLow == 1){
f9580 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e . if (pFile->
f9590 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b local.bPending){
f95a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f . pFile->lo
f95b0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46 cal.bPending = F
f95c0 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c ALSE;. pFil
f95d0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 e->shared->bPend
f95e0 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 ing = FALSE;.
f95f0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 bReturn = TRU
f9600 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f E;. }. }. /
f9610 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 * Releasing a re
f9620 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 served lock */.
f9630 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 else if (dwFile
f9640 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 OffsetLow == (DW
f9650 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54 ORD)RESERVED_BYT
f9660 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 E && nNumberOfBy
f9670 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d tesToUnlockLow =
f9680 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46 = 1){. if (pF
f9690 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 ile->local.bRese
f96a0 72 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46 rved) {. pF
f96b0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 ile->local.bRese
f96c0 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 rved = FALSE;.
f96d0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pFile->share
f96e0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 d->bReserved = F
f96f0 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74 ALSE;. bRet
f9700 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 urn = TRUE;.
f9710 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 }. }.. winceMu
f9720 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 texRelease(pFile
f9730 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 ->hMutex);. ret
f9740 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a urn bReturn;.}..
f9750 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 /*.** An impleme
f9760 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c ntation of the L
f9770 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20 ockFileEx() API
f9780 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 of windows for w
f9790 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 ince.*/.static B
f97a0 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c OOL winceLockFil
f97b0 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 eEx(. HANDLE *p
f97c0 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 hFile,. DWORD d
f97d0 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20 wFlags,. DWORD
f97e0 64 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57 dwReserved,. DW
f97f0 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 ORD nNumberOfByt
f9800 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 esToLockLow,. D
f9810 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 WORD nNumberOfBy
f9820 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20 tesToLockHigh,.
f9830 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70 LPOVERLAPPED lp
f9840 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20 Overlapped.){.
f9850 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f9860 28 64 77 52 65 73 65 72 76 65 64 29 3b 0a 20 20 (dwReserved);.
f9870 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
f9880 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 (nNumberOfBytesT
f9890 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 2f oLockHigh);.. /
f98a0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 * If the caller
f98b0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72 wants a shared r
f98c0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72 ead lock, forwar
f98d0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a d this call. **
f98e0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c to winceLockFil
f98f0 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65 e */. if (lpOve
f9900 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20 rlapped->Offset
f9910 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 == (DWORD)SHARED
f9920 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 _FIRST &&.
f9930 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 0a dwFlags == 1 &&.
f9940 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 nNumberOfB
f9950 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d ytesToLockLow ==
f9960 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53 (DWORD)SHARED_S
f9970 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e IZE){. return
f9980 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70 winceLockFile(p
f9990 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 hFile, SHARED_FI
f99a0 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 RST, 0, 1, 0);.
f99b0 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53 }. return FALS
f99c0 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f E;.}./*.** End o
f99d0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f f the special co
f99e0 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a de for wince.***
f99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 **********/.#end
f9a40 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f if /* SQLITE_OS_
f9a50 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a WINCE */../*****
f9a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9aa0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 ********.** The
f9ab0 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f next group of ro
f9ac0 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 utines implement
f9ad0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 the I/O methods
f9ae0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 specified.** by
f9af0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f the sqlite3_io_
f9b00 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a methods object..
f9b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
f9b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
f9b60 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 ./*.** Close a f
f9b70 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 ile..**.** It is
f9b80 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 61 reported that a
f9b90 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f n attempt to clo
f9ba0 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68 se a handle migh
f9bb0 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 t sometimes.** f
f9bc0 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20 ail. This is a
f9bd0 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c very unreasonabl
f9be0 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69 e result, but wi
f9bf0 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f ndows is notorio
f9c00 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20 us.** for being
f9c10 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20 unreasonable so
f9c20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74 I do not doubt t
f9c30 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 70 hat it might hap
f9c40 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 pen. If.** the
f9c50 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20 close fails, we
f9c60 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69 pause for 100 mi
f9c70 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 lliseconds and t
f9c80 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a ry again. As.**
f9c90 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53 many as MX_CLOS
f9ca0 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70 E_ATTEMPT attemp
f9cb0 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 ts to close the
f9cc0 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20 handle are made
f9cd0 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67 before.** giving
f9ce0 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e up and returnin
f9cf0 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 g an error..*/.#
f9d00 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f define MX_CLOSE_
f9d10 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63 ATTEMPT 3.static
f9d20 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71 int winClose(sq
f9d30 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b lite3_file *id){
f9d40 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d . int rc, cnt =
f9d50 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 0;. winFile *p
f9d60 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
f9d70 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 )id;.. assert(
f9d80 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 id!=0 );. OSTRA
f9d90 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 CE2("CLOSE %d\n"
f9da0 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64 , pFile->h);. d
f9db0 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73 o{. rc = Clos
f9dc0 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 eHandle(pFile->h
f9dd0 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d );. }while( rc=
f9de0 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58 =0 && ++cnt < MX
f9df0 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26 _CLOSE_ATTEMPT &
f9e00 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31 & (Sleep(100), 1
f9e10 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f ) );.#if SQLITE_
f9e20 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e 65 OS_WINCE.#define
f9e30 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f WINCE_DELETION_
f9e40 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 69 6e ATTEMPTS 3. win
f9e50 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46 ceDestroyLock(pF
f9e60 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c ile);. if( pFil
f9e70 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 e->zDeleteOnClos
f9e80 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 e ){. int cnt
f9e90 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 = 0;. while(
f9ea0 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 6c 65 . Dele
f9eb0 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a teFileW(pFile->z
f9ec0 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d DeleteOnClose)==
f9ed0 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 65 74 0. && Get
f9ee0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 FileAttributesW(
f9ef0 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e pFile->zDeleteOn
f9f00 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 66 66 Close)!=0xffffff
f9f10 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 20 63 ff . && c
f9f20 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c nt++ < WINCE_DEL
f9f30 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20 ETION_ATTEMPTS.
f9f40 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c 65 ){. Sle
f9f50 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 69 ep(100); /* Wai
f9f60 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 6f 72 t a little befor
f9f70 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e 20 2a e trying again *
f9f80 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 /. }. free
f9f90 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f (pFile->zDeleteO
f9fa0 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e nClose);. }.#en
f9fb0 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 dif. OpenCounte
f9fc0 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 r(-1);. return
f9fd0 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a rc ? SQLITE_OK :
f9fe0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d SQLITE_IOERR;.}
f9ff0 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 ../*.** Some mic
fa000 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 rosoft compilers
fa010 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e lack this defin
fa020 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 ition..*/.#ifnde
fa030 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 f INVALID_SET_FI
fa040 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66 LE_POINTER.# def
fa050 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f ine INVALID_SET_
fa060 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44 FILE_POINTER ((D
fa070 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a WORD)-1).#endif.
fa080 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 ./*.** Read data
fa090 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74 from a file int
fa0a0 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74 o a buffer. Ret
fa0b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
fa0c0 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65 all.** bytes we
fa0d0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66 re read successf
fa0e0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f ully and SQLITE_
fa0f0 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e IOERR if anythin
fa100 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e g goes.** wrong.
fa110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fa120 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 inRead(. sqlite
fa130 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 3_file *id,
fa140 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 /* File to
fa150 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 read from */. v
fa160 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
fa170 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
fa180 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 te content into
fa190 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 this buffer */.
fa1a0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 int amt,
fa1b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
fa1c0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
fa1d0 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 o read */. sqli
fa1e0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 te3_int64 offset
fa1f0 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 /* Begin
fa200 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 reading at this
fa210 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c offset */.){. L
fa220 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d 20 ONG upperBits =
fa230 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e (LONG)((offset>>
fa240 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66 32) & 0x7fffffff
fa250 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 );. LONG lowerB
fa260 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66 its = (LONG)(off
fa270 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 66 set & 0xffffffff
fa280 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 );. DWORD rc;.
fa290 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 winFile *pFile
fa2a0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a = (winFile*)id;.
fa2b0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20 DWORD error;.
fa2c0 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 61 DWORD got;.. a
fa2d0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a ssert( id!=0 );.
fa2e0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
fa2f0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
fa300 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f IOERR_READ);. O
fa310 53 54 52 41 43 45 33 28 22 52 45 41 44 20 25 64 STRACE3("READ %d
fa320 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 lock=%d\n", pFi
fa330 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f le->h, pFile->lo
fa340 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 cktype);. rc =
fa350 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 SetFilePointer(p
fa360 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 File->h, lowerBi
fa370 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 ts, &upperBits,
fa380 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 FILE_BEGIN);. i
fa390 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 f( rc==INVALID_S
fa3a0 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 ET_FILE_POINTER
fa3b0 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 && (error=GetLas
fa3c0 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 tError())!=NO_ER
fa3d0 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 ROR ){. pFile
fa3e0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 ->lastErrno = er
fa3f0 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ror;. return
fa400 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d SQLITE_FULL;. }
fa410 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c 65 . if( !ReadFile
fa420 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c (pFile->h, pBuf,
fa430 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 29 amt, &got, 0) )
fa440 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
fa450 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
fa460 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 Error();. ret
fa470 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fa480 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28 _READ;. }. if(
fa490 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74 got==(DWORD)amt
fa4a0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
fa4b0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
fa4c0 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64 e{. /* Unread
fa4d0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75 parts of the bu
fa4e0 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72 ffer must be zer
fa4f0 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 o-filled */.
fa500 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29 memset(&((char*)
fa510 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 pBuf)[got], 0, a
fa520 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 mt-got);. ret
fa530 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 urn SQLITE_IOERR
fa540 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d _SHORT_READ;. }
fa550 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 .}../*.** Write
fa560 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 data from a buff
fa570 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 er into a file.
fa580 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
fa590 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 K on success.**
fa5a0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 or some other er
fa5b0 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c ror code on fail
fa5c0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ure..*/.static i
fa5d0 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73 nt winWrite(. s
fa5e0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fa5f0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
fa600 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a to write into *
fa610 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a /. const void *
fa620 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a pBuf, /*
fa630 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 The bytes to be
fa640 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e written */. in
fa650 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 t amt,
fa660 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
fa670 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 r of bytes to wr
fa680 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ite */. sqlite3
fa690 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 _int64 offset
fa6a0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 /* Offset int
fa6b0 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65 o the file to be
fa6c0 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a gin writing at *
fa6d0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 /.){. LONG uppe
fa6e0 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 rBits = (LONG)((
fa6f0 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78 offset>>32) & 0x
fa700 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 7fffffff);. LON
fa710 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c G lowerBits = (L
fa720 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78 ONG)(offset & 0x
fa730 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f ffffffff);. DWO
fa740 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 RD rc;. winFile
fa750 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
fa760 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20 le*)id;. DWORD
fa770 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 77 error;. DWORD w
fa780 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 rote = 0;.. ass
fa790 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
fa7a0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 SimulateIOError(
fa7b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
fa7c0 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69 ERR_WRITE);. Si
fa7d0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 mulateDiskfullEr
fa7e0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 ror(return SQLIT
fa7f0 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41 E_FULL);. OSTRA
fa800 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f CE3("WRITE %d lo
fa810 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d ck=%d\n", pFile-
fa820 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 >h, pFile->lockt
fa830 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74 ype);. rc = Set
fa840 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c FilePointer(pFil
fa850 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c e->h, lowerBits,
fa860 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c &upperBits, FIL
fa870 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20 E_BEGIN);. if(
fa880 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f rc==INVALID_SET_
fa890 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20 FILE_POINTER &&
fa8a0 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72 (error=GetLastEr
fa8b0 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 ror())!=NO_ERROR
fa8c0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c ){. pFile->l
fa8d0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 astErrno = error
fa8e0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
fa8f0 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 ITE_FULL;. }.
fa900 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b assert( amt>0 );
fa910 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 61 . while(. a
fa920 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72 63 mt>0. && (rc
fa930 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46 69 = WriteFile(pFi
fa940 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 le->h, pBuf, amt
fa950 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d 30 , &wrote, 0))!=0
fa960 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e 30 . && wrote>0
fa970 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d . ){. amt -=
fa980 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66 wrote;. pBuf
fa990 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66 = &((char*)pBuf
fa9a0 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20 )[wrote];. }.
fa9b0 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e 28 if( !rc || amt>(
fa9c0 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 20 int)wrote ){.
fa9d0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e pFile->lastErrn
fa9e0 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 o = GetLastError
fa9f0 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ();. return S
faa00 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a QLITE_FULL;. }.
faa10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
faa20 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 OK;.}../*.** Tru
faa30 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69 ncate an open fi
faa40 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65 le to a specifie
faa50 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63 d size.*/.static
faa60 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65 int winTruncate
faa70 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 (sqlite3_file *i
faa80 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 d, sqlite3_int64
faa90 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20 nByte){. LONG
faaa0 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e upperBits = (LON
faab0 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 G)((nByte>>32) &
faac0 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 0x7fffffff);.
faad0 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d LONG lowerBits =
faae0 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20 (LONG)(nByte &
faaf0 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 0xffffffff);. D
fab00 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 WORD rc;. winFi
fab10 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fab20 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
fab30 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 D error;.. asse
fab40 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f rt( id!=0 );. O
fab50 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 54 STRACE3("TRUNCAT
fab60 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 E %d %lld\n", pF
fab70 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a ile->h, nByte);.
fab80 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f SimulateIOErro
fab90 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f r(return SQLITE_
faba0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b IOERR_TRUNCATE);
fabb0 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 . rc = SetFileP
fabc0 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c ointer(pFile->h,
fabd0 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 lowerBits, &upp
fabe0 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 erBits, FILE_BEG
fabf0 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 IN);. if( rc==I
fac00 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f NVALID_SET_FILE_
fac10 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f POINTER && (erro
fac20 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 r=GetLastError()
fac30 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 )!=NO_ERROR ){.
fac40 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 pFile->lastEr
fac50 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 rno = error;.
fac60 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 return SQLITE_I
fac70 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20 OERR_TRUNCATE;.
fac80 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66 }. /* SetEndOf
fac90 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 69 File will fail i
faca0 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 61 74 f nByte is negat
facb0 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 65 ive */. if( !Se
facc0 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c 65 tEndOfFile(pFile
facd0 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 69 6c ->h) ){. pFil
face0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 e->lastErrno = G
facf0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fad00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fad10 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b _IOERR_TRUNCATE;
fad20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
fad30 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 LITE_OK;.}..#ifd
fad40 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f ef SQLITE_TEST./
fad50 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e *.** Count the n
fad60 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e umber of fullsyn
fad70 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 cs and normal sy
fad80 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 ncs. This is us
fad90 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 ed to test.** th
fada0 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c at syncs and ful
fadb0 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72 lsyncs are occur
fadc0 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74 ing at the right
fadd0 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 times..*/.SQLIT
fade0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
fadf0 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3_sync_count = 0
fae00 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 ;.SQLITE_API int
fae10 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e sqlite3_fullsyn
fae20 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e c_count = 0;.#en
fae30 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 dif../*.** Make
fae40 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 sure all writes
fae50 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 to a particular
fae60 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 file are committ
fae70 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73 ed to disk..*/.s
fae80 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e tatic int winSyn
fae90 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a c(sqlite3_file *
faea0 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a id, int flags){.
faeb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e #ifndef SQLITE_N
faec0 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65 O_SYNC. winFile
faed0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 *pFile = (winFi
faee0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 le*)id;.. asser
faef0 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 t( id!=0 );. OS
faf00 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 20 TRACE3("SYNC %d
faf10 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c lock=%d\n", pFil
faf20 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 e->h, pFile->loc
faf30 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 20 ktype);.#else.
faf40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
faf50 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 (id);.#endif.#if
faf60 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 ndef SQLITE_TEST
faf70 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 . UNUSED_PARAME
faf80 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c 73 TER(flags);.#els
faf90 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 e. if( flags &
fafa0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c SQLITE_SYNC_FULL
fafb0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
fafc0 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b fullsync_count++
fafd0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f ;. }. sqlite3_
fafe0 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 sync_count++;.#e
faff0 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20 ndif. /* If we
fb000 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 compiled with th
fb010 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 e SQLITE_NO_SYNC
fb020 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 flag, then sync
fb030 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f ing is a. ** no
fb040 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 -op. */.#ifdef
fb050 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 SQLITE_NO_SYNC.
fb060 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fb070 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28 _OK;.#else. if(
fb080 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72 FlushFileBuffer
fb090 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 s(pFile->h) ){.
fb0a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fb0b0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
fb0c0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 pFile->lastErr
fb0d0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f no = GetLastErro
fb0e0 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 r();. return
fb0f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 SQLITE_IOERR;.
fb100 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a }.#endif.}../*.*
fb110 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 * Determine the
fb120 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 current size of
fb130 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a a file in bytes.
fb140 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fb150 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 nFileSize(sqlite
fb160 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 3_file *id, sqli
fb170 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 te3_int64 *pSize
fb180 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 65 72 ){. DWORD upper
fb190 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c 6f Bits;. DWORD lo
fb1a0 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 69 werBits;. winFi
fb1b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e le *pFile = (win
fb1c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 File*)id;. DWOR
fb1d0 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65 D error;.. asse
fb1e0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 rt( id!=0 );. S
fb1f0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 imulateIOError(r
fb200 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 eturn SQLITE_IOE
fb210 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77 RR_FSTAT);. low
fb220 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c 65 erBits = GetFile
fb230 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 26 Size(pFile->h, &
fb240 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 69 66 upperBits);. if
fb250 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20 3d ( (lowerBits =
fb260 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 53 = INVALID_FILE_S
fb270 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 28 65 IZE). && ((e
fb280 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 rror = GetLastEr
fb290 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 52 ror()) != NO_ERR
fb2a0 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 46 OR) ). {. pF
fb2b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d ile->lastErrno =
fb2c0 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 error;. retu
fb2d0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f rn SQLITE_IOERR_
fb2e0 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 FSTAT;. }. *pS
fb2f0 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 65 33 ize = (((sqlite3
fb300 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 74 73 _int64)upperBits
fb310 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 69 )<<32) + lowerBi
fb320 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c ts;. return SQL
fb330 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
fb340 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 LOCKFILE_FAIL_I
fb350 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e MMEDIATELY is un
fb360 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 defined on some
fb370 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e Windows systems.
fb380 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b .*/.#ifndef LOCK
fb390 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 FILE_FAIL_IMMEDI
fb3a0 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c ATELY.# define L
fb3b0 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d OCKFILE_FAIL_IMM
fb3c0 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69 EDIATELY 1.#endi
fb3d0 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 f../*.** Acquire
fb3e0 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a a reader lock..
fb3f0 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 50 49 ** Different API
fb400 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 routines are ca
fb410 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f lled depending o
fb420 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 n whether or not
fb430 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39 this.** is Win9
fb440 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73 5 or WinNT..*/.s
fb450 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61 tatic int getRea
fb460 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70 dLock(winFile *p
fb470 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 File){. int res
fb480 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 ;. if( isNT() )
fb490 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44 {. OVERLAPPED
fb4a0 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 2e ovlp;. ovlp.
fb4b0 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f Offset = SHARED_
fb4c0 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 2e FIRST;. ovlp.
fb4d0 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 3b 0a OffsetHigh = 0;.
fb4e0 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 20 ovlp.hEvent
fb4f0 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c = 0;. res = L
fb500 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 2d ockFileEx(pFile-
fb510 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 >h, LOCKFILE_FAI
fb520 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a 20 L_IMMEDIATELY,.
fb530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fb540 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 53 49 0, SHARED_SI
fb550 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a 2f ZE, 0, &ovlp);./
fb560 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 * isNT() is 1 if
fb570 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fb580 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 ==1, so this els
fb590 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 e is never execu
fb5a0 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c ted. .*/.#if SQL
fb5b0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
fb5c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 }else{. int
fb5d0 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 lk;. sqlite3
fb5e0 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 _randomness(size
fb5f0 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20 of(lk), &lk);.
fb600 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c pFile->sharedL
fb610 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74 ockByte = (short
fb620 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66 )((lk & 0x7fffff
fb630 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45 ff)%(SHARED_SIZE
fb640 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 73 20 - 1));. res
fb650 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 = LockFile(pFile
fb660 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fb670 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c T+pFile->sharedL
fb680 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 ockByte, 0, 1, 0
fb690 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 );.#endif. }.
fb6a0 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a if( res == 0 ){.
fb6b0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 pFile->lastE
fb6c0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 rrno = GetLastEr
fb6d0 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ror();. }. ret
fb6e0 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a urn res;.}../*.*
fb6f0 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 * Undo a readloc
fb700 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 k.*/.static int
fb710 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77 unlockReadLock(w
fb720 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a inFile *pFile){.
fb730 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28 int res;. if(
fb740 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 72 isNT() ){. r
fb750 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 es = UnlockFile(
fb760 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 pFile->h, SHARED
fb770 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 _FIRST, 0, SHARE
fb780 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 D_SIZE, 0);./* i
fb790 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 sNT() is 1 if SQ
fb7a0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 LITE_OS_WINCE==1
fb7b0 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 , so this else i
fb7c0 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 s never executed
fb7d0 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 . .*/.#if SQLITE
fb7e0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d _OS_WINCE==0. }
fb7f0 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20 else{. res =
fb800 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
fb810 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fb820 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 T + pFile->share
fb830 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c dLockByte, 0, 1,
fb840 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0);.#endif. }.
fb850 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 if( res == 0 )
fb860 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 {. pFile->las
fb870 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 tErrno = GetLast
fb880 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 Error();. }. r
fb890 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a eturn res;.}../*
fb8a0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c .** Lock the fil
fb8b0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 e with the lock
fb8c0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 specified by par
fb8d0 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 ameter locktype
fb8e0 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 - one.** of the
fb8f0 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a following:.**.**
fb900 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f (1) SHARED_
fb910 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 LOCK.** (2)
fb920 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a RESERVED_LOCK.**
fb930 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 (3) PENDING
fb940 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 _LOCK.** (4)
fb950 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a EXCLUSIVE_LOCK.
fb960 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 **.** Sometimes
fb970 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 when requesting
fb980 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 one lock state,
fb990 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 additional lock
fb9a0 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e states.** are in
fb9b0 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 serted in betwee
fb9c0 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 n. The locking
fb9d0 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e might fail on on
fb9e0 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a e of the later.*
fb9f0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 * transitions le
fba00 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 aving the lock s
fba10 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 tate different f
fba20 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 rom what it star
fba30 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c ted but.** still
fba40 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f short of its go
fba50 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 al. The followi
fba60 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 ng chart shows t
fba70 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 he allowed.** tr
fba80 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 ansitions and th
fba90 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 e inserted inter
fbaa0 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a mediate states:.
fbab0 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 **.** UNLOCKE
fbac0 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 D -> SHARED.**
fbad0 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 SHARED -> RESE
fbae0 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 RVED.** SHARE
fbaf0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d D -> (PENDING) -
fbb00 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 > EXCLUSIVE.**
fbb10 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 RESERVED -> (P
fbb20 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 ENDING) -> EXCLU
fbb30 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 SIVE.** PENDI
fbb40 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a NG -> EXCLUSIVE.
fbb50 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
fbb60 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 ne will only inc
fbb70 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 rease a lock. T
fbb80 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 he winUnlock() r
fbb90 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 outine.** erases
fbba0 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e all locks at on
fbbb0 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 ce and returns u
fbbc0 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f s immediately to
fbbd0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 locking level 0
fbbe0 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 ..** It is not p
fbbf0 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 ossible to lower
fbc00 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 the locking lev
fbc10 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 el one step at a
fbc20 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d time. You.** m
fbc30 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 ust go straight
fbc40 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c to locking level
fbc50 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 0..*/.static in
fbc60 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 t winLock(sqlite
fbc70 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fbc80 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 locktype){. int
fbc90 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
fbca0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
fbcb0 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 de from subrouti
fbcc0 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 nes */. int res
fbcd0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 = 1;
fbce0 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 /* Result of a w
fbcf0 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c indows lock call
fbd00 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 */. int newLoc
fbd10 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 ktype; /*
fbd20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 Set pFile->lockt
fbd30 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 ype to this valu
fbd40 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 e before exiting
fbd50 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e */. int gotPen
fbd60 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 dingLock = 0;/*
fbd70 54 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 True if we acqui
fbd80 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f red a PENDING lo
fbd90 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a ck this time */.
fbda0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 winFile *pFile
fbdb0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b = (winFile*)id;
fbdc0 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d . DWORD error =
fbdd0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73 NO_ERROR;.. as
fbde0 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 sert( id!=0 );.
fbdf0 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 OSTRACE5("LOCK
fbe00 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 29 %d %d was %d(%d)
fbe10 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 \n",. p
fbe20 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 File->h, locktyp
fbe30 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 e, pFile->lockty
fbe40 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 pe, pFile->share
fbe50 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f dLockByte);.. /
fbe60 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c * If there is al
fbe70 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20 ready a lock of
fbe80 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72 this type or mor
fbe90 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e e restrictive on
fbea0 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65 the. ** OsFile
fbeb0 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f , do nothing. Do
fbec0 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f n't use the end_
fbed0 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c lock: exit path,
fbee0 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 as. ** sqlite3
fbef0 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 OsEnterMutex() h
fbf00 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 asn't been calle
fbf10 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 d yet.. */. if
fbf20 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 ( pFile->locktyp
fbf30 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 e>=locktype ){.
fbf40 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
fbf50 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d _OK;. }.. /* M
fbf60 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 ake sure the loc
fbf70 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 king sequence is
fbf80 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 correct. */.
fbf90 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c assert( pFile->l
fbfa0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b ocktype!=NO_LOCK
fbfb0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 || locktype==SH
fbfc0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 ARED_LOCK );. a
fbfd0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 ssert( locktype!
fbfe0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b =PENDING_LOCK );
fbff0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 . assert( lockt
fc000 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f ype!=RESERVED_LO
fc010 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 CK || pFile->loc
fc020 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f ktype==SHARED_LO
fc030 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b CK );.. /* Lock
fc040 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 the PENDING_LOC
fc050 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 K byte if we nee
fc060 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 d to acquire a P
fc070 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 ENDING lock or.
fc080 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 ** a SHARED loc
fc090 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 k. If we are ac
fc0a0 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 quiring a SHARED
fc0b0 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 lock, the acqui
fc0c0 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 sition of. ** t
fc0d0 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 he PENDING_LOCK
fc0e0 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 byte is temporar
fc0f0 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 y.. */. newLoc
fc100 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c ktype = pFile->l
fc110 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 20 ocktype;. if(
fc120 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 (pFile->locktyp
fc130 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20 e==NO_LOCK).
fc140 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 70 || ( (locktyp
fc150 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 e==EXCLUSIVE_LOC
fc160 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 K). && (
fc170 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d pFile->locktype=
fc180 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29 =RESERVED_LOCK))
fc190 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e . ){. int cn
fc1a0 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c 65 t = 3;. while
fc1b0 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65 ( cnt-->0 && (re
fc1c0 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 s = LockFile(pFi
fc1d0 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 le->h, PENDING_B
fc1e0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d YTE, 0, 1, 0))==
fc1f0 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72 0 ){. /* Tr
fc200 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74 y 3 times to get
fc210 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 the pending loc
fc220 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 k. The pending
fc230 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20 lock might be.
fc240 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61 ** held by a
fc250 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 70 72 nother reader pr
fc260 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72 ocess who will r
fc270 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74 elease it moment
fc280 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a arily.. */.
fc290 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 OSTRACE2("
fc2a0 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20 could not get a
fc2b0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e PENDING lock. cn
fc2c0 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20 t=%d\n", cnt);.
fc2d0 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20 Sleep(1);.
fc2e0 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64 }. gotPend
fc2f0 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20 ingLock = res;.
fc300 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20 if( !res ){.
fc310 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 error = Get
fc320 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
fc330 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
fc340 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f uire a shared lo
fc350 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f ck. */. if( lo
fc360 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c cktype==SHARED_L
fc370 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 OCK && res ){.
fc380 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
fc390 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f >locktype==NO_LO
fc3a0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 CK );. res =
fc3b0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
fc3c0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 e);. if( res
fc3d0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
fc3e0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f type = SHARED_LO
fc3f0 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 CK;. }else{.
fc400 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74 error = Get
fc410 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 LastError();.
fc420 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 }. }.. /* Acq
fc430 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 uire a RESERVED
fc440 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 lock. */. if(
fc450 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 locktype==RESERV
fc460 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 ED_LOCK && res )
fc470 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 {. assert( pF
fc480 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 ile->locktype==S
fc490 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 HARED_LOCK );.
fc4a0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 res = LockFile
fc4b0 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 (pFile->h, RESER
fc4c0 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 VED_BYTE, 0, 1,
fc4d0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 0);. if( res
fc4e0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b ){. newLock
fc4f0 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f type = RESERVED_
fc500 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b LOCK;. }else{
fc510 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 . error = G
fc520 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 etLastError();.
fc530 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 }. }.. /* A
fc540 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 cquire a PENDING
fc550 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 lock. */. if(
fc560 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 locktype==EXCLU
fc570 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 SIVE_LOCK && res
fc580 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74 ){. newLockt
fc590 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f ype = PENDING_LO
fc5a0 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69 CK;. gotPendi
fc5b0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a ngLock = 0;. }.
fc5c0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e . /* Acquire an
fc5d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a EXCLUSIVE lock.
fc5e0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 */. if( lockt
fc5f0 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c ype==EXCLUSIVE_L
fc600 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 OCK && res ){.
fc610 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d assert( pFile-
fc620 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 >locktype>=SHARE
fc630 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 D_LOCK );. re
fc640 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f s = unlockReadLo
fc650 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f ck(pFile);. O
fc660 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c STRACE2("unreadl
fc670 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 ock = %d\n", res
fc680 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 );. res = Loc
fc690 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 kFile(pFile->h,
fc6a0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c SHARED_FIRST, 0,
fc6b0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 SHARED_SIZE, 0)
fc6c0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b ;. if( res ){
fc6d0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 . newLockty
fc6e0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c pe = EXCLUSIVE_L
fc6f0 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a OCK;. }else{.
fc700 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 error = Ge
fc710 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 tLastError();.
fc720 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 OSTRACE2("er
fc730 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 ror-code = %d\n"
fc740 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 20 , error);.
fc750 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c getReadLock(pFil
fc760 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 e);. }. }..
fc770 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f /* If we are ho
fc780 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20 lding a PENDING
fc790 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20 lock that ought
fc7a0 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20 to be released,
fc7b0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73 then. ** releas
fc7c0 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 e it now.. */.
fc7d0 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c if( gotPendingL
fc7e0 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d ock && locktype=
fc7f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
fc800 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 UnlockFile(p
fc810 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 File->h, PENDING
fc820 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b _BYTE, 0, 1, 0);
fc830 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 . }.. /* Updat
fc840 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 e the state of t
fc850 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 he lock has held
fc860 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 in the file des
fc870 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a criptor then. *
fc880 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 * return the app
fc890 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 ropriate result
fc8a0 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 code.. */. if(
fc8b0 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d res ){. rc =
fc8c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 SQLITE_OK;. }e
fc8d0 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 lse{. OSTRACE
fc8e0 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 4("LOCK FAILED %
fc8f0 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 d trying for %d
fc900 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 but got %d\n", p
fc910 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 File->h,.
fc920 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 locktype, ne
fc930 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 wLocktype);.
fc940 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f pFile->lastErrno
fc950 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 63 = error;. rc
fc960 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a = SQLITE_BUSY;.
fc970 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 }. pFile->loc
fc980 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 77 4c ktype = (u8)newL
fc990 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 ocktype;. retur
fc9a0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 n rc;.}../*.** T
fc9b0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
fc9c0 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 ks if there is a
fc9d0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 RESERVED lock h
fc9e0 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 eld on the speci
fc9f0 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 fied.** file by
fca00 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 this or any othe
fca10 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 r process. If su
fca20 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c ch a lock is hel
fca30 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e d, return.** non
fca40 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 -zero, otherwise
fca50 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 zero..*/.static
fca60 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73 int winCheckRes
fca70 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 ervedLock(sqlite
fca80 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 3_file *id, int
fca90 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 *pResOut){. int
fcaa0 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a rc;. winFile *
fcab0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 pFile = (winFile
fcac0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 *)id;.. assert(
fcad0 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 id!=0 );. if(
fcae0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e pFile->locktype>
fcaf0 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 =RESERVED_LOCK )
fcb00 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 {. rc = 1;.
fcb10 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 OSTRACE3("TEST
fcb20 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 WR-LOCK %d %d (
fcb30 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 local)\n", pFile
fcb40 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73 ->h, rc);. }els
fcb50 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b e{. rc = Lock
fcb60 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 File(pFile->h, R
fcb70 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c ESERVED_BYTE, 0,
fcb80 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 1, 0);. if(
fcb90 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f rc ){. Unlo
fcba0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c ckFile(pFile->h,
fcbb0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 RESERVED_BYTE,
fcbc0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0, 1, 0);. }.
fcbd0 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 20 rc = !rc;.
fcbe0 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54 OSTRACE3("TEST
fcbf0 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 WR-LOCK %d %d (
fcc00 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c remote)\n", pFil
fcc10 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 e->h, rc);. }.
fcc20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a *pResOut = rc;.
fcc30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
fcc40 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 OK;.}../*.** Low
fcc50 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c er the locking l
fcc60 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 evel on file des
fcc70 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f criptor id to lo
fcc80 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 cktype. locktyp
fcc90 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 e.** must be eit
fcca0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 her NO_LOCK or S
fccb0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a HARED_LOCK..**.*
fccc0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 * If the locking
fccd0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 level of the fi
fcce0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 le descriptor is
fccf0 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 already at or b
fcd00 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 elow.** the requ
fcd10 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 ested locking le
fcd20 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e vel, this routin
fcd30 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a e is a no-op..**
fcd40 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f .** It is not po
fcd50 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20 ssible for this
fcd60 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20 routine to fail
fcd70 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 if the second ar
fcd80 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f gument.** is NO_
fcd90 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65 LOCK. If the se
fcda0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 cond argument is
fcdb0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65 SHARED_LOCK the
fcdc0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
fcdd0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53 * might return S
fcde0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a QLITE_IOERR;.*/.
fcdf0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e static int winUn
fce00 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c lock(sqlite3_fil
fce10 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 e *id, int lockt
fce20 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 ype){. int type
fce30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 ;. winFile *pFi
fce40 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 le = (winFile*)i
fce50 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 d;. int rc = SQ
fce60 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 LITE_OK;. asser
fce70 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 t( pFile!=0 );.
fce80 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 assert( locktyp
fce90 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e<=SHARED_LOCK )
fcea0 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e ;. OSTRACE5("UN
fceb0 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 LOCK %d to %d wa
fcec0 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 69 s %d(%d)\n", pFi
fced0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c le->h, locktype,
fcee0 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 . pFile
fcef0 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c ->locktype, pFil
fcf00 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 e->sharedLockByt
fcf10 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69 e);. type = pFi
fcf20 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 le->locktype;.
fcf30 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 if( type>=EXCLUS
fcf40 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 IVE_LOCK ){.
fcf50 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 UnlockFile(pFile
fcf60 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 ->h, SHARED_FIRS
fcf70 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a T, 0, SHARED_SIZ
fcf80 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c E, 0);. if( l
fcf90 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f ocktype==SHARED_
fcfa0 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 61 64 LOCK && !getRead
fcfb0 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20 Lock(pFile) ){.
fcfc0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f /* This sho
fcfd0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e uld never happen
fcfe0 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 . We should alw
fcff0 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 ays be able to.
fd000 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 ** reacquir
fd010 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 e the read lock
fd020 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 */. rc = SQ
fd030 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 LITE_IOERR_UNLOC
fd040 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 K;. }. }. i
fd050 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 f( type>=RESERVE
fd060 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e D_LOCK ){. Un
fd070 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e lockFile(pFile->
fd080 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 h, RESERVED_BYTE
fd090 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a , 0, 1, 0);. }.
fd0a0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d if( locktype==
fd0b0 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e NO_LOCK && type>
fd0c0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a =SHARED_LOCK ){.
fd0d0 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f unlockReadLo
fd0e0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20 ck(pFile);. }.
fd0f0 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 if( type>=PENDI
fd100 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 NG_LOCK ){. U
fd110 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d nlockFile(pFile-
fd120 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 >h, PENDING_BYTE
fd130 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a , 0, 1, 0);. }.
fd140 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 pFile->locktyp
fd150 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 e = (u8)locktype
fd160 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
fd170 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 ../*.** Control
fd180 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 and query of the
fd190 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c open file handl
fd1a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
fd1b0 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 winFileControl(
fd1c0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 sqlite3_file *id
fd1d0 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a , int op, void *
fd1e0 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 pArg){. switch(
fd1f0 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
fd200 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 SQLITE_FCNTL_LOC
fd210 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 KSTATE: {.
fd220 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 *(int*)pArg = ((
fd230 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f winFile*)id)->lo
fd240 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 cktype;. re
fd250 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fd260 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 }. case S
fd270 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f QLITE_LAST_ERRNO
fd280 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a : {. *(int*
fd290 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 77 )pArg = (int)((w
fd2a0 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 inFile*)id)->las
fd2b0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 tErrno;. re
fd2c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
fd2d0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
fd2e0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
fd2f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
fd300 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 the sector size
fd310 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 in bytes of the
fd320 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 underlying bloc
fd330 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 k device for.**
fd340 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 the specified fi
fd350 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f le. This is almo
fd360 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 st always 512 by
fd370 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a tes, but may be.
fd380 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f ** larger for so
fd390 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a me devices..**.*
fd3a0 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 * SQLite code as
fd3b0 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 sumes this funct
fd3c0 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e ion cannot fail.
fd3d0 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 It also assumes
fd3e0 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 that.** if two
fd3f0 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 files are create
fd400 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 d in the same fi
fd410 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 le-system direct
fd420 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 ory (i.e..** a d
fd430 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 atabase and its
fd440 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 journal file) th
fd450 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 at the sector si
fd460 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a ze will be the.*
fd470 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e * same for both.
fd480 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 .*/.static int w
fd490 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c inSectorSize(sql
fd4a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a ite3_file *id){.
fd4b0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 assert( id!=0
fd4c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 );. return (int
fd4d0 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 )(((winFile*)id)
fd4e0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 7d ->sectorSize);.}
fd4f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
fd500 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 vector of devic
fd510 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 e characteristic
fd520 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 s..*/.static int
fd530 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 winDeviceCharac
fd540 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 teristics(sqlite
fd550 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55 3_file *id){. U
fd560 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
fd570 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b id);. return 0;
fd580 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 .}../*.** This v
fd590 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c ector defines al
fd5a0 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68 l the methods th
fd5b0 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f at can operate o
fd5c0 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f n an.** sqlite3_
fd5d0 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a file for win32..
fd5e0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 */.static const
fd5f0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f sqlite3_io_metho
fd600 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d ds winIoMethod =
fd610 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 {. 1,
fd620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
fd630 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 * iVersion */.
fd640 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52 winClose,. winR
fd650 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c ead,. winWrite,
fd660 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a . winTruncate,.
fd670 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e winSync,. win
fd680 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c FileSize,. winL
fd690 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b ock,. winUnlock
fd6a0 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65 ,. winCheckRese
fd6b0 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46 rvedLock,. winF
fd6c0 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69 ileControl,. wi
fd6d0 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77 nSectorSize,. w
fd6e0 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 inDeviceCharacte
fd6f0 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a ristics.};../***
fd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd740 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65 ********.** Here
fd750 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65 ends the I/O me
fd760 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20 thods that form
fd770 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d the sqlite3_io_m
fd780 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a ethods object..*
fd790 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c *.** The next bl
fd7a0 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c ock of code impl
fd7b0 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d ements the VFS m
fd7c0 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a ethods..********
fd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
fd810 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e ****/../*.** Con
fd820 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c vert a UTF-8 fil
fd830 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65 ename into whate
fd840 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64 ver form the und
fd850 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 erlying.** opera
fd860 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74 ting system want
fd870 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20 s filenames in.
fd880 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 Space to hold t
fd890 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 he result.** is
fd8a0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 obtained from ma
fd8b0 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65 lloc and must be
fd8c0 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61 freed by the ca
fd8d0 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f lling.** functio
fd8e0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 n..*/.static voi
fd8f0 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 d *convertUtf8Fi
fd900 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61 lename(const cha
fd910 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
fd920 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
fd930 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e d = 0;. if( isN
fd940 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76 T() ){. zConv
fd950 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e erted = utf8ToUn
fd960 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 icode(zFilename)
fd970 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 ;./* isNT() is 1
fd980 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 if SQLITE_OS_WI
fd990 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 NCE==1, so this
fd9a0 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 else is never ex
fd9b0 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 ecuted. .*/.#if
fd9c0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fd9d0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
fd9e0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66 zConverted = utf
fd9f0 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d 8ToMbcs(zFilenam
fda00 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 e);.#endif. }.
fda10 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 /* caller will
fda20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65 handle out of me
fda30 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e mory */. return
fda40 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a zConverted;.}..
fda50 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 /*.** Create a t
fda60 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 emporary file na
fda70 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 me in zBuf. zBu
fda80 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e f must be big en
fda90 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 ough to.** hold
fdaa0 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e at pVfs->mxPathn
fdab0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a ame characters..
fdac0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 */.static int ge
fdad0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 tTempname(int nB
fdae0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b uf, char *zBuf){
fdaf0 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a . static char z
fdb00 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 Chars[] =. "a
fdb10 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 bcdefghijklmnopq
fdb20 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 rstuvwxyz". "
fdb30 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 ABCDEFGHIJKLMNOP
fdb40 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 QRSTUVWXYZ".
fdb50 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 "0123456789";.
fdb60 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 63 size_t i, j;. c
fdb70 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41 har zTempPath[MA
fdb80 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 28 X_PATH+1];. if(
fdb90 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 sqlite3_temp_di
fdba0 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73 rectory ){. s
fdbb0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
fdbc0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 MAX_PATH-30, zTe
fdbd0 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73 71 mpPath, "%s", sq
fdbe0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 lite3_temp_direc
fdbf0 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 69 tory);. }else i
fdc00 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 f( isNT() ){.
fdc10 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20 char *zMulti;.
fdc20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 61 WCHAR zWidePa
fdc30 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 th[MAX_PATH];.
fdc40 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d GetTempPathW(M
fdc50 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64 AX_PATH-30, zWid
fdc60 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c ePath);. zMul
fdc70 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 ti = unicodeToUt
fdc80 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20 f8(zWidePath);.
fdc90 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b if( zMulti ){
fdca0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
fdcb0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 nprintf(MAX_PATH
fdcc0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20 -30, zTempPath,
fdcd0 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20 "%s", zMulti);.
fdce0 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 69 free(zMulti
fdcf0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
fdd00 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
fdd10 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 2f E_NOMEM;. }./
fdd20 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 * isNT() is 1 if
fdd30 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
fdd40 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 ==1, so this els
fdd50 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 e is never execu
fdd60 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 ted. .** Since t
fdd70 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e he ASCII version
fdd80 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 of these Window
fdd90 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 s API do not exi
fdda0 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a st for WINCE,.**
fddb0 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 it's important
fddc0 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 to not reference
fddd0 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 them for WINCE
fdde0 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 builds..*/.#if S
fddf0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
fde00 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 0. }else{. c
fde10 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20 har *zUtf8;.
fde20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d char zMbcsPath[M
fde30 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 AX_PATH];. Ge
fde40 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50 tTempPathA(MAX_P
fde50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74 ATH-30, zMbcsPat
fde60 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20 h);. zUtf8 =
fde70 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 sqlite3_win32_mb
fde80 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73 cs_to_utf8(zMbcs
fde90 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a Path);. if( z
fdea0 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71 Utf8 ){. sq
fdeb0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d lite3_snprintf(M
fdec0 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d AX_PATH-30, zTem
fded0 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74 pPath, "%s", zUt
fdee0 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28 f8);. free(
fdef0 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73 zUtf8);. }els
fdf00 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 e{. return
fdf10 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
fdf20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 }.#endif. }.
fdf30 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 74 for(i=sqlite3St
fdf40 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68 rlen30(zTempPath
fdf50 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50 ); i>0 && zTempP
fdf60 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20 ath[i-1]=='\\';
fdf70 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61 i--){}. zTempPa
fdf80 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c th[i] = 0;. sql
fdf90 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 ite3_snprintf(nB
fdfa0 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 uf-30, zBuf,.
fdfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fdfc0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d "%s\\"SQLITE_TEM
fdfd0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a P_FILE_PREFIX, z
fdfe0 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d TempPath);. j =
fdff0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 sqlite3Strlen30
fe000 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65 (zBuf);. sqlite
fe010 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c 3_randomness(20,
fe020 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f &zBuf[j]);. fo
fe030 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b r(i=0; i<20; i++
fe040 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 , j++){. zBuf
fe050 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 [j] = (char)zCha
fe060 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 rs[ ((unsigned c
fe070 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 har)zBuf[j])%(si
fe080 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 zeof(zChars)-1)
fe090 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d ];. }. zBuf[j]
fe0a0 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 = 0;. OSTRACE2
fe0b0 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a ("TEMP FILENAME:
fe0c0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 %s\n", zBuf);.
fe0d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
fe0e0 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 K; .}../*.** The
fe0f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 return value of
fe100 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 getLastErrorMsg
fe110 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 .** is zero if t
fe120 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
fe130 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 fits in the buf
fe140 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f fer, or non-zero
fe150 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 .** otherwise (i
fe160 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 f the message wa
fe170 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f s truncated)..*/
fe180 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c .static int getL
fe190 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20 astErrorMsg(int
fe1a0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 nBuf, char *zBuf
fe1b0 29 7b 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 ){. /* FormatMe
fe1c0 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20 ssage returns 0
fe1d0 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 on failure. Oth
fe1e0 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 erwise it. ** r
fe1f0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 eturns the numbe
fe200 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74 r of TCHARs writ
fe210 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75 ten to the outpu
fe220 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 t. ** buffer, e
fe230 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72 xcluding the ter
fe240 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 minating null ch
fe250 61 72 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 44 ar.. */. DWORD
fe260 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 error = GetLast
fe270 45 72 72 6f 72 28 29 3b 0a 20 20 44 57 4f 52 44 Error();. DWORD
fe280 20 64 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68 dwLen = 0;. ch
fe290 61 72 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 0a 20 ar *zOut = 0;..
fe2a0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 if( isNT() ){.
fe2b0 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 57 WCHAR *zTempW
fe2c0 69 64 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 ide = NULL;.
fe2d0 64 77 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65 dwLen = FormatMe
fe2e0 73 73 61 67 65 57 28 46 4f 52 4d 41 54 5f 4d 45 ssageW(FORMAT_ME
fe2f0 53 53 41 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 SSAGE_ALLOCATE_B
fe300 55 46 46 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d UFFER | FORMAT_M
fe310 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 ESSAGE_FROM_SYST
fe320 45 4d 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 EM | FORMAT_MESS
fe330 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 AGE_IGNORE_INSER
fe340 54 53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 TS,.
fe350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e N
fe360 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ULL,.
fe370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe380 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 error,.
fe390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe3a0 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 0,.
fe3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe3c0 28 4c 50 57 53 54 52 29 20 26 7a 54 65 6d 70 57 (LPWSTR) &zTempW
fe3d0 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ide,.
fe3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe3f0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 0,.
fe400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29 0)
fe410 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 65 6e 20 ;. if( dwLen
fe420 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 > 0 ){. /*
fe430 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65 allocate a buffe
fe440 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f r and convert to
fe450 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a UTF8 */. z
fe460 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 Out = unicodeToU
fe470 74 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a tf8(zTempWide);.
fe480 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20 74 68 /* free th
fe490 65 20 73 79 73 74 65 6d 20 62 75 66 66 65 72 20 e system buffer
fe4a0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46 6f 72 allocated by For
fe4b0 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0a 20 20 matMessage */.
fe4c0 20 20 20 20 4c 6f 63 61 6c 46 72 65 65 28 7a 54 LocalFree(zT
fe4d0 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 20 7d 0a empWide);. }.
fe4e0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 /* isNT() is 1 i
fe4f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 f SQLITE_OS_WINC
fe500 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c E==1, so this el
fe510 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 se is never exec
fe520 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 uted. .** Since
fe530 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f the ASCII versio
fe540 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f n of these Windo
fe550 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 ws API do not ex
fe560 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a ist for WINCE,.*
fe570 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 * it's important
fe580 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 to not referenc
fe590 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 e them for WINCE
fe5a0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 builds..*/.#if
fe5b0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d SQLITE_OS_WINCE=
fe5c0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 =0. }else{.
fe5d0 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 4e 55 char *zTemp = NU
fe5e0 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 LL;. dwLen =
fe5f0 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46 FormatMessageA(F
fe600 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c ORMAT_MESSAGE_AL
fe610 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20 7c 20 LOCATE_BUFFER |
fe620 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 FORMAT_MESSAGE_F
fe630 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52 ROM_SYSTEM | FOR
fe640 4d 41 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f MAT_MESSAGE_IGNO
fe650 52 45 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 RE_INSERTS,.
fe660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe670 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 NULL,.
fe680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe690 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c 0a 20 error,.
fe6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe6b0 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 0,.
fe6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe6d0 20 20 20 20 20 20 20 20 28 4c 50 53 54 52 29 20 (LPSTR)
fe6e0 26 7a 54 65 6d 70 2c 0a 20 20 20 20 20 20 20 20 &zTemp,.
fe6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe700 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 0,.
fe710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
fe720 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 0);. if( dwL
fe730 65 6e 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 en > 0 ){.
fe740 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 /* allocate a bu
fe750 66 66 65 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 ffer and convert
fe760 20 74 6f 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 to UTF8 */.
fe770 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 zOut = sqlite3
fe780 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 _win32_mbcs_to_u
fe790 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 tf8(zTemp);.
fe7a0 20 20 2f 2a 20 66 72 65 65 20 74 68 65 20 73 79 /* free the sy
fe7b0 73 74 65 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f stem buffer allo
fe7c0 63 61 74 65 64 20 62 79 20 46 6f 72 6d 61 74 4d cated by FormatM
fe7d0 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 essage */.
fe7e0 4c 6f 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 29 LocalFree(zTemp)
fe7f0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
fe800 20 7d 0a 20 20 69 66 28 20 30 20 3d 3d 20 64 77 }. if( 0 == dw
fe810 4c 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Len ){. sqlit
fe820 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 e3_snprintf(nBuf
fe830 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 , zBuf, "OsError
fe840 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 0x%x (%u)", err
fe850 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 65 or, error);. }e
fe860 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 lse{. /* copy
fe870 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 6e 42 a maximum of nB
fe880 75 66 20 63 68 61 72 73 20 74 6f 20 6f 75 74 70 uf chars to outp
fe890 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 ut buffer */.
fe8a0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
fe8b0 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 25 f(nBuf, zBuf, "%
fe8c0 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2f s", zOut);. /
fe8d0 2a 20 66 72 65 65 20 74 68 65 20 55 54 46 38 20 * free the UTF8
fe8e0 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 66 72 buffer */. fr
fe8f0 65 65 28 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 ee(zOut);. }.
fe900 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
fe910 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a ** Open a file..
fe920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 */.static int wi
fe930 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 nOpen(. sqlite3
fe940 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
fe950 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a /* Not used *
fe960 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
fe970 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a zName, /*
fe980 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c Name of the fil
fe990 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 e (UTF-8) */. s
fe9a0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c qlite3_file *id,
fe9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 /* Writ
fe9c0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c e the SQLite fil
fe9d0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f e handle here */
fe9e0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 . int flags,
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
fea00 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 Open mode flags
fea10 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c */. int *pOutFl
fea20 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f ags /
fea30 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 * Status return
fea40 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 flags */.){. HA
fea50 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 NDLE h;. DWORD
fea60 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b dwDesiredAccess;
fea70 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65 . DWORD dwShare
fea80 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 Mode;. DWORD dw
fea90 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 CreationDisposit
feaa0 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 ion;. DWORD dwF
feab0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
feac0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 s = 0;.#if SQLIT
fead0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 E_OS_WINCE. int
feae0 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e isTemp = 0;.#en
feaf0 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 dif. winFile *p
feb00 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a File = (winFile*
feb10 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f )id;. void *zCo
feb20 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20 nverted;
feb30 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 /* File
feb40 6e 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 name in OS encod
feb50 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 ing */. const c
feb60 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d har *zUtf8Name =
feb70 20 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 zName; /* Fi
feb80 6c 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 lename in UTF-8
feb90 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 encoding */. ch
feba0 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f ar zTmpname[MAX_
febb0 50 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 PATH+1];
febc0 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74 /* Buffer used t
febd0 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69 o create temp fi
febe0 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73 lename */.. ass
febf0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 ert( id!=0 );.
fec00 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
fec10 28 70 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 (pVfs);.. /* If
fec20 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
fec30 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e ment to this fun
fec40 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 ction is NULL, g
fec50 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 enerate a . **
fec60 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e temporary file n
fec70 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f ame to use . */
fec80 0a 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d . if( !zUtf8Nam
fec90 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 e ){. int rc
feca0 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 = getTempname(MA
fecb0 58 5f 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 X_PATH+1, zTmpna
fecc0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 me);. if( rc!
fecd0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
fece0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
fecf0 20 20 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 }. zUtf8Na
fed00 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 me = zTmpname;.
fed10 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 }.. /* Convert
fed20 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f the filename to
fed30 20 74 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f the system enco
fed40 64 69 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 ding. */. zConv
fed50 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 erted = convertU
fed60 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 tf8Filename(zUtf
fed70 38 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 8Name);. if( zC
fed80 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 onverted==0 ){.
fed90 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
feda0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 _NOMEM;. }.. i
fedb0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 f( flags & SQLIT
fedc0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
fedd0 20 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 ){. dwDesire
fede0 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 dAccess = GENERI
fedf0 43 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 C_READ | GENERIC
fee00 5f 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b _WRITE;. }else{
fee10 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 . dwDesiredAc
fee20 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 cess = GENERIC_R
fee30 45 41 44 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51 EAD;. }. /* SQ
fee40 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 LITE_OPEN_EXCLUS
fee50 49 56 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d IVE is used to m
fee60 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 20 ake sure that a
fee70 6e 65 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a new file is . *
fee80 2a 20 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74 * created. SQLit
fee90 65 20 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74 e doesn't use it
feea0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78 to indicate "ex
feeb0 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 22 20 clusive access"
feec0 0a 20 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75 . ** as it is u
feed0 73 75 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f sually understoo
feee0 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 d.. */. assert
feef0 28 21 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 (!(flags & SQLIT
fef00 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 E_OPEN_EXCLUSIVE
fef10 29 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51 ) || (flags & SQ
fef20 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
fef30 29 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 ));. if( flags
fef40 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 & SQLITE_OPEN_EX
fef50 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 2f CLUSIVE ){. /
fef60 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20 * Creates a new
fef70 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 file, only if it
fef80 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 does not alread
fef90 79 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 y exist. */.
fefa0 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65 /* If the file e
fefb0 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e xists, it fails.
fefc0 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 */. dwCreati
fefd0 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 onDisposition =
fefe0 43 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 CREATE_NEW;. }e
feff0 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
ff000 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
ff010 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 TE ){. /* Ope
ff020 6e 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c n existing file,
ff030 20 6f 72 20 63 72 65 61 74 65 20 69 66 20 69 74 or create if it
ff040 20 64 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a doesn't exist *
ff050 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e /. dwCreation
ff060 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 Disposition = OP
ff070 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c EN_ALWAYS;. }el
ff080 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 se{. /* Opens
ff090 20 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 a file, only if
ff0a0 20 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 it exists. */.
ff0b0 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 dwCreationDis
ff0c0 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f position = OPEN_
ff0d0 45 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 EXISTING;. }.
ff0e0 64 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 dwShareMode = FI
ff0f0 4c 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 LE_SHARE_READ |
ff100 46 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 FILE_SHARE_WRITE
ff110 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 ;. if( flags &
ff120 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 SQLITE_OPEN_DELE
ff130 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66 TEONCLOSE ){.#if
ff140 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 SQLITE_OS_WINCE
ff150 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 . dwFlagsAndA
ff160 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 ttributes = FILE
ff170 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45 _ATTRIBUTE_HIDDE
ff180 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20 N;. isTemp =
ff190 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 46 1;.#else. dwF
ff1a0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 lagsAndAttribute
ff1b0 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 s = FILE_ATTRIBU
ff1c0 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 20 TE_TEMPORARY.
ff1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff1e0 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 | FI
ff1f0 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 LE_ATTRIBUTE_HID
ff200 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 DEN.
ff210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ff220 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 | FILE_FLAG_D
ff230 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a ELETE_ON_CLOSE;.
ff240 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a #endif. }else{.
ff250 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
ff260 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f tributes = FILE_
ff270 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c ATTRIBUTE_NORMAL
ff280 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72 ;. }. /* Repor
ff290 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 ts from the inte
ff2a0 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 65 rnet are that pe
ff2b0 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c 77 rformance is alw
ff2c0 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 ays. ** better
ff2d0 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e if FILE_FLAG_RAN
ff2e0 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 73 DOM_ACCESS is us
ff2f0 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 39 ed. Ticket #269
ff300 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 9. */.#if SQLITE
ff310 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c _OS_WINCE. dwFl
ff320 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 agsAndAttributes
ff330 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 |= FILE_FLAG_RA
ff340 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e NDOM_ACCESS;.#en
ff350 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 29 dif. if( isNT()
ff360 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61 ){. h = Crea
ff370 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29 teFileW((WCHAR*)
ff380 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 zConverted,.
ff390 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 dwDesiredAcce
ff3a0 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 ss,. dwSha
ff3b0 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e reMode,. N
ff3c0 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 ULL,. dwCr
ff3d0 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f eationDispositio
ff3e0 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 n,. dwFlag
ff3f0 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a sAndAttributes,.
ff400 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 NULL.
ff410 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
ff420 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
ff430 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
ff440 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
ff450 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
ff460 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
ff470 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
ff480 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
ff490 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
ff4a0 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
ff4b0 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
ff4c0 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
ff4d0 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
ff4e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
ff4f0 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
ff500 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c h = CreateFil
ff510 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 eA((char*)zConve
ff520 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 rted,. dwD
ff530 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 esiredAccess,.
ff540 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 dwShareMode
ff550 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 ,. NULL,.
ff560 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e dwCreation
ff570 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 Disposition,.
ff580 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 dwFlagsAndAt
ff590 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 tributes,.
ff5a0 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e NULL. );.#en
ff5b0 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d dif. }. if( h=
ff5c0 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f =INVALID_HANDLE_
ff5d0 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 65 VALUE ){. fre
ff5e0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 e(zConverted);.
ff5f0 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 if( flags & S
ff600 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
ff610 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 65 RITE ){. re
ff620 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 turn winOpen(pVf
ff630 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 s, zName, id, .
ff640 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c ((fl
ff650 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f ags|SQLITE_OPEN_
ff660 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 READONLY)&~SQLIT
ff670 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 E_OPEN_READWRITE
ff680 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 ), pOutFlags);.
ff690 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
ff6a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 return SQLITE_CA
ff6b0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 NTOPEN;. }.
ff6c0 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 }. if( pOutFlag
ff6d0 73 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 s ){. if( fla
ff6e0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e gs & SQLITE_OPEN
ff6f0 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 _READWRITE ){.
ff700 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d *pOutFlags =
ff710 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
ff720 44 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 DWRITE;. }els
ff730 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c e{. *pOutFl
ff740 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 ags = SQLITE_OPE
ff750 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 N_READONLY;.
ff760 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 }. }. memset(p
ff770 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 File, 0, sizeof(
ff780 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c *pFile));. pFil
ff790 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 e->pMethod = &wi
ff7a0 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 nIoMethod;. pFi
ff7b0 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69 le->h = h;. pFi
ff7c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 le->lastErrno =
ff7d0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c NO_ERROR;. pFil
ff7e0 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 e->sectorSize =
ff7f0 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 56 getSectorSize(pV
ff800 66 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a fs, zUtf8Name);.
ff810 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 #if SQLITE_OS_WI
ff820 4e 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 NCE. if( (flags
ff830 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f & (SQLITE_OPEN_
ff840 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 READWRITE|SQLITE
ff850 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 _OPEN_MAIN_DB))
ff860 3d 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ==.
ff870 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 (SQLITE_OPEN_R
ff880 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f EADWRITE|SQLITE_
ff890 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 OPEN_MAIN_DB).
ff8a0 20 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 && !winceCr
ff8b0 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 eateLock(zName,
ff8c0 70 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 pFile). ){.
ff8d0 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a CloseHandle(h);.
ff8e0 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 free(zConver
ff8f0 74 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e ted);. return
ff900 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e SQLITE_CANTOPEN
ff910 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 ;. }. if( isTe
ff920 6d 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d mp ){. pFile-
ff930 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 >zDeleteOnClose
ff940 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 = zConverted;.
ff950 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b }else.#endif. {
ff960 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 . free(zConve
ff970 72 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 rted);. }. Ope
ff980 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 nCounter(+1);.
ff990 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
ff9a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
ff9b0 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 e the named file
ff9c0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
ff9d0 74 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e t windows does n
ff9e0 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 ot allow a file
ff9f0 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 to be deleted if
ffa00 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 some other.** p
ffa10 72 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 rocess has it op
ffa20 65 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 en. Sometimes a
ffa30 20 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f virus scanner o
ffa40 72 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 r indexing progr
ffa50 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 am.** will open
ffa60 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 a journal file s
ffa70 68 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 hortly after it
ffa80 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 is created in or
ffa90 64 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 der to do.** wha
ffaa0 74 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 tever it does.
ffab0 57 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 While this other
ffac0 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 process is hold
ffad0 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 ing the.** file
ffae0 6f 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 open, we will be
ffaf0 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 unable to delet
ffb00 65 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 e it. To work a
ffb10 72 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 round this.** pr
ffb20 6f 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 oblem, we delay
ffb30 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 100 milliseconds
ffb40 20 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 and try to dele
ffb50 74 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a te again. Up.**
ffb60 20 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f to MX_DELETION_
ffb70 41 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f ATTEMPTs deletio
ffb80 6e 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 n attempts are r
ffb90 75 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 un before giving
ffba0 0a 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 .** up and retur
ffbb0 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a ning an error..*
ffbc0 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c /.#define MX_DEL
ffbd0 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 ETION_ATTEMPTS 5
ffbe0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 .static int winD
ffbf0 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 elete(. sqlite3
ffc00 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 _vfs *pVfs,
ffc10 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 /* Not used
ffc20 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 on win32 */. c
ffc30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
ffc40 6e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 name, /* Na
ffc50 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 me of file to de
ffc60 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 lete */. int sy
ffc70 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 ncDir
ffc80 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 /* Not use
ffc90 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b d on win32 */.){
ffca0 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a . int cnt = 0;.
ffcb0 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 DWORD rc;. DW
ffcc0 4f 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 ORD error = 0;.
ffcd0 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 void *zConverte
ffce0 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 d = convertUtf8F
ffcf0 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d ilename(zFilenam
ffd00 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 e);. UNUSED_PAR
ffd10 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
ffd20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
ffd30 28 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 (syncDir);. if(
ffd40 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 zConverted==0 )
ffd50 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
ffd60 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 ITE_NOMEM;. }.
ffd70 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 SimulateIOError
ffd80 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 (return SQLITE_I
ffd90 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 OERR_DELETE);.
ffda0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 if( isNT() ){.
ffdb0 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 do{. Dele
ffdc0 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 teFileW(zConvert
ffdd0 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 ed);. }while(
ffde0 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 ( ((rc = Ge
ffdf0 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 tFileAttributesW
ffe00 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d (zConverted)) !=
ffe10 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 INVALID_FILE_AT
ffe20 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 TRIBUTES).
ffe30 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 || ((er
ffe40 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 ror = GetLastErr
ffe50 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 or()) == ERROR_A
ffe60 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 CCESS_DENIED)).
ffe70 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b && (++
ffe80 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f cnt < MX_DELETIO
ffe90 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 N_ATTEMPTS).
ffea0 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 && (Sleep
ffeb0 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 (100), 1) );./*
ffec0 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
ffed0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
ffee0 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
ffef0 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
fff00 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
fff10 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
fff20 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
fff30 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
fff40 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
fff50 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
fff60 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
fff70 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
fff80 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
fff90 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
fffa0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b }else{. do{
fffb0 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c . DeleteFil
fffc0 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a eA(zConverted);.
fffd0 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 }while( (
fffe0 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 ((rc = GetFile
ffff0 41 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e AttributesA(zCon
10000 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 verted)) != INVA
10001 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 LID_FILE_ATTRIBU
10002 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 TES).
10003 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d || ((error =
10004 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 GetLastError())
10005 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 == ERROR_ACCESS
10006 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 _DENIED)).
10007 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c && (++cnt <
10008 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 MX_DELETION_ATT
10009 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 EMPTS).
1000a 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 && (Sleep(100)
1000b 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 , 1) );.#endif.
1000c 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 }. free(zConve
1000d 72 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 rted);. OSTRACE
1000e 32 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 2("DELETE \"%s\"
1000f 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b \n", zFilename);
10010 0a 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 . return ( (r
10011 63 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c c == INVALID_FIL
10012 45 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 E_ATTRIBUTES) .
10013 20 20 20 20 20 20 20 20 20 26 26 20 28 65 72 72 && (err
10014 6f 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 or == ERROR_FILE
10015 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 _NOT_FOUND)) ? S
10016 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 QLITE_OK : SQLIT
10017 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a E_IOERR_DELETE;.
10018 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 }../*.** Check t
10019 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 he existance and
1001a 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c status of a fil
1001b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1001c 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 winAccess(. sq
1001d 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c lite3_vfs *pVfs,
1001e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 /* Not
1001f 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f used on win32 */
10020 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
10021 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a Filename, /*
10022 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f Name of file to
10023 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 check */. int
10024 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 flags,
10025 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f /* Type o
10026 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f f test to make o
10027 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 n this file */.
10028 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 int *pResOut
10029 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1002a 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b UT: Result */.){
1002b 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 . DWORD attr;.
1002c 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 int rc = 0;. v
1002d 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 oid *zConverted
1002e 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c = convertUtf8Fil
1002f 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 ename(zFilename)
10030 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d ;. UNUSED_PARAM
10031 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 ETER(pVfs);. if
10032 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 ( zConverted==0
10033 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
10034 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
10035 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
10036 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 attr = GetFi
10037 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57 leAttributesW((W
10038 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 CHAR*)zConverted
10039 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 );./* isNT() is
1003a 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 1 if SQLITE_OS_W
1003b 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 INCE==1, so this
1003c 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 else is never e
1003d 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e xecuted. .** Sin
1003e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 ce the ASCII ver
1003f 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 sion of these Wi
10040 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 ndows API do not
10041 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 exist for WINCE
10042 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 ,.** it's import
10043 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 ant to not refer
10044 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 ence them for WI
10045 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 NCE builds..*/.#
10046 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e if SQLITE_OS_WIN
10047 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 CE==0. }else{.
10048 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c attr = GetFil
10049 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68 eAttributesA((ch
1004a 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b ar*)zConverted);
1004b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 .#endif. }. fr
1004c 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a ee(zConverted);.
1004d 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 switch( flags
1004e 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 ){. case SQLI
1004f 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a TE_ACCESS_READ:.
10050 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
10051 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 ACCESS_EXISTS:.
10052 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d rc = attr!=
10053 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 INVALID_FILE_ATT
10054 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 RIBUTES;. b
10055 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 reak;. case S
10056 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 QLITE_ACCESS_REA
10057 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 DWRITE:. rc
10058 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f = (attr & FILE_
10059 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e ATTRIBUTE_READON
1005a 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 LY)==0;. br
1005b 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 eak;. default
1005c 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 :. assert(!
1005d 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 "Invalid flags a
1005e 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 rgument");. }.
1005f 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a *pResOut = rc;.
10060 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
10061 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 OK;.}.../*.** Tu
10062 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 rn a relative pa
10063 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 thname into a fu
10064 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 ll pathname. Wr
10065 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 ite the full.**
10066 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f pathname into zO
10067 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 ut[]. zOut[] wi
10068 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 ll be at least p
10069 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a Vfs->mxPathname.
1006a 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 ** bytes in size
1006b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1006c 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 winFullPathname(
1006d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
1006e 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 pVfs,
1006f 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 /* Pointer to v
10070 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 fs object */. c
10071 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 onst char *zRela
10072 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 tive, /*
10073 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 Possibly relativ
10074 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a e input path */.
10075 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 int nFull,
10076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10077 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 /* Size of outpu
10078 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 t buffer in byte
10079 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 s */. char *zFu
1007a 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ll
1007b 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 /* Output b
1007c 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 uffer */.){. .#
1007d 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 if defined(__CYG
1007e 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f WIN__). UNUSED_
1007f 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 PARAMETER(nFull)
10080 3b 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f ;. cygwin_conv_
10081 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 to_full_win32_pa
10082 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 th(zRelative, zF
10083 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 ull);. return S
10084 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 QLITE_OK;.#endif
10085 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f ..#if SQLITE_OS_
10086 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 WINCE. UNUSED_P
10087 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b ARAMETER(nFull);
10088 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 . /* WinCE has
10089 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 no concept of a
1008a 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d relative pathnam
1008b 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f e, or so I am to
1008c 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ld. */. sqlite3
1008d 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e _snprintf(pVfs->
1008e 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c mxPathname, zFul
1008f 6c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 l, "%s", zRelati
10090 76 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 ve);. return SQ
10091 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a LITE_OK;.#endif.
10092 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f .#if !SQLITE_OS_
10093 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 WINCE && !define
10094 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 d(__CYGWIN__).
10095 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 int nByte;. voi
10096 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 d *zConverted;.
10097 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 char *zOut;. U
10098 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
10099 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 nFull);. zConve
1009a 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 rted = convertUt
1009b 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 f8Filename(zRela
1009c 74 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e tive);. if( isN
1009d 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 T() ){. WCHAR
1009e 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 *zTemp;. nBy
1009f 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 te = GetFullPath
100a0 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 NameW((WCHAR*)zC
100a1 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 onverted, 0, 0,
100a2 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 0) + 3;. zTem
100a3 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 p = malloc( nByt
100a4 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 e*sizeof(zTemp[0
100a5 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 ]) );. if( zT
100a6 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 emp==0 ){.
100a7 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 free(zConverted)
100a8 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 ;. return S
100a9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
100aa 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 }. GetFullPa
100ab 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 thNameW((WCHAR*)
100ac 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 zConverted, nByt
100ad 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 e, zTemp, 0);.
100ae 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 free(zConverte
100af 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 d);. zOut = u
100b0 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 nicodeToUtf8(zTe
100b1 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 mp);. free(zT
100b2 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 emp);./* isNT()
100b3 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f is 1 if SQLITE_O
100b4 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 S_WINCE==1, so t
100b5 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 his else is neve
100b6 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 r executed. .**
100b7 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 Since the ASCII
100b8 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 version of these
100b9 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 Windows API do
100ba 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 not exist for WI
100bb 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 NCE,.** it's imp
100bc 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 ortant to not re
100bd 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 ference them for
100be 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a WINCE builds..*
100bf 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f /.#if SQLITE_OS_
100c0 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 WINCE==0. }else
100c1 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d {. char *zTem
100c2 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 p;. nByte = G
100c3 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 etFullPathNameA(
100c4 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 (char*)zConverte
100c5 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b d, 0, 0, 0) + 3;
100c6 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c . zTemp = mal
100c7 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f loc( nByte*sizeo
100c8 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 f(zTemp[0]) );.
100c9 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 if( zTemp==0
100ca 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 ){. free(zC
100cb 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 onverted);.
100cc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
100cd 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
100ce 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 GetFullPathNameA
100cf 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 ((char*)zConvert
100d0 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 ed, nByte, zTemp
100d1 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a , 0);. free(z
100d2 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 Converted);.
100d3 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 zOut = sqlite3_w
100d4 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 in32_mbcs_to_utf
100d5 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 8(zTemp);. fr
100d6 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 ee(zTemp);.#endi
100d7 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 f. }. if( zOut
100d8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f ){. sqlite3_
100d9 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d snprintf(pVfs->m
100da 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c xPathname, zFull
100db 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 , "%s", zOut);.
100dc 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 free(zOut);.
100dd 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
100de 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 _OK;. }else{.
100df 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
100e0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 NOMEM;. }.#endi
100e1 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 f.}../*.** Get t
100e2 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f he sector size o
100e3 66 20 74 68 65 20 64 65 76 69 63 65 20 75 73 65 f the device use
100e4 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69 d to store.** fi
100e5 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e le..*/.static in
100e6 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 t getSectorSize(
100e7 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 . sqlite3_vfs
100e8 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 *pVfs,. cons
100e9 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 t char *zRelativ
100ea 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 e /* UTF-8 f
100eb 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 ile name */.){.
100ec 20 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 53 DWORD bytesPerS
100ed 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 ector = SQLITE_D
100ee 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 EFAULT_SECTOR_SI
100ef 5a 45 3b 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b ZE;. /* GetDisk
100f0 46 72 65 65 53 70 61 63 65 20 69 73 20 6e 6f 74 FreeSpace is not
100f1 20 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72 supported under
100f2 20 57 49 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51 WINCE */.#if SQ
100f3 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 LITE_OS_WINCE.
100f4 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
100f5 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 (pVfs);. UNUSED
100f6 5f 50 41 52 41 4d 45 54 45 52 28 7a 52 65 6c 61 _PARAMETER(zRela
100f7 74 69 76 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63 tive);.#else. c
100f8 68 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 har zFullpath[MA
100f9 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 X_PATH+1];. int
100fa 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 rc;. DWORD dwR
100fb 65 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 et = 0;. DWORD
100fc 64 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 dwDummy;.. /*.
100fd 20 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 67 ** We need to g
100fe 65 74 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 et the full path
100ff 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c name of the fil
10100 65 0a 20 20 2a 2a 20 74 6f 20 67 65 74 20 74 68 e. ** to get th
10101 65 20 64 72 69 76 65 20 6c 65 74 74 65 72 20 74 e drive letter t
10102 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 o look up the se
10103 63 74 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a ctor. ** size..
10104 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46 */. rc = winF
10105 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 ullPathname(pVfs
10106 2c 20 7a 52 65 6c 61 74 69 76 65 2c 20 4d 41 58 , zRelative, MAX
10107 5f 50 41 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68 _PATH, zFullpath
10108 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 53 );. if( rc == S
10109 51 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20 QLITE_OK ). {.
1010a 20 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 void *zConver
1010b 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 ted = convertUtf
1010c 38 46 69 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70 8Filename(zFullp
1010d 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 ath);. if( zC
1010e 6f 6e 76 65 72 74 65 64 20 29 7b 0a 20 20 20 20 onverted ){.
1010f 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a if( isNT() ){.
10110 20 20 20 20 20 20 20 20 2f 2a 20 74 72 69 6d 20 /* trim
10111 70 61 74 68 20 74 6f 20 6a 75 73 74 20 64 72 69 path to just dri
10112 76 65 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a ve reference */.
10113 20 20 20 20 20 20 20 20 57 43 48 41 52 20 2a 70 WCHAR *p
10114 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 = zConverted;.
10115 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 for(;*p;p
10116 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 ++){. i
10117 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b f( *p == '\\' ){
10118 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 . *p
10119 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 = '\0';.
1011a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
1011b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
1011c 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d . dwRet =
1011d 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 GetDiskFreeSpac
1011e 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 eW((WCHAR*)zConv
1011f 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 erted,.
10120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10121 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d &dwDumm
10122 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 y,.
10123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10124 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 &bytesPerSe
10125 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 ctor,.
10126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10127 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 &dwDummy
10128 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
10129 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1012a 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 &dwDummy);.
1012b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1012c 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 /* trim path
1012d 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 to just drive r
1012e 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 eference */.
1012f 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20 28 63 char *p = (c
10130 68 61 72 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 har *)zConverted
10131 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a ;. for(;*
10132 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 p;p++){.
10133 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 if( *p == '\\'
10134 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
10135 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 *p = '\0';.
10136 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
10137 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
10138 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 }. dwRe
10139 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 t = GetDiskFreeS
1013a 70 61 63 65 41 28 28 63 68 61 72 2a 29 7a 43 6f paceA((char*)zCo
1013b 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 nverted,.
1013c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1013d 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 &dwDu
1013e 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 mmy,.
1013f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10140 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72 &bytesPer
10141 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 Sector,.
10142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10143 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d &dwDum
10144 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 my,.
10145 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10146 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b &dwDummy);
10147 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 . }. f
10148 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
10149 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 . }. if( !
1014a 64 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 62 dwRet ){. b
1014b 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 ytesPerSector =
1014c 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 SQLITE_DEFAULT_S
1014d 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20 ECTOR_SIZE;.
1014e 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 }. }.#endif. r
1014f 65 74 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65 eturn (int) byte
10150 73 50 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a sPerSector; .}..
10151 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
10152 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 MIT_LOAD_EXTENSI
10153 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 ON./*.** Interfa
10154 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 ces for opening
10155 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 a shared library
10156 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 , finding entry
10157 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e points.** within
10158 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
10159 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 ary, and closing
1015a 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 the shared libr
1015b 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e ary..*/./*.** In
1015c 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 terfaces for ope
1015d 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 ning a shared li
1015e 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 brary, finding e
1015f 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 ntry points.** w
10160 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 ithin the shared
10161 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c library, and cl
10162 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 osing the shared
10163 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 library..*/.sta
10164 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f tic void *winDlO
10165 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 pen(sqlite3_vfs
10166 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 *pVfs, const cha
10167 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 r *zFilename){.
10168 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 HANDLE h;. voi
10169 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 d *zConverted =
1016a 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e convertUtf8Filen
1016b 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a ame(zFilename);.
1016c 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 UNUSED_PARAMET
1016d 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 ER(pVfs);. if(
1016e 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b zConverted==0 ){
1016f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
10170 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 }. if( isNT()
10171 29 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c ){. h = LoadL
10172 69 62 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 ibraryW((WCHAR*)
10173 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 zConverted);./*
10174 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 isNT() is 1 if S
10175 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d QLITE_OS_WINCE==
10176 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 1, so this else
10177 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 is never execute
10178 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 d. .** Since the
10179 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f ASCII version o
1017a 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 f these Windows
1017b 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 API do not exist
1017c 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 for WINCE,.** i
1017d 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f t's important to
1017e 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 not reference t
1017f 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 hem for WINCE bu
10180 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c ilds..*/.#if SQL
10181 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a ITE_OS_WINCE==0.
10182 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d }else{. h =
10183 20 4c 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 LoadLibraryA((c
10184 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 har*)zConverted)
10185 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 ;.#endif. }. f
10186 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b ree(zConverted);
10187 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a . return (void*
10188 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 )h;.}.static voi
10189 64 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c d winDlError(sql
1018a 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 ite3_vfs *pVfs,
1018b 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a int nBuf, char *
1018c 7a 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 zBufOut){. UNUS
1018d 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 ED_PARAMETER(pVf
1018e 73 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 s);. getLastErr
1018f 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 orMsg(nBuf, zBuf
10190 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 Out);.}.void (*w
10191 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f inDlSym(sqlite3_
10192 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 vfs *pVfs, void
10193 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 *pHandle, const
10194 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 char *zSymbol))(
10195 76 6f 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f void){. UNUSED_
10196 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b PARAMETER(pVfs);
10197 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
10198 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 INCE. /* The Ge
10199 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 tProcAddressA()
1019a 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 routine is only
1019b 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e available on win
1019c 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 ce. */. return
1019d 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 (void(*)(void))G
1019e 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 etProcAddressA((
1019f 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 HANDLE)pHandle,
101a0 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a zSymbol);.#else.
101a1 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 /* All other w
101a2 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 indows platforms
101a3 20 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 expect GetProcA
101a4 64 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 ddress() to take
101a5 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 . ** an Ansi st
101a6 72 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 ring regardless
101a7 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 of the _UNICODE
101a8 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 setting */. ret
101a9 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 urn (void(*)(voi
101aa 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 d))GetProcAddres
101ab 73 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c s((HANDLE)pHandl
101ac 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e e, zSymbol);.#en
101ad 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c dif.}.void winDl
101ae 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 Close(sqlite3_vf
101af 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 s *pVfs, void *p
101b0 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 Handle){. UNUSE
101b1 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
101b2 29 3b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 );. FreeLibrary
101b3 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 ((HANDLE)pHandle
101b4 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 );.}.#else /* if
101b5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 SQLITE_OMIT_LOA
101b6 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 D_EXTENSION is d
101b7 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 efined: */. #de
101b8 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 fine winDlOpen
101b9 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 0. #define winD
101ba 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 lError 0. #defi
101bb 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a ne winDlSym 0.
101bc 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 #define winDlC
101bd 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a lose 0.#endif...
101be 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 /*.** Write up t
101bf 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 o nBuf bytes of
101c0 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 randomness into
101c1 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 zBuf..*/.static
101c2 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 int winRandomnes
101c3 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 s(sqlite3_vfs *p
101c4 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 Vfs, int nBuf, c
101c5 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e har *zBuf){. in
101c6 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 t n = 0;. UNUSE
101c7 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 D_PARAMETER(pVfs
101c8 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 );.#if defined(S
101c9 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 QLITE_TEST). n
101ca 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 = nBuf;. memset
101cb 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b (zBuf, 0, nBuf);
101cc 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a .#else. if( siz
101cd 65 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c eof(SYSTEMTIME)<
101ce 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 =nBuf-n ){. S
101cf 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 YSTEMTIME x;.
101d0 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 GetSystemTime(&
101d1 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 x);. memcpy(&
101d2 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a zBuf[n], &x, siz
101d3 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b eof(x));. n +
101d4 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d = sizeof(x);. }
101d5 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 . if( sizeof(DW
101d6 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a ORD)<=nBuf-n ){.
101d7 20 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 DWORD pid =
101d8 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 GetCurrentProces
101d9 73 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 sId();. memcp
101da 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 y(&zBuf[n], &pid
101db 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a , sizeof(pid));.
101dc 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 n += sizeof(
101dd 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 pid);. }. if(
101de 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e sizeof(DWORD)<=n
101df 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f Buf-n ){. DWO
101e0 52 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b RD cnt = GetTick
101e1 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d Count();. mem
101e2 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 cpy(&zBuf[n], &c
101e3 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 nt, sizeof(cnt))
101e4 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f ;. n += sizeo
101e5 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 f(cnt);. }. if
101e6 28 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 ( sizeof(LARGE_I
101e7 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 NTEGER)<=nBuf-n
101e8 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 ){. LARGE_INT
101e9 45 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72 EGER i;. Quer
101ea 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e yPerformanceCoun
101eb 74 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d ter(&i);. mem
101ec 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 cpy(&zBuf[n], &i
101ed 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 , sizeof(i));.
101ee 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 n += sizeof(i)
101ef 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
101f0 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a eturn n;.}.../*.
101f1 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c ** Sleep for a l
101f2 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 ittle while. Re
101f3 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 turn the amount
101f4 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a of time slept..*
101f5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e /.static int win
101f6 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 Sleep(sqlite3_vf
101f7 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 s *pVfs, int mic
101f8 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 rosec){. Sleep(
101f9 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 (microsec+999)/1
101fa 30 30 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 000);. UNUSED_P
101fb 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a ARAMETER(pVfs);.
101fc 20 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f return ((micro
101fd 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 sec+999)/1000)*1
101fe 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 000;.}../*.** Th
101ff 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 e following vari
10200 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 able, if set to
10201 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 a non-zero value
10202 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 , becomes the re
10203 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 sult.** returned
10204 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 from sqlite3OsC
10205 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 urrentTime(). T
10206 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 his is used for
10207 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 testing..*/.#ifd
10208 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 ef SQLITE_TEST.S
10209 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1020a 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 lite3_current_ti
1020b 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a me = 0;.#endif..
1020c 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 /*.** Find the c
1020d 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 urrent time (in
1020e 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 Universal Coordi
1020f 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 nated Time). Wr
10210 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 ite the.** curre
10211 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 nt time and date
10212 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 as a Julian Day
10213 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 number into *pr
10214 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 Now and.** retur
10215 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 n 0. Return 1 i
10216 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 f the time and d
10217 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f ate cannot be fo
10218 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 und..*/.int winC
10219 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 urrentTime(sqlit
1021a 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f e3_vfs *pVfs, do
1021b 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 uble *prNow){.
1021c 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f FILETIME ft;. /
1021d 2a 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 * FILETIME struc
1021e 74 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 ture is a 64-bit
1021f 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 value represent
10220 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ing the number o
10221 66 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f f . 100-nano
10222 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 second intervals
10223 20 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 since January 1
10224 2c 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 , 1601 (= JD 230
10225 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 5813.5). . */.
10226 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 sqlite3_int64 t
10227 69 6d 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 imeW; /* Whole
10228 20 64 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 days */. sqlit
10229 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 e3_int64 timeF;
1022a 20 20 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 /* Fractional
1022b 44 61 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 Days */.. /* Nu
1022c 6d 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f mber of 100-nano
1022d 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 second intervals
1022e 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79 in a single day
1022f 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e */. static con
10230 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 st sqlite3_int64
10231 20 6e 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20 ntuPerDay = .
10232 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 10000000*(sq
10233 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 lite3_int64)8640
10234 30 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 0;.. /* Number
10235 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e of 100-nanosecon
10236 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 d intervals in h
10237 61 6c 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a alf of a day */.
10238 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 static const s
10239 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 qlite3_int64 ntu
1023a 50 65 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 PerHalfDay = .
1023b 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 10000000*(sq
1023c 6c 69 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30 lite3_int64)4320
1023d 30 3b 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 0;.. /* 2^32 -
1023e 74 6f 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20 to avoid use of
1023f 4c 4c 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 LL and warnings
10240 69 6e 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 in gcc */. stat
10241 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 ic const sqlite3
10242 5f 69 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56 _int64 max32BitV
10243 61 6c 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73 alue = . (s
10244 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 qlite3_int64)200
10245 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 0000000 + (sqlit
10246 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 e3_int64)2000000
10247 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 000 + (sqlite3_i
10248 6e 74 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a nt64)294967296;.
10249 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 .#if SQLITE_OS_W
1024a 49 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d INCE. SYSTEMTIM
1024b 45 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 E time;. GetSys
1024c 74 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a temTime(&time);.
1024d 20 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 /* if SystemTi
1024e 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 meToFileTime() f
1024f 61 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 ails, it returns
10250 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 zero. */. if (
10251 21 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c !SystemTimeToFil
10252 65 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 eTime(&time,&ft)
10253 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b ){. return 1;
10254 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 . }.#else. Get
10255 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 SystemTimeAsFile
10256 54 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e Time( &ft );.#en
10257 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 dif. UNUSED_PAR
10258 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 AMETER(pVfs);.
10259 74 69 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74 timeW = (((sqlit
1025a 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69 e3_int64)ft.dwHi
1025b 67 68 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 ghDateTime)*max3
1025c 32 42 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71 2BitValue) + (sq
1025d 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 lite3_int64)ft.d
1025e 77 4c 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 wLowDateTime;.
1025f 74 69 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20 timeF = timeW %
10260 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 ntuPerDay;
10261 20 20 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 /* fractiona
10262 6c 20 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f l days (100-nano
10263 73 65 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 seconds) */. ti
10264 6d 65 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 meW = timeW / nt
10265 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 uPerDay;
10266 20 20 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 /* whole days
10267 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d */. timeW = tim
10268 65 57 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20 eW + 2305813;
10269 20 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20 /* add
1026a 77 68 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d whole days (from
1026b 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 2305813.5) */.
1026c 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b timeF = timeF +
1026d 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20 ntuPerHalfDay;
1026e 20 20 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 /* add half
1026f 20 61 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30 a day (from 230
10270 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 5813.5) */. tim
10271 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 eW = timeW + (ti
10272 6d 65 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 meF/ntuPerDay);
10273 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 /* add whole da
10274 79 20 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61 y if half day ma
10275 64 65 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 de one */. time
10276 46 20 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50 F = timeF % ntuP
10277 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 erDay;
10278 2f 2a 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 /* compute new f
10279 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a ractional days *
1027a 2f 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f /. *prNow = (do
1027b 75 62 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64 uble)timeW + ((d
1027c 6f 75 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 ouble)timeF / (d
1027d 6f 75 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29 ouble)ntuPerDay)
1027e 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1027f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 TEST. if( sqlit
10280 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 e3_current_time
10281 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 ){. *prNow =
10282 28 28 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 ((double)sqlite3
10283 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 _current_time +
10284 28 64 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f (double)43200) /
10285 20 28 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b (double)86400 +
10286 20 28 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37 (double)2440587
10287 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
10288 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
10289 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68 * The idea is th
1028a 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e at this function
1028b 20 77 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f works like a co
1028c 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 mbination of.**
1028d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61 GetLastError() a
1028e 6e 64 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 nd FormatMessage
1028f 28 29 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f () on windows (o
10290 72 20 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 r errno and.** s
10291 74 72 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 trerror_r() on u
10292 6e 69 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 nix). After an e
10293 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 rror is returned
10294 20 62 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e by an OS.** fun
10295 63 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 ction, SQLite ca
10296 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f lls this functio
10297 6e 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e n with zBuf poin
10298 74 69 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 ting to.** a buf
10299 66 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 fer of nBuf byte
1029a 73 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 s. The OS layer
1029b 73 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 should populate
1029c 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 the.** buffer wi
1029d 74 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 th a nul-termina
1029e 74 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 ted UTF-8 encode
1029f 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a d error message.
102a0 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 ** describing th
102a1 65 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20 e last IO error
102a2 74 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 to have occurred
102a3 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c within the call
102a4 69 6e 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a ing.** thread..*
102a5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f *.** If the erro
102a6 72 20 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f r message is too
102a7 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 large for the s
102a8 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a upplied buffer,.
102a9 2a 2a 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 ** it should be
102aa 74 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72 truncated. The r
102ab 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 eturn value of x
102ac 47 65 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 GetLastError.**
102ad 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 is zero if the e
102ae 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 rror message fit
102af 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c s in the buffer,
102b0 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 or non-zero.**
102b1 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 otherwise (if th
102b2 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 e message was tr
102b3 75 6e 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e uncated). If non
102b4 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 -zero is returne
102b5 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 d,.** then it is
102b6 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 not necessary t
102b7 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 o include the nu
102b8 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 l-terminator cha
102b9 72 61 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 racter.** in the
102ba 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a output buffer..
102bb 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 **.** Not supply
102bc 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 ing an error mes
102bd 73 61 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e sage will have n
102be 6f 20 61 64 76 65 72 73 65 20 65 66 66 65 63 74 o adverse effect
102bf 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 .** on SQLite. I
102c0 74 20 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76 t is fine to hav
102c1 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 e an implementat
102c2 69 6f 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a ion that never.*
102c3 2a 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 * returns an err
102c4 6f 72 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a or message:.**.*
102c5 2a 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 * int xGetLast
102c6 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 Error(sqlite3_vf
102c7 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 s *pVfs, int nBu
102c8 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a f, char *zBuf){.
102c9 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 ** assert(zB
102ca 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a uf[0]=='\0');.**
102cb 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a return 0;.*
102cc 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 * }.**.** Howe
102cd 76 65 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 ver if an error
102ce 6d 65 73 73 61 67 65 20 69 73 20 73 75 70 70 6c message is suppl
102cf 69 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 ied, it will be
102d0 69 6e 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 incorporated.**
102d1 62 79 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 by sqlite into t
102d2 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 he error message
102d3 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 available to th
102d4 65 20 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 e user using.**
102d5 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 sqlite3_errmsg()
102d6 2c 20 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e , possibly makin
102d7 67 20 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69 g IO errors easi
102d8 65 72 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a er to debug..*/.
102d9 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65 static int winGe
102da 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 tLastError(sqlit
102db 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e e3_vfs *pVfs, in
102dc 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 t nBuf, char *zB
102dd 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 uf){. UNUSED_PA
102de 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 RAMETER(pVfs);.
102df 20 72 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45 return getLastE
102e0 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 rrorMsg(nBuf, zB
102e1 75 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e uf);.}../*.** In
102e2 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 itialize and dei
102e3 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 nitialize the op
102e4 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 erating system i
102e5 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c nterface..*/.SQL
102e6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
102e7 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 te3_os_init(void
102e8 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 ){. static sqli
102e9 74 65 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d te3_vfs winVfs =
102ea 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 {. 1,
102eb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
102ec 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a rsion */. siz
102ed 65 6f 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 eof(winFile),
102ee 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 /* szOsFile */.
102ef 20 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 MAX_PATH,
102f0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e /* mxPathn
102f1 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 ame */. 0,
102f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
102f3 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 pNext */. "w
102f4 69 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20 20 in32",
102f5 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 /* zName */.
102f6 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
102f7 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 /* pAppData
102f8 2a 2f 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e */. . winOpen
102f9 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 , /* x
102fa 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 Open */. winD
102fb 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f elete, /
102fc 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 * xDelete */.
102fd 20 77 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20 winAccess,
102fe 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a /* xAccess *
102ff 2f 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 /. winFullPat
10300 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c hname, /* xFul
10301 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 lPathname */.
10302 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 winDlOpen,
10303 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a /* xDlOpen *
10304 2f 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 /. winDlError
10305 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 , /* xDlE
10306 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 rror */. winD
10307 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f lSym, /
10308 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 * xDlSym */.
10309 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 winDlClose,
1030a 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a /* xDlClose *
1030b 2f 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e /. winRandomn
1030c 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e ess, /* xRan
1030d 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 domness */. w
1030e 69 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 inSleep,
1030f 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 /* xSleep */.
10310 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d winCurrentTim
10311 65 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e e, /* xCurren
10312 74 54 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e tTime */. win
10313 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 GetLastError
10314 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 /* xGetLastError
10315 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69 */. };.. sqli
10316 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 te3_vfs_register
10317 28 26 77 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 (&winVfs, 1);.
10318 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
10319 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 ; .}.SQLITE_API
1031a 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 int sqlite3_os_e
1031b 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 nd(void){ . ret
1031c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1031d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ..#endif /* SQLI
1031e 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a TE_OS_WIN */../*
1031f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
10320 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a d of os_win.c **
10321 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10322 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
10325 67 69 6e 20 66 69 6c 65 20 62 69 74 76 65 63 2e gin file bitvec.
10326 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
10327 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10328 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10329 0a 2a 2a 20 32 30 30 38 20 46 65 62 72 75 61 72 .** 2008 Februar
1032a 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 y 16.**.** The a
1032b 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 uthor disclaims
1032c 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 copyright to thi
1032d 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 s source code.
1032e 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 In place of.** a
1032f 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 legal notice, h
10330 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e ere is a blessin
10331 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 g:.**.** May
10332 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 you do good and
10333 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 not evil..**
10334 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 May you find for
10335 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 giveness for you
10336 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 rself and forgiv
10337 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 e others..**
10338 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 May you share fr
10339 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 eely, never taki
1033a 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 ng more than you
1033b 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a give..**.******
1033c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1033f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10340 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 ***.** This file
10341 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f implements an o
10342 62 6a 65 63 74 20 74 68 61 74 20 72 65 70 72 65 bject that repre
10343 73 65 6e 74 73 20 61 20 66 69 78 65 64 2d 6c 65 sents a fixed-le
10344 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 ngth.** bitmap.
10345 20 42 69 74 73 20 61 72 65 20 6e 75 6d 62 65 72 Bits are number
10346 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 ed starting with
10347 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 1..**.** A bitm
10348 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 ap is used to re
10349 63 6f 72 64 20 77 68 69 63 68 20 70 61 67 65 73 cord which pages
1034a 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 of a database f
1034b 69 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a ile have been.**
1034c 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 journalled duri
1034d 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ng a transaction
1034e 2c 20 6f 72 20 77 68 69 63 68 20 70 61 67 65 73 , or which pages
1034f 20 68 61 76 65 20 74 68 65 20 22 64 6f 6e 74 2d have the "dont-
10350 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 write".** proper
10351 74 79 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c ty. Usually onl
10352 79 20 61 20 66 65 77 20 70 61 67 65 73 20 61 72 y a few pages ar
10353 65 20 6d 65 65 74 20 65 69 74 68 65 72 20 63 6f e meet either co
10354 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 ndition..** So t
10355 68 65 20 62 69 74 6d 61 70 20 69 73 20 75 73 75 he bitmap is usu
10356 61 6c 6c 79 20 73 70 61 72 73 65 20 61 6e 64 20 ally sparse and
10357 68 61 73 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c has low cardinal
10358 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 ity..** But some
10359 74 69 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 times (for examp
1035a 6c 65 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61 le when during a
1035b 20 44 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 DROP of a large
1035c 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 table) most.**
1035d 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 or all of the pa
1035e 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 ges in a databas
1035f 65 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 e can get journa
10360 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 lled. In those
10361 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 cases, .** the b
10362 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65 itmap becomes de
10363 6e 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61 nse with high ca
10364 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 rdinality. The
10365 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 algorithm needs
10366 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f .** to handle bo
10367 74 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a th cases well..*
10368 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 *.** The size of
10369 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 66 the bitmap is f
1036a 69 78 65 64 20 77 68 65 6e 20 74 68 65 20 6f 62 ixed when the ob
1036b 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 2e ject is created.
1036c 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 .**.** All bits
1036d 61 72 65 20 63 6c 65 61 72 20 77 68 65 6e 20 74 are clear when t
1036e 68 65 20 62 69 74 6d 61 70 20 69 73 20 63 72 65 he bitmap is cre
1036f 61 74 65 64 2e 20 20 49 6e 64 69 76 69 64 75 61 ated. Individua
10370 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 l bits.** may be
10371 20 73 65 74 20 6f 72 20 63 6c 65 61 72 65 64 20 set or cleared
10372 6f 6e 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a one at a time..*
10373 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65 72 61 74 *.** Test operat
10374 69 6f 6e 73 20 61 72 65 20 61 62 6f 75 74 20 31 ions are about 1
10375 30 30 20 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 00 times more co
10376 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74 20 6f 70 mmon that set op
10377 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 erations..** Cle
10378 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 ar operations ar
10379 65 20 65 78 63 65 65 64 69 6e 67 6c 79 20 72 61 e exceedingly ra
1037a 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 75 re. There are u
1037b 73 75 61 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a sually between.*
1037c 2a 20 35 20 61 6e 64 20 35 30 30 20 73 65 74 20 * 5 and 500 set
1037d 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 72 20 42 operations per B
1037e 69 74 76 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 itvec object, th
1037f 6f 75 67 68 20 74 68 65 20 6e 75 6d 62 65 72 20 ough the number
10380 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 of sets can.** s
10381 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77 20 69 6e ometimes grow in
10382 74 6f 20 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 to tens of thous
10383 61 6e 64 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 ands or larger.
10384 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 The size of the
10385 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63 .** Bitvec objec
10386 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 t is the number
10387 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 of pages in the
10388 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 database file at
10389 20 74 68 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 the.** start of
1038a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 a transaction,
1038b 61 6e 64 20 69 73 20 74 68 75 73 20 75 73 75 61 and is thus usua
1038c 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61 20 lly less than a
1038d 66 65 77 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a few thousand,.**
1038e 20 62 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c but can be as l
1038f 61 72 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f arge as 2 billio
10390 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 n for a really b
10391 69 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a ig database..*/.
10392 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 ./* Size of the
10393 42 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65 Bitvec structure
10394 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 23 64 in bytes. */.#d
10395 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 20 efine BITVEC_SZ
10396 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 76 (sizeof(v
10397 6f 69 64 2a 29 2a 31 32 38 29 20 20 2f 2a 20 35 oid*)*128) /* 5
10398 31 32 20 6f 6e 20 33 32 62 69 74 2e 20 20 31 30 12 on 32bit. 10
10399 32 34 20 6f 6e 20 36 34 62 69 74 20 2a 2f 0a 0a 24 on 64bit */..
1039a 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 /* Round the uni
1039b 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 on size down to
1039c 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e the nearest poin
1039d 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 ter boundary, si
1039e 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a nce that's how .
1039f 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c ** it will be al
103a0 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 igned within the
103a1 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20 Bitvec struct.
103a2 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
103a3 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42 C_USIZE (((B
103a4 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 ITVEC_SZ-(3*size
103a5 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 of(u32)))/sizeof
103a6 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f (Bitvec*))*sizeo
103a7 66 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 f(Bitvec*))../*
103a8 54 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61 Type of the arra
103a9 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 y "element" for
103aa 74 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65 the bitmap repre
103ab 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 sentation. .** S
103ac 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72 hould be a power
103ad 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c of 2, and ideal
103ae 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64 ly, evenly divid
103af 65 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 e into BITVEC_US
103b0 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 IZE. .** Setting
103b1 20 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61 this to the "na
103b2 74 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 tural word" size
103b3 20 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79 of your CPU may
103b4 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 improve.** perf
103b5 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 ormance. */.#def
103b6 69 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d ine BITVEC_TELEM
103b7 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c u8./* Size,
103b8 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65 in bits, of the
103b9 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e bitmap element.
103ba 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 */.#define BITV
103bb 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f EC_SZELEM 8./
103bc 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d * Number of elem
103bd 65 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70 ents in a bitmap
103be 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 array. */.#defi
103bf 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 ne BITVEC_NELEM
103c0 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a (BITVEC_USIZ
103c1 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f E/sizeof(BITVEC_
103c2 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 TELEM))./* Numbe
103c3 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 r of bits in the
103c4 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a bitmap array. *
103c5 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 /.#define BITVEC
103c6 5f 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56 _NBIT (BITV
103c7 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f EC_NELEM*BITVEC_
103c8 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 SZELEM)../* Numb
103c9 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73 er of u32 values
103ca 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 in hash table.
103cb 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 */.#define BITVE
103cc 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54 C_NINT (BIT
103cd 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 VEC_USIZE/sizeof
103ce 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 (u32))./* Maximu
103cf 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 m number of entr
103d0 69 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c ies in hash tabl
103d1 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 e before .** sub
103d2 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65 -dividing and re
103d3 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 -hashing. */.#de
103d4 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41 fine BITVEC_MXHA
103d5 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49 SH (BITVEC_NI
103d6 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 NT/2)./* Hashing
103d7 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 function for th
103d8 65 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e e aHash represen
103d9 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 tation..** Empir
103da 69 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f ical testing sho
103db 77 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37 wed that the *37
103dc 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 multiplier .**
103dd 28 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72 (an arbitrary pr
103de 69 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20 ime)in the hash
103df 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 function provide
103e0 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 d .** no fewer c
103e1 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 ollisions than t
103e2 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a he no-op *1. */.
103e3 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48 #define BITVEC_H
103e4 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31 ASH(X) (((X)*1
103e5 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a )%BITVEC_NINT)..
103e6 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e #define BITVEC_N
103e7 50 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43 PTR (BITVEC
103e8 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 _USIZE/sizeof(Bi
103e9 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a tvec *)).../*.**
103ea 20 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20 A bitmap is an
103eb 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
103ec 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
103ed 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ure..**.** This
103ee 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74 bitmap records t
103ef 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20 he existance of
103f0 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 zero or more bit
103f1 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73 s.** with values
103f2 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 between 1 and i
103f3 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e Size, inclusive.
103f4 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 .**.** There are
103f5 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20 three possible
103f6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 representations
103f7 6f 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a of the bitmap..*
103f8 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 * If iSize<=BITV
103f9 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 EC_NBIT, then Bi
103fa 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d tvec.u.aBitmap[]
103fb 20 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a is a straight.*
103fc 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c * bitmap. The l
103fd 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 east significant
103fe 20 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a bit is bit 1..*
103ff 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 *.** If iSize>BI
10400 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 TVEC_NBIT and iD
10401 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 ivisor==0 then B
10402 69 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 itvec.u.aHash[]
10403 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62 is.** a hash tab
10404 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c le that will hol
10405 64 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d d up to BITVEC_M
10406 58 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76 XHASH distinct v
10407 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 alues..**.** Oth
10408 65 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 erwise, the valu
10409 65 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65 e i is redirecte
1040a 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 d into one of BI
1040b 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 TVEC_NPTR.** sub
1040c 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 -bitmaps pointed
1040d 20 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e to by Bitvec.u.
1040e 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 apSub[]. Each s
1040f 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 ubbitmap.** hand
10410 6c 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73 les up to iDivis
10411 6f 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75 or separate valu
10412 65 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b es of i. apSub[
10413 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 0] holds.** valu
10414 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 es between 1 and
10415 20 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75 iDivisor. apSu
10416 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 b[1] holds value
10417 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 s between.** iDi
10418 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 visor+1 and 2*iD
10419 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e ivisor. apSub[N
1041a 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 ] holds values b
1041b 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 etween.** N*iDiv
1041c 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 isor+1 and (N+1)
1041d 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68 *iDivisor. Each
1041e 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f subbitmap is no
1041f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 rmalized.** to h
10420 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61 old deal with va
10421 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 lues between 1 a
10422 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a nd iDivisor..*/.
10423 73 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a struct Bitvec {.
10424 20 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20 u32 iSize;
10425 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 /* Maximum bit
10426 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69 index. Max iSi
10427 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c ze is 4,294,967,
10428 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 296. */. u32 nS
10429 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d et; /* Num
1042a 62 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74 ber of bits that
1042b 20 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 are set - only
1042c 76 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 0a valid for aHash.
1042d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1042e 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d ** element. M
1042f 61 78 20 69 73 20 42 49 54 56 45 43 5f 4e 49 4e ax is BITVEC_NIN
10430 54 2e 20 20 46 6f 72 20 42 49 54 56 45 43 5f 53 T. For BITVEC_S
10431 5a 20 6f 66 20 35 31 32 2c 0a 20 20 20 20 20 20 Z of 512,.
10432 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 ** t
10433 68 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 35 his would be 125
10434 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69 . */. u32 iDivi
10435 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 sor; /* Number
10436 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64 of bits handled
10437 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d by each apSub[]
10438 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20 entry. */.
10439 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1043a 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61 Should >=0 for a
1043b 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f pSub element. */
1043c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1043d 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 73 /* Max iDivis
1043e 6f 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 2f or is max(u32) /
1043f 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 31 BITVEC_NPTR + 1
10440 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 . */.
10441 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 /* For a
10442 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 BITVEC_SZ of 51
10443 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 2, this would be
10444 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a 34,359,739. */.
10445 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49 union {. BI
10446 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d TVEC_TELEM aBitm
10447 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d ap[BITVEC_NELEM]
10448 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 ; /* Bitmap r
10449 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f epresentation */
1044a 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42 . u32 aHash[B
1044b 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 ITVEC_NINT];
1044c 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 /* Hash table
1044d 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
1044e 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70 /. Bitvec *ap
1044f 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d Sub[BITVEC_NPTR]
10450 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20 ; /* Recursive
10451 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a representation *
10452 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a /. } u;.};../*.
10453 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
10454 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 bitmap object ab
10455 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 le to handle bit
10456 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 s between 0 and
10457 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 iSize,.** inclus
10458 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 ive. Return a p
10459 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 ointer to the ne
1045a 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72 w object. Retur
1045b 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 n NULL if .** ma
1045c 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 lloc fails..*/.S
1045d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 QLITE_PRIVATE Bi
1045e 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 tvec *sqlite3Bit
1045f 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53 vecCreate(u32 iS
10460 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a ize){. Bitvec *
10461 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a p;. assert( siz
10462 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f eof(*p)==BITVEC_
10463 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 SZ );. p = sqli
10464 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 te3MallocZero( s
10465 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 izeof(*p) );. i
10466 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 f( p ){. p->i
10467 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 Size = iSize;.
10468 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
10469 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 ./*.** Check to
1046a 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20 see if the i-th
1046b 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74 bit is set. Ret
1046c 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73 urn true or fals
1046d 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 e..** If p is NU
1046e 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61 LL (if the bitma
1046f 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 p has not been c
10470 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a reated) or if.**
10471 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e i is out of ran
10472 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20 ge, then return
10473 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 false..*/.SQLITE
10474 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10475 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 ite3BitvecTest(B
10476 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 itvec *p, u32 i)
10477 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 {. if( p==0 ) r
10478 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 eturn 0;. if( i
10479 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d >p->iSize || i==
1047a 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 0 ) return 0;.
1047b 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d i--;. while( p-
1047c 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 >iDivisor ){.
1047d 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e u32 bin = i/p->
1047e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 iDivisor;. i
1047f 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b = i%p->iDivisor;
10480 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 . p = p->u.ap
10481 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 Sub[bin];. if
10482 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 (!p) {. re
10483 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 turn 0;. }.
10484 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 }. if( p->iSize
10485 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b <=BITVEC_NBIT ){
10486 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d 3e . return (p->
10487 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 u.aBitmap[i/BITV
10488 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c EC_SZELEM] & (1<
10489 3c 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c <(i&(BITVEC_SZEL
1048a 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d EM-1))))!=0;. }
1048b 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 68 else{. u32 h
1048c 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69 = BITVEC_HASH(i
1048d 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 ++);. while(
1048e 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b p->u.aHash[h] ){
1048f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e . if( p->u.
10490 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 aHash[h]==i ) re
10491 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 20 turn 1;. h
10492 3d 20 28 68 2b 31 29 20 25 20 42 49 54 56 45 43 = (h+1) % BITVEC
10493 5f 4e 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _NINT;. }.
10494 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d return 0;. }.}
10495 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
10496 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72 i-th bit. Retur
10497 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 n 0 on success a
10498 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 nd an error code
10499 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 if.** anything
1049a 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a goes wrong..**.*
1049b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d * This routine m
1049c 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62 ight cause sub-b
1049d 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c itmaps to be all
1049e 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 ocated. Failing
1049f 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d .** to get the m
104a0 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20 emory needed to
104a1 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74 hold the sub-bit
104a2 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a map is the only.
104a3 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77 ** that can go w
104a4 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73 rong with an ins
104a5 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 ert, assuming p
104a6 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e and i are valid.
104a7 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 .**.** The calli
104a8 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 ng function must
104a9 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69 ensure that p i
104aa 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63 s a valid Bitvec
104ab 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 object.** and t
104ac 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f hat the value fo
104ad 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20 r "i" is within
104ae 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74 range of the Bit
104af 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f vec object..** O
104b0 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68 therwise the beh
104b1 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e avior is undefin
104b2 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
104b3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
104b4 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65 3BitvecSet(Bitve
104b5 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 c *p, u32 i){.
104b6 75 33 32 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d u32 h;. if( p==
104b7 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 0 ) return SQLIT
104b8 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
104b9 69 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 i>0 );. assert(
104ba 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a i<=p->iSize );.
104bb 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28 i--;. while((
104bc 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56 45 p->iSize > BITVE
104bd 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69 44 C_NBIT) && p->iD
104be 69 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 75 33 ivisor) {. u3
104bf 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 2 bin = i/p->iDi
104c0 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 visor;. i = i
104c1 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 %p->iDivisor;.
104c2 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 if( p->u.apSub
104c3 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 [bin]==0 ){.
104c4 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e p->u.apSub[bin
104c5 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 ] = sqlite3Bitve
104c6 63 43 72 65 61 74 65 28 20 70 2d 3e 69 44 69 76 cCreate( p->iDiv
104c7 69 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 69 66 isor );. if
104c8 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e ( p->u.apSub[bin
104c9 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 ]==0 ) return SQ
104ca 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 LITE_NOMEM;.
104cb 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 }. p = p->u.a
104cc 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20 pSub[bin];. }.
104cd 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 if( p->iSize<=B
104ce 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 ITVEC_NBIT ){.
104cf 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 p->u.aBitmap[i
104d0 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 /BITVEC_SZELEM]
104d1 7c 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 54 56 |= 1 << (i&(BITV
104d2 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20 EC_SZELEM-1));.
104d3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
104d4 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42 _OK;. }. h = B
104d5 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b ITVEC_HASH(i++);
104d6 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77 . /* if there w
104d7 61 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c asn't a hash col
104d8 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 lision, and this
104d9 20 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a doesn't */. /*
104da 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c completely fill
104db 20 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20 the hash, then
104dc 6a 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68 just add it with
104dd 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72 out */. /* worr
104de 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64 69 ing about sub-di
104df 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 viding and re-ha
104e0 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 shing. */. if(
104e1 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 !p->u.aHash[h] )
104e2 7b 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e 53 65 {. if (p->nSe
104e3 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31 t<(BITVEC_NINT-1
104e4 29 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 )) {. goto
104e5 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a bitvec_set_end;.
104e6 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 } else {.
104e7 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 goto bitvec_s
104e8 65 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 20 7d et_rehash;. }
104e9 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20 . }. /* there
104ea 77 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c was a collision,
104eb 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 check to see if
104ec 20 69 74 27 73 20 61 6c 72 65 61 64 79 20 2a 2f it's already */
104ed 0a 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69 . /* in hash, i
104ee 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 66 69 f not, try to fi
104ef 6e 64 20 61 20 73 70 6f 74 20 66 6f 72 20 69 74 nd a spot for it
104f0 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 */. do {. i
104f1 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d f( p->u.aHash[h]
104f2 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c ==i ) return SQL
104f3 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b ITE_OK;. h++;
104f4 0a 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 . if( h>=BITV
104f5 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b EC_NINT ) h = 0;
104f6 0a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 . } while( p->u
104f7 2e 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f .aHash[h] );. /
104f8 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64 * we didn't find
104f9 20 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e it in the hash.
104fa 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 h points to th
104fb 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20 e first */. /*
104fc 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20 73 available free s
104fd 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73 65 pot. check to se
104fe 65 20 69 66 20 74 68 69 73 20 69 73 20 67 6f 69 e if this is goi
104ff 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61 ng to */. /* ma
10500 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f 6f 20 ke our hash too
10501 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76 "full". */.bitv
10502 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3a 0a 20 ec_set_rehash:.
10503 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 if( p->nSet>=BI
10504 54 56 45 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20 TVEC_MXHASH ){.
10505 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 unsigned int
10506 6a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 j;. int rc;.
10507 20 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 u32 *aiValues
10508 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 = sqlite3StackA
10509 6c 6c 6f 63 52 61 77 28 30 2c 20 73 69 7a 65 6f llocRaw(0, sizeo
1050a 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a f(p->u.aHash));.
1050b 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 if( aiValues
1050c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ==0 ){. ret
1050d 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
1050e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
1050f 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 memcpy(aiValu
10510 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 es, p->u.aHash,
10511 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 sizeof(p->u.aHas
10512 68 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 h));. memse
10513 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c t(p->u.apSub, 0,
10514 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 sizeof(p->u.apS
10515 75 62 29 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 ub));. p->i
10516 44 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 Divisor = (p->iS
10517 69 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 ize + BITVEC_NPT
10518 52 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 R - 1)/BITVEC_NP
10519 54 52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 TR;. rc = s
1051a 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
1051b 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 p, i);. for
1051c 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e (j=0; j<BITVEC_N
1051d 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 INT; j++){.
1051e 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b if( aiValues[
1051f 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 j] ) rc |= sqlit
10520 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61 e3BitvecSet(p, a
10521 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 iValues[j]);.
10522 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
10523 65 33 53 74 61 63 6b 46 72 65 65 28 30 2c 20 61 e3StackFree(0, a
10524 69 56 61 6c 75 65 73 29 3b 0a 20 20 20 20 20 20 iValues);.
10525 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
10526 0a 20 20 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f . }.bitvec_set_
10527 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b end:. p->nSet++
10528 3b 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 ;. p->u.aHash[h
10529 5d 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e 20 ] = i;. return
1052a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
1052b 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 2d .** Clear the i-
1052c 74 68 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 th bit..**.** pB
1052d 75 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 uf must be a poi
1052e 6e 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74 nter to at least
1052f 20 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73 BITVEC_SZ bytes
10530 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74 of temporary st
10531 6f 72 61 67 65 0a 2a 2a 20 74 68 61 74 20 42 69 orage.** that Bi
10532 74 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73 tvecClear can us
10533 65 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73 e to rebuilt its
10534 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a hash table..*/.
10535 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10536 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 oid sqlite3Bitve
10537 63 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70 cClear(Bitvec *p
10538 2c 20 75 33 32 20 69 2c 20 76 6f 69 64 20 2a 70 , u32 i, void *p
10539 42 75 66 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 Buf){. if( p==0
1053a 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 ) return;. ass
1053b 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d ert( i>0 );. i-
1053c 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 -;. while( p->i
1053d 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 Divisor ){. u
1053e 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 32 bin = i/p->iD
1053f 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 ivisor;. i =
10540 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 i%p->iDivisor;.
10541 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 p = p->u.apSu
10542 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 b[bin];. if (
10543 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 !p) {. retu
10544 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 rn;. }. }.
10545 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 if( p->iSize<=BI
10546 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 TVEC_NBIT ){.
10547 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f p->u.aBitmap[i/
10548 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 BITVEC_SZELEM] &
10549 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54 = ~(1 << (i&(BIT
1054a 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b VEC_SZELEM-1)));
1054b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e . }else{. un
1054c 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 signed int j;.
1054d 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 u32 *aiValues
1054e 3d 20 70 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63 = pBuf;. memc
1054f 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e py(aiValues, p->
10550 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 u.aHash, sizeof(
10551 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 p->u.aHash));.
10552 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 memset(p->u.aH
10553 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 ash, 0, sizeof(p
10554 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 ->u.aHash));.
10555 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 p->nSet = 0;.
10556 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 for(j=0; j<BIT
10557 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a VEC_NINT; j++){.
10558 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 if( aiValu
10559 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 es[j] && aiValue
1055a 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20 s[j]!=(i+1) ){.
1055b 20 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 42 u32 h = B
1055c 49 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 6c ITVEC_HASH(aiVal
1055d 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20 ues[j]-1);.
1055e 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 p->nSet++;.
1055f 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e while( p->
10560 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 u.aHash[h] ){.
10561 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 h++;.
10562 20 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 49 if( h>=BI
10563 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 TVEC_NINT ) h =
10564 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 0;. }.
10565 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b p->u.aHash[
10566 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d h] = aiValues[j]
10567 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10568 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 }.}../*.** Des
10569 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62 troy a bitmap ob
1056a 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61 ject. Reclaim a
1056b 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a ll memory used..
1056c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1056d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 E void sqlite3Bi
1056e 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 tvecDestroy(Bitv
1056f 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d ec *p){. if( p=
10570 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 =0 ) return;. i
10571 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 f( p->iDivisor )
10572 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 {. unsigned i
10573 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d nt i;. for(i=
10574 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 0; i<BITVEC_NPTR
10575 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 ; i++){. sq
10576 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 lite3BitvecDestr
10577 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d oy(p->u.apSub[i]
10578 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 );. }. }. s
10579 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a qlite3_free(p);.
1057a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
1057b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 the value of the
1057c 20 69 53 69 7a 65 20 70 61 72 61 6d 65 74 65 72 iSize parameter
1057d 20 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 specified when
1057e 42 69 74 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73 Bitvec *p.** was
1057f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c created..*/.SQL
10580 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 ITE_PRIVATE u32
10581 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a sqlite3BitvecSiz
10582 65 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 e(Bitvec *p){.
10583 72 65 74 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b return p->iSize;
10584 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .}..#ifndef SQLI
10585 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f TE_OMIT_BUILTIN_
10586 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 TEST./*.** Let V
10587 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f [] be an array o
10588 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 f unsigned chara
10589 63 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74 cters sufficient
1058a 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 to hold.** up t
1058b 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49 o N bits. Let I
1058c 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62 be an integer b
1058d 65 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 etween 0 and N.
1058e 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 0<=I<N..** Then
1058f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
10590 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 acros can be use
10591 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c d to set, clear,
10592 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 or test.** indi
10593 76 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68 vidual bits with
10594 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 in V..*/.#define
10595 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20 SETBIT(V,I)
10596 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c V[I>>3] |= (1<
10597 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 <(I&7)).#define
10598 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20 CLEARBIT(V,I)
10599 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c V[I>>3] &= ~(1<
1059a 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20 <(I&7)).#define
1059b 54 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20 TESTBIT(V,I)
1059c 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 (V[I>>3]&(1<<(I
1059d 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 &7)))!=0../*.**
1059e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e This routine run
1059f 73 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74 s an extensive t
105a0 65 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65 est of the Bitve
105a1 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 c code..**.** Th
105a2 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72 e input is an ar
105a3 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20 ray of integers
105a4 74 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70 that acts as a p
105a5 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 rogram.** to tes
105a6 74 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54 t the Bitvec. T
105a7 68 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 he integers are
105a8 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 opcodes followed
105a9 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 .** by 0, 1, or
105aa 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65 3 operands, depe
105ab 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 nding on the opc
105ac 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a ode. Another.**
105ad 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 opcode follows
105ae 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 immediately afte
105af 72 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61 r the last opera
105b0 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 nd..**.** There
105b1 61 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 are 6 opcodes nu
105b2 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68 mbered from 0 th
105b3 72 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74 rough 5. 0 is t
105b4 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 he.** "halt" opc
105b5 6f 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74 ode and causes t
105b6 68 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a he test to end..
105b7 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20 **.** 0
105b8 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74 Halt and ret
105b9 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f urn the number o
105ba 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 f errors.** 1
105bb 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 N S X Set N
105bc 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 bits beginning w
105bd 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d ith S and increm
105be 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 enting by X.**
105bf 20 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65 2 N S X Cle
105c0 61 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e ar N bits beginn
105c1 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69 ing with S and i
105c2 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 ncrementing by X
105c3 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20 .** 3 N
105c4 20 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 Set N randomly
105c5 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 chosen bits.**
105c6 20 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c 4 N Cl
105c7 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 ear N randomly c
105c8 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 hosen bits.**
105c9 20 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 5 N S X Set
105ca 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e N bits from S in
105cb 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 crement X in arr
105cc 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 ay only, not in
105cd 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 bitvec.**.** The
105ce 20 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75 opcodes 1 throu
105cf 67 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74 gh 4 perform set
105d0 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61 and clear opera
105d1 74 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72 tions are perfor
105d2 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 med.** on both a
105d3 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61 Bitvec object a
105d4 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 nd on a linear a
105d5 72 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74 rray of bits obt
105d6 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f ained from mallo
105d7 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 c..** Opcode 5 w
105d8 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 orks on the line
105d9 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e ar array only, n
105da 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63 ot on the Bitvec
105db 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 ..** Opcode 5 is
105dc 20 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72 used to deliber
105dd 61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66 ately induce a f
105de 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f ault in order to
105df 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 .** confirm that
105e0 20 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e error detection
105e1 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 works..**.** At
105e2 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 the conclusion
105e3 6f 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20 of the test the
105e4 6c 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20 linear array is
105e5 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 compared.** agai
105e6 6e 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f nst the Bitvec o
105e7 62 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65 bject. If there
105e8 20 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65 are any differe
105e9 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f nces,.** an erro
105ea 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 r is returned.
105eb 49 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20 If they are the
105ec 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 same, zero is re
105ed 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 turned..**.** If
105ee 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 a memory alloca
105ef 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 tion error occur
105f0 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f s, return -1..*/
105f1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
105f2 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 int sqlite3Bitve
105f3 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 cBuiltinTest(int
105f4 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a sz, int *aOp){.
105f5 20 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65 Bitvec *pBitve
105f6 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 c = 0;. unsigne
105f7 64 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a d char *pV = 0;.
105f8 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 int rc = -1;.
105f9 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 int i, nx, pc,
105fa 6f 70 3b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70 op;. void *pTmp
105fb 53 70 61 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c Space;.. /* All
105fc 6f 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63 ocate the Bitvec
105fd 20 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e to be tested an
105fe 64 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 d a linear array
105ff 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f of. ** bits to
10600 20 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65 act as the refe
10601 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 rence */. pBitv
10602 65 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 ec = sqlite3Bitv
10603 65 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a ecCreate( sz );.
10604 20 20 70 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d pV = sqlite3_m
10605 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 alloc( (sz+7)/8
10606 2b 20 31 20 29 3b 0a 20 20 70 54 6d 70 53 70 61 + 1 );. pTmpSpa
10607 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c ce = sqlite3_mal
10608 6c 6f 63 28 42 49 54 56 45 43 5f 53 5a 29 3b 0a loc(BITVEC_SZ);.
10609 20 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d 30 if( pBitvec==0
1060a 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d || pV==0 || pTm
1060b 70 53 70 61 63 65 3d 3d 30 20 20 29 20 67 6f 74 pSpace==0 ) got
1060c 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 o bitvec_end;.
1060d 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73 memset(pV, 0, (s
1060e 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20 z+7)/8 + 1);..
1060f 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76 65 63 20 /* NULL pBitvec
10610 74 65 73 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 tests */. sqlit
10611 65 33 42 69 74 76 65 63 53 65 74 28 30 2c 20 31 e3BitvecSet(0, 1
10612 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 );. sqlite3Bitv
10613 65 63 43 6c 65 61 72 28 30 2c 20 31 2c 20 70 54 ecClear(0, 1, pT
10614 6d 70 53 70 61 63 65 29 3b 0a 0a 20 20 2f 2a 20 mpSpace);.. /*
10615 52 75 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20 Run the program
10616 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a 20 20 77 */. pc = 0;. w
10617 68 69 6c 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b hile( (op = aOp[
10618 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 pc])!=0 ){. s
10619 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 witch( op ){.
1061a 20 20 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20 case 1:.
1061b 20 63 61 73 65 20 32 3a 0a 20 20 20 20 20 20 63 case 2:. c
1061c 61 73 65 20 35 3a 20 7b 0a 20 20 20 20 20 20 20 ase 5: {.
1061d 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 nx = 4;.
1061e 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d i = aOp[pc+2] -
1061f 20 31 3b 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 1;. aOp[
10620 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b pc+2] += aOp[pc+
10621 33 5d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 3];. brea
10622 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
10623 20 63 61 73 65 20 33 3a 0a 20 20 20 20 20 20 63 case 3:. c
10624 61 73 65 20 34 3a 20 0a 20 20 20 20 20 20 64 65 ase 4: . de
10625 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 fault: {.
10626 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20 20 20 20 nx = 2;.
10627 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e sqlite3_randomn
10628 65 73 73 28 73 69 7a 65 6f 66 28 69 29 2c 20 26 ess(sizeof(i), &
10629 69 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 i);. brea
1062a 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d k;. }. }
1062b 0a 20 20 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b . if( (--aOp[
1062c 70 63 2b 31 5d 29 20 3e 20 30 20 29 20 6e 78 20 pc+1]) > 0 ) nx
1062d 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e = 0;. pc += n
1062e 78 3b 0a 20 20 20 20 69 20 3d 20 28 69 20 26 20 x;. i = (i &
1062f 30 78 37 66 66 66 66 66 66 66 29 25 73 7a 3b 0a 0x7fffffff)%sz;.
10630 20 20 20 20 69 66 28 20 28 6f 70 20 26 20 31 29 if( (op & 1)
10631 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 45 54 !=0 ){. SET
10632 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a BIT(pV, (i+1));.
10633 20 20 20 20 20 20 69 66 28 20 6f 70 21 3d 35 20 if( op!=5
10634 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 ){. if( s
10635 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
10636 70 42 69 74 76 65 63 2c 20 69 2b 31 29 20 29 20 pBitvec, i+1) )
10637 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b goto bitvec_end;
10638 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c . }. }el
10639 73 65 7b 0a 20 20 20 20 20 20 43 4c 45 41 52 42 se{. CLEARB
1063a 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 IT(pV, (i+1));.
1063b 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 sqlite3Bitv
1063c 65 63 43 6c 65 61 72 28 70 42 69 74 76 65 63 2c ecClear(pBitvec,
1063d 20 69 2b 31 2c 20 70 54 6d 70 53 70 61 63 65 29 i+1, pTmpSpace)
1063e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
1063f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 * Test to make s
10640 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20 61 ure the linear a
10641 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61 74 rray exactly mat
10642 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 69 ches the. ** Bi
10643 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 74 tvec object. St
10644 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73 73 art with the ass
10645 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68 65 umption that the
10646 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20 y do. ** match
10647 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65 (rc==0). Change
10648 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 rc to non-zero
10649 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63 79 if a discrepancy
1064a 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a . ** is found..
1064b 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
1064c 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30 2c te3BitvecTest(0,
1064d 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 0) + sqlite3Bitv
1064e 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 ecTest(pBitvec,
1064f 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20 20 sz+1).
10650 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 + sqlite3BitvecT
10651 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29 0a est(pBitvec, 0).
10652 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c + (sql
10653 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 ite3BitvecSize(p
10654 42 69 74 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20 Bitvec) - sz);.
10655 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b for(i=1; i<=sz;
10656 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 i++){. if(
10657 28 54 45 53 54 42 49 54 28 70 56 2c 69 29 29 21 (TESTBIT(pV,i))!
10658 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 =sqlite3BitvecTe
10659 73 74 28 70 42 69 74 76 65 63 2c 69 29 20 29 7b st(pBitvec,i) ){
1065a 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 . rc = i;.
1065b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1065c 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 }. }.. /* Free
1065d 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 75 63 allocated struc
1065e 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 ture */.bitvec_e
1065f 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 nd:. sqlite3_fr
10660 65 65 28 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 ee(pTmpSpace);.
10661 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56 sqlite3_free(pV
10662 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 );. sqlite3Bitv
10663 65 63 44 65 73 74 72 6f 79 28 70 42 69 74 76 65 ecDestroy(pBitve
10664 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b c);. return rc;
10665 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
10666 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e ITE_OMIT_BUILTIN
10667 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a _TEST */../*****
10668 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
10669 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a bitvec.c ******
1066a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1066b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1066c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1066d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1066e 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a 2a file pcache.c **
1066f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10671 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
10672 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2008 August 05.*
10673 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
10674 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
10675 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
10676 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
10677 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
10678 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
10679 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1067a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1067b 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1067c 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1067d 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1067e 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1067f 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
10680 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
10681 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
10682 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
10683 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
10684 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
10685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
10689 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 This file imple
1068a 6d 65 6e 74 73 20 74 68 61 74 20 70 61 67 65 20 ments that page
1068b 63 61 63 68 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a cache..*/../*.**
1068c 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 A complete page
1068d 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 cache is an ins
1068e 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 tance of this st
1068f 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 ructure..*/.stru
10690 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 ct PCache {. Pg
10691 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 Hdr *pDirty, *pD
10692 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 irtyTail;
10693 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 /* List of dir
10694 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 ty pages in LRU
10695 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 order */. PgHdr
10696 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 *pSynced;
10697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
10698 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 * Last synced pa
10699 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 ge in dirty page
1069a 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e list */. int n
1069b 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Ref;
1069c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1069d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 * Number of refe
1069e 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a renced pages */.
1069f 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 int nMax;
106a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106a1 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
106a2 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a red cache size *
106a3 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 /. int szPage;
106a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106a5 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
106a6 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e of every page in
106a7 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20 this cache */.
106a8 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20 int szExtra;
106a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106aa 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
106ab 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20 extra space for
106ac 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 each page */. i
106ad 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 nt bPurgeable;
106ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
106af 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 /* True if pa
106b0 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 ges are on backi
106b1 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e ng store */. in
106b2 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 t (*xStress)(voi
106b3 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20 d*,PgHdr*);
106b4 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 /* Call to try
106b5 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65 make a page cle
106b6 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 an */. void *pS
106b7 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 tress;
106b8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
106b9 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 rgument to xStre
106ba 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ss */. sqlite3_
106bb 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 pcache *pCache;
106bc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
106bd 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d luggable cache m
106be 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72 odule */. PgHdr
106bf 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20 *pPage1;
106c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
106c1 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 * Reference to p
106c2 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a age 1 */.};../*.
106c3 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61 ** Some of the a
106c4 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69 ssert() macros i
106c5 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20 n this code are
106c6 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f too expensive to
106c7 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72 run.** even dur
106c8 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67 ing normal debug
106c9 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20 ging. Use them
106ca 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c only rarely on l
106cb 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74 ong-running.** t
106cc 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68 ests. Enable th
106cd 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65 e expensive asse
106ce 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a rts using the.**
106cf 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 -DSQLITE_ENABLE
106d0 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
106d1 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 T=1 compile-time
106d2 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64 option..*/.#ifd
106d3 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
106d4 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
106d5 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e T.# define expen
106d6 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20 sive_assert(X)
106d7 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a assert(X).#else.
106d8 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69 # define expensi
106d9 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e ve_assert(X).#en
106da 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
106db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106dc 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20 ******** Linked
106dd 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20 List Management
106de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
106df 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 ****/..#if !defi
106e0 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64 ned(NDEBUG) && d
106e1 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
106e2 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 ABLE_EXPENSIVE_A
106e3 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65 SSERT)./*.** Che
106e4 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63 ck that the pCac
106e5 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69 he->pSynced vari
106e6 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72 able is set corr
106e7 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20 ectly. If it.**
106e8 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66 is not, either f
106e9 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72 ail an assert or
106ea 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74 return zero. Ot
106eb 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a herwise, return.
106ec 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69 ** non-zero. Thi
106ed 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 s is only used i
106ee 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c n debugging buil
106ef 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ds, as follows:.
106f0 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76 **.** expensiv
106f1 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 e_assert( pcache
106f2 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 CheckSynced(pCac
106f3 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 he) );.*/.static
106f4 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b int pcacheCheck
106f5 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70 Synced(PCache *p
106f6 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20 Cache){. PgHdr
106f7 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 *p;. for(p=pCac
106f8 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20 he->pDirtyTail;
106f9 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 p!=pCache->pSync
106fa 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50 ed; p=p->pDirtyP
106fb 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74 rev){. assert
106fc 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d ( p->nRef || (p-
106fd 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
106fe 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20 D_SYNC) );. }.
106ff 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c return (p==0 ||
10700 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e p->nRef || (p->
10701 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
10702 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65 _SYNC)==0);.}.#e
10703 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20 ndif /* !NDEBUG
10704 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
10705 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52 _EXPENSIVE_ASSER
10706 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f T */../*.** Remo
10707 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72 ve page pPage fr
10708 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64 om the list of d
10709 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73 irty pages..*/.s
1070a 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
1070b 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 eRemoveFromDirty
1070c 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67 List(PgHdr *pPag
1070d 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20 e){. PCache *p
1070e 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b = pPage->pCache;
1070f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
10710 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c e->pDirtyNext ||
10711 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 pPage==p->pDirt
10712 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72 yTail );. asser
10713 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 t( pPage->pDirty
10714 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70 Prev || pPage==p
10715 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f ->pDirty );.. /
10716 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61 * Update the PCa
10717 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72 che1.pSynced var
10718 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61 iable if necessa
10719 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e ry. */. if( p->
1071a 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29 pSynced==pPage )
1071b 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79 {. PgHdr *pSy
1071c 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44 nced = pPage->pD
1071d 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68 irtyPrev;. wh
1071e 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20 ile( pSynced &&
1071f 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26 (pSynced->flags&
10720 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
10721 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65 ){. pSynce
10722 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69 d = pSynced->pDi
10723 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 rtyPrev;. }.
10724 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 p->pSynced =
10725 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20 pSynced;. }..
10726 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 if( pPage->pDirt
10727 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61 yNext ){. pPa
10728 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e ge->pDirtyNext->
10729 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61 pDirtyPrev = pPa
1072a 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a ge->pDirtyPrev;.
1072b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 }else{. ass
1072c 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 ert( pPage==p->p
1072d 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20 DirtyTail );.
1072e 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d p->pDirtyTail =
1072f 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 pPage->pDirtyPr
10730 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 ev;. }. if( pP
10731 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 age->pDirtyPrev
10732 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 ){. pPage->pD
10733 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79 irtyPrev->pDirty
10734 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44 Next = pPage->pD
10735 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 irtyNext;. }els
10736 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
10737 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20 Page==p->pDirty
10738 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 );. p->pDirty
10739 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 = pPage->pDirty
1073a 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67 Next;. }. pPag
1073b 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20 e->pDirtyNext =
1073c 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72 0;. pPage->pDir
1073d 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65 tyPrev = 0;.. e
1073e 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 xpensive_assert(
1073f 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63 pcacheCheckSync
10740 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a ed(p) );.}../*.*
10741 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65 * Add page pPage
10742 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 to the head of
10743 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28 the dirty list (
10744 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69 PCache1.pDirty i
10745 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67 s set to.** pPag
10746 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f e)..*/.static vo
10747 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 id pcacheAddToDi
10748 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 rtyList(PgHdr *p
10749 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 Page){. PCache
1074a 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 *p = pPage->pCac
1074b 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 he;.. assert( p
1074c 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 Page->pDirtyNext
1074d 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44 ==0 && pPage->pD
1074e 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70 irtyPrev==0 && p
1074f 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20 ->pDirty!=pPage
10750 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 );.. pPage->pDi
10751 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69 rtyNext = p->pDi
10752 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 rty;. if( pPage
10753 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a ->pDirtyNext ){.
10754 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
10755 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70 e->pDirtyNext->p
10756 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a DirtyPrev==0 );.
10757 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74 pPage->pDirt
10758 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65 yNext->pDirtyPre
10759 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 v = pPage;. }.
1075a 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 p->pDirty = pPa
1075b 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44 ge;. if( !p->pD
1075c 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20 irtyTail ){.
1075d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20 p->pDirtyTail =
1075e 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 pPage;. }. if(
1075f 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20 !p->pSynced &&
10760 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73 0==(pPage->flags
10761 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 &PGHDR_NEED_SYNC
10762 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e ) ){. p->pSyn
10763 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d ced = pPage;. }
10764 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 . expensive_ass
10765 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b ert( pcacheCheck
10766 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a Synced(p) );.}..
10767 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72 /*.** Wrapper ar
10768 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62 ound the pluggab
10769 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e le caches xUnpin
1076a 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20 method. If the
1076b 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e cache is.** bein
1076c 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e g used for an in
1076d 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1076e 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
1076f 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 is a no-op..*/.s
10770 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10771 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29 eUnpin(PgHdr *p)
10772 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 {. PCache *pCac
10773 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a he = p->pCache;.
10774 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 if( pCache->bP
10775 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 urgeable ){.
10776 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 if( p->pgno==1 )
10777 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e {. pCache->
10778 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 pPage1 = 0;.
10779 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f }. sqlite3Glo
1077a 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
1077b 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e .xUnpin(pCache->
1077c 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20 pCache, p, 0);.
1077d 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a }.}../*********
1077e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1077f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 ********** Gener
10781 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a al Interfaces **
10782 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69 ****.**.** Initi
10783 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f alize and shutdo
10784 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 wn the page cach
10785 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69 e subsystem. Nei
10786 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a ther of these .*
10787 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 * functions are
10788 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 threadsafe..*/.S
10789 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1078a 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 t sqlite3PcacheI
1078b 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b nitialize(void){
1078c 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c . if( sqlite3Gl
1078d 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
1078e 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 e.xInit==0 ){.
1078f 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 sqlite3PCacheS
10790 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d etDefault();. }
10791 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
10792 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10793 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74 ache.xInit(sqlit
10794 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
10795 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53 cache.pArg);.}.S
10796 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
10797 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
10798 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a Shutdown(void){.
10799 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f if( sqlite3Glo
1079a 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 balConfig.pcache
1079b 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20 .xShutdown ){.
1079c 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 sqlite3GlobalC
1079d 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68 onfig.pcache.xSh
1079e 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c utdown(sqlite3Gl
1079f 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 obalConfig.pcach
107a0 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a e.pArg);. }.}..
107a1 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
107a2 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
107a3 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63 f a PCache objec
107a4 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 t..*/.SQLITE_PRI
107a5 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
107a6 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29 PcacheSize(void)
107a7 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28 { return sizeof(
107a8 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a PCache); }../*.*
107a9 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50 * Create a new P
107aa 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74 Cache object. St
107ab 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68 orage space to h
107ac 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a old the object.*
107ad 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 * has already be
107ae 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 en allocated and
107af 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73 is passed in as
107b0 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20 the p pointer.
107b1 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64 .** The caller d
107b2 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63 iscovers how muc
107b3 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f h space needs to
107b4 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 be allocated by
107b5 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c .** calling sql
107b6 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 ite3PcacheSize()
107b7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
107b8 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
107b9 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e PcacheOpen(. in
107ba 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20 t szPage,
107bb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 /* Si
107bc 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 ze of every page
107bd 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 */. int szExtr
107be 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 a,
107bf 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63 /* Extra spac
107c0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 e associated wit
107c1 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 h each page */.
107c2 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c int bPurgeable,
107c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
107c4 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 True if pages a
107c5 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 re on backing st
107c6 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 ore */. int (*x
107c7 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 Stress)(void*,Pg
107c8 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f Hdr*),/* Call to
107c9 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 try to make pag
107ca 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f es clean */. vo
107cb 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 id *pStress,
107cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 /* Ar
107cd 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 gument to xStres
107ce 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 s */. PCache *p
107cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
107d0 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61 /* Prealloca
107d1 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68 ted space for th
107d2 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20 e PCache */.){.
107d3 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 memset(p, 0, si
107d4 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20 zeof(PCache));.
107d5 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 p->szPage = szP
107d6 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72 age;. p->szExtr
107d7 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70 a = szExtra;. p
107d8 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62 ->bPurgeable = b
107d9 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e Purgeable;. p->
107da 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73 xStress = xStres
107db 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20 s;. p->pStress
107dc 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e = pStress;. p->
107dd 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f nMax = 100;.}../
107de 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
107df 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43 page size for PC
107e0 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65 ache object. The
107e1 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 caller must ens
107e2 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a ure that there.*
107e3 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e * are no outstan
107e4 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65 ding page refere
107e5 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66 nces when this f
107e6 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
107e7 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
107e8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
107e9 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69 3PcacheSetPageSi
107ea 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 ze(PCache *pCach
107eb 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a e, int szPage){.
107ec 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65 assert( pCache
107ed 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61 ->nRef==0 && pCa
107ee 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29 che->pDirty==0 )
107ef 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e ;. if( pCache->
107f0 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 pCache ){. sq
107f1 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
107f2 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f g.pcache.xDestro
107f3 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 y(pCache->pCache
107f4 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 );. pCache->p
107f5 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 Cache = 0;. }.
107f6 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 pCache->szPage
107f7 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a = szPage;.}../*.
107f8 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e ** Try to obtain
107f9 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 a page from the
107fa 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
107fb 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
107fc 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
107fd 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 (. PCache *pCac
107fe 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74 he, /* Obt
107ff 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f ain the page fro
10800 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a m this cache */.
10801 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 Pgno pgno,
10802 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
10803 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e number to obtain
10804 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65 */. int create
10805 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49 Flag, /* I
10806 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70 f true, create p
10807 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e age if it does n
10808 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79 ot exist already
10809 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70 */. PgHdr **pp
1080a 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57 Page /* W
1080b 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65 rite the page he
1080c 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72 re */.){. PgHdr
1080d 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 *pPage = 0;. i
1080e 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61 nt eCreate;.. a
1080f 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30 ssert( pCache!=0
10810 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72 );. assert( cr
10811 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63 eateFlag==1 || c
10812 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a reateFlag==0 );.
10813 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 assert( pgno>0
10814 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 );.. /* If the
10815 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 pluggable cache
10816 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 (sqlite3_pcache
10817 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 *) has not been
10818 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 allocated,. **
10819 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e allocate it now.
1081a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 . */. if( !pCa
1081b 63 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63 che->pCache && c
1081c 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 reateFlag ){.
1081d 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 sqlite3_pcache
1081e 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 *p;. int nByt
1081f 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70 e;. nByte = p
10820 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 Cache->szPage +
10821 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 pCache->szExtra
10822 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b + sizeof(PgHdr);
10823 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
10824 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 GlobalConfig.pca
10825 63 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74 che.xCreate(nByt
10826 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 e, pCache->bPurg
10827 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 eable);. if(
10828 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 !p ){. retu
10829 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1082a 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
1082b 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
1082c 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 cache.xCachesize
1082d 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 (p, pCache->nMax
1082e 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70 );. pCache->p
1082f 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a Cache = p;. }..
10830 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61 eCreate = crea
10831 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21 teFlag * (1 + (!
10832 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10833 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70 le || !pCache->p
10834 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70 Dirty));. if( p
10835 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b Cache->pCache ){
10836 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c . pPage = sql
10837 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
10838 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 .pcache.xFetch(p
10839 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 Cache->pCache, p
1083a 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20 gno, eCreate);.
1083b 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 }.. if( !pPage
1083c 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29 && eCreate==1 )
1083d 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 {. PgHdr *pPg
1083e 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 ;.. /* Find a
1083f 20 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77 dirty page to w
10840 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63 rite-out and rec
10841 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20 ycle. First try
10842 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a to find a . *
10843 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 * page that does
10844 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a not require a j
10845 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65 ournal-sync (one
10846 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44 with PGHDR_NEED
10847 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65 _SYNC. ** cle
10848 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68 ared), but if th
10849 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 at is not possib
1084a 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e le settle for an
1084b 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 y other . **
1084c 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72 unreferenced dir
1084d 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a ty page.. */.
1084e 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 expensive_as
1084f 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 sert( pcacheChec
10850 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20 kSynced(pCache)
10851 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70 );. for(pPg=p
10852 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 Cache->pSynced;
10853 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20 . pPg &&
10854 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 (pPg->nRef || (p
10855 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f Pg->flags&PGHDR_
10856 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20 NEED_SYNC)); .
10857 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70 pPg=pPg->p
10858 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b DirtyPrev. );
10859 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b . if( !pPg ){
1085a 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70 . for(pPg=p
1085b 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 Cache->pDirtyTai
1085c 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e l; pPg && pPg->n
1085d 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44 Ref; pPg=pPg->pD
1085e 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d irtyPrev);. }
1085f 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a . if( pPg ){.
10860 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 int rc;.
10861 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d rc = pCache-
10862 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d >xStress(pCache-
10863 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a >pStress, pPg);.
10864 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
10865 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 LITE_OK && rc!=S
10866 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
10867 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
10868 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
10869 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69 pPage = sqli
1086a 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
1086b 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43 pcache.xFetch(pC
1086c 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67 ache->pCache, pg
1086d 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69 no, 2);. }.. i
1086e 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 f( pPage ){.
1086f 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74 if( !pPage->pDat
10870 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 a ){. memse
10871 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65 t(pPage, 0, size
10872 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63 of(PgHdr) + pCac
10873 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20 he->szExtra);.
10874 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72 pPage->pExtr
10875 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67 a = (void*)&pPag
10876 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 e[1];. pPag
10877 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64 e->pData = (void
10878 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61 *)&((char *)pPa
10879 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 ge)[sizeof(PgHdr
1087a 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 ) + pCache->szEx
1087b 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67 tra];. pPag
1087c 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 e->pCache = pCac
1087d 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d he;. pPage-
1087e 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20 >pgno = pgno;.
1087f 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
10880 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70 pPage->pCache==p
10881 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73 Cache );. ass
10882 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f ert( pPage->pgno
10883 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73 ==pgno );. as
10884 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78 sert( pPage->pEx
10885 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50 tra==(void *)&pP
10886 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69 age[1] );.. i
10887 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65 f( 0==pPage->nRe
10888 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 f ){. pCach
10889 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d e->nRef++;. }
1088a 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66 . pPage->nRef
1088b 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f ++;. if( pgno
1088c 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ==1 ){. pCa
1088d 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 che->pPage1 = pP
1088e 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 age;. }. }.
1088f 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 *ppPage = pPage
10890 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67 ;. return (pPag
10891 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29 e==0 && eCreate)
10892 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 ? SQLITE_NOMEM
10893 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a : SQLITE_OK;.}..
10894 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 /*.** Decrement
10895 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f the reference co
10896 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49 unt on a page. I
10897 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c f the page is cl
10898 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72 ean and the.** r
10899 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64 eference count d
1089a 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20 rops to 0, then
1089b 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c it is made elibl
1089c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e e for recycling.
1089d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1089e 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 TE void sqlite3P
1089f 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 cacheRelease(PgH
108a0 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 dr *p){. assert
108a1 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
108a2 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 p->nRef--;. if
108a3 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
108a4 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 PCache *pCac
108a5 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a he = p->pCache;.
108a6 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 pCache->nRef
108a7 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e --;. if( (p->
108a8 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
108a9 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 Y)==0 ){. p
108aa 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20 cacheUnpin(p);.
108ab 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
108ac 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 /* Move the page
108ad 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 to the head of
108ae 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20 the dirty list.
108af 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52 */. pcacheR
108b0 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69 emoveFromDirtyLi
108b1 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61 st(p);. pca
108b2 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 cheAddToDirtyLis
108b3 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a t(p);. }. }.
108b4 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73 }../*.** Increas
108b5 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 e the reference
108b6 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c count of a suppl
108b7 69 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a ied page by 1..*
108b8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
108b9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
108ba 63 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29 cheRef(PgHdr *p)
108bb 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52 {. assert(p->nR
108bc 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 ef>0);. p->nRef
108bd 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f ++;.}../*.** Dro
108be 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 p a page from th
108bf 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d e cache. There m
108c0 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f ust be exactly o
108c1 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 ne reference to
108c2 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69 the.** page. Thi
108c3 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 s function delet
108c4 65 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 es that referenc
108c5 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72 e, so after it r
108c6 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61 eturns the.** pa
108c7 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ge pointed to by
108c8 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a p is invalid..*
108c9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
108ca 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 void sqlite3Pca
108cb 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70 cheDrop(PgHdr *p
108cc 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 ){. PCache *pCa
108cd 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 che;. assert( p
108ce 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 ->nRef==1 );. i
108cf 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 f( p->flags&PGHD
108d0 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70 R_DIRTY ){. p
108d1 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 cacheRemoveFromD
108d2 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d irtyList(p);. }
108d3 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70 . pCache = p->p
108d4 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d Cache;. pCache-
108d5 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 >nRef--;. if( p
108d6 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 ->pgno==1 ){.
108d7 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 pCache->pPage1
108d8 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = 0;. }. sqlit
108d9 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 e3GlobalConfig.p
108da 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 cache.xUnpin(pCa
108db 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 che->pCache, p,
108dc 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 1);.}../*.** Mak
108dd 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 e sure the page
108de 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 is marked as dir
108df 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 ty. If it isn't
108e0 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a dirty already,.*
108e1 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f * make it so..*/
108e2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
108e3 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
108e4 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64 heMakeDirty(PgHd
108e5 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67 r *p){. p->flag
108e6 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54 s &= ~PGHDR_DONT
108e7 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74 _WRITE;. assert
108e8 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 ( p->nRef>0 );.
108e9 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67 if( 0==(p->flag
108ea 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29 s & PGHDR_DIRTY)
108eb 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 ){. p->flags
108ec 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b |= PGHDR_DIRTY;
108ed 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f . pcacheAddTo
108ee 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 DirtyList( p);.
108ef 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 }.}../*.** Make
108f0 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69 sure the page i
108f1 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61 s marked as clea
108f2 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63 n. If it isn't c
108f3 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a lean already,.**
108f4 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a make it so..*/.
108f5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
108f6 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
108f7 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 eMakeClean(PgHdr
108f8 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e *p){. if( (p->
108f9 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 flags & PGHDR_DI
108fa 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63 RTY) ){. pcac
108fb 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 heRemoveFromDirt
108fc 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d yList(p);. p-
108fd 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44 >flags &= ~(PGHD
108fe 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 R_DIRTY|PGHDR_NE
108ff 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 ED_SYNC);. if
10900 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a ( p->nRef==0 ){.
10901 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69 pcacheUnpi
10902 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a n(p);. }. }.
10903 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 }../*.** Make ev
10904 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
10905 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a cache clean..*/.
10906 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10907 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10908 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 eCleanAll(PCache
10909 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 *pCache){. PgH
1090a 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 dr *p;. while(
1090b 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 (p = pCache->pDi
1090c 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 rty)!=0 ){. s
1090d 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 qlite3PcacheMake
1090e 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a Clean(p);. }.}.
1090f 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 ./*.** Clear the
10910 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 PGHDR_NEED_SYNC
10911 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 flag from all d
10912 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 irty pages..*/.S
10913 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
10914 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 id sqlite3Pcache
10915 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50 ClearSyncFlags(P
10916 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a Cache *pCache){.
10917 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f PgHdr *p;. fo
10918 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 r(p=pCache->pDir
10919 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 ty; p; p=p->pDir
1091a 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e tyNext){. p->
1091b 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f flags &= ~PGHDR_
1091c 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 NEED_SYNC;. }.
1091d 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 pCache->pSynced
1091e 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 = pCache->pDirt
1091f 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 yTail;.}../*.**
10920 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 Change the page
10921 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70 number of page p
10922 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f to newPgno. .*/
10923 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10924 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10925 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c heMove(PgHdr *p,
10926 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a Pgno newPgno){.
10927 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 PCache *pCache
10928 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 = p->pCache;.
10929 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e assert( p->nRef>
1092a 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 0 );. assert( n
1092b 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 ewPgno>0 );. sq
1092c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
1092d 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28 g.pcache.xRekey(
1092e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
1092f 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 p, p->pgno, newP
10930 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 gno);. p->pgno
10931 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 = newPgno;. if(
10932 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 (p->flags&PGHDR
10933 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66 _DIRTY) && (p->f
10934 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f lags&PGHDR_NEED_
10935 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61 SYNC) ){. pca
10936 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 cheRemoveFromDir
10937 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 tyList(p);. p
10938 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c cacheAddToDirtyL
10939 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f ist(p);. }.}../
1093a 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20 *.** Drop every
1093b 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73 cache entry whos
1093c 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 e page number is
1093d 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70 greater than "p
1093e 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c gno". The.** cal
1093f 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 ler must ensure
10940 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e that there are n
10941 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 o outstanding re
10942 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 ferences to any
10943 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 pages.** other t
10944 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20 han page 1 with
10945 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72 a page number gr
10946 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e eater than pgno.
10947 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 .**.** If there
10948 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 is a reference t
10949 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65 o page 1 and the
1094a 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 pgno parameter
1094b 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a passed to this.*
1094c 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c * function is 0,
1094d 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61 then the data a
1094e 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 rea associated w
1094f 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65 ith page 1 is ze
10950 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 roed, but.** the
10951 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 page object is
10952 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a not dropped..*/.
10953 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
10954 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 oid sqlite3Pcach
10955 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65 eTruncate(PCache
10956 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 *pCache, Pgno p
10957 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63 gno){. if( pCac
10958 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 he->pCache ){.
10959 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 PgHdr *p;.
1095a 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 PgHdr *pNext;.
1095b 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e for(p=pCache->
1095c 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 pDirty; p; p=pNe
1095d 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 xt){. pNext
1095e 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 = p->pDirtyNext
1095f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 ;. if( p->p
10960 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 gno>pgno ){.
10961 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66 assert( p->f
10962 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 lags&PGHDR_DIRTY
10963 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 );. sqli
10964 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
10965 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 an(p);. }.
10966 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e }. if( pgn
10967 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e o==0 && pCache->
10968 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20 pPage1 ){.
10969 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70 memset(pCache->p
1096a 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c Page1->pData, 0,
1096b 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 pCache->szPage)
1096c 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 ;. pgno = 1
1096d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
1096e 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e te3GlobalConfig.
1096f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 pcache.xTruncate
10970 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c (pCache->pCache,
10971 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a pgno+1);. }.}.
10972 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 ./*.** Close a c
10973 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ache..*/.SQLITE_
10974 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
10975 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 ite3PcacheClose(
10976 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b PCache *pCache){
10977 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 . if( pCache->p
10978 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c Cache ){. sql
10979 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
1097a 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 .pcache.xDestroy
1097b 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29 (pCache->pCache)
1097c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 ;. }.}../* .**
1097d 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 Discard the cont
1097e 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 ents of the cach
1097f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
10980 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
10981 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61 3PcacheClear(PCa
10982 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 che *pCache){.
10983 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 sqlite3PcacheTru
10984 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29 ncate(pCache, 0)
10985 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 ;.}../*.** Merge
10986 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61 two lists of pa
10987 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 ges connected by
10988 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70 pDirty and in p
10989 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f gno order..** Do
1098a 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67 not both fixing
1098b 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20 the pDirtyPrev
1098c 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 pointers..*/.sta
1098d 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68 tic PgHdr *pcach
1098e 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28 eMergeDirtyList(
1098f 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72 PgHdr *pA, PgHdr
10990 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72 *pB){. PgHdr r
10991 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20 esult, *pTail;.
10992 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74 pTail = &result
10993 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 ;. while( pA &&
10994 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70 pB ){. if( p
10995 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f A->pgno<pB->pgno
10996 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d ){. pTail-
10997 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20 >pDirty = pA;.
10998 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a pTail = pA;.
10999 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
1099a 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65 Dirty;. }else
1099b 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 {. pTail->p
1099c 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20 Dirty = pB;.
1099d 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20 pTail = pB;.
1099e 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69 pB = pB->pDi
1099f 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 rty;. }. }.
109a0 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70 if( pA ){. p
109a1 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 Tail->pDirty = p
109a2 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 A;. }else if( p
109a3 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e B ){. pTail->
109a4 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d pDirty = pB;. }
109a5 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d else{. pTail-
109a6 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d >pDirty = 0;. }
109a7 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74 . return result
109a8 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a .pDirty;.}../*.*
109a9 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20 * Sort the list
109aa 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 of pages in acce
109ab 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70 nding order by p
109ac 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a gno. Pages are.
109ad 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 ** connected by
109ae 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e pDirty pointers.
109af 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76 The pDirtyPrev
109b0 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a pointers are.**
109b1 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68 corrupted by th
109b2 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53 is sort..**.** S
109b3 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f ince there canno
109b4 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32 t be more than 2
109b5 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67 ^31 distinct pag
109b6 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 es in a database
109b7 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f ,.** there canno
109b8 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33 t be more than 3
109b9 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72 1 buckets requir
109ba 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20 ed by the merge
109bb 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65 sorter..** One e
109bc 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61 xtra bucket is a
109bd 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76 dded to catch ov
109be 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73 erflow in case s
109bf 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72 omething.** ever
109c0 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65 changes to make
109c1 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 the previous se
109c2 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74 ntence incorrect
109c3 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53 ..*/.#define N_S
109c4 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73 ORT_BUCKET 32.s
109c5 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 tatic PgHdr *pca
109c6 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 cheSortDirtyList
109c7 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 (PgHdr *pIn){.
109c8 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f PgHdr *a[N_SORT_
109c9 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69 BUCKET], *p;. i
109ca 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61 nt i;. memset(a
109cb 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b , 0, sizeof(a));
109cc 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b . while( pIn ){
109cd 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20 . p = pIn;.
109ce 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74 pIn = p->pDirt
109cf 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79 y;. p->pDirty
109d0 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d = 0;. for(i=
109d1 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f 0; ALWAYS(i<N_SO
109d2 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b RT_BUCKET-1); i+
109d3 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b +){. if( a[
109d4 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 i]==0 ){.
109d5 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20 a[i] = p;.
109d6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
109d7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 }else{. p
109d8 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 = pcacheMergeDi
109d9 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29 rtyList(a[i], p)
109da 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d ;. a[i] =
109db 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
109dc 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 }. if( NEVER(
109dd 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 i==N_SORT_BUCKET
109de 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 -1) ){. /*
109df 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 To get here, the
109e0 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e re need to be 2^
109e1 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 (N_SORT_BUCKET)
109e2 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20 elements in.
109e3 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c ** the input l
109e4 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69 ist. But that i
109e5 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20 s impossible..
109e6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69 */. a[i
109e7 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 ] = pcacheMergeD
109e8 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 irtyList(a[i], p
109e9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 );. }. }. p
109ea 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 = a[0];. for(i
109eb 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43 =1; i<N_SORT_BUC
109ec 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 KET; i++){. p
109ed 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69 = pcacheMergeDi
109ee 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29 rtyList(p, a[i])
109ef 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
109f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
109f1 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 n a list of all
109f2 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 dirty pages in t
109f3 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64 he cache, sorted
109f4 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e by page number.
109f5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
109f6 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65 TE PgHdr *sqlite
109f7 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74 3PcacheDirtyList
109f8 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
109f9 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 {. PgHdr *p;.
109fa 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 for(p=pCache->pD
109fb 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 irty; p; p=p->pD
109fc 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 irtyNext){. p
109fd 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44 ->pDirty = p->pD
109fe 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 irtyNext;. }.
109ff 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72 return pcacheSor
10a00 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68 tDirtyList(pCach
10a01 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f e->pDirty);.}../
10a02 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 * .** Return the
10a03 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 total number of
10a04 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 referenced page
10a05 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 s held by the ca
10a06 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 che..*/.SQLITE_P
10a07 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
10a08 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 e3PcacheRefCount
10a09 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 (PCache *pCache)
10a0a 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68 {. return pCach
10a0b 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a e->nRef;.}../*.*
10a0c 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
10a0d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 ber of reference
10a0e 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75 s to the page su
10a0f 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 pplied as an arg
10a10 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ument..*/.SQLITE
10a11 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
10a12 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 ite3PcachePageRe
10a13 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29 fcount(PgHdr *p)
10a14 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 {. return p->nR
10a15 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 ef;.}../* .** Re
10a16 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e turn the total n
10a17 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
10a18 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a n the cache..*/.
10a19 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
10a1a 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 nt sqlite3Pcache
10a1b 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65 Pagecount(PCache
10a1c 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74 *pCache){. int
10a1d 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66 nPage = 0;. if
10a1e 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 ( pCache->pCache
10a1f 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 ){. nPage =
10a20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e sqlite3GlobalCon
10a21 66 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65 fig.pcache.xPage
10a22 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43 count(pCache->pC
10a23 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 ache);. }. ret
10a24 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69 urn nPage;.}..#i
10a25 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
10a26 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73 ./*.** Get the s
10a27 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 uggested cache-s
10a28 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 ize value..*/.SQ
10a29 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
10a2a 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65 sqlite3PcacheGe
10a2b 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 tCachesize(PCach
10a2c 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65 e *pCache){. re
10a2d 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61 turn pCache->nMa
10a2e 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a x;.}.#endif../*.
10a2f 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65 ** Set the sugge
10a30 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 sted cache-size
10a31 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 value..*/.SQLITE
10a32 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10a33 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 lite3PcacheSetCa
10a34 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a chesize(PCache *
10a35 70 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61 pCache, int mxPa
10a36 67 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e ge){. pCache->n
10a37 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 Max = mxPage;.
10a38 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 if( pCache->pCac
10a39 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 he ){. sqlite
10a3a 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 3GlobalConfig.pc
10a3b 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 ache.xCachesize(
10a3c 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 pCache->pCache,
10a3d 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a mxPage);. }.}..
10a3e 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
10a3f 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 TE_CHECK_PAGES)
10a40 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 || defined(SQLIT
10a41 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46 E_DEBUG)./*.** F
10a42 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 or all dirty pag
10a43 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 es currently in
10a44 74 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b the cache, invok
10a45 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a e the specified.
10a46 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69 ** callback. Thi
10a47 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 s is only used i
10a48 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45 f the SQLITE_CHE
10a49 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69 CK_PAGES macro i
10a4a 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f s.** defined..*/
10a4b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
10a4c 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 void sqlite3Pcac
10a4d 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 heIterateDirty(P
10a4e 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 Cache *pCache, v
10a4f 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 oid (*xIter)(PgH
10a50 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20 dr *)){. PgHdr
10a51 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70 *pDirty;. for(p
10a52 44 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44 Dirty=pCache->pD
10a53 69 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44 irty; pDirty; pD
10a54 69 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69 irty=pDirty->pDi
10a55 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49 rtyNext){. xI
10a56 74 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d ter(pDirty);. }
10a57 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a .}.#endif../****
10a58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f ********** End o
10a59 66 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a f pcache.c *****
10a5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a *********/./****
10a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e ********** Begin
10a5e 20 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20 file pcache1.c
10a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a *********/./*.**
10a62 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 2008 November 0
10a63 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 5.**.** The auth
10a64 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
10a65 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
10a66 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
10a67 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
10a68 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
10a69 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
10a6a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
10a6b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
10a6c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
10a6d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
10a6e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
10a6f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
10a70 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
10a71 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
10a72 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
10a73 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
10a74 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
10a75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10a79 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
10a7a 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 implements the
10a7b 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 default page cac
10a7c 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
10a7d 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 n (the.** sqlite
10a7e 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61 3_pcache interfa
10a7f 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e ce). It also con
10a80 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68 tains part of th
10a81 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
10a82 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54 .** of the SQLIT
10a83 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 E_CONFIG_PAGECAC
10a84 48 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 HE and sqlite3_r
10a85 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
10a86 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 features..** If
10a87 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 the default page
10a88 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 cache implement
10a89 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 ation is overrid
10a8a 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 en, then neither
10a8b 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f of.** these two
10a8c 20 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76 features are av
10a8d 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 0a 0a 74 79 ailable..*/...ty
10a8e 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61 pedef struct PCa
10a8f 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79 che1 PCache1;.ty
10a90 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48 pedef struct PgH
10a91 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65 dr1 PgHdr1;.type
10a92 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 65 def struct PgFre
10a93 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74 eslot PgFreeslot
10a94 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 ;../* Pointers t
10a95 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 o structures of
10a96 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 61 this type are ca
10a97 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 st and returned
10a98 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71 as .** opaque sq
10a99 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61 lite3_pcache* ha
10a9a 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20 ndles.*/.struct
10a9b 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43 PCache1 {. /* C
10a9c 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 ache configurati
10a9d 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50 on parameters. P
10a9e 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65 age size (szPage
10a9f 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61 ) and the purgea
10aa0 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62 ble. ** flag (b
10aa1 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73 Purgeable) are s
10aa2 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68 et when the cach
10aa3 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d e is created. nM
10aa4 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20 ax may be . **
10aa5 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20 modified at any
10aa6 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74 time by a call t
10aa7 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63 o the pcache1Cac
10aa8 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e heSize() method.
10aa9 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c . ** The global
10aaa 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 mutex must be h
10aab 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69 eld when accessi
10aac 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20 ng nMax.. */.
10aad 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20 int szPage;
10aae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10aaf 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 /* Size of a
10ab0 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 69 llocated pages i
10ab1 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 n bytes */. int
10ab2 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20 bPurgeable;
10ab3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10ab4 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68 /* True if cach
10ab5 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a e is purgeable *
10ab6 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10ab7 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20 nMin;
10ab8 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d /* Minim
10ab9 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 um number of pag
10aba 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 es reserved */.
10abb 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d unsigned int nM
10abc 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ax;
10abd 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 /* Configur
10abe 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20 ed "cache_size"
10abf 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48 value */.. /* H
10ac0 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c ash table of all
10ac1 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c pages. The foll
10ac2 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 owing variables
10ac3 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65 may only be acce
10ac4 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74 ssed. ** when t
10ac5 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 68 he accessor is h
10ac6 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61 olding the globa
10ac7 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 61 l mutex (see pca
10ac8 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 che1EnterMutex()
10ac9 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68 . ** and pcach
10aca 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e e1LeaveMutex()).
10acb 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 . */. unsigned
10acc 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 int nRecyclable
10acd 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
10ace 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 umber of pages i
10acf 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a n the LRU list *
10ad0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 /. unsigned int
10ad1 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 nPage;
10ad2 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
10ad3 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
10ad4 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20 in apHash */.
10ad5 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61 unsigned int nHa
10ad6 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 sh;
10ad7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
10ad8 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68 slots in apHash
10ad9 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a [] */. PgHdr1 *
10ada 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20 *apHash;
10adb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 /* H
10adc 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61 ash table for fa
10add 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79 st lookup by key
10ade 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20 */.. unsigned
10adf 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20 int iMaxKey;
10ae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
10ae1 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73 rgest key seen s
10ae2 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 29 ince xTruncate()
10ae3 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 */.};../*.** Ea
10ae4 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 69 ch cache entry i
10ae5 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 s represented by
10ae6 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 an instance of
10ae7 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a the following .*
10ae8 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 62 * structure. A b
10ae9 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e uffer of PgHdr1.
10aea 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62 pCache->szPage b
10aeb 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 ytes is allocate
10aec 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62 d .** directly b
10aed 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 75 63 efore this struc
10aee 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28 ture in memory (
10aef 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54 see the PGHDR1_T
10af0 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63 O_PAGE() .** mac
10af1 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74 ro below)..*/.st
10af2 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20 ruct PgHdr1 {.
10af3 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 unsigned int iKe
10af4 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f y; /
10af5 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67 * Key value (pag
10af6 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50 e number) */. P
10af7 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20 gHdr1 *pNext;
10af8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10af9 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61 Next in hash ta
10afa 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50 ble chain */. P
10afb 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20 Cache1 *pCache;
10afc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10afd 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72 Cache that curr
10afe 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20 ently owns this
10aff 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31 page */. PgHdr1
10b00 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20 *pLruNext;
10b01 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 /* Next
10b02 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 in LRU list of
10b03 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a unpinned pages *
10b04 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 /. PgHdr1 *pLru
10b05 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 Prev;
10b06 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 /* Previous i
10b07 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e n LRU list of un
10b08 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a pinned pages */.
10b09 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73 };../*.** Free s
10b0a 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f lots in the allo
10b0b 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69 cator used to di
10b0c 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66 vide up the buff
10b0d 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e er provided usin
10b0e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f g.** the SQLITE_
10b0f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10b10 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73 mechanism..*/.s
10b11 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74 truct PgFreeslot
10b12 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20 {. PgFreeslot
10b13 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 *pNext; /* Next
10b14 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b free slot */.};
10b15 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64 ../*.** Global d
10b16 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73 ata used by this
10b17 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 cache..*/.stati
10b18 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 c SQLITE_WSD str
10b19 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c uct PCacheGlobal
10b1a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 {. sqlite3_mut
10b1b 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 ex *mutex;
10b1c 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74 /* stat
10b1d 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53 ic mutex MUTEX_S
10b1e 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20 TATIC_LRU */..
10b1f 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20 int nMaxPage;
10b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b21 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d /* Sum of nM
10b22 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65 axPage for purge
10b23 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 able caches */.
10b24 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20 int nMinPage;
10b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b26 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e /* Sum of n
10b27 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67 MinPage for purg
10b28 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a eable caches */.
10b29 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61 int nCurrentPa
10b2a 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
10b2b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
10b2c 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67 of purgeable pag
10b2d 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a es allocated */.
10b2e 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65 PgHdr1 *pLruHe
10b2f 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20 ad, *pLruTail;
10b30 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73 /* LRU lis
10b31 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 t of unpinned pa
10b32 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72 ges */.. /* Var
10b33 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74 iables related t
10b34 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f o SQLITE_CONFIG_
10b35 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e PAGECACHE settin
10b36 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 gs. */. int szS
10b37 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 lot;
10b38 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b39 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65 Size of each fre
10b3a 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64 e slot */. void
10b3b 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b *pStart, *pEnd;
10b3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10b3d 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67 /* Bounds of pag
10b3e 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61 ecache malloc ra
10b3f 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73 nge */. PgFrees
10b40 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20 lot *pFree;
10b41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10b42 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73 Free page blocks
10b43 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 */. int isInit
10b44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10b45 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
10b46 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 e if initialized
10b47 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b */.} pcache1_g;
10b48 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65 ../*.** All code
10b49 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68 in this file sh
10b4a 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20 ould access the
10b4b 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 global structure
10b4c 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a above via the.*
10b4d 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31 * alias "pcache1
10b4e 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 ". This ensures
10b4f 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75 that the WSD emu
10b50 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77 lation is used w
10b51 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 hen.** compiling
10b52 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61 for systems tha
10b53 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 t do not support
10b54 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64 real WSD..*/.#d
10b55 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47 efine pcache1 (G
10b56 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61 LOBAL(struct PCa
10b57 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68 cheGlobal, pcach
10b58 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 e1_g))../*.** Wh
10b59 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75 en a PgHdr1 stru
10b5a 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 cture is allocat
10b5b 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74 ed, the associat
10b5c 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67 ed PCache1.szPag
10b5d 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61 e.** bytes of da
10b5e 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64 ta are located d
10b5f 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69 irectly before i
10b60 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65 t in memory (i.e
10b61 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73 . the total.** s
10b62 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 ize of the alloc
10b63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28 ation is sizeof(
10b64 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e PgHdr1)+PCache1.
10b65 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68 szPage byte). Th
10b66 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50 e.** PGHDR1_TO_P
10b67 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65 AGE() macro take
10b68 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
10b69 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72 PgHdr1 structur
10b6a 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d e as.** an argum
10b6b 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20 ent and returns
10b6c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
10b6d 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63 associated bloc
10b6e 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62 k of szPage.** b
10b6f 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54 ytes. The PAGE_T
10b70 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f O_PGHDR1() macro
10b71 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69 does the opposi
10b72 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74 te: its argument
10b73 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 is.** a pointer
10b74 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73 to a block of s
10b75 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64 zPage bytes of d
10b76 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75 ata and the retu
10b77 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 rn value is.** a
10b78 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
10b79 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72 associated PgHdr
10b7a 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 1 structure..**.
10b7b 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48 ** assert( PGH
10b7c 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45 DR1_TO_PAGE(PAGE
10b7d 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 _TO_PGHDR1(pCach
10b7e 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a e, X))==X );.*/.
10b7f 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54 #define PGHDR1_T
10b80 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 76 6f O_PAGE(p) (vo
10b81 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20 id*)(((char*)p)
10b82 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50 - p->pCache->szP
10b83 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47 age).#define PAG
10b84 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70 E_TO_PGHDR1(c, p
10b85 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 63 68 ) (PgHdr1*)(((ch
10b86 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61 ar*)p) + c->szPa
10b87 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f ge)../*.** Macro
10b88 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c s to enter and l
10b89 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 eave the global
10b8a 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 LRU mutex..*/.#d
10b8b 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74 efine pcache1Ent
10b8c 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 erMutex() sqlite
10b8d 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63 3_mutex_enter(pc
10b8e 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 ache1.mutex).#de
10b8f 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76 fine pcache1Leav
10b90 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 eMutex() sqlite3
10b91 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61 _mutex_leave(pca
10b92 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a che1.mutex)../**
10b93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10b97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10b98 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c ******* Page All
10b99 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 ocation/SQLITE_C
10b9a 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c ONFIG_PCACHE Rel
10b9b 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a ated Functions *
10b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
10b9d 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10b9e 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 ion is called du
10b9f 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 ring initializat
10ba0 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20 ion if a static
10ba1 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 buffer is .** su
10ba2 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f pplied to use fo
10ba3 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 r the page-cache
10ba4 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20 by passing the
10ba5 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 SQLITE_CONFIG_PA
10ba6 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 GECACHE.** verb
10ba7 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 to sqlite3_confi
10ba8 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70 g(). Parameter p
10ba9 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e Buf points to an
10baa 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 allocation larg
10bab 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 e.** enough to c
10bac 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65 ontain 'n' buffe
10bad 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73 rs of 'sz' bytes
10bae 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 each..*/.SQLITE
10baf 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
10bb0 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65 lite3PCacheBuffe
10bb1 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75 rSetup(void *pBu
10bb2 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e f, int sz, int n
10bb3 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31 ){. if( pcache1
10bb4 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50 .isInit ){. P
10bb5 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20 gFreeslot *p;.
10bb6 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e sz = ROUNDDOWN
10bb7 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68 8(sz);. pcach
10bb8 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a e1.szSlot = sz;.
10bb9 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 74 61 pcache1.pSta
10bba 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70 rt = pBuf;. p
10bbb 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30 cache1.pFree = 0
10bbc 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d ;. while( n--
10bbd 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50 ){. p = (P
10bbe 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b gFreeslot*)pBuf;
10bbf 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 . p->pNext
10bc0 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b = pcache1.pFree;
10bc1 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
10bc2 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 Free = p;.
10bc3 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28 pBuf = (void*)&(
10bc4 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d (char*)pBuf)[sz]
10bc5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 ;. }. pcac
10bc6 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b he1.pEnd = pBuf;
10bc7 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 . }.}../*.** Ma
10bc8 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 lloc function us
10bc9 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 ed within this f
10bca 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 ile to allocate
10bcb 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
10bcc 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 uffer.** configu
10bcd 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 red using sqlite
10bce 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
10bcf 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10bd0 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
10bd1 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 .** such buffer
10bd2 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 exists or there
10bd3 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 is no space left
10bd4 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e in it, this fun
10bd5 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 ction falls .**
10bd6 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d back to sqlite3M
10bd7 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 alloc()..*/.stat
10bd8 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 ic void *pcache1
10bd9 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 Alloc(int nByte)
10bda 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 {. void *p;. a
10bdb 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
10bdc 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 utex_held(pcache
10bdd 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 1.mutex) );. if
10bde 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 ( nByte<=pcache1
10bdf 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 .szSlot && pcach
10be0 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 e1.pFree ){.
10be1 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e assert( pcache1.
10be2 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20 isInit );. p
10be3 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63 = (PgHdr1 *)pcac
10be4 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 he1.pFree;. p
10be5 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 cache1.pFree = p
10be6 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e cache1.pFree->pN
10be7 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ext;. sqlite3
10be8 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 StatusSet(SQLITE
10be9 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 _STATUS_PAGECACH
10bea 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a E_SIZE, nByte);.
10beb 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 sqlite3Statu
10bec 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 sAdd(SQLITE_STAT
10bed 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 US_PAGECACHE_USE
10bee 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a D, 1);. }else{.
10bef 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 . /* Allocate
10bf0 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73 a new buffer us
10bf1 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f ing sqlite3Mallo
10bf2 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 c. Before doing
10bf3 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20 so, exit the.
10bf4 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68 ** global pcach
10bf5 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f e mutex and unlo
10bf6 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 ck the pager-cac
10bf7 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65 he object pCache
10bf8 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a . This is . *
10bf9 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65 * so that if the
10bfa 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f attempt to allo
10bfb 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 cate a new buffe
10bfc 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65 r causes the the
10bfd 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75 . ** configu
10bfe 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69 red soft-heap-li
10bff 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68 mit to be breach
10c00 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 ed, it will be p
10c01 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a ossible to. *
10c02 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 * reclaim memory
10c03 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72 from this pager
10c04 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 -cache.. */.
10c05 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d pcache1LeaveM
10c06 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20 utex();. p =
10c07 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42 sqlite3Malloc(nB
10c08 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 yte);. pcache
10c09 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 1EnterMutex();.
10c0a 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 if( p ){.
10c0b 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74 int sz = sqlit
10c0c 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b e3MallocSize(p);
10c0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 . sqlite3St
10c0e 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 atusAdd(SQLITE_S
10c0f 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f TATUS_PAGECACHE_
10c10 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20 OVERFLOW, sz);.
10c11 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
10c12 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 n p;.}../*.** Fr
10c13 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 ee an allocated
10c14 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 buffer obtained
10c15 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f from pcache1Allo
10c16 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c()..*/.static v
10c17 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28 oid pcache1Free(
10c18 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65 void *p){. asse
10c19 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
10c1a 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d x_held(pcache1.m
10c1b 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 utex) );. if( p
10c1c 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ==0 ) return;.
10c1d 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70 if( p>=pcache1.p
10c1e 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68 Start && p<pcach
10c1f 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50 e1.pEnd ){. P
10c20 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74 gFreeslot *pSlot
10c21 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10c22 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10c23 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 ATUS_PAGECACHE_U
10c24 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53 SED, -1);. pS
10c25 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f lot = (PgFreeslo
10c26 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d t*)p;. pSlot-
10c27 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 >pNext = pcache1
10c28 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63 .pFree;. pcac
10c29 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f he1.pFree = pSlo
10c2a 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 t;. }else{.
10c2b 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69 int iSize = sqli
10c2c 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 te3MallocSize(p)
10c2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 ;. sqlite3Sta
10c2e 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 tusAdd(SQLITE_ST
10c2f 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f ATUS_PAGECACHE_O
10c30 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 VERFLOW, -iSize)
10c31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 ;. sqlite3_fr
10c32 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ee(p);. }.}../*
10c33 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
10c34 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 ew page object i
10c35 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61 nitially associa
10c36 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70 ted with cache p
10c37 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 Cache..*/.static
10c38 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31 PgHdr1 *pcache1
10c39 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65 AllocPage(PCache
10c3a 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 1 *pCache){. in
10c3b 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 t nByte = sizeof
10c3c 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68 (PgHdr1) + pCach
10c3d 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69 e->szPage;. voi
10c3e 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31 d *pPg = pcache1
10c3f 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20 Alloc(nByte);.
10c40 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28 PgHdr1 *p;. if(
10c41 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20 pPg ){. p =
10c42 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10c43 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20 Cache, pPg);.
10c44 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10c45 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 rgeable ){.
10c46 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10c47 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 tPage++;. }.
10c48 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 }else{. p =
10c49 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
10c4a 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 p;.}../*.** Free
10c4b 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61 a page object a
10c4c 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63 llocated by pcac
10c4d 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a he1AllocPage()..
10c4e 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 **.** The pointe
10c4f 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 r is allowed to
10c50 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 be NULL, which i
10c51 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20 s prudent. But
10c52 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20 it turns out.**
10c53 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 that the current
10c54 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 implementation
10c55 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72 happens to never
10c56 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 call this routi
10c57 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c ne.** with a NUL
10c58 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65 L pointer, so we
10c59 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74 mark the NULL t
10c5a 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28 est with ALWAYS(
10c5b 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 )..*/.static voi
10c5c 64 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 d pcache1FreePag
10c5d 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20 e(PgHdr1 *p){.
10c5e 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b if( ALWAYS(p) ){
10c5f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63 . if( p->pCac
10c60 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29 he->bPurgeable )
10c61 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e {. pcache1.
10c62 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a nCurrentPage--;.
10c63 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65 }. pcache
10c64 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f 1Free(PGHDR1_TO_
10c65 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a PAGE(p));. }.}.
10c66 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75 ./*.** Malloc fu
10c67 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53 nction used by S
10c68 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20 QLite to obtain
10c69 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 space from the b
10c6a 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64 uffer configured
10c6b 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 .** using sqlite
10c6c 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 3_config(SQLITE_
10c6d 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 CONFIG_PAGECACHE
10c6e 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 ) option. If no
10c6f 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65 such buffer.** e
10c70 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63 xists, this func
10c71 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20 tion falls back
10c72 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 to sqlite3Malloc
10c73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ()..*/.SQLITE_PR
10c74 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 IVATE void *sqli
10c75 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e te3PageMalloc(in
10c76 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 t sz){. void *p
10c77 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10c78 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70 Mutex();. p = p
10c79 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b cache1Alloc(sz);
10c7a 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d . pcache1LeaveM
10c7b 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e utex();. return
10c7c 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 p;.}../*.** Fre
10c7d 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 e an allocated b
10c7e 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66 uffer obtained f
10c7f 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d rom sqlite3PageM
10c80 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 alloc()..*/.SQLI
10c81 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
10c82 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
10c83 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63 void *p){. pcac
10c84 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b he1EnterMutex();
10c85 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70 . pcache1Free(p
10c86 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 );. pcache1Leav
10c87 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a eMutex();.}../**
10c88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
10c8d 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 ******* General
10c8e 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46 Implementation F
10c8f 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a unctions *******
10c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10c91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a *************/..
10c92 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
10c93 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72 ion is used to r
10c94 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 esize the hash t
10c95 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 able used by the
10c96 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a cache passed.**
10c97 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
10c98 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 gument..**.** Th
10c99 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d e global mutex m
10c9a 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e ust be held when
10c9b 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
10c9c 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 s called..*/.sta
10c9d 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52 tic int pcache1R
10c9e 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65 esizeHash(PCache
10c9f 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20 1 *p){. PgHdr1
10ca0 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67 **apNew;. unsig
10ca1 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20 ned int nNew;.
10ca2 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a unsigned int i;.
10ca3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10ca4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10ca5 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10ca6 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61 . nNew = p->nHa
10ca7 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77 sh*2;. if( nNew
10ca8 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77 <256 ){. nNew
10ca9 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70 = 256;. }.. p
10caa 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10cab 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 ();. if( p->nHa
10cac 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67 sh ){ sqlite3Beg
10cad 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 inBenignMalloc()
10cae 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50 ; }. apNew = (P
10caf 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33 gHdr1 **)sqlite3
10cb0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50 _malloc(sizeof(P
10cb1 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a gHdr1 *)*nNew);.
10cb2 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29 if( p->nHash )
10cb3 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 { sqlite3EndBeni
10cb4 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20 gnMalloc(); }.
10cb5 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 pcache1EnterMute
10cb6 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77 x();. if( apNew
10cb7 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61 ){. memset(a
10cb8 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 pNew, 0, sizeof(
10cb9 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b PgHdr1 *)*nNew);
10cba 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
10cbb 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a p->nHash; i++){.
10cbc 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 PgHdr1 *pP
10cbd 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72 age;. PgHdr
10cbe 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70 1 *pNext = p->ap
10cbf 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77 Hash[i];. w
10cc0 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70 hile( (pPage = p
10cc1 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 Next)!=0 ){.
10cc2 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
10cc3 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 h = pPage->iKey
10cc4 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 % nNew;.
10cc5 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e pNext = pPage->
10cc6 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 pNext;. p
10cc7 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 Page->pNext = ap
10cc8 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 New[h];.
10cc9 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 apNew[h] = pPage
10cca 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
10ccb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 sqlite3_free
10ccc 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 (p->apHash);.
10ccd 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e p->apHash = apN
10cce 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 ew;. p->nHash
10ccf 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 = nNew;. }..
10cd0 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 return (p->apHas
10cd1 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 h ? SQLITE_OK :
10cd2 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d SQLITE_NOMEM);.}
10cd3 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ../*.** This fun
10cd4 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e ction is used in
10cd5 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f ternally to remo
10cd6 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67 ve the page pPag
10cd7 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 e from the .** g
10cd8 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 lobal LRU list,
10cd9 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 if is part of it
10cda 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f . If pPage is no
10cdb 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c t part of the gl
10cdc 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 obal.** LRU list
10cdd 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 , then this func
10cde 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e tion is a no-op.
10cdf 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10ce0 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10ce1 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10ce2 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10ce3 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10ce4 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 d pcache1PinPage
10ce5 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
10ce6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
10ce7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 e3_mutex_held(pc
10ce8 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a ache1.mutex) );.
10ce9 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 if( pPage && (
10cea 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10ceb 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 || pPage==pcache
10cec 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 1.pLruTail) ){.
10ced 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c if( pPage->pL
10cee 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 ruPrev ){.
10cef 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d pPage->pLruPrev-
10cf0 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 >pLruNext = pPag
10cf1 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 e->pLruNext;.
10cf2 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 }. if( pPage
10cf3 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 ->pLruNext ){.
10cf4 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e pPage->pLruN
10cf5 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 ext->pLruPrev =
10cf6 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b pPage->pLruPrev;
10cf7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
10cf8 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d cache1.pLruHead=
10cf9 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 =pPage ){.
10cfa 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 pcache1.pLruHead
10cfb 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 = pPage->pLruNe
10cfc 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 xt;. }. if
10cfd 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 ( pcache1.pLruTa
10cfe 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 il==pPage ){.
10cff 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 pcache1.pLruT
10d00 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 ail = pPage->pLr
10d01 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 uPrev;. }.
10d02 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10d03 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d = 0;. pPage-
10d04 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 >pLruPrev = 0;.
10d05 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 pPage->pCache
10d06 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b ->nRecyclable--;
10d07 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 . }.}.../*.** R
10d08 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 emove the page s
10d09 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 upplied as an ar
10d0a 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 gument from the
10d0b 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 hash table .** (
10d0c 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 PCache1.apHash s
10d0d 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 tructure) that i
10d0e 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 t is currently s
10d0f 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 tored in..**.**
10d10 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 The global mutex
10d11 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 must be held wh
10d12 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e en this function
10d13 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 is called..*/.s
10d14 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10d15 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 e1RemoveFromHash
10d16 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b (PgHdr1 *pPage){
10d17 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10d18 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 h;. PCache1 *pC
10d19 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 ache = pPage->pC
10d1a 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a ache;. PgHdr1 *
10d1b 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 *pp;.. h = pPag
10d1c 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 e->iKey % pCache
10d1d 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 ->nHash;. for(p
10d1e 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 p=&pCache->apHas
10d1f 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 h[h]; (*pp)!=pPa
10d20 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 ge; pp=&(*pp)->p
10d21 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 Next);. *pp = (
10d22 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 *pp)->pNext;..
10d23 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b pCache->nPage--;
10d24 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
10d25 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 re are currently
10d26 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 more than pcach
10d27 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 e.nMaxPage pages
10d28 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a allocated, try.
10d29 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 ** to recycle pa
10d2a 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 ges to reduce th
10d2b 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 e number allocat
10d2c 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 ed to pcache.nMa
10d2d 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 xPage..*/.static
10d2e 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 void pcache1Enf
10d2f 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 orceMaxPage(void
10d30 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ){. assert( sql
10d31 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
10d32 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 pcache1.mutex) )
10d33 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 ;. while( pcach
10d34 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e e1.nCurrentPage>
10d35 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 pcache1.nMaxPage
10d36 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 && pcache1.pLru
10d37 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 Tail ){. PgHd
10d38 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e r1 *p = pcache1.
10d39 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 pLruTail;. pc
10d3a 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b ache1PinPage(p);
10d3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f . pcache1Remo
10d3c 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 veFromHash(p);.
10d3d 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 pcache1FreePa
10d3e 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a ge(p);. }.}../*
10d3f 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 .** Discard all
10d40 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 pages from cache
10d41 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70 pCache with a p
10d42 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 age number (key
10d43 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 value) .** great
10d44 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c er than or equal
10d45 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 to iLimit. Any
10d46 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 pinned pages tha
10d47 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 t meet this .**
10d48 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 criteria are unp
10d49 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 inned before the
10d4a 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e y are discarded.
10d4b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 .**.** The globa
10d4c 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 l mutex must be
10d4d 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 held when this f
10d4e 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
10d4f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 d..*/.static voi
10d50 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 d pcache1Truncat
10d51 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 eUnsafe(. PCach
10d52 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 e1 *pCache, . u
10d53 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d nsigned int iLim
10d54 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c it .){. TESTONL
10d55 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 Y( unsigned int
10d56 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20 nPage = 0; )
10d57 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73 /* Used to ass
10d58 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 ert pCache->nPag
10d59 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a e is correct */.
10d5a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 unsigned int h
10d5b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
10d5c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
10d5d 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b cache1.mutex) );
10d5e 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43 . for(h=0; h<pC
10d5f 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b ache->nHash; h++
10d60 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a ){. PgHdr1 **
10d61 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 pp = &pCache->ap
10d62 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67 Hash[h]; . Pg
10d63 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20 Hdr1 *pPage;.
10d64 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d while( (pPage =
10d65 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20 *pp)!=0 ){.
10d66 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65 if( pPage->iKe
10d67 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 y>=iLimit ){.
10d68 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 pCache->nPa
10d69 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70 ge--;. *p
10d6a 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 p = pPage->pNext
10d6b 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 ;. pcache
10d6c 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 1PinPage(pPage);
10d6d 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 . pcache1
10d6e 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b FreePage(pPage);
10d6f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
10d70 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 61 67 pp = &pPag
10d71 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 e->pNext;.
10d72 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67 TESTONLY( nPag
10d73 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20 e++; ). }.
10d74 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 }. }. asser
10d75 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 t( pCache->nPage
10d76 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a ==nPage );.}../*
10d77 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
10d7c 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 ******** sqlite3
10d7d 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20 _pcache Methods
10d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
10d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
10d81 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 ./*.** Implement
10d82 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c ation of the sql
10d83 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69 ite3_pcache.xIni
10d84 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 t method..*/.sta
10d85 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49 tic int pcache1I
10d86 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 nit(void *NotUse
10d87 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 d){. UNUSED_PAR
10d88 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b AMETER(NotUsed);
10d89 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 . assert( pcach
10d8a 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a e1.isInit==0 );.
10d8b 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65 memset(&pcache
10d8c 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 1, 0, sizeof(pca
10d8d 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71 che1));. if( sq
10d8e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 lite3GlobalConfi
10d8f 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a g.bCoreMutex ){.
10d90 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65 pcache1.mute
10d91 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 x = sqlite3_mute
10d92 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d x_alloc(SQLITE_M
10d93 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29 UTEX_STATIC_LRU)
10d94 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e ;. }. pcache1.
10d95 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65 isInit = 1;. re
10d96 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
10d97 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 }../*.** Impleme
10d98 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 ntation of the s
10d99 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53 qlite3_pcache.xS
10d9a 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a hutdown method..
10d9b 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 ** Note that the
10d9c 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c static mutex al
10d9d 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74 located in xInit
10d9e 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65 does .** not ne
10d9f 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a ed to be freed..
10da0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 */.static void p
10da1 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76 cache1Shutdown(v
10da2 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 oid *NotUsed){.
10da3 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
10da4 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 R(NotUsed);. as
10da5 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73 sert( pcache1.is
10da6 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d Init!=0 );. mem
10da7 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c set(&pcache1, 0,
10da8 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29 sizeof(pcache1)
10da9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c );.}../*.** Impl
10daa 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 ementation of th
10dab 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 e sqlite3_pcache
10dac 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e .xCreate method.
10dad 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 .**.** Allocate
10dae 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a a new cache..*/.
10daf 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70 static sqlite3_p
10db0 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 72 cache *pcache1Cr
10db1 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c eate(int szPage,
10db2 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 int bPurgeable)
10db3 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 {. PCache1 *pCa
10db4 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d che;.. pCache =
10db5 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69 (PCache1 *)sqli
10db6 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f te3_malloc(sizeo
10db7 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69 f(PCache1));. i
10db8 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20 f( pCache ){.
10db9 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20 memset(pCache,
10dba 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 0, sizeof(PCache
10dbb 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 1));. pCache-
10dbc 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65 >szPage = szPage
10dbd 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50 ;. pCache->bP
10dbe 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72 urgeable = (bPur
10dbf 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b geable ? 1 : 0);
10dc0 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 61 . if( bPurgea
10dc1 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61 ble ){. pCa
10dc2 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a che->nMin = 10;.
10dc3 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e 74 pcache1Ent
10dc4 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 erMutex();.
10dc5 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 pcache1.nMinPag
10dc6 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 e += pCache->nMi
10dc7 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 n;. pcache1
10dc8 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 LeaveMutex();.
10dc9 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
10dca 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 (sqlite3_pcache
10dcb 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a *)pCache;.}../*
10dcc 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10dcd 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10dce 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 3_pcache.xCaches
10dcf 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a ize method. .**.
10dd0 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 ** Configure the
10dd1 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 cache_size limi
10dd2 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a t for a cache..*
10dd3 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 /.static void pc
10dd4 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 73 ache1Cachesize(s
10dd5 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 qlite3_pcache *p
10dd6 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 , int nMax){. P
10dd7 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10dd8 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
10dd9 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 if( pCache->bPu
10dda 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 rgeable ){. p
10ddb 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 cache1EnterMutex
10ddc 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e ();. pcache1.
10ddd 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 nMaxPage += (nMa
10dde 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 x - pCache->nMax
10ddf 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e );. pCache->n
10de0 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 Max = nMax;.
10de1 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 pcache1EnforceMa
10de2 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61 xPage();. pca
10de3 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 che1LeaveMutex()
10de4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 ;. }.}../*.** I
10de5 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 mplementation of
10de6 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 the sqlite3_pca
10de7 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d che.xPagecount m
10de8 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 ethod. .*/.stati
10de9 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 67 c int pcache1Pag
10dea 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 ecount(sqlite3_p
10deb 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 cache *p){. int
10dec 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 n;. pcache1Ent
10ded 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d erMutex();. n =
10dee 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d ((PCache1 *)p)-
10def 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65 >nPage;. pcache
10df0 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 1LeaveMutex();.
10df1 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a return n;.}../*
10df2 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 .** Implementati
10df3 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 on of the sqlite
10df4 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20 3_pcache.xFetch
10df5 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 method. .**.** F
10df6 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 6b etch a page by k
10df7 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 ey value..**.**
10df8 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 Whether or not a
10df9 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 65 new page may be
10dfa 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 allocated by th
10dfb 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 is function depe
10dfc 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 nds on.** the va
10dfd 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 74 lue of the creat
10dfe 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20 eFlag argument.
10dff 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20 0 means do not
10e00 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a allocate a new.*
10e01 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73 * page. 1 means
10e02 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 allocate a new
10e03 70 61 67 65 20 69 66 20 73 70 61 63 65 20 69 73 page if space is
10e04 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c easily availabl
10e05 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20 e. 2 .** means
10e06 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61 to try really ha
10e07 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 rd to allocate a
10e08 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a new page..**.**
10e09 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65 For a non-purge
10e0a 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63 61 able cache (a ca
10e0b 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65 20 che used as the
10e0c 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69 storage for an i
10e0d 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 n-memory.** data
10e0e 62 61 73 65 29 20 74 68 65 72 65 20 69 73 20 72 base) there is r
10e0f 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65 eally no differe
10e10 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 65 61 nce between crea
10e11 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20 teFlag 1 and 2.
10e12 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 So.** the calli
10e13 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61 ng function (pca
10e14 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65 che.c) will neve
10e15 72 20 68 61 76 65 20 61 20 63 72 65 61 74 65 46 r have a createF
10e16 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61 lag of 1 on.** a
10e17 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61 non-purgable ca
10e18 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 che..**.** There
10e19 20 61 72 65 20 74 68 72 65 65 20 64 69 66 66 65 are three diffe
10e1a 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20 rent approaches
10e1b 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61 to obtaining spa
10e1c 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a ce for a page,.*
10e1d 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 * depending on t
10e1e 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61 he value of para
10e1f 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c 61 67 meter createFlag
10e20 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 30 (which may be 0
10e21 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a , 1 or 2)..**.**
10e22 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73 1. Regardless
10e23 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 of the value of
10e24 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65 createFlag, the
10e25 20 63 61 63 68 65 20 69 73 20 73 65 61 72 63 68 cache is search
10e26 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20 ed for a .**
10e27 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 copy of the re
10e28 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 49 66 quested page. If
10e29 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69 one is found, i
10e2a 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a t is returned..*
10e2b 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65 *.** 2. If cre
10e2c 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74 ateFlag==0 and t
10e2d 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 he page is not a
10e2e 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61 lready in the ca
10e2f 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 che, NULL is.**
10e30 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a returned..*
10e31 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65 *.** 3. If cre
10e32 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e ateFlag is 1, an
10e33 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f d the page is no
10e34 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 t already in the
10e35 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20 cache,.**
10e36 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 6f 66 and if either of
10e37 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
10e38 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20 re true, return
10e39 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 NULL:.**.**
10e3a 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72 (a) the number
10e3b 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64 of pages pinned
10e3c 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73 by the cache is
10e3d 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a greater than.**
10e3e 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68 PCach
10e3f 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20 e1.nMax, or.**
10e40 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d (b) the num
10e41 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e ber of pages pin
10e42 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 ned by the cache
10e43 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e is greater than
10e44 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 .** th
10e45 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f e sum of nMax fo
10e46 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 r all purgeable
10e47 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65 caches, less the
10e48 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20 sum of .**
10e49 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61 nMin for a
10e4a 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62 ll other purgeab
10e4b 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a le caches. .**.*
10e4c 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f * 4. If none o
10e4d 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 f the first thre
10e4e 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70 e conditions app
10e4f 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 65 ly and the cache
10e50 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20 is marked.**
10e51 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c as purgeable,
10e52 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74 and if one of t
10e53 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
10e54 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 true:.**.**
10e55 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72 (a) The number
10e56 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61 of pages alloca
10e57 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68 ted for the cach
10e58 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a e is already .**
10e59 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68 PCach
10e5a 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a e1.nMax, or.**.*
10e5b 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 20 * (b) The
10e5c 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
10e5d 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c allocated for al
10e5e 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68 l purgeable cach
10e5f 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 es is.**
10e60 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c already equal
10e61 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 to or greater t
10e62 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e han the sum of n
10e63 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20 Max for all.**
10e64 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 62 purgeab
10e65 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a le caches,.**.**
10e66 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d then attem
10e67 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 pt to recycle a
10e68 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52 page from the LR
10e69 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73 U list. If it is
10e6a 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20 the right.**
10e6b 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20 size, return
10e6c 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 66 the recycled buf
10e6d 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 fer. Otherwise,
10e6e 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20 free the buffer
10e6f 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63 and.** proc
10e70 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a eed to step 5. .
10e71 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72 **.** 5. Other
10e72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 wise, allocate a
10e73 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20 nd return a new
10e74 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a page buffer..*/.
10e75 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 static void *pca
10e76 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65 che1Fetch(sqlite
10e77 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 3_pcache *p, uns
10e78 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20 igned int iKey,
10e79 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b int createFlag){
10e7a 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 . unsigned int
10e7b 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68 nPinned;. PCach
10e7c 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 e1 *pCache = (PC
10e7d 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 ache1 *)p;. PgH
10e7e 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a dr1 *pPage = 0;.
10e7f 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 . assert( pCach
10e80 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c e->bPurgeable ||
10e81 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29 createFlag!=1 )
10e82 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10e83 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63 Mutex();. if( c
10e84 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 reateFlag==1 ) s
10e85 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
10e86 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a nMalloc();.. /*
10e87 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68 Search the hash
10e88 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78 table for an ex
10e89 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f isting entry. */
10e8a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e . if( pCache->n
10e8b 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e Hash>0 ){. un
10e8c 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 signed int h = i
10e8d 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 Key % pCache->nH
10e8e 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61 ash;. for(pPa
10e8f 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73 ge=pCache->apHas
10e90 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61 h[h]; pPage&&pPa
10e91 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20 ge->iKey!=iKey;
10e92 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65 pPage=pPage->pNe
10e93 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 xt);. }.. if(
10e94 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46 pPage || createF
10e95 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 lag==0 ){. pc
10e96 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 ache1PinPage(pPa
10e97 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65 ge);. goto fe
10e98 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 tch_out;. }..
10e99 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61 /* Step 3 of hea
10e9a 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a der comment. */.
10e9b 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63 nPinned = pCac
10e9c 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63 he->nPage - pCac
10e9d 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b he->nRecyclable;
10e9e 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 . if( createFla
10e9f 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20 g==1 && (.
10ea0 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63 nPinned>=(pcac
10ea1 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61 he1.nMaxPage+pCa
10ea2 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65 che->nMin-pcache
10ea3 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20 1.nMinPage).
10ea4 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43 || nPinned>=(pC
10ea5 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f ache->nMax * 9 /
10ea6 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 10). )){. g
10ea7 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 oto fetch_out;.
10ea8 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 }.. if( pCache
10ea9 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d ->nPage>=pCache-
10eaa 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 >nHash && pcache
10eab 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 1ResizeHash(pCac
10eac 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 he) ){. goto
10ead 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a fetch_out;. }..
10eae 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 /* Step 4. Try
10eaf 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 to recycle a pa
10eb0 67 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70 ge buffer if app
10eb1 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 ropriate. */. i
10eb2 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 f( pCache->bPurg
10eb3 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 eable && pcache1
10eb4 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 .pLruTail && (.
10eb5 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61 (pCache->nPa
10eb6 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d ge+1>=pCache->nM
10eb7 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e ax) || pcache1.n
10eb8 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 CurrentPage>=pca
10eb9 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 che1.nMaxPage.
10eba 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 )){. pPage =
10ebb 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
10ebc 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d ;. pcache1Rem
10ebd 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 oveFromHash(pPag
10ebe 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 e);. pcache1P
10ebf 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 inPage(pPage);.
10ec0 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 if( pPage->pC
10ec1 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 ache->szPage!=pC
10ec2 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a ache->szPage ){.
10ec3 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65 pcache1Fre
10ec4 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 ePage(pPage);.
10ec5 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 pPage = 0;.
10ec6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
10ec7 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 pcache1.nCurrent
10ec8 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e Page -= (pPage->
10ec9 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 pCache->bPurgeab
10eca 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 le - pCache->bPu
10ecb 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a rgeable);. }.
10ecc 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 }.. /* Step 5
10ecd 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 . If a usable pa
10ece 67 65 20 62 75 66 66 65 72 20 68 61 73 20 73 74 ge buffer has st
10ecf 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 ill not been fou
10ed0 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 nd, . ** attemp
10ed1 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 t to allocate a
10ed2 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 new one. . */.
10ed3 20 69 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 if( !pPage ){.
10ed4 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 pPage = pcach
10ed5 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 e1AllocPage(pCac
10ed6 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 he);. }.. if(
10ed7 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 pPage ){. uns
10ed8 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b igned int h = iK
10ed9 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 ey % pCache->nHa
10eda 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e sh;. pCache->
10edb 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61 nPage++;. pPa
10edc 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b ge->iKey = iKey;
10edd 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 . pPage->pNex
10ede 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61 t = pCache->apHa
10edf 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65 sh[h];. pPage
10ee0 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68 ->pCache = pCach
10ee1 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c e;. pPage->pL
10ee2 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 ruPrev = 0;.
10ee3 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 pPage->pLruNext
10ee4 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20 = 0;. *(void
10ee5 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41 **)(PGHDR1_TO_PA
10ee6 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a GE(pPage)) = 0;.
10ee7 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61 pCache->apHa
10ee8 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20 sh[h] = pPage;.
10ee9 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20 }..fetch_out:.
10eea 20 69 66 28 20 70 50 61 67 65 20 26 26 20 69 4b if( pPage && iK
10eeb 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b ey>pCache->iMaxK
10eec 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 ey ){. pCache
10eed 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79 ->iMaxKey = iKey
10eee 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61 ;. }. if( crea
10eef 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69 teFlag==1 ) sqli
10ef0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
10ef1 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c oc();. pcache1L
10ef2 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 eaveMutex();. r
10ef3 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50 eturn (pPage ? P
10ef4 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50 GHDR1_TO_PAGE(pP
10ef5 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f age) : 0);.}.../
10ef6 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10ef7 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10ef8 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e e3_pcache.xUnpin
10ef9 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d method..**.** M
10efa 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e ark a page as un
10efb 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65 pinned (eligible
10efc 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75 for asynchronou
10efd 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f s recycling)..*/
10efe 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 .static void pca
10eff 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65 che1Unpin(sqlite
10f00 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69 3_pcache *p, voi
10f01 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73 d *pPg, int reus
10f02 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43 eUnlikely){. PC
10f03 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 ache1 *pCache =
10f04 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 (PCache1 *)p;.
10f05 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 PgHdr1 *pPage =
10f06 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 PAGE_TO_PGHDR1(p
10f07 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20 Cache, pPg);. .
10f08 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
10f09 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29 pCache==pCache )
10f0a 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 ;. pcache1Enter
10f0b 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49 Mutex();.. /* I
10f0c 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f t is an error to
10f0d 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 call this funct
10f0e 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20 ion if the page
10f0f 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a is already . **
10f10 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f part of the glo
10f11 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20 bal LRU list..
10f12 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 */. assert( pPa
10f13 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20 ge->pLruPrev==0
10f14 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 && pPage->pLruNe
10f15 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 xt==0 );. asser
10f16 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 t( pcache1.pLruH
10f17 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63 ead!=pPage && pc
10f18 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d ache1.pLruTail!=
10f19 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20 pPage );.. if(
10f1a 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c reuseUnlikely ||
10f1b 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e pcache1.nCurren
10f1c 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d tPage>pcache1.nM
10f1d 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63 axPage ){. pc
10f1e 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 ache1RemoveFromH
10f1f 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20 ash(pPage);.
10f20 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28 pcache1FreePage(
10f21 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b pPage);. }else{
10f22 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 . /* Add the
10f23 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62 page to the glob
10f24 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72 al LRU list. Nor
10f25 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20 mally, the page
10f26 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20 is added to.
10f27 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 ** the head of t
10f28 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61 he list (last pa
10f29 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 ge to be recycle
10f2a 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 d). However, if
10f2b 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73 the . ** reus
10f2c 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70 eUnlikely flag p
10f2d 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 assed to this fu
10f2e 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20 nction is true,
10f2f 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65 the page is adde
10f30 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 d. ** to the
10f31 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74 tail of the list
10f32 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20 (first page to
10f33 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20 be recycled)..
10f34 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61 */. if( pca
10f35 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b che1.pLruHead ){
10f36 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 . pcache1.p
10f37 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65 LruHead->pLruPre
10f38 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20 v = pPage;.
10f39 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 pPage->pLruNext
10f3a 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 = pcache1.pLruH
10f3b 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68 ead;. pcach
10f3c 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50 e1.pLruHead = pP
10f3d 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a age;. }else{.
10f3e 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10f3f 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a ruTail = pPage;.
10f40 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c pcache1.pL
10f41 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a ruHead = pPage;.
10f42 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65 }. pCache
10f43 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b ->nRecyclable++;
10f44 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c . }.. pcache1L
10f45 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a eaveMutex();.}..
10f46 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 /*.** Implementa
10f47 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 tion of the sqli
10f48 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65 te3_pcache.xReke
10f49 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 y method. .*/.st
10f4a 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10f4b 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 1Rekey(. sqlite
10f4c 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76 3_pcache *p,. v
10f4d 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69 oid *pPg,. unsi
10f4e 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20 gned int iOld,.
10f4f 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e unsigned int iN
10f50 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20 ew.){. PCache1
10f51 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 *pCache = (PCach
10f52 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31 e1 *)p;. PgHdr1
10f53 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54 *pPage = PAGE_T
10f54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c O_PGHDR1(pCache,
10f55 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20 pPg);. PgHdr1
10f56 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 **pp;. unsigned
10f57 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72 int h; . asser
10f58 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d t( pPage->iKey==
10f59 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 iOld );. assert
10f5a 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d ( pPage->pCache=
10f5b 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63 =pCache );.. pc
10f5c 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 ache1EnterMutex(
10f5d 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70 );.. h = iOld%p
10f5e 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 Cache->nHash;.
10f5f 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70 pp = &pCache->ap
10f60 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65 Hash[h];. while
10f61 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29 ( (*pp)!=pPage )
10f62 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 {. pp = &(*pp
10f63 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 )->pNext;. }.
10f64 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 *pp = pPage->pNe
10f65 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25 xt;.. h = iNew%
10f66 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 pCache->nHash;.
10f67 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 pPage->iKey = i
10f68 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e New;. pPage->pN
10f69 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 ext = pCache->ap
10f6a 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68 Hash[h];. pCach
10f6b 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70 e->apHash[h] = p
10f6c 50 61 67 65 3b 0a 20 20 69 66 28 20 69 4e 65 77 Page;. if( iNew
10f6d 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 >pCache->iMaxKey
10f6e 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e ){. pCache->
10f6f 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a iMaxKey = iNew;.
10f70 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65 }.. pcache1Le
10f71 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f aveMutex();.}../
10f72 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 *.** Implementat
10f73 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 ion of the sqlit
10f74 65 33 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63 e3_pcache.xTrunc
10f75 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a ate method. .**.
10f76 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75 ** Discard all u
10f77 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e npinned pages in
10f78 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20 the cache with
10f79 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71 a page number eq
10f7a 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 ual to.** or gre
10f7b 61 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65 ater than parame
10f7c 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 ter iLimit. Any
10f7d 70 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74 pinned pages wit
10f7e 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a h a page number.
10f7f 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67 ** equal to or g
10f80 72 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d reater than iLim
10f81 69 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c it are implicitl
10f82 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 y unpinned..*/.s
10f83 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 tatic void pcach
10f84 65 31 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 e1Truncate(sqlit
10f85 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e e3_pcache *p, un
10f86 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69 signed int iLimi
10f87 74 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 t){. PCache1 *p
10f88 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 Cache = (PCache1
10f89 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 *)p;. pcache1E
10f8a 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 nterMutex();. i
10f8b 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68 f( iLimit<=pCach
10f8c 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20 e->iMaxKey ){.
10f8d 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 pcache1Truncat
10f8e 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20 eUnsafe(pCache,
10f8f 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61 iLimit);. pCa
10f90 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 che->iMaxKey = i
10f91 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70 Limit-1;. }. p
10f92 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10f93 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 ();.}../*.** Imp
10f94 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 lementation of t
10f95 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 he sqlite3_pcach
10f96 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f e.xDestroy metho
10f97 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f d. .**.** Destro
10f98 79 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 y a cache alloca
10f99 74 65 64 20 75 73 69 6e 67 20 70 63 61 63 68 65 ted using pcache
10f9a 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74 1Create()..*/.st
10f9b 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 atic void pcache
10f9c 31 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 1Destroy(sqlite3
10f9d 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50 _pcache *p){. P
10f9e 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d Cache1 *pCache =
10f9f 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 (PCache1 *)p;.
10fa0 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 pcache1EnterMut
10fa1 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54 ex();. pcache1T
10fa2 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 runcateUnsafe(pC
10fa3 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63 ache, 0);. pcac
10fa4 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20 he1.nMaxPage -=
10fa5 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20 pCache->nMax;.
10fa6 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 pcache1.nMinPage
10fa7 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e -= pCache->nMin
10fa8 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 ;. pcache1Enfor
10fa9 63 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70 ceMaxPage();. p
10faa 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 cache1LeaveMutex
10fab 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 ();. sqlite3_fr
10fac 65 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73 ee(pCache->apHas
10fad 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 h);. sqlite3_fr
10fae 65 65 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f ee(pCache);.}../
10faf 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
10fb0 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 on is called dur
10fb1 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 ing initializati
10fb2 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74 on (sqlite3_init
10fb3 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20 ialize()) to.**
10fb4 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61 install the defa
10fb5 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61 ult pluggable ca
10fb6 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75 che module, assu
10fb7 6d 69 6e 67 20 74 68 65 20 75 73 65 72 20 68 61 ming the user ha
10fb8 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 s not.** already
10fb9 20 70 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74 provided an alt
10fba 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c ernative..*/.SQL
10fbb 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
10fbc 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 sqlite3PCacheSe
10fbd 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a tDefault(void){.
10fbe 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 static sqlite3
10fbf 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 _pcache_methods
10fc0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d defaultMethods =
10fc1 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 {. 0,
10fc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10fc3 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70 /* pArg */. p
10fc4 63 61 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20 cache1Init,
10fc5 20 20 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74 /* xInit
10fc6 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53 */. pcache1S
10fc7 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20 hutdown,
10fc8 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f /* xShutdown */
10fc9 0a 20 20 20 20 70 63 61 63 68 65 31 43 72 65 61 . pcache1Crea
10fca 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a te, /*
10fcb 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20 xCreate */.
10fcc 70 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 pcache1Cachesize
10fcd 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63 , /* xCac
10fce 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63 hesize */. pc
10fcf 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20 ache1Pagecount,
10fd0 20 20 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63 /* xPagec
10fd1 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63 ount */. pcac
10fd2 68 65 31 46 65 74 63 68 2c 20 20 20 20 20 20 20 he1Fetch,
10fd3 20 20 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a /* xFetch *
10fd4 2f 0a 20 20 20 20 70 63 61 63 68 65 31 55 6e 70 /. pcache1Unp
10fd5 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f in, /
10fd6 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20 * xUnpin */.
10fd7 70 63 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20 pcache1Rekey,
10fd8 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b /* xRek
10fd9 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 ey */. pcache
10fda 31 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 1Truncate,
10fdb 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 /* xTruncate
10fdc 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 44 65 */. pcache1De
10fdd 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 stroy
10fde 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20 /* xDestroy */.
10fdf 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f };. sqlite3_co
10fe0 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 nfig(SQLITE_CONF
10fe1 49 47 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61 IG_PCACHE, &defa
10fe2 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a ultMethods);.}..
10fe3 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e #ifdef SQLITE_EN
10fe4 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 ABLE_MEMORY_MANA
10fe5 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69 GEMENT./*.** Thi
10fe6 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 s function is ca
10fe7 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70 lled to free sup
10fe8 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63 erfluous dynamic
10fe9 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d ally allocated m
10fea 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79 emory.** held by
10feb 20 74 68 65 20 70 61 67 65 72 20 73 79 73 74 65 the pager syste
10fec 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65 m. Memory in use
10fed 20 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70 by any SQLite p
10fee 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a ager allocated.*
10fef 2a 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 * by the current
10ff0 20 74 68 72 65 61 64 20 6d 61 79 20 62 65 20 73 thread may be s
10ff1 71 6c 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e qlite3_free()ed.
10ff2 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74 .**.** nReq is t
10ff3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
10ff4 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71 es of memory req
10ff5 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73 uired. Once this
10ff6 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65 much has.** bee
10ff7 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20 n released, the
10ff8 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 function returns
10ff9 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c . The return val
10ffa 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 ue is the total
10ffb 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79 number .** of by
10ffc 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 tes of memory re
10ffd 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 leased..*/.SQLIT
10ffe 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
10fff 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 lite3PcacheRelea
11000 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65 seMemory(int nRe
11001 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20 q){. int nFree
11002 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68 = 0;. if( pcach
11003 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a e1.pStart==0 ){.
11004 20 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 PgHdr1 *p;.
11005 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d pcache1EnterM
11006 75 74 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c utex();. whil
11007 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46 e( (nReq<0 || nF
11008 72 65 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d ree<nReq) && (p=
11009 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c pcache1.pLruTail
1100a 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 ) ){. nFree
1100b 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f += sqlite3Mallo
1100c 63 53 69 7a 65 28 50 47 48 44 52 31 5f 54 4f 5f cSize(PGHDR1_TO_
1100d 50 41 47 45 28 70 29 29 3b 0a 20 20 20 20 20 20 PAGE(p));.
1100e 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 pcache1PinPage(p
1100f 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 );. pcache1
11010 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 RemoveFromHash(p
11011 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 );. pcache1
11012 46 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20 FreePage(p);.
11013 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 }. pcache1Le
11014 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a aveMutex();. }.
11015 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a return nFree;.
11016 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
11017 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 TE_ENABLE_MEMORY
11018 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a _MANAGEMENT */..
11019 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1101a 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 ST./*.** This fu
1101b 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 nction is used b
1101c 79 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 y test procedure
1101d 73 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 s to inspect the
1101e 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a internal state.
1101f 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c ** of the global
11020 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 cache..*/.SQLIT
11021 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
11022 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74 qlite3PcacheStat
11023 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72 s(. int *pnCurr
11024 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54 ent, /* OUT
11025 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f : Total number o
11026 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 2a f pages cached *
11027 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20 /. int *pnMax,
11028 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a /* OUT:
11029 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20 Global maximum
1102a 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 cache size */.
1102b 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20 int *pnMin,
1102c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d /* OUT: Sum
1102d 20 6f 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e of PCache1.nMin
1102e 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63 for purgeable c
1102f 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a aches */. int *
11030 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20 pnRecyclable
11031 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 /* OUT: Total nu
11032 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 76 mber of pages av
11033 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79 ailable for recy
11034 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67 cling */.){. Pg
11035 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e Hdr1 *p;. int n
11036 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a Recyclable = 0;.
11037 20 20 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e for(p=pcache1.
11038 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70 pLruHead; p; p=p
11039 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20 ->pLruNext){.
1103a 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a nRecyclable++;.
1103b 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74 }. *pnCurrent
1103c 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 = pcache1.nCurr
1103d 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61 entPage;. *pnMa
1103e 78 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 x = pcache1.nMax
1103f 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d Page;. *pnMin =
11040 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 pcache1.nMinPag
11041 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62 e;. *pnRecyclab
11042 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65 le = nRecyclable
11043 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a ;.}.#endif../***
11044 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
11045 6f 66 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a of pcache1.c ***
11046 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11047 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
11049 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
1104a 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20 n file rowset.c
1104b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1104c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1104d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
1104e 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65 72 20 * 2008 December
1104f 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 3.**.** The auth
11050 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
11051 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
11052 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
11053 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
11054 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
11055 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
11056 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
11057 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
11058 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
11059 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1105a 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1105b 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1105c 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1105d 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1105e 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1105f 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
11060 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
11061 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11062 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11065 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75 .**.** This modu
11066 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e le implements an
11067 20 6f 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20 object we call
11068 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a 2a 0a 2a a "RowSet"..**.*
11069 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a * The RowSet obj
1106a 65 63 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74 ect is a collect
1106b 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20 ion of rowids.
1106c 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 20 69 6e Rowids.** are in
1106d 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 serted into the
1106e 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62 RowSet in an arb
1106f 69 74 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49 itrary order. I
11070 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e 20 62 65 nserts.** can be
11071 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68 intermixed with
11072 20 74 65 73 74 73 20 74 6f 20 73 65 65 20 69 66 tests to see if
11073 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68 a given rowid h
11074 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 76 69 as been.** previ
11075 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20 69 ously inserted i
11076 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a nto the RowSet..
11077 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20 **.** After all
11078 69 6e 73 65 72 74 73 20 61 72 65 20 66 69 6e 69 inserts are fini
11079 73 68 65 64 2c 20 69 74 20 69 73 20 70 6f 73 73 shed, it is poss
1107a 69 62 6c 65 20 74 6f 20 65 78 74 72 61 63 74 20 ible to extract
1107b 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 the.** elements
1107c 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e of the RowSet in
1107d 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 sorted order.
1107e 4f 6e 63 65 20 74 68 69 73 20 65 78 74 72 61 63 Once this extrac
1107f 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 20 tion.** process
11080 68 61 73 20 73 74 61 72 74 65 64 2c 20 6e 6f 20 has started, no
11081 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 6d 61 79 new elements may
11082 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a be inserted..**
11083 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 65 20 70 .** Hence, the p
11084 72 69 6d 69 74 69 76 65 20 6f 70 65 72 61 74 69 rimitive operati
11085 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 53 65 74 ons for a RowSet
11086 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43 are:.**.** C
11087 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 4e 53 45 REATE.** INSE
11088 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 0a 2a 2a RT.** TEST.**
11089 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 SMALLEST.**
1108a 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a 0a 2a 2a DESTROY.**.**
1108b 20 54 68 65 20 43 52 45 41 54 45 20 61 6e 64 20 The CREATE and
1108c 44 45 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76 DESTROY primitiv
1108d 65 73 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74 es are the const
1108e 72 75 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72 ructor and destr
1108f 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 6f 75 uctor,.** obviou
11090 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54 sly. The INSERT
11091 20 70 72 69 6d 69 74 69 76 65 20 61 64 64 73 20 primitive adds
11092 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f a new element to
11093 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 the RowSet..**
11094 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f 20 73 TEST checks to s
11095 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e 74 ee if an element
11096 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 is already in t
11097 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d 41 4c he RowSet. SMAL
11098 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 63 74 73 LEST.** extracts
11099 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c 75 65 the least value
1109a 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74 from the RowSet
1109b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e 53 45 ..**.** The INSE
1109c 52 54 20 70 72 69 6d 69 74 69 76 65 20 6d 69 67 RT primitive mig
1109d 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 64 64 69 ht allocate addi
1109e 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e 20 20 tional memory.
1109f 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 6c 6c Memory is.** all
110a0 6f 63 61 74 65 64 20 69 6e 20 63 68 75 6e 6b 73 ocated in chunks
110a1 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 52 54 73 so most INSERTs
110a2 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 69 6f do no allocatio
110a3 6e 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20 n. There is an
110a4 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 6e 64 20 .** upper bound
110a5 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 on the size of a
110a6 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e llocated memory.
110a7 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 20 66 No memory is f
110a8 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 44 45 reed.** until DE
110a9 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 STROY..**.** The
110aa 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 20 TEST primitive
110ab 69 6e 63 6c 75 64 65 73 20 61 20 22 62 61 74 63 includes a "batc
110ac 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 h" number. The
110ad 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 0a 2a TEST primitive.*
110ae 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 65 20 * will only see
110af 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65 elements that we
110b0 72 65 20 69 6e 73 65 72 74 65 64 20 62 65 66 6f re inserted befo
110b1 72 65 20 74 68 65 20 6c 61 73 74 20 63 68 61 6e re the last chan
110b2 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 62 61 74 ge.** in the bat
110b3 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 6e 20 6f ch number. In o
110b4 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 ther words, if a
110b5 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72 73 20 n INSERT occurs
110b6 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 6f 20 54 between.** two T
110b7 45 53 54 73 20 77 68 65 72 65 20 74 68 65 20 54 ESTs where the T
110b8 45 53 54 73 20 68 61 76 65 20 74 68 65 20 73 61 ESTs have the sa
110b9 6d 65 20 62 61 74 63 68 20 6e 75 62 6d 65 72 2c me batch nubmer,
110ba 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c then the.** val
110bb 75 65 20 61 64 64 65 64 20 62 79 20 74 68 65 20 ue added by the
110bc 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20 INSERT will not
110bd 62 65 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68 be visible to th
110be 65 20 73 65 63 6f 6e 64 20 54 45 53 54 2e 0a 2a e second TEST..*
110bf 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61 * The initial ba
110c0 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65 tch number is ze
110c1 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65 ro, so if the ve
110c2 72 79 20 66 69 72 73 74 20 54 45 53 54 20 63 6f ry first TEST co
110c3 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f 6e 2d ntains.** a non-
110c4 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65 zero batch numbe
110c5 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61 r, it will see a
110c6 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73 ll prior INSERTs
110c7 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 45 52 ..**.** No INSER
110c8 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20 61 66 Ts may occurs af
110c9 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e 20 ter a SMALLEST.
110ca 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 77 69 An assertion wi
110cb 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a 20 74 68 ll fail if.** th
110cc 61 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e at is attempted.
110cd 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 .**.** The cost
110ce 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 69 73 20 of an INSERT is
110cf 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 61 6e 74 roughly constant
110d0 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 6e 65 77 . (Sometime new
110d1 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 20 74 memory.** has t
110d2 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f o be allocated o
110d3 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 20 20 54 n an INSERT.) T
110d4 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 he cost of a TES
110d5 54 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20 T with a new.**
110d6 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 batch number is
110d7 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 65 20 4e O(NlogN) where N
110d8 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f is the number o
110d9 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 f elements in th
110da 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 68 65 e RowSet..** The
110db 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20 cost of a TEST
110dc 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 62 using the same b
110dd 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f atch number is O
110de 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 63 6f 73 (logN). The cos
110df 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 t.** of the firs
110e0 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 20 4f 28 t SMALLEST is O(
110e1 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f 6e 64 20 NlogN). Second
110e2 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 53 and subsequent S
110e3 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 6d 69 MALLEST.** primi
110e4 74 69 76 65 73 20 61 72 65 20 63 6f 6e 73 74 61 tives are consta
110e5 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 20 63 6f nt time. The co
110e6 73 74 20 6f 66 20 44 45 53 54 52 4f 59 20 69 73 st of DESTROY is
110e7 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 O(N)..**.** The
110e8 72 65 20 69 73 20 61 6e 20 61 64 64 65 64 20 63 re is an added c
110e9 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 68 65 6e ost of O(N) when
110ea 20 73 77 69 74 63 68 69 6e 67 20 62 65 74 77 65 switching betwe
110eb 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a 2a 20 53 en TEST and.** S
110ec 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 74 69 76 MALLEST primitiv
110ed 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 es..*/.../*.** T
110ee 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61 arget size for a
110ef 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 llocation chunks
110f0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 ..*/.#define ROW
110f1 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 SET_ALLOCATION_S
110f2 49 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 IZE 1024../*.**
110f3 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f The number of ro
110f4 77 73 65 74 20 65 6e 74 72 69 65 73 20 70 65 72 wset entries per
110f5 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e allocation chun
110f6 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f k..*/.#define RO
110f7 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 WSET_ENTRY_PER_C
110f8 48 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 HUNK \.
110f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
110fa 28 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 (ROWSET_ALLOCATI
110fb 4f 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f ON_SIZE-8)/sizeo
110fc 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 f(struct RowSetE
110fd 6e 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 ntry))../*.** Ea
110fe 63 68 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f ch entry in a Ro
110ff 77 53 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 wSet is an insta
11100 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
11101 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a wing object..*/.
11102 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
11103 72 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 ry {
11104 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 . i64 v;
11105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11106 20 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 /* ROWID value
11107 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a for this entry *
11108 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 /. struct RowSe
11109 74 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20 tEntry *pRight;
1110a 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72 /* Right subtr
1110b 65 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69 ee (larger entri
1110c 65 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 es) or list */.
1110d 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e struct RowSetEn
1110e 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f try *pLeft; /
1110f 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 28 * Left subtree (
11110 73 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 smaller entries)
11111 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f */.};../*.** Ro
11112 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 wSetEntry object
11113 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 s are allocated
11114 69 6e 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 in large chunks
11115 28 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 (instances of th
11116 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 e.** following s
11117 74 72 75 63 74 75 72 65 29 20 74 6f 20 72 65 64 tructure) to red
11118 75 63 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 uce memory alloc
11119 61 74 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 ation overhead.
1111a 20 54 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 The.** chunks a
1111b 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e re kept on a lin
1111c 6b 65 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74 ked list so that
1111d 20 74 68 65 79 20 63 61 6e 20 62 65 20 64 65 61 they can be dea
1111e 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e llocated.** when
1111f 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 64 the RowSet is d
11120 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 estroyed..*/.str
11121 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 uct RowSetChunk
11122 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 {. struct RowSe
11123 74 43 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 tChunk *pNextChu
11124 6e 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 nk; /* Ne
11125 78 74 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 xt chunk on list
11126 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a of them all */.
11127 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
11128 6e 74 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 ntry aEntry[ROWS
11129 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 ET_ENTRY_PER_CHU
1112a 4e 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 NK]; /* Allocate
1112b 64 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a d entries */.};.
1112c 0a 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 ./*.** A RowSet
1112d 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f in an instance o
1112e 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
1112f 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a structure..**.**
11130 20 41 20 74 79 70 65 64 65 66 20 6f 66 20 74 68 A typedef of th
11131 69 73 20 73 74 72 75 63 74 75 72 65 20 69 66 20 is structure if
11132 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 found in sqliteI
11133 6e 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 nt.h..*/.struct
11134 52 6f 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63 RowSet {. struc
11135 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 t RowSetChunk *p
11136 43 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 Chunk; /* Lis
11137 74 20 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 t of all chunk a
11138 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 llocations */.
11139 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
1113a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1113b 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 * The database c
1113c 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 onnection */. s
1113d 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
1113e 79 20 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a y *pEntry; /*
1113f 20 4c 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73 List of entries
11140 20 75 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f using pRight */
11141 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
11142 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 Entry *pLast;
11143 20 20 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 /* Last entry
11144 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 on the pEntry li
11145 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 st */. struct R
11146 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65 owSetEntry *pFre
11147 73 68 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 sh; /* Source
11148 20 6f 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 of new entry ob
11149 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 jects */. struc
1114a 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 t RowSetEntry *p
1114b 54 72 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e Tree; /* Bin
1114c 61 72 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72 ary tree of entr
1114d 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 ies */. u16 nFr
1114e 65 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 esh;
1114f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
11150 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 r of objects on
11151 70 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 pFresh */. u8 i
11152 73 53 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 sSorted;
11153 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
11154 75 65 20 69 66 20 70 45 6e 74 72 79 20 69 73 20 ue if pEntry is
11155 73 6f 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 sorted */. u8 i
11156 42 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 Batch;
11157 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 /* Cu
11158 72 72 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74 rrent insert bat
11159 63 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 ch */.};../*.**
1115a 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 Turn bulk memory
1115b 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f into a RowSet o
1115c 62 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 bject. N bytes
1115d 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 of memory.** are
1115e 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 available at pS
1115f 70 61 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f pace. The db po
11160 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 inter is used as
11161 20 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 a memory contex
11162 74 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 t.** for any sub
11163 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 sequent allocati
11164 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f ons that need to
11165 20 6f 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 occur..** Retur
11166 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 n a pointer to t
11167 68 65 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 he new RowSet ob
11168 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d ject..**.** It m
11169 75 73 74 20 62 65 20 74 68 65 20 63 61 73 65 20 ust be the case
1116a 74 68 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 that N is suffic
1116b 69 65 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 ient to make a R
1116c 6f 77 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a owset. If not.*
1116d 2a 20 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 * an assertion f
1116e 61 75 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 ault occurs..**
1116f 0a 2a 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 .** If N is larg
11170 65 72 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 er than the mini
11171 6d 75 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 mum, use the sur
11172 70 6c 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 plus as an initi
11173 61 6c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e al.** allocation
11174 20 6f 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 of entries avai
11175 6c 61 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c lable to be fill
11176 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
11177 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 IVATE RowSet *sq
11178 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 lite3RowSetInit(
11179 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 sqlite3 *db, voi
1117a 64 20 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 d *pSpace, unsig
1117b 6e 65 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f ned int N){. Ro
1117c 77 53 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 wSet *p;. asser
1117d 74 28 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 t( N >= ROUND8(s
1117e 69 7a 65 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20 izeof(*p)) );.
1117f 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 70 2d p = pSpace;. p-
11180 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 >pChunk = 0;. p
11181 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e ->db = db;. p->
11182 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d pEntry = 0;. p-
11183 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d >pLast = 0;. p-
11184 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d >pTree = 0;. p-
11185 3e 70 46 72 65 73 68 20 3d 20 28 73 74 72 75 63 >pFresh = (struc
11186 74 20 52 6f 77 53 65 74 45 6e 74 72 79 2a 29 28 t RowSetEntry*)(
11187 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 ROUND8(sizeof(*p
11188 29 29 20 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a )) + (char*)p);.
11189 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75 p->nFresh = (u
1118a 31 36 29 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28 16)((N - ROUND8(
1118b 73 69 7a 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a sizeof(*p)))/siz
1118c 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 eof(struct RowSe
1118d 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69 tEntry));. p->i
1118e 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 70 sSorted = 1;. p
1118f 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 20 ->iBatch = 0;.
11190 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a return p;.}../*.
11191 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c ** Deallocate al
11192 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 l chunks from a
11193 52 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 72 RowSet. This fr
11194 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 ees all memory t
11195 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 hat.** the RowSe
11196 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 t has allocated
11197 6f 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d over its lifetim
11198 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
11199 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 is.** the destr
1119a 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f uctor for the Ro
1119b 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f wSet..*/.SQLITE_
1119c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1119d 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 ite3RowSetClear(
1119e 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 74 RowSet *p){. st
1119f 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b ruct RowSetChunk
111a0 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74 *pChunk, *pNext
111a1 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68 Chunk;. for(pCh
111a2 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70 unk=p->pChunk; p
111a3 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20 Chunk; pChunk =
111a4 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20 pNextChunk){.
111a5 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 43 pNextChunk = pC
111a6 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b hunk->pNextChunk
111a7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 ;. sqlite3DbF
111a8 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e ree(p->db, pChun
111a9 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 k);. }. p->pCh
111aa 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 unk = 0;. p->nF
111ab 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 resh = 0;. p->p
111ac 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e Entry = 0;. p->
111ad 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e pLast = 0;. p->
111ae 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e pTree = 0;. p->
111af 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a isSorted = 1;.}.
111b0 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 ./*.** Insert a
111b1 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 new value into a
111b2 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 RowSet..**.** T
111b3 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 he mallocFailed
111b4 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61 flag of the data
111b5 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
111b6 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d is set if a.** m
111b7 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
111b8 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 fails..*/.SQLIT
111b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
111ba 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
111bb 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 rt(RowSet *p, i6
111bc 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 4 rowid){. stru
111bd 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
111be 70 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 20 pEntry; /* The
111bf 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 new entry */. s
111c0 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
111c1 79 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54 y *pLast; /* T
111c2 68 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 6e he last prior en
111c3 74 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 try */. assert(
111c4 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 p!=0 );. if( p
111c5 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20 ->nFresh==0 ){.
111c6 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
111c7 43 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20 Chunk *pNew;.
111c8 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 pNew = sqlite3D
111c9 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 bMallocRaw(p->db
111ca 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 , sizeof(*pNew))
111cb 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d ;. if( pNew==
111cc 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 0 ){. retur
111cd 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 n;. }. pNe
111ce 77 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 w->pNextChunk =
111cf 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 p->pChunk;. p
111d0 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b ->pChunk = pNew;
111d1 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d . p->pFresh =
111d2 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 pNew->aEntry;.
111d3 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 p->nFresh = R
111d4 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f OWSET_ENTRY_PER_
111d5 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e CHUNK;. }. pEn
111d6 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b try = p->pFresh+
111d7 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d +;. p->nFresh--
111d8 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 ;. pEntry->v =
111d9 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d rowid;. pEntry-
111da 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 >pRight = 0;. p
111db 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b Last = p->pLast;
111dc 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a . if( pLast ){.
111dd 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 if( p->isSor
111de 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c ted && rowid<=pL
111df 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 ast->v ){.
111e0 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b p->isSorted = 0;
111e1 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 . }. pLast
111e2 2d 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72 ->pRight = pEntr
111e3 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 y;. }else{.
111e4 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 assert( p->pEntr
111e5 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 y==0 ); /* Fires
111e6 20 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72 if INSERT after
111e7 20 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20 SMALLEST */.
111e8 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e p->pEntry = pEn
111e9 74 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c try;. }. p->pL
111ea 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a ast = pEntry;.}.
111eb 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f ./*.** Merge two
111ec 20 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 74 lists of RowSet
111ed 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 Entry objects.
111ee 52 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 65 Remove duplicate
111ef 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 s..**.** The inp
111f0 75 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f 6e ut lists are con
111f1 6e 65 63 74 65 64 20 76 69 61 20 70 52 69 67 68 nected via pRigh
111f2 74 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 t pointers and a
111f3 72 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 re .** assumed t
111f4 6f 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 62 o each already b
111f5 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 e in sorted orde
111f6 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 r..*/.static str
111f7 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
111f8 2a 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 20 *rowSetMerge(.
111f9 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 struct RowSetEnt
111fa 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 ry *pA, /* Fi
111fb 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20 rst sorted list
111fc 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a to be merged */.
111fd 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 struct RowSetE
111fe 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 ntry *pB /*
111ff 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 Second sorted li
11200 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 st to be merged
11201 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 */.){. struct R
11202 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b owSetEntry head;
11203 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 . struct RowSet
11204 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 Entry *pTail;..
11205 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a pTail = &head;.
11206 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 while( pA && p
11207 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 B ){. assert(
11208 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c pA->pRight==0 |
11209 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 | pA->v<=pA->pRi
1120a 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 ght->v );. as
1120b 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 74 sert( pB->pRight
1120c 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 ==0 || pB->v<=pB
1120d 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 ->pRight->v );.
1120e 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d if( pA->v<pB-
1120f 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 >v ){. pTai
11210 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a l->pRight = pA;.
11211 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 pA = pA->p
11212 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61 Right;. pTa
11213 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 il = pTail->pRig
11214 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 ht;. }else if
11215 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b ( pB->v<pA->v ){
11216 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 . pTail->pR
11217 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 20 ight = pB;.
11218 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 74 pB = pB->pRight
11219 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 ;. pTail =
1121a 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 pTail->pRight;.
1121b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1121c 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b pA = pA->pRight;
1121d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1121e 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 72 pA ){. asser
1121f 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 t( pA->pRight==0
11220 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 || pA->v<=pA->p
11221 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 Right->v );.
11222 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 pTail->pRight =
11223 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 pA;. }else{.
11224 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c assert( pB==0 |
11225 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 | pB->pRight==0
11226 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 || pB->v<=pB->pR
11227 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 ight->v );. p
11228 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 Tail->pRight = p
11229 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 B;. }. return
1122a 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a head.pRight;.}..
1122b 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 /*.** Sort all e
1122c 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 lements on the p
1122d 45 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 68 Entry list of th
1122e 65 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 e RowSet into as
1122f 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a cending order..*
11230 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 / .static void r
11231 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74 owSetSort(RowSet
11232 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 *p){. unsigned
11233 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 int i;. struct
11234 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 RowSetEntry *pE
11235 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 ntry;. struct R
11236 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63 owSetEntry *aBuc
11237 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 ket[40];.. asse
11238 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d rt( p->isSorted=
11239 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 =0 );. memset(a
1123a 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f Bucket, 0, sizeo
1123b 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 f(aBucket));. w
1123c 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 hile( p->pEntry
1123d 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 ){. pEntry =
1123e 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 p->pEntry;. p
1123f 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 ->pEntry = pEntr
11240 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 y->pRight;. p
11241 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 Entry->pRight =
11242 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 0;. for(i=0;
11243 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 aBucket[i]; i++)
11244 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d {. pEntry =
11245 20 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 75 rowSetMerge(aBu
11246 63 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29 cket[i], pEntry)
11247 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b ;. aBucket[
11248 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 i] = 0;. }.
11249 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 aBucket[i] = p
1124a 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e Entry;. }. pEn
1124b 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 try = 0;. for(i
1124c 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 =0; i<sizeof(aBu
1124d 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 cket)/sizeof(aBu
1124e 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a cket[0]); i++){.
1124f 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 pEntry = row
11250 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 2c SetMerge(pEntry,
11251 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 aBucket[i]);.
11252 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 }. p->pEntry =
11253 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 pEntry;. p->pLa
11254 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 st = 0;. p->isS
11255 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f orted = 1;.}.../
11256 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c 20 *.** The input,
11257 70 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 79 pIn, is a binary
11258 20 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 65 tree (or subtre
11259 65 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 e) of RowSetEntr
1125a 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f y objects..** Co
1125b 6e 76 65 72 74 20 74 68 69 73 20 74 72 65 65 20 nvert this tree
1125c 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 into a linked li
1125d 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 st connected by
1125e 74 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e 74 the pRight point
1125f 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 ers.** and retur
11260 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 n pointers to th
11261 65 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74 e first and last
11262 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
11263 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 new list..*/.st
11264 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 atic void rowSet
11265 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 74 TreeToList(. st
11266 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
11267 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f *pIn, /
11268 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e * Root of the in
11269 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 put tree */. st
1126a 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
1126b 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 2f **ppFirst, /
1126c 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 20 * Write head of
1126d 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 the output list
1126e 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 here */. struct
1126f 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 RowSetEntry **p
11270 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 72 pLast /* Wr
11271 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 ite tail of the
11272 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 output list here
11273 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 */.){. assert(
11274 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28 pIn!=0 );. if(
11275 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 pIn->pLeft ){.
11276 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 struct RowSet
11277 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f Entry *p;. ro
11278 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 wSetTreeToList(p
11279 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 72 In->pLeft, ppFir
1127a 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e st, &p);. p->
1127b 70 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20 pRight = pIn;.
1127c 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 69 }else{. *ppFi
1127d 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 rst = pIn;. }.
1127e 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 74 if( pIn->pRight
1127f 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 72 ){. rowSetTr
11280 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 eeToList(pIn->pR
11281 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 ight, &pIn->pRig
11282 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d ht, ppLast);. }
11283 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73 else{. *ppLas
11284 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61 t = pIn;. }. a
11285 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 29 ssert( (*ppLast)
11286 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d ->pRight==0 );.}
11287 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 .../*.** Convert
11288 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f a sorted list o
11289 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e f elements (conn
1128a 65 63 74 65 64 20 62 79 20 70 52 69 67 68 74 29 ected by pRight)
1128b 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a into a binary.*
1128c 2a 20 74 72 65 65 20 77 69 74 68 20 64 65 70 74 * tree with dept
1128d 68 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 20 h of iDepth. A
1128e 64 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e 73 depth of 1 means
1128f 20 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 69 the tree contai
11290 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e ns a single.** n
11291 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 ode taken from t
11292 68 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c 69 he head of *ppLi
11293 73 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20 st. A depth of
11294 32 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 77 2 means a tree w
11295 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64 ith.** three nod
11296 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 es. And so fort
11297 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20 h..**.** Use as
11298 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 6f many entries fro
11299 6d 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74 m the input list
1129a 20 61 73 20 72 65 71 75 69 72 65 64 20 61 6e 64 as required and
1129b 20 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a update the.** *
1129c 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 ppList to point
1129d 74 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 6c to the unused el
1129e 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 ements of the li
1129f 73 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 st. If the inpu
112a0 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 t.** list contai
112a1 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 ns too few eleme
112a2 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 nts, then constr
112a3 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 uct an incomplet
112a4 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65 e tree.** and le
112a5 61 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20 ave *ppList set
112a6 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 to NULL..**.** R
112a7 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
112a8 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 to the root of t
112a9 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 he constructed b
112aa 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73 inary tree..*/.s
112ab 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f 77 tatic struct Row
112ac 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 SetEntry *rowSet
112ad 4e 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 72 NDeepTree(. str
112ae 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 uct RowSetEntry
112af 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20 **ppList,. int
112b0 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 75 iDepth.){. stru
112b1 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a ct RowSetEntry *
112b2 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f p; /* Ro
112b3 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 72 ot of the new tr
112b4 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ee */. struct R
112b5 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 owSetEntry *pLef
112b6 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 t; /* Left s
112b7 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 20 ubtree */. if(
112b8 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 *ppList==0 ){.
112b9 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
112ba 20 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 20 if( iDepth==1
112bb 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c 69 ){. p = *ppLi
112bc 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 st;. *ppList
112bd 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 = p->pRight;.
112be 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 p->pLeft = p->p
112bf 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 72 Right = 0;. r
112c0 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 eturn p;. }. p
112c1 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 Left = rowSetNDe
112c2 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 epTree(ppList, i
112c3 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20 Depth-1);. p =
112c4 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 *ppList;. if( p
112c5 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ==0 ){. retur
112c6 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70 n pLeft;. }. p
112c7 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b ->pLeft = pLeft;
112c8 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e . *ppList = p->
112c9 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69 pRight;. p->pRi
112ca 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 ght = rowSetNDee
112cb 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 pTree(ppList, iD
112cc 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 72 epth-1);. retur
112cd 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f n p;.}../*.** Co
112ce 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c nvert a sorted l
112cf 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ist of elements
112d0 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 72 into a binary tr
112d1 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 65 ee. Make the tre
112d2 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20 e.** as deep as
112d3 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 it needs to be i
112d4 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 n order to conta
112d5 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 in the entire li
112d6 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 st..*/.static st
112d7 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 ruct RowSetEntry
112d8 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 *rowSetListToTr
112d9 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 ee(struct RowSet
112da 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20 Entry *pList){.
112db 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 int iDepth;
112dc 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 /* Depth
112dd 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 66 of the tree so f
112de 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 ar */. struct R
112df 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 owSetEntry *p;
112e0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
112e1 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73 tree root */. s
112e2 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 truct RowSetEntr
112e3 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c y *pLeft; /* L
112e4 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a eft subtree */..
112e5 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 assert( pList!
112e6 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73 =0 );. p = pLis
112e7 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e t;. pList = p->
112e8 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65 pRight;. p->pLe
112e9 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d ft = p->pRight =
112ea 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 68 0;. for(iDepth
112eb 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 74 =1; pList; iDept
112ec 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 20 h++){. pLeft
112ed 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69 = p;. p = pLi
112ee 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 st;. pList =
112ef 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 p->pRight;. p
112f0 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b ->pLeft = pLeft;
112f1 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d . p->pRight =
112f2 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 rowSetNDeepTree
112f3 28 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29 (&pList, iDepth)
112f4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 ;. }. return p
112f5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
112f6 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70 rt the list in p
112f7 2d 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 20 ->pEntry into a
112f8 73 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 69 sorted list if i
112f9 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74 t is not.** sort
112fa 65 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 20 ed already. If
112fb 74 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 72 there is a binar
112fc 79 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72 y tree on p->pTr
112fd 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 ee, then.** conv
112fe 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c 69 ert it into a li
112ff 73 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65 st too and merge
11300 20 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e it into the p->
11301 70 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a pEntry list..*/.
11302 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 static void rowS
11303 65 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 etToList(RowSet
11304 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69 *p){. if( !p->i
11305 73 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 72 sSorted ){. r
11306 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 owSetSort(p);.
11307 7d 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 65 }. if( p->pTree
11308 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 ){. struct R
11309 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 61 owSetEntry *pHea
1130a 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72 d, *pTail;. r
1130b 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 owSetTreeToList(
1130c 70 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64 p->pTree, &pHead
1130d 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 70 , &pTail);. p
1130e 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 20 ->pTree = 0;.
1130f 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 p->pEntry = row
11310 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 SetMerge(p->pEnt
11311 72 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a ry, pHead);. }.
11312 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 }../*.** Extract
11313 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c the smallest el
11314 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 ement from the R
11315 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 owSet..** Write
11316 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f the element into
11317 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 *pRowid. Retur
11318 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 n 1 on success.
11319 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 Return.** 0 if
1131a 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c the RowSet is al
1131b 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a ready empty..**.
1131c 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f ** After this ro
1131d 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 utine has been c
1131e 61 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74 alled, the sqlit
1131f 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29 e3RowSetInsert()
11320 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 .** routine may
11321 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 not be called ag
11322 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 ain. .*/.SQLITE
11323 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
11324 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 ite3RowSetNext(R
11325 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 owSet *p, i64 *p
11326 52 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 74 Rowid){. rowSet
11327 54 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 28 ToList(p);. if(
11328 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 p->pEntry ){.
11329 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 *pRowid = p->p
1132a 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d Entry->v;. p-
1132b 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e >pEntry = p->pEn
1132c 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 try->pRight;.
1132d 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d if( p->pEntry==
1132e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1132f 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 29 e3RowSetClear(p)
11330 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
11331 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 rn 1;. }else{.
11332 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
11333 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 .}../*.** Check
11334 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 6e to see if elemen
11335 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e 73 t iRowid was ins
11336 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 erted into the t
11337 68 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 he rowset as.**
11338 70 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 part of any inse
11339 72 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 74 rt batch prior t
1133a 6f 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 72 o iBatch. Retur
1133b 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c n 1 or 0..*/.SQL
1133c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1133d 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 sqlite3RowSetTes
1133e 74 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 t(RowSet *pRowSe
1133f 74 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 71 t, u8 iBatch, sq
11340 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 lite3_int64 iRow
11341 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f id){. struct Ro
11342 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 wSetEntry *p;.
11343 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77 if( iBatch!=pRow
11344 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20 Set->iBatch ){.
11345 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d 3e if( pRowSet->
11346 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 20 pEntry ){.
11347 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 6f rowSetToList(pRo
11348 77 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 6f wSet);. pRo
11349 77 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f wSet->pTree = ro
1134a 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 70 wSetListToTree(p
1134b 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b RowSet->pEntry);
1134c 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e . pRowSet->
1134d 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 pEntry = 0;.
1134e 20 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 pRowSet->pLast
1134f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
11350 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20 pRowSet->iBatch
11351 3d 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20 = iBatch;. }.
11352 70 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 p = pRowSet->pTr
11353 65 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 ee;. while( p )
11354 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c 69 {. if( p->v<i
11355 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70 Rowid ){. p
11356 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 = p->pRight;.
11357 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 }else if( p->v
11358 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 >iRowid ){.
11359 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 p = p->pLeft;.
1135a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1135b 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
1135c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
1135d 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a }../************
1135e 2a 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 74 ** End of rowset
1135f 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
11360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11361 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11362 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */./************
11363 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 ** Begin file pa
11364 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ger.c **********
11365 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11366 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
11367 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 */./*.** 2001 Se
11368 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a ptember 15.**.**
11369 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
1136a 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
1136b 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
1136c 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
1136d 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
1136e 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
1136f 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
11370 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
11371 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
11372 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
11373 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
11374 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
11375 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
11376 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
11377 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
11378 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
11379 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
1137a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1137b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1137c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1137d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1137e 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
1137f 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 s is the impleme
11380 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 ntation of the p
11381 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 age cache subsys
11382 74 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a tem or "pager"..
11383 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 ** .** The pager
11384 20 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 is used to acce
11385 73 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69 ss a database di
11386 73 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 sk file. It imp
11387 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 lements.** atomi
11388 63 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c c commit and rol
11389 6c 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 lback through th
1138a 65 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e e use of a journ
1138b 61 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 al file that.**
1138c 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d is separate from
1138d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1138e 6c 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 le. The pager a
1138f 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 lso implements f
11390 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 ile.** locking t
11391 6f 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 o prevent two pr
11392 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 ocesses from wri
11393 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 ting the same da
11394 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 tabase.** file s
11395 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f imultaneously, o
11396 72 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 r one process fr
11397 6f 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 om reading the d
11398 61 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a atabase while.**
11399 20 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 another is writ
1139a 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 ing..*/.#ifndef
1139b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
1139c 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 IO../*.** Macros
1139d 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f for troubleshoo
1139e 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 ting. Normally
1139f 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 turned off.*/.#i
113a0 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 f 0.int sqlite3P
113a1 61 67 65 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a agerTrace=1; /*
113a2 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 True to enable
113a3 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64 65 66 69 tracing */.#defi
113a4 6e 65 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 ne sqlite3DebugP
113a5 72 69 6e 74 66 20 70 72 69 6e 74 66 0a 23 64 65 rintf printf.#de
113a6 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 28 fine PAGERTRACE(
113a7 58 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 X) if( sqlit
113a8 65 33 50 61 67 65 72 54 72 61 63 65 20 29 7b 20 e3PagerTrace ){
113a9 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
113aa 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65 0a 23 64 tf X; }.#else.#d
113ab 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 efine PAGERTRACE
113ac 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (X).#endif../*.*
113ad 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
113ae 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75 two macros are u
113af 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50 sed within the P
113b0 41 47 45 52 54 52 41 43 45 28 29 20 6d 61 63 72 AGERTRACE() macr
113b1 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70 os above.** to p
113b2 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64 65 rint out file-de
113b3 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a scriptors. .**.*
113b4 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b 65 * PAGERID() take
113b5 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 s a pointer to a
113b6 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61 73 Pager struct as
113b7 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 20 54 its argument. T
113b8 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 he.** associated
113b9 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 file-descriptor
113ba 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46 49 is returned. FI
113bb 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61 6b LEHANDLEID() tak
113bc 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 69 es an sqlite3_fi
113bd 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 73 20 le.** struct as
113be 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f its argument..*/
113bf 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 49 44 .#define PAGERID
113c0 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e 66 64 (p) ((int)(p->fd
113c1 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 48 )).#define FILEH
113c2 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69 6e ANDLEID(fd) ((in
113c3 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 t)fd)../*.** The
113c4 20 70 61 67 65 20 63 61 63 68 65 20 61 73 20 61 page cache as a
113c5 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 79 73 whole is always
113c6 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 in one of the f
113c7 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 74 ollowing.** stat
113c8 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 es:.**.** PAGE
113c9 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 R_UNLOCK
113ca 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 The page cache i
113cb 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 s not currently
113cc 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 20 reading or .**
113cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113ce 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 68 65 writing the
113cf 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
113d0 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 There is no.**
113d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113d2 20 20 20 20 20 20 64 61 74 61 20 68 65 6c 64 20 data held
113d3 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 in memory. This
113d4 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c 0a is the initial.
113d5 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
113d6 20 20 20 20 20 20 20 20 20 73 74 61 74 65 2e 0a state..
113d7 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 48 **.** PAGER_SH
113d8 41 52 45 44 20 20 20 20 20 20 20 20 54 68 65 20 ARED The
113d9 70 61 67 65 20 63 61 63 68 65 20 69 73 20 72 65 page cache is re
113da 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 ading the databa
113db 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 se..**
113dc 20 20 20 20 20 20 20 20 20 20 20 20 20 57 72 69 Wri
113dd 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 72 6d ting is not perm
113de 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 63 61 itted. There ca
113df 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 n be.**
113e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 75 mu
113e1 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 ltiple readers a
113e2 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d ccessing the sam
113e3 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 e database.**
113e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113e5 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 65 20 file at the
113e6 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a same time..**.**
113e7 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 PAGER_RESERVE
113e8 44 20 20 20 20 20 20 54 68 69 73 20 70 72 6f 63 D This proc
113e9 65 73 73 20 68 61 73 20 72 65 73 65 72 76 65 64 ess has reserved
113ea 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f the database fo
113eb 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 20 20 r writing.**
113ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113ed 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 but has not y
113ee 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 61 6e et made any chan
113ef 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 70 ges. Only one p
113f0 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 rocess.**
113f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113f2 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 72 65 at a time can re
113f3 73 65 72 76 65 20 74 68 65 20 64 61 74 61 62 61 serve the databa
113f4 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 se. The origina
113f5 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 l.**
113f6 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 datab
113f7 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 ase file has not
113f8 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 been modified s
113f9 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 o other.**
113fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113fb 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 73 processes may s
113fc 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20 till be reading
113fd 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20 the on-disk.**
113fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
113ff 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69 database fi
11400 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 le..**.** PAGE
11401 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 R_EXCLUSIVE
11402 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69 The page cache i
11403 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 s writing the da
11404 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 tabase..**
11405 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11406 20 41 63 63 65 73 73 20 69 73 20 65 78 63 6c 75 Access is exclu
11407 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 72 20 sive. No other
11408 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a 2a 20 processes or.**
11409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1140a 20 20 20 20 20 20 74 68 72 65 61 64 73 20 63 61 threads ca
1140b 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f 72 20 n be reading or
1140c 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 6f 6e writing while on
1140d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
1140e 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65 proce
1140f 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a ss is writing..*
11410 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 59 4e *.** PAGER_SYN
11411 43 45 44 20 20 20 20 20 20 20 20 54 68 65 20 70 CED The p
11412 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74 68 ager moves to th
11413 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 50 41 is state from PA
11414 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a GER_EXCLUSIVE.**
11415 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11416 20 20 20 20 20 20 20 61 66 74 65 72 20 61 6c 6c after all
11417 20 64 69 72 74 79 20 70 61 67 65 73 20 68 61 76 dirty pages hav
11418 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 e been written t
11419 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 o the.**
1141a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
1141b 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1141c 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 62 65 the file has be
1141d 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a 2a 20 en synced to.**
1141e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1141f 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c 6c 20 disk. All
11420 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 that remains to
11421 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 65 20 do is to remove
11422 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 or.**
11423 20 20 20 20 20 20 20 20 20 20 20 20 74 72 75 6e trun
11424 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c cate the journal
11425 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 74 72 file and the tr
11426 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 ansaction .**
11427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11428 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d will be comm
11429 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 itted..**.** The
1142a 20 70 61 67 65 20 63 61 63 68 65 20 63 6f 6d 65 page cache come
1142b 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f 55 4e s up in PAGER_UN
1142c 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74 LOCK. The first
1142d 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 74 time a.** sqlit
1142e 65 33 50 61 67 65 72 47 65 74 28 29 20 6f 63 63 e3PagerGet() occ
1142f 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 20 74 urs, the state t
11430 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 ransitions to PA
11431 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 41 GER_SHARED..** A
11432 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 20 68 fter all pages h
11433 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 73 65 ave been release
11434 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 5f 70 d using sqlite_p
11435 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a 20 age_unref(),.**
11436 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 the state transi
11437 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 50 41 tions back to PA
11438 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 GER_UNLOCK. The
11439 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 first time.** t
1143a 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 hat sqlite3Pager
1143b 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 Write() is calle
1143c 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 61 d, the state tra
1143d 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50 nsitions to.** P
1143e 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 20 AGER_RESERVED.
1143f 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c 69 74 (Note that sqlit
11440 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 63 e3PagerWrite() c
11441 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 61 an only be.** ca
11442 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 73 74 lled on an outst
11443 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 69 63 anding page whic
11444 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 h means that the
11445 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a 20 62 pager must.** b
11446 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 e in PAGER_SHARE
11447 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 61 6e D before it tran
11448 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 sitions to PAGER
11449 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a 20 50 _RESERVED.).** P
1144a 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 6d 65 AGER_RESERVED me
1144b 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 69 ans that there i
1144c 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 61 s an open rollba
1144d 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 ck journal..** T
1144e 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f he transition to
1144f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
11450 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 61 occurs before a
11451 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 ny changes.** ar
11452 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64 61 e made to the da
11453 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 6f tabase file, tho
11454 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 74 68 ugh writes to th
11455 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f e rollback.** jo
11456 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 69 74 urnal occurs wit
11457 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 45 53 h just PAGER_RES
11458 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 61 6e ERVED. After an
11459 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c sqlite3PagerRol
1145a 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 71 lback().** or sq
1145b 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 lite3PagerCommit
1145c 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 65 20 PhaseTwo(), the
1145d 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 61 63 state can go bac
1145e 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 k to PAGER_SHARE
1145f 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 6e 20 D,.** or it can
11460 73 74 61 79 20 61 74 20 50 41 47 45 52 5f 45 58 stay at PAGER_EX
11461 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 61 72 CLUSIVE if we ar
11462 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61 e in exclusive a
11463 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 ccess mode..*/.#
11464 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 4e 4c define PAGER_UNL
11465 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 66 69 OCK 0.#defi
11466 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 ne PAGER_SHARED
11467 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 6d 65 1 /* same
11468 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 as SHARED_LOCK
11469 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 */.#define PAGER
1146a 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 20 20 _RESERVED 2
1146b 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 53 45 /* same as RESE
1146c 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 RVED_LOCK */.#de
1146d 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 4c 55 fine PAGER_EXCLU
1146e 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 73 61 SIVE 4 /* sa
1146f 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 45 5f me as EXCLUSIVE_
11470 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 LOCK */.#define
11471 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20 PAGER_SYNCED
11472 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 5../*.** A mac
11473 72 6f 20 75 73 65 64 20 66 6f 72 20 69 6e 76 6f ro used for invo
11474 6b 69 6e 67 20 74 68 65 20 63 6f 64 65 63 20 69 king the codec i
11475 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a f there is one.*
11476 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
11477 48 41 53 5f 43 4f 44 45 43 0a 23 20 64 65 66 69 HAS_CODEC.# defi
11478 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c ne CODEC1(P,D,N,
11479 58 2c 45 29 20 5c 0a 20 20 20 20 69 66 28 20 50 X,E) \. if( P
1147a 2d 3e 78 43 6f 64 65 63 20 26 26 20 50 2d 3e 78 ->xCodec && P->x
1147b 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c Codec(P->pCodec,
1147c 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b 20 45 3b 20 D,N,X)==0 ){ E;
1147d 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 }.# define CODEC
1147e 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 5c 2(P,D,N,X,E,O) \
1147f 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 . if( P->xCod
11480 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 63 68 61 72 ec==0 ){ O=(char
11481 2a 29 44 3b 20 7d 65 6c 73 65 20 5c 0a 20 20 20 *)D; }else \.
11482 20 69 66 28 20 28 4f 3d 28 63 68 61 72 2a 29 28 if( (O=(char*)(
11483 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f P->xCodec(P->pCo
11484 64 65 63 2c 44 2c 4e 2c 58 29 29 29 3d 3d 30 20 dec,D,N,X)))==0
11485 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 ){ E; }.#else.#
11486 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c define CODEC1(P,
11487 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f D,N,X,E) /* NO
11488 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 -OP */.# define
11489 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 CODEC2(P,D,N,X,E
1148a 2c 4f 29 20 4f 3d 28 63 68 61 72 2a 29 44 0a 23 ,O) O=(char*)D.#
1148b 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 endif../*.** The
1148c 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 maximum allowed
1148d 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 36 34 sector size. 64
1148e 4b 69 42 2e 20 49 66 20 74 68 65 20 78 53 65 63 KiB. If the xSec
1148f 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 torsize() method
11490 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76 .** returns a v
11491 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 6e alue larger than
11492 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f this, then MAX_
11493 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 75 SECTOR_SIZE is u
11494 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 sed instead..**
11495 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65 This could conce
11496 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f 72 ivably cause cor
11497 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e ruption followin
11498 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 g a power failur
11499 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73 e on.** such a s
1149a 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 63 ystem. This is c
1149b 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f urrently an undo
1149c 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a cumented limit..
1149d 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 53 */.#define MAX_S
1149e 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 31 30 30 ECTOR_SIZE 0x100
1149f 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 00../*.** An ins
114a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
114a1 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
114a2 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f is allocated fo
114a3 72 20 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a r each active.**
114a4 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 savepoint and s
114a5 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
114a6 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74 tion in the syst
114a7 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72 em. All such str
114a8 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 uctures.** are s
114a9 74 6f 72 65 64 20 69 6e 20 74 68 65 20 50 61 67 tored in the Pag
114aa 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 er.aSavepoint[]
114ab 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 array, which is
114ac 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a allocated and.**
114ad 20 72 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73 resized using s
114ae 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e qlite3Realloc().
114af 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 .**.** When a sa
114b0 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74 vepoint is creat
114b1 65 64 2c 20 74 68 65 20 50 61 67 65 72 53 61 76 ed, the PagerSav
114b2 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 epoint.iHdrOffse
114b3 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 t field is.** se
114b4 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 t to 0. If a jou
114b5 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77 rnal-header is w
114b6 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
114b7 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 main journal whi
114b8 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f le.** the savepo
114b9 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c 20 74 int is active, t
114ba 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69 hen iHdrOffset i
114bb 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 s set to the byt
114bc 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d e offset .** imm
114bd 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 ediately followi
114be 6e 67 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 ng the last jour
114bf 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74 nal record writt
114c0 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e en into the main
114c1 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f .** journal befo
114c2 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 re the journal-h
114c3 65 61 64 65 72 2e 20 54 68 69 73 20 69 73 20 72 eader. This is r
114c4 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 20 73 equired during s
114c5 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c avepoint.** roll
114c6 62 61 63 6b 20 28 73 65 65 20 70 61 67 65 72 50 back (see pagerP
114c7 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 laybackSavepoint
114c8 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 ())..*/.typedef
114c9 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65 struct PagerSave
114ca 70 6f 69 6e 74 20 50 61 67 65 72 53 61 76 65 70 point PagerSavep
114cb 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67 oint;.struct Pag
114cc 65 72 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 erSavepoint {.
114cd 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 i64 iOffset;
114ce 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
114cf 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 Starting offset
114d0 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 in main journal
114d1 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66 */. i64 iHdrOff
114d2 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 set;
114d3 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a /* See above *
114d4 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53 /. Bitvec *pInS
114d5 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 avepoint;
114d6 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73 /* Set of pages
114d7 20 69 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69 in this savepoi
114d8 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 nt */. Pgno nOr
114d9 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ig;
114da 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c /* Original
114db 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
114dc 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 in file */. Pg
114dd 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 20 20 20 no iSubRec;
114de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
114df 64 65 78 20 6f 66 20 66 69 72 73 74 20 72 65 63 dex of first rec
114e0 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e ord in sub-journ
114e1 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 al */.};../*.**
114e2 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68 A open page cach
114e3 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 e is an instance
114e4 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e of the followin
114e5 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a g structure..**.
114e6 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a ** errCode.**.**
114e7 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 Pager.errCode
114e8 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 53 may be set to S
114e9 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c QLITE_IOERR, SQL
114ea 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a ITE_CORRUPT, or.
114eb 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 ** or SQLITE_F
114ec 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 ULL. Once one of
114ed 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65 the first three
114ee 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 errors occurs,
114ef 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 it persists.**
114f0 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 and is returned
114f1 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f as the result o
114f2 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 f every major pa
114f3 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 ger API call. T
114f4 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 he.** SQLITE_F
114f5 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ULL return code
114f6 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 is slightly diff
114f7 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 69 73 erent. It persis
114f8 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 ts only until th
114f9 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75 63 63 e.** next succ
114fa 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 essful rollback
114fb 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 is performed on
114fc 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e the pager cache.
114fd 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 Also,.** SQLI
114fe 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 TE_FULL does not
114ff 20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c 69 affect the sqli
11500 74 65 33 50 61 67 65 72 47 65 74 28 29 20 61 6e te3PagerGet() an
11501 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f d sqlite3PagerLo
11502 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 okup().** APIs
11503 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c , they may still
11504 20 62 65 20 75 73 65 64 20 73 75 63 63 65 73 73 be used success
11505 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 fully..**.** dbS
11506 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 izeValid, dbSize
11507 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 , dbOrigSize, db
11508 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 FileSize.**.**
11509 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69 Managing the si
1150a 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ze of the databa
1150b 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 se file in pages
1150c 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d is a little com
1150d 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 plicated..** T
1150e 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 he variable Page
1150f 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e r.dbSize contain
11510 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
11511 70 61 67 65 73 20 74 68 61 74 20 74 68 65 20 64 pages that the d
11512 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 atabase.** ima
11513 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e ge currently con
11514 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20 64 61 tains. As the da
11515 74 61 62 61 73 65 20 69 6d 61 67 65 20 67 72 6f tabase image gro
11516 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 ws or shrinks th
11517 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 is.** variable
11518 20 69 73 20 75 70 64 61 74 65 64 2e 20 54 68 65 is updated. The
11519 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e variable Pager.
1151a 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 dbFileSize conta
1151b 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a ins the number.*
1151c 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69 6e 20 * of pages in
1151d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1151e 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65 20 64 e. This may be d
1151f 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 ifferent from Pa
11520 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 ger.dbSize.**
11521 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20 68 61 if some pages ha
11522 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64 ve been appended
11523 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 to the database
11524 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74 20 79 image but not y
11525 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 et written.**
11526 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 61 63 out from the cac
11527 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c he to the actual
11528 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f file on disk. O
11529 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 20 68 r if the image h
1152a 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 as been.** tru
1152b 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63 ncated by an inc
1152c 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 remental-vacuum
1152d 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 operation. The P
1152e 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 ager.dbOrigSize
1152f 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f variable.** co
11530 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 ntains the numbe
11531 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
11532 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 e database image
11533 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e when the curren
11534 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69 t.** transacti
11535 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 on was opened. T
11536 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 he contents of a
11537 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68 65 73 ll three of thes
11538 65 20 76 61 72 69 61 62 6c 65 73 20 69 73 0a 2a e variables is.*
11539 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 * only guarant
1153a 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72 65 63 eed to be correc
1153b 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e t if the boolean
1153c 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56 61 6c Pager.dbSizeVal
1153d 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a id is true..**.*
1153e 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 * TODO: Under
1153f 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 what conditions
11540 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64 20 73 is dbSizeValid s
11541 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a et? Cleared?.**.
11542 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f ** changeCountDo
11543 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 ne.**.** This
11544 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 boolean variable
11545 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 is used to make
11546 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63 sure that the c
11547 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a hange-counter .*
11548 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74 65 20 * (the 4-byte
11549 68 65 61 64 65 72 20 66 69 65 6c 64 20 61 74 20 header field at
1154a 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f byte offset 24 o
1154b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 f the database f
1154c 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f ile) is .** no
1154d 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f t updated more o
1154e 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 ften than necess
1154f 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 ary. .**.** It
11550 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 is set to true
11551 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d when the change-
11552 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 69 73 counter field is
11553 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 68 20 updated, which
11554 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 .** can only h
11555 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78 63 6c appen if an excl
11556 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 usive lock is he
11557 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ld on the databa
11558 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 se file..** It
11559 20 69 73 20 63 6c 65 61 72 65 64 20 28 73 65 74 is cleared (set
1155a 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65 6e 65 to false) whene
1155b 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65 ver an exclusive
1155c 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 lock is .** r
1155d 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 elinquished on t
1155e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1155f 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 74 72 . Each time a tr
11560 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
11561 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 mitted,.** The
11562 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
11563 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65 63 74 flag is inspect
11564 65 64 2e 20 49 66 20 69 74 20 69 73 20 74 72 75 ed. If it is tru
11565 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a e, the work of.*
11566 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74 68 65 * updating the
11567 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 change-counter
11568 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 is omitted for t
11569 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
1156a 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 action..**.**
1156b 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d This mechanism m
1156c 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20 72 eans that when r
1156d 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
1156e 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e ive mode, a conn
1156f 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 ection .** nee
11570 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20 74 68 d only update th
11571 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
11572 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65 20 66 once, for the f
11573 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e irst transaction
11574 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e .** committed.
11575 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 .**.** dbModifie
11576 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 d.**.** The db
11577 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 Modified flag is
11578 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 61 20 set whenever a
11579 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 database page is
1157a 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 dirtied..** I
1157b 74 20 69 73 20 63 6c 65 61 72 65 64 20 61 74 20 t is cleared at
1157c 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 the end of each
1157d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
1157e 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65 64 20 ** It is used
1157f 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 when committing
11580 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65 6e 64 or otherwise end
11581 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f ing a transactio
11582 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 n. If.** the d
11583 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 bModified flag i
11584 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 s clear then les
11585 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 s work has to be
11586 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 done..**.** jou
11587 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a rnalStarted.**.*
11588 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20 69 73 * This flag is
11589 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 74 68 set whenever th
1158a 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e e the main journ
1158b 61 6c 20 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a al is synced. .*
1158c 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 *.** The point
1158d 20 6f 66 20 74 68 69 73 20 66 6c 61 67 20 69 73 of this flag is
1158e 20 74 68 61 74 20 69 74 20 6d 75 73 74 20 62 65 that it must be
1158f 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20 0a set after the .
11590 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f 75 72 6e ** first journ
11591 61 6c 20 68 65 61 64 65 72 20 69 6e 20 61 20 6a al header in a j
11592 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 ournal file has
11593 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 been synced to d
11594 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 isk..** After
11595 74 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65 this has happene
11596 64 2c 20 6e 65 77 20 70 61 67 65 73 20 61 70 70 d, new pages app
11597 65 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74 ended to the dat
11598 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e abase .** do n
11599 6f 74 20 6e 65 65 64 20 74 68 65 20 50 47 48 44 ot need the PGHD
1159a 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
1159b 20 73 65 74 2c 20 61 73 20 74 68 65 79 20 64 6f set, as they do
1159c 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 not need.** t
1159d 6f 20 77 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 o wait for a jou
1159e 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 rnal sync before
1159f 20 74 68 65 79 20 63 61 6e 20 62 65 20 77 72 69 they can be wri
115a0 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 tten out to.**
115a1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
115a2 6c 65 20 28 73 65 65 20 66 75 6e 63 74 69 6f 6e le (see function
115a3 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 29 2e pager_write()).
115a4 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 .** .** setMas
115a5 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 ter.**.** This
115a6 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65 variable is use
115a7 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 d to ensure that
115a8 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
115a9 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a nal file name.**
115aa 20 20 20 28 69 66 20 61 6e 79 29 20 69 73 20 6f (if any) is o
115ab 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f nly written into
115ac 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
115ad 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 e once..**.**
115ae 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 When committing
115af 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 a transaction, t
115b0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 he master journa
115b1 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 l file name (if
115b2 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 any).** may be
115b3 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 written into th
115b4 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 e journal file w
115b5 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 hile the pager i
115b6 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 s still in.**
115b7 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 PAGER_RESERVED s
115b8 74 61 74 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 tate (see Commit
115b9 50 68 61 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 PhaseOne() for t
115ba 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a he action). It.*
115bb 2a 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74 * then attempt
115bc 73 20 74 6f 20 75 70 67 72 61 64 65 20 74 6f 20 s to upgrade to
115bd 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 an exclusive loc
115be 6b 2e 20 49 66 20 74 68 69 73 20 61 74 74 65 6d k. If this attem
115bf 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 pt.** fails, t
115c0 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 hen SQLITE_BUSY
115c1 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20 may be returned
115c2 74 6f 20 74 68 65 20 75 73 65 72 20 61 6e 64 20 to the user and
115c3 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 the user.** ma
115c4 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d y attempt to com
115c5 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 mit the transact
115c6 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 65 72 20 ion again later
115c7 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f (calling.** Co
115c8 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 61 mmitPhaseOne() a
115c9 67 61 69 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 gain). This flag
115ca 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 is used to ensu
115cb 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 re that the .**
115cc 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c master journal
115cd 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 name is only wr
115ce 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 itten to the jou
115cf 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 66 69 rnal file the fi
115d0 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f rst.** time Co
115d1 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 69 mmitPhaseOne() i
115d2 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 s called..**.**
115d3 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 doNotSync.**.**
115d4 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 This variable
115d5 69 73 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72 is set and clear
115d6 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 61 67 ed by sqlite3Pag
115d7 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a erWrite()..**.**
115d8 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 needSync.**.**
115d9 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 TODO: It might
115da 20 62 65 20 65 61 73 69 65 72 20 74 6f 20 73 65 be easier to se
115db 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 t this variable
115dc 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 in writeJournalH
115dd 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 dr().** and wr
115de 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c iteMasterJournal
115df 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 () only. Change
115e0 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 its meaning to "
115e1 75 6e 73 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a unsynced data.**
115e2 20 20 20 68 61 73 20 62 65 65 6e 20 77 72 69 74 has been writ
115e3 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e ten to the journ
115e4 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 al"..**.** subjI
115e5 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 nMemory.**.**
115e6 54 68 69 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 This is a boolea
115e7 6e 20 76 61 72 69 61 62 6c 65 2e 20 49 66 20 74 n variable. If t
115e8 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 rue, then any re
115e9 71 75 69 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e quired sub-journ
115ea 61 6c 0a 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 al.** is opene
115eb 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 d as an in-memor
115ec 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 y journal file.
115ed 49 66 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 If false, then i
115ee 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 n-memory.** su
115ef 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f b-journals are o
115f0 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 69 6e 2d nly used for in-
115f1 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 66 69 6c memory pager fil
115f2 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 es..*/.struct Pa
115f3 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f ger {. sqlite3_
115f4 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 vfs *pVfs;
115f5 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 /* OS functi
115f6 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49 ons to use for I
115f7 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 O */. u8 exclus
115f8 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 iveMode;
115f9 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 /* Boolean. T
115fa 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d rue if locking_m
115fb 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a ode==EXCLUSIVE *
115fc 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f /. u8 journalMo
115fd 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 de;
115fe 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 /* On of the PAG
115ff 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a ER_JOURNALMODE_*
11600 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 values */. u8
11601 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 useJournal;
11602 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 /* Use
11603 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
11604 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 al on this file
11605 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f */. u8 noReadlo
11606 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ck;
11607 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 /* Do not bothe
11608 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 r to obtain read
11609 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f locks */. u8 no
1160a 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Sync;
1160b 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 /* Do not
1160c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 sync the journa
1160d 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 l if true */. u
1160e 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 8 fullSync;
1160f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f /* Do
11610 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20 extra syncs of
11611 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 the journal for
11612 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 robustness */.
11613 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 u8 sync_flags;
11614 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
11615 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 ne of SYNC_NORMA
11616 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a L or SYNC_FULL *
11617 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b /. u8 tempFile;
11618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11619 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 /* zFilename is
1161a 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 a temporary file
1161b 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c */. u8 readOnl
1161c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
1161d 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 /* True for a
1161e 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 read-only databa
1161f 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 se */. u8 memDb
11620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11621 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 /* True to i
11622 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 nhibit all file
11623 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 I/O */.. /* The
11624 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b following block
11625 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 contains those
11626 63 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 74 68 class members th
11627 61 74 20 61 72 65 20 64 79 6e 61 6d 69 63 61 6c at are dynamical
11628 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 ly. ** modified
11629 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f during normal o
1162a 70 65 72 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f perations. The o
1162b 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 ther variables i
1162c 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 n this structure
1162d 0a 20 20 2a 2a 20 61 72 65 20 65 69 74 68 65 72 . ** are either
1162e 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 constant throug
1162f 68 6f 75 74 20 74 68 65 20 6c 69 66 65 74 69 6d hout the lifetim
11630 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20 e of the pager,
11631 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 or else. ** use
11632 64 20 74 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 d to store confi
11633 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 guration paramet
11634 65 72 73 20 74 68 61 74 20 61 66 66 65 63 74 20 ers that affect
11635 74 68 65 20 77 61 79 20 74 68 65 20 70 61 67 65 the way the page
11636 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 r . ** operates
11637 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 .. **. ** The
11638 27 73 74 61 74 65 27 20 76 61 72 69 61 62 6c 65 'state' variable
11639 20 69 73 20 64 65 73 63 72 69 62 65 64 20 69 6e is described in
1163a 20 6d 6f 72 65 20 64 65 74 61 69 6c 20 61 6c 6f more detail alo
1163b 6e 67 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a ng with the. **
1163c 20 64 65 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 descriptions of
1163d 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 6d the values it m
1163e 61 79 20 74 61 6b 65 20 2d 20 50 41 47 45 52 5f ay take - PAGER_
1163f 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 UNLOCK etc. Many
11640 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 of the. ** oth
11641 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 er variables in
11642 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 this block are d
11643 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 escribed in the
11644 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 comment directly
11645 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 . ** above thi
11646 73 20 63 6c 61 73 73 20 64 65 66 69 6e 69 74 69 s class definiti
11647 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 on.. */. u8 st
11648 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ate;
11649 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f /* PAGER_
1164a 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c UNLOCK, _SHARED,
1164b 20 5f 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e _RESERVED, etc.
1164c 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 */. u8 dbModif
1164d 69 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 ied;
1164e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
1164f 72 65 20 61 72 65 20 61 6e 79 20 63 68 61 6e 67 re are any chang
11650 65 73 20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a es to the Db */.
11651 20 20 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 u8 needSync;
11652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11653 20 54 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e True if an fsyn
11654 63 28 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e c() is needed on
11655 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a the journal */.
11656 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 u8 journalStar
11657 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ted; /*
11658 20 54 72 75 65 20 69 66 20 68 65 61 64 65 72 20 True if header
11659 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 of journal is sy
1165a 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 nced */. u8 cha
1165b 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 ngeCountDone;
1165c 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74 /* Set aft
1165d 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 er incrementing
1165e 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
1165f 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 er */. u8 setMa
11660 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 ster;
11661 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
11662 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 m-j name has be
11663 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 en written to jr
11664 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 nl */. u8 doNot
11665 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 Sync;
11666 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 /* Boolean.
11667 57 68 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e While true, do n
11668 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63 ot spill the cac
11669 68 65 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a he */. u8 dbSiz
1166a 65 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20 eValid;
1166b 20 20 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 /* Set when
1166c 64 62 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63 dbSize is correc
1166d 74 20 2a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e t */. u8 subjIn
1166e 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 20 20 20 Memory;
1166f 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 /* True to us
11670 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d e in-memory sub-
11671 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 journals */. Pg
11672 6e 6f 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 no dbSize;
11673 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
11674 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 ber of pages in
11675 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a the database */.
11676 20 20 50 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a Pgno dbOrigSiz
11677 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a e; /*
11678 20 64 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74 dbSize before t
11679 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
1167a 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f action */. Pgno
1167b 20 64 62 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 dbFileSize;
1167c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1167d 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 r of pages in th
1167e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1167f 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 */. int errCode
11680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11681 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 /* One of sever
11682 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f al kinds of erro
11683 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 rs */. int nRec
11684 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11685 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 /* Pages jou
11686 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 rnalled since la
11687 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74 st j-header writ
11688 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 ten */. u32 cks
11689 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 umInit;
1168a 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 /* Quasi-ra
1168b 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64 ndom value added
1168c 20 74 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73 to every checks
1168d 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 um */. u32 nSub
1168e 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
1168f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
11690 20 72 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e records written
11691 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 to sub-journal
11692 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e */. Bitvec *pIn
11693 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
11694 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 /* One bit for
11695 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65 each page in the
11696 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a database file *
11697 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 /. sqlite3_file
11698 20 2a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 *fd;
11699 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 /* File descript
1169a 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 or for database
1169b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
1169c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20 e *jfd;
1169d 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 /* File descrip
1169e 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 tor for main jou
1169f 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 rnal */. sqlite
116a0 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 3_file *sjfd;
116a1 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 /* File de
116a2 73 63 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62 scriptor for sub
116a3 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 -journal */. i6
116a4 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 4 journalOff;
116a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
116a6 72 65 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65 rent write offse
116a7 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c t in the journal
116a8 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a file */. i64 j
116a9 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 ournalHdr;
116aa 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f /* Byte o
116ab 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75 ffset to previou
116ac 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 s journal header
116ad 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70 */. PagerSavep
116ae 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 oint *aSavepoint
116af 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63 ; /* Array of ac
116b0 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
116b1 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f */. int nSavepo
116b2 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 int;
116b3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c /* Number of el
116b4 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70 ements in aSavep
116b5 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 oint[] */. char
116b6 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b dbFileVers[16];
116b7 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 /* Chang
116b8 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61 es whenever data
116b9 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65 base file change
116ba 73 20 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f s */. u32 secto
116bb 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 rSize;
116bc 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 /* Assumed se
116bd 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 ctor size during
116be 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 rollback */..
116bf 75 31 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20 u16 nExtra;
116c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
116c1 64 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 dd this many byt
116c2 65 73 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 es to each in-me
116c3 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 mory page */. i
116c4 31 36 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 16 nReserve;
116c5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
116c6 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62 mber of unused b
116c7 79 74 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65 ytes at end of e
116c8 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 ach page */. u3
116c9 32 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20 2 vfsFlags;
116ca 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 /* Fla
116cb 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 gs for sqlite3_v
116cc 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 fs.xOpen() */.
116cd 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20 int pageSize;
116ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
116cf 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
116d0 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 n a page */. Pg
116d1 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 no mxPgno;
116d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 /* Max
116d3 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a imum allowed siz
116d4 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
116d5 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 e */. char *zFi
116d6 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 lename;
116d7 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
116d8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
116d9 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 */. char *zJour
116da 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 nal;
116db 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
116dc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
116dd 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e int (*xBusyHan
116de 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a dler)(void*); /*
116df 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c Function to cal
116e0 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 l when busy */.
116e1 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 void *pBusyHand
116e2 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 lerArg; /*
116e3 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74 Context argument
116e4 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65 for xBusyHandle
116e5 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 r */.#ifdef SQLI
116e6 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 TE_TEST. int nH
116e7 69 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 it, nMiss;
116e8 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68 /* Cache h
116e9 69 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 its and missing
116ea 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 */. int nRead,
116eb 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20 nWrite;
116ec 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67 /* Database pag
116ed 65 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 es read/written
116ee 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 */.#endif. void
116ef 20 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62 (*xReiniter)(Db
116f0 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 Page*); /* Call
116f1 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 this routine whe
116f2 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 n reloading page
116f3 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 s */.#ifdef SQLI
116f4 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 TE_HAS_CODEC. v
116f5 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 oid *(*xCodec)(v
116f6 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c oid*,void*,Pgno,
116f7 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 int); /* Routine
116f8 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 for en/decoding
116f9 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 data */. void
116fa 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 (*xCodecSizeChng
116fb 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 )(void*,int,int)
116fc 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70 ; /* Notify of p
116fd 61 67 65 20 73 69 7a 65 20 63 68 61 6e 67 65 73 age size changes
116fe 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f */. void (*xCo
116ff 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 3b decFree)(void*);
11700 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
11701 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 Destructor for t
11702 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f he codec */. vo
11703 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 20 20 20 id *pCodec;
11704 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 /* Fir
11705 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 st argument to x
11706 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 Codec... methods
11707 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 */.#endif. cha
11708 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 r *pTmpSpace;
11709 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
1170a 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 r.pageSize bytes
1170b 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d of space for tm
1170c 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 20 6a p use */. i64 j
1170d 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b ournalSizeLimit;
1170e 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c /* Size l
1170f 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73 74 imit for persist
11710 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ent journal file
11711 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 s */. PCache *p
11712 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 PCache;
11713 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
11714 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 6a 65 page cache obje
11715 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f ct */. sqlite3_
11716 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b backup *pBackup;
11717 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
11718 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e o list of ongoin
11719 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 g backup process
1171a 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 es */.};../*.**
1171b 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c The following gl
1171c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68 obal variables h
1171d 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65 old counters use
1171e 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 d for.** testing
1171f 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 purposes only.
11720 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 These variables
11721 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e do not exist in
11722 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e .** a non-testin
11723 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20 g build. These
11724 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f variables are no
11725 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a t thread-safe..*
11726 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
11727 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 TEST.SQLITE_API
11728 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 int sqlite3_page
11729 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d r_readdb_count =
1172a 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
1172b 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72 of full pages r
1172c 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 ead from DB */.S
1172d 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1172e 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 lite3_pager_writ
1172f 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 edb_count = 0;
11730 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 /* Number of fu
11731 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 6e ll pages written
11732 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 to DB */.SQLITE
11733 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
11734 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f _pager_writej_co
11735 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e unt = 0; /* N
11736 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 77 umber of pages w
11737 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 ritten to journa
11738 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 l */.# define PA
11739 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b GER_INCR(v) v++
1173a 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
1173b 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 PAGER_INCR(v).#e
1173c 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f ndif..../*.** Jo
1173d 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69 urnal files begi
1173e 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f n with the follo
1173f 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e wing magic strin
11740 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 g. The data.**
11741 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f was obtained fro
11742 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 m /dev/random.
11743 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 It is used only
11744 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 63 as a sanity chec
11745 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 k..**.** Since v
11746 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 ersion 2.8.0, th
11747 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 e journal format
11748 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69 contains additi
11749 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 onal sanity.** c
1174a 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 hecking informat
1174b 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77 ion. If the pow
1174c 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74 er fails while t
1174d 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 he journal is be
1174e 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 ing.** written,
1174f 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 semi-random garb
11750 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 61 age data might a
11751 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 ppear in the jou
11752 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 rnal.** file aft
11753 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 74 er power is rest
11754 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74 ored. If an att
11755 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64 empt is then mad
11756 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 e.** to roll the
11757 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 journal back, t
11758 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c he database coul
11759 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 d be corrupted.
1175a 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a The additional.
1175b 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 ** sanity checki
1175c 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 74 ng data is an at
1175d 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65 tempt to discove
1175e 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 6e r the garbage in
1175f 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
11760 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a and ignore it..*
11761 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20 *.** The sanity
11762 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 checking informa
11763 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 tion for the new
11764 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 journal format
11765 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 consists.** of a
11766 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 32-bit checksum
11767 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 on each page of
11768 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 63 data. The chec
11769 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 ksum covers both
1176a 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d .** the page num
1176b 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 67 ber and the pPag
1176c 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 er->pageSize byt
1176d 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74 es of data for t
1176e 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 he page..** This
1176f 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61 cksum is initia
11770 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69 lized to a 32-bi
11771 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 t random value t
11772 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 74 hat appears in t
11773 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 he.** journal fi
11774 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 74 le right after t
11775 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20 he header. The
11776 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a random initializ
11777 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c er is important,
11778 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 62 .** because garb
11779 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 70 age data that ap
1177a 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 pears at the end
1177b 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 of a journal is
1177c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 likely.** data
1177d 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e that was once in
1177e 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 61 other files tha
1177f 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 t have now been
11780 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65 deleted. If the
11781 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 61 .** garbage data
11782 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 came from an ob
11783 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 solete journal f
11784 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75 ile, the checksu
11785 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 ms might.** be c
11786 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 20 orrect. But by
11787 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 initializing the
11788 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e checksum to ran
11789 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a dom value which.
1178a 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 ** is different
1178b 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 for every journa
1178c 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 l, we minimize t
1178d 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 hat risk..*/.sta
1178e 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e tic const unsign
1178f 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c ed char aJournal
11790 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 Magic[] = {. 0x
11791 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 d9, 0xd5, 0x05,
11792 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31 0xf9, 0x20, 0xa1
11793 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b , 0x63, 0xd7,.};
11794 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 ../*.** The size
11795 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68 20 of the of each
11796 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20 74 page record in t
11797 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69 he journal is gi
11798 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f ven by.** the fo
11799 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a llowing macro..*
1179a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 /.#define JOURNA
1179b 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20 L_PG_SZ(pPager)
1179c 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ((pPager->pageS
1179d 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a ize) + 8)../*.**
1179e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 The journal hea
1179f 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 der size for thi
117a0 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69 73 s pager. This is
117a1 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61 6d usually the sam
117a2 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 e .** size as a
117a3 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 single disk sect
117a4 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 74 or. See also set
117a5 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f SectorSize()..*/
117a6 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c .#define JOURNAL
117a7 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
117a8 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 (pPager->sectorS
117a9 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ize)../*.** The
117aa 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 macro MEMDB is t
117ab 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 65 rue if we are de
117ac 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e aling with an in
117ad 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
117ae 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20 ..** We do this
117af 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 as a macro so th
117b0 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 at if the SQLITE
117b1 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d _OMIT_MEMORYDB m
117b2 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 acro is set,.**
117b3 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d the value of MEM
117b4 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e DB will be a con
117b5 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f stant and the co
117b6 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 mpiler will opti
117b7 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 mize.** out code
117b8 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 that would neve
117b9 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 r execute..*/.#i
117ba 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
117bb 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 _MEMORYDB.# defi
117bc 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 ne MEMDB 0.#else
117bd 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 .# define MEMDB
117be 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 pPager->memDb.#e
117bf 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 ndif../*.** The
117c0 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 maximum legal pa
117c1 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e ge number is (2^
117c2 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 31 - 1)..*/.#def
117c3 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 ine PAGER_MAX_PG
117c4 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a 23 NO 2147483647..#
117c5 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a 2f ifndef NDEBUG ./
117c6 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a *.** Usage:.**.*
117c7 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73 65 * assert( asse
117c8 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 rt_pager_state(p
117c9 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 61 Pager) );.*/.sta
117ca 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f 70 tic int assert_p
117cb 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65 72 ager_state(Pager
117cc 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f 2a *pPager){.. /*
117cd 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73 20 A temp-file is
117ce 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52 5f always in PAGER_
117cf 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41 47 EXCLUSIVE or PAG
117d0 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 2e ER_SYNCED state.
117d1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 */. assert( pP
117d2 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d ager->tempFile==
117d3 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 0 || pPager->sta
117d4 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
117d5 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 IVE );.. /* The
117d6 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 changeCountDone
117d7 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 flag is always
117d8 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c set for temp-fil
117d9 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 es */. assert(
117da 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
117db 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63 ==0 || pPager->c
117dc 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29 hangeCountDone )
117dd 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d ;.. return 1;.}
117de 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 .#endif../*.** R
117df 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 eturn true if it
117e0 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f is necessary to
117e1 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 67 write page *pPg
117e2 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f into the sub-jo
117e3 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65 urnal..** A page
117e4 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69 needs to be wri
117e5 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 75 tten into the su
117e6 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 65 b-journal if the
117e7 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a re exists one.**
117e8 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61 or more open sa
117e9 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 69 vepoints for whi
117ea 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 ch:.**.** * Th
117eb 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 e page-number is
117ec 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
117ed 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 65 ual to PagerSave
117ee 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64 point.nOrig, and
117ef 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 20 .** * The bit
117f0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f corresponding to
117f1 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 the page-number
117f2 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a is not set in.*
117f3 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70 * PagerSavep
117f4 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e oint.pInSavepoin
117f5 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
117f6 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
117f7 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 e(PgHdr *pPg){.
117f8 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 Pgno pgno = pPg
117f9 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 ->pgno;. Pager
117fa 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
117fb 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a Pager;. int i;.
117fc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
117fd 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
117fe 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 i++){. Pager
117ff 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 Savepoint *p = &
11800 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 pPager->aSavepoi
11801 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 nt[i];. if( p
11802 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26 ->nOrig>=pgno &&
11803 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 65 0==sqlite3Bitve
11804 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 65 cTest(p->pInSave
11805 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a point, pgno) ){.
11806 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
11807 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
11808 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn 0;.}../*.** R
11809 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 eturn true if th
1180a 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 e page is alread
1180b 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c y in the journal
1180c 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 file..*/.static
1180d 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e int pageInJourn
1180e 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a al(PgHdr *pPg){.
1180f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
11810 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3e BitvecTest(pPg->
11811 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
11812 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a al, pPg->pgno);.
11813 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 }../*.** Read a
11814 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 66 32-bit integer f
11815 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 69 rom the given fi
11816 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
11817 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 65 Store the intege
11818 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 61 r.** that is rea
11819 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 74 d in *pRes. Ret
1181a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
1181b 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b everything work
1181c 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 ed, or an.** err
1181d 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 or code is somet
1181e 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e hing goes wrong.
1181f 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 .**.** All value
11820 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 s are stored on
11821 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 69 disk as big-endi
11822 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e an..*/.static in
11823 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 6c t read32bits(sql
11824 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 ite3_file *fd, i
11825 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 2a 64 offset, u32 *
11826 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 pRes){. unsigne
11827 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 d char ac[4];.
11828 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 int rc = sqlite3
11829 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73 OsRead(fd, ac, s
1182a 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 65 izeof(ac), offse
1182b 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 t);. if( rc==SQ
1182c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a LITE_OK ){. *
1182d 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 65 pRes = sqlite3Ge
1182e 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a t4byte(ac);. }.
1182f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11830 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 /*.** Write a 32
11831 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 -bit integer int
11832 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65 o a string buffe
11833 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20 r in big-endian
11834 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 byte order..*/.#
11835 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 73 define put32bits
11836 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 75 (A,B) sqlite3Pu
11837 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 29 t4byte((u8*)A,B)
11838 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 ../*.** Write a
11839 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 32-bit integer i
1183a 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 69 nto the given fi
1183b 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 le descriptor.
1183c 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
1183d 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 6f .** on success o
1183e 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 r an error code
1183f 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 is something goe
11840 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 s wrong..*/.stat
11841 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 69 ic int write32bi
11842 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 ts(sqlite3_file
11843 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c *fd, i64 offset,
11844 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 61 u32 val){. cha
11845 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 32 r ac[4];. put32
11846 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 20 bits(ac, val);.
11847 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f return sqlite3O
11848 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 34 sWrite(fd, ac, 4
11849 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a , offset);.}../*
1184a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 .** The argument
1184b 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 to this macro i
1184c 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 s a file descrip
1184d 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 65 tor (type sqlite
1184e 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74 3_file*)..** Ret
1184f 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 6e urn 0 if it is n
11850 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d ot open, or non-
11851 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 29 zero (but not 1)
11852 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a if it is..**.**
11853 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 This is so that
11854 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e expressions can
11855 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a 0a be written as:.
11856 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f 70 **.** if( isOp
11857 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11858 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 ){ ....**.** ins
11859 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 tead of.**.**
1185a 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d if( pPager->jfd-
1185b 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e >pMethods ){ ...
1185c 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 .*/.#define isOp
1185d 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e en(pFd) ((pFd)->
1185e 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a pMethods)../*.**
1185f 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 20 If file pFd is
11860 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 open, call sqlit
11861 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 e3OsUnlock() on
11862 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e it..*/.static in
11863 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 t osUnlock(sqlit
11864 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e e3_file *pFd, in
11865 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 t eLock){. if(
11866 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b 0a !isOpen(pFd) ){.
11867 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
11868 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 E_OK;. }. retu
11869 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f rn sqlite3OsUnlo
1186a 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a ck(pFd, eLock);.
1186b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
1186c 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 nction determine
1186d 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 s whether or not
1186e 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 the atomic-writ
1186f 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a e optimization.*
11870 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69 * can be used wi
11871 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 54 th this pager. T
11872 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 he optimization
11873 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a 0a can be used if:.
11874 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 76 **.** (a) the v
11875 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
11876 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 OsDeviceCharact
11877 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 63 eristics() indic
11878 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 ates that.**
11879 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 a database pag
1187a 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e e may be written
1187b 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64 atomically, and
1187c 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 6c .** (b) the val
1187d 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f ue returned by O
1187e 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73 sSectorSize() is
1187f 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 less than or eq
11880 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 ual.** to t
11881 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a he page size..**
11882 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 .** The optimiza
11883 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c 77 tion is also alw
11884 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72 20 ays enabled for
11885 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e temporary files.
11886 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72 72 It is.** an err
11887 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 or to call this
11888 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61 67 function if pPag
11889 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 er is opened on
1188a 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 an in-memory.**
1188b 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 database..**.**
1188c 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 If the optimizat
1188d 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 ion cannot be us
1188e 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 ed, 0 is returne
1188f 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20 d. If it can be
11890 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 used,.** then th
11891 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 e value returned
11892 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 is the size of
11893 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
11894 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 when it.** cont
11895 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61 ains rollback da
11896 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f ta for exactly o
11897 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 ne page..*/.#ifd
11898 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
11899 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74 _ATOMIC_WRITE.st
1189a 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66 atic int jrnlBuf
1189b 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 ferSize(Pager *p
1189c 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 Pager){. assert
1189d 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 ( !MEMDB );. if
1189e 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 ( !pPager->tempF
1189f 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 64 ile ){. int d
118a0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
118a1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
118a2 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 Device character
118a3 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69 6e istics */. in
118a4 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 20 t nSector;
118a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
118a6 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 2a /* Sector size *
118a7 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 65 /. int szPage
118a8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
118a9 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
118aa 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73 size */.. ass
118ab 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 ert( isOpen(pPag
118ac 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20 64 er->fd) );. d
118ad 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 c = sqlite3OsDev
118ae 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 iceCharacteristi
118af 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a cs(pPager->fd);.
118b0 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70 50 nSector = pP
118b1 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
118b2 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 70 ;. szPage = p
118b3 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
118b4 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c .. assert(SQL
118b5 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 ITE_IOCAP_ATOMIC
118b6 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 512==(512>>8));.
118b7 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 assert(SQLIT
118b8 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 E_IOCAP_ATOMIC64
118b9 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a K==(65536>>8));.
118ba 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26 28 if( 0==(dc&(
118bb 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
118bc 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29 29 MIC|(szPage>>8))
118bd 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50 61 || nSector>szPa
118be 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 ge) ){. ret
118bf 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d urn 0;. }. }
118c0 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e .. return JOURN
118c1 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
118c2 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 ) + JOURNAL_PG_S
118c3 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65 6e Z(pPager);.}.#en
118c4 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 dif../*.** If SQ
118c5 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
118c6 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 6e is defined then
118c7 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e 69 we do some sani
118c8 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f ty checking.** o
118c9 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e n the cache usin
118ca 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69 6f g a hash functio
118cb 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 n. This is used
118cc 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 for testing.**
118cd 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e and debugging on
118ce 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ly..*/.#ifdef SQ
118cf 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 LITE_CHECK_PAGES
118d0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
118d1 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20 74 32-bit hash of t
118d2 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f 72 he page data for
118d3 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 pPage..*/.stati
118d4 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74 61 c u32 pager_data
118d5 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c 20 hash(int nByte,
118d6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 unsigned char *p
118d7 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61 73 Data){. u32 has
118d8 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a h = 0;. int i;.
118d9 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 79 for(i=0; i<nBy
118da 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68 61 te; i++){. ha
118db 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39 29 sh = (hash*1039)
118dc 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20 7d + pData[i];. }
118dd 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b 0a . return hash;.
118de 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67 }.static u32 pag
118df 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48 64 er_pagehash(PgHd
118e0 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 r *pPage){. ret
118e1 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68 61 urn pager_dataha
118e2 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65 72 sh(pPage->pPager
118e3 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e 73 ->pageSize, (uns
118e4 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50 61 igned char *)pPa
118e5 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 74 ge->pData);.}.st
118e6 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f atic void pager_
118e7 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 48 set_pagehash(PgH
118e8 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70 50 dr *pPage){. pP
118e9 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 age->pageHash =
118ea 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
118eb 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Page);.}../*.**
118ec 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20 6d The CHECK_PAGE m
118ed 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67 48 acro takes a PgH
118ee 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d 65 dr* as an argume
118ef 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43 48 nt. If SQLITE_CH
118f0 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73 20 ECK_PAGES.** is
118f1 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44 45 defined, and NDE
118f2 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e BUG is not defin
118f3 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 20 ed, an assert()
118f4 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b 73 statement checks
118f5 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61 67 .** that the pag
118f6 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72 74 e is either dirt
118f7 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63 68 y or still match
118f8 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74 65 es the calculate
118f9 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f 0a d page-hash..*/.
118fa 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 #define CHECK_PA
118fb 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65 28 GE(x) checkPage(
118fc 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 x).static void c
118fd 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20 2a heckPage(PgHdr *
118fe 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
118ff 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
11900 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 21 ger;. assert( !
11901 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c 7c pPg->pageHash ||
11902 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11903 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d 3e . || (pPg->
11904 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 flags&PGHDR_DIRT
11905 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 48 Y) || pPg->pageH
11906 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 68 ash==pager_pageh
11907 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23 ash(pPg) );.}..#
11908 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 67 else.#define pag
11909 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 29 er_datahash(X,Y)
1190a 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65 0.#define page
1190b 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 30 r_pagehash(X) 0
1190c 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 .#define CHECK_P
1190d 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 2f AGE(x).#endif /
1190e 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 * SQLITE_CHECK_P
1190f 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 AGES */../*.** W
11910 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c hen this is call
11911 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ed the journal f
11912 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50 ile for pager pP
11913 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 65 ager must be ope
11914 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 n..** This funct
11915 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 ion attempts to
11916 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a 6f read a master jo
11917 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 urnal file name
11918 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64 from the .** end
11919 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 of the file and
1191a 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c , if successful,
1191b 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f 20 copies it into
1191c 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 20 memory supplied
1191d 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 65 .** by the calle
1191e 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 r. See comments
1191f 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 65 above writeMaste
11920 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 74 rJournal() for t
11921 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65 he format.** use
11922 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 73 d to store a mas
11923 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
11924 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e 64 name at the end
11925 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 of a journal fi
11926 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 le..**.** zMaste
11927 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 r must point to
11928 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c a buffer of at l
11929 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74 east nMaster byt
1192a 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a es allocated by.
1192b 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 ** the caller. T
1192c 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71 his should be sq
1192d 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 lite3_vfs.mxPath
1192e 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 name+1 (to ensur
1192f 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e e there is.** en
11930 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72 ough space to wr
11931 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a ite the master j
11932 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 ournal name). If
11933 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
11934 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 nal.** name in t
11935 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f he journal is lo
11936 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 nger than nMaste
11937 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69 r bytes (includi
11938 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d ng a.** nul-term
11939 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 inator), then th
1193a 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73 is is handled as
1193b 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f if no master jo
1193c 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 urnal name.** we
1193d 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 re present in th
1193e 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a e journal..**.**
1193f 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 If a master jou
11940 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 rnal file name i
11941 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65 s present at the
11942 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 end of the jour
11943 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 nal.** file, the
11944 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69 n it is copied i
11945 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 nto the buffer p
11946 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 ointed to by zMa
11947 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74 ster. A.** nul-t
11948 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 69 erminator byte i
11949 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 s appended to th
1194a 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69 e buffer followi
1194b 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a ng the master.**
1194c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
1194d 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 me..**.** If it
1194e 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 is determined th
1194f 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 at no master jou
11950 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 rnal file name i
11951 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d s present .** zM
11952 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 20 aster[0] is set
11953 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f to 0 and SQLITE_
11954 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a OK returned..**.
11955 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
11956 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 64 ccurs while read
11957 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 ing from the jou
11958 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51 rnal file, an SQ
11959 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f Lite.** error co
1195a 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a de is returned..
1195b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
1195c 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 adMasterJournal(
1195d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1195e 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 rnl, char *zMast
1195f 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 29 er, u32 nMaster)
11960 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 {. int rc;
11961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11962 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
11963 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 . u32 len;
11964 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11965 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73 Length in bytes
11966 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e of master journ
11967 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34 al name */. i64
11968 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 szJ;
11969 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c /* Total
1196a 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f size in bytes o
1196b 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 f journal file p
1196c 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b Jrnl */. u32 ck
1196d 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 sum;
1196e 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b /* MJ check
1196f 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66 sum value read f
11970 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 rom journal */.
11971 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20 u32 u;
11972 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 /* U
11973 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 nsigned loop cou
11974 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e nter */. unsign
11975 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 ed char aMagic[8
11976 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 ]; /* A buffer
11977 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 to hold the mag
11978 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a ic header */. z
11979 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 Master[0] = '\0'
1197a 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f ;.. if( SQLITE_
1197b 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK!=(rc = sqlite
1197c 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e 3OsFileSize(pJrn
1197d 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20 l, &szJ)). ||
1197e 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c szJ<16. || SQL
1197f 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 ITE_OK!=(rc = re
11980 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 ad32bits(pJrnl,
11981 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20 szJ-16, &len)).
11982 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 || len>=nMaste
11983 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f r . || SQLITE_
11984 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 OK!=(rc = read32
11985 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d bits(pJrnl, szJ-
11986 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 20 12, &cksum)).
11987 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 || SQLITE_OK!=(r
11988 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
11989 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c d(pJrnl, aMagic,
1198a 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c 8, szJ-8)). |
1198b 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c | memcmp(aMagic,
1198c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
1198d 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 8). || SQLITE_
1198e 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 OK!=(rc = sqlite
1198f 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a 3OsRead(pJrnl, z
11990 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a Master, len, szJ
11991 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20 -16-len)). ){.
11992 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
11993 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74 }.. /* See if t
11994 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 he checksum matc
11995 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a hes the master j
11996 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 ournal name */.
11997 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b for(u=0; u<len;
11998 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d u++){. cksum
11999 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a -= zMaster[u];.
1199a 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 }. if( cksum
1199b 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ){. /* If the
1199c 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 checksum doesn'
1199d 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f t add up, then o
1199e 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 ne or more of th
1199f 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 e disk sectors.
119a0 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 ** containing
119a1 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
119a2 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 nal filename is
119a3 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20 corrupted. This
119a4 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 means. ** def
119a5 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 initely roll bac
119a6 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72 k, so just retur
119a7 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 n SQLITE_OK and
119a8 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 report a (nul).
119a9 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 ** master-jou
119aa 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 rnal filename..
119ab 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 */. len =
119ac 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 0;. }. zMaster
119ad 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 [len] = '\0';.
119ae 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 . return SQLIT
119af 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 E_OK;.}../*.** R
119b0 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74 eturn the offset
119b1 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 62 of the sector b
119b2 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 6d oundary at or im
119b3 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f mediately .** fo
119b4 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c 75 llowing the valu
119b5 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 e in pPager->jou
119b6 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e rnalOff, assumin
119b7 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73 g a sector .** s
119b8 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 73 ize of pPager->s
119b9 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 2e ectorSize bytes.
119ba 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 .**.** i.e for a
119bb 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 sector size of
119bc 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 512:.**.** Pag
119bd 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20 er.journalOff
119be 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 61 Return va
119bf 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d lue.** -------
119c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
119c1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
119c2 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20 .** 0
119c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119c4 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 20 0.** 512
119c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119c6 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 20 512.** 100
119c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
119c8 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 30 512.** 200
119c9 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0
119ca 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2048.** .
119cb 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f */.static i64 jo
119cc 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50 urnalHdrOffset(P
119cd 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
119ce 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b i64 offset = 0;
119cf 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 65 . i64 c = pPage
119d0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 r->journalOff;.
119d1 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 if( c ){. of
119d2 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f fset = ((c-1)/JO
119d3 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
119d4 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 ger) + 1) * JOUR
119d5 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 NAL_HDR_SZ(pPage
119d6 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 r);. }. assert
119d7 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c ( offset%JOURNAL
119d8 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d _HDR_SZ(pPager)=
119d9 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
119da 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 offset>=c );. a
119db 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d 63 ssert( (offset-c
119dc 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a )<JOURNAL_HDR_SZ
119dd 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 (pPager) );. re
119de 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a turn offset;.}..
119df 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 /*.** The journa
119e0 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f l file must be o
119e1 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75 pen when this fu
119e2 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
119e3 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e ..**.** This fun
119e4 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
119e5 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 if the journal
119e6 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 file has not bee
119e7 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 n written to.**
119e8 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65 within the curre
119e9 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 nt transaction (
119ea 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f i.e. if Pager.jo
119eb 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a urnalOff==0)..**
119ec 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 74 .** If doTruncat
119ed 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 e is non-zero or
119ee 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e the Pager.journ
119ef 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 69 alSizeLimit vari
119f0 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 able is.** set t
119f1 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 o 0, then trunca
119f2 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 te the journal f
119f3 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 ile to zero byte
119f4 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72 s in size. Other
119f5 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68 wise,.** zero th
119f6 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 72 e 28-byte header
119f7 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
119f8 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
119f9 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 e. In either cas
119fa 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 e, .** if the pa
119fb 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f ger is not in no
119fc 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63 -sync mode, sync
119fd 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
119fe 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a e immediately .*
119ff 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 20 * after writing
11a00 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 74 or truncating it
11a01 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72 ..**.** If Pager
11a02 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 .journalSizeLimi
11a03 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f t is set to a po
11a04 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f sitive, non-zero
11a05 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66 value, and.** f
11a06 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 75 ollowing the tru
11a07 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69 ncation or zeroi
11a08 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62 6f ng described abo
11a09 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ve the size of t
11a0a 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 he .** journal f
11a0b 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 20 ile in bytes is
11a0c 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 larger than this
11a0d 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 75 value, then tru
11a0e 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 ncate the.** jou
11a0f 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 67 rnal file to Pag
11a10 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 er.journalSizeLi
11a11 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 6a mit bytes. The j
11a12 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 ournal file does
11a13 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 .** not need to
11a14 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 be synced follow
11a15 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 ing this operati
11a16 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 on..**.** If an
11a17 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c IO error occurs,
11a18 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 abandon process
11a19 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74 ing and return t
11a1a 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
11a1b 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ..** Otherwise,
11a1c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
11a1d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11a1e 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 zeroJournalHdr(P
11a1f 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
11a20 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 t doTruncate){.
11a21 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
11a22 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 _OK;
11a23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11a24 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
11a25 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 e */. assert( i
11a26 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
11a27 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 d) );. if( pPag
11a28 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 er->journalOff )
11a29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 {. const i64
11a2a 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 2d iLimit = pPager-
11a2b 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 >journalSizeLimi
11a2c 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 t; /* Local c
11a2d 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a ache of jsl */..
11a2e 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 5a IOTRACE(("JZ
11a2f 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 50 EROHDR %p\n", pP
11a30 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 64 ager)). if( d
11a31 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69 oTruncate || iLi
11a32 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 mit==0 ){.
11a33 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 rc = sqlite3OsTr
11a34 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a uncate(pPager->j
11a35 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 fd, 0);. }els
11a36 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 e{. static
11a37 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48 const char zeroH
11a38 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20 dr[28] = {0};.
11a39 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
11a3a 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
11a3b 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69 jfd, zeroHdr, si
11a3c 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 30 zeof(zeroHdr), 0
11a3d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
11a3e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
11a3f 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e & !pPager->noSyn
11a40 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 c ){. rc =
11a41 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 sqlite3OsSync(pP
11a42 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 ager->jfd, SQLIT
11a43 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c E_SYNC_DATAONLY|
11a44 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 pPager->sync_fla
11a45 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 gs);. }..
11a46 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 /* At this point
11a47 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
11a48 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 75 is committed bu
11a49 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b t the write lock
11a4a 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c . ** is stil
11a4b 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 l held on the fi
11a4c 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 le. If there is
11a4d 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e a size limit con
11a4e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 20 figured for .
11a4f 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 ** the persiste
11a50 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 nt journal and t
11a51 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11a52 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d currently consum
11a53 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73 es more. ** s
11a54 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 6c pace than that l
11a55 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c imit allows for,
11a56 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f 77 truncate it now
11a57 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 . There is no ne
11a58 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e ed. ** to syn
11a59 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f c the file follo
11a5a 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 wing this operat
11a5b 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
11a5c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
11a5d 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b K && iLimit>0 ){
11a5e 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a 20 . i64 sz;.
11a5f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
11a60 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 3OsFileSize(pPag
11a61 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20 er->jfd, &sz);.
11a62 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c if( rc==SQL
11a63 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69 ITE_OK && sz>iLi
11a64 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 mit ){. r
11a65 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 c = sqlite3OsTru
11a66 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 ncate(pPager->jf
11a67 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 d, iLimit);.
11a68 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
11a69 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
11a6a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 .** The journal
11a6b 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 file must be ope
11a6c 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 n when this rout
11a6d 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 ine is called. A
11a6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64 journal.** head
11a6f 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f er (JOURNAL_HDR_
11a70 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 69 SZ bytes) is wri
11a71 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f tten into the jo
11a72 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 68 urnal file at th
11a73 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 e.** current loc
11a74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ation..**.** The
11a75 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 20 format for the
11a76 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 journal header i
11a77 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a s as follows:.**
11a78 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 69 - 8 bytes: Magi
11a79 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f c identifying jo
11a7a 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a urnal format..**
11a7b 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d 62 - 4 bytes: Numb
11a7c 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e er of records in
11a7d 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20 journal, or -1
11a7e 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 no-sync mode is
11a7f 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 on..** - 4 bytes
11a80 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 : Random number
11a81 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 61 used for page ha
11a82 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 sh..** - 4 bytes
11a83 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 61 : Initial databa
11a84 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a se page count..*
11a85 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 63 * - 4 bytes: Sec
11a86 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 79 tor size used by
11a87 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 the process tha
11a88 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f 75 t wrote this jou
11a89 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 rnal..** - 4 byt
11a8a 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 67 es: Database pag
11a8b 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 e size..** .** F
11a8c 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 ollowed by (JOUR
11a8d 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 NAL_HDR_SZ - 28)
11a8e 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 bytes of unused
11a8f 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 space..*/.stati
11a90 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e c int writeJourn
11a91 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61 alHdr(Pager *pPa
11a92 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ger){. int rc =
11a93 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
11a94 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
11a95 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 eturn code */.
11a96 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 char *zHeader =
11a97 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
11a98 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 e; /* Temporary
11a99 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 62 space used to b
11a9a 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a 20 uild header */.
11a9b 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20 70 u32 nHeader = p
11a9c 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b Pager->pageSize;
11a9d 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
11a9e 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 buffer pointed t
11a9f 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a o by zHeader */.
11aa0 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20 20 u32 nWrite;
11aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11aa2 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f /* Bytes o
11aa3 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72 20 f header sector
11aa4 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 written */. int
11aa5 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 ii;
11aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11aa7 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
11aa8 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 */.. assert( i
11aa9 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 sOpen(pPager->jf
11aaa 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f d) ); /* Jo
11aab 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 urnal file must
11aac 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69 be open. */.. i
11aad 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e f( nHeader>JOURN
11aae 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 AL_HDR_SZ(pPager
11aaf 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 ) ){. nHeader
11ab0 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 = JOURNAL_HDR_S
11ab1 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a Z(pPager);. }..
11ab2 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 /* If there ar
11ab3 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 e active savepoi
11ab4 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 nts and any of t
11ab5 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65 64 hem were created
11ab6 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 . ** since the
11ab7 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 most recent jou
11ab8 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 20 rnal header was
11ab9 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 written, update
11aba 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 53 the . ** PagerS
11abb 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 avepoint.iHdrOff
11abc 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a set fields now..
11abd 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b */. for(ii=0;
11abe 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 ii<pPager->nSav
11abf 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 epoint; ii++){.
11ac0 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61 if( pPager->a
11ac1 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 Savepoint[ii].iH
11ac2 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20 drOffset==0 ){.
11ac3 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 pPager->aSa
11ac4 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 vepoint[ii].iHdr
11ac5 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d Offset = pPager-
11ac6 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 >journalOff;.
11ac7 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 }. }.. pPager
11ac8 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 ->journalHdr = p
11ac9 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11aca 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 f = journalHdrOf
11acb 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 0a 20 fset(pPager);..
11acc 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 /* . ** Write
11acd 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d the nRec Field -
11ace 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 the number of p
11acf 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 74 age records that
11ad0 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a follow this. *
11ad1 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 * journal header
11ad2 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f . Normally, zero
11ad3 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
11ad4 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 68 69 his value at thi
11ad5 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 s time.. ** Aft
11ad6 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 20 61 er the records a
11ad7 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 re added to the
11ad8 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 journal (and the
11ad9 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c journal synced,
11ada 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c . ** if in ful
11adb 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 l-sync mode), th
11adc 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72 e zero is overwr
11add 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 74 itten with the t
11ade 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 rue number. **
11adf 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 20 of records (see
11ae0 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a syncJournal())..
11ae1 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 **. ** A fast
11ae2 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 er alternative i
11ae3 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46 46 s to write 0xFFF
11ae4 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65 FFFFF to the nRe
11ae5 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 c field. When.
11ae6 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a ** reading the j
11ae7 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 ournal this valu
11ae8 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 e tells SQLite t
11ae9 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 o assume that th
11aea 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 e. ** rest of t
11aeb 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11aec 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70 contains valid p
11aed 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69 age records. Thi
11aee 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a s assumption. *
11aef 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 * is dangerous,
11af0 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 as if a failure
11af1 6f 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 20 occurred whilst
11af2 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a writing to the j
11af3 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 ournal. ** file
11af4 20 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 it may contain
11af5 73 6f 6d 65 20 67 61 72 62 61 67 65 20 64 61 74 some garbage dat
11af6 61 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f a. There are two
11af7 20 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 scenarios. **
11af8 77 68 65 72 65 20 74 68 69 73 20 72 69 73 6b 20 where this risk
11af9 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a can be ignored:.
11afa 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 **. ** * Wh
11afb 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 20 en the pager is
11afc 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e in no-sync mode.
11afd 20 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 Corruption can
11afe 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 follow a. **
11aff 20 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 power failure
11b00 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 79 in this case any
11b01 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 way.. **. **
11b02 20 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 * When the SQLI
11b03 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
11b04 50 45 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74 PEND flag is set
11b05 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 . This guarantee
11b06 73 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20 s. ** that
11b07 67 61 72 62 61 67 65 20 64 61 74 61 20 69 73 20 garbage data is
11b08 6e 65 76 65 72 20 61 70 70 65 6e 64 65 64 20 74 never appended t
11b09 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 o the journal fi
11b0a 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 le.. */. asser
11b0b 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11b0c 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d ->fd) || pPager-
11b0d 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 28 >noSync );. if(
11b0e 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 (pPager->noSync
11b0f 29 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f ) || (pPager->jo
11b10 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 urnalMode==PAGER
11b11 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d _JOURNALMODE_MEM
11b12 4f 52 59 29 0a 20 20 20 7c 7c 20 28 73 71 6c 69 ORY). || (sqli
11b13 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 te3OsDeviceChara
11b14 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65 cteristics(pPage
11b15 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f r->fd)&SQLITE_IO
11b16 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 CAP_SAFE_APPEND)
11b17 20 0a 20 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 . ){. memcp
11b18 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 y(zHeader, aJour
11b19 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 nalMagic, sizeof
11b1a 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 (aJournalMagic))
11b1b 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 ;. put32bits(
11b1c 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 &zHeader[sizeof(
11b1d 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c aJournalMagic)],
11b1e 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 0xffffffff);.
11b1f 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 }else{. memse
11b20 74 28 7a 48 65 61 64 65 72 2c 20 30 2c 20 73 69 t(zHeader, 0, si
11b21 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 zeof(aJournalMag
11b22 69 63 29 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f ic)+4);. }.. /
11b23 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 65 * The random che
11b24 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c 69 ck-hash initiali
11b25 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 ser */ . sqlite
11b26 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 3_randomness(siz
11b27 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 eof(pPager->cksu
11b28 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 2d mInit), &pPager-
11b29 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 70 >cksumInit);. p
11b2a 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65 ut32bits(&zHeade
11b2b 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 r[sizeof(aJourna
11b2c 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 67 lMagic)+4], pPag
11b2d 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a er->cksumInit);.
11b2e 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 6c /* The initial
11b2f 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 2a database size *
11b30 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a /. put32bits(&z
11b31 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a Header[sizeof(aJ
11b32 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d 2c ournalMagic)+8],
11b33 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
11b34 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 ize);. /* The a
11b35 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69 ssumed sector si
11b36 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 ze for this proc
11b37 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 ess */. put32bi
11b38 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 ts(&zHeader[size
11b39 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
11b3a 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 )+12], pPager->s
11b3b 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 2f ectorSize);.. /
11b3c 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 * The page size
11b3d 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 */. put32bits(&
11b3e 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 zHeader[sizeof(a
11b3f 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 JournalMagic)+16
11b40 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 ], pPager->pageS
11b41 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 ize);.. /* Init
11b42 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61 69 ializing the tai
11b43 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 l of the buffer
11b44 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 is not necessary
11b45 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20 20 . Everything.
11b46 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69 66 ** works find if
11b47 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d the following m
11b48 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74 74 emset() is omitt
11b49 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61 6c ed. But initial
11b4a 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d izing. ** the m
11b4b 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20 76 emory prevents v
11b4c 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f 6d algrind from com
11b4d 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65 20 plaining, so we
11b4e 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a 20 are willing to.
11b4f 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65 72 ** take the per
11b50 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20 20 formance hit..
11b51 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48 65 */. memset(&zHe
11b52 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 ader[sizeof(aJou
11b53 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c 20 rnalMagic)+20],
11b54 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65 61 0,. nHea
11b55 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f 75 der-(sizeof(aJou
11b56 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29 3b rnalMagic)+20));
11b57 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 .. /* In theory
11b58 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65 63 , it is only nec
11b59 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 essary to write
11b5a 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68 61 the 28 bytes tha
11b5b 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 72 t the . ** jour
11b5c 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73 75 nal header consu
11b5d 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e mes to the journ
11b5e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54 68 al file here. Th
11b5f 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 en increment the
11b60 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f 75 . ** Pager.jou
11b61 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c 65 rnalOff variable
11b62 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f by JOURNAL_HDR_
11b63 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e SZ so that the n
11b64 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72 64 ext . ** record
11b65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
11b66 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 he following sec
11b67 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20 67 tor (leaving a g
11b68 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 20 ap in the file.
11b69 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 65 ** that will be
11b6a 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c implicitly fill
11b6b 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53 29 ed in by the OS)
11b6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 .. **. ** Howe
11b6d 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 ver it has been
11b6e 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20 discovered that
11b6f 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 on some systems
11b70 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 6e this pattern can
11b71 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 66 . ** be signif
11b72 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74 icantly slower t
11b73 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 han contiguously
11b74 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f writing data to
11b75 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 the file,. **
11b76 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 even if that mea
11b77 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77 72 ns explicitly wr
11b78 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 iting data to th
11b79 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a e block of . **
11b7a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a (JOURNAL_HDR_SZ
11b7b 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68 61 - 28) bytes tha
11b7c 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 t will not be us
11b7d 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20 77 ed. So that is w
11b7e 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e 65 hat. ** is done
11b7f 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 . . **. ** The
11b80 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72 65 loop is require
11b81 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20 74 d here in case t
11b82 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 69 he sector-size i
11b83 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 s larger than th
11b84 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 e . ** database
11b85 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e 63 page size. Sinc
11b86 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62 75 e the zHeader bu
11b87 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61 67 ffer is only Pag
11b88 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a 2a er.pageSize. **
11b89 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 bytes in size,
11b8a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 61 more than one ca
11b8b 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 57 ll to sqlite3OsW
11b8c 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72 65 rite() may be re
11b8d 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20 70 quired. ** to p
11b8e 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74 69 opulate the enti
11b8f 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 re journal heade
11b90 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a r sector.. */ .
11b91 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b 20 for(nWrite=0;
11b92 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 6e rc==SQLITE_OK&&n
11b93 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44 Write<JOURNAL_HD
11b94 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e 57 R_SZ(pPager); nW
11b95 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b 0a rite+=nHeader){.
11b96 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 IOTRACE(("JH
11b97 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 DR %p %lld %d\n"
11b98 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 , pPager, pPager
11b99 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e 48 ->journalHdr, nH
11b9a 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20 3d eader)). rc =
11b9b 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
11b9c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 pPager->jfd, zHe
11b9d 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20 70 ader, nHeader, p
11b9e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
11b9f 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e f);. pPager->
11ba0 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48 journalOff += nH
11ba1 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 eader;. }.. re
11ba2 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
11ba3 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
11ba4 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 le must be open
11ba5 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c when this is cal
11ba6 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68 led. A journal h
11ba7 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a eader file.** (J
11ba8 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 OURNAL_HDR_SZ by
11ba9 74 65 73 29 20 69 73 20 72 65 61 64 20 66 72 6f tes) is read fro
11baa 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f m the current lo
11bab 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f cation in the jo
11bac 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54 urnal.** file. T
11bad 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 he current locat
11bae 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e ion in the journ
11baf 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e al file is given
11bb0 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a by.** pPager->j
11bb1 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63 ournalOff. See c
11bb2 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 omments above fu
11bb3 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 nction writeJour
11bb4 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a 2a 20 nalHdr() for.**
11bb5 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 a description of
11bb6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 the journal hea
11bb7 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a der format..**.*
11bb8 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 72 20 * If the header
11bb9 69 73 20 72 65 61 64 20 73 75 63 63 65 73 73 66 is read successf
11bba 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 73 20 ully, *pNRec is
11bbb 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
11bbc 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 r of.** page rec
11bbd 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 ords following t
11bbe 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 20 2a his header and *
11bbf 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 20 74 pDbSize is set t
11bc0 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 o the size of th
11bc1 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62 65 e.** database be
11bc2 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 fore the transac
11bc3 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70 tion began, in p
11bc4 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67 ages. Also, pPag
11bc5 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a er->cksumInit.**
11bc6 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 is set to the v
11bc7 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 alue read from t
11bc8 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
11bc9 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 r. SQLITE_OK is
11bca 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 returned.** in t
11bcb 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 his case..**.**
11bcc 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 If the journal h
11bcd 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65 61 eader file appea
11bce 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70 74 rs to be corrupt
11bcf 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 ed, SQLITE_DONE
11bd0 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 61 is.** returned a
11bd1 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 2a 50 nd *pNRec and *P
11bd2 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 65 66 DbSize are undef
11bd3 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 4e 41 ined. If JOURNA
11bd4 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a L_HDR_SZ bytes.*
11bd5 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 61 64 * cannot be read
11bd6 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 from the journa
11bd7 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f 72 20 l file an error
11bd8 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 code is returned
11bd9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
11bda 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a readJournalHdr(.
11bdb 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c Pager *pPager,
11bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11bdd 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a * Pager object *
11bde 2f 0a 20 20 69 6e 74 20 69 73 48 6f 74 2c 0a 20 /. int isHot,.
11bdf 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 i64 journalSize
11be0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
11be1 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 70 65 Size of the ope
11be2 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 n journal file i
11be3 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 n bytes */. u32
11be4 20 2a 70 4e 52 65 63 2c 20 20 20 20 20 20 20 20 *pNRec,
11be5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
11be6 3a 20 56 61 6c 75 65 20 72 65 61 64 20 66 72 6f : Value read fro
11be7 6d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 m the nRec field
11be8 20 2a 2f 0a 20 20 75 33 32 20 2a 70 44 62 53 69 */. u32 *pDbSi
11be9 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ze
11bea 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 /* OUT: Value
11beb 20 6f 66 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 of original dat
11bec 61 62 61 73 65 20 73 69 7a 65 20 66 69 65 6c 64 abase size field
11bed 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
11bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11bef 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
11bf0 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e code */. unsign
11bf1 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38 ed char aMagic[8
11bf2 5d 3b 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66 ]; /* A buff
11bf3 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d er to hold the m
11bf4 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 agic header */.
11bf5 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 i64 iHdrOff;
11bf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11bf7 20 4f 66 66 73 65 74 20 6f 66 20 6a 6f 75 72 6e Offset of journ
11bf8 61 6c 20 68 65 61 64 65 72 20 62 65 69 6e 67 20 al header being
11bf9 72 65 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 read */.. asser
11bfa 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
11bfb 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f ->jfd) ); /
11bfc 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d * Journal file m
11bfd 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a ust be open. */.
11bfe 0a 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 50 61 . /* Advance Pa
11bff 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 74 ger.journalOff t
11c00 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 o the start of t
11c01 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 2e 20 he next sector.
11c02 49 66 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 If the. ** jour
11c03 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 6f 6f 20 nal file is too
11c04 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 72 65 20 small for there
11c05 74 6f 20 62 65 20 61 20 68 65 61 64 65 72 20 73 to be a header s
11c06 74 6f 72 65 64 20 61 74 20 74 68 69 73 0a 20 20 tored at this.
11c07 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 74 75 72 6e ** point, return
11c08 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 20 20 SQLITE_DONE..
11c09 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 */. pPager->jou
11c0a 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 rnalOff = journa
11c0b 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 lHdrOffset(pPage
11c0c 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 r);. if( pPager
11c0d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 ->journalOff+JOU
11c0e 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 RNAL_HDR_SZ(pPag
11c0f 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a er) > journalSiz
11c10 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 e ){. return
11c11 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
11c12 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 . iHdrOff = pPa
11c13 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b ger->journalOff;
11c14 0a 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 .. /* Read in t
11c15 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73 he first 8 bytes
11c16 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
11c17 68 65 61 64 65 72 2e 20 49 66 20 74 68 65 79 20 header. If they
11c18 64 6f 20 6e 6f 74 20 6d 61 74 63 68 0a 20 20 2a do not match. *
11c19 2a 20 74 68 65 20 20 6d 61 67 69 63 20 73 74 72 * the magic str
11c1a 69 6e 67 20 66 6f 75 6e 64 20 61 74 20 74 68 65 ing found at the
11c1b 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 6a start of each j
11c1c 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 72 ournal header, r
11c1d 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 eturn. ** SQLIT
11c1e 45 5f 44 4f 4e 45 2e 20 49 66 20 61 6e 20 49 4f E_DONE. If an IO
11c1f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 error occurs, r
11c20 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 eturn an error c
11c21 6f 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a ode. Otherwise,.
11c22 20 20 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 20 20 ** proceed..
11c23 2a 2f 0a 20 20 69 66 28 20 69 73 48 6f 74 20 7c */. if( isHot |
11c24 7c 20 69 48 64 72 4f 66 66 21 3d 70 50 61 67 65 | iHdrOff!=pPage
11c25 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 29 7b r->journalHdr ){
11c26 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
11c27 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 3OsRead(pPager->
11c28 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73 69 7a jfd, aMagic, siz
11c29 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 69 48 64 eof(aMagic), iHd
11c2a 72 4f 66 66 29 3b 0a 20 20 20 20 69 66 28 20 72 rOff);. if( r
11c2b 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 c ){. retur
11c2c 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
11c2d 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 if( memcmp(aMagi
11c2e 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 c, aJournalMagic
11c2f 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 , sizeof(aMagic)
11c30 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 )!=0 ){. re
11c31 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 turn SQLITE_DONE
11c32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
11c33 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74 * Read the first
11c34 20 74 68 72 65 65 20 33 32 2d 62 69 74 20 66 69 three 32-bit fi
11c35 65 6c 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 elds of the jour
11c36 6e 61 6c 20 68 65 61 64 65 72 3a 20 54 68 65 20 nal header: The
11c37 6e 52 65 63 0a 20 20 2a 2a 20 66 69 65 6c 64 2c nRec. ** field,
11c38 20 74 68 65 20 63 68 65 63 6b 73 75 6d 2d 69 6e the checksum-in
11c39 69 74 69 61 6c 69 7a 65 72 20 61 6e 64 20 74 68 itializer and th
11c3a 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 e database size
11c3b 61 74 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a at the start. *
11c3c 2a 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 * of the transac
11c3d 74 69 6f 6e 2e 20 52 65 74 75 72 6e 20 61 6e 20 tion. Return an
11c3e 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e error code if an
11c3f 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e ything goes wron
11c40 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 g.. */. if( SQ
11c41 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 LITE_OK!=(rc = r
11c42 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 ead32bits(pPager
11c43 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 38 ->jfd, iHdrOff+8
11c44 2c 20 70 4e 52 65 63 29 29 0a 20 20 20 7c 7c 20 , pNRec)). ||
11c45 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11c46 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
11c47 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11c48 2b 31 32 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b +12, &pPager->ck
11c49 73 75 6d 49 6e 69 74 29 29 0a 20 20 20 7c 7c 20 sumInit)). ||
11c4a 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11c4b 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
11c4c 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11c4d 2b 31 36 2c 20 70 44 62 53 69 7a 65 29 29 0a 20 +16, pDbSize)).
11c4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 ){. return r
11c4f 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 c;. }.. if( pP
11c50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11c51 3d 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 69 ==0 ){. u32 i
11c52 50 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 PageSize;
11c53 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 2d /* Page-
11c54 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f size field of jo
11c55 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a urnal header */.
11c56 20 20 20 20 75 33 32 20 69 53 65 63 74 6f 72 53 u32 iSectorS
11c57 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
11c58 20 2f 2a 20 53 65 63 74 6f 72 2d 73 69 7a 65 20 /* Sector-size
11c59 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c field of journal
11c5a 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 header */. u
11c5b 31 36 20 69 50 61 67 65 53 69 7a 65 31 36 3b 20 16 iPageSize16;
11c5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
11c5d 6f 70 79 20 6f 66 20 69 50 61 67 65 53 69 7a 65 opy of iPageSize
11c5e 20 69 6e 20 31 36 2d 62 69 74 20 76 61 72 69 61 in 16-bit varia
11c5f 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 ble */.. /* R
11c60 65 61 64 20 74 68 65 20 70 61 67 65 2d 73 69 7a ead the page-siz
11c61 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a e and sector-siz
11c62 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 e journal header
11c63 20 66 69 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20 fields. */.
11c64 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 if( SQLITE_OK!=(
11c65 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28 rc = read32bits(
11c66 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 pPager->jfd, iHd
11c67 72 4f 66 66 2b 32 30 2c 20 26 69 53 65 63 74 6f rOff+20, &iSecto
11c68 72 53 69 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20 rSize)). ||
11c69 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d SQLITE_OK!=(rc =
11c6a 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 read32bits(pPag
11c6b 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 er->jfd, iHdrOff
11c6c 2b 32 34 2c 20 26 69 50 61 67 65 53 69 7a 65 29 +24, &iPageSize)
11c6d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 ). ){. r
11c6e 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
11c6f 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 . /* Check th
11c70 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 72 65 at the values re
11c71 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 ad from the page
11c72 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 -size and sector
11c73 2d 73 69 7a 65 20 66 69 65 6c 64 73 0a 20 20 20 -size fields.
11c74 20 2a 2a 20 61 72 65 20 77 69 74 68 69 6e 20 72 ** are within r
11c75 61 6e 67 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 ange. To be 'in
11c76 72 61 6e 67 65 27 2c 20 62 6f 74 68 20 76 61 6c range', both val
11c77 75 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61 ues need to be a
11c78 20 70 6f 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66 power. ** of
11c79 20 74 77 6f 20 67 72 65 61 74 65 72 20 74 68 61 two greater tha
11c7a 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31 n or equal to 51
11c7b 32 20 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74 2 or 32, and not
11c7c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
11c7d 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70 eir . ** resp
11c7e 65 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74 ective compile t
11c7f 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69 ime maximum limi
11c80 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ts.. */. i
11c81 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32 f( iPageSize<512
11c82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11c83 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 || iSectorSize
11c84 3c 33 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67 <32. || iPag
11c85 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 eSize>SQLITE_MAX
11c86 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53 _PAGE_SIZE || iS
11c87 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 ectorSize>MAX_SE
11c88 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c CTOR_SIZE. |
11c89 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29 | ((iPageSize-1)
11c8a 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20 &iPageSize)!=0
11c8b 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a || ((iSectorSiz
11c8c 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65 e-1)&iSectorSize
11c8d 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20 )!=0 . ){.
11c8e 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74 /* If the eit
11c8f 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a her the page-siz
11c90 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65 e or sector-size
11c91 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d in the journal-
11c92 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20 header is .
11c93 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65 ** invalid, the
11c94 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 n the process th
11c95 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75 at wrote the jou
11c96 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74 rnal-header must
11c97 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20 have . **
11c98 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74 crashed before t
11c99 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79 he header was sy
11c9a 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 nced. In this ca
11c9b 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20 se stop reading
11c9c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f . ** the jo
11c9d 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e urnal file here.
11c9e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
11c9f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f return SQLITE_DO
11ca0 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f NE;. }.. /
11ca1 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67 * Update the pag
11ca2 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 e-size to match
11ca3 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 the value read f
11ca4 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e rom the journal.
11ca5 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74 . ** Use a t
11ca6 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 estcase() macro
11ca7 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
11ca8 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 t malloc failure
11ca9 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20 within . **
11caa 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
11cab 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20 () is tested..
11cac 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69 */. iPageSi
11cad 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67 ze16 = (u16)iPag
11cae 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 eSize;. rc =
11caf 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
11cb0 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20 agesize(pPager,
11cb1 26 69 50 61 67 65 53 69 7a 65 31 36 2c 20 2d 31 &iPageSize16, -1
11cb2 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
11cb3 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
11cb4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
11cb5 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 !=SQLITE_OK || i
11cb6 50 61 67 65 53 69 7a 65 31 36 3d 3d 28 75 31 36 PageSize16==(u16
11cb7 29 69 50 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20 )iPageSize );..
11cb8 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
11cb9 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d assumed sector-
11cba 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68 size to match th
11cbb 65 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20 e value used by
11cbc 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 . ** the proc
11cbd 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64 ess that created
11cbe 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49 this journal. I
11cbf 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77 f this journal w
11cc0 61 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 as. ** create
11cc1 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f d by a process o
11cc2 74 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f ther than this o
11cc3 6e 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f ne, then this ro
11cc4 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20 utine. ** is
11cc5 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f being called fro
11cc6 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70 m within pager_p
11cc7 6c 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c layback(). The l
11cc8 6f 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a ocal value. *
11cc9 2a 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f * of Pager.secto
11cca 72 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65 rSize is restore
11ccb 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 d at the end of
11ccc 74 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20 that routine..
11ccd 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
11cce 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53 >sectorSize = iS
11ccf 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a ectorSize;. }..
11cd0 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11cd1 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f lOff += JOURNAL_
11cd2 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a HDR_SZ(pPager);.
11cd3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
11cd4 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 ./*.** Write the
11cd5 20 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72 supplied master
11cd6 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e journal name in
11cd7 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
11cd8 69 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a ile for pager.**
11cd9 20 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63 pPager at the c
11cda 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e urrent location.
11cdb 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 The master jour
11cdc 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 nal name must be
11cdd 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69 the last.** thi
11cde 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20 ng written to a
11cdf 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
11ce0 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
11ce1 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c full-sync mode,
11ce2 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
11ce3 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
11ce4 69 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74 is advanced to t
11ce5 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62 he next sector b
11ce6 6f 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a oundary before.*
11ce7 2a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72 * anything is wr
11ce8 69 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61 itten. The forma
11ce9 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 t is:.**.** +
11cea 34 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d 4 bytes: PAGER_M
11ceb 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e J_PGNO..** + N
11cec 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a bytes: Master j
11ced 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 ournal filename
11cee 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b in utf-8..** +
11cef 20 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e 4 bytes: N (len
11cf0 67 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f gth of master jo
11cf1 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79 urnal name in by
11cf2 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d tes, no nul-term
11cf3 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20 inator)..** +
11cf4 34 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 4 bytes: Master
11cf5 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 journal name che
11cf6 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20 cksum..** + 8
11cf7 62 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d bytes: aJournalM
11cf8 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 agic[]..**.** Th
11cf9 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
11cfa 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69 page checksum i
11cfb 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 s the sum of the
11cfc 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61 bytes in the ma
11cfd 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 ster.** journal
11cfe 6e 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68 name, where each
11cff 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 byte is interpr
11d00 65 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64 eted as a signed
11d01 20 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 8-bit integer..
11d02 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72 **.** If zMaster
11d03 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 is a NULL point
11d04 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61 er (occurs for a
11d05 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
11d06 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a transaction), .
11d07 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 ** this call is
11d08 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 a no-op..*/.stat
11d09 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74 ic int writeMast
11d0a 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 erJournal(Pager
11d0b 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 *pPager, const c
11d0c 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 har *zMaster){.
11d0d 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
11d0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d0f 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
11d10 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65 */. int nMaste
11d11 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
11d12 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 /* Length
11d13 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74 of string zMast
11d14 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 er */. i64 iHdr
11d15 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 Off;
11d16 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
11d17 65 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20 et of header in
11d18 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a journal file */.
11d19 20 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20 i64 jrnlSize;
11d1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11d1b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f /* Size of jo
11d1c 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69 urnal file on di
11d1d 73 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 sk */. u32 cksu
11d1e 6d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 m = 0;
11d1f 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 /* Chec
11d20 6b 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a ksum of string z
11d21 4d 61 73 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28 Master */.. if(
11d22 20 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 !zMaster || pPa
11d23 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20 ger->setMaster.
11d24 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 || pPager->jou
11d25 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
11d26 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f JOURNALMODE_MEMO
11d27 52 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72 RY . || pPager
11d28 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
11d29 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
11d2a 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72 _OFF . ){. r
11d2b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
11d2c 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 . }. pPager->s
11d2d 65 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 etMaster = 1;.
11d2e 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 assert( isOpen(p
11d2f 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a Pager->jfd) );..
11d30 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74 /* Calculate t
11d31 68 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74 he length in byt
11d32 65 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b es and the check
11d33 73 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a sum of zMaster *
11d34 2f 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d /. for(nMaster=
11d35 30 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74 0; zMaster[nMast
11d36 65 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b er]; nMaster++){
11d37 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d . cksum += zM
11d38 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a aster[nMaster];.
11d39 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 }.. /* If in
11d3a 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 full-sync mode,
11d3b 61 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e advance to the n
11d3c 65 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 ext disk sector
11d3d 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 before writing.
11d3e 20 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a ** the master j
11d3f 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 ournal name. Thi
11d40 73 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 s is in case the
11d41 20 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 previous page w
11d42 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 ritten to. ** t
11d43 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 he journal has a
11d44 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 lready been sync
11d45 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 ed.. */. if( p
11d46 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 Pager->fullSync
11d47 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a ){. pPager->j
11d48 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 ournalOff = jour
11d49 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 nalHdrOffset(pPa
11d4a 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72 ger);. }. iHdr
11d4b 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f Off = pPager->jo
11d4c 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20 urnalOff;.. /*
11d4d 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 Write the master
11d4e 20 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f journal data to
11d4f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
11d50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
11d51 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f . ** an error o
11d52 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68 ccurs, return th
11d53 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 e error code to
11d54 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f the caller.. */
11d55 0a 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63 . if( (0 != (rc
11d56 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
11d57 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 Pager->jfd, iHdr
11d58 4f 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47 Off, PAGER_MJ_PG
11d59 4e 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20 NO(pPager)))).
11d5a 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 || (0 != (rc =
11d5b 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 sqlite3OsWrite(p
11d5c 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 Pager->jfd, zMas
11d5d 74 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48 ter, nMaster, iH
11d5e 64 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c drOff+4))). ||
11d5f 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 (0 != (rc = wri
11d60 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d te32bits(pPager-
11d61 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b >jfd, iHdrOff+4+
11d62 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72 nMaster, nMaster
11d63 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 ))). || (0 !=
11d64 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 (rc = write32bit
11d65 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 s(pPager->jfd, i
11d66 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 HdrOff+4+nMaster
11d67 2b 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20 +4, cksum))).
11d68 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73 || (0 != (rc = s
11d69 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
11d6a 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72 ager->jfd, aJour
11d6b 6e 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64 nalMagic, 8, iHd
11d6c 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38 rOff+4+nMaster+8
11d6d 29 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 ))). ){. ret
11d6e 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 urn rc;. }. pP
11d6f 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
11d70 20 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29 += (nMaster+20)
11d71 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 ;. pPager->need
11d72 53 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e Sync = !pPager->
11d73 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66 noSync;.. /* If
11d74 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
11d75 20 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e peristent-journ
11d76 61 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 al mode, then th
11d77 65 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a e physical . **
11d78 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61 journal-file ma
11d79 79 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68 y extend past th
11d7a 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 e end of the mas
11d7b 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 ter-journal name
11d7c 0a 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65 . ** and 8 byte
11d7d 73 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20 s of magic data
11d7e 6a 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20 just written to
11d7f 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 the file. This i
11d80 73 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75 s . ** dangerou
11d81 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f s because the co
11d82 64 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 de to rollback a
11d83 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c hot-journal fil
11d84 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 e. ** will not
11d85 62 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20 be able to find
11d86 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e the master-journ
11d87 61 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 al name to deter
11d88 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68 mine . ** wheth
11d89 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f er or not the jo
11d8a 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20 urnal is hot. .
11d8b 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74 **. ** Easiest
11d8c 20 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20 thing to do in
11d8d 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73 this scenario is
11d8e 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65 to truncate the
11d8f 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 journal . ** f
11d90 69 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69 ile to the requi
11d91 72 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a red size.. */ .
11d92 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
11d93 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 =(rc = sqlite3Os
11d94 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d FileSize(pPager-
11d95 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29 >jfd, &jrnlSize)
11d96 29 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65 ). && jrnlSize
11d97 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c >pPager->journal
11d98 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 Off. ){. rc
11d99 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 = sqlite3OsTrunc
11d9a 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c ate(pPager->jfd,
11d9b 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
11d9c 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Off);. }. retu
11d9d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
11d9e 46 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 Find a page in t
11d9f 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 he hash table gi
11da0 76 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d ven its page num
11da1 62 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61 ber. Return.** a
11da2 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
11da3 70 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20 page or NULL if
11da4 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
11da5 67 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c ge is not .** al
11da6 72 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e ready in memory.
11da7 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72 .*/.static PgHdr
11da8 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50 *pager_lookup(P
11da9 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 ager *pPager, Pg
11daa 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 no pgno){. PgHd
11dab 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 r *p;
11dac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
11dad 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f Return value */
11dae 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 .. /* It is not
11daf 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20 possible for a
11db0 63 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65 call to PcacheFe
11db1 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74 tch() with creat
11db2 65 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a eFlag==0 to. **
11db3 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20 fail, since no
11db4 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 attempt to alloc
11db5 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f ate dynamic memo
11db6 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e ry will be made.
11db7 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71 . */. (void)sq
11db8 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 lite3PcacheFetch
11db9 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 (pPager->pPCache
11dba 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a , pgno, 0, &p);.
11dbb 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f return p;.}../
11dbc 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20 *.** Unless the
11dbd 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f pager is in erro
11dbe 72 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 r-state, discard
11dbf 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 all in-memory p
11dc0 61 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 ages. If.** the
11dc1 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f pager is in erro
11dc2 72 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68 r-state, then th
11dc3 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d is call is a no-
11dc4 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 op..**.** TODO:
11dc5 57 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72 Why can we not r
11dc6 65 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77 eset the pager w
11dc7 68 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74 hile in error st
11dc8 61 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ate?.*/.static v
11dc9 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 oid pager_reset(
11dca 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
11dcb 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d if( SQLITE_OK=
11dcc 3d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 =pPager->errCode
11dcd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 ){. sqlite3B
11dce 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61 ackupRestart(pPa
11dcf 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20 ger->pBackup);.
11dd0 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 sqlite3Pcache
11dd1 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50 Clear(pPager->pP
11dd2 43 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67 Cache);. pPag
11dd3 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
11dd4 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a = 0;. }.}../*.*
11dd5 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63 * Free all struc
11dd6 74 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67 tures in the Pag
11dd7 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 er.aSavepoint[]
11dd8 61 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f array and set bo
11dd9 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76 th.** Pager.aSav
11dda 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72 epoint and Pager
11ddb 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a .nSavepoint to z
11ddc 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73 ero. Close the s
11ddd 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66 ub-journal.** if
11dde 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 it is open and
11ddf 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74 the pager is not
11de0 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
11de1 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f de..*/.static vo
11de2 69 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 id releaseAllSav
11de3 65 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70 epoints(Pager *p
11de4 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69 Pager){. int ii
11de5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
11de6 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20 /* Iterator for
11de7 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 looping through
11de8 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 Pager.aSavepoint
11de9 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 */. for(ii=0;
11dea 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 ii<pPager->nSave
11deb 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 point; ii++){.
11dec 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
11ded 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 estroy(pPager->a
11dee 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 Savepoint[ii].pI
11def 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d nSavepoint);. }
11df0 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e . if( !pPager->
11df1 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c exclusiveMode ||
11df2 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 sqlite3IsMemJou
11df3 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66 rnal(pPager->sjf
11df4 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 d) ){. sqlite
11df5 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
11df6 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71 >sjfd);. }. sq
11df7 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 lite3_free(pPage
11df8 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a r->aSavepoint);.
11df9 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 pPager->aSavep
11dfa 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67 oint = 0;. pPag
11dfb 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d er->nSavepoint =
11dfc 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 0;. pPager->nS
11dfd 75 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a ubRec = 0;.}../*
11dfe 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20 .** Set the bit
11dff 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74 number pgno in t
11e00 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
11e01 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a t.pInSavepoint .
11e02 2a 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c ** bitvecs of al
11e03 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 l open savepoint
11e04 73 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 s. Return SQLITE
11e05 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75 _OK if successfu
11e06 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e l.** or SQLITE_N
11e07 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 OMEM if a malloc
11e08 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e failure occurs.
11e09 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
11e0a 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 ddToSavepointBit
11e0b 76 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67 vecs(Pager *pPag
11e0c 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a er, Pgno pgno){.
11e0d 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 int ii;
11e0e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
11e0f 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
11e10 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
11e11 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 _OK; /* Re
11e12 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 sult code */..
11e13 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 for(ii=0; ii<pPa
11e14 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b ger->nSavepoint;
11e15 20 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 ii++){. Page
11e16 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 rSavepoint *p =
11e17 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f &pPager->aSavepo
11e18 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 int[ii];. if(
11e19 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20 pgno<=p->nOrig
11e1a 29 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73 ){. rc |= s
11e1b 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
11e1c 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c p->pInSavepoint,
11e1d 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65 pgno);. te
11e1e 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 stcase( rc==SQLI
11e1f 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 TE_NOMEM );.
11e20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
11e21 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 LITE_OK || rc==S
11e22 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 QLITE_NOMEM );.
11e23 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
11e24 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 n rc;.}../*.** U
11e25 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 nlock the databa
11e26 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 66 75 se file. This fu
11e27 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f nction is a no-o
11e28 70 20 69 66 20 74 68 65 20 70 61 67 65 72 0a 2a p if the pager.*
11e29 2a 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76 * is in exclusiv
11e2a 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 e mode..**.** If
11e2b 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75 the pager is cu
11e2c 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72 rrently in error
11e2d 20 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20 state, discard
11e2e 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11e2f 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 6e .** the cache an
11e30 64 20 72 65 73 65 74 20 74 68 65 20 50 61 67 65 d reset the Page
11e31 72 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 65 r structure inte
11e32 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 74 rnal state. If t
11e33 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70 here is.** an op
11e34 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c en journal-file,
11e35 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 74 then the next t
11e36 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 ime a shared-loc
11e37 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a k is obtained.**
11e38 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 69 on the pager fi
11e39 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 61 le (by this or a
11e3a 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 ny other process
11e3b 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a ), it will be.**
11e3c 20 74 72 65 61 74 65 64 20 61 73 20 61 20 68 6f treated as a ho
11e3d 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f t-journal and ro
11e3e 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74 lled back..*/.st
11e3f 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f atic void pager_
11e40 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 unlock(Pager *pP
11e41 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50 ager){. if( !pP
11e42 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d ager->exclusiveM
11e43 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 ode ){. int r
11e44 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
11e45 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
11e46 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f n code */.. /
11e47 2a 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 * Always close t
11e48 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11e49 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 when dropping th
11e4a 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e e database lock.
11e4b 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 . ** Otherwis
11e4c 65 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 e, another conne
11e4d 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e ction with journ
11e4e 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d al_mode=delete m
11e4f 69 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 ight. ** dele
11e50 74 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 te the file out
11e51 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 from under us..
11e52 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
11e53 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3OsClose(pPager-
11e54 3e 6a 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 >jfd);. sqlit
11e55 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 e3BitvecDestroy(
11e56 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
11e57 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d al);. pPager-
11e58 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b >pInJournal = 0;
11e59 0a 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 . releaseAllS
11e5a 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 avepoints(pPager
11e5b 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 );.. /* If th
11e5c 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b e file is unlock
11e5d 65 64 2c 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73 ed, somebody els
11e5e 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 e might change i
11e5f 74 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 76 61 t. The. ** va
11e60 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 50 lues stored in P
11e61 61 67 65 72 2e 64 62 53 69 7a 65 20 65 74 63 2e ager.dbSize etc.
11e62 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 69 6e might become in
11e63 76 61 6c 69 64 20 69 66 0a 20 20 20 20 2a 2a 20 valid if. **
11e64 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 54 4f this happens. TO
11e65 44 4f 3a 20 52 65 61 6c 6c 79 2c 20 74 68 69 73 DO: Really, this
11e66 20 64 6f 65 73 6e 27 74 20 6e 65 65 64 20 74 6f doesn't need to
11e67 20 62 65 20 63 6c 65 61 72 65 64 0a 20 20 20 20 be cleared.
11e68 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 68 61 ** until the cha
11e69 6e 67 65 2d 63 6f 75 6e 74 65 72 20 63 68 65 63 nge-counter chec
11e6a 6b 20 66 61 69 6c 73 20 69 6e 20 50 61 67 65 72 k fails in Pager
11e6b 53 68 61 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20 SharedLock()..
11e6c 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d */. pPager-
11e6d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 >dbSizeValid = 0
11e6e 3b 0a 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e ;.. rc = osUn
11e6f 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c lock(pPager->fd,
11e70 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 NO_LOCK);. i
11e71 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70 f( rc ){. p
11e72 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d Pager->errCode =
11e73 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 rc;. }. I
11e74 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20 OTRACE(("UNLOCK
11e75 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a %p\n", pPager)).
11e76 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72 . /* If Pager
11e77 2e 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c .errCode is set,
11e78 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
11e79 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 the pager cache
11e7a 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a cannot be. *
11e7b 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74 * trusted. Now t
11e7c 68 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69 hat the pager fi
11e7d 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 le is unlocked,
11e7e 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
11e7f 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 the. ** cache
11e80 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65 can be discarde
11e81 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 d and the error
11e82 63 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61 code safely clea
11e83 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 red.. */.
11e84 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
11e85 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ode ){. if(
11e86 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
11e87 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 {. pPager
11e88 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 ->errCode = SQLI
11e89 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 TE_OK;. }.
11e8a 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 pager_reset
11e8b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a (pPager);. }.
11e8c 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 . pPager->cha
11e8d 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 ngeCountDone = 0
11e8e 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 ;. pPager->st
11e8f 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f ate = PAGER_UNLO
11e90 43 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a CK;. }.}../*.**
11e91 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 This function s
11e92 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 hould be called
11e93 77 68 65 6e 20 61 6e 20 49 4f 45 52 52 2c 20 43 when an IOERR, C
11e94 4f 52 52 55 50 54 20 6f 72 20 46 55 4c 4c 20 65 ORRUPT or FULL e
11e95 72 72 6f 72 0a 2a 2a 20 6d 61 79 20 68 61 76 65 rror.** may have
11e96 20 6f 63 63 75 72 72 65 64 2e 20 54 68 65 20 66 occurred. The f
11e97 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 irst argument is
11e98 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
11e99 65 20 70 61 67 65 72 20 0a 2a 2a 20 73 74 72 75 e pager .** stru
11e9a 63 74 75 72 65 2c 20 74 68 65 20 73 65 63 6f 6e cture, the secon
11e9b 64 20 74 68 65 20 65 72 72 6f 72 2d 63 6f 64 65 d the error-code
11e9c 20 61 62 6f 75 74 20 74 6f 20 62 65 20 72 65 74 about to be ret
11e9d 75 72 6e 65 64 20 62 79 20 61 20 70 61 67 65 72 urned by a pager
11e9e 20 0a 2a 2a 20 41 50 49 20 66 75 6e 63 74 69 6f .** API functio
11e9f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 n. The value ret
11ea0 75 72 6e 65 64 20 69 73 20 61 20 63 6f 70 79 20 urned is a copy
11ea1 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 of the second ar
11ea2 67 75 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 gument .** to th
11ea3 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a is function. .**
11ea4 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e .** If the secon
11ea5 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 d argument is SQ
11ea6 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 LITE_IOERR, SQLI
11ea7 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 20 53 TE_CORRUPT, or S
11ea8 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 QLITE_FULL.** th
11ea9 65 20 65 72 72 6f 72 20 62 65 63 6f 6d 65 73 20 e error becomes
11eaa 70 65 72 73 69 73 74 65 6e 74 2e 20 55 6e 74 69 persistent. Unti
11eab 6c 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 20 l the persisten
11eac 65 72 72 6f 72 20 69 73 20 63 6c 65 61 72 65 64 error is cleared
11ead 2c 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74 20 ,.** subsequent
11eae 41 50 49 20 63 61 6c 6c 73 20 6f 6e 20 74 68 69 API calls on thi
11eaf 73 20 50 61 67 65 72 20 77 69 6c 6c 20 69 6d 6d s Pager will imm
11eb0 65 64 69 61 74 65 6c 79 20 72 65 74 75 72 6e 20 ediately return
11eb1 74 68 65 20 73 61 6d 65 20 0a 2a 2a 20 65 72 72 the same .** err
11eb2 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 or code..**.** A
11eb3 20 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f persistent erro
11eb4 72 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 r indicates that
11eb5 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
11eb6 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 the pager-cache
11eb7 20 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 74 .** cannot be t
11eb8 72 75 73 74 65 64 2e 20 54 68 69 73 20 73 74 61 rusted. This sta
11eb9 74 65 20 63 61 6e 20 62 65 20 63 6c 65 61 72 65 te can be cleare
11eba 64 20 62 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 d by completely
11ebb 64 69 73 63 61 72 64 69 6e 67 20 0a 2a 2a 20 74 discarding .** t
11ebc 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
11ebd 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 2e 20 he pager-cache.
11ebe 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e If a transaction
11ebf 20 77 61 73 20 61 63 74 69 76 65 20 77 68 65 6e was active when
11ec0 0a 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 .** the persiste
11ec1 6e 74 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 nt error occurre
11ec2 64 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c d, then the roll
11ec3 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 back journal may
11ec4 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 72 need.** to be r
11ec5 65 70 6c 61 79 65 64 20 74 6f 20 72 65 73 74 6f eplayed to resto
11ec6 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 re the contents
11ec7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
11ec8 66 69 6c 65 20 28 61 73 20 69 66 0a 2a 2a 20 69 file (as if.** i
11ec9 74 20 77 65 72 65 20 61 20 68 6f 74 2d 6a 6f 75 t were a hot-jou
11eca 72 6e 61 6c 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 rnal)..*/.static
11ecb 20 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f 72 int pager_error
11ecc 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
11ecd 69 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 int rc){. int r
11ece 63 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b 0a c2 = rc & 0xff;.
11ecf 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
11ed0 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44 LITE_OK || !MEMD
11ed1 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 0a 20 B );. assert(.
11ed2 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 pPager->er
11ed3 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 46 55 rCode==SQLITE_FU
11ed4 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 70 50 61 LL ||. pPa
11ed5 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 ger->errCode==SQ
11ed6 4c 49 54 45 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 LITE_OK ||.
11ed7 20 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f (pPager->errCo
11ed8 64 65 20 26 20 30 78 66 66 29 3d 3d 53 51 4c 49 de & 0xff)==SQLI
11ed9 54 45 5f 49 4f 45 52 52 0a 20 20 29 3b 0a 20 20 TE_IOERR. );.
11eda 69 66 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f if( rc2==SQLITE_
11edb 46 55 4c 4c 20 7c 7c 20 72 63 32 3d 3d 53 51 4c FULL || rc2==SQL
11edc 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 ITE_IOERR ){.
11edd 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11ede 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 72 65 74 = rc;. }. ret
11edf 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
11ee0 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62 Execute a rollb
11ee1 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63 ack if a transac
11ee2 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 tion is active a
11ee3 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a nd unlock the .*
11ee4 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e * database file.
11ee5 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 .**.** If the p
11ee6 61 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 ager has already
11ee7 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 entered the err
11ee8 6f 72 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 or state, do not
11ee9 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 74 68 65 attempt .** the
11eea 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 74 68 69 rollback at thi
11eeb 73 20 74 69 6d 65 2e 20 49 6e 73 74 65 61 64 2c s time. Instead,
11eec 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 pager_unlock()
11eed 69 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 0a 2a is called. The.*
11eee 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f * call to pager_
11eef 75 6e 6c 6f 63 6b 28 29 20 77 69 6c 6c 20 64 69 unlock() will di
11ef0 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d scard all in-mem
11ef1 6f 72 79 20 70 61 67 65 73 2c 20 75 6e 6c 6f 63 ory pages, unloc
11ef2 6b 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 k.** the databas
11ef3 65 20 66 69 6c 65 20 61 6e 64 20 63 6c 65 61 72 e file and clear
11ef4 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
11ef5 2e 20 49 66 20 74 68 69 73 20 6d 65 61 6e 73 20 . If this means
11ef6 74 68 61 74 0a 2a 2a 20 74 68 65 72 65 20 69 73 that.** there is
11ef7 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c a hot-journal l
11ef8 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c 65 2d eft in the file-
11ef9 73 79 73 74 65 6d 2c 20 74 68 65 20 6e 65 78 74 system, the next
11efa 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 74 connection.** t
11efb 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65 o obtain a share
11efc 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 70 61 d lock on the pa
11efd 67 65 72 20 28 77 68 69 63 68 20 6d 61 79 20 62 ger (which may b
11efe 65 20 74 68 69 73 20 6f 6e 65 29 20 77 69 6c 6c e this one) will
11eff 0a 2a 2a 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b .** roll it back
11f00 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
11f01 61 67 65 72 20 68 61 73 20 6e 6f 74 20 61 6c 72 ager has not alr
11f02 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 eady entered the
11f03 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 62 75 error state, bu
11f04 74 20 61 6e 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 t an IO or.** ma
11f05 6c 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 lloc error occur
11f06 73 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 s during a rollb
11f07 61 63 6b 2c 20 74 68 65 6e 20 74 68 69 73 20 77 ack, then this w
11f08 69 6c 6c 20 69 74 73 65 6c 66 20 63 61 75 73 65 ill itself cause
11f09 20 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 74 .** the pager t
11f0a 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f o enter the erro
11f0b 72 20 73 74 61 74 65 2e 20 57 68 69 63 68 20 77 r state. Which w
11f0c 69 6c 6c 20 62 65 20 63 6c 65 61 72 65 64 20 62 ill be cleared b
11f0d 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f y the.** call to
11f0e 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c pager_unlock(),
11f0f 20 61 73 20 64 65 73 63 72 69 62 65 64 20 61 62 as described ab
11f10 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ove..*/.static v
11f11 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 oid pagerUnlockA
11f12 6e 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 ndRollback(Pager
11f13 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 *pPager){. if(
11f14 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
11f15 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 ==SQLITE_OK && p
11f16 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
11f17 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a GER_RESERVED ){.
11f18 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e sqlite3Begin
11f19 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
11f1a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
11f1b 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 Rollback(pPager)
11f1c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 ;. sqlite3End
11f1d 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a BenignMalloc();.
11f1e 20 20 7d 0a 20 20 70 61 67 65 72 5f 75 6e 6c 6f }. pager_unlo
11f1f 63 6b 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f ck(pPager);.}../
11f20 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
11f21 65 20 65 6e 64 73 20 61 20 74 72 61 6e 73 61 63 e ends a transac
11f22 74 69 6f 6e 2e 20 41 20 74 72 61 6e 73 61 63 74 tion. A transact
11f23 69 6f 6e 20 69 73 20 75 73 75 61 6c 6c 79 20 65 ion is usually e
11f24 6e 64 65 64 20 62 79 20 0a 2a 2a 20 65 69 74 68 nded by .** eith
11f25 65 72 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 er a COMMIT or a
11f26 20 52 4f 4c 4c 42 41 43 4b 20 6f 70 65 72 61 74 ROLLBACK operat
11f27 69 6f 6e 2e 20 54 68 69 73 20 72 6f 75 74 69 6e ion. This routin
11f28 65 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 e may be called
11f29 0a 2a 2a 20 61 66 74 65 72 20 72 6f 6c 6c 62 61 .** after rollba
11f2a 63 6b 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 ck of a hot-jour
11f2b 6e 61 6c 2c 20 6f 72 20 69 66 20 61 6e 20 65 72 nal, or if an er
11f2c 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 ror occurs while
11f2d 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 opening.** the
11f2e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 journal file or
11f2f 77 72 69 74 69 6e 67 20 74 68 65 20 76 65 72 79 writing the very
11f30 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 first journal-h
11f31 65 61 64 65 72 20 6f 66 20 61 0a 2a 2a 20 64 61 eader of a.** da
11f32 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 tabase transacti
11f33 6f 6e 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 on..** .** If th
11f34 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 50 41 e pager is in PA
11f35 47 45 52 5f 53 48 41 52 45 44 20 6f 72 20 50 41 GER_SHARED or PA
11f36 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 GER_UNLOCK state
11f37 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f when this.** ro
11f38 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
11f39 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 20 28 it is a no-op (
11f3a 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f returns SQLITE_O
11f3b 4b 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 K)..**.** Otherw
11f3c 69 73 65 2c 20 61 6e 79 20 61 63 74 69 76 65 20 ise, any active
11f3d 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 72 savepoints are r
11f3e 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 eleased..**.** I
11f3f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
11f40 6c 65 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 6e le is open, then
11f41 20 69 74 20 69 73 20 22 66 69 6e 61 6c 69 7a 65 it is "finalize
11f42 64 22 2e 20 4f 6e 63 65 20 61 20 6a 6f 75 72 6e d". Once a journ
11f43 61 6c 20 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 al .** file has
11f44 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 20 69 been finalized i
11f45 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c t is not possibl
11f46 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f 20 72 e to use it to r
11f47 6f 6c 6c 20 62 61 63 6b 20 61 20 0a 2a 2a 20 74 oll back a .** t
11f48 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4e 6f 72 20 ransaction. Nor
11f49 77 69 6c 6c 20 69 74 20 62 65 20 63 6f 6e 73 69 will it be consi
11f4a 64 65 72 65 64 20 74 6f 20 62 65 20 61 20 68 6f dered to be a ho
11f4b 74 2d 6a 6f 75 72 6e 61 6c 20 62 79 20 74 68 69 t-journal by thi
11f4c 73 0a 2a 2a 20 6f 72 20 61 6e 79 20 6f 74 68 65 s.** or any othe
11f4d 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 r database conne
11f4e 63 74 69 6f 6e 2e 20 45 78 61 63 74 6c 79 20 68 ction. Exactly h
11f4f 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 ow a journal is
11f50 66 69 6e 61 6c 69 7a 65 64 0a 2a 2a 20 64 65 70 finalized.** dep
11f51 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72 20 ends on whether
11f52 6f 72 20 6e 6f 74 20 74 68 65 20 70 61 67 65 72 or not the pager
11f53 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 is running in e
11f54 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e xclusive mode an
11f55 64 0a 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 d.** the current
11f56 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 28 50 journal-mode (P
11f57 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ager.journalMode
11f58 20 76 61 6c 75 65 29 2c 20 61 73 20 66 6f 6c 6c value), as foll
11f59 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 ows:.**.** jou
11f5a 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 rnalMode==MEMORY
11f5b 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 .** Journal
11f5c 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 file descriptor
11f5d 69 73 20 73 69 6d 70 6c 79 20 63 6c 6f 73 65 64 is simply closed
11f5e 2e 20 54 68 69 73 20 64 65 73 74 72 6f 79 73 20 . This destroys
11f5f 61 6e 20 0a 2a 2a 20 20 20 20 20 69 6e 2d 6d 65 an .** in-me
11f60 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a mory journal..**
11f61 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 .** journalMod
11f62 65 3d 3d 54 52 55 4e 43 41 54 45 0a 2a 2a 20 20 e==TRUNCATE.**
11f63 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 Journal file
11f64 69 73 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 is truncated to
11f65 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 zero bytes in si
11f66 7a 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 ze..**.** jour
11f67 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 nalMode==PERSIST
11f68 0a 2a 2a 20 20 20 20 20 54 68 65 20 66 69 72 73 .** The firs
11f69 74 20 32 38 20 62 79 74 65 73 20 6f 66 20 74 68 t 28 bytes of th
11f6a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 e journal file a
11f6b 72 65 20 7a 65 72 6f 65 64 2e 20 54 68 69 73 20 re zeroed. This
11f6c 69 6e 76 61 6c 69 64 61 74 65 73 0a 2a 2a 20 20 invalidates.**
11f6d 20 20 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 the first jou
11f6e 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6e 20 74 rnal header in t
11f6f 68 65 20 66 69 6c 65 2c 20 61 6e 64 20 68 65 6e he file, and hen
11f70 63 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f ce the entire jo
11f71 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c urnal.** fil
11f72 65 2e 20 41 6e 20 69 6e 76 61 6c 69 64 20 6a 6f e. An invalid jo
11f73 75 72 6e 61 6c 20 66 69 6c 65 20 63 61 6e 6e 6f urnal file canno
11f74 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b t be rolled back
11f75 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 ..**.** journa
11f76 6c 4d 6f 64 65 3d 3d 44 45 4c 45 54 45 0a 2a 2a lMode==DELETE.**
11f77 20 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c The journal
11f78 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 20 file is closed
11f79 61 6e 64 20 64 65 6c 65 74 65 64 20 75 73 69 6e and deleted usin
11f7a 67 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 g sqlite3OsDelet
11f7b 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 e()..**.** I
11f7c 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 72 f the pager is r
11f7d 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 unning in exclus
11f7e 69 76 65 20 6d 6f 64 65 2c 20 74 68 69 73 20 6d ive mode, this m
11f7f 65 74 68 6f 64 20 6f 66 20 66 69 6e 61 6c 69 7a ethod of finaliz
11f80 69 6e 67 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a ing.** the j
11f81 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e ournal file is n
11f82 65 76 65 72 20 75 73 65 64 2e 20 49 6e 73 74 65 ever used. Inste
11f83 61 64 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e ad, if the journ
11f84 61 6c 4d 6f 64 65 20 69 73 0a 2a 2a 20 20 20 20 alMode is.**
11f85 20 44 45 4c 45 54 45 20 61 6e 64 20 74 68 65 20 DELETE and the
11f86 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c pager is in excl
11f87 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 20 usive mode, the
11f88 6d 65 74 68 6f 64 20 64 65 73 63 72 69 62 65 64 method described
11f89 20 75 6e 64 65 72 0a 2a 2a 20 20 20 20 20 6a 6f under.** jo
11f8a 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 urnalMode==PERSI
11f8b 53 54 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 ST is used inste
11f8c 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 ad..**.** After
11f8d 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 the journal is f
11f8e 69 6e 61 6c 69 7a 65 64 2c 20 69 66 20 72 75 6e inalized, if run
11f8f 6e 69 6e 67 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c ning in non-excl
11f90 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 0a usive mode, the.
11f91 2a 2a 20 70 61 67 65 72 20 6d 6f 76 65 73 20 74 ** pager moves t
11f92 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 73 o PAGER_SHARED s
11f93 74 61 74 65 20 28 61 6e 64 20 64 6f 77 6e 67 72 tate (and downgr
11f94 61 64 65 73 20 74 68 65 20 6c 6f 63 6b 20 6f 6e ades the lock on
11f95 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
11f96 20 66 69 6c 65 20 61 63 63 6f 72 64 69 6e 67 6c file accordingl
11f97 79 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 y)..**.** If the
11f98 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e pager is runnin
11f99 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d g in exclusive m
11f9a 6f 64 65 20 61 6e 64 20 69 73 20 69 6e 20 50 41 ode and is in PA
11f9b 47 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 GER_SYNCED state
11f9c 2c 0a 2a 2a 20 69 74 20 6d 6f 76 65 73 20 74 6f ,.** it moves to
11f9d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
11f9e 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 64 . No locks are d
11f9f 6f 77 6e 67 72 61 64 65 64 20 77 68 65 6e 20 72 owngraded when r
11fa0 75 6e 6e 69 6e 67 20 69 6e 0a 2a 2a 20 65 78 63 unning in.** exc
11fa1 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a lusive mode..**.
11fa2 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 ** SQLITE_OK is
11fa3 72 65 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 returned if no e
11fa4 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 49 66 20 rror occurs. If
11fa5 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 an error occurs
11fa6 64 75 72 69 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 during.** any of
11fa7 20 74 68 65 20 49 4f 20 6f 70 65 72 61 74 69 6f the IO operatio
11fa8 6e 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 ns to finalize t
11fa9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
11faa 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a or unlock the.**
11fab 20 64 61 74 61 62 61 73 65 20 74 68 65 6e 20 74 database then t
11fac 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 he IO error code
11fad 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 is returned to
11fae 74 68 65 20 75 73 65 72 2e 20 49 66 20 74 68 65 the user. If the
11faf 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 .** operation t
11fb0 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a o finalize the j
11fb1 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c ournal file fail
11fb2 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 64 65 s, then the code
11fb3 20 73 74 69 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 still.** tries
11fb4 74 6f 20 75 6e 6c 6f 63 6b 20 74 68 65 20 64 61 to unlock the da
11fb5 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 6e tabase file if n
11fb6 6f 74 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 ot in exclusive
11fb7 6d 6f 64 65 2e 20 49 66 20 74 68 65 0a 2a 2a 20 mode. If the.**
11fb8 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69 6f 6e unlock operation
11fb9 20 66 61 69 6c 73 20 61 73 20 77 65 6c 6c 2c 20 fails as well,
11fba 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 65 then the first e
11fbb 72 72 6f 72 20 63 6f 64 65 20 72 65 6c 61 74 65 rror code relate
11fbc 64 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 d.** to the firs
11fbd 74 20 65 72 72 6f 72 20 65 6e 63 6f 75 6e 74 65 t error encounte
11fbe 72 65 64 20 28 74 68 65 20 6a 6f 75 72 6e 61 6c red (the journal
11fbf 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e finalization on
11fc0 65 29 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 e) is.** returne
11fc1 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 d..*/.static int
11fc2 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 pager_end_trans
11fc3 61 63 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 action(Pager *pP
11fc4 61 67 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 ager, int hasMas
11fc5 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ter){. int rc =
11fc6 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
11fc7 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 /* Error code f
11fc8 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 rom journal fina
11fc9 6c 69 7a 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 lization operati
11fca 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 32 20 on */. int rc2
11fcb 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 = SQLITE_OK;
11fcc 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 /* Error code f
11fcd 72 6f 6d 20 64 62 20 66 69 6c 65 20 75 6e 6c 6f rom db file unlo
11fce 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a ck operation */.
11fcf 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
11fd0 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 tate<PAGER_RESER
11fd1 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 VED ){. retur
11fd2 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
11fd3 0a 20 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76 . releaseAllSav
11fd4 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72 29 3b epoints(pPager);
11fd5 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 .. assert( isOp
11fd6 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
11fd7 7c 7c 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f || pPager->pInJo
11fd8 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 urnal==0 );. if
11fd9 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
11fda 3e 6a 66 64 29 20 29 7b 0a 0a 20 20 20 20 2f 2a >jfd) ){.. /*
11fdb 20 46 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f Finalize the jo
11fdc 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 urnal file. */.
11fdd 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 if( sqlite3Is
11fde 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 MemJournal(pPage
11fdf 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 r->jfd) ){.
11fe0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
11fe1 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
11fe2 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
11fe3 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 20 20 MEMORY );.
11fe4 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 sqlite3OsClose(p
11fe5 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 Pager->jfd);.
11fe6 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 }else if( pPage
11fe7 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11fe8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11fe9 45 5f 54 52 55 4e 43 41 54 45 20 29 7b 0a 20 20 E_TRUNCATE ){.
11fea 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e if( pPager->
11feb 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b journalOff==0 ){
11fec 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
11fed 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d LITE_OK;. }
11fee 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 else{. rc
11fef 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e = sqlite3OsTrun
11ff0 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 cate(pPager->jfd
11ff1 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 , 0);. }.
11ff2 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
11ff3 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 nalOff = 0;.
11ff4 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
11ff5 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 lStarted = 0;.
11ff6 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 }else if( pPag
11ff7 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
11ff8 65 20 0a 20 20 20 20 20 7c 7c 20 70 50 61 67 65 e . || pPage
11ff9 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d r->journalMode==
11ffa 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
11ffb 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20 29 7b E_PERSIST. ){
11ffc 0a 20 20 20 20 20 20 72 63 20 3d 20 7a 65 72 6f . rc = zero
11ffd 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 JournalHdr(pPage
11ffe 72 2c 20 68 61 73 4d 61 73 74 65 72 29 3b 0a 20 r, hasMaster);.
11fff 20 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 pager_error
12000 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 (pPager, rc);.
12001 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
12002 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 nalOff = 0;.
12003 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
12004 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 lStarted = 0;.
12005 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f }else{. /
12006 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 6d 61 * This branch ma
12007 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69 y be executed wi
12008 74 68 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c th Pager.journal
12009 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a Mode==MEMORY if.
1200a 20 20 20 20 20 20 2a 2a 20 61 20 68 6f 74 2d 6a ** a hot-j
1200b 6f 75 72 6e 61 6c 20 77 61 73 20 6a 75 73 74 20 ournal was just
1200c 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 6e 20 rolled back. In
1200d 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f this case the jo
1200e 75 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 66 urnal. ** f
1200f 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c ile should be cl
12010 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65 64 osed and deleted
12011 2e 20 49 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 . If this connec
12012 74 69 6f 6e 20 77 72 69 74 65 73 20 74 6f 0a 20 tion writes to.
12013 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 ** the data
12014 62 61 73 65 20 66 69 6c 65 2c 20 69 74 20 77 69 base file, it wi
12015 6c 6c 20 64 6f 20 73 6f 20 75 73 69 6e 67 20 61 ll do so using a
12016 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 n in-memory jour
12017 6e 61 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61 nal. */. a
12018 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a ssert( pPager->j
12019 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
1201a 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 R_JOURNALMODE_DE
1201b 4c 45 54 45 20 0a 20 20 20 20 20 20 20 20 20 20 LETE .
1201c 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 || pPager->jour
1201d 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a nalMode==PAGER_J
1201e 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 OURNALMODE_MEMOR
1201f 59 20 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 Y . );.
12020 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 sqlite3OsClose
12021 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 (pPager->jfd);.
12022 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72 if( !pPager
12023 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 ->tempFile ){.
12024 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12025 65 33 4f 73 44 65 6c 65 74 65 28 70 50 61 67 65 e3OsDelete(pPage
12026 72 2d 3e 70 56 66 73 2c 20 70 50 61 67 65 72 2d r->pVfs, pPager-
12027 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 >zJournal, 0);.
12028 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 }. }..#i
12029 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 fdef SQLITE_CHEC
1202a 4b 5f 50 41 47 45 53 0a 20 20 20 20 73 71 6c 69 K_PAGES. sqli
1202b 74 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 te3PcacheIterate
1202c 44 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 Dirty(pPager->pP
1202d 43 61 63 68 65 2c 20 70 61 67 65 72 5f 73 65 74 Cache, pager_set
1202e 5f 70 61 67 65 68 61 73 68 29 3b 0a 23 65 6e 64 _pagehash);.#end
1202f 69 66 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 if.. sqlite3P
12030 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50 cacheCleanAll(pP
12031 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
12032 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 sqlite3Bitve
12033 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d cDestroy(pPager-
12034 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 >pInJournal);.
12035 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 pPager->pInJou
12036 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 70 50 rnal = 0;. pP
12037 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a ager->nRec = 0;.
12038 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 }.. if( !pPag
12039 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 er->exclusiveMod
1203a 65 20 29 7b 0a 20 20 20 20 72 63 32 20 3d 20 6f e ){. rc2 = o
1203b 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e sUnlock(pPager->
1203c 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 fd, SHARED_LOCK)
1203d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 ;. pPager->st
1203e 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 ate = PAGER_SHAR
1203f 45 44 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ED;. pPager->
12040 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
12041 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 = 0;. }else if(
12042 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d pPager->state==
12043 50 41 47 45 52 5f 53 59 4e 43 45 44 20 29 7b 0a PAGER_SYNCED ){.
12044 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 pPager->stat
12045 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 e = PAGER_EXCLUS
12046 49 56 45 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 IVE;. }. pPage
12047 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 r->setMaster = 0
12048 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 ;. pPager->need
12049 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61 67 Sync = 0;. pPag
1204a 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d er->dbModified =
1204b 20 30 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 0;.. /* TODO:
1204c 49 73 20 74 68 69 73 20 6f 70 74 69 6d 61 6c 3f Is this optimal?
1204d 20 57 68 79 20 69 73 20 74 68 65 20 64 62 20 73 Why is the db s
1204e 69 7a 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 ize invalidated
1204f 68 65 72 65 20 0a 20 20 2a 2a 20 77 68 65 6e 20 here . ** when
12050 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
12051 65 20 69 73 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 e is not unlocke
12052 64 3f 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e d? */. pPager->
12053 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 30 3b 0a dbOrigSize = 0;.
12054 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 sqlite3PcacheT
12055 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e runcate(pPager->
12056 70 50 43 61 63 68 65 2c 20 70 50 61 67 65 72 2d pPCache, pPager-
12057 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 >dbSize);. if(
12058 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70 50 !MEMDB ){. pP
12059 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 ager->dbSizeVali
1205a 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 d = 0;. }.. re
1205b 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 turn (rc==SQLITE
1205c 5f 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a _OK?rc2:rc);.}..
1205d 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 /*.** Parameter
1205e 61 44 61 74 61 20 6d 75 73 74 20 70 6f 69 6e 74 aData must point
1205f 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66 20 to a buffer of
12060 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12061 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 bytes.** of dat
12062 61 2e 20 43 6f 6d 70 75 74 65 20 61 6e 64 20 72 a. Compute and r
12063 65 74 75 72 6e 20 61 20 63 68 65 63 6b 73 75 6d eturn a checksum
12064 20 62 61 73 65 64 20 6f 6e 74 20 74 68 65 20 63 based ont the c
12065 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 0a ontents of the .
12066 2a 2a 20 70 61 67 65 20 6f 66 20 64 61 74 61 20 ** page of data
12067 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 and the current
12068 76 61 6c 75 65 20 6f 66 20 70 50 61 67 65 72 2d value of pPager-
12069 3e 63 6b 73 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a >cksumInit..**.*
1206a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61 20 * This is not a
1206b 72 65 61 6c 20 63 68 65 63 6b 73 75 6d 2e 20 49 real checksum. I
1206c 74 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 t is really just
1206d 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 the sum of the
1206e 0a 2a 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 .** random initi
1206f 61 6c 20 76 61 6c 75 65 20 28 70 50 61 67 65 72 al value (pPager
12070 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 20 61 6e 64 ->cksumInit) and
12071 20 65 76 65 72 79 20 32 30 30 74 68 20 62 79 74 every 200th byt
12072 65 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 e.** of the page
12073 20 64 61 74 61 2c 20 73 74 61 72 74 69 6e 67 20 data, starting
12074 77 69 74 68 20 62 79 74 65 20 6f 66 66 73 65 74 with byte offset
12075 20 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 (pPager->pageSi
12076 7a 65 25 32 30 30 29 2e 0a 2a 2a 20 45 61 63 68 ze%200)..** Each
12077 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72 byte is interpr
12078 65 74 65 64 20 61 73 20 61 6e 20 38 2d 62 69 74 eted as an 8-bit
12079 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 unsigned intege
1207a 72 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e r..**.** Changin
1207b 67 20 74 68 65 20 66 6f 72 6d 75 6c 61 20 75 73 g the formula us
1207c 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 ed to compute th
1207d 69 73 20 63 68 65 63 6b 73 75 6d 20 72 65 73 75 is checksum resu
1207e 6c 74 73 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 lts in an.** inc
1207f 6f 6d 70 61 74 69 62 6c 65 20 6a 6f 75 72 6e 61 ompatible journa
12080 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a l file format..*
12081 2a 0a 2a 2a 20 49 66 20 6a 6f 75 72 6e 61 6c 20 *.** If journal
12082 63 6f 72 72 75 70 74 69 6f 6e 20 6f 63 63 75 72 corruption occur
12083 73 20 64 75 65 20 74 6f 20 61 20 70 6f 77 65 72 s due to a power
12084 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20 6d 6f failure, the mo
12085 73 74 20 6c 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 st likely .** sc
12086 65 6e 61 72 69 6f 20 69 73 20 74 68 61 74 20 6f enario is that o
12087 6e 65 20 65 6e 64 20 6f 72 20 74 68 65 20 6f 74 ne end or the ot
12088 68 65 72 20 6f 66 20 74 68 65 20 72 65 63 6f 72 her of the recor
12089 64 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67 65 d will be change
1208a 64 2e 20 0a 2a 2a 20 49 74 20 69 73 20 6d 75 63 d. .** It is muc
1208b 68 20 6c 65 73 73 20 6c 69 6b 65 6c 79 20 74 68 h less likely th
1208c 61 74 20 74 68 65 20 74 77 6f 20 65 6e 64 73 20 at the two ends
1208d 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 of the journal r
1208e 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 0a 2a 2a ecord will be.**
1208f 20 63 6f 72 72 65 63 74 20 61 6e 64 20 74 68 65 correct and the
12090 20 6d 69 64 64 6c 65 20 62 65 20 63 6f 72 72 75 middle be corru
12091 70 74 2e 20 20 54 68 75 73 2c 20 74 68 69 73 20 pt. Thus, this
12092 22 63 68 65 63 6b 73 75 6d 22 20 73 63 68 65 6d "checksum" schem
12093 65 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 66 61 73 e,.** though fas
12094 74 20 61 6e 64 20 73 69 6d 70 6c 65 2c 20 63 61 t and simple, ca
12095 74 63 68 65 73 20 74 68 65 20 6d 6f 73 74 6c 79 tches the mostly
12096 20 6c 69 6b 65 6c 79 20 6b 69 6e 64 20 6f 66 20 likely kind of
12097 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 corruption..*/.s
12098 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f tatic u32 pager_
12099 63 6b 73 75 6d 28 50 61 67 65 72 20 2a 70 50 61 cksum(Pager *pPa
1209a 67 65 72 2c 20 63 6f 6e 73 74 20 75 38 20 2a 61 ger, const u8 *a
1209b 44 61 74 61 29 7b 0a 20 20 75 33 32 20 63 6b 73 Data){. u32 cks
1209c 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 um = pPager->cks
1209d 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 umInit;
1209e 2f 2a 20 43 68 65 63 6b 73 75 6d 20 76 61 6c 75 /* Checksum valu
1209f 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 e to return */.
120a0 20 69 6e 74 20 69 20 3d 20 70 50 61 67 65 72 2d int i = pPager-
120a1 3e 70 61 67 65 53 69 7a 65 2d 32 30 30 3b 20 20 >pageSize-200;
120a2 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
120a3 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 77 68 69 counter */. whi
120a4 6c 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 le( i>0 ){. c
120a5 6b 73 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d ksum += aData[i]
120a6 3b 0a 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a ;. i -= 200;.
120a7 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 }. return cks
120a8 75 6d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 um;.}../*.** Rea
120a9 64 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 d a single page
120aa 66 72 6f 6d 20 65 69 74 68 65 72 20 74 68 65 20 from either the
120ab 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 journal file (if
120ac 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 isMainJrnl==1)
120ad 6f 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 or.** from the s
120ae 75 62 2d 6a 6f 75 72 6e 61 6c 20 28 69 66 20 69 ub-journal (if i
120af 73 4d 61 69 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e sMainJrnl==0) an
120b0 64 20 70 6c 61 79 62 61 63 6b 20 74 68 61 74 20 d playback that
120b1 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 70 61 67 page..** The pag
120b2 65 20 62 65 67 69 6e 73 20 61 74 20 6f 66 66 73 e begins at offs
120b3 65 74 20 2a 70 4f 66 66 73 65 74 20 69 6e 74 6f et *pOffset into
120b4 20 74 68 65 20 66 69 6c 65 2e 20 54 68 65 20 2a the file. The *
120b5 70 4f 66 66 73 65 74 0a 2a 2a 20 76 61 6c 75 65 pOffset.** value
120b6 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 74 6f is increased to
120b7 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 the start of th
120b8 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 e next page in t
120b9 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a he journal..**.*
120ba 2a 20 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c * The isMainJrnl
120bb 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 flag is true if
120bc 20 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 this is the mai
120bd 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e n rollback journ
120be 61 6c 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 al and.** false
120bf 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e for the statemen
120c0 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 t journal. The
120c1 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f main rollback jo
120c2 75 72 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 urnal uses.** ch
120c3 65 63 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 ecksums - the st
120c4 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 atement journal
120c5 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 does not..**.**
120c6 49 66 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 If the page numb
120c7 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 72 er of the page r
120c8 65 63 6f 72 64 20 72 65 61 64 20 66 72 6f 6d 20 ecord read from
120c9 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61 the (sub-)journa
120ca 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 67 72 65 l file.** is gre
120cb 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 ater than the cu
120cc 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 rrent value of P
120cd 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 65 ager.dbSize, the
120ce 6e 20 70 6c 61 79 62 61 63 6b 20 69 73 0a 2a 2a n playback is.**
120cf 20 73 6b 69 70 70 65 64 20 61 6e 64 20 53 51 4c skipped and SQL
120d0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
120d1 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f ed..**.** If pDo
120d2 6e 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 ne is not NULL,
120d3 74 68 65 6e 20 69 74 20 69 73 20 61 20 72 65 63 then it is a rec
120d4 6f 72 64 20 6f 66 20 70 61 67 65 73 20 74 68 61 ord of pages tha
120d5 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a t have already.*
120d6 2a 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 * been played ba
120d7 63 6b 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 ck. If the page
120d8 20 61 74 20 2a 70 4f 66 66 73 65 74 20 68 61 73 at *pOffset has
120d9 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 6c already been pl
120da 61 79 65 64 20 62 61 63 6b 0a 2a 2a 20 28 69 66 ayed back.** (if
120db 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
120dc 6e 67 20 70 44 6f 6e 65 20 62 69 74 20 69 73 20 ng pDone bit is
120dd 73 65 74 29 20 74 68 65 6e 20 73 6b 69 70 20 74 set) then skip t
120de 68 65 20 70 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 he playback..**
120df 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 44 Make sure the pD
120e0 6f 6e 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f one bit correspo
120e1 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 2a 70 4f nding to the *pO
120e2 66 66 73 65 74 20 70 61 67 65 20 69 73 20 73 65 ffset page is se
120e3 74 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 t.** prior to re
120e4 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 turning..**.** I
120e5 66 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72 f the page recor
120e6 64 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c d is successfull
120e7 79 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 y read from the
120e8 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 (sub-)journal fi
120e9 6c 65 0a 2a 2a 20 61 6e 64 20 70 6c 61 79 65 64 le.** and played
120ea 20 62 61 63 6b 2c 20 74 68 65 6e 20 53 51 4c 49 back, then SQLI
120eb 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
120ec 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f d. If an IO erro
120ed 72 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 69 6c r occurs.** whil
120ee 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 72 65 e reading the re
120ef 63 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 28 73 cord from the (s
120f0 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ub-)journal file
120f1 20 6f 72 20 77 68 69 6c 65 20 77 72 69 74 69 6e or while writin
120f2 67 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 g.** to the data
120f3 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 base file, then
120f4 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 the IO error cod
120f5 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 e is returned. I
120f6 66 20 64 61 74 61 0a 2a 2a 20 69 73 20 73 75 63 f data.** is suc
120f7 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 cessfully read f
120f8 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f rom the (sub-)jo
120f9 75 72 6e 61 6c 20 66 69 6c 65 20 62 75 74 20 61 urnal file but a
120fa 70 70 65 61 72 73 20 74 6f 20 62 65 0a 2a 2a 20 ppears to be.**
120fb 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54 corrupted, SQLIT
120fc 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75 72 6e E_DONE is return
120fd 65 64 2e 20 44 61 74 61 20 69 73 20 63 6f 6e 73 ed. Data is cons
120fe 69 64 65 72 65 64 20 63 6f 72 72 75 70 74 65 64 idered corrupted
120ff 20 69 6e 0a 2a 2a 20 74 77 6f 20 63 69 72 63 75 in.** two circu
12100 6d 73 74 61 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a mstances:.** .**
12101 20 20 20 2a 20 49 66 20 74 68 65 20 72 65 63 6f * If the reco
12102 72 64 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 rd page-number i
12103 73 20 69 6c 6c 65 67 61 6c 20 28 30 20 6f 72 20 s illegal (0 or
12104 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 PAGER_MJ_PGNO),
12105 6f 72 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 or.** * If the
12106 20 72 65 63 6f 72 64 20 69 73 20 62 65 69 6e 67 record is being
12107 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 66 72 6f rolled back fro
12108 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e m the main journ
12109 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20 61 al file.** a
1210a 6e 64 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 nd the checksum
1210b 66 69 65 6c 64 20 64 6f 65 73 20 6e 6f 74 20 6d field does not m
1210c 61 74 63 68 20 74 68 65 20 72 65 63 6f 72 64 20 atch the record
1210d 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e content..**.** N
1210e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 either of these
1210f 74 77 6f 20 73 63 65 6e 61 72 69 6f 73 20 61 72 two scenarios ar
12110 65 20 70 6f 73 73 69 62 6c 65 20 64 75 72 69 6e e possible durin
12111 67 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f g a savepoint ro
12112 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 llback..**.** If
12113 20 74 68 69 73 20 69 73 20 61 20 73 61 76 65 70 this is a savep
12114 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 oint rollback, t
12115 68 65 6e 20 6d 65 6d 6f 72 79 20 6d 61 79 20 68 hen memory may h
12116 61 76 65 20 74 6f 20 62 65 20 64 79 6e 61 6d 69 ave to be dynami
12117 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 cally.** allocat
12118 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 ed by this funct
12119 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20 ion. If this is
1211a 74 68 65 20 63 61 73 65 20 61 6e 64 20 61 6e 20 the case and an
1211b 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
1211c 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 ,.** SQLITE_NOME
1211d 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a M is returned..*
1211e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
1211f 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f er_playback_one_
12120 70 61 67 65 28 0a 20 20 50 61 67 65 72 20 2a 70 page(. Pager *p
12121 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20 Pager,
12122 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
12123 65 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20 er being played
12124 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 back */. int is
12125 4d 61 69 6e 4a 72 6e 6c 2c 20 20 20 20 20 20 20 MainJrnl,
12126 20 20 20 20 20 20 20 20 2f 2a 20 31 20 2d 3e 20 /* 1 ->
12127 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 main journal. 0
12128 2d 3e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 -> sub-journal.
12129 2a 2f 0a 20 20 69 6e 74 20 69 73 55 6e 73 79 6e */. int isUnsyn
1212a 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c,
1212b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 /* True if re
1212c 61 64 69 6e 67 20 66 72 6f 6d 20 75 6e 73 79 6e ading from unsyn
1212d 63 65 64 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c ced main journal
1212e 20 2a 2f 0a 20 20 69 36 34 20 2a 70 4f 66 66 73 */. i64 *pOffs
1212f 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 et,
12130 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 /* Offset of
12131 20 72 65 63 6f 72 64 20 74 6f 20 70 6c 61 79 62 record to playb
12132 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 73 53 ack */. int isS
12133 61 76 65 70 6e 74 2c 20 20 20 20 20 20 20 20 20 avepnt,
12134 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 /* True f
12135 6f 72 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 or a savepoint r
12136 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 42 69 74 ollback */. Bit
12137 76 65 63 20 2a 70 44 6f 6e 65 20 20 20 20 20 20 vec *pDone
12138 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 /* Bi
12139 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 61 6c tvec of pages al
1213a 72 65 61 64 79 20 70 6c 61 79 65 64 20 62 61 63 ready played bac
1213b 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 k */.){. int rc
1213c 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b 20 ;. PgHdr *pPg;
1213d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1213e 20 20 2f 2a 20 41 6e 20 65 78 69 73 74 69 6e 67 /* An existing
1213f 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63 page in the cac
12140 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e he */. Pgno pgn
12141 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 o;
12142 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
12143 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 70 61 e number of a pa
12144 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f ge in journal */
12145 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20 20 20 . u32 cksum;
12146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12147 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 75 73 65 /* Checksum use
12148 64 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 d for sanity che
12149 63 6b 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 cking */. char
1214a 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 *aData;
1214b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
1214c 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20 66 6f orary storage fo
1214d 72 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 r the page */.
1214e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66 sqlite3_file *jf
1214f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a d; /*
12150 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 The file descri
12151 70 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 75 ptor for the jou
12152 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 rnal file */..
12153 61 73 73 65 72 74 28 20 28 69 73 4d 61 69 6e 4a assert( (isMainJ
12154 72 6e 6c 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 rnl&~1)==0 );
12155 20 20 20 2f 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c /* isMainJrnl
12156 20 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 is 0 or 1 */.
12157 61 73 73 65 72 74 28 20 28 69 73 53 61 76 65 70 assert( (isSavep
12158 6e 74 26 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 nt&~1)==0 );
12159 20 20 20 2f 2a 20 69 73 53 61 76 65 70 6e 74 20 /* isSavepnt
1215a 69 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 is 0 or 1 */. a
1215b 73 73 65 72 74 28 20 69 73 4d 61 69 6e 4a 72 6e ssert( isMainJrn
1215c 6c 20 7c 7c 20 70 44 6f 6e 65 20 29 3b 20 20 20 l || pDone );
1215d 20 20 2f 2a 20 70 44 6f 6e 65 20 61 6c 77 61 79 /* pDone alway
1215e 73 20 75 73 65 64 20 6f 6e 20 73 75 62 2d 6a 6f s used on sub-jo
1215f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 61 73 73 65 urnals */. asse
12160 72 74 28 20 69 73 53 61 76 65 70 6e 74 20 7c 7c rt( isSavepnt ||
12161 20 70 44 6f 6e 65 3d 3d 30 20 29 3b 20 20 20 2f pDone==0 ); /
12162 2a 20 70 44 6f 6e 65 20 6e 65 76 65 72 20 75 73 * pDone never us
12163 65 64 20 6f 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f ed on non-savepo
12164 69 6e 74 20 2a 2f 0a 0a 20 20 61 44 61 74 61 20 int */.. aData
12165 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 = pPager->pTmpSp
12166 61 63 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 ace;. assert( a
12167 44 61 74 61 20 29 3b 20 20 20 20 20 20 20 20 20 Data );
12168 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 /* Temp storage
12169 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 61 64 must have alread
1216a 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 y been allocated
1216b 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 */.. /* Read t
1216c 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 he page number a
1216d 6e 64 20 70 61 67 65 20 64 61 74 61 20 66 72 6f nd page data fro
1216e 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 m the journal or
1216f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 20 20 2a sub-journal. *
12170 2a 20 66 69 6c 65 2e 20 52 65 74 75 72 6e 20 61 * file. Return a
12171 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 n error code to
12172 74 68 65 20 63 61 6c 6c 65 72 20 69 66 20 61 6e the caller if an
12173 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 IO error occurs
12174 2e 0a 20 20 2a 2f 0a 20 20 6a 66 64 20 3d 20 69 .. */. jfd = i
12175 73 4d 61 69 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 sMainJrnl ? pPag
12176 65 72 2d 3e 6a 66 64 20 3a 20 70 50 61 67 65 72 er->jfd : pPager
12177 2d 3e 73 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 ->sjfd;. rc = r
12178 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 2a ead32bits(jfd, *
12179 70 4f 66 66 73 65 74 2c 20 26 70 67 6e 6f 29 3b pOffset, &pgno);
1217a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1217b 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 E_OK ) return rc
1217c 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1217d 4f 73 52 65 61 64 28 6a 66 64 2c 20 28 75 38 2a OsRead(jfd, (u8*
1217e 29 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e )aData, pPager->
1217f 70 61 67 65 53 69 7a 65 2c 20 28 2a 70 4f 66 66 pageSize, (*pOff
12180 73 65 74 29 2b 34 29 3b 0a 20 20 69 66 28 20 72 set)+4);. if( r
12181 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 c!=SQLITE_OK ) r
12182 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 eturn rc;. *pOf
12183 66 73 65 74 20 2b 3d 20 70 50 61 67 65 72 2d 3e fset += pPager->
12184 70 61 67 65 53 69 7a 65 20 2b 20 34 20 2b 20 69 pageSize + 4 + i
12185 73 4d 61 69 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 sMainJrnl*4;..
12186 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 /* Sanity checki
12187 6e 67 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 20 ng on the page.
12188 20 54 68 69 73 20 69 73 20 6d 6f 72 65 20 69 6d This is more im
12189 70 6f 72 74 61 6e 74 20 74 68 61 74 20 49 20 6f portant that I o
1218a 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 riginally. ** t
1218b 68 6f 75 67 68 74 2e 20 20 49 66 20 61 20 70 6f hought. If a po
1218c 77 65 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 wer failure occu
1218d 72 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 rs while the jou
1218e 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 20 77 72 rnal is being wr
1218f 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 itten,. ** it c
12190 6f 75 6c 64 20 63 61 75 73 65 20 69 6e 76 61 6c ould cause inval
12191 69 64 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 id data to be wr
12192 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a itten into the j
12193 6f 75 72 6e 61 6c 2e 20 20 57 65 20 6e 65 65 64 ournal. We need
12194 20 74 6f 0a 20 20 2a 2a 20 64 65 74 65 63 74 20 to. ** detect
12195 74 68 69 73 20 69 6e 76 61 6c 69 64 20 64 61 74 this invalid dat
12196 61 20 28 77 69 74 68 20 68 69 67 68 20 70 72 6f a (with high pro
12197 62 61 62 69 6c 69 74 79 29 20 61 6e 64 20 69 67 bability) and ig
12198 6e 6f 72 65 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 nore it.. */.
12199 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 if( pgno==0 || p
1219a 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 gno==PAGER_MJ_PG
1219b 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 NO(pPager) ){.
1219c 20 20 61 73 73 65 72 74 28 20 21 69 73 53 61 76 assert( !isSav
1219d 65 70 6e 74 20 29 3b 0a 20 20 20 20 72 65 74 75 epnt );. retu
1219e 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a rn SQLITE_DONE;.
1219f 20 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3e 28 }. if( pgno>(
121a0 50 67 6e 6f 29 70 50 61 67 65 72 2d 3e 64 62 53 Pgno)pPager->dbS
121a1 69 7a 65 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 ize || sqlite3Bi
121a2 74 76 65 63 54 65 73 74 28 70 44 6f 6e 65 2c 20 tvecTest(pDone,
121a3 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 72 65 74 pgno) ){. ret
121a4 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
121a5 20 7d 0a 20 20 69 66 28 20 69 73 4d 61 69 6e 4a }. if( isMainJ
121a6 72 6e 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 rnl ){. rc =
121a7 72 65 61 64 33 32 62 69 74 73 28 6a 66 64 2c 20 read32bits(jfd,
121a8 28 2a 70 4f 66 66 73 65 74 29 2d 34 2c 20 26 63 (*pOffset)-4, &c
121a9 6b 73 75 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 ksum);. if( r
121aa 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
121ab 20 20 20 69 66 28 20 21 69 73 53 61 76 65 70 6e if( !isSavepn
121ac 74 20 26 26 20 70 61 67 65 72 5f 63 6b 73 75 6d t && pager_cksum
121ad 28 70 50 61 67 65 72 2c 20 28 75 38 2a 29 61 44 (pPager, (u8*)aD
121ae 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 ata)!=cksum ){.
121af 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
121b0 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 TE_DONE;. }.
121b1 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65 20 }.. if( pDone
121b2 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 && (rc = sqlite3
121b3 42 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65 2c BitvecSet(pDone,
121b4 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45 5f pgno))!=SQLITE_
121b5 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
121b6 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 rc;. }.. asse
121b7 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
121b8 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 e==PAGER_RESERVE
121b9 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 D || pPager->sta
121ba 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 te>=PAGER_EXCLUS
121bb 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 IVE );.. /* If
121bc 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 the pager is in
121bd 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c 20 RESERVED state,
121be 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74 20 then there must
121bf 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 69 be a copy of thi
121c0 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 s. ** page in t
121c1 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 he pager cache.
121c2 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 73 In this case jus
121c3 74 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67 t update the pag
121c4 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20 6e er cache,. ** n
121c5 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ot the database
121c6 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 file. The page i
121c7 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64 69 s left marked di
121c8 72 74 79 20 69 6e 20 74 68 69 73 20 63 61 73 65 rty in this case
121c9 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 .. **. ** An e
121ca 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65 20 xception to the
121cb 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20 74 above rule: If t
121cc 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 69 he database is i
121cd 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 n no-sync mode.
121ce 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20 69 ** and a page i
121cf 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 s moved during a
121d0 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 n incremental va
121d1 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70 61 cuum then the pa
121d2 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 ge may. ** not
121d3 62 65 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 be in the pager
121d4 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69 66 cache. Later: if
121d5 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 49 a malloc() or I
121d6 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 20 O error occurs.
121d7 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f 76 ** during a Mov
121d8 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74 68 epage() call, th
121d9 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79 20 en the page may
121da 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 not be in the ca
121db 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72 2e che. ** either.
121dc 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69 6f So the conditio
121dd 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 74 n described in t
121de 68 65 20 61 62 6f 76 65 20 70 61 72 61 67 72 61 he above paragra
121df 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 ph is not. ** a
121e0 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20 2a ssert()able.. *
121e1 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58 43 *. ** If in EXC
121e2 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 LUSIVE state, th
121e3 65 6e 20 77 65 20 75 70 64 61 74 65 20 74 68 65 en we update the
121e4 20 70 61 67 65 72 20 63 61 63 68 65 20 69 66 20 pager cache if
121e5 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20 61 it exists. ** a
121e6 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 nd the main file
121e7 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 74 68 . The page is th
121e8 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64 69 en marked not di
121e9 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 rty.. **. ** T
121ea 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54 68 icket #1171: Th
121eb 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 e statement jour
121ec 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 nal might contai
121ed 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 n page content t
121ee 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66 66 hat is. ** diff
121ef 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70 erent from the p
121f0 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20 74 age content at t
121f1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
121f2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a transaction.. *
121f3 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 77 68 * This occurs wh
121f4 65 6e 20 61 20 70 61 67 65 20 69 73 20 63 68 61 en a page is cha
121f5 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 nged prior to th
121f6 65 20 73 74 61 72 74 20 6f 66 20 61 20 73 74 61 e start of a sta
121f7 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65 6e tement. ** then
121f8 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20 77 changed again w
121f9 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d ithin the statem
121fa 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c 69 ent. When rolli
121fb 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a 20 ng back such a.
121fc 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 ** statement we
121fd 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65 20 must not write
121fe 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
121ff 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73 20 database unless
12200 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 we know. ** for
12201 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f 72 certain that or
12202 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 iginal page cont
12203 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64 20 ents are synced
12204 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72 6f into the main ro
12205 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 llback. ** jour
12206 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 2c nal. Otherwise,
12207 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d 69 a power loss mi
12208 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66 69 ght leave modifi
12209 65 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a 20 ed data in the.
1220a 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c ** database fil
1220b 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e 74 e without an ent
1220c 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62 61 ry in the rollba
1220d 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 ck journal that
1220e 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 can. ** restore
1220f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f the database to
12210 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66 6f its original fo
12211 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74 69 rm. Two conditi
12212 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a ons must be. **
12213 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69 74 met before writ
12214 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 ing to the datab
12215 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20 74 ase files. (1) t
12216 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74 he database must
12217 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e be. ** locked.
12218 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74 68 (2) we know th
12219 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 at the original
1221a 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73 20 page content is
1221b 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20 2a fully synced. *
1221c 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a 6f * in the main jo
1221d 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65 63 urnal either bec
1221e 61 75 73 65 20 74 68 65 20 70 61 67 65 20 69 73 ause the page is
1221f 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 not in cache or
12220 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20 70 else. ** the p
12221 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 age is marked as
12222 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 needSync==0..
12223 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34 2d **. ** 2008-04-
12224 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d 70 14: When attemp
12225 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20 61 ting to vacuum a
12226 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 corrupt databas
12227 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 e file, it. **
12228 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 is possible to f
12229 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 ail a statement
1222a 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 74 68 on a database th
1222b 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74 20 at does not yet
1222c 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e exist.. ** Do n
1222d 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 ot attempt to wr
1222e 69 74 65 20 69 66 20 64 61 74 61 62 61 73 65 20 ite if database
1222f 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20 62 file has never b
12230 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f een opened.. */
12231 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f 6c . pPg = pager_l
12232 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 ookup(pPager, pg
12233 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 no);. assert( p
12234 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a Pg || !MEMDB );.
12235 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 50 PAGERTRACE(("P
12236 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65 20 LAYBACK %d page
12237 25 64 20 68 61 73 68 28 25 30 38 78 29 20 25 73 %d hash(%08x) %s
12238 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 \n",.
12239 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
1223a 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64 61 74 pgno, pager_dat
1223b 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e 70 61 ahash(pPager->pa
1223c 67 65 53 69 7a 65 2c 20 28 75 38 2a 29 61 44 61 geSize, (u8*)aDa
1223d 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ta),.
1223e 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 (isMainJrnl?"mai
1223f 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22 73 75 62 2d n-journal":"sub-
12240 6a 6f 75 72 6e 61 6c 22 29 0a 20 20 29 29 3b 0a journal"). ));.
12241 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73 if( (pPager->s
12242 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c tate>=PAGER_EXCL
12243 55 53 49 56 45 29 0a 20 20 20 26 26 20 28 70 50 USIVE). && (pP
12244 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70 50 67 2d g==0 || 0==(pPg-
12245 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 >flags&PGHDR_NEE
12246 44 5f 53 59 4e 43 29 29 0a 20 20 20 26 26 20 69 D_SYNC)). && i
12247 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
12248 29 0a 20 20 20 26 26 20 21 69 73 55 6e 73 79 6e ). && !isUnsyn
12249 63 0a 20 20 29 7b 0a 20 20 20 20 69 36 34 20 6f c. ){. i64 o
1224a 66 73 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 fst = (pgno-1)*(
1224b 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 i64)pPager->page
1224c 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20 73 Size;. rc = s
1224d 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
1224e 61 67 65 72 2d 3e 66 64 2c 20 28 75 38 2a 29 61 ager->fd, (u8*)a
1224f 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 Data, pPager->pa
12250 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 geSize, ofst);.
12251 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 if( pgno>pPag
12252 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 er->dbFileSize )
12253 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
12254 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e dbFileSize = pgn
12255 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 o;. }. if(
12256 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 pPager->pBackup
12257 20 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 ){. CODEC1
12258 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 (pPager, aData,
12259 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 pgno, 3, rc=SQLI
1225a 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 TE_NOMEM);.
1225b 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 sqlite3BackupUp
1225c 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 date(pPager->pBa
1225d 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a ckup, pgno, (u8*
1225e 29 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 43 )aData);. C
1225f 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 61 44 ODEC2(pPager, aD
12260 61 74 61 2c 20 70 67 6e 6f 2c 20 37 2c 20 72 63 ata, pgno, 7, rc
12261 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 61 =SQLITE_NOMEM, a
12262 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d Data);. }. }
12263 65 6c 73 65 20 69 66 28 20 21 69 73 4d 61 69 6e else if( !isMain
12264 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d 30 20 29 Jrnl && pPg==0 )
12265 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 {. /* If this
12266 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f is a rollback o
12267 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 61 6e f a savepoint an
12268 64 20 64 61 74 61 20 77 61 73 20 6e 6f 74 20 77 d data was not w
12269 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 2a 2a ritten to. **
1226a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e the database an
1226b 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f d the page is no
1226c 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 t in-memory, the
1226d 72 65 20 69 73 20 61 20 70 6f 74 65 6e 74 69 61 re is a potentia
1226e 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d l. ** problem
1226f 2e 20 57 68 65 6e 20 74 68 65 20 70 61 67 65 20 . When the page
12270 69 73 20 6e 65 78 74 20 66 65 74 63 68 65 64 20 is next fetched
12271 62 79 20 74 68 65 20 62 2d 74 72 65 65 20 6c 61 by the b-tree la
12272 79 65 72 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 yer, it . **
12273 77 69 6c 6c 20 62 65 20 72 65 61 64 20 66 72 6f will be read fro
12274 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
12275 69 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 6f ile, which may o
12276 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 0a 20 20 r may not be .
12277 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e 20 0a 20 ** current. .
12278 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
12279 72 65 20 61 72 65 20 61 20 63 6f 75 70 6c 65 20 re are a couple
1227a 6f 66 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 of different way
1227b 73 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 s this can happe
1227c 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75 69 74 65 n. All are quite
1227d 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75 72 65 2e . ** obscure.
1227e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e When running in
1227f 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 synchronous mod
12280 65 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 e, this can only
12281 20 68 61 70 70 65 6e 20 0a 20 20 20 20 2a 2a 20 happen . **
12282 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f if the page is o
12283 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 n the free-list
12284 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 at the start of
12285 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c the transaction,
12286 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 then. ** pop
12287 75 6c 61 74 65 64 2c 20 74 68 65 6e 20 6d 6f 76 ulated, then mov
12288 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ed using sqlite3
12289 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 29 2e PagerMovepage().
1228a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
1228b 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 he solution is t
1228c 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f o add an in-memo
1228d 72 79 20 70 61 67 65 20 74 6f 20 74 68 65 20 63 ry page to the c
1228e 61 63 68 65 20 63 6f 6e 74 61 69 6e 69 6e 67 0a ache containing.
1228f 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 20 ** the data
12290 6a 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 74 just read from t
12291 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 he sub-journal.
12292 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 Mark the page as
12293 20 64 69 72 74 79 20 0a 20 20 20 20 2a 2a 20 61 dirty . ** a
12294 6e 64 20 69 66 20 74 68 65 20 70 61 67 65 72 20 nd if the pager
12295 72 65 71 75 69 72 65 73 20 61 20 6a 6f 75 72 6e requires a journ
12296 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e 20 6d 61 al-sync, then ma
12297 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 0a rk the page as .
12298 20 20 20 20 2a 2a 20 72 65 71 75 69 72 69 6e 67 ** requiring
12299 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 a journal-sync
1229a 62 65 66 6f 72 65 20 69 74 20 69 73 20 77 72 69 before it is wri
1229b 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tten.. */.
1229c 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65 70 assert( isSavep
1229d 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 28 72 nt );. if( (r
1229e 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1229f 41 63 71 75 69 72 65 28 70 50 61 67 65 72 2c 20 Acquire(pPager,
122a0 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31 29 29 21 pgno, &pPg, 1))!
122a1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
122a2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
122a3 20 20 20 7d 0a 20 20 20 20 70 50 67 2d 3e 66 6c }. pPg->fl
122a4 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
122a5 45 44 5f 52 45 41 44 3b 0a 20 20 20 20 73 71 6c ED_READ;. sql
122a6 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 ite3PcacheMakeDi
122a7 72 74 79 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 rty(pPg);. }.
122a8 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 2f if( pPg ){. /
122a9 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f 75 6c 64 * No page should
122aa 20 65 76 65 72 20 62 65 20 65 78 70 6c 69 63 69 ever be explici
122ab 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 tly rolled back
122ac 74 68 61 74 20 69 73 20 69 6e 20 75 73 65 2c 20 that is in use,
122ad 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f except. ** fo
122ae 72 20 70 61 67 65 20 31 20 77 68 69 63 68 20 69 r page 1 which i
122af 73 20 68 65 6c 64 20 69 6e 20 75 73 65 20 69 6e s held in use in
122b0 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 order to keep t
122b1 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 he lock on the.
122b2 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 61 ** database a
122b3 63 74 69 76 65 2e 20 48 6f 77 65 76 65 72 20 73 ctive. However s
122b4 75 63 68 20 61 20 70 61 67 65 20 6d 61 79 20 62 uch a page may b
122b5 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 73 e rolled back as
122b6 20 61 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a a result. **
122b7 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 of an internal
122b8 65 72 72 6f 72 20 72 65 73 75 6c 74 69 6e 67 20 error resulting
122b9 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 in an automatic
122ba 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 call to. ** s
122bb 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
122bc 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 ack().. */.
122bd 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 0a 20 void *pData;.
122be 20 20 20 70 44 61 74 61 20 3d 20 70 50 67 2d 3e pData = pPg->
122bf 70 44 61 74 61 3b 0a 20 20 20 20 6d 65 6d 63 70 pData;. memcp
122c0 79 28 70 44 61 74 61 2c 20 28 75 38 2a 29 61 44 y(pData, (u8*)aD
122c1 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 ata, pPager->pag
122c2 65 53 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 eSize);. pPag
122c3 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 er->xReiniter(pP
122c4 67 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61 g);. if( isMa
122c5 69 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 inJrnl && (!isSa
122c6 76 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 vepnt || *pOffse
122c7 74 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e t<=pPager->journ
122c8 61 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 alHdr) ){.
122c9 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e /* If the conten
122ca 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 ts of this page
122cb 77 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 were just restor
122cc 65 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e ed from the main
122cd 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e . ** journ
122ce 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 al file, then it
122cf 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 s content must b
122d0 65 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77 e as they were w
122d1 68 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a hen the . *
122d2 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 * transaction wa
122d3 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 s first opened.
122d4 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 In this case we
122d5 63 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 can mark the pag
122d6 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c e. ** as cl
122d7 65 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 ean, since there
122d8 20 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 will be no need
122d9 20 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 to write it out
122da 20 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a to the.. *
122db 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 *. ** There
122dc 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f is one exceptio
122dd 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 n to this rule.
122de 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62 If the page is b
122df 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 eing rolled.
122e0 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 ** back as par
122e1 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 t of a savepoint
122e2 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 (or statement)
122e3 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e rollback from an
122e4 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e . ** unsyn
122e5 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 ced portion of t
122e6 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 he main journal
122e7 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 file, then it is
122e8 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 not safe.
122e9 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 ** to mark the p
122ea 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 age as clean. Th
122eb 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 is is because ma
122ec 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 rking the page a
122ed 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e s. ** clean
122ee 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 will clear the
122ef 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
122f0 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 flag. Since the
122f1 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a page is. **
122f2 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
122f3 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 journal file (re
122f4 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e corded in Pager.
122f5 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a pInJournal) and.
122f6 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 ** the PGH
122f7 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 DR_NEED_SYNC fla
122f8 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 g is cleared, if
122f9 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69 the page is wri
122fa 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a tten to. **
122fb 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 again within th
122fc 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 is transaction,
122fd 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 it will be marke
122fe 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 d as dirty but.
122ff 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 ** the PGHD
12300 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 R_NEED_SYNC flag
12301 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 will not be set
12302 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 . It could then
12303 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 potentially.
12304 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 ** be written
12305 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 out into the dat
12306 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 abase file befor
12307 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 e its journal fi
12308 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d le. ** segm
12309 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 ent is synced. I
1230a 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 f a crash occurs
1230b 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f during or follo
1230c 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 wing this,.
1230d 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 ** database cor
1230e 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 ruption may ensu
1230f 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
12310 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
12311 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 akeClean(pPg);.
12312 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
12313 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 TE_CHECK_PAGES.
12314 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 pPg->pageHash
12315 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 = pager_pagehas
12316 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 h(pPg);.#endif.
12317 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 /* If this wa
12318 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 s page 1, then r
12319 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 estore the value
1231a 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 of Pager.dbFile
1231b 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 Vers.. ** Do
1231c 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 this before any
1231d 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 decoding. */.
1231e 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a if( pgno==1 ){.
1231f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 memcpy(&pP
12320 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 ager->dbFileVers
12321 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b , &((u8*)pData)[
12322 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 24],sizeof(pPage
12323 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b r->dbFileVers));
12324 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 . }.. /* D
12325 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a ecode the page j
12326 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 ust read from di
12327 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 sk */. CODEC1
12328 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 (pPager, pData,
12329 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 pPg->pgno, 3, rc
1232a 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a =SQLITE_NOMEM);.
1232b 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 sqlite3Pcach
1232c 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 eRelease(pPg);.
1232d 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1232e 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 }../*.** Paramet
1232f 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68 er zMaster is th
12330 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 e name of a mast
12331 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e er journal file.
12332 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 A single journa
12333 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72 l.** file that r
12334 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d eferred to the m
12335 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
12336 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e le has just been
12337 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
12338 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 This routine ch
12339 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f ecks if it is po
1233a 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 ssible to delete
1233b 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1233c 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 nal file,.** and
1233d 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69 does so if it i
1233e 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e s..**.** Argumen
1233f 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f t zMaster may po
12340 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d int to Pager.pTm
12341 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20 pSpace. So that
12342 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a buffer is not .*
12343 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 * available for
12344 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 use within this
12345 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
12346 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f When a master jo
12347 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 72 urnal file is cr
12348 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 6f 70 eated, it is pop
12349 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ulated with the
1234a 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c names .** of all
1234b 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 6a 6f of its child jo
1234c 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65 urnals, one afte
1234d 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61 r another, forma
1234e 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 0a 2a tted as utf-8 .*
1234f 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 2e 20 * encoded text.
12350 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 The end of each
12351 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69 child journal fi
12352 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 le is marked wit
12353 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d h a .** nul-term
12354 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 78 30 inator byte (0x0
12355 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 74 0). i.e. the ent
12356 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 ire contents of
12357 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
12358 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 74 .** file for a t
12359 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c ransaction invol
1235a 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 61 73 ving two databas
1235b 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a es might be:.**.
1235c 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c ** "/home/bill
1235d 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 /a.db-journal\x0
1235e 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 0/home/bill/b.db
1235f 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a -journal\x00".**
12360 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f 75 .** A master jou
12361 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e rnal file may on
12362 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 6f 6e ly be deleted on
12363 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 ce all of its ch
12364 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 ild .** journals
12365 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65 have been rolle
12366 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 d back..**.** Th
12367 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64 is function read
12368 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f s the contents o
12369 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 f the master-jou
1236a 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a rnal file into .
1236b 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f ** memory and lo
1236c 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 ops through each
1236d 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f of the child jo
1236e 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 urnal names. For
1236f 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 6a .** each child j
12370 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b ournal, it check
12371 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 s if:.**.** *
12372 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 if the child jou
12373 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 6e 64 rnal exists, and
12374 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 if so.** * if
12375 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e the child journ
12376 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 al contains a re
12377 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 65 ference to maste
12378 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 r journal .**
12379 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a file zMaster.*
1237a 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 20 *.** If a child
1237b 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 journal can be f
1237c 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 68 65 ound that matche
1237d 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 63 72 s both of the cr
1237e 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c iteria.** above,
1237f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 this function r
12380 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 64 eturns without d
12381 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f oing anything. O
12382 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20 therwise, if.**
12383 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 6a 6f no such child jo
12384 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 urnal can be fou
12385 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 nd, file zMaster
12386 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d is deleted from
12387 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 .** the file-sys
12388 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 tem using sqlite
12389 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 3OsDelete()..**.
1238a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f ** If an IO erro
1238b 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 r within this fu
1238c 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 nction, an error
1238d 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
1238e 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 d. This.** funct
1238f 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65 ion allocates me
12390 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 mory by calling
12391 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e sqlite3Malloc().
12392 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f If an allocatio
12393 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 n.** fails, SQLI
12394 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 TE_NOMEM is retu
12395 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c rned. Otherwise,
12396 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c if no IO or mal
12397 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f loc errors .** o
12398 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ccur, SQLITE_OK
12399 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
1239a 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 75 ** TODO: This fu
1239b 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 nction allocates
1239c 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 a single block
1239d 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 of memory to loa
1239e 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 d.** the entire
1239f 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
123a0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
123a1 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 20 ile. This could
123a2 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f be.** a couple o
123a3 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 73 f kilobytes or s
123a4 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 o - potentially
123a5 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 larger than the
123a6 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a page .** size..*
123a7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 /.static int pag
123a8 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67 er_delmaster(Pag
123a9 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 er *pPager, cons
123aa 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 t char *zMaster)
123ab 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
123ac 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e *pVfs = pPager->
123ad 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 pVfs;. int rc;
123ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
123af 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 /* Return code
123b0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 */. sqlite3_fi
123b1 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 le *pMaster;
123b2 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74 /* Malloc'd mast
123b3 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er-journal file
123b4 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 descriptor */.
123b5 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
123b6 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c ournal; /* Mal
123b7 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72 loc'd child-jour
123b8 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 nal file descrip
123b9 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a tor */. char *z
123ba 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 MasterJournal =
123bb 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 0; /* Contents o
123bc 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c f master journal
123bd 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e file */. i64 n
123be 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 MasterJournal;
123bf 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
123c0 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 master journal f
123c1 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c ile */.. /* All
123c2 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 ocate space for
123c3 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 both the pJourna
123c4 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 66 69 l and pMaster fi
123c5 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2e 0a le descriptors..
123c6 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 ** If successf
123c7 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 ul, open the mas
123c8 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
123c9 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 for reading..
123ca 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28 */. pMaster = (
123cb 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 sqlite3_file *)s
123cc 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f qlite3MallocZero
123cd 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 (pVfs->szOsFile
123ce 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c * 2);. pJournal
123cf 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 = (sqlite3_file
123d0 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 73 74 *)(((u8 *)pMast
123d1 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 er) + pVfs->szOs
123d2 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 4d File);. if( !pM
123d3 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 63 20 aster ){. rc
123d4 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
123d5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e }else{. con
123d6 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 st int flags = (
123d7 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 SQLITE_OPEN_READ
123d8 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e ONLY|SQLITE_OPEN
123d9 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 _MASTER_JOURNAL)
123da 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
123db 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a e3OsOpen(pVfs, z
123dc 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c Master, pMaster,
123dd 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a flags, 0);. }.
123de 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
123df 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 _OK ) goto delma
123e0 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 ster_out;.. rc
123e1 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 = sqlite3OsFileS
123e2 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d ize(pMaster, &nM
123e3 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 asterJournal);.
123e4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
123e5 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 OK ) goto delmas
123e6 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 ter_out;.. if(
123e7 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 nMasterJournal>0
123e8 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a ){. char *zJ
123e9 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 ournal;. char
123ea 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 *zMasterPtr = 0
123eb 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 ;. int nMaste
123ec 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50 rPtr = pVfs->mxP
123ed 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 athname+1;..
123ee 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 /* Load the enti
123ef 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 re master journa
123f0 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63 l file into spac
123f1 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a e obtained from.
123f2 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d ** sqlite3_m
123f3 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e alloc() and poin
123f4 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 ted to by zMaste
123f5 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a rJournal. . *
123f6 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 /. zMasterJou
123f7 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 rnal = sqlite3Ma
123f8 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 lloc((int)nMaste
123f9 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 rJournal + nMast
123fa 65 72 50 74 72 20 2b 20 31 29 3b 0a 20 20 20 20 erPtr + 1);.
123fb 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 if( !zMasterJour
123fc 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 nal ){. rc
123fd 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
123fe 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
123ff 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a ster_out;. }.
12400 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d zMasterPtr =
12401 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c &zMasterJournal
12402 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b [nMasterJournal+
12403 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 1];. rc = sql
12404 69 74 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 ite3OsRead(pMast
12405 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e er, zMasterJourn
12406 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 al, (int)nMaster
12407 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 Journal, 0);.
12408 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
12409 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 OK ) goto delmas
1240a 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 ter_out;. zMa
1240b 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 sterJournal[nMas
1240c 74 65 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b terJournal] = 0;
1240d 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d .. zJournal =
1240e 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b zMasterJournal;
1240f 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f . while( (zJo
12410 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 urnal-zMasterJou
12411 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 rnal)<nMasterJou
12412 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e rnal ){. in
12413 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 20 20 t exists;.
12414 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 rc = sqlite3OsAc
12415 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72 cess(pVfs, zJour
12416 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 nal, SQLITE_ACCE
12417 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73 SS_EXISTS, &exis
12418 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ts);. if( r
12419 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
1241a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c goto del
1241b 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 master_out;.
1241c 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 78 }. if( ex
1241d 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20 ists ){.
1241e 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f /* One of the jo
1241f 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 urnals pointed t
12420 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 o by the master
12421 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a journal exists..
12422 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 ** Open
12423 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20 it and check if
12424 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 it points at the
12425 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e master journal.
12426 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 If. ** s
12427 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 o, return withou
12428 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6d t deleting the m
12429 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1242a 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 le.. */.
1242b 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 int c;.
1242c 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 int flags
1242d 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 = (SQLITE_OPEN_R
1242e 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f EADONLY|SQLITE_O
1242f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c PEN_MAIN_JOURNAL
12430 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
12431 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 sqlite3OsOpen(pV
12432 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a fs, zJournal, pJ
12433 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30 ournal, flags, 0
12434 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
12435 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
12436 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 goto d
12437 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 elmaster_out;.
12438 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
12439 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 rc = readMaster
1243a 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c Journal(pJournal
1243b 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d , zMasterPtr, nM
1243c 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20 asterPtr);.
1243d 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
1243e 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 e(pJournal);.
1243f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
12440 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
12441 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 goto delmast
12442 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 er_out;.
12443 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 7a }.. c = z
12444 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 20 MasterPtr[0]!=0
12445 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 65 && strcmp(zMaste
12446 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d rPtr, zMaster)==
12447 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 0;. if( c
12448 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a ){. /*
12449 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 We have a match
1244a 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 . Do not delete
1244b 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e the master journ
1244c 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 al file. */.
1244d 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 goto delma
1244e 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 ster_out;.
1244f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
12450 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 zJournal += (s
12451 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
12452 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 Journal)+1);.
12453 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d }. }. . rc =
12454 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
12455 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
12456 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 0);..delmaster_o
12457 75 74 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 ut:. if( zMaste
12458 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 rJournal ){.
12459 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 sqlite3_free(zMa
1245a 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 sterJournal);.
1245b 7d 20 20 0a 20 20 69 66 28 20 70 4d 61 73 74 65 } . if( pMaste
1245c 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 r ){. sqlite3
1245d 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29 OsClose(pMaster)
1245e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 ;. assert( !i
1245f 73 4f 70 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 sOpen(pJournal)
12460 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
12461 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a _free(pMaster);.
12462 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
12463 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
12464 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 tion is used to
12465 63 68 61 6e 67 65 20 74 68 65 20 61 63 74 75 61 change the actua
12466 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 l size of the da
12467 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 tabase .** file
12468 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 in the file-syst
12469 65 6d 2e 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 em. This only ha
1246a 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69 ppens when commi
1246b 74 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 tting a transact
1246c 69 6f 6e 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 ion,.** or rolli
1246d 6e 67 20 62 61 63 6b 20 61 20 74 72 61 6e 73 61 ng back a transa
1246e 63 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 ction (including
1246f 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 rolling back a
12470 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a hot-journal)..**
12471 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e 20 .** If the main
12472 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 database file is
12473 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e not open, or an
12474 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 exclusive lock
12475 69 73 20 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 is not.** held,
12476 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 this function is
12477 20 61 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 a no-op. Otherw
12478 69 73 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 ise, the size of
12479 20 74 68 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 the file is.**
1247a 63 68 61 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 changed to nPage
1247b 20 70 61 67 65 73 20 28 6e 50 61 67 65 2a 70 50 pages (nPage*pP
1247c 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 ager->pageSize b
1247d 79 74 65 73 29 2e 20 49 66 20 74 68 65 20 66 69 ytes). If the fi
1247e 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 le.** on disk is
1247f 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 currently large
12480 72 20 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 r than nPage pag
12481 65 73 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 es, then use the
12482 20 56 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 VFS.** xTruncat
12483 65 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 e() method to tr
12484 75 6e 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a uncate it..**.**
12485 20 4f 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 Or, it might mi
12486 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20 ght be the case
12487 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e that the file on
12488 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 disk is smaller
12489 20 74 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 than .** nPage
1248a 70 61 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 pages. Some oper
1248b 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 ating system imp
1248c 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e lementations can
1248d 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 get confused if
1248e 20 0a 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 .** you try to
1248f 74 72 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 truncate a file
12490 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 to some size tha
12491 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e t is larger than
12492 20 69 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c it .** currentl
12493 79 20 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20 y is, so detect
12494 74 68 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 this case and wr
12495 69 74 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 ite a single zer
12496 6f 20 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 o byte to .** th
12497 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 e end of the new
12498 20 66 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a file instead..*
12499 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 *.** If successf
1249a 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ul, return SQLIT
1249b 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 E_OK. If an IO e
1249c 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
1249d 65 20 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 e modifying.** t
1249e 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1249f 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 , return the err
124a0 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 or code to the c
124a1 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 aller..*/.static
124a2 20 69 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 int pager_trunc
124a3 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 ate(Pager *pPage
124a4 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a r, Pgno nPage){.
124a5 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
124a6 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 E_OK;. if( pPag
124a7 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
124a8 5f 45 58 43 4c 55 53 49 56 45 20 26 26 20 69 73 _EXCLUSIVE && is
124a9 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
124aa 20 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 ){. i64 curr
124ab 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 entSize, newSize
124ac 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 ;. /* TODO: I
124ad 73 20 69 74 20 73 61 66 65 20 74 6f 20 75 73 65 s it safe to use
124ae 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a Pager.dbFileSiz
124af 65 20 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 e here? */. r
124b0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c c = sqlite3OsFil
124b1 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 eSize(pPager->fd
124b2 2c 20 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b , ¤tSize);
124b3 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 . newSize = p
124b4 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a Pager->pageSize*
124b5 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 (i64)nPage;.
124b6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
124b7 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 K && currentSize
124b8 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 !=newSize ){.
124b9 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 if( currentSi
124ba 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 ze>newSize ){.
124bb 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
124bc 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 e3OsTruncate(pPa
124bd 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 ger->fd, newSize
124be 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
124bf 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
124c0 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
124c1 65 72 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e er->fd, "", 1, n
124c2 65 77 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 ewSize-1);.
124c3 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d }. if( rc=
124c4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
124c5 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
124c6 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 FileSize = nPage
124c7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
124c8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
124c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 .}../*.** Set th
124ca 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 e value of the P
124cb 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 ager.sectorSize
124cc 76 61 72 69 61 62 6c 65 20 66 6f 72 20 74 68 65 variable for the
124cd 20 67 69 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20 given.** pager
124ce 62 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c based on the val
124cf 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 ue returned by t
124d0 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 20 6d he xSectorSize m
124d1 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 ethod.** of the
124d2 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 open database fi
124d3 6c 65 2e 20 54 68 65 20 73 65 63 74 6f 72 20 73 le. The sector s
124d4 69 7a 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 ize will be used
124d5 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74 used .** to det
124d6 65 72 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20 ermine the size
124d7 61 6e 64 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 and alignment of
124d8 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 journal header
124d9 61 6e 64 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a and .** master j
124da 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 ournal pointers
124db 77 69 74 68 69 6e 20 63 72 65 61 74 65 64 20 6a within created j
124dc 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a ournal files..**
124dd 0a 2a 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 .** For temporar
124de 79 20 66 69 6c 65 73 20 74 68 65 20 65 66 66 65 y files the effe
124df 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a ctive sector siz
124e0 65 20 69 73 20 61 6c 77 61 79 73 20 35 31 32 20 e is always 512
124e1 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 bytes..**.** Oth
124e2 65 72 77 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d erwise, for non-
124e3 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c temporary files,
124e4 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73 the effective s
124e5 65 63 74 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a ector size is.**
124e6 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 the value retur
124e7 6e 65 64 20 62 79 20 74 68 65 20 78 53 65 63 74 ned by the xSect
124e8 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 orSize() method
124e9 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 33 32 rounded up to 32
124ea 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 if.** it is les
124eb 73 20 74 68 61 6e 20 33 32 2c 20 6f 72 20 72 6f s than 32, or ro
124ec 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41 unded down to MA
124ed 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66 X_SECTOR_SIZE if
124ee 20 69 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 it.** is greate
124ef 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f r than MAX_SECTO
124f0 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 R_SIZE..*/.stati
124f1 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72 c void setSector
124f2 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 Size(Pager *pPag
124f3 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 er){. assert( i
124f4 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
124f5 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d ) || pPager->tem
124f6 70 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20 pFile );.. if(
124f7 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c !pPager->tempFil
124f8 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 e ){. /* Sect
124f9 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 or size doesn't
124fa 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f matter for tempo
124fb 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f rary files. Also
124fc 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a , the file. *
124fd 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 * may not have b
124fe 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 een opened yet,
124ff 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 in which case th
12500 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 e OsSectorSize()
12501 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c . ** call wil
12502 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 l segfault..
12503 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 */. pPager->s
12504 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 ectorSize = sqli
12505 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 te3OsSectorSize(
12506 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d pPager->fd);. }
12507 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 . if( pPager->s
12508 65 63 74 6f 72 53 69 7a 65 3c 33 32 20 29 7b 0a ectorSize<32 ){.
12509 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 pPager->sect
1250a 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 orSize = 512;.
1250b 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e }. if( pPager->
1250c 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 sectorSize>MAX_S
1250d 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20 20 ECTOR_SIZE ){.
1250e 20 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53 45 assert( MAX_SE
1250f 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20 29 CTOR_SIZE>=512 )
12510 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 ;. pPager->se
12511 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f 53 ctorSize = MAX_S
12512 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a ECTOR_SIZE;. }.
12513 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 }../*.** Playbac
12514 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e k the journal an
12515 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20 74 d thus restore t
12516 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12517 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 to.** the state
12518 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f 72 it was in befor
12519 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61 6b e we started mak
1251a 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a 2a ing changes. .*
1251b 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c *.** The journal
1251c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73 20 file format is
1251d 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a as follows: .**.
1251e 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65 20 ** (1) 8 byte
1251f 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79 20 prefix. A copy
12520 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of aJournalMagic
12521 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20 62 []..** (2) 4 b
12522 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 yte big-endian i
12523 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 nteger which is
12524 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 the number of va
12525 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64 73 lid page records
12526 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68 65 .** in the
12527 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74 68 journal. If th
12528 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 is value is 0xff
12529 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f 6d ffffff, then com
1252a 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 pute the.**
1252b 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 number of page
1252c 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 records from th
1252d 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e 0a e journal size..
1252e 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65 20 ** (3) 4 byte
1252f 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
12530 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 er which is the
12531 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66 6f initial value fo
12532 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 r the .**
12533 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d 2e sanity checksum.
12534 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74 65 .** (4) 4 byte
12535 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 integer which i
12536 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
12537 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61 74 pages to truncat
12538 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 64 e the.** d
12539 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69 6e atabase to durin
1253a 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a g a rollback..**
1253b 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62 69 (5) 4 byte bi
1253c 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 g-endian integer
1253d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 65 which is the se
1253e 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65 20 ctor size. The
1253f 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20 20 header.**
12540 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 is this many byt
12541 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 es in size..**
12542 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67 2d (6) 4 byte big-
12543 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 endian integer w
12544 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67 65 hich is the page
12545 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 37 29 20 20 size..** (7)
12546 7a 65 72 6f 20 70 61 64 64 69 6e 67 20 6f 75 74 zero padding out
12547 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65 63 to the next sec
12548 74 6f 72 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 38 tor size..** (8
12549 29 20 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 ) Zero or more
1254a 70 61 67 65 73 20 69 6e 73 74 61 6e 63 65 73 2c pages instances,
1254b 20 65 61 63 68 20 61 73 20 66 6f 6c 6c 6f 77 73 each as follows
1254c 3a 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34 :.** + 4
1254d 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 byte page numbe
1254e 72 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 r..** +
1254f 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
12550 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 2e 0a bytes of data..
12551 2a 2a 20 20 20 20 20 20 20 20 2b 20 20 34 20 62 ** + 4 b
12552 79 74 65 20 63 68 65 63 6b 73 75 6d 0a 2a 2a 0a yte checksum.**.
12553 2a 2a 20 57 68 65 6e 20 77 65 20 73 70 65 61 6b ** When we speak
12554 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
12555 68 65 61 64 65 72 2c 20 77 65 20 6d 65 61 6e 20 header, we mean
12556 74 68 65 20 66 69 72 73 74 20 37 20 69 74 65 6d the first 7 item
12557 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 45 61 63 68 s above..** Each
12558 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 6a 6f entry in the jo
12559 75 72 6e 61 6c 20 69 73 20 61 6e 20 69 6e 73 74 urnal is an inst
1255a 61 6e 63 65 20 6f 66 20 74 68 65 20 38 74 68 20 ance of the 8th
1255b 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c item..**.** Call
1255c 20 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 the value from
1255d 74 68 65 20 73 65 63 6f 6e 64 20 62 75 6c 6c 65 the second bulle
1255e 74 20 22 6e 52 65 63 22 2e 20 20 6e 52 65 63 20 t "nRec". nRec
1255f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
12560 0a 2a 2a 20 76 61 6c 69 64 20 70 61 67 65 20 65 .** valid page e
12561 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 6a 6f ntries in the jo
12562 75 72 6e 61 6c 2e 20 20 49 6e 20 6d 6f 73 74 20 urnal. In most
12563 63 61 73 65 73 2c 20 79 6f 75 20 63 61 6e 20 63 cases, you can c
12564 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 76 61 ompute the.** va
12565 6c 75 65 20 6f 66 20 6e 52 65 63 20 66 72 6f 6d lue of nRec from
12566 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 the size of the
12567 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 journal file.
12568 42 75 74 20 69 66 20 61 20 70 6f 77 65 72 0a 2a But if a power.*
12569 2a 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 * failure occurr
1256a 65 64 20 77 68 69 6c 65 20 74 68 65 20 6a 6f 75 ed while the jou
1256b 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 77 rnal was being w
1256c 72 69 74 74 65 6e 2c 20 69 74 20 63 6f 75 6c 64 ritten, it could
1256d 20 62 65 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 be the.** case
1256e 74 68 61 74 20 74 68 65 20 73 69 7a 65 20 6f 66 that the size of
1256f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c the journal fil
12570 65 20 68 61 64 20 61 6c 72 65 61 64 79 20 62 65 e had already be
12571 65 6e 20 69 6e 63 72 65 61 73 65 64 20 62 75 74 en increased but
12572 0a 2a 2a 20 74 68 65 20 65 78 74 72 61 20 65 6e .** the extra en
12573 74 72 69 65 73 20 68 61 64 20 6e 6f 74 20 79 65 tries had not ye
12574 74 20 6d 61 64 65 20 69 74 20 73 61 66 65 6c 79 t made it safely
12575 20 74 6f 20 64 69 73 6b 2e 20 20 49 6e 20 73 75 to disk. In su
12576 63 68 20 61 20 63 61 73 65 2c 0a 2a 2a 20 74 68 ch a case,.** th
12577 65 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 e value of nRec
12578 63 6f 6d 70 75 74 65 64 20 66 72 6f 6d 20 74 68 computed from th
12579 65 20 66 69 6c 65 20 73 69 7a 65 20 77 6f 75 6c e file size woul
1257a 64 20 62 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 d be too large.
1257b 20 46 6f 72 0a 2a 2a 20 74 68 61 74 20 72 65 61 For.** that rea
1257c 73 6f 6e 2c 20 77 65 20 61 6c 77 61 79 73 20 75 son, we always u
1257d 73 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 se the nRec valu
1257e 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e e in the header.
1257f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 52 .**.** If the nR
12580 65 63 20 76 61 6c 75 65 20 69 73 20 30 78 66 66 ec value is 0xff
12581 66 66 66 66 66 66 20 69 74 20 6d 65 61 6e 73 20 ffffff it means
12582 74 68 61 74 20 6e 52 65 63 20 73 68 6f 75 6c 64 that nRec should
12583 20 62 65 20 63 6f 6d 70 75 74 65 64 0a 2a 2a 20 be computed.**
12584 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 from the file si
12585 7a 65 2e 20 20 54 68 69 73 20 76 61 6c 75 65 20 ze. This value
12586 69 73 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 is used when the
12587 20 75 73 65 72 20 73 65 6c 65 63 74 73 20 74 68 user selects th
12588 65 0a 2a 2a 20 6e 6f 2d 73 79 6e 63 20 6f 70 74 e.** no-sync opt
12589 69 6f 6e 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 ion for the jour
1258a 6e 61 6c 2e 20 20 41 20 70 6f 77 65 72 20 66 61 nal. A power fa
1258b 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 64 ilure could lead
1258c 20 74 6f 20 63 6f 72 72 75 70 74 69 6f 6e 0a 2a to corruption.*
1258d 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 * in this case.
1258e 20 42 75 74 20 66 6f 72 20 74 68 69 6e 67 73 20 But for things
1258f 6c 69 6b 65 20 74 65 6d 70 6f 72 61 72 79 20 74 like temporary t
12590 61 62 6c 65 20 28 77 68 69 63 68 20 77 69 6c 6c able (which will
12591 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 be.** deleted w
12592 68 65 6e 20 74 68 65 20 70 6f 77 65 72 20 69 73 hen the power is
12593 20 72 65 73 74 6f 72 65 64 29 20 77 65 20 64 6f restored) we do
12594 6e 27 74 20 63 61 72 65 2e 20 20 0a 2a 2a 0a 2a n't care. .**.*
12595 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 * If the file op
12596 65 6e 65 64 20 61 73 20 74 68 65 20 6a 6f 75 72 ened as the jour
12597 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 nal file is not
12598 61 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a a well-formed.**
12599 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 journal file th
1259a 65 6e 20 61 6c 6c 20 70 61 67 65 73 20 75 70 20 en all pages up
1259b 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 6f 72 to the first cor
1259c 72 75 70 74 65 64 20 70 61 67 65 20 61 72 65 20 rupted page are
1259d 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b 20 28 rolled.** back (
1259e 6f 72 20 6e 6f 20 70 61 67 65 73 20 69 66 20 74 or no pages if t
1259f 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 he journal heade
125a0 72 20 69 73 20 63 6f 72 72 75 70 74 65 64 29 2e r is corrupted).
125a1 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c The journal fil
125a2 65 0a 2a 2a 20 69 73 20 74 68 65 6e 20 64 65 6c e.** is then del
125a3 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f eted and SQLITE_
125a4 4f 4b 20 72 65 74 75 72 6e 65 64 2c 20 6a 75 73 OK returned, jus
125a5 74 20 61 73 20 69 66 20 6e 6f 20 63 6f 72 72 75 t as if no corru
125a6 70 74 69 6f 6e 20 68 61 64 0a 2a 2a 20 62 65 65 ption had.** bee
125a7 6e 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a n encountered..*
125a8 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 2f 4f 20 6f *.** If an I/O o
125a9 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 r malloc() error
125aa 20 6f 63 63 75 72 73 2c 20 74 68 65 20 6a 6f 75 occurs, the jou
125ab 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 rnal-file is not
125ac 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 6e 64 20 deleted.** and
125ad 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 an error code is
125ae 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
125af 20 54 68 65 20 69 73 48 6f 74 20 70 61 72 61 6d The isHot param
125b0 65 74 65 72 20 69 6e 64 69 63 61 74 65 73 20 74 eter indicates t
125b1 68 61 74 20 77 65 20 61 72 65 20 74 72 79 69 6e hat we are tryin
125b2 67 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 g to rollback a
125b3 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 74 68 61 74 20 journal.** that
125b4 6d 69 67 68 74 20 62 65 20 61 20 68 6f 74 20 6a might be a hot j
125b5 6f 75 72 6e 61 6c 2e 20 20 4f 72 2c 20 69 74 20 ournal. Or, it
125b6 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 could be that th
125b7 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 0a 2a 2a e journal is .**
125b8 20 70 72 65 73 65 72 76 65 64 20 62 65 63 61 75 preserved becau
125b9 73 65 20 6f 66 20 4a 4f 55 52 4e 41 4c 4d 4f 44 se of JOURNALMOD
125ba 45 5f 50 45 52 53 49 53 54 20 6f 72 20 4a 4f 55 E_PERSIST or JOU
125bb 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 RNALMODE_TRUNCAT
125bc 45 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 E..** If the jou
125bd 72 6e 61 6c 20 72 65 61 6c 6c 79 20 69 73 20 68 rnal really is h
125be 6f 74 2c 20 72 65 73 65 74 20 74 68 65 20 70 61 ot, reset the pa
125bf 67 65 72 20 63 61 63 68 65 20 70 72 69 6f 72 20 ger cache prior
125c0 72 6f 6c 6c 69 6e 67 0a 2a 2a 20 62 61 63 6b 20 rolling.** back
125c1 61 6e 79 20 63 6f 6e 74 65 6e 74 2e 20 20 49 66 any content. If
125c2 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
125c3 6d 65 72 65 6c 79 20 70 65 72 73 69 73 74 65 6e merely persisten
125c4 74 2c 20 6e 6f 20 72 65 73 65 74 20 69 73 0a 2a t, no reset is.*
125c5 2a 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 * needed..*/.sta
125c6 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c tic int pager_pl
125c7 61 79 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 ayback(Pager *pP
125c8 61 67 65 72 2c 20 69 6e 74 20 69 73 48 6f 74 29 ager, int isHot)
125c9 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 {. sqlite3_vfs
125ca 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e *pVfs = pPager->
125cb 70 56 66 73 3b 0a 20 20 69 36 34 20 73 7a 4a 3b pVfs;. i64 szJ;
125cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
125cd 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
125ce 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 journal file in
125cf 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 20 6e bytes */. u32 n
125d0 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 Rec;
125d1 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
125d2 20 52 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 Records in the
125d3 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 journal */. u32
125d4 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 u;
125d5 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 /* Unsigne
125d6 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a d loop counter *
125d7 2f 0a 20 20 50 67 6e 6f 20 6d 78 50 67 20 3d 20 /. Pgno mxPg =
125d8 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 0; /*
125d9 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 72 69 67 Size of the orig
125da 69 6e 61 6c 20 66 69 6c 65 20 69 6e 20 70 61 67 inal file in pag
125db 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 es */. int rc;
125dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
125dd 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 /* Result code
125de 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 of a subroutine
125df 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 31 */. int res = 1
125e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
125e1 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 Value returned
125e2 62 79 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 by sqlite3OsAcce
125e3 73 73 28 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a ss() */. char *
125e4 7a 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 20 zMaster = 0;
125e5 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 /* Name of ma
125e6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
125e7 65 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e e if any */. in
125e8 74 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 t needPagerReset
125e9 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 ; /* True t
125ea 6f 20 72 65 73 65 74 20 70 61 67 65 20 70 72 69 o reset page pri
125eb 6f 72 20 74 6f 20 66 69 72 73 74 20 70 61 67 65 or to first page
125ec 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 rollback */..
125ed 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f /* Figure out ho
125ee 77 20 6d 61 6e 79 20 72 65 63 6f 72 64 73 20 61 w many records a
125ef 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 re in the journa
125f0 6c 2e 20 20 41 62 6f 72 74 20 65 61 72 6c 79 20 l. Abort early
125f1 69 66 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 if. ** the jour
125f2 6e 61 6c 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 nal is empty..
125f3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f */. assert( isO
125f4 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 pen(pPager->jfd)
125f5 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
125f6 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
125f7 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b ger->jfd, &szJ);
125f8 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
125f9 45 5f 4f 4b 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29 E_OK || szJ==0 )
125fa 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 {. goto end_p
125fb 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 layback;. }..
125fc 2f 2a 20 52 65 61 64 20 74 68 65 20 6d 61 73 74 /* Read the mast
125fd 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 er journal name
125fe 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c from the journal
125ff 2c 20 69 66 20 69 74 20 69 73 20 70 72 65 73 65 , if it is prese
12600 6e 74 2e 0a 20 20 2a 2a 20 49 66 20 61 20 6d 61 nt.. ** If a ma
12601 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
12602 65 20 6e 61 6d 65 20 69 73 20 73 70 65 63 69 66 e name is specif
12603 69 65 64 2c 20 62 75 74 20 74 68 65 20 66 69 6c ied, but the fil
12604 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 70 72 e is not. ** pr
12605 65 73 65 6e 74 20 6f 6e 20 64 69 73 6b 2c 20 74 esent on disk, t
12606 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hen the journal
12607 69 73 20 6e 6f 74 20 68 6f 74 20 61 6e 64 20 64 is not hot and d
12608 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 oes not need to
12609 62 65 0a 20 20 2a 2a 20 70 6c 61 79 65 64 20 62 be. ** played b
1260a 61 63 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 ack.. **. ** T
1260b 4f 44 4f 3a 20 54 65 63 68 6e 69 63 61 6c 6c 79 ODO: Technically
1260c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 the following i
1260d 73 20 61 6e 20 65 72 72 6f 72 20 62 65 63 61 75 s an error becau
1260e 73 65 20 69 74 20 61 73 73 75 6d 65 73 20 74 68 se it assumes th
1260f 61 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 20 50 at. ** buffer P
12610 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 69 ager.pTmpSpace i
12611 73 20 28 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 s (mxPathname+1)
12612 20 62 79 74 65 73 20 6f 72 20 6c 61 72 67 65 72 bytes or larger
12613 2e 20 69 2e 65 2e 20 74 68 61 74 0a 20 20 2a 2a . i.e. that. **
12614 20 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 (pPager->pageSi
12615 7a 65 20 3e 3d 20 70 50 61 67 65 72 2d 3e 70 56 ze >= pPager->pV
12616 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 fs->mxPathname+1
12617 29 2e 20 55 73 69 6e 67 20 6f 73 5f 75 6e 69 78 ). Using os_unix
12618 2e 63 2c 0a 20 20 2a 2a 20 20 6d 78 50 61 74 68 .c,. ** mxPath
12619 6e 61 6d 65 20 69 73 20 35 31 32 2c 20 77 68 69 name is 512, whi
1261a 63 68 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 ch is the same a
1261b 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c s the minimum al
1261c 6c 6f 77 61 62 6c 65 20 76 61 6c 75 65 0a 20 20 lowable value.
1261d 2a 2a 20 66 6f 72 20 70 61 67 65 53 69 7a 65 2e ** for pageSize.
1261e 0a 20 20 2a 2f 0a 20 20 7a 4d 61 73 74 65 72 20 . */. zMaster
1261f 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 = pPager->pTmpSp
12620 61 63 65 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 ace;. rc = read
12621 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 MasterJournal(pP
12622 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 ager->jfd, zMast
12623 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 er, pPager->pVfs
12624 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b ->mxPathname+1);
12625 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
12626 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b E_OK && zMaster[
12627 30 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 0] ){. rc = s
12628 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 qlite3OsAccess(p
12629 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 Vfs, zMaster, SQ
1262a 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 LITE_ACCESS_EXIS
1262b 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 7d 0a 20 TS, &res);. }.
1262c 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 zMaster = 0;.
1262d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1262e 4b 20 7c 7c 20 21 72 65 73 20 29 7b 0a 20 20 20 K || !res ){.
1262f 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 goto end_playba
12630 63 6b 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 ck;. }. pPager
12631 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
12632 3b 0a 20 20 6e 65 65 64 50 61 67 65 72 52 65 73 ;. needPagerRes
12633 65 74 20 3d 20 69 73 48 6f 74 3b 0a 0a 20 20 2f et = isHot;.. /
12634 2a 20 54 68 69 73 20 6c 6f 6f 70 20 74 65 72 6d * This loop term
12635 69 6e 61 74 65 73 20 65 69 74 68 65 72 20 77 68 inates either wh
12636 65 6e 20 61 20 72 65 61 64 4a 6f 75 72 6e 61 6c en a readJournal
12637 48 64 72 28 29 20 6f 72 20 0a 20 20 2a 2a 20 70 Hdr() or . ** p
12638 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e ager_playback_on
12639 65 5f 70 61 67 65 28 29 20 63 61 6c 6c 20 72 65 e_page() call re
1263a 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e turns SQLITE_DON
1263b 45 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 E or an IO error
1263c 20 0a 20 20 2a 2a 20 6f 63 63 75 72 73 2e 20 0a . ** occurs. .
1263d 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 31 20 */. while( 1
1263e 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 55 6e 73 ){. int isUns
1263f 79 6e 63 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a ync = 0;.. /*
12640 20 52 65 61 64 20 74 68 65 20 6e 65 78 74 20 6a Read the next j
12641 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 72 ournal header fr
12642 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 om the journal f
12643 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65 20 61 ile. If there a
12644 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 65 6e re. ** not en
12645 6f 75 67 68 20 62 79 74 65 73 20 6c 65 66 74 20 ough bytes left
12646 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 in the journal f
12647 69 6c 65 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 ile for a comple
12648 74 65 20 68 65 61 64 65 72 2c 20 6f 72 0a 20 20 te header, or.
12649 20 20 2a 2a 20 69 74 20 69 73 20 63 6f 72 72 75 ** it is corru
1264a 70 74 65 64 2c 20 74 68 65 6e 20 61 20 70 72 6f pted, then a pro
1264b 63 65 73 73 20 6d 75 73 74 20 6f 66 20 66 61 69 cess must of fai
1264c 6c 65 64 20 77 68 69 6c 65 20 77 72 69 74 69 6e led while writin
1264d 67 20 69 74 2e 0a 20 20 20 20 2a 2a 20 54 68 69 g it.. ** Thi
1264e 73 20 69 6e 64 69 63 61 74 65 73 20 6e 6f 74 68 s indicates noth
1264f 69 6e 67 20 6d 6f 72 65 20 6e 65 65 64 73 20 74 ing more needs t
12650 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b o be rolled back
12651 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
12652 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 = readJournalHdr
12653 28 70 50 61 67 65 72 2c 20 69 73 48 6f 74 2c 20 (pPager, isHot,
12654 73 7a 4a 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50 szJ, &nRec, &mxP
12655 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d g);. if( rc!=
12656 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 SQLITE_OK ){ .
12657 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12658 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 TE_DONE ){.
12659 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f rc = SQLITE_O
1265a 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 K;. }.
1265b 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 goto end_playba
1265c 63 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f ck;. }.. /
1265d 2a 20 49 66 20 6e 52 65 63 20 69 73 20 30 78 66 * If nRec is 0xf
1265e 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 74 68 fffffff, then th
1265f 69 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63 is journal was c
12660 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 reated by a proc
12661 65 73 73 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69 ess. ** worki
12662 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f ng in no-sync mo
12663 64 65 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 de. This means t
12664 68 61 74 20 74 68 65 20 72 65 73 74 20 6f 66 20 hat the rest of
12665 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
12666 2a 2a 20 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 ** file consists
12667 20 6f 66 20 70 61 67 65 73 2c 20 74 68 65 72 65 of pages, there
12668 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 are no more jou
12669 72 6e 61 6c 20 68 65 61 64 65 72 73 2e 20 43 6f rnal headers. Co
1266a 6d 70 75 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 mpute. ** the
1266b 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63 20 62 value of nRec b
1266c 61 73 65 64 20 6f 6e 20 74 68 69 73 20 61 73 73 ased on this ass
1266d 75 6d 70 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a umption.. */.
1266e 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 78 if( nRec==0x
1266f 66 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 ffffffff ){.
12670 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12671 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f ->journalOff==JO
12672 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 URNAL_HDR_SZ(pPa
12673 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 6e 52 ger) );. nR
12674 65 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 ec = (int)((szJ
12675 2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a - JOURNAL_HDR_SZ
12676 28 70 50 61 67 65 72 29 29 2f 4a 4f 55 52 4e 41 (pPager))/JOURNA
12677 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 29 L_PG_SZ(pPager))
12678 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
12679 49 66 20 6e 52 65 63 20 69 73 20 30 20 61 6e 64 If nRec is 0 and
1267a 20 74 68 69 73 20 72 6f 6c 6c 62 61 63 6b 20 69 this rollback i
1267b 73 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 s of a transacti
1267c 6f 6e 20 63 72 65 61 74 65 64 20 62 79 20 74 68 on created by th
1267d 69 73 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 is. ** proces
1267e 73 20 61 6e 64 20 69 66 20 74 68 69 73 20 69 73 s and if this is
1267f 20 74 68 65 20 66 69 6e 61 6c 20 68 65 61 64 65 the final heade
12680 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c r in the journal
12681 2c 20 74 68 65 6e 20 69 74 20 6d 65 61 6e 73 0a , then it means.
12682 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 69 73 ** that this
12683 20 70 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 part of the jou
12684 72 6e 61 6c 20 77 61 73 20 62 65 69 6e 67 20 66 rnal was being f
12685 69 6c 6c 65 64 20 62 75 74 20 68 61 73 20 6e 6f illed but has no
12686 74 20 79 65 74 20 62 65 65 6e 0a 20 20 20 20 2a t yet been. *
12687 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b * synced to disk
12688 2e 20 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6e . Compute the n
12689 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 62 umber of pages b
1268a 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65 6d 61 ased on the rema
1268b 69 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 73 69 7a ining. ** siz
1268c 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 20 e of the file..
1268d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 **. ** The
1268e 20 74 68 69 72 64 20 74 65 72 6d 20 6f 66 20 74 third term of t
1268f 68 65 20 74 65 73 74 20 77 61 73 20 61 64 64 65 he test was adde
12690 64 20 74 6f 20 66 69 78 20 74 69 63 6b 65 74 20 d to fix ticket
12691 23 32 35 36 35 2e 0a 20 20 20 20 2a 2a 20 57 68 #2565.. ** Wh
12692 65 6e 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 en rolling back
12693 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e a hot journal, n
12694 52 65 63 3d 3d 30 20 61 6c 77 61 79 73 20 6d 65 Rec==0 always me
12695 61 6e 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 ans that the nex
12696 74 0a 20 20 20 20 2a 2a 20 63 68 75 6e 6b 20 6f t. ** chunk o
12697 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 63 6f f the journal co
12698 6e 74 61 69 6e 73 20 7a 65 72 6f 20 70 61 67 65 ntains zero page
12699 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 s to be rolled b
1269a 61 63 6b 2e 20 20 42 75 74 0a 20 20 20 20 2a 2a ack. But. **
1269b 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20 52 4f when doing a RO
1269c 4c 4c 42 41 43 4b 20 61 6e 64 20 74 68 65 20 6e LLBACK and the n
1269d 52 65 63 3d 3d 30 20 63 68 75 6e 6b 20 69 73 20 Rec==0 chunk is
1269e 74 68 65 20 6c 61 73 74 20 63 68 75 6e 6b 20 69 the last chunk i
1269f 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 n. ** the jou
126a0 72 6e 61 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 rnal, it means t
126a1 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 hat the journal
126a2 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 64 might contain ad
126a3 64 69 74 69 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 ditional. **
126a4 70 61 67 65 73 20 74 68 61 74 20 6e 65 65 64 20 pages that need
126a5 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 to be rolled bac
126a6 6b 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 6e k and that the n
126a7 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 0a umber of pages .
126a8 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 ** should be
126a9 20 63 6f 6d 70 75 74 65 64 20 62 61 73 65 64 20 computed based
126aa 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 on the journal f
126ab 69 6c 65 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f ile size.. */
126ac 0a 20 20 20 20 69 66 28 20 6e 52 65 63 3d 3d 30 . if( nRec==0
126ad 20 26 26 20 21 69 73 48 6f 74 20 26 26 0a 20 20 && !isHot &&.
126ae 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f pPager->jo
126af 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c urnalHdr+JOURNAL
126b0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d _HDR_SZ(pPager)=
126b1 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c =pPager->journal
126b2 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 6e 52 65 Off ){. nRe
126b3 63 20 3d 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d c = (int)((szJ -
126b4 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
126b5 4f 66 66 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 Off) / JOURNAL_P
126b6 47 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 G_SZ(pPager));.
126b7 20 20 20 20 20 69 73 55 6e 73 79 6e 63 20 3d 20 isUnsync =
126b8 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 1;. }.. /*
126b9 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
126ba 66 69 72 73 74 20 68 65 61 64 65 72 20 72 65 61 first header rea
126bb 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e d from the journ
126bc 61 6c 2c 20 74 72 75 6e 63 61 74 65 20 74 68 65 al, truncate the
126bd 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 . ** database
126be 20 66 69 6c 65 20 62 61 63 6b 20 74 6f 20 69 74 file back to it
126bf 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e s original size.
126c0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
126c1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
126c2 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f ff==JOURNAL_HDR_
126c3 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 SZ(pPager) ){.
126c4 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 rc = pager_t
126c5 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 runcate(pPager,
126c6 6d 78 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 mxPg);. if(
126c7 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
126c8 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 {. goto e
126c9 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 nd_playback;.
126ca 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 }. pPage
126cb 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6d 78 50 67 r->dbSize = mxPg
126cc 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
126cd 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61 Copy original pa
126ce 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6a ges out of the j
126cf 6f 75 72 6e 61 6c 20 61 6e 64 20 62 61 63 6b 20 ournal and back
126d0 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 2a 2a into the . **
126d1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
126d2 6e 64 2f 6f 72 20 70 61 67 65 20 63 61 63 68 65 nd/or page cache
126d3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 .. */. for
126d4 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75 2b (u=0; u<nRec; u+
126d5 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 +){. if( ne
126d6 65 64 50 61 67 65 72 52 65 73 65 74 20 29 7b 0a edPagerReset ){.
126d7 20 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 pager_re
126d8 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 set(pPager);.
126d9 20 20 20 20 20 6e 65 65 64 50 61 67 65 72 52 65 needPagerRe
126da 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d set = 0;. }
126db 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
126dc 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 r_playback_one_p
126dd 61 67 65 28 70 50 61 67 65 72 2c 31 2c 69 73 55 age(pPager,1,isU
126de 6e 73 79 6e 63 2c 26 70 50 61 67 65 72 2d 3e 6a nsync,&pPager->j
126df 6f 75 72 6e 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a ournalOff,0,0);.
126e0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
126e1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
126e2 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
126e3 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 E_DONE ){.
126e4 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
126e5 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 OK;. pP
126e6 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 ager->journalOff
126e7 20 3d 20 73 7a 4a 3b 0a 20 20 20 20 20 20 20 20 = szJ;.
126e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
126e9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
126ea 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 75 /* If we are u
126eb 6e 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 nable to rollbac
126ec 6b 2c 20 71 75 69 74 20 61 6e 64 20 72 65 74 75 k, quit and retu
126ed 72 6e 20 74 68 65 20 65 72 72 6f 72 0a 20 20 20 rn the error.
126ee 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 2e 20 ** code.
126ef 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 This will cause
126f0 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e the pager to en
126f1 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 ter the error st
126f2 61 74 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ate. **
126f3 20 73 6f 20 74 68 61 74 20 6e 6f 20 66 75 72 74 so that no furt
126f4 68 65 72 20 68 61 72 6d 20 77 69 6c 6c 20 62 65 her harm will be
126f5 20 64 6f 6e 65 2e 20 20 50 65 72 68 61 70 73 20 done. Perhaps
126f6 74 68 65 20 6e 65 78 74 0a 20 20 20 20 20 20 20 the next.
126f7 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 74 6f ** process to
126f8 20 63 6f 6d 65 20 61 6c 6f 6e 67 20 77 69 6c 6c come along will
126f9 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c be able to roll
126fa 62 61 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 back the databas
126fb 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a e.. */.
126fc 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
126fd 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 nd_playback;.
126fe 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
126ff 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 }. }. /*NOT
12700 52 45 41 43 48 45 44 2a 2f 0a 20 20 61 73 73 65 REACHED*/. asse
12701 72 74 28 20 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c rt( 0 );..end_pl
12702 61 79 62 61 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c ayback:. /* Fol
12703 6c 6f 77 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 lowing a rollbac
12704 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 k, the database
12705 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 62 file should be b
12706 61 63 6b 20 69 6e 20 69 74 73 20 6f 72 69 67 69 ack in its origi
12707 6e 61 6c 0a 20 20 2a 2a 20 73 74 61 74 65 20 70 nal. ** state p
12708 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72 rior to the star
12709 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 t of the transac
1270a 74 69 6f 6e 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 tion, so invoke
1270b 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f the. ** SQLITE_
1270c 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 FCNTL_DB_UNCHANG
1270d 45 44 20 66 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 ED file-control
1270e 6d 65 74 68 6f 64 20 74 6f 20 64 69 73 61 62 6c method to disabl
1270f 65 20 74 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 e the. ** asser
12710 74 69 6f 6e 20 74 68 61 74 20 74 68 65 20 74 72 tion that the tr
12711 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 ansaction counte
12712 72 20 77 61 73 20 6d 6f 64 69 66 69 65 64 2e 0a r was modified..
12713 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 0a 20 */. assert(.
12714 20 20 20 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 pPager->fd->p
12715 4d 65 74 68 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20 Methods==0 ||.
12716 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 sqlite3OsFileC
12717 6f 6e 74 72 6f 6c 28 70 50 61 67 65 72 2d 3e 66 ontrol(pPager->f
12718 64 2c 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 d,SQLITE_FCNTL_D
12719 42 5f 55 4e 43 48 41 4e 47 45 44 2c 30 29 3e 3d B_UNCHANGED,0)>=
1271a 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a SQLITE_OK. );..
1271b 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 6c 61 /* If this pla
1271c 79 62 61 63 6b 20 69 73 20 68 61 70 70 65 6e 69 yback is happeni
1271d 6e 67 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ng automatically
1271e 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 as a result of
1271f 61 6e 20 49 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d an IO or . ** m
12720 61 6c 6c 6f 63 20 65 72 72 6f 72 20 74 68 61 74 alloc error that
12721 20 6f 63 63 75 72 72 65 64 20 61 66 74 65 72 20 occurred after
12722 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 the change-count
12723 65 72 20 77 61 73 20 75 70 64 61 74 65 64 20 62 er was updated b
12724 75 74 20 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20 ut . ** before
12725 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
12726 77 61 73 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 was committed, t
12727 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 hen the change-c
12728 6f 75 6e 74 65 72 20 0a 20 20 2a 2a 20 6d 6f 64 ounter . ** mod
12729 69 66 69 63 61 74 69 6f 6e 20 6d 61 79 20 6a 75 ification may ju
1272a 73 74 20 68 61 76 65 20 62 65 65 6e 20 72 65 76 st have been rev
1272b 65 72 74 65 64 2e 20 49 66 20 74 68 69 73 20 68 erted. If this h
1272c 61 70 70 65 6e 73 20 69 6e 20 65 78 63 6c 75 73 appens in exclus
1272d 69 76 65 20 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20 ive . ** mode,
1272e 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 then subsequent
1272f 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 70 65 72 transactions per
12730 66 6f 72 6d 65 64 20 62 79 20 74 68 65 20 63 6f formed by the co
12731 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f nnection will no
12732 74 0a 20 20 2a 2a 20 75 70 64 61 74 65 20 74 68 t. ** update th
12733 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 e change-counter
12734 20 61 74 20 61 6c 6c 2e 20 54 68 69 73 20 6d 61 at all. This ma
12735 79 20 6c 65 61 64 20 74 6f 20 63 61 63 68 65 20 y lead to cache
12736 69 6e 63 6f 6e 73 69 73 74 65 6e 63 79 0a 20 20 inconsistency.
12737 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 ** problems for
12738 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 other processes
12739 61 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e at some point in
1273a 20 74 68 65 20 66 75 74 75 72 65 2e 20 53 6f 2c the future. So,
1273b 20 6a 75 73 74 0a 20 20 2a 2a 20 69 6e 20 63 61 just. ** in ca
1273c 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70 70 se this has happ
1273d 65 6e 65 64 2c 20 63 6c 65 61 72 20 74 68 65 20 ened, clear the
1273e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 changeCountDone
1273f 66 6c 61 67 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 flag now.. */.
12740 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 pPager->changeC
12741 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 ountDone = pPage
12742 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 0a 20 20 r->tempFile;..
12743 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
12744 4b 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 K ){. zMaster
12745 20 3d 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 = pPager->pTmpS
12746 70 61 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72 pace;. rc = r
12747 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c eadMasterJournal
12748 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d (pPager->jfd, zM
12749 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 aster, pPager->p
1274a 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b Vfs->mxPathname+
1274b 31 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 1);. testcase
1274c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
1274d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
1274e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1274f 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 rc = pager_end
12750 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 _transaction(pPa
12751 67 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 ger, zMaster[0]!
12752 3d 27 5c 30 27 29 3b 0a 20 20 20 20 74 65 73 74 ='\0');. test
12753 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 case( rc!=SQLITE
12754 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 _OK );. }. if(
12755 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
12756 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20 & zMaster[0] &&
12757 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 res ){. /* If
12758 20 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73 there was a mas
12759 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 ter journal and
1275a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
1275b 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73 l return success
1275c 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 ,. ** see if
1275d 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
1275e 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 o delete the mas
1275f 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 ter journal..
12760 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 */. rc = pag
12761 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61 er_delmaster(pPa
12762 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 ger, zMaster);.
12763 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 testcase( rc!
12764 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
12765 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67 65 }.. /* The Page
12766 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 r.sectorSize var
12767 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20 62 iable may have b
12768 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 69 6c een updated whil
12769 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62 e rolling. ** b
1276a 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63 72 ack a journal cr
1276b 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 eated by a proce
1276c 73 73 20 77 69 74 68 20 61 20 64 69 66 66 65 72 ss with a differ
1276d 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65 0a ent sector size.
1276e 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73 65 ** value. Rese
1276f 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72 72 t it to the corr
12770 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ect value for th
12771 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a 2f is process.. */
12772 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 . setSectorSize
12773 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 (pPager);. retu
12774 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
12775 50 6c 61 79 62 61 63 6b 20 73 61 76 65 70 6f 69 Playback savepoi
12776 6e 74 20 70 53 61 76 65 70 6f 69 6e 74 2e 20 4f nt pSavepoint. O
12777 72 2c 20 69 66 20 70 53 61 76 65 70 6f 69 6e 74 r, if pSavepoint
12778 3d 3d 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 ==NULL, then pla
12779 79 62 61 63 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 yback.** the ent
1277a 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e ire master journ
1277b 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 63 61 73 al file. The cas
1277c 65 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 e pSavepoint==NU
1277d 4c 4c 20 6f 63 63 75 72 73 20 77 68 65 6e 20 0a LL occurs when .
1277e 2a 2a 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f ** a ROLLBACK TO
1277f 20 63 6f 6d 6d 61 6e 64 20 69 73 20 69 6e 76 6f command is invo
12780 6b 65 64 20 6f 6e 20 61 20 53 41 56 45 50 4f 49 ked on a SAVEPOI
12781 4e 54 20 74 68 61 74 20 69 73 20 61 20 74 72 61 NT that is a tra
12782 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 nsaction .** sav
12783 65 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 epoint..**.** Wh
12784 65 6e 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 en pSavepoint is
12785 20 6e 6f 74 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 not NULL (meani
12786 6e 67 20 61 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 ng a non-transac
12787 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 tion savepoint i
12788 73 20 0a 2a 2a 20 62 65 69 6e 67 20 72 6f 6c 6c s .** being roll
12789 65 64 20 62 61 63 6b 29 2c 20 74 68 65 6e 20 74 ed back), then t
1278a 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 he rollback cons
1278b 69 73 74 73 20 6f 66 20 75 70 20 74 6f 20 74 68 ists of up to th
1278c 72 65 65 20 73 74 61 67 65 73 2c 0a 2a 2a 20 70 ree stages,.** p
1278d 65 72 66 6f 72 6d 65 64 20 69 6e 20 74 68 65 20 erformed in the
1278e 6f 72 64 65 72 20 73 70 65 63 69 66 69 65 64 3a order specified:
1278f 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 .**.** * Pages
12790 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b are played back
12791 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a from the main j
12792 6f 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 ournal starting
12793 61 74 20 62 79 74 65 0a 2a 2a 20 20 20 20 20 6f at byte.** o
12794 66 66 73 65 74 20 50 61 67 65 72 53 61 76 65 70 ffset PagerSavep
12795 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 oint.iOffset and
12796 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 0a continuing to .
12797 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 ** PagerSave
12798 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 point.iHdrOffset
12799 2c 20 6f 72 20 74 6f 20 74 68 65 20 65 6e 64 20 , or to the end
1279a 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
1279b 6e 61 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 20 nal.** file
1279c 69 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e if PagerSavepoin
1279d 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 t.iHdrOffset is
1279e 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 zero..**.** *
1279f 49 66 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e If PagerSavepoin
127a0 74 2e 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 t.iHdrOffset is
127a1 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 70 not zero, then p
127a2 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64 0a ages are played.
127a3 2a 2a 20 20 20 20 20 62 61 63 6b 20 73 74 61 72 ** back star
127a4 74 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f ting from the jo
127a5 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 6d 6d urnal header imm
127a6 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 ediately followi
127a7 6e 67 20 0a 2a 2a 20 20 20 20 20 50 61 67 65 72 ng .** Pager
127a8 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 Savepoint.iHdrOf
127a9 66 73 65 74 20 74 6f 20 74 68 65 20 65 6e 64 20 fset to the end
127aa 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 of the main jour
127ab 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 nal file..**.**
127ac 20 20 2a 20 50 61 67 65 73 20 61 72 65 20 74 68 * Pages are th
127ad 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 en played back f
127ae 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 rom the sub-jour
127af 6e 61 6c 20 66 69 6c 65 2c 20 73 74 61 72 74 69 nal file, starti
127b0 6e 67 0a 2a 2a 20 20 20 20 20 77 69 74 68 20 74 ng.** with t
127b1 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
127b2 74 2e 69 53 75 62 52 65 63 20 61 6e 64 20 63 6f t.iSubRec and co
127b3 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 ntinuing to the
127b4 65 6e 64 20 6f 66 0a 2a 2a 20 20 20 20 20 74 68 end of.** th
127b5 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a e journal file..
127b6 2a 2a 0a 2a 2a 20 54 68 72 6f 75 67 68 6f 75 74 **.** Throughout
127b7 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 70 72 the rollback pr
127b8 6f 63 65 73 73 2c 20 65 61 63 68 20 74 69 6d 65 ocess, each time
127b9 20 61 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 a page is rolle
127ba 64 20 62 61 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 d back, the.** c
127bb 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 orresponding bit
127bc 20 69 73 20 73 65 74 20 69 6e 20 61 20 62 69 74 is set in a bit
127bd 76 65 63 20 73 74 72 75 63 74 75 72 65 20 28 76 vec structure (v
127be 61 72 69 61 62 6c 65 20 70 44 6f 6e 65 20 69 6e ariable pDone in
127bf 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
127c0 74 61 74 69 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 tation below). T
127c1 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 65 his is used to e
127c2 6e 73 75 72 65 20 74 68 61 74 20 61 20 70 61 67 nsure that a pag
127c3 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c e is only.** rol
127c4 6c 65 64 20 62 61 63 6b 20 74 68 65 20 66 69 72 led back the fir
127c5 73 74 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e st time it is en
127c6 63 6f 75 6e 74 65 72 65 64 20 69 6e 20 65 69 74 countered in eit
127c7 68 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a her journal..**.
127c8 2a 2a 20 49 66 20 70 53 61 76 65 70 6f 69 6e 74 ** If pSavepoint
127c9 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 is NULL, then p
127ca 61 67 65 73 20 61 72 65 20 6f 6e 6c 79 20 70 6c ages are only pl
127cb 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 ayed back from t
127cc 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e he main.** journ
127cd 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 20 69 al file. There i
127ce 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 61 20 s no need for a
127cf 62 69 74 76 65 63 20 69 6e 20 74 68 69 73 20 63 bitvec in this c
127d0 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 ase..**.** In ei
127d1 74 68 65 72 20 63 61 73 65 2c 20 62 65 66 6f 72 ther case, befor
127d2 65 20 70 6c 61 79 62 61 63 6b 20 63 6f 6d 6d 65 e playback comme
127d3 6e 63 65 73 20 74 68 65 20 50 61 67 65 72 2e 64 nces the Pager.d
127d4 62 53 69 7a 65 20 76 61 72 69 61 62 6c 65 0a 2a bSize variable.*
127d5 2a 20 69 73 20 72 65 73 65 74 20 74 6f 20 74 68 * is reset to th
127d6 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 74 20 e value that it
127d7 68 65 6c 64 20 61 74 20 74 68 65 20 73 74 61 72 held at the star
127d8 74 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69 t of the savepoi
127d9 6e 74 20 0a 2a 2a 20 28 6f 72 20 74 72 61 6e 73 nt .** (or trans
127da 61 63 74 69 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 action). No page
127db 20 77 69 74 68 20 61 20 70 61 67 65 2d 6e 75 6d with a page-num
127dc 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61 6e ber greater than
127dd 20 74 68 69 73 20 76 61 6c 75 65 0a 2a 2a 20 69 this value.** i
127de 73 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 49 s played back. I
127df 66 20 6f 6e 65 20 69 73 20 65 6e 63 6f 75 6e 74 f one is encount
127e0 65 72 65 64 20 69 74 20 69 73 20 73 69 6d 70 6c ered it is simpl
127e1 79 20 73 6b 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 y skipped..*/.st
127e2 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 50 6c atic int pagerPl
127e3 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 aybackSavepoint(
127e4 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
127e5 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 agerSavepoint *p
127e6 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 36 Savepoint){. i6
127e7 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 4 szJ;
127e8 20 20 20 20 20 20 20 2f 2a 20 45 66 66 65 63 74 /* Effect
127e9 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 ive size of the
127ea 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a main journal */.
127eb 20 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 i64 iHdrOff;
127ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e /* En
127ed 64 20 6f 66 20 66 69 72 73 74 20 73 65 67 6d 65 d of first segme
127ee 6e 74 20 6f 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e nt of main-journ
127ef 61 6c 20 72 65 63 6f 72 64 73 20 2a 2f 0a 20 20 al records */.
127f0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
127f1 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 OK; /* Retu
127f2 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 69 74 rn code */. Bit
127f3 76 65 63 20 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 vec *pDone = 0;
127f4 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63 20 /* Bitvec
127f5 74 6f 20 65 6e 73 75 72 65 20 70 61 67 65 73 20 to ensure pages
127f6 70 6c 61 79 65 64 20 62 61 63 6b 20 6f 6e 6c 79 played back only
127f7 20 6f 6e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 once */.. asse
127f8 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
127f9 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e>=PAGER_SHARED
127fa 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 );.. /* Allocat
127fb 65 20 61 20 62 69 74 76 65 63 20 74 6f 20 75 73 e a bitvec to us
127fc 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 e to store the s
127fd 65 74 20 6f 66 20 70 61 67 65 73 20 72 6f 6c 6c et of pages roll
127fe 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 ed back */. if(
127ff 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 pSavepoint ){.
12800 20 20 20 70 44 6f 6e 65 20 3d 20 73 71 6c 69 74 pDone = sqlit
12801 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 e3BitvecCreate(p
12802 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 Savepoint->nOrig
12803 29 3b 0a 20 20 20 20 69 66 28 20 21 70 44 6f 6e );. if( !pDon
12804 65 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 e ){. retur
12805 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
12806 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
12807 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 Set the database
12808 20 73 69 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 size back to th
12809 65 20 76 61 6c 75 65 20 69 74 20 77 61 73 20 62 e value it was b
1280a 65 66 6f 72 65 20 74 68 65 20 73 61 76 65 70 6f efore the savepo
1280b 69 6e 74 20 0a 20 20 2a 2a 20 62 65 69 6e 67 20 int . ** being
1280c 72 65 76 65 72 74 65 64 20 77 61 73 20 6f 70 65 reverted was ope
1280d 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 ned.. */. pPag
1280e 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 53 61 er->dbSize = pSa
1280f 76 65 70 6f 69 6e 74 20 3f 20 70 53 61 76 65 70 vepoint ? pSavep
12810 6f 69 6e 74 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 oint->nOrig : pP
12811 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
12812 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 70 50 61 67 ;.. /* Use pPag
12813 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 61 er->journalOff a
12814 73 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 s the effective
12815 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e size of the main
12816 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a rollback. ** j
12817 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 61 63 74 ournal. The act
12818 75 61 6c 20 66 69 6c 65 20 6d 69 67 68 74 20 62 ual file might b
12819 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 e larger than th
1281a 69 73 20 69 6e 0a 20 20 2a 2a 20 50 41 47 45 52 is in. ** PAGER
1281b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 _JOURNALMODE_TRU
1281c 4e 43 41 54 45 20 6f 72 20 50 41 47 45 52 5f 4a NCATE or PAGER_J
1281d 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
1281e 53 54 2e 20 20 42 75 74 20 61 6e 79 74 68 69 6e ST. But anythin
1281f 67 0a 20 20 2a 2a 20 70 61 73 74 20 70 50 61 67 g. ** past pPag
12820 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 er->journalOff i
12821 73 20 6f 66 66 2d 6c 69 6d 69 74 73 20 74 6f 20 s off-limits to
12822 75 73 2e 0a 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d us.. */. szJ =
12823 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12824 4f 66 66 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e Off;.. /* Begin
12825 20 62 79 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b by rolling back
12826 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 records from th
12827 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 e main journal s
12828 74 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 tarting at. **
12829 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 PagerSavepoint.i
1282a 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 Offset and conti
1282b 6e 75 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 78 nuing to the nex
1282c 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 t journal header
1282d 2e 0a 20 20 2a 2a 20 54 68 65 72 65 20 6d 69 67 .. ** There mig
1282e 68 74 20 62 65 20 72 65 63 6f 72 64 73 20 69 6e ht be records in
1282f 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 the main journa
12830 6c 20 74 68 61 74 20 68 61 76 65 20 61 20 70 61 l that have a pa
12831 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 67 ge number. ** g
12832 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
12833 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73 65 current database
12834 20 73 69 7a 65 20 28 70 50 61 67 65 72 2d 3e 64 size (pPager->d
12835 62 53 69 7a 65 29 20 62 75 74 20 74 68 6f 73 65 bSize) but those
12836 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b . ** will be sk
12837 69 70 70 65 64 20 61 75 74 6f 6d 61 74 69 63 61 ipped automatica
12838 6c 6c 79 2e 20 20 50 61 67 65 73 20 61 72 65 20 lly. Pages are
12839 61 64 64 65 64 20 74 6f 20 70 44 6f 6e 65 20 61 added to pDone a
1283a 73 20 74 68 65 79 0a 20 20 2a 2a 20 61 72 65 20 s they. ** are
1283b 70 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a played back.. *
1283c 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 /. if( pSavepoi
1283d 6e 74 20 29 7b 0a 20 20 20 20 69 48 64 72 4f 66 nt ){. iHdrOf
1283e 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e f = pSavepoint->
1283f 69 48 64 72 4f 66 66 73 65 74 20 3f 20 70 53 61 iHdrOffset ? pSa
12840 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 vepoint->iHdrOff
12841 73 65 74 20 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 set : szJ;. p
12842 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
12843 66 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e f = pSavepoint->
12844 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69 iOffset;. whi
12845 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f le( rc==SQLITE_O
12846 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 K && pPager->jou
12847 72 6e 61 6c 4f 66 66 3c 69 48 64 72 4f 66 66 20 rnalOff<iHdrOff
12848 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 ){. rc = pa
12849 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 ger_playback_one
1284a 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c _page(pPager, 1,
1284b 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75 0, &pPager->jou
1284c 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e rnalOff, 1, pDon
1284d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 e);. }. as
1284e 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 sert( rc!=SQLITE
1284f 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 65 6c 73 65 _DONE );. }else
12850 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f {. pPager->jo
12851 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 urnalOff = 0;.
12852 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 }.. /* Continue
12853 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 rolling back re
12854 63 6f 72 64 73 20 6f 75 74 20 6f 66 20 74 68 65 cords out of the
12855 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 main journal st
12856 61 72 74 69 6e 67 20 61 74 0a 20 20 2a 2a 20 74 arting at. ** t
12857 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c he first journal
12858 20 68 65 61 64 65 72 20 73 65 65 6e 20 61 6e 64 header seen and
12859 20 63 6f 6e 74 69 6e 75 69 6e 67 20 75 6e 74 69 continuing unti
1285a 6c 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 l the effective
1285b 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 end. ** of the
1285c 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c main journal fil
1285d 65 2e 20 20 43 6f 6e 74 69 6e 75 65 20 74 6f 20 e. Continue to
1285e 73 6b 69 70 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 skip out-of-rang
1285f 65 20 70 61 67 65 73 20 61 6e 64 0a 20 20 2a 2a e pages and. **
12860 20 63 6f 6e 74 69 6e 75 65 20 61 64 64 69 6e 67 continue adding
12861 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62 61 pages rolled ba
12862 63 6b 20 74 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a ck to pDone.. *
12863 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 /. while( rc==S
12864 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 QLITE_OK && pPag
12865 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 er->journalOff<s
12866 7a 4a 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69 zJ ){. u32 ii
12867 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
12868 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
12869 20 20 20 20 75 33 32 20 6e 4a 52 65 63 20 3d 20 u32 nJRec =
1286a 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 0; /* Number
1286b 20 6f 66 20 4a 6f 75 72 6e 61 6c 20 52 65 63 6f of Journal Reco
1286c 72 64 73 20 2a 2f 0a 20 20 20 20 75 33 32 20 64 rds */. u32 d
1286d 75 6d 6d 79 3b 0a 20 20 20 20 72 63 20 3d 20 72 ummy;. rc = r
1286e 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 eadJournalHdr(pP
1286f 61 67 65 72 2c 20 30 2c 20 73 7a 4a 2c 20 26 6e ager, 0, szJ, &n
12870 4a 52 65 63 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 JRec, &dummy);.
12871 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 assert( rc!=S
12872 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 QLITE_DONE );..
12873 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 /*. ** The
12874 20 22 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 "pPager->journa
12875 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 lHdr+JOURNAL_HDR
12876 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 _SZ(pPager)==pPa
12877 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 ger->journalOff"
12878 0a 20 20 20 20 2a 2a 20 74 65 73 74 20 69 73 20 . ** test is
12879 72 65 6c 61 74 65 64 20 74 6f 20 74 69 63 6b 65 related to ticke
1287a 74 20 23 32 35 36 35 2e 20 20 53 65 65 20 74 68 t #2565. See th
1287b 65 20 64 69 73 63 75 73 73 69 6f 6e 20 69 6e 20 e discussion in
1287c 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 the. ** pager
1287d 5f 70 6c 61 79 62 61 63 6b 28 29 20 66 75 6e 63 _playback() func
1287e 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f tion for additio
1287f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e nal information.
12880 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
12881 6e 4a 52 65 63 3d 3d 30 20 0a 20 20 20 20 20 26 nJRec==0 . &
12882 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 & pPager->journa
12883 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 lHdr+JOURNAL_HDR
12884 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61 _SZ(pPager)==pPa
12885 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a ger->journalOff.
12886 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 4a 52 ){. nJR
12887 65 63 20 3d 20 28 75 33 32 29 28 28 73 7a 4a 20 ec = (u32)((szJ
12888 2d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 - pPager->journa
12889 6c 4f 66 66 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 lOff)/JOURNAL_PG
1288a 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20 _SZ(pPager));.
1288b 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 }. for(ii=0
1288c 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ; rc==SQLITE_OK
1288d 26 26 20 69 69 3c 6e 4a 52 65 63 20 26 26 20 70 && ii<nJRec && p
1288e 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
1288f 66 3c 73 7a 4a 3b 20 69 69 2b 2b 29 7b 0a 20 20 f<szJ; ii++){.
12890 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
12891 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 layback_one_page
12892 28 70 50 61 67 65 72 2c 20 31 2c 20 30 2c 20 26 (pPager, 1, 0, &
12893 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
12894 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 ff, 1, pDone);.
12895 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
12896 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc!=SQLITE_DONE
12897 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 );. }. assert
12898 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
12899 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e || pPager->journ
1289a 61 6c 4f 66 66 3d 3d 73 7a 4a 20 29 3b 0a 0a 20 alOff==szJ );..
1289b 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 20 72 6f /* Finally, ro
1289c 6c 6c 62 61 63 6b 20 70 61 67 65 73 20 66 72 6f llback pages fro
1289d 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 m the sub-journa
1289e 6c 2e 20 20 50 61 67 65 20 74 68 61 74 20 77 65 l. Page that we
1289f 72 65 0a 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 re. ** previous
128a0 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 6f ly rolled back o
128a1 75 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a ut of the main j
128a2 6f 75 72 6e 61 6c 20 28 61 6e 64 20 61 72 65 20 ournal (and are
128a3 68 65 6e 63 65 20 69 6e 20 70 44 6f 6e 65 29 0a hence in pDone).
128a4 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 73 6b 69 ** will be ski
128a5 70 70 65 64 2e 20 20 4f 75 74 2d 6f 66 2d 72 61 pped. Out-of-ra
128a6 6e 67 65 20 70 61 67 65 73 20 61 72 65 20 61 6c nge pages are al
128a7 73 6f 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f so skipped.. */
128a8 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e . if( pSavepoin
128a9 74 20 29 7b 0a 20 20 20 20 75 33 32 20 69 69 3b t ){. u32 ii;
128aa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
128ab 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 oop counter */.
128ac 20 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 i64 offset =
128ad 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75 62 pSavepoint->iSub
128ae 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 Rec*(4+pPager->p
128af 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 66 6f ageSize);. fo
128b0 72 28 69 69 3d 70 53 61 76 65 70 6f 69 6e 74 2d r(ii=pSavepoint-
128b1 3e 69 53 75 62 52 65 63 3b 20 72 63 3d 3d 53 51 >iSubRec; rc==SQ
128b2 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 70 50 LITE_OK && ii<pP
128b3 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b 20 69 ager->nSubRec; i
128b4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 i++){. asse
128b5 72 74 28 20 6f 66 66 73 65 74 3d 3d 69 69 2a 28 rt( offset==ii*(
128b6 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 4+pPager->pageSi
128b7 7a 65 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 ze) );. rc
128b8 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
128b9 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 _one_page(pPager
128ba 2c 20 30 2c 20 30 2c 20 26 6f 66 66 73 65 74 2c , 0, 0, &offset,
128bb 20 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 1, pDone);.
128bc 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 }. assert( rc
128bd 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
128be 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 . }.. sqlite3B
128bf 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 44 6f itvecDestroy(pDo
128c0 6e 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ne);. if( rc==S
128c1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
128c2 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
128c3 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20 ff = szJ;. }.
128c4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
128c5 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6d .** Change the m
128c6 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 aximum number of
128c7 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 in-memory pages
128c8 20 74 68 61 74 20 61 72 65 20 61 6c 6c 6f 77 65 that are allowe
128c9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 d..*/.SQLITE_PRI
128ca 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
128cb 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73 69 3PagerSetCachesi
128cc 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ze(Pager *pPager
128cd 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 , int mxPage){.
128ce 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 sqlite3PcacheSe
128cf 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 67 65 tCachesize(pPage
128d0 72 2d 3e 70 50 43 61 63 68 65 2c 20 6d 78 50 61 r->pPCache, mxPa
128d1 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 ge);.}../*.** Ad
128d2 6a 75 73 74 20 74 68 65 20 72 6f 62 75 73 74 6e just the robustn
128d3 65 73 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 ess of the datab
128d4 61 73 65 20 74 6f 20 64 61 6d 61 67 65 20 64 75 ase to damage du
128d5 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 0a e to OS crashes.
128d6 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69 6c ** or power fail
128d7 75 72 65 73 20 62 79 20 63 68 61 6e 67 69 6e 67 ures by changing
128d8 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 the number of s
128d9 79 6e 63 73 28 29 73 20 77 68 65 6e 20 77 72 69 yncs()s when wri
128da 74 69 6e 67 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c ting.** the roll
128db 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 back journal. T
128dc 68 65 72 65 20 61 72 65 20 74 68 72 65 65 20 6c here are three l
128dd 65 76 65 6c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 evels:.**.**
128de 4f 46 46 20 20 20 20 20 20 20 73 71 6c 69 74 65 OFF sqlite
128df 33 4f 73 53 79 6e 63 28 29 20 69 73 20 6e 65 76 3OsSync() is nev
128e0 65 72 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 er called. This
128e1 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a is the default.
128e2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
128e3 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 61 6e for temporary an
128e4 64 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 d transient file
128e5 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d s..**.** NORM
128e6 41 4c 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 AL The journa
128e7 6c 20 69 73 20 73 79 6e 63 65 64 20 6f 6e 63 65 l is synced once
128e8 20 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 before writes b
128e9 65 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 egin on the.**
128ea 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 data
128eb 62 61 73 65 2e 20 20 54 68 69 73 20 69 73 20 6e base. This is n
128ec 6f 72 6d 61 6c 6c 79 20 61 64 65 71 75 61 74 65 ormally adequate
128ed 20 70 72 6f 74 65 63 74 69 6f 6e 2c 20 62 75 74 protection, but
128ee 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
128ef 20 69 74 20 69 73 20 74 68 65 6f 72 65 74 69 63 it is theoretic
128f0 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65 2c 20 74 ally possible, t
128f1 68 6f 75 67 68 20 76 65 72 79 20 75 6e 6c 69 6b hough very unlik
128f2 65 6c 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ely,.**
128f3 20 20 20 20 20 74 68 61 74 20 61 6e 20 69 6e 6f that an ino
128f4 70 65 72 74 75 6e 65 20 70 6f 77 65 72 20 66 61 pertune power fa
128f5 69 6c 75 72 65 20 63 6f 75 6c 64 20 6c 65 61 76 ilure could leav
128f6 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a e the journal.**
128f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
128f8 20 61 20 73 74 61 74 65 20 77 68 69 63 68 20 77 a state which w
128f9 6f 75 6c 64 20 63 61 75 73 65 20 64 61 6d 61 67 ould cause damag
128fa 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 e to the databas
128fb 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 e.**
128fc 20 20 77 68 65 6e 20 69 74 20 69 73 20 72 6f 6c when it is rol
128fd 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 led back..**.**
128fe 20 20 20 46 55 4c 4c 20 20 20 20 20 20 54 68 65 FULL The
128ff 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 journal is sync
12900 65 64 20 74 77 69 63 65 20 62 65 66 6f 72 65 20 ed twice before
12901 77 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 writes begin on
12902 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 the.**
12903 20 20 20 20 64 61 74 61 62 61 73 65 20 28 77 69 database (wi
12904 74 68 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e th some addition
12905 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d al information -
12906 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 0a the nRec field.
12907 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
12908 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 of the journal h
12909 65 61 64 65 72 20 2d 20 62 65 69 6e 67 20 77 72 eader - being wr
1290a 69 74 74 65 6e 20 69 6e 20 62 65 74 77 65 65 6e itten in between
1290b 20 74 68 65 20 74 77 6f 0a 2a 2a 20 20 20 20 20 the two.**
1290c 20 20 20 20 20 20 20 20 20 73 79 6e 63 73 29 2e syncs).
1290d 20 20 49 66 20 77 65 20 61 73 73 75 6d 65 20 74 If we assume t
1290e 68 61 74 20 77 72 69 74 69 6e 67 20 61 0a 2a 2a hat writing a.**
1290f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 si
12910 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 ngle disk sector
12911 20 69 73 20 61 74 6f 6d 69 63 2c 20 74 68 65 6e is atomic, then
12912 20 74 68 69 73 20 6d 6f 64 65 20 70 72 6f 76 69 this mode provi
12913 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 des.**
12914 20 20 20 20 61 73 73 75 72 61 6e 63 65 20 74 68 assurance th
12915 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 at the journal w
12916 69 6c 6c 20 6e 6f 74 20 62 65 20 63 6f 72 72 75 ill not be corru
12917 70 74 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 20 pted to the.**
12918 20 20 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e poin
12919 74 20 6f 66 20 63 61 75 73 69 6e 67 20 64 61 6d t of causing dam
1291a 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 age to the datab
1291b 61 73 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62 ase during rollb
1291c 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72 ack..**.** Numer
1291d 69 63 20 76 61 6c 75 65 73 20 61 73 73 6f 63 69 ic values associ
1291e 61 74 65 64 20 77 69 74 68 20 74 68 65 73 65 20 ated with these
1291f 73 74 61 74 65 73 20 61 72 65 20 4f 46 46 3d 3d states are OFF==
12920 31 2c 20 4e 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20 1, NORMAL=2,.**
12921 61 6e 64 20 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23 and FULL=3..*/.#
12922 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
12923 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 IT_PAGER_PRAGMAS
12924 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12925 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
12926 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 rSetSafetyLevel(
12927 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
12928 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20 62 46 nt level, int bF
12929 75 6c 6c 46 73 79 6e 63 29 7b 0a 20 20 70 50 61 ullFsync){. pPa
1292a 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 20 28 ger->noSync = (
1292b 6c 65 76 65 6c 3d 3d 31 20 7c 7c 20 70 50 61 67 level==1 || pPag
1292c 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31 er->tempFile) ?1
1292d 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 66 75 :0;. pPager->fu
1292e 6c 6c 53 79 6e 63 20 3d 20 28 6c 65 76 65 6c 3d llSync = (level=
1292f 3d 33 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74 =3 && !pPager->t
12930 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 empFile) ?1:0;.
12931 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c pPager->sync_fl
12932 61 67 73 20 3d 20 28 62 46 75 6c 6c 46 73 79 6e ags = (bFullFsyn
12933 63 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 c?SQLITE_SYNC_FU
12934 4c 4c 3a 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e LL:SQLITE_SYNC_N
12935 4f 52 4d 41 4c 29 3b 0a 20 20 69 66 28 20 70 50 ORMAL);. if( pP
12936 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 20 70 ager->noSync ) p
12937 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
12938 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f = 0;.}.#endif../
12939 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
1293a 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 ng global variab
1293b 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 le is incremente
1293c 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 6c d whenever the l
1293d 69 62 72 61 72 79 0a 2a 2a 20 61 74 74 65 6d 70 ibrary.** attemp
1293e 74 73 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d ts to open a tem
1293f 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 20 54 68 porary file. Th
12940 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 is information i
12941 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 s used for.** te
12942 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 sting and analys
12943 69 73 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 69 is only. .*/.#i
12944 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
12945 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
12946 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 sqlite3_opentemp
12947 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 _count = 0;.#end
12948 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 if../*.** Open a
12949 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e temporary file.
1294a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 .**.** Write the
1294b 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 file descriptor
1294c 20 69 6e 74 6f 20 2a 70 46 69 6c 65 2e 20 52 65 into *pFile. Re
1294d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
1294e 6e 20 73 75 63 63 65 73 73 20 0a 2a 2a 20 6f 72 n success .** or
1294f 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f some other erro
12950 72 20 63 6f 64 65 20 69 66 20 77 65 20 66 61 69 r code if we fai
12951 6c 2e 20 54 68 65 20 4f 53 20 77 69 6c 6c 20 61 l. The OS will a
12952 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a utomatically .**
12953 20 64 65 6c 65 74 65 20 74 68 65 20 74 65 6d 70 delete the temp
12954 6f 72 61 72 79 20 66 69 6c 65 20 77 68 65 6e 20 orary file when
12955 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a it is closed..**
12956 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 .** The flags pa
12957 73 73 65 64 20 74 6f 20 74 68 65 20 56 46 53 20 ssed to the VFS
12958 6c 61 79 65 72 20 78 4f 70 65 6e 28 29 20 63 61 layer xOpen() ca
12959 6c 6c 20 61 72 65 20 74 68 6f 73 65 20 73 70 65 ll are those spe
1295a 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 70 61 72 cified.** by par
1295b 61 6d 65 74 65 72 20 76 66 73 46 6c 61 67 73 20 ameter vfsFlags
1295c 4f 52 65 64 20 77 69 74 68 20 74 68 65 20 66 6f ORed with the fo
1295d 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
1295e 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 SQLITE_OPEN_R
1295f 45 41 44 57 52 49 54 45 0a 2a 2a 20 20 20 20 20 EADWRITE.**
12960 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 SQLITE_OPEN_CREA
12961 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 TE.** SQLITE
12962 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 0a _OPEN_EXCLUSIVE.
12963 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 ** SQLITE_OP
12964 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
12965 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 .*/.static int p
12966 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a 20 20 agerOpentemp(.
12967 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 Pager *pPager,
12968 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 /* The pag
12969 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 er object */. s
1296a 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 qlite3_file *pFi
1296b 6c 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74 68 le, /* Write th
1296c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f e file descripto
1296d 72 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 r here */. int
1296e 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 vfsFlags
1296f 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 /* Flags passe
12970 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 d through to the
12971 20 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 VFS */.){. int
12972 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
12973 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
12974 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c e */..#ifdef SQL
12975 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 ITE_TEST. sqlit
12976 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e e3_opentemp_coun
12977 74 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 66 6f t++; /* Used fo
12978 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e r testing and an
12979 61 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f 0a 23 alysis only */.#
1297a 65 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c 61 67 endif.. vfsFlag
1297b 73 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50 45 s |= SQLITE_OPE
1297c 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 N_READWRITE | SQ
1297d 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 LITE_OPEN_CREATE
1297e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 53 |. S
1297f 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 QLITE_OPEN_EXCLU
12980 53 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 SIVE | SQLITE_OP
12981 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 EN_DELETEONCLOSE
12982 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
12983 4f 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 70 OsOpen(pPager->p
12984 56 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c 20 76 Vfs, 0, pFile, v
12985 66 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 61 fsFlags, 0);. a
12986 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
12987 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 E_OK || isOpen(p
12988 46 69 6c 65 29 20 29 3b 0a 20 20 72 65 74 75 72 File) );. retur
12989 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n rc;.}../*.** S
1298a 65 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 et the busy hand
1298b 6c 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a ler function..**
1298c 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 6e .** The pager in
1298d 76 6f 6b 65 73 20 74 68 65 20 62 75 73 79 2d 68 vokes the busy-h
1298e 61 6e 64 6c 65 72 20 69 66 20 73 71 6c 69 74 65 andler if sqlite
1298f 33 4f 73 4c 6f 63 6b 28 29 20 72 65 74 75 72 6e 3OsLock() return
12990 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 s .** SQLITE_BUS
12991 59 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74 6f Y when trying to
12992 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 6e 6f upgrade from no
12993 2d 6c 6f 63 6b 20 74 6f 20 61 20 53 48 41 52 45 -lock to a SHARE
12994 44 20 6c 6f 63 6b 2c 0a 2a 2a 20 6f 72 20 77 68 D lock,.** or wh
12995 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67 en trying to upg
12996 72 61 64 65 20 66 72 6f 6d 20 61 20 52 45 53 45 rade from a RESE
12997 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20 61 6e 20 RVED lock to an
12998 45 58 43 4c 55 53 49 56 45 20 0a 2a 2a 20 6c 6f EXCLUSIVE .** lo
12999 63 6b 2e 20 49 74 20 64 6f 65 73 20 2a 6e 6f 74 ck. It does *not
1299a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 * invoke the bus
1299b 79 20 68 61 6e 64 6c 65 72 20 77 68 65 6e 20 75 y handler when u
1299c 70 67 72 61 64 69 6e 67 20 66 72 6f 6d 0a 2a 2a pgrading from.**
1299d 20 53 48 41 52 45 44 20 74 6f 20 52 45 53 45 52 SHARED to RESER
1299e 56 45 44 2c 20 6f 72 20 77 68 65 6e 20 75 70 67 VED, or when upg
1299f 72 61 64 69 6e 67 20 66 72 6f 6d 20 53 48 41 52 rading from SHAR
129a0 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56 45 0a ED to EXCLUSIVE.
129a1 2a 2a 20 28 77 68 69 63 68 20 6f 63 63 75 72 73 ** (which occurs
129a2 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 during hot-jour
129a3 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 2e 20 53 nal rollback). S
129a4 75 6d 6d 61 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 ummary:.**.**
129a5 54 72 61 6e 73 69 74 69 6f 6e 20 20 20 20 20 20 Transition
129a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129a7 20 20 7c 20 49 6e 76 6f 6b 65 73 20 78 42 75 73 | Invokes xBus
129a8 79 48 61 6e 64 6c 65 72 0a 2a 2a 20 20 20 2d 2d yHandler.** --
129a9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129aa 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129ab 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
129ac 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 4e 4f 5f 4c ------.** NO_L
129ad 4f 43 4b 20 20 20 20 20 20 20 2d 3e 20 53 48 41 OCK -> SHA
129ae 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 20 7c 20 RED_LOCK |
129af 59 65 73 0a 2a 2a 20 20 20 53 48 41 52 45 44 5f Yes.** SHARED_
129b0 4c 4f 43 4b 20 20 20 2d 3e 20 52 45 53 45 52 56 LOCK -> RESERV
129b1 45 44 5f 4c 4f 43 4b 20 20 20 20 7c 20 4e 6f 0a ED_LOCK | No.
129b2 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b ** SHARED_LOCK
129b3 20 20 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 5f -> EXCLUSIVE_
129b4 4c 4f 43 4b 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 LOCK | No.**
129b5 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 2d RESERVED_LOCK -
129b6 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b > EXCLUSIVE_LOCK
129b7 20 20 20 7c 20 59 65 73 0a 2a 2a 0a 2a 2a 20 49 | Yes.**.** I
129b8 66 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c f the busy-handl
129b9 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 er callback retu
129ba 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 rns non-zero, th
129bb 65 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 72 65 e lock is .** re
129bc 74 72 69 65 64 2e 20 49 66 20 69 74 20 72 65 74 tried. If it ret
129bd 75 72 6e 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 urns zero, then
129be 74 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 the SQLITE_BUSY
129bf 65 72 72 6f 72 20 69 73 0a 2a 2a 20 72 65 74 75 error is.** retu
129c0 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c rned to the call
129c1 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 er of the pager
129c2 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f API function..*/
129c3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
129c4 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 void sqlite3Page
129c5 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 rSetBusyhandler(
129c6 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 . Pager *pPager
129c7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
129c8 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 /* Pager
129c9 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 object */. int
129ca 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 (*xBusyHandler)
129cb 28 76 6f 69 64 20 2a 29 2c 20 20 20 20 20 20 20 (void *),
129cc 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
129cd 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e busy-handler fun
129ce 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 ction */. void
129cf 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 *pBusyHandlerArg
129d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129d1 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 /* Argument to p
129d2 61 73 73 20 74 6f 20 78 42 75 73 79 48 61 6e 64 ass to xBusyHand
129d3 6c 65 72 20 2a 2f 0a 29 7b 20 20 0a 20 20 70 50 ler */.){ . pP
129d4 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c ager->xBusyHandl
129d5 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64 6c 65 er = xBusyHandle
129d6 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 42 75 r;. pPager->pBu
129d7 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 70 syHandlerArg = p
129d8 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 3b 0a BusyHandlerArg;.
129d9 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 }../*.** Report
129da 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 the current page
129db 20 73 69 7a 65 20 61 6e 64 20 6e 75 6d 62 65 72 size and number
129dc 20 6f 66 20 72 65 73 65 72 76 65 64 20 62 79 74 of reserved byt
129dd 65 73 20 62 61 63 6b 0a 2a 2a 20 74 6f 20 74 68 es back.** to th
129de 65 20 63 6f 64 65 63 2e 0a 2a 2f 0a 23 69 66 64 e codec..*/.#ifd
129df 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f ef SQLITE_HAS_CO
129e0 44 45 43 0a 73 74 61 74 69 63 20 76 6f 69 64 20 DEC.static void
129e1 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 pagerReportSize(
129e2 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a Pager *pPager){.
129e3 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 43 if( pPager->xC
129e4 6f 64 65 63 53 69 7a 65 43 68 6e 67 20 29 7b 0a odecSizeChng ){.
129e5 20 20 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 pPager->xCod
129e6 65 63 53 69 7a 65 43 68 6e 67 28 70 50 61 67 65 ecSizeChng(pPage
129e7 72 2d 3e 70 43 6f 64 65 63 2c 20 70 50 61 67 65 r->pCodec, pPage
129e8 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 r->pageSize,.
129e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
129ea 20 20 20 20 20 20 20 20 28 69 6e 74 29 70 50 61 (int)pPa
129eb 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 29 3b 0a ger->nReserve);.
129ec 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 }.}.#else.# de
129ed 66 69 6e 65 20 70 61 67 65 72 52 65 70 6f 72 74 fine pagerReport
129ee 53 69 7a 65 28 58 29 20 20 20 20 20 2f 2a 20 4e Size(X) /* N
129ef 6f 2d 6f 70 20 69 66 20 77 65 20 64 6f 20 6e 6f o-op if we do no
129f0 74 20 73 75 70 70 6f 72 74 20 61 20 63 6f 64 65 t support a code
129f1 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a c */.#endif../*.
129f2 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 ** Change the pa
129f3 67 65 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 ge size used by
129f4 74 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 the Pager object
129f5 2e 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 73 . The new page s
129f6 69 7a 65 20 0a 2a 2a 20 69 73 20 70 61 73 73 65 ize .** is passe
129f7 64 20 69 6e 20 2a 70 50 61 67 65 53 69 7a 65 2e d in *pPageSize.
129f8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
129f9 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72 ger is in the er
129fa 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 ror state when t
129fb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
129fc 63 61 6c 6c 65 64 2c 20 69 74 0a 2a 2a 20 69 73 called, it.** is
129fd 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 65 20 76 61 a no-op. The va
129fe 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 lue returned is
129ff 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 the error state
12a00 65 72 72 6f 72 20 63 6f 64 65 20 28 69 2e 65 2e error code (i.e.
12a01 20 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 .** one of SQLI
12a02 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 TE_IOERR, SQLITE
12a03 5f 43 4f 52 52 55 50 54 20 6f 72 20 53 51 4c 49 _CORRUPT or SQLI
12a04 54 45 5f 46 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 TE_FULL)..**.**
12a05 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6c Otherwise, if al
12a06 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 l of the followi
12a07 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a 0a ng are true:.**.
12a08 2a 2a 20 20 20 2a 20 74 68 65 20 6e 65 77 20 70 ** * the new p
12a09 61 67 65 20 73 69 7a 65 20 28 76 61 6c 75 65 20 age size (value
12a0a 6f 66 20 2a 70 50 61 67 65 53 69 7a 65 29 20 69 of *pPageSize) i
12a0b 73 20 76 61 6c 69 64 20 28 61 20 70 6f 77 65 72 s valid (a power
12a0c 20 0a 2a 2a 20 20 20 20 20 6f 66 20 74 77 6f 20 .** of two
12a0d 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 between 512 and
12a0e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
12a0f 53 49 5a 45 2c 20 69 6e 63 6c 75 73 69 76 65 29 SIZE, inclusive)
12a10 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 , and.**.** *
12a11 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 there are no out
12a12 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 standing page re
12a13 66 65 72 65 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a ferences, and.**
12a14 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 .** * the data
12a15 62 61 73 65 20 69 73 20 65 69 74 68 65 72 20 6e base is either n
12a16 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 ot an in-memory
12a17 64 61 74 61 62 61 73 65 20 6f 72 20 69 74 20 69 database or it i
12a18 73 0a 2a 2a 20 20 20 20 20 61 6e 20 69 6e 2d 6d s.** an in-m
12a19 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 74 emory database t
12a1a 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 63 6f hat currently co
12a1b 6e 73 69 73 74 73 20 6f 66 20 7a 65 72 6f 20 70 nsists of zero p
12a1c 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e ages..**.** then
12a1d 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 the pager objec
12a1e 74 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 t page size is s
12a1f 65 74 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65 et to *pPageSize
12a20 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 ..**.** If the p
12a21 61 67 65 20 73 69 7a 65 20 69 73 20 63 68 61 6e age size is chan
12a22 67 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 66 ged, then this f
12a23 75 6e 63 74 69 6f 6e 20 75 73 65 73 20 73 71 6c unction uses sql
12a24 69 74 65 33 50 61 67 65 72 4d 61 6c 6c 6f 63 28 ite3PagerMalloc(
12a25 29 20 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 ) .** to obtain
12a26 61 20 6e 65 77 20 50 61 67 65 72 2e 70 54 6d 70 a new Pager.pTmp
12a27 53 70 61 63 65 20 62 75 66 66 65 72 2e 20 49 66 Space buffer. If
12a28 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e this allocation
12a29 20 61 74 74 65 6d 70 74 20 0a 2a 2a 20 66 61 69 attempt .** fai
12a2a 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d ls, SQLITE_NOMEM
12a2b 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 is returned and
12a2c 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 72 the page size r
12a2d 65 6d 61 69 6e 73 20 75 6e 63 68 61 6e 67 65 64 emains unchanged
12a2e 2e 20 0a 2a 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 . .** In all oth
12a2f 65 72 20 63 61 73 65 73 2c 20 53 51 4c 49 54 45 er cases, SQLITE
12a30 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
12a31 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 .**.** If the pa
12a32 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74 20 63 ge size is not c
12a33 68 61 6e 67 65 64 2c 20 65 69 74 68 65 72 20 62 hanged, either b
12a34 65 63 61 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 ecause one of th
12a35 65 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 e enumerated.**
12a36 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 conditions above
12a37 20 69 73 20 6e 6f 74 20 74 72 75 65 2c 20 74 68 is not true, th
12a38 65 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 e pager was in e
12a39 72 72 6f 72 20 73 74 61 74 65 20 77 68 65 6e 20 rror state when
12a3a 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e this.** function
12a3b 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 6f 72 20 was called, or
12a3c 62 65 63 61 75 73 65 20 74 68 65 20 6d 65 6d 6f because the memo
12a3d 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 ry allocation at
12a3e 74 65 6d 70 74 20 66 61 69 6c 65 64 2c 20 0a 2a tempt failed, .*
12a3f 2a 20 74 68 65 6e 20 2a 70 50 61 67 65 53 69 7a * then *pPageSiz
12a40 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 e is set to the
12a41 6f 6c 64 2c 20 72 65 74 61 69 6e 65 64 20 70 61 old, retained pa
12a42 67 65 20 73 69 7a 65 20 62 65 66 6f 72 65 20 72 ge size before r
12a43 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c eturning..*/.SQL
12a44 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
12a45 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 sqlite3PagerSetP
12a46 61 67 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70 agesize(Pager *p
12a47 50 61 67 65 72 2c 20 75 31 36 20 2a 70 50 61 67 Pager, u16 *pPag
12a48 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 eSize, int nRese
12a49 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d rve){. int rc =
12a4a 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
12a4b 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c ;.. if( rc==SQL
12a4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 31 ITE_OK ){. u1
12a4d 36 20 70 61 67 65 53 69 7a 65 20 3d 20 2a 70 50 6 pageSize = *pP
12a4e 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 61 73 73 ageSize;. ass
12a4f 65 72 74 28 20 70 61 67 65 53 69 7a 65 3d 3d 30 ert( pageSize==0
12a50 20 7c 7c 20 28 70 61 67 65 53 69 7a 65 3e 3d 35 || (pageSize>=5
12a51 31 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 12 && pageSize<=
12a52 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f SQLITE_MAX_PAGE_
12a53 53 49 5a 45 29 20 29 3b 0a 20 20 20 20 69 66 28 SIZE) );. if(
12a54 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d (pPager->memDb=
12a55 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 =0 || pPager->db
12a56 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20 20 26 26 Size==0). &&
12a57 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 sqlite3PcacheRe
12a58 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 fCount(pPager->p
12a59 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20 20 20 PCache)==0 .
12a5a 20 26 26 20 70 61 67 65 53 69 7a 65 20 26 26 20 && pageSize &&
12a5b 70 61 67 65 53 69 7a 65 21 3d 70 50 61 67 65 72 pageSize!=pPager
12a5c 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 20 20 20 ->pageSize .
12a5d 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 ){. char *p
12a5e 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73 71 New = (char *)sq
12a5f 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 lite3PageMalloc(
12a60 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 pageSize);.
12a61 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 if( !pNew ){.
12a62 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
12a63 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d E_NOMEM;. }
12a64 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 61 else{. pa
12a65 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 ger_reset(pPager
12a66 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 );. pPage
12a67 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70 61 r->pageSize = pa
12a68 67 65 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 geSize;.
12a69 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
12a6a 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
12a6b 65 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67 e);. pPag
12a6c 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 er->pTmpSpace =
12a6d 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 73 71 pNew;. sq
12a6e 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 lite3PcacheSetPa
12a6f 67 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 geSize(pPager->p
12a70 50 43 61 63 68 65 2c 20 70 61 67 65 53 69 7a 65 PCache, pageSize
12a71 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
12a72 0a 20 20 20 20 2a 70 50 61 67 65 53 69 7a 65 20 . *pPageSize
12a73 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 70 = (u16)pPager->p
12a74 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 69 66 28 ageSize;. if(
12a75 20 6e 52 65 73 65 72 76 65 3c 30 20 29 20 6e 52 nReserve<0 ) nR
12a76 65 73 65 72 76 65 20 3d 20 70 50 61 67 65 72 2d eserve = pPager-
12a77 3e 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61 >nReserve;. a
12a78 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e ssert( nReserve>
12a79 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c 31 =0 && nReserve<1
12a7a 30 30 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 000 );. pPage
12a7b 72 2d 3e 6e 52 65 73 65 72 76 65 20 3d 20 28 69 r->nReserve = (i
12a7c 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 20 16)nReserve;.
12a7d 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65 pagerReportSize
12a7e 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 (pPager);. }.
12a7f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
12a80 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
12a81 6e 74 65 72 20 74 6f 20 74 68 65 20 22 74 65 6d nter to the "tem
12a82 70 6f 72 61 72 79 20 70 61 67 65 22 20 62 75 66 porary page" buf
12a83 66 65 72 20 68 65 6c 64 20 69 6e 74 65 72 6e 61 fer held interna
12a84 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 70 61 lly.** by the pa
12a85 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 61 20 ger. This is a
12a86 62 75 66 66 65 72 20 74 68 61 74 20 69 73 20 62 buffer that is b
12a87 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c ig enough to hol
12a88 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 d the.** entire
12a89 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64 61 74 content of a dat
12a8a 61 62 61 73 65 20 70 61 67 65 2e 20 20 54 68 69 abase page. Thi
12a8b 73 20 62 75 66 66 65 72 20 69 73 20 75 73 65 64 s buffer is used
12a8c 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 internally.** d
12a8d 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20 61 uring rollback a
12a8e 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 65 72 77 nd will be overw
12a8f 72 69 74 74 65 6e 20 77 68 65 6e 65 76 65 72 20 ritten whenever
12a90 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 a rollback.** oc
12a91 63 75 72 73 2e 20 20 42 75 74 20 6f 74 68 65 72 curs. But other
12a92 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 66 72 65 modules are fre
12a93 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f 6f 2c e to use it too,
12a94 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e as long as.** n
12a95 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65 20 o rollbacks are
12a96 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 happening..*/.SQ
12a97 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
12a98 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 d *sqlite3PagerT
12a99 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 20 2a empSpace(Pager *
12a9a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
12a9b 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 n pPager->pTmpSp
12a9c 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 ace;.}../*.** At
12a9d 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68 65 tempt to set the
12a9e 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 maximum databas
12a9f 65 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66 20 e page count if
12aa0 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 mxPage is positi
12aa1 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 ve. .** Make no
12aa2 63 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61 67 changes if mxPag
12aa3 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67 e is zero or neg
12aa4 61 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76 65 ative. And neve
12aa5 72 20 72 65 64 75 63 65 20 74 68 65 0a 2a 2a 20 r reduce the.**
12aa6 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 maximum page cou
12aa7 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75 72 nt below the cur
12aa8 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 rent size of the
12aa9 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
12aaa 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 6d Regardless of m
12aab 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68 xPage, return th
12aac 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 e current maximu
12aad 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f m page count..*/
12aae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
12aaf 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
12ab0 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67 MaxPageCount(Pag
12ab1 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 er *pPager, int
12ab2 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20 6d mxPage){. if( m
12ab3 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 70 xPage>0 ){. p
12ab4 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 Pager->mxPgno =
12ab5 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73 71 mxPage;. }. sq
12ab6 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
12ab7 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a unt(pPager, 0);.
12ab8 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d return pPager-
12ab9 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a >mxPgno;.}../*.*
12aba 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
12abb 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 set of routines
12abc 61 72 65 20 75 73 65 64 20 74 6f 20 64 69 73 61 are used to disa
12abd 62 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74 65 ble the simulate
12abe 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20 6d d.** I/O error m
12abf 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73 65 echanism. These
12ac0 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 routines are us
12ac1 65 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d 75 ed to avoid simu
12ac2 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73 20 lated.** errors
12ac3 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 in places where
12ac4 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 we do not care a
12ac5 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a bout errors..**.
12ac6 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c 49 ** Unless -DSQLI
12ac7 54 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73 65 TE_TEST=1 is use
12ac8 64 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 d, these routine
12ac9 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 s are all no-ops
12aca 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 .** and generate
12acb 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 no code..*/.#if
12acc 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
12acd 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 SQLITE_API exter
12ace 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f n int sqlite3_io
12acf 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a _error_pending;.
12ad0 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 SQLITE_API exter
12ad1 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f n int sqlite3_io
12ad2 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61 74 _error_hit;.stat
12ad3 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e 74 ic int saved_cnt
12ad4 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 ;.void disable_s
12ad5 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
12ad6 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76 65 rs(void){. save
12ad7 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f d_cnt = sqlite3_
12ad8 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 io_error_pending
12ad9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 ;. sqlite3_io_e
12ada 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 2d rror_pending = -
12adb 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c 65 1;.}.void enable
12adc 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 _simulated_io_er
12add 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 71 rors(void){. sq
12ade 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 lite3_io_error_p
12adf 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f 63 ending = saved_c
12ae0 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 nt;.}.#else.# de
12ae1 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d fine disable_sim
12ae2 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 ulated_io_errors
12ae3 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 ().# define enab
12ae4 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f le_simulated_io_
12ae5 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a errors().#endif.
12ae6 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 ./*.** Read the
12ae7 66 69 72 73 74 20 4e 20 62 79 74 65 73 20 66 72 first N bytes fr
12ae8 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 om the beginning
12ae9 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 74 of the file int
12aea 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 o memory.** that
12aeb 20 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74 6f pDest points to
12aec 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 . .**.** If the
12aed 70 61 67 65 72 20 77 61 73 20 6f 70 65 6e 65 64 pager was opened
12aee 20 6f 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20 on a transient
12aef 66 69 6c 65 20 28 7a 46 69 6c 65 6e 61 6d 65 3d file (zFilename=
12af0 3d 22 22 29 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e =""), or.** open
12af1 65 64 20 6f 6e 20 61 20 66 69 6c 65 20 6c 65 73 ed on a file les
12af2 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 69 s than N bytes i
12af3 6e 20 73 69 7a 65 2c 20 74 68 65 20 6f 75 74 70 n size, the outp
12af4 75 74 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 ut buffer is.**
12af5 7a 65 72 6f 65 64 20 61 6e 64 20 53 51 4c 49 54 zeroed and SQLIT
12af6 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 54 E_OK returned. T
12af7 68 65 20 72 61 74 69 6f 6e 61 6c 65 20 66 6f 72 he rationale for
12af8 20 74 68 69 73 20 69 73 20 74 68 61 74 20 74 68 this is that th
12af9 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 is .** function
12afa 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 is used to read
12afb 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 73 database headers
12afc 2c 20 61 6e 64 20 61 20 6e 65 77 20 74 72 61 6e , and a new tran
12afd 73 69 65 6e 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f sient or.** zero
12afe 20 73 69 7a 65 64 20 64 61 74 61 62 61 73 65 20 sized database
12aff 68 61 73 20 61 20 68 65 61 64 65 72 20 74 68 61 has a header tha
12b00 6e 20 63 6f 6e 73 69 73 74 73 20 65 6e 74 69 72 n consists entir
12b01 65 6c 79 20 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a ely of zeroes..*
12b02 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 49 4f 20 65 *.** If any IO e
12b03 72 72 6f 72 20 61 70 61 72 74 20 66 72 6f 6d 20 rror apart from
12b04 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
12b05 52 54 5f 52 45 41 44 20 69 73 20 65 6e 63 6f 75 RT_READ is encou
12b06 6e 74 65 72 65 64 2c 0a 2a 2a 20 74 68 65 20 65 ntered,.** the e
12b07 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 rror code is ret
12b08 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c urned to the cal
12b09 6c 65 72 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 ler and the cont
12b0a 65 6e 74 73 20 6f 66 20 74 68 65 0a 2a 2a 20 6f ents of the.** o
12b0b 75 74 70 75 74 20 62 75 66 66 65 72 20 75 6e 64 utput buffer und
12b0c 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 efined..*/.SQLIT
12b0d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
12b0e 6c 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 lite3PagerReadFi
12b0f 6c 65 68 65 61 64 65 72 28 50 61 67 65 72 20 2a leheader(Pager *
12b10 70 50 61 67 65 72 2c 20 69 6e 74 20 4e 2c 20 75 pPager, int N, u
12b11 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 nsigned char *pD
12b12 65 73 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d est){. int rc =
12b13 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 SQLITE_OK;. me
12b14 6d 73 65 74 28 70 44 65 73 74 2c 20 30 2c 20 4e mset(pDest, 0, N
12b15 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f );. assert( isO
12b16 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12b17 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 || pPager->tempF
12b18 69 6c 65 20 29 3b 0a 20 20 69 66 28 20 69 73 4f ile );. if( isO
12b19 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
12b1a 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 ){. IOTRACE((
12b1b 22 44 42 48 44 52 20 25 70 20 30 20 25 64 5c 6e "DBHDR %p 0 %d\n
12b1c 22 2c 20 70 50 61 67 65 72 2c 20 4e 29 29 0a 20 ", pPager, N)).
12b1d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
12b1e 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 sRead(pPager->fd
12b1f 2c 20 70 44 65 73 74 2c 20 4e 2c 20 30 29 3b 0a , pDest, N, 0);.
12b20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
12b21 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 TE_IOERR_SHORT_R
12b22 45 41 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 EAD ){. rc
12b23 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 = SQLITE_OK;.
12b24 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
12b25 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 rc;.}../*.** Ret
12b26 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 urn the total nu
12b27 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
12b28 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
12b29 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a le associated .*
12b2a 2a 20 77 69 74 68 20 70 50 61 67 65 72 2e 20 4e * with pPager. N
12b2b 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 ormally, this is
12b2c 20 63 61 6c 63 75 6c 61 74 65 64 20 61 73 20 28 calculated as (
12b2d 3c 64 62 20 66 69 6c 65 20 73 69 7a 65 3e 2f 3c <db file size>/<
12b2e 70 61 67 65 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 page-size>)..**
12b2f 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65 20 However, if the
12b30 66 69 6c 65 20 69 73 20 62 65 74 77 65 65 6e 20 file is between
12b31 31 20 61 6e 64 20 3c 70 61 67 65 2d 73 69 7a 65 1 and <page-size
12b32 3e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c > bytes in size,
12b33 20 74 68 65 6e 20 0a 2a 2a 20 74 68 69 73 20 69 then .** this i
12b34 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 31 s considered a 1
12b35 20 70 61 67 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a page file..**.*
12b36 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 * If the pager i
12b37 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 s in error state
12b38 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
12b39 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 ion is called, t
12b3a 68 65 6e 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 hen the.** error
12b3b 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 state error cod
12b3c 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e e is returned an
12b3d 64 20 2a 70 6e 50 61 67 65 20 6c 65 66 74 20 75 d *pnPage left u
12b3e 6e 63 68 61 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a nchanged. Or,.**
12b3f 20 69 66 20 74 68 65 20 66 69 6c 65 20 73 79 73 if the file sys
12b40 74 65 6d 20 68 61 73 20 74 6f 20 62 65 20 71 75 tem has to be qu
12b41 65 72 69 65 64 20 66 6f 72 20 74 68 65 20 73 69 eried for the si
12b42 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 ze of the file a
12b43 6e 64 0a 2a 2a 20 74 68 65 20 71 75 65 72 79 20 nd.** the query
12b44 61 74 74 65 6d 70 74 20 72 65 74 75 72 6e 73 20 attempt returns
12b45 61 6e 20 49 4f 20 65 72 72 6f 72 2c 20 74 68 65 an IO error, the
12b46 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 IO error code i
12b47 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e s returned.** an
12b48 64 20 2a 70 6e 50 61 67 65 20 69 73 20 6c 65 66 d *pnPage is lef
12b49 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a t unchanged..**.
12b4a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 ** Otherwise, if
12b4b 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 everything is s
12b4c 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 uccessful, then
12b4d 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
12b4e 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e urned.** and *pn
12b4f 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 74 Page is set to t
12b50 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
12b51 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 es in the databa
12b52 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 se..*/.SQLITE_PR
12b53 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
12b54 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 3PagerPagecount(
12b55 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
12b56 6e 74 20 2a 70 6e 50 61 67 65 29 7b 0a 20 20 50 nt *pnPage){. P
12b57 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 gno nPage;
12b58 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 /* Valu
12b59 65 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 e to return via
12b5a 2a 70 6e 50 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a *pnPage */.. /*
12b5b 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 If the pager is
12b5c 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
12b5d 65 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 error state, ret
12b5e 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f urn the error co
12b5f 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 de. */. if( pPa
12b60 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a ger->errCode ){.
12b61 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 return pPage
12b62 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 0a r->errCode;. }.
12b63 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 . /* Determine
12b64 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 the number of pa
12b65 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2e ges in the file.
12b66 20 53 74 6f 72 65 20 74 68 69 73 20 69 6e 20 6e Store this in n
12b67 50 61 67 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 Page. */. if( p
12b68 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
12b69 69 64 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 id ){. nPage
12b6a 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 = pPager->dbSize
12b6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
12b6c 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
12b6d 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 /* Error
12b6e 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 returned by OsFi
12b6f 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 20 20 20 20 leSize() */.
12b70 69 36 34 20 6e 20 3d 20 30 3b 20 20 20 20 20 20 i64 n = 0;
12b71 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 /* File
12b72 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 72 65 size in bytes re
12b73 74 75 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 turned by OsFile
12b74 53 69 7a 65 28 29 20 2a 2f 0a 0a 20 20 20 20 61 Size() */.. a
12b75 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 ssert( isOpen(pP
12b76 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 ager->fd) || pPa
12b77 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
12b78 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 . if( isOpen(
12b79 70 50 61 67 65 72 2d 3e 66 64 29 20 26 26 20 28 pPager->fd) && (
12b7a 30 20 21 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 0 != (rc = sqlit
12b7b 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 e3OsFileSize(pPa
12b7c 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 29 20 29 ger->fd, &n))) )
12b7d 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 65 72 {. pager_er
12b7e 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b ror(pPager, rc);
12b7f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
12b80 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
12b81 6e 3e 30 20 26 26 20 6e 3c 70 50 61 67 65 72 2d n>0 && n<pPager-
12b82 3e 70 61 67 65 53 69 7a 65 20 29 7b 0a 20 20 20 >pageSize ){.
12b83 20 20 20 6e 50 61 67 65 20 3d 20 31 3b 0a 20 20 nPage = 1;.
12b84 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e }else{. n
12b85 50 61 67 65 20 3d 20 28 50 67 6e 6f 29 28 6e 20 Page = (Pgno)(n
12b86 2f 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 / pPager->pageSi
12b87 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ze);. }. i
12b88 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
12b89 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 !=PAGER_UNLOCK )
12b8a 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e {. pPager->
12b8b 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a dbSize = nPage;.
12b8c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
12b8d 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 FileSize = nPage
12b8e 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e ;. pPager->
12b8f 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 31 3b dbSizeValid = 1;
12b90 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
12b91 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 If the current
12b92 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
12b93 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 67 in the file is g
12b94 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 reater than the
12b95 0a 20 20 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 . ** configured
12b96 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 72 20 6e maximum pager n
12b97 75 6d 62 65 72 2c 20 69 6e 63 72 65 61 73 65 20 umber, increase
12b98 74 68 65 20 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 the allowed limi
12b99 74 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 74 t so. ** that t
12b9a 68 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 72 he file can be r
12b9b 65 61 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 ead.. */. if(
12b9c 6e 50 61 67 65 3e 70 50 61 67 65 72 2d 3e 6d 78 nPage>pPager->mx
12b9d 50 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 Pgno ){. pPag
12b9e 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 28 50 67 er->mxPgno = (Pg
12b9f 6e 6f 29 6e 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 no)nPage;. }..
12ba0 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 /* Set the outp
12ba1 75 74 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 ut variable and
12ba2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
12ba3 20 2a 2f 0a 20 20 69 66 28 20 70 6e 50 61 67 65 */. if( pnPage
12ba4 20 29 7b 0a 20 20 20 20 2a 70 6e 50 61 67 65 20 ){. *pnPage
12ba5 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 72 = nPage;. }. r
12ba6 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
12ba7 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 .}.../*.** Try t
12ba8 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 o obtain a lock
12ba9 6f 66 20 74 79 70 65 20 6c 6f 63 6b 74 79 70 65 of type locktype
12baa 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
12bab 20 66 69 6c 65 2e 20 49 66 0a 2a 2a 20 61 20 73 file. If.** a s
12bac 69 6d 69 6c 61 72 20 6f 72 20 67 72 65 61 74 65 imilar or greate
12bad 72 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 r lock is alread
12bae 79 20 68 65 6c 64 2c 20 74 68 69 73 20 66 75 6e y held, this fun
12baf 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
12bb0 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 53 .** (returning S
12bb1 51 4c 49 54 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 QLITE_OK immedia
12bb2 74 65 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 tely)..**.** Oth
12bb3 65 72 77 69 73 65 2c 20 61 74 74 65 6d 70 74 20 erwise, attempt
12bb4 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f to obtain the lo
12bb5 63 6b 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 ck using sqlite3
12bb6 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 OsLock(). Invoke
12bb7 20 0a 2a 2a 20 74 68 65 20 62 75 73 79 20 63 61 .** the busy ca
12bb8 6c 6c 62 61 63 6b 20 69 66 20 74 68 65 20 6c 6f llback if the lo
12bb9 63 6b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 ck is currently
12bba 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 52 not available. R
12bbb 65 70 65 61 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 epeat .** until
12bbc 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 the busy callbac
12bbd 6b 20 72 65 74 75 72 6e 73 20 66 61 6c 73 65 20 k returns false
12bbe 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 61 74 74 or until the att
12bbf 65 6d 70 74 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 empt to .** obta
12bc0 69 6e 20 74 68 65 20 6c 6f 63 6b 20 73 75 63 63 in the lock succ
12bc1 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 eeds..**.** Retu
12bc2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 rn SQLITE_OK on
12bc3 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 65 success and an e
12bc4 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77 65 20 rror code if we
12bc5 63 61 6e 6e 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a cannot obtain.**
12bc6 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66 20 74 68 the lock. If th
12bc7 65 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e e lock is obtain
12bc8 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c ed successfully,
12bc9 20 73 65 74 20 74 68 65 20 50 61 67 65 72 2e 73 set the Pager.s
12bca 74 61 74 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c tate .** variabl
12bcb 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 20 62 65 e to locktype be
12bcc 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a fore returning..
12bcd 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 */.static int pa
12bce 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b ger_wait_on_lock
12bcf 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 (Pager *pPager,
12bd0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 int locktype){.
12bd1 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
12bd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12bd3 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 /* Return
12bd4 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 code */.. /* Th
12bd5 65 20 4f 53 20 6c 6f 63 6b 20 76 61 6c 75 65 73 e OS lock values
12bd6 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d must be the sam
12bd7 65 20 61 73 20 74 68 65 20 50 61 67 65 72 20 6c e as the Pager l
12bd8 6f 63 6b 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 ock values */.
12bd9 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 53 48 assert( PAGER_SH
12bda 41 52 45 44 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 ARED==SHARED_LOC
12bdb 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 K );. assert( P
12bdc 41 47 45 52 5f 52 45 53 45 52 56 45 44 3d 3d 52 AGER_RESERVED==R
12bdd 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a ESERVED_LOCK );.
12bde 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f assert( PAGER_
12bdf 45 58 43 4c 55 53 49 56 45 3d 3d 45 58 43 4c 55 EXCLUSIVE==EXCLU
12be0 53 49 56 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 SIVE_LOCK );..
12be1 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 /* If the file i
12be2 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f s currently unlo
12be3 63 6b 65 64 20 74 68 65 6e 20 74 68 65 20 73 69 cked then the si
12be4 7a 65 20 6d 75 73 74 20 62 65 20 75 6e 6b 6e 6f ze must be unkno
12be5 77 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 wn */. assert(
12be6 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
12be7 41 47 45 52 5f 53 48 41 52 45 44 20 7c 7c 20 70 AGER_SHARED || p
12be8 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c Pager->dbSizeVal
12be9 69 64 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 43 id==0 );.. /* C
12bea 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 heck that this i
12beb 73 20 65 69 74 68 65 72 20 61 20 6e 6f 2d 6f 70 s either a no-op
12bec 20 28 62 65 63 61 75 73 65 20 74 68 65 20 72 65 (because the re
12bed 71 75 65 73 74 65 64 20 6c 6f 63 6b 20 69 73 20 quested lock is
12bee 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 . ** already he
12bef 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 ld, or one of th
12bf0 65 20 74 72 61 6e 73 69 73 74 69 6f 6e 73 20 74 e transistions t
12bf1 68 61 74 20 74 68 65 20 62 75 73 79 2d 68 61 6e hat the busy-han
12bf2 64 6c 65 72 0a 20 20 2a 2a 20 6d 61 79 20 62 65 dler. ** may be
12bf3 20 69 6e 76 6f 6b 65 64 20 64 75 72 69 6e 67 2c invoked during,
12bf4 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 according to th
12bf5 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a e comment above.
12bf6 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 ** sqlite3Page
12bf7 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 rSetBusyhandler(
12bf8 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 ).. */. assert
12bf9 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 ( (pPager->state
12bfa 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a 20 20 20 20 >=locktype).
12bfb 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 73 || (pPager->s
12bfc 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f tate==PAGER_UNLO
12bfd 43 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d CK && locktype==
12bfe 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 PAGER_SHARED).
12bff 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d || (pPager-
12c00 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 >state==PAGER_RE
12c01 53 45 52 56 45 44 20 26 26 20 6c 6f 63 6b 74 79 SERVED && lockty
12c02 70 65 3d 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 pe==PAGER_EXCLUS
12c03 49 56 45 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 IVE). );.. if(
12c04 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d pPager->state>=
12c05 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 locktype ){.
12c06 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
12c07 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 20 }else{. do
12c08 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
12c09 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 ite3OsLock(pPage
12c0a 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74 79 70 65 29 r->fd, locktype)
12c0b 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 ;. }while( rc
12c0c 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 ==SQLITE_BUSY &&
12c0d 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 pPager->xBusyHa
12c0e 6e 64 6c 65 72 28 70 50 61 67 65 72 2d 3e 70 42 ndler(pPager->pB
12c0f 75 73 79 48 61 6e 64 6c 65 72 41 72 67 29 20 29 usyHandlerArg) )
12c10 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
12c11 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
12c12 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d pPager->state =
12c13 20 28 75 38 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 (u8)locktype;.
12c14 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4c IOTRACE(("L
12c15 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 OCK %p %d\n", pP
12c16 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70 65 29 29 ager, locktype))
12c17 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
12c18 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
12c19 20 46 75 6e 63 74 69 6f 6e 20 61 73 73 65 72 74 Function assert
12c1a 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 TruncateConstrai
12c1b 6e 74 28 70 50 61 67 65 72 29 20 63 68 65 63 6b nt(pPager) check
12c1c 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 s that one of th
12c1d 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 e .** following
12c1e 69 73 20 74 72 75 65 20 66 6f 72 20 61 6c 6c 20 is true for all
12c1f 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 dirty pages curr
12c20 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 70 61 67 ently in the pag
12c21 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a 2a 2a 20 20 e-cache:.**.**
12c22 20 61 29 20 54 68 65 20 70 61 67 65 20 6e 75 6d a) The page num
12c23 62 65 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e ber is less than
12c24 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
12c25 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a 2a size of the .**
12c26 20 20 20 20 20 20 63 75 72 72 65 6e 74 20 64 61 current da
12c27 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 6e tabase image, in
12c28 20 70 61 67 65 73 2c 20 4f 52 0a 2a 2a 0a 2a 2a pages, OR.**.**
12c29 20 20 20 62 29 20 69 66 20 74 68 65 20 70 61 67 b) if the pag
12c2a 65 20 63 6f 6e 74 65 6e 74 20 77 65 72 65 20 77 e content were w
12c2b 72 69 74 74 65 6e 20 61 74 20 74 68 69 73 20 74 ritten at this t
12c2c 69 6d 65 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f ime, it would no
12c2d 74 0a 2a 2a 20 20 20 20 20 20 62 65 20 6e 65 63 t.** be nec
12c2e 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20 essary to write
12c2f 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 the current cont
12c30 65 6e 74 20 6f 75 74 20 74 6f 20 74 68 65 20 73 ent out to the s
12c31 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 ub-journal.**
12c32 20 20 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 (as determine
12c33 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 20 73 75 d by function su
12c34 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29 bjRequiresPage()
12c35 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 )..**.** If the
12c36 63 6f 6e 64 69 74 69 6f 6e 20 61 73 73 65 72 74 condition assert
12c37 65 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 ed by this funct
12c38 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 ion were not tru
12c39 65 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 69 e, and the.** di
12c3a 72 74 79 20 70 61 67 65 20 77 65 72 65 20 74 6f rty page were to
12c3b 20 62 65 20 64 69 73 63 61 72 64 65 64 20 66 72 be discarded fr
12c3c 6f 6d 20 74 68 65 20 63 61 63 68 65 20 76 69 61 om the cache via
12c3d 20 74 68 65 20 70 61 67 65 72 53 74 72 65 73 73 the pagerStress
12c3e 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 70 ().** routine, p
12c3f 61 67 65 72 53 74 72 65 73 73 28 29 20 77 6f 75 agerStress() wou
12c40 6c 64 20 6e 6f 74 20 77 72 69 74 65 20 74 68 65 ld not write the
12c41 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 63 6f current page co
12c42 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 ntent to.** the
12c43 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 database file. I
12c44 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 74 72 f a savepoint tr
12c45 61 6e 73 61 63 74 69 6f 6e 20 77 65 72 65 20 72 ansaction were r
12c46 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72 olled back after
12c47 0a 2a 2a 20 74 68 69 73 20 68 61 70 70 65 6e 65 .** this happene
12c48 64 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 d, the correct b
12c49 65 68 61 76 69 6f 75 72 20 77 6f 75 6c 64 20 62 ehaviour would b
12c4a 65 20 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 e to restore the
12c4b 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 6f 6e 74 current.** cont
12c4c 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e ent of the page.
12c4d 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 However, since
12c4e 74 68 69 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 this content is
12c4f 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 65 not present in e
12c50 69 74 68 65 72 0a 2a 2a 20 74 68 65 20 64 61 74 ither.** the dat
12c51 61 62 61 73 65 20 66 69 6c 65 20 6f 72 20 74 68 abase file or th
12c52 65 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 e portion of the
12c53 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 rollback journa
12c54 6c 20 61 6e 64 20 0a 2a 2a 20 73 75 62 2d 6a 6f l and .** sub-jo
12c55 75 72 6e 61 6c 20 72 6f 6c 6c 65 64 20 62 61 63 urnal rolled bac
12c56 6b 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 6f k the content co
12c57 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 73 74 6f uld not be resto
12c58 72 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 red and the.** d
12c59 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 6f atabase image wo
12c5a 75 6c 64 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 uld become corru
12c5b 70 74 2e 20 49 74 20 69 73 20 74 68 65 72 65 66 pt. It is theref
12c5c 6f 72 65 20 66 6f 72 74 75 6e 61 74 65 20 74 68 ore fortunate th
12c5d 61 74 20 0a 2a 2a 20 74 68 69 73 20 63 69 72 63 at .** this circ
12c5e 75 6d 73 74 61 6e 63 65 20 63 61 6e 6e 6f 74 20 umstance cannot
12c5f 61 72 69 73 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 arise..*/.#if de
12c60 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
12c61 55 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 UG).static void
12c62 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f assertTruncateCo
12c63 6e 73 74 72 61 69 6e 74 43 62 28 50 67 48 64 72 nstraintCb(PgHdr
12c64 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 65 72 74 *pPg){. assert
12c65 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 ( pPg->flags&PGH
12c66 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 61 73 DR_DIRTY );. as
12c67 73 65 72 74 28 20 21 73 75 62 6a 52 65 71 75 69 sert( !subjRequi
12c68 72 65 73 50 61 67 65 28 70 50 67 29 20 7c 7c 20 resPage(pPg) ||
12c69 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 67 2d 3e pPg->pgno<=pPg->
12c6a 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 29 pPager->dbSize )
12c6b 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 ;.}.static void
12c6c 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f assertTruncateCo
12c6d 6e 73 74 72 61 69 6e 74 28 50 61 67 65 72 20 2a nstraint(Pager *
12c6e 70 50 61 67 65 72 29 7b 0a 20 20 73 71 6c 69 74 pPager){. sqlit
12c6f 65 33 50 63 61 63 68 65 49 74 65 72 61 74 65 44 e3PcacheIterateD
12c70 69 72 74 79 28 70 50 61 67 65 72 2d 3e 70 50 43 irty(pPager->pPC
12c71 61 63 68 65 2c 20 61 73 73 65 72 74 54 72 75 6e ache, assertTrun
12c72 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 cateConstraintCb
12c73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 );.}.#else.# def
12c74 69 6e 65 20 61 73 73 65 72 74 54 72 75 6e 63 61 ine assertTrunca
12c75 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 teConstraint(pPa
12c76 67 65 72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a ger).#endif../*.
12c77 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 ** Truncate the
12c78 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
12c79 73 65 20 66 69 6c 65 20 69 6d 61 67 65 20 74 6f se file image to
12c7a 20 6e 50 61 67 65 20 70 61 67 65 73 2e 20 54 68 nPage pages. Th
12c7b 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 is .** function
12c7c 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c does not actuall
12c7d 79 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 y modify the dat
12c7e 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20 64 69 abase file on di
12c7f 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a 75 73 74 20 sk. It .** just
12c80 73 65 74 73 20 74 68 65 20 69 6e 74 65 72 6e 61 sets the interna
12c81 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 l state of the p
12c82 61 67 65 72 20 6f 62 6a 65 63 74 20 73 6f 20 74 ager object so t
12c83 68 61 74 20 74 68 65 20 0a 2a 2a 20 74 72 75 6e hat the .** trun
12c84 63 61 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 64 cation will be d
12c85 6f 6e 65 20 77 68 65 6e 20 74 68 65 20 63 75 72 one when the cur
12c86 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e rent transaction
12c87 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a is committed..*
12c88 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
12c89 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
12c8a 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
12c8b 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 Pager *pPager, P
12c8c 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 61 73 gno nPage){. as
12c8d 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
12c8e 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 61 SizeValid );. a
12c8f 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 ssert( pPager->d
12c90 62 53 69 7a 65 3e 3d 6e 50 61 67 65 20 29 3b 0a bSize>=nPage );.
12c91 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
12c92 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 ->state>=PAGER_R
12c93 45 53 45 52 56 45 44 20 29 3b 0a 20 20 70 50 61 ESERVED );. pPa
12c94 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 ger->dbSize = nP
12c95 61 67 65 3b 0a 20 20 61 73 73 65 72 74 54 72 75 age;. assertTru
12c96 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 ncateConstraint(
12c97 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pPager);.}../*.*
12c98 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 70 * Shutdown the p
12c99 61 67 65 20 63 61 63 68 65 2e 20 20 46 72 65 65 age cache. Free
12c9a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6e 64 20 all memory and
12c9b 63 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 73 2e close all files.
12c9c 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 61 6e .**.** If a tran
12c9d 73 61 63 74 69 6f 6e 20 77 61 73 20 69 6e 20 70 saction was in p
12c9e 72 6f 67 72 65 73 73 20 77 68 65 6e 20 74 68 69 rogress when thi
12c9f 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c s routine is cal
12ca0 6c 65 64 2c 20 74 68 61 74 0a 2a 2a 20 74 72 61 led, that.** tra
12ca1 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c nsaction is roll
12ca2 65 64 20 62 61 63 6b 2e 20 20 41 6c 6c 20 6f 75 ed back. All ou
12ca3 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 tstanding pages
12ca4 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 0a are invalidated.
12ca5 2a 2a 20 61 6e 64 20 74 68 65 69 72 20 6d 65 6d ** and their mem
12ca6 6f 72 79 20 69 73 20 66 72 65 65 64 2e 20 20 41 ory is freed. A
12ca7 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
12ca8 65 20 61 20 70 61 67 65 20 61 73 73 6f 63 69 61 e a page associa
12ca9 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 69 73 ted.** with this
12caa 20 70 61 67 65 20 63 61 63 68 65 20 61 66 74 65 page cache afte
12cab 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 r this function
12cac 72 65 74 75 72 6e 73 20 77 69 6c 6c 20 6c 69 6b returns will lik
12cad 65 6c 79 0a 2a 2a 20 72 65 73 75 6c 74 20 69 6e ely.** result in
12cae 20 61 20 63 6f 72 65 64 75 6d 70 2e 0a 2a 2a 0a a coredump..**.
12caf 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
12cb0 20 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 always succeeds
12cb1 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 . If a transacti
12cb2 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61 6e 20 on is active an
12cb3 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d 61 attempt.** is ma
12cb4 64 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 de to roll it ba
12cb5 63 6b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 ck. If an error
12cb6 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 occurs during th
12cb7 65 20 72 6f 6c 6c 62 61 63 6b 20 0a 2a 2a 20 61 e rollback .** a
12cb8 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 hot journal may
12cb9 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65 20 be left in the
12cba 66 69 6c 65 73 79 73 74 65 6d 20 62 75 74 20 6e filesystem but n
12cbb 6f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 o error is retur
12cbc 6e 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 ned.** to the ca
12cbd 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ller..*/.SQLITE_
12cbe 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
12cbf 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 te3PagerClose(Pa
12cc0 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 ger *pPager){.
12cc1 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 disable_simulate
12cc2 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20 d_io_errors();.
12cc3 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e sqlite3BeginBen
12cc4 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 70 ignMalloc();. p
12cc5 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d Pager->errCode =
12cc6 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78 0;. pPager->ex
12cc7 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 30 3b clusiveMode = 0;
12cc8 0a 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 . pager_reset(p
12cc9 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 4d 45 Pager);. if( ME
12cca 4d 44 42 20 29 7b 0a 20 20 20 20 70 61 67 65 72 MDB ){. pager
12ccb 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b _unlock(pPager);
12ccc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a . }else{. /*
12ccd 20 53 65 74 20 50 61 67 65 72 2e 6a 6f 75 72 6e Set Pager.journ
12cce 61 6c 48 64 72 20 74 6f 20 2d 31 20 66 6f 72 20 alHdr to -1 for
12ccf 74 68 65 20 62 65 6e 65 66 69 74 20 6f 66 20 74 the benefit of t
12cd0 68 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 he pager_playbac
12cd1 6b 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c k() . ** call
12cd2 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 6d 61 which may be ma
12cd3 64 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 de from within p
12cd4 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c agerUnlockAndRol
12cd5 6c 62 61 63 6b 28 29 2e 20 49 66 20 69 74 0a 20 lback(). If it.
12cd6 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 2d 31 2c ** is not -1,
12cd7 20 74 68 65 6e 20 74 68 65 20 75 6e 73 79 6e 63 then the unsync
12cd8 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 61 6e ed portion of an
12cd9 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 open journal fi
12cda 6c 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 62 65 le may. ** be
12cdb 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 6e 74 played back int
12cdc 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 o the database.
12cdd 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 If a power failu
12cde 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 0a re occurs while.
12cdf 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 ** this is h
12ce0 61 70 70 65 6e 69 6e 67 2c 20 74 68 65 20 64 61 appening, the da
12ce1 74 61 62 61 73 65 20 6d 61 79 20 62 65 63 6f 6d tabase may becom
12ce2 65 20 63 6f 72 72 75 70 74 2e 0a 20 20 20 20 2a e corrupt.. *
12ce3 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f /. pPager->jo
12ce4 75 72 6e 61 6c 48 64 72 20 3d 20 2d 31 3b 0a 20 urnalHdr = -1;.
12ce5 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e pagerUnlockAn
12ce6 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 dRollback(pPager
12ce7 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
12ce8 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
12ce9 29 3b 0a 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 );. enable_simu
12cea 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 lated_io_errors(
12ceb 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 );. PAGERTRACE(
12cec 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 ("CLOSE %d\n", P
12ced 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 AGERID(pPager)))
12cee 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c ;. IOTRACE(("CL
12cef 4f 53 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 OSE %p\n", pPage
12cf0 72 29 29 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 r)). sqlite3OsC
12cf1 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 lose(pPager->fd)
12cf2 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 ;. sqlite3PageF
12cf3 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d 70 ree(pPager->pTmp
12cf4 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69 74 65 Space);. sqlite
12cf5 33 50 63 61 63 68 65 43 6c 6f 73 65 28 70 50 61 3PcacheClose(pPa
12cf6 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a ger->pPCache);..
12cf7 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 #ifdef SQLITE_HA
12cf8 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20 70 50 S_CODEC. if( pP
12cf9 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 ager->xCodecFree
12cfa 20 29 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 ) pPager->xCode
12cfb 63 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 43 cFree(pPager->pC
12cfc 6f 64 65 63 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 odec);.#endif..
12cfd 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 assert( !pPager
12cfe 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 26 26 20 ->aSavepoint &&
12cff 21 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 !pPager->pInJour
12d00 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nal );. assert(
12d01 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d !isOpen(pPager-
12d02 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 65 6e >jfd) && !isOpen
12d03 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 (pPager->sjfd) )
12d04 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 ;.. sqlite3_fre
12d05 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 e(pPager);. ret
12d06 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
12d07 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e ..#if !defined(N
12d08 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 DEBUG) || define
12d09 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f d(SQLITE_TEST)./
12d0a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
12d0b 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 page number for
12d0c 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c page pPg..*/.SQL
12d0d 49 54 45 5f 50 52 49 56 41 54 45 20 50 67 6e 6f ITE_PRIVATE Pgno
12d0e 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
12d0f 65 6e 75 6d 62 65 72 28 44 62 50 61 67 65 20 2a enumber(DbPage *
12d10 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 pPg){. return p
12d11 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a 23 65 6e 64 Pg->pgno;.}.#end
12d12 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d if../*.** Increm
12d13 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 ent the referenc
12d14 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 61 67 65 e count for page
12d15 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f pPg..*/.SQLITE_
12d16 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
12d17 69 74 65 33 50 61 67 65 72 52 65 66 28 44 62 50 ite3PagerRef(DbP
12d18 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c age *pPg){. sql
12d19 69 74 65 33 50 63 61 63 68 65 52 65 66 28 70 50 ite3PcacheRef(pP
12d1a 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e g);.}../*.** Syn
12d1b 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 49 c the journal. I
12d1c 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 6d n other words, m
12d1d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 ake sure all the
12d1e 20 70 61 67 65 73 20 74 68 61 74 20 68 61 76 65 pages that have
12d1f 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e .** been written
12d20 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 to the journal
12d21 68 61 76 65 20 61 63 74 75 61 6c 6c 79 20 72 65 have actually re
12d22 61 63 68 65 64 20 74 68 65 20 73 75 72 66 61 63 ached the surfac
12d23 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69 73 6b e of the.** disk
12d24 20 61 6e 64 20 63 61 6e 20 62 65 20 72 65 73 74 and can be rest
12d25 6f 72 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e ored in the even
12d26 74 20 6f 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e t of a hot-journ
12d27 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a al rollback..**.
12d28 2a 2a 20 49 66 20 74 68 65 20 50 61 67 65 72 2e ** If the Pager.
12d29 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20 69 73 needSync flag is
12d2a 20 6e 6f 74 20 73 65 74 2c 20 74 68 65 6e 20 74 not set, then t
12d2b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
12d2c 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 a.** no-op. Othe
12d2d 72 77 69 73 65 2c 20 74 68 65 20 61 63 74 69 6f rwise, the actio
12d2e 6e 73 20 72 65 71 75 69 72 65 64 20 64 65 70 65 ns required depe
12d2f 6e 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e 61 nd on the journa
12d30 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e 64 20 74 68 l-mode.** and th
12d31 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74 e device charact
12d32 65 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 eristics of the
12d33 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c the file-system,
12d34 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
12d35 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 6a 6f ** * If the jo
12d36 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6e urnal file is an
12d37 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e in-memory journ
12d38 61 6c 20 66 69 6c 65 2c 20 6e 6f 20 61 63 74 69 al file, no acti
12d39 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20 20 20 20 62 on need.** b
12d3a 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 e taken..**.**
12d3b 20 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 * Otherwise, if
12d3c 20 74 68 65 20 64 65 76 69 63 65 20 64 6f 65 73 the device does
12d3d 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65 not support the
12d3e 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f SAFE_APPEND pro
12d3f 70 65 72 74 79 2c 0a 2a 2a 20 20 20 20 20 74 68 perty,.** th
12d40 65 6e 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c en the nRec fiel
12d41 64 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 d of the most re
12d42 63 65 6e 74 6c 79 20 77 72 69 74 74 65 6e 20 6a cently written j
12d43 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a ournal header.**
12d44 20 20 20 20 20 69 73 20 75 70 64 61 74 65 64 20 is updated
12d45 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e to contain the n
12d46 75 6d 62 65 72 20 6f 66 20 6a 6f 75 72 6e 61 6c umber of journal
12d47 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 68 61 records that ha
12d48 76 65 0a 2a 2a 20 20 20 20 20 62 65 65 6e 20 77 ve.** been w
12d49 72 69 74 74 65 6e 20 66 6f 6c 6c 6f 77 69 6e 67 ritten following
12d4a 20 69 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 it. If the page
12d4b 72 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 69 r is operating i
12d4c 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a 2a 2a 20 20 n full-sync.**
12d4d 20 20 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 mode, then th
12d4e 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
12d4f 73 20 73 79 6e 63 65 64 20 62 65 66 6f 72 65 20 s synced before
12d50 74 68 69 73 20 66 69 65 6c 64 20 69 73 20 75 70 this field is up
12d51 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a dated..**.** *
12d52 20 49 66 20 74 68 65 20 64 65 76 69 63 65 20 64 If the device d
12d53 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 oes not support
12d54 74 68 65 20 53 45 51 55 45 4e 54 49 41 4c 20 70 the SEQUENTIAL p
12d55 72 6f 70 65 72 74 79 2c 20 74 68 65 6e 20 0a 2a roperty, then .*
12d56 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69 * journal fi
12d57 6c 65 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a le is synced..**
12d58 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70 73 65 75 64 .** Or, in pseud
12d59 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 o-code:.**.**
12d5a 69 66 28 20 4e 4f 54 20 3c 69 6e 2d 6d 65 6d 6f if( NOT <in-memo
12d5b 72 79 20 6a 6f 75 72 6e 61 6c 3e 20 29 7b 0a 2a ry journal> ){.*
12d5c 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 41 * if( NOT SA
12d5d 46 45 5f 41 50 50 45 4e 44 20 29 7b 0a 2a 2a 20 FE_APPEND ){.**
12d5e 20 20 20 20 20 20 69 66 28 20 3c 66 75 6c 6c 2d if( <full-
12d5f 73 79 6e 63 20 6d 6f 64 65 3e 20 29 20 78 53 79 sync mode> ) xSy
12d60 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 nc(<journal file
12d61 3e 29 3b 0a 2a 2a 20 20 20 20 20 20 20 3c 75 70 >);.** <up
12d62 64 61 74 65 20 6e 52 65 63 20 66 69 65 6c 64 3e date nRec field>
12d63 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a 2a 20 20 20 .** } .**
12d64 20 20 69 66 28 20 4e 4f 54 20 53 45 51 55 45 4e if( NOT SEQUEN
12d65 54 49 41 4c 20 29 20 78 53 79 6e 63 28 3c 6a 6f TIAL ) xSync(<jo
12d66 75 72 6e 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a urnal file>);.**
12d67 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 }.**.** The P
12d68 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c ager.needSync fl
12d69 61 67 20 69 73 20 6e 65 76 65 72 20 62 65 20 73 ag is never be s
12d6a 65 74 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 et for temporary
12d6b 20 66 69 6c 65 73 2c 20 6f 72 20 61 6e 79 0a 2a files, or any.*
12d6c 2a 20 66 69 6c 65 20 6f 70 65 72 61 74 69 6e 67 * file operating
12d6d 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 in no-sync mode
12d6e 20 28 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 73 (Pager.noSync s
12d6f 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 29 2e et to non-zero).
12d70 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 .**.** If succes
12d71 73 66 75 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 sful, this routi
12d72 6e 65 20 63 6c 65 61 72 73 20 74 68 65 20 50 47 ne clears the PG
12d73 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c HDR_NEED_SYNC fl
12d74 61 67 20 6f 66 20 65 76 65 72 79 20 0a 2a 2a 20 ag of every .**
12d75 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20 68 page currently h
12d76 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 65 eld in memory be
12d77 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 53 fore returning S
12d78 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 QLITE_OK. If an
12d79 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 69 73 20 65 IO.** error is e
12d7a 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 65 6e ncountered, then
12d7b 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f the IO error co
12d7c 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 de is returned t
12d7d 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f o the caller..*/
12d7e 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 .static int sync
12d7f 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
12d80 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 Pager){. if( pP
12d81 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 ager->needSync )
12d82 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 {. assert( !p
12d83 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
12d84 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 );. if( pPage
12d85 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d r->journalMode!=
12d86 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
12d87 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 E_MEMORY ){.
12d88 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
12d89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12d8a 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
12d8b 20 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 63 code */. c
12d8c 6f 6e 73 74 20 69 6e 74 20 69 44 63 20 3d 20 73 onst int iDc = s
12d8d 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 qlite3OsDeviceCh
12d8e 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 aracteristics(pP
12d8f 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 ager->fd);.
12d90 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 assert( isOpen(
12d91 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a pPager->jfd) );.
12d92 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 . if( 0==(i
12d93 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f Dc&SQLITE_IOCAP_
12d94 53 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a SAFE_APPEND) ){.
12d95 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 /* This
12d96 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69 74 68 block deals with
12d97 20 61 6e 20 6f 62 73 63 75 72 65 20 70 72 6f 62 an obscure prob
12d98 6c 65 6d 2e 20 49 66 20 74 68 65 20 6c 61 73 74 lem. If the last
12d99 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 20 connection.
12d9a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 6f 74 ** that wrot
12d9b 65 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 e to this databa
12d9c 73 65 20 77 61 73 20 6f 70 65 72 61 74 69 6e 67 se was operating
12d9d 20 69 6e 20 70 65 72 73 69 73 74 65 6e 74 2d 6a in persistent-j
12d9e 6f 75 72 6e 61 6c 0a 20 20 20 20 20 20 20 20 2a ournal. *
12d9f 2a 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68 65 * mode, then the
12da0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 61 journal file ma
12da1 79 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 y at this point
12da2 61 63 74 75 61 6c 6c 79 20 62 65 20 6c 61 72 67 actually be larg
12da3 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 er. ** th
12da4 61 6e 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c an Pager.journal
12da5 4f 66 66 20 62 79 74 65 73 2e 20 49 66 20 74 68 Off bytes. If th
12da6 65 20 6e 65 78 74 20 74 68 69 6e 67 20 69 6e 20 e next thing in
12da7 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 the journal.
12da8 20 20 20 20 2a 2a 20 66 69 6c 65 20 68 61 70 70 ** file happ
12da9 65 6e 73 20 74 6f 20 62 65 20 61 20 6a 6f 75 72 ens to be a jour
12daa 6e 61 6c 2d 68 65 61 64 65 72 20 28 77 72 69 74 nal-header (writ
12dab 74 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 74 ten as part of t
12dac 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 72 he. ** pr
12dad 65 76 69 6f 75 73 20 63 6f 6e 6e 65 63 74 69 6f evious connectio
12dae 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c ns transaction),
12daf 20 61 6e 64 20 61 20 63 72 61 73 68 20 6f 72 20 and a crash or
12db0 70 6f 77 65 72 2d 66 61 69 6c 75 72 65 20 0a 20 power-failure .
12db1 20 20 20 20 20 20 20 2a 2a 20 6f 63 63 75 72 73 ** occurs
12db2 20 61 66 74 65 72 20 6e 52 65 63 20 69 73 20 75 after nRec is u
12db3 70 64 61 74 65 64 20 62 75 74 20 62 65 66 6f 72 pdated but befor
12db4 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f e this connectio
12db5 6e 20 77 72 69 74 65 73 20 0a 20 20 20 20 20 20 n writes .
12db6 20 20 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c ** anything el
12db7 73 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 se to the journa
12db8 6c 20 66 69 6c 65 20 28 6f 72 20 63 6f 6d 6d 69 l file (or commi
12db9 74 73 2f 72 6f 6c 6c 73 20 62 61 63 6b 20 69 74 ts/rolls back it
12dba 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 s . ** tr
12dbb 61 6e 73 61 63 74 69 6f 6e 29 2c 20 74 68 65 6e ansaction), then
12dbc 20 53 51 4c 69 74 65 20 6d 61 79 20 62 65 63 6f SQLite may beco
12dbd 6d 65 20 63 6f 6e 66 75 73 65 64 20 77 68 65 6e me confused when
12dbe 20 64 6f 69 6e 67 20 74 68 65 20 0a 20 20 20 20 doing the .
12dbf 20 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e ** hot-journ
12dc0 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 66 6f 6c 6c al rollback foll
12dc1 6f 77 69 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 owing recovery.
12dc2 49 74 20 6d 61 79 20 72 6f 6c 6c 20 62 61 63 6b It may roll back
12dc3 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 all. **
12dc4 6f 66 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 of this connecti
12dc5 6f 6e 73 20 64 61 74 61 2c 20 74 68 65 6e 20 70 ons data, then p
12dc6 72 6f 63 65 65 64 20 74 6f 20 72 6f 6c 6c 69 6e roceed to rollin
12dc7 67 20 62 61 63 6b 20 74 68 65 20 6f 6c 64 2c 0a g back the old,.
12dc8 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f ** out-o
12dc9 66 2d 64 61 74 65 20 64 61 74 61 20 74 68 61 74 f-date data that
12dca 20 66 6f 6c 6c 6f 77 73 20 69 74 2e 20 44 61 74 follows it. Dat
12dcb 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e abase corruption
12dcc 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 .. **.
12dcd 20 20 20 20 20 2a 2a 20 54 6f 20 77 6f 72 6b 20 ** To work
12dce 61 72 6f 75 6e 64 20 74 68 69 73 2c 20 69 66 20 around this, if
12dcf 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
12dd0 20 64 6f 65 73 20 61 70 70 65 61 72 20 74 6f 20 does appear to
12dd1 63 6f 6e 74 61 69 6e 0a 20 20 20 20 20 20 20 20 contain.
12dd2 2a 2a 20 61 20 76 61 6c 69 64 20 68 65 61 64 65 ** a valid heade
12dd3 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 50 61 67 65 r following Page
12dd4 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 74 68 r.journalOff, th
12dd5 65 6e 20 77 72 69 74 65 20 61 20 30 78 30 30 0a en write a 0x00.
12dd6 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 20 ** byte
12dd7 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 to the start of
12dd8 69 74 20 74 6f 20 70 72 65 76 65 6e 74 20 69 74 it to prevent it
12dd9 20 66 72 6f 6d 20 62 65 69 6e 67 20 72 65 63 6f from being reco
12dda 67 6e 69 7a 65 64 2e 0a 20 20 20 20 20 20 20 20 gnized..
12ddb 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 56 61 **. ** Va
12ddc 72 69 61 62 6c 65 20 69 4e 65 78 74 48 64 72 4f riable iNextHdrO
12ddd 66 66 73 65 74 20 69 73 20 73 65 74 20 74 6f 20 ffset is set to
12dde 74 68 65 20 6f 66 66 73 65 74 20 61 74 20 77 68 the offset at wh
12ddf 69 63 68 20 74 68 69 73 0a 20 20 20 20 20 20 20 ich this.
12de0 20 2a 2a 20 70 72 6f 62 6c 65 6d 61 74 69 63 20 ** problematic
12de1 68 65 61 64 65 72 20 77 69 6c 6c 20 6f 63 63 75 header will occu
12de2 72 2c 20 69 66 20 69 74 20 65 78 69 73 74 73 2e r, if it exists.
12de3 20 61 4d 61 67 69 63 20 69 73 20 75 73 65 64 20 aMagic is used
12de4 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73 20 61 . ** as a
12de5 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65 temporary buffe
12de6 72 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65 r to inspect the
12de7 20 66 69 72 73 74 20 63 6f 75 70 6c 65 20 6f 66 first couple of
12de8 20 62 79 74 65 73 20 6f 66 0a 20 20 20 20 20 20 bytes of.
12de9 20 20 2a 2a 20 74 68 65 20 70 6f 74 65 6e 74 69 ** the potenti
12dea 61 6c 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 al journal heade
12deb 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 r.. */.
12dec 20 20 20 20 20 20 69 36 34 20 69 4e 65 78 74 48 i64 iNextH
12ded 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 drOffset;.
12dee 20 20 75 38 20 61 4d 61 67 69 63 5b 38 5d 3b 0a u8 aMagic[8];.
12def 09 75 38 20 7a 48 65 61 64 65 72 5b 73 69 7a 65 .u8 zHeader[size
12df0 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 of(aJournalMagic
12df1 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63 70 79 28 7a )+4];...memcpy(z
12df2 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c Header, aJournal
12df3 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a Magic, sizeof(aJ
12df4 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 09 ournalMagic));..
12df5 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 put32bits(&zHead
12df6 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e er[sizeof(aJourn
12df7 61 6c 4d 61 67 69 63 29 5d 2c 20 70 50 61 67 65 alMagic)], pPage
12df8 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20 20 r->nRec);..
12df9 20 20 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 iNextHdrOffse
12dfa 74 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 t = journalHdrOf
12dfb 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 fset(pPager);.
12dfc 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
12dfd 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d e3OsRead(pPager-
12dfe 3e 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 38 2c >jfd, aMagic, 8,
12dff 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 29 iNextHdrOffset)
12e00 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
12e01 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 ==SQLITE_OK && 0
12e02 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c ==memcmp(aMagic,
12e03 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 aJournalMagic,
12e04 38 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 8) ){.
12e05 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 static const u8
12e06 7a 65 72 6f 62 79 74 65 20 3d 20 30 3b 0a 20 20 zerobyte = 0;.
12e07 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
12e08 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 ite3OsWrite(pPag
12e09 65 72 2d 3e 6a 66 64 2c 20 26 7a 65 72 6f 62 79 er->jfd, &zeroby
12e0a 74 65 2c 20 31 2c 20 69 4e 65 78 74 48 64 72 4f te, 1, iNextHdrO
12e0b 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 ffset);.
12e0c 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
12e0d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 !=SQLITE_OK && r
12e0e 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c!=SQLITE_IOERR_
12e0f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 SHORT_READ ){.
12e10 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 return r
12e11 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 c;. }..
12e12 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 /* Write t
12e13 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e he nRec value in
12e14 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 to the journal f
12e15 69 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20 69 ile header. If i
12e16 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6c n. ** ful
12e17 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d 6f l-synchronous mo
12e18 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 de, sync the jou
12e19 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69 73 rnal first. This
12e1a 20 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20 20 ensures that.
12e1b 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61 74 ** all dat
12e1c 61 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69 74 a has really hit
12e1d 20 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72 65 the disk before
12e1e 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65 64 nRec is updated
12e1f 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20 20 to mark.
12e20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e 64 ** it as a cand
12e21 69 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62 61 idate for rollba
12e22 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 ck.. **.
12e23 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 ** This i
12e24 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 69 s not required i
12e25 66 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 f the persistent
12e26 20 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73 20 media supports
12e27 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 the. ** S
12e28 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70 65 AFE_APPEND prope
12e29 72 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e 20 rty. Because in
12e2a 74 68 69 73 20 63 61 73 65 20 69 74 20 69 73 20 this case it is
12e2b 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20 20 not possible .
12e2c 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61 72 ** for gar
12e2d 62 61 67 65 20 64 61 74 61 20 74 6f 20 62 65 20 bage data to be
12e2e 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 appended to the
12e2f 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20 66 file, the nRec f
12e30 69 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 ield. **
12e31 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 is populated wit
12e32 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68 65 h 0xFFFFFFFF whe
12e33 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 n the journal he
12e34 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a ader is written.
12e35 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6e ** and n
12e36 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65 ever needs to be
12e37 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20 20 updated..
12e38 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 */. if(
12e39 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e pPager->fullSyn
12e3a 63 20 26 26 20 30 3d 3d 28 69 44 63 26 53 51 4c c && 0==(iDc&SQL
12e3b 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e ITE_IOCAP_SEQUEN
12e3c 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 TIAL) ){.
12e3d 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
12e3e 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 SYNC journal of
12e3f 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
12e40 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 Pager)));.
12e41 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 IOTRACE(("JS
12e42 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 YNC %p\n", pPage
12e43 72 29 29 0a 20 20 20 20 20 20 20 20 20 20 72 63 r)). rc
12e44 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
12e45 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 (pPager->jfd, pP
12e46 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 ager->sync_flags
12e47 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
12e48 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
12e49 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
12e4a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f }. IO
12e4b 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70 20 TRACE(("JHDR %p
12e4c 25 6c 6c 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c %lld\n", pPager,
12e4d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
12e4e 48 64 72 29 29 3b 0a 20 20 20 20 20 20 20 20 72 Hdr));. r
12e4f 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 c = sqlite3OsWri
12e50 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 te(.
12e51 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 pPager->jfd, zHe
12e52 61 64 65 72 2c 20 73 69 7a 65 6f 66 28 7a 48 65 ader, sizeof(zHe
12e53 61 64 65 72 29 2c 20 70 50 61 67 65 72 2d 3e 6a ader), pPager->j
12e54 6f 75 72 6e 61 6c 48 64 72 0a 09 29 3b 0a 20 20 ournalHdr..);.
12e55 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
12e56 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e LITE_OK ) return
12e57 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
12e58 20 20 20 69 66 28 20 30 3d 3d 28 69 44 63 26 53 if( 0==(iDc&S
12e59 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 QLITE_IOCAP_SEQU
12e5a 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20 20 20 ENTIAL) ){.
12e5b 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
12e5c 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20 6f 66 20 SYNC journal of
12e5d 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 %d\n", PAGERID(p
12e5e 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 20 20 Pager)));.
12e5f 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53 59 4e IOTRACE(("JSYN
12e60 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 C %p\n", pPager)
12e61 29 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ). rc = s
12e62 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 qlite3OsSync(pPa
12e63 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 ger->jfd, pPager
12e64 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c 20 0a 20 ->sync_flags| .
12e65 20 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72 (pPager
12e66 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d 3d 53 51 ->sync_flags==SQ
12e67 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3f 53 LITE_SYNC_FULL?S
12e68 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f QLITE_SYNC_DATAO
12e69 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20 20 29 NLY:0). )
12e6a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
12e6b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 !=SQLITE_OK ) re
12e6c 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
12e6d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 . }.. /* T
12e6e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
12e6f 77 61 73 20 6a 75 73 74 20 73 75 63 63 65 73 73 was just success
12e70 66 75 6c 6c 79 20 73 79 6e 63 65 64 2e 20 53 65 fully synced. Se
12e71 74 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 t Pager.needSync
12e72 20 0a 20 20 20 20 2a 2a 20 74 6f 20 7a 65 72 6f . ** to zero
12e73 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65 20 50 and clear the P
12e74 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 GHDR_NEED_SYNC f
12e75 6c 61 67 20 6f 6e 20 61 6c 6c 20 70 61 67 65 73 lag on all pages
12e76 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 s.. */. pP
12e77 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d ager->needSync =
12e78 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 0;. pPager->
12e79 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d journalStarted =
12e7a 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 1;. sqlite3P
12e7b 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c cacheClearSyncFl
12e7c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43 61 ags(pPager->pPCa
12e7d 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 che);. }.. ret
12e7e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
12e7f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 ../*.** The argu
12e80 6d 65 6e 74 20 69 73 20 74 68 65 20 66 69 72 73 ment is the firs
12e81 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 t in a linked li
12e82 73 74 20 6f 66 20 64 69 72 74 79 20 70 61 67 65 st of dirty page
12e83 73 20 63 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 62 s connected.** b
12e84 79 20 74 68 65 20 50 67 48 64 72 2e 70 44 69 72 y the PgHdr.pDir
12e85 74 79 20 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 ty pointer. This
12e86 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 73 function writes
12e87 20 65 61 63 68 20 6f 6e 65 20 6f 66 20 74 68 65 each one of the
12e88 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 .** in-memory pa
12e89 67 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 ges in the list
12e8a 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
12e8b 66 69 6c 65 2e 20 54 68 65 20 61 72 67 75 6d 65 file. The argume
12e8c 6e 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c nt may.** be NUL
12e8d 4c 2c 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 L, representing
12e8e 61 6e 20 65 6d 70 74 79 20 6c 69 73 74 2e 20 49 an empty list. I
12e8f 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 69 73 n this case this
12e90 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 function is.**
12e91 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 a no-op..**.** T
12e92 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 68 6f he pager must ho
12e93 6c 64 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 ld at least a RE
12e94 53 45 52 56 45 44 20 6c 6f 63 6b 20 77 68 65 6e SERVED lock when
12e95 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a this function.*
12e96 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 42 65 66 * is called. Bef
12e97 6f 72 65 20 77 72 69 74 69 6e 67 20 61 6e 79 74 ore writing anyt
12e98 68 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 hing to the data
12e99 62 61 73 65 20 66 69 6c 65 2c 20 74 68 69 73 20 base file, this
12e9a 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75 70 67 72 61 lock.** is upgra
12e9b 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 ded to an EXCLUS
12e9c 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 IVE lock. If the
12e9d 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20 lock cannot be
12e9e 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a 20 53 51 4c obtained,.** SQL
12e9f 49 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 ITE_BUSY is retu
12ea0 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 rned and no data
12ea1 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
12ea2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12ea3 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 ..** .** If the
12ea4 70 61 67 65 72 20 69 73 20 61 20 74 65 6d 70 2d pager is a temp-
12ea5 66 69 6c 65 20 70 61 67 65 72 20 61 6e 64 20 74 file pager and t
12ea6 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 2d 73 he actual file-s
12ea7 79 73 74 65 6d 20 66 69 6c 65 0a 2a 2a 20 69 73 ystem file.** is
12ea8 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 2c 20 69 not yet open, i
12ea9 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e 64 t is created and
12eaa 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 61 opened before a
12eab 6e 79 20 64 61 74 61 20 69 73 20 0a 2a 2a 20 77 ny data is .** w
12eac 72 69 74 74 65 6e 20 6f 75 74 2e 0a 2a 2a 0a 2a ritten out..**.*
12ead 2a 20 4f 6e 63 65 20 74 68 65 20 6c 6f 63 6b 20 * Once the lock
12eae 68 61 73 20 62 65 65 6e 20 75 70 67 72 61 64 65 has been upgrade
12eaf 64 20 61 6e 64 2c 20 69 66 20 6e 65 63 65 73 73 d and, if necess
12eb0 61 72 79 2c 20 74 68 65 20 66 69 6c 65 20 6f 70 ary, the file op
12eb1 65 6e 65 64 2c 0a 2a 2a 20 74 68 65 20 70 61 67 ened,.** the pag
12eb2 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f es are written o
12eb3 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ut to the databa
12eb4 73 65 20 66 69 6c 65 20 69 6e 20 6c 69 73 74 20 se file in list
12eb5 6f 72 64 65 72 2e 20 57 72 69 74 69 6e 67 0a 2a order. Writing.*
12eb6 2a 20 61 20 70 61 67 65 20 69 73 20 73 6b 69 70 * a page is skip
12eb7 70 65 64 20 69 66 20 69 74 20 6d 65 65 74 73 20 ped if it meets
12eb8 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66 6f either of the fo
12eb9 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 llowing criteria
12eba 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 :.**.** * The
12ebb 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 page number is g
12ebc 72 65 61 74 65 72 20 74 68 61 6e 20 50 61 67 65 reater than Page
12ebd 72 2e 64 62 53 69 7a 65 2c 20 6f 72 0a 2a 2a 20 r.dbSize, or.**
12ebe 20 20 2a 20 54 68 65 20 50 47 48 44 52 5f 44 4f * The PGHDR_DO
12ebf 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 20 69 73 NT_WRITE flag is
12ec0 20 73 65 74 20 6f 6e 20 74 68 65 20 70 61 67 65 set on the page
12ec1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 72 69 74 69 ..**.** If writi
12ec2 6e 67 20 6f 75 74 20 61 20 70 61 67 65 20 63 61 ng out a page ca
12ec3 75 73 65 73 20 74 68 65 20 64 61 74 61 62 61 73 uses the databas
12ec4 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 e file to grow,
12ec5 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 Pager.dbFileSize
12ec6 0a 2a 2a 20 69 73 20 75 70 64 61 74 65 64 20 61 .** is updated a
12ec7 63 63 6f 72 64 69 6e 67 6c 79 2e 20 49 66 20 70 ccordingly. If p
12ec8 61 67 65 20 31 20 69 73 20 77 72 69 74 74 65 6e age 1 is written
12ec9 20 6f 75 74 2c 20 74 68 65 6e 20 74 68 65 20 76 out, then the v
12eca 61 6c 75 65 20 63 61 63 68 65 64 0a 2a 2a 20 69 alue cached.** i
12ecb 6e 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 n Pager.dbFileVe
12ecc 72 73 5b 5d 20 69 73 20 75 70 64 61 74 65 64 20 rs[] is updated
12ecd 74 6f 20 6d 61 74 63 68 20 74 68 65 20 6e 65 77 to match the new
12ece 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
12ecf 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 .** the database
12ed0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 file..**.** If
12ed1 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 everything is su
12ed2 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 ccessful, SQLITE
12ed3 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
12ed4 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 If an IO error
12ed5 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 61 6e 20 49 .** occurs, an I
12ed6 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 O error code is
12ed7 72 65 74 75 72 6e 65 64 2e 20 4f 72 2c 20 69 66 returned. Or, if
12ed8 20 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c the EXCLUSIVE l
12ed9 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 ock cannot.** be
12eda 20 6f 62 74 61 69 6e 65 64 2c 20 53 51 4c 49 54 obtained, SQLIT
12edb 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72 6e E_BUSY is return
12edc 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
12edd 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 t pager_write_pa
12ede 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70 4c gelist(PgHdr *pL
12edf 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 ist){. Pager *p
12ee0 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 20 20 Pager;
12ee1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12ee2 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a Pager object */.
12ee3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 int rc;
12ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12ee5 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e /* Return
12ee6 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 69 66 28 20 code */.. if(
12ee7 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 NEVER(pList==0)
12ee8 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
12ee9 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d 20 70 OK;. pPager = p
12eea 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a 0a 20 List->pPager;..
12eeb 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e /* At this poin
12eec 74 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 65 t there may be e
12eed 69 74 68 65 72 20 61 20 52 45 53 45 52 56 45 44 ither a RESERVED
12eee 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f or EXCLUSIVE lo
12eef 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 ck on the. ** d
12ef0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 atabase file. If
12ef1 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 there is alread
12ef2 79 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c y an EXCLUSIVE l
12ef3 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ock, the followi
12ef4 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 20 69 73 20 ng. ** call is
12ef5 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 a no-op.. **.
12ef6 2a 2a 20 4d 6f 76 69 6e 67 20 74 68 65 20 6c 6f ** Moving the lo
12ef7 63 6b 20 66 72 6f 6d 20 52 45 53 45 52 56 45 44 ck from RESERVED
12ef8 20 74 6f 20 45 58 43 4c 55 53 49 56 45 20 61 63 to EXCLUSIVE ac
12ef9 74 75 61 6c 6c 79 20 69 6e 76 6f 6c 76 65 73 20 tually involves
12efa 67 6f 69 6e 67 0a 20 20 2a 2a 20 74 68 72 6f 75 going. ** throu
12efb 67 68 20 61 6e 20 69 6e 74 65 72 6d 65 64 69 61 gh an intermedia
12efc 74 65 20 73 74 61 74 65 20 50 45 4e 44 49 4e 47 te state PENDING
12efd 2e 20 20 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f . A PENDING lo
12efe 63 6b 20 70 72 65 76 65 6e 74 73 20 6e 65 77 0a ck prevents new.
12eff 20 20 2a 2a 20 72 65 61 64 65 72 73 20 66 72 6f ** readers fro
12f00 6d 20 61 74 74 61 63 68 69 6e 67 20 74 6f 20 74 m attaching to t
12f01 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20 he database but
12f02 69 73 20 75 6e 73 75 66 66 69 63 69 65 6e 74 20 is unsufficient
12f03 66 6f 72 20 75 73 20 74 6f 0a 20 20 2a 2a 20 77 for us to. ** w
12f04 72 69 74 65 2e 20 20 54 68 65 20 69 64 65 61 20 rite. The idea
12f05 6f 66 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 of a PENDING loc
12f06 6b 20 69 73 20 74 6f 20 70 72 65 76 65 6e 74 20 k is to prevent
12f07 6e 65 77 20 72 65 61 64 65 72 73 20 66 72 6f 6d new readers from
12f08 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67 20 69 6e 20 . ** coming in
12f09 77 68 69 6c 65 20 77 65 20 77 61 69 74 20 66 6f while we wait fo
12f0a 72 20 65 78 69 73 74 69 6e 67 20 72 65 61 64 65 r existing reade
12f0b 72 73 20 74 6f 20 63 6c 65 61 72 2e 0a 20 20 2a rs to clear.. *
12f0c 2a 0a 20 20 2a 2a 20 57 68 69 6c 65 20 74 68 65 *. ** While the
12f0d 20 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 pager is in the
12f0e 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c RESERVED state,
12f0f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 the original da
12f10 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 2a 2a tabase file. **
12f11 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20 61 6e is unchanged an
12f12 64 20 77 65 20 63 61 6e 20 72 6f 6c 6c 62 61 63 d we can rollbac
12f13 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 k without having
12f14 20 74 6f 20 70 6c 61 79 62 61 63 6b 20 74 68 65 to playback the
12f15 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 69 6e . ** journal in
12f16 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 to the original
12f17 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 database file.
12f18 4f 6e 63 65 20 77 65 20 74 72 61 6e 73 69 74 69 Once we transiti
12f19 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45 58 43 4c 55 on to. ** EXCLU
12f1a 53 49 56 45 2c 20 69 74 20 6d 65 61 6e 73 20 74 SIVE, it means t
12f1b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
12f1c 20 68 61 73 20 62 65 65 6e 20 63 68 61 6e 67 65 has been change
12f1d 64 20 61 6e 64 20 61 6e 79 20 72 6f 6c 6c 62 61 d and any rollba
12f1e 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c 20 72 65 71 ck. ** will req
12f1f 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c 20 70 uire a journal p
12f20 6c 61 79 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 layback.. */.
12f21 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
12f22 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 state>=PAGER_RES
12f23 45 52 56 45 44 20 29 3b 0a 20 20 72 63 20 3d 20 ERVED );. rc =
12f24 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f pager_wait_on_lo
12f25 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 ck(pPager, EXCLU
12f26 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 0a 20 20 2f SIVE_LOCK);.. /
12f27 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 * If the file is
12f28 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 68 61 73 a temp-file has
12f29 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70 not yet been op
12f2a 65 6e 65 64 2c 20 6f 70 65 6e 20 69 74 20 6e 6f ened, open it no
12f2b 77 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 6e 6f w. It. ** is no
12f2c 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 72 t possible for r
12f2d 63 20 74 6f 20 62 65 20 6f 74 68 65 72 20 74 68 c to be other th
12f2e 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 an SQLITE_OK if
12f2f 74 68 69 73 20 62 72 61 6e 63 68 0a 20 20 2a 2a this branch. **
12f30 20 69 73 20 74 61 6b 65 6e 2c 20 61 73 20 70 61 is taken, as pa
12f31 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b ger_wait_on_lock
12f32 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f () is a no-op fo
12f33 72 20 74 65 6d 70 2d 66 69 6c 65 73 2e 0a 20 20 r temp-files..
12f34 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e */. if( !isOpen
12f35 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a (pPager->fd) ){.
12f36 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
12f37 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 er->tempFile &&
12f38 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b rc==SQLITE_OK );
12f39 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f . rc = pagerO
12f3a 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20 pentemp(pPager,
12f3b 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 67 pPager->fd, pPag
12f3c 65 72 2d 3e 76 66 73 46 6c 61 67 73 29 3b 0a 20 er->vfsFlags);.
12f3d 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d }.. while( rc=
12f3e 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4c =SQLITE_OK && pL
12f3f 69 73 74 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 ist ){. Pgno
12f40 70 67 6e 6f 20 3d 20 70 4c 69 73 74 2d 3e 70 67 pgno = pList->pg
12f41 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 no;.. /* If t
12f42 68 65 72 65 20 61 72 65 20 64 69 72 74 79 20 70 here are dirty p
12f43 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65 ages in the page
12f44 20 63 61 63 68 65 20 77 69 74 68 20 70 61 67 65 cache with page
12f45 20 6e 75 6d 62 65 72 73 20 67 72 65 61 74 65 72 numbers greater
12f46 0a 20 20 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 . ** than Pag
12f47 65 72 2e 64 62 53 69 7a 65 2c 20 74 68 69 73 20 er.dbSize, this
12f48 6d 65 61 6e 73 20 73 71 6c 69 74 65 33 50 61 67 means sqlite3Pag
12f49 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 erTruncateImage(
12f4a 29 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a ) was called to.
12f4b 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20 ** make the
12f4c 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72 file smaller (pr
12f4d 65 73 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f esumably by auto
12f4e 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44 -vacuum code). D
12f4f 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20 o not write.
12f50 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 61 67 65 ** any such page
12f51 73 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20 s to the file..
12f52 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 **. ** Als
12f53 6f 2c 20 64 6f 20 6e 6f 74 20 77 72 69 74 65 20 o, do not write
12f54 6f 75 74 20 61 6e 79 20 70 61 67 65 20 74 68 61 out any page tha
12f55 74 20 68 61 73 20 74 68 65 20 50 47 48 44 52 5f t has the PGHDR_
12f56 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61 67 0a DONT_WRITE flag.
12f57 20 20 20 20 2a 2a 20 73 65 74 20 28 73 65 74 20 ** set (set
12f58 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 by sqlite3PagerD
12f59 6f 6e 74 57 72 69 74 65 28 29 29 2e 20 20 4e 6f ontWrite()). No
12f5a 74 65 20 74 68 61 74 20 69 66 20 63 6f 6d 70 69 te that if compi
12f5b 6c 65 64 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 led with. **
12f5c 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 SQLITE_SECURE_DE
12f5d 4c 45 54 45 20 74 68 65 20 50 47 48 44 52 5f 44 LETE the PGHDR_D
12f5e 4f 4e 54 5f 57 52 49 54 45 20 62 69 74 20 69 73 ONT_WRITE bit is
12f5f 20 6e 65 76 65 72 20 73 65 74 20 61 6e 64 20 73 never set and s
12f60 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 65 63 o. ** the sec
12f61 6f 6e 64 20 74 65 73 74 20 69 73 20 61 6c 77 61 ond test is alwa
12f62 79 73 20 74 72 75 65 2e 0a 20 20 20 20 2a 2f 0a ys true.. */.
12f63 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 if( pgno<=pP
12f64 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 ager->dbSize &&
12f65 30 3d 3d 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 0==(pList->flags
12f66 26 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 &PGHDR_DONT_WRIT
12f67 45 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 E) ){. i64
12f68 6f 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 offset = (pgno-1
12f69 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 )*(i64)pPager->p
12f6a 61 67 65 53 69 7a 65 3b 20 20 20 2f 2a 20 4f 66 ageSize; /* Of
12f6b 66 73 65 74 20 74 6f 20 77 72 69 74 65 20 2a 2f fset to write */
12f6c 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61 . char *pDa
12f6d 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
12f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12f6f 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 6f /* Data to
12f70 20 77 72 69 74 65 20 2a 2f 20 20 20 20 0a 0a 20 write */ ..
12f71 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 65 20 74 /* Encode t
12f72 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 he database */.
12f73 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67 CODEC2(pPag
12f74 65 72 2c 20 70 4c 69 73 74 2d 3e 70 44 61 74 61 er, pList->pData
12f75 2c 20 70 67 6e 6f 2c 20 36 2c 20 72 65 74 75 72 , pgno, 6, retur
12f76 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 n SQLITE_NOMEM,
12f77 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 2f pData);.. /
12f78 2a 20 57 72 69 74 65 20 6f 75 74 20 74 68 65 20 * Write out the
12f79 70 61 67 65 20 64 61 74 61 2e 20 2a 2f 0a 20 20 page data. */.
12f7a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
12f7b 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e OsWrite(pPager->
12f7c 66 64 2c 20 70 44 61 74 61 2c 20 70 50 61 67 65 fd, pData, pPage
12f7d 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66 66 r->pageSize, off
12f7e 73 65 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 set);.. /*
12f7f 49 66 20 70 61 67 65 20 31 20 77 61 73 20 6a 75 If page 1 was ju
12f80 73 74 20 77 72 69 74 74 65 6e 2c 20 75 70 64 61 st written, upda
12f81 74 65 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 te Pager.dbFileV
12f82 65 72 73 20 74 6f 20 6d 61 74 63 68 0a 20 20 20 ers to match.
12f83 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 ** the value
12f84 6e 6f 77 20 73 74 6f 72 65 64 20 69 6e 20 74 68 now stored in th
12f85 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
12f86 20 49 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 If writing this
12f87 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 . ** page
12f88 63 61 75 73 65 64 20 74 68 65 20 64 61 74 61 62 caused the datab
12f89 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72 6f 77 ase file to grow
12f8a 2c 20 75 70 64 61 74 65 20 64 62 46 69 6c 65 53 , update dbFileS
12f8b 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 ize. . */.
12f8c 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 if( pgno==1
12f8d 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
12f8e 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62 46 69 py(&pPager->dbFi
12f8f 6c 65 56 65 72 73 2c 20 26 70 44 61 74 61 5b 32 leVers, &pData[2
12f90 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65 4], sizeof(pPage
12f91 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b r->dbFileVers));
12f92 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
12f93 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e f( pgno>pPager->
12f94 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 dbFileSize ){.
12f95 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 pPager->db
12f96 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b FileSize = pgno;
12f97 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 . }..
12f98 2f 2a 20 55 70 64 61 74 65 20 61 6e 79 20 62 61 /* Update any ba
12f99 63 6b 75 70 20 6f 62 6a 65 63 74 73 20 63 6f 70 ckup objects cop
12f9a 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 ying the content
12f9b 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 72 2e s of this pager.
12f9c 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
12f9d 33 42 61 63 6b 75 70 55 70 64 61 74 65 28 70 50 3BackupUpdate(pP
12f9e 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 2c 20 70 ager->pBackup, p
12f9f 67 6e 6f 2c 20 28 75 38 2a 29 70 4c 69 73 74 2d gno, (u8*)pList-
12fa0 3e 70 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 >pData);..
12fa1 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4f PAGERTRACE(("STO
12fa2 52 45 20 25 64 20 70 61 67 65 20 25 64 20 68 61 RE %d page %d ha
12fa3 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 sh(%08x)\n",.
12fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12fa5 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
12fa6 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 pgno, pager_pag
12fa7 65 68 61 73 68 28 70 4c 69 73 74 29 29 29 3b 0a ehash(pList)));.
12fa8 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 IOTRACE(("
12fa9 50 47 4f 55 54 20 25 70 20 25 64 5c 6e 22 2c 20 PGOUT %p %d\n",
12faa 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a pPager, pgno));.
12fab 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 PAGER_INCR
12fac 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 (sqlite3_pager_w
12fad 72 69 74 65 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 ritedb_count);.
12fae 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 PAGER_INCR(
12faf 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 29 3b pPager->nWrite);
12fb0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
12fb1 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4e PAGERTRACE(("N
12fb2 4f 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 OSTORE %d page %
12fb3 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 d\n", PAGERID(pP
12fb4 61 67 65 72 29 2c 20 70 67 6e 6f 29 29 3b 0a 20 ager), pgno));.
12fb5 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 }.#ifdef SQLI
12fb6 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 TE_CHECK_PAGES.
12fb7 20 20 20 70 4c 69 73 74 2d 3e 70 61 67 65 48 61 pList->pageHa
12fb8 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 sh = pager_pageh
12fb9 61 73 68 28 70 4c 69 73 74 29 3b 0a 23 65 6e 64 ash(pList);.#end
12fba 69 66 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 if. pList = p
12fbb 4c 69 73 74 2d 3e 70 44 69 72 74 79 3b 0a 20 20 List->pDirty;.
12fbc 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }.. return rc;.
12fbd 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 }../*.** Append
12fbe 61 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65 20 a record of the
12fbf 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 current state of
12fc0 20 70 61 67 65 20 70 50 67 20 74 6f 20 74 68 65 page pPg to the
12fc1 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 0a 2a sub-journal. .*
12fc2 2a 20 49 74 20 69 73 20 74 68 65 20 63 61 6c 6c * It is the call
12fc3 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 ers responsibili
12fc4 74 79 20 74 6f 20 75 73 65 20 73 75 62 6a 52 65 ty to use subjRe
12fc5 71 75 69 72 65 73 50 61 67 65 28 29 20 74 6f 20 quiresPage() to
12fc6 63 68 65 63 6b 20 0a 2a 2a 20 74 68 61 74 20 69 check .** that i
12fc7 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 71 75 t is really requ
12fc8 69 72 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c ired before call
12fc9 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f ing this functio
12fca 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 n..**.** If succ
12fcb 65 73 73 66 75 6c 2c 20 73 65 74 20 74 68 65 20 essful, set the
12fcc 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e bit correspondin
12fcd 67 20 74 6f 20 70 50 67 2d 3e 70 67 6e 6f 20 69 g to pPg->pgno i
12fce 6e 20 74 68 65 20 62 69 74 76 65 63 73 0a 2a 2a n the bitvecs.**
12fcf 20 66 6f 72 20 61 6c 6c 20 6f 70 65 6e 20 73 61 for all open sa
12fd0 76 65 70 6f 69 6e 74 73 20 62 65 66 6f 72 65 20 vepoints before
12fd1 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a returning..**.**
12fd2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
12fd3 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b eturns SQLITE_OK
12fd4 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69 if everything i
12fd5 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 6e s successful, an
12fd6 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 IO.** error cod
12fd7 65 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74 e if the attempt
12fd8 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 to write to the
12fd9 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 61 69 sub-journal fai
12fda 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53 51 4c 49 54 ls, or .** SQLIT
12fdb 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c E_NOMEM if a mal
12fdc 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65 20 loc fails while
12fdd 73 65 74 74 69 6e 67 20 61 20 62 69 74 20 69 6e setting a bit in
12fde 20 61 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 a savepoint.**
12fdf 62 69 74 76 65 63 2e 0a 2a 2f 0a 73 74 61 74 69 bitvec..*/.stati
12fe0 63 20 69 6e 74 20 73 75 62 6a 6f 75 72 6e 61 6c c int subjournal
12fe1 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 Page(PgHdr *pPg)
12fe2 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
12fe3 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67 65 72 20 ITE_OK;. Pager
12fe4 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 *pPager = pPg->p
12fe5 50 61 67 65 72 3b 0a 20 20 69 66 28 20 69 73 4f Pager;. if( isO
12fe6 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 pen(pPager->sjfd
12fe7 29 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 ) ){. void *p
12fe8 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 Data = pPg->pDat
12fe9 61 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 a;. i64 offse
12fea 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 t = pPager->nSub
12feb 52 65 63 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70 Rec*(4+pPager->p
12fec 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 63 68 ageSize);. ch
12fed 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 ar *pData2;..
12fee 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 CODEC2(pPager,
12fef 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f pData, pPg->pgno
12ff0 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 , 7, return SQLI
12ff1 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 TE_NOMEM, pData2
12ff2 29 3b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 );. PAGERTRAC
12ff3 45 28 28 22 53 54 4d 54 2d 4a 4f 55 52 4e 41 4c E(("STMT-JOURNAL
12ff4 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 %d page %d\n",
12ff5 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c PAGERID(pPager),
12ff6 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 pPg->pgno));.
12ff7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 . assert( pag
12ff8 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 eInJournal(pPg)
12ff9 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 || pPg->pgno>pPa
12ffa 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
12ffb 29 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 );. rc = writ
12ffc 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e e32bits(pPager->
12ffd 73 6a 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 sjfd, offset, pP
12ffe 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 69 66 g->pgno);. if
12fff 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
13000 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
13001 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 lite3OsWrite(pPa
13002 67 65 72 2d 3e 73 6a 66 64 2c 20 70 44 61 74 61 ger->sjfd, pData
13003 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 2, pPager->pageS
13004 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b 0a ize, offset+4);.
13005 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
13006 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13007 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 . pPager->nSu
13008 62 52 65 63 2b 2b 3b 0a 20 20 20 20 61 73 73 65 bRec++;. asse
13009 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 rt( pPager->nSav
1300a 65 70 6f 69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 epoint>0 );.
1300b 72 63 20 3d 20 61 64 64 54 6f 53 61 76 65 70 6f rc = addToSavepo
1300c 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 intBitvecs(pPage
1300d 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 r, pPg->pgno);.
1300e 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
1300f 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 }.../*.** This f
13010 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
13011 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65 20 d by the pcache
13012 6c 61 79 65 72 20 77 68 65 6e 20 69 74 20 68 61 layer when it ha
13013 73 20 72 65 61 63 68 65 64 20 73 6f 6d 65 0a 2a s reached some.*
13014 2a 20 73 6f 66 74 20 6d 65 6d 6f 72 79 20 6c 69 * soft memory li
13015 6d 69 74 2e 20 54 68 65 20 66 69 72 73 74 20 61 mit. The first a
13016 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 rgument is a poi
13017 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65 72 20 nter to a Pager
13018 6f 62 6a 65 63 74 0a 2a 2a 20 28 63 61 73 74 20 object.** (cast
13019 61 73 20 61 20 76 6f 69 64 2a 29 2e 20 54 68 65 as a void*). The
1301a 20 70 61 67 65 72 20 69 73 20 61 6c 77 61 79 73 pager is always
1301b 20 27 70 75 72 67 65 61 62 6c 65 27 20 28 6e 6f 'purgeable' (no
1301c 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a t an in-memory.*
1301d 2a 20 64 61 74 61 62 61 73 65 29 2e 20 54 68 65 * database). The
1301e 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1301f 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 is a reference
13020 74 6f 20 61 20 70 61 67 65 20 74 68 61 74 20 69 to a page that i
13021 73 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 s .** currently
13022 64 69 72 74 79 20 62 75 74 20 68 61 73 20 6e 6f dirty but has no
13023 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 outstanding ref
13024 65 72 65 6e 63 65 73 2e 20 54 68 65 20 70 61 67 erences. The pag
13025 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 e.** is always a
13026 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 ssociated with t
13027 68 65 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 he Pager object
13028 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 passed as the fi
13029 72 73 74 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 rst .** argument
1302a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 62 20 ..**.** The job
1302b 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
1302c 20 69 73 20 74 6f 20 6d 61 6b 65 20 70 50 67 20 is to make pPg
1302d 63 6c 65 61 6e 20 62 79 20 77 72 69 74 69 6e 67 clean by writing
1302e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a its contents.**
1302f 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 out to the data
13030 62 61 73 65 20 66 69 6c 65 2c 20 69 66 20 70 6f base file, if po
13031 73 73 69 62 6c 65 2e 20 54 68 69 73 20 6d 61 79 ssible. This may
13032 20 69 6e 76 6f 6c 76 65 20 73 79 6e 63 69 6e 67 involve syncing
13033 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 the.** journal
13034 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 file. .**.** If
13035 73 75 63 63 65 73 73 66 75 6c 2c 20 73 71 6c 69 successful, sqli
13036 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 te3PcacheMakeCle
13037 61 6e 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f an() is called o
13038 6e 20 74 68 65 20 70 61 67 65 20 61 6e 64 0a 2a n the page and.*
13039 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 * SQLITE_OK retu
1303a 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 rned. If an IO e
1303b 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
1303c 65 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 e trying to make
1303d 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 63 6c 65 the.** page cle
1303e 61 6e 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 an, the IO error
1303f 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
13040 64 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 63 d. If the page c
13041 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6d 61 64 65 annot be.** made
13042 20 63 6c 65 61 6e 20 66 6f 72 20 73 6f 6d 65 20 clean for some
13043 6f 74 68 65 72 20 72 65 61 73 6f 6e 2c 20 62 75 other reason, bu
13044 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 t no error occur
13045 73 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f s, then SQLITE_O
13046 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 K.** is returned
13047 20 62 79 20 73 71 6c 69 74 65 33 50 63 61 63 68 by sqlite3Pcach
13048 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73 20 eMakeClean() is
13049 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 not called..*/.s
1304a 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 53 tatic int pagerS
1304b 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c 20 50 tress(void *p, P
1304c 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 gHdr *pPg){. Pa
1304d 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 28 50 ger *pPager = (P
1304e 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74 20 ager *)p;. int
1304f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
13050 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e . assert( pPg->
13051 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 pPager==pPager )
13052 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d ;. assert( pPg-
13053 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 >flags&PGHDR_DIR
13054 54 59 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 TY );.. /* The
13055 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 69 doNotSync flag i
13056 73 20 73 65 74 20 62 79 20 74 68 65 20 73 71 6c s set by the sql
13057 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 ite3PagerWrite()
13058 20 66 75 6e 63 74 69 6f 6e 20 77 68 69 6c 65 20 function while
13059 69 74 0a 20 20 2a 2a 20 69 73 20 6a 6f 75 72 6e it. ** is journ
1305a 61 6c 6c 69 6e 67 20 61 20 73 65 74 20 6f 66 20 alling a set of
1305b 74 77 6f 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 two or more data
1305c 62 61 73 65 20 70 61 67 65 73 20 74 68 61 74 20 base pages that
1305d 61 72 65 20 73 74 6f 72 65 64 0a 20 20 2a 2a 20 are stored. **
1305e 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 69 73 6b on the same disk
1305f 20 73 65 63 74 6f 72 2e 20 53 79 6e 63 69 6e 67 sector. Syncing
13060 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 the journal is
13061 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 77 68 69 6c not allowed whil
13062 65 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68 e. ** this is h
13063 61 70 70 65 6e 69 6e 67 20 61 73 20 69 74 20 69 appening as it i
13064 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 s important that
13065 20 61 6c 6c 20 6d 65 6d 62 65 72 73 20 6f 66 20 all members of
13066 73 75 63 68 20 61 0a 20 20 2a 2a 20 73 65 74 20 such a. ** set
13067 6f 66 20 70 61 67 65 73 20 61 72 65 20 73 79 6e of pages are syn
13068 63 65 64 20 74 6f 20 64 69 73 6b 20 74 6f 67 65 ced to disk toge
13069 74 68 65 72 2e 20 53 6f 2c 20 69 66 20 74 68 65 ther. So, if the
1306a 20 70 61 67 65 20 74 68 69 73 20 66 75 6e 63 74 page this funct
1306b 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 72 79 69 ion. ** is tryi
1306c 6e 67 20 74 6f 20 6d 61 6b 65 20 63 6c 65 61 6e ng to make clean
1306d 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 will require a
1306e 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 20 61 6e 64 journal sync and
1306f 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 0a 20 the doNotSync.
13070 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c ** flag is set,
13071 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 return without
13072 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 doing anything.
13073 54 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 The pcache layer
13074 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a 75 73 74 20 will. ** just
13075 68 61 76 65 20 74 6f 20 67 6f 20 61 68 65 61 64 have to go ahead
13076 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 61 20 and allocate a
13077 6e 65 77 20 70 61 67 65 20 62 75 66 66 65 72 20 new page buffer
13078 69 6e 73 74 65 61 64 20 6f 66 0a 20 20 2a 2a 20 instead of. **
13079 72 65 75 73 69 6e 67 20 70 50 67 2e 0a 20 20 2a reusing pPg.. *
1307a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 *. ** Similarly
1307b 2c 20 69 66 20 74 68 65 20 70 61 67 65 72 20 68 , if the pager h
1307c 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 as already enter
1307d 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 ed the error sta
1307e 74 65 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 te, do not. **
1307f 74 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 try to write the
13080 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 50 67 contents of pPg
13081 20 74 6f 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 to disk.. */.
13082 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 if( NEVER(pPage
13083 72 2d 3e 65 72 72 43 6f 64 65 29 0a 20 20 20 7c r->errCode). |
13084 7c 20 28 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 | (pPager->doNot
13085 53 79 6e 63 20 26 26 20 70 50 67 2d 3e 66 6c 61 Sync && pPg->fla
13086 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
13087 4e 43 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 NC). ){. ret
13088 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
13089 20 7d 0a 0a 20 20 2f 2a 20 53 79 6e 63 20 74 68 }.. /* Sync th
1308a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
1308b 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 f required. */.
1308c 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 if( pPg->flags&
1308d 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 PGHDR_NEED_SYNC
1308e 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 79 6e 63 ){. rc = sync
1308f 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b Journal(pPager);
13090 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
13091 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 ITE_OK && pPager
13092 2d 3e 66 75 6c 6c 53 79 6e 63 20 26 26 20 0a 20 ->fullSync && .
13093 20 20 20 20 20 21 28 70 50 61 67 65 72 2d 3e 6a !(pPager->j
13094 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 ournalMode==PAGE
13095 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 R_JOURNALMODE_ME
13096 4d 4f 52 59 29 20 26 26 0a 20 20 20 20 20 20 21 MORY) &&. !
13097 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 (sqlite3OsDevice
13098 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 Characteristics(
13099 70 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49 pPager->fd)&SQLI
1309a 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 TE_IOCAP_SAFE_AP
1309b 50 45 4e 44 29 0a 20 20 20 20 29 7b 0a 20 20 20 PEND). ){.
1309c 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 pPager->nRec
1309d 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
1309e 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 writeJournalHdr(
1309f 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pPager);. }.
130a0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 }.. /* If the
130a1 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
130a2 68 69 73 20 70 61 67 65 20 69 73 20 6c 61 72 67 his page is larg
130a3 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 er than the curr
130a4 65 6e 74 20 73 69 7a 65 20 6f 66 0a 20 20 2a 2a ent size of. **
130a5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d the database im
130a6 61 67 65 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 age, it may need
130a7 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74 to be written t
130a8 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 o the sub-journa
130a9 6c 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 l.. ** This is
130aa 62 65 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c because the call
130ab 20 74 6f 20 70 61 67 65 72 5f 77 72 69 74 65 5f to pager_write_
130ac 70 61 67 65 6c 69 73 74 28 29 20 62 65 6c 6f 77 pagelist() below
130ad 20 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 61 will not. ** a
130ae 63 74 75 61 6c 6c 79 20 77 72 69 74 65 20 64 61 ctually write da
130af 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65 20 69 ta to the file i
130b0 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 2a n this case.. *
130b1 2a 0a 20 20 2a 2a 20 43 6f 6e 73 69 64 65 72 20 *. ** Consider
130b2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 the following se
130b3 71 75 65 6e 63 65 20 6f 66 20 65 76 65 6e 74 73 quence of events
130b4 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 :. **. ** BE
130b5 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a GIN;. ** <j
130b6 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 3e 0a 20 ournal page X>.
130b7 20 2a 2a 20 20 20 20 20 3c 6d 6f 64 69 66 79 20 ** <modify
130b8 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20 page X>. **
130b9 20 53 41 56 45 50 4f 49 4e 54 20 73 70 3b 0a 20 SAVEPOINT sp;.
130ba 20 2a 2a 20 20 20 20 20 20 20 3c 73 68 72 69 6e ** <shrin
130bb 6b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 k database file
130bc 74 6f 20 59 20 70 61 67 65 73 3e 0a 20 20 2a 2a to Y pages>. **
130bd 20 20 20 20 20 20 20 70 61 67 65 72 53 74 72 65 pagerStre
130be 73 73 28 70 61 67 65 20 58 29 0a 20 20 2a 2a 20 ss(page X). **
130bf 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 ROLLBACK TO
130c0 73 70 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 sp;. **. ** If
130c1 20 28 58 3e 59 29 2c 20 74 68 65 6e 20 77 68 65 (X>Y), then whe
130c2 6e 20 70 61 67 65 72 53 74 72 65 73 73 20 69 73 n pagerStress is
130c3 20 63 61 6c 6c 65 64 20 70 61 67 65 20 58 20 77 called page X w
130c4 69 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 ill not be writt
130c5 65 6e 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 74 en. ** out to t
130c6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
130c7 2c 20 62 75 74 20 77 69 6c 6c 20 62 65 20 64 72 , but will be dr
130c8 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 opped from the c
130c9 61 63 68 65 2e 20 54 68 65 6e 2c 0a 20 20 2a 2a ache. Then,. **
130ca 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 following the "
130cb 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 ROLLBACK TO sp"
130cc 73 74 61 74 65 6d 65 6e 74 2c 20 72 65 61 64 69 statement, readi
130cd 6e 67 20 70 61 67 65 20 58 20 77 69 6c 6c 20 72 ng page X will r
130ce 65 61 64 0a 20 20 2a 2a 20 64 61 74 61 20 66 72 ead. ** data fr
130cf 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
130d0 66 69 6c 65 2e 20 54 68 69 73 20 77 69 6c 6c 20 file. This will
130d1 62 65 20 74 68 65 20 63 6f 70 79 20 6f 66 20 70 be the copy of p
130d2 61 67 65 20 58 20 61 73 20 69 74 0a 20 20 2a 2a age X as it. **
130d3 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 74 72 was when the tr
130d4 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 72 74 65 ansaction starte
130d5 64 2c 20 6e 6f 74 20 61 73 20 69 74 20 77 61 73 d, not as it was
130d6 20 77 68 65 6e 20 22 53 41 56 45 50 4f 49 4e 54 when "SAVEPOINT
130d7 20 73 70 22 0a 20 20 2a 2a 20 77 61 73 20 65 78 sp". ** was ex
130d8 65 63 75 74 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a ecuted.. **. *
130d9 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 * The solution i
130da 73 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63 s to write the c
130db 75 72 72 65 6e 74 20 64 61 74 61 20 66 6f 72 20 urrent data for
130dc 70 61 67 65 20 58 20 69 6e 74 6f 20 74 68 65 20 page X into the
130dd 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 . ** sub-journa
130de 6c 20 66 69 6c 65 20 6e 6f 77 20 28 69 66 20 69 l file now (if i
130df 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 t is not already
130e0 20 74 68 65 72 65 29 2c 20 73 6f 20 74 68 61 74 there), so that
130e1 20 69 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 it will. ** be
130e2 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 restored to its
130e3 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 77 current value w
130e4 68 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 hen the "ROLLBAC
130e5 4b 20 54 4f 20 73 70 22 20 69 73 20 0a 20 20 2a K TO sp" is . *
130e6 2a 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2f * executed.. */
130e7 0a 20 20 69 66 28 20 4e 45 56 45 52 28 0a 20 20 . if( NEVER(.
130e8 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f rc==SQLITE_O
130e9 4b 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 K && pPg->pgno>p
130ea 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 26 26 Pager->dbSize &&
130eb 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 subjRequiresPag
130ec 65 28 70 50 67 29 0a 20 20 29 20 29 7b 0a 20 20 e(pPg). ) ){.
130ed 20 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 rc = subjourna
130ee 6c 50 61 67 65 28 70 50 67 29 3b 0a 20 20 7d 0a lPage(pPg);. }.
130ef 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
130f0 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 contents of the
130f1 70 61 67 65 20 6f 75 74 20 74 6f 20 74 68 65 20 page out to the
130f2 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a database file. *
130f3 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 /. if( rc==SQLI
130f4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 67 TE_OK ){. pPg
130f5 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 ->pDirty = 0;.
130f6 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 rc = pager_wri
130f7 74 65 5f 70 61 67 65 6c 69 73 74 28 70 50 67 29 te_pagelist(pPg)
130f8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b ;. }.. /* Mark
130f9 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 the page as cle
130fa 61 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d an. */. if( rc=
130fb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
130fc 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53 PAGERTRACE(("S
130fd 54 52 45 53 53 20 25 64 20 70 61 67 65 20 25 64 TRESS %d page %d
130fe 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
130ff 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 ger), pPg->pgno)
13100 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 );. sqlite3Pc
13101 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 70 50 acheMakeClean(pP
13102 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 g);. }.. retur
13103 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 n pager_error(pP
13104 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 0a 2f ager, rc);.}.../
13105 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e *.** Allocate an
13106 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e d initialize a n
13107 65 77 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 ew Pager object
13108 61 6e 64 20 70 75 74 20 61 20 70 6f 69 6e 74 65 and put a pointe
13109 72 20 74 6f 20 69 74 0a 2a 2a 20 69 6e 20 2a 70 r to it.** in *p
1310a 70 50 61 67 65 72 2e 20 54 68 65 20 70 61 67 65 pPager. The page
1310b 72 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 r should eventua
1310c 6c 6c 79 20 62 65 20 66 72 65 65 64 20 62 79 20 lly be freed by
1310d 70 61 73 73 69 6e 67 20 69 74 0a 2a 2a 20 74 6f passing it.** to
1310e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f sqlite3PagerClo
1310f 73 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 se()..**.** The
13110 7a 46 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 zFilename argume
13111 6e 74 20 69 73 20 74 68 65 20 70 61 74 68 20 74 nt is the path t
13112 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
13113 69 6c 65 20 74 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 ile to open..**
13114 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 If zFilename is
13115 4e 55 4c 4c 20 74 68 65 6e 20 61 20 72 61 6e 64 NULL then a rand
13116 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74 65 6d 70 6f omly-named tempo
13117 72 61 72 79 20 66 69 6c 65 20 69 73 20 63 72 65 rary file is cre
13118 61 74 65 64 0a 2a 2a 20 61 6e 64 20 75 73 65 64 ated.** and used
13119 20 61 73 20 74 68 65 20 66 69 6c 65 20 74 6f 20 as the file to
1311a 62 65 20 63 61 63 68 65 64 2e 20 54 65 6d 70 6f be cached. Tempo
1311b 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 62 rary files are b
1311c 65 20 64 65 6c 65 74 65 64 0a 2a 2a 20 61 75 74 e deleted.** aut
1311d 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 omatically when
1311e 74 68 65 79 20 61 72 65 20 63 6c 6f 73 65 64 2e they are closed.
1311f 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 If zFilename is
13120 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e ":memory:" then
13121 20 0a 2a 2a 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 .** all informa
13122 74 69 6f 6e 20 69 73 20 68 65 6c 64 20 69 6e 20 tion is held in
13123 63 61 63 68 65 2e 20 49 74 20 69 73 20 6e 65 76 cache. It is nev
13124 65 72 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 er written to di
13125 73 6b 2e 20 0a 2a 2a 20 54 68 69 73 20 63 61 6e sk. .** This can
13126 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c be used to impl
13127 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f ement an in-memo
13128 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a ry database..**.
13129 2a 2a 20 54 68 65 20 6e 45 78 74 72 61 20 70 61 ** The nExtra pa
1312a 72 61 6d 65 74 65 72 20 73 70 65 63 69 66 69 65 rameter specifie
1312b 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
1312c 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 bytes of space a
1312d 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 61 6c 6f 6e llocated.** alon
1312e 67 20 77 69 74 68 20 65 61 63 68 20 70 61 67 65 g with each page
1312f 20 72 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73 reference. This
13130 20 73 70 61 63 65 20 69 73 20 61 76 61 69 6c 61 space is availa
13131 62 6c 65 20 74 6f 20 74 68 65 20 75 73 65 72 0a ble to the user.
13132 2a 2a 20 76 69 61 20 74 68 65 20 73 71 6c 69 74 ** via the sqlit
13133 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28 e3PagerGetExtra(
13134 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ) API..**.** The
13135 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 flags argument
13136 69 73 20 75 73 65 64 20 74 6f 20 73 70 65 63 69 is used to speci
13137 66 79 20 70 72 6f 70 65 72 74 69 65 73 20 74 68 fy properties th
13138 61 74 20 61 66 66 65 63 74 20 74 68 65 0a 2a 2a at affect the.**
13139 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 operation of th
1313a 65 20 70 61 67 65 72 2e 20 49 74 20 73 68 6f 75 e pager. It shou
1313b 6c 64 20 62 65 20 70 61 73 73 65 64 20 73 6f 6d ld be passed som
1313c 65 20 62 69 74 77 69 73 65 20 63 6f 6d 62 69 6e e bitwise combin
1313d 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 ation.** of the
1313e 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e PAGER_OMIT_JOURN
1313f 41 4c 20 61 6e 64 20 50 41 47 45 52 5f 4e 4f 5f AL and PAGER_NO_
13140 52 45 41 44 4c 4f 43 4b 20 66 6c 61 67 73 2e 0a READLOCK flags..
13141 2a 2a 0a 2a 2a 20 54 68 65 20 76 66 73 46 6c 61 **.** The vfsFla
13142 67 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 gs parameter is
13143 61 20 62 69 74 6d 61 73 6b 20 74 6f 20 70 61 73 a bitmask to pas
13144 73 20 74 6f 20 74 68 65 20 66 6c 61 67 73 20 70 s to the flags p
13145 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 66 20 74 arameter.** of t
13146 68 65 20 78 4f 70 65 6e 28 29 20 6d 65 74 68 6f he xOpen() metho
13147 64 20 6f 66 20 74 68 65 20 73 75 70 70 6c 69 65 d of the supplie
13148 64 20 56 46 53 20 77 68 65 6e 20 6f 70 65 6e 69 d VFS when openi
13149 6e 67 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a ng files. .**.**
1314a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 If the pager ob
1314b 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 ject is allocate
1314c 64 20 61 6e 64 20 74 68 65 20 73 70 65 63 69 66 d and the specif
1314d 69 65 64 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 ied file opened
1314e 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79 .** successfully
1314f 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 , SQLITE_OK is r
13150 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 eturned and *ppP
13151 61 67 65 72 20 73 65 74 20 74 6f 20 70 6f 69 6e ager set to poin
13152 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 t to.** the new
13153 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49 66 pager object. If
13154 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
13155 2c 20 2a 70 70 50 61 67 65 72 20 69 73 20 73 65 , *ppPager is se
13156 74 20 74 6f 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 t to NULL.** and
13157 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 error code retu
13158 72 6e 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74 rned. This funct
13159 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 ion may return S
1315a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 QLITE_NOMEM.** (
1315b 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 sqlite3Malloc()
1315c 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 is used to alloc
1315d 61 74 65 20 6d 65 6d 6f 72 79 29 2c 20 53 51 4c ate memory), SQL
1315e 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 6f 72 20 ITE_CANTOPEN or
1315f 0a 2a 2a 20 76 61 72 69 6f 75 73 20 53 51 4c 49 .** various SQLI
13160 54 45 5f 49 4f 5f 58 58 58 20 65 72 72 6f 72 73 TE_IO_XXX errors
13161 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
13162 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
13163 61 67 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 agerOpen(. sqli
13164 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 te3_vfs *pVfs,
13165 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 /* The virt
13166 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 20 ual file system
13167 74 6f 20 75 73 65 20 2a 2f 0a 20 20 50 61 67 65 to use */. Page
13168 72 20 2a 2a 70 70 50 61 67 65 72 2c 20 20 20 20 r **ppPager,
13169 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 /* OUT: Ret
1316a 75 72 6e 20 74 68 65 20 50 61 67 65 72 20 73 74 urn the Pager st
1316b 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a ructure here */.
1316c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 const char *zF
1316d 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 ilename, /* Na
1316e 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 me of the databa
1316f 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 se file to open
13170 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c */. int nExtra,
13171 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
13172 20 45 78 74 72 61 20 62 79 74 65 73 20 61 70 70 Extra bytes app
13173 65 6e 64 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d end to each in-m
13174 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 emory page */.
13175 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 int flags,
13176 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 /* flag
13177 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 s controlling th
13178 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 is file */. int
13179 20 76 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20 vfsFlags,
1317a 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 /* flags p
1317b 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f assed through to
1317c 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 sqlite3_vfs.xOp
1317d 65 6e 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 en() */. void (
1317e 2a 78 52 65 69 6e 69 74 29 28 44 62 50 61 67 65 *xReinit)(DbPage
1317f 2a 29 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 *) /* Function t
13180 6f 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 o reinitialize p
13181 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 ages */.){. u8
13182 2a 70 50 74 72 3b 0a 20 20 50 61 67 65 72 20 2a *pPtr;. Pager *
13183 70 50 61 67 65 72 20 3d 20 30 3b 20 20 20 20 20 pPager = 0;
13184 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 /* Pager objec
13185 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e t to allocate an
13186 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e d return */. in
13187 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
13188 3b 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e ; /* Return
13189 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 74 code */. int t
1318a 65 6d 70 46 69 6c 65 20 3d 20 30 3b 20 20 20 20 empFile = 0;
1318b 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 /* True for
1318c 74 65 6d 70 20 66 69 6c 65 73 20 28 69 6e 63 6c temp files (incl
1318d 2e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 . in-memory file
1318e 73 29 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 44 s) */. int memD
1318f 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 b = 0;
13190 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
13191 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 is an in-memory
13192 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 file */. int r
13193 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 20 20 20 20 eadOnly = 0;
13194 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
13195 68 69 73 20 69 73 20 61 20 72 65 61 64 2d 6f 6e his is a read-on
13196 6c 79 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 ly file */. int
13197 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
13198 3b 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 ; /* Bytes t
13199 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 65 o allocate for e
1319a 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 64 20 2a ach journal fd *
1319b 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 6e /. char *zPathn
1319c 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 ame = 0; /*
1319d 46 75 6c 6c 20 70 61 74 68 20 74 6f 20 64 61 74 Full path to dat
1319e 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
1319f 69 6e 74 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 int nPathname =
131a0 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 0; /* Numb
131a1 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a er of bytes in z
131a2 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e Pathname */. in
131a3 74 20 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 t useJournal = (
131a4 66 6c 61 67 73 20 26 20 50 41 47 45 52 5f 4f 4d flags & PAGER_OM
131a5 49 54 5f 4a 4f 55 52 4e 41 4c 29 3d 3d 30 3b 20 IT_JOURNAL)==0;
131a6 2f 2a 20 46 61 6c 73 65 20 74 6f 20 6f 6d 69 74 /* False to omit
131a7 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 6e journal */. in
131a8 74 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 t noReadlock = (
131a9 66 6c 61 67 73 20 26 20 50 41 47 45 52 5f 4e 4f flags & PAGER_NO
131aa 5f 52 45 41 44 4c 4f 43 4b 29 21 3d 30 3b 20 20 _READLOCK)!=0;
131ab 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 /* True to omit
131ac 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 69 read-lock */. i
131ad 6e 74 20 70 63 61 63 68 65 53 69 7a 65 20 3d 20 nt pcacheSize =
131ae 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a sqlite3PcacheSiz
131af 65 28 29 3b 20 20 20 20 20 20 20 2f 2a 20 42 79 e(); /* By
131b0 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 tes to allocate
131b1 66 6f 72 20 50 43 61 63 68 65 20 2a 2f 0a 20 20 for PCache */.
131b2 75 31 36 20 73 7a 50 61 67 65 44 66 6c 74 20 3d u16 szPageDflt =
131b3 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
131b4 50 41 47 45 5f 53 49 5a 45 3b 20 20 2f 2a 20 44 PAGE_SIZE; /* D
131b5 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 efault page size
131b6 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 */.. /* Figure
131b7 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70 out how much sp
131b8 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20 ace is required
131b9 66 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c for each journal
131ba 20 66 69 6c 65 2d 68 61 6e 64 6c 65 0a 20 20 2a file-handle. *
131bb 2a 20 28 74 68 65 72 65 20 61 72 65 20 74 77 6f * (there are two
131bc 20 6f 66 20 74 68 65 6d 2c 20 74 68 65 20 6d 61 of them, the ma
131bd 69 6e 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 in journal and t
131be 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 29 2e he sub-journal).
131bf 20 54 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 This. ** is th
131c0 65 20 6d 61 78 69 6d 75 6d 20 73 70 61 63 65 20 e maximum space
131c1 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 required for an
131c2 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 in-memory journa
131c3 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 0a 20 l file handle .
131c4 20 2a 2a 20 61 6e 64 20 61 20 72 65 67 75 6c 61 ** and a regula
131c5 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68 r journal file-h
131c6 61 6e 64 6c 65 2e 20 4e 6f 74 65 20 74 68 61 74 andle. Note that
131c7 20 61 20 22 72 65 67 75 6c 61 72 20 6a 6f 75 72 a "regular jour
131c8 6e 61 6c 2d 68 61 6e 64 6c 65 22 0a 20 20 2a 2a nal-handle". **
131c9 20 6d 61 79 20 62 65 20 61 20 77 72 61 70 70 65 may be a wrappe
131ca 72 20 63 61 70 61 62 6c 65 20 6f 66 20 63 61 63 r capable of cac
131cb 68 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 hing the first p
131cc 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6a 6f ortion of the jo
131cd 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 urnal. ** file
131ce 69 6e 20 6d 65 6d 6f 72 79 20 74 6f 20 69 6d 70 in memory to imp
131cf 6c 65 6d 65 6e 74 20 74 68 65 20 61 74 6f 6d 69 lement the atomi
131d0 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
131d1 74 69 6f 6e 20 28 73 65 65 20 0a 20 20 2a 2a 20 tion (see . **
131d2 73 6f 75 72 63 65 20 66 69 6c 65 20 6a 6f 75 72 source file jour
131d3 6e 61 6c 2e 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 nal.c).. */. i
131d4 66 28 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 f( sqlite3Journa
131d5 6c 53 69 7a 65 28 70 56 66 73 29 3e 73 71 6c 69 lSize(pVfs)>sqli
131d6 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a te3MemJournalSiz
131d7 65 28 29 20 29 7b 0a 20 20 20 20 6a 6f 75 72 6e e() ){. journ
131d8 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f 55 alFileSize = ROU
131d9 4e 44 38 28 73 71 6c 69 74 65 33 4a 6f 75 72 6e ND8(sqlite3Journ
131da 61 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 alSize(pVfs));.
131db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6a 6f 75 72 }else{. jour
131dc 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d 20 52 4f nalFileSize = RO
131dd 55 4e 44 38 28 73 71 6c 69 74 65 33 4d 65 6d 4a UND8(sqlite3MemJ
131de 6f 75 72 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 ournalSize());.
131df 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 }.. /* Set the
131e0 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 output variable
131e1 20 74 6f 20 4e 55 4c 4c 20 69 6e 20 63 61 73 65 to NULL in case
131e2 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
131e3 2e 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 . */. *ppPager
131e4 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 = 0;.. /* Compu
131e5 74 65 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 te and store the
131e6 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69 full pathname i
131e7 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 n an allocated b
131e8 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20 uffer pointed.
131e9 2a 2a 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61 ** to by zPathna
131ea 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68 me, length nPath
131eb 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69 name. Or, if thi
131ec 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79 s is a temporary
131ed 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76 file,. ** leav
131ee 65 20 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65 e both nPathname
131ef 20 61 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73 and zPathname s
131f0 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20 et to 0.. */.
131f1 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 if( zFilename &&
131f2 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b zFilename[0] ){
131f3 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d . nPathname =
131f4 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d pVfs->mxPathnam
131f5 65 2b 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61 e+1;. zPathna
131f6 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c me = sqlite3Mall
131f7 6f 63 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b oc(nPathname*2);
131f8 0a 20 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61 . if( zPathna
131f9 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 me==0 ){. r
131fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
131fb 45 4d 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 EM;. }.#ifnde
131fc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 f SQLITE_OMIT_ME
131fd 4d 4f 52 59 44 42 0a 20 20 20 20 69 66 28 20 73 MORYDB. if( s
131fe 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c trcmp(zFilename,
131ff 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29 ":memory:")==0 )
13200 7b 0a 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20 {. memDb =
13201 31 3b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 1;. zPathna
13202 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d me[0] = 0;. }
13203 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 else.#endif.
13204 7b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61 6d {. zPathnam
13205 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a 20 4d 61 6b e[0] = 0; /* Mak
13206 65 20 73 75 72 65 20 69 6e 69 74 69 61 6c 69 7a e sure initializ
13207 65 64 20 65 76 65 6e 20 69 66 20 46 75 6c 6c 50 ed even if FullP
13208 61 74 68 6e 61 6d 65 28 29 20 66 61 69 6c 73 20 athname() fails
13209 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 */. rc = sq
1320a 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e lite3OsFullPathn
1320b 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e ame(pVfs, zFilen
1320c 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 ame, nPathname,
1320d 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 zPathname);.
1320e 7d 0a 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 }.. nPathname
1320f 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
13210 33 30 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 30(zPathname);.
13211 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13212 45 5f 4f 4b 20 26 26 20 6e 50 61 74 68 6e 61 6d E_OK && nPathnam
13213 65 2b 38 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 e+8>pVfs->mxPath
13214 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a name ){. /*
13215 20 54 68 69 73 20 62 72 61 6e 63 68 20 69 73 20 This branch is
13216 74 61 6b 65 6e 20 77 68 65 6e 20 74 68 65 20 6a taken when the j
13217 6f 75 72 6e 61 6c 20 70 61 74 68 20 72 65 71 75 ournal path requ
13218 69 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a ired by. **
13219 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 the database be
1321a 69 6e 67 20 6f 70 65 6e 65 64 20 77 69 6c 6c 20 ing opened will
1321b 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 70 56 66 be more than pVf
1321c 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 20 20 s->mxPathname.
1321d 20 20 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 ** bytes in
1321e 6c 65 6e 67 74 68 2e 20 54 68 69 73 20 6d 65 61 length. This mea
1321f 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
13220 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 cannot be opened
13221 2c 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 69 74 ,. ** as it
13222 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 70 6f 73 will not be pos
13223 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 sible to open th
13224 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f e journal file o
13225 72 20 65 76 65 6e 0a 20 20 20 20 20 20 2a 2a 20 r even. **
13226 63 68 65 63 6b 20 66 6f 72 20 61 20 68 6f 74 2d check for a hot-
13227 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 72 journal before r
13228 65 61 64 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f eading.. */
13229 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1322a 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 TE_CANTOPEN;.
1322b 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 }. if( rc!=S
1322c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1322d 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
1322e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 Pathname);.
1322f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
13230 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f }. }.. /* Allo
13231 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 cate memory for
13232 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 the Pager struct
13233 75 72 65 2c 20 50 43 61 63 68 65 20 6f 62 6a 65 ure, PCache obje
13234 63 74 2c 20 74 68 65 0a 20 20 2a 2a 20 74 68 72 ct, the. ** thr
13235 65 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 ee file descript
13236 6f 72 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 ors, the databas
13237 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 6e 64 20 e file name and
13238 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a the journal . *
13239 2a 20 66 69 6c 65 20 6e 61 6d 65 2e 20 54 68 65 * file name. The
1323a 20 6c 61 79 6f 75 74 20 69 6e 20 6d 65 6d 6f 72 layout in memor
1323b 79 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a y is as follows:
1323c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 50 . **. ** P
1323d 61 67 65 72 20 6f 62 6a 65 63 74 20 20 20 20 20 ager object
1323e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1323f 73 69 7a 65 6f 66 28 50 61 67 65 72 29 20 62 79 sizeof(Pager) by
13240 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 50 43 tes). ** PC
13241 61 63 68 65 20 6f 62 6a 65 63 74 20 20 20 20 20 ache object
13242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 73 (s
13243 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 qlite3PcacheSize
13244 28 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 () bytes). **
13245 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 Database file
13246 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 handle
13247 20 20 20 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 (pVfs->szOsFi
13248 6c 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 le bytes). **
13249 20 20 20 53 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 Sub-journal f
1324a 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20 20 ile handle
1324b 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 (journalFileS
1324c 69 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 ize bytes). **
1324d 20 20 20 20 4d 61 69 6e 20 6a 6f 75 72 6e 61 6c Main journal
1324e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 file handle
1324f 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 (journalFile
13250 53 69 7a 65 20 62 79 74 65 73 29 0a 20 20 2a 2a Size bytes). **
13251 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66 69 Database fi
13252 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20 le name
13253 20 20 20 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b (nPathname+
13254 31 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 1 bytes). **
13255 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e Journal file n
13256 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 ame
13257 20 20 28 6e 50 61 74 68 6e 61 6d 65 2b 38 2b 31 (nPathname+8+1
13258 20 62 79 74 65 73 29 0a 20 20 2a 2f 0a 20 20 70 bytes). */. p
13259 50 74 72 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 Ptr = (u8 *)sqli
1325a 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 0a 20 te3MallocZero(.
1325b 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 ROUND8(sizeof
1325c 28 2a 70 50 61 67 65 72 29 29 20 2b 20 20 20 20 (*pPager)) +
1325d 20 20 2f 2a 20 50 61 67 65 72 20 73 74 72 75 63 /* Pager struc
1325e 74 75 72 65 20 2a 2f 0a 20 20 20 20 52 4f 55 4e ture */. ROUN
1325f 44 38 28 70 63 61 63 68 65 53 69 7a 65 29 20 2b D8(pcacheSize) +
13260 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 43 /* PC
13261 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 ache object */.
13262 20 20 20 52 4f 55 4e 44 38 28 70 56 66 73 2d 3e ROUND8(pVfs->
13263 73 7a 4f 73 46 69 6c 65 29 20 2b 20 20 20 20 20 szOsFile) +
13264 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62 /* The main db
13265 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75 file */. jou
13266 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32 rnalFileSize * 2
13267 20 2b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 + /* T
13268 68 65 20 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 he two journal f
13269 69 6c 65 73 20 2a 2f 20 0a 20 20 20 20 6e 50 61 iles */ . nPa
1326a 74 68 6e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 thname + 1 +
1326b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a /* z
1326c 46 69 6c 65 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 Filename */.
1326d 6e 50 61 74 68 6e 61 6d 65 20 2b 20 38 20 2b 20 nPathname + 8 +
1326e 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 1 /
1326f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 * zJournal */.
13270 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
13271 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
13272 54 28 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f T(SQLITE_INT_TO_
13273 50 54 52 28 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 PTR(journalFileS
13274 69 7a 65 29 29 20 29 3b 0a 20 20 69 66 28 20 21 ize)) );. if( !
13275 70 50 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 pPtr ){. sqli
13276 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 te3_free(zPathna
13277 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 me);. return
13278 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
13279 7d 0a 20 20 70 50 61 67 65 72 20 3d 20 20 20 20 }. pPager =
1327a 20 20 20 20 20 20 20 20 20 20 28 50 61 67 65 72 (Pager
1327b 2a 29 28 70 50 74 72 29 3b 0a 20 20 70 50 61 67 *)(pPtr);. pPag
1327c 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20 20 20 er->pPCache =
1327d 20 28 50 43 61 63 68 65 2a 29 28 70 50 74 72 20 (PCache*)(pPtr
1327e 2b 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 += ROUND8(sizeof
1327f 28 2a 70 50 61 67 65 72 29 29 29 3b 0a 20 20 70 (*pPager)));. p
13280 50 61 67 65 72 2d 3e 66 64 20 3d 20 20 20 28 73 Pager->fd = (s
13281 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 qlite3_file*)(pP
13282 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70 63 61 tr += ROUND8(pca
13283 63 68 65 53 69 7a 65 29 29 3b 0a 20 20 70 50 61 cheSize));. pPa
13284 67 65 72 2d 3e 73 6a 66 64 20 3d 20 28 73 71 6c ger->sjfd = (sql
13285 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 ite3_file*)(pPtr
13286 20 2b 3d 20 52 4f 55 4e 44 38 28 70 56 66 73 2d += ROUND8(pVfs-
13287 3e 73 7a 4f 73 46 69 6c 65 29 29 3b 0a 20 20 70 >szOsFile));. p
13288 50 61 67 65 72 2d 3e 6a 66 64 20 3d 20 20 28 73 Pager->jfd = (s
13289 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28 70 50 qlite3_file*)(pP
1328a 74 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c tr += journalFil
1328b 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 72 eSize);. pPager
1328c 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 20 20 ->zFilename =
1328d 20 28 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d (char*)(pPtr +=
1328e 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 journalFileSize
1328f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
13290 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
13291 54 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 T(pPager->jfd) )
13292 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 ;.. /* Fill in
13293 74 68 65 20 50 61 67 65 72 2e 7a 46 69 6c 65 6e the Pager.zFilen
13294 61 6d 65 20 61 6e 64 20 50 61 67 65 72 2e 7a 4a ame and Pager.zJ
13295 6f 75 72 6e 61 6c 20 62 75 66 66 65 72 73 2c 20 ournal buffers,
13296 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a if required. */.
13297 20 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 if( zPathname
13298 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 7a ){. pPager->z
13299 4a 6f 75 72 6e 61 6c 20 3d 20 20 20 28 63 68 61 Journal = (cha
1329a 72 2a 29 28 70 50 74 72 20 2b 3d 20 6e 50 61 74 r*)(pPtr += nPat
1329b 68 6e 61 6d 65 20 2b 20 31 29 3b 0a 20 20 20 20 hname + 1);.
1329c 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a memcpy(pPager->z
1329d 46 69 6c 65 6e 61 6d 65 2c 20 7a 50 61 74 68 6e Filename, zPathn
1329e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b ame, nPathname);
1329f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 . memcpy(pPag
132a0 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 7a 50 er->zJournal, zP
132a1 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61 athname, nPathna
132a2 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 me);. memcpy(
132a3 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 &pPager->zJourna
132a4 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c 20 22 2d l[nPathname], "-
132a5 6a 6f 75 72 6e 61 6c 22 2c 20 38 29 3b 0a 20 20 journal", 8);.
132a6 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 7a 46 if( pPager->zF
132a7 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 20 ilename[0]==0 )
132a8 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
132a9 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c [0] = 0;. sql
132aa 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e ite3_free(zPathn
132ab 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 ame);. }. pPag
132ac 65 72 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b er->pVfs = pVfs;
132ad 0a 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c . pPager->vfsFl
132ae 61 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a ags = vfsFlags;.
132af 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 70 . /* Open the p
132b0 61 67 65 72 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a ager file.. */.
132b1 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 if( zFilename
132b2 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 && zFilename[0]
132b3 26 26 20 21 6d 65 6d 44 62 20 29 7b 0a 20 20 20 && !memDb ){.
132b4 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b 20 20 int fout = 0;
132b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
132b6 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 72 /* VFS flags r
132b7 65 74 75 72 6e 65 64 20 62 79 20 78 4f 70 65 6e eturned by xOpen
132b8 28 29 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 () */. rc = s
132b9 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 qlite3OsOpen(pVf
132ba 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 s, pPager->zFile
132bb 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64 name, pPager->fd
132bc 2c 20 76 66 73 46 6c 61 67 73 2c 20 26 66 6f 75 , vfsFlags, &fou
132bd 74 29 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 t);. readOnly
132be 20 3d 20 28 66 6f 75 74 26 53 51 4c 49 54 45 5f = (fout&SQLITE_
132bf 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a OPEN_READONLY);.
132c0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 . /* If the f
132c1 69 6c 65 20 77 61 73 20 73 75 63 63 65 73 73 66 ile was successf
132c2 75 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20 ully opened for
132c3 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 read/write acces
132c4 73 2c 0a 20 20 20 20 2a 2a 20 63 68 6f 6f 73 65 s,. ** choose
132c5 20 61 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 a default page
132c6 73 69 7a 65 20 69 6e 20 63 61 73 65 20 77 65 20 size in case we
132c7 68 61 76 65 20 74 6f 20 63 72 65 61 74 65 20 74 have to create t
132c8 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 he. ** databa
132c9 73 65 20 66 69 6c 65 2e 20 54 68 65 20 64 65 66 se file. The def
132ca 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 ault page size i
132cb 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 s the maximum of
132cc 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
132cd 20 20 20 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 + SQLITE_DEFA
132ce 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 ULT_PAGE_SIZE,.
132cf 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 ** + The v
132d0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 alue returned by
132d1 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 sqlite3OsSector
132d2 53 69 7a 65 28 29 0a 20 20 20 20 2a 2a 20 20 20 Size(). **
132d3 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20 70 + The largest p
132d4 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63 61 age size that ca
132d5 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f n be written ato
132d6 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 2a 2f 0a mically.. */.
132d7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
132d8 54 45 5f 4f 4b 20 26 26 20 21 72 65 61 64 4f 6e TE_OK && !readOn
132d9 6c 79 20 29 7b 0a 20 20 20 20 20 20 73 65 74 53 ly ){. setS
132da 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 ectorSize(pPager
132db 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
132dc 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 SQLITE_DEFAULT_P
132dd 41 47 45 5f 53 49 5a 45 3c 3d 53 51 4c 49 54 45 AGE_SIZE<=SQLITE
132de 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
132df 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20 20 69 E_SIZE);. i
132e0 66 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 70 50 f( szPageDflt<pP
132e1 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 ager->sectorSize
132e2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
132e3 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 pPager->sectorSi
132e4 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 ze>SQLITE_MAX_DE
132e5 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 20 FAULT_PAGE_SIZE
132e6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 ){. szP
132e7 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45 ageDflt = SQLITE
132e8 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 _MAX_DEFAULT_PAG
132e9 45 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 20 20 E_SIZE;.
132ea 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
132eb 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 28 75 szPageDflt = (u
132ec 31 36 29 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 16)pPager->secto
132ed 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 7d rSize;. }
132ee 0a 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66 20 . }.#ifdef
132ef 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 SQLITE_ENABLE_AT
132f0 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 OMIC_WRITE.
132f1 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 {. int i
132f2 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 Dc = sqlite3OsDe
132f3 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 viceCharacterist
132f4 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b ics(pPager->fd);
132f5 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b . int ii;
132f6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
132f7 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f SQLITE_IOCAP_ATO
132f8 4d 49 43 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 MIC512==(512>>8)
132f9 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
132fa 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 t(SQLITE_IOCAP_A
132fb 54 4f 4d 49 43 36 34 4b 3d 3d 28 36 35 35 33 36 TOMIC64K==(65536
132fc 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61 >>8));. a
132fd 73 73 65 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 ssert(SQLITE_MAX
132fe 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 _DEFAULT_PAGE_SI
132ff 5a 45 3c 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 ZE<=65536);.
13300 20 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 for(ii=szPag
13301 65 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 eDflt; ii<=SQLIT
13302 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 E_MAX_DEFAULT_PA
13303 47 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 GE_SIZE; ii=ii*2
13304 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
13305 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f 43 iDc&(SQLITE_IOC
13306 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e 38 AP_ATOMIC|(ii>>8
13307 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 )) ){.
13308 20 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 69 szPageDflt = i
13309 69 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 i;. }.
1330a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
1330b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 .#endif. }.
1330c 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 }else{. /* If
1330d 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c a temporary fil
1330e 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 e is requested,
1330f 69 74 20 69 73 20 6e 6f 74 20 6f 70 65 6e 65 64 it is not opened
13310 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 immediately..
13311 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 ** In this cas
13312 65 20 77 65 20 61 63 63 65 70 74 20 74 68 65 20 e we accept the
13313 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a default page siz
13314 65 20 61 6e 64 20 64 65 6c 61 79 20 61 63 74 75 e and delay actu
13315 61 6c 6c 79 0a 20 20 20 20 2a 2a 20 6f 70 65 6e ally. ** open
13316 69 6e 67 20 74 68 65 20 66 69 6c 65 20 75 6e 74 ing the file unt
13317 69 6c 20 74 68 65 20 66 69 72 73 74 20 63 61 6c il the first cal
13318 6c 20 74 6f 20 4f 73 57 72 69 74 65 28 29 2e 0a l to OsWrite()..
13319 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
1331a 69 73 20 62 72 61 6e 63 68 20 69 73 20 61 6c 73 is branch is als
1331b 6f 20 72 75 6e 20 66 6f 72 20 61 6e 20 69 6e 2d o run for an in-
1331c 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e memory database.
1331d 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 An in-memory.
1331e 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 ** database is
1331f 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 74 the same as a t
13320 65 6d 70 2d 66 69 6c 65 20 74 68 61 74 20 69 73 emp-file that is
13321 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 6f never written o
13322 75 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69 73 ut to. ** dis
13323 6b 20 61 6e 64 20 75 73 65 73 20 61 6e 20 69 6e k and uses an in
13324 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b -memory rollback
13325 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f journal.. */
13326 20 0a 20 20 20 20 74 65 6d 70 46 69 6c 65 20 3d . tempFile =
13327 20 31 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 1;. pPager->
13328 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 state = PAGER_EX
13329 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 72 65 61 CLUSIVE;. rea
1332a 64 4f 6e 6c 79 20 3d 20 28 76 66 73 46 6c 61 67 dOnly = (vfsFlag
1332b 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 s&SQLITE_OPEN_RE
1332c 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d 0a 0a 20 20 ADONLY);. }..
1332d 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 /* The following
1332e 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72 53 65 call to PagerSe
1332f 74 50 61 67 65 73 69 7a 65 28 29 20 73 65 72 76 tPagesize() serv
13330 65 73 20 74 6f 20 73 65 74 20 74 68 65 20 76 61 es to set the va
13331 6c 75 65 20 6f 66 20 0a 20 20 2a 2a 20 50 61 67 lue of . ** Pag
13332 65 72 2e 70 61 67 65 53 69 7a 65 20 61 6e 64 20 er.pageSize and
13333 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 to allocate the
13334 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65 20 Pager.pTmpSpace
13335 62 75 66 66 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 buffer.. */. i
13336 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13337 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
13338 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 pPager->memDb==0
13339 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
1333a 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 ite3PagerSetPage
1333b 73 69 7a 65 28 70 50 61 67 65 72 2c 20 26 73 7a size(pPager, &sz
1333c 50 61 67 65 44 66 6c 74 2c 20 2d 31 29 3b 0a 20 PageDflt, -1);.
1333d 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21 testcase( rc!
1333e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
1333f 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 }.. /* If an er
13340 72 6f 72 20 6f 63 63 75 72 72 65 64 20 69 6e 20 ror occurred in
13341 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62 6c either of the bl
13342 6f 63 6b 73 20 61 62 6f 76 65 2c 20 66 72 65 65 ocks above, free
13343 20 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 the . ** Pager
13344 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63 structure and c
13345 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20 lose the file..
13346 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 */. if( rc!=SQ
13347 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 LITE_OK ){. a
13348 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e ssert( !pPager->
13349 70 54 6d 70 53 70 61 63 65 20 29 3b 0a 20 20 20 pTmpSpace );.
1334a 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
1334b 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 pPager->fd);.
1334c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 sqlite3_free(pP
1334d 61 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 ager);. retur
1334e 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 n rc;. }.. /*
1334f 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 50 Initialize the P
13350 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 2a 2f Cache object. */
13351 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 . assert( nExtr
13352 61 3c 31 30 30 30 20 29 3b 0a 20 20 6e 45 78 74 a<1000 );. nExt
13353 72 61 20 3d 20 52 4f 55 4e 44 38 28 6e 45 78 74 ra = ROUND8(nExt
13354 72 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 ra);. sqlite3Pc
13355 61 63 68 65 4f 70 65 6e 28 73 7a 50 61 67 65 44 acheOpen(szPageD
13356 66 6c 74 2c 20 6e 45 78 74 72 61 2c 20 21 6d 65 flt, nExtra, !me
13357 6d 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 mDb,.
13358 20 20 20 20 20 20 20 20 20 21 6d 65 6d 44 62 3f !memDb?
13359 70 61 67 65 72 53 74 72 65 73 73 3a 30 2c 20 28 pagerStress:0, (
1335a 76 6f 69 64 20 2a 29 70 50 61 67 65 72 2c 20 70 void *)pPager, p
1335b 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b Pager->pPCache);
1335c 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 .. PAGERTRACE((
1335d 22 4f 50 45 4e 20 25 64 20 25 73 5c 6e 22 2c 20 "OPEN %d %s\n",
1335e 46 49 4c 45 48 41 4e 44 4c 45 49 44 28 70 50 61 FILEHANDLEID(pPa
1335f 67 65 72 2d 3e 66 64 29 2c 20 70 50 61 67 65 72 ger->fd), pPager
13360 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 3b 0a 20 ->zFilename));.
13361 20 49 4f 54 52 41 43 45 28 28 22 4f 50 45 4e 20 IOTRACE(("OPEN
13362 25 70 20 25 73 5c 6e 22 2c 20 70 50 61 67 65 72 %p %s\n", pPager
13363 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e , pPager->zFilen
13364 61 6d 65 29 29 0a 0a 20 20 70 50 61 67 65 72 2d ame)).. pPager-
13365 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 >useJournal = (u
13366 38 29 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 8)useJournal;.
13367 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f pPager->noReadlo
13368 63 6b 20 3d 20 28 6e 6f 52 65 61 64 6c 6f 63 6b ck = (noReadlock
13369 20 26 26 20 72 65 61 64 4f 6e 6c 79 29 20 3f 31 && readOnly) ?1
1336a 3a 30 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d :0;. /* pPager-
1336b 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 20 2a >stmtOpen = 0; *
1336c 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 /. /* pPager->s
1336d 74 6d 74 49 6e 55 73 65 20 3d 20 30 3b 20 2a 2f tmtInUse = 0; */
1336e 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 52 . /* pPager->nR
1336f 65 66 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 ef = 0; */. pPa
13370 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 ger->dbSizeValid
13371 20 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 = (u8)memDb;.
13372 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 /* pPager->stmtS
13373 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a ize = 0; */. /*
13374 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 pPager->stmtJSi
13375 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 ze = 0; */. /*
13376 70 50 61 67 65 72 2d 3e 6e 50 61 67 65 20 3d 20 pPager->nPage =
13377 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 0; */. pPager->
13378 6d 78 50 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f mxPgno = SQLITE_
13379 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a MAX_PAGE_COUNT;.
1337a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 /* pPager->sta
1337b 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 te = PAGER_UNLOC
1337c 4b 3b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 K; */. assert(
1337d 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d pPager->state ==
1337e 20 28 74 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 (tempFile ? PAG
1337f 45 52 5f 45 58 43 4c 55 53 49 56 45 20 3a 20 50 ER_EXCLUSIVE : P
13380 41 47 45 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a AGER_UNLOCK) );.
13381 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 /* pPager->err
13382 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 Mask = 0; */. p
13383 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 Pager->tempFile
13384 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b 0a = (u8)tempFile;.
13385 20 20 61 73 73 65 72 74 28 20 74 65 6d 70 46 69 assert( tempFi
13386 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e le==PAGER_LOCKIN
13387 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 0a 20 20 GMODE_NORMAL .
13388 20 20 20 20 20 20 20 20 7c 7c 20 74 65 6d 70 46 || tempF
13389 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ile==PAGER_LOCKI
1338a 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
1338b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 );. assert( PA
1338c 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
1338d 45 58 43 4c 55 53 49 56 45 3d 3d 31 20 29 3b 0a EXCLUSIVE==1 );.
1338e 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 pPager->exclus
1338f 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 74 65 iveMode = (u8)te
13390 6d 70 46 69 6c 65 3b 20 0a 20 20 70 50 61 67 65 mpFile; . pPage
13391 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f r->changeCountDo
13392 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d ne = pPager->tem
13393 70 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d pFile;. pPager-
13394 3e 6d 65 6d 44 62 20 3d 20 28 75 38 29 6d 65 6d >memDb = (u8)mem
13395 44 62 3b 0a 20 20 70 50 61 67 65 72 2d 3e 72 65 Db;. pPager->re
13396 61 64 4f 6e 6c 79 20 3d 20 28 75 38 29 72 65 61 adOnly = (u8)rea
13397 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 dOnly;. /* pPag
13398 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 er->needSync = 0
13399 3b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 ; */. assert( u
1339a 73 65 4a 6f 75 72 6e 61 6c 20 7c 7c 20 70 50 61 seJournal || pPa
1339b 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b ger->tempFile );
1339c 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e . pPager->noSyn
1339d 63 20 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 c = pPager->temp
1339e 46 69 6c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e File;. pPager->
1339f 66 75 6c 6c 53 79 6e 63 20 3d 20 70 50 61 67 65 fullSync = pPage
133a0 72 2d 3e 6e 6f 53 79 6e 63 20 3f 30 3a 31 3b 0a r->noSync ?0:1;.
133a1 20 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 pPager->sync_f
133a2 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 53 59 lags = SQLITE_SY
133a3 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 2f 2a 20 NC_NORMAL;. /*
133a4 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 20 3d pPager->pFirst =
133a5 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 0; */. /* pPag
133a6 65 72 2d 3e 70 46 69 72 73 74 53 79 6e 63 65 64 er->pFirstSynced
133a7 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 = 0; */. /* pP
133a8 61 67 65 72 2d 3e 70 4c 61 73 74 20 3d 20 30 3b ager->pLast = 0;
133a9 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 45 */. pPager->nE
133aa 78 74 72 61 20 3d 20 28 75 31 36 29 6e 45 78 74 xtra = (u16)nExt
133ab 72 61 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f ra;. pPager->jo
133ac 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d urnalSizeLimit =
133ad 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
133ae 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d JOURNAL_SIZE_LIM
133af 49 54 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 IT;. assert( is
133b0 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 Open(pPager->fd)
133b1 20 7c 7c 20 74 65 6d 70 46 69 6c 65 20 29 3b 0a || tempFile );.
133b2 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 setSectorSize(
133b3 70 50 61 67 65 72 29 3b 0a 20 20 69 66 28 20 21 pPager);. if( !
133b4 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 useJournal ){.
133b5 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
133b6 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f lMode = PAGER_JO
133b7 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 3b 0a 20 URNALMODE_OFF;.
133b8 20 7d 65 6c 73 65 20 69 66 28 20 6d 65 6d 44 62 }else if( memDb
133b9 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e ){. pPager->
133ba 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 journalMode = PA
133bb 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
133bc 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a MEMORY;. }. /*
133bd 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 pPager->xBusyHa
133be 6e 64 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20 ndler = 0; */.
133bf 2f 2a 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 /* pPager->pBusy
133c0 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20 HandlerArg = 0;
133c1 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 78 52 65 */. pPager->xRe
133c2 69 6e 69 74 65 72 20 3d 20 78 52 65 69 6e 69 74 initer = xReinit
133c3 3b 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70 50 ;. /* memset(pP
133c4 61 67 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c 20 ager->aHash, 0,
133c5 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 61 sizeof(pPager->a
133c6 48 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70 70 Hash)); */. *pp
133c7 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a Pager = pPager;.
133c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
133c9 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 OK;.}..../*.** T
133ca 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
133cb 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74 72 61 called after tra
133cc 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d 20 nsitioning from
133cd 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 74 6f 0a PAGER_UNLOCK to.
133ce 2a 2a 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 ** PAGER_SHARED
133cf 73 74 61 74 65 2e 20 49 74 20 74 65 73 74 73 20 state. It tests
133d0 69 66 20 74 68 65 72 65 20 69 73 20 61 20 68 6f if there is a ho
133d1 74 20 6a 6f 75 72 6e 61 6c 20 70 72 65 73 65 6e t journal presen
133d2 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 69 6c 65 t in.** the file
133d3 2d 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 -system for the
133d4 67 69 76 65 6e 20 70 61 67 65 72 2e 20 41 20 68 given pager. A h
133d5 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 6e ot journal is on
133d6 65 20 74 68 61 74 20 0a 2a 2a 20 6e 65 65 64 73 e that .** needs
133d7 20 74 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 to be played ba
133d8 63 6b 2e 20 41 63 63 6f 72 64 69 6e 67 20 74 6f ck. According to
133d9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
133da 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a a hot-journal.**
133db 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 66 20 file exists if
133dc 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 the following cr
133dd 69 74 65 72 69 61 20 61 72 65 20 6d 65 74 3a 0a iteria are met:.
133de 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 6a 6f **.** * The jo
133df 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 urnal file exist
133e0 73 20 69 6e 20 74 68 65 20 66 69 6c 65 20 73 79 s in the file sy
133e1 73 74 65 6d 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a stem, and.** *
133e2 20 4e 6f 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 No process hold
133e3 73 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 s a RESERVED or
133e4 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 greater lock on
133e5 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
133e6 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 e, and.** * Th
133e7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
133e8 69 74 73 65 6c 66 20 69 73 20 67 72 65 61 74 65 itself is greate
133e9 72 20 74 68 61 6e 20 30 20 62 79 74 65 73 20 69 r than 0 bytes i
133ea 6e 20 73 69 7a 65 2c 20 61 6e 64 0a 2a 2a 20 20 n size, and.**
133eb 20 2a 20 54 68 65 20 66 69 72 73 74 20 62 79 74 * The first byt
133ec 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c e of the journal
133ed 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 file exists and
133ee 20 69 73 20 6e 6f 74 20 30 78 30 30 2e 0a 2a 2a is not 0x00..**
133ef 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 .** If the curre
133f0 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 nt size of the d
133f1 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 atabase file is
133f2 30 20 62 75 74 20 61 20 6a 6f 75 72 6e 61 6c 20 0 but a journal
133f3 66 69 6c 65 0a 2a 2a 20 65 78 69 73 74 73 2c 20 file.** exists,
133f4 74 68 61 74 20 69 73 20 70 72 6f 62 61 62 6c 79 that is probably
133f5 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 an old journal
133f6 6c 65 66 74 20 6f 76 65 72 20 66 72 6f 6d 20 61 left over from a
133f7 20 70 72 69 6f 72 0a 2a 2a 20 64 61 74 61 62 61 prior.** databa
133f8 73 65 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 se with the same
133f9 20 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 name. In this c
133fa 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ase the journal
133fb 66 69 6c 65 20 69 73 0a 2a 2a 20 6a 75 73 74 20 file is.** just
133fc 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 4f 73 deleted using Os
133fd 44 65 6c 65 74 65 2c 20 2a 70 45 78 69 73 74 73 Delete, *pExists
133fe 20 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 is set to 0 and
133ff 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73 SQLITE_OK.** is
13400 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
13401 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f This routine do
13402 65 73 20 6e 6f 74 20 63 68 65 63 6b 20 69 66 20 es not check if
13403 74 68 65 72 65 20 69 73 20 61 20 6d 61 73 74 65 there is a maste
13404 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 r journal filena
13405 6d 65 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 me.** at the end
13406 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 49 66 of the file. If
13407 20 74 68 65 72 65 20 69 73 2c 20 61 6e 64 20 74 there is, and t
13408 68 61 74 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e hat master journ
13409 61 6c 20 66 69 6c 65 0a 2a 2a 20 64 6f 65 73 20 al file.** does
1340a 6e 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 not exist, then
1340b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1340c 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 is not really h
1340d 6f 74 2e 20 49 6e 20 74 68 69 73 0a 2a 2a 20 63 ot. In this.** c
1340e 61 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ase this routine
1340f 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61 20 66 will return a f
13410 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 2e 20 54 alse-positive. T
13411 68 65 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 he pager_playbac
13412 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 k().** routine w
13413 69 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61 ill discover tha
13414 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 t the journal fi
13415 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 le is not really
13416 20 68 6f 74 20 61 6e 64 20 0a 2a 2a 20 77 69 6c hot and .** wil
13417 6c 20 6e 6f 74 20 72 6f 6c 6c 20 69 74 20 62 61 l not roll it ba
13418 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ck. .**.** If a
13419 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 hot-journal file
1341a 20 69 73 20 66 6f 75 6e 64 20 74 6f 20 65 78 69 is found to exi
1341b 73 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 st, *pExists is
1341c 73 65 74 20 74 6f 20 31 20 61 6e 64 20 0a 2a 2a set to 1 and .**
1341d 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 SQLITE_OK retur
1341e 6e 65 64 2e 20 49 66 20 6e 6f 20 68 6f 74 2d 6a ned. If no hot-j
1341f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 ournal file is p
13420 72 65 73 65 6e 74 2c 20 2a 70 45 78 69 73 74 73 resent, *pExists
13421 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 20 is.** set to 0
13422 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 and SQLITE_OK re
13423 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f turned. If an IO
13424 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
13425 69 6c 65 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f ile trying.** to
13426 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 determine wheth
13427 65 72 20 6f 72 20 6e 6f 74 20 61 20 68 6f 74 2d er or not a hot-
13428 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 journal file exi
13429 73 74 73 2c 20 74 68 65 20 49 4f 20 65 72 72 6f sts, the IO erro
1342a 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74 r.** code is ret
1342b 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 76 61 urned and the va
1342c 6c 75 65 20 6f 66 20 2a 70 45 78 69 73 74 73 20 lue of *pExists
1342d 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f is undefined..*/
1342e 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 48 .static int hasH
1342f 6f 74 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 otJournal(Pager
13430 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 45 *pPager, int *pE
13431 78 69 73 74 73 29 7b 0a 20 20 73 71 6c 69 74 65 xists){. sqlite
13432 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 3_vfs * const pV
13433 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 fs = pPager->pVf
13434 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 s;. int rc;
13435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13436 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 /* Return cod
13437 65 20 2a 2f 0a 20 20 69 6e 74 20 65 78 69 73 74 e */. int exist
13438 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
13439 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1343a 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 a journal file i
1343b 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 0a 20 20 s present */..
1343c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d assert( pPager!=
1343d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
1343e 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
1343f 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 l );. assert( i
13440 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
13441 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 ) );. assert( !
13442 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
13443 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 fd) );. assert(
13444 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c pPager->state <
13445 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 = PAGER_SHARED )
13446 3b 0a 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 ;.. *pExists =
13447 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 0;. rc = sqlite
13448 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 3OsAccess(pVfs,
13449 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
1344a 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f , SQLITE_ACCESS_
1344b 45 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 EXISTS, &exists)
1344c 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1344d 54 45 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 TE_OK && exists
1344e 29 7b 0a 20 20 20 20 69 6e 74 20 6c 6f 63 6b 65 ){. int locke
1344f 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
13450 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 73 6f /* True if so
13451 6d 65 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 me process holds
13452 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b a RESERVED lock
13453 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 61 63 65 */.. /* Race
13454 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 65 3a condition here:
13455 20 20 41 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 Another proces
13456 73 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 s might have bee
13457 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 n holding the.
13458 20 20 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45 ** the RESERVE
13459 44 20 6c 6f 63 6b 20 61 6e 64 20 68 61 76 65 20 D lock and have
1345a 61 20 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 20 61 a journal open a
1345b 74 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 41 t the sqlite3OsA
1345c 63 63 65 73 73 28 29 20 0a 20 20 20 20 2a 2a 20 ccess() . **
1345d 63 61 6c 6c 20 61 62 6f 76 65 2c 20 62 75 74 20 call above, but
1345e 74 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 then delete the
1345f 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 64 72 6f 70 journal and drop
13460 20 74 68 65 20 6c 6f 63 6b 20 62 65 66 6f 72 65 the lock before
13461 0a 20 20 20 20 2a 2a 20 77 65 20 67 65 74 20 74 . ** we get t
13462 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 o the following
13463 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 sqlite3OsCheckRe
13464 73 65 72 76 65 64 4c 6f 63 6b 28 29 20 63 61 6c servedLock() cal
13465 6c 2e 20 20 49 66 20 74 68 61 74 0a 20 20 20 20 l. If that.
13466 2a 2a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 ** is the case,
13467 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 this routine mig
13468 68 74 20 74 68 69 6e 6b 20 74 68 65 72 65 20 69 ht think there i
13469 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 s a hot journal
1346a 77 68 65 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 66 when. ** in f
1346b 61 63 74 20 74 68 65 72 65 20 69 73 20 6e 6f 6e act there is non
1346c 65 2e 20 20 54 68 69 73 20 72 65 73 75 6c 74 73 e. This results
1346d 20 69 6e 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 in a false-posi
1346e 74 69 76 65 20 77 68 69 63 68 20 77 69 6c 6c 0a tive which will.
1346f 20 20 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 ** be dealt
13470 77 69 74 68 20 62 79 20 74 68 65 20 70 6c 61 79 with by the play
13471 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 20 20 54 back routine. T
13472 69 63 6b 65 74 20 23 33 38 38 33 2e 0a 20 20 20 icket #3883..
13473 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c */. rc = sql
13474 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 ite3OsCheckReser
13475 76 65 64 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e vedLock(pPager->
13476 66 64 2c 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 fd, &locked);.
13477 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13478 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b 65 64 20 29 _OK && !locked )
13479 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 {. int nPag
1347a 65 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 e;.. /* Che
1347b 63 6b 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ck the size of t
1347c 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1347d 2e 20 49 66 20 69 74 20 63 6f 6e 73 69 73 74 73 . If it consists
1347e 20 6f 66 20 30 20 70 61 67 65 73 2c 0a 20 20 20 of 0 pages,.
1347f 20 20 20 2a 2a 20 74 68 65 6e 20 64 65 6c 65 74 ** then delet
13480 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 e the journal fi
13481 6c 65 2e 20 53 65 65 20 74 68 65 20 68 65 61 64 le. See the head
13482 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 er comment above
13483 20 66 6f 72 20 0a 20 20 20 20 20 20 2a 2a 20 74 for . ** t
13484 68 65 20 72 65 61 73 6f 6e 69 6e 67 20 68 65 72 he reasoning her
13485 65 2e 20 20 44 65 6c 65 74 65 20 74 68 65 20 6f e. Delete the o
13486 62 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 bsolete journal
13487 66 69 6c 65 20 75 6e 64 65 72 0a 20 20 20 20 20 file under.
13488 20 2a 2a 20 61 20 52 45 53 45 52 56 45 44 20 6c ** a RESERVED l
13489 6f 63 6b 20 74 6f 20 61 76 6f 69 64 20 72 61 63 ock to avoid rac
1348a 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 6e 64 e conditions and
1348b 20 74 6f 20 61 76 6f 69 64 20 76 69 6f 6c 61 74 to avoid violat
1348c 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 5b 48 33 ing. ** [H3
1348d 33 30 32 30 5d 2e 0a 20 20 20 20 20 20 2a 2f 0a 3020].. */.
1348e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
1348f 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 e3PagerPagecount
13490 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 (pPager, &nPage)
13491 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d ;. if( rc==
13492 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13493 20 20 20 20 20 69 66 28 20 6e 50 61 67 65 3d 3d if( nPage==
13494 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 0 ){. s
13495 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 qlite3BeginBenig
13496 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
13497 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
13498 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 OsLock(pPager->f
13499 64 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b d, RESERVED_LOCK
1349a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a )==SQLITE_OK ){.
1349b 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1349c 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 te3OsDelete(pVfs
1349d 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e , pPager->zJourn
1349e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 al, 0);.
1349f 20 20 20 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c sqlite3OsUnl
134a0 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 ock(pPager->fd,
134a1 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SHARED_LOCK);.
134a2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
134a3 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 sqlite3EndBe
134a4 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
134a5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
134a6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f /* The jo
134a7 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 urnal file exist
134a8 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 63 s and no other c
134a9 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 20 onnection has a
134aa 72 65 73 65 72 76 65 64 0a 20 20 20 20 20 20 20 reserved.
134ab 20 20 20 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 ** or greater
134ac 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
134ad 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 77 20 abase file. Now
134ae 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 check that there
134af 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a is. **
134b0 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6e 6f at least one no
134b1 6e 2d 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 n-zero bytes at
134b2 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 the start of the
134b3 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 journal file..
134b4 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 74 ** If t
134b5 68 65 72 65 20 69 73 2c 20 74 68 65 6e 20 77 65 here is, then we
134b6 20 63 6f 6e 73 69 64 65 72 20 74 68 69 73 20 6a consider this j
134b7 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 68 6f 74 ournal to be hot
134b8 2e 20 49 66 20 6e 6f 74 2c 20 0a 20 20 20 20 20 . If not, .
134b9 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 ** it can b
134ba 65 20 69 67 6e 6f 72 65 64 2e 0a 20 20 20 20 20 e ignored..
134bb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
134bc 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45 int f = SQLITE
134bd 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 _OPEN_READONLY|S
134be 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
134bf 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20 20 JOURNAL;.
134c0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f rc = sqlite3O
134c1 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 sOpen(pVfs, pPag
134c2 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 er->zJournal, pP
134c3 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 ager->jfd, f, &f
134c4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
134c5 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
134c6 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 38 {. u8
134c7 20 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 first = 0;.
134c8 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
134c9 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 ite3OsRead(pPage
134ca 72 2d 3e 6a 66 64 2c 20 28 76 6f 69 64 20 2a 29 r->jfd, (void *)
134cb 26 66 69 72 73 74 2c 20 31 2c 20 30 29 3b 0a 20 &first, 1, 0);.
134cc 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 if( r
134cd 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f c==SQLITE_IOERR_
134ce 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 SHORT_READ ){.
134cf 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d rc =
134d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
134d1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
134d2 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
134d3 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 lose(pPager->jfd
134d4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a );. *
134d5 70 45 78 69 73 74 73 20 3d 20 28 66 69 72 73 74 pExists = (first
134d6 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 !=0);.
134d7 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 }else if( rc==SQ
134d8 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 29 7b LITE_CANTOPEN ){
134d9 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
134da 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 70 65 If we cannot ope
134db 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a n the rollback j
134dc 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 6f ournal file in o
134dd 72 64 65 72 20 74 6f 20 73 65 65 20 69 66 0a 20 rder to see if.
134de 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 ** it
134df 73 20 68 61 73 20 61 20 7a 65 72 6f 20 68 65 61 s has a zero hea
134e0 64 65 72 2c 20 74 68 61 74 20 6d 69 67 68 74 20 der, that might
134e1 62 65 20 64 75 65 20 74 6f 20 61 6e 20 49 2f 4f be due to an I/O
134e2 20 65 72 72 6f 72 2c 20 6f 72 0a 20 20 20 20 20 error, or.
134e3 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 ** it mig
134e4 68 74 20 62 65 20 64 75 65 20 74 6f 20 74 68 65 ht be due to the
134e5 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 race condition
134e6 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 described above
134e7 61 6e 64 20 69 6e 0a 20 20 20 20 20 20 20 20 20 and in.
134e8 20 20 20 2a 2a 20 74 69 63 6b 65 74 20 23 33 38 ** ticket #38
134e9 38 33 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 83. Either way,
134ea 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 assume that the
134eb 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e journal is hot.
134ec 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 . **
134ed 54 68 69 73 20 6d 69 67 68 74 20 62 65 20 61 20 This might be a
134ee 66 61 6c 73 65 20 70 6f 73 69 74 69 76 65 2e 20 false positive.
134ef 20 42 75 74 20 69 66 20 69 74 20 69 73 2c 20 74 But if it is, t
134f0 68 65 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 hen the.
134f1 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 ** automatic
134f2 20 6a 6f 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 journal playbac
134f3 6b 20 61 6e 64 20 72 65 63 6f 76 65 72 79 20 6d k and recovery m
134f4 65 63 68 61 6e 69 73 6d 20 77 69 6c 6c 20 64 65 echanism will de
134f5 61 6c 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a al. *
134f6 2a 20 77 69 74 68 20 69 74 20 75 6e 64 65 72 20 * with it under
134f7 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 an EXCLUSIVE loc
134f8 6b 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f k where we do no
134f9 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 t need to.
134fa 20 20 20 20 20 20 2a 2a 20 77 6f 72 72 79 20 73 ** worry s
134fb 6f 20 6d 75 63 68 20 77 69 74 68 20 72 61 63 65 o much with race
134fc 20 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 20 20 20 conditions..
134fd 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 */.
134fe 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 74 73 *pExists
134ff 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
13500 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
13501 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
13502 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
13503 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 }. }.. ret
13504 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13505 20 52 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e Read the conten
13506 74 20 66 6f 72 20 70 61 67 65 20 70 50 67 20 6f t for page pPg o
13507 75 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ut of the databa
13508 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 74 6f se file and into
13509 20 0a 2a 2a 20 70 50 67 2d 3e 70 44 61 74 61 2e .** pPg->pData.
1350a 20 41 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f A shared lock o
1350b 72 20 67 72 65 61 74 65 72 20 6d 75 73 74 20 62 r greater must b
1350c 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 64 61 e held on the da
1350d 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 62 tabase.** file b
1350e 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 efore this funct
1350f 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a ion is called..*
13510 2a 0a 2a 2a 20 49 66 20 70 61 67 65 20 31 20 69 *.** If page 1 i
13511 73 20 72 65 61 64 2c 20 74 68 65 6e 20 74 68 65 s read, then the
13512 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e value of Pager.
13513 64 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 dbFileVers[] is
13514 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 76 61 set to.** the va
13515 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 lue read from th
13516 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
13517 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 .**.** If an IO
13518 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
13519 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 en the IO error
1351a 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 is returned to t
1351b 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 he caller..** Ot
1351c 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f herwise, SQLITE_
1351d 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a OK is returned..
1351e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
1351f 61 64 44 62 50 61 67 65 28 50 67 48 64 72 20 2a adDbPage(PgHdr *
13520 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 pPg){. Pager *p
13521 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 Pager = pPg->pPa
13522 67 65 72 3b 20 2f 2a 20 50 61 67 65 72 20 6f 62 ger; /* Pager ob
13523 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 ject associated
13524 77 69 74 68 20 70 61 67 65 20 70 50 67 20 2a 2f with page pPg */
13525 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 . Pgno pgno = p
13526 50 67 2d 3e 70 67 6e 6f 3b 20 20 20 20 20 20 20 Pg->pgno;
13527 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 74 /* Page number t
13528 6f 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 o read */. int
13529 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 rc;
1352a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 /* Retu
1352b 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 36 34 rn code */. i64
1352c 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 iOffset;
1352d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 /* Byt
1352e 65 20 6f 66 66 73 65 74 20 6f 66 20 66 69 6c 65 e offset of file
1352f 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f to read from */
13530 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 .. assert( pPag
13531 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 er->state>=PAGER
13532 5f 53 48 41 52 45 44 20 26 26 20 21 4d 45 4d 44 _SHARED && !MEMD
13533 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 B );. assert( i
13534 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 sOpen(pPager->fd
13535 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 ) );.. if( NEVE
13536 52 28 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 R(!isOpen(pPager
13537 2d 3e 66 64 29 29 20 29 7b 0a 20 20 20 20 61 73 ->fd)) ){. as
13538 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 74 65 sert( pPager->te
13539 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 6d 65 mpFile );. me
1353a 6d 73 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c mset(pPg->pData,
1353b 20 30 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 0, pPager->page
1353c 53 69 7a 65 29 3b 0a 20 20 20 20 72 65 74 75 72 Size);. retur
1353d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d n SQLITE_OK;. }
1353e 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 28 70 67 . iOffset = (pg
1353f 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 no-1)*(i64)pPage
13540 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 72 r->pageSize;. r
13541 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 c = sqlite3OsRea
13542 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 d(pPager->fd, pP
13543 67 2d 3e 70 44 61 74 61 2c 20 70 50 61 67 65 72 g->pData, pPager
13544 2d 3e 70 61 67 65 53 69 7a 65 2c 20 69 4f 66 66 ->pageSize, iOff
13545 73 65 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d set);. if( rc==
13546 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f SQLITE_IOERR_SHO
13547 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 72 RT_READ ){. r
13548 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
13549 20 7d 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 31 }. if( pgno==1
1354a 20 29 7b 0a 20 20 20 20 75 38 20 2a 64 62 46 69 ){. u8 *dbFi
1354b 6c 65 56 65 72 73 20 3d 20 26 28 28 75 38 2a 29 leVers = &((u8*)
1354c 70 50 67 2d 3e 70 44 61 74 61 29 5b 32 34 5d 3b pPg->pData)[24];
1354d 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 . memcpy(&pPa
1354e 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c ger->dbFileVers,
1354f 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a dbFileVers, siz
13550 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69 eof(pPager->dbFi
13551 6c 65 56 65 72 73 29 29 3b 0a 20 20 7d 0a 20 20 leVers));. }.
13552 43 4f 44 45 43 31 28 70 50 61 67 65 72 2c 20 70 CODEC1(pPager, p
13553 50 67 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c Pg->pData, pgno,
13554 20 33 2c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 3, rc = SQLITE_
13555 4e 4f 4d 45 4d 29 3b 0a 0a 20 20 50 41 47 45 52 NOMEM);.. PAGER
13556 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 _INCR(sqlite3_pa
13557 67 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 ger_readdb_count
13558 29 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 );. PAGER_INCR(
13559 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a pPager->nRead);.
1355a 20 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e IOTRACE(("PGIN
1355b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
1355c 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 50 41 47 r, pgno));. PAG
1355d 45 52 54 52 41 43 45 28 28 22 46 45 54 43 48 20 ERTRACE(("FETCH
1355e 25 64 20 70 61 67 65 20 25 64 20 68 61 73 68 28 %d page %d hash(
1355f 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 %08x)\n",.
13560 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 PAGERID
13561 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 (pPager), pgno,
13562 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 pager_pagehash(p
13563 50 67 29 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e Pg)));.. return
13564 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
13565 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
13566 61 6c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 alled to obtain
13567 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e a shared lock on
13568 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13569 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c le..** It is ill
1356a 65 67 61 6c 20 74 6f 20 63 61 6c 6c 20 73 71 6c egal to call sql
1356b 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 ite3PagerAcquire
1356c 28 29 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 () until after t
1356d 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 his function.**
1356e 68 61 73 20 62 65 65 6e 20 73 75 63 63 65 73 73 has been success
1356f 66 75 6c 6c 79 20 63 61 6c 6c 65 64 2e 20 49 66 fully called. If
13570 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 69 a shared-lock i
13571 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20 77 s already held w
13572 68 65 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 hen.** this func
13573 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 tion is called,
13574 69 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a it is a no-op..*
13575 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 *.** The followi
13576 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 ng operations ar
13577 65 20 61 6c 73 6f 20 70 65 72 66 6f 72 6d 65 64 e also performed
13578 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
13579 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 n..**.** 1) If
1357a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75 the pager is cu
1357b 72 72 65 6e 74 6c 79 20 69 6e 20 50 41 47 45 52 rrently in PAGER
1357c 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 28 6e _UNLOCK state (n
1357d 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a 2a 2a 20 20 o lock held.**
1357e 20 20 20 20 6f 6e 20 74 68 65 20 64 61 74 61 62 on the datab
1357f 61 73 65 20 66 69 6c 65 29 2c 20 74 68 65 6e 20 ase file), then
13580 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 an attempt is ma
13581 64 65 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a de to obtain a.*
13582 2a 20 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f * SHARED lo
13583 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
13584 73 65 20 66 69 6c 65 2e 20 49 6d 6d 65 64 69 61 se file. Immedia
13585 74 65 6c 79 20 61 66 74 65 72 20 6f 62 74 61 69 tely after obtai
13586 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65 ning.** the
13587 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 SHARED lock, th
13588 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 69 73 e file-system is
13589 20 63 68 65 63 6b 65 64 20 66 6f 72 20 61 20 68 checked for a h
1358a 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a 2a 2a 20 20 ot-journal,.**
1358b 20 20 20 20 77 68 69 63 68 20 69 73 20 70 6c 61 which is pla
1358c 79 65 64 20 62 61 63 6b 20 69 66 20 70 72 65 73 yed back if pres
1358d 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 ent. Following a
1358e 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 0a ny hot-journal .
1358f 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b ** rollback
13590 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f , the contents o
13591 66 20 74 68 65 20 63 61 63 68 65 20 61 72 65 20 f the cache are
13592 76 61 6c 69 64 61 74 65 64 20 62 79 20 63 68 65 validated by che
13593 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 cking.** th
13594 65 20 27 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 e 'change-counte
13595 72 27 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 r' field of the
13596 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 database file he
13597 61 64 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 ader and.**
13598 20 64 69 73 63 61 72 64 65 64 20 69 66 20 74 68 discarded if th
13599 65 79 20 61 72 65 20 66 6f 75 6e 64 20 74 6f 20 ey are found to
1359a 62 65 20 69 6e 76 61 6c 69 64 2e 0a 2a 2a 0a 2a be invalid..**.*
1359b 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 70 61 * 2) If the pa
1359c 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 ger is running i
1359d 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 n exclusive-mode
1359e 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 , and there are
1359f 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 currently.**
135a0 20 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 no outstanding
135a1 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 references to a
135a2 6e 79 20 70 61 67 65 73 2c 20 61 6e 64 20 69 73 ny pages, and is
135a3 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 in the error st
135a4 61 74 65 2c 0a 2a 2a 20 20 20 20 20 20 74 68 65 ate,.** the
135a5 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 n an attempt is
135a6 6d 61 64 65 20 74 6f 20 63 6c 65 61 72 20 74 68 made to clear th
135a7 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 62 79 e error state by
135a8 20 64 69 73 63 61 72 64 69 6e 67 0a 2a 2a 20 20 discarding.**
135a9 20 20 20 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 the contents
135aa 20 6f 66 20 74 68 65 20 70 61 67 65 20 63 61 63 of the page cac
135ab 68 65 20 61 6e 64 20 72 6f 6c 6c 69 6e 67 20 62 he and rolling b
135ac 61 63 6b 20 61 6e 79 20 6f 70 65 6e 20 6a 6f 75 ack any open jou
135ad 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 66 69 6c rnal.** fil
135ae 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e..**.** If the
135af 6f 70 65 72 61 74 69 6f 6e 20 64 65 73 63 72 69 operation descri
135b0 62 65 64 20 62 79 20 28 32 29 20 61 62 6f 76 65 bed by (2) above
135b1 20 69 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 65 is not attempte
135b2 64 2c 20 61 6e 64 20 69 66 20 74 68 65 0a 2a 2a d, and if the.**
135b3 20 70 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 pager is in an
135b4 65 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 error state othe
135b5 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 r than SQLITE_FU
135b6 4c 4c 20 77 68 65 6e 20 74 68 69 73 20 69 73 20 LL when this is
135b7 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65 20 65 called,.** the e
135b8 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f 72 rror state error
135b9 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 code is returne
135ba 64 2e 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 d. It is permitt
135bb 65 64 20 74 6f 20 72 65 61 64 20 74 68 65 0a 2a ed to read the.*
135bc 2a 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 20 * database when
135bd 69 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 in SQLITE_FULL e
135be 72 72 6f 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a rror state..**.*
135bf 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 * Otherwise, if
135c0 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 everything is su
135c1 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 ccessful, SQLITE
135c2 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
135c3 20 49 66 20 61 6e 0a 2a 2a 20 49 4f 20 65 72 72 If an.** IO err
135c4 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 or occurs while
135c5 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 61 74 61 locking the data
135c6 62 61 73 65 2c 20 63 68 65 63 6b 69 6e 67 20 66 base, checking f
135c7 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c or a hot-journal
135c8 0a 2a 2a 20 66 69 6c 65 20 6f 72 20 72 6f 6c 6c .** file or roll
135c9 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e ing back a journ
135ca 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 49 4f 20 al file, the IO
135cb 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
135cc 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
135cd 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
135ce 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 lite3PagerShared
135cf 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 Lock(Pager *pPag
135d0 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
135d1 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
135d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
135d3 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e urn code */. in
135d4 74 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 3d t isErrorReset =
135d5 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
135d6 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 63 6f /* True if reco
135d7 76 65 72 69 6e 67 20 66 72 6f 6d 20 65 72 72 6f vering from erro
135d8 72 20 73 74 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a r state */.. /*
135d9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
135da 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f only called fro
135db 6d 20 62 2d 74 72 65 65 20 61 6e 64 20 6f 6e 6c m b-tree and onl
135dc 79 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 y when there are
135dd 20 6e 6f 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e no. ** outstan
135de 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a 20 20 ding pages */.
135df 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
135e0 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
135e1 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
135e2 30 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 0 );. if( NEVER
135e3 28 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 (MEMDB && pPager
135e4 2d 3e 65 72 72 43 6f 64 65 29 20 29 7b 20 72 65 ->errCode) ){ re
135e5 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 turn pPager->err
135e6 43 6f 64 65 3b 20 7d 0a 0a 20 20 2f 2a 20 49 66 Code; }.. /* If
135e7 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 69 this database i
135e8 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 2d 73 74 s in an error-st
135e9 61 74 65 2c 20 6e 6f 77 20 69 73 20 61 20 63 68 ate, now is a ch
135ea 61 6e 63 65 20 74 6f 20 63 6c 65 61 72 0a 20 20 ance to clear.
135eb 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 20 44 69 ** the error. Di
135ec 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e scard the conten
135ed 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d ts of the pager-
135ee 63 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 62 61 cache and rollba
135ef 63 6b 0a 20 20 2a 2a 20 61 6e 79 20 68 6f 74 20 ck. ** any hot
135f0 6a 6f 75 72 6e 61 6c 20 69 6e 20 74 68 65 20 66 journal in the f
135f1 69 6c 65 2d 73 79 73 74 65 6d 2e 0a 20 20 2a 2f ile-system.. */
135f2 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 . if( pPager->e
135f3 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 69 66 rrCode ){. if
135f4 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d ( isOpen(pPager-
135f5 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d >jfd) || pPager-
135f6 3e 7a 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 >zJournal ){.
135f7 20 20 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 isErrorReset
135f8 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 = 1;. }. p
135f9 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d Pager->errCode =
135fa 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
135fb 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 pager_reset(pPag
135fc 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 er);. }.. if(
135fd 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
135fe 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c 20 69 AGER_UNLOCK || i
135ff 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 sErrorReset ){.
13600 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a sqlite3_vfs *
13601 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 const pVfs = pP
13602 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 20 20 ager->pVfs;.
13603 69 6e 74 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c int isHotJournal
13604 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 = 0;. assert
13605 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20 ( !MEMDB );.
13606 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
13607 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
13608 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d ager->pPCache)==
13609 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 0 );. if( pPa
1360a 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 ger->noReadlock
1360b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
1360c 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c pPager->readOnl
1360d 79 20 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 y );. pPage
1360e 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
1360f 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 7d 65 6c _SHARED;. }el
13610 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 se{. rc = p
13611 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 ager_wait_on_loc
13612 6b 28 70 50 61 67 65 72 2c 20 53 48 41 52 45 44 k(pPager, SHARED
13613 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 _LOCK);. if
13614 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13615 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
13616 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 t( pPager->state
13617 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 ==PAGER_UNLOCK )
13618 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
13619 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 pager_error(pPa
1361a 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 ger, rc);.
1361b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 }. }. asse
1361c 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
1361d 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 e>=SHARED_LOCK )
1361e 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 6a ;.. /* If a j
1361f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 ournal file exis
13620 74 73 2c 20 61 6e 64 20 74 68 65 72 65 20 69 73 ts, and there is
13621 20 6e 6f 20 52 45 53 45 52 56 45 44 20 6c 6f 63 no RESERVED loc
13622 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 k on the. **
13623 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 database file, t
13624 68 65 6e 20 69 74 20 65 69 74 68 65 72 20 6e 65 hen it either ne
13625 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64 eds to be played
13626 20 62 61 63 6b 20 6f 72 20 64 65 6c 65 74 65 64 back or deleted
13627 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
13628 20 21 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 !isErrorReset )
13629 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
1362a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3c 3d pPager->state <=
1362b 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b PAGER_SHARED );
1362c 0a 20 20 20 20 20 20 72 63 20 3d 20 68 61 73 48 . rc = hasH
1362d 6f 74 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 otJournal(pPager
1362e 2c 20 26 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 29 , &isHotJournal)
1362f 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d ;. if( rc!=
13630 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13631 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 goto failed
13632 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
13633 20 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 52 if( isErrorR
13634 65 73 65 74 20 7c 7c 20 69 73 48 6f 74 4a 6f 75 eset || isHotJou
13635 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a rnal ){. /*
13636 20 47 65 74 20 61 6e 20 45 58 43 4c 55 53 49 56 Get an EXCLUSIV
13637 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 E lock on the da
13638 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 74 20 tabase file. At
13639 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 69 73 this point it is
1363a 0a 20 20 20 20 20 20 2a 2a 20 69 6d 70 6f 72 74 . ** import
1363b 61 6e 74 20 74 68 61 74 20 61 20 52 45 53 45 52 ant that a RESER
1363c 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 VED lock is not
1363d 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 obtained on the
1363e 77 61 79 20 74 6f 20 74 68 65 0a 20 20 20 20 20 way to the.
1363f 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f ** EXCLUSIVE lo
13640 63 6b 2e 20 49 66 20 69 74 20 77 65 72 65 2c 20 ck. If it were,
13641 61 6e 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 another process
13642 6d 69 67 68 74 20 6f 70 65 6e 20 74 68 65 0a 20 might open the.
13643 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 ** database
13644 20 66 69 6c 65 2c 20 64 65 74 65 63 74 20 74 68 file, detect th
13645 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c e RESERVED lock,
13646 20 61 6e 64 20 63 6f 6e 63 6c 75 64 65 20 74 68 and conclude th
13647 61 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 at the. **
13648 64 61 74 61 62 61 73 65 20 69 73 20 73 61 66 65 database is safe
13649 20 74 6f 20 72 65 61 64 20 77 68 69 6c 65 20 74 to read while t
1364a 68 69 73 20 70 72 6f 63 65 73 73 20 69 73 20 73 his process is s
1364b 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67 20 74 68 65 till rolling the
1364c 20 0a 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a . ** hot-j
1364d 6f 75 72 6e 61 6c 20 62 61 63 6b 2e 0a 20 20 20 ournal back..
1364e 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 ** . **
1364f 42 65 63 61 75 73 65 20 74 68 65 20 69 6e 74 65 Because the inte
13650 72 6d 65 64 69 61 74 65 20 52 45 53 45 52 56 45 rmediate RESERVE
13651 44 20 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 D lock is not re
13652 71 75 65 73 74 65 64 2c 20 61 6e 79 0a 20 20 20 quested, any.
13653 20 20 20 2a 2a 20 6f 74 68 65 72 20 70 72 6f 63 ** other proc
13654 65 73 73 20 61 74 74 65 6d 70 74 69 6e 67 20 74 ess attempting t
13655 6f 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74 o access the dat
13656 61 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 abase file will
13657 67 65 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a get to . **
13658 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 this point in t
13659 68 65 20 63 6f 64 65 20 61 6e 64 20 66 61 69 6c he code and fail
1365a 20 74 6f 20 6f 62 74 61 69 6e 20 69 74 73 20 6f to obtain its o
1365b 77 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 wn EXCLUSIVE loc
1365c 6b 20 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 k . ** on t
1365d 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
1365e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1365f 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 if( pPager->sta
13660 74 65 3c 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 te<EXCLUSIVE_LOC
13661 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 K ){. rc
13662 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 = sqlite3OsLock(
13663 70 50 61 67 65 72 2d 3e 66 64 2c 20 45 58 43 4c pPager->fd, EXCL
13664 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 USIVE_LOCK);.
13665 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
13666 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
13667 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 rc = pager_e
13668 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
13669 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f ;. goto
1366a 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 failed;.
1366b 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 }. pPage
1366c 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 r->state = PAGER
1366d 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20 20 _EXCLUSIVE;.
1366e 20 20 7d 0a 20 0a 20 20 20 20 20 20 2f 2a 20 4f }. . /* O
1366f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 pen the journal
13670 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61 for read/write a
13671 63 63 65 73 73 2e 20 54 68 69 73 20 69 73 20 62 ccess. This is b
13672 65 63 61 75 73 65 20 69 6e 20 0a 20 20 20 20 20 ecause in .
13673 20 2a 2a 20 65 78 63 6c 75 73 69 76 65 2d 61 63 ** exclusive-ac
13674 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 66 69 cess mode the fi
13675 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77 69 le descriptor wi
13676 6c 6c 20 62 65 20 6b 65 70 74 20 6f 70 65 6e 20 ll be kept open
13677 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 70 6f 73 and. ** pos
13678 73 69 62 6c 79 20 75 73 65 64 20 66 6f 72 20 61 sibly used for a
13679 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6c 61 74 transaction lat
1367a 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 er on. On some s
1367b 79 73 74 65 6d 73 2c 20 74 68 65 0a 20 20 20 20 ystems, the.
1367c 20 20 2a 2a 20 4f 73 54 72 75 6e 63 61 74 65 28 ** OsTruncate(
1367d 29 20 63 61 6c 6c 20 75 73 65 64 20 69 6e 20 65 ) call used in e
1367e 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 xclusive-access
1367f 6d 6f 64 65 20 61 6c 73 6f 20 72 65 71 75 69 72 mode also requir
13680 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 72 65 es. ** a re
13681 61 64 2f 77 72 69 74 65 20 66 69 6c 65 20 68 61 ad/write file ha
13682 6e 64 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ndle.. */.
13683 20 20 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e if( !isOpen
13684 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b (pPager->jfd) ){
13685 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 . int res
13686 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
13687 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 qlite3OsAccess(p
13688 56 66 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 Vfs,pPager->zJou
13689 72 6e 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 rnal,SQLITE_ACCE
1368a 53 53 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b SS_EXISTS,&res);
1368b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d . if( rc=
1368c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1368d 20 20 20 20 20 20 20 20 69 66 28 20 72 65 73 20 if( res
1368e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 ){. i
1368f 6e 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 nt fout = 0;.
13690 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d int f =
13691 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 SQLITE_OPEN_REA
13692 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 DWRITE|SQLITE_OP
13693 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b EN_MAIN_JOURNAL;
13694 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 . ass
13695 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 ert( !pPager->te
13696 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 mpFile );.
13697 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
13698 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 e3OsOpen(pVfs, p
13699 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c Pager->zJournal,
1369a 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c pPager->jfd, f,
1369b 20 26 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 &fout);.
1369c 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 assert( rc!
1369d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 =SQLITE_OK || is
1369e 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 Open(pPager->jfd
1369f 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ) );.
136a0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
136a1 4f 4b 20 26 26 20 66 6f 75 74 26 53 51 4c 49 54 OK && fout&SQLIT
136a2 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20 E_OPEN_READONLY
136a3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
136a4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e rc = SQLITE_CAN
136a5 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 20 20 20 TOPEN;.
136a6 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c sqlite3OsCl
136a7 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 ose(pPager->jfd)
136a8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
136a9 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
136aa 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
136ab 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 64 If the journal d
136ac 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 69 oes not exist, i
136ad 74 20 75 73 75 61 6c 6c 79 20 6d 65 61 6e 73 20 t usually means
136ae 74 68 61 74 20 73 6f 6d 65 20 0a 20 20 20 20 20 that some .
136af 20 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 ** other
136b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 6e 61 67 connection manag
136b1 65 64 20 74 6f 20 67 65 74 20 69 6e 20 61 6e 64 ed to get in and
136b2 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 20 62 65 roll it back be
136b3 66 6f 72 65 20 0a 20 20 20 20 20 20 20 20 20 20 fore .
136b4 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e 6e 65 63 ** this connec
136b5 74 69 6f 6e 20 6f 62 74 61 69 6e 65 64 20 74 68 tion obtained th
136b6 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b e exclusive lock
136b7 20 61 62 6f 76 65 2e 20 4f 72 2c 20 69 74 20 0a above. Or, it .
136b8 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d ** m
136b9 61 79 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 ay mean that the
136ba 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 74 68 pager was in th
136bb 65 20 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 e error-state wh
136bc 65 6e 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 en this.
136bd 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 ** function
136be 77 61 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 was called and t
136bf 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
136c0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 does not exist.
136c1 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 */.
136c2 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 rc = pager_end_t
136c3 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 ransaction(pPage
136c4 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 r, 0);.
136c5 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
136c6 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 }. if( r
136c7 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
136c8 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 goto fai
136c9 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 led;. }..
136ca 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 79 /* TODO: Why
136cb 20 61 72 65 20 74 68 65 73 65 20 63 6c 65 61 72 are these clear
136cc 65 64 20 68 65 72 65 3f 20 49 73 20 69 74 20 6e ed here? Is it n
136cd 65 63 65 73 73 61 72 79 3f 20 2a 2f 0a 20 20 20 ecessary? */.
136ce 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
136cf 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 alStarted = 0;.
136d0 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 pPager->jou
136d1 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 rnalOff = 0;.
136d2 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 pPager->setMa
136d3 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 ster = 0;.
136d4 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 pPager->journalH
136d5 64 72 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20 20 dr = 0;. .
136d6 2f 2a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64 20 /* Playback and
136d7 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e delete the journ
136d8 61 6c 2e 20 20 44 72 6f 70 20 74 68 65 20 64 61 al. Drop the da
136d9 74 61 62 61 73 65 20 77 72 69 74 65 0a 20 20 20 tabase write.
136da 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20 72 ** lock and r
136db 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61 eacquire the rea
136dc 64 20 6c 6f 63 6b 2e 20 50 75 72 67 65 20 74 68 d lock. Purge th
136dd 65 20 63 61 63 68 65 20 62 65 66 6f 72 65 0a 20 e cache before.
136de 20 20 20 20 20 2a 2a 20 70 6c 61 79 69 6e 67 20 ** playing
136df 62 61 63 6b 20 74 68 65 20 68 6f 74 2d 6a 6f 75 back the hot-jou
136e0 72 6e 61 6c 20 73 6f 20 74 68 61 74 20 77 65 20 rnal so that we
136e1 64 6f 6e 27 74 20 65 6e 64 20 75 70 20 77 69 74 don't end up wit
136e2 68 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e h. ** an in
136e3 63 6f 6e 73 69 73 74 65 6e 74 20 63 61 63 68 65 consistent cache
136e4 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
136e5 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 if( isOpen(pPag
136e6 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 er->jfd) ){.
136e7 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 rc = pager_p
136e8 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 layback(pPager,
136e9 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 1);. if(
136ea 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
136eb 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
136ec 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 pager_error(pPag
136ed 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20 er, rc);.
136ee 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a goto failed;.
136ef 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
136f0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
136f1 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d (pPager->state==
136f2 50 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 PAGER_SHARED).
136f3 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 || (pPa
136f4 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
136f5 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 de && pPager->st
136f6 61 74 65 3e 50 41 47 45 52 5f 53 48 41 52 45 44 ate>PAGER_SHARED
136f7 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d ). );. }
136f8 0a 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 .. if( pPager
136f9 2d 3e 70 42 61 63 6b 75 70 20 7c 7c 20 73 71 6c ->pBackup || sql
136fa 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f ite3PcachePageco
136fb 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 unt(pPager->pPCa
136fc 63 68 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 che)>0 ){.
136fd 2f 2a 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f /* The shared-lo
136fe 63 6b 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e ck has just been
136ff 20 61 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 acquired on the
13700 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 database file.
13701 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 ** and ther
13702 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 e are already pa
13703 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 ges in the cache
13704 20 28 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 (from a previou
13705 73 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 s. ** read
13706 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 or write transac
13707 74 69 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f tion). Check to
13708 20 73 65 65 20 69 66 20 74 68 65 20 64 61 74 61 see if the data
13709 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 base. ** ha
1370a 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e s been modified.
1370b 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 If the databas
1370c 65 20 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 e has changed, f
1370d 6c 75 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a lush the. *
1370e 2a 20 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a * cache.. *
1370f 2a 0a 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 *. ** Datab
13710 61 73 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 ase changes is d
13711 65 74 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 etected by looki
13712 6e 67 20 61 74 20 31 35 20 62 79 74 65 73 20 62 ng at 15 bytes b
13713 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a eginning. *
13714 2a 20 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 * at offset 24 i
13715 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 nto the file. T
13716 68 65 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 he first 4 of th
13717 65 73 65 20 31 36 20 62 79 74 65 73 20 61 72 65 ese 16 bytes are
13718 0a 20 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 . ** a 32-b
13719 69 74 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 it counter that
1371a 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 is incremented w
1371b 69 74 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e ith each change.
1371c 20 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f The. ** o
1371d 74 68 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 ther bytes chang
1371e 65 20 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 e randomly with
1371f 65 61 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 each file change
13720 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 when. ** a
13721 20 63 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 codec is in use
13722 2e 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 .. ** .
13723 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 ** There is a
13724 76 61 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c vanishingly smal
13725 6c 20 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 l chance that a
13726 63 68 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 change will not
13727 62 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 be . ** det
13728 65 63 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e ected. The chan
13729 63 65 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 ce of an undetec
1372a 74 65 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f ted change is so
1372b 20 73 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 small that.
1372c 20 20 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e ** it can be n
1372d 65 67 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 eglected..
1372e 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 */. char db
1372f 46 69 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 FileVers[sizeof(
13730 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 pPager->dbFileVe
13731 72 73 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 rs)];. sqli
13732 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
13733 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 t(pPager, 0);..
13734 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d if( pPager-
13735 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 >errCode ){.
13736 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d rc = pPager-
13737 3e 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 >errCode;.
13738 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 goto failed;.
13739 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 }.. as
1373a 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 sert( pPager->db
1373b 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 SizeValid );.
1373c 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 if( pPager->d
1373d 62 53 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 bSize>0 ){.
1373e 20 20 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 IOTRACE(("CKV
1373f 45 52 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 ERS %p %d\n", pP
13740 61 67 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 ager, sizeof(dbF
13741 69 6c 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 ileVers)));.
13742 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13743 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 OsRead(pPager->f
13744 64 2c 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 d, &dbFileVers,
13745 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 sizeof(dbFileVer
13746 73 29 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 s), 24);.
13747 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13748 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
13749 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 goto failed;.
1374a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1374b 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 se{. mems
1374c 65 74 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 et(dbFileVers, 0
1374d 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 , sizeof(dbFileV
1374e 65 72 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a ers));. }..
1374f 20 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 if( memcmp
13750 28 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 (pPager->dbFileV
13751 65 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c ers, dbFileVers,
13752 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 sizeof(dbFileVe
13753 72 73 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 rs))!=0 ){.
13754 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 pager_reset(p
13755 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a Pager);. }.
13756 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
13757 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 ( pPager->exclus
13758 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 iveMode || pPage
13759 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f r->state==PAGER_
1375a 53 48 41 52 45 44 20 29 3b 0a 20 20 7d 0a 0a 20 SHARED );. }..
1375b 66 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 failed:. if( rc
1375c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1375d 20 20 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f /* pager_unlo
1375e 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 ck() is a no-op
1375f 66 6f 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f for exclusive mo
13760 64 65 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 de and in-memory
13761 20 64 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 databases. */.
13762 20 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 pager_unlock(
13763 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 pPager);. }. r
13764 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
13765 2a 2a 20 49 66 20 74 68 65 20 72 65 66 65 72 65 ** If the refere
13766 6e 63 65 20 63 6f 75 6e 74 20 68 61 73 20 72 65 nce count has re
13767 61 63 68 65 64 20 7a 65 72 6f 2c 20 72 6f 6c 6c ached zero, roll
13768 62 61 63 6b 20 61 6e 79 20 61 63 74 69 76 65 0a back any active.
13769 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 ** transaction a
1376a 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 nd unlock the pa
1376b 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 ger..**.** Excep
1376c 74 2c 20 69 6e 20 6c 6f 63 6b 69 6e 67 5f 6d 6f t, in locking_mo
1376d 64 65 3d 45 58 43 4c 55 53 49 56 45 20 77 68 65 de=EXCLUSIVE whe
1376e 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 n there is nothi
1376f 6e 67 20 74 6f 20 69 6e 0a 2a 2a 20 74 68 65 20 ng to in.** the
13770 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
13771 2c 20 74 68 65 20 75 6e 6c 6f 63 6b 20 69 73 20 , the unlock is
13772 6e 6f 74 20 70 65 72 66 6f 72 6d 65 64 20 61 6e not performed an
13773 64 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f d there is.** no
13774 74 68 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63 thing to rollbac
13775 6b 2c 20 73 6f 20 74 68 69 73 20 72 6f 75 74 69 k, so this routi
13776 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a ne is a no-op..*
13777 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 / .static void p
13778 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 agerUnlockIfUnus
13779 65 64 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ed(Pager *pPager
1377a 29 7b 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65 ){. if( (sqlite
1377b 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 3PcacheRefCount(
1377c 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 pPager->pPCache)
1377d 3d 3d 30 29 0a 20 20 20 26 26 20 28 21 70 50 61 ==0). && (!pPa
1377e 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f ger->exclusiveMo
1377f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f de || pPager->jo
13780 75 72 6e 61 6c 4f 66 66 3e 30 29 20 0a 20 20 29 urnalOff>0) . )
13781 7b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63 {. pagerUnloc
13782 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50 61 kAndRollback(pPa
13783 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a ger);. }.}../*.
13784 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 66 ** Acquire a ref
13785 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 6e erence to page n
13786 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 70 61 umber pgno in pa
13787 67 65 72 20 70 50 61 67 65 72 20 28 61 20 70 61 ger pPager (a pa
13788 67 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 ge.** reference
13789 68 61 73 20 74 79 70 65 20 44 62 50 61 67 65 2a has type DbPage*
1378a 29 2e 20 49 66 20 74 68 65 20 72 65 71 75 65 73 ). If the reques
1378b 74 65 64 20 72 65 66 65 72 65 6e 63 65 20 69 73 ted reference is
1378c 20 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c .** successfull
1378d 79 20 6f 62 74 61 69 6e 65 64 2c 20 69 74 20 69 y obtained, it i
1378e 73 20 63 6f 70 69 65 64 20 74 6f 20 2a 70 70 50 s copied to *ppP
1378f 61 67 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f age and SQLITE_O
13790 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a K returned..**.*
13791 2a 20 49 66 20 74 68 65 20 72 65 71 75 65 73 74 * If the request
13792 65 64 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 ed page is alrea
13793 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c dy in the cache,
13794 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e it is returned.
13795 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 .** Otherwise,
13796 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 a new page objec
13797 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 t is allocated a
13798 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 nd populated wit
13799 68 20 64 61 74 61 0a 2a 2a 20 72 65 61 64 20 66 h data.** read f
1379a 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
1379b 20 66 69 6c 65 2e 20 49 6e 20 73 6f 6d 65 20 63 file. In some c
1379c 61 73 65 73 2c 20 74 68 65 20 70 63 61 63 68 65 ases, the pcache
1379d 20 6d 6f 64 75 6c 65 20 6d 61 79 0a 2a 2a 20 63 module may.** c
1379e 68 6f 6f 73 65 20 6e 6f 74 20 74 6f 20 61 6c 6c hoose not to all
1379f 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 ocate a new page
137a0 20 6f 62 6a 65 63 74 20 61 6e 64 20 6d 61 79 20 object and may
137a1 72 65 75 73 65 20 61 6e 20 65 78 69 73 74 69 6e reuse an existin
137a2 67 0a 2a 2a 20 6f 62 6a 65 63 74 20 77 69 74 68 g.** object with
137a3 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
137a4 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a references..**.*
137a5 2a 20 54 68 65 20 65 78 74 72 61 20 64 61 74 61 * The extra data
137a6 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61 20 70 appended to a p
137a7 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e age is always in
137a8 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 itialized to zer
137a9 6f 73 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 os the .** first
137aa 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20 time a page is
137ab 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f loaded into memo
137ac 72 79 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 ry. If the page
137ad 72 65 71 75 65 73 74 65 64 20 69 73 20 0a 2a 2a requested is .**
137ae 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 already in the
137af 63 61 63 68 65 20 77 68 65 6e 20 74 68 69 73 20 cache when this
137b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
137b1 65 64 2c 20 74 68 65 6e 20 74 68 65 20 65 78 74 ed, then the ext
137b2 72 61 0a 2a 2a 20 64 61 74 61 20 69 73 20 6c 65 ra.** data is le
137b3 66 74 20 61 73 20 69 74 20 77 61 73 20 77 68 65 ft as it was whe
137b4 6e 20 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 n the page objec
137b5 74 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e t was last used.
137b6 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 .**.** If the da
137b7 74 61 62 61 73 65 20 69 6d 61 67 65 20 69 73 20 tabase image is
137b8 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 smaller than the
137b9 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 requested page
137ba 6f 72 20 69 66 20 61 20 0a 2a 2a 20 6e 6f 6e 2d or if a .** non-
137bb 7a 65 72 6f 20 76 61 6c 75 65 20 69 73 20 70 61 zero value is pa
137bc 73 73 65 64 20 61 73 20 74 68 65 20 6e 6f 43 6f ssed as the noCo
137bd 6e 74 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20 ntent parameter
137be 61 6e 64 20 74 68 65 20 0a 2a 2a 20 72 65 71 75 and the .** requ
137bf 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f ested page is no
137c0 74 20 61 6c 72 65 61 64 79 20 73 74 6f 72 65 64 t already stored
137c1 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74 in the cache, t
137c2 68 65 6e 20 6e 6f 20 0a 2a 2a 20 61 63 74 75 61 hen no .** actua
137c3 6c 20 64 69 73 6b 20 72 65 61 64 20 6f 63 63 75 l disk read occu
137c4 72 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 rs. In this case
137c5 20 74 68 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 the memory imag
137c6 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 e of the .** pag
137c7 65 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 e is initialized
137c8 20 74 6f 20 61 6c 6c 20 7a 65 72 6f 73 2e 20 0a to all zeros. .
137c9 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 **.** If noConte
137ca 6e 74 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d nt is true, it m
137cb 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 eans that we do
137cc 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 not care about t
137cd 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f he contents.** o
137ce 66 20 74 68 65 20 70 61 67 65 2e 20 54 68 69 73 f the page. This
137cf 20 6f 63 63 75 72 73 20 69 6e 20 74 77 6f 20 73 occurs in two s
137d0 65 70 65 72 61 74 65 20 73 63 65 6e 61 72 69 6f eperate scenario
137d1 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 29 20 57 68 s:.**.** a) Wh
137d2 65 6e 20 72 65 61 64 69 6e 67 20 61 20 66 72 65 en reading a fre
137d3 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 e-list leaf page
137d4 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 from the databa
137d5 73 65 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 se, and.**.**
137d6 62 29 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f b) When a savepo
137d7 69 6e 74 20 69 73 20 62 65 69 6e 67 20 72 6f 6c int is being rol
137d8 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 77 65 20 led back and we
137d9 6e 65 65 64 20 74 6f 20 6c 6f 61 64 0a 2a 2a 20 need to load.**
137da 20 20 20 20 20 61 20 6e 65 77 20 70 61 67 65 20 a new page
137db 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65 20 74 into the cache t
137dc 6f 20 70 6f 70 75 6c 61 74 65 20 77 69 74 68 20 o populate with
137dd 74 68 65 20 64 61 74 61 20 72 65 61 64 0a 2a 2a the data read.**
137de 20 20 20 20 20 20 66 72 6f 6d 20 74 68 65 20 73 from the s
137df 61 76 65 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c avepoint journal
137e0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e ..**.** If noCon
137e1 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68 tent is true, th
137e2 65 6e 20 74 68 65 20 64 61 74 61 20 72 65 74 75 en the data retu
137e3 72 6e 65 64 20 69 73 20 7a 65 72 6f 65 64 20 69 rned is zeroed i
137e4 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 62 65 69 nstead of.** bei
137e5 6e 67 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 ng read from the
137e6 20 64 61 74 61 62 61 73 65 2e 20 41 64 64 69 74 database. Addit
137e7 69 6f 6e 61 6c 6c 79 2c 20 74 68 65 20 62 69 74 ionally, the bit
137e8 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a s corresponding.
137e9 2a 2a 20 74 6f 20 70 67 6e 6f 20 69 6e 20 50 61 ** to pgno in Pa
137ea 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 28 ger.pInJournal (
137eb 62 69 74 76 65 63 20 6f 66 20 70 61 67 65 73 20 bitvec of pages
137ec 61 6c 72 65 61 64 79 20 77 72 69 74 74 65 6e 20 already written
137ed 74 6f 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 to the.** journa
137ee 6c 20 66 69 6c 65 29 20 61 6e 64 20 74 68 65 20 l file) and the
137ef 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 PagerSavepoint.p
137f0 49 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74 76 InSavepoint bitv
137f1 65 63 73 20 6f 66 20 61 6e 79 20 6f 70 65 6e 0a ecs of any open.
137f2 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 ** savepoints ar
137f3 65 20 73 65 74 2e 20 54 68 69 73 20 6d 65 61 6e e set. This mean
137f4 73 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 s if the page is
137f5 20 6d 61 64 65 20 77 72 69 74 61 62 6c 65 20 61 made writable a
137f6 74 20 61 6e 79 0a 2a 2a 20 70 6f 69 6e 74 20 69 t any.** point i
137f7 6e 20 74 68 65 20 66 75 74 75 72 65 2c 20 75 73 n the future, us
137f8 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 ing a call to sq
137f9 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
137fa 29 2c 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 0a ), its contents.
137fb 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6a ** will not be j
137fc 6f 75 72 6e 61 6c 65 64 2e 20 54 68 69 73 20 73 ournaled. This s
137fd 61 76 65 73 20 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 aves IO..**.** T
137fe 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6d he acquisition m
137ff 69 67 68 74 20 66 61 69 6c 20 66 6f 72 20 73 65 ight fail for se
13800 76 65 72 61 6c 20 72 65 61 73 6f 6e 73 2e 20 20 veral reasons.
13801 49 6e 20 61 6c 6c 20 63 61 73 65 73 2c 0a 2a 2a In all cases,.**
13802 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
13803 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 error code is re
13804 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 50 61 turned and *ppPa
13805 67 65 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c ge is set to NUL
13806 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 L..**.** See als
13807 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f o sqlite3PagerLo
13808 6f 6b 75 70 28 29 2e 20 20 42 6f 74 68 20 74 68 okup(). Both th
13809 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 64 20 4c is routine and L
1380a 6f 6f 6b 75 70 28 29 20 61 74 74 65 6d 70 74 0a ookup() attempt.
1380b 2a 2a 20 74 6f 20 66 69 6e 64 20 61 20 70 61 67 ** to find a pag
1380c 65 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f e in the in-memo
1380d 72 79 20 63 61 63 68 65 20 66 69 72 73 74 2e 20 ry cache first.
1380e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 If the page is
1380f 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 not already.** i
13810 6e 20 6d 65 6d 6f 72 79 2c 20 74 68 69 73 20 72 n memory, this r
13811 6f 75 74 69 6e 65 20 67 6f 65 73 20 74 6f 20 64 outine goes to d
13812 69 73 6b 20 74 6f 20 72 65 61 64 20 69 74 20 69 isk to read it i
13813 6e 20 77 68 65 72 65 61 73 20 4c 6f 6f 6b 75 70 n whereas Lookup
13814 28 29 0a 2a 2a 20 6a 75 73 74 20 72 65 74 75 72 ().** just retur
13815 6e 73 20 30 2e 20 20 54 68 69 73 20 72 6f 75 74 ns 0. This rout
13816 69 6e 65 20 61 63 71 75 69 72 65 73 20 61 20 72 ine acquires a r
13817 65 61 64 2d 6c 6f 63 6b 20 74 68 65 20 66 69 72 ead-lock the fir
13818 73 74 20 74 69 6d 65 20 69 74 0a 2a 2a 20 68 61 st time it.** ha
13819 73 20 74 6f 20 67 6f 20 74 6f 20 64 69 73 6b 2c s to go to disk,
1381a 20 61 6e 64 20 63 6f 75 6c 64 20 61 6c 73 6f 20 and could also
1381b 70 6c 61 79 62 61 63 6b 20 61 6e 20 6f 6c 64 20 playback an old
1381c 6a 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 journal if neces
1381d 73 61 72 79 2e 0a 2a 2a 20 53 69 6e 63 65 20 4c sary..** Since L
1381e 6f 6f 6b 75 70 28 29 20 6e 65 76 65 72 20 67 6f ookup() never go
1381f 65 73 20 74 6f 20 64 69 73 6b 2c 20 69 74 20 6e es to disk, it n
13820 65 76 65 72 20 68 61 73 20 74 6f 20 64 65 61 6c ever has to deal
13821 20 77 69 74 68 20 6c 6f 63 6b 73 0a 2a 2a 20 6f with locks.** o
13822 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e r journal files.
13823 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13824 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
13825 67 65 72 41 63 71 75 69 72 65 28 0a 20 20 50 61 gerAcquire(. Pa
13826 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 ger *pPager,
13827 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f /* The pager o
13828 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 pen on the datab
13829 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 ase file */. Pg
1382a 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 no pgno,
1382b 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 /* Page number
1382c 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20 20 44 to fetch */. D
1382d 62 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 bPage **ppPage,
1382e 20 20 20 2f 2a 20 57 72 69 74 65 20 61 20 70 6f /* Write a po
1382f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 inter to the pag
13830 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 e here */. int
13831 6e 6f 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 noContent
13832 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 /* Do not bother
13833 20 72 65 61 64 69 6e 67 20 63 6f 6e 74 65 6e 74 reading content
13834 20 66 72 6f 6d 20 64 69 73 6b 20 69 66 20 74 72 from disk if tr
13835 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 ue */.){. int r
13836 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 3b c;. PgHdr *pPg;
13837 0a 0a 20 20 61 73 73 65 72 74 28 20 61 73 73 65 .. assert( asse
13838 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70 rt_pager_state(p
13839 50 61 67 65 72 29 20 29 3b 0a 20 20 61 73 73 65 Pager) );. asse
1383a 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
1383b 65 3e 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 e>PAGER_UNLOCK )
1383c 3b 0a 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 ;.. if( pgno==0
1383d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
1383e 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
1383f 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 PT;. }.. /* If
13840 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
13841 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
13842 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f , return an erro
13843 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a r immediately. .
13844 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 ** Otherwise,
13845 72 65 71 75 65 73 74 20 74 68 65 20 70 61 67 65 request the page
13846 20 66 72 6f 6d 20 74 68 65 20 50 43 61 63 68 65 from the PCache
13847 20 6c 61 79 65 72 2e 20 2a 2f 0a 20 20 69 66 28 layer. */. if(
13848 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
13849 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 !=SQLITE_OK && p
1384a 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d Pager->errCode!=
1384b 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 SQLITE_FULL ){.
1384c 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e rc = pPager->
1384d 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 errCode;. }else
1384e 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1384f 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50 e3PcacheFetch(pP
13850 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 ager->pPCache, p
13851 67 6e 6f 2c 20 31 2c 20 70 70 50 61 67 65 29 3b gno, 1, ppPage);
13852 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d . }.. if( rc!=
13853 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13854 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 63 /* Either the c
13855 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 63 all to sqlite3Pc
13856 61 63 68 65 46 65 74 63 68 28 29 20 72 65 74 75 acheFetch() retu
13857 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 20 6f 72 rned an error or
13858 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 the. ** page
13859 72 20 77 61 73 20 61 6c 72 65 61 64 79 20 69 6e r was already in
1385a 20 74 68 65 20 65 72 72 6f 72 2d 73 74 61 74 65 the error-state
1385b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 when this funct
1385c 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a ion was called..
1385d 20 20 20 20 2a 2a 20 53 65 74 20 70 50 67 20 74 ** Set pPg t
1385e 6f 20 30 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 o 0 and jump to
1385f 74 68 65 20 65 78 63 65 70 74 69 6f 6e 20 68 61 the exception ha
13860 6e 64 6c 65 72 2e 20 20 2a 2f 0a 20 20 20 20 70 ndler. */. p
13861 50 67 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f Pg = 0;. goto
13862 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 pager_acquire_e
13863 72 72 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rr;. }. assert
13864 28 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 67 6e ( (*ppPage)->pgn
13865 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 o==pgno );. ass
13866 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 2d 3e ert( (*ppPage)->
13867 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 7c pPager==pPager |
13868 7c 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 | (*ppPage)->pPa
13869 67 65 72 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 ger==0 );.. if(
1386a 20 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 (*ppPage)->pPag
1386b 65 72 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 er ){. /* In
1386c 74 68 69 73 20 63 61 73 65 20 74 68 65 20 70 63 this case the pc
1386d 61 63 68 65 20 61 6c 72 65 61 64 79 20 63 6f 6e ache already con
1386e 74 61 69 6e 73 20 61 6e 20 69 6e 69 74 69 61 6c tains an initial
1386f 69 7a 65 64 20 63 6f 70 79 20 6f 66 0a 20 20 20 ized copy of.
13870 20 2a 2a 20 74 68 65 20 70 61 67 65 2e 20 52 65 ** the page. Re
13871 74 75 72 6e 20 77 69 74 68 6f 75 74 20 66 75 72 turn without fur
13872 74 68 65 72 20 61 64 6f 2e 20 20 2a 2f 0a 20 20 ther ado. */.
13873 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3c 3d assert( pgno<=
13874 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 26 PAGER_MAX_PGNO &
13875 26 20 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a & pgno!=PAGER_MJ
13876 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 3b _PGNO(pPager) );
13877 0a 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 . PAGER_INCR(
13878 70 50 61 67 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 pPager->nHit);.
13879 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1387a 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 _OK;.. }else{.
1387b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 /* The pager
1387c 63 61 63 68 65 20 68 61 73 20 63 72 65 61 74 65 cache has create
1387d 64 20 61 20 6e 65 77 20 70 61 67 65 2e 20 49 74 d a new page. It
1387e 73 20 63 6f 6e 74 65 6e 74 20 6e 65 65 64 73 20 s content needs
1387f 74 6f 20 0a 20 20 20 20 2a 2a 20 62 65 20 69 6e to . ** be in
13880 69 74 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 itialized. */.
13881 20 20 20 69 6e 74 20 6e 4d 61 78 3b 0a 0a 20 20 int nMax;..
13882 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 PAGER_INCR(pPa
13883 67 65 72 2d 3e 6e 4d 69 73 73 29 3b 0a 20 20 20 ger->nMiss);.
13884 20 70 50 67 20 3d 20 2a 70 70 50 61 67 65 3b 0a pPg = *ppPage;.
13885 20 20 20 20 70 50 67 2d 3e 70 50 61 67 65 72 20 pPg->pPager
13886 3d 20 70 50 61 67 65 72 3b 0a 0a 20 20 20 20 2f = pPager;.. /
13887 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 * The maximum pa
13888 67 65 20 6e 75 6d 62 65 72 20 69 73 20 32 5e 33 ge number is 2^3
13889 31 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 1. Return SQLITE
1388a 5f 43 4f 52 52 55 50 54 20 69 66 20 61 20 70 61 _CORRUPT if a pa
1388b 67 65 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 ge. ** number
1388c 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 greater than th
1388d 69 73 2c 20 6f 72 20 74 68 65 20 75 6e 75 73 65 is, or the unuse
1388e 64 20 6c 6f 63 6b 69 6e 67 2d 70 61 67 65 2c 20 d locking-page,
1388f 69 73 20 72 65 71 75 65 73 74 65 64 2e 20 2a 2f is requested. */
13890 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 . if( pgno>PA
13891 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 GER_MAX_PGNO ||
13892 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 pgno==PAGER_MJ_P
13893 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 GNO(pPager) ){.
13894 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
13895 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
13896 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f goto pager_
13897 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 acquire_err;.
13898 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c }.. rc = sql
13899 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
1389a 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 4d 61 78 nt(pPager, &nMax
1389b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
1389c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
1389d 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 goto pager_acq
1389e 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a uire_err;. }.
1389f 0a 20 20 20 20 69 66 28 20 6e 4d 61 78 3c 28 69 . if( nMax<(i
138a0 6e 74 29 70 67 6e 6f 20 7c 7c 20 4d 45 4d 44 42 nt)pgno || MEMDB
138a1 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b || noContent ){
138a2 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f 3e . if( pgno>
138a3 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 pPager->mxPgno )
138a4 7b 0a 09 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 {..rc = SQLITE_F
138a5 55 4c 4c 3b 0a 09 67 6f 74 6f 20 70 61 67 65 72 ULL;..goto pager
138a6 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 _acquire_err;.
138a7 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
138a8 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 noContent ){.
138a9 20 20 20 20 20 2f 2a 20 46 61 69 6c 75 72 65 20 /* Failure
138aa 74 6f 20 73 65 74 20 74 68 65 20 62 69 74 73 20 to set the bits
138ab 69 6e 20 74 68 65 20 49 6e 4a 6f 75 72 6e 61 6c in the InJournal
138ac 20 62 69 74 2d 76 65 63 74 6f 72 73 20 69 73 20 bit-vectors is
138ad 62 65 6e 69 67 6e 2e 0a 20 20 20 20 20 20 20 20 benign..
138ae 2a 2a 20 49 74 20 6d 65 72 65 6c 79 20 6d 65 61 ** It merely mea
138af 6e 73 20 74 68 61 74 20 77 65 20 6d 69 67 68 74 ns that we might
138b0 20 64 6f 20 73 6f 6d 65 20 65 78 74 72 61 20 77 do some extra w
138b1 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 61 ork to journal a
138b2 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 . ** pag
138b3 65 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 e that does not
138b4 6e 65 65 64 20 74 6f 20 62 65 20 6a 6f 75 72 6e need to be journ
138b5 61 6c 65 64 2e 20 20 4e 65 76 65 72 74 68 65 6c aled. Neverthel
138b6 65 73 73 2c 20 62 65 20 73 75 72 65 20 0a 20 20 ess, be sure .
138b7 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65 73 74 ** to test
138b8 20 74 68 65 20 63 61 73 65 20 77 68 65 72 65 20 the case where
138b9 61 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 20 6f a malloc error o
138ba 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 ccurs while tryi
138bb 6e 67 20 74 6f 20 73 65 74 20 0a 20 20 20 20 20 ng to set .
138bc 20 20 20 2a 2a 20 61 20 62 69 74 20 69 6e 20 61 ** a bit in a
138bd 20 62 69 74 20 76 65 63 74 6f 72 2e 0a 20 20 20 bit vector..
138be 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
138bf 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
138c0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
138c1 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 50 if( pgno<=pP
138c2 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
138c3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 45 ){. TE
138c4 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 STONLY( rc = ) s
138c5 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 qlite3BitvecSet(
138c6 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e pPager->pInJourn
138c7 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 al, pgno);.
138c8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 testcase( r
138c9 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 c==SQLITE_NOMEM
138ca 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
138cb 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 TESTONLY( r
138cc 63 20 3d 20 29 20 61 64 64 54 6f 53 61 76 65 70 c = ) addToSavep
138cd 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 ointBitvecs(pPag
138ce 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 er, pgno);.
138cf 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d testcase( rc=
138d0 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b =SQLITE_NOMEM );
138d1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
138d2 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 EndBenignMalloc(
138d3 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a );. }else{.
138d4 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 memset(p
138d5 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 Pg->pData, 0, pP
138d6 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b ager->pageSize);
138d7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 49 . }. I
138d8 4f 54 52 41 43 45 28 28 22 5a 45 52 4f 20 25 70 OTRACE(("ZERO %p
138d9 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
138da 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 65 6c 73 pgno));. }els
138db 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 e{. assert(
138dc 20 70 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 pPg->pPager==pP
138dd 61 67 65 72 20 29 3b 0a 20 20 20 20 20 20 72 63 ager );. rc
138de 20 3d 20 72 65 61 64 44 62 50 61 67 65 28 70 50 = readDbPage(pP
138df 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 g);. if( rc
138e0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
138e1 20 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 goto page
138e2 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 r_acquire_err;.
138e3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 }. }.#if
138e4 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b def SQLITE_CHECK
138e5 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67 2d 3e _PAGES. pPg->
138e6 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 pageHash = pager
138e7 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a _pagehash(pPg);.
138e8 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 72 65 #endif. }.. re
138e9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
138ea 0a 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 .pager_acquire_e
138eb 72 72 3a 0a 20 20 61 73 73 65 72 74 28 20 72 63 rr:. assert( rc
138ec 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 !=SQLITE_OK );.
138ed 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 if( pPg ){.
138ee 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f sqlite3PcacheDro
138ef 70 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 70 61 p(pPg);. }. pa
138f0 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 gerUnlockIfUnuse
138f1 64 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2a 70 d(pPager);.. *p
138f2 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 72 65 74 pPage = 0;. ret
138f3 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
138f4 20 41 63 71 75 69 72 65 20 61 20 70 61 67 65 20 Acquire a page
138f5 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79 if it is already
138f6 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 in the in-memor
138f7 79 20 63 61 63 68 65 2e 20 20 44 6f 0a 2a 2a 20 y cache. Do.**
138f8 6e 6f 74 20 72 65 61 64 20 74 68 65 20 70 61 67 not read the pag
138f9 65 20 66 72 6f 6d 20 64 69 73 6b 2e 20 20 52 65 e from disk. Re
138fa 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 turn a pointer t
138fb 6f 20 74 68 65 20 70 61 67 65 2c 0a 2a 2a 20 6f o the page,.** o
138fc 72 20 30 20 69 66 20 74 68 65 20 70 61 67 65 20 r 0 if the page
138fd 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 2e is not in cache.
138fe 20 41 6c 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 Also, return 0
138ff 69 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 72 if the .** pager
13900 20 69 73 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c is in PAGER_UNL
13901 4f 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 OCK state when t
13902 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
13903 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f 72 20 69 66 called,.** or if
13904 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e the pager is in
13905 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 an error state
13906 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 other than SQLIT
13907 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 E_FULL..**.** Se
13908 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 e also sqlite3Pa
13909 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 64 gerGet(). The d
1390a 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 ifference betwee
1390b 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a n this routine.*
1390c 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67 * and sqlite3Pag
1390d 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 20 erGet() is that
1390e 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74 _get() will go t
1390f 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72 o the disk and r
13910 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 ead.** in the pa
13911 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69 ge if the page i
13912 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e s not already in
13913 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f cache. This ro
13914 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73 utine.** returns
13915 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67 NULL if the pag
13916 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 e is not in cach
13917 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49 e or if a disk I
13918 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73 /O error .** has
13919 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a ever happened..
1391a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1391b 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 E DbPage *sqlite
1391c 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 3PagerLookup(Pag
1391d 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f er *pPager, Pgno
1391e 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20 pgno){. PgHdr
1391f 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65 *pPg = 0;. asse
13920 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b rt( pPager!=0 );
13921 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21 . assert( pgno!
13922 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
13923 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 21 pPager->pPCache!
13924 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
13925 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3e 20 pPager->state >
13926 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a PAGER_UNLOCK );.
13927 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 sqlite3PcacheF
13928 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 etch(pPager->pPC
13929 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 ache, pgno, 0, &
1392a 70 50 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 pPg);. return p
1392b 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c Pg;.}../*.** Rel
1392c 65 61 73 65 20 61 20 70 61 67 65 20 72 65 66 65 ease a page refe
1392d 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 rence..**.** If
1392e 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 the number of re
1392f 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
13930 70 61 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72 page drop to zer
13931 6f 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70 o, then the.** p
13932 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 age is added to
13933 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57 the LRU list. W
13934 68 65 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63 hen all referenc
13935 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a es to all pages.
13936 2a 2a 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c ** are released,
13937 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 a rollback occu
13938 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20 rs and the lock
13939 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
1393a 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a is.** removed..*
1393b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1393c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 void sqlite3Pag
1393d 65 72 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a erUnref(DbPage *
1393e 70 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 pPg){. if( pPg
1393f 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 ){. Pager *pP
13940 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 ager = pPg->pPag
13941 65 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 er;. sqlite3P
13942 63 61 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 cacheRelease(pPg
13943 29 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f );. pagerUnlo
13944 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 ckIfUnused(pPage
13945 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a r);. }.}../*.**
13946 20 49 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 If the main jou
13947 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 61 6c rnal file has al
13948 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65 ready been opene
13949 64 2c 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 d, ensure that t
1394a 68 65 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 he.** sub-journa
1394b 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 20 74 l file is open t
1394c 6f 6f 2e 20 49 66 20 74 68 65 20 6d 61 69 6e 20 oo. If the main
1394d 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 6f journal is not o
1394e 70 65 6e 2c 0a 2a 2a 20 74 68 69 73 20 66 75 6e pen,.** this fun
1394f 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
13950 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f ..**.** SQLITE_O
13951 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 K is returned if
13952 20 65 76 65 72 79 74 68 69 6e 67 20 67 6f 65 73 everything goes
13953 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c according to pl
13954 61 6e 2e 20 0a 2a 2a 20 41 6e 20 53 51 4c 49 54 an. .** An SQLIT
13955 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72 6f E_IOERR_XXX erro
13956 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e r code is return
13957 65 64 20 69 66 20 61 20 63 61 6c 6c 20 74 6f 20 ed if a call to
13958 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 .** sqlite3OsOpe
13959 6e 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 n() fails..*/.st
1395a 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 53 75 62 atic int openSub
1395b 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 Journal(Pager *p
1395c 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
1395d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1395e 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 if( isOpen(pPage
1395f 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73 4f 70 r->jfd) && !isOp
13960 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 en(pPager->sjfd)
13961 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 ){. if( pPag
13962 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d er->journalMode=
13963 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f =PAGER_JOURNALMO
13964 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c 20 70 50 61 DE_MEMORY || pPa
13965 67 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 ger->subjInMemor
13966 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 y ){. sqlit
13967 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e e3MemJournalOpen
13968 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a (pPager->sjfd);.
13969 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1396a 20 72 63 20 3d 20 70 61 67 65 72 4f 70 65 6e 74 rc = pagerOpent
1396b 65 6d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 emp(pPager, pPag
1396c 65 72 2d 3e 73 6a 66 64 2c 20 53 51 4c 49 54 45 er->sjfd, SQLITE
1396d 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c _OPEN_SUBJOURNAL
1396e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
1396f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
13970 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
13971 20 69 73 20 63 61 6c 6c 65 64 20 61 74 20 74 68 is called at th
13972 65 20 73 74 61 72 74 20 6f 66 20 65 76 65 72 79 e start of every
13973 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
13974 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 on..** There mus
13975 74 20 61 6c 72 65 61 64 79 20 62 65 20 61 20 52 t already be a R
13976 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 ESERVED or EXCLU
13977 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 SIVE lock on the
13978 20 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 database .** fi
13979 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 le when this rou
1397a 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a tine is called..
1397b 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 6a **.** Open the j
1397c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 ournal file for
1397d 70 61 67 65 72 20 70 50 61 67 65 72 20 61 6e 64 pager pPager and
1397e 20 77 72 69 74 65 20 61 20 6a 6f 75 72 6e 61 6c write a journal
1397f 20 68 65 61 64 65 72 0a 2a 2a 20 74 6f 20 74 68 header.** to th
13980 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e 20 49 e start of it. I
13981 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 f there are acti
13982 76 65 20 73 61 76 65 70 6f 69 6e 74 73 2c 20 6f ve savepoints, o
13983 70 65 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 pen the sub-jour
13984 6e 61 6c 0a 2a 2a 20 61 73 20 77 65 6c 6c 2e 20 nal.** as well.
13985 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
13986 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20 only used when
13987 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
13988 20 69 73 20 62 65 69 6e 67 20 0a 2a 2a 20 6f 70 is being .** op
13989 65 6e 65 64 20 74 6f 20 77 72 69 74 65 20 61 20 ened to write a
1398a 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67 20 66 6f 72 rollback log for
1398b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 a transaction.
1398c 49 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 0a It is not used .
1398d 2a 2a 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 ** when opening
1398e 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 66 69 a hot journal fi
1398f 6c 65 20 74 6f 20 72 6f 6c 6c 20 69 74 20 62 61 le to roll it ba
13990 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ck..**.** If the
13991 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
13992 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20 28 61 already open (a
13993 73 20 69 74 20 6d 61 79 20 62 65 20 69 6e 20 65 s it may be in e
13994 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 2c 0a xclusive mode),.
13995 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e ** then this fun
13996 63 74 69 6f 6e 20 6a 75 73 74 20 77 72 69 74 65 ction just write
13997 73 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 s a journal head
13998 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 er to the start
13999 6f 66 20 74 68 65 0a 2a 2a 20 61 6c 72 65 61 64 of the.** alread
1399a 79 20 6f 70 65 6e 20 66 69 6c 65 2e 20 0a 2a 2a y open file. .**
1399b 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e .** Whether or n
1399c 6f 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 ot the journal f
1399d 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 62 79 ile is opened by
1399e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 this function,
1399f 74 68 65 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e the.** Pager.pIn
139a0 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 73 Journal bitvec s
139a1 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f tructure is allo
139a2 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 cated..**.** Ret
139a3 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 urn SQLITE_OK if
139a4 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 everything is s
139a5 75 63 63 65 73 73 66 75 6c 2e 20 4f 74 68 65 72 uccessful. Other
139a6 77 69 73 65 2c 20 72 65 74 75 72 6e 20 0a 2a 2a wise, return .**
139a7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 SQLITE_NOMEM if
139a8 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 the attempt to
139a9 61 6c 6c 6f 63 61 74 65 20 50 61 67 65 72 2e 70 allocate Pager.p
139aa 49 6e 4a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c InJournal fails,
139ab 20 6f 72 20 0a 2a 2a 20 61 6e 20 49 4f 20 65 72 or .** an IO er
139ac 72 6f 72 20 63 6f 64 65 20 69 66 20 6f 70 65 6e ror code if open
139ad 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 20 74 ing or writing t
139ae 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
139af 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 fails..*/.static
139b0 20 69 6e 74 20 70 61 67 65 72 5f 6f 70 65 6e 5f int pager_open_
139b1 6a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 journal(Pager *p
139b2 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
139b3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 = SQLITE_OK;
139b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139b5 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 /* Return c
139b6 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ode */. sqlite3
139b7 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 _vfs * const pVf
139b8 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 s = pPager->pVfs
139b9 3b 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 ; /* Local cac
139ba 68 65 20 6f 66 20 76 66 73 20 70 6f 69 6e 74 65 he of vfs pointe
139bb 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 r */.. assert(
139bc 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 pPager->state>=P
139bd 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b AGER_RESERVED );
139be 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
139bf 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b r->useJournal );
139c0 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
139c1 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d r->journalMode!=
139c2 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
139c3 45 5f 4f 46 46 20 29 3b 0a 20 20 61 73 73 65 72 E_OFF );. asser
139c4 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f t( pPager->pInJo
139c5 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 0a 20 urnal==0 );. .
139c6 20 2f 2a 20 49 66 20 61 6c 72 65 61 64 79 20 69 /* If already i
139c7 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 n the error stat
139c8 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e e, this function
139c9 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 is a no-op. Bu
139ca 74 20 6f 6e 0a 20 20 2a 2a 20 74 68 65 20 6f 74 t on. ** the ot
139cb 68 65 72 20 68 61 6e 64 2c 20 74 68 69 73 20 72 her hand, this r
139cc 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 outine is never
139cd 63 61 6c 6c 65 64 20 69 66 20 77 65 20 61 72 65 called if we are
139ce 20 61 6c 72 65 61 64 79 20 69 6e 0a 20 20 2a 2a already in. **
139cf 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2e an error state.
139d0 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 */. if( NEVER(
139d1 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 pPager->errCode)
139d2 20 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 ) return pPager
139d3 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a ->errCode;.. /*
139d4 20 54 4f 44 4f 3a 20 49 73 20 69 74 20 72 65 61 TODO: Is it rea
139d5 6c 6c 79 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 lly possible to
139d6 67 65 74 20 68 65 72 65 20 77 69 74 68 20 64 62 get here with db
139d7 53 69 7a 65 56 61 6c 69 64 3d 3d 30 3f 20 49 66 SizeValid==0? If
139d8 20 6e 6f 74 2c 0a 20 20 2a 2a 20 74 68 65 20 63 not,. ** the c
139d9 61 6c 6c 20 74 6f 20 50 61 67 65 72 50 61 67 65 all to PagerPage
139da 63 6f 75 6e 74 28 29 20 63 61 6e 20 62 65 20 72 count() can be r
139db 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 74 emoved.. */. t
139dc 65 73 74 63 61 73 65 28 20 70 50 61 67 65 72 2d estcase( pPager-
139dd 3e 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 >dbSizeValid==0
139de 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 );. sqlite3Page
139df 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 rPagecount(pPage
139e0 72 2c 20 30 29 3b 0a 0a 20 20 70 50 61 67 65 72 r, 0);.. pPager
139e1 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 ->pInJournal = s
139e2 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 qlite3BitvecCrea
139e3 74 65 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a te(pPager->dbSiz
139e4 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 e);. if( pPager
139e5 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 ->pInJournal==0
139e6 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
139e7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a LITE_NOMEM;. }.
139e8 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a . /* Open the j
139e9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 69 ournal file if i
139ea 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 t is not already
139eb 20 6f 70 65 6e 2e 20 2a 2f 0a 20 20 69 66 28 20 open. */. if(
139ec 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e !isOpen(pPager->
139ed 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20 jfd) ){. if(
139ee 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
139ef 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode==PAGER_JOURN
139f0 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b ALMODE_MEMORY ){
139f1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 . sqlite3Me
139f2 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 mJournalOpen(pPa
139f3 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d ger->jfd);. }
139f4 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 else{. cons
139f5 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 20 20 t int flags =
139f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
139f7 2f 2a 20 56 46 53 20 66 6c 61 67 73 20 74 6f 20 /* VFS flags to
139f8 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c open journal fil
139f9 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 51 4c e */. SQL
139fa 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 ITE_OPEN_READWRI
139fb 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 TE|SQLITE_OPEN_C
139fc 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20 28 REATE|. (
139fd 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 pPager->tempFile
139fe 20 3f 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 ? . (S
139ff 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 QLITE_OPEN_DELET
13a00 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c 49 54 45 5f EONCLOSE|SQLITE_
13a01 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 OPEN_TEMP_JOURNA
13a02 4c 29 3a 0a 20 20 20 20 20 20 20 20 20 20 28 53 L):. (S
13a03 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f QLITE_OPEN_MAIN_
13a04 4a 4f 55 52 4e 41 4c 29 0a 20 20 20 20 20 20 20 JOURNAL).
13a05 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 );.#ifdef SQLIT
13a06 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
13a07 57 52 49 54 45 0a 20 20 20 20 20 20 72 63 20 3d WRITE. rc =
13a08 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 4f sqlite3JournalO
13a09 70 65 6e 28 0a 20 20 20 20 20 20 20 20 20 20 70 pen(. p
13a0a 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f Vfs, pPager->zJo
13a0b 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a urnal, pPager->j
13a0c 66 64 2c 20 66 6c 61 67 73 2c 20 6a 72 6e 6c 42 fd, flags, jrnlB
13a0d 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 65 72 ufferSize(pPager
13a0e 29 0a 20 20 20 20 20 20 29 3b 0a 23 65 6c 73 65 ). );.#else
13a0f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
13a10 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 te3OsOpen(pVfs,
13a11 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c pPager->zJournal
13a12 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 , pPager->jfd, f
13a13 6c 61 67 73 2c 20 30 29 3b 0a 23 65 6e 64 69 66 lags, 0);.#endif
13a14 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
13a15 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b t( rc!=SQLITE_OK
13a16 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 || isOpen(pPage
13a17 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 7d 0a 0a r->jfd) );. }..
13a18 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 . /* Write the
13a19 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 first journal he
13a1a 61 64 65 72 20 74 6f 20 74 68 65 20 6a 6f 75 72 ader to the jour
13a1b 6e 61 6c 20 66 69 6c 65 20 61 6e 64 20 6f 70 65 nal file and ope
13a1c 6e 20 0a 20 20 2a 2a 20 74 68 65 20 73 75 62 2d n . ** the sub-
13a1d 6a 6f 75 72 6e 61 6c 20 69 66 20 6e 65 63 65 73 journal if neces
13a1e 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 sary.. */. if(
13a1f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
13a20 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 43 {. /* TODO: C
13a21 68 65 63 6b 20 69 66 20 61 6c 6c 20 6f 66 20 74 heck if all of t
13a22 68 65 73 65 20 61 72 65 20 72 65 61 6c 6c 79 20 hese are really
13a23 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 20 required. */.
13a24 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 pPager->dbOrigS
13a25 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 ize = pPager->db
13a26 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 65 72 Size;. pPager
13a27 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 ->journalStarted
13a28 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
13a29 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a ->needSync = 0;.
13a2a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 pPager->nRec
13a2b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 = 0;. pPager
13a2c 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 ->journalOff = 0
13a2d 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 ;. pPager->se
13a2e 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 tMaster = 0;.
13a2f 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13a30 48 64 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 Hdr = 0;. rc
13a31 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 = writeJournalHd
13a32 72 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 r(pPager);. }.
13a33 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
13a34 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 OK && pPager->nS
13a35 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 avepoint ){.
13a36 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 rc = openSubJour
13a37 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d nal(pPager);. }
13a38 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 .. if( rc!=SQLI
13a39 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c TE_OK ){. sql
13a3a 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f ite3BitvecDestro
13a3b 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 y(pPager->pInJou
13a3c 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 rnal);. pPage
13a3d 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 r->pInJournal =
13a3e 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
13a3f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 rc;.}../*.** Beg
13a40 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 in a write-trans
13a41 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 70 action on the sp
13a42 65 63 69 66 69 65 64 20 70 61 67 65 72 20 6f 62 ecified pager ob
13a43 6a 65 63 74 2e 20 49 66 20 61 20 0a 2a 2a 20 77 ject. If a .** w
13a44 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e rite-transaction
13a45 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
13a46 6e 20 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 66 n opened, this f
13a47 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d unction is a no-
13a48 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 op..**.** If the
13a49 20 65 78 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 exFlag argument
13a4a 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 is false, then
13a4b 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 73 74 acquire at least
13a4c 20 61 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c a RESERVED.** l
13a4d 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ock on the datab
13a4e 61 73 65 20 66 69 6c 65 2e 20 49 66 20 65 78 46 ase file. If exF
13a4f 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 lag is true, the
13a50 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c 65 61 n acquire at lea
13a51 73 74 0a 2a 2a 20 61 6e 20 45 58 43 4c 55 53 49 st.** an EXCLUSI
13a52 56 45 20 6c 6f 63 6b 2e 20 49 66 20 73 75 63 68 VE lock. If such
13a53 20 61 20 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 a lock is alrea
13a54 64 79 20 68 65 6c 64 2c 20 6e 6f 20 6c 6f 63 6b dy held, no lock
13a55 69 6e 67 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e ing .** function
13a56 73 20 6e 65 65 64 20 62 65 20 63 61 6c 6c 65 64 s need be called
13a57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 ..**.** If this
13a58 69 73 20 6e 6f 74 20 61 20 74 65 6d 70 6f 72 61 is not a tempora
13a59 72 79 20 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 ry or in-memory
13a5a 66 69 6c 65 20 61 6e 64 2c 20 74 68 65 20 6a 6f file and, the jo
13a5b 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 2a urnal file is .*
13a5c 2a 20 6f 70 65 6e 65 64 20 69 66 20 69 74 20 68 * opened if it h
13a5d 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 72 65 as not been alre
13a5e 61 64 79 2e 20 46 6f 72 20 61 20 74 65 6d 70 6f ady. For a tempo
13a5f 72 61 72 79 20 66 69 6c 65 2c 20 74 68 65 20 6f rary file, the o
13a60 70 65 6e 69 6e 67 20 0a 2a 2a 20 6f 66 20 74 68 pening .** of th
13a61 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
13a62 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c s deferred until
13a63 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 there is an act
13a64 75 61 6c 20 6e 65 65 64 20 74 6f 20 0a 2a 2a 20 ual need to .**
13a65 77 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 write to the jou
13a66 72 6e 61 6c 2e 20 54 4f 44 4f 3a 20 57 68 79 20 rnal. TODO: Why
13a67 68 61 6e 64 6c 65 20 74 65 6d 70 6f 72 61 72 79 handle temporary
13a68 20 66 69 6c 65 73 20 64 69 66 66 65 72 65 6e 74 files different
13a69 6c 79 3f 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ly?.**.** If the
13a6a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 journal file is
13a6b 20 6f 70 65 6e 65 64 20 28 6f 72 20 69 66 20 69 opened (or if i
13a6c 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 t is already ope
13a6d 6e 29 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 6a 6f n), then a.** jo
13a6e 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 urnal-header is
13a6f 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 written to the s
13a70 74 61 72 74 20 6f 66 20 69 74 2e 0a 2a 2a 0a 2a tart of it..**.*
13a71 2a 20 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d * If the subjInM
13a72 65 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 emory argument i
13a73 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e s non-zero, then
13a74 20 61 6e 79 20 73 75 62 2d 6a 6f 75 72 6e 61 6c any sub-journal
13a75 20 6f 70 65 6e 65 64 0a 2a 2a 20 77 69 74 68 69 opened.** withi
13a76 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 n this transacti
13a77 6f 6e 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 on will be opene
13a78 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 d as an in-memor
13a79 79 20 66 69 6c 65 2e 20 54 68 69 73 0a 2a 2a 20 y file. This.**
13a7a 68 61 73 20 6e 6f 20 65 66 66 65 63 74 20 69 66 has no effect if
13a7b 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
13a7c 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e is already open
13a7d 65 64 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 ed (as it may be
13a7e 20 77 68 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 when.** running
13a7f 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
13a80 64 65 29 20 6f 72 20 69 66 20 74 68 65 20 74 72 de) or if the tr
13a81 61 6e 73 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e ansaction does n
13a82 6f 74 20 72 65 71 75 69 72 65 20 61 0a 2a 2a 20 ot require a.**
13a83 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 sub-journal. If
13a84 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 the subjInMemory
13a85 20 61 72 67 75 6d 65 6e 74 20 69 73 20 7a 65 72 argument is zer
13a86 6f 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 o, then any requ
13a87 69 72 65 64 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 ired.** sub-jour
13a88 6e 61 6c 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 nal is implement
13a89 65 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 66 20 ed in-memory if
13a8a 70 50 61 67 65 72 20 69 73 20 61 6e 20 69 6e 2d pPager is an in-
13a8b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c memory database,
13a8c 20 0a 2a 2a 20 6f 72 20 75 73 69 6e 67 20 61 20 .** or using a
13a8d 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6f temporary file o
13a8e 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c therwise..*/.SQL
13a8f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
13a90 73 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 sqlite3PagerBegi
13a91 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c n(Pager *pPager,
13a92 20 69 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 int exFlag, int
13a93 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 7b 0a subjInMemory){.
13a94 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
13a95 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
13a96 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 pPager->state!=P
13a97 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 AGER_UNLOCK );.
13a98 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d pPager->subjInM
13a99 65 6d 6f 72 79 20 3d 20 28 75 38 29 73 75 62 6a emory = (u8)subj
13a9a 49 6e 4d 65 6d 6f 72 79 3b 0a 20 20 69 66 28 20 InMemory;. if(
13a9b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 pPager->state==P
13a9c 41 47 45 52 5f 53 48 41 52 45 44 20 29 7b 0a 20 AGER_SHARED ){.
13a9d 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13a9e 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 r->pInJournal==0
13a9f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
13aa0 21 4d 45 4d 44 42 20 26 26 20 21 70 50 61 67 65 !MEMDB && !pPage
13aa1 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 0a r->tempFile );..
13aa2 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 /* Obtain a
13aa3 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e RESERVED lock on
13aa4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13aa5 6c 65 2e 20 49 66 20 74 68 65 20 65 78 46 6c 61 le. If the exFla
13aa6 67 20 70 61 72 61 6d 65 74 65 72 0a 20 20 20 20 g parameter.
13aa7 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e ** is true, then
13aa8 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 67 immediately upg
13aa9 72 61 64 65 20 74 68 69 73 20 74 6f 20 61 6e 20 rade this to an
13aaa 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 EXCLUSIVE lock.
13aab 54 68 65 0a 20 20 20 20 2a 2a 20 62 75 73 79 2d The. ** busy-
13aac 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b handler callback
13aad 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 68 65 can be used whe
13aae 6e 20 75 70 67 72 61 64 69 6e 67 20 74 6f 20 74 n upgrading to t
13aaf 68 65 20 45 58 43 4c 55 53 49 56 45 0a 20 20 20 he EXCLUSIVE.
13ab0 20 2a 2a 20 6c 6f 63 6b 2c 20 62 75 74 20 6e 6f ** lock, but no
13ab1 74 20 77 68 65 6e 20 6f 62 74 61 69 6e 69 6e 67 t when obtaining
13ab2 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f the RESERVED lo
13ab3 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 ck.. */. r
13ab4 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 c = sqlite3OsLoc
13ab5 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 k(pPager->fd, RE
13ab6 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 SERVED_LOCK);.
13ab7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
13ab8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 _OK ){. pPa
13ab9 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 ger->state = PAG
13aba 45 52 5f 52 45 53 45 52 56 45 44 3b 0a 20 20 20 ER_RESERVED;.
13abb 20 20 20 69 66 28 20 65 78 46 6c 61 67 20 29 7b if( exFlag ){
13abc 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 . rc = pa
13abd 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b ger_wait_on_lock
13abe 28 70 50 61 67 65 72 2c 20 45 58 43 4c 55 53 49 (pPager, EXCLUSI
13abf 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 VE_LOCK);.
13ac0 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
13ac1 49 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 If the required
13ac2 6c 6f 63 6b 73 20 77 65 72 65 20 73 75 63 63 65 locks were succe
13ac3 73 73 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 ssfully obtained
13ac4 2c 20 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e , open the journ
13ac5 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 61 al. ** file a
13ac6 6e 64 20 77 72 69 74 65 20 74 68 65 20 66 69 72 nd write the fir
13ac7 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 st journal-heade
13ac8 72 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a r to it.. */.
13ac9 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
13aca 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d TE_OK && pPager-
13acb 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 >journalMode!=PA
13acc 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
13acd 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 72 63 20 OFF ){. rc
13ace 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 = pager_open_jou
13acf 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
13ad0 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 }. }else if(
13ad1 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a isOpen(pPager->j
13ad2 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6a fd) && pPager->j
13ad3 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a ournalOff==0 ){.
13ad4 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 /* This happ
13ad5 65 6e 73 20 77 68 65 6e 20 74 68 65 20 70 61 67 ens when the pag
13ad6 65 72 20 77 61 73 20 69 6e 20 65 78 63 6c 75 73 er was in exclus
13ad7 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 ive-access mode
13ad8 74 68 65 20 6c 61 73 74 0a 20 20 20 20 2a 2a 20 the last. **
13ad9 74 69 6d 65 20 61 20 28 72 65 61 64 20 6f 72 20 time a (read or
13ada 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 write) transacti
13adb 6f 6e 20 77 61 73 20 73 75 63 63 65 73 73 66 75 on was successfu
13adc 6c 6c 79 20 63 6f 6e 63 6c 75 64 65 64 0a 20 20 lly concluded.
13add 20 20 2a 2a 20 62 79 20 74 68 69 73 20 63 6f 6e ** by this con
13ade 6e 65 63 74 69 6f 6e 2e 20 49 6e 73 74 65 61 64 nection. Instead
13adf 20 6f 66 20 64 65 6c 65 74 69 6e 67 20 74 68 65 of deleting the
13ae0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 74 journal file it
13ae1 20 77 61 73 20 0a 20 20 20 20 2a 2a 20 6b 65 70 was . ** kep
13ae2 74 20 6f 70 65 6e 20 61 6e 64 20 65 69 74 68 65 t open and eithe
13ae3 72 20 77 61 73 20 74 72 75 6e 63 61 74 65 64 20 r was truncated
13ae4 74 6f 20 30 20 62 79 74 65 73 20 6f 72 20 69 74 to 0 bytes or it
13ae5 73 20 68 65 61 64 65 72 20 77 61 73 0a 20 20 20 s header was.
13ae6 20 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 ** overwritten
13ae7 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 20 20 with zeros..
13ae8 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
13ae9 50 61 67 65 72 2d 3e 6e 52 65 63 3d 3d 30 20 29 Pager->nRec==0 )
13aea 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
13aeb 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 ager->dbOrigSize
13aec 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ==0 );. asser
13aed 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f t( pPager->pInJo
13aee 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 urnal==0 );.
13aef 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f rc = pager_open_
13af0 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b journal(pPager);
13af1 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 . }.. PAGERTRA
13af2 43 45 28 28 22 54 52 41 4e 53 41 43 54 49 4f 4e CE(("TRANSACTION
13af3 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 %d\n", PAGERID(
13af4 70 50 61 67 65 72 29 29 29 3b 0a 20 20 61 73 73 pPager)));. ass
13af5 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 ert( !isOpen(pPa
13af6 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 ger->jfd) || pPa
13af7 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e ger->journalOff>
13af8 30 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 0 || rc!=SQLITE_
13af9 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d OK );. if( rc!=
13afa 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13afb 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 assert( !pPager
13afc 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a ->dbModified );.
13afd 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 61 6e /* Ignore an
13afe 79 20 49 4f 20 65 72 72 6f 72 20 74 68 61 74 20 y IO error that
13aff 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 70 61 occurs within pa
13b00 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
13b01 69 6f 6e 28 29 2e 20 54 68 65 0a 20 20 20 20 2a ion(). The. *
13b02 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 * purpose of thi
13b03 73 20 63 61 6c 6c 20 69 73 20 74 6f 20 72 65 73 s call is to res
13b04 65 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 et the internal
13b05 73 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 67 state of the pag
13b06 65 72 0a 20 20 20 20 2a 2a 20 73 75 62 2d 73 79 er. ** sub-sy
13b07 73 74 65 6d 2e 20 49 74 20 64 6f 65 73 6e 27 74 stem. It doesn't
13b08 20 6d 61 74 74 65 72 20 69 66 20 74 68 65 20 6a matter if the j
13b09 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69 73 20 6e ournal-file is n
13b0a 6f 74 20 70 72 6f 70 65 72 6c 79 0a 20 20 20 20 ot properly.
13b0b 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 61 74 20 ** finalized at
13b0c 74 68 69 73 20 70 6f 69 6e 74 20 28 73 69 6e 63 this point (sinc
13b0d 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20 76 61 e it is not a va
13b0e 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 lid journal file
13b0f 20 61 6e 79 77 61 79 29 2e 0a 20 20 20 20 2a 2f anyway).. */
13b10 0a 20 20 20 20 70 61 67 65 72 5f 65 6e 64 5f 74 . pager_end_t
13b11 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 ransaction(pPage
13b12 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 r, 0);. }. ret
13b13 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13b14 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 64 Mark a single d
13b15 61 74 61 20 70 61 67 65 20 61 73 20 77 72 69 74 ata page as writ
13b16 65 61 62 6c 65 2e 20 54 68 65 20 70 61 67 65 20 eable. The page
13b17 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 is written into
13b18 74 68 65 20 0a 2a 2a 20 6d 61 69 6e 20 6a 6f 75 the .** main jou
13b19 72 6e 61 6c 20 6f 72 20 73 75 62 2d 6a 6f 75 72 rnal or sub-jour
13b1a 6e 61 6c 20 61 73 20 72 65 71 75 69 72 65 64 2e nal as required.
13b1b 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 If the page is
13b1c 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 written into.**
13b1d 6f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e one of the journ
13b1e 61 6c 73 2c 20 74 68 65 20 63 6f 72 72 65 73 70 als, the corresp
13b1f 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20 73 65 onding bit is se
13b20 74 20 69 6e 20 74 68 65 20 0a 2a 2a 20 50 61 67 t in the .** Pag
13b21 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 er.pInJournal bi
13b22 74 76 65 63 20 61 6e 64 20 74 68 65 20 50 61 67 tvec and the Pag
13b23 65 72 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 erSavepoint.pInS
13b24 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 avepoint bitvecs
13b25 0a 2a 2a 20 6f 66 20 61 6e 79 20 6f 70 65 6e 20 .** of any open
13b26 73 61 76 65 70 6f 69 6e 74 73 20 61 73 20 61 70 savepoints as ap
13b27 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 propriate..*/.st
13b28 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 atic int pager_w
13b29 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67 29 rite(PgHdr *pPg)
13b2a 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 {. void *pData
13b2b 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 = pPg->pData;.
13b2c 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 Pager *pPager =
13b2d 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 pPg->pPager;. i
13b2e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
13b2f 4b 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f K;.. /* This ro
13b30 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 63 61 6c utine is not cal
13b31 6c 65 64 20 75 6e 6c 65 73 73 20 61 20 74 72 61 led unless a tra
13b32 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c 72 nsaction has alr
13b33 65 61 64 79 20 62 65 65 6e 0a 20 20 2a 2a 20 73 eady been. ** s
13b34 74 61 72 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 tarted.. */. a
13b35 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 ssert( pPager->s
13b36 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 tate>=PAGER_RESE
13b37 52 56 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 RVED );.. /* If
13b38 20 61 6e 20 65 72 72 6f 72 20 68 61 73 20 62 65 an error has be
13b39 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 64 65 en previously de
13b3a 74 65 63 74 65 64 2c 20 77 65 20 73 68 6f 75 6c tected, we shoul
13b3b 64 20 6e 6f 74 20 62 65 0a 20 20 2a 2a 20 63 61 d not be. ** ca
13b3c 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 lling this routi
13b3d 6e 65 2e 20 20 52 65 70 65 61 74 20 74 68 65 20 ne. Repeat the
13b3e 65 72 72 6f 72 20 66 6f 72 20 72 6f 62 75 73 74 error for robust
13b3f 6e 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ness.. */. if(
13b40 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 NEVER(pPager->e
13b41 72 72 43 6f 64 65 29 20 29 20 20 72 65 74 75 72 rrCode) ) retur
13b42 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 n pPager->errCod
13b43 65 3b 0a 0a 20 20 2f 2a 20 48 69 67 68 65 72 2d e;.. /* Higher-
13b44 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 6e level routines n
13b45 65 76 65 72 20 63 61 6c 6c 20 74 68 69 73 20 66 ever call this f
13b46 75 6e 63 74 69 6f 6e 20 69 66 20 64 61 74 61 62 unction if datab
13b47 61 73 65 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 ase is not. **
13b48 77 72 69 74 61 62 6c 65 2e 20 20 42 75 74 20 63 writable. But c
13b49 68 65 63 6b 20 61 6e 79 77 61 79 2c 20 6a 75 73 heck anyway, jus
13b4a 74 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 t for robustness
13b4b 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 . */. if( NEVER
13b4c 28 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c (pPager->readOnl
13b4d 79 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 y) ) return SQLI
13b4e 54 45 5f 50 45 52 4d 3b 0a 0a 20 20 61 73 73 65 TE_PERM;.. asse
13b4f 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 rt( !pPager->set
13b50 4d 61 73 74 65 72 20 29 3b 0a 0a 20 20 43 48 45 Master );.. CHE
13b51 43 4b 5f 50 41 47 45 28 70 50 67 29 3b 0a 0a 20 CK_PAGE(pPg);..
13b52 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61 67 /* Mark the pag
13b53 65 20 61 73 20 64 69 72 74 79 2e 20 20 49 66 20 e as dirty. If
13b54 74 68 65 20 70 61 67 65 20 68 61 73 20 61 6c 72 the page has alr
13b55 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 eady been writte
13b56 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 6a 6f n. ** to the jo
13b57 75 72 6e 61 6c 20 74 68 65 6e 20 77 65 20 63 61 urnal then we ca
13b58 6e 20 72 65 74 75 72 6e 20 72 69 67 68 74 20 61 n return right a
13b59 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 way.. */. sqli
13b5a 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72 te3PcacheMakeDir
13b5b 74 79 28 70 50 67 29 3b 0a 20 20 69 66 28 20 70 ty(pPg);. if( p
13b5c 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 ageInJournal(pPg
13b5d 29 20 26 26 20 21 73 75 62 6a 52 65 71 75 69 72 ) && !subjRequir
13b5e 65 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a 20 esPage(pPg) ){.
13b5f 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 pPager->dbMod
13b60 69 66 69 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c ified = 1;. }el
13b61 73 65 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 se{.. /* If w
13b62 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 e get this far,
13b63 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 it means that th
13b64 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f 20 e page needs to
13b65 62 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 74 65 be. ** writte
13b66 6e 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 n to the transac
13b67 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 tion journal or
13b68 74 68 65 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a the ckeckpoint j
13b69 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 ournal. ** or
13b6a 20 62 6f 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 both.. **.
13b6b 20 20 2a 2a 20 48 69 67 68 65 72 20 6c 65 76 65 ** Higher leve
13b6c 6c 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c l routines shoul
13b6d 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 73 d have already s
13b6e 74 61 72 74 65 64 20 61 20 74 72 61 6e 73 61 63 tarted a transac
13b6f 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a 20 77 68 69 tion,. ** whi
13b70 63 68 20 6d 65 61 6e 73 20 74 68 65 79 20 68 61 ch means they ha
13b71 76 65 20 61 63 71 75 69 72 65 64 20 74 68 65 20 ve acquired the
13b72 6e 65 63 65 73 73 61 72 79 20 6c 6f 63 6b 73 20 necessary locks
13b73 61 6e 64 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a and opened. *
13b74 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 * a rollback jou
13b75 72 6e 61 6c 2e 20 20 44 6f 75 62 6c 65 2d 63 68 rnal. Double-ch
13b76 65 63 6b 20 74 6f 20 6d 61 6b 65 73 20 73 75 72 eck to makes sur
13b77 65 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 e this is the ca
13b78 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 se.. */. r
13b79 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
13b7a 42 65 67 69 6e 28 70 50 61 67 65 72 2c 20 30 2c Begin(pPager, 0,
13b7b 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d pPager->subjInM
13b7c 65 6d 6f 72 79 29 3b 0a 20 20 20 20 69 66 28 20 emory);. if(
13b7d 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 NEVER(rc!=SQLITE
13b7e 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 _OK) ){. re
13b7f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 turn rc;. }.
13b80 20 20 20 69 66 28 20 21 69 73 4f 70 65 6e 28 70 if( !isOpen(p
13b81 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 70 Pager->jfd) && p
13b82 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f Pager->journalMo
13b83 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 de!=PAGER_JOURNA
13b84 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 LMODE_OFF ){.
13b85 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13b86 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b r->useJournal );
13b87 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
13b88 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 r_open_journal(p
13b89 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 Pager);. if
13b8a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13b8b 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
13b8c 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 }. pPager->d
13b8d 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20 bModified = 1;.
13b8e 20 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 72 61 . /* The tra
13b8f 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c nsaction journal
13b90 20 6e 6f 77 20 65 78 69 73 74 73 20 61 6e 64 20 now exists and
13b91 77 65 20 68 61 76 65 20 61 20 52 45 53 45 52 56 we have a RESERV
13b92 45 44 20 6f 72 20 61 6e 0a 20 20 20 20 2a 2a 20 ED or an. **
13b93 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f EXCLUSIVE lock o
13b94 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 n the main datab
13b95 61 73 65 20 66 69 6c 65 2e 20 20 57 72 69 74 65 ase file. Write
13b96 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 the current pag
13b97 65 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 e to. ** the
13b98 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 transaction jour
13b99 6e 61 6c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 nal if it is not
13b9a 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a there already..
13b9b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 */. if( !
13b9c 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 pageInJournal(pP
13b9d 67 29 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 g) && isOpen(pPa
13b9e 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
13b9f 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f if( pPg->pgno
13ba0 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 <=pPager->dbOrig
13ba1 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Size ){.
13ba2 75 33 32 20 63 6b 73 75 6d 3b 0a 20 20 20 20 20 u32 cksum;.
13ba3 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b char *pData2;
13ba4 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 .. /* We
13ba5 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 77 72 69 should never wri
13ba6 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 te to the journa
13ba7 6c 20 66 69 6c 65 20 74 68 65 20 70 61 67 65 20 l file the page
13ba8 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 that. **
13ba9 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 contains the dat
13baa 61 62 61 73 65 20 6c 6f 63 6b 73 2e 20 20 54 68 abase locks. Th
13bab 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 e following asse
13bac 72 74 20 76 65 72 69 66 69 65 73 0a 20 20 20 20 rt verifies.
13bad 20 20 20 20 2a 2a 20 74 68 61 74 20 77 65 20 64 ** that we d
13bae 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 o not. */.
13baf 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70 assert( pPg->p
13bb0 67 6e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 gno!=PAGER_MJ_PG
13bb1 4e 4f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 NO(pPager) );.
13bb2 20 20 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 CODEC2(pPa
13bb3 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d ger, pData, pPg-
13bb4 3e 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e >pgno, 7, return
13bb5 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 SQLITE_NOMEM, p
13bb6 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 20 20 Data2);.
13bb7 63 6b 73 75 6d 20 3d 20 70 61 67 65 72 5f 63 6b cksum = pager_ck
13bb8 73 75 6d 28 70 50 61 67 65 72 2c 20 28 75 38 2a sum(pPager, (u8*
13bb9 29 70 44 61 74 61 32 29 3b 0a 20 20 20 20 20 20 )pData2);.
13bba 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 rc = write32bi
13bbb 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 ts(pPager->jfd,
13bbc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13bbd 66 66 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a ff, pPg->pgno);.
13bbe 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d if( rc==
13bbf 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
13bc0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
13bc1 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 te3OsWrite(pPage
13bc2 72 2d 3e 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 r->jfd, pData2,
13bc3 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13bc4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
13bc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13bc6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f pPager->journalO
13bc7 66 66 20 2b 20 34 29 3b 0a 20 20 20 20 20 20 20 ff + 4);.
13bc8 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13bc9 61 6c 4f 66 66 20 2b 3d 20 70 50 61 67 65 72 2d alOff += pPager-
13bca 3e 70 61 67 65 53 69 7a 65 2b 34 3b 0a 20 20 20 >pageSize+4;.
13bcb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
13bcc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13bcd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
13bce 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 = write32bits(p
13bcf 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 Pager->jfd, pPag
13bd0 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 er->journalOff,
13bd1 63 6b 73 75 6d 29 3b 0a 20 20 20 20 20 20 20 20 cksum);.
13bd2 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 pPager->journa
13bd3 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20 20 20 20 20 lOff += 4;.
13bd4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 49 4f 54 }. IOT
13bd5 52 41 43 45 28 28 22 4a 4f 55 54 20 25 70 20 25 RACE(("JOUT %p %
13bd6 64 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 d %lld %d\n", pP
13bd7 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c ager, pPg->pgno,
13bd8 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
13bd9 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e pPager->journ
13bda 61 6c 4f 66 66 2c 20 70 50 61 67 65 72 2d 3e 70 alOff, pPager->p
13bdb 61 67 65 53 69 7a 65 29 29 3b 0a 20 20 20 20 20 ageSize));.
13bdc 20 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 PAGER_INCR(sq
13bdd 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 lite3_pager_writ
13bde 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 ej_count);.
13bdf 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 PAGERTRACE(("
13be0 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67 65 20 JOURNAL %d page
13be1 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 20 68 %d needSync=%d h
13be2 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 ash(%08x)\n",.
13be3 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 PAGER
13be4 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d ID(pPager), pPg-
13be5 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 >pgno, .
13be6 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 ((pPg->flag
13be7 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e s&PGHDR_NEED_SYN
13be8 43 29 3f 31 3a 30 29 2c 20 70 61 67 65 72 5f 70 C)?1:0), pager_p
13be9 61 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a agehash(pPg)));.
13bea 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 76 65 6e . /* Even
13beb 20 69 66 20 61 6e 20 49 4f 20 6f 72 20 64 69 73 if an IO or dis
13bec 6b 66 75 6c 6c 20 65 72 72 6f 72 20 6f 63 63 75 kfull error occu
13bed 72 72 65 64 20 77 68 69 6c 65 20 6a 6f 75 72 6e rred while journ
13bee 61 6c 6c 69 6e 67 20 74 68 65 0a 20 20 20 20 20 alling the.
13bef 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 ** page in th
13bf0 65 20 62 6c 6f 63 6b 20 61 62 6f 76 65 2c 20 73 e block above, s
13bf1 65 74 20 74 68 65 20 6e 65 65 64 2d 73 79 6e 63 et the need-sync
13bf2 20 66 6c 61 67 20 66 6f 72 20 74 68 65 20 70 61 flag for the pa
13bf3 67 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f ge.. ** O
13bf4 74 68 65 72 77 69 73 65 2c 20 77 68 65 6e 20 74 therwise, when t
13bf5 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 he transaction i
13bf6 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 s rolled back, t
13bf7 68 65 20 6c 6f 67 69 63 20 69 6e 0a 20 20 20 20 he logic in.
13bf8 20 20 20 20 2a 2a 20 70 6c 61 79 62 61 63 6b 5f ** playback_
13bf9 6f 6e 65 5f 70 61 67 65 28 29 20 77 69 6c 6c 20 one_page() will
13bfa 74 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 70 think that the p
13bfb 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 age needs to be
13bfc 72 65 73 74 6f 72 65 64 0a 20 20 20 20 20 20 20 restored.
13bfd 20 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 ** in the datab
13bfe 61 73 65 20 66 69 6c 65 2e 20 41 6e 64 20 69 66 ase file. And if
13bff 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 an IO error occ
13c00 75 72 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 urs while doing
13c01 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 so,. ** t
13c02 68 65 6e 20 63 6f 72 72 75 70 74 69 6f 6e 20 6d hen corruption m
13c03 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20 20 20 20 20 ay follow..
13c04 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 */. if
13c05 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e ( !pPager->noSyn
13c06 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 c ){. p
13c07 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 Pg->flags |= PGH
13c08 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
13c09 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e pPager->
13c0a 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 needSync = 1;.
13c0b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
13c0c 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 /* An error has
13c0d 20 6f 63 63 75 72 72 65 64 20 77 72 69 74 69 6e occurred writin
13c0e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c g to the journal
13c0f 20 66 69 6c 65 2e 20 54 68 65 20 0a 20 20 20 20 file. The .
13c10 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 ** transacti
13c11 6f 6e 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 on will be rolle
13c12 64 20 62 61 63 6b 20 62 79 20 74 68 65 20 6c 61 d back by the la
13c13 79 65 72 20 61 62 6f 76 65 2e 0a 20 20 20 20 20 yer above..
13c14 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 */. if
13c15 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
13c16 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 ){. ret
13c17 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 urn rc;.
13c18 7d 0a 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 }.. pPage
13c19 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 r->nRec++;.
13c1a 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
13c1b 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 r->pInJournal!=0
13c1c 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d );. rc =
13c1d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 sqlite3BitvecSe
13c1e 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 t(pPager->pInJou
13c1f 72 6e 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 rnal, pPg->pgno)
13c20 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
13c21 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e se( rc==SQLITE_N
13c22 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 OMEM );.
13c23 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
13c24 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c TE_OK || rc==SQL
13c25 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 ITE_NOMEM );.
13c26 20 20 20 20 20 72 63 20 7c 3d 20 61 64 64 54 6f rc |= addTo
13c27 53 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 SavepointBitvecs
13c28 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 (pPager, pPg->pg
13c29 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 no);. if(
13c2a 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
13c2b 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
13c2c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e rt( rc==SQLITE_N
13c2d 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 OMEM );.
13c2e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
13c2f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
13c30 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 se{. if(
13c31 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c !pPager->journal
13c32 53 74 61 72 74 65 64 20 26 26 20 21 70 50 61 67 Started && !pPag
13c33 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 er->noSync ){.
13c34 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 pPg->fla
13c35 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 gs |= PGHDR_NEED
13c36 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 _SYNC;.
13c37 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e pPager->needSyn
13c38 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d c = 1;. }
13c39 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 . PAGERTR
13c3a 41 43 45 28 28 22 41 50 50 45 4e 44 20 25 64 20 ACE(("APPEND %d
13c3b 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 page %d needSync
13c3c 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 =%d\n",.
13c3d 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 PAGERID(
13c3e 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 pPager), pPg->pg
13c3f 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 no,.
13c40 20 20 20 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 ((pPg->flags&
13c41 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 PGHDR_NEED_SYNC)
13c42 3f 31 3a 30 29 29 29 3b 0a 20 20 20 20 20 20 7d ?1:0)));. }
13c43 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
13c44 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e If the statemen
13c45 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 t journal is ope
13c46 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 n and the page i
13c47 73 20 6e 6f 74 20 69 6e 20 69 74 2c 0a 20 20 20 s not in it,.
13c48 20 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20 74 ** then write t
13c49 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 he current page
13c4a 74 6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 to the statement
13c4b 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 74 65 20 journal. Note
13c4c 74 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 that. ** the
13c4d 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 statement journa
13c4e 6c 20 66 6f 72 6d 61 74 20 64 69 66 66 65 72 73 l format differs
13c4f 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 from the standa
13c50 72 64 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 rd journal forma
13c51 74 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 61 74 t. ** in that
13c52 20 69 74 20 6f 6d 69 74 73 20 74 68 65 20 63 68 it omits the ch
13c53 65 63 6b 73 75 6d 73 20 61 6e 64 20 74 68 65 20 ecksums and the
13c54 68 65 61 64 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 header.. */.
13c55 20 20 20 69 66 28 20 73 75 62 6a 52 65 71 75 69 if( subjRequi
13c56 72 65 73 50 61 67 65 28 70 50 67 29 20 29 7b 0a resPage(pPg) ){.
13c57 20 20 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f rc = subjo
13c58 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a urnalPage(pPg);.
13c59 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
13c5a 55 70 64 61 74 65 20 74 68 65 20 64 61 74 61 62 Update the datab
13c5b 61 73 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74 ase size and ret
13c5c 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 urn.. */. asse
13c5d 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 rt( pPager->stat
13c5e 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 e>=PAGER_SHARED
13c5f 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d );. if( pPager-
13c60 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e >dbSize<pPg->pgn
13c61 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d o ){. pPager-
13c62 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70 >dbSize = pPg->p
13c63 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 gno;. }. retur
13c64 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d n rc;.}../*.** M
13c65 61 72 6b 20 61 20 64 61 74 61 20 70 61 67 65 20 ark a data page
13c66 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20 54 68 as writeable. Th
13c67 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 is routine must
13c68 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 be called before
13c69 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20 63 68 61 6e .** making chan
13c6a 67 65 73 20 74 6f 20 61 20 70 61 67 65 2e 20 54 ges to a page. T
13c6b 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 he caller must c
13c6c 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 heck the return
13c6d 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 74 68 69 value .** of thi
13c6e 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 62 s function and b
13c6f 65 20 63 61 72 65 66 75 6c 20 6e 6f 74 20 74 6f e careful not to
13c70 20 63 68 61 6e 67 65 20 61 6e 79 20 70 61 67 65 change any page
13c71 20 64 61 74 61 20 75 6e 6c 65 73 73 20 0a 2a 2a data unless .**
13c72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
13c73 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e turns SQLITE_OK.
13c74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 .**.** The diffe
13c75 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 rence between th
13c76 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 is function and
13c77 70 61 67 65 72 5f 77 72 69 74 65 28 29 20 69 73 pager_write() is
13c78 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 66 75 that this.** fu
13c79 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65 61 6c nction also deal
13c7a 73 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69 s with the speci
13c7b 61 6c 20 63 61 73 65 20 77 68 65 72 65 20 32 20 al case where 2
13c7c 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a 2a 2a or more pages.**
13c7d 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65 fit on a single
13c7e 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 49 6e disk sector. In
13c7f 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 63 this case all c
13c80 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67 65 73 o-resident pages
13c81 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65 .** must have be
13c82 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 en written to th
13c83 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 62 e journal file b
13c84 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e efore returning.
13c85 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 .**.** If an err
13c86 6f 72 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 or occurs, SQLIT
13c87 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 6e 20 49 4f E_NOMEM or an IO
13c88 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 error code is r
13c89 65 74 75 72 6e 65 64 0a 2a 2a 20 61 73 20 61 70 eturned.** as ap
13c8a 70 72 6f 70 72 69 61 74 65 2e 20 4f 74 68 65 72 propriate. Other
13c8b 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e wise, SQLITE_OK.
13c8c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13c8d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
13c8e 67 65 72 57 72 69 74 65 28 44 62 50 61 67 65 20 gerWrite(DbPage
13c8f 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69 6e 74 *pDbPage){. int
13c90 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13c91 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d .. PgHdr *pPg =
13c92 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61 67 65 pDbPage;. Page
13c93 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d r *pPager = pPg-
13c94 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e 6f 20 >pPager;. Pgno
13c95 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 20 3d nPagePerSector =
13c96 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 (pPager->sector
13c97 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70 61 67 Size/pPager->pag
13c98 65 53 69 7a 65 29 3b 0a 0a 20 20 69 66 28 20 6e eSize);.. if( n
13c99 50 61 67 65 50 65 72 53 65 63 74 6f 72 3e 31 20 PagePerSector>1
13c9a 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 ){. Pgno nPag
13c9b 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 eCount;
13c9c 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
13c9d 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64 61 74 of pages in dat
13c9e 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 abase file */.
13c9f 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 20 20 20 Pgno pg1;
13ca0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
13ca1 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 irst page of the
13ca2 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c sector pPg is l
13ca3 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a 20 20 ocated on. */.
13ca4 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 int nPage;
13ca5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
13ca6 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 umber of pages s
13ca7 74 61 72 74 69 6e 67 20 61 74 20 70 67 31 20 74 tarting at pg1 t
13ca8 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 o journal */.
13ca9 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 int ii;
13caa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
13cab 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
13cac 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d int needSync =
13cad 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 0; /* T
13cae 72 75 65 20 69 66 20 61 6e 79 20 70 61 67 65 20 rue if any page
13caf 68 61 73 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 has PGHDR_NEED_S
13cb0 59 4e 43 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 YNC */.. /* S
13cb1 65 74 20 74 68 65 20 64 6f 4e 6f 74 53 79 6e 63 et the doNotSync
13cb2 20 66 6c 61 67 20 74 6f 20 31 2e 20 54 68 69 73 flag to 1. This
13cb3 20 69 73 20 62 65 63 61 75 73 65 20 77 65 20 63 is because we c
13cb4 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 6a 6f annot allow a jo
13cb5 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 68 65 61 urnal. ** hea
13cb6 64 65 72 20 74 6f 20 62 65 20 77 72 69 74 74 65 der to be writte
13cb7 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 70 61 n between the pa
13cb8 67 65 73 20 6a 6f 75 72 6e 61 6c 65 64 20 62 79 ges journaled by
13cb9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
13cba 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 */. asser
13cbb 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 t( !MEMDB );.
13cbc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
13cbd 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 30 20 29 3b >doNotSync==0 );
13cbe 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e . pPager->doN
13cbf 6f 74 53 79 6e 63 20 3d 20 31 3b 0a 0a 20 20 20 otSync = 1;..
13cc0 20 2f 2a 20 54 68 69 73 20 74 72 69 63 6b 20 61 /* This trick a
13cc1 73 73 75 6d 65 73 20 74 68 61 74 20 62 6f 74 68 ssumes that both
13cc2 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 the page-size a
13cc3 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 61 nd sector-size a
13cc4 72 65 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 re. ** an int
13cc5 65 67 65 72 20 70 6f 77 65 72 20 6f 66 20 32 2e eger power of 2.
13cc6 20 49 74 20 73 65 74 73 20 76 61 72 69 61 62 6c It sets variabl
13cc7 65 20 70 67 31 20 74 6f 20 74 68 65 20 69 64 65 e pg1 to the ide
13cc8 6e 74 69 66 69 65 72 0a 20 20 20 20 2a 2a 20 6f ntifier. ** o
13cc9 66 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 f the first page
13cca 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 70 of the sector p
13ccb 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e Pg is located on
13ccc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 67 31 .. */. pg1
13ccd 20 3d 20 28 28 70 50 67 2d 3e 70 67 6e 6f 2d 31 = ((pPg->pgno-1
13cce 29 20 26 20 7e 28 6e 50 61 67 65 50 65 72 53 65 ) & ~(nPagePerSe
13ccf 63 74 6f 72 2d 31 29 29 20 2b 20 31 3b 0a 0a 20 ctor-1)) + 1;..
13cd0 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 sqlite3PagerP
13cd1 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c agecount(pPager,
13cd2 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 65 43 6f (int *)&nPageCo
13cd3 75 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 70 50 unt);. if( pP
13cd4 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67 65 43 6f 75 g->pgno>nPageCou
13cd5 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 nt ){. nPag
13cd6 65 20 3d 20 28 70 50 67 2d 3e 70 67 6e 6f 20 2d e = (pPg->pgno -
13cd7 20 70 67 31 29 2b 31 3b 0a 20 20 20 20 7d 65 6c pg1)+1;. }el
13cd8 73 65 20 69 66 28 20 28 70 67 31 2b 6e 50 61 67 se if( (pg1+nPag
13cd9 65 50 65 72 53 65 63 74 6f 72 2d 31 29 3e 6e 50 ePerSector-1)>nP
13cda 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 ageCount ){.
13cdb 20 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 43 nPage = nPageC
13cdc 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a 20 20 20 20 ount+1-pg1;.
13cdd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 }else{. nPa
13cde 67 65 20 3d 20 6e 50 61 67 65 50 65 72 53 65 63 ge = nPagePerSec
13cdf 74 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 tor;. }. a
13ce0 73 73 65 72 74 28 6e 50 61 67 65 3e 30 29 3b 0a ssert(nPage>0);.
13ce1 20 20 20 20 61 73 73 65 72 74 28 70 67 31 3c 3d assert(pg1<=
13ce2 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 pPg->pgno);.
13ce3 61 73 73 65 72 74 28 28 70 67 31 2b 6e 50 61 67 assert((pg1+nPag
13ce4 65 29 3e 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a e)>pPg->pgno);..
13ce5 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 for(ii=0; ii
13ce6 3c 6e 50 61 67 65 20 26 26 20 72 63 3d 3d 53 51 <nPage && rc==SQ
13ce7 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b 2b 29 7b 0a LITE_OK; ii++){.
13ce8 20 20 20 20 20 20 50 67 6e 6f 20 70 67 20 3d 20 Pgno pg =
13ce9 70 67 31 2b 69 69 3b 0a 20 20 20 20 20 20 50 67 pg1+ii;. Pg
13cea 48 64 72 20 2a 70 50 61 67 65 3b 0a 20 20 20 20 Hdr *pPage;.
13ceb 20 20 69 66 28 20 70 67 3d 3d 70 50 67 2d 3e 70 if( pg==pPg->p
13cec 67 6e 6f 20 7c 7c 20 21 73 71 6c 69 74 65 33 42 gno || !sqlite3B
13ced 69 74 76 65 63 54 65 73 74 28 70 50 61 67 65 72 itvecTest(pPager
13cee 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 ->pInJournal, pg
13cef 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ) ){. if(
13cf0 20 70 67 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 pg!=PAGER_MJ_PG
13cf1 4e 4f 28 70 50 61 67 65 72 29 20 29 7b 0a 20 20 NO(pPager) ){.
13cf2 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
13cf3 69 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 ite3PagerGet(pPa
13cf4 67 65 72 2c 20 70 67 2c 20 26 70 50 61 67 65 29 ger, pg, &pPage)
13cf5 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
13cf6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
13cf7 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 . rc
13cf8 3d 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 50 = pager_write(pP
13cf9 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 age);.
13cfa 20 20 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 if( pPage->fla
13cfb 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 gs&PGHDR_NEED_SY
13cfc 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 NC ){.
13cfd 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 needSync = 1
13cfe 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
13cff 61 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e assert(pPager->n
13d00 65 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 20 20 eedSync);.
13d01 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
13d02 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
13d03 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 Unref(pPage);.
13d04 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
13d05 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 }. }else
13d06 69 66 28 20 28 70 50 61 67 65 20 3d 20 70 61 67 if( (pPage = pag
13d07 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 er_lookup(pPager
13d08 2c 20 70 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 , pg))!=0 ){.
13d09 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e if( pPage->
13d0a 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 flags&PGHDR_NEED
13d0b 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20 20 _SYNC ){.
13d0c 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b needSync = 1;
13d0d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13d0e 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 sqlite3PagerU
13d0f 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 nref(pPage);.
13d10 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
13d11 2f 2a 20 49 66 20 74 68 65 20 50 47 48 44 52 5f /* If the PGHDR_
13d12 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 NEED_SYNC flag i
13d13 73 20 73 65 74 20 66 6f 72 20 61 6e 79 20 6f 66 s set for any of
13d14 20 74 68 65 20 6e 50 61 67 65 20 70 61 67 65 73 the nPage pages
13d15 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69 6e . ** startin
13d16 67 20 61 74 20 70 67 31 2c 20 74 68 65 6e 20 69 g at pg1, then i
13d17 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 65 t needs to be se
13d18 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 t for all of the
13d19 6d 2e 20 42 65 63 61 75 73 65 0a 20 20 20 20 2a m. Because. *
13d1a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 * writing to any
13d1b 20 6f 66 20 74 68 65 73 65 20 6e 50 61 67 65 20 of these nPage
13d1c 70 61 67 65 73 20 6d 61 79 20 64 61 6d 61 67 65 pages may damage
13d1d 20 74 68 65 20 6f 74 68 65 72 73 2c 20 74 68 65 the others, the
13d1e 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 . ** journal
13d1f 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61 69 file must contai
13d20 6e 20 73 79 6e 63 28 29 65 64 20 63 6f 70 69 65 n sync()ed copie
13d21 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65 6d s of all of them
13d22 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 61 . ** before a
13d23 6e 79 20 6f 66 20 74 68 65 6d 20 63 61 6e 20 62 ny of them can b
13d24 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f e written out to
13d25 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13d26 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 le.. */. i
13d27 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
13d28 20 26 26 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a && needSync ){.
13d29 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d assert( !M
13d2a 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e EMDB && pPager->
13d2b 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 noSync==0 );.
13d2c 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c for(ii=0; ii<
13d2d 6e 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 nPage; ii++){.
13d2e 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 PgHdr *pPa
13d2f 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 ge = pager_looku
13d30 70 28 70 50 61 67 65 72 2c 20 70 67 31 2b 69 69 p(pPager, pg1+ii
13d31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 );. if( p
13d32 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Page ){.
13d33 20 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20 7c pPage->flags |
13d34 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e = PGHDR_NEED_SYN
13d35 43 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c C;. sql
13d36 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
13d37 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 7d Page);. }
13d38 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
13d39 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 6e 65 ssert(pPager->ne
13d3a 65 64 53 79 6e 63 29 3b 0a 20 20 20 20 7d 0a 0a edSync);. }..
13d3b 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
13d3c 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d 31 er->doNotSync==1
13d3d 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e );. pPager->
13d3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 30 3b 0a 20 doNotSync = 0;.
13d3f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d }else{. rc =
13d40 20 70 61 67 65 72 5f 77 72 69 74 65 28 70 44 62 pager_write(pDb
13d41 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 Page);. }. ret
13d42 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
13d43 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 Return TRUE if
13d44 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 20 69 the page given i
13d45 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 n the argument w
13d46 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 70 61 as previously pa
13d47 73 73 65 64 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 ssed.** to sqlit
13d48 65 33 50 61 67 65 72 57 72 69 74 65 28 29 2e 20 e3PagerWrite().
13d49 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
13d4a 20 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 return TRUE if
13d4b 69 74 20 69 73 20 6f 6b 0a 2a 2a 20 74 6f 20 63 it is ok.** to c
13d4c 68 61 6e 67 65 20 74 68 65 20 63 6f 6e 74 65 6e hange the conten
13d4d 74 20 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a t of the page..*
13d4e 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
13d4f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
13d50 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
13d51 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 Iswriteable(DbPa
13d52 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75 ge *pPg){. retu
13d53 72 6e 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 rn pPg->flags&PG
13d54 48 44 52 5f 44 49 52 54 59 3b 0a 7d 0a 23 65 6e HDR_DIRTY;.}.#en
13d55 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c dif..#ifndef SQL
13d56 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 ITE_SECURE_DELET
13d57 45 0a 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 E./*.** A call t
13d58 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 o this routine t
13d59 65 6c 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 ells the pager t
13d5a 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 hat it is not ne
13d5b 63 65 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 cessary to.** wr
13d5c 69 74 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 ite the informat
13d5d 69 6f 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 ion on page pPg
13d5e 62 61 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b back to the disk
13d5f 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a , even though.**
13d60 20 74 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 that page might
13d61 20 62 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 be marked as di
13d62 72 74 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 rty. This happe
13d63 6e 73 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c ns, for example,
13d64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 when.** the pag
13d65 65 20 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 e has been added
13d66 20 61 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 as a leaf of th
13d67 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 e freelist and s
13d68 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 o its.** content
13d69 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 no longer matte
13d6a 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 rs..**.** The ov
13d6b 65 72 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 erlying software
13d6c 20 6c 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 layer calls thi
13d6d 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 s routine when a
13d6e 6c 6c 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a ll of the data.*
13d6f 2a 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 * on the given p
13d70 61 67 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 age is unused. T
13d71 68 65 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 he pager marks t
13d72 68 65 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e he page as clean
13d73 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 so.** that it d
13d74 6f 65 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 oes not get writ
13d75 74 65 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a ten to disk..**.
13d76 2a 2a 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 ** Tests show th
13d77 61 74 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 at this optimiza
13d78 74 69 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 tion can quadrup
13d79 6c 65 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 le the speed of
13d7a 6c 61 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 large .** DELETE
13d7b 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a operations..*/.
13d7c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
13d7d 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 oid sqlite3Pager
13d7e 44 6f 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 DontWrite(PgHdr
13d7f 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a *pPg){. Pager *
13d80 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 pPager = pPg->pP
13d81 61 67 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 ager;. if( (pPg
13d82 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 ->flags&PGHDR_DI
13d83 52 54 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e RTY) && pPager->
13d84 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b nSavepoint==0 ){
13d85 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 . PAGERTRACE(
13d86 28 22 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 ("DONT_WRITE pag
13d87 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 e %d of %d\n", p
13d88 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 Pg->pgno, PAGERI
13d89 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 D(pPager)));.
13d8a 20 49 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e IOTRACE(("CLEAN
13d8b 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 %p %d\n", pPage
13d8c 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 r, pPg->pgno)).
13d8d 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d pPg->flags |=
13d8e 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 PGHDR_DONT_WRIT
13d8f 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 E;.#ifdef SQLITE
13d90 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 _CHECK_PAGES.
13d91 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d pPg->pageHash =
13d92 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 pager_pagehash(
13d93 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d pPg);.#endif. }
13d94 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
13d95 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 45 43 fined(SQLITE_SEC
13d96 55 52 45 5f 44 45 4c 45 54 45 29 20 2a 2f 0a 0a URE_DELETE) */..
13d97 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
13d98 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 ne is called to
13d99 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61 increment the va
13d9a 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 lue of the datab
13d9b 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61 ase file .** cha
13d9c 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f nge-counter, sto
13d9d 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 65 20 red as a 4-byte
13d9e 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 big-endian integ
13d9f 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a er starting at .
13da0 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 20 32 ** byte offset 2
13da1 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66 4 of the pager f
13da2 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 ile..**.** If th
13da3 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 e isDirectMode f
13da4 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 lag is zero, the
13da5 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 n this is done b
13da6 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71 y calling .** sq
13da7 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
13da8 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65 ) on page 1, the
13da9 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20 n modifying the
13daa 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a contents of the.
13dab 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e ** page data. In
13dac 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 66 this case the f
13dad 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61 ile will be upda
13dae 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 72 ted when the cur
13daf 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 rent.** transact
13db0 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 ion is committed
13db1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69 ..**.** The isDi
13db2 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61 rectMode flag ma
13db3 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65 y only be non-ze
13db4 72 6f 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 ro if the librar
13db5 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a y was compiled.*
13db6 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 * with the SQLIT
13db7 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f E_ENABLE_ATOMIC_
13db8 57 52 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69 WRITE macro defi
13db9 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 ned. In this cas
13dba 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63 e,.** if isDirec
13dbb 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 t is non-zero, t
13dbc 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 hen the database
13dbd 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 file is updated
13dbe 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 directly.** by
13dbf 77 72 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74 writing an updat
13dc0 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61 ed version of pa
13dc1 67 65 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c ge 1 using a cal
13dc2 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c l to the .** sql
13dc3 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 66 75 ite3OsWrite() fu
13dc4 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 nction..*/.stati
13dc5 63 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 c int pager_incr
13dc6 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 _changecounter(P
13dc7 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
13dc8 74 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b t isDirectMode){
13dc9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
13dca 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63 TE_OK;.. /* Dec
13dcb 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c lare and initial
13dcc 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 ize constant int
13dcd 65 67 65 72 20 27 69 73 44 69 72 65 63 74 27 2e eger 'isDirect'.
13dce 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f If the. ** ato
13dcf 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 mic-write optimi
13dd0 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 zation is enable
13dd1 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c d in this build,
13dd2 20 74 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20 then isDirect.
13dd3 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a ** is initializ
13dd4 65 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 ed to the value
13dd5 70 61 73 73 65 64 20 61 73 20 74 68 65 20 69 73 passed as the is
13dd6 44 69 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d DirectMode param
13dd7 65 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69 eter. ** to thi
13dd8 73 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65 s function. Othe
13dd9 72 77 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77 rwise, it is alw
13dda 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e ays set to zero.
13ddb 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69 . **. ** The i
13ddc 64 65 61 20 69 73 20 74 68 61 74 20 69 66 20 74 dea is that if t
13ddd 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 he atomic-write
13dde 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 optimization is
13ddf 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64 not. ** enabled
13de0 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 at compile time
13de1 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 , the compiler c
13de2 61 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74 an omit the test
13de3 73 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72 s of. ** 'isDir
13de4 65 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77 ect' below, as w
13de5 65 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b ell as the block
13de6 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65 enclosed in the
13de7 0a 20 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72 . ** "if( isDir
13de8 65 63 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e ect )" condition
13de9 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 .. */.#ifndef S
13dea 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f QLITE_ENABLE_ATO
13deb 4d 49 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69 MIC_WRITE.# defi
13dec 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30 ne DIRECT_MODE 0
13ded 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 69 72 . assert( isDir
13dee 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20 ectMode==0 );.
13def 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
13df0 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a (isDirectMode);.
13df1 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44 #else.# define D
13df2 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72 IRECT_MODE isDir
13df3 65 63 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a ectMode.#endif..
13df4 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13df5 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 ->state>=PAGER_R
13df6 45 53 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28 ESERVED );. if(
13df7 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 !pPager->change
13df8 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 70 50 61 CountDone && pPa
13df9 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b ger->dbSize>0 ){
13dfa 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 48 . PgHdr *pPgH
13dfb 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
13dfc 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 /* Reference
13dfd 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 20 to page 1 */.
13dfe 20 75 33 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e u32 change_coun
13dff 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f ter; /
13e00 2a 20 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 * Initial value
13e01 6f 66 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 of change-counte
13e02 72 20 66 69 65 6c 64 20 2a 2f 0a 0a 20 20 20 20 r field */..
13e03 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d assert( !pPager-
13e04 3e 74 65 6d 70 46 69 6c 65 20 26 26 20 69 73 4f >tempFile && isO
13e05 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 pen(pPager->fd)
13e06 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 );.. /* Open
13e07 70 61 67 65 20 31 20 6f 66 20 74 68 65 20 66 69 page 1 of the fi
13e08 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 le for writing.
13e09 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
13e0a 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 te3PagerGet(pPag
13e0b 65 72 2c 20 31 2c 20 26 70 50 67 48 64 72 29 3b er, 1, &pPgHdr);
13e0c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 . assert( pPg
13e0d 48 64 72 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 Hdr==0 || rc==SQ
13e0e 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 20 20 LITE_OK );..
13e0f 2f 2a 20 49 66 20 70 61 67 65 20 6f 6e 65 20 77 /* If page one w
13e10 61 73 20 66 65 74 63 68 65 64 20 73 75 63 63 65 as fetched succe
13e11 73 73 66 75 6c 6c 79 2c 20 61 6e 64 20 74 68 69 ssfully, and thi
13e12 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f s function is no
13e13 74 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 t. ** operati
13e14 6e 67 20 69 6e 20 64 69 72 65 63 74 2d 6d 6f 64 ng in direct-mod
13e15 65 2c 20 6d 61 6b 65 20 70 61 67 65 20 31 20 77 e, make page 1 w
13e16 72 69 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6e ritable. When n
13e17 6f 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 64 69 ot in . ** di
13e18 72 65 63 74 20 6d 6f 64 65 2c 20 70 61 67 65 20 rect mode, page
13e19 31 20 69 73 20 61 6c 77 61 79 73 20 68 65 6c 64 1 is always held
13e1a 20 69 6e 20 63 61 63 68 65 20 61 6e 64 20 68 65 in cache and he
13e1b 6e 63 65 20 74 68 65 20 50 61 67 65 72 47 65 74 nce the PagerGet
13e1c 28 29 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20 (). ** above
13e1d 69 73 20 61 6c 77 61 79 73 20 73 75 63 63 65 73 is always succes
13e1e 73 66 75 6c 20 2d 20 68 65 6e 63 65 20 74 68 65 sful - hence the
13e1f 20 41 4c 57 41 59 53 20 6f 6e 20 72 63 3d 3d 53 ALWAYS on rc==S
13e20 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20 20 20 2a 2f QLITE_OK.. */
13e21 0a 20 20 20 20 69 66 28 20 21 44 49 52 45 43 54 . if( !DIRECT
13e22 5f 4d 4f 44 45 20 26 26 20 41 4c 57 41 59 53 28 _MODE && ALWAYS(
13e23 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 rc==SQLITE_OK) )
13e24 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
13e25 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
13e26 50 67 48 64 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 PgHdr);. }..
13e27 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
13e28 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a E_OK ){. /*
13e29 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 Increment the v
13e2a 61 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 alue just read a
13e2b 6e 64 20 77 72 69 74 65 20 69 74 20 62 61 63 6b nd write it back
13e2c 20 74 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a to byte 24. */.
13e2d 20 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 change_cou
13e2e 6e 74 65 72 20 3d 20 73 71 6c 69 74 65 33 47 65 nter = sqlite3Ge
13e2f 74 34 62 79 74 65 28 28 75 38 2a 29 70 50 61 67 t4byte((u8*)pPag
13e30 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 3b er->dbFileVers);
13e31 0a 20 20 20 20 20 20 63 68 61 6e 67 65 5f 63 6f . change_co
13e32 75 6e 74 65 72 2b 2b 3b 0a 20 20 20 20 20 20 70 unter++;. p
13e33 75 74 33 32 62 69 74 73 28 28 28 63 68 61 72 2a ut32bits(((char*
13e34 29 70 50 67 48 64 72 2d 3e 70 44 61 74 61 29 2b )pPgHdr->pData)+
13e35 32 34 2c 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 24, change_count
13e36 65 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 er);.. /* I
13e37 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 64 69 72 f running in dir
13e38 65 63 74 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 ect mode, write
13e39 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
13e3a 70 61 67 65 20 31 20 74 6f 20 74 68 65 20 66 69 page 1 to the fi
13e3b 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 le. */. if(
13e3c 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 29 7b 0a DIRECT_MODE ){.
13e3d 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f const vo
13e3e 69 64 20 2a 7a 42 75 66 20 3d 20 70 50 67 48 64 id *zBuf = pPgHd
13e3f 72 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 20 20 r->pData;.
13e40 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
13e41 2d 3e 64 62 46 69 6c 65 53 69 7a 65 3e 30 20 29 ->dbFileSize>0 )
13e42 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 ;. rc = s
13e43 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 qlite3OsWrite(pP
13e44 61 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c 20 ager->fd, zBuf,
13e45 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 pPager->pageSize
13e46 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , 0);. if
13e47 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
13e48 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 ){. pPa
13e49 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 ger->changeCount
13e4a 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 Done = 1;.
13e4b 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b }. }else{
13e4c 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d . pPager-
13e4d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 >changeCountDone
13e4e 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 = 1;. }.
13e4f 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 }.. /* Rele
13e50 61 73 65 20 74 68 65 20 70 61 67 65 20 72 65 66 ase the page ref
13e51 65 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73 erence. */. s
13e52 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 qlite3PagerUnref
13e53 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 20 20 (pPgHdr);. }.
13e54 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
13e55 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 70 61 67 .** Sync the pag
13e56 65 72 20 66 69 6c 65 20 74 6f 20 64 69 73 6b 2e er file to disk.
13e57 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 This is a no-op
13e58 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 for in-memory f
13e59 69 6c 65 73 0a 2a 2a 20 6f 72 20 70 61 67 65 73 iles.** or pages
13e5a 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 2e with the Pager.
13e5b 6e 6f 53 79 6e 63 20 66 6c 61 67 20 73 65 74 2e noSync flag set.
13e5c 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 .**.** If succes
13e5d 73 66 75 6c 2c 20 6f 72 20 63 61 6c 6c 65 64 20 sful, or called
13e5e 6f 6e 20 61 20 70 61 67 65 72 20 66 6f 72 20 77 on a pager for w
13e5f 68 69 63 68 20 69 74 20 69 73 20 61 20 6e 6f 2d hich it is a no-
13e60 6f 70 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 op, this.** func
13e61 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c tion returns SQL
13e62 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 ITE_OK. Otherwis
13e63 65 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 e, an IO error c
13e64 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e ode is returned.
13e65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
13e66 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
13e67 67 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 gerSync(Pager *p
13e68 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 Pager){. int rc
13e69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
13e6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
13e6b 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f * Return code */
13e6c 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 . assert( !MEMD
13e6d 42 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 B );. if( pPage
13e6e 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 r->noSync ){.
13e6f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
13e70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
13e71 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
13e72 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 (pPager->fd, pPa
13e73 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 ger->sync_flags)
13e74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
13e75 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 c;.}../*.** Sync
13e76 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
13e77 6c 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 le for the pager
13e78 20 70 50 61 67 65 72 2e 20 7a 4d 61 73 74 65 72 pPager. zMaster
13e79 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e points to the n
13e7a 61 6d 65 0a 2a 2a 20 6f 66 20 61 20 6d 61 73 74 ame.** of a mast
13e7b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
13e7c 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 77 that should be w
13e7d 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 ritten into the
13e7e 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 6a 6f individual.** jo
13e7f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 7a 4d 61 73 urnal file. zMas
13e80 74 65 72 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2c ter may be NULL,
13e81 20 77 68 69 63 68 20 69 73 20 69 6e 74 65 72 70 which is interp
13e82 72 65 74 65 64 20 61 73 20 6e 6f 20 6d 61 73 74 reted as no mast
13e83 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 28 61 er.** journal (a
13e84 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 single database
13e85 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a 2a transaction)..*
13e86 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
13e87 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 3a 0a e ensures that:.
13e88 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 **.** * The da
13e89 74 61 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e tabase file chan
13e8a 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 75 70 ge-counter is up
13e8b 64 61 74 65 64 2c 0a 2a 2a 20 20 20 2a 20 74 68 dated,.** * th
13e8c 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e e journal is syn
13e8d 63 65 64 20 28 75 6e 6c 65 73 73 20 74 68 65 20 ced (unless the
13e8e 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 atomic-write opt
13e8f 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 imization is use
13e90 64 29 2c 0a 2a 2a 20 20 20 2a 20 61 6c 6c 20 64 d),.** * all d
13e91 69 72 74 79 20 70 61 67 65 73 20 61 72 65 20 77 irty pages are w
13e92 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 ritten to the da
13e93 74 61 62 61 73 65 20 66 69 6c 65 2c 20 0a 2a 2a tabase file, .**
13e94 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 * the databas
13e95 65 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 e file is trunca
13e96 74 65 64 20 28 69 66 20 72 65 71 75 69 72 65 64 ted (if required
13e97 29 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 74 68 ), and.** * th
13e98 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
13e99 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 synced. .**.** T
13e9a 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68 he only thing th
13e9b 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f at remains to co
13e9c 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 mmit the transac
13e9d 74 69 6f 6e 20 69 73 20 74 6f 20 66 69 6e 61 6c tion is to final
13e9e 69 7a 65 20 0a 2a 2a 20 28 64 65 6c 65 74 65 2c ize .** (delete,
13e9f 20 74 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 truncate or zer
13ea0 6f 20 74 68 65 20 66 69 72 73 74 20 70 61 72 74 o the first part
13ea1 20 6f 66 29 20 74 68 65 20 6a 6f 75 72 6e 61 6c of) the journal
13ea2 20 66 69 6c 65 20 28 6f 72 20 0a 2a 2a 20 64 65 file (or .** de
13ea3 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 lete the master
13ea4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 journal file if
13ea5 73 70 65 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a specified)..**.*
13ea6 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 7a * Note that if z
13ea7 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 Master==NULL, th
13ea8 69 73 20 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 is does not over
13ea9 77 72 69 74 65 20 61 20 70 72 65 76 69 6f 75 73 write a previous
13eaa 20 76 61 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 value.** passed
13eab 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 50 61 to an sqlite3Pa
13eac 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e gerCommitPhaseOn
13ead 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 e() call..**.**
13eae 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70 61 72 If the final par
13eaf 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e 63 20 ameter - noSync
13eb0 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 - is true, then
13eb1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
13eb2 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73 20 6e e itself.** is n
13eb3 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65 20 63 ot synced. The c
13eb4 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 aller must call
13eb5 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 sqlite3PagerSync
13eb6 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f 0a 2a () directly to.*
13eb7 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 * sync the datab
13eb8 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 ase file before
13eb9 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74 50 68 calling CommitPh
13eba 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65 6c 65 aseTwo() to dele
13ebb 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 te the.** journa
13ebc 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20 63 l file in this c
13ebd 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ase..*/.SQLITE_P
13ebe 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
13ebf 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
13ec0 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72 20 2a seOne(. Pager *
13ec1 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 pPager,
13ec2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
13ec3 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f r object */. co
13ec4 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
13ec5 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r, /*
13ec6 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 If not NULL, th
13ec7 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
13ec8 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e name */. int n
13ec9 6f 53 79 6e 63 20 20 20 20 20 20 20 20 20 20 20 oSync
13eca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
13ecb 75 65 20 74 6f 20 6f 6d 69 74 20 74 68 65 20 78 ue to omit the x
13ecc 53 79 6e 63 20 6f 6e 20 74 68 65 20 64 62 20 66 Sync on the db f
13ecd 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ile */.){. int
13ece 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 rc = SQLITE_OK;
13ecf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
13ed0 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
13ed1 20 2f 2a 20 54 68 65 20 64 62 4f 72 69 67 53 69 /* The dbOrigSi
13ed2 7a 65 20 69 73 20 6e 65 76 65 72 20 73 65 74 20 ze is never set
13ed3 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d if journal_mode=
13ed4 4f 46 46 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 OFF */. assert(
13ed5 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c pPager->journal
13ed6 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode!=PAGER_JOUR
13ed7 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 7c 7c 20 70 NALMODE_OFF || p
13ed8 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a Pager->dbOrigSiz
13ed9 65 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 e==0 );.. /* If
13eda 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 6f a prior error o
13edb 63 63 75 72 72 65 64 2c 20 74 68 69 73 20 72 6f ccurred, this ro
13edc 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 utine should not
13edd 20 62 65 20 63 61 6c 6c 65 64 2e 20 20 52 4f 4c be called. ROL
13ede 4c 42 41 43 4b 0a 20 20 2a 2a 20 69 73 20 74 68 LBACK. ** is th
13edf 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 e appropriate re
13ee0 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 sponse to an err
13ee1 6f 72 2c 20 6e 6f 74 20 43 4f 4d 4d 49 54 2e 20 or, not COMMIT.
13ee2 20 47 75 61 72 64 20 61 67 61 69 6e 73 74 0a 20 Guard against.
13ee3 20 2a 2a 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 ** coding error
13ee4 73 20 62 79 20 72 65 70 65 61 74 69 6e 67 20 74 s by repeating t
13ee5 68 65 20 70 72 69 6f 72 20 65 72 72 6f 72 2e 20 he prior error.
13ee6 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 */. if( NEVER(p
13ee7 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 Pager->errCode)
13ee8 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d ) return pPager-
13ee9 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 50 41 47 >errCode;.. PAG
13eea 45 52 54 52 41 43 45 28 28 22 44 41 54 41 42 41 ERTRACE(("DATABA
13eeb 53 45 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 SE SYNC: File=%s
13eec 20 7a 4d 61 73 74 65 72 3d 25 73 20 6e 53 69 7a zMaster=%s nSiz
13eed 65 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 e=%d\n", .
13eee 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d pPager->zFilenam
13eef 65 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 e, zMaster, pPag
13ef0 65 72 2d 3e 64 62 53 69 7a 65 29 29 3b 0a 0a 20 er->dbSize));..
13ef1 20 69 66 28 20 4d 45 4d 44 42 20 26 26 20 70 50 if( MEMDB && pP
13ef2 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 ager->dbModified
13ef3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
13ef4 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f is is an in-memo
13ef5 72 79 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 ry db, or no pag
13ef6 65 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 es have been wri
13ef7 74 74 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 tten to, or this
13ef8 0a 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e . ** function
13ef9 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
13efa 6e 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 n called, it is
13efb 6d 6f 73 74 6c 79 20 61 20 6e 6f 2d 6f 70 2e 20 mostly a no-op.
13efc 20 48 6f 77 65 76 65 72 2c 20 61 6e 79 0a 20 20 However, any.
13efd 20 20 2a 2a 20 62 61 63 6b 75 70 20 69 6e 20 70 ** backup in p
13efe 72 6f 67 72 65 73 73 20 6e 65 65 64 73 20 74 6f rogress needs to
13eff 20 62 65 20 72 65 73 74 61 72 74 65 64 2e 0a 20 be restarted..
13f00 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
13f01 33 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 3BackupRestart(p
13f02 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b Pager->pBackup);
13f03 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 . }else if( pPa
13f04 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 ger->state!=PAGE
13f05 52 5f 53 59 4e 43 45 44 20 26 26 20 70 50 61 67 R_SYNCED && pPag
13f06 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 er->dbModified )
13f07 7b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f {.. /* The fo
13f08 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 75 70 llowing block up
13f09 64 61 74 65 73 20 74 68 65 20 63 68 61 6e 67 65 dates the change
13f0a 2d 63 6f 75 6e 74 65 72 2e 20 45 78 61 63 74 6c -counter. Exactl
13f0b 79 20 68 6f 77 20 69 74 0a 20 20 20 20 2a 2a 20 y how it. **
13f0c 64 6f 65 73 20 74 68 69 73 20 64 65 70 65 6e 64 does this depend
13f0d 73 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 s on whether or
13f0e 6e 6f 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 not the atomic-u
13f0f 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 pdate optimizati
13f10 6f 6e 0a 20 20 20 20 2a 2a 20 77 61 73 20 65 6e on. ** was en
13f11 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 abled at compile
13f12 20 74 69 6d 65 2c 20 61 6e 64 20 69 66 20 74 68 time, and if th
13f13 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d is transaction m
13f14 65 65 74 73 20 74 68 65 20 0a 20 20 20 20 2a 2a eets the . **
13f15 20 72 75 6e 74 69 6d 65 20 63 72 69 74 65 72 69 runtime criteri
13f16 61 20 74 6f 20 75 73 65 20 74 68 65 20 6f 70 65 a to use the ope
13f17 72 61 74 69 6f 6e 3a 20 0a 20 20 20 20 2a 2a 0a ration: . **.
13f18 20 20 20 20 2a 2a 20 20 20 20 2a 20 54 68 65 20 ** * The
13f19 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 file-system supp
13f1a 6f 72 74 73 20 74 68 65 20 61 74 6f 6d 69 63 2d orts the atomic-
13f1b 77 72 69 74 65 20 70 72 6f 70 65 72 74 79 20 66 write property f
13f1c 6f 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 62 or. ** b
13f1d 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 70 61 locks of size pa
13f1e 67 65 2d 73 69 7a 65 2c 20 61 6e 64 20 0a 20 20 ge-size, and .
13f1f 20 20 2a 2a 20 20 20 20 2a 20 54 68 69 73 20 63 ** * This c
13f20 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20 70 61 72 ommit is not par
13f21 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d 66 69 6c t of a multi-fil
13f22 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 e transaction, a
13f23 6e 64 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 45 nd. ** * E
13f24 78 61 63 74 6c 79 20 6f 6e 65 20 70 61 67 65 20 xactly one page
13f25 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 has been modifie
13f26 64 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 74 d and store in t
13f27 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e he journal file.
13f28 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
13f29 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 f the optimizati
13f2a 6f 6e 20 77 61 73 20 6e 6f 74 20 65 6e 61 62 6c on was not enabl
13f2b 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 ed at compile ti
13f2c 6d 65 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 me, then the.
13f2d 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 ** pager_incr_c
13f2e 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 66 hangecounter() f
13f2f 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 unction is calle
13f30 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 d to update the
13f31 63 68 61 6e 67 65 0a 20 20 20 20 2a 2a 20 63 6f change. ** co
13f32 75 6e 74 65 72 20 69 6e 20 27 69 6e 64 69 72 65 unter in 'indire
13f33 63 74 2d 6d 6f 64 65 27 2e 20 49 66 20 74 68 65 ct-mode'. If the
13f34 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 optimization is
13f35 20 63 6f 6d 70 69 6c 65 64 20 69 6e 20 62 75 74 compiled in but
13f36 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 . ** is not a
13f37 70 70 6c 69 63 61 62 6c 65 20 74 6f 20 74 68 69 pplicable to thi
13f38 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 63 s transaction, c
13f39 61 6c 6c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e all sqlite3Journ
13f3a 61 6c 43 72 65 61 74 65 28 29 0a 20 20 20 20 2a alCreate(). *
13f3b 2a 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 * to make sure t
13f3c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 he journal file
13f3d 68 61 73 20 61 63 74 75 61 6c 6c 79 20 62 65 65 has actually bee
13f3e 6e 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 n created, then
13f3f 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65 call. ** page
13f40 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
13f41 6e 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 nter() to update
13f42 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
13f43 74 65 72 20 69 6e 20 69 6e 64 69 72 65 63 74 0a ter in indirect.
13f44 20 20 20 20 2a 2a 20 6d 6f 64 65 2e 20 0a 20 20 ** mode. .
13f45 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 74 68 65 **. ** Othe
13f46 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 6f 70 rwise, if the op
13f47 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 6f timization is bo
13f48 74 68 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 61 th enabled and a
13f49 70 70 6c 69 63 61 62 6c 65 2c 0a 20 20 20 20 2a pplicable,. *
13f4a 2a 20 74 68 65 6e 20 63 61 6c 6c 20 70 61 67 65 * then call page
13f4b 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 r_incr_changecou
13f4c 6e 74 65 72 28 29 20 74 6f 20 75 70 64 61 74 65 nter() to update
13f4d 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e the change-coun
13f4e 74 65 72 0a 20 20 20 20 2a 2a 20 69 6e 20 27 64 ter. ** in 'd
13f4f 69 72 65 63 74 27 20 6d 6f 64 65 2e 20 49 6e 20 irect' mode. In
13f50 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f this case the jo
13f51 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 urnal file will
13f52 6e 65 76 65 72 20 62 65 0a 20 20 20 20 2a 2a 20 never be. **
13f53 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 created for this
13f54 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 transaction..
13f55 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 */.#ifdef SQLI
13f56 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 TE_ENABLE_ATOMIC
13f57 5f 57 52 49 54 45 0a 20 20 20 20 50 67 48 64 72 _WRITE. PgHdr
13f58 20 2a 70 50 67 3b 0a 20 20 20 20 61 73 73 65 72 *pPg;. asser
13f59 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 t( isOpen(pPager
13f5a 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 ->jfd) || pPager
13f5b 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 ->journalMode==P
13f5c 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 AGER_JOURNALMODE
13f5d 5f 4f 46 46 20 29 3b 0a 20 20 20 20 69 66 28 20 _OFF );. if(
13f5e 21 7a 4d 61 73 74 65 72 20 26 26 20 69 73 4f 70 !zMaster && isOp
13f5f 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 en(pPager->jfd)
13f60 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d . && pPager-
13f61 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 6a 72 6e >journalOff==jrn
13f62 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50 61 67 lBufferSize(pPag
13f63 65 72 29 20 0a 20 20 20 20 20 26 26 20 70 50 61 er) . && pPa
13f64 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 70 50 61 ger->dbSize>=pPa
13f65 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 0a ger->dbFileSize.
13f66 20 20 20 20 20 26 26 20 28 30 3d 3d 28 70 50 67 && (0==(pPg
13f67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 = sqlite3Pcache
13f68 44 69 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 DirtyList(pPager
13f69 2d 3e 70 50 43 61 63 68 65 29 29 20 7c 7c 20 30 ->pPCache)) || 0
13f6a 3d 3d 70 50 67 2d 3e 70 44 69 72 74 79 29 0a 20 ==pPg->pDirty).
13f6b 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 ){. /* U
13f6c 70 64 61 74 65 20 74 68 65 20 64 62 20 66 69 6c pdate the db fil
13f6d 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 e change counter
13f6e 20 76 69 61 20 74 68 65 20 64 69 72 65 63 74 2d via the direct-
13f6f 77 72 69 74 65 20 6d 65 74 68 6f 64 2e 20 54 68 write method. Th
13f70 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f 6c 6c e . ** foll
13f71 6f 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20 owing call will
13f72 6d 6f 64 69 66 79 20 74 68 65 20 69 6e 2d 6d 65 modify the in-me
13f73 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 mory representat
13f74 69 6f 6e 20 6f 66 20 70 61 67 65 20 31 20 0a 20 ion of page 1 .
13f75 20 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 6c 75 ** to inclu
13f76 64 65 20 74 68 65 20 75 70 64 61 74 65 64 20 63 de the updated c
13f77 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 61 6e hange counter an
13f78 64 20 74 68 65 6e 20 77 72 69 74 65 20 70 61 67 d then write pag
13f79 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 e 1 . ** di
13f7a 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 rectly to the da
13f7b 74 61 62 61 73 65 20 66 69 6c 65 2e 20 42 65 63 tabase file. Bec
13f7c 61 75 73 65 20 6f 66 20 74 68 65 20 61 74 6f 6d ause of the atom
13f7d 69 63 2d 77 72 69 74 65 20 0a 20 20 20 20 20 20 ic-write .
13f7e 2a 2a 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 ** property of t
13f7f 68 65 20 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 he host file-sys
13f80 74 65 6d 2c 20 74 68 69 73 20 69 73 20 73 61 66 tem, this is saf
13f81 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 e.. */.
13f82 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 rc = pager_inc
13f83 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 r_changecounter(
13f84 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20 20 pPager, 1);.
13f85 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
13f86 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c = sqlite3Journal
13f87 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 6a Create(pPager->j
13f88 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 fd);. if( r
13f89 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
13f8a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 rc = pag
13f8b 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f er_incr_changeco
13f8c 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 30 29 unter(pPager, 0)
13f8d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
13f8e 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 70 #else. rc = p
13f8f 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 ager_incr_change
13f90 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20 counter(pPager,
13f91 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 0);.#endif. i
13f92 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
13f93 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 ) goto commit_p
13f94 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a hase_one_exit;..
13f95 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 74 /* If this t
13f96 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6d ransaction has m
13f97 61 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65 ade the database
13f98 20 73 6d 61 6c 6c 65 72 2c 20 74 68 65 6e 20 61 smaller, then a
13f99 6c 6c 20 70 61 67 65 73 0a 20 20 20 20 2a 2a 20 ll pages. **
13f9a 62 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 being discarded
13f9b 62 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f by the truncatio
13f9c 6e 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 n must be writte
13f9d 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c n to the journal
13f9e 0a 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 54 68 . ** file. Th
13f9f 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 is can only happ
13fa0 65 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 en in auto-vacuu
13fa1 6d 20 6d 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 m mode.. **.
13fa2 20 20 20 2a 2a 20 42 65 66 6f 72 65 20 72 65 61 ** Before rea
13fa3 64 69 6e 67 20 74 68 65 20 70 61 67 65 73 20 77 ding the pages w
13fa4 69 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 ith page numbers
13fa5 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 larger than the
13fa6 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 . ** current
13fa7 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72 2e value of Pager.
13fa8 64 62 53 69 7a 65 2c 20 73 65 74 20 64 62 53 69 dbSize, set dbSi
13fa9 7a 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 ze back to the v
13faa 61 6c 75 65 0a 20 20 20 20 2a 2a 20 74 68 61 74 alue. ** that
13fab 20 69 74 20 74 6f 6f 6b 20 61 74 20 74 68 65 20 it took at the
13fac 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 start of the tra
13fad 6e 73 61 63 74 69 6f 6e 2e 20 4f 74 68 65 72 77 nsaction. Otherw
13fae 69 73 65 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 ise, the. **
13faf 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
13fb0 50 61 67 65 72 47 65 74 28 29 20 72 65 74 75 72 PagerGet() retur
13fb1 6e 20 7a 65 72 6f 65 64 20 70 61 67 65 73 20 69 n zeroed pages i
13fb2 6e 73 74 65 61 64 20 6f 66 20 0a 20 20 20 20 2a nstead of . *
13fb3 2a 20 72 65 61 64 69 6e 67 20 64 61 74 61 20 66 * reading data f
13fb4 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
13fb5 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 file.. **.
13fb6 20 20 2a 2a 20 57 68 65 6e 20 6a 6f 75 72 6e 61 ** When journa
13fb7 6c 5f 6d 6f 64 65 3d 3d 4f 46 46 20 74 68 65 20 l_mode==OFF the
13fb8 64 62 4f 72 69 67 53 69 7a 65 20 69 73 20 61 6c dbOrigSize is al
13fb9 77 61 79 73 20 7a 65 72 6f 2c 20 73 6f 20 74 68 ways zero, so th
13fba 69 73 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 is. ** block
13fbb 6e 65 76 65 72 20 72 75 6e 73 20 69 66 20 6a 6f never runs if jo
13fbc 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 2e 0a urnal_mode=OFF..
13fbd 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 */.#ifndef S
13fbe 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
13fbf 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50 ACUUM. if( pP
13fc0 61 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 61 ager->dbSize<pPa
13fc1 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 ger->dbOrigSize
13fc2 0a 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 . && ALWAYS(
13fc3 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d pPager->journalM
13fc4 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e ode!=PAGER_JOURN
13fc5 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 20 ALMODE_OFF).
13fc6 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 3b ){. Pgno i;
13fc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13fc9 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 /* Iterator v
13fca 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 ariable */.
13fcb 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 6b 69 const Pgno iSki
13fcc 70 20 3d 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e p = PAGER_MJ_PGN
13fcd 4f 28 70 50 61 67 65 72 29 3b 20 2f 2a 20 50 65 O(pPager); /* Pe
13fce 6e 64 69 6e 67 20 6c 6f 63 6b 20 70 61 67 65 20 nding lock page
13fcf 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50 */. const P
13fd0 67 6e 6f 20 64 62 53 69 7a 65 20 3d 20 70 50 61 gno dbSize = pPa
13fd1 67 65 72 2d 3e 64 62 53 69 7a 65 3b 20 20 20 20 ger->dbSize;
13fd2 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 /* Database i
13fd3 6d 61 67 65 20 73 69 7a 65 20 2a 2f 20 0a 20 20 mage size */ .
13fd4 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 pPager->dbSi
13fd5 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 4f ze = pPager->dbO
13fd6 72 69 67 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 rigSize;. f
13fd7 6f 72 28 20 69 3d 64 62 53 69 7a 65 2b 31 3b 20 or( i=dbSize+1;
13fd8 69 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 i<=pPager->dbOri
13fd9 67 53 69 7a 65 3b 20 69 2b 2b 20 29 7b 0a 20 20 gSize; i++ ){.
13fda 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 if( !sqlit
13fdb 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61 e3BitvecTest(pPa
13fdc 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c ger->pInJournal,
13fdd 20 69 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 i) && i!=iSkip
13fde 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 67 48 ){. PgH
13fdf 64 72 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20 dr *pPage;
13fe0 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 74 /* Page t
13fe1 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20 o journal */.
13fe2 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
13fe3 74 65 33 50 61 67 65 72 47 65 74 28 70 50 61 67 te3PagerGet(pPag
13fe4 65 72 2c 20 69 2c 20 26 70 50 61 67 65 29 3b 0a er, i, &pPage);.
13fe5 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 if( rc
13fe6 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
13fe7 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f to commit_phase_
13fe8 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 one_exit;.
13fe9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
13fea 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
13feb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
13fec 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
13fed 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Page);.
13fee 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
13fef 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 OK ) goto commit
13ff0 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b _phase_one_exit;
13ff1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13ff2 20 7d 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 } . pPager
13ff3 2d 3e 64 62 53 69 7a 65 20 3d 20 64 62 53 69 7a ->dbSize = dbSiz
13ff4 65 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a e;. }.#endif.
13ff5 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 . /* Write th
13ff6 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
13ff7 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a name into the j
13ff8 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 ournal file. If
13ff9 61 20 6d 61 73 74 65 72 20 0a 20 20 20 20 2a 2a a master . **
13ffa 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 journal file na
13ffb 6d 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 me has already b
13ffc 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 een written to t
13ffd 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c he journal file,
13ffe 20 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 7a . ** or if z
13fff 4d 61 73 74 65 72 20 69 73 20 4e 55 4c 4c 20 28 Master is NULL (
14000 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 no master journa
14001 6c 29 2c 20 74 68 65 6e 20 74 68 69 73 20 63 61 l), then this ca
14002 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 ll is a no-op..
14003 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 77 */. rc = w
14004 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 riteMasterJourna
14005 6c 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65 l(pPager, zMaste
14006 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d r);. if( rc!=
14007 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f SQLITE_OK ) goto
14008 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e commit_phase_on
14009 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 e_exit;.. /*
1400a 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c Sync the journal
1400b 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 61 74 file. If the at
1400c 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 omic-update opti
1400d 6d 69 7a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e mization is bein
1400e 67 0a 20 20 20 20 2a 2a 20 75 73 65 64 2c 20 74 g. ** used, t
1400f 68 69 73 20 63 61 6c 6c 20 77 69 6c 6c 20 6e 6f his call will no
14010 74 20 63 72 65 61 74 65 20 74 68 65 20 6a 6f 75 t create the jou
14011 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 70 65 72 rnal file or per
14012 66 6f 72 6d 20 61 6e 79 0a 20 20 20 20 2a 2a 20 form any. **
14013 72 65 61 6c 20 49 4f 2e 0a 20 20 20 20 2a 2f 0a real IO.. */.
14014 20 20 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 rc = syncJou
14015 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 rnal(pPager);.
14016 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
14017 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 _OK ) goto commi
14018 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 t_phase_one_exit
14019 3b 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74 65 20 ;.. /* Write
1401a 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 all dirty pages
1401b 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 to the database
1401c 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 file. */. rc
1401d 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 = pager_write_pa
1401e 67 65 6c 69 73 74 28 73 71 6c 69 74 65 33 50 63 gelist(sqlite3Pc
1401f 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 acheDirtyList(pP
14020 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 29 3b ager->pPCache));
14021 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c . if( rc!=SQL
14022 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
14023 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 assert( rc!=SQLI
14024 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 TE_IOERR_BLOCKED
14025 20 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 );. goto c
14026 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f ommit_phase_one_
14027 65 78 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 exit;. }.
14028 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 sqlite3PcacheCle
14029 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 anAll(pPager->pP
1402a 43 61 63 68 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 Cache);.. /*
1402b 49 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 If the file on d
1402c 69 73 6b 20 69 73 20 6e 6f 74 20 74 68 65 20 73 isk is not the s
1402d 61 6d 65 20 73 69 7a 65 20 61 73 20 74 68 65 20 ame size as the
1402e 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c 0a database image,.
1402f 20 20 20 20 2a 2a 20 74 68 65 6e 20 75 73 65 20 ** then use
14030 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 20 74 pager_truncate t
14031 6f 20 67 72 6f 77 20 6f 72 20 73 68 72 69 6e 6b o grow or shrink
14032 20 74 68 65 20 66 69 6c 65 20 68 65 72 65 2e 0a the file here..
14033 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
14034 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 21 3d 70 Pager->dbSize!=p
14035 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a Pager->dbFileSiz
14036 65 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 e ){. Pgno
14037 6e 4e 65 77 20 3d 20 70 50 61 67 65 72 2d 3e 64 nNew = pPager->d
14038 62 53 69 7a 65 20 2d 20 28 70 50 61 67 65 72 2d bSize - (pPager-
14039 3e 64 62 53 69 7a 65 3d 3d 50 41 47 45 52 5f 4d >dbSize==PAGER_M
1403a 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 29 3b J_PGNO(pPager));
1403b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1403c 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
1403d 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 3b GER_EXCLUSIVE );
1403e 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 . rc = page
1403f 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61 67 65 r_truncate(pPage
14040 72 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 r, nNew);.
14041 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
14042 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f K ) goto commit_
14043 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a phase_one_exit;.
14044 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 }.. /* Fi
14045 6e 61 6c 6c 79 2c 20 73 79 6e 63 20 74 68 65 20 nally, sync the
14046 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a database file. *
14047 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 /. if( !pPage
14048 72 2d 3e 6e 6f 53 79 6e 63 20 26 26 20 21 6e 6f r->noSync && !no
14049 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 Sync ){. rc
1404a 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 = sqlite3OsSync
1404b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 50 61 (pPager->fd, pPa
1404c 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 ger->sync_flags)
1404d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49 4f 54 52 ;. }. IOTR
1404e 41 43 45 28 28 22 44 42 53 59 4e 43 20 25 70 5c ACE(("DBSYNC %p\
1404f 6e 22 2c 20 70 50 61 67 65 72 29 29 0a 0a 20 20 n", pPager))..
14050 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 pPager->state
14051 3d 20 50 41 47 45 52 5f 53 59 4e 43 45 44 3b 0a = PAGER_SYNCED;.
14052 20 20 7d 0a 0a 63 6f 6d 6d 69 74 5f 70 68 61 73 }..commit_phas
14053 65 5f 6f 6e 65 5f 65 78 69 74 3a 0a 20 20 72 65 e_one_exit:. re
14054 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a turn rc;.}.../*.
14055 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e ** When this fun
14056 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c ction is called,
14057 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
14058 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70 le has been comp
14059 6c 65 74 65 6c 79 0a 2a 2a 20 75 70 64 61 74 65 letely.** update
1405a 64 20 74 6f 20 72 65 66 6c 65 63 74 20 74 68 65 d to reflect the
1405b 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 changes made by
1405c 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 the current tra
1405d 6e 73 61 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 nsaction and.**
1405e 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 synced to disk.
1405f 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 The journal file
14060 20 73 74 69 6c 6c 20 65 78 69 73 74 73 20 69 6e still exists in
14061 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d the file-system
14062 20 0a 2a 2a 20 74 68 6f 75 67 68 2c 20 61 6e 64 .** though, and
14063 20 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 if a failure oc
14064 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 curs at this poi
14065 6e 74 20 69 74 20 77 69 6c 6c 20 65 76 65 6e 74 nt it will event
14066 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 75 73 65 64 ually.** be used
14067 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 as a hot-journa
14068 6c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e l and the curren
14069 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f t transaction ro
1406a 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a lled back..**.**
1406b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 This function f
1406c 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 inalizes the jou
1406d 72 6e 61 6c 20 66 69 6c 65 2c 20 65 69 74 68 65 rnal file, eithe
1406e 72 20 62 79 20 64 65 6c 65 74 69 6e 67 2c 20 0a r by deleting, .
1406f 2a 2a 20 74 72 75 6e 63 61 74 69 6e 67 20 6f 72 ** truncating or
14070 20 70 61 72 74 69 61 6c 6c 79 20 7a 65 72 6f 69 partially zeroi
14071 6e 67 20 69 74 2c 20 73 6f 20 74 68 61 74 20 69 ng it, so that i
14072 74 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 t cannot be used
14073 20 0a 2a 2a 20 66 6f 72 20 68 6f 74 2d 6a 6f 75 .** for hot-jou
14074 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e 20 4f rnal rollback. O
14075 6e 63 65 20 74 68 69 73 20 69 73 20 64 6f 6e 65 nce this is done
14076 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
14077 20 69 73 0a 2a 2a 20 69 72 72 65 76 6f 63 61 62 is.** irrevocab
14078 6c 79 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a ly committed..**
14079 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
1407a 6f 63 63 75 72 73 2c 20 61 6e 20 49 4f 20 65 72 occurs, an IO er
1407b 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
1407c 72 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 rned and the pag
1407d 65 72 0a 2a 2a 20 6d 6f 76 65 73 20 69 6e 74 6f er.** moves into
1407e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 the error state
1407f 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c . Otherwise, SQL
14080 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e ITE_OK is return
14081 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
14082 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
14083 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 3PagerCommitPhas
14084 65 54 77 6f 28 50 61 67 65 72 20 2a 70 50 61 67 eTwo(Pager *pPag
14085 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 er){. int rc =
14086 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 SQLITE_OK;
14087 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
14088 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 eturn code */..
14089 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 /* This routine
1408a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 should not be c
1408b 61 6c 6c 65 64 20 69 66 20 61 20 70 72 69 6f 72 alled if a prior
1408c 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
1408d 72 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 69 66 red.. ** But if
1408e 20 28 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e (due to a codin
1408f 67 20 65 72 72 6f 72 20 65 6c 73 65 77 68 65 72 g error elsewher
14090 65 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 29 e in the system)
14091 20 69 74 20 64 6f 65 73 20 67 65 74 0a 20 20 2a it does get. *
14092 2a 20 63 61 6c 6c 65 64 2c 20 6a 75 73 74 20 72 * called, just r
14093 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 65 eturn the same e
14094 72 72 6f 72 20 63 6f 64 65 20 77 69 74 68 6f 75 rror code withou
14095 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 t doing anything
14096 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 . */. if( NEVER
14097 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 (pPager->errCode
14098 29 20 29 20 72 65 74 75 72 6e 20 70 50 61 67 65 ) ) return pPage
14099 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f r->errCode;.. /
1409a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
1409b 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 should not be ca
1409c 6c 6c 65 64 20 69 66 20 74 68 65 20 70 61 67 65 lled if the page
1409d 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 74 20 6c r is not in at l
1409e 65 61 73 74 0a 20 20 2a 2a 20 50 41 47 45 52 5f east. ** PAGER_
1409f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2e 20 RESERVED state.
140a0 41 6e 64 20 69 6e 64 65 65 64 20 53 51 4c 69 74 And indeed SQLit
140a1 65 20 6e 65 76 65 72 20 64 6f 65 73 20 74 68 69 e never does thi
140a2 73 2e 20 42 75 74 20 69 74 20 69 73 0a 20 20 2a s. But it is. *
140a3 2a 20 6e 69 63 65 20 74 6f 20 68 61 76 65 20 74 * nice to have t
140a4 68 69 73 20 64 65 66 65 6e 73 69 76 65 20 74 65 his defensive te
140a5 73 74 20 68 65 72 65 20 61 6e 79 77 61 79 2e 0a st here anyway..
140a6 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 */. if( NEVER
140a7 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 (pPager->state<P
140a8 41 47 45 52 5f 52 45 53 45 52 56 45 44 29 20 29 AGER_RESERVED) )
140a9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 return SQLITE_E
140aa 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 41 6e 20 6f RROR;.. /* An o
140ab 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 49 66 20 ptimization. If
140ac 74 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 the database was
140ad 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 6d 6f not actually mo
140ae 64 69 66 69 65 64 20 64 75 72 69 6e 67 0a 20 20 dified during.
140af 2a 2a 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 ** this transact
140b0 69 6f 6e 2c 20 74 68 65 20 70 61 67 65 72 20 69 ion, the pager i
140b1 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 s running in exc
140b2 6c 75 73 69 76 65 2d 6d 6f 64 65 20 61 6e 64 20 lusive-mode and
140b3 69 73 0a 20 20 2a 2a 20 75 73 69 6e 67 20 70 65 is. ** using pe
140b4 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c rsistent journal
140b5 73 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e s, then this fun
140b6 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 ction is a no-op
140b7 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 .. **. ** The
140b8 73 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 start of the jou
140b9 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65 6e rnal file curren
140ba 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 tly contains a s
140bb 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 ingle journal .
140bc 20 2a 2a 20 68 65 61 64 65 72 20 77 69 74 68 20 ** header with
140bd 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 73 the nRec field s
140be 65 74 20 74 6f 20 30 2e 20 49 66 20 73 75 63 68 et to 0. If such
140bf 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 a journal is us
140c0 65 64 20 61 73 0a 20 20 2a 2a 20 61 20 68 6f 74 ed as. ** a hot
140c1 2d 6a 6f 75 72 6e 61 6c 20 64 75 72 69 6e 67 20 -journal during
140c2 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c hot-journal roll
140c3 62 61 63 6b 2c 20 30 20 63 68 61 6e 67 65 73 20 back, 0 changes
140c4 77 69 6c 6c 20 62 65 20 6d 61 64 65 0a 20 20 2a will be made. *
140c5 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 * to the databas
140c6 65 20 66 69 6c 65 2e 20 53 6f 20 74 68 65 72 65 e file. So there
140c7 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 7a is no need to z
140c8 65 72 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 ero the journal
140c9 0a 20 20 2a 2a 20 68 65 61 64 65 72 2e 20 53 69 . ** header. Si
140ca 6e 63 65 20 74 68 65 20 70 61 67 65 72 20 69 73 nce the pager is
140cb 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f in exclusive mo
140cc 64 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 de, there is no
140cd 6e 65 65 64 0a 20 20 2a 2a 20 74 6f 20 64 72 6f need. ** to dro
140ce 70 20 61 6e 79 20 6c 6f 63 6b 73 20 65 69 74 68 p any locks eith
140cf 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 er.. */. if( p
140d0 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
140d1 64 3d 3d 30 20 26 26 20 70 50 61 67 65 72 2d 3e d==0 && pPager->
140d2 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 exclusiveMode .
140d3 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 && pPager->jou
140d4 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f rnalMode==PAGER_
140d5 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 JOURNALMODE_PERS
140d6 49 53 54 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 IST. ){. ass
140d7 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 ert( pPager->jou
140d8 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c rnalOff==JOURNAL
140d9 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 _HDR_SZ(pPager)
140da 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 );. return SQ
140db 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
140dc 50 41 47 45 52 54 52 41 43 45 28 28 22 43 4f 4d PAGERTRACE(("COM
140dd 4d 49 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 MIT %d\n", PAGER
140de 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 ID(pPager)));.
140df 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e assert( pPager->
140e0 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e state==PAGER_SYN
140e1 43 45 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 CED || MEMDB ||
140e2 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 !pPager->dbModif
140e3 69 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 ied );. rc = pa
140e4 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 ger_end_transact
140e5 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 ion(pPager, pPag
140e6 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a er->setMaster);.
140e7 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 return pager_e
140e8 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 rror(pPager, rc)
140e9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 ;.}../*.** Rollb
140ea 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 2e ack all changes.
140eb 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 61 The database fa
140ec 6c 6c 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 lls back to PAGE
140ed 52 5f 53 48 41 52 45 44 20 6d 6f 64 65 2e 0a 2a R_SHARED mode..*
140ee 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
140ef 6f 6e 20 70 65 72 66 6f 72 6d 73 20 74 77 6f 20 on performs two
140f0 74 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 tasks:.**.** 1
140f1 29 20 49 74 20 72 6f 6c 6c 73 20 62 61 63 6b 20 ) It rolls back
140f2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
140f3 2c 20 72 65 73 74 6f 72 69 6e 67 20 61 6c 6c 20 , restoring all
140f4 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e database file an
140f5 64 20 0a 2a 2a 20 20 20 20 20 20 69 6e 2d 6d 65 d .** in-me
140f6 6d 6f 72 79 20 63 61 63 68 65 20 70 61 67 65 73 mory cache pages
140f7 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 74 68 to the state th
140f8 65 79 20 77 65 72 65 20 69 6e 20 77 68 65 6e 20 ey were in when
140f9 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a the transaction.
140fa 2a 2a 20 20 20 20 20 20 77 61 73 20 6f 70 65 6e ** was open
140fb 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 20 32 29 20 ed, and.** 2)
140fc 49 74 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 It finalizes the
140fd 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73 journal file, s
140fe 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 o that it is not
140ff 20 75 73 65 64 20 66 6f 72 20 68 6f 74 0a 2a 2a used for hot.**
14100 20 20 20 20 20 20 72 6f 6c 6c 62 61 63 6b 20 61 rollback a
14101 74 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 t any point in t
14102 68 65 20 66 75 74 75 72 65 2e 0a 2a 2a 0a 2a 2a he future..**.**
14103 20 73 75 62 6a 65 63 74 20 74 6f 20 74 68 65 20 subject to the
14104 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 61 6c 69 66 following qualif
14105 69 63 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 ications:.**.**
14106 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c * If the journal
14107 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 79 65 74 file is not yet
14108 20 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 open when this
14109 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
1410a 65 64 2c 0a 2a 2a 20 20 20 74 68 65 6e 20 6f 6e ed,.** then on
1410b 6c 79 20 28 32 29 20 69 73 20 70 65 72 66 6f 72 ly (2) is perfor
1410c 6d 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 med. In this cas
1410d 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6a 6f e there is no jo
1410e 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 20 20 urnal file.**
1410f 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 0a 2a 2a to roll back..**
14110 0a 2a 2a 20 2a 20 49 66 20 69 6e 20 61 6e 20 65 .** * If in an e
14111 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 rror state other
14112 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c than SQLITE_FUL
14113 4c 2c 20 74 68 65 6e 20 74 61 73 6b 20 28 31 29 L, then task (1)
14114 20 69 73 20 0a 2a 2a 20 20 20 70 65 72 66 6f 72 is .** perfor
14115 6d 65 64 2e 20 49 66 20 73 75 63 63 65 73 73 66 med. If successf
14116 75 6c 2c 20 74 61 73 6b 20 28 32 29 2e 20 52 65 ul, task (2). Re
14117 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 gardless of the
14118 6f 75 74 63 6f 6d 65 0a 2a 2a 20 20 20 6f 66 20 outcome.** of
14119 65 69 74 68 65 72 2c 20 74 68 65 20 65 72 72 6f either, the erro
1411a 72 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f r state error co
1411b 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 de is returned t
1411c 6f 20 74 68 65 20 63 61 6c 6c 65 72 0a 2a 2a 20 o the caller.**
1411d 20 20 28 69 2e 65 2e 20 65 69 74 68 65 72 20 53 (i.e. either S
1411e 51 4c 49 54 45 5f 49 4f 45 52 52 20 6f 72 20 53 QLITE_IOERR or S
1411f 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 29 2e 0a QLITE_CORRUPT)..
14120 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 70 **.** * If the p
14121 61 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 ager is in PAGER
14122 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c _RESERVED state,
14123 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 then attempt (1
14124 29 2e 20 57 68 65 74 68 65 72 0a 2a 2a 20 20 20 ). Whether.**
14125 6f 72 20 6e 6f 74 20 28 31 29 20 69 73 20 73 75 or not (1) is su
14126 63 63 75 73 73 66 75 6c 2c 20 61 6c 73 6f 20 61 ccussful, also a
14127 74 74 65 6d 70 74 20 28 32 29 2e 20 49 66 20 73 ttempt (2). If s
14128 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 uccessful, retur
14129 6e 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4f 4b n.** SQLITE_OK
1412a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 65 6e 74 . Otherwise, ent
1412b 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 er the error sta
1412c 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 te and return th
1412d 65 20 66 69 72 73 74 20 0a 2a 2a 20 20 20 65 72 e first .** er
1412e 72 6f 72 20 63 6f 64 65 20 65 6e 63 6f 75 6e 74 ror code encount
1412f 65 72 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 ered. .**.** I
14130 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 n this case ther
14131 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 e is no chance t
14132 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 hat the database
14133 20 77 61 73 20 77 72 69 74 74 65 6e 20 74 6f 2e was written to.
14134 20 0a 2a 2a 20 20 20 53 6f 20 69 73 20 73 61 66 .** So is saf
14135 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 e to finalize th
14136 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 e journal file e
14137 76 65 6e 20 69 66 20 74 68 65 20 70 6c 61 79 62 ven if the playb
14138 61 63 6b 20 0a 2a 2a 20 20 20 28 6f 70 65 72 61 ack .** (opera
14139 74 69 6f 6e 20 31 29 20 66 61 69 6c 65 64 2e 20 tion 1) failed.
1413a 48 6f 77 65 76 65 72 20 74 68 65 20 70 61 67 65 However the page
1413b 72 20 6d 75 73 74 20 65 6e 74 65 72 20 74 68 65 r must enter the
1413c 20 65 72 72 6f 72 20 73 74 61 74 65 0a 2a 2a 20 error state.**
1413d 20 20 61 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 as the content
1413e 73 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f s of the in-memo
1413f 72 79 20 63 61 63 68 65 20 61 72 65 20 6e 6f 77 ry cache are now
14140 20 73 75 73 70 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 suspect..**.**
14141 2a 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 69 6e * Finally, if in
14142 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 PAGER_EXCLUSIVE
14143 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74 74 state, then att
14144 65 6d 70 74 20 28 31 29 2e 20 4f 6e 6c 79 0a 2a empt (1). Only.*
14145 2a 20 20 20 61 74 74 65 6d 70 74 20 28 32 29 20 * attempt (2)
14146 69 66 20 28 31 29 20 69 73 20 73 75 63 63 65 73 if (1) is succes
14147 73 66 75 6c 2e 20 52 65 74 75 72 6e 20 53 51 4c sful. Return SQL
14148 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 ITE_OK if succes
14149 73 66 75 6c 2c 0a 2a 2a 20 20 20 6f 74 68 65 72 sful,.** other
1414a 77 69 73 65 20 65 6e 74 65 72 20 74 68 65 20 65 wise enter the e
1414b 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72 rror state and r
1414c 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 eturn the error
1414d 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a code from the .*
1414e 2a 20 20 20 66 61 69 6c 69 6e 67 20 6f 70 65 72 * failing oper
1414f 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 ation..**.** I
14150 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 n this case the
14151 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 database file ma
14152 79 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74 y have been writ
14153 74 65 6e 20 74 6f 2e 20 53 6f 20 69 66 20 74 68 ten to. So if th
14154 65 0a 2a 2a 20 20 20 70 6c 61 79 62 61 63 6b 20 e.** playback
14155 6f 70 65 72 61 74 69 6f 6e 20 64 69 64 20 6e 6f operation did no
14156 74 20 73 75 63 63 65 65 64 20 69 74 20 77 6f 75 t succeed it wou
14157 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 65 20 74 ld not be safe t
14158 6f 20 66 69 6e 61 6c 69 7a 65 0a 2a 2a 20 20 20 o finalize.**
14159 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 the journal file
1415a 2e 20 49 74 20 6e 65 65 64 73 20 74 6f 20 62 65 . It needs to be
1415b 20 6c 65 66 74 20 69 6e 20 74 68 65 20 66 69 6c left in the fil
1415c 65 2d 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 e-system so that
1415d 0a 2a 2a 20 20 20 73 6f 6d 65 20 6f 74 68 65 72 .** some other
1415e 20 70 72 6f 63 65 73 73 20 63 61 6e 20 75 73 65 process can use
1415f 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 20 74 it to restore t
14160 68 65 20 64 61 74 61 62 61 73 65 20 73 74 61 74 he database stat
14161 65 20 28 62 79 0a 2a 2a 20 20 20 68 6f 74 2d 6a e (by.** hot-j
14162 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 29 ournal rollback)
14163 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14164 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 ATE int sqlite3P
14165 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 agerRollback(Pag
14166 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 er *pPager){. i
14167 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
14168 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 K;
14169 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
1416a 64 65 20 2a 2f 0a 20 20 50 41 47 45 52 54 52 41 de */. PAGERTRA
1416b 43 45 28 28 22 52 4f 4c 4c 42 41 43 4b 20 25 64 CE(("ROLLBACK %d
1416c 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 \n", PAGERID(pPa
1416d 67 65 72 29 29 29 3b 0a 20 20 69 66 28 20 21 70 ger)));. if( !p
1416e 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 Pager->dbModifie
1416f 64 20 7c 7c 20 21 69 73 4f 70 65 6e 28 70 50 61 d || !isOpen(pPa
14170 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
14171 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f rc = pager_end_
14172 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 transaction(pPag
14173 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d er, pPager->setM
14174 61 73 74 65 72 29 3b 0a 20 20 7d 65 6c 73 65 20 aster);. }else
14175 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 if( pPager->errC
14176 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 65 ode && pPager->e
14177 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 46 rrCode!=SQLITE_F
14178 55 4c 4c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ULL ){. if( p
14179 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 Pager->state>=PA
1417a 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29 7b GER_EXCLUSIVE ){
1417b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 70 6c 61 . pager_pla
1417c 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 yback(pPager, 0)
1417d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
1417e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 pPager->errCode
1417f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
14180 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 f( pPager->state
14181 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 ==PAGER_RESERVED
14182 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 ){. int rc
14183 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 2;. rc = pa
14184 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 ger_playback(pPa
14185 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 ger, 0);. r
14186 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 c2 = pager_end_t
14187 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 ransaction(pPage
14188 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 r, pPager->setMa
14189 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 ster);. if(
1418a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1418b 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 {. rc = r
1418c 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 c2;. }.
1418d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
1418e 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b = pager_playback
1418f 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 (pPager, 0);.
14190 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 4d 45 4d }.. if( !MEM
14191 44 42 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 DB ){. pPag
14192 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 er->dbSizeValid
14193 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 = 0;. }..
14194 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f /* If an error o
14195 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 20 52 ccurs during a R
14196 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61 6e 20 OLLBACK, we can
14197 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73 74 20 no longer trust
14198 74 68 65 20 70 61 67 65 72 0a 20 20 20 20 2a 2a the pager. **
14199 20 63 61 63 68 65 2e 20 53 6f 20 63 61 6c 6c 20 cache. So call
1419a 70 61 67 65 72 5f 65 72 72 6f 72 28 29 20 6f 6e pager_error() on
1419b 20 74 68 65 20 77 61 79 20 6f 75 74 20 74 6f 20 the way out to
1419c 6d 61 6b 65 20 61 6e 79 20 65 72 72 6f 72 20 0a make any error .
1419d 20 20 20 20 2a 2a 20 70 65 72 73 69 73 74 65 6e ** persisten
1419e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 t.. */. rc
1419f 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 = pager_error(p
141a0 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 7d 0a Pager, rc);. }.
141a1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
141a2 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 /*.** Return TRU
141a3 45 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 E if the databas
141a4 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 e file is opened
141a5 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52 65 74 read-only. Ret
141a6 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20 urn FALSE.** if
141a7 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 the database is
141a8 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72 69 74 (in theory) writ
141a9 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f able..*/.SQLITE_
141aa 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 PRIVATE u8 sqlit
141ab 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c e3PagerIsreadonl
141ac 79 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 y(Pager *pPager)
141ad 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 {. return pPage
141ae 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a 7d 0a 0a r->readOnly;.}..
141af 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
141b0 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 number of refer
141b1 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 ences to the pag
141b2 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 er..*/.SQLITE_PR
141b3 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
141b4 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 3PagerRefcount(P
141b5 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 ager *pPager){.
141b6 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 return sqlite3P
141b7 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 cacheRefCount(pP
141b8 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a ager->pPCache);.
141b9 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
141ba 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 the number of re
141bb 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 ferences to the
141bc 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e 0a specified page..
141bd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
141be 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 E int sqlite3Pag
141bf 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 44 erPageRefcount(D
141c0 62 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 bPage *pPage){.
141c1 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 return sqlite3P
141c2 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e cachePageRefcoun
141c3 74 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 23 69 66 t(pPage);.}..#if
141c4 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a def SQLITE_TEST.
141c5 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
141c6 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 ne is used for t
141c7 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 esting and analy
141c8 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c sis only..*/.SQL
141c9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
141ca 2a 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 *sqlite3PagerSta
141cb 74 73 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ts(Pager *pPager
141cc 29 7b 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 ){. static int
141cd 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d 20 3d 20 a[11];. a[0] =
141ce 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 sqlite3PcacheRef
141cf 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 Count(pPager->pP
141d0 43 61 63 68 65 29 3b 0a 20 20 61 5b 31 5d 20 3d Cache);. a[1] =
141d1 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 sqlite3PcachePa
141d2 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e gecount(pPager->
141d3 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 32 5d pPCache);. a[2]
141d4 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 = sqlite3Pcache
141d5 47 65 74 43 61 63 68 65 73 69 7a 65 28 70 50 61 GetCachesize(pPa
141d6 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 ger->pPCache);.
141d7 20 61 5b 33 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[3] = pPager->
141d8 64 62 53 69 7a 65 56 61 6c 69 64 20 3f 20 28 69 dbSizeValid ? (i
141d9 6e 74 29 20 70 50 61 67 65 72 2d 3e 64 62 53 69 nt) pPager->dbSi
141da 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 5b 34 5d 20 ze : -1;. a[4]
141db 3d 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3b = pPager->state;
141dc 0a 20 20 61 5b 35 5d 20 3d 20 70 50 61 67 65 72 . a[5] = pPager
141dd 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 61 5b 36 ->errCode;. a[6
141de 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 48 69 74 ] = pPager->nHit
141df 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 50 61 67 65 ;. a[7] = pPage
141e0 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 61 5b 38 5d r->nMiss;. a[8]
141e1 20 3d 20 30 3b 20 20 2f 2a 20 55 73 65 64 20 74 = 0; /* Used t
141e2 6f 20 62 65 20 70 50 61 67 65 72 2d 3e 6e 4f 76 o be pPager->nOv
141e3 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d 20 3d 20 70 fl */. a[9] = p
141e4 50 61 67 65 72 2d 3e 6e 52 65 61 64 3b 0a 20 20 Pager->nRead;.
141e5 61 5b 31 30 5d 20 3d 20 70 50 61 67 65 72 2d 3e a[10] = pPager->
141e6 6e 57 72 69 74 65 3b 0a 20 20 72 65 74 75 72 6e nWrite;. return
141e7 20 61 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a a;.}.#endif../*
141e8 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 .** Return true
141e9 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e if this is an in
141ea 2d 6d 65 6d 6f 72 79 20 70 61 67 65 72 2e 0a 2a -memory pager..*
141eb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
141ec 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 int sqlite3Page
141ed 72 49 73 4d 65 6d 64 62 28 50 61 67 65 72 20 2a rIsMemdb(Pager *
141ee 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 pPager){. retur
141ef 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a 2f 2a 0a 2a n MEMDB;.}../*.*
141f0 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 * Check that the
141f1 72 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 re are at least
141f2 6e 53 61 76 65 70 6f 69 6e 74 20 73 61 76 65 70 nSavepoint savep
141f3 6f 69 6e 74 73 20 6f 70 65 6e 2e 20 49 66 20 74 oints open. If t
141f4 68 65 72 65 20 61 72 65 0a 2a 2a 20 63 75 72 72 here are.** curr
141f5 65 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 ently less than
141f6 6e 53 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e nSavepoints open
141f7 2c 20 74 68 65 6e 20 6f 70 65 6e 20 6f 6e 65 20 , then open one
141f8 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 6f 69 6e or more savepoin
141f9 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 75 70 ts.** to make up
141fa 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 2e the difference.
141fb 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f If the number o
141fc 66 20 73 61 76 65 70 6f 69 6e 74 73 20 69 73 20 f savepoints is
141fd 61 6c 72 65 61 64 79 0a 2a 2a 20 65 71 75 61 6c already.** equal
141fe 20 74 6f 20 6e 53 61 76 65 70 6f 69 6e 74 2c 20 to nSavepoint,
141ff 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 then this functi
14200 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a on is a no-op..*
14201 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 *.** If a memory
14202 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c allocation fail
14203 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 s, SQLITE_NOMEM
14204 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 is returned. If
14205 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 an error .** occ
14206 75 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e urs while openin
14207 67 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 g the sub-journa
14208 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 61 6e 20 l file, then an
14209 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 IO error code is
1420a 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 .** returned. Ot
1420b 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f herwise, SQLITE_
1420c 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 OK..*/.SQLITE_PR
1420d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1420e 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 3PagerOpenSavepo
1420f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 int(Pager *pPage
14210 72 2c 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e r, int nSavepoin
14211 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 t){. int rc = S
14212 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 QLITE_OK;
14213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14214 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
14215 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 /. int nCurrent
14216 20 3d 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 = pPager->nSave
14217 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a point; /*
14218 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 Current number
14219 6f 66 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f of savepoints */
1421a 0a 0a 20 20 69 66 28 20 6e 53 61 76 65 70 6f 69 .. if( nSavepoi
1421b 6e 74 3e 6e 43 75 72 72 65 6e 74 20 26 26 20 70 nt>nCurrent && p
1421c 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 Pager->useJourna
1421d 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b l ){. int ii;
1421e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1421f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14220 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 /* Iterator var
14221 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 50 61 67 iable */. Pag
14222 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 61 4e 65 erSavepoint *aNe
14223 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 w;
14224 20 20 20 20 20 2f 2a 20 4e 65 77 20 50 61 67 65 /* New Page
14225 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 r.aSavepoint arr
14226 61 79 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 45 69 ay */.. /* Ei
14227 74 68 65 72 20 74 68 65 72 65 20 69 73 20 6e 6f ther there is no
14228 20 61 63 74 69 76 65 20 6a 6f 75 72 6e 61 6c 20 active journal
14229 6f 72 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e or the sub-journ
1422a 61 6c 20 69 73 20 6f 70 65 6e 20 6f 72 20 0a 20 al is open or .
1422b 20 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 ** the journa
1422c 6c 20 69 73 20 61 6c 77 61 79 73 20 73 74 6f 72 l is always stor
1422d 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 2a 2f 0a ed in memory */.
1422e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
1422f 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d er->nSavepoint==
14230 30 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 0 || isOpen(pPag
14231 65 72 2d 3e 73 6a 66 64 29 20 7c 7c 0a 20 20 20 er->sjfd) ||.
14232 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d pPager-
14233 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 >journalMode==PA
14234 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f GER_JOURNALMODE_
14235 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20 20 20 20 2f MEMORY );.. /
14236 2a 20 47 72 6f 77 20 74 68 65 20 50 61 67 65 72 * Grow the Pager
14237 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 72 72 61 .aSavepoint arra
14238 79 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63 28 y using realloc(
14239 29 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 ). Return SQLITE
1423a 5f 4e 4f 4d 45 4d 0a 20 20 20 20 2a 2a 20 69 66 _NOMEM. ** if
1423b 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 the allocation
1423c 66 61 69 6c 73 2e 20 4f 74 68 65 72 77 69 73 65 fails. Otherwise
1423d 2c 20 7a 65 72 6f 20 74 68 65 20 6e 65 77 20 70 , zero the new p
1423e 6f 72 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 61 ortion in case a
1423f 20 0a 20 20 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 . ** malloc
14240 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 failure occurs w
14241 68 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 hile populating
14242 69 74 20 69 6e 20 74 68 65 20 66 6f 72 28 2e 2e it in the for(..
14243 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f 77 2e 0a 20 .) loop below..
14244 20 20 20 2a 2f 0a 20 20 20 20 61 4e 65 77 20 3d */. aNew =
14245 20 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 (PagerSavepoint
14246 20 2a 29 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f *)sqlite3Reallo
14247 63 28 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 c(. pPage
14248 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 2c 20 73 r->aSavepoint, s
14249 69 7a 65 6f 66 28 50 61 67 65 72 53 61 76 65 70 izeof(PagerSavep
1424a 6f 69 6e 74 29 2a 6e 53 61 76 65 70 6f 69 6e 74 oint)*nSavepoint
1424b 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 . );. if(
1424c 21 61 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 72 !aNew ){. r
1424d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1424e 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 EM;. }. me
1424f 6d 73 65 74 28 26 61 4e 65 77 5b 6e 43 75 72 72 mset(&aNew[nCurr
14250 65 6e 74 5d 2c 20 30 2c 20 28 6e 53 61 76 65 70 ent], 0, (nSavep
14251 6f 69 6e 74 2d 6e 43 75 72 72 65 6e 74 29 20 2a oint-nCurrent) *
14252 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53 61 76 sizeof(PagerSav
14253 65 70 6f 69 6e 74 29 29 3b 0a 20 20 20 20 70 50 epoint));. pP
14254 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 ager->aSavepoint
14255 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 70 50 61 = aNew;. pPa
14256 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 ger->nSavepoint
14257 3d 20 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 0a 20 = nSavepoint;..
14258 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 /* Populate t
14259 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e he PagerSavepoin
1425a 74 20 73 74 72 75 63 74 75 72 65 73 20 6a 75 73 t structures jus
1425b 74 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a t allocated. */.
1425c 20 20 20 20 66 6f 72 28 69 69 3d 6e 43 75 72 72 for(ii=nCurr
1425d 65 6e 74 3b 20 69 69 3c 6e 53 61 76 65 70 6f 69 ent; ii<nSavepoi
1425e 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 nt; ii++){.
1425f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14260 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b 0a >dbSizeValid );.
14261 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 6e aNew[ii].n
14262 4f 72 69 67 20 3d 20 70 50 61 67 65 72 2d 3e 64 Orig = pPager->d
14263 62 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 bSize;. if(
14264 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e isOpen(pPager->
14265 6a 66 64 29 20 26 26 20 41 4c 57 41 59 53 28 70 jfd) && ALWAYS(p
14266 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 Pager->journalOf
14267 66 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 f>0) ){.
14268 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74 aNew[ii].iOffset
14269 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e = pPager->journ
1426a 61 6c 4f 66 66 3b 0a 20 20 20 20 20 20 7d 65 6c alOff;. }el
1426b 73 65 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77 se{. aNew
1426c 5b 69 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 4a [ii].iOffset = J
1426d 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 OURNAL_HDR_SZ(pP
1426e 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ager);. }.
1426f 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 53 aNew[ii].iS
14270 75 62 52 65 63 20 3d 20 70 50 61 67 65 72 2d 3e ubRec = pPager->
14271 6e 53 75 62 52 65 63 3b 0a 20 20 20 20 20 20 61 nSubRec;. a
14272 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 New[ii].pInSavep
14273 6f 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 oint = sqlite3Bi
14274 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67 65 tvecCreate(pPage
14275 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 20 20 r->dbSize);.
14276 20 20 69 66 28 20 21 61 4e 65 77 5b 69 69 5d 2e if( !aNew[ii].
14277 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a pInSavepoint ){.
14278 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
14279 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1427a 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
1427b 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 75 62 2d /* Open the sub-
1427c 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 journal, if it i
1427d 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 s not already op
1427e 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 72 63 20 ened. */. rc
1427f 3d 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c = openSubJournal
14280 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 61 73 (pPager);. as
14281 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 sertTruncateCons
14282 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a traint(pPager);.
14283 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
14284 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 ;.}../*.** This
14285 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c function is call
14286 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f ed to rollback o
14287 72 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 r release (commi
14288 74 29 20 61 20 73 61 76 65 70 6f 69 6e 74 2e 0a t) a savepoint..
14289 2a 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 ** The savepoint
1428a 20 74 6f 20 72 65 6c 65 61 73 65 20 6f 72 20 72 to release or r
1428b 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 20 6e 6f 74 ollback need not
1428c 20 62 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 be the most rec
1428d 65 6e 74 6c 79 20 0a 2a 2a 20 63 72 65 61 74 65 ently .** create
1428e 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a d savepoint..**.
1428f 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 6f 70 20 ** Parameter op
14290 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 is always either
14291 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 SAVEPOINT_ROLLB
14292 41 43 4b 20 6f 72 20 53 41 56 45 50 4f 49 4e 54 ACK or SAVEPOINT
14293 5f 52 45 4c 45 41 53 45 2e 0a 2a 2a 20 49 66 20 _RELEASE..** If
14294 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f it is SAVEPOINT_
14295 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 72 65 RELEASE, then re
14296 6c 65 61 73 65 20 61 6e 64 20 64 65 73 74 72 6f lease and destro
14297 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 y the savepoint
14298 77 69 74 68 0a 2a 2a 20 69 6e 64 65 78 20 69 53 with.** index iS
14299 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 74 20 avepoint. If it
1429a 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c is SAVEPOINT_ROL
1429b 4c 42 41 43 4b 2c 20 74 68 65 6e 20 72 6f 6c 6c LBACK, then roll
1429c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e 67 65 73 back all changes
1429d 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20 6f 63 .** that have oc
1429e 63 75 72 72 65 64 20 73 69 6e 63 65 20 74 68 65 curred since the
1429f 20 73 70 65 63 69 66 69 65 64 20 73 61 76 65 70 specified savep
142a0 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 oint was created
142a1 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 76 65 ..**.** The save
142a2 70 6f 69 6e 74 20 74 6f 20 72 6f 6c 6c 62 61 63 point to rollbac
142a3 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 69 73 20 k or release is
142a4 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 70 61 identified by pa
142a5 72 61 6d 65 74 65 72 20 0a 2a 2a 20 69 53 61 76 rameter .** iSav
142a6 65 70 6f 69 6e 74 2e 20 41 20 76 61 6c 75 65 20 epoint. A value
142a7 6f 66 20 30 20 6d 65 61 6e 73 20 74 6f 20 6f 70 of 0 means to op
142a8 65 72 61 74 65 20 6f 6e 20 74 68 65 20 6f 75 74 erate on the out
142a9 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 ermost savepoint
142aa 0a 2a 2a 20 28 74 68 65 20 66 69 72 73 74 20 63 .** (the first c
142ab 72 65 61 74 65 64 29 2e 20 41 20 76 61 6c 75 65 reated). A value
142ac 20 6f 66 20 28 50 61 67 65 72 2e 6e 53 61 76 65 of (Pager.nSave
142ad 70 6f 69 6e 74 2d 31 29 20 6d 65 61 6e 73 20 6f point-1) means o
142ae 70 65 72 61 74 65 0a 2a 2a 20 6f 6e 20 74 68 65 perate.** on the
142af 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 most recently c
142b0 72 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 reated savepoint
142b1 2e 20 49 66 20 69 53 61 76 65 70 6f 69 6e 74 20 . If iSavepoint
142b2 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a is greater than.
142b3 2a 2a 20 28 50 61 67 65 72 2e 6e 53 61 76 65 70 ** (Pager.nSavep
142b4 6f 69 6e 74 2d 31 29 2c 20 74 68 65 6e 20 74 68 oint-1), then th
142b5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
142b6 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 no-op..**.** If
142b7 20 61 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 a negative valu
142b8 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 e is passed to t
142b9 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 his function, th
142ba 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a en the current.*
142bb 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
142bc 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 rolled back. Th
142bd 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 is is different
142be 74 6f 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 to calling .** s
142bf 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 qlite3PagerRollb
142c0 61 63 6b 28 29 20 62 65 63 61 75 73 65 20 74 68 ack() because th
142c1 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 is function does
142c2 20 6e 6f 74 20 74 65 72 6d 69 6e 61 74 65 0a 2a not terminate.*
142c3 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f * the transactio
142c4 6e 20 6f 72 20 75 6e 6c 6f 63 6b 20 74 68 65 20 n or unlock the
142c5 64 61 74 61 62 61 73 65 2c 20 69 74 20 6a 75 73 database, it jus
142c6 74 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 0a t restores the .
142c7 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 ** contents of t
142c8 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 he database to i
142c9 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 ts original stat
142ca 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 79 e. .**.** In any
142cb 20 63 61 73 65 2c 20 61 6c 6c 20 73 61 76 65 70 case, all savep
142cc 6f 69 6e 74 73 20 77 69 74 68 20 61 6e 20 69 6e oints with an in
142cd 64 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e dex greater than
142ce 20 69 53 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 iSavepoint .**
142cf 61 72 65 20 64 65 73 74 72 6f 79 65 64 2e 20 49 are destroyed. I
142d0 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 f this is a rele
142d1 61 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6f ase operation (o
142d2 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c p==SAVEPOINT_REL
142d3 45 41 53 45 29 2c 0a 2a 2a 20 74 68 65 6e 20 73 EASE),.** then s
142d4 61 76 65 70 6f 69 6e 74 20 69 53 61 76 65 70 6f avepoint iSavepo
142d5 69 6e 74 20 69 73 20 61 6c 73 6f 20 64 65 73 74 int is also dest
142d6 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 royed..**.** Thi
142d7 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 s function may r
142d8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
142d9 45 4d 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 EM if a memory a
142da 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c llocation fails,
142db 0a 2a 2a 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 .** or an IO err
142dc 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 20 49 4f or code if an IO
142dd 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 error occurs wh
142de 69 6c 65 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b ile rolling back
142df 20 61 20 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 a .** savepoint
142e0 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 73 20 6f . If no errors o
142e1 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 ccur, SQLITE_OK
142e2 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 20 is returned..*/
142e3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
142e4 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
142e5 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 Savepoint(Pager
142e6 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c *pPager, int op,
142e7 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 int iSavepoint)
142e8 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
142e9 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 ITE_OK;.. asser
142ea 74 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 t( op==SAVEPOINT
142eb 5f 52 45 4c 45 41 53 45 20 7c 7c 20 6f 70 3d 3d _RELEASE || op==
142ec 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
142ed 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 CK );. assert(
142ee 69 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 7c 7c iSavepoint>=0 ||
142ef 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 op==SAVEPOINT_R
142f0 4f 4c 4c 42 41 43 4b 20 29 3b 0a 0a 20 20 69 66 OLLBACK );.. if
142f1 28 20 69 53 61 76 65 70 6f 69 6e 74 3c 70 50 61 ( iSavepoint<pPa
142f2 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 ger->nSavepoint
142f3 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 ){. int ii;
142f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
142f5 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
142f6 2f 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 3b 20 /. int nNew;
142f7 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
142f8 65 72 20 6f 66 20 72 65 6d 61 69 6e 69 6e 67 20 er of remaining
142f9 73 61 76 65 70 6f 69 6e 74 73 20 61 66 74 65 72 savepoints after
142fa 20 74 68 69 73 20 6f 70 2e 20 2a 2f 0a 0a 20 20 this op. */..
142fb 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
142fc 68 6f 77 20 6d 61 6e 79 20 73 61 76 65 70 6f 69 how many savepoi
142fd 6e 74 73 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 nts will still b
142fe 65 20 61 63 74 69 76 65 20 61 66 74 65 72 20 74 e active after t
142ff 68 69 73 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 his. ** opera
14300 74 69 6f 6e 2e 20 53 74 6f 72 65 20 74 68 69 73 tion. Store this
14301 20 76 61 6c 75 65 20 69 6e 20 6e 4e 65 77 2e 20 value in nNew.
14302 54 68 65 6e 20 66 72 65 65 20 72 65 73 6f 75 72 Then free resour
14303 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 0a ces associated .
14304 20 20 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 ** with any
14305 73 61 76 65 70 6f 69 6e 74 73 20 74 68 61 74 20 savepoints that
14306 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 62 79 are destroyed by
14307 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e this operation.
14308 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 4e 65 77 . */. nNew
14309 20 3d 20 69 53 61 76 65 70 6f 69 6e 74 20 2b 20 = iSavepoint +
1430a 28 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 (op==SAVEPOINT_R
1430b 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20 20 20 66 6f OLLBACK);. fo
1430c 72 28 69 69 3d 6e 4e 65 77 3b 20 69 69 3c 70 50 r(ii=nNew; ii<pP
1430d 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ager->nSavepoint
1430e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 ; ii++){. s
1430f 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 qlite3BitvecDest
14310 72 6f 79 28 70 50 61 67 65 72 2d 3e 61 53 61 76 roy(pPager->aSav
14311 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49 6e 53 61 epoint[ii].pInSa
14312 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 7d 0a vepoint);. }.
14313 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 pPager->nSav
14314 65 70 6f 69 6e 74 20 3d 20 6e 4e 65 77 3b 0a 0a epoint = nNew;..
14315 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
14316 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 70 65 s a rollback ope
14317 72 61 74 69 6f 6e 2c 20 70 6c 61 79 62 61 63 6b ration, playback
14318 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 73 the specified s
14319 61 76 65 70 6f 69 6e 74 2e 0a 20 20 20 20 2a 2a avepoint.. **
1431a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 74 65 If this is a te
1431b 6d 70 2d 66 69 6c 65 2c 20 69 74 20 69 73 20 70 mp-file, it is p
1431c 6f 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 ossible that the
1431d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 journal file ha
1431e 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 79 65 74 s. ** not yet
1431f 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 49 6e been opened. In
14320 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 this case there
14321 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 63 68 have been no ch
14322 61 6e 67 65 73 20 74 6f 0a 20 20 20 20 2a 2a 20 anges to. **
14323 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
14324 65 2c 20 73 6f 20 74 68 65 20 70 6c 61 79 62 61 e, so the playba
14325 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 63 61 6e ck operation can
14326 20 62 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 be skipped..
14327 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 70 3d 3d */. if( op==
14328 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
14329 43 4b 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 CK && isOpen(pPa
1432a 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
1432b 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e PagerSavepoin
1432c 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 t *pSavepoint =
1432d 28 6e 4e 65 77 3d 3d 30 29 3f 30 3a 26 70 50 61 (nNew==0)?0:&pPa
1432e 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b ger->aSavepoint[
1432f 6e 4e 65 77 2d 31 5d 3b 0a 20 20 20 20 20 20 72 nNew-1];. r
14330 63 20 3d 20 70 61 67 65 72 50 6c 61 79 62 61 63 c = pagerPlaybac
14331 6b 53 61 76 65 70 6f 69 6e 74 28 70 50 61 67 65 kSavepoint(pPage
14332 72 2c 20 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a r, pSavepoint);.
14333 20 20 20 20 20 20 61 73 73 65 72 74 28 72 63 21 assert(rc!
14334 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 3b 0a 20 =SQLITE_DONE);.
14335 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 }. . /* I
14336 66 20 74 68 69 73 20 69 73 20 61 20 72 65 6c 65 f this is a rele
14337 61 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 ase of the outer
14338 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74 2c 20 most savepoint,
14339 74 72 75 6e 63 61 74 65 20 0a 20 20 20 20 2a 2a truncate . **
1433a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c the sub-journal
1433b 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 to zero bytes i
1433c 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20 20 20 20 69 n size. */. i
1433d 66 28 20 6e 4e 65 77 3d 3d 30 20 26 26 20 6f 70 f( nNew==0 && op
1433e 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 ==SAVEPOINT_RELE
1433f 41 53 45 20 26 26 20 69 73 4f 70 65 6e 28 70 50 ASE && isOpen(pP
14340 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 ager->sjfd) ){.
14341 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d assert( rc=
14342 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
14343 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
14344 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 OsTruncate(pPage
14345 72 2d 3e 73 6a 66 64 2c 20 30 29 3b 0a 20 20 20 r->sjfd, 0);.
14346 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 pPager->nSubR
14347 65 63 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 ec = 0;. }.
14348 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
14349 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
1434a 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 he full pathname
1434b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1434c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 file..*/.SQLITE
1434d 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
1434e 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 har *sqlite3Page
1434f 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72 20 rFilename(Pager
14350 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
14351 72 6e 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 rn pPager->zFile
14352 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 name;.}../*.** R
14353 65 74 75 72 6e 20 74 68 65 20 56 46 53 20 73 74 eturn the VFS st
14354 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 ructure for the
14355 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pager..*/.SQLITE
14356 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 _PRIVATE const s
14357 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 qlite3_vfs *sqli
14358 74 65 33 50 61 67 65 72 56 66 73 28 50 61 67 65 te3PagerVfs(Page
14359 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 r *pPager){. re
1435a 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 56 66 turn pPager->pVf
1435b 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 s;.}../*.** Retu
1435c 72 6e 20 74 68 65 20 66 69 6c 65 20 68 61 6e 64 rn the file hand
1435d 6c 65 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 le for the datab
1435e 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 ase file associa
1435f 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 ted.** with the
14360 70 61 67 65 72 2e 20 20 54 68 69 73 20 6d 69 67 pager. This mig
14361 68 74 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 69 ht return NULL i
14362 66 20 74 68 65 20 66 69 6c 65 20 68 61 73 0a 2a f the file has.*
14363 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f * not yet been o
14364 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 pened..*/.SQLITE
14365 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 _PRIVATE sqlite3
14366 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50 61 _file *sqlite3Pa
14367 67 65 72 46 69 6c 65 28 50 61 67 65 72 20 2a 70 gerFile(Pager *p
14368 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e Pager){. return
14369 20 70 50 61 67 65 72 2d 3e 66 64 3b 0a 7d 0a 0a pPager->fd;.}..
1436a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 /*.** Return the
1436b 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f full pathname o
1436c 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 f the journal fi
1436d 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 le..*/.SQLITE_PR
1436e 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 IVATE const char
1436f 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f *sqlite3PagerJo
14370 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 urnalname(Pager
14371 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 *pPager){. retu
14372 72 6e 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 rn pPager->zJour
14373 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 nal;.}../*.** Re
14374 74 75 72 6e 20 74 72 75 65 20 69 66 20 66 73 79 turn true if fsy
14375 6e 63 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64 nc() calls are d
14376 69 73 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73 isabled for this
14377 20 70 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20 pager. Return
14378 46 41 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e FALSE.** if fsyn
14379 63 28 29 73 20 61 72 65 20 65 78 65 63 75 74 65 c()s are execute
1437a 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 d normally..*/.S
1437b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1437c 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f t sqlite3PagerNo
1437d 73 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 sync(Pager *pPag
1437e 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
1437f 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a ager->noSync;.}.
14380 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 .#ifdef SQLITE_H
14381 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 AS_CODEC./*.** S
14382 65 74 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 et or retrieve t
14383 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74 68 69 he codec for thi
14384 73 20 70 61 67 65 72 0a 2a 2f 0a 73 74 61 74 69 s pager.*/.stati
14385 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 c void sqlite3Pa
14386 67 65 72 53 65 74 43 6f 64 65 63 28 0a 20 20 50 gerSetCodec(. P
14387 61 67 65 72 20 2a 70 50 61 67 65 72 2c 0a 20 20 ager *pPager,.
14388 76 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 void *(*xCodec)(
14389 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f void*,void*,Pgno
1438a 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a ,int),. void (*
1438b 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28 xCodecSizeChng)(
1438c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 2c 0a void*,int,int),.
1438d 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 void (*xCodecF
1438e 72 65 65 29 28 76 6f 69 64 2a 29 2c 0a 20 20 76 ree)(void*),. v
1438f 6f 69 64 20 2a 70 43 6f 64 65 63 0a 29 7b 0a 20 oid *pCodec.){.
14390 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f if( pPager->xCo
14391 64 65 63 46 72 65 65 20 29 20 70 50 61 67 65 72 decFree ) pPager
14392 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 70 50 61 ->xCodecFree(pPa
14393 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 20 20 ger->pCodec);.
14394 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 20 3d pPager->xCodec =
14395 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3f pPager->memDb ?
14396 20 30 20 3a 20 78 43 6f 64 65 63 3b 0a 20 20 70 0 : xCodec;. p
14397 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a Pager->xCodecSiz
14398 65 43 68 6e 67 20 3d 20 78 43 6f 64 65 63 53 69 eChng = xCodecSi
14399 7a 65 43 68 6e 67 3b 0a 20 20 70 50 61 67 65 72 zeChng;. pPager
1439a 2d 3e 78 43 6f 64 65 63 46 72 65 65 20 3d 20 78 ->xCodecFree = x
1439b 43 6f 64 65 63 46 72 65 65 3b 0a 20 20 70 50 61 CodecFree;. pPa
1439c 67 65 72 2d 3e 70 43 6f 64 65 63 20 3d 20 70 43 ger->pCodec = pC
1439d 6f 64 65 63 3b 0a 20 20 70 61 67 65 72 52 65 70 odec;. pagerRep
1439e 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b ortSize(pPager);
1439f 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a .}.static void *
143a0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 43 sqlite3PagerGetC
143a1 6f 64 65 63 28 50 61 67 65 72 20 2a 70 50 61 67 odec(Pager *pPag
143a2 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 er){. return pP
143a3 61 67 65 72 2d 3e 70 43 6f 64 65 63 3b 0a 7d 0a ager->pCodec;.}.
143a4 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
143a5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
143a6 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 4d 6f 76 VACUUM./*.** Mov
143a7 65 20 74 68 65 20 70 61 67 65 20 70 50 67 20 74 e the page pPg t
143a8 6f 20 6c 6f 63 61 74 69 6f 6e 20 70 67 6e 6f 20 o location pgno
143a9 69 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a in the file..**.
143aa 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 ** There must be
143ab 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 73 20 74 no references t
143ac 6f 20 74 68 65 20 70 61 67 65 20 70 72 65 76 69 o the page previ
143ad 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 74 ously located at
143ae 0a 2a 2a 20 70 67 6e 6f 20 28 77 68 69 63 68 20 .** pgno (which
143af 77 65 20 63 61 6c 6c 20 70 50 67 4f 6c 64 29 20 we call pPgOld)
143b0 74 68 6f 75 67 68 20 74 68 61 74 20 70 61 67 65 though that page
143b1 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 is allowed to b
143b2 65 0a 2a 2a 20 69 6e 20 63 61 63 68 65 2e 20 20 e.** in cache.
143b3 49 66 20 74 68 65 20 70 61 67 65 20 70 72 65 76 If the page prev
143b4 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65 64 20 61 iously located a
143b5 74 20 70 67 6e 6f 20 69 73 20 6e 6f 74 20 61 6c t pgno is not al
143b6 72 65 61 64 79 0a 2a 2a 20 69 6e 20 74 68 65 20 ready.** in the
143b7 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c rollback journal
143b8 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 75 74 20 , it is not put
143b9 74 68 65 72 65 20 62 79 20 62 79 20 74 68 69 73 there by by this
143ba 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
143bb 52 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 References to th
143bc 65 20 70 61 67 65 20 70 50 67 20 72 65 6d 61 69 e page pPg remai
143bd 6e 20 76 61 6c 69 64 2e 20 55 70 64 61 74 69 6e n valid. Updatin
143be 67 20 61 6e 79 0a 2a 2a 20 6d 65 74 61 2d 64 61 g any.** meta-da
143bf 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 ta associated wi
143c0 74 68 20 70 50 67 20 28 69 2e 65 2e 20 64 61 74 th pPg (i.e. dat
143c1 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 a stored in the
143c2 6e 45 78 74 72 61 20 62 79 74 65 73 0a 2a 2a 20 nExtra bytes.**
143c3 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20 allocated along
143c4 77 69 74 68 20 74 68 65 20 70 61 67 65 29 20 69 with the page) i
143c5 73 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 s the responsibi
143c6 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c lity of the call
143c7 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e er..**.** A tran
143c8 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 saction must be
143c9 61 63 74 69 76 65 20 77 68 65 6e 20 74 68 69 73 active when this
143ca 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
143cb 65 64 2e 20 49 74 20 75 73 65 64 20 74 6f 20 62 ed. It used to b
143cc 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 74 68 e.** required th
143cd 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 at a statement t
143ce 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6e ransaction was n
143cf 6f 74 20 61 63 74 69 76 65 2c 20 62 75 74 20 74 ot active, but t
143d0 68 69 73 20 72 65 73 74 72 69 63 74 69 6f 6e 0a his restriction.
143d1 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 65 6d 6f ** has been remo
143d2 76 65 64 20 28 43 52 45 41 54 45 20 49 4e 44 45 ved (CREATE INDE
143d3 58 20 6e 65 65 64 73 20 74 6f 20 6d 6f 76 65 20 X needs to move
143d4 61 20 70 61 67 65 20 77 68 65 6e 20 61 20 73 74 a page when a st
143d5 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 atement.** trans
143d6 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
143d7 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 )..**.** If the
143d8 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c fourth argument,
143d9 20 69 73 43 6f 6d 6d 69 74 2c 20 69 73 20 6e 6f isCommit, is no
143da 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 69 n-zero, then thi
143db 73 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 0a s page is being.
143dc 2a 2a 20 6d 6f 76 65 64 20 61 73 20 70 61 72 74 ** moved as part
143dd 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 72 of a database r
143de 65 6f 72 67 61 6e 69 7a 61 74 69 6f 6e 20 6a 75 eorganization ju
143df 73 74 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 st before the tr
143e0 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 ansaction .** is
143e1 20 62 65 69 6e 67 20 63 6f 6d 6d 69 74 74 65 64 being committed
143e2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 . In this case,
143e3 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 it is guaranteed
143e4 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61 that the databa
143e5 73 65 20 70 61 67 65 20 0a 2a 2a 20 70 50 67 20 se page .** pPg
143e6 72 65 66 65 72 73 20 74 6f 20 77 69 6c 6c 20 6e refers to will n
143e7 6f 74 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f ot be written to
143e8 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 again within th
143e9 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a is transaction..
143ea 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 **.** This funct
143eb 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 ion may return S
143ec 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61 QLITE_NOMEM or a
143ed 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 n IO error code
143ee 69 66 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20 6f if an error.** o
143ef 63 63 75 72 73 2e 20 4f 74 68 65 72 77 69 73 65 ccurs. Otherwise
143f0 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c , it returns SQL
143f1 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 ITE_OK..*/.SQLIT
143f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
143f3 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 lite3PagerMovepa
143f4 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 ge(Pager *pPager
143f5 2c 20 44 62 50 61 67 65 20 2a 70 50 67 2c 20 50 , DbPage *pPg, P
143f6 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e 74 20 69 73 gno pgno, int is
143f7 43 6f 6d 6d 69 74 29 7b 0a 20 20 50 67 48 64 72 Commit){. PgHdr
143f8 20 2a 70 50 67 4f 6c 64 3b 20 20 20 20 20 20 20 *pPgOld;
143f9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 /* The p
143fa 61 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72 age being overwr
143fb 69 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f itten. */. Pgno
143fc 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 needSyncPgno =
143fd 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6c 64 20 0; /* Old
143fe 76 61 6c 75 65 20 6f 66 20 70 50 67 2d 3e 70 67 value of pPg->pg
143ff 6e 6f 2c 20 69 66 20 73 79 6e 63 20 69 73 20 72 no, if sync is r
14400 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 equired */. int
14401 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 rc;
14402 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 /* Ret
14403 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 67 urn code */. Pg
14404 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b 20 20 20 20 no origPgno;
14405 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
14406 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 e original page
14407 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 61 73 73 number */.. ass
14408 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 ert( pPg->nRef>0
14409 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6f 72 64 );.. /* In ord
1440a 65 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f er to be able to
1440b 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 6e 20 69 6e rollback, an in
1440c 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 -memory database
1440d 20 6d 75 73 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 must journal.
1440e 2a 2a 20 74 68 65 20 70 61 67 65 20 77 65 20 61 ** the page we a
1440f 72 65 20 6d 6f 76 69 6e 67 20 66 72 6f 6d 2e 0a re moving from..
14410 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42 */. if( MEMDB
14411 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
14412 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
14413 50 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 Pg);. if( rc
14414 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d ) return rc;. }
14415 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 .. /* If the pa
14416 67 65 20 62 65 69 6e 67 20 6d 6f 76 65 64 20 69 ge being moved i
14417 73 20 64 69 72 74 79 20 61 6e 64 20 68 61 73 20 s dirty and has
14418 6e 6f 74 20 62 65 65 6e 20 73 61 76 65 64 20 62 not been saved b
14419 79 20 74 68 65 20 6c 61 74 65 73 74 0a 20 20 2a y the latest. *
1441a 2a 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 * savepoint, the
1441b 6e 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65 n save the curre
1441c 6e 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 nt contents of t
1441d 68 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 he page into the
1441e 20 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e . ** sub-journ
1441f 61 6c 20 6e 6f 77 2e 20 54 68 69 73 20 69 73 20 al now. This is
14420 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64 required to hand
14421 6c 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 le the following
14422 20 73 63 65 6e 61 72 69 6f 3a 0a 20 20 2a 2a 0a scenario:. **.
14423 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20 ** BEGIN;.
14424 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20 ** <journal
14425 70 61 67 65 20 58 2c 20 74 68 65 6e 20 6d 6f 64 page X, then mod
14426 69 66 79 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 ify it in memory
14427 3e 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 >. ** SAVEP
14428 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 2a 2a 20 20 OINT one;. **
14429 20 20 20 20 20 3c 4d 6f 76 65 20 70 61 67 65 20 <Move page
1442a 58 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 59 3e X to location Y>
1442b 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 . ** ROLLBA
1442c 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 2a 2a 0a CK TO one;. **.
1442d 20 20 2a 2a 20 49 66 20 70 61 67 65 20 58 20 77 ** If page X w
1442e 65 72 65 20 6e 6f 74 20 77 72 69 74 74 65 6e 20 ere not written
1442f 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e to the sub-journ
14430 61 6c 20 68 65 72 65 2c 20 69 74 20 77 6f 75 6c al here, it woul
14431 64 20 6e 6f 74 0a 20 20 2a 2a 20 62 65 20 70 6f d not. ** be po
14432 73 73 69 62 6c 65 20 74 6f 20 72 65 73 74 6f 72 ssible to restor
14433 65 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 e its contents w
14434 68 65 6e 20 74 68 65 20 22 52 4f 4c 4c 42 41 43 hen the "ROLLBAC
14435 4b 20 54 4f 20 6f 6e 65 22 0a 20 20 2a 2a 20 73 K TO one". ** s
14436 74 61 74 65 6d 65 6e 74 20 77 65 72 65 20 69 73 tatement were is
14437 20 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2a processed.. **
14438 0a 20 20 2a 2a 20 73 75 62 6a 6f 75 72 6e 61 6c . ** subjournal
14439 50 61 67 65 28 29 20 6d 61 79 20 6e 65 65 64 20 Page() may need
1443a 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 to allocate spac
1443b 65 20 74 6f 20 73 74 6f 72 65 20 70 50 67 2d 3e e to store pPg->
1443c 70 67 6e 6f 20 69 6e 74 6f 0a 20 20 2a 2a 20 6f pgno into. ** o
1443d 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65 70 ne or more savep
1443e 6f 69 6e 74 20 62 69 74 76 65 63 73 2e 20 54 68 oint bitvecs. Th
1443f 69 73 20 69 73 20 74 68 65 20 72 65 61 73 6f 6e is is the reason
14440 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 this function.
14441 20 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 ** may return S
14442 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a QLITE_NOMEM.. *
14443 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61 /. if( pPg->fla
14444 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 0a 20 gs&PGHDR_DIRTY.
14445 20 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65 && subjRequire
14446 73 50 61 67 65 28 70 50 67 29 0a 20 20 20 26 26 sPage(pPg). &&
14447 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 SQLITE_OK!=(rc
14448 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 = subjournalPage
14449 28 70 50 67 29 29 0a 20 20 29 7b 0a 20 20 20 20 (pPg)). ){.
1444a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a return rc;. }..
1444b 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 4d PAGERTRACE(("M
1444c 4f 56 45 20 25 64 20 70 61 67 65 20 25 64 20 28 OVE %d page %d (
1444d 6e 65 65 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 needSync=%d) mov
1444e 65 73 20 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 es to %d\n", .
1444f 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 PAGERID(pPag
14450 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 er), pPg->pgno,
14451 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
14452 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 R_NEED_SYNC)?1:0
14453 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 49 4f 54 52 , pgno));. IOTR
14454 41 43 45 28 28 22 4d 4f 56 45 20 25 70 20 25 64 ACE(("MOVE %p %d
14455 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 %d\n", pPager,
14456 70 50 67 2d 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 pPg->pgno, pgno)
14457 29 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6a ).. /* If the j
14458 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 ournal needs to
14459 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f be sync()ed befo
1445a 72 65 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e re page pPg->pgn
1445b 6f 20 63 61 6e 0a 20 20 2a 2a 20 62 65 20 77 72 o can. ** be wr
1445c 69 74 74 65 6e 20 74 6f 2c 20 73 74 6f 72 65 20 itten to, store
1445d 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 6c 6f 63 pPg->pgno in loc
1445e 61 6c 20 76 61 72 69 61 62 6c 65 20 6e 65 65 64 al variable need
1445f 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 2a 2a 0a 20 SyncPgno.. **.
14460 20 2a 2a 20 49 66 20 74 68 65 20 69 73 43 6f 6d ** If the isCom
14461 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c mit flag is set,
14462 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 there is no nee
14463 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 d to remember th
14464 61 74 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 at. ** the jour
14465 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 nal needs to be
14466 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 sync()ed before
14467 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 50 database page pP
14468 67 2d 3e 70 67 6e 6f 20 0a 20 20 2a 2a 20 63 61 g->pgno . ** ca
14469 6e 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2e n be written to.
1446a 20 54 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 The caller has
1446b 61 6c 72 65 61 64 79 20 70 72 6f 6d 69 73 65 64 already promised
1446c 20 6e 6f 74 20 74 6f 20 77 72 69 74 65 20 74 6f not to write to
1446d 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 it.. */. if(
1446e 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 (pPg->flags&PGHD
1446f 52 5f 4e 45 45 44 5f 53 59 4e 43 29 20 26 26 20 R_NEED_SYNC) &&
14470 21 69 73 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 !isCommit ){.
14471 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 needSyncPgno =
14472 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 61 pPg->pgno;. a
14473 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 ssert( pageInJou
14474 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 rnal(pPg) || pPg
14475 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 ->pgno>pPager->d
14476 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 bOrigSize );.
14477 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c assert( pPg->fl
14478 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 ags&PGHDR_DIRTY
14479 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
1447a 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 Pager->needSync
1447b 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 );. }.. /* If
1447c 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 the cache contai
1447d 6e 73 20 61 20 70 61 67 65 20 77 69 74 68 20 70 ns a page with p
1447e 61 67 65 2d 6e 75 6d 62 65 72 20 70 67 6e 6f 2c age-number pgno,
1447f 20 72 65 6d 6f 76 65 20 69 74 0a 20 20 2a 2a 20 remove it. **
14480 66 72 6f 6d 20 69 74 73 20 68 61 73 68 20 63 68 from its hash ch
14481 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 66 20 74 68 ain. Also, if th
14482 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 e PgHdr.needSync
14483 20 77 61 73 20 73 65 74 20 66 6f 72 20 0a 20 20 was set for .
14484 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62 65 66 ** page pgno bef
14485 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27 20 6f ore the 'move' o
14486 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e 65 65 peration, it nee
14487 64 73 20 74 6f 20 62 65 20 72 65 74 61 69 6e 65 ds to be retaine
14488 64 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 d . ** for the
14489 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65 72 65 page moved there
1448a 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 2d 3e 66 6c .. */. pPg->fl
1448b 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 ags &= ~PGHDR_NE
1448c 45 44 5f 53 59 4e 43 3b 0a 20 20 70 50 67 4f 6c ED_SYNC;. pPgOl
1448d 64 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 d = pager_lookup
1448e 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a (pPager, pgno);.
1448f 20 20 61 73 73 65 72 74 28 20 21 70 50 67 4f 6c assert( !pPgOl
14490 64 20 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e 52 65 d || pPgOld->nRe
14491 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 70 50 f==1 );. if( pP
14492 67 4f 6c 64 20 29 7b 0a 20 20 20 20 70 50 67 2d gOld ){. pPg-
14493 3e 66 6c 61 67 73 20 7c 3d 20 28 70 50 67 4f 6c >flags |= (pPgOl
14494 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e d->flags&PGHDR_N
14495 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 EED_SYNC);. i
14496 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 f( MEMDB ){.
14497 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 69 73 63 /* Do not disc
14498 61 72 64 20 70 61 67 65 73 20 66 72 6f 6d 20 61 ard pages from a
14499 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
1449a 62 61 73 65 20 73 69 6e 63 65 20 77 65 20 6d 69 base since we mi
1449b 67 68 74 0a 20 20 20 20 20 20 2a 2a 20 6e 65 65 ght. ** nee
1449c 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6c 61 d to rollback la
1449d 74 65 72 2e 20 20 4a 75 73 74 20 6d 6f 76 65 20 ter. Just move
1449e 74 68 65 20 70 61 67 65 20 6f 75 74 20 6f 66 20 the page out of
1449f 74 68 65 20 77 61 79 2e 20 2a 2f 0a 20 20 20 20 the way. */.
144a0 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 assert( pPager
144a1 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b ->dbSizeValid );
144a2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 . sqlite3Pc
144a3 61 63 68 65 4d 6f 76 65 28 70 50 67 4f 6c 64 2c acheMove(pPgOld,
144a4 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b pPager->dbSize+
144a5 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1);. }else{.
144a6 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 sqlite3Pcac
144a7 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b 0a heDrop(pPgOld);.
144a8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6f 72 69 }. }.. ori
144a9 67 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e gPgno = pPg->pgn
144aa 6f 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 o;. sqlite3Pcac
144ab 68 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f heMove(pPg, pgno
144ac 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 );. sqlite3Pcac
144ad 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 heMakeDirty(pPg)
144ae 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f ;. pPager->dbMo
144af 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69 dified = 1;.. i
144b0 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 f( needSyncPgno
144b1 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 ){. /* If nee
144b2 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e dSyncPgno is non
144b3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
144b4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 journal file nee
144b5 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a ds to be . **
144b6 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 sync()ed before
144b7 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 any data is wri
144b8 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 tten to database
144b9 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 file page needS
144ba 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 yncPgno.. **
144bb 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 Currently, no su
144bc 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20 69 ch page exists i
144bd 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 n the page-cache
144be 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a and the . **
144bf 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 "is journaled"
144c0 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 bitvec flag has
144c1 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e been set. This n
144c2 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 eeds to be remed
144c3 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f ied by. ** lo
144c4 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 ading the page i
144c5 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 nto the pager-ca
144c6 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 che and setting
144c7 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 the PgHdr.needSy
144c8 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e nc . ** flag.
144c9 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 . **. ** I
144ca 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f f the attempt to
144cb 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 load the page i
144cc 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 nto the page-cac
144cd 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 he fails, (due.
144ce 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f ** to a mallo
144cf 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 c() or IO failur
144d0 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 e), clear the bi
144d1 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 t in the pInJour
144d2 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 nal[]. ** arr
144d3 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 ay. Otherwise, i
144d4 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f f the page is lo
144d5 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e aded and written
144d6 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a again in. **
144d7 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f this transactio
144d8 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 n, it may be wri
144d9 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 tten to the data
144da 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 base file before
144db 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 . ** it is sy
144dc 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f nced into the jo
144dd 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 urnal file. This
144de 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 way, it may end
144df 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 up in. ** th
144e0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 e journal file t
144e1 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 wice, but that i
144e2 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e s not a problem.
144e3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
144e4 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 he sqlite3PagerG
144e5 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 et() call may ca
144e6 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 use the journal
144e7 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 to sync. So make
144e8 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65 . ** sure the
144e9 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 Pager.needSync
144ea 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e flag is set too.
144eb 0a 20 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 . */. PgHd
144ec 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 61 r *pPgHdr;. a
144ed 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e ssert( pPager->n
144ee 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 72 eedSync );. r
144ef 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
144f0 47 65 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 Get(pPager, need
144f1 53 79 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 SyncPgno, &pPgHd
144f2 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d r);. if( rc!=
144f3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
144f4 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 if( needSyncP
144f5 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f gno<=pPager->dbO
144f6 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 rigSize ){.
144f7 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 assert( pPage
144f8 72 2d 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 r->pTmpSpace!=0
144f9 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
144fa 65 33 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 e3BitvecClear(pP
144fb 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c ager->pInJournal
144fc 2c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 , needSyncPgno,
144fd 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 pPager->pTmpSpac
144fe 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
144ff 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
14500 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e }. pPager->n
14501 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 eedSync = 1;.
14502 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d assert( pPager-
14503 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d >noSync==0 && !M
14504 45 4d 44 42 20 29 3b 0a 20 20 20 20 70 50 67 48 EMDB );. pPgH
14505 64 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 dr->flags |= PGH
14506 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 DR_NEED_SYNC;.
14507 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d sqlite3PcacheM
14508 61 6b 65 44 69 72 74 79 28 70 50 67 48 64 72 29 akeDirty(pPgHdr)
14509 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 ;. sqlite3Pag
1450a 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b erUnref(pPgHdr);
1450b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 . }.. /*. **
1450c 46 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 For an in-memory
1450d 20 64 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 database, make
1450e 73 75 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 sure the origina
1450f 6c 20 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 l page continues
14510 0a 20 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 . ** to exist,
14511 69 6e 20 63 61 73 65 20 74 68 65 20 74 72 61 6e in case the tran
14512 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f saction needs to
14513 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 20 55 73 65 roll back. Use
14514 20 70 50 67 4f 6c 64 0a 20 20 2a 2a 20 61 73 20 pPgOld. ** as
14515 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 the original pag
14516 65 20 73 69 6e 63 65 20 69 74 20 68 61 73 20 61 e since it has a
14517 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f lready been allo
14518 63 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 cated.. */. if
14519 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 73 ( MEMDB ){. s
1451a 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65 qlite3PcacheMove
1451b 28 70 50 67 4f 6c 64 2c 20 6f 72 69 67 50 67 6e (pPgOld, origPgn
1451c 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 o);. sqlite3P
1451d 61 67 65 72 55 6e 72 65 66 28 70 50 67 4f 6c 64 agerUnref(pPgOld
1451e 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e );. }.. return
1451f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
14520 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
14521 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 rn a pointer to
14522 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
14523 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e specified page.
14524 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14525 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
14526 50 61 67 65 72 47 65 74 44 61 74 61 28 44 62 50 PagerGetData(DbP
14527 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61 73 73 age *pPg){. ass
14528 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66 3e 30 ert( pPg->nRef>0
14529 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65 72 2d || pPg->pPager-
1452a 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65 74 75 >memDb );. retu
1452b 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 7d rn pPg->pData;.}
1452c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 ../*.** Return a
1452d 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
1452e 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62 79 74 Pager.nExtra byt
1452f 65 73 20 6f 66 20 22 65 78 74 72 61 22 20 73 70 es of "extra" sp
14530 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 ace .** allocate
14531 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 d along with the
14532 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65 2e specified page.
14533 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14534 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 TE void *sqlite3
14535 50 61 67 65 72 47 65 74 45 78 74 72 61 28 44 62 PagerGetExtra(Db
14536 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 Page *pPg){. re
14537 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74 72 61 turn pPg->pExtra
14538 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
14539 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d et the locking-m
1453a 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 ode for this pag
1453b 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d er. Parameter eM
1453c 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 0a ode must be one.
1453d 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f 43 4b ** of PAGER_LOCK
1453e 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c 20 50 INGMODE_QUERY, P
1453f 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
14540 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a 20 50 _NORMAL or .** P
14541 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 AGER_LOCKINGMODE
14542 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66 20 74 _EXCLUSIVE. If t
14543 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 he parameter is
14544 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68 65 6e not _QUERY, then
14545 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d .** the locking-
14546 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 74 mode is set to t
14547 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69 he value specifi
14548 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 ed..**.** The re
14549 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 turned value is
1454a 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c 4f 43 either PAGER_LOC
1454b 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 KINGMODE_NORMAL
1454c 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b or.** PAGER_LOCK
1454d 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 INGMODE_EXCLUSIV
1454e 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 E, indicating th
1454f 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73 73 69 e current (possi
14550 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a 2a 20 bly updated).**
14551 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a 2a 2f locking-mode..*/
14552 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14553 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 int sqlite3Pager
14554 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61 67 65 LockingMode(Page
14555 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 r *pPager, int e
14556 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72 74 28 Mode){. assert(
14557 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f eMode==PAGER_LO
14558 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 0a CKINGMODE_QUERY.
14559 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
1455a 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b Mode==PAGER_LOCK
1455b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 0a 20 INGMODE_NORMAL.
1455c 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d || eM
1455d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 ode==PAGER_LOCKI
1455e 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 NGMODE_EXCLUSIVE
1455f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 );. assert( PA
14560 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f GER_LOCKINGMODE_
14561 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61 73 73 QUERY<0 );. ass
14562 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 ert( PAGER_LOCKI
14563 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e 3d 30 NGMODE_NORMAL>=0
14564 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e && PAGER_LOCKIN
14565 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3e GMODE_EXCLUSIVE>
14566 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d 6f 64 =0 );. if( eMod
14567 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65 72 2d e>=0 && !pPager-
14568 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 >tempFile ){.
14569 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 pPager->exclusi
1456a 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f veMode = (u8)eMo
1456b 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e de;. }. return
1456c 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 (int)pPager->ex
1456d 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a clusiveMode;.}..
1456e 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 /*.** Get/set th
1456f 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 e journal-mode f
14570 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 or this pager. P
14571 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d arameter eMode m
14572 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a ust be one of:.*
14573 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f *.** PAGER_JO
14574 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a URNALMODE_QUERY.
14575 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
14576 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a NALMODE_DELETE.*
14577 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e * PAGER_JOURN
14578 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a ALMODE_TRUNCATE.
14579 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
1457a 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a NALMODE_PERSIST.
1457b 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 ** PAGER_JOUR
1457c 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 20 20 NALMODE_OFF.**
1457d 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d PAGER_JOURNALM
1457e 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a 2a 2a ODE_MEMORY.**.**
1457f 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 If the paramete
14580 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c r is not _QUERY,
14581 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 then the journa
14582 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f l_mode is set to
14583 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 the.** value sp
14584 65 63 69 66 69 65 64 20 69 66 20 74 68 65 20 63 ecified if the c
14585 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 hange is allowed
14586 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20 69 73 . The change is
14587 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a 20 66 disallowed.** f
14588 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 or the following
14589 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 reasons:.**.**
1458a 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 * An in-memor
1458b 79 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 6f y database can o
1458c 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a 6f 75 nly have its jou
1458d 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20 74 6f rnal_mode set to
1458e 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20 6f 72 _OFF.** or
1458f 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a 2a 20 _MEMORY..**.**
14590 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c * The journal
14591 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20 62 65 mode may not be
14592 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65 20 61 changed while a
14593 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
14594 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 active..**.** Th
14595 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 e returned indic
14596 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 ate the current
14597 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 (possibly update
14598 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 2e d) journal-mode.
14599 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1459a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 TE int sqlite3Pa
1459b 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 gerJournalMode(P
1459c 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e ager *pPager, in
1459d 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 t eMode){. asse
1459e 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 rt( eMode==PAGER
1459f 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 _JOURNALMODE_QUE
145a0 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c RY. |
145a1 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
145a2 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 OURNALMODE_DELET
145a3 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c E. ||
145a4 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f eMode==PAGER_JO
145a5 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 URNALMODE_TRUNCA
145a6 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c TE. |
145a7 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
145a8 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 OURNALMODE_PERSI
145a9 53 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c ST. |
145aa 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a | eMode==PAGER_J
145ab 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a OURNALMODE_OFF .
145ac 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 || e
145ad 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 Mode==PAGER_JOUR
145ae 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 NALMODE_MEMORY )
145af 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 ;. assert( PAGE
145b0 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 R_JOURNALMODE_QU
145b1 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28 20 65 ERY<0 );. if( e
145b2 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20 28 21 Mode>=0. && (!
145b3 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65 3d 3d MEMDB || eMode==
145b4 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 PAGER_JOURNALMOD
145b5 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 E_MEMORY .
145b6 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 || eMode
145b7 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d ==PAGER_JOURNALM
145b8 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26 20 21 ODE_OFF). && !
145b9 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 pPager->dbModifi
145ba 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f 70 65 ed. && (!isOpe
145bb 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c n(pPager->jfd) |
145bc 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 | 0==pPager->jou
145bd 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a 20 20 rnalOff). ){.
145be 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 if( isOpen(pPa
145bf 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 ger->jfd) ){.
145c0 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 sqlite3OsClos
145c1 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a e(pPager->jfd);.
145c2 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 }. pPager
145c3 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 ->journalMode =
145c4 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 (u8)eMode;. }.
145c5 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 50 61 return (int)pPa
145c6 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 ger->journalMode
145c7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 ;.}../*.** Get/s
145c8 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69 6d 69 et the size-limi
145c9 74 20 75 73 65 64 20 66 6f 72 20 70 65 72 73 69 t used for persi
145ca 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 stent journal fi
145cb 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 les..**.** Setti
145cc 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 ng the size limi
145cd 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20 6e 6f t to -1 means no
145ce 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f 72 63 limit is enforc
145cf 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 ed..** An attemp
145d0 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d 69 74 t to set a limit
145d1 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 2d 31 smaller than -1
145d2 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a is a no-op..*/.
145d3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
145d4 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 64 sqlite3PagerJ
145d5 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 ournalSizeLimit(
145d6 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 Pager *pPager, i
145d7 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 64 iLimit){. if
145d8 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a ( iLimit>=-1 ){.
145d9 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 pPager->jour
145da 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 nalSizeLimit = i
145db 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 Limit;. }. ret
145dc 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 urn pPager->jour
145dd 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a nalSizeLimit;.}.
145de 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
145df 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 pointer to the p
145e0 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 76 Pager->pBackup v
145e1 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62 61 63 ariable. The bac
145e2 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20 69 6e kup module.** in
145e3 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e 74 61 backup.c mainta
145e4 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 ins the content
145e5 6f 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 of this variable
145e6 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a 2a 2a . This module.**
145e7 20 75 73 65 73 20 69 74 20 6f 70 61 71 75 65 6c uses it opaquel
145e8 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 y as an argument
145e9 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63 6b 75 to sqlite3Backu
145ea 70 52 65 73 74 61 72 74 28 29 20 61 6e 64 0a 2a pRestart() and.*
145eb 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 * sqlite3BackupU
145ec 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a 2a 2f pdate() only..*/
145ed 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
145ee 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
145ef 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 *sqlite3PagerBac
145f0 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a 70 50 kupPtr(Pager *pP
145f1 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 ager){. return
145f2 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 &pPager->pBackup
145f3 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 ;.}..#endif /* S
145f4 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 QLITE_OMIT_DISKI
145f5 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a O */../*********
145f6 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 ***** End of pag
145f7 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a er.c ***********
145f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145fa 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
145fb 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
145fc 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
145fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
145ff 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 ****/./*.** 2007
14600 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a 2a 2a August 27.**.**
14601 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
14602 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
14603 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
14604 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
14605 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
14606 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
14607 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
14608 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
14609 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
1460a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
1460b 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
1460c 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
1460d 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
1460e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
1460f 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
14610 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
14611 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
14612 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14613 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14614 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14615 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14616 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 *********.**.**
14617 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
14618 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 ns code used to
14619 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 implement mutexe
1461a 73 20 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 s on Btree objec
1461b 74 73 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 ts..** This code
1461c 20 72 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 really belongs
1461d 69 6e 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74 in btree.c. But
1461e 20 62 74 72 65 65 2e 63 20 69 73 20 67 65 74 74 btree.c is gett
1461f 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 ing too.** big a
14620 6e 64 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72 nd we want to br
14621 65 61 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 eak it down some
14622 2e 20 20 54 68 69 73 20 70 61 63 6b 61 67 65 64 . This packaged
14623 20 73 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 seemed like.**
14624 61 20 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e a good breakout.
14625 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .*/./***********
14626 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 *** Include btre
14627 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 eInt.h in the mi
14628 64 64 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e ddle of btmutex.
14629 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a c **************
1462a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
1462b 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
1462c 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a treeInt.h ******
1462d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1462e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1462f 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 **/./*.** 2004 A
14630 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 pril 6.**.** The
14631 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d author disclaim
14632 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 s copyright to t
14633 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e his source code.
14634 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a In place of.**
14635 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c a legal notice,
14636 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 here is a bless
14637 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 ing:.**.** Ma
14638 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e y you do good an
14639 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 d not evil..**
1463a 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 May you find f
1463b 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 orgiveness for y
1463c 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 ourself and forg
1463d 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 ive others..**
1463e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 May you share
1463f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 freely, never ta
14640 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 king more than y
14641 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a ou give..**.****
14642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14646 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 *****.** This fi
14647 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 le implements a
14648 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62 external (disk-b
14649 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75 ased) database u
1464a 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20 sing BTrees..**
1464b 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64 For a detailed d
1464c 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72 iscussion of BTr
1464d 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a ees, refer to.**
1464e 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45 .** Donald E
1464f 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54 . Knuth, THE ART
14650 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f OF COMPUTER PRO
14651 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65 GRAMMING, Volume
14652 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74 3:.** "Sort
14653 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e ing And Searchin
14654 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38 g", pages 473-48
14655 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65 0. Addison-Wesle
14656 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68 y.** Publish
14657 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61 ing Company, Rea
14658 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65 ding, Massachuse
14659 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 tts..**.** The b
1465a 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61 asic idea is tha
1465b 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 t each page of t
1465c 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 he file contains
1465d 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65 N database.** e
1465e 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70 ntries and N+1 p
1465f 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61 ointers to subpa
14660 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d ges..**.** ---
14661 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14662 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14663 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
14664 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a -------------.**
14665 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b | Ptr(0) | K
14666 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c ey(0) | Ptr(1) |
14667 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20 Key(1) | ... |
14668 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e Key(N-1) | Ptr(N
14669 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d ) |.** -------
1466a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1466b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1466c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1466d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 ---------.**.**
1466e 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20 All of the keys
1466f 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74 on the page that
14670 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74 Ptr(0) points t
14671 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65 o have values le
14672 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30 ss.** than Key(0
14673 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b ). All of the k
14674 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28 eys on page Ptr(
14675 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 1) and its subpa
14676 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 ges have.** valu
14677 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 es greater than
14678 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20 Key(0) and less
14679 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c than Key(1). Al
1467a 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a l of the keys.**
1467b 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69 on Ptr(N) and i
1467c 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 ts subpages have
1467d 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 values greater
1467e 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20 than Key(N-1).
1467f 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e And.** so forth.
14680 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61 .**.** Finding a
14681 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 particular key
14682 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67 requires reading
14683 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73 O(log(M)) pages
14684 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69 from the .** di
14685 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68 sk where M is th
14686 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
14687 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e ies in the tree.
14688 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69 .**.** In this i
14689 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61 mplementation, a
1468a 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e single file can
1468b 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 hold one or mor
1468c 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42 e separate .** B
1468d 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72 Trees. Each BTr
1468e 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 ee is identified
1468f 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66 by the index of
14690 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 its root page.
14691 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 The.** key and
14692 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74 data for any ent
14693 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 ry are combined
14694 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79 to form the "pay
14695 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78 load". A.** fix
14696 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 ed amount of pay
14697 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72 load can be carr
14698 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20 ied directly on
14699 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 the database.**
1469a 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61 page. If the pa
1469b 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20 yload is larger
1469c 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20 than the preset
1469d 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70 amount then surp
1469e 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65 lus.** bytes are
1469f 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66 stored on overf
146a0 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 low pages. The
146a1 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65 payload for an e
146a2 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 ntry.** and the
146a3 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65 preceding pointe
146a4 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74 r are combined t
146a5 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e o form a "Cell".
146a6 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20 Each .** page
146a7 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64 has a small head
146a8 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e er which contain
146a9 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69 s the Ptr(N) poi
146aa 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a nter and other.*
146ab 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 * information su
146ac 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f ch as the size o
146ad 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a f key and data..
146ae 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54 **.** FORMAT DET
146af 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 AILS.**.** The f
146b0 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69 ile is divided i
146b1 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20 nto pages. The
146b2 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61 first page is ca
146b3 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20 lled page 1,.**
146b4 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61 the second is pa
146b5 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72 ge 2, and so for
146b6 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62 th. A page numb
146b7 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63 er of zero indic
146b8 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68 ates.** "no such
146b9 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67 page". The pag
146ba 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e e size can be an
146bb 79 20 70 6f 77 65 72 20 6f 66 20 32 20 62 65 74 y power of 2 bet
146bc 77 65 65 6e 20 35 31 32 20 61 6e 64 20 33 32 37 ween 512 and 327
146bd 36 38 2e 0a 2a 2a 20 45 61 63 68 20 70 61 67 65 68..** Each page
146be 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 can be either a
146bf 20 62 74 72 65 65 20 70 61 67 65 2c 20 61 20 66 btree page, a f
146c0 72 65 65 6c 69 73 74 20 70 61 67 65 2c 20 61 6e reelist page, an
146c1 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 overflow.** pag
146c2 65 2c 20 6f 72 20 61 20 70 6f 69 6e 74 65 72 2d e, or a pointer-
146c3 6d 61 70 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 map page..**.**
146c4 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69 The first page i
146c5 73 20 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 s always a btree
146c6 20 70 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 page. The firs
146c7 74 20 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 t 100 bytes of t
146c8 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 he first.** page
146c9 20 63 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 contain a speci
146ca 61 6c 20 68 65 61 64 65 72 20 28 74 68 65 20 22 al header (the "
146cb 66 69 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 file header") th
146cc 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 at describes the
146cd 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f file..** The fo
146ce 72 6d 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 rmat of the file
146cf 20 68 65 61 64 65 72 20 69 73 20 61 73 20 66 6f header is as fo
146d0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f llows:.**.** O
146d1 46 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 FFSET SIZE
146d2 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 DESCRIPTION.**
146d3 20 20 20 20 30 20 20 20 20 20 20 31 36 20 20 20 0 16
146d4 20 20 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a Header string:
146d5 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 "SQLite format
146d6 33 5c 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 3\000".** 16
146d7 20 20 20 20 20 20 20 32 20 20 20 20 20 50 61 67 2 Pag
146d8 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e e size in bytes.
146d9 20 20 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 .** 18
146da 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 1 File fo
146db 72 6d 61 74 20 77 72 69 74 65 20 76 65 72 73 69 rmat write versi
146dc 6f 6e 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 on.** 19
146dd 20 20 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 1 File fo
146de 72 6d 61 74 20 72 65 61 64 20 76 65 72 73 69 6f rmat read versio
146df 6e 0a 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 n.** 20
146e0 20 20 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 1 Bytes of
146e1 20 75 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 unused space at
146e2 20 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 the end of each
146e3 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 page.** 21
146e4 20 20 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 1 Max
146e5 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 embedded payload
146e6 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 fraction.**
146e7 20 32 32 20 20 20 20 20 20 20 31 20 20 20 20 20 22 1
146e8 4d 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 Min embedded pay
146e9 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
146ea 20 20 20 20 20 32 33 20 20 20 20 20 20 20 31 20 23 1
146eb 20 20 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 Min leaf pay
146ec 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a load fraction.**
146ed 20 20 20 20 20 32 34 20 20 20 20 20 20 20 34 20 24 4
146ee 20 20 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 File change
146ef 63 6f 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 counter.** 2
146f0 38 20 20 20 20 20 20 20 34 20 20 20 20 20 52 65 8 4 Re
146f1 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 served for futur
146f2 65 20 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 e use.** 32
146f3 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73 4 Firs
146f4 74 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a t freelist page.
146f5 2a 2a 20 20 20 20 20 33 36 20 20 20 20 20 20 20 ** 36
146f6 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 4 Number of
146f7 66 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 freelist pages i
146f8 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 n the file.**
146f9 20 20 34 30 20 20 20 20 20 20 36 30 20 20 20 20 40 60
146fa 20 31 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 15 4-byte meta
146fb 76 61 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f values passed to
146fc 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a higher layers.*
146fd 2a 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 *.** 40
146fe 20 20 34 20 20 20 20 20 53 63 68 65 6d 61 20 63 4 Schema c
146ff 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20 ookie.** 44
14700 20 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 4 File
14701 20 66 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d format of schem
14702 61 20 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34 a layer.** 4
14703 38 20 20 20 20 20 20 20 34 20 20 20 20 20 53 69 8 4 Si
14704 7a 65 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 ze of page cache
14705 0a 2a 2a 20 20 20 20 20 35 32 20 20 20 20 20 20 .** 52
14706 20 34 20 20 20 20 20 4c 61 72 67 65 73 74 20 72 4 Largest r
14707 6f 6f 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69 oot-page (auto/i
14708 6e 63 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20 ncr_vacuum).**
14709 20 20 20 35 36 20 20 20 20 20 20 20 34 20 20 20 56 4
1470a 20 20 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31 1=UTF-8 2=UTF1
1470b 36 6c 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a 6le 3=UTF16be.**
1470c 20 20 20 20 20 36 30 20 20 20 20 20 20 20 34 20 60 4
1470d 20 20 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e User version
1470e 0a 2a 2a 20 20 20 20 20 36 34 20 20 20 20 20 20 .** 64
1470f 20 34 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 4 Increment
14710 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a al vacuum mode.*
14711 2a 20 20 20 20 20 36 38 20 20 20 20 20 20 20 34 * 68 4
14712 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 unused.**
14713 20 20 20 37 32 20 20 20 20 20 20 20 34 20 20 20 72 4
14714 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 unused.**
14715 37 36 20 20 20 20 20 20 20 34 20 20 20 20 20 75 76 4 u
14716 6e 75 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 nused.**.** All
14717 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 of the integer v
14718 61 6c 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e alues are big-en
14719 64 69 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 dian (most signi
1471a 66 69 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 ficant byte firs
1471b 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 t)..**.** The fi
1471c 6c 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 le change counte
1471d 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 r is incremented
1471e 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 when the databa
1471f 73 65 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a se is changed.**
14720 20 54 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c This counter al
14721 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 lows other proce
14722 73 73 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 sses to know whe
14723 6e 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 n the file has c
14724 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 hanged.** and th
14725 75 73 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 us when they nee
14726 64 20 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 d to flush their
14727 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 cache..**.** Th
14728 65 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 e max embedded p
14729 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 ayload fraction
1472a 69 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 is the amount of
1472b 20 74 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c the total usabl
1472c 65 0a 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 e.** space in a
1472d 70 61 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 page that can be
1472e 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 consumed by a s
1472f 69 6e 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 ingle cell for s
14730 74 61 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 tandard.** B-tre
14731 65 20 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 e (non-LEAFDATA)
14732 20 74 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 tables. A valu
14733 65 20 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 e of 255 means 1
14734 30 30 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 00%. The defaul
14735 74 0a 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 t.** is to limit
14736 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c the maximum cel
14737 6c 20 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 l size so that a
14738 74 20 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 t least 4 cells
14739 77 69 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f will fit.** on o
1473a 6e 65 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 ne page. Thus t
1473b 68 65 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 he default max e
1473c 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 mbedded payload
1473d 66 72 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a fraction is 64..
1473e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 **.** If the pay
1473f 6c 6f 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 load for a cell
14740 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 is larger than t
14741 68 65 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 he max payload,
14742 74 68 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 then extra.** pa
14743 79 6c 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 yload is spilled
14744 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 to overflow pag
14745 65 73 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 es. Once an ove
14746 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c rflow page is al
14747 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d located,.** as m
14748 61 6e 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 any bytes as pos
14749 73 69 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 sible are moved
1474a 69 6e 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f into the overflo
1474b 77 20 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 w pages without
1474c 6c 65 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 letting.** the c
1474d 65 6c 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 ell size drop be
1474e 6c 6f 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 low the min embe
1474f 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 dded payload fra
14750 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 ction..**.** The
14751 20 6d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 min leaf payloa
14752 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 d fraction is li
14753 6b 65 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 ke the min embed
14754 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 ded payload frac
14755 74 69 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 tion.** except t
14756 68 61 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 hat it applies t
14757 6f 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 o leaf nodes in
14758 61 20 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e a LEAFDATA tree.
14759 20 20 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a The maximum.**
1475a 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f payload fractio
1475b 6e 20 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 n for a LEAFDATA
1475c 20 74 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 tree is always
1475d 31 30 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 100% (or 255) an
1475e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 d it.** not spec
1475f 69 66 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 ified in the hea
14760 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 der..**.** Each
14761 62 74 72 65 65 20 70 61 67 65 73 20 69 73 20 64 btree pages is d
14762 69 76 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 ivided into thre
14763 65 20 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 e sections: The
14764 20 68 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 header, the.**
14765 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 cell pointer arr
14766 61 79 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c ay, and the cell
14767 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 content area.
14768 50 61 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 Page 1 also has
14769 61 20 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 a 100-byte.** fi
1476a 6c 65 20 68 65 61 64 65 72 20 74 68 61 74 20 6f le header that o
1476b 63 63 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 ccurs before the
1476c 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a page header..**
1476d 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d .** |------
1476e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 ----------|.**
1476f 20 20 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 | file heade
14770 72 20 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 r | 100 byt
14771 65 73 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 es. Page 1 only
14772 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d ..** |-----
14773 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 -----------|.**
14774 20 20 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 | page head
14775 65 72 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 er | 8 byte
14776 73 20 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 s for leaves. 1
14777 32 20 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 2 bytes for inte
14778 72 69 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 rior nodes.**
14779 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |------------
1477a 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 ----|.** |
1477b 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c cell pointer |
1477c 20 20 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 | 2 bytes pe
1477d 72 20 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 r cell. Sorted
1477e 6f 72 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c order..** |
1477f 20 61 72 72 61 79 20 20 20 20 20 20 20 20 20 20 array
14780 7c 20 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 | | Grows dow
14781 6e 77 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 nward.** |
14782 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c |
14783 20 20 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d v.** |--
14784 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a --------------|.
14785 2a 2a 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f ** | unallo
14786 63 61 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 cated |.**
14787 20 20 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 | space
14788 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d |.** |-
14789 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c ---------------|
1478a 20 20 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 ^ Grows upwa
1478b 72 64 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 rds.** | ce
1478c 6c 6c 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 ll content |
1478d 20 7c 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 | Arbitrary or
1478e 64 65 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 der interspersed
1478f 20 77 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 with freeblocks
14790 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 ..** | area
14791 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c | |
14792 20 20 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 and free space
14793 20 66 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 fragments..**
14794 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |-----------
14795 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 -----|.**.** The
14796 20 70 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f page headers lo
14797 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a oks like this:.*
14798 2a 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 *.** OFFSET
14799 53 49 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
1479a 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 TION.** 0
1479b 20 20 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 1 Flag
1479c 73 2e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a s. 1: intkey, 2:
1479d 20 7a 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 zerodata, 4: le
1479e 61 66 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a afdata, 8: leaf.
1479f 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20 20 ** 1
147a0 32 20 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 2 byte offs
147a1 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 et to the first
147a2 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 freeblock.**
147a3 20 20 33 20 20 20 20 20 20 20 32 20 20 20 20 20 3 2
147a4 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 number of cells
147a5 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a on this page.**
147a6 20 20 20 20 20 20 35 20 20 20 20 20 20 20 32 20 5 2
147a7 20 20 20 20 20 66 69 72 73 74 20 62 79 74 65 20 first byte
147a8 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 of the cell cont
147a9 65 6e 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 ent area.**
147aa 20 37 20 20 20 20 20 20 20 31 20 20 20 20 20 20 7 1
147ab 6e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 number of fragme
147ac 6e 74 65 64 20 66 72 65 65 20 62 79 74 65 73 0a nted free bytes.
147ad 2a 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 ** 8
147ae 34 20 20 20 20 20 20 52 69 67 68 74 20 63 68 69 4 Right chi
147af 6c 64 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 ld (the Ptr(N) v
147b0 61 6c 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 alue). Omitted
147b1 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a on leaves..**.**
147b2 20 54 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e The flags defin
147b3 65 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 e the format of
147b4 74 68 69 73 20 62 74 72 65 65 20 70 61 67 65 2e this btree page.
147b5 20 20 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 The leaf flag
147b6 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 means that.** th
147b7 69 73 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 is page has no c
147b8 68 69 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 hildren. The ze
147b9 72 6f 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e rodata flag mean
147ba 73 20 74 68 61 74 20 74 68 69 73 20 70 61 67 65 s that this page
147bb 20 63 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 carries.** only
147bc 20 6b 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 keys and no dat
147bd 61 2e 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 a. The intkey f
147be 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 lag means that t
147bf 68 65 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 he key is a inte
147c0 67 65 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 ger.** which is
147c1 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 stored in the ke
147c2 79 20 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 y size entry of
147c3 74 68 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 the cell header
147c4 72 61 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a rather than in.*
147c5 2a 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 * the payload ar
147c6 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 ea..**.** The ce
147c7 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 ll pointer array
147c8 20 62 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 begins on the f
147c9 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 irst byte after
147ca 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2e the page header.
147cb 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 .** The cell poi
147cc 6e 74 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 nter array conta
147cd 69 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 ins zero or more
147ce 20 32 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 2-byte numbers
147cf 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 which are.** off
147d0 73 65 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 sets from the be
147d1 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 ginning of the p
147d2 61 67 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 age to the cell
147d3 63 6f 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 content in the c
147d4 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 ell.** content a
147d5 72 65 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 rea. The cell p
147d6 6f 69 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e ointers occur in
147d7 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 sorted order.
147d8 54 68 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 The system striv
147d9 65 73 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 es.** to keep fr
147da 65 65 20 73 70 61 63 65 20 61 66 74 65 72 20 74 ee space after t
147db 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 he last cell poi
147dc 6e 74 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 nter so that new
147dd 20 63 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 cells can.** be
147de 20 65 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 easily added wi
147df 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 thout having to
147e0 64 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 defragment the p
147e1 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 age..**.** Cell
147e2 63 6f 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 content is store
147e3 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e d at the very en
147e4 64 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e d of the page an
147e5 64 20 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 d grows toward t
147e6 68 65 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 he.** beginning
147e7 6f 66 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a of the page..**.
147e8 2a 2a 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 ** Unused space
147e9 77 69 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 within the cell
147ea 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 content area is
147eb 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 collected into a
147ec 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a linked list of.
147ed 2a 2a 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 ** freeblocks.
147ee 45 61 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 Each freeblock i
147ef 73 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 s at least 4 byt
147f0 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 es in size. The
147f1 20 62 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 byte offset.**
147f2 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72 65 to the first fre
147f3 65 62 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 eblock is given
147f4 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 in the header.
147f5 46 72 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 Freeblocks occur
147f6 20 69 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e in.** increasin
147f7 67 20 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 g order. Becaus
147f8 65 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 e a freeblock mu
147f9 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 st be at least 4
147fa 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a bytes in size,.
147fb 2a 2a 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 ** any group of
147fc 33 20 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 3 or fewer unuse
147fd 64 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 d bytes in the c
147fe 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 ell content area
147ff 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 cannot.** exist
14800 20 6f 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 on the freebloc
14801 6b 20 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 k chain. A grou
14802 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 p of 3 or fewer
14803 66 72 65 65 20 62 79 74 65 73 20 69 73 20 63 61 free bytes is ca
14804 6c 6c 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 lled.** a fragme
14805 6e 74 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e nt. The total n
14806 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
14807 6e 20 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 n all fragments
14808 69 73 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 is recorded..**
14809 69 6e 20 74 68 65 20 70 61 67 65 20 68 65 61 64 in the page head
1480a 65 72 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a er at offset 7..
1480b 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 **.** SIZE
1480c 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 DESCRIPTION.**
1480d 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 20 2 Byte
1480e 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 offset of the ne
1480f 78 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 xt freeblock.**
14810 20 20 20 20 20 32 20 20 20 20 20 42 79 74 65 73 2 Bytes
14811 20 69 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f in this freeblo
14812 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 ck.**.** Cells a
14813 72 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c re of variable l
14814 65 6e 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 ength. Cells ar
14815 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
14816 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 cell content are
14817 61 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 a at.** the end
14818 6f 66 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f of the page. Po
14819 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 inters to the ce
1481a 6c 6c 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 lls are in the c
1481b 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
1481c 79 0a 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 y.** that immedi
1481d 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 ately follows th
1481e 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 e page header.
1481f 43 65 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 Cells is not nec
14820 65 73 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 essarily.** cont
14821 69 67 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 iguous or in ord
14822 65 72 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 er, but cell poi
14823 6e 74 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 nters are contig
14824 75 6f 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 uous and in orde
14825 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f r..**.** Cell co
14826 6e 74 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 ntent makes use
14827 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 of variable leng
14828 74 68 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 th integers. A
14829 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 variable.** leng
1482a 74 68 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 th integer is 1
1482b 74 6f 20 39 20 62 79 74 65 73 20 77 68 65 72 65 to 9 bytes where
1482c 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 the lower 7 bit
1482d 73 20 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 s of each .** by
1482e 74 65 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 te are used. Th
1482f 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 e integer consis
14830 74 73 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 ts of all bytes
14831 74 68 61 74 20 68 61 76 65 20 62 69 74 20 38 20 that have bit 8
14832 73 65 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 set and.** the f
14833 69 72 73 74 20 62 79 74 65 20 77 69 74 68 20 62 irst byte with b
14834 69 74 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 it 8 clear. The
14835 20 6d 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e most significan
14836 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e t byte of the in
14837 74 65 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 teger.** appears
14838 20 66 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 first. A varia
14839 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 ble-length integ
1483a 65 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f er may not be mo
1483b 72 65 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 re than 9 bytes
1483c 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 long..** As a sp
1483d 65 63 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 ecial case, all
1483e 38 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 8 bytes of the 9
1483f 74 68 20 62 79 74 65 20 61 72 65 20 75 73 65 64 th byte are used
14840 20 61 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a as data. This.
14841 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 ** allows a 64-b
14842 69 74 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 it integer to be
14843 20 65 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 encoded in 9 by
14844 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 tes..**.** 0x
14845 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00
14846 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
14847 20 30 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 0x00000000.**
14848 20 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 0x7f
14849 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f beco
1484a 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a mes 0x0000007f.
1484b 2a 2a 20 20 20 20 30 78 38 31 20 30 78 30 30 20 ** 0x81 0x00
1484c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1484d 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 becomes 0x00000
1484e 30 38 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 080.** 0x82 0
1484f 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 x00
14850 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 becomes 0x0
14851 30 30 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 0000100.** 0x
14852 38 30 20 30 78 37 66 20 20 20 20 20 20 20 20 20 80 0x7f
14853 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 becomes
14854 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 0x0000007f.**
14855 20 20 30 78 38 61 20 30 78 39 31 20 30 78 64 31 0x8a 0x91 0xd1
14856 20 30 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 0xac 0x78 beco
14857 6d 65 73 20 20 30 78 31 32 33 34 35 36 37 38 0a mes 0x12345678.
14858 2a 2a 20 20 20 20 30 78 38 31 20 30 78 38 31 20 ** 0x81 0x81
14859 30 78 38 31 20 30 78 38 31 20 30 78 30 31 20 20 0x81 0x81 0x01
1485a 62 65 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 becomes 0x10204
1485b 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 081.**.** Variab
1485c 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 le length intege
1485d 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 rs are used for
1485e 72 6f 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f rowids and to ho
1485f 6c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ld the number of
14860 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 .** bytes of key
14861 20 61 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 and data in a b
14862 74 72 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a tree cell..**.**
14863 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 The content of
14864 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b a cell looks lik
14865 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 e this:.**.**
14866 20 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 SIZE DESCRIP
14867 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 TION.** 4
14868 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f Page number o
14869 66 20 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 f the left child
1486a 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 . Omitted if lea
1486b 66 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a f flag is set..*
1486c 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d * var Num
1486d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 ber of bytes of
1486e 64 61 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 data. Omitted if
1486f 20 74 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c the zerodata fl
14870 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 ag is set..**
14871 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 var Number
14872 6f 66 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e of bytes of key.
14873 20 4f 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 Or the key itse
14874 6c 66 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 lf if intkey fla
14875 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 g is set..**
14876 20 20 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a * Payload.
14877 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 46 69 ** 4 Fi
14878 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 rst page of the
14879 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 overflow chain.
1487a 20 4f 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f Omitted if no o
1487b 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 verflow.**.** Ov
1487c 65 72 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 erflow pages for
1487d 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e m a linked list.
1487e 20 20 45 61 63 68 20 70 61 67 65 20 65 78 63 65 Each page exce
1487f 70 74 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 pt the last is c
14880 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c ompletely.** fil
14881 6c 65 64 20 77 69 74 68 20 64 61 74 61 20 28 70 led with data (p
14882 61 67 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 agesize - 4 byte
14883 73 29 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 s). The last pa
14884 67 65 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c ge can have as l
14885 69 74 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 ittle.** as 1 by
14886 74 65 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a te of data..**.*
14887 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
14888 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
14889 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
1488a 65 72 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 er of next overf
1488b 6c 6f 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 low page.**
1488c 20 2a 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a * Data.**.*
1488d 2a 20 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 * Freelist pages
1488e 20 63 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 come in two sub
1488f 74 79 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 types: trunk pag
14890 65 73 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 es and leaf page
14891 73 2e 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 s. The.** file
14892 68 65 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f header points to
14893 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 the first in a
14894 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 linked list of t
14895 72 75 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 runk page. Each
14896 20 74 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 trunk.** page p
14897 6f 69 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c oints to multipl
14898 65 20 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 e leaf pages. T
14899 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 he content of a
1489a 6c 65 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 leaf page is.**
1489b 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 unspecified. A
1489c 74 72 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 trunk page looks
1489d 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a like this:.**.*
1489e 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 * SIZE DES
1489f 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 CRIPTION.**
148a0 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 4 Page numb
148a1 65 72 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b er of next trunk
148a2 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 page.** 4
148a3 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 Number of le
148a4 61 66 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 af pointers on t
148a5 68 69 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 his page.**
148a6 20 2a 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d * zero or m
148a7 6f 72 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 ore pages number
148a8 73 20 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a s of leaves.*/..
148a9 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ./* The followin
148aa 67 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d g value is the m
148ab 61 78 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 aximum cell size
148ac 20 61 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 assuming a maxi
148ad 6d 75 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 mum page.** size
148ae 20 67 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a give above..*/.
148af 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f #define MX_CELL_
148b0 53 49 5a 45 28 70 42 74 29 20 20 28 70 42 74 2d SIZE(pBt) (pBt-
148b1 3e 70 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a >pageSize-8)../*
148b2 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d The maximum num
148b3 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 ber of cells on
148b4 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 a single page of
148b5 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
148b6 54 68 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 This.** assumes
148b7 61 20 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 a minimum cell s
148b8 69 7a 65 20 6f 66 20 36 20 62 79 74 65 73 20 20 ize of 6 bytes
148b9 28 34 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 (4 bytes for the
148ba 20 63 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 cell itself.**
148bb 70 6c 75 73 20 32 20 62 79 74 65 73 20 66 6f 72 plus 2 bytes for
148bc 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 74 68 the index to th
148bd 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 e cell in the pa
148be 67 65 20 68 65 61 64 65 72 29 2e 20 20 53 75 63 ge header). Suc
148bf 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 h.** small cells
148c0 20 77 69 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 will be rare, b
148c1 75 74 20 74 68 65 79 20 61 72 65 20 70 6f 73 73 ut they are poss
148c2 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 ible..*/.#define
148c3 20 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 MX_CELL(pBt) ((
148c4 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 pBt->pageSize-8)
148c5 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 /6)../* Forward
148c6 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a declarations */.
148c7 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d typedef struct M
148c8 65 6d 50 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a emPage MemPage;.
148c9 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42 typedef struct B
148ca 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f tLock BtLock;../
148cb 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d *.** This is a m
148cc 61 67 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 agic string that
148cd 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 appears at the
148ce 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 beginning of eve
148cf 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 ry.** SQLite dat
148d0 61 62 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74 abase in order t
148d1 6f 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 66 o identify the f
148d2 69 6c 65 20 61 73 20 61 20 72 65 61 6c 20 64 61 ile as a real da
148d3 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f tabase..**.** Yo
148d4 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69 u can change thi
148d5 73 20 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 s value at compi
148d6 6c 65 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69 le-time by speci
148d7 66 79 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c fying a.** -DSQL
148d8 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d ITE_FILE_HEADER=
148d9 22 2e 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d "..." on the com
148da 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 piler command-li
148db 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 ne. The.** head
148dc 65 72 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 er must be exact
148dd 6c 79 20 31 36 20 62 79 74 65 73 20 69 6e 63 6c ly 16 bytes incl
148de 75 64 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 uding the zero-t
148df 65 72 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 erminator so.**
148e0 74 68 65 20 73 74 72 69 6e 67 20 69 74 73 65 6c the string itsel
148e1 66 20 73 68 6f 75 6c 64 20 62 65 20 31 35 20 63 f should be 15 c
148e2 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 haracters long.
148e3 20 49 66 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a If you change.*
148e4 2a 20 74 68 65 20 68 65 61 64 65 72 2c 20 74 68 * the header, th
148e5 65 6e 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c en your custom l
148e6 69 62 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 ibrary will not
148e7 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 be able to read
148e8 0a 2a 2a 20 64 61 74 61 62 61 73 65 73 20 67 65 .** databases ge
148e9 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 73 nerated by the s
148ea 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e tandard tools an
148eb 64 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 d the standard t
148ec 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 ools.** will not
148ed 20 62 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 be able to read
148ee 20 64 61 74 61 62 61 73 65 73 20 63 72 65 61 74 databases creat
148ef 65 64 20 62 79 20 79 6f 75 72 20 63 75 73 74 6f ed by your custo
148f0 6d 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 m library..*/.#i
148f1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 46 49 4c fndef SQLITE_FIL
148f2 45 5f 48 45 41 44 45 52 20 2f 2a 20 31 32 33 34 E_HEADER /* 1234
148f3 35 36 37 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 56789 123456 */.
148f4 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 # define SQLITE
148f5 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 22 53 51 _FILE_HEADER "SQ
148f6 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 Lite format 3".#
148f7 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 endif../*.** Pag
148f8 65 20 74 79 70 65 20 66 6c 61 67 73 2e 20 20 41 e type flags. A
148f9 6e 20 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 n ORed combinati
148fa 6f 6e 20 6f 66 20 74 68 65 73 65 20 66 6c 61 67 on of these flag
148fb 73 20 61 70 70 65 61 72 20 61 73 20 74 68 65 0a s appear as the.
148fc 2a 2a 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 ** first byte of
148fd 20 6f 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f on-disk image o
148fe 66 20 65 76 65 72 79 20 42 54 72 65 65 20 70 61 f every BTree pa
148ff 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 ge..*/.#define P
14900 54 46 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30 TF_INTKEY 0x0
14901 31 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 1.#define PTF_ZE
14902 52 4f 44 41 54 41 20 20 30 78 30 32 0a 23 64 65 RODATA 0x02.#de
14903 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 44 41 54 fine PTF_LEAFDAT
14904 41 20 20 30 78 30 34 0a 23 64 65 66 69 6e 65 20 A 0x04.#define
14905 50 54 46 5f 4c 45 41 46 20 20 20 20 20 20 30 78 PTF_LEAF 0x
14906 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 08../*.** As eac
14907 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 h page of the fi
14908 6c 65 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 le is loaded int
14909 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 o memory, an ins
1490a 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
1490b 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 lowing.** struct
1490c 75 72 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 ure is appended
1490d 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 and initialized
1490e 74 6f 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 to zero. This s
1490f 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73 0a tructure stores.
14910 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ** information a
14911 62 6f 75 74 20 74 68 65 20 70 61 67 65 20 74 68 bout the page th
14912 61 74 20 69 73 20 64 65 63 6f 64 65 64 20 66 72 at is decoded fr
14913 6f 6d 20 74 68 65 20 72 61 77 20 66 69 6c 65 20 om the raw file
14914 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 page..**.** The
14915 70 50 61 72 65 6e 74 20 66 69 65 6c 64 20 70 6f pParent field po
14916 69 6e 74 73 20 62 61 63 6b 20 74 6f 20 74 68 65 ints back to the
14917 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 20 54 parent page. T
14918 68 69 73 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f his allows us to
14919 0a 2a 2a 20 77 61 6c 6b 20 75 70 20 74 68 65 20 .** walk up the
1491a 42 54 72 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c BTree from any l
1491b 65 61 66 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e eaf to the root.
1491c 20 20 43 61 72 65 20 6d 75 73 74 20 62 65 20 74 Care must be t
1491d 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 aken to.** unref
1491e 28 29 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 () the parent pa
1491f 67 65 20 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 ge pointer when
14920 74 68 69 73 20 70 61 67 65 20 69 73 20 6e 6f 20 this page is no
14921 6c 6f 6e 67 65 72 20 72 65 66 65 72 65 6e 63 65 longer reference
14922 64 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 44 65 d..** The pageDe
14923 73 74 72 75 63 74 6f 72 28 29 20 72 6f 75 74 69 structor() routi
14924 6e 65 20 68 61 6e 64 6c 65 73 20 74 68 61 74 20 ne handles that
14925 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 chore..**.** Acc
14926 65 73 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 ess to all field
14927 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 s of this struct
14928 75 72 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 ure is controlle
14929 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a d by the mutex.*
1492a 2a 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 * stored in MemP
1492b 61 67 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a age.pBt->mutex..
1492c 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67 */.struct MemPag
1492d 65 20 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b e {. u8 isInit;
1492e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1492f 75 65 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79 ue if previously
14930 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 initialized. MU
14931 53 54 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a ST BE FIRST! */.
14932 20 20 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 u8 nOverflow;
14933 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14934 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c of overflow cel
14935 6c 20 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c l bodies in aCel
14936 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b l[] */. u8 intK
14937 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ey; /*
14938 20 54 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20 True if intkey
14939 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 flag is set */.
1493a 20 75 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20 u8 leaf;
1493b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1493c 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 leaf flag is se
1493d 74 20 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74 t */. u8 hasDat
1493e 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 a; /* T
1493f 72 75 65 20 69 66 20 74 68 69 73 20 70 61 67 65 rue if this page
14940 20 73 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a stores data */.
14941 20 20 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20 u8 hdrOffset;
14942 20 20 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f /* 100 fo
14943 72 20 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68 r page 1. 0 oth
14944 65 72 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 erwise */. u8 c
14945 68 69 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20 hildPtrSize;
14946 20 2f 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 /* 0 if leaf==1
14947 2e 20 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 . 4 if leaf==0
14948 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 */. u16 maxLoca
14949 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 l; /* Cop
1494a 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61 y of BtShared.ma
1494b 78 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 xLocal or BtShar
1494c 65 64 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 ed.maxLeaf */.
1494d 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 u16 minLocal;
1494e 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 /* Copy of
1494f 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 BtShared.minLoca
14950 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69 l or BtShared.mi
14951 6e 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 nLeaf */. u16 c
14952 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 ellOffset;
14953 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74 /* Index in aDat
14954 61 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 a of first cell
14955 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 pointer */. u16
14956 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 nFree;
14957 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 /* Number of f
14958 72 65 65 20 62 79 74 65 73 20 6f 6e 20 74 68 65 ree bytes on the
14959 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e page */. u16 n
1495a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 Cell;
1495b 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c /* Number of cel
1495c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c ls on this page,
1495d 20 6c 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 local and ovfl
1495e 2a 2f 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67 */. u16 maskPag
1495f 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 e; /* Mas
14960 6b 20 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65 k for page offse
14961 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f t */. struct _O
14962 76 66 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 vflCell { /* C
14963 65 6c 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e ells that will n
14964 6f 74 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b ot fit on aData[
14965 5d 20 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 ] */. u8 *pCe
14966 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ll; /*
14967 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 Pointers to the
14968 62 6f 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72 body of the over
14969 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 flow cell */.
1496a 20 75 31 36 20 69 64 78 3b 20 20 20 20 20 20 20 u16 idx;
1496b 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 /* Insert t
1496c 68 69 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 his cell before
1496d 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 idx-th non-overf
1496e 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 low cell */. }
1496f 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 aOvfl[5];. BtSh
14970 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
14971 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 /* Pointer to B
14972 74 53 68 61 72 65 64 20 74 68 61 74 20 74 68 69 tShared that thi
14973 73 20 70 61 67 65 20 69 73 20 70 61 72 74 20 6f s page is part o
14974 66 20 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 f */. u8 *aData
14975 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 ; /* P
14976 6f 69 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 69 ointer to disk i
14977 6d 61 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 mage of the page
14978 20 64 61 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 data */. DbPag
14979 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 20 20 e *pDbPage;
1497a 2f 2a 20 50 61 67 65 72 20 70 61 67 65 20 68 61 /* Pager page ha
1497b 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 ndle */. Pgno p
1497c 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 2f gno; /
1497d 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f * Page number fo
1497e 72 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d r this page */.}
1497f 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d ;../*.** The in-
14980 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 memory image of
14981 61 20 64 69 73 6b 20 70 61 67 65 20 68 61 73 20 a disk page has
14982 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e the auxiliary in
14983 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 formation append
14984 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 ed.** to the end
14985 2e 20 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73 . EXTRA_SIZE is
14986 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 the number of b
14987 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65 ytes of space ne
14988 65 64 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 eded to hold.**
14989 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 that extra infor
1498a 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 mation..*/.#defi
1498b 6e 65 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69 ne EXTRA_SIZE si
1498c 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f zeof(MemPage)../
1498d 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 *.** A linked li
1498e 73 74 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 st of the follow
1498f 69 6e 67 20 73 74 72 75 63 74 75 72 65 73 20 69 ing structures i
14990 73 20 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 s stored at BtSh
14991 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c ared.pLock..** L
14992 6f 63 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 ocks are added (
14993 6f 72 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d or upgraded from
14994 20 52 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 READ_LOCK to WR
14995 49 54 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 ITE_LOCK) when a
14996 20 63 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f cursor .** is o
14997 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 pened on the tab
14998 6c 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 le with root pag
14999 65 20 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c e BtShared.iTabl
1499a 65 2e 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d e. Locks are rem
1499b 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 oved.** from thi
1499c 73 20 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 s list when a tr
1499d 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d ansaction is com
1499e 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 mitted or rolled
1499f 20 62 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a back, or when.*
149a0 2a 20 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 * a btree handle
149a1 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 is closed..*/.s
149a2 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 truct BtLock {.
149a3 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 Btree *pBtree;
149a4 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 /* Btree
149a5 68 61 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 handle holding t
149a6 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 his lock */. Pg
149a7 6e 6f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 no iTable;
149a8 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 /* Root page
149a9 20 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 of table */. u
149aa 38 20 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 8 eLock;
149ab 20 20 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 /* READ_LOC
149ac 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 K or WRITE_LOCK
149ad 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 */. BtLock *pNe
149ae 78 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 xt; /* Ne
149af 78 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 xt in BtShared.p
149b0 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a Lock list */.};.
149b1 0a 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 ./* Candidate va
149b2 6c 75 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e lues for BtLock.
149b3 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 eLock */.#define
149b4 20 52 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 READ_LOCK 1
149b5 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c .#define WRITE_L
149b6 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a 20 41 20 42 OCK 2../* A B
149b7 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a tree handle.**.*
149b8 2a 20 41 20 64 61 74 61 62 61 73 65 20 63 6f 6e * A database con
149b9 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 nection contains
149ba 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e a pointer to an
149bb 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 instance of.**
149bc 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 this object for
149bd 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20 66 every database f
149be 69 6c 65 20 74 68 61 74 20 69 74 20 68 61 73 20 ile that it has
149bf 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 open. This stru
149c0 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 71 cture.** is opaq
149c1 75 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 ue to the databa
149c2 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 se connection.
149c3 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e The database con
149c4 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a nection cannot.*
149c5 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65 72 6e * see the intern
149c6 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 als of this stru
149c7 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64 cture and only d
149c8 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 eals with pointe
149c9 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 74 rs to.** this st
149ca 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 ructure..**.** F
149cb 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65 or some database
149cc 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65 files, the same
149cd 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 underlying data
149ce 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 68 74 base cache might
149cf 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 be .** shared b
149d0 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 20 etween multiple
149d1 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 6e connections. In
149d2 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 63 68 that case, each
149d3 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 68 connection.** h
149d4 61 73 20 69 74 20 6f 77 6e 20 69 6e 73 74 61 6e as it own instan
149d5 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
149d6 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e 73 t. But each ins
149d7 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
149d8 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 ject.** points t
149d9 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 61 o the same BtSha
149da 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 red object. The
149db 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20 database cache
149dc 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d and the.** schem
149dd 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 a associated wit
149de 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 h the database f
149df 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74 ile are all cont
149e0 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 ained within.**
149e1 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a the BtShared obj
149e2 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 ect..**.** All f
149e3 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 ields in this st
149e4 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 ructure are acce
149e5 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74 ssed under sqlit
149e6 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 e3.mutex..** The
149e7 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73 pBt pointer its
149e8 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 elf may not be c
149e9 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65 hanged while the
149ea 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f 72 re exists cursor
149eb 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66 s .** in the ref
149ec 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65 64 erenced BtShared
149ed 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b that point back
149ee 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20 73 to this Btree s
149ef 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75 ince those.** cu
149f0 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f rsors have to do
149f1 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73 go through this
149f2 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74 Btree to find t
149f3 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 6e heir BtShared an
149f4 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20 d.** they often
149f5 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f do so without ho
149f6 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 lding sqlite3.mu
149f7 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 tex..*/.struct B
149f8 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 tree {. sqlite3
149f9 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 *db; /* T
149fa 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
149fb 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 ection holding t
149fc 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 his btree */. B
149fd 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 tShared *pBt;
149fe 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f /* Sharable co
149ff 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74 ntent of this bt
14a00 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 ree */. u8 inTr
14a01 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 ans; /* T
14a02 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 RANS_NONE, TRANS
14a03 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 _READ or TRANS_W
14a04 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61 RITE */. u8 sha
14a05 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 rable; /*
14a06 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 73 True if we can s
14a07 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61 6e hare pBt with an
14a08 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 38 other db */. u8
14a09 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 locked;
14a0a 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 63 /* True if db c
14a0b 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74 urrently has pBt
14a0c 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 locked */. int
14a0d 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 wantToLock;
14a0e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 /* Number of nes
14a0f 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c ted calls to sql
14a10 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 29 ite3BtreeEnter()
14a11 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75 */. int nBacku
14a12 70 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 p; /* Numb
14a13 65 72 20 6f 66 20 62 61 63 6b 75 70 20 6f 70 65 er of backup ope
14a14 72 61 74 69 6f 6e 73 20 72 65 61 64 69 6e 67 20 rations reading
14a15 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 this btree */.
14a16 42 74 72 65 65 20 2a 70 4e 65 78 74 3b 20 20 20 Btree *pNext;
14a17 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74 /* List of ot
14a18 68 65 72 20 73 68 61 72 61 62 6c 65 20 42 74 72 her sharable Btr
14a19 65 65 73 20 66 72 6f 6d 20 74 68 65 20 73 61 6d ees from the sam
14a1a 65 20 64 62 20 2a 2f 0a 20 20 42 74 72 65 65 20 e db */. Btree
14a1b 2a 70 50 72 65 76 3b 20 20 20 20 20 20 2f 2a 20 *pPrev; /*
14a1c 42 61 63 6b 20 70 6f 69 6e 74 65 72 20 6f 66 20 Back pointer of
14a1d 74 68 65 20 73 61 6d 65 20 6c 69 73 74 20 2a 2f the same list */
14a1e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14a1f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14a20 45 0a 20 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b E. BtLock lock;
14a21 20 20 20 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 /* Object
14a22 20 75 73 65 64 20 74 6f 20 6c 6f 63 6b 20 70 61 used to lock pa
14a23 67 65 20 31 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d ge 1 */.#endif.}
14a24 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 ;../*.** Btree.i
14a25 6e 54 72 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 nTrans may take
14a26 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f one of the follo
14a27 77 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a wing values..**.
14a28 2a 2a 20 49 66 20 74 68 65 20 73 68 61 72 65 64 ** If the shared
14a29 2d 64 61 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 -data extension
14a2a 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 is enabled, ther
14a2b 65 20 6d 61 79 20 62 65 20 6d 75 6c 74 69 70 6c e may be multipl
14a2c 65 20 75 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 e users.** of th
14a2d 65 20 42 74 72 65 65 20 73 74 72 75 63 74 75 72 e Btree structur
14a2e 65 2e 20 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f e. At most one o
14a2f 66 20 74 68 65 73 65 20 6d 61 79 20 6f 70 65 6e f these may open
14a30 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 a write transac
14a31 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 tion,.** but any
14a32 20 6e 75 6d 62 65 72 20 6d 61 79 20 68 61 76 65 number may have
14a33 20 61 63 74 69 76 65 20 72 65 61 64 20 74 72 61 active read tra
14a34 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 nsactions..*/.#d
14a35 65 66 69 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 efine TRANS_NONE
14a36 20 20 30 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 0.#define TRAN
14a37 53 5f 52 45 41 44 20 20 31 0a 23 64 65 66 69 6e S_READ 1.#defin
14a38 65 20 54 52 41 4e 53 5f 57 52 49 54 45 20 32 0a e TRANS_WRITE 2.
14a39 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
14a3a 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
14a3b 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 t represents a s
14a3c 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 ingle database f
14a3d 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 ile..** .** A si
14a3e 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 ngle database fi
14a3f 6c 65 20 63 61 6e 20 62 65 20 69 6e 20 75 73 65 le can be in use
14a40 20 61 73 20 74 68 65 20 73 61 6d 65 20 74 69 6d as the same tim
14a41 65 20 62 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d e by two.** or m
14a42 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e ore database con
14a43 6e 65 63 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 nections. When
14a44 74 77 6f 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e two or more conn
14a45 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 ections are.** s
14a46 68 61 72 69 6e 67 20 74 68 65 20 73 61 6d 65 20 haring the same
14a47 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 65 database file, e
14a48 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 ach connection h
14a49 61 73 20 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 as it own.** pri
14a4a 76 61 74 65 20 42 74 72 65 65 20 6f 62 6a 65 63 vate Btree objec
14a4b 74 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 61 t for the file a
14a4c 6e 64 20 65 61 63 68 20 6f 66 20 74 68 6f 73 65 nd each of those
14a4d 20 42 74 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a Btrees points.*
14a4e 2a 20 74 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 * to this one Bt
14a4f 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 Shared object.
14a50 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 69 73 BtShared.nRef is
14a51 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a the number of.*
14a52 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 * connections cu
14a53 72 72 65 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 rrently sharing
14a54 74 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 this database fi
14a55 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 le..**.** Fields
14a56 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 in this structu
14a57 72 65 20 61 72 65 20 61 63 63 65 73 73 65 64 20 re are accessed
14a58 75 6e 64 65 72 20 74 68 65 20 42 74 53 68 61 72 under the BtShar
14a59 65 64 2e 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 ed.mutex.** mute
14a5a 78 2c 20 65 78 63 65 70 74 20 66 6f 72 20 6e 52 x, except for nR
14a5b 65 66 20 61 6e 64 20 70 4e 65 78 74 20 77 68 69 ef and pNext whi
14a5c 63 68 20 61 72 65 20 61 63 63 65 73 73 65 64 20 ch are accessed
14a5d 75 6e 64 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f under the.** glo
14a5e 62 61 6c 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 bal SQLITE_MUTEX
14a5f 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d _STATIC_MASTER m
14a60 75 74 65 78 2e 20 20 54 68 65 20 70 50 61 67 65 utex. The pPage
14a61 72 20 66 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e r field.** may n
14a62 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f ot be modified o
14a63 6e 63 65 20 69 74 20 69 73 20 69 6e 69 74 69 61 nce it is initia
14a64 6c 6c 79 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 lly set as long
14a65 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 as nRef>0..** Th
14a66 65 20 70 53 63 68 65 6d 61 20 66 69 65 6c 64 20 e pSchema field
14a67 6d 61 79 20 62 65 20 73 65 74 20 6f 6e 63 65 20 may be set once
14a68 75 6e 64 65 72 20 42 74 53 68 61 72 65 64 2e 6d under BtShared.m
14a69 75 74 65 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 utex and.** ther
14a6a 65 61 66 74 65 72 20 69 73 20 75 6e 63 68 61 6e eafter is unchan
14a6b 67 65 64 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e ged as long as n
14a6c 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 Ref>0..**.** isP
14a6d 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 ending:.**.**
14a6e 49 66 20 61 20 42 74 53 68 61 72 65 64 20 63 6c If a BtShared cl
14a6f 69 65 6e 74 20 66 61 69 6c 73 20 74 6f 20 6f 62 ient fails to ob
14a70 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 tain a write-loc
14a71 6b 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 0a k on a database.
14a72 2a 2a 20 20 20 74 61 62 6c 65 20 28 62 65 63 61 ** table (beca
14a73 75 73 65 20 74 68 65 72 65 20 65 78 69 73 74 73 use there exists
14a74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 one or more rea
14a75 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 74 d-locks on the t
14a76 61 62 6c 65 29 2c 0a 2a 2a 20 20 20 74 68 65 20 able),.** the
14a77 73 68 61 72 65 64 2d 63 61 63 68 65 20 65 6e 74 shared-cache ent
14a78 65 72 73 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 ers 'pending-loc
14a79 6b 27 20 73 74 61 74 65 20 61 6e 64 20 69 73 50 k' state and isP
14a7a 65 6e 64 69 6e 67 20 69 73 0a 2a 2a 20 20 20 73 ending is.** s
14a7b 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a et to true..**.*
14a7c 2a 20 20 20 54 68 65 20 73 68 61 72 65 64 2d 63 * The shared-c
14a7d 61 63 68 65 20 6c 65 61 76 65 73 20 74 68 65 20 ache leaves the
14a7e 27 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 73 'pending lock' s
14a7f 74 61 74 65 20 77 68 65 6e 20 65 69 74 68 65 72 tate when either
14a80 20 6f 66 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c of.** the fol
14a81 6c 6f 77 69 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a lowing occur:.**
14a82 0a 2a 2a 20 20 20 20 20 31 29 20 54 68 65 20 63 .** 1) The c
14a83 75 72 72 65 6e 74 20 77 72 69 74 65 72 20 28 42 urrent writer (B
14a84 74 53 68 61 72 65 64 2e 70 57 72 69 74 65 72 29 tShared.pWriter)
14a85 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 74 concludes its t
14a86 72 61 6e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a ransaction, OR.*
14a87 2a 20 20 20 20 20 32 29 20 54 68 65 20 6e 75 6d * 2) The num
14a88 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c ber of locks hel
14a89 64 20 62 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 d by other conne
14a8a 63 74 69 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20 ctions drops to
14a8b 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 zero..**.** wh
14a8c 69 6c 65 20 69 6e 20 74 68 65 20 27 70 65 6e 64 ile in the 'pend
14a8d 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 2c ing-lock' state,
14a8e 20 6e 6f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d no connection m
14a8f 61 79 20 73 74 61 72 74 20 61 20 6e 65 77 0a 2a ay start a new.*
14a90 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e * transaction.
14a91 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 66 65 .**.** This fe
14a92 61 74 75 72 65 20 69 73 20 69 6e 63 6c 75 64 65 ature is include
14a93 64 20 74 6f 20 68 65 6c 70 20 70 72 65 76 65 6e d to help preven
14a94 74 20 77 72 69 74 65 72 2d 73 74 61 72 76 61 74 t writer-starvat
14a95 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 ion..*/.struct B
14a96 74 53 68 61 72 65 64 20 7b 0a 20 20 50 61 67 65 tShared {. Page
14a97 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 r *pPager;
14a98 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 63 61 /* The page ca
14a99 63 68 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 che */. sqlite3
14a9a 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f *db; /
14a9b 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 * Database conne
14a9c 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 ction currently
14a9d 75 73 69 6e 67 20 74 68 69 73 20 42 74 72 65 65 using this Btree
14a9e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
14a9f 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 pCursor; /* A
14aa0 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 list of all ope
14aa1 6e 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d n cursors */. M
14aa2 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 20 emPage *pPage1;
14aa3 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 /* First pa
14aa4 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 ge of the databa
14aa5 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f se */. u8 readO
14aa6 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a nly; /*
14aa7 20 54 72 75 65 20 69 66 20 74 68 65 20 75 6e 64 True if the und
14aa8 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 69 73 20 erlying file is
14aa9 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 readonly */. u8
14aaa 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 3b 20 pageSizeFixed;
14aab 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 /* True if t
14aac 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e he page size can
14aad 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 no longer be ch
14aae 61 6e 67 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 anged */.#ifndef
14aaf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
14ab0 4f 56 41 43 55 55 4d 0a 20 20 75 38 20 61 75 74 OVACUUM. u8 aut
14ab1 6f 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20 20 oVacuum;
14ab2 2f 2a 20 54 72 75 65 20 69 66 20 61 75 74 6f 2d /* True if auto-
14ab3 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 vacuum is enable
14ab4 64 20 2a 2f 0a 20 20 75 38 20 69 6e 63 72 56 61 d */. u8 incrVa
14ab5 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 cuum; /*
14ab6 54 72 75 65 20 69 66 20 69 6e 63 72 2d 76 61 63 True if incr-vac
14ab7 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a uum is enabled *
14ab8 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36 20 70 /.#endif. u16 p
14ab9 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 ageSize;
14aba 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 /* Total number
14abb 20 6f 66 20 62 79 74 65 73 20 6f 6e 20 61 20 70 of bytes on a p
14abc 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 age */. u16 usa
14abd 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 2f bleSize; /
14abe 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 * Number of usab
14abf 6c 65 20 62 79 74 65 73 20 6f 6e 20 65 61 63 68 le bytes on each
14ac0 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d page */. u16 m
14ac1 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 axLocal;
14ac2 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 /* Maximum loca
14ac3 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e l payload in non
14ac4 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 -LEAFDATA tables
14ac5 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 */. u16 minLoc
14ac6 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d al; /* M
14ac7 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 inimum local pay
14ac8 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 load in non-LEAF
14ac9 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 DATA tables */.
14aca 20 75 31 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 u16 maxLeaf;
14acb 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 /* Maximu
14acc 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 m local payload
14acd 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 in a LEAFDATA ta
14ace 62 6c 65 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e ble */. u16 min
14acf 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f Leaf; /
14ad0 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 * Minimum local
14ad1 70 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 payload in a LEA
14ad2 46 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 FDATA table */.
14ad3 20 75 38 20 69 6e 54 72 61 6e 73 61 63 74 69 6f u8 inTransactio
14ad4 6e 3b 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 n; /* Transa
14ad5 63 74 69 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 ction state */.
14ad6 20 69 6e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f int nTransactio
14ad7 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 n; /* Number
14ad8 20 6f 66 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 of open transac
14ad9 74 69 6f 6e 73 20 28 72 65 61 64 20 2b 20 77 72 tions (read + wr
14ada 69 74 65 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a ite) */. void *
14adb 70 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 pSchema;
14adc 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 /* Pointer to sp
14add 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 ace allocated by
14ade 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 sqlite3BtreeSch
14adf 65 6d 61 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 ema() */. void
14ae0 28 2a 78 46 72 65 65 53 63 68 65 6d 61 29 28 76 (*xFreeSchema)(v
14ae1 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 oid*); /* Destr
14ae2 75 63 74 6f 72 20 66 6f 72 20 42 74 53 68 61 72 uctor for BtShar
14ae3 65 64 2e 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 ed.pSchema */.
14ae4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d sqlite3_mutex *m
14ae5 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 utex; /* Non-rec
14ae6 75 72 73 69 76 65 20 6d 75 74 65 78 20 72 65 71 ursive mutex req
14ae7 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73 20 uired to access
14ae8 74 68 69 73 20 73 74 72 75 63 74 20 2a 2f 0a 20 this struct */.
14ae9 20 42 69 74 76 65 63 20 2a 70 48 61 73 43 6f 6e Bitvec *pHasCon
14aea 74 65 6e 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 tent; /* Set of
14aeb 20 70 61 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 pages moved to
14aec 66 72 65 65 2d 6c 69 73 74 20 74 68 69 73 20 74 free-list this t
14aed 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 ransaction */.#i
14aee 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
14aef 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
14af0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 int nRef;
14af1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14af2 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 of references t
14af3 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 o this structure
14af4 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a */. BtShared *
14af5 70 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e pNext; /* N
14af6 65 78 74 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 ext on a list of
14af7 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 sharable BtShar
14af8 65 64 20 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 ed structs */.
14af9 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 BtLock *pLock;
14afa 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 /* List of
14afb 20 6c 6f 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 locks held on t
14afc 68 69 73 20 73 68 61 72 65 64 2d 62 74 72 65 65 his shared-btree
14afd 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 struct */. Btr
14afe 65 65 20 2a 70 57 72 69 74 65 72 3b 20 20 20 20 ee *pWriter;
14aff 20 20 20 2f 2a 20 42 74 72 65 65 20 77 69 74 68 /* Btree with
14b00 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 currently open
14b01 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f write transactio
14b02 6e 20 2a 2f 0a 20 20 75 38 20 69 73 45 78 63 6c n */. u8 isExcl
14b03 75 73 69 76 65 3b 20 20 20 20 20 20 20 2f 2a 20 usive; /*
14b04 54 72 75 65 20 69 66 20 70 57 72 69 74 65 72 20 True if pWriter
14b05 68 61 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 has an EXCLUSIVE
14b06 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 62 20 lock on the db
14b07 2a 2f 0a 20 20 75 38 20 69 73 50 65 6e 64 69 6e */. u8 isPendin
14b08 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 g; /* If
14b09 20 77 61 69 74 69 6e 67 20 66 6f 72 20 72 65 61 waiting for rea
14b0a 64 2d 6c 6f 63 6b 73 20 74 6f 20 63 6c 65 61 72 d-locks to clear
14b0b 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 */.#endif. u8
14b0c 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 *pTmpSpace;
14b0d 20 20 20 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 /* BtShared.p
14b0e 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 ageSize bytes of
14b0f 20 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 space for tmp u
14b10 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 se */.};../*.**
14b11 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 An instance of t
14b12 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 he following str
14b13 75 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 ucture is used t
14b14 6f 20 68 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 o hold informati
14b15 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 on.** about a ce
14b16 6c 6c 2e 20 20 54 68 65 20 70 61 72 73 65 43 65 ll. The parseCe
14b17 6c 6c 50 74 72 28 29 20 66 75 6e 63 74 69 6f 6e llPtr() function
14b18 20 66 69 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 fills in this s
14b19 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 tructure.** base
14b1a 64 20 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e d on information
14b1b 20 65 78 74 72 61 63 74 20 66 72 6f 6d 20 74 68 extract from th
14b1c 65 20 72 61 77 20 64 69 73 6b 20 70 61 67 65 2e e raw disk page.
14b1d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 .*/.typedef stru
14b1e 63 74 20 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c ct CellInfo Cell
14b1f 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c Info;.struct Cel
14b20 6c 49 6e 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 lInfo {. u8 *pC
14b21 65 6c 6c 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e ell; /* Poin
14b22 74 65 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 ter to the start
14b23 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 of cell content
14b24 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 */. i64 nKey;
14b25 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 /* The key
14b26 66 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 for INTKEY table
14b27 73 2c 20 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 s, or number of
14b28 62 79 74 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a bytes in key */.
14b29 20 20 75 33 32 20 6e 44 61 74 61 3b 20 20 20 20 u32 nData;
14b2a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
14b2b 74 65 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 tes of data */.
14b2c 20 75 33 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 u32 nPayload;
14b2d 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 /* Total amount
14b2e 6f 66 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 of payload */.
14b2f 75 31 36 20 6e 48 65 61 64 65 72 3b 20 20 20 2f u16 nHeader; /
14b30 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 * Size of the ce
14b31 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 ll content heade
14b32 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 r in bytes */.
14b33 75 31 36 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f u16 nLocal; /
14b34 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c * Amount of payl
14b35 6f 61 64 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 oad held locally
14b36 20 2a 2f 0a 20 20 75 31 36 20 69 4f 76 65 72 66 */. u16 iOverf
14b37 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 low; /* Offset t
14b38 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 o overflow page
14b39 6e 75 6d 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 number. Zero if
14b3a 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a no overflow */.
14b3b 20 20 75 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 u16 nSize;
14b3c 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
14b3d 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 cell content on
14b3e 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 the main b-tree
14b3f 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a page */.};../*.*
14b40 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 * Maximum depth
14b41 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 of an SQLite B-T
14b42 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20 41 ree structure. A
14b43 6e 79 20 42 2d 54 72 65 65 20 64 65 65 70 65 72 ny B-Tree deeper
14b44 20 74 68 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 than.** this wi
14b45 6c 6c 20 62 65 20 64 65 63 6c 61 72 65 64 20 63 ll be declared c
14b46 6f 72 72 75 70 74 2e 20 54 68 69 73 20 76 61 6c orrupt. This val
14b47 75 65 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 ue is calculated
14b48 20 62 61 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d based on a.** m
14b49 61 78 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 aximum database
14b4a 73 69 7a 65 20 6f 66 20 32 5e 33 31 20 70 61 67 size of 2^31 pag
14b4b 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e es a minimum fan
14b4c 6f 75 74 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a out of 2 for a.*
14b4d 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 * root-node and
14b4e 33 20 66 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 3 for all other
14b4f 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a internal nodes..
14b50 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 72 65 65 20 **.** If a tree
14b51 74 68 61 74 20 61 70 70 65 61 72 73 20 74 6f 20 that appears to
14b52 62 65 20 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 be taller than t
14b53 68 69 73 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 his is encounter
14b54 65 64 2c 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 ed, it is.** ass
14b55 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 64 61 umed that the da
14b56 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 tabase is corrup
14b57 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 t..*/.#define BT
14b58 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 CURSOR_MAX_DEPTH
14b59 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 20../*.** A cur
14b5a 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 sor is a pointer
14b5b 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 to a particular
14b5c 20 65 6e 74 72 79 20 77 69 74 68 69 6e 20 61 20 entry within a
14b5d 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d particular.** b-
14b5e 74 72 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 tree within a da
14b5f 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a tabase file..**.
14b60 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 69 73 20 ** The entry is
14b61 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 69 74 identified by it
14b62 73 20 4d 65 6d 50 61 67 65 20 61 6e 64 20 74 68 s MemPage and th
14b63 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 e index in.** Me
14b64 6d 50 61 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 mPage.aCell[] of
14b65 20 74 68 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a the entry..**.*
14b66 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 * A single datab
14b67 61 73 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 ase file can sha
14b68 72 65 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 red by two more
14b69 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 database connect
14b6a 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 ions,.** but cur
14b6b 73 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 sors cannot be s
14b6c 68 61 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 hared. Each cur
14b6d 73 6f 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 sor is associate
14b6e 64 20 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 d with a.** part
14b6f 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 icular database
14b70 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 connection ident
14b71 69 66 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 ified BtCursor.p
14b72 42 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 Btree.db..**.**
14b73 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 Fields in this s
14b74 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 tructure are acc
14b75 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 essed under the
14b76 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a BtShared.mutex.*
14b77 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d * found at self-
14b78 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f >pBt->mutex. .*/
14b79 0a 73 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 .struct BtCursor
14b7a 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 {. Btree *pBtr
14b7b 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f ee; /
14b7c 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 * The Btree to w
14b7d 68 69 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 hich this cursor
14b7e 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 belongs */. Bt
14b7f 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 Shared *pBt;
14b80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 /* The B
14b81 74 53 68 61 72 65 64 20 74 68 69 73 20 63 75 72 tShared this cur
14b82 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f sor points to */
14b83 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 . BtCursor *pNe
14b84 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 xt, *pPrev; /*
14b85 46 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c Forms a linked l
14b86 69 73 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f ist of all curso
14b87 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b rs */. struct K
14b88 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
14b89 3b 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 ; /* Argument pa
14b8a 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 ssed to comparis
14b8b 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 on function */.
14b8c 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 Pgno pgnoRoot;
14b8d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
14b8e 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 e root page of t
14b8f 68 69 73 20 74 72 65 65 20 2a 2f 0a 20 20 73 71 his tree */. sq
14b90 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 61 63 68 lite3_int64 cach
14b91 65 64 52 6f 77 69 64 3b 20 2f 2a 20 4e 65 78 74 edRowid; /* Next
14b92 20 72 6f 77 69 64 20 63 61 63 68 65 2e 20 20 30 rowid cache. 0
14b93 20 6d 65 61 6e 73 20 6e 6f 74 20 76 61 6c 69 64 means not valid
14b94 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 */. CellInfo i
14b95 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 nfo;
14b96 2f 2a 20 41 20 70 61 72 73 65 20 6f 66 20 74 68 /* A parse of th
14b97 65 20 63 65 6c 6c 20 77 65 20 61 72 65 20 70 6f e cell we are po
14b98 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 75 inting at */. u
14b99 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20 20 20 8 wrFlag;
14b9a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
14b9b 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a 2f 0a if writable */.
14b9c 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20 20 20 u8 atLast;
14b9d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
14b9e 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 ursor pointing t
14b9f 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 o the last entry
14ba0 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64 4e 4b */. u8 validNK
14ba1 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ey;
14ba2 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66 6f 2e /* True if info.
14ba3 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20 2a 2f nKey is valid */
14ba4 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20 20 20 . u8 eState;
14ba5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
14ba6 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52 53 4f One of the CURSO
14ba7 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 R_XXX constants
14ba8 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f 0a 20 (see below) */.
14ba9 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20 20 20 void *pKey;
14baa 20 20 2f 2a 20 53 61 76 65 64 20 6b 65 79 20 74 /* Saved key t
14bab 68 61 74 20 77 61 73 20 63 75 72 73 6f 72 27 73 hat was cursor's
14bac 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f 73 69 last known posi
14bad 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20 6e 4b tion */. i64 nK
14bae 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 ey; /* Si
14baf 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72 20 6c ze of pKey, or l
14bb0 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65 79 20 ast integer key
14bb1 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4e 65 78 */. int skipNex
14bb2 74 3b 20 20 20 20 2f 2a 20 50 72 65 76 28 29 20 t; /* Prev()
14bb3 69 73 20 6e 6f 6f 70 20 69 66 20 6e 65 67 61 74 is noop if negat
14bb4 69 76 65 2e 20 4e 65 78 74 28 29 20 69 73 20 6e ive. Next() is n
14bb5 6f 6f 70 20 69 66 20 70 6f 73 69 74 69 76 65 20 oop if positive
14bb6 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
14bb7 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
14bb8 20 20 75 38 20 69 73 49 6e 63 72 62 6c 6f 62 48 u8 isIncrblobH
14bb9 61 6e 64 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54 andle; /* T
14bba 72 75 65 20 69 66 20 74 68 69 73 20 63 75 72 73 rue if this curs
14bbb 6f 72 20 69 73 20 61 6e 20 69 6e 63 72 2e 20 69 or is an incr. i
14bbc 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67 o handle */. Pg
14bbd 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f 77 3b 20 20 no *aOverflow;
14bbe 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 /* Cache
14bbf 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 of overflow pag
14bc0 65 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 23 e locations */.#
14bc1 65 6e 64 69 66 0a 20 20 69 31 36 20 69 50 61 67 endif. i16 iPag
14bc2 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
14bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14bc4 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e Index of curren
14bc5 74 20 70 61 67 65 20 69 6e 20 61 70 50 61 67 65 t page in apPage
14bc6 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 */. MemPage *a
14bc7 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52 5f 4d pPage[BTCURSOR_M
14bc8 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a 20 50 AX_DEPTH]; /* P
14bc9 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74 20 74 ages from root t
14bca 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 2a o current page *
14bcb 2f 0a 20 20 75 31 36 20 61 69 49 64 78 5b 42 54 /. u16 aiIdx[BT
14bcc 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 CURSOR_MAX_DEPTH
14bcd 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 ]; /* Cur
14bce 72 65 6e 74 20 69 6e 64 65 78 20 69 6e 20 61 70 rent index in ap
14bcf 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f Page[i] */.};../
14bd0 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c 20 76 *.** Potential v
14bd1 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75 72 73 alues for BtCurs
14bd2 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a 2a 2a or.eState..**.**
14bd3 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 0a 2a CURSOR_VALID:.*
14bd4 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 * Cursor point
14bd5 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 s to a valid ent
14bd6 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64 28 29 ry. getPayload()
14bd7 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63 61 6c etc. may be cal
14bd8 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f led..**.** CURSO
14bd9 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 R_INVALID:.**
14bda 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 Cursor does not
14bdb 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 point to a valid
14bdc 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63 61 6e entry. This can
14bdd 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65 78 61 happen (for exa
14bde 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65 63 61 mple) .** beca
14bdf 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 use the table is
14be0 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61 75 73 empty or becaus
14be1 65 20 42 74 72 65 65 43 75 72 73 6f 72 46 69 72 e BtreeCursorFir
14be2 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62 65 65 st() has not bee
14be3 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e 0a 2a n.** called..*
14be4 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45 51 55 *.** CURSOR_REQU
14be5 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20 54 68 IRESEEK:.** Th
14be6 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 69 e table that thi
14be7 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 s cursor was ope
14be8 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65 78 69 ned on still exi
14be9 73 74 73 2c 20 62 75 74 20 68 61 73 20 62 65 65 sts, but has bee
14bea 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69 65 64 n .** modified
14beb 20 73 69 6e 63 65 20 74 68 65 20 63 75 72 73 6f since the curso
14bec 72 20 77 61 73 20 6c 61 73 74 20 75 73 65 64 2e r was last used.
14bed 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 73 69 The cursor posi
14bee 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a 2a 2a tion is saved.**
14bef 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65 73 20 in variables
14bf0 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20 61 6e BtCursor.pKey an
14bf1 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 2e d BtCursor.nKey.
14bf2 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72 20 69 When a cursor i
14bf3 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69 73 20 s in .** this
14bf4 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65 43 75 state, restoreCu
14bf5 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 rsorPosition() c
14bf6 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 an be called to
14bf7 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 attempt to.**
14bf8 73 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 seek the cursor
14bf9 74 6f 20 74 68 65 20 73 61 76 65 64 20 70 6f 73 to the saved pos
14bfa 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 ition..**.** CUR
14bfb 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20 20 20 SOR_FAULT:.**
14bfc 41 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 A unrecoverable
14bfd 65 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20 65 72 error (an I/O er
14bfe 72 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f 63 20 ror or a malloc
14bff 66 61 69 6c 75 72 65 29 20 68 61 73 20 6f 63 63 failure) has occ
14c00 75 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20 61 20 urred.** on a
14c01 64 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e 65 63 different connec
14c02 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72 65 73 tion that shares
14c03 20 74 68 65 20 42 74 53 68 61 72 65 64 20 63 61 the BtShared ca
14c04 63 68 65 20 77 69 74 68 20 74 68 69 73 0a 2a 2a che with this.**
14c05 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 cursor. The
14c06 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74 20 74 error has left t
14c07 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e 20 69 he cache in an i
14c08 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 nconsistent stat
14c09 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74 68 69 e..** Do nothi
14c0a 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74 68 69 ng else with thi
14c0b 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79 20 61 s cursor. Any a
14c0c 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 ttempt to use th
14c0d 65 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20 73 68 e cursor.** sh
14c0e 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 ould return the
14c0f 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 error code store
14c10 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e 73 6b d in BtCursor.sk
14c11 69 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 55 ip.*/.#define CU
14c12 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20 20 20 RSOR_INVALID
14c13 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 0.#define
14c14 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 CURSOR_VALID
14c15 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 1.#def
14c16 69 6e 65 20 43 55 52 53 4f 52 5f 52 45 51 55 49 ine CURSOR_REQUI
14c17 52 45 53 45 45 4b 20 20 20 20 20 20 20 32 0a 23 RESEEK 2.#
14c18 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 46 41 define CURSOR_FA
14c19 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 ULT
14c1a 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 64 61 3../* .** The da
14c1b 74 61 62 61 73 65 20 70 61 67 65 20 74 68 65 20 tabase page the
14c1c 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f 63 63 PENDING_BYTE occ
14c1d 75 70 69 65 73 2e 20 54 68 69 73 20 70 61 67 65 upies. This page
14c1e 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 0a is never used..
14c1f 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 */.# define PEND
14c20 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 ING_BYTE_PAGE(pB
14c21 74 29 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f t) PAGER_MJ_PGNO
14c22 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 (pBt)../*.** The
14c23 73 65 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 se macros define
14c24 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 the location of
14c25 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 the pointer-map
14c26 20 65 6e 74 72 79 20 66 6f 72 20 61 20 0a 2a 2a entry for a .**
14c27 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 database page.
14c28 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 The first argume
14c29 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20 74 68 nt to each is th
14c2a 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 e number of usab
14c2b 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e 20 65 le.** bytes on e
14c2c 61 63 68 20 70 61 67 65 20 6f 66 20 74 68 65 20 ach page of the
14c2d 64 61 74 61 62 61 73 65 20 28 6f 66 74 65 6e 20 database (often
14c2e 31 30 32 34 29 2e 20 54 68 65 20 73 65 63 6f 6e 1024). The secon
14c2f 64 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 d is the.** page
14c30 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f 6b 20 number to look
14c31 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 up in the pointe
14c32 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 r map..**.** PTR
14c33 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65 74 75 72 MAP_PAGENO retur
14c34 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 ns the database
14c35 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 page number of t
14c36 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a he pointer-map.*
14c37 2a 20 70 61 67 65 20 74 68 61 74 20 73 74 6f 72 * page that stor
14c38 65 73 20 74 68 65 20 72 65 71 75 69 72 65 64 20 es the required
14c39 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41 50 5f pointer. PTRMAP_
14c3a 50 54 52 4f 46 46 53 45 54 20 72 65 74 75 72 6e PTROFFSET return
14c3b 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 s.** the offset
14c3c 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 of the requested
14c3d 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a map entry..**.*
14c3e 2a 20 49 66 20 74 68 65 20 70 67 6e 6f 20 61 72 * If the pgno ar
14c3f 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f gument passed to
14c40 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 69 PTRMAP_PAGENO i
14c41 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 s a pointer-map
14c42 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20 70 67 page,.** then pg
14c43 6e 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 no is returned.
14c44 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d 41 50 So (pgno==PTRMAP
14c45 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20 70 67 _PAGENO(pgsz, pg
14c46 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a 20 75 no)) can be.** u
14c47 73 65 64 20 74 6f 20 74 65 73 74 20 69 66 20 70 sed to test if p
14c48 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 gno is a pointer
14c49 2d 6d 61 70 20 70 61 67 65 2e 20 50 54 52 4d 41 -map page. PTRMA
14c4a 50 5f 49 53 50 41 47 45 20 69 6d 70 6c 65 6d 65 P_ISPAGE impleme
14c4b 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74 65 73 74 nts.** this test
14c4c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 ..*/.#define PTR
14c4d 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 MAP_PAGENO(pBt,
14c4e 70 67 6e 6f 29 20 70 74 72 6d 61 70 50 61 67 65 pgno) ptrmapPage
14c4f 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a 23 64 no(pBt, pgno).#d
14c50 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 54 52 efine PTRMAP_PTR
14c51 4f 46 46 53 45 54 28 70 67 70 74 72 6d 61 70 2c OFFSET(pgptrmap,
14c52 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e 6f 2d pgno) (5*(pgno-
14c53 70 67 70 74 72 6d 61 70 2d 31 29 29 0a 23 64 65 pgptrmap-1)).#de
14c54 66 69 6e 65 20 50 54 52 4d 41 50 5f 49 53 50 41 fine PTRMAP_ISPA
14c55 47 45 28 70 42 74 2c 20 70 67 6e 6f 29 20 28 50 GE(pBt, pgno) (P
14c56 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28 70 42 TRMAP_PAGENO((pB
14c57 74 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70 67 6e t),(pgno))==(pgn
14c58 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 o))../*.** The p
14c59 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 61 20 ointer map is a
14c5a 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74 68 61 lookup table tha
14c5b 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 t identifies the
14c5c 20 70 61 72 65 6e 74 20 70 61 67 65 20 66 6f 72 parent page for
14c5d 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 70 .** each child p
14c5e 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 age in the datab
14c5f 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20 70 ase file. The p
14c60 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 74 68 arent page is th
14c61 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 63 e page that.** c
14c62 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 ontains a pointe
14c63 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 r to the child.
14c64 20 45 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 Every page in t
14c65 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 he database cont
14c66 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31 20 70 ains.** 0 or 1 p
14c67 61 72 65 6e 74 20 70 61 67 65 73 2e 20 20 28 49 arent pages. (I
14c68 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74 20 27 n this context '
14c69 64 61 74 61 62 61 73 65 20 70 61 67 65 27 20 72 database page' r
14c6a 65 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 efers.** to any
14c6b 70 61 67 65 20 74 68 61 74 20 69 73 20 6e 6f 74 page that is not
14c6c 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 6f 69 part of the poi
14c6d 6e 74 65 72 20 6d 61 70 20 69 74 73 65 6c 66 2e nter map itself.
14c6e 29 20 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 ) Each pointer
14c6f 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 6e map.** entry con
14c70 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c sists of a singl
14c71 65 20 62 79 74 65 20 27 74 79 70 65 27 20 61 6e e byte 'type' an
14c72 64 20 61 20 34 20 62 79 74 65 20 70 61 72 65 6e d a 4 byte paren
14c73 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a t page number..*
14c74 2a 20 54 68 65 20 50 54 52 4d 41 50 5f 58 58 58 * The PTRMAP_XXX
14c75 20 69 64 65 6e 74 69 66 69 65 72 73 20 62 65 6c identifiers bel
14c76 6f 77 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 ow are the valid
14c77 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 types..**.** Th
14c78 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 e purpose of the
14c79 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 pointer map is
14c7a 74 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f 76 69 to facility movi
14c7b 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20 6f 6e ng pages from on
14c7c 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e e.** position in
14c7d 20 74 68 65 20 66 69 6c 65 20 74 6f 20 61 6e 6f the file to ano
14c7e 74 68 65 72 20 61 73 20 70 61 72 74 20 6f 66 20 ther as part of
14c7f 61 75 74 6f 76 61 63 75 75 6d 2e 20 20 57 68 65 autovacuum. Whe
14c80 6e 20 61 20 70 61 67 65 0a 2a 2a 20 69 73 20 6d n a page.** is m
14c81 6f 76 65 64 2c 20 74 68 65 20 70 6f 69 6e 74 65 oved, the pointe
14c82 72 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 20 r in its parent
14c83 6d 75 73 74 20 62 65 20 75 70 64 61 74 65 64 20 must be updated
14c84 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a to point to the.
14c85 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f 6e 2e ** new location.
14c86 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 The pointer ma
14c87 70 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 p is used to loc
14c88 61 74 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 ate the parent p
14c89 61 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 0a age quickly..**.
14c8a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 ** PTRMAP_ROOTPA
14c8b 47 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 GE: The database
14c8c 20 70 61 67 65 20 69 73 20 61 20 72 6f 6f 74 2d page is a root-
14c8d 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d 6e page. The page-n
14c8e 75 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 umber is not.**
14c8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14c90 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 63 61 used in this ca
14c91 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 se..**.** PTRMAP
14c92 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65 20 64 _FREEPAGE: The d
14c93 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 atabase page is
14c94 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65 65 29 an unused (free)
14c95 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 2d page. The page-
14c96 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20 20 20 number .**
14c97 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 6e is n
14c98 6f 74 20 75 73 65 64 20 69 6e 20 74 68 69 73 20 ot used in this
14c99 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d case..**.** PTRM
14c9a 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20 54 68 AP_OVERFLOW1: Th
14c9b 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 e database page
14c9c 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 67 is the first pag
14c9d 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 0a e in a list of .
14c9e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
14c9f 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 overflow pa
14ca0 67 65 73 2e 20 54 68 65 20 70 61 67 65 20 6e 75 ges. The page nu
14ca1 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 mber identifies
14ca2 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a the page that.**
14ca3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ca4 20 20 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 contains the
14ca5 63 65 6c 6c 20 77 69 74 68 20 61 20 70 6f 69 6e cell with a poin
14ca6 74 65 72 20 74 6f 20 74 68 69 73 20 6f 76 65 72 ter to this over
14ca7 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a flow page..**.**
14ca8 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 PTRMAP_OVERFLOW
14ca9 32 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2: The database
14caa 70 61 67 65 20 69 73 20 74 68 65 20 73 65 63 6f page is the seco
14cab 6e 64 20 6f 72 20 6c 61 74 65 72 20 70 61 67 65 nd or later page
14cac 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a 2a 2a in a list of.**
14cad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14cae 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 overflow page
14caf 73 2e 20 54 68 65 20 70 61 67 65 2d 6e 75 6d 62 s. The page-numb
14cb0 65 72 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 er identifies th
14cb1 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 20 20 e previous.**
14cb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14cb3 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 page in the over
14cb4 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e 0a flow page list..
14cb5 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 **.** PTRMAP_BTR
14cb6 45 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 EE: The database
14cb7 20 70 61 67 65 20 69 73 20 61 20 6e 6f 6e 2d 72 page is a non-r
14cb8 6f 6f 74 20 62 74 72 65 65 20 70 61 67 65 2e 20 oot btree page.
14cb9 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a The page number.
14cba 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 **
14cbb 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 identifies the
14cbc 70 61 72 65 6e 74 20 70 61 67 65 20 69 6e 20 74 parent page in t
14cbd 68 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23 64 65 he btree..*/.#de
14cbe 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f 4f 54 fine PTRMAP_ROOT
14cbf 50 41 47 45 20 31 0a 23 64 65 66 69 6e 65 20 50 PAGE 1.#define P
14cc0 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 32 TRMAP_FREEPAGE 2
14cc1 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f .#define PTRMAP_
14cc2 4f 56 45 52 46 4c 4f 57 31 20 33 0a 23 64 65 66 OVERFLOW1 3.#def
14cc3 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 ine PTRMAP_OVERF
14cc4 4c 4f 57 32 20 34 0a 23 64 65 66 69 6e 65 20 50 LOW2 4.#define P
14cc5 54 52 4d 41 50 5f 42 54 52 45 45 20 35 0a 0a 2f TRMAP_BTREE 5../
14cc6 2a 20 41 20 62 75 6e 63 68 20 6f 66 20 61 73 73 * A bunch of ass
14cc7 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 ert() statements
14cc8 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 74 72 to check the tr
14cc9 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 ansaction state
14cca 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 variables.** of
14ccb 68 61 6e 64 6c 65 20 70 20 28 74 79 70 65 20 42 handle p (type B
14ccc 74 72 65 65 2a 29 20 61 72 65 20 69 6e 74 65 72 tree*) are inter
14ccd 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 nally consistent
14cce 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62 74 72 ..*/.#define btr
14ccf 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 20 5c eeIntegrity(p) \
14cd0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 . assert( p->pB
14cd1 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e t->inTransaction
14cd2 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c 7c 20 !=TRANS_NONE ||
14cd3 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 p->pBt->nTransac
14cd4 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20 20 61 tion==0 ); \. a
14cd5 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 69 ssert( p->pBt->i
14cd6 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d 70 2d nTransaction>=p-
14cd7 3e 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a 0a 2f >inTrans ); .../
14cd8 2a 0a 2a 2a 20 54 68 65 20 49 53 41 55 54 4f 56 *.** The ISAUTOV
14cd9 41 43 55 55 4d 20 6d 61 63 72 6f 20 69 73 20 75 ACUUM macro is u
14cda 73 65 64 20 77 69 74 68 69 6e 20 62 61 6c 61 6e sed within balan
14cdb 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 ce_nonroot() to
14cdc 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69 66 20 determine.** if
14cdd 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 the database sup
14cde 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 ports auto-vacuu
14cdf 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61 75 73 m or not. Becaus
14ce0 65 20 69 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 e it is used.**
14ce1 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72 65 73 within an expres
14ce2 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 6e 20 sion that is an
14ce3 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e 6f 74 argument to anot
14ce4 68 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20 28 73 her macro .** (s
14ce5 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77 29 2c qliteMallocRaw),
14ce6 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 it is not possi
14ce7 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e 64 69 ble to use condi
14ce8 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61 74 69 tional compilati
14ce9 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69 73 20 on..** So, this
14cea 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 macro is defined
14ceb 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23 69 66 instead..*/.#if
14cec 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
14ced 5f 41 55 54 4f 56 41 43 55 55 4d 0a 23 64 65 66 _AUTOVACUUM.#def
14cee 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d ine ISAUTOVACUUM
14cef 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 (pBt->autoVacuu
14cf0 6d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 m).#else.#define
14cf1 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 30 0a ISAUTOVACUUM 0.
14cf2 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 #endif.../*.** T
14cf3 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 his structure is
14cf4 20 70 61 73 73 65 64 20 61 72 6f 75 6e 64 20 74 passed around t
14cf5 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 73 hrough all the s
14cf6 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 72 anity checking r
14cf7 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 72 outines.** in or
14cf8 64 65 72 20 74 6f 20 6b 65 65 70 20 74 72 61 63 der to keep trac
14cf9 6b 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62 61 6c k of some global
14cfa 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 state informati
14cfb 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 on..*/.typedef s
14cfc 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 truct IntegrityC
14cfd 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b 0a 73 k IntegrityCk;.s
14cfe 74 72 75 63 74 20 49 6e 74 65 67 72 69 74 79 43 truct IntegrityC
14cff 6b 20 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a k {. BtShared *
14d00 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 pBt; /* The t
14d01 72 65 65 20 62 65 69 6e 67 20 63 68 65 63 6b 65 ree being checke
14d02 64 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67 65 72 d out */. Pager
14d03 20 2a 70 50 61 67 65 72 3b 20 20 20 20 2f 2a 20 *pPager; /*
14d04 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 70 The associated p
14d05 61 67 65 72 2e 20 20 41 6c 73 6f 20 61 63 63 65 ager. Also acce
14d06 73 73 69 62 6c 65 20 62 79 20 70 42 74 2d 3e 70 ssible by pBt->p
14d07 50 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 Pager */. Pgno
14d08 6e 50 61 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 nPage; /*
14d09 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
14d0a 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
14d0b 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65 66 3b */. int *anRef;
14d0c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
14d0d 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68 20 70 of times each p
14d0e 61 67 65 20 69 73 20 72 65 66 65 72 65 6e 63 65 age is reference
14d0f 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 d */. int mxErr
14d10 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 70 ; /* Stop
14d11 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 65 72 accumulating er
14d12 72 6f 72 73 20 77 68 65 6e 20 74 68 69 73 20 72 rors when this r
14d13 65 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f 0a 20 eaches zero */.
14d14 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20 int nErr;
14d15 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
14d16 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e messages written
14d17 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f 20 66 to zErrMsg so f
14d18 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 6c 6c ar */. int mall
14d19 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41 20 6d ocFailed; /* A m
14d1a 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
14d1b 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 error has occur
14d1c 72 65 64 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 red */. StrAccu
14d1d 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20 41 63 m errMsg; /* Ac
14d1e 63 75 6d 75 6c 61 74 65 20 74 68 65 20 65 72 72 cumulate the err
14d1f 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 or message text
14d20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a here */.};../*.*
14d21 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74 65 20 * Read or write
14d22 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75 72 2d a two- and four-
14d23 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 byte big-endian
14d24 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 2e 0a integer values..
14d25 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 32 62 */.#define get2b
14d26 79 74 65 28 78 29 20 20 20 28 28 78 29 5b 30 5d yte(x) ((x)[0]
14d27 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a 23 64 <<8 | (x)[1]).#d
14d28 65 66 69 6e 65 20 70 75 74 32 62 79 74 65 28 70 efine put2byte(p
14d29 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20 28 75 ,v) ((p)[0] = (u
14d2a 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 70 29 5b 8)((v)>>8), (p)[
14d2b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 0a 23 64 1] = (u8)(v)).#d
14d2c 65 66 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 efine get4byte s
14d2d 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 qlite3Get4byte.#
14d2e 64 65 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 define put4byte
14d2f 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a sqlite3Put4byte.
14d30 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
14d31 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49 6e 74 End of btreeInt
14d32 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .h *************
14d33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14d34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
14d35 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ./**************
14d36 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 Continuing wher
14d37 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e e we left off in
14d38 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a btmutex.c *****
14d39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f ***************/
14d3a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
14d3b 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
14d3c 45 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 E.#if SQLITE_THR
14d3d 45 41 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a 20 4f EADSAFE../*.** O
14d3e 62 74 61 69 6e 20 74 68 65 20 42 74 53 68 61 72 btain the BtShar
14d3f 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 ed mutex associa
14d40 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 ted with B-Tree
14d41 68 61 6e 64 6c 65 20 70 2e 20 41 6c 73 6f 2c 0a handle p. Also,.
14d42 2a 2a 20 73 65 74 20 42 74 53 68 61 72 65 64 2e ** set BtShared.
14d43 64 62 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 db to the databa
14d44 73 65 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 se handle associ
14d45 61 74 65 64 20 77 69 74 68 20 70 20 61 6e 64 20 ated with p and
14d46 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b 65 64 the.** p->locked
14d47 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 74 72 75 65 boolean to true
14d48 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
14d49 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 lockBtreeMutex(
14d4a 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
14d4b 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d ert( p->locked==
14d4c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
14d4d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 qlite3_mutex_not
14d4e 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 held(p->pBt->mut
14d4f 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ex) );. assert(
14d50 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
14d51 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
14d52 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f ) );.. sqlite3_
14d53 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 mutex_enter(p->p
14d54 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d Bt->mutex);. p-
14d55 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 >pBt->db = p->db
14d56 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 ;. p->locked =
14d57 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 1;.}../*.** Rele
14d58 61 73 65 20 74 68 65 20 42 74 53 68 61 72 65 64 ase the BtShared
14d59 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 mutex associate
14d5a 64 20 77 69 74 68 20 42 2d 54 72 65 65 20 68 61 d with B-Tree ha
14d5b 6e 64 6c 65 20 70 20 61 6e 64 0a 2a 2a 20 63 6c ndle p and.** cl
14d5c 65 61 72 20 74 68 65 20 70 2d 3e 6c 6f 63 6b 65 ear the p->locke
14d5d 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a 73 74 d boolean..*/.st
14d5e 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b atic void unlock
14d5f 42 74 72 65 65 4d 75 74 65 78 28 42 74 72 65 65 BtreeMutex(Btree
14d60 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 *p){. assert(
14d61 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29 3b 0a p->locked==1 );.
14d62 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14d63 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
14d64 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
14d65 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
14d66 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
14d67 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
14d68 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d 70 2d ssert( p->db==p-
14d69 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 >pBt->db );.. s
14d6a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
14d6b 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ve(p->pBt->mutex
14d6c 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d );. p->locked =
14d6d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 0;.}../*.** Ent
14d6e 65 72 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 er a mutex on th
14d6f 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 6f 62 e given BTree ob
14d70 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ject..**.** If t
14d71 68 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74 he object is not
14d72 20 73 68 61 72 61 62 6c 65 2c 20 74 68 65 6e 20 sharable, then
14d73 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 76 65 72 no mutex is ever
14d74 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 61 6e 64 required.** and
14d75 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
14d76 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 65 20 75 a no-op. The u
14d77 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 65 78 20 nderlying mutex
14d78 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 is non-recursive
14d79 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b 65 65 70 ..** But we keep
14d7a 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 a reference cou
14d7b 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 61 6e 74 nt in Btree.want
14d7c 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 20 62 65 ToLock so the be
14d7d 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 havior.** of thi
14d7e 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 s interface is r
14d7f 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 ecursive..**.**
14d80 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 To avoid deadloc
14d81 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 42 74 72 ks, multiple Btr
14d82 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 64 20 69 ees are locked i
14d83 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 n the same order
14d84 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 74 61 62 .** by all datab
14d85 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e ase connections.
14d86 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 74 20 69 The p->pNext i
14d87 73 20 61 20 6c 69 73 74 20 6f 66 20 6f 74 68 65 s a list of othe
14d88 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 65 6c 6f r.** Btrees belo
14d89 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 73 61 6d nging to the sam
14d8a 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
14d8b 63 74 69 6f 6e 20 61 73 20 74 68 65 20 70 20 42 ction as the p B
14d8c 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 20 6e 65 tree.** which ne
14d8d 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 ed to be locked
14d8e 61 66 74 65 72 20 70 2e 20 20 49 66 20 77 65 20 after p. If we
14d8f 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c 6f 63 cannot get a loc
14d90 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 k on.** p, then
14d91 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c first unlock all
14d92 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73 20 6f of the others o
14d93 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68 65 6e n p->pNext, then
14d94 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 74 68 65 wait.** for the
14d95 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d 65 20 lock to become
14d96 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 available on p,
14d97 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 then relock all
14d98 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 73 65 71 of the.** subseq
14d99 75 65 6e 74 20 42 74 72 65 65 73 20 74 68 61 74 uent Btrees that
14d9a 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b 2e 0a desire a lock..
14d9b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14d9c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 E void sqlite3Bt
14d9d 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a reeEnter(Btree *
14d9e 70 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 4c 61 p){. Btree *pLa
14d9f 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 ter;.. /* Some
14da0 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65 basic sanity che
14da1 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 42 74 72 cking on the Btr
14da2 65 65 2e 20 20 54 68 65 20 6c 69 73 74 20 6f 66 ee. The list of
14da3 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 63 6f 6e Btrees. ** con
14da4 6e 65 63 74 65 64 20 62 79 20 70 4e 65 78 74 20 nected by pNext
14da5 61 6e 64 20 70 50 72 65 76 20 73 68 6f 75 6c 64 and pPrev should
14da6 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 be in sorted or
14da7 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 74 72 65 der by. ** Btre
14da8 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 41 6c 6c e.pBt value. All
14da9 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 elements of the
14daa 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 62 65 6c list should bel
14dab 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 ong to. ** the
14dac 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e same connection.
14dad 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 42 74 72 Only shared Btr
14dae 65 65 73 20 61 72 65 20 6f 6e 20 74 68 65 20 6c ees are on the l
14daf 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 ist. */. assert
14db0 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c ( p->pNext==0 ||
14db1 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 p->pNext->pBt>p
14db2 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 ->pBt );. asser
14db3 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c t( p->pPrev==0 |
14db4 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 42 74 3c | p->pPrev->pBt<
14db5 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 p->pBt );. asse
14db6 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 rt( p->pNext==0
14db7 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d || p->pNext->db=
14db8 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 =p->db );. asse
14db9 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30 20 rt( p->pPrev==0
14dba 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 64 62 3d || p->pPrev->db=
14dbb 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 =p->db );. asse
14dbc 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 rt( p->sharable
14dbd 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 || (p->pNext==0
14dbe 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30 29 20 && p->pPrev==0)
14dbf 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 );.. /* Check f
14dc0 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 or locking consi
14dc1 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 73 73 65 stency */. asse
14dc2 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c rt( !p->locked |
14dc3 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e | p->wantToLock>
14dc4 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
14dc5 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 70 2d ->sharable || p-
14dc6 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 >wantToLock==0 )
14dc7 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c ;.. /* We shoul
14dc8 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 d already hold a
14dc9 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
14dca 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
14dcb 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 */. assert( sq
14dcc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
14dcd 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 (p->db->mutex) )
14dce 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20 74 ;.. /* Unless t
14dcf 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 73 he database is s
14dd0 68 61 72 61 62 6c 65 20 61 6e 64 20 75 6e 6c 6f harable and unlo
14dd1 63 6b 65 64 2c 20 74 68 65 6e 20 42 74 53 68 61 cked, then BtSha
14dd2 72 65 64 2e 64 62 0a 20 20 2a 2a 20 73 68 6f 75 red.db. ** shou
14dd3 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20 73 65 ld already be se
14dd4 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f 0a t correctly. */.
14dd5 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 6c 6f assert( (p->lo
14dd6 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e 73 68 cked==0 && p->sh
14dd7 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e 70 42 arable) || p->pB
14dd8 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a t->db==p->db );.
14dd9 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 . if( !p->shara
14dda 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 ble ) return;.
14ddb 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b p->wantToLock++;
14ddc 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 . if( p->locked
14ddd 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a ) return;.. /*
14dde 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 In most cases,
14ddf 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c we should be abl
14de0 65 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 e to acquire the
14de1 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 lock we. ** wa
14de2 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e nt without havin
14de3 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 g to go throught
14de4 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c the ascending l
14de5 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 ock. ** procedu
14de6 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e re that follows.
14de7 20 20 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e Just be sure n
14de8 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a ot to block.. *
14de9 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f /. if( sqlite3_
14dea 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 mutex_try(p->pBt
14deb 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 ->mutex)==SQLITE
14dec 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 70 42 _OK ){. p->pB
14ded 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 t->db = p->db;.
14dee 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 p->locked = 1
14def 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 ;. return;.
14df0 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f 69 64 }.. /* To avoid
14df1 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72 73 74 deadlock, first
14df2 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c 6f 63 release all loc
14df3 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67 65 72 ks with a larger
14df4 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64 20 61 . ** BtShared a
14df5 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20 61 63 ddress. Then ac
14df6 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b 2e 20 quire our lock.
14df7 20 54 68 65 6e 20 72 65 61 63 71 75 69 72 65 0a Then reacquire.
14df8 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 42 ** the other B
14df9 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20 74 68 tShared locks th
14dfa 61 74 20 77 65 20 75 73 65 64 20 74 6f 20 68 6f at we used to ho
14dfb 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 0a ld in ascending.
14dfc 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20 2a 2f ** order.. */
14dfd 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d . for(pLater=p-
14dfe 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 >pNext; pLater;
14dff 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 pLater=pLater->p
14e00 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73 65 72 Next){. asser
14e01 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61 72 61 t( pLater->shara
14e02 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ble );. asser
14e03 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 t( pLater->pNext
14e04 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d 3e 70 ==0 || pLater->p
14e05 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74 65 72 Next->pBt>pLater
14e06 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
14e07 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e 6c 6f ert( !pLater->lo
14e08 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72 2d 3e cked || pLater->
14e09 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a wantToLock>0 );.
14e0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e if( pLater->
14e0b 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 locked ){.
14e0c 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 unlockBtreeMutex
14e0d 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d 0a (pLater);. }.
14e0e 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 72 65 65 4d }. lockBtreeM
14e0f 75 74 65 78 28 70 29 3b 0a 20 20 66 6f 72 28 70 utex(p);. for(p
14e10 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 Later=p->pNext;
14e11 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 pLater; pLater=p
14e12 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 Later->pNext){.
14e13 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 77 if( pLater->w
14e14 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 antToLock ){.
14e15 20 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 lockBtreeMute
14e16 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 7d x(pLater);. }
14e17 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 . }.}../*.** Ex
14e18 69 74 20 74 68 65 20 72 65 63 75 72 73 69 76 65 it the recursive
14e19 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 72 65 mutex on a Btre
14e1a 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
14e1b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
14e1c 33 42 74 72 65 65 4c 65 61 76 65 28 42 74 72 65 3BtreeLeave(Btre
14e1d 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e e *p){. if( p->
14e1e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 sharable ){.
14e1f 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 assert( p->wantT
14e20 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 70 oLock>0 );. p
14e21 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a ->wantToLock--;.
14e22 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 if( p->wantT
14e23 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 oLock==0 ){.
14e24 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 unlockBtreeMut
14e25 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ex(p);. }. }
14e26 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 .}..#ifndef NDEB
14e27 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 UG./*.** Return
14e28 74 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68 true if the BtSh
14e29 61 72 65 64 20 6d 75 74 65 78 20 69 73 20 68 65 ared mutex is he
14e2a 6c 64 20 6f 6e 20 74 68 65 20 62 74 72 65 65 2c ld on the btree,
14e2b 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 42 2d or if the.** B-
14e2c 54 72 65 65 20 69 73 20 6e 6f 74 20 6d 61 72 6b Tree is not mark
14e2d 65 64 20 61 73 20 73 68 61 72 61 62 6c 65 2e 0a ed as sharable..
14e2e 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
14e2f 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 ne is used only
14e30 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73 73 65 from within asse
14e31 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e rt() statements.
14e32 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
14e33 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
14e34 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 reeHoldsMutex(Bt
14e35 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
14e36 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d t( p->sharable==
14e37 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 0 || p->locked==
14e38 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 0 || p->wantToLo
14e39 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 ck>0 );. assert
14e3a 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 ( p->sharable==0
14e3b 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 || p->locked==0
14e3c 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e 70 42 || p->db==p->pB
14e3d 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 t->db );. asser
14e3e 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d 3d t( p->sharable==
14e3f 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 0 || p->locked==
14e40 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 0 || sqlite3_mut
14e41 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e ex_held(p->pBt->
14e42 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
14e43 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d rt( p->sharable=
14e44 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d =0 || p->locked=
14e45 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
14e46 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
14e47 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72 65 74 mutex) );.. ret
14e48 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62 6c 65 urn (p->sharable
14e49 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 ==0 || p->locked
14e4a 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 );.}.#endif...#i
14e4b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
14e4c 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a T_INCRBLOB./*.**
14e4d 20 45 6e 74 65 72 20 61 6e 64 20 6c 65 61 76 65 Enter and leave
14e4e 20 61 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74 a mutex on a Bt
14e4f 72 65 65 20 67 69 76 65 6e 20 61 20 63 75 72 73 ree given a curs
14e50 6f 72 20 6f 77 6e 65 64 20 62 79 20 74 68 61 74 or owned by that
14e51 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54 68 65 73 .** Btree. Thes
14e52 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 20 61 e entry points a
14e53 72 65 20 75 73 65 64 20 62 79 20 69 6e 63 72 65 re used by incre
14e54 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64 20 63 mental I/O and c
14e55 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 74 65 64 an be.** omitted
14e56 20 69 66 20 74 68 61 74 20 6d 6f 64 75 6c 65 20 if that module
14e57 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a is not used..*/.
14e58 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
14e59 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
14e5a 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 EnterCursor(BtCu
14e5b 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 rsor *pCur){. s
14e5c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
14e5d 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a (pCur->pBtree);.
14e5e 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 }.SQLITE_PRIVATE
14e5f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 void sqlite3Btr
14e60 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 42 74 eeLeaveCursor(Bt
14e61 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 Cursor *pCur){.
14e62 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
14e63 76 65 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29 ve(pCur->pBtree)
14e64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
14e65 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
14e66 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e OB */.../*.** En
14e67 74 65 72 20 74 68 65 20 6d 75 74 65 78 20 6f 6e ter the mutex on
14e68 20 65 76 65 72 79 20 42 74 72 65 65 20 61 73 73 every Btree ass
14e69 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 ociated with a d
14e6a 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
14e6b 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 ction. This is
14e6c 6e 65 65 64 65 64 20 28 66 6f 72 20 65 78 61 6d needed (for exam
14e6d 70 6c 65 29 20 70 72 69 6f 72 20 74 6f 20 70 61 ple) prior to pa
14e6e 72 73 69 6e 67 0a 2a 2a 20 61 20 73 74 61 74 65 rsing.** a state
14e6f 6d 65 6e 74 20 73 69 6e 63 65 20 77 65 20 77 69 ment since we wi
14e70 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e 67 20 ll be comparing
14e71 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e table and column
14e72 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 69 6e 73 names.** agains
14e73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 20 61 6e t all schemas an
14e74 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 d we do not want
14e75 20 74 68 6f 73 65 20 73 63 68 65 6d 61 73 20 62 those schemas b
14e76 65 69 6e 67 0a 2a 2a 20 72 65 73 65 74 20 6f 75 eing.** reset ou
14e77 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e t from under us.
14e78 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 .**.** There is
14e79 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 a corresponding
14e7a 6c 65 61 76 65 2d 61 6c 6c 20 70 72 6f 63 65 64 leave-all proced
14e7b 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 ures..**.** Ente
14e7c 72 20 74 68 65 20 6d 75 74 65 78 65 73 20 69 6e r the mutexes in
14e7d 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 accending order
14e7e 20 62 79 20 42 74 53 68 61 72 65 64 20 70 6f 69 by BtShared poi
14e7f 6e 74 65 72 20 61 64 64 72 65 73 73 0a 2a 2a 20 nter address.**
14e80 74 6f 20 61 76 6f 69 64 20 74 68 65 20 70 6f 73 to avoid the pos
14e81 73 69 62 69 6c 69 74 79 20 6f 66 20 64 65 61 64 sibility of dead
14e82 6c 6f 63 6b 20 77 68 65 6e 20 74 77 6f 20 74 68 lock when two th
14e83 72 65 61 64 73 20 77 69 74 68 0a 2a 2a 20 74 77 reads with.** tw
14e84 6f 20 6f 72 20 6d 6f 72 65 20 62 74 72 65 65 73 o or more btrees
14e85 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74 68 20 in common both
14e86 74 72 79 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 try to lock all
14e87 74 68 65 69 72 20 62 74 72 65 65 73 0a 2a 2a 20 their btrees.**
14e88 61 74 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 at the same inst
14e89 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ant..*/.SQLITE_P
14e8a 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
14e8b 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
14e8c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 (sqlite3 *db){.
14e8d 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 int i;. Btree
14e8e 2a 70 2c 20 2a 70 4c 61 74 65 72 3b 0a 20 20 61 *p, *pLater;. a
14e8f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
14e90 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 utex_held(db->mu
14e91 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d tex) );. for(i=
14e92 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
14e93 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e +){. p = db->
14e94 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 aDb[i].pBt;.
14e95 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20 28 70 assert( !p || (p
14e96 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 ->locked==0 && p
14e97 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 ->sharable) || p
14e98 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 ->pBt->db==p->db
14e99 20 29 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 );. if( p &&
14e9a 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
14e9b 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c p->wantToL
14e9c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 ock++;. if(
14e9d 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 !p->locked ){.
14e9e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
14e9f 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 31 20 ->wantToLock==1
14ea0 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 );. while
14ea1 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70 20 3d ( p->pPrev ) p =
14ea2 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 p->pPrev;.
14ea3 20 20 20 2f 2a 20 52 65 61 73 6f 6e 20 66 6f 72 /* Reason for
14ea4 20 41 4c 57 41 59 53 3a 20 20 54 68 65 72 65 20 ALWAYS: There
14ea5 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 must be at least
14ea6 20 6f 6e 20 75 6e 6c 6f 63 6b 65 64 20 42 74 72 on unlocked Btr
14ea7 65 65 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a ee in. **
14ea8 20 74 68 65 20 63 68 61 69 6e 2e 20 20 4f 74 68 the chain. Oth
14ea9 65 72 77 69 73 65 20 74 68 65 20 21 70 2d 3e 6c erwise the !p->l
14eaa 6f 63 6b 65 64 20 74 65 73 74 20 61 62 6f 76 65 ocked test above
14eab 20 77 6f 75 6c 64 20 68 61 76 65 20 66 61 69 6c would have fail
14eac 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 ed */. wh
14ead 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 26 ile( p->locked &
14eae 26 20 41 4c 57 41 59 53 28 70 2d 3e 70 4e 65 78 & ALWAYS(p->pNex
14eaf 74 29 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78 t) ) p = p->pNex
14eb0 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 t;. for(p
14eb1 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74 Later = p->pNext
14eb2 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 ; pLater; pLater
14eb3 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b =pLater->pNext){
14eb4 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 . if( p
14eb5 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b Later->locked ){
14eb6 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 6c . unl
14eb7 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 4c ockBtreeMutex(pL
14eb8 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 ater);.
14eb9 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
14eba 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b while( p ){
14ebb 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 42 . lockB
14ebc 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14ebd 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 p = p->p
14ebe 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a Next;. }.
14ebf 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
14ec0 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 }.}.SQLITE_PRIVA
14ec1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
14ec2 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c treeLeaveAll(sql
14ec3 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 ite3 *db){. int
14ec4 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 3b 0a i;. Btree *p;.
14ec5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
14ec6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3_mutex_held(db-
14ec7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
14ec8 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b (i=0; i<db->nDb;
14ec9 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 i++){. p = d
14eca 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 b->aDb[i].pBt;.
14ecb 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73 if( p && p->s
14ecc 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 harable ){.
14ecd 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 assert( p->want
14ece 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20 20 ToLock>0 );.
14ecf 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d p->wantToLock-
14ed0 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e -;. if( p->
14ed1 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b wantToLock==0 ){
14ed2 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 . unlockB
14ed3 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14ed4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
14ed5 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 }..#ifndef NDEBU
14ed6 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 G./*.** Return t
14ed7 72 75 65 20 69 66 20 74 68 65 20 63 75 72 72 65 rue if the curre
14ed8 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 nt thread holds
14ed9 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e the database con
14eda 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74 65 78 nection.** mutex
14edb 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69 72 65 and all require
14edc 64 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 d BtShared mutex
14edd 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 es..**.** This r
14ede 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 69 outine is used i
14edf 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 nside assert() s
14ee0 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 0a tatements only..
14ee1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
14ee2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
14ee3 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 eeHoldsAllMutexe
14ee4 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a s(sqlite3 *db){.
14ee5 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 int i;. if( !
14ee6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14ee7 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 7b ld(db->mutex) ){
14ee8 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
14ee9 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c }. for(i=0; i<
14eea 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 db->nDb; i++){.
14eeb 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20 20 20 Btree *p;.
14eec 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e p = db->aDb[i].
14eed 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 pBt;. if( p &
14eee 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 26 26 & p->sharable &&
14eef 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e 77 61 . (p->wa
14ef0 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 21 ntToLock==0 || !
14ef1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
14ef2 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 ld(p->pBt->mutex
14ef3 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 )) ){. retu
14ef4 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a rn 0;. }. }.
14ef5 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
14ef6 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a ndif /* NDEBUG *
14ef7 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e /../*.** Add a n
14ef8 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74 65 72 ew Btree pointer
14ef9 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74 65 78 to a BtreeMutex
14efa 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20 74 68 Array. .** if th
14efb 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20 70 6f e pointer can po
14efc 73 73 69 62 6c 79 20 62 65 20 73 68 61 72 65 64 ssibly be shared
14efd 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68 65 72 with.** another
14efe 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
14eff 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 tion..**.** The
14f00 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b 65 70 pointers are kep
14f01 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 t in sorted orde
14f02 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70 42 74 r by pBtree->pBt
14f03 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79 20 77 . That.** way w
14f04 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65 6e 74 hen we go to ent
14f05 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74 65 78 er all the mutex
14f06 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74 65 72 es, we can enter
14f07 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72 64 65 them.** in orde
14f08 72 20 77 69 74 68 6f 75 74 20 65 76 65 72 79 20 r without every
14f09 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b 75 70 having to backup
14f0a 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64 20 77 and retry and w
14f0b 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72 79 69 ithout.** worryi
14f0c 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c 6f 63 ng about deadloc
14f0d 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d k..**.** The num
14f0e 62 65 72 20 6f 66 20 73 68 61 72 65 64 20 62 74 ber of shared bt
14f0f 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61 79 73 rees will always
14f10 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75 61 6c be small (usual
14f11 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20 73 6f ly 0 or 1).** so
14f12 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f an insertion so
14f13 72 74 20 69 73 20 61 6e 20 61 64 65 71 75 61 74 rt is an adequat
14f14 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65 72 65 e algorithm here
14f15 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14f16 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14f17 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 BtreeMutexArrayI
14f18 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 nsert(BtreeMutex
14f19 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c 20 42 Array *pArray, B
14f1a 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b 0a 20 tree *pBtree){.
14f1b 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42 74 53 int i, j;. BtS
14f1c 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 66 hared *pBt;. if
14f1d 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c 20 70 ( pBtree==0 || p
14f1e 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c 65 3d Btree->sharable=
14f1f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 =0 ) return;.#if
14f20 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a ndef NDEBUG. {.
14f21 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
14f22 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 Array->nMutex; i
14f23 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 ++){. asser
14f24 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 t( pArray->aBtre
14f25 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a e[i]!=pBtree );.
14f26 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
14f27 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 . assert( pArra
14f28 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29 3b 0a y->nMutex>=0 );.
14f29 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79 assert( pArray
14f2a 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79 53 69 ->nMutex<ArraySi
14f2b 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74 72 65 ze(pArray->aBtre
14f2c 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20 3d 20 e)-1 );. pBt =
14f2d 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 66 pBtree->pBt;. f
14f2e 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 or(i=0; i<pArray
14f2f 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a ->nMutex; i++){.
14f30 20 20 20 20 61 73 73 65 72 74 28 20 70 41 72 72 assert( pArr
14f31 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 ay->aBtree[i]!=p
14f32 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66 28 Btree );. if(
14f33 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b pArray->aBtree[
14f34 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b 0a 20 i]->pBt>pBt ){.
14f35 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72 72 61 for(j=pArra
14f36 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69 3b 20 y->nMutex; j>i;
14f37 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 70 41 j--){. pA
14f38 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a 5d 20 rray->aBtree[j]
14f39 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 = pArray->aBtree
14f3a 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 [j-1];. }.
14f3b 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74 pArray->aBt
14f3c 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65 65 3b ree[i] = pBtree;
14f3d 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 6e . pArray->n
14f3e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20 20 72 Mutex++;. r
14f3f 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d eturn;. }. }
14f40 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 . pArray->aBtre
14f41 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 e[pArray->nMutex
14f42 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a 7d 0a ++] = pBtree;.}.
14f43 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 ./*.** Enter the
14f44 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 mutex of every
14f45 62 74 72 65 65 20 69 6e 20 74 68 65 20 61 72 72 btree in the arr
14f46 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ay. This routin
14f47 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 61 e is.** called a
14f48 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
14f49 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 of sqlite3VdbeEx
14f4a 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74 65 78 ec(). The mutex
14f4b 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74 65 64 es are.** exited
14f4c 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 at the end of t
14f4d 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e he same function
14f4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
14f4f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
14f50 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 BtreeMutexArrayE
14f51 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 nter(BtreeMutexA
14f52 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 rray *pArray){.
14f53 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
14f54 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 0; i<pArray->nMu
14f55 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 tex; i++){. B
14f56 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 tree *p = pArray
14f57 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 ->aBtree[i];.
14f58 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 /* Some basic s
14f59 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a anity checking *
14f5a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d /. assert( i=
14f5b 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 =0 || pArray->aB
14f5c 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 tree[i-1]->pBt<p
14f5d 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 ->pBt );. ass
14f5e 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 ert( !p->locked
14f5f 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b || p->wantToLock
14f60 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 >0 );.. /* We
14f61 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
14f62 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 hold a lock on t
14f63 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e he database conn
14f64 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 ection */. as
14f65 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
14f66 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
14f67 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f mutex) );.. /
14f68 2a 20 54 68 65 20 42 74 72 65 65 20 69 73 20 73 * The Btree is s
14f69 68 61 72 61 62 6c 65 20 62 65 63 61 75 73 65 20 harable because
14f6a 6f 6e 6c 79 20 73 68 61 72 61 62 6c 65 20 42 74 only sharable Bt
14f6b 72 65 65 73 20 61 72 65 20 65 6e 74 65 72 65 64 rees are entered
14f6c 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 . ** into the
14f6d 20 61 72 72 61 79 20 69 6e 20 74 68 65 20 66 69 array in the fi
14f6e 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 rst place. */.
14f6f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 assert( p->sha
14f70 72 61 62 6c 65 20 29 3b 0a 0a 20 20 20 20 70 2d rable );.. p-
14f71 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 >wantToLock++;.
14f72 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 if( !p->locke
14f73 64 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 d ){. lockB
14f74 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14f75 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
14f76 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 65 78 Leave the mutex
14f77 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20 of every btree
14f78 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f in the group..*/
14f79 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14f7a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14f7b 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 eMutexArrayLeave
14f7c 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 (BtreeMutexArray
14f7d 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 *pArray){. int
14f7e 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 i;. for(i=0; i
14f7f 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b <pArray->nMutex;
14f80 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 i++){. Btree
14f81 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 *p = pArray->aB
14f82 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 tree[i];. /*
14f83 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 Some basic sanit
14f84 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 y checking */.
14f85 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c assert( i==0 |
14f86 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 | pArray->aBtree
14f87 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 [i-1]->pBt<p->pB
14f88 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 t );. assert(
14f89 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a 20 20 p->locked );.
14f8a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e assert( p->wan
14f8b 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 tToLock>0 );..
14f8c 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 /* We should a
14f8d 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f lready hold a lo
14f8e 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 ck on the databa
14f8f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
14f90 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c . assert( sql
14f91 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
14f92 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
14f93 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c .. p->wantToL
14f94 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 ock--;. if( p
14f95 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 ->wantToLock==0
14f96 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 ){. unlockB
14f97 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 treeMutex(p);.
14f98 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 }. }.}..#else
14f99 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
14f9a 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
14f9b 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 eEnter(Btree *p)
14f9c 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d {. p->pBt->db =
14f9d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49 54 45 p->db;.}.SQLITE
14f9e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
14f9f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 lite3BtreeEnterA
14fa0 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b ll(sqlite3 *db){
14fa1 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
14fa2 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 i=0; i<db->nDb;
14fa3 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 i++){. Btree
14fa4 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e *p = db->aDb[i].
14fa5 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20 29 pBt;. if( p )
14fa6 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 74 2d 3e {. p->pBt->
14fa7 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 db = p->db;.
14fa8 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f }. }.}.#endif /
14fa9 2a 20 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 * if SQLITE_THRE
14faa 41 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64 69 66 ADSAFE */.#endif
14fab 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 /* ifndef SQLIT
14fac 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
14fad 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a CHE */../*******
14fae 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 ******* End of b
14faf 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a tmutex.c *******
14fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fb2 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a ******/./*******
14fb3 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 ******* Begin fi
14fb4 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a le btree.c *****
14fb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fb7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 ******/./*.** 20
14fb8 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 04 April 6.**.**
14fb9 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 The author disc
14fba 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 laims copyright
14fbb 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 to this source c
14fbc 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f ode. In place o
14fbd 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 f.** a legal not
14fbe 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 ice, here is a b
14fbf 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 lessing:.**.**
14fc0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f May you do goo
14fc1 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a d and not evil..
14fc2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 ** May you fi
14fc3 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 nd forgiveness f
14fc4 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 or yourself and
14fc5 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a forgive others..
14fc6 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 ** May you sh
14fc7 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 are freely, neve
14fc8 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 r taking more th
14fc9 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a an you give..**.
14fca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
14fce 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 *********.** Thi
14fcf 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 s file implement
14fd0 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69 s a external (di
14fd1 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61 sk-based) databa
14fd2 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e se using BTrees.
14fd3 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64 .** See the head
14fd4 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 er comment on "b
14fd5 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 treeInt.h" for a
14fd6 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
14fd7 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 ation..** Includ
14fd8 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f ing a descriptio
14fd9 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 n of file format
14fda 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77 and an overview
14fdb 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a of operation..*
14fdc 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 /../*.** The hea
14fdd 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20 der string that
14fde 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 appears at the b
14fdf 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 eginning of ever
14fe0 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 y.** SQLite data
14fe1 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 base..*/.static
14fe2 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69 const char zMagi
14fe3 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 cHeader[] = SQLI
14fe4 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a TE_FILE_HEADER;.
14fe5 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 ./*.** Set this
14fe6 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
14fe7 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 to 1 to enable t
14fe8 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65 racing using the
14fe9 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e TRACE.** macro.
14fea 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 .*/.#if 0.int sq
14feb 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d lite3BtreeTrace=
14fec 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 1; /* True to e
14fed 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f nable tracing */
14fee 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28 .# define TRACE(
14fef 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74 X) if(sqlite3Bt
14ff0 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66 reeTrace){printf
14ff1 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74 X;fflush(stdout
14ff2 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 );}.#else.# defi
14ff3 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64 ne TRACE(X).#end
14ff4 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 if....#ifndef SQ
14ff5 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 LITE_OMIT_SHARED
14ff6 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c _CACHE./*.** A l
14ff7 69 73 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 ist of BtShared
14ff8 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 objects that are
14ff9 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 eligible for pa
14ffa 72 74 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 rticipation.** i
14ffb 6e 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 n shared cache.
14ffc 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 This variable h
14ffd 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 as file scope du
14ffe 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c ring normal buil
14fff 64 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 ds,.** but the t
15000 65 73 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 est harness need
15001 73 20 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 s to access it s
15002 6f 20 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f o we make it glo
15003 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 bal for .** test
15004 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 builds..**.** A
15005 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 76 61 ccess to this va
15006 72 69 61 62 6c 65 20 69 73 20 70 72 6f 74 65 63 riable is protec
15007 74 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 ted by SQLITE_MU
15008 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 TEX_STATIC_MASTE
15009 52 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c R..*/.#ifdef SQL
1500a 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f ITE_TEST.SQLITE_
1500b 50 52 49 56 41 54 45 20 42 74 53 68 61 72 65 64 PRIVATE BtShared
1500c 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c *SQLITE_WSD sql
1500d 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c ite3SharedCacheL
1500e 69 73 74 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 ist = 0;.#else.s
1500f 74 61 74 69 63 20 42 74 53 68 61 72 65 64 20 2a tatic BtShared *
15010 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 SQLITE_WSD sqlit
15011 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 e3SharedCacheLis
15012 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 t = 0;.#endif.#e
15013 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
15014 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
15015 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
15016 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f ITE_OMIT_SHARED_
15017 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 CACHE./*.** Enab
15018 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 le or disable th
15019 65 20 73 68 61 72 65 64 20 70 61 67 65 72 20 61 e shared pager a
1501a 6e 64 20 73 63 68 65 6d 61 20 66 65 61 74 75 72 nd schema featur
1501b 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 es..**.** This r
1501c 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 outine has no ef
1501d 66 65 63 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 fect on existing
1501e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
1501f 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 tions..** The sh
15020 61 72 65 64 20 63 61 63 68 65 20 73 65 74 74 69 ared cache setti
15021 6e 67 20 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 ng effects only
15022 66 75 74 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a future calls to.
15023 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 ** sqlite3_open(
15024 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 ), sqlite3_open1
15025 36 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6(), or sqlite3_
15026 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 open_v2()..*/.SQ
15027 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
15028 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 ite3_enable_shar
15029 65 64 5f 63 61 63 68 65 28 69 6e 74 20 65 6e 61 ed_cache(int ena
1502a 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 ble){. sqlite3G
1502b 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 lobalConfig.shar
1502c 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 3d edCacheEnabled =
1502d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 enable;. retur
1502e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
1502f 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 64 65 66 20 endif....#ifdef
15030 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15031 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 ED_CACHE. /*.
15032 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 ** The functions
15033 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 querySharedCach
15034 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 eTableLock(), se
15035 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c tSharedCacheTabl
15036 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e eLock(),. ** an
15037 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 d clearAllShared
15038 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 CacheTableLocks(
15039 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 ). ** manipulat
1503a 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 e entries in the
1503b 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 BtShared.pLock
1503c 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64 linked list used
1503d 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 to store. ** s
1503e 68 61 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c hared-cache tabl
1503f 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 e level locks. I
15040 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 f the library is
15041 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 compiled with t
15042 68 65 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 he. ** shared-c
15043 61 63 68 65 20 66 65 61 74 75 72 65 20 64 69 73 ache feature dis
15044 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 abled, then ther
15045 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f e is only ever o
15046 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 ne user. ** of
15047 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74 each BtShared st
15048 72 75 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74 ructure and so t
15049 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e his locking is n
1504a 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 ot necessary. .
1504b 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68 ** So define th
1504c 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 e lock related f
1504d 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f unctions as no-o
1504e 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 ps.. */. #defi
1504f 6e 65 20 71 75 65 72 79 53 68 61 72 65 64 43 61 ne querySharedCa
15050 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 cheTableLock(a,b
15051 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 ,c) SQLITE_OK.
15052 23 64 65 66 69 6e 65 20 73 65 74 53 68 61 72 65 #define setShare
15053 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
15054 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b a,b,c) SQLITE_OK
15055 0a 20 20 23 64 65 66 69 6e 65 20 63 6c 65 61 72 . #define clear
15056 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 AllSharedCacheTa
15057 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 bleLocks(a). #d
15058 65 66 69 6e 65 20 64 6f 77 6e 67 72 61 64 65 41 efine downgradeA
15059 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 llSharedCacheTab
1505a 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 leLocks(a). #de
1505b 66 69 6e 65 20 68 61 73 53 68 61 72 65 64 43 61 fine hasSharedCa
1505c 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 cheTableLock(a,b
1505d 2c 63 2c 64 29 20 31 0a 20 20 23 64 65 66 69 6e ,c,d) 1. #defin
1505e 65 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 e hasReadConflic
1505f 74 73 28 61 2c 20 62 29 20 30 0a 23 65 6e 64 69 ts(a, b) 0.#endi
15060 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
15061 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
15062 43 48 45 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 CHE..#ifdef SQLI
15063 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a TE_DEBUG./*.****
15064 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 This function i
15065 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73 20 70 s only used as p
15066 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65 72 74 art of an assert
15067 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2a () statement. **
15068 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f *.**.** Check to
15069 20 73 65 65 20 69 66 20 70 42 74 72 65 65 20 68 see if pBtree h
1506a 6f 6c 64 73 20 74 68 65 20 72 65 71 75 69 72 65 olds the require
1506b 64 20 6c 6f 63 6b 73 20 74 6f 20 72 65 61 64 20 d locks to read
1506c 6f 72 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 or write to the
1506d 0a 2a 2a 20 74 61 62 6c 65 20 77 69 74 68 20 72 .** table with r
1506e 6f 6f 74 20 70 61 67 65 20 69 52 6f 6f 74 2e 20 oot page iRoot.
1506f 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 69 74 Return 1 if it
15070 20 64 6f 65 73 20 61 6e 64 20 30 20 69 66 20 6e does and 0 if n
15071 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 ot..**.** For ex
15072 61 6d 70 6c 65 2c 20 77 68 65 6e 20 77 72 69 74 ample, when writ
15073 69 6e 67 20 74 6f 20 61 20 74 61 62 6c 65 20 77 ing to a table w
15074 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 ith root-page iR
15075 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74 72 65 oot via .** Btre
15076 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 74 e connection pBt
15077 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 73 ree:.**.** as
15078 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 sert( hasSharedC
15079 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 42 acheTableLock(pB
1507a 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30 2c 20 tree, iRoot, 0,
1507b 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b 0a 2a WRITE_LOCK) );.*
1507c 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74 69 6e *.** When writin
1507d 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20 74 68 g to an index th
1507e 61 74 20 72 65 73 69 64 65 73 20 69 6e 20 61 20 at resides in a
1507f 73 68 61 72 61 62 6c 65 20 64 61 74 61 62 61 73 sharable databas
15080 65 2c 20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 e, the .** calle
15081 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20 66 69 r should have fi
15082 72 73 74 20 6f 62 74 61 69 6e 65 64 20 61 20 6c rst obtained a l
15083 6f 63 6b 20 73 70 65 63 69 66 79 69 6e 67 20 74 ock specifying t
15084 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a he root page of.
15085 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e ** the correspon
15086 64 69 6e 67 20 74 61 62 6c 65 2e 20 54 68 69 73 ding table. This
15087 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20 61 20 makes things a
15088 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 bit more complic
15089 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68 69 73 ated,.** as this
1508a 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73 20 65 module treats e
1508b 61 63 68 20 74 61 62 6c 65 20 61 73 20 61 20 73 ach table as a s
1508c 65 70 61 72 61 74 65 20 73 74 72 75 63 74 75 72 eparate structur
1508d 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e 65 0a e. To determine.
1508e 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 63 6f 72 ** the table cor
1508f 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 responding to th
15090 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 77 72 e index being wr
15091 69 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a 20 66 itten, this.** f
15092 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 unction has to s
15093 65 61 72 63 68 20 74 68 72 6f 75 67 68 20 74 68 earch through th
15094 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d e database schem
15095 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 a..**.** Instead
15096 20 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 of a lock on th
15097 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20 72 6f e table/index ro
15098 6f 74 65 64 20 61 74 20 70 61 67 65 20 69 52 6f oted at page iRo
15099 6f 74 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d ot, the caller m
1509a 61 79 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 72 69 ay.** hold a wri
1509b 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 te-lock on the s
1509c 63 68 65 6d 61 20 74 61 62 6c 65 20 28 72 6f 6f chema table (roo
1509d 74 20 70 61 67 65 20 31 29 2e 20 54 68 69 73 20 t page 1). This
1509e 69 73 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 65 70 is also.** accep
1509f 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 table..*/.static
150a0 20 69 6e 74 20 68 61 73 53 68 61 72 65 64 43 61 int hasSharedCa
150a1 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 cheTableLock(.
150a2 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 Btree *pBtree,
150a3 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 /* Handle
150a4 20 74 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20 that must hold
150a5 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 lock */. Pgno i
150a6 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 Root,
150a7 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 /* Root page of
150a8 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 b-tree */. int
150a9 20 69 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20 isIndex,
150aa 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 /* True if i
150ab 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74 Root is the root
150ac 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 of an index b-t
150ad 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4c 6f ree */. int eLo
150ae 63 6b 54 79 70 65 20 20 20 20 20 20 20 20 20 20 ckType
150af 2f 2a 20 52 65 71 75 69 72 65 64 20 6c 6f 63 6b /* Required lock
150b0 20 74 79 70 65 20 28 52 45 41 44 5f 4c 4f 43 4b type (READ_LOCK
150b1 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 or WRITE_LOCK)
150b2 2a 2f 0a 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a */.){. Schema *
150b3 70 53 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d pSchema = (Schem
150b4 61 20 2a 29 70 42 74 72 65 65 2d 3e 70 42 74 2d a *)pBtree->pBt-
150b5 3e 70 53 63 68 65 6d 61 3b 0a 20 20 50 67 6e 6f >pSchema;. Pgno
150b6 20 69 54 61 62 20 3d 20 30 3b 0a 20 20 42 74 4c iTab = 0;. BtL
150b7 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 20 2f ock *pLock;.. /
150b8 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61 * If this databa
150b9 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72 65 61 se is not sharea
150ba 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65 20 63 ble, or if the c
150bb 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 lient is reading
150bc 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20 74 68 . ** and has th
150bd 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 e read-uncommitt
150be 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 ed flag set, the
150bf 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72 65 71 n no lock is req
150c0 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 52 65 74 uired. . ** Ret
150c1 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64 69 61 urn true immedia
150c2 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 tely.. */. if(
150c3 20 28 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 (pBtree->sharab
150c4 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c 20 28 65 4c le==0). || (eL
150c5 6f 63 6b 54 79 70 65 3d 3d 52 45 41 44 5f 4c 4f ockType==READ_LO
150c6 43 4b 20 26 26 20 28 70 42 74 72 65 65 2d 3e 64 CK && (pBtree->d
150c7 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
150c8 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 E_ReadUncommitte
150c9 64 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 d)). ){. ret
150ca 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 1;. }.. /*
150cb 20 49 66 20 74 68 65 20 63 6c 69 65 6e 74 20 69 If the client i
150cc 73 20 72 65 61 64 69 6e 67 20 20 6f 72 20 77 72 s reading or wr
150cd 69 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 61 iting an index a
150ce 6e 64 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 nd the schema is
150cf 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64 65 64 . ** not loaded
150d0 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 6f 6f , then it is too
150d1 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 61 63 difficult to ac
150d2 74 75 61 6c 6c 79 20 63 68 65 63 6b 20 74 6f 20 tually check to
150d3 73 65 65 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 see if. ** the
150d4 63 6f 72 72 65 63 74 20 6c 6f 63 6b 73 20 61 72 correct locks ar
150d5 65 20 68 65 6c 64 2e 20 20 53 6f 20 64 6f 20 6e e held. So do n
150d6 6f 74 20 62 6f 74 68 65 72 20 2d 20 6a 75 73 74 ot bother - just
150d7 20 72 65 74 75 72 6e 20 74 72 75 65 2e 0a 20 20 return true..
150d8 2a 2a 20 54 68 69 73 20 63 61 73 65 20 64 6f 65 ** This case doe
150d9 73 20 6e 6f 74 20 63 6f 6d 65 20 75 70 20 76 65 s not come up ve
150da 72 79 20 6f 66 74 65 6e 20 61 6e 79 68 6f 77 2e ry often anyhow.
150db 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e . */. if( isIn
150dc 64 65 78 20 26 26 20 28 21 70 53 63 68 65 6d 61 dex && (!pSchema
150dd 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d 3e 66 6c || (pSchema->fl
150de 61 67 73 26 44 42 5f 53 63 68 65 6d 61 4c 6f 61 ags&DB_SchemaLoa
150df 64 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 ded)==0) ){.
150e0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 return 1;. }..
150e1 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 /* Figure out t
150e2 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 68 61 he root-page tha
150e3 74 20 74 68 65 20 6c 6f 63 6b 20 73 68 6f 75 6c t the lock shoul
150e4 64 20 62 65 20 68 65 6c 64 20 6f 6e 2e 20 46 6f d be held on. Fo
150e5 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 62 2d 74 r table. ** b-t
150e6 72 65 65 73 2c 20 74 68 69 73 20 69 73 20 6a 75 rees, this is ju
150e7 73 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 st the root page
150e8 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 62 of the b-tree b
150e9 65 69 6e 67 20 72 65 61 64 20 6f 72 0a 20 20 2a eing read or. *
150ea 2a 20 77 72 69 74 74 65 6e 2e 20 46 6f 72 20 69 * written. For i
150eb 6e 64 65 78 20 62 2d 74 72 65 65 73 2c 20 69 74 ndex b-trees, it
150ec 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 is the root pag
150ed 65 20 6f 66 20 74 68 65 20 61 73 73 6f 63 69 61 e of the associa
150ee 74 65 64 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 ted. ** table.
150ef 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e 64 65 */. if( isInde
150f0 78 20 29 7b 0a 20 20 20 20 48 61 73 68 45 6c 65 x ){. HashEle
150f1 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d m *p;. for(p=
150f2 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 sqliteHashFirst(
150f3 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 &pSchema->idxHas
150f4 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 h); p; p=sqliteH
150f5 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 ashNext(p)){.
150f6 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d Index *pIdx =
150f7 20 28 49 6e 64 65 78 20 2a 29 73 71 6c 69 74 65 (Index *)sqlite
150f8 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 HashData(p);.
150f9 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 if( pIdx->tnu
150fa 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f 74 20 29 7b m==(int)iRoot ){
150fb 0a 20 20 20 20 20 20 20 20 69 54 61 62 20 3d 20 . iTab =
150fc 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 74 6e pIdx->pTable->tn
150fd 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 um;. }.
150fe 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 }. }else{. i
150ff 54 61 62 20 3d 20 69 52 6f 6f 74 3b 0a 20 20 7d Tab = iRoot;. }
15100 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f .. /* Search fo
15101 72 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6c r the required l
15102 6f 63 6b 2e 20 45 69 74 68 65 72 20 61 20 77 72 ock. Either a wr
15103 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f 6f 74 ite-lock on root
15104 2d 70 61 67 65 20 69 54 61 62 2c 20 61 20 0a 20 -page iTab, a .
15105 20 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f ** write-lock o
15106 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 n the schema tab
15107 6c 65 2c 20 6f 72 20 28 69 66 20 74 68 65 20 63 le, or (if the c
15108 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69 6e 67 lient is reading
15109 29 20 61 0a 20 20 2a 2a 20 72 65 61 64 2d 6c 6f ) a. ** read-lo
1510a 63 6b 20 6f 6e 20 69 54 61 62 20 77 69 6c 6c 20 ck on iTab will
1510b 73 75 66 66 69 63 65 2e 20 52 65 74 75 72 6e 20 suffice. Return
1510c 31 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 73 1 if any of thes
1510d 65 20 61 72 65 20 66 6f 75 6e 64 2e 20 20 2a 2f e are found. */
1510e 0a 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 . for(pLock=pBt
1510f 72 65 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63 6b 3b ree->pBt->pLock;
15110 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c pLock; pLock=pL
15111 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 ock->pNext){.
15112 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 if( pLock->pBtr
15113 65 65 3d 3d 70 42 74 72 65 65 20 0a 20 20 20 20 ee==pBtree .
15114 20 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54 61 62 && (pLock->iTab
15115 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70 4c 6f le==iTab || (pLo
15116 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 ck->eLock==WRITE
15117 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e _LOCK && pLock->
15118 69 54 61 62 6c 65 3d 3d 31 29 29 0a 20 20 20 20 iTable==1)).
15119 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b && pLock->eLock
1511a 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 0a 20 20 20 >=eLockType .
1511b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
1511c 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 1;. }. }..
1511d 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 66 69 /* Failed to fi
1511e 6e 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 nd the required
1511f 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74 75 72 lock. */. retur
15120 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a n 0;.}.#endif /*
15121 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f SQLITE_DEBUG */
15122 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
15123 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20 54 68 DEBUG./*.**** Th
15124 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 is function may
15125 62 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20 be used as part
15126 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 74 of assert() stat
15127 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2a 2a ements only. ***
15128 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 *.**.** Return t
15129 72 75 65 20 69 66 20 69 74 20 77 6f 75 6c 64 20 rue if it would
1512a 62 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 be illegal for p
1512b 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 Btree to write i
1512c 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 nto the.** table
1512d 20 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74 65 64 or index rooted
1512e 20 61 74 20 69 52 6f 6f 74 20 62 65 63 61 75 73 at iRoot becaus
1512f 65 20 6f 74 68 65 72 20 73 68 61 72 65 64 20 63 e other shared c
15130 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a 2a onnections are.*
15131 2a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 * simultaneously
15132 20 72 65 61 64 69 6e 67 20 74 68 61 74 20 73 61 reading that sa
15133 6d 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 me table or inde
15134 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 x..**.** It is i
15135 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74 72 65 llegal for pBtre
15136 65 20 74 6f 20 77 72 69 74 65 20 69 66 20 73 6f e to write if so
15137 6d 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f me other Btree o
15138 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 73 68 bject that.** sh
15139 61 72 65 73 20 74 68 65 20 73 61 6d 65 20 42 74 ares the same Bt
1513a 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 69 73 Shared object is
1513b 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69 currently readi
1513c 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a ng or writing.**
1513d 20 74 68 65 20 69 52 6f 6f 74 20 74 61 62 6c 65 the iRoot table
1513e 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20 74 68 . Except, if th
1513f 65 20 6f 74 68 65 72 20 42 74 72 65 65 20 6f 62 e other Btree ob
15140 6a 65 63 74 20 68 61 73 20 74 68 65 0a 2a 2a 20 ject has the.**
15141 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 read-uncommitted
15142 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65 6e 20 flag set, then
15143 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 it is OK for the
15144 20 6f 74 68 65 72 20 6f 62 6a 65 63 74 20 74 6f other object to
15145 0a 2a 2a 20 68 61 76 65 20 61 20 72 65 61 64 20 .** have a read
15146 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 46 6f cursor..**.** Fo
15147 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 66 6f 72 r example, befor
15148 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 79 e writing to any
15149 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 61 62 part of the tab
1514a 6c 65 20 6f 72 20 69 6e 64 65 78 0a 2a 2a 20 72 le or index.** r
1514b 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 69 52 ooted at page iR
1514c 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f 75 6c 64 20 oot, one should
1514d 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 61 call:.**.** a
1514e 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43 ssert( !hasReadC
1514f 6f 6e 66 6c 69 63 74 73 28 70 42 74 72 65 65 2c onflicts(pBtree,
15150 20 69 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a 73 74 iRoot) );.*/.st
15151 61 74 69 63 20 69 6e 74 20 68 61 73 52 65 61 64 atic int hasRead
15152 43 6f 6e 66 6c 69 63 74 73 28 42 74 72 65 65 20 Conflicts(Btree
15153 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20 69 52 *pBtree, Pgno iR
15154 6f 6f 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 oot){. BtCursor
15155 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 *p;. for(p=pBt
15156 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f ree->pBt->pCurso
15157 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 r; p; p=p->pNext
15158 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 ){. if( p->pg
15159 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20 0a 20 noRoot==iRoot .
1515a 20 20 20 20 26 26 20 70 2d 3e 70 42 74 72 65 65 && p->pBtree
1515b 21 3d 70 42 74 72 65 65 0a 20 20 20 20 20 26 26 !=pBtree. &&
1515c 20 30 3d 3d 28 70 2d 3e 70 42 74 72 65 65 2d 3e 0==(p->pBtree->
1515d 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 db->flags & SQLI
1515e 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 TE_ReadUncommitt
1515f 65 64 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 ed). ){.
15160 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
15161 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
15162 0a 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 .}.#endif /*
15163 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
15164 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 BUG */../*.** Qu
15165 65 72 79 20 74 6f 20 73 65 65 20 69 66 20 42 74 ery to see if Bt
15166 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d 61 79 ree handle p may
15167 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f obtain a lock o
15168 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a 2a 2a f type eLock .**
15169 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 (READ_LOCK or W
1516a 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20 74 68 RITE_LOCK) on th
1516b 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f e table with roo
1516c 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52 65 74 t-page iTab. Ret
1516d 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b urn.** SQLITE_OK
1516e 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d 61 79 if the lock may
1516f 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28 62 79 be obtained (by
15170 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65 74 53 calling.** setS
15171 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
15172 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c 49 54 ock()), or SQLIT
15173 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f 74 2e E_LOCKED if not.
15174 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 71 .*/.static int q
15175 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 uerySharedCacheT
15176 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65 20 2a ableLock(Btree *
15177 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20 75 38 p, Pgno iTab, u8
15178 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 eLock){. BtSha
15179 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
1517a 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 t;. BtLock *pIt
1517b 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 er;.. assert( s
1517c 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 qlite3BtreeHolds
1517d 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 Mutex(p) );. as
1517e 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 sert( eLock==REA
1517f 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d D_LOCK || eLock=
15180 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 =WRITE_LOCK );.
15181 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d assert( p->db!=
15182 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 0 );. assert( !
15183 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 (p->db->flags&SQ
15184 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 LITE_ReadUncommi
15185 74 74 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d 57 52 tted)||eLock==WR
15186 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62 3d 3d ITE_LOCK||iTab==
15187 31 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 1 );. . /* If
15188 72 65 71 75 65 73 74 69 6e 67 20 61 20 77 72 69 requesting a wri
15189 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68 te-lock, then th
1518a 65 20 42 74 72 65 65 20 6d 75 73 74 20 68 61 76 e Btree must hav
1518b 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 0a e an open write.
1518c 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e ** transaction
1518d 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 41 on this file. A
1518e 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79 2c 20 66 nd, obviously, f
1518f 6f 72 20 74 68 69 73 20 74 6f 20 62 65 20 73 6f or this to be so
15190 20 74 68 65 72 65 20 0a 20 20 2a 2a 20 6d 75 73 there . ** mus
15191 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 t be an open wri
15192 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te transaction o
15193 6e 20 74 68 65 20 66 69 6c 65 20 69 74 73 65 6c n the file itsel
15194 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 f.. */. assert
15195 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f ( eLock==READ_LO
15196 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 2d 3e 70 CK || (p==pBt->p
15197 57 72 69 74 65 72 20 26 26 20 70 2d 3e 69 6e 54 Writer && p->inT
15198 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 rans==TRANS_WRIT
15199 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 E) );. assert(
1519a 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
1519b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 || pBt->inTrans
1519c 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
1519d 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 ITE );. . /* T
1519e 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 his routine is a
1519f 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 68 no-op if the sh
151a0 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e 6f ared-cache is no
151a1 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 69 t enabled */. i
151a2 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65 20 f( !p->sharable
151a3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 ){. return SQ
151a4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 LITE_OK;. }..
151a5 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 /* If some other
151a6 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68 connection is h
151a7 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 olding an exclus
151a8 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20 20 ive lock, the.
151a9 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 ** requested loc
151aa 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62 74 k may not be obt
151ab 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 ained.. */. if
151ac 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 21 3d ( pBt->pWriter!=
151ad 70 20 26 26 20 70 42 74 2d 3e 69 73 45 78 63 6c p && pBt->isExcl
151ae 75 73 69 76 65 20 29 7b 0a 20 20 20 20 73 71 6c usive ){. sql
151af 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c ite3ConnectionBl
151b0 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 ocked(p->db, pBt
151b1 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29 3b 0a ->pWriter->db);.
151b2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
151b3 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 E_LOCKED_SHAREDC
151b4 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 ACHE;. }.. for
151b5 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 (pIter=pBt->pLoc
151b6 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d k; pIter; pIter=
151b7 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 pIter->pNext){.
151b8 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64 69 74 /* The condit
151b9 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c 6f 63 ion (pIter->eLoc
151ba 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74 68 65 k!=eLock) in the
151bb 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28 2e 2e following if(..
151bc 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 .) . ** state
151bd 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70 6c 69 ment is a simpli
151be 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20 20 20 fication of:.
151bf 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28 65 4c **. ** (eL
151c0 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 ock==WRITE_LOCK
151c1 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 3d || pIter->eLock=
151c2 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20 20 20 =WRITE_LOCK).
151c3 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 **. ** since
151c4 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 66 we know that if
151c5 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f eLock==WRITE_LO
151c6 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74 68 65 CK, then no othe
151c7 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20 20 r connection.
151c8 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61 20 57 ** may hold a W
151c9 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61 6e 79 RITE_LOCK on any
151ca 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73 20 66 table in this f
151cb 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65 72 65 ile (since there
151cc 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e 6c 79 can. ** only
151cd 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77 72 69 be a single wri
151ce 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ter).. */.
151cf 20 61 73 73 65 72 74 28 20 70 49 74 65 72 2d 3e assert( pIter->
151d0 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b eLock==READ_LOCK
151d1 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b || pIter->eLock
151d2 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a ==WRITE_LOCK );.
151d3 20 20 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 assert( eLoc
151d4 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 k==READ_LOCK ||
151d5 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 pIter->pBtree==p
151d6 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b || pIter->eLock
151d7 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 ==READ_LOCK);.
151d8 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 if( pIter->pBt
151d9 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72 2d ree!=p && pIter-
151da 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 >iTable==iTab &&
151db 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 pIter->eLock!=e
151dc 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 Lock ){. sq
151dd 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 lite3ConnectionB
151de 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 49 locked(p->db, pI
151df 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29 ter->pBtree->db)
151e0 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c 6f 63 ;. if( eLoc
151e1 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 7b k==WRITE_LOCK ){
151e2 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
151e3 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72 p==pBt->pWriter
151e4 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d );. pBt-
151e5 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 31 3b 0a >isPending = 1;.
151e6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 }. re
151e7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b turn SQLITE_LOCK
151e8 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a ED_SHAREDCACHE;.
151e9 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
151ea 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
151eb 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 #endif /* !SQLIT
151ec 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
151ed 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 CHE */..#ifndef
151ee 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
151ef 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 ED_CACHE./*.** A
151f0 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 dd a lock on the
151f1 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
151f2 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20 -page iTable to
151f3 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
151f4 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65 used.** by Btre
151f5 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61 e handle p. Para
151f6 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74 meter eLock must
151f7 20 62 65 20 65 69 74 68 65 72 20 52 45 41 44 5f be either READ_
151f8 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54 LOCK or .** WRIT
151f9 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 E_LOCK..**.** Th
151fa 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 is function assu
151fb 6d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e mes the followin
151fc 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20 54 g:.**.** (a) T
151fd 68 65 20 73 70 65 63 69 66 69 65 64 20 42 74 72 he specified Btr
151fe 65 65 20 6f 62 6a 65 63 74 20 70 20 69 73 20 63 ee object p is c
151ff 6f 6e 6e 65 63 74 65 64 20 74 6f 20 61 20 73 68 onnected to a sh
15200 61 72 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 arable.**
15201 64 61 74 61 62 61 73 65 20 28 6f 6e 65 20 77 69 database (one wi
15202 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 2e th the BtShared.
15203 73 68 61 72 61 62 6c 65 20 66 6c 61 67 20 73 65 sharable flag se
15204 74 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 t), and.**.**
15205 28 62 29 20 4e 6f 20 6f 74 68 65 72 20 42 74 72 (b) No other Btr
15206 65 65 20 6f 62 6a 65 63 74 73 20 68 6f 6c 64 20 ee objects hold
15207 61 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 a lock that conf
15208 6c 69 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77 licts.** w
15209 69 74 68 20 74 68 65 20 72 65 71 75 65 73 74 65 ith the requeste
1520a 64 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65 d lock (i.e. que
1520b 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
1520c 6c 65 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20 leLock() has.**
1520d 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 62 65 already be
1520e 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65 en called and re
1520f 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b turned SQLITE_OK
15210 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f )..**.** SQLITE_
15211 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 OK is returned i
15212 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64 f the lock is ad
15213 64 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 ded successfully
15214 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a . SQLITE_NOMEM .
15215 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 ** is returned i
15216 66 20 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d f a malloc attem
15217 70 74 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 pt fails..*/.sta
15218 74 69 63 20 69 6e 74 20 73 65 74 53 68 61 72 65 tic int setShare
15219 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 dCacheTableLock(
1521a 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 Btree *p, Pgno i
1521b 54 61 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29 Table, u8 eLock)
1521c 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
1521d 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 t = p->pBt;. Bt
1521e 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b Lock *pLock = 0;
1521f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 . BtLock *pIter
15220 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
15221 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
15222 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
15223 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f rt( eLock==READ_
15224 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 LOCK || eLock==W
15225 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 RITE_LOCK );. a
15226 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 ssert( p->db!=0
15227 29 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 );.. /* A conne
15228 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 ction with the r
15229 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 ead-uncommitted
1522a 66 6c 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 flag set will ne
1522b 76 65 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 ver try to. **
1522c 6f 62 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f obtain a read-lo
1522d 63 6b 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 ck using this fu
1522e 6e 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79 nction. The only
1522f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 read-lock obtai
15230 6e 65 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f ned. ** by a co
15231 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64 nnection in read
15232 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64 -uncommitted mod
15233 65 20 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69 e is on the sqli
15234 74 65 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20 te_master . **
15235 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20 table, and that
15236 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 lock is obtained
15237 20 69 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72 in BtreeBeginTr
15238 61 6e 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73 ans(). */. ass
15239 65 72 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e ert( 0==(p->db->
1523a 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 flags&SQLITE_Rea
1523b 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c dUncommitted) ||
1523c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f eLock==WRITE_LO
1523d 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 CK );.. /* This
1523e 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
1523f 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 only be called
15240 6f 6e 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d on a sharable b-
15241 74 72 65 65 20 61 66 74 65 72 20 69 74 20 0a 20 tree after it .
15242 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74 ** has been det
15243 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 ermined that no
15244 6f 74 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c other b-tree hol
15245 64 73 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67 ds a conflicting
15246 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 lock. */. ass
15247 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 ert( p->sharable
15248 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 );. assert( SQ
15249 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 LITE_OK==querySh
1524a 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f aredCacheTableLo
1524b 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c ck(p, iTable, eL
1524c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 ock) );.. /* Fi
1524d 72 73 74 20 73 65 61 72 63 68 20 74 68 65 20 6c rst search the l
1524e 69 73 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74 ist for an exist
1524f 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 ing lock on this
15250 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 table. */. for
15251 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 (pIter=pBt->pLoc
15252 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d k; pIter; pIter=
15253 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 pIter->pNext){.
15254 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54 if( pIter->iT
15255 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 able==iTable &&
15256 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 pIter->pBtree==p
15257 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 ){. pLock
15258 3d 20 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62 = pIter;. b
15259 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1525a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f . /* If the abo
1525b 76 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f ve search did no
1525c 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20 t find a BtLock
1525d 73 74 72 75 63 74 20 61 73 73 6f 63 69 61 74 69 struct associati
1525e 6e 67 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20 ng Btree p. **
1525f 77 69 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c with table iTabl
15260 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 e, allocate one
15261 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f and link it into
15262 20 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a the list.. */.
15263 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a if( !pLock ){.
15264 20 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c pLock = (BtL
15265 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c ock *)sqlite3Mal
15266 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 locZero(sizeof(B
15267 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28 tLock));. if(
15268 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 !pLock ){.
15269 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
1526a 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 OMEM;. }.
1526b 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 pLock->iTable =
1526c 69 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 iTable;. pLoc
1526d 6b 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 k->pBtree = p;.
1526e 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 pLock->pNext
1526f 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 = pBt->pLock;.
15270 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 pBt->pLock = p
15271 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 Lock;. }.. /*
15272 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65 Set the BtLock.e
15273 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f Lock variable to
15274 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 the maximum of
15275 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b the current lock
15276 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 . ** and the re
15277 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68 quested lock. Th
15278 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72 is means if a wr
15279 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72 ite-lock was alr
1527a 65 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61 eady held. ** a
1527b 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72 nd a read-lock r
1527c 65 71 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e equested, we don
1527d 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64 't incorrectly d
1527e 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63 owngrade the loc
1527f 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 k.. */. assert
15280 28 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 ( WRITE_LOCK>REA
15281 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 D_LOCK );. if(
15282 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f eLock>pLock->eLo
15283 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d ck ){. pLock-
15284 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a >eLock = eLock;.
15285 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 }.. return SQ
15286 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
15287 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 f /* !SQLITE_OMI
15288 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a T_SHARED_CACHE *
15289 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1528a 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 E_OMIT_SHARED_CA
1528b 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 CHE./*.** Releas
1528c 65 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 e all the table
1528d 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 locks (locks obt
1528e 61 69 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 ained via calls
1528f 74 6f 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61 to.** the setSha
15290 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 redCacheTableLoc
15291 6b 28 29 20 70 72 6f 63 65 64 75 72 65 29 20 68 k() procedure) h
15292 65 6c 64 20 62 79 20 42 74 72 65 65 20 6f 62 6a eld by Btree obj
15293 65 63 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ect p..**.** Thi
15294 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d s function assum
15295 65 73 20 74 68 61 74 20 42 74 72 65 65 20 70 20 es that Btree p
15296 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64 has an open read
15297 20 6f 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72 or write .** tr
15298 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74 ansaction. If it
15299 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20 does not, then
1529a 74 68 65 20 42 74 53 68 61 72 65 64 2e 69 73 50 the BtShared.isP
1529b 65 6e 64 69 6e 67 20 76 61 72 69 61 62 6c 65 0a ending variable.
1529c 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 72 ** may be incorr
1529d 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a 2a ectly cleared..*
1529e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c /.static void cl
1529f 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 earAllSharedCach
152a0 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 eTableLocks(Btre
152a1 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
152a2 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
152a3 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74 . BtLock **ppIt
152a4 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b er = &pBt->pLock
152a5 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
152a6 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 ite3BtreeHoldsMu
152a7 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 tex(p) );. asse
152a8 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 rt( p->sharable
152a9 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b || 0==*ppIter );
152aa 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e . assert( p->in
152ab 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 68 Trans>0 );.. wh
152ac 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b 0a ile( *ppIter ){.
152ad 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 BtLock *pLoc
152ae 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 20 k = *ppIter;.
152af 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 assert( pBt->is
152b0 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 Exclusive==0 ||
152b1 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c pBt->pWriter==pL
152b2 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20 ock->pBtree );.
152b3 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
152b4 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e ->pBtree->inTran
152b5 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 s>=pLock->eLock
152b6 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b );. if( pLock
152b7 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20 ->pBtree==p ){.
152b8 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70 *ppIter = p
152b9 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 Lock->pNext;.
152ba 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b assert( pLock
152bb 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70 ->iTable!=1 || p
152bc 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29 Lock==&p->lock )
152bd 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 ;. if( pLoc
152be 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a k->iTable!=1 ){.
152bf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f sqlite3_
152c0 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 free(pLock);.
152c1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
152c2 20 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26 ppIter = &
152c3 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 pLock->pNext;.
152c4 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 }. }.. asser
152c5 74 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e t( pBt->isPendin
152c6 67 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72 g==0 || pBt->pWr
152c7 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42 iter );. if( pB
152c8 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b t->pWriter==p ){
152c9 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 . pBt->pWrite
152ca 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e r = 0;. pBt->
152cb 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b isExclusive = 0;
152cc 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 . pBt->isPend
152cd 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 ing = 0;. }else
152ce 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 if( pBt->nTrans
152cf 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20 action==2 ){.
152d0 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f /* This functio
152d1 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e n is called when
152d2 20 42 74 72 65 65 20 70 20 69 73 20 63 6f 6e 63 Btree p is conc
152d3 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20 20 20 luding its .
152d4 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 ** transaction.
152d5 49 66 20 74 68 65 72 65 20 63 75 72 72 65 6e 74 If there current
152d6 6c 79 20 65 78 69 73 74 73 20 61 20 77 72 69 74 ly exists a writ
152d7 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e 6f 74 er, and p is not
152d8 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 72 69 . ** that wri
152d9 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 ter, then the nu
152da 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 68 65 mber of locks he
152db 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e ld by connection
152dc 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 s other. ** t
152dd 68 61 6e 20 74 68 65 20 77 72 69 74 65 72 20 6d han the writer m
152de 75 73 74 20 62 65 20 61 62 6f 75 74 20 74 6f 20 ust be about to
152df 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20 49 6e drop to zero. In
152e0 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20 2a this case. *
152e1 2a 20 73 65 74 20 74 68 65 20 69 73 50 65 6e 64 * set the isPend
152e2 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e 0a 20 ing flag to 0..
152e3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 **. ** If
152e4 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63 75 72 there is not cur
152e5 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65 72 2c rently a writer,
152e6 20 74 68 65 6e 20 42 74 53 68 61 72 65 64 2e 69 then BtShared.i
152e7 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a 20 20 sPending must.
152e8 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61 6c 72 ** be zero alr
152e9 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20 6e 65 eady. So this ne
152ea 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72 6d 6c xt line is harml
152eb 65 73 73 20 69 6e 20 74 68 61 74 20 63 61 73 65 ess in that case
152ec 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 42 74 .. */. pBt
152ed 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b ->isPending = 0;
152ee 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 . }.}../*.** Th
152ef 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68 61 6e is function chan
152f0 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d 6c 6f ges all write-lo
152f1 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74 72 65 cks held by Btre
152f2 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d 6c 6f e p into read-lo
152f3 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 cks..*/.static v
152f4 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c oid downgradeAll
152f5 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 SharedCacheTable
152f6 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70 29 7b Locks(Btree *p){
152f7 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
152f8 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 66 28 = p->pBt;. if(
152f9 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 pBt->pWriter==p
152fa 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a ){. BtLock *
152fb 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e pLock;. pBt->
152fc 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 pWriter = 0;.
152fd 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 pBt->isExclusiv
152fe 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e e = 0;. pBt->
152ff 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 isPending = 0;.
15300 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70 42 74 for(pLock=pBt
15301 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 ->pLock; pLock;
15302 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 pLock=pLock->pNe
15303 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 xt){. asser
15304 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d t( pLock->eLock=
15305 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c =READ_LOCK || pL
15306 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 ock->pBtree==p )
15307 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 65 ;. pLock->e
15308 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b Lock = READ_LOCK
15309 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 ;. }. }.}..#
1530a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
1530b 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
1530c 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 E */..static voi
1530d 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 d releasePage(Me
1530e 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 mPage *pPage);
1530f 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 /* Forward refer
15310 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 2a 2a ence */../*.****
15311 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 * This routine i
15312 73 20 75 73 65 64 20 69 6e 73 69 64 65 20 6f 66 s used inside of
15313 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79 20 2a assert() only *
15314 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69 66 79 ***.**.** Verify
15315 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 that the cursor
15316 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78 holds the mutex
15317 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72 65 64 on its BtShared
15318 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 .*/.#ifdef SQLIT
15319 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 E_DEBUG.static i
1531a 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 nt cursorHoldsMu
1531b 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a 70 29 tex(BtCursor *p)
1531c 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 {. return sqlit
1531d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d e3_mutex_held(p-
1531e 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a >pBt->mutex);.}.
1531f 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 #endif...#ifndef
15320 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 SQLITE_OMIT_INC
15321 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 RBLOB./*.** Inva
15322 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 lidate the overf
15323 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 low page-list ca
15324 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 che for cursor p
15325 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a Cur, if any..*/.
15326 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
15327 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 lidateOverflowCa
15328 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 che(BtCursor *pC
15329 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 ur){. assert( c
1532a 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
1532b 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 pCur) );. sqlit
1532c 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 61 4f e3_free(pCur->aO
1532d 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 verflow);. pCur
1532e 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b ->aOverflow = 0;
1532f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 .}../*.** Invali
15330 64 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f date the overflo
15331 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 w page-list cach
15332 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 e for all cursor
15333 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 s opened.** on t
15334 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 20 he shared btree
15335 73 74 72 75 63 74 75 72 65 20 70 42 74 2e 0a 2a structure pBt..*
15336 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e /.static void in
15337 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 validateAllOverf
15338 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61 72 65 lowCache(BtShare
15339 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43 75 72 d *pBt){. BtCur
1533a 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 sor *p;. assert
1533b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1533c 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1533d 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d );. for(p=pBt-
1533e 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 >pCursor; p; p=p
1533f 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e ->pNext){. in
15340 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
15341 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a Cache(p);. }.}.
15342 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
15343 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 tion is called b
15344 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e 67 20 efore modifying
15345 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 the contents of
15346 61 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 69 6e a table.** to in
15347 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 validate any inc
15348 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 74 68 rblob cursors th
15349 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 at are open on t
1534a 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 he.** row or one
1534b 20 6f 66 20 74 68 65 20 72 6f 77 73 20 62 65 69 of the rows bei
1534c 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a ng modified..**.
1534d 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 69 ** If argument i
1534e 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 20 74 sClearTable is t
1534f 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 65 6e rue, then the en
15350 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 tire contents of
15351 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 the.** table is
15352 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64 65 6c about to be del
15353 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 eted. In this ca
15354 73 65 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6c se invalidate al
15355 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 l incrblob.** cu
15356 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e rsors open on an
15357 79 20 72 6f 77 20 77 69 74 68 69 6e 20 74 68 65 y row within the
15358 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 table with root
15359 2d 70 61 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a -page pgnoRoot..
1535a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c **.** Otherwise,
1535b 20 69 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 if argument isC
1535c 6c 65 61 72 54 61 62 6c 65 20 69 73 20 66 61 6c learTable is fal
1535d 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 se, then the row
1535e 20 77 69 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 with.** rowid i
1535f 52 6f 77 20 69 73 20 62 65 69 6e 67 20 72 65 70 Row is being rep
15360 6c 61 63 65 64 20 6f 72 20 64 65 6c 65 74 65 64 laced or deleted
15361 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 69 . In this case i
15362 6e 76 61 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c nvalidate.** onl
15363 79 20 74 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 y those incrblob
15364 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e cursors open on
15365 20 74 68 61 74 20 73 70 65 63 69 66 69 63 20 72 that specific r
15366 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ow..*/.static vo
15367 69 64 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 id invalidateInc
15368 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 rblobCursors(.
15369 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 Btree *pBtree,
1536a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 /* The d
1536b 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 atabase file to
1536c 63 68 65 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 check */. i64 i
1536d 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 Row,
1536e 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 /* The rowid
1536f 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 63 68 that might be ch
15370 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 anging */. int
15371 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20 20 20 isClearTable
15372 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 /* True if a
15373 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65 69 6e ll rows are bein
15374 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a g deleted */.){.
15375 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
15376 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d BtShared *pBt =
15377 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 pBtree->pBt;.
15378 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
15379 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
1537a 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 Btree) );. for(
1537b 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 p=pBt->pCursor;
1537c 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a p; p=p->pNext){.
1537d 20 20 20 20 69 66 28 20 70 2d 3e 69 73 49 6e 63 if( p->isInc
1537e 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 28 rblobHandle && (
1537f 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 isClearTable ||
15380 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 p->info.nKey==iR
15381 6f 77 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e ow) ){. p->
15382 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f eState = CURSOR_
15383 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 INVALID;. }.
15384 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a }.}..#else. /*
15385 20 53 74 75 62 20 66 75 6e 63 74 69 6f 6e 73 20 Stub functions
15386 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20 69 73 when INCRBLOB is
15387 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20 23 64 omitted */. #d
15388 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 efine invalidate
15389 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 OverflowCache(x)
1538a 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c . #define inval
1538b 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 idateAllOverflow
1538c 43 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 Cache(x). #defi
1538d 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 ne invalidateInc
1538e 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 rblobCursors(x,y
1538f 2c 7a 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ,z).#endif /* SQ
15390 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c LITE_OMIT_INCRBL
15391 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 OB */../*.** Set
15392 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74 68 65 bit pgno of the
15393 20 42 74 53 68 61 72 65 64 2e 70 48 61 73 43 6f BtShared.pHasCo
15394 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20 54 68 ntent bitvec. Th
15395 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a 2a 2a is is called .**
15396 20 77 68 65 6e 20 61 20 70 61 67 65 20 74 68 61 when a page tha
15397 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e t previously con
15398 74 61 69 6e 65 64 20 64 61 74 61 20 62 65 63 6f tained data beco
15399 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73 74 20 mes a free-list
1539a 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e 0a 2a leaf .** page..*
1539b 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61 72 65 *.** The BtShare
1539c 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 d.pHasContent bi
1539d 74 76 65 63 20 65 78 69 73 74 73 20 74 6f 20 77 tvec exists to w
1539e 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20 6f 62 ork around an ob
1539f 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63 61 75 scure.** bug cau
153a0 73 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 72 sed by the inter
153a1 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20 75 73 action of two us
153a2 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69 7a 61 eful IO optimiza
153a3 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64 69 6e tions surroundin
153a4 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 6c g.** free-list l
153a5 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a 2a 2a eaf pages:.**.**
153a6 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c 20 64 1) When all d
153a7 61 74 61 20 69 73 20 64 65 6c 65 74 65 64 20 66 ata is deleted f
153a8 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64 20 74 rom a page and t
153a9 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65 73 0a he page becomes.
153aa 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65 2d 6c ** a free-l
153ab 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c 20 74 ist leaf page, t
153ac 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 77 he page is not w
153ad 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 ritten to the da
153ae 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 28 tabase.** (
153af 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 as free-list lea
153b0 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69 6e 20 f pages contain
153b1 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20 64 61 no meaningful da
153b2 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73 0a 2a ta). Sometimes.*
153b3 2a 20 20 20 20 20 20 73 75 63 68 20 61 20 70 61 * such a pa
153b4 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e 20 6a ge is not even j
153b5 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20 69 74 ournalled (as it
153b6 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d 6f 64 will not be mod
153b7 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 20 77 ified,.** w
153b8 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72 6e 61 hy bother journa
153b9 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a 0a 2a lling it?)..**.*
153ba 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20 66 72 * 2) When a fr
153bb 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 ee-list leaf pag
153bc 65 20 69 73 20 72 65 75 73 65 64 2c 20 69 74 73 e is reused, its
153bd 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 content is not
153be 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f read.** fro
153bf 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f m the database o
153c0 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 r written to the
153c1 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 77 journal file (w
153c2 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a 2a 20 hy should it.**
153c3 20 20 20 20 20 62 65 2c 20 69 66 20 69 74 20 69 be, if it i
153c4 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d 65 61 s not at all mea
153c5 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a 2a 2a ningful?)..**.**
153c6 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73 2c 20 By themselves,
153c7 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69 these optimizati
153c8 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20 61 6e ons work fine an
153c9 64 20 70 72 6f 76 69 64 65 20 61 20 68 61 6e 64 d provide a hand
153ca 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 y.** performance
153cb 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b 20 64 boost to bulk d
153cc 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72 74 20 elete or insert
153cd 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f 77 65 operations. Howe
153ce 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70 61 67 ver, if.** a pag
153cf 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 74 68 e is moved to th
153d0 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 e free-list and
153d1 74 68 65 6e 20 72 65 75 73 65 64 20 77 69 74 68 then reused with
153d2 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74 in the same.** t
153d3 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 70 72 ransaction, a pr
153d4 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70 2e 20 oblem comes up.
153d5 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e If the page is n
153d6 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 77 68 ot journalled wh
153d7 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f 76 65 en.** it is move
153d8 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 d to the free-li
153d9 73 74 20 61 6e 64 20 69 74 20 69 73 20 61 6c 73 st and it is als
153da 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 o not journalled
153db 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73 20 65 when it.** is e
153dc 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68 xtracted from th
153dd 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e 64 20 e free-list and
153de 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74 68 65 reused, then the
153df 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 0a 2a original data.*
153e0 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e 20 49 * may be lost. I
153e1 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 n the event of a
153e2 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20 6d 61 rollback, it ma
153e3 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c y not be possibl
153e4 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72 65 20 e.** to restore
153e5 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 the database to
153e6 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e its original con
153e7 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a figuration..**.*
153e8 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69 * The solution i
153e9 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 s the BtShared.p
153ea 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 HasContent bitve
153eb 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 70 61 c. Whenever a pa
153ec 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65 64 20 ge is .** moved
153ed 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72 65 65 to become a free
153ee 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c -list leaf page,
153ef 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
153f0 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73 65 74 ng bit is.** set
153f1 20 69 6e 20 74 68 65 20 62 69 74 76 65 63 2e 20 in the bitvec.
153f2 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61 66 20 Whenever a leaf
153f3 70 61 67 65 20 69 73 20 65 78 74 72 61 63 74 65 page is extracte
153f4 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65 65 2d d from the free-
153f5 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d 69 7a list,.** optimiz
153f6 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20 69 73 ation 2 above is
153f7 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68 65 20 omitted if the
153f8 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 corresponding bi
153f9 74 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 t is already.**
153fa 73 65 74 20 69 6e 20 42 74 53 68 61 72 65 64 2e set in BtShared.
153fb 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54 68 65 pHasContent. The
153fc 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
153fd 20 62 69 74 76 65 63 20 61 72 65 20 63 6c 65 61 bitvec are clea
153fe 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e red.** at the en
153ff 64 20 6f 66 20 65 76 65 72 79 20 74 72 61 6e 73 d of every trans
15400 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 action..*/.stati
15401 63 20 69 6e 74 20 62 74 72 65 65 53 65 74 48 61 c int btreeSetHa
15402 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 sContent(BtShare
15403 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e d *pBt, Pgno pgn
15404 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 o){. int rc = S
15405 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
15406 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e !pBt->pHasConten
15407 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 50 61 t ){. int nPa
15408 67 65 20 3d 20 31 30 30 3b 0a 20 20 20 20 73 71 ge = 100;. sq
15409 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f lite3PagerPageco
1540a 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c unt(pBt->pPager,
1540b 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 2f 2a &nPage);. /*
1540c 20 49 66 20 73 71 6c 69 74 65 33 50 61 67 65 72 If sqlite3Pager
1540d 50 61 67 65 63 6f 75 6e 74 28 29 20 66 61 69 6c Pagecount() fail
1540e 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20 68 61 s there is no ha
1540f 72 6d 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 rm because the.
15410 20 20 20 2a 2a 20 6e 50 61 67 65 20 76 61 72 69 ** nPage vari
15411 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 able is unchange
15412 64 20 66 72 6f 6d 20 69 74 73 20 64 65 66 61 75 d from its defau
15413 6c 74 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20 lt value of 100
15414 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48 61 73 */. pBt->pHas
15415 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69 74 65 Content = sqlite
15416 33 42 69 74 76 65 63 43 72 65 61 74 65 28 28 75 3BitvecCreate((u
15417 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 32)nPage);. i
15418 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e f( !pBt->pHasCon
15419 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 72 63 tent ){. rc
1541a 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b = SQLITE_NOMEM;
1541b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
1541c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1541d 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65 33 42 & pgno<=sqlite3B
1541e 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d 3e 70 itvecSize(pBt->p
1541f 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b 0a 20 HasContent) ){.
15420 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
15421 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e 70 48 itvecSet(pBt->pH
15422 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e 6f 29 asContent, pgno)
15423 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
15424 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 c;.}../*.** Quer
15425 79 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 y the BtShared.p
15426 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63 74 6f HasContent vecto
15427 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 r..**.** This fu
15428 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 nction is called
15429 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c 69 73 when a free-lis
1542a 74 20 6c 65 61 66 20 70 61 67 65 20 69 73 20 72 t leaf page is r
1542b 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 0a emoved from the.
1542c 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66 6f 72 ** free-list for
1542d 20 72 65 75 73 65 2e 20 49 74 20 72 65 74 75 72 reuse. It retur
1542e 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74 20 69 ns false if it i
1542f 73 20 73 61 66 65 20 74 6f 20 72 65 74 72 69 65 s safe to retrie
15430 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 66 ve the.** page f
15431 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 rom the pager la
15432 79 65 72 20 77 69 74 68 20 74 68 65 20 27 6e 6f yer with the 'no
15433 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67 20 73 -content' flag s
15434 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72 77 69 et. True otherwi
15435 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e se..*/.static in
15436 74 20 62 74 72 65 65 47 65 74 48 61 73 43 6f 6e t btreeGetHasCon
15437 74 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 tent(BtShared *p
15438 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a Bt, Pgno pgno){.
15439 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20 70 42 Bitvec *p = pB
1543a 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 3b 0a t->pHasContent;.
1543b 20 20 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 return (p && (
1543c 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69 74 76 pgno>sqlite3Bitv
1543d 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73 71 6c ecSize(p) || sql
1543e 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 ite3BitvecTest(p
1543f 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a 2f 2a , pgno)));.}../*
15440 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73 74 72 .** Clear (destr
15441 6f 79 29 20 74 68 65 20 42 74 53 68 61 72 65 64 oy) the BtShared
15442 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 .pHasContent bit
15443 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75 6c 64 vec. This should
15444 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 61 be.** invoked a
15445 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e t the conclusion
15446 20 6f 66 20 65 61 63 68 20 77 72 69 74 65 2d 74 of each write-t
15447 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 ransaction..*/.s
15448 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65 65 tatic void btree
15449 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e 74 28 ClearHasContent(
1544a 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a BtShared *pBt){.
1544b 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 sqlite3BitvecD
1544c 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48 61 73 estroy(pBt->pHas
1544d 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42 74 2d Content);. pBt-
1544e 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 30 >pHasContent = 0
1544f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 ;.}../*.** Save
15450 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 the current curs
15451 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 or position in t
15452 68 65 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 he variables BtC
15453 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a 20 61 ursor.nKey .** a
15454 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 nd BtCursor.pKey
15455 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73 20 73 . The cursor's s
15456 74 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 43 tate is set to C
15457 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 URSOR_REQUIRESEE
15458 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c K..**.** The cal
15459 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 ler must ensure
1545a 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 that the cursor
1545b 69 73 20 76 61 6c 69 64 20 28 68 61 73 20 65 53 is valid (has eS
1545c 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c tate==CURSOR_VAL
1545d 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 ID).** prior to
1545e 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 calling this rou
1545f 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 tine. .*/.stati
15460 63 20 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 c int saveCursor
15461 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f Position(BtCurso
15462 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 r *pCur){. int
15463 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 rc;.. assert( C
15464 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 URSOR_VALID==pCu
15465 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 r->eState );. a
15466 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e ssert( 0==pCur->
15467 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 pKey );. assert
15468 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
15469 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 ex(pCur) );.. r
1546a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1546b 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 KeySize(pCur, &p
1546c 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20 61 73 Cur->nKey);. as
1546d 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 sert( rc==SQLITE
1546e 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79 53 69 _OK ); /* KeySi
1546f 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c ze() cannot fail
15470 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 */.. /* If thi
15471 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79 20 74 s is an intKey t
15472 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 61 able, then the a
15473 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42 74 72 bove call to Btr
15474 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20 2a 2a eeKeySize(). **
15475 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e 74 65 stores the inte
15476 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75 72 2d ger key in pCur-
15477 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73 20 63 >nKey. In this c
15478 61 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 69 ase this value i
15479 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74 20 s. ** all that
1547a 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f 74 68 is required. Oth
1547b 65 72 77 69 73 65 2c 20 69 66 20 70 43 75 72 20 erwise, if pCur
1547c 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e 20 61 is not open on a
1547d 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20 74 61 n intKey. ** ta
1547e 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c 6f 63 ble, then malloc
1547f 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 73 space for and s
15480 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d 3e 6e tore the pCur->n
15481 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b 65 79 Key bytes of key
15482 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20 20 2a . ** data.. *
15483 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75 72 2d /. if( 0==pCur-
15484 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b >apPage[0]->intK
15485 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a ey ){. void *
15486 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 pKey = sqlite3Ma
15487 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 75 72 2d lloc( (int)pCur-
15488 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 >nKey );. if(
15489 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 pKey ){. r
1548a 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1548b 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 28 69 6e Key(pCur, 0, (in
1548c 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20 70 4b t)pCur->nKey, pK
1548d 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 ey);. if( r
1548e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1548f 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4b pCur->pK
15490 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20 20 20 ey = pKey;.
15491 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
15492 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4b 65 sqlite3_free(pKe
15493 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 y);. }.
15494 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 }else{. rc
15495 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
15496 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
15497 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50 61 67 rt( !pCur->apPag
15498 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 e[0]->intKey ||
15499 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 0a !pCur->pKey );..
1549a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1549b 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 _OK ){. int i
1549c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1549d 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 <=pCur->iPage; i
1549e 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 ++){. relea
1549f 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 sePage(pCur->apP
154a0 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 age[i]);. p
154a1 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d Cur->apPage[i] =
154a2 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 0;. }. pC
154a3 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a ur->iPage = -1;.
154a4 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
154a5 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 = CURSOR_REQUIR
154a6 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20 69 6e ESEEK;. }.. in
154a7 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 validateOverflow
154a8 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 72 Cache(pCur);. r
154a9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
154aa 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 ** Save the posi
154ab 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63 75 72 tions of all cur
154ac 73 6f 72 73 20 28 65 78 63 65 70 74 20 70 45 78 sors (except pEx
154ad 63 65 70 74 29 20 74 68 61 74 20 61 72 65 20 6f cept) that are o
154ae 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 61 pen on.** the ta
154af 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74 2d 70 ble with root-p
154b0 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c age iRoot. Usual
154b1 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c ly, this is call
154b2 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 ed just before c
154b3 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 ursor.** pExcept
154b4 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 is used to modi
154b5 66 79 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 fy the table (Bt
154b6 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 reeDelete() or B
154b7 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a treeInsert())..*
154b8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 /.static int sav
154b9 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 eAllCursors(BtSh
154ba 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
154bb 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 iRoot, BtCursor
154bc 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 *pExcept){. BtC
154bd 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 ursor *p;. asse
154be 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
154bf 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
154c0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
154c1 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 pExcept==0 || pE
154c2 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 xcept->pBt==pBt
154c3 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e );. for(p=pBt->
154c4 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d pCursor; p; p=p-
154c5 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 >pNext){. if(
154c6 20 70 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 p!=pExcept && (
154c7 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 0==iRoot || p->p
154c8 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 gnoRoot==iRoot)
154c9 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 && . p->e
154ca 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
154cb 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 LID ){. int
154cc 20 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 rc = saveCursor
154cd 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 Position(p);.
154ce 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b if( SQLITE_OK
154cf 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 !=rc ){.
154d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
154d1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 }. }. }. r
154d2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
154d3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 .}../*.** Clear
154d4 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 the current curs
154d5 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a or position..*/.
154d6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
154d7 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 oid sqlite3Btree
154d8 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 ClearCursor(BtCu
154d9 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 rsor *pCur){. a
154da 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
154db 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
154dc 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
154dd 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 pCur->pKey);. p
154de 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 Cur->pKey = 0;.
154df 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
154e0 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
154e1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 }../*.** In this
154e2 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74 72 65 version of Btre
154e3 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20 69 73 eMoveto, pKey is
154e4 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65 78 20 a packed index
154e5 72 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68 20 61 record.** such a
154e6 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 s is generated b
154e7 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 y the OP_MakeRec
154e8 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55 6e 70 ord opcode. Unp
154e9 61 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 ack the.** recor
154ea 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c 6c 20 d and then call
154eb 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
154ec 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68 65 20 ked() to do the
154ed 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 work..*/.static
154ee 69 6e 74 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 int btreeMoveto(
154ef 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
154f0 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 r, /* Cursor
154f1 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62 74 72 open on the btr
154f2 65 65 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 ee to be searche
154f3 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 d */. const voi
154f4 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 50 61 d *pKey, /* Pa
154f5 63 6b 65 64 20 6b 65 79 20 69 66 20 74 68 65 20 cked key if the
154f6 62 74 72 65 65 20 69 73 20 61 6e 20 69 6e 64 65 btree is an inde
154f7 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65 79 2c x */. i64 nKey,
154f8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
154f9 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 61 teger key for ta
154fa 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66 20 70 bles. Size of p
154fb 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 65 73 20 Key for indices
154fc 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c 20 20 */. int bias,
154fd 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 61 73 /* Bias
154fe 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20 68 search to the h
154ff 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 igh end */. int
15500 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20 20 *pRes
15501 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 /* Write search
15502 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f results here */
15503 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 .){. int rc;
15504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15505 20 2f 2a 20 53 74 61 74 75 73 20 63 6f 64 65 20 /* Status code
15506 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 */. UnpackedRec
15507 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20 20 20 ord *pIdxKey;
15508 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 /* Unpacked inde
15509 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72 20 x key */. char
1550a 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 20 20 20 aSpace[150];
1550b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 70 /* Temp sp
1550c 61 63 65 20 66 6f 72 20 70 49 64 78 4b 65 79 20 ace for pIdxKey
1550d 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 61 6c - to avoid a mal
1550e 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4b loc */.. if( pK
1550f 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ey ){. assert
15510 28 20 6e 4b 65 79 3d 3d 28 69 36 34 29 28 69 6e ( nKey==(i64)(in
15511 74 29 6e 4b 65 79 20 29 3b 0a 20 20 20 20 70 49 t)nKey );. pI
15512 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 dxKey = sqlite3V
15513 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 dbeRecordUnpack(
15514 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 pCur->pKeyInfo,
15515 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65 79 2c (int)nKey, pKey,
15516 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
15517 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15518 20 20 20 20 20 20 20 61 53 70 61 63 65 2c 20 73 aSpace, s
15519 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29 3b 0a izeof(aSpace));.
1551a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d if( pIdxKey=
1551b 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 =0 ) return SQLI
1551c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 TE_NOMEM;. }els
1551d 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d e{. pIdxKey =
1551e 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 0;. }. rc = s
1551f 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 qlite3BtreeMovet
15520 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20 oUnpacked(pCur,
15521 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62 pIdxKey, nKey, b
15522 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66 ias, pRes);. if
15523 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 ( pKey ){. sq
15524 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 lite3VdbeDeleteU
15525 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 npackedRecord(pI
15526 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65 dxKey);. }. re
15527 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
15528 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 63 75 * Restore the cu
15529 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f 73 69 rsor to the posi
1552a 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e 20 28 tion it was in (
1552b 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f 20 61 or as close to a
1552c 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a 20 77 s possible).** w
1552d 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72 50 6f hen saveCursorPo
1552e 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63 61 6c sition() was cal
1552f 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 led. Note that t
15530 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74 65 73 his call deletes
15531 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64 20 70 the .** saved p
15532 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73 74 6f osition info sto
15533 72 65 64 20 62 79 20 73 61 76 65 43 75 72 73 6f red by saveCurso
15534 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73 6f 20 rPosition(), so
15535 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a 20 there can be.**
15536 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66 66 65 at most one effe
15537 63 74 69 76 65 20 72 65 73 74 6f 72 65 43 75 72 ctive restoreCur
15538 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 63 61 sorPosition() ca
15539 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20 0a 2a ll after each .*
1553a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f 73 69 * saveCursorPosi
1553b 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 tion()..*/.stati
1553c 63 20 69 6e 74 20 62 74 72 65 65 52 65 73 74 6f c int btreeResto
1553d 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e reCursorPosition
1553e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 (BtCursor *pCur)
1553f 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 {. int rc;. as
15540 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 sert( cursorHold
15541 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a sMutex(pCur) );.
15542 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e assert( pCur->
15543 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 eState>=CURSOR_R
15544 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 EQUIRESEEK );.
15545 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
15546 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 ==CURSOR_FAULT )
15547 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 {. return pCu
15548 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 7d r->skipNext;. }
15549 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 . pCur->eState
1554a 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
1554b 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 4d 6f ;. rc = btreeMo
1554c 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75 72 2d veto(pCur, pCur-
1554d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e 4b 65 >pKey, pCur->nKe
1554e 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73 6b 69 y, 0, &pCur->ski
1554f 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 pNext);. if( rc
15550 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
15551 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15552 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 pCur->pKey);.
15553 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b pCur->pKey = 0;
15554 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 . assert( pCu
15555 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
15556 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d R_VALID || pCur-
15557 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
15558 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d 0a 20 INVALID );. }.
15559 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
1555a 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65 43 75 define restoreCu
1555b 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 rsorPosition(p)
1555c 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65 3e 3d \. (p->eState>=
1555d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 CURSOR_REQUIRESE
1555e 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20 20 20 EK ? \.
1555f 62 74 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 btreeRestoreCurs
15560 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20 3a 20 orPosition(p) :
15561 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 \. SQLIT
15562 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 E_OK)../*.** Det
15563 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f ermine whether o
15564 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72 20 68 r not a cursor h
15565 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 as moved from th
15566 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0a 2a 2a e position it.**
15567 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63 65 64 was last placed
15568 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20 63 61 at. Cursors ca
15569 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68 65 20 n move when the
1556a 72 6f 77 20 74 68 65 79 20 61 72 65 20 70 6f 69 row they are poi
1556b 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 73 20 64 nting.** at is d
1556c 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 eleted out from
1556d 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a 2a 0a 2a under them..**.*
1556e 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
1556f 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 eturns an error
15570 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 68 69 6e code if somethin
15571 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 54 g goes wrong. T
15572 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 2a 70 he.** integer *p
15573 48 61 73 4d 6f 76 65 64 20 69 73 20 73 65 74 20 HasMoved is set
15574 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 20 63 75 to one if the cu
15575 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 61 rsor has moved a
15576 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a nd 0 if not..*/.
15577 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
15578 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 nt sqlite3BtreeC
15579 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 ursorHasMoved(Bt
1557a 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
1557b 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b 0a 20 t *pHasMoved){.
1557c 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d int rc;.. rc =
1557d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f restoreCursorPo
1557e 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 sition(pCur);.
1557f 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 if( rc ){. *p
15580 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 HasMoved = 1;.
15581 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
15582 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 . if( pCur->eSt
15583 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate!=CURSOR_VALI
15584 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69 70 4e D || pCur->skipN
15585 65 78 74 21 3d 30 20 29 7b 0a 20 20 20 20 2a 70 ext!=0 ){. *p
15586 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a 20 20 HasMoved = 1;.
15587 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 48 61 73 }else{. *pHas
15588 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 Moved = 0;. }.
15589 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1558a 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 K;.}..#ifndef SQ
1558b 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1558c 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e CUUM./*.** Given
1558d 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f a page number o
1558e 66 20 61 20 72 65 67 75 6c 61 72 20 64 61 74 61 f a regular data
1558f 62 61 73 65 20 70 61 67 65 2c 20 72 65 74 75 72 base page, retur
15590 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 n the page.** nu
15591 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 6f 69 mber for the poi
15592 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20 74 68 nter-map page th
15593 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
15594 65 6e 74 72 79 20 66 6f 72 20 74 68 65 0a 2a 2a entry for the.**
15595 20 69 6e 70 75 74 20 70 61 67 65 20 6e 75 6d 62 input page numb
15596 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 er..*/.static Pg
15597 6e 6f 20 70 74 72 6d 61 70 50 61 67 65 6e 6f 28 no ptrmapPageno(
15598 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
15599 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 gno pgno){. int
1559a 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 nPagesPerMapPag
1559b 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 4d 61 e;. Pgno iPtrMa
1559c 70 2c 20 72 65 74 3b 0a 20 20 61 73 73 65 72 74 p, ret;. assert
1559d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1559e 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 held(pBt->mutex)
1559f 20 29 3b 0a 20 20 6e 50 61 67 65 73 50 65 72 4d );. nPagesPerM
155a0 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d 3e 75 apPage = (pBt->u
155a1 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31 3b 0a sableSize/5)+1;.
155a2 20 20 69 50 74 72 4d 61 70 20 3d 20 28 70 67 6e iPtrMap = (pgn
155a3 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 72 4d 61 o-2)/nPagesPerMa
155a4 70 50 61 67 65 3b 0a 20 20 72 65 74 20 3d 20 28 pPage;. ret = (
155a5 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 73 50 65 iPtrMap*nPagesPe
155a6 72 4d 61 70 50 61 67 65 29 20 2b 20 32 3b 20 0a rMapPage) + 2; .
155a7 20 20 69 66 28 20 72 65 74 3d 3d 50 45 4e 44 49 if( ret==PENDI
155a8 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 NG_BYTE_PAGE(pBt
155a9 29 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b 3b 0a ) ){. ret++;.
155aa 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 }. return ret
155ab 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
155ac 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 an entry into t
155ad 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a he pointer map..
155ae 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
155af 6e 65 20 75 70 64 61 74 65 73 20 74 68 65 20 70 ne updates the p
155b0 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
155b1 20 66 6f 72 20 70 61 67 65 20 6e 75 6d 62 65 72 for page number
155b2 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74 68 61 'key'.** so tha
155b3 74 20 69 74 20 6d 61 70 73 20 74 6f 20 74 79 70 t it maps to typ
155b4 65 20 27 65 54 79 70 65 27 20 61 6e 64 20 70 61 e 'eType' and pa
155b5 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 rent page number
155b6 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a 20 49 'pgno'..**.** I
155b7 66 20 2a 70 52 43 20 69 73 20 69 6e 69 74 69 61 f *pRC is initia
155b8 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28 6e 6f lly non-zero (no
155b9 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 74 68 65 n-SQLITE_OK) the
155ba 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
155bb 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 s.** a no-op. I
155bc 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 f an error occur
155bd 73 2c 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 s, the appropria
155be 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 te error code is
155bf 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f written.** into
155c0 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 *pRC..*/.static
155c1 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 28 void ptrmapPut(
155c2 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 BtShared *pBt, P
155c3 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54 79 70 gno key, u8 eTyp
155c4 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74 2c 20 e, Pgno parent,
155c5 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 44 62 50 int *pRC){. DbP
155c6 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 2f age *pDbPage; /
155c7 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 * The pointer ma
155c8 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a p page */. u8 *
155c9 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 2f 2a pPtrmap; /*
155ca 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 The pointer map
155cb 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e 6f 20 data */. Pgno
155cc 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f 2a 20 iPtrmap; /*
155cd 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 The pointer map
155ce 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 page number */.
155cf 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 int offset;
155d0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 /* Offset in
155d1 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 pointer map page
155d2 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 */. int rc;
155d3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
155d4 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 66 n code from subf
155d5 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20 20 69 unctions */.. i
155d6 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e f( *pRC ) return
155d7 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c ;.. assert( sql
155d8 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
155d9 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
155da 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72 2d 6a /* The master-j
155db 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75 6d 62 ournal page numb
155dc 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20 62 65 er must never be
155dd 20 75 73 65 64 20 61 73 20 61 20 70 6f 69 6e 74 used as a point
155de 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 er map page */.
155df 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54 52 4d assert( 0==PTRM
155e0 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 50 AP_ISPAGE(pBt, P
155e1 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
155e2 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61 73 73 (pBt)) );.. ass
155e3 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 ert( pBt->autoVa
155e4 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20 6b 65 cuum );. if( ke
155e5 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 y==0 ){. *pRC
155e6 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
155e7 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75 T_BKPT;. retu
155e8 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 72 6d 61 rn;. }. iPtrma
155e9 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e p = PTRMAP_PAGEN
155ea 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 O(pBt, key);. r
155eb 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
155ec 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c Get(pBt->pPager,
155ed 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 iPtrmap, &pDbPa
155ee 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 ge);. if( rc!=S
155ef 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
155f0 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 *pRC = rc;. r
155f1 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f 66 66 eturn;. }. off
155f2 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 set = PTRMAP_PTR
155f3 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 OFFSET(iPtrmap,
155f4 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f 66 66 73 key);. if( offs
155f5 65 74 3c 30 20 29 7b 0a 20 20 20 20 2a 70 52 43 et<0 ){. *pRC
155f6 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
155f7 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 6f 74 6f T_BKPT;. goto
155f8 20 70 74 72 6d 61 70 5f 65 78 69 74 3b 0a 20 20 ptrmap_exit;.
155f9 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75 }. pPtrmap = (u
155fa 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 8 *)sqlite3Pager
155fb 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29 GetData(pDbPage)
155fc 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d ;.. if( eType!=
155fd 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 pPtrmap[offset]
155fe 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70 50 74 || get4byte(&pPt
155ff 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 21 rmap[offset+1])!
15600 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 54 =parent ){. T
15601 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55 50 RACE(("PTRMAP_UP
15602 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25 64 DATE: %d->(%d,%d
15603 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70 65 )\n", key, eType
15604 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20 20 , parent));.
15605 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 6c 69 74 *pRC= rc = sqlit
15606 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62 e3PagerWrite(pDb
15607 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
15608 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
15609 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b 6f 66 pPtrmap[of
1560a 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b 0a 20 fset] = eType;.
1560b 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 put4byte(&p
1560c 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d Ptrmap[offset+1]
1560d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d , parent);. }
1560e 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f 65 78 69 . }..ptrmap_exi
1560f 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 t:. sqlite3Page
15610 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b rUnref(pDbPage);
15611 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 .}../*.** Read a
15612 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 n entry from the
15613 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a pointer map..**
15614 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
15615 20 72 65 74 72 69 65 76 65 73 20 74 68 65 20 70 retrieves the p
15616 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 79 ointer map entry
15617 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79 27 2c for page 'key',
15618 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65 20 writing.** the
15619 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e 74 20 type and parent
1561a 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 2a page number to *
1561b 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50 67 6e pEType and *pPgn
1561c 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a o respectively..
1561d 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 ** An error code
1561e 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
1561f 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 something goes w
15620 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20 rong, otherwise
15621 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 SQLITE_OK..*/.st
15622 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 47 atic int ptrmapG
15623 65 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 et(BtShared *pBt
15624 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 2a , Pgno key, u8 *
15625 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a 70 50 pEType, Pgno *pP
15626 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a gno){. DbPage *
15627 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20 54 68 pDbPage; /* Th
15628 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 e pointer map pa
15629 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50 74 72 ge */. int iPtr
1562a 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f map; /* Po
1562b 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 69 inter map page i
1562c 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a 70 50 ndex */. u8 *pP
1562d 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20 trmap; /*
1562e 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 Pointer map page
1562f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6f data */. int o
15630 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a ffset; /*
15631 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74 72 79 Offset of entry
15632 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 in pointer map
15633 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 */. int rc;..
15634 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15635 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
15636 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 50 74 mutex) );.. iPt
15637 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 rmap = PTRMAP_PA
15638 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a GENO(pBt, key);.
15639 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1563a 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 gerGet(pBt->pPag
1563b 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 er, iPtrmap, &pD
1563c 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 bPage);. if( rc
1563d 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 !=0 ){. retur
1563e 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 74 72 n rc;. }. pPtr
1563f 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 map = (u8 *)sqli
15640 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 te3PagerGetData(
15641 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f 66 66 pDbPage);.. off
15642 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 set = PTRMAP_PTR
15643 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70 2c 20 OFFSET(iPtrmap,
15644 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 key);. assert(
15645 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20 20 2a pEType!=0 );. *
15646 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d 61 70 pEType = pPtrmap
15647 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66 28 20 [offset];. if(
15648 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f 20 3d pPgno ) *pPgno =
15649 20 67 65 74 34 62 79 74 65 28 26 70 50 74 72 6d get4byte(&pPtrm
1564a 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b 0a 0a ap[offset+1]);..
1564b 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
1564c 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 ref(pDbPage);.
1564d 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20 7c 7c if( *pEType<1 ||
1564e 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72 65 74 *pEType>5 ) ret
1564f 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15650 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74 75 72 PT_BKPT;. retur
15651 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
15652 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65 66 69 #else /* if defi
15653 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned SQLITE_OMIT_
15654 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 20 20 AUTOVACUUM */.
15655 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 #define ptrmapPu
15656 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a 20 20 t(w,x,y,z,rc).
15657 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70 47 65 #define ptrmapGe
15658 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c 49 54 t(w,x,y,z) SQLIT
15659 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 70 E_OK. #define p
1565a 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 trmapPutOvflPtr(
1565b 78 2c 20 79 2c 20 72 63 29 0a 23 65 6e 64 69 66 x, y, rc).#endif
1565c 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 ../*.** Given a
1565d 62 74 72 65 65 20 70 61 67 65 20 61 6e 64 20 61 btree page and a
1565e 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30 20 6d cell index (0 m
1565f 65 61 6e 73 20 74 68 65 20 66 69 72 73 74 20 63 eans the first c
15660 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20 70 61 ell on.** the pa
15661 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68 65 20 ge, 1 means the
15662 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61 6e 64 second cell, and
15663 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74 75 72 so forth) retur
15664 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 n a pointer.** t
15665 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 o the cell conte
15666 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 nt..**.** This r
15667 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f 6e 6c outine works onl
15668 79 20 66 6f 72 20 70 61 67 65 73 20 74 68 61 74 y for pages that
15669 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 do not contain
1566a 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a overflow cells..
1566b 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e 64 43 */.#define findC
1566c 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28 28 50 ell(P,I) \. ((P
1566d 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50 29 2d )->aData + ((P)-
1566e 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65 74 32 >maskPage & get2
1566f 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61 74 61 byte(&(P)->aData
15670 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 [(P)->cellOffset
15671 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a 0a 2a +2*(I)])))../*.*
15672 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20 63 6f * This a more co
15673 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20 6f 66 mplex version of
15674 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68 61 74 findCell() that
15675 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20 70 61 works for.** pa
15676 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f 6e 74 ges that do cont
15677 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c ain overflow cel
15678 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 ls..*/.static u8
15679 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 *findOverflowCe
1567a 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ll(MemPage *pPag
1567b 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 e, int iCell){.
1567c 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 int i;. assert
1567d 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1567e 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d held(pPage->pBt-
1567f 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 >mutex) );. for
15680 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 (i=pPage->nOverf
15681 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d low-1; i>=0; i--
15682 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 ){. int k;.
15683 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65 struct _OvflCe
15684 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70 ll *pOvfl;. p
15685 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61 Ovfl = &pPage->a
15686 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d Ovfl[i];. k =
15687 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20 pOvfl->idx;.
15688 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b if( k<=iCell ){
15689 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43 . if( k==iC
1568a 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 ell ){. r
1568b 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65 eturn pOvfl->pCe
1568c 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ll;. }.
1568d 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d iCell--;. }
1568e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69 . }. return fi
1568f 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 ndCell(pPage, iC
15690 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 ell);.}../*.** P
15691 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74 arse a cell cont
15692 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69 ent block and fi
15693 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e ll in the CellIn
15694 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 fo structure. T
15695 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20 here.** are two
15696 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73 versions of this
15697 20 66 75 6e 63 74 69 6f 6e 2e 20 20 62 74 72 65 function. btre
15698 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 61 6b eParseCell() tak
15699 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20 69 6e es a .** cell in
1569a 64 65 78 20 61 73 20 74 68 65 20 73 65 63 6f 6e dex as the secon
1569b 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 62 d argument and b
1569c 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
1569d 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 () .** takes a p
1569e 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f ointer to the bo
1569f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 dy of the cell a
156a0 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 s its second arg
156a1 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 ument..**.** Wit
156a2 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 hin this file, t
156a3 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d he parseCell() m
156a4 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c acro can be call
156a5 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a ed instead of.**
156a6 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 btreeParseCellP
156a7 74 72 28 29 2e 20 55 73 69 6e 67 20 73 6f 6d 65 tr(). Using some
156a8 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 68 69 73 compilers, this
156a9 20 77 69 6c 6c 20 62 65 20 66 61 73 74 65 72 2e will be faster.
156aa 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
156ab 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 btreeParseCellPt
156ac 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 r(. MemPage *pP
156ad 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 age, /*
156ae 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 Page containing
156af 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38 the cell */. u8
156b0 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 *pCell,
156b1 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
156b2 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74 65 78 to the cell tex
156b3 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f t. */. CellInfo
156b4 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20 *pInfo
156b5 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20 /* Fill in this
156b6 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a structure */.){.
156b7 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20 u16 n;
156b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
156b9 62 65 72 20 62 79 74 65 73 20 69 6e 20 63 65 6c ber bytes in cel
156ba 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 l content header
156bb 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f */. u32 nPaylo
156bc 61 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ad; /*
156bd 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 Number of bytes
156be 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f 61 64 of cell payload
156bf 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 */.. assert( s
156c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
156c1 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
156c2 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e 66 6f tex) );.. pInfo
156c3 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b ->pCell = pCell;
156c4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
156c5 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70 50 61 ->leaf==0 || pPa
156c6 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b 0a 20 ge->leaf==1 );.
156c7 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68 69 6c n = pPage->chil
156c8 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73 73 65 dPtrSize;. asse
156c9 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61 67 65 rt( n==4-4*pPage
156ca 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66 28 20 ->leaf );. if(
156cb 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b pPage->intKey ){
156cc 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e . if( pPage->
156cd 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 hasData ){.
156ce 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 n += getVarint3
156cf 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 2(&pCell[n], nPa
156d0 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65 6c 73 yload);. }els
156d1 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c 6f 61 e{. nPayloa
156d2 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 d = 0;. }.
156d3 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 28 n += getVarint(
156d4 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36 34 2a &pCell[n], (u64*
156d5 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29 3b 0a )&pInfo->nKey);.
156d6 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 pInfo->nData
156d7 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d = nPayload;. }
156d8 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f 2d else{. pInfo-
156d9 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 >nData = 0;.
156da 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 n += getVarint32
156db 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50 61 79 (&pCell[n], nPay
156dc 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e 66 6f load);. pInfo
156dd 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c 6f 61 ->nKey = nPayloa
156de 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f 2d 3e d;. }. pInfo->
156df 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61 79 6c nPayload = nPayl
156e0 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e 6e 48 oad;. pInfo->nH
156e1 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 74 65 73 eader = n;. tes
156e2 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d tcase( nPayload=
156e3 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c =pPage->maxLocal
156e4 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 );. testcase(
156e5 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d nPayload==pPage-
156e6 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 >maxLocal+1 );.
156e7 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79 if( likely(nPay
156e8 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 load<=pPage->max
156e9 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a Local) ){. /*
156ea 20 54 68 69 73 20 69 73 20 74 68 65 20 28 65 61 This is the (ea
156eb 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 sy) common case
156ec 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 65 where the entire
156ed 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20 payload fits.
156ee 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61 ** on the loca
156ef 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72 l page. No over
156f0 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 64 flow is required
156f1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 .. */. int
156f2 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 nSize;
156f3 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f /* Total size o
156f4 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 f cell content i
156f5 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e n bytes */. n
156f6 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20 Size = nPayload
156f7 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e + n;. pInfo->
156f8 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6e 50 nLocal = (u16)nP
156f9 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 49 6e 66 ayload;. pInf
156fa 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d 20 30 o->iOverflow = 0
156fb 3b 0a 20 20 20 20 69 66 28 20 28 6e 53 69 7a 65 ;. if( (nSize
156fc 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a 20 20 20 & ~3)==0 ){.
156fd 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 20 20 20 nSize = 4;
156fe 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 /* Minimum
156ff 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a cell size is 4 *
15700 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 /. }. pInf
15701 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75 31 36 29 o->nSize = (u16)
15702 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a nSize;. }else{.
15703 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 /* If the pa
15704 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f 74 20 66 yload will not f
15705 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 6f 6e it completely on
15706 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2c the local page,
15707 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 we have. **
15708 74 6f 20 64 65 63 69 64 65 20 68 6f 77 20 6d 75 to decide how mu
15709 63 68 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 ch to store loca
1570a 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d 75 63 68 lly and how much
1570b 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f 0a 20 to spill onto.
1570c 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70 ** overflow p
1570d 61 67 65 73 2e 20 20 54 68 65 20 73 74 72 61 74 ages. The strat
1570e 65 67 79 20 69 73 20 74 6f 20 6d 69 6e 69 6d 69 egy is to minimi
1570f 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 ze the amount of
15710 20 75 6e 75 73 65 64 0a 20 20 20 20 2a 2a 20 73 unused. ** s
15711 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 pace on overflow
15712 20 70 61 67 65 73 20 77 68 69 6c 65 20 6b 65 65 pages while kee
15713 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e 74 20 ping the amount
15714 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 of local storage
15715 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65 74 77 65 . ** in betwe
15716 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e 64 20 en minLocal and
15717 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 20 2a 2a maxLocal.. **
15718 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69 6e 67 3a . ** Warning:
15719 20 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 77 changing the w
1571a 61 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 79 6c ay overflow payl
1571b 6f 61 64 20 69 73 20 64 69 73 74 72 69 62 75 74 oad is distribut
1571c 65 64 20 69 6e 20 61 6e 79 0a 20 20 20 20 2a 2a ed in any. **
1571d 20 77 61 79 20 77 69 6c 6c 20 72 65 73 75 6c 74 way will result
1571e 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 in an incompati
1571f 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e ble file format.
15720 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 . */. int
15721 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 69 minLocal; /* Mi
15722 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 nimum amount of
15723 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 payload held loc
15724 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ally */. int
15725 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20 4d 61 maxLocal; /* Ma
15726 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 ximum amount of
15727 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63 payload held loc
15728 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ally */. int
15729 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20 4f 76 surplus; /* Ov
1572a 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64 20 61 erflow payload a
1572b 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c 6f 63 vailable for loc
1572c 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a 0a 20 al storage */..
1572d 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 minLocal = pP
1572e 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 age->minLocal;.
1572f 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 50 maxLocal = pP
15730 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b 0a 20 age->maxLocal;.
15731 20 20 20 73 75 72 70 6c 75 73 20 3d 20 6d 69 6e surplus = min
15732 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79 6c 6f 61 Local + (nPayloa
15733 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25 28 70 d - minLocal)%(p
15734 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
15735 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 eSize - 4);.
15736 74 65 73 74 63 61 73 65 28 20 73 75 72 70 6c 75 testcase( surplu
15737 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 s==maxLocal );.
15738 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 testcase( sur
15739 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 2b 31 plus==maxLocal+1
1573a 20 29 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 );. if( surp
1573b 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 lus <= maxLocal
1573c 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e ){. pInfo->
1573d 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 73 75 nLocal = (u16)su
1573e 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c 73 65 rplus;. }else
1573f 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e {. pInfo->n
15740 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6d 69 6e Local = (u16)min
15741 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 Local;. }.
15742 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f pInfo->iOverflo
15743 77 20 3d 20 28 75 31 36 29 28 70 49 6e 66 6f 2d w = (u16)(pInfo-
15744 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 3b 0a 20 20 >nLocal + n);.
15745 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d pInfo->nSize =
15746 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f pInfo->iOverflo
15747 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 w + 4;. }.}.#de
15748 66 69 6e 65 20 70 61 72 73 65 43 65 6c 6c 28 70 fine parseCell(p
15749 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e Page, iCell, pIn
1574a 66 6f 29 20 5c 0a 20 20 62 74 72 65 65 50 61 72 fo) \. btreePar
1574b 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61 67 65 seCellPtr((pPage
1574c 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70 50 61 ), findCell((pPa
1574d 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c 20 28 ge), (iCell)), (
1574e 70 49 6e 66 6f 29 29 0a 73 74 61 74 69 63 20 76 pInfo)).static v
1574f 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65 oid btreeParseCe
15750 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 ll(. MemPage *p
15751 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f 2a Page, /*
15752 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 Page containing
15753 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 the cell */. i
15754 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20 20 20 nt iCell,
15755 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65 /* The ce
15756 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72 73 74 ll index. First
15757 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a 20 20 cell is 0 */.
15758 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 CellInfo *pInfo
15759 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 /* Fill
1575a 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 in this structur
1575b 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 73 65 43 e */.){. parseC
1575c 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c ell(pPage, iCell
1575d 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f 2a 0a , pInfo);.}../*.
1575e 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 ** Compute the t
1575f 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 otal number of b
15760 79 74 65 73 20 74 68 61 74 20 61 20 43 65 6c 6c ytes that a Cell
15761 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20 63 65 needs in the ce
15762 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65 61 20 ll.** data area
15763 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70 61 67 of the btree-pag
15764 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6e e. The return n
15765 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73 20 74 umber includes t
15766 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 he cell.** data
15767 68 65 61 64 65 72 20 61 6e 64 20 74 68 65 20 6c header and the l
15768 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20 62 75 ocal payload, bu
15769 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72 66 6c t not any overfl
1576a 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20 74 68 ow page or.** th
1576b 65 20 73 70 61 63 65 20 75 73 65 64 20 62 79 20 e space used by
1576c 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 the cell pointer
1576d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 ..*/.static u16
1576e 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50 cellSizePtr(MemP
1576f 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
15770 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 49 pCell){. u8 *pI
15771 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70 50 61 ter = &pCell[pPa
15772 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 ge->childPtrSize
15773 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a 65 3b 0a ];. u32 nSize;.
15774 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
15775 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20 76 61 EBUG. /* The va
15776 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 lue returned by
15777 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 this function sh
15778 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 74 ould always be t
15779 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a 2a 20 he same as. **
1577a 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e 6e 53 the (CellInfo.nS
1577b 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75 6e 64 ize) value found
1577c 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75 6c 6c by doing a full
1577d 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a 20 20 parse of the.
1577e 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51 4c 49 ** cell. If SQLI
1577f 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 TE_DEBUG is defi
15780 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 ned, an assert()
15781 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f at the bottom o
15782 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75 6e 63 f. ** this func
15783 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20 74 68 tion verifies th
15784 61 74 20 74 68 69 73 20 69 6e 76 61 72 69 61 6e at this invarian
15785 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61 74 65 t is not violate
15786 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f d. */. CellInfo
15787 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20 62 74 debuginfo;. bt
15788 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
15789 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 64 pPage, pCell, &d
1578a 65 62 75 67 69 6e 66 6f 29 3b 0a 23 65 6e 64 69 ebuginfo);.#endi
1578b 66 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e f.. if( pPage->
1578c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 75 38 intKey ){. u8
1578d 20 2a 70 45 6e 64 3b 0a 20 20 20 20 69 66 28 20 *pEnd;. if(
1578e 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 pPage->hasData )
1578f 7b 0a 20 20 20 20 20 20 70 49 74 65 72 20 2b 3d {. pIter +=
15790 20 67 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 getVarint32(pIt
15791 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 er, nSize);.
15792 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 }else{. nSi
15793 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 ze = 0;. }..
15794 20 20 20 2f 2a 20 70 49 74 65 72 20 6e 6f 77 20 /* pIter now
15795 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 36 34 points at the 64
15796 2d 62 69 74 20 69 6e 74 65 67 65 72 20 6b 65 79 -bit integer key
15797 20 76 61 6c 75 65 2c 20 61 20 76 61 72 69 61 62 value, a variab
15798 6c 65 20 6c 65 6e 67 74 68 20 0a 20 20 20 20 2a le length . *
15799 2a 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 66 * integer. The f
1579a 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d ollowing block m
1579b 6f 76 65 73 20 70 49 74 65 72 20 74 6f 20 70 6f oves pIter to po
1579c 69 6e 74 20 61 74 20 74 68 65 20 66 69 72 73 74 int at the first
1579d 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 70 61 73 byte. ** pas
1579e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 t the end of the
1579f 20 6b 65 79 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 key value. */.
157a0 20 20 20 70 45 6e 64 20 3d 20 26 70 49 74 65 72 pEnd = &pIter
157a1 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 [9];. while(
157a2 28 2a 70 49 74 65 72 2b 2b 29 26 30 78 38 30 20 (*pIter++)&0x80
157a3 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20 29 3b && pIter<pEnd );
157a4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 . }else{. pI
157a5 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 ter += getVarint
157a6 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 32(pIter, nSize)
157a7 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 74 63 61 73 ;. }.. testcas
157a8 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d e( nSize==pPage-
157a9 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 >maxLocal );. t
157aa 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d estcase( nSize==
157ab 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b pPage->maxLocal+
157ac 31 20 29 3b 0a 20 20 69 66 28 20 6e 53 69 7a 65 1 );. if( nSize
157ad 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c >pPage->maxLocal
157ae 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 4c ){. int minL
157af 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 ocal = pPage->mi
157b0 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6e 53 69 7a nLocal;. nSiz
157b1 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 e = minLocal + (
157b2 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c 6f 63 61 6c nSize - minLocal
157b3 29 20 25 20 28 70 50 61 67 65 2d 3e 70 42 74 2d ) % (pPage->pBt-
157b4 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 >usableSize - 4)
157b5 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
157b6 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 nSize==pPage->ma
157b7 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20 74 65 xLocal );. te
157b8 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 stcase( nSize==p
157b9 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 Page->maxLocal+1
157ba 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 53 69 7a );. if( nSiz
157bb 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 e>pPage->maxLoca
157bc 6c 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 l ){. nSize
157bd 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 = minLocal;.
157be 20 7d 0a 20 20 20 20 6e 53 69 7a 65 20 2b 3d 20 }. nSize +=
157bf 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 7a 65 20 2b 4;. }. nSize +
157c0 3d 20 28 75 33 32 29 28 70 49 74 65 72 20 2d 20 = (u32)(pIter -
157c1 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 54 68 pCell);.. /* Th
157c2 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f e minimum size o
157c3 66 20 61 6e 79 20 63 65 6c 6c 20 69 73 20 34 20 f any cell is 4
157c4 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 66 28 20 bytes. */. if(
157c5 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20 20 20 6e nSize<4 ){. n
157c6 53 69 7a 65 20 3d 20 34 3b 0a 20 20 7d 0a 0a 20 Size = 4;. }..
157c7 20 61 73 73 65 72 74 28 20 6e 53 69 7a 65 3d 3d assert( nSize==
157c8 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 7a 65 20 debuginfo.nSize
157c9 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 31 36 );. return (u16
157ca 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66 64 65 )nSize;.}..#ifde
157cb 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f f SQLITE_DEBUG./
157cc 2a 20 54 68 69 73 20 76 61 72 69 61 74 69 6f 6e * This variation
157cd 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 on cellSizePtr(
157ce 29 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 ) is used inside
157cf 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61 of assert() sta
157d0 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c 79 2e tements.** only.
157d1 20 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 63 */.static u16 c
157d2 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 67 65 20 ellSize(MemPage
157d3 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c *pPage, int iCel
157d4 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 65 6c l){. return cel
157d5 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 lSizePtr(pPage,
157d6 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 findCell(pPage,
157d7 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 iCell));.}.#endi
157d8 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 f..#ifndef SQLIT
157d9 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
157da 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 M./*.** If the c
157db 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 ell pCell, part
157dc 6f 66 20 70 61 67 65 20 70 50 61 67 65 20 63 6f of page pPage co
157dd 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 ntains a pointer
157de 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c .** to an overfl
157df 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 ow page, insert
157e0 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 an entry into th
157e1 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a e pointer-map.**
157e2 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f for the overflo
157e3 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 w page..*/.stati
157e4 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75 74 c void ptrmapPut
157e5 4f 76 66 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 OvflPtr(MemPage
157e6 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43 65 6c *pPage, u8 *pCel
157e7 6c 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 l, int *pRC){.
157e8 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
157e9 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 if( *pRC ) retu
157ea 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 rn;. assert( pC
157eb 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 62 74 72 65 ell!=0 );. btre
157ec 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 eParseCellPtr(pP
157ed 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 age, pCell, &inf
157ee 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 69 o);. assert( (i
157ef 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 nfo.nData+(pPage
157f0 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e ->intKey?0:info.
157f1 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 nKey))==info.nPa
157f2 79 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28 20 69 yload );. if( i
157f3 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b nfo.iOverflow ){
157f4 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d . Pgno ovfl =
157f5 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c get4byte(&pCell
157f6 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d [info.iOverflow]
157f7 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 );. ptrmapPut
157f8 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 (pPage->pBt, ovf
157f9 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c l, PTRMAP_OVERFL
157fa 4f 57 31 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f OW1, pPage->pgno
157fb 2c 20 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 , pRC);. }.}.#e
157fc 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 ndif.../*.** Def
157fd 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67 65 ragment the page
157fe 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c given. All Cel
157ff 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 ls are moved to
15800 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 the.** end of th
15801 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 e page and all f
15802 72 65 65 20 73 70 61 63 65 20 69 73 20 63 6f 6c ree space is col
15803 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a lected into one.
15804 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b 20 74 ** big FreeBlk t
15805 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 62 65 hat occurs in be
15806 74 77 65 65 6e 20 74 68 65 20 68 65 61 64 65 72 tween the header
15807 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 and cell.** poi
15808 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64 20 74 nter array and t
15809 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
1580a 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 area..*/.static
1580b 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74 50 61 int defragmentPa
1580c 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ge(MemPage *pPag
1580d 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 e){. int i;
1580e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1580f 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
15810 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 */. int pc;
15811 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15812 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 /* Address of a
15813 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 i-th cell */.
15814 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 int hdr;
15815 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 /* Of
15816 66 73 65 74 20 74 6f 20 74 68 65 20 70 61 67 65 fset to the page
15817 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 header */. int
15818 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 size;
15819 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
1581a 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 of a cell */. i
1581b 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 nt usableSize;
1581c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1581d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20 62 79 ber of usable by
1581e 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f tes on a page */
1581f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 . int cellOffse
15820 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a t; /*
15821 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 Offset to the c
15822 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 ell pointer arra
15823 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b y */. int cbrk;
15824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15825 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 /* Offset to t
15826 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
15827 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 area */. int nC
15828 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 ell;
15829 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1582a 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 f cells on the p
1582b 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 age */. unsigne
1582c 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20 20 20 d char *data;
1582d 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 /* The page
1582e 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e data */. unsign
1582f 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 ed char *temp;
15830 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 /* Temp are
15831 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 a for cell conte
15832 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c nt */. int iCel
15833 6c 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 lFirst;
15834 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f /* First allo
15835 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 wable cell index
15836 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c */. int iCellL
15837 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 ast;
15838 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c /* Last possibl
15839 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a e cell index */.
1583a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1583b 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
1583c 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ble(pPage->pDbPa
1583d 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
1583e 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 pPage->pBt!=0 )
1583f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
15840 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 e->pBt->usableSi
15841 7a 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 ze <= SQLITE_MAX
15842 5f 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 _PAGE_SIZE );.
15843 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e assert( pPage->n
15844 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 Overflow==0 );.
15845 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15846 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
15847 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
15848 3b 0a 20 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 ;. temp = sqlit
15849 65 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 e3PagerTempSpace
1584a 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 (pPage->pBt->pPa
1584b 67 65 72 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 ger);. data = p
1584c 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 Page->aData;. h
1584d 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f dr = pPage->hdrO
1584e 66 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 ffset;. cellOff
1584f 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c set = pPage->cel
15850 6c 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c lOffset;. nCell
15851 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
15852 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c . assert( nCell
15853 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 ==get2byte(&data
15854 5b 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 [hdr+3]) );. us
15855 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 ableSize = pPage
15856 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a ->pBt->usableSiz
15857 65 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 e;. cbrk = get2
15858 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 byte(&data[hdr+5
15859 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 ]);. memcpy(&te
1585a 6d 70 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b mp[cbrk], &data[
1585b 63 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a cbrk], usableSiz
1585c 65 20 2d 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 e - cbrk);. cbr
1585d 6b 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a k = usableSize;.
1585e 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 iCellFirst = c
1585f 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 ellOffset + 2*nC
15860 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 ell;. iCellLast
15861 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 = usableSize -
15862 34 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4;. for(i=0; i<
15863 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 nCell; i++){.
15864 20 75 38 20 2a 70 41 64 64 72 3b 20 20 20 20 20 u8 *pAddr;
15865 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 65 6c 6c /* The i-th cell
15866 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 pointer */.
15867 70 41 64 64 72 20 3d 20 26 64 61 74 61 5b 63 65 pAddr = &data[ce
15868 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b llOffset + i*2];
15869 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 . pc = get2by
1586a 74 65 28 70 41 64 64 72 29 3b 0a 20 20 20 20 74 te(pAddr);. t
1586b 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 estcase( pc==iCe
1586c 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 74 llFirst );. t
1586d 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 estcase( pc==iCe
1586e 6c 6c 4c 61 73 74 20 29 3b 0a 23 69 66 20 21 64 llLast );.#if !d
1586f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
15870 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 ABLE_OVERSIZE_CE
15871 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 2f 2a LL_CHECK). /*
15872 20 54 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e These condition
15873 73 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 s have already b
15874 65 65 6e 20 76 65 72 69 66 69 65 64 20 69 6e 20 een verified in
15875 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 0a btreeInitPage().
15876 20 20 20 20 2a 2a 20 69 66 20 53 51 4c 49 54 45 ** if SQLITE
15877 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 _ENABLE_OVERSIZE
15878 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64 _CELL_CHECK is d
15879 65 66 69 6e 65 64 20 0a 20 20 20 20 2a 2f 0a 20 efined . */.
1587a 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 if( pc<iCellF
1587b 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c irst || pc>iCell
1587c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 72 65 Last ){. re
1587d 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 turn SQLITE_CORR
1587e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a UPT_BKPT;. }.
1587f 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 #endif. asser
15880 74 28 20 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73 t( pc>=iCellFirs
15881 74 20 26 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 t && pc<=iCellLa
15882 73 74 20 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d st );. size =
15883 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
15884 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a ge, &temp[pc]);.
15885 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 cbrk -= size
15886 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 ;.#if defined(SQ
15887 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 LITE_ENABLE_OVER
15888 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 SIZE_CELL_CHECK)
15889 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c 69 43 . if( cbrk<iC
1588a 65 6c 6c 46 69 72 73 74 20 29 7b 0a 20 20 20 20 ellFirst ){.
1588b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1588c 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1588d 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 }.#else. if
1588e 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 ( cbrk<iCellFirs
1588f 74 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 t || pc+size>usa
15890 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 bleSize ){.
15891 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15892 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 ORRUPT_BKPT;.
15893 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 }.#endif. as
15894 73 65 72 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c sert( cbrk+size<
15895 3d 75 73 61 62 6c 65 53 69 7a 65 20 26 26 20 63 =usableSize && c
15896 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 brk>=iCellFirst
15897 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
15898 20 63 62 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 cbrk+size==usab
15899 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 74 65 leSize );. te
1589a 73 74 63 61 73 65 28 20 70 63 2b 73 69 7a 65 3d stcase( pc+size=
1589b 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 =usableSize );.
1589c 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b memcpy(&data[
1589d 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d cbrk], &temp[pc]
1589e 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 , size);. put
1589f 32 62 79 74 65 28 70 41 64 64 72 2c 20 63 62 72 2byte(pAddr, cbr
158a0 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 k);. }. assert
158a1 28 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 ( cbrk>=iCellFir
158a2 73 74 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 st );. put2byte
158a3 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 (&data[hdr+5], c
158a4 62 72 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 brk);. data[hdr
158a5 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b +1] = 0;. data[
158a6 68 64 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 hdr+2] = 0;. da
158a7 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 ta[hdr+7] = 0;.
158a8 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 69 43 memset(&data[iC
158a9 65 6c 6c 46 69 72 73 74 5d 2c 20 30 2c 20 63 62 ellFirst], 0, cb
158aa 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a rk-iCellFirst);.
158ab 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
158ac 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
158ad 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
158ae 29 20 29 3b 0a 20 20 69 66 28 20 63 62 72 6b 2d ) );. if( cbrk-
158af 69 43 65 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 iCellFirst!=pPag
158b0 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 e->nFree ){.
158b1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f return SQLITE_CO
158b2 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a RRUPT_BKPT;. }.
158b3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
158b4 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c OK;.}../*.** All
158b5 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74 65 ocate nByte byte
158b6 73 20 6f 66 20 73 70 61 63 65 20 66 72 6f 6d 20 s of space from
158b7 77 69 74 68 69 6e 20 74 68 65 20 42 2d 54 72 65 within the B-Tre
158b8 65 20 70 61 67 65 20 70 61 73 73 65 64 0a 2a 2a e page passed.**
158b9 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 as the first ar
158ba 67 75 6d 65 6e 74 2e 20 57 72 69 74 65 20 69 6e gument. Write in
158bb 74 6f 20 2a 70 49 64 78 20 74 68 65 20 69 6e 64 to *pIdx the ind
158bc 65 78 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 ex into pPage->a
158bd 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 Data[].** of the
158be 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 first byte of a
158bf 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 llocated space.
158c0 52 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51 Return either SQ
158c1 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e LITE_OK or.** an
158c2 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73 75 error code (usu
158c3 61 6c 6c 79 20 53 51 4c 49 54 45 5f 43 4f 52 52 ally SQLITE_CORR
158c4 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 UPT)..**.** The
158c5 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 caller guarantee
158c6 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 s that there is
158c7 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 sufficient space
158c8 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 to make the.**
158c9 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 allocation. Thi
158ca 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 s routine might
158cb 6e 65 65 64 20 74 6f 20 64 65 66 72 61 67 6d 65 need to defragme
158cc 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 nt in order to b
158cd 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 ring.** all the
158ce 73 70 61 63 65 20 74 6f 67 65 74 68 65 72 2c 20 space together,
158cf 68 6f 77 65 76 65 72 2e 20 20 54 68 69 73 20 72 however. This r
158d0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 outine will avoi
158d1 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 d using.** the f
158d2 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 70 irst two bytes p
158d3 61 73 74 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 ast the cell poi
158d4 6e 74 65 72 20 61 72 65 61 20 73 69 6e 63 65 20 nter area since
158d5 70 72 65 73 75 6d 61 62 6c 79 20 74 68 69 73 0a presumably this.
158d6 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 ** allocation is
158d7 20 62 65 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f being made in o
158d8 72 64 65 72 20 74 6f 20 69 6e 73 65 72 74 20 61 rder to insert a
158d9 20 6e 65 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 new cell, so we
158da 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e will.** also en
158db 64 20 75 70 20 6e 65 65 64 69 6e 67 20 61 20 6e d up needing a n
158dc 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e ew cell pointer.
158dd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 .*/.static int a
158de 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d 65 6d llocateSpace(Mem
158df 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 Page *pPage, int
158e0 20 6e 42 79 74 65 2c 20 69 6e 74 20 2a 70 49 64 nByte, int *pId
158e1 78 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 x){. const int
158e2 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
158e3 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f Offset; /* Lo
158e4 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61 cal cache of pPa
158e5 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f ge->hdrOffset */
158e6 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 . u8 * const da
158e7 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
158e8 61 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c a; /* Local
158e9 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d cache of pPage-
158ea 3e 61 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 >aData */. int
158eb 6e 46 72 61 67 3b 20 20 20 20 20 20 20 20 20 20 nFrag;
158ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
158ed 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 /* Number of fr
158ee 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20 6f agmented bytes o
158ef 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 n pPage */. int
158f0 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 top;
158f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
158f2 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 /* First byte
158f3 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 of cell content
158f4 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20 67 61 area */. int ga
158f5 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 p; /* Fir
158f6 73 74 20 62 79 74 65 20 6f 66 20 67 61 70 20 62 st byte of gap b
158f7 65 74 77 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e etween cell poin
158f8 74 65 72 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f ters and cell co
158f9 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 ntent */. int r
158fa 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e c; /* In
158fb 74 65 67 65 72 20 72 65 74 75 72 6e 20 63 6f 64 teger return cod
158fc 65 20 2a 2f 0a 20 20 0a 20 20 61 73 73 65 72 74 e */. . assert
158fd 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
158fe 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
158ff 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
15900 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 ssert( pPage->pB
15901 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 t );. assert( s
15902 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15903 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
15904 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
15905 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f ( nByte>=0 ); /
15906 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 * Minimum cell s
15907 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 ize is 4 */. as
15908 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 sert( pPage->nFr
15909 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 ee>=nByte );. a
1590a 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f ssert( pPage->nO
1590b 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 verflow==0 );.
1590c 61 73 73 65 72 74 28 20 6e 42 79 74 65 3c 70 50 assert( nByte<pP
1590d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 age->pBt->usable
1590e 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 72 Size-8 );.. nFr
1590f 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d ag = data[hdr+7]
15910 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 ;. assert( pPag
15911 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 3d e->cellOffset ==
15912 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 hdr + 12 - 4*pP
15913 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 67 age->leaf );. g
15914 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c ap = pPage->cell
15915 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 Offset + 2*pPage
15916 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 3d ->nCell;. top =
15917 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15918 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67 hdr+5]);. if( g
15919 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72 6e 20 ap>top ) return
1591a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1591b 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73 65 28 KPT;. testcase(
1591c 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 gap+2==top );.
1591d 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b 31 testcase( gap+1
1591e 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 ==top );. testc
1591f 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b ase( gap==top );
15920 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 .. if( nFrag>=6
15921 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 0 ){. /* Alwa
15922 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20 68 69 ys defragment hi
15923 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65 64 20 ghly fragmented
15924 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 63 20 pages */. rc
15925 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 = defragmentPage
15926 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 (pPage);. if(
15927 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
15928 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62 . top = get2b
15929 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d yte(&data[hdr+5]
1592a 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 );. }else if( g
1592b 61 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 ap+2<=top ){.
1592c 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 66 /* Search the f
1592d 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 reelist looking
1592e 66 6f 72 20 61 20 66 72 65 65 20 73 6c 6f 74 20 for a free slot
1592f 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 big enough to sa
15930 74 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 tisfy . ** th
15931 65 20 72 65 71 75 65 73 74 2e 20 54 68 65 20 61 e request. The a
15932 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 llocation is mad
15933 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 e from the first
15934 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 free slot in .
15935 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 ** the list t
15936 68 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e 6f hat is large eno
15937 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 ugh to accomadat
15938 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 e it.. */.
15939 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 int pc, addr;.
1593a 20 20 20 66 6f 72 28 61 64 64 72 3d 68 64 72 2b for(addr=hdr+
1593b 31 3b 20 28 70 63 20 3d 20 67 65 74 32 62 79 74 1; (pc = get2byt
1593c 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e e(&data[addr]))>
1593d 30 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 0; addr=pc){.
1593e 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 67 65 int size = ge
1593f 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b t2byte(&data[pc+
15940 32 5d 29 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 2]); /* Size
15941 20 6f 66 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f of free slot */
15942 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e . if( size>
15943 3d 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 =nByte ){.
15944 20 20 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d int x = size -
15945 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 nByte;.
15946 74 65 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29 testcase( x==4 )
15947 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
15948 73 65 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 se( x==3 );.
15949 20 20 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 if( x<4 ){.
1594a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f /* Remo
1594b 76 65 20 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d ve the slot from
1594c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 the free-list.
1594d 55 70 64 61 74 65 20 74 68 65 20 6e 75 6d 62 65 Update the numbe
1594e 72 20 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a r of. *
1594f 2a 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 * fragmented byt
15950 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 es within the pa
15951 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 ge. */.
15952 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64 memcpy(&data[ad
15953 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 dr], &data[pc],
15954 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61 2);. da
15955 74 61 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 ta[hdr+7] = (u8)
15956 28 6e 46 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 (nFrag + x);.
15957 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
15958 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f /* The slo
15959 74 20 72 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 t remains on the
1595a 20 66 72 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 free-list. Redu
1595b 63 65 20 69 74 73 20 73 69 7a 65 20 74 6f 20 61 ce its size to a
1595c 63 63 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 20 ccount.
1595d 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 6f 72 74 ** for the port
1595e 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 ion used by the
1595f 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 new allocation.
15960 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 */. put
15961 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 2byte(&data[pc+2
15962 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d ], x);. }
15963 0a 20 20 20 20 20 20 20 20 2a 70 49 64 78 20 3d . *pIdx =
15964 20 70 63 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 pc + x;.
15965 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
15966 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d K;. }. }
15967 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b . }.. /* Check
15968 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 to make sure th
15969 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 ere is enough sp
1596a 61 63 65 20 69 6e 20 74 68 65 20 67 61 70 20 74 ace in the gap t
1596b 6f 20 73 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 o satisfy. ** t
1596c 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 he allocation.
1596d 49 66 20 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 If not, defragme
1596e 6e 74 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 nt.. */. testc
1596f 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 ase( gap+2+nByte
15970 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 ==top );. if( g
15971 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 ap+2+nByte>top )
15972 7b 0a 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 {. rc = defra
15973 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 gmentPage(pPage)
15974 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 ;. if( rc ) r
15975 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f eturn rc;. to
15976 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 p = get2byte(&da
15977 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 ta[hdr+5]);.
15978 61 73 73 65 72 74 28 20 67 61 70 2b 6e 42 79 74 assert( gap+nByt
15979 65 3c 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a e<=top );. }...
1597a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 /* Allocate me
1597b 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 mory from the ga
1597c 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 p in between the
1597d 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 cell pointer ar
1597e 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 ray. ** and the
1597f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 cell content ar
15980 65 61 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e ea. The btreeIn
15981 69 74 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61 itPage() call ha
15982 73 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 s already. ** v
15983 61 6c 69 64 61 74 65 64 20 74 68 65 20 66 72 65 alidated the fre
15984 65 6c 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68 elist. Given th
15985 61 74 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 at the freelist
15986 69 73 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a is valid, there.
15987 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 ** is no way t
15988 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 hat the allocati
15989 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 on can extend of
1598a 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
1598b 20 70 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 page.. ** The
1598c 61 73 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76 assert() below v
1598d 65 72 69 66 69 65 73 20 74 68 65 20 70 72 65 76 erifies the prev
1598e 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 ious sentence..
1598f 20 2a 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 */. top -= nBy
15990 74 65 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 te;. put2byte(&
15991 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 data[hdr+5], top
15992 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70 );. assert( top
15993 2b 6e 42 79 74 65 20 3c 3d 20 70 50 61 67 65 2d +nByte <= pPage-
15994 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 >pBt->usableSize
15995 20 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f );. *pIdx = to
15996 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 p;. return SQLI
15997 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 TE_OK;.}../*.**
15998 52 65 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e Return a section
15999 20 6f 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 of the pPage->a
1599a 44 61 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 Data to the free
1599b 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 list..** The fir
1599c 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e st byte of the n
1599d 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 ew free block is
1599e 20 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 pPage->aDisk[st
1599f 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 art].** and the
159a0 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 size of the bloc
159a1 6b 20 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 k is "size" byte
159a2 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 s..**.** Most of
159a3 20 74 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 the effort here
159a4 20 69 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 is involved in
159a5 63 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 coalesing adjace
159a6 6e 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b nt.** free block
159a7 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 s into a single
159a8 62 69 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a big free block..
159a9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 */.static int fr
159aa 65 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 eeSpace(MemPage
159ab 2a 70 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 *pPage, int star
159ac 74 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 t, int size){.
159ad 69 6e 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e int addr, pbegin
159ae 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 , hdr;. int iLa
159af 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 st;
159b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 /* La
159b1 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 66 rgest possible f
159b2 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 reeblock offset
159b3 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 */. unsigned ch
159b4 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 ar *data = pPage
159b5 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 ->aData;.. asse
159b6 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d rt( pPage->pBt!=
159b7 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 0 );. assert( s
159b8 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
159b9 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
159ba 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 bPage) );. asse
159bb 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65 rt( start>=pPage
159bc 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 ->hdrOffset+6+pP
159bd 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a age->childPtrSiz
159be 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 e );. assert( (
159bf 73 74 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 start + size)<=p
159c0 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c Page->pBt->usabl
159c1 65 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 eSize );. asser
159c2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
159c3 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 _held(pPage->pBt
159c4 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 ->mutex) );. as
159c5 73 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b sert( size>=0 );
159c6 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 /* Minimum ce
159c7 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a ll size is 4 */.
159c8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
159c9 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f ECURE_DELETE. /
159ca 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 * Overwrite dele
159cb 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ted information
159cc 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 with zeros when
159cd 74 68 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 the SECURE_DELET
159ce 45 20 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 E . ** option i
159cf 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d s enabled at com
159d0 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d pile-time */. m
159d1 65 6d 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 emset(&data[star
159d2 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 t], 0, size);.#e
159d3 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 ndif.. /* Add t
159d4 68 65 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e he space back in
159d5 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 to the linked li
159d6 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 st of freeblocks
159d7 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a . Note that. *
159d8 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 * even though th
159d9 65 20 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 e freeblock list
159da 20 77 61 73 20 63 68 65 63 6b 65 64 20 62 79 20 was checked by
159db 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c btreeInitPage(),
159dc 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 . ** btreeInitP
159dd 61 67 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65 age() did not de
159de 74 65 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 tect overlapping
159df 20 63 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 cells or. ** f
159e0 72 65 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f reeblocks that o
159e1 76 65 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e verlapped cells.
159e2 20 20 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 Nor does it d
159e3 65 74 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20 etect when the.
159e4 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 ** cell content
159e5 20 61 72 65 61 20 65 78 63 65 65 64 73 20 74 68 area exceeds th
159e6 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 e value in the p
159e7 61 67 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 age header. If
159e8 74 68 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 these. ** situa
159e9 74 69 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 tions arise, the
159ea 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 n subsequent ins
159eb 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d ert operations m
159ec 69 67 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a ight corrupt. *
159ed 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 * the freelist.
159ee 20 53 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74 So we do need t
159ef 6f 20 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 o check for corr
159f0 75 70 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 uption while sca
159f1 6e 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 nning. ** the f
159f2 72 65 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 reelist.. */.
159f3 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 hdr = pPage->hdr
159f4 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d Offset;. addr =
159f5 20 68 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 hdr + 1;. iLas
159f6 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e t = pPage->pBt->
159f7 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a usableSize - 4;.
159f8 20 20 61 73 73 65 72 74 28 20 73 74 61 72 74 3c assert( start<
159f9 3d 69 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c =iLast );. whil
159fa 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 e( (pbegin = get
159fb 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 2byte(&data[addr
159fc 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 ]))<start && pbe
159fd 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 gin>0 ){. if(
159fe 20 70 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 pbegin<addr+4 )
159ff 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 {. return S
15a00 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15a01 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 PT;. }. ad
15a02 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d dr = pbegin;. }
15a03 0a 20 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c . if( pbegin>iL
15a04 61 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 ast ){. retur
15a05 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 n SQLITE_CORRUPT
15a06 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 _BKPT;. }. ass
15a07 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 ert( pbegin>addr
15a08 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b || pbegin==0 );
15a09 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
15a0a 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b a[addr], start);
15a0b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 . put2byte(&dat
15a0c 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e a[start], pbegin
15a0d 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 );. put2byte(&d
15a0e 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 ata[start+2], si
15a0f 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 ze);. pPage->nF
15a10 72 65 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 ree = pPage->nFr
15a11 65 65 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a ee + (u16)size;.
15a12 0a 20 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 . /* Coalesce a
15a13 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f djacent free blo
15a14 63 6b 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 cks */. addr =
15a15 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 hdr + 1;. while
15a16 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 ( (pbegin = get2
15a17 62 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d byte(&data[addr]
15a18 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 ))>0 ){. int
15a19 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b pnext, psize, x;
15a1a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 . assert( pbe
15a1b 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 gin>addr );.
15a1c 61 73 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d assert( pbegin<=
15a1d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
15a1e 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 leSize-4 );.
15a1f 70 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 pnext = get2byte
15a20 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b (&data[pbegin]);
15a21 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 . psize = get
15a22 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 2byte(&data[pbeg
15a23 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 in+2]);. if(
15a24 70 62 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b pbegin + psize +
15a25 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 3 >= pnext && p
15a26 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 next>0 ){.
15a27 69 6e 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 int frag = pnext
15a28 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 - (pbegin+psize
15a29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 );. if( (fr
15a2a 61 67 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 ag<0) || (frag>(
15a2b 69 6e 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 int)data[hdr+7])
15a2c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
15a2d 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 rn SQLITE_CORRUP
15a2e 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a T_BKPT;. }.
15a2f 20 20 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 data[hdr+7
15a30 5d 20 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 ] -= (u8)frag;.
15a31 20 20 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 x = get2byt
15a32 65 28 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b e(&data[pnext]);
15a33 0a 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 . put2byte(
15a34 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 &data[pbegin], x
15a35 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 );. x = pne
15a36 78 74 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 xt + get2byte(&d
15a37 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 ata[pnext+2]) -
15a38 70 62 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 pbegin;. pu
15a39 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 t2byte(&data[pbe
15a3a 67 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 gin+2], x);.
15a3b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 }else{. add
15a3c 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 r = pbegin;.
15a3d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
15a3e 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 he cell content
15a3f 61 72 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 area begins with
15a40 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 a freeblock, re
15a41 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 move it. */. if
15a42 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 ( data[hdr+1]==d
15a43 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 ata[hdr+5] && da
15a44 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b ta[hdr+2]==data[
15a45 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e hdr+6] ){. in
15a46 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 t top;. pbegi
15a47 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 n = get2byte(&da
15a48 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 ta[hdr+1]);.
15a49 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 memcpy(&data[hdr
15a4a 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 +1], &data[pbegi
15a4b 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 n], 2);. top
15a4c 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 = get2byte(&data
15a4d 5b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 [hdr+5]) + get2b
15a4e 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e yte(&data[pbegin
15a4f 2b 32 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 +2]);. put2by
15a50 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c te(&data[hdr+5],
15a51 20 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 top);. }. ass
15a52 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 ert( sqlite3Page
15a53 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 rIswriteable(pPa
15a54 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a ge->pDbPage) );.
15a55 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
15a56 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 OK;.}../*.** Dec
15a57 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 ode the flags by
15a58 74 65 20 28 74 68 65 20 66 69 72 73 74 20 62 79 te (the first by
15a59 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 te of the header
15a5a 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 ) for a page.**
15a5b 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 and initialize f
15a5c 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d ields of the Mem
15a5d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 20 61 Page structure a
15a5e 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a ccordingly..**.*
15a5f 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f * Only the follo
15a60 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e wing combination
15a61 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e s are supported.
15a62 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 Anything diffe
15a63 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 rent.** indicate
15a64 73 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 s a corrupt data
15a65 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a base files:.**.*
15a66 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 * PTF_ZE
15a67 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 RODATA.**
15a68 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c PTF_ZERODATA |
15a69 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 PTF_LEAF.**
15a6a 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 PTF_LEAFDAT
15a6b 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a A | PTF_INTKEY.*
15a6c 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 * PTF_LE
15a6d 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 AFDATA | PTF_INT
15a6e 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a KEY | PTF_LEAF.*
15a6f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 /.static int dec
15a70 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 odeFlags(MemPage
15a71 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 *pPage, int fla
15a72 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 gByte){. BtShar
15a73 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 ed *pBt; /*
15a74 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d A copy of pPage-
15a75 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 >pBt */.. asser
15a76 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 t( pPage->hdrOff
15a77 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e set==(pPage->pgn
15a78 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 o==1 ? 100 : 0)
15a79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
15a7a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
15a7b 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
15a7c 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c x) );. pPage->l
15a7d 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 eaf = (u8)(flagB
15a7e 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 yte>>3); assert
15a7f 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c ( PTF_LEAF == 1<
15a80 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 <3 );. flagByte
15a81 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 &= ~PTF_LEAF;.
15a82 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 pPage->childPtr
15a83 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 Size = 4-4*pPage
15a84 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 ->leaf;. pBt =
15a85 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 pPage->pBt;. if
15a86 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 ( flagByte==(PTF
15a87 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f _LEAFDATA | PTF_
15a88 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 INTKEY) ){. p
15a89 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 Page->intKey = 1
15a8a 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 ;. pPage->has
15a8b 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 Data = pPage->le
15a8c 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d af;. pPage->m
15a8d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d axLocal = pBt->m
15a8e 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 axLeaf;. pPag
15a8f 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 e->minLocal = pB
15a90 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 t->minLeaf;. }e
15a91 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 lse if( flagByte
15a92 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 ==PTF_ZERODATA )
15a93 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 {. pPage->int
15a94 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 Key = 0;. pPa
15a95 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b ge->hasData = 0;
15a96 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c . pPage->maxL
15a97 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c ocal = pBt->maxL
15a98 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d ocal;. pPage-
15a99 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d >minLocal = pBt-
15a9a 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c >minLocal;. }el
15a9b 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 se{. return S
15a9c 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15a9d 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e PT;. }. return
15a9e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
15a9f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 *.** Initialize
15aa0 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e the auxiliary in
15aa1 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 formation for a
15aa2 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a disk block..**.*
15aa3 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f * Return SQLITE_
15aa4 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 OK on success.
15aa5 49 66 20 77 65 20 73 65 65 20 74 68 61 74 20 74 If we see that t
15aa6 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 he page does.**
15aa7 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 not contain a we
15aa8 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 ll-formed databa
15aa9 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 se page, then re
15aaa 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f turn .** SQLITE_
15aab 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 CORRUPT. Note t
15aac 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 hat a return of
15aad 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e SQLITE_OK does n
15aae 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 ot.** guarantee
15aaf 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 that the page is
15ab0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 well-formed. I
15ab1 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 t only shows tha
15ab2 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 t.** we failed t
15ab3 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 o detect any cor
15ab4 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 ruption..*/.stat
15ab5 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 ic int btreeInit
15ab6 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 Page(MemPage *pP
15ab7 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 age){.. assert(
15ab8 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 pPage->pBt!=0 )
15ab9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
15aba 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
15abb 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 Page->pBt->mutex
15abc 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
15abd 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 Page->pgno==sqli
15abe 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 te3PagerPagenumb
15abf 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 er(pPage->pDbPag
15ac0 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 e) );. assert(
15ac1 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 pPage == sqlite3
15ac2 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50 PagerGetExtra(pP
15ac3 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b age->pDbPage) );
15ac4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
15ac5 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 ->aData == sqlit
15ac6 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 e3PagerGetData(p
15ac7 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 Page->pDbPage) )
15ac8 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d ;.. if( !pPage-
15ac9 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 >isInit ){. u
15aca 31 36 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 16 pc;
15acb 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
15acc 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 a freeblock with
15acd 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b in pPage->aData[
15ace 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b ] */. u8 hdr;
15acf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
15ad0 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 ffset to beginni
15ad1 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 ng of page heade
15ad2 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 r */. u8 *dat
15ad3 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 a; /* E
15ad4 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 qual to pPage->a
15ad5 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 Data */. BtSh
15ad6 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 ared *pBt;
15ad7 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 /* The main bt
15ad8 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f ree structure */
15ad9 0a 20 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 . u16 usableS
15ada 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e ize; /* Amoun
15adb 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 t of usable spac
15adc 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a e on each page *
15add 2f 0a 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 /. u16 cellOf
15ade 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 fset; /* Offs
15adf 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 et from start of
15ae0 20 70 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 page to first c
15ae1 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 ell pointer */.
15ae2 20 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 u16 nFree;
15ae3 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
15ae4 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 of unused bytes
15ae5 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 on the page */.
15ae6 20 20 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 u16 top;
15ae7 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 /* First b
15ae8 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 yte of the cell
15ae9 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a content area */.
15aea 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 int iCellFir
15aeb 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 st; /* First
15aec 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f allowable cell o
15aed 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 r freeblock offs
15aee 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 et */. int iC
15aef 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 ellLast; /*
15af0 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 Last possible ce
15af1 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 ll or freeblock
15af2 6f 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 offset */.. p
15af3 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
15af4 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 .. hdr = pPag
15af5 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
15af6 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e data = pPage->
15af7 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 aData;. if( d
15af8 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 ecodeFlags(pPage
15af9 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 , data[hdr]) ) r
15afa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
15afb 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 RUPT_BKPT;. a
15afc 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 ssert( pBt->page
15afd 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 Size>=512 && pBt
15afe 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 ->pageSize<=3276
15aff 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 8 );. pPage->
15b00 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e maskPage = pBt->
15b01 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 pageSize - 1;.
15b02 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c pPage->nOverfl
15b03 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 ow = 0;. usab
15b04 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 leSize = pBt->us
15b05 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 ableSize;. pP
15b06 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 age->cellOffset
15b07 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 = cellOffset = h
15b08 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 dr + 12 - 4*pPag
15b09 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 e->leaf;. top
15b0a 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
15b0b 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 a[hdr+5]);. p
15b0c 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 Page->nCell = ge
15b0d 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
15b0e 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 +3]);. if( pP
15b0f 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 age->nCell>MX_CE
15b10 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 LL(pBt) ){.
15b11 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c /* To many cell
15b12 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 s for a single p
15b13 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d age. The page m
15b14 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a ust be corrupt *
15b15 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 /. return S
15b16 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15b17 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 PT;. }. te
15b18 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e stcase( pPage->n
15b19 43 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 Cell==MX_CELL(pB
15b1a 74 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 t) );.. /* A
15b1b 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 malformed databa
15b1c 73 65 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 se page might ca
15b1d 75 73 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 use us to read p
15b1e 61 73 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 ast the end.
15b1f 2a 2a 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 ** of page when
15b20 70 61 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 parsing a cell.
15b21 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 . **. **
15b22 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c The following bl
15b23 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 ock of code chec
15b24 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 ks early to see
15b25 69 66 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 if a cell extend
15b26 73 0a 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 s. ** past th
15b27 65 20 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 e end of a page
15b28 62 6f 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 boundary and cau
15b29 73 65 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 ses SQLITE_CORRU
15b2a 50 54 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a PT to be . **
15b2b 20 72 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 returned if it
15b2c 64 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 does.. */.
15b2d 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 iCellFirst = ce
15b2e 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 llOffset + 2*pPa
15b2f 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 ge->nCell;. i
15b30 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c CellLast = usabl
15b31 65 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 eSize - 4;.#if d
15b32 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e efined(SQLITE_EN
15b33 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 ABLE_OVERSIZE_CE
15b34 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a LL_CHECK). {.
15b35 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 int i;
15b36 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
15b37 20 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 into the cell p
15b38 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a ointer array */.
15b39 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 int sz;
15b3a 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 /* Size
15b3b 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 of a cell */..
15b3c 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
15b3d 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 leaf ) iCellLast
15b3e 2d 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d --;. for(i=
15b3f 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 0; i<pPage->nCel
15b40 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 l; i++){.
15b41 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 pc = get2byte(&
15b42 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b data[cellOffset+
15b43 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 i*2]);. t
15b44 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 estcase( pc==iCe
15b45 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 llFirst );.
15b46 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d testcase( pc=
15b47 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 =iCellLast );.
15b48 20 20 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 if( pc<iCe
15b49 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 llFirst || pc>iC
15b4a 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 ellLast ){.
15b4b 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 return SQLI
15b4c 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
15b4d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
15b4e 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 sz = cellSize
15b4f 50 74 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 Ptr(pPage, &data
15b50 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 [pc]);. t
15b51 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d estcase( pc+sz==
15b52 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 usableSize );.
15b53 20 20 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e if( pc+sz>
15b54 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 usableSize ){.
15b55 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 return S
15b56 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
15b57 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 PT;. }.
15b58 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
15b59 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 !pPage->leaf ) i
15b5a 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 CellLast++;.
15b5b 7d 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 } .#endif..
15b5c 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 /* Compute the t
15b5d 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 otal free space
15b5e 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 on the page */.
15b5f 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 pc = get2byte
15b60 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a (&data[hdr+1]);.
15b61 20 20 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 nFree = data
15b62 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 [hdr+7] + top;.
15b63 20 20 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 while( pc>0 )
15b64 7b 0a 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 {. u16 next
15b65 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 , size;. if
15b66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 ( pc<iCellFirst
15b67 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 || pc>iCellLast
15b68 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 ){. /* St
15b69 61 72 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 art of free bloc
15b6a 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 k is off the pag
15b6b 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 e */. ret
15b6c 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15b6d 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 PT_BKPT; .
15b6e 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 }. next = g
15b6f 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 et2byte(&data[pc
15b70 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d ]);. size =
15b71 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
15b72 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 pc+2]);. if
15b73 28 20 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 ( (next>0 && nex
15b74 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c t<=pc+size+3) ||
15b75 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 pc+size>usableS
15b76 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f ize ){. /
15b77 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 * Free blocks mu
15b78 73 74 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 st be in ascendi
15b79 6e 67 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 ng order. And th
15b7a 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 09 e last byte of..
15b7b 2a 2a 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 ** the free-bloc
15b7c 6b 20 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 k must lie on th
15b7d 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e e database page.
15b7e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 */. ret
15b7f 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
15b80 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 PT_BKPT; .
15b81 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 }. nFree =
15b82 6e 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 nFree + size;.
15b83 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 pc = next;.
15b84 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 }.. /* At
15b85 74 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 this point, nFre
15b86 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 e contains the s
15b87 75 6d 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 um of the offset
15b88 20 74 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 to the start.
15b89 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c ** of the cell
15b8a 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c -content area pl
15b8b 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 us the number of
15b8c 20 66 72 65 65 20 62 79 74 65 73 20 77 69 74 68 free bytes with
15b8d 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 in. ** the ce
15b8e 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e ll-content area.
15b8f 20 49 66 20 74 68 69 73 20 69 73 20 67 72 65 61 If this is grea
15b90 74 65 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 ter than the usa
15b91 62 6c 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 ble-size. **
15b92 6f 66 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 of the page, the
15b93 6e 20 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 n the page must
15b94 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 be corrupted. Th
15b95 69 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 is check also.
15b96 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 ** serves to v
15b97 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6f erify that the o
15b98 66 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 ffset to the sta
15b99 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 rt of the cell-c
15b9a 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 ontent. ** ar
15b9b 65 61 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f ea, according to
15b9c 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72 the page header
15b9d 2c 20 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 , lies within th
15b9e 65 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 e page.. */.
15b9f 20 20 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 if( nFree>usa
15ba0 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 bleSize ){.
15ba1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15ba2 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 ORRUPT_BKPT; .
15ba3 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e }. pPage->n
15ba4 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 Free = (u16)(nFr
15ba5 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 ee - iCellFirst)
15ba6 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 ;. pPage->isI
15ba7 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 nit = 1;. }. r
15ba8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
15ba9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 .}../*.** Set up
15baa 20 61 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 a raw page so t
15bab 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b hat it looks lik
15bac 65 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 e a database pag
15bad 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 e holding.** no
15bae 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 entries..*/.stat
15baf 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 ic void zeroPage
15bb0 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
15bb1 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 int flags){. u
15bb2 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 nsigned char *da
15bb3 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
15bb4 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 a;. BtShared *p
15bb5 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b Bt = pPage->pBt;
15bb6 0a 20 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 . u8 hdr = pPag
15bb7 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 e->hdrOffset;.
15bb8 75 31 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 u16 first;.. as
15bb9 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
15bba 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 erPagenumber(pPa
15bbb 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 ge->pDbPage)==pP
15bbc 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 age->pgno );. a
15bbd 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15bbe 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 gerGetExtra(pPag
15bbf 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 e->pDbPage) == (
15bc0 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 void*)pPage );.
15bc1 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15bc2 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 PagerGetData(pPa
15bc3 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 ge->pDbPage) ==
15bc4 64 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 data );. assert
15bc5 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
15bc6 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
15bc7 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
15bc8 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
15bc9 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
15bca 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d utex) );. /*mem
15bcb 73 65 74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20 set(&data[hdr],
15bcc 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 0, pBt->usableSi
15bcd 7a 65 20 2d 20 68 64 72 29 3b 2a 2f 0a 20 20 64 ze - hdr);*/. d
15bce 61 74 61 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 ata[hdr] = (char
15bcf 29 66 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 )flags;. first
15bd0 3d 20 68 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 = hdr + 8 + 4*((
15bd1 66 6c 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d flags&PTF_LEAF)=
15bd2 3d 30 20 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 =0 ?1:0);. mems
15bd3 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c et(&data[hdr+1],
15bd4 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 0, 4);. data[h
15bd5 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 dr+7] = 0;. put
15bd6 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
15bd7 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 5], pBt->usableS
15bd8 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e ize);. pPage->n
15bd9 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 Free = pBt->usab
15bda 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a leSize - first;.
15bdb 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 decodeFlags(pP
15bdc 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 age, flags);. p
15bdd 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 Page->hdrOffset
15bde 3d 20 68 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e = hdr;. pPage->
15bdf 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 cellOffset = fir
15be0 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 st;. pPage->nOv
15be1 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 erflow = 0;. as
15be2 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 sert( pBt->pageS
15be3 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d ize>=512 && pBt-
15be4 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 >pageSize<=32768
15be5 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 );. pPage->mas
15be6 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 kPage = pBt->pag
15be7 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 eSize - 1;. pPa
15be8 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 ge->nCell = 0;.
15be9 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d pPage->isInit =
15bea 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 1;.}.../*.** Co
15beb 6e 76 65 72 74 20 61 20 44 62 50 61 67 65 20 6f nvert a DbPage o
15bec 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 btained from the
15bed 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 pager into a Me
15bee 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a 2a 2a mPage used by.**
15bef 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 the btree layer
15bf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 ..*/.static MemP
15bf1 61 67 65 20 2a 62 74 72 65 65 50 61 67 65 46 72 age *btreePageFr
15bf2 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67 65 20 omDbPage(DbPage
15bf3 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 *pDbPage, Pgno p
15bf4 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20 2a 70 gno, BtShared *p
15bf5 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a Bt){. MemPage *
15bf6 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 pPage = (MemPage
15bf7 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 *)sqlite3PagerGe
15bf8 74 45 78 74 72 61 28 70 44 62 50 61 67 65 29 3b tExtra(pDbPage);
15bf9 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 . pPage->aData
15bfa 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 = sqlite3PagerGe
15bfb 74 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a tData(pDbPage);.
15bfc 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 pPage->pDbPage
15bfd 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 70 50 = pDbPage;. pP
15bfe 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a age->pBt = pBt;.
15bff 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 pPage->pgno =
15c00 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 pgno;. pPage->h
15c01 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 drOffset = pPage
15c02 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 ->pgno==1 ? 100
15c03 3a 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 : 0;. return pP
15c04 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 age; .}../*.** G
15c05 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 et a page from t
15c06 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69 74 69 he pager. Initi
15c07 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 alize the MemPag
15c08 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d e.pBt and.** Mem
15c09 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 Page.aData eleme
15c0a 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a nts if needed..*
15c0b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f *.** If the noCo
15c0c 6e 74 65 6e 74 20 66 6c 61 67 20 69 73 20 73 65 ntent flag is se
15c0d 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 t, it means that
15c0e 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 we do not care
15c0f 61 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e about.** the con
15c10 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67 65 tent of the page
15c11 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 20 at this time.
15c12 53 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 So do not go to
15c13 74 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 the disk.** to f
15c14 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 etch the content
15c15 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 . Just fill in
15c16 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 the content with
15c17 20 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a zeros for now..
15c18 2a 2a 20 49 66 20 69 6e 20 74 68 65 20 66 75 74 ** If in the fut
15c19 75 72 65 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 ure we call sqli
15c1a 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 te3PagerWrite()
15c1b 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 74 68 on this page, th
15c1c 61 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 at.** means we h
15c1d 61 76 65 20 73 74 61 72 74 65 64 20 74 6f 20 62 ave started to b
15c1e 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 e concerned abou
15c1f 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 t content and th
15c20 65 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 e disk.** read s
15c21 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74 20 74 hould occur at t
15c22 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 hat point..*/.st
15c23 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 47 65 atic int btreeGe
15c24 74 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 tPage(. BtShare
15c25 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 2f 2a d *pBt, /*
15c26 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 The btree */.
15c27 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 Pgno pgno,
15c28 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
15c29 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 66 65 f the page to fe
15c2a 74 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 tch */. MemPage
15c2b 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a **ppPage, /*
15c2c 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 Return the page
15c2d 20 69 6e 20 74 68 69 73 20 70 61 72 61 6d 65 74 in this paramet
15c2e 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f er */. int noCo
15c2f 6e 74 65 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 ntent /*
15c30 44 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 Do not load page
15c31 20 63 6f 6e 74 65 6e 74 20 69 66 20 74 72 75 65 content if true
15c32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b */.){. int rc;
15c33 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 . DbPage *pDbPa
15c34 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 ge;.. assert( s
15c35 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
15c36 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b d(pBt->mutex) );
15c37 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 . rc = sqlite3P
15c38 61 67 65 72 41 63 71 75 69 72 65 28 70 42 74 2d agerAcquire(pBt-
15c39 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 >pPager, pgno, (
15c3a 44 62 50 61 67 65 2a 2a 29 26 70 44 62 50 61 67 DbPage**)&pDbPag
15c3b 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 e, noContent);.
15c3c 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
15c3d 20 72 63 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d rc;. *ppPage =
15c3e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44 62 btreePageFromDb
15c3f 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70 67 Page(pDbPage, pg
15c40 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65 74 75 no, pBt);. retu
15c41 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
15c42 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 ./*.** Retrieve
15c43 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 a page from the
15c44 70 61 67 65 72 20 63 61 63 68 65 2e 20 49 66 20 pager cache. If
15c45 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 the requested pa
15c46 67 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 ge is not.** alr
15c47 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65 eady in the page
15c48 72 20 63 61 63 68 65 20 72 65 74 75 72 6e 20 4e r cache return N
15c49 55 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 ULL. Initialize
15c4a 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 the MemPage.pBt
15c4b 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 and.** MemPage.a
15c4c 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 Data elements if
15c4d 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 needed..*/.stat
15c4e 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 ic MemPage *btre
15c4f 65 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 ePageLookup(BtSh
15c50 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 ared *pBt, Pgno
15c51 70 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 pgno){. DbPage
15c52 2a 70 44 62 50 61 67 65 3b 0a 20 20 61 73 73 65 *pDbPage;. asse
15c53 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
15c54 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 x_held(pBt->mute
15c55 78 29 20 29 3b 0a 20 20 70 44 62 50 61 67 65 20 x) );. pDbPage
15c56 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f = sqlite3PagerLo
15c57 6f 6b 75 70 28 70 42 74 2d 3e 70 50 61 67 65 72 okup(pBt->pPager
15c58 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 , pgno);. if( p
15c59 44 62 50 61 67 65 20 29 7b 0a 20 20 20 20 72 65 DbPage ){. re
15c5a 74 75 72 6e 20 62 74 72 65 65 50 61 67 65 46 72 turn btreePageFr
15c5b 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61 67 65 omDbPage(pDbPage
15c5c 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 , pgno, pBt);.
15c5d 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
15c5e 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
15c5f 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 e size of the da
15c60 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 70 tabase file in p
15c61 61 67 65 73 2e 20 49 66 20 74 68 65 72 65 20 69 ages. If there i
15c62 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a s any kind of.**
15c63 20 65 72 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 error, return (
15c64 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 (unsigned int)-1
15c65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e )..*/.static Pgn
15c66 6f 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 o pagerPagecount
15c67 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
15c68 0a 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d . int nPage = -
15c69 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 1;. int rc;. a
15c6a 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 ssert( pBt->pPag
15c6b 65 31 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c e1 );. rc = sql
15c6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
15c6d 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 nt(pBt->pPager,
15c6e 26 6e 50 61 67 65 29 3b 0a 20 20 61 73 73 65 72 &nPage);. asser
15c6f 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
15c70 20 7c 7c 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b || nPage==-1 );
15c71 0a 20 20 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 . return (Pgno)
15c72 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 nPage;.}../*.**
15c73 47 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d 20 Get a page from
15c74 74 68 65 20 70 61 67 65 72 20 61 6e 64 20 69 6e the pager and in
15c75 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 itialize it. Th
15c76 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 is routine is ju
15c77 73 74 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 st a.** convenie
15c78 6e 63 65 20 77 72 61 70 70 65 72 20 61 72 6f 75 nce wrapper arou
15c79 6e 64 20 73 65 70 61 72 61 74 65 20 63 61 6c 6c nd separate call
15c7a 73 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 s to btreeGetPag
15c7b 65 28 29 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 e() and .** btre
15c7c 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a eInitPage()..**.
15c7d 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f ** If an error o
15c7e 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 ccurs, then the
15c7f 76 61 6c 75 65 20 2a 70 70 50 61 67 65 20 69 73 value *ppPage is
15c80 20 73 65 74 20 74 6f 20 69 73 20 75 6e 64 65 66 set to is undef
15c81 69 6e 65 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 ined. It.** may
15c82 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 remain unchanged
15c83 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20 73 , or it may be s
15c84 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 et to an invalid
15c85 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 value..*/.stati
15c86 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 c int getAndInit
15c87 50 61 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 Page(. BtShared
15c88 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 *pBt,
15c89 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
15c8a 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 file */. Pgno p
15c8b 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f gno, /
15c8c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 * Number of the
15c8d 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 page to get */.
15c8e 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 MemPage **ppPag
15c8f 65 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 e /* Write t
15c90 68 65 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 he page pointer
15c91 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 here */.){. int
15c92 20 72 63 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 rc;. TESTONLY(
15c93 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 Pgno iLastPg =
15c94 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
15c95 42 74 29 3b 20 29 0a 20 20 61 73 73 65 72 74 28 Bt); ). assert(
15c96 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15c97 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
15c98 29 3b 0a 0a 20 20 72 63 20 3d 20 62 74 72 65 65 );.. rc = btree
15c99 47 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e GetPage(pBt, pgn
15c9a 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 o, ppPage, 0);.
15c9b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
15c9c 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 OK ){. rc = b
15c9d 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a 70 70 treeInitPage(*pp
15c9e 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 Page);. if( r
15c9f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
15ca0 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
15ca1 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 e(*ppPage);.
15ca2 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 }. }.. /* If t
15ca3 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 he requested pag
15ca4 65 20 6e 75 6d 62 65 72 20 77 61 73 20 65 69 74 e number was eit
15ca5 68 65 72 20 30 20 6f 72 20 67 72 65 61 74 65 72 her 0 or greater
15ca6 20 74 68 61 6e 20 74 68 65 20 70 61 67 65 0a 20 than the page.
15ca7 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 ** number of th
15ca8 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20 74 e last page in t
15ca9 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 he database, thi
15caa 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c s function shoul
15cab 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 d return. ** SQ
15cac 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 LITE_CORRUPT or
15cad 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 some other error
15cae 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 (i.e. SQLITE_FU
15caf 4c 4c 29 2e 20 43 68 65 63 6b 20 74 68 61 74 20 LL). Check that
15cb0 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 this. ** is the
15cb1 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 case. */. ass
15cb2 65 72 74 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 ert( (pgno>0 &&
15cb3 70 67 6e 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c pgno<=iLastPg) |
15cb4 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 | rc!=SQLITE_OK
15cb5 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
15cb6 67 6e 6f 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 gno==0 );. test
15cb7 63 61 73 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 case( pgno==iLas
15cb8 74 50 67 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e tPg );.. return
15cb9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rc;.}../*.** Re
15cba 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67 65 2e lease a MemPage.
15cbb 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 This should be
15cbc 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 called once for
15cbd 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 each prior.** c
15cbe 61 6c 6c 20 74 6f 20 62 74 72 65 65 47 65 74 50 all to btreeGetP
15cbf 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 age..*/.static v
15cc0 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 oid releasePage(
15cc1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b MemPage *pPage){
15cc2 0a 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a . if( pPage ){.
15cc3 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
15cc4 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 e->nOverflow==0
15cc5 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 || sqlite3PagerP
15cc6 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67 ageRefcount(pPag
15cc7 65 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 3b e->pDbPage)>1 );
15cc8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
15cc9 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 ge->aData );.
15cca 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
15ccb 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 pBt );. asser
15ccc 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 t( sqlite3PagerG
15ccd 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70 etExtra(pPage->p
15cce 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 DbPage) == (void
15ccf 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 20 20 61 *)pPage );. a
15cd0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 ssert( sqlite3Pa
15cd1 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 gerGetData(pPage
15cd2 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 ->pDbPage)==pPag
15cd3 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 e->aData );.
15cd4 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15cd5 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
15cd6 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
15cd7 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 . sqlite3Page
15cd8 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e 70 44 rUnref(pPage->pD
15cd9 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f bPage);. }.}../
15cda 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20 72 6f *.** During a ro
15cdb 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 llback, when the
15cdc 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73 20 69 pager reloads i
15cdd 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 nformation into
15cde 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 the cache.** so
15cdf 74 68 61 74 20 74 68 65 20 63 61 63 68 65 20 69 that the cache i
15ce0 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20 69 74 s restored to it
15ce1 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 s original state
15ce2 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 at the start of
15ce3 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 .** the transact
15ce4 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20 70 61 ion, for each pa
15ce5 67 65 20 72 65 73 74 6f 72 65 64 20 74 68 69 73 ge restored this
15ce6 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c routine is call
15ce7 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ed..**.** This r
15ce8 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 outine needs to
15ce9 72 65 73 65 74 20 74 68 65 20 65 78 74 72 61 20 reset the extra
15cea 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61 74 20 data section at
15ceb 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a the end of the.*
15cec 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65 65 20 * page to agree
15ced 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65 with the restore
15cee 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 d data..*/.stati
15cef 63 20 76 6f 69 64 20 70 61 67 65 52 65 69 6e 69 c void pageReini
15cf0 74 28 44 62 50 61 67 65 20 2a 70 44 61 74 61 29 t(DbPage *pData)
15cf1 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 {. MemPage *pPa
15cf2 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d ge;. pPage = (M
15cf3 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 emPage *)sqlite3
15cf4 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 44 PagerGetExtra(pD
15cf5 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ata);. assert(
15cf6 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
15cf7 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e Refcount(pData)>
15cf8 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 0 );. if( pPage
15cf9 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 ->isInit ){.
15cfa 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
15cfb 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
15cfc 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
15cfd 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e . pPage->isIn
15cfe 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 it = 0;. if(
15cff 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 sqlite3PagerPage
15d00 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e Refcount(pData)>
15d01 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 1 ){. /* pP
15d02 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 age might not be
15d03 20 61 20 62 74 72 65 65 20 70 61 67 65 3b 20 20 a btree page;
15d04 69 74 20 6d 69 67 68 74 20 62 65 20 61 6e 20 6f it might be an o
15d05 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 verflow page.
15d06 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 ** or ptrmap
15d07 70 61 67 65 20 6f 72 20 61 20 66 72 65 65 20 70 page or a free p
15d08 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 age. In those c
15d09 61 73 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 ases, the follow
15d0a 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c ing. ** cal
15d0b 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 74 50 61 l to btreeInitPa
15d0c 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 ge() will likely
15d0d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 return SQLITE_C
15d0e 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a ORRUPT.. **
15d0f 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 But no harm is
15d10 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20 20 41 done by this. A
15d11 6e 64 20 69 74 20 69 73 20 76 65 72 79 20 69 6d nd it is very im
15d12 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20 20 20 portant that.
15d13 20 20 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 ** btreeInitP
15d14 61 67 65 28 29 20 62 65 20 63 61 6c 6c 65 64 20 age() be called
15d15 6f 6e 20 65 76 65 72 79 20 62 74 72 65 65 20 70 on every btree p
15d16 61 67 65 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 age so we make.
15d17 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c ** the call
15d18 20 66 6f 72 20 65 76 65 72 79 20 70 61 67 65 20 for every page
15d19 74 68 61 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f that comes in fo
15d1a 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f r re-initing. */
15d1b 0a 20 20 20 20 20 20 62 74 72 65 65 49 6e 69 74 . btreeInit
15d1c 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
15d1d 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
15d1e 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 Invoke the busy
15d1f 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 62 74 handler for a bt
15d20 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ree..*/.static i
15d21 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 nt btreeInvokeBu
15d22 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a syHandler(void *
15d23 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61 72 65 pArg){. BtShare
15d24 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68 61 72 d *pBt = (BtShar
15d25 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73 73 65 ed*)pArg;. asse
15d26 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 rt( pBt->db );.
15d27 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
15d28 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d _mutex_held(pBt-
15d29 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
15d2a 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 return sqlite3I
15d2b 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
15d2c 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 (&pBt->db->busyH
15d2d 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a andler);.}../*.*
15d2e 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62 61 73 * Open a databas
15d2f 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a e file..** .** z
15d30 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 Filename is the
15d31 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 name of the data
15d32 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 7a base file. If z
15d33 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c Filename is NULL
15d34 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61 62 61 .** a new databa
15d35 73 65 20 77 69 74 68 20 61 20 72 61 6e 64 6f 6d se with a random
15d36 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74 65 64 name is created
15d37 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 . This randomly
15d38 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 named.** databa
15d39 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 se file will be
15d3a 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73 71 6c deleted when sql
15d3b 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 29 ite3BtreeClose()
15d3c 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 is called..** I
15d3d 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 f zFilename is "
15d3e 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 :memory:" then a
15d3f 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 n in-memory data
15d40 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64 0a base is created.
15d41 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d ** that is autom
15d42 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 atically destroy
15d43 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c ed when it is cl
15d44 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 osed..**.** If t
15d45 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 he database is a
15d46 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 69 6e lready opened in
15d47 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 the same databa
15d48 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a se connection.**
15d49 20 61 6e 64 20 77 65 20 61 72 65 20 69 6e 20 73 and we are in s
15d4a 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65 hared cache mode
15d4b 2c 20 74 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 , then the open
15d4c 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 will fail with a
15d4d 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 n.** SQLITE_CONS
15d4e 54 52 41 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 TRAINT error. W
15d4f 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 e cannot allow t
15d50 77 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 wo or more BtSha
15d51 72 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 red.** objects i
15d52 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 n the same datab
15d53 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 ase connection s
15d54 69 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 ince doing so wi
15d55 6c 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 ll lead.** to pr
15d56 6f 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b oblems with lock
15d57 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ing..*/.SQLITE_P
15d58 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
15d59 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 e3BtreeOpen(. c
15d5a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 onst char *zFile
15d5b 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f name, /* Name o
15d5c 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 f the file conta
15d5d 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 20 ining the BTree
15d5e 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 database */. sq
15d5f 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
15d60 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 /* Associa
15d61 74 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e ted database han
15d62 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a dle */. Btree *
15d63 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20 20 20 *ppBtree,
15d64 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e /* Pointer to n
15d65 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 ew Btree object
15d66 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a written here */.
15d67 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 int flags,
15d68 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 /* Opt
15d69 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 ions */. int vf
15d6a 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 sFlags
15d6b 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 /* Flags passe
15d6c 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c d through to sql
15d6d 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 ite3_vfs.xOpen()
15d6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 */.){. sqlite3
15d6f 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 _vfs *pVfs;
15d70 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 /* The V
15d71 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 FS to use for th
15d72 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 is btree */. Bt
15d73 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 30 3b Shared *pBt = 0;
15d74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
15d75 53 68 61 72 65 64 20 70 61 72 74 20 6f 66 20 62 Shared part of b
15d76 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a tree structure *
15d77 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 /. Btree *p;
15d78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d79 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 /* Handle to
15d7a 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 return */. sqli
15d7b 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
15d7c 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 Open = 0; /* Pr
15d7d 65 76 65 6e 74 73 20 61 20 72 61 63 65 20 63 6f events a race co
15d7e 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 ndition. Ticket
15d7f 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 #3537 */. int r
15d80 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 c = SQLITE_OK;
15d81 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
15d82 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 ult code from th
15d83 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 is function */.
15d84 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20 20 20 u8 nReserve;
15d85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15d86 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75 73 65 /* Byte of unuse
15d87 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63 68 20 d space on each
15d88 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e page */. unsign
15d89 65 64 20 63 68 61 72 20 7a 44 62 48 65 61 64 65 ed char zDbHeade
15d8a 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 r[100]; /* Data
15d8b 62 61 73 65 20 68 65 61 64 65 72 20 63 6f 6e 74 base header cont
15d8c 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 ent */.. /* Set
15d8d 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 the variable is
15d8e 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f Memdb to true fo
15d8f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 r an in-memory d
15d90 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a atabase, or . *
15d91 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 * false for a fi
15d92 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 le-based databas
15d93 65 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 e. This symbol i
15d94 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 s only required
15d95 69 66 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f if. ** either o
15d96 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 f the shared-dat
15d97 61 20 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 a or autovacuum
15d98 66 65 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d features are com
15d99 70 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f piled . ** into
15d9a 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 the library..
15d9b 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 */.#if !defined(
15d9c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
15d9d 45 44 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 ED_CACHE) || !de
15d9e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
15d9f 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 T_AUTOVACUUM).
15da0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d #ifdef SQLITE_OM
15da1 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 IT_MEMORYDB.
15da2 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 const int isMemd
15da3 62 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 b = 0;. #else.
15da4 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d const int isM
15da5 65 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 emdb = zFilename
15da6 20 26 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c && !strcmp(zFil
15da7 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a ename, ":memory:
15da8 22 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e ");. #endif.#en
15da9 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 dif.. assert( d
15daa 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 b!=0 );. assert
15dab 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
15dac 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 held(db->mutex)
15dad 29 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d );.. pVfs = db-
15dae 3e 70 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c >pVfs;. p = sql
15daf 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 ite3MallocZero(s
15db0 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 izeof(Btree));.
15db1 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 if( !p ){. r
15db2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
15db3 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 EM;. }. p->inT
15db4 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e rans = TRANS_NON
15db5 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b E;. p->db = db;
15db6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15db7 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 OMIT_SHARED_CACH
15db8 45 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 E. p->lock.pBtr
15db9 65 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 ee = p;. p->loc
15dba 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 k.iTable = 1;.#e
15dbb 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e ndif..#if !defin
15dbc 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
15dbd 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 HARED_CACHE) &&
15dbe 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
15dbf 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f OMIT_DISKIO). /
15dc0 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20 42 *. ** If this B
15dc1 74 72 65 65 20 69 73 20 61 20 63 61 6e 64 69 64 tree is a candid
15dc2 61 74 65 20 66 6f 72 20 73 68 61 72 65 64 20 63 ate for shared c
15dc3 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66 69 6e ache, try to fin
15dc4 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 d an. ** existi
15dc5 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 ng BtShared obje
15dc6 63 74 20 74 68 61 74 20 77 65 20 63 61 6e 20 73 ct that we can s
15dc7 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 hare with. */.
15dc8 20 69 66 28 20 69 73 4d 65 6d 64 62 3d 3d 30 20 if( isMemdb==0
15dc9 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 && zFilename &&
15dca 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a zFilename[0] ){.
15dcb 20 20 20 20 69 66 28 20 76 66 73 46 6c 61 67 73 if( vfsFlags
15dcc 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 & SQLITE_OPEN_S
15dcd 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a 20 20 HAREDCACHE ){.
15dce 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 int nFullPat
15dcf 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 hname = pVfs->mx
15dd0 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 Pathname+1;.
15dd1 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 char *zFullPat
15dd2 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d hname = sqlite3M
15dd3 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e alloc(nFullPathn
15dd4 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ame);. sqli
15dd5 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 te3_mutex *mutex
15dd6 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 2d Shared;. p-
15dd7 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 >sharable = 1;.
15dd8 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 if( !zFullP
15dd9 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 athname ){.
15dda 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15ddb 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 p);. retu
15ddc 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
15ddd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 . }. s
15dde 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 qlite3OsFullPath
15ddf 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 name(pVfs, zFile
15de0 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e name, nFullPathn
15de1 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 ame, zFullPathna
15de2 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 me);. mutex
15de3 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 Open = sqlite3Mu
15de4 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f texAlloc(SQLITE_
15de5 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 MUTEX_STATIC_OPE
15de6 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 N);. sqlite
15de7 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 3_mutex_enter(mu
15de8 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 texOpen);.
15de9 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 mutexShared = sq
15dea 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 lite3MutexAlloc(
15deb 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 SQLITE_MUTEX_STA
15dec 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 TIC_MASTER);.
15ded 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
15dee 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68 61 72 _enter(mutexShar
15def 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 ed);. for(p
15df0 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 Bt=GLOBAL(BtShar
15df1 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 ed*,sqlite3Share
15df2 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70 42 74 dCacheList); pBt
15df3 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 ; pBt=pBt->pNext
15df4 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 ){. asser
15df5 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 t( pBt->nRef>0 )
15df6 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d ;. if( 0=
15df7 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 =strcmp(zFullPat
15df8 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 hname, sqlite3Pa
15df9 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d gerFilename(pBt-
15dfa 3e 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 >pPager)).
15dfb 20 20 20 20 20 20 20 20 20 20 20 26 26 20 73 71 && sq
15dfc 6c 69 74 65 33 50 61 67 65 72 56 66 73 28 70 42 lite3PagerVfs(pB
15dfd 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56 66 73 t->pPager)==pVfs
15dfe 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e ){. in
15dff 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 20 t iDb;.
15e00 20 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 for(iDb=db->nDb
15e01 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d -1; iDb>=0; iDb-
15e02 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 -){.
15e03 42 74 72 65 65 20 2a 70 45 78 69 73 74 69 6e 67 Btree *pExisting
15e04 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e = db->aDb[iDb].
15e05 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 pBt;.
15e06 20 69 66 28 20 70 45 78 69 73 74 69 6e 67 20 26 if( pExisting &
15e07 26 20 70 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 & pExisting->pBt
15e08 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ==pBt ){.
15e09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d sqlite3_m
15e0a 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
15e0b 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 20 Shared);.
15e0c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d sqlite3_m
15e0d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 utex_leave(mutex
15e0e 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 Open);.
15e0f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 sqlite3_fre
15e10 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 e(zFullPathname)
15e11 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
15e12 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
15e13 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
15e14 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e eturn SQLITE_CON
15e15 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 STRAINT;.
15e16 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
15e17 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e }. p->
15e18 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 pBt = pBt;.
15e19 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b pBt->nRef++
15e1a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 ;. brea
15e1b 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 k;. }.
15e1c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
15e1d 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d e3_mutex_leave(m
15e1e 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 utexShared);.
15e1f 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15e20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a zFullPathname);.
15e21 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c }.#ifdef SQL
15e22 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 65 6c ITE_DEBUG. el
15e23 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 se{. /* In
15e24 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d debug mode, we m
15e25 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73 74 65 ark all persiste
15e26 6e 74 20 64 61 74 61 62 61 73 65 73 20 61 73 20 nt databases as
15e27 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20 20 2a sharable. *
15e28 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 79 * even when they
15e29 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 are not. This
15e2a 65 78 65 72 63 69 73 65 73 20 74 68 65 20 6c 6f exercises the lo
15e2b 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 cking code and.
15e2c 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f ** gives mo
15e2d 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 re opportunity f
15e2e 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c 69 74 or asserts(sqlit
15e2f 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 e3_mutex_held())
15e30 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d . ** statem
15e31 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 ents to find loc
15e32 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 king problems..
15e33 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d */. p-
15e34 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 >sharable = 1;.
15e35 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a }.#endif. }.
15e36 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 42 74 #endif. if( pBt
15e37 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 ==0 ){. /*.
15e38 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
15e39 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b 65 20 ng asserts make
15e3a 73 75 72 65 20 74 68 61 74 20 73 74 72 75 63 74 sure that struct
15e3b 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68 65 ures used by the
15e3c 20 62 74 72 65 65 20 61 72 65 0a 20 20 20 20 2a btree are. *
15e3d 2a 20 74 68 65 20 72 69 67 68 74 20 73 69 7a 65 * the right size
15e3e 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20 67 75 . This is to gu
15e3f 61 72 64 20 61 67 61 69 6e 73 74 20 73 69 7a 65 ard against size
15e40 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 72 65 changes that re
15e41 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e sult. ** when
15e42 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 compiling on a
15e43 64 69 66 66 65 72 65 6e 74 20 61 72 63 68 69 74 different archit
15e44 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 ecture.. */.
15e45 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
15e46 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a f(i64)==8 || siz
15e47 65 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 eof(i64)==4 );.
15e48 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
15e49 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a f(u64)==8 || siz
15e4a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 eof(u64)==4 );.
15e4b 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
15e4c 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 f(u32)==4 );.
15e4d 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 assert( sizeof(
15e4e 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 u16)==2 );. a
15e4f 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 50 67 ssert( sizeof(Pg
15e50 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 no)==4 );. .
15e51 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 pBt = sqlite3Ma
15e52 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 llocZero( sizeof
15e53 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 (*pBt) );. if
15e54 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 ( pBt==0 ){.
15e55 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f rc = SQLITE_NO
15e56 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 MEM;. goto
15e57 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a btree_open_out;.
15e58 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 }. rc = s
15e59 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28 qlite3PagerOpen(
15e5a 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 pVfs, &pBt->pPag
15e5b 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 er, zFilename,.
15e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15e5d 20 20 20 20 20 20 20 20 20 45 58 54 52 41 5f 53 EXTRA_S
15e5e 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 IZE, flags, vfsF
15e5f 6c 61 67 73 2c 20 70 61 67 65 52 65 69 6e 69 74 lags, pageReinit
15e60 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 );. if( rc==S
15e61 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
15e62 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
15e63 67 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 gerReadFileheade
15e64 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 r(pBt->pPager,si
15e65 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 72 29 2c zeof(zDbHeader),
15e66 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 20 20 20 zDbHeader);.
15e67 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 }. if( rc!=SQ
15e68 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
15e69 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e goto btree_open
15e6a 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 _out;. }.
15e6b 70 42 74 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 pBt->db = db;.
15e6c 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 sqlite3PagerSe
15e6d 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70 42 74 tBusyhandler(pBt
15e6e 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 65 65 49 ->pPager, btreeI
15e6f 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 nvokeBusyHandler
15e70 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 , pBt);. p->p
15e71 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 Bt = pBt;. .
15e72 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 pBt->pCursor =
15e73 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 0;. pBt->pPag
15e74 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d e1 = 0;. pBt-
15e75 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 >readOnly = sqli
15e76 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e te3PagerIsreadon
15e77 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b ly(pBt->pPager);
15e78 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 . pBt->pageSi
15e79 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26 7a ze = get2byte(&z
15e7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 DbHeader[16]);.
15e7b 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 if( pBt->page
15e7c 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d Size<512 || pBt-
15e7d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 >pageSize>SQLITE
15e7e 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 _MAX_PAGE_SIZE.
15e7f 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42 74 || ((pBt
15e80 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70 42 ->pageSize-1)&pB
15e81 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30 20 t->pageSize)!=0
15e82 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 ){. pBt->pa
15e83 67 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e geSize = 0;.#ifn
15e84 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
15e85 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
15e86 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67 69 63 /* If the magic
15e87 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 name ":memory:"
15e88 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 will create an
15e89 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 in-memory databa
15e8a 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a se, then. *
15e8b 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75 74 6f * leave the auto
15e8c 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 Vacuum mode at 0
15e8d 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 (do not auto-va
15e8e 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 cuum), even if.
15e8f 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 ** SQLITE_D
15e90 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 EFAULT_AUTOVACUU
15e91 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 M is true. On th
15e92 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 66 e other hand, if
15e93 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 . ** SQLITE
15e94 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 _OMIT_MEMORYDB h
15e95 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65 64 2c as been defined,
15e96 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 then ":memory:"
15e97 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20 20 20 is just a.
15e98 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69 6c 65 ** regular file
15e99 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 -name. In this c
15e9a 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76 61 63 ase the auto-vac
15e9b 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73 20 70 uum applies as p
15e9c 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 er normal..
15e9d 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 46 */. if( zF
15e9e 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 ilename && !isMe
15e9f 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20 20 70 mdb ){. p
15ea0 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d Bt->autoVacuum =
15ea1 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 (SQLITE_DEFAULT
15ea2 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 _AUTOVACUUM ? 1
15ea3 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 : 0);. pB
15ea4 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
15ea5 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f (SQLITE_DEFAULT_
15ea6 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 AUTOVACUUM==2 ?
15ea7 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 1 : 0);. }.
15ea8 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e 52 65 #endif. nRe
15ea9 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d serve = 0;. }
15eaa 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 else{. nRes
15eab 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64 65 72 erve = zDbHeader
15eac 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d [20];. pBt-
15ead 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d >pageSizeFixed =
15eae 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 1;.#ifndef SQLI
15eaf 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
15eb0 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e 61 75 UM. pBt->au
15eb1 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 toVacuum = (get4
15eb2 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b byte(&zDbHeader[
15eb3 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 36 + 4*4])?1:0);
15eb4 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 . pBt->incr
15eb5 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 Vacuum = (get4by
15eb6 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 te(&zDbHeader[36
15eb7 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 + 7*4])?1:0);.#
15eb8 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 endif. }.
15eb9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
15eba 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 rSetPagesize(pBt
15ebb 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e ->pPager, &pBt->
15ebc 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 pageSize, nReser
15ebd 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ve);. if( rc
15ebe 29 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 ) goto btree_ope
15ebf 6e 5f 6f 75 74 3b 0a 20 20 20 20 70 42 74 2d 3e n_out;. pBt->
15ec0 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 usableSize = pBt
15ec1 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e 52 65 ->pageSize - nRe
15ec2 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72 serve;. asser
15ec3 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a t( (pBt->pageSiz
15ec4 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a e & 7)==0 ); /*
15ec5 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 8-byte alignmen
15ec6 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20 2a 2f t of pageSize */
15ec7 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69 6e 65 . .#if !define
15ec8 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 d(SQLITE_OMIT_SH
15ec9 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 ARED_CACHE) && !
15eca 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
15ecb 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20 MIT_DISKIO).
15ecc 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 42 /* Add the new B
15ecd 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 tShared object t
15ece 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 o the linked lis
15ecf 74 20 73 68 61 72 61 62 6c 65 20 42 74 53 68 61 t sharable BtSha
15ed0 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 reds.. */.
15ed1 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 if( p->sharable
15ed2 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
15ed3 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 3_mutex *mutexSh
15ed4 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42 74 2d ared;. pBt-
15ed5 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 >nRef = 1;.
15ed6 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20 73 mutexShared = s
15ed7 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 qlite3MutexAlloc
15ed8 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 (SQLITE_MUTEX_ST
15ed9 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 ATIC_MASTER);.
15eda 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 if( SQLITE_T
15edb 48 52 45 41 44 53 41 46 45 20 26 26 20 73 71 6c HREADSAFE && sql
15edc 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 ite3GlobalConfig
15edd 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 .bCoreMutex ){.
15ede 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75 74 65 pBt->mute
15edf 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 x = sqlite3Mutex
15ee0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 Alloc(SQLITE_MUT
15ee1 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20 20 20 EX_FAST);.
15ee2 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74 65 78 if( pBt->mutex
15ee3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
15ee4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d rc = SQLITE_NOM
15ee5 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 EM;. db
15ee6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
15ee7 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 0;. go
15ee8 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 to btree_open_ou
15ee9 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 t;. }.
15eea 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
15eeb 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d e3_mutex_enter(m
15eec 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 utexShared);.
15eed 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 pBt->pNext =
15eee 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15eef 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15ef0 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 cheList);.
15ef1 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a GLOBAL(BtShared*
15ef2 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 ,sqlite3SharedCa
15ef3 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a cheList) = pBt;.
15ef4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
15ef5 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 tex_leave(mutexS
15ef6 68 61 72 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65 hared);. }.#e
15ef7 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64 ndif. }..#if !d
15ef8 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
15ef9 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 IT_SHARED_CACHE)
15efa 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
15efb 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 ITE_OMIT_DISKIO)
15efc 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 . /* If the new
15efd 20 42 74 72 65 65 20 75 73 65 73 20 61 20 73 68 Btree uses a sh
15efe 61 72 61 62 6c 65 20 70 42 74 53 68 61 72 65 64 arable pBtShared
15eff 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20 , then link the
15f00 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 65 20 69 new. ** Btree i
15f01 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 nto the list of
15f02 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 42 74 72 all sharable Btr
15f03 65 65 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65 ees for the same
15f04 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a connection.. *
15f05 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20 6b 65 * The list is ke
15f06 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 pt in ascending
15f07 6f 72 64 65 72 20 62 79 20 70 42 74 20 61 64 64 order by pBt add
15f08 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 ress.. */. if(
15f09 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a p->sharable ){.
15f0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 int i;. B
15f0b 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 20 20 20 tree *pSib;.
15f0c 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e for(i=0; i<db->n
15f0d 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 Db; i++){.
15f0e 69 66 28 20 28 70 53 69 62 20 3d 20 64 62 2d 3e if( (pSib = db->
15f0f 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 aDb[i].pBt)!=0 &
15f10 26 20 70 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 & pSib->sharable
15f11 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c ){. whil
15f12 65 28 20 70 53 69 62 2d 3e 70 50 72 65 76 20 29 e( pSib->pPrev )
15f13 7b 20 70 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 { pSib = pSib->p
15f14 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 Prev; }.
15f15 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d if( p->pBt<pSib-
15f16 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 >pBt ){.
15f17 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 p->pNext = pSi
15f18 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e b;. p->
15f19 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 pPrev = 0;.
15f1a 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72 65 76 pSib->pPrev
15f1b 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 = p;. }e
15f1c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77 lse{. w
15f1d 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 hile( pSib->pNex
15f1e 74 20 26 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 t && pSib->pNext
15f1f 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a ->pBt<p->pBt ){.
15f20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 69 62 pSib
15f21 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a = pSib->pNext;.
15f22 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
15f23 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d p->pNext =
15f24 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 pSib->pNext;.
15f25 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65 76 p->pPrev
15f26 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20 20 20 = pSib;.
15f27 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 if( p->pNext
15f28 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
15f29 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d ->pNext->pPrev =
15f2a 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a p;. }.
15f2b 20 20 20 20 20 20 20 20 20 20 70 53 69 62 2d 3e pSib->
15f2c 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 pNext = p;.
15f2d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 }. bre
15f2e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
15f2f 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a }. }.#endif. *
15f30 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 ppBtree = p;..bt
15f31 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 ree_open_out:.
15f32 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
15f33 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42 74 K ){. if( pBt
15f34 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 && pBt->pPager
15f35 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
15f36 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e PagerClose(pBt->
15f37 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 pPager);. }.
15f38 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
15f39 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 pBt);. sqlite
15f3a 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 3_free(p);. *
15f3b 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d ppBtree = 0;. }
15f3c 0a 20 20 69 66 28 20 6d 75 74 65 78 4f 70 65 6e . if( mutexOpen
15f3d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
15f3e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
15f3f 6c 64 28 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b ld(mutexOpen) );
15f40 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
15f41 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 ex_leave(mutexOp
15f42 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 en);. }. retur
15f43 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 n rc;.}../*.** D
15f44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42 74 53 ecrement the BtS
15f45 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 hared.nRef count
15f46 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72 65 61 er. When it rea
15f47 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 ches zero,.** re
15f48 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61 72 65 move the BtShare
15f49 64 20 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d d structure from
15f4a 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73 the sharing lis
15f4b 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 t. Return.** tr
15f4c 75 65 20 69 66 20 74 68 65 20 42 74 53 68 61 72 ue if the BtShar
15f4d 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 ed.nRef counter
15f4e 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 reaches zero and
15f4f 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 return.** false
15f50 20 69 66 20 69 74 20 69 73 20 73 74 69 6c 6c 20 if it is still
15f51 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 positive..*/.sta
15f52 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 tic int removeFr
15f53 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 42 74 omSharingList(Bt
15f54 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 Shared *pBt){.#i
15f55 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
15f56 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 T_SHARED_CACHE.
15f57 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
15f58 70 4d 61 73 74 65 72 3b 0a 20 20 42 74 53 68 61 pMaster;. BtSha
15f59 72 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e red *pList;. in
15f5a 74 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a t removed = 0;..
15f5b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
15f5c 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 3_mutex_notheld(
15f5d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 pBt->mutex) );.
15f5e 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74 pMaster = sqlit
15f5f 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c e3MutexAlloc(SQL
15f60 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 ITE_MUTEX_STATIC
15f61 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 _MASTER);. sqli
15f62 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
15f63 70 4d 61 73 74 65 72 29 3b 0a 20 20 70 42 74 2d pMaster);. pBt-
15f64 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 >nRef--;. if( p
15f65 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 Bt->nRef<=0 ){.
15f66 20 20 20 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 if( GLOBAL(Bt
15f67 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 Shared*,sqlite3S
15f68 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 29 3d haredCacheList)=
15f69 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 47 4c =pBt ){. GL
15f6a 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 OBAL(BtShared*,s
15f6b 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
15f6c 65 4c 69 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e eList) = pBt->pN
15f6d 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ext;. }else{.
15f6e 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 47 4c pList = GL
15f6f 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 OBAL(BtShared*,s
15f70 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 qlite3SharedCach
15f71 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 77 68 eList);. wh
15f72 69 6c 65 28 20 41 4c 57 41 59 53 28 70 4c 69 73 ile( ALWAYS(pLis
15f73 74 29 20 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 t) && pList->pNe
15f74 78 74 21 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 xt!=pBt ){.
15f75 20 20 20 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e pList=pList->
15f76 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 pNext;. }.
15f77 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 if( ALWAYS(
15f78 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20 pList) ){.
15f79 20 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d pList->pNext =
15f7a 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pBt->pNext;.
15f7b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
15f7c 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 f( SQLITE_THREAD
15f7d 53 41 46 45 20 29 7b 0a 20 20 20 20 20 20 73 71 SAFE ){. sq
15f7e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 lite3_mutex_free
15f7f 28 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 (pBt->mutex);.
15f80 20 20 7d 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 }. removed
15f81 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 = 1;. }. sqlit
15f82 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
15f83 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 Master);. retur
15f84 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 n removed;.#else
15f85 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e . return 1;.#en
15f86 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b dif.}../*.** Mak
15f87 65 20 73 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 e sure pBt->pTmp
15f88 53 70 61 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 Space points to
15f89 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 an allocation of
15f8a 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a .** MX_CELL_SIZ
15f8b 45 28 70 42 74 29 20 62 79 74 65 73 2e 0a 2a 2f E(pBt) bytes..*/
15f8c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6c 6c .static void all
15f8d 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 42 ocateTempSpace(B
15f8e 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 tShared *pBt){.
15f8f 20 69 66 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 if( !pBt->pTmpS
15f90 70 61 63 65 20 29 7b 0a 20 20 20 20 70 42 74 2d pace ){. pBt-
15f91 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 73 71 6c >pTmpSpace = sql
15f92 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 ite3PageMalloc(
15f93 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b pBt->pageSize );
15f94 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 . }.}../*.** Fr
15f95 65 65 20 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 ee the pBt->pTmp
15f96 53 70 61 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e Space allocation
15f97 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
15f98 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 42 74 freeTempSpace(Bt
15f99 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
15f9a 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 sqlite3PageFree(
15f9b 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 29 pBt->pTmpSpace)
15f9c 3b 0a 20 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 ;. pBt->pTmpSpa
15f9d 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ce = 0;.}../*.**
15f9e 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 Close an open d
15f9f 61 74 61 62 61 73 65 20 61 6e 64 20 69 6e 76 61 atabase and inva
15fa0 6c 69 64 61 74 65 20 61 6c 6c 20 63 75 72 73 6f lidate all curso
15fa1 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 rs..*/.SQLITE_PR
15fa2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
15fa3 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 3BtreeClose(Btre
15fa4 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 e *p){. BtShare
15fa5 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b d *pBt = p->pBt;
15fa6 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 . BtCursor *pCu
15fa7 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 r;.. /* Close a
15fa8 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 ll cursors opene
15fa9 64 20 76 69 61 20 74 68 69 73 20 68 61 6e 64 6c d via this handl
15faa 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 e. */. assert(
15fab 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
15fac 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 eld(p->db->mutex
15fad 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 ) );. sqlite3Bt
15fae 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 reeEnter(p);. p
15faf 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 Cur = pBt->pCurs
15fb0 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70 43 75 or;. while( pCu
15fb1 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72 73 6f r ){. BtCurso
15fb2 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a r *pTmp = pCur;.
15fb3 20 20 20 20 70 43 75 72 20 3d 20 70 43 75 72 2d pCur = pCur-
15fb4 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 >pNext;. if(
15fb5 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 pTmp->pBtree==p
15fb6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
15fb7 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 BtreeCloseCursor
15fb8 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 (pTmp);. }.
15fb9 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b }.. /* Rollback
15fba 20 61 6e 79 20 61 63 74 69 76 65 20 74 72 61 6e any active tran
15fbb 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 saction and free
15fbc 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74 72 75 the handle stru
15fbd 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 cture.. ** The
15fbe 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 call to sqlite3B
15fbf 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 treeRollback() d
15fc0 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c rops any table-l
15fc1 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20 20 2a ocks held by. *
15fc2 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 * this handle..
15fc3 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 */. sqlite3Btr
15fc4 65 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 eeRollback(p);.
15fc5 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
15fc6 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 ve(p);.. /* If
15fc7 74 68 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 there are still
15fc8 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64 69 6e other outstandin
15fc9 67 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 g references to
15fca 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 the shared-btree
15fcb 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 2c . ** structure,
15fcc 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 return now. The
15fcd 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 remainder of th
15fce 69 73 20 70 72 6f 63 65 64 75 72 65 20 63 6c 65 is procedure cle
15fcf 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 ans . ** up the
15fd0 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 shared-btree..
15fd1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
15fd2 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 >wantToLock==0 &
15fd3 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 & p->locked==0 )
15fd4 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 ;. if( !p->shar
15fd5 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 able || removeFr
15fd6 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28 70 42 omSharingList(pB
15fd7 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 t) ){. /* The
15fd8 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 pBt is no longe
15fd9 72 20 6f 6e 20 74 68 65 20 73 68 61 72 69 6e 67 r on the sharing
15fda 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 63 61 6e list, so we can
15fdb 20 61 63 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 access. ** i
15fdc 74 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 t without having
15fdd 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 to hold the mut
15fde 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a ex.. **. *
15fdf 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 * Clean out and
15fe0 64 65 6c 65 74 65 20 74 68 65 20 42 74 53 68 61 delete the BtSha
15fe1 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 red object..
15fe2 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 */. assert( !
15fe3 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a pBt->pCursor );.
15fe4 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 sqlite3Pager
15fe5 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67 65 Close(pBt->pPage
15fe6 72 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 2d r);. if( pBt-
15fe7 3e 78 46 72 65 65 53 63 68 65 6d 61 20 26 26 20 >xFreeSchema &&
15fe8 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a pBt->pSchema ){.
15fe9 20 20 20 20 20 20 70 42 74 2d 3e 78 46 72 65 65 pBt->xFree
15fea 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 Schema(pBt->pSch
15feb 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ema);. }.
15fec 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 sqlite3_free(pBt
15fed 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 ->pSchema);.
15fee 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 freeTempSpace(pB
15fef 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f t);. sqlite3_
15ff0 66 72 65 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a free(pBt);. }..
15ff1 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
15ff2 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
15ff3 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 . assert( p->wa
15ff4 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 ntToLock==0 );.
15ff5 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b assert( p->lock
15ff6 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 ed==0 );. if( p
15ff7 2d 3e 70 50 72 65 76 20 29 20 70 2d 3e 70 50 72 ->pPrev ) p->pPr
15ff8 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 ev->pNext = p->p
15ff9 4e 65 78 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 Next;. if( p->p
15ffa 4e 65 78 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d Next ) p->pNext-
15ffb 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 >pPrev = p->pPre
15ffc 76 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c v;.#endif.. sql
15ffd 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 ite3_free(p);.
15ffe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
15fff 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 ;.}../*.** Chang
16000 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 e the limit on t
16001 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
16002 65 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 es allowed in th
16003 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 e cache..**.** T
16004 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 he maximum numbe
16005 72 20 6f 66 20 63 61 63 68 65 20 70 61 67 65 73 r of cache pages
16006 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 61 is set to the a
16007 62 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 bsolute.** value
16008 20 6f 66 20 6d 78 50 61 67 65 2e 20 20 49 66 20 of mxPage. If
16009 6d 78 50 61 67 65 20 69 73 20 6e 65 67 61 74 69 mxPage is negati
1600a 76 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 69 ve, the pager wi
1600b 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 ll.** operate as
1600c 79 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 ynchronously - i
1600d 74 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 t will not stop
1600e 74 6f 20 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a to do fsync()s.*
1600f 2a 20 74 6f 20 69 6e 73 75 72 65 20 64 61 74 61 * to insure data
16010 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 is written to t
16011 68 65 20 64 69 73 6b 20 73 75 72 66 61 63 65 20 he disk surface
16012 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e before.** contin
16013 75 69 6e 67 2e 20 20 54 72 61 6e 73 61 63 74 69 uing. Transacti
16014 6f 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 ons still work i
16015 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 f synchronous is
16016 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 off,.** and the
16017 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 database cannot
16018 20 62 65 20 63 6f 72 72 75 70 74 65 64 20 69 66 be corrupted if
16019 20 74 68 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a this program.**
1601a 20 63 72 61 73 68 65 73 2e 20 20 42 75 74 20 69 crashes. But i
1601b 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 f the operating
1601c 73 79 73 74 65 6d 20 63 72 61 73 68 65 73 20 6f system crashes o
1601d 72 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e r there is.** an
1601e 20 61 62 72 75 70 74 20 70 6f 77 65 72 20 66 61 abrupt power fa
1601f 69 6c 75 72 65 20 77 68 65 6e 20 73 79 6e 63 68 ilure when synch
16020 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 ronous is off, t
16021 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 he database.** c
16022 6f 75 6c 64 20 62 65 20 6c 65 66 74 20 69 6e 20 ould be left in
16023 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 an inconsistent
16024 61 6e 64 20 75 6e 72 65 63 6f 76 65 72 61 62 6c and unrecoverabl
16025 65 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 e state..** Sync
16026 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 hronous is on by
16027 20 64 65 66 61 75 6c 74 20 73 6f 20 64 61 74 61 default so data
16028 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 base corruption
16029 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c is not.** normal
1602a 6c 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 ly a worry..*/.S
1602b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1602c 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 t sqlite3BtreeSe
1602d 74 43 61 63 68 65 53 69 7a 65 28 42 74 72 65 65 tCacheSize(Btree
1602e 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 *p, int mxPage)
1602f 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 {. BtShared *pB
16030 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 t = p->pBt;. as
16031 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
16032 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e tex_held(p->db->
16033 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
16034 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
16035 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 ;. sqlite3Pager
16036 53 65 74 43 61 63 68 65 73 69 7a 65 28 70 42 74 SetCachesize(pBt
16037 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61 67 65 ->pPager, mxPage
16038 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
16039 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
1603a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
1603b 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 ../*.** Change t
1603c 68 65 20 77 61 79 20 64 61 74 61 20 69 73 20 73 he way data is s
1603d 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20 69 6e ynced to disk in
1603e 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72 65 61 order to increa
1603f 73 65 20 6f 72 20 64 65 63 72 65 61 73 65 0a 2a se or decrease.*
16040 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 * how well the d
16041 61 74 61 62 61 73 65 20 72 65 73 69 73 74 73 20 atabase resists
16042 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 damage due to OS
16043 20 63 72 61 73 68 65 73 20 61 6e 64 20 70 6f 77 crashes and pow
16044 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 er.** failures.
16045 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68 65 20 Level 1 is the
16046 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68 72 6f same as asynchro
16047 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 nous (no syncs()
16048 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 occur and.** th
16049 65 72 65 20 69 73 20 61 20 68 69 67 68 20 70 72 ere is a high pr
1604a 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d obability of dam
1604b 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20 69 73 age) Level 2 is
1604c 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 the default. T
1604d 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 here.** is a ver
1604e 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 y low but non-ze
1604f 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f ro probability o
16050 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c f damage. Level
16051 20 33 20 72 65 64 75 63 65 73 20 74 68 65 0a 2a 3 reduces the.*
16052 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 * probability of
16053 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 damage to near
16054 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20 61 20 zero but with a
16055 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 write performanc
16056 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a e reduction..*/.
16057 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16058 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 MIT_PAGER_PRAGMA
16059 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 S.SQLITE_PRIVATE
1605a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
1605b 65 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 eSetSafetyLevel(
1605c 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 Btree *p, int le
1605d 76 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e vel, int fullSyn
1605e 63 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a c){. BtShared *
1605f 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
16060 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
16061 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
16062 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 ->mutex) );. sq
16063 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
16064 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 p);. sqlite3Pag
16065 65 72 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c erSetSafetyLevel
16066 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 (pBt->pPager, le
16067 76 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a vel, fullSync);.
16068 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
16069 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
1606a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 SQLITE_OK;.}.#e
1606b 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 ndif../*.** Retu
1606c 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 67 rn TRUE if the g
1606d 69 76 65 6e 20 62 74 72 65 65 20 69 73 20 73 65 iven btree is se
1606e 74 20 74 6f 20 73 61 66 65 74 79 20 6c 65 76 65 t to safety leve
1606f 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a l 1. In other.*
16070 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 * words, return
16071 54 52 55 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 TRUE if no sync(
16072 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 20 ) occurs on the
16073 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 disk files..*/.S
16074 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16075 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 t sqlite3BtreeSy
16076 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 ncDisabled(Btree
16077 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 *p){. BtShared
16078 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
16079 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 int rc;. asse
1607a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1607b 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 x_held(p->db->mu
1607c 74 65 78 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 tex) ); . sqli
1607d 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
1607e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 20 ;. assert( pBt
1607f 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 && pBt->pPager )
16080 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
16081 50 61 67 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d PagerNosync(pBt-
16082 3e 70 50 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 >pPager);. sqli
16083 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16084 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d ;. return rc;.}
16085 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
16086 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 QLITE_OMIT_PAGER
16087 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 _PRAGMAS) || !de
16088 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
16089 54 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 T_VACUUM)./*.**
1608a 43 68 61 6e 67 65 20 74 68 65 20 64 65 66 61 75 Change the defau
1608b 6c 74 20 70 61 67 65 73 20 73 69 7a 65 20 61 6e lt pages size an
1608c 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 d the number of
1608d 72 65 73 65 72 76 65 64 20 62 79 74 65 73 20 70 reserved bytes p
1608e 65 72 20 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 er page..** Or,
1608f 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 if the page size
16090 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 has already bee
16091 6e 20 66 69 78 65 64 2c 20 72 65 74 75 72 6e 20 n fixed, return
16092 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 SQLITE_READONLY
16093 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 68 61 6e .** without chan
16094 67 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a ging anything..*
16095 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73 69 *.** The page si
16096 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 ze must be a pow
16097 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e 20 er of 2 between
16098 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20 20 512 and 65536.
16099 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 If the page.** s
1609a 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f 65 ize supplied doe
1609b 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73 20 s not meet this
1609c 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e 20 constraint then
1609d 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 the page size is
1609e 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e not.** changed.
1609f 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 .**.** Page size
160a0 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 s are constraine
160a1 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72 20 d to be a power
160a2 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20 74 of two so that t
160a3 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 he region.** of
160a4 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
160a5 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 e used for locki
160a6 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 ng (beginning at
160a7 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a PENDING_BYTE,.*
160a8 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 * the first byte
160a9 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62 6f past the 1GB bo
160aa 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30 30 undary, 0x400000
160ab 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 00) needs to occ
160ac 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65 67 ur.** at the beg
160ad 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67 65 inning of a page
160ae 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d ..**.** If param
160af 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69 73 eter nReserve is
160b0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c less than zero,
160b1 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 then the number
160b2 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 of reserved.**
160b3 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20 69 bytes per page i
160b4 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 s left unchanged
160b5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 ..**.** If the i
160b6 46 69 78 21 3d 30 20 74 68 65 6e 20 74 68 65 20 Fix!=0 then the
160b7 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 66 6c pageSizeFixed fl
160b8 61 67 20 69 73 20 73 65 74 20 73 6f 20 74 68 61 ag is set so tha
160b9 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 0a t the page size.
160ba 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 63 75 75 ** and autovacuu
160bb 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f m mode can no lo
160bc 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65 64 2e nger be changed.
160bd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
160be 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
160bf 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 reeSetPageSize(B
160c0 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 tree *p, int pag
160c1 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 eSize, int nRese
160c2 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a rve, int iFix){.
160c3 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
160c4 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 E_OK;. BtShared
160c5 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
160c6 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 assert( nReser
160c7 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 ve>=-1 && nReser
160c8 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c ve<=255 );. sql
160c9 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
160ca 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 );. if( pBt->pa
160cb 67 65 53 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 geSizeFixed ){.
160cc 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
160cd 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 eave(p);. ret
160ce 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
160cf 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e NLY;. }. if( n
160d0 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 Reserve<0 ){.
160d1 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42 74 2d nReserve = pBt-
160d2 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42 74 2d >pageSize - pBt-
160d3 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d >usableSize;. }
160d4 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 65 . assert( nRese
160d5 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 rve>=0 && nReser
160d6 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 ve<=255 );. if(
160d7 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 pageSize>=512 &
160d8 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 & pageSize<=SQLI
160d9 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 TE_MAX_PAGE_SIZE
160da 20 26 26 0a 20 20 20 20 20 20 20 20 28 28 70 61 &&. ((pa
160db 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 geSize-1)&pageSi
160dc 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 ze)==0 ){. as
160dd 73 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 sert( (pageSize
160de 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 & 7)==0 );. a
160df 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70 50 61 ssert( !pBt->pPa
160e0 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70 43 75 ge1 && !pBt->pCu
160e1 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42 74 2d rsor );. pBt-
160e2 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36 >pageSize = (u16
160e3 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 )pageSize;. f
160e4 72 65 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 reeTempSpace(pBt
160e5 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 );. }. rc = sq
160e6 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61 67 lite3PagerSetPag
160e7 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 esize(pBt->pPage
160e8 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a r, &pBt->pageSiz
160e9 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 e, nReserve);.
160ea 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
160eb 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 = pBt->pageSize
160ec 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76 65 3b - (u16)nReserve;
160ed 0a 20 20 69 66 28 20 69 46 69 78 20 29 20 70 42 . if( iFix ) pB
160ee 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 t->pageSizeFixed
160ef 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 = 1;. sqlite3B
160f0 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 treeLeave(p);.
160f1 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
160f2 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 .** Return the c
160f3 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 urrently defined
160f4 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 page size.*/.SQ
160f5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
160f6 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
160f7 50 61 67 65 53 69 7a 65 28 42 74 72 65 65 20 2a PageSize(Btree *
160f8 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
160f9 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d pBt->pageSize;.}
160fa 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 ../*.** Return t
160fb 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 he number of byt
160fc 65 73 20 6f 66 20 73 70 61 63 65 20 61 74 20 74 es of space at t
160fd 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 he end of every
160fe 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 page that.** are
160ff 20 69 6e 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 intentually lef
16100 74 20 75 6e 75 73 65 64 2e 20 20 54 68 69 73 20 t unused. This
16101 69 73 20 74 68 65 20 22 72 65 73 65 72 76 65 64 is the "reserved
16102 22 20 73 70 61 63 65 20 74 68 61 74 20 69 73 0a " space that is.
16103 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 ** sometimes use
16104 64 20 62 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e d by extensions.
16105 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
16106 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
16107 72 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 reeGetReserve(Bt
16108 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e ree *p){. int n
16109 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1610a 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 Enter(p);. n =
1610b 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 p->pBt->pageSize
1610c 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c - p->pBt->usabl
1610d 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 eSize;. sqlite3
1610e 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
1610f 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a return n;.}../*
16110 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61 78 69 .** Set the maxi
16111 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 66 mum page count f
16112 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 69 66 or a database if
16113 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74 mxPage is posit
16114 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 ive..** No chang
16115 65 73 20 61 72 65 20 6d 61 64 65 20 69 66 20 6d es are made if m
16116 78 50 61 67 65 20 69 73 20 30 20 6f 72 20 6e 65 xPage is 0 or ne
16117 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 gative..** Regar
16118 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76 61 6c dless of the val
16119 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 ue of mxPage, re
1611a 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d turn the maximum
1611b 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a page count..*/.
1611c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
1611d 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d nt sqlite3BtreeM
1611e 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72 65 axPageCount(Btre
1611f 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 e *p, int mxPage
16120 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 ){. int n;. sq
16121 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
16122 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 p);. n = sqlite
16123 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 3PagerMaxPageCou
16124 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 nt(p->pBt->pPage
16125 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 r, mxPage);. sq
16126 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
16127 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a p);. return n;.
16128 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 }.#endif /* !def
16129 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1612a 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29 20 _PAGER_PRAGMAS)
1612b 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 || !defined(SQLI
1612c 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 TE_OMIT_VACUUM)
1612d 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 */../*.** Change
1612e 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63 75 75 the 'auto-vacuu
1612f 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66 20 74 m' property of t
16130 68 65 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 he database. If
16131 74 68 65 20 27 61 75 74 6f 56 61 63 75 75 6d 27 the 'autoVacuum'
16132 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 .** parameter is
16133 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 non-zero, then
16134 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 auto-vacuum mode
16135 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 is enabled. If
16136 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 zero, it.** is d
16137 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64 65 66 isabled. The def
16138 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 ault value for t
16139 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 he auto-vacuum p
1613a 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 roperty is .** d
1613b 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
1613c 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f SQLITE_DEFAULT_
1613d 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f AUTOVACUUM macro
1613e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1613f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
16140 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 treeSetAutoVacuu
16141 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 m(Btree *p, int
16142 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 autoVacuum){.#if
16143 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
16144 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 AUTOVACUUM. ret
16145 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f urn SQLITE_READO
16146 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 NLY;.#else. BtS
16147 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e hared *pBt = p->
16148 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 pBt;. int rc =
16149 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 SQLITE_OK;. u8
1614a 61 76 20 3d 20 28 75 38 29 61 75 74 6f 56 61 63 av = (u8)autoVac
1614b 75 75 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 uum;.. sqlite3B
1614c 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 treeEnter(p);.
1614d 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a if( pBt->pageSiz
1614e 65 46 69 78 65 64 20 26 26 20 28 61 76 20 3f 31 eFixed && (av ?1
1614f 3a 30 29 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 :0)!=pBt->autoVa
16150 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d cuum ){. rc =
16151 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 SQLITE_READONLY
16152 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
16153 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d Bt->autoVacuum =
16154 20 61 76 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 av ?1:0;. pB
16155 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 t->incrVacuum =
16156 61 76 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a av==2 ?1:0;. }.
16157 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
16158 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
16159 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f rc;.#endif.}../
1615a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1615b 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 value of the 'au
1615c 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 to-vacuum' prope
1615d 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 rty. If auto-vac
1615e 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c uum is .** enabl
1615f 65 64 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 ed 1 is returned
16160 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a . Otherwise 0..*
16161 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16162 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16163 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 eGetAutoVacuum(B
16164 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 tree *p){.#ifdef
16165 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
16166 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e OVACUUM. return
16167 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 BTREE_AUTOVACUU
16168 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 M_NONE;.#else.
16169 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
1616a 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
1616b 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 rc = (. (!p
1616c 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ->pBt->autoVacuu
1616d 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 m)?BTREE_AUTOVAC
1616e 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 UUM_NONE:. (!
1616f 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 p->pBt->incrVacu
16170 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 um)?BTREE_AUTOVA
16171 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 CUUM_FULL:. B
16172 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f TREE_AUTOVACUUM_
16173 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 INCR. );. sqli
16174 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 te3BtreeLeave(p)
16175 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 ;. return rc;.#
16176 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 endif.}.../*.**
16177 47 65 74 20 61 20 72 65 66 65 72 65 6e 63 65 20 Get a reference
16178 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74 68 65 to pPage1 of the
16179 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1617a 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c This will.** al
1617b 73 6f 20 61 63 71 75 69 72 65 20 61 20 72 65 61 so acquire a rea
1617c 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 dlock on that fi
1617d 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 le..**.** SQLITE
1617e 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 _OK is returned
1617f 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 on success. If
16180 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 the file is not
16181 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 a.** well-formed
16182 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 database file,
16183 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 then SQLITE_CORR
16184 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e UPT is returned.
16185 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 .** SQLITE_BUSY
16186 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 is returned if t
16187 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c he database is l
16188 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e ocked. SQLITE_N
16189 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 OMEM.** is retur
1618a 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75 ned if we run ou
1618b 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f t of memory. .*/
1618c 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b .static int lock
1618d 42 74 72 65 65 28 42 74 53 68 61 72 65 64 20 2a Btree(BtShared *
1618e 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a pBt){. int rc;.
1618f 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
16190 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 1;. int nPage;.
16191 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16192 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
16193 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
16194 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 ssert( pBt->pPag
16195 65 31 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 e1==0 );. rc =
16196 73 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 sqlite3PagerShar
16197 65 64 4c 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 edLock(pBt->pPag
16198 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 er);. if( rc!=S
16199 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 QLITE_OK ) retur
1619a 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 62 74 72 n rc;. rc = btr
1619b 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 eeGetPage(pBt, 1
1619c 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 , &pPage1, 0);.
1619d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1619e 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a OK ) return rc;.
1619f 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 . /* Do some ch
161a0 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 ecking to help i
161a1 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65 20 77 nsure the file w
161a2 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 e opened really
161a3 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 is. ** a valid
161a4 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a database file. .
161a5 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
161a6 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e te3PagerPagecoun
161a7 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 t(pBt->pPager, &
161a8 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 nPage);. if( rc
161a9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
161aa 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e goto page1_in
161ab 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c it_failed;. }el
161ac 73 65 20 69 66 28 20 6e 50 61 67 65 3e 30 20 29 se if( nPage>0 )
161ad 7b 0a 20 20 20 20 69 6e 74 20 70 61 67 65 53 69 {. int pageSi
161ae 7a 65 3b 0a 20 20 20 20 69 6e 74 20 75 73 61 62 ze;. int usab
161af 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a leSize;. u8 *
161b0 70 61 67 65 31 20 3d 20 70 50 61 67 65 31 2d 3e page1 = pPage1->
161b1 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 aData;. rc =
161b2 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 SQLITE_NOTADB;.
161b3 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 if( memcmp(pa
161b4 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 ge1, zMagicHeade
161b5 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 r, 16)!=0 ){.
161b6 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e goto page1_in
161b7 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d it_failed;. }
161b8 0a 20 20 20 20 69 66 28 20 70 61 67 65 31 5b 31 . if( page1[1
161b9 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 70 42 8]>1 ){. pB
161ba 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b t->readOnly = 1;
161bb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
161bc 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 age1[19]>1 ){.
161bd 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 goto page1_i
161be 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 nit_failed;.
161bf 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 }.. /* The ma
161c0 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64 20 66 ximum embedded f
161c1 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 raction must be
161c2 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20 41 6e exactly 25%. An
161c3 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 d the minimum.
161c4 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20 66 72 ** embedded fr
161c5 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 action must be 1
161c6 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 2.5% for both le
161c7 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d af-data and non-
161c8 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a leaf-data.. *
161c9 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 * The original d
161ca 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 esign allowed th
161cb 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 ese amounts to v
161cc 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 ary, but as of.
161cd 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e ** version 3.
161ce 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72 65 20 6.0, we require
161cf 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78 65 64 them to be fixed
161d0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
161d1 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 memcmp(&page1[2
161d2 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 1], "\100\040\04
161d3 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 0",3)!=0 ){.
161d4 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 goto page1_ini
161d5 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a t_failed;. }.
161d6 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d 20 67 pageSize = g
161d7 65 74 32 62 79 74 65 28 26 70 61 67 65 31 5b 31 et2byte(&page1[1
161d8 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 28 28 70 6]);. if( ((p
161d9 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65 53 ageSize-1)&pageS
161da 69 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 ize)!=0 || pageS
161db 69 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 ize<512 ||.
161dc 20 20 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 (SQLITE_MAX_P
161dd 41 47 45 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 AGE_SIZE<32768 &
161de 26 20 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 & pageSize>SQLIT
161df 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 E_MAX_PAGE_SIZE)
161e0 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 67 6f . ){. go
161e1 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 to page1_init_fa
161e2 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iled;. }.
161e3 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a assert( (pageSiz
161e4 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 e & 7)==0 );.
161e5 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 usableSize = pa
161e6 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 geSize - page1[2
161e7 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65 0];. if( page
161e8 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 Size!=pBt->pageS
161e9 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 ize ){. /*
161ea 41 66 74 65 72 20 72 65 61 64 69 6e 67 20 74 68 After reading th
161eb 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 e first page of
161ec 74 68 65 20 64 61 74 61 62 61 73 65 20 61 73 73 the database ass
161ed 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73 69 7a uming a page siz
161ee 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 e. ** of Bt
161ef 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 2c Shared.pageSize,
161f0 20 77 65 20 68 61 76 65 20 64 69 73 63 6f 76 65 we have discove
161f1 72 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67 red that the pag
161f2 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20 20 20 e-size is.
161f3 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61 67 65 ** actually page
161f4 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 Size. Unlock the
161f5 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61 76 65 database, leave
161f6 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61 74 0a pBt->pPage1 at.
161f7 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e ** zero an
161f8 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f d return SQLITE_
161f9 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 OK. The caller w
161fa 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 ill call this fu
161fb 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 nction. **
161fc 61 67 61 69 6e 20 77 69 74 68 20 74 68 65 20 63 again with the c
161fd 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69 7a 65 orrect page-size
161fe 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
161ff 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
16200 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42 74 2d ge1);. pBt-
16201 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 >usableSize = (u
16202 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 16)usableSize;.
16203 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69 pBt->pageSi
16204 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65 53 69 ze = (u16)pageSi
16205 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65 54 65 ze;. freeTe
16206 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 mpSpace(pBt);.
16207 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16208 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 PagerSetPagesize
16209 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 (pBt->pPager, &p
1620a 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 Bt->pageSize,.
1620b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1620c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1620d 20 70 61 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 pageSize-usable
1620e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72 65 74 Size);. ret
1620f 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 urn rc;. }.
16210 20 20 69 66 28 20 75 73 61 62 6c 65 53 69 7a 65 if( usableSize
16211 3c 34 38 30 20 29 7b 0a 20 20 20 20 20 20 67 6f <480 ){. go
16212 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 to page1_init_fa
16213 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iled;. }.
16214 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 pBt->pageSize =
16215 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 (u16)pageSize;.
16216 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 pBt->usableSi
16217 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62 6c 65 ze = (u16)usable
16218 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 Size;.#ifndef SQ
16219 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
1621a 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 CUUM. pBt->au
1621b 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34 toVacuum = (get4
1621c 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b byte(&page1[36 +
1621d 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 4*4])?1:0);.
1621e 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d pBt->incrVacuum
1621f 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61 = (get4byte(&pa
16220 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 ge1[36 + 7*4])?1
16221 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a :0);.#endif. }.
16222 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 . /* maxLocal i
16223 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d s the maximum am
16224 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 ount of payload
16225 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 to store locally
16226 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c for. ** a cell
16227 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 . Make sure it
16228 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 is small enough
16229 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 so that at least
1622a 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 minFanout. **
1622b 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 cells can will f
1622c 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 it on one page.
1622d 20 57 65 20 61 73 73 75 6d 65 20 61 20 31 30 2d We assume a 10-
1622e 62 79 74 65 20 70 61 67 65 20 68 65 61 64 65 72 byte page header
1622f 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73 20 74 .. ** Besides t
16230 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 he payload, the
16231 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a cell must store:
16232 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 . ** 2-byte
16233 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
16234 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d cell. ** 4-
16235 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 byte child point
16236 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 er. ** 9-by
16237 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 te nKey value.
16238 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20 6e 44 ** 4-byte nD
16239 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 ata value. **
1623a 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 4-byte overfl
1623b 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 0a ow page pointer.
1623c 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 ** So a cell c
1623d 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32 2d 62 onsists of a 2-b
1623e 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 yte poiner, a he
1623f 61 64 65 72 20 77 68 69 63 68 20 69 73 20 61 73 ader which is as
16240 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 much as. ** 17
16241 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 bytes long, 0 t
16242 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70 61 79 o N bytes of pay
16243 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 load, and an opt
16244 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f 76 65 ional 4 byte ove
16245 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 rflow. ** page
16246 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 pointer.. */.
16247 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 pBt->maxLocal =
16248 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 (pBt->usableSize
16249 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 -12)*64/255 - 23
1624a 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 ;. pBt->minLoca
1624b 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 l = (pBt->usable
1624c 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 Size-12)*32/255
1624d 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 - 23;. pBt->max
1624e 4c 65 61 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 Leaf = pBt->usab
1624f 6c 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 leSize - 35;. p
16250 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 Bt->minLeaf = (p
16251 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 Bt->usableSize-1
16252 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 2)*32/255 - 23;.
16253 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6d assert( pBt->m
16254 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d axLeaf + 23 <= M
16255 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 X_CELL_SIZE(pBt)
16256 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 );. pBt->pPage
16257 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20 72 65 1 = pPage1;. re
16258 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
16259 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c .page1_init_fail
1625a 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 ed:. releasePag
1625b 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70 42 74 e(pPage1);. pBt
1625c 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 ->pPage1 = 0;.
1625d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1625e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 .** If there are
1625f 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 no outstanding
16260 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65 20 61 cursors and we a
16261 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 re not in the mi
16262 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 ddle.** of a tra
16263 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74 68 65 nsaction but the
16264 72 65 20 69 73 20 61 20 72 65 61 64 20 6c 6f 63 re is a read loc
16265 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
16266 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 e, then.** this
16267 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73 20 74 routine unrefs t
16268 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 he first page of
16269 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1626a 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 le which .** has
1626b 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 72 the effect of r
1626c 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72 65 61 eleasing the rea
1626d 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 d lock..**.** If
1626e 20 74 68 65 72 65 20 69 73 20 61 20 74 72 61 6e there is a tran
1626f 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 saction in progr
16270 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e ess, this routin
16271 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f e is a no-op..*/
16272 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c .static void unl
16273 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 ockBtreeIfUnused
16274 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b (BtShared *pBt){
16275 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16276 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
16277 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
16278 73 73 65 72 74 28 20 70 42 74 2d 3e 70 43 75 72 ssert( pBt->pCur
16279 73 6f 72 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 sor==0 || pBt->i
1627a 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 nTransaction>TRA
1627b 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 NS_NONE );. if(
1627c 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
1627d 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ion==TRANS_NONE
1627e 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31 21 3d && pBt->pPage1!=
1627f 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 0 ){. assert(
16280 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 pBt->pPage1->aD
16281 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65 72 ata );. asser
16282 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 t( sqlite3PagerR
16283 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 efcount(pBt->pPa
16284 67 65 72 29 3d 3d 31 20 29 3b 0a 20 20 20 20 61 ger)==1 );. a
16285 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67 ssert( pBt->pPag
16286 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 e1->aData );.
16287 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 42 74 releasePage(pBt
16288 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20 20 70 ->pPage1);. p
16289 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a Bt->pPage1 = 0;.
1628a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 }.}../*.** If
1628b 70 42 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e pBt points to an
1628c 20 65 6d 70 74 79 20 66 69 6c 65 20 74 68 65 6e empty file then
1628d 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 65 6d convert that em
1628e 70 74 79 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f pty file.** into
1628f 20 61 20 6e 65 77 20 65 6d 70 74 79 20 64 61 74 a new empty dat
16290 61 62 61 73 65 20 62 79 20 69 6e 69 74 69 61 6c abase by initial
16291 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 izing the first
16292 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64 page of.** the d
16293 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 atabase..*/.stat
16294 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61 62 61 ic int newDataba
16295 73 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 se(BtShared *pBt
16296 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 ){. MemPage *pP
16297 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 1;. unsigned ch
16298 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 ar *data;. int
16299 72 63 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b rc;. int nPage;
1629a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 .. assert( sqli
1629b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1629c 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1629d 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 /* The database
1629e 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79 size has already
1629f 20 62 65 65 6e 20 6d 65 61 73 75 72 65 64 20 61 been measured a
162a0 6e 64 20 63 61 63 68 65 64 2c 20 73 6f 20 66 61 nd cached, so fa
162a1 69 6c 75 72 65 0a 20 20 2a 2a 20 69 73 20 69 6d ilure. ** is im
162a2 70 6f 73 73 69 62 6c 65 20 68 65 72 65 2e 20 20 possible here.
162a3 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 If the original
162a4 73 69 7a 65 20 6d 65 61 73 75 72 65 6d 65 6e 74 size measurement
162a5 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 0a 20 20 failed, then.
162a6 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 61 62 ** processing ab
162a7 6f 72 74 73 20 62 65 66 6f 72 65 20 65 6e 74 65 orts before ente
162a8 72 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ring this routin
162a9 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c e. */. rc = sql
162aa 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
162ab 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 nt(pBt->pPager,
162ac 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20 4e &nPage);. if( N
162ad 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f EVER(rc!=SQLITE_
162ae 4f 4b 29 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 OK) || nPage>0 )
162af 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b {. return rc;
162b0 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 . }. pP1 = pBt
162b1 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 73 73 65 ->pPage1;. asse
162b2 72 74 28 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 rt( pP1!=0 );.
162b3 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 data = pP1->aDat
162b4 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 a;. rc = sqlite
162b5 33 50 61 67 65 72 57 72 69 74 65 28 70 50 31 2d 3PagerWrite(pP1-
162b6 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 >pDbPage);. if(
162b7 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b rc ) return rc;
162b8 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 . memcpy(data,
162b9 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 73 69 zMagicHeader, si
162ba 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 zeof(zMagicHeade
162bb 72 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 r));. assert( s
162bc 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 izeof(zMagicHead
162bd 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 er)==16 );. put
162be 32 62 79 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 2byte(&data[16],
162bf 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b pBt->pageSize);
162c0 0a 20 20 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b . data[18] = 1;
162c1 0a 20 20 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b . data[19] = 1;
162c2 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e . assert( pBt->
162c3 75 73 61 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d usableSize<=pBt-
162c4 3e 70 61 67 65 53 69 7a 65 20 26 26 20 70 42 74 >pageSize && pBt
162c5 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 ->usableSize+255
162c6 3e 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 >=pBt->pageSize)
162c7 3b 0a 20 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 ;. data[20] = (
162c8 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53 69 7a u8)(pBt->pageSiz
162c9 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e - pBt->usableS
162ca 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d ize);. data[21]
162cb 20 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 = 64;. data[22
162cc 5d 20 3d 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 ] = 32;. data[2
162cd 33 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 3] = 32;. memse
162ce 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 t(&data[24], 0,
162cf 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 100-24);. zeroP
162d0 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 age(pP1, PTF_INT
162d1 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 KEY|PTF_LEAF|PTF
162d2 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70 _LEAFDATA );. p
162d3 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 Bt->pageSizeFixe
162d4 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 d = 1;.#ifndef S
162d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
162d6 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20 ACUUM. assert(
162d7 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d pBt->autoVacuum=
162d8 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 =1 || pBt->autoV
162d9 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 acuum==0 );. as
162da 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 sert( pBt->incrV
162db 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d acuum==1 || pBt-
162dc 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 >incrVacuum==0 )
162dd 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61 ;. put4byte(&da
162de 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 ta[36 + 4*4], pB
162df 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a t->autoVacuum);.
162e0 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 put4byte(&data
162e1 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d [36 + 7*4], pBt-
162e2 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 >incrVacuum);.#e
162e3 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 ndif. return SQ
162e4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
162e5 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61 * Attempt to sta
162e6 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 rt a new transac
162e7 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 tion. A write-tr
162e8 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 ansaction.** is
162e9 73 74 61 72 74 65 64 20 69 66 20 74 68 65 20 73 started if the s
162ea 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 econd argument i
162eb 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 s nonzero, other
162ec 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 wise a read-.**
162ed 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 transaction. If
162ee 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 the second argu
162ef 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 ment is 2 or mor
162f0 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a e and exclusive.
162f1 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ** transaction i
162f2 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 s started, meani
162f3 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 ng that no other
162f4 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f process is allo
162f5 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 wed.** to access
162f6 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 the database.
162f7 41 20 70 72 65 65 78 69 73 74 69 6e 67 20 74 72 A preexisting tr
162f8 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f ansaction may no
162f9 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 t be.** upgraded
162fa 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 to exclusive by
162fb 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f calling this ro
162fc 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 utine a second t
162fd 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 ime - the.** exc
162fe 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e lusivity flag on
162ff 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e ly works for a n
16300 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a ew transaction..
16301 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 **.** A write-tr
16302 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
16303 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 e started before
16304 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 attempting any
16305 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74 .** changes to t
16306 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f he database. No
16307 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 ne of the follow
16308 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a ing routines .**
16309 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 will work unles
1630a 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 s a transaction
1630b 69 73 20 73 74 61 72 74 65 64 20 66 69 72 73 74 is started first
1630c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c :.**.** sql
1630d 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 ite3BtreeCreateT
1630e 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 able().** s
1630f 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 qlite3BtreeCreat
16310 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 eIndex().**
16311 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 sqlite3BtreeCle
16312 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 arTable().**
16313 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 sqlite3BtreeDr
16314 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 opTable().**
16315 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e sqlite3BtreeIn
16316 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 sert().** s
16317 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 qlite3BtreeDelet
16318 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 e().** sqli
16319 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 te3BtreeUpdateMe
1631a 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ta().**.** If an
1631b 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 initial attempt
1631c 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 to acquire the
1631d 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75 lock fails becau
1631e 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 se of lock conte
1631f 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 ntion.** and the
16320 20 64 61 74 61 62 61 73 65 20 77 61 73 20 70 72 database was pr
16321 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 eviously unlocke
16322 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 d, then invoke t
16323 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a he busy handler.
16324 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f ** if there is o
16325 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 ne. But if ther
16326 65 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 e was previously
16327 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f a read-lock, do
16328 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 not.** invoke t
16329 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 he busy handler
1632a 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 - just return SQ
1632b 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 LITE_BUSY. SQLI
1632c 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 TE_BUSY is .** r
1632d 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65 eturned when the
1632e 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 re is already a
1632f 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 read-lock in ord
16330 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 er to avoid a de
16331 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 adlock..**.** Su
16332 70 70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20 ppose there are
16333 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20 two processes A
16334 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61 20 and B. A has a
16335 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 read lock and B
16336 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 has.** a reserve
16337 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 d lock. B tries
16338 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 to promote to e
16339 78 63 6c 75 73 69 76 65 20 62 75 74 20 69 73 20 xclusive but is
1633a 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a blocked because.
1633b 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c ** of A's read l
1633c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f ock. A tries to
1633d 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 promote to rese
1633e 72 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63 rved but is bloc
1633f 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 ked by B..** One
16340 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 or the other of
16341 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73 the two process
16342 65 73 20 6d 75 73 74 20 67 69 76 65 20 77 61 79 es must give way
16343 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62 65 or there can be
16344 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e .** no progress.
16345 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53 By returning S
16346 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e QLITE_BUSY and n
16347 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 ot invoking the
16348 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a busy callback.**
16349 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20 when A already
1634a 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c has a read lock,
1634b 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20 we encourage A
1634c 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c to give up and l
1634d 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e et B.** proceed.
1634e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1634f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
16350 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 reeBeginTrans(Bt
16351 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c ree *p, int wrfl
16352 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a ag){. sqlite3 *
16353 70 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 pBlock = 0;. Bt
16354 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d Shared *pBt = p-
16355 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d >pBt;. int rc =
16356 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 SQLITE_OK;.. s
16357 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
16358 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 (p);. btreeInte
16359 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 grity(p);.. /*
1635a 49 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 If the btree is
1635b 61 6c 72 65 61 64 79 20 69 6e 20 61 20 77 72 69 already in a wri
1635c 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 te-transaction,
1635d 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c or it. ** is al
1635e 72 65 61 64 79 20 69 6e 20 61 20 72 65 61 64 2d ready in a read-
1635f 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 transaction and
16360 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 a read-transacti
16361 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 65 on. ** is reque
16362 73 74 65 64 2c 20 74 68 69 73 20 69 73 20 61 20 sted, this is a
16363 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 no-op.. */. if
16364 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
16365 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d ANS_WRITE || (p-
16366 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
16367 52 45 41 44 20 26 26 20 21 77 72 66 6c 61 67 29 READ && !wrflag)
16368 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 ){. goto tra
16369 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 ns_begun;. }..
1636a 20 2f 2a 20 57 72 69 74 65 20 74 72 61 6e 73 61 /* Write transa
1636b 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 ctions are not p
1636c 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 ossible on a rea
1636d 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 d-only database
1636e 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 72 65 */. if( pBt->re
1636f 61 64 4f 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 adOnly && wrflag
16370 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
16371 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 ITE_READONLY;.
16372 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 goto trans_beg
16373 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 un;. }..#ifndef
16374 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
16375 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 RED_CACHE. /* I
16376 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 f another databa
16377 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 6c se handle has al
16378 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61 20 77 ready opened a w
16379 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e rite transaction
1637a 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 . ** on this s
1637b 68 61 72 65 64 2d 62 74 72 65 65 20 73 74 72 75 hared-btree stru
1637c 63 74 75 72 65 20 61 6e 64 20 61 20 73 65 63 6f cture and a seco
1637d 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 nd write transac
1637e 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 tion is. ** req
1637f 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e 20 53 uested, return S
16380 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 QLITE_LOCKED..
16381 2a 2f 0a 20 20 69 66 28 20 28 77 72 66 6c 61 67 */. if( (wrflag
16382 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 && pBt->inTrans
16383 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 action==TRANS_WR
16384 49 54 45 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 ITE) || pBt->isP
16385 65 6e 64 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 ending ){. pB
16386 6c 6f 63 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 lock = pBt->pWri
16387 74 65 72 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 ter->db;. }else
16388 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29 7b if( wrflag>1 ){
16389 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 . BtLock *pIt
1638a 65 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 74 65 er;. for(pIte
1638b 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 r=pBt->pLock; pI
1638c 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65 72 ter; pIter=pIter
1638d 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
1638e 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72 65 if( pIter->pBtre
1638f 65 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 e!=p ){.
16390 70 42 6c 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e pBlock = pIter->
16391 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 pBtree->db;.
16392 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
16393 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 }. }. }. i
16394 66 28 20 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 f( pBlock ){.
16395 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 sqlite3Connecti
16396 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c onBlocked(p->db,
16397 20 70 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 pBlock);. rc
16398 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 = SQLITE_LOCKED
16399 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 _SHAREDCACHE;.
1639a 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 goto trans_beg
1639b 75 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a un;. }.#endif..
1639c 20 20 2f 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e /* Any read-on
1639d 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69 74 65 ly or read-write
1639e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 transaction imp
1639f 6c 69 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b lies a read-lock
163a0 20 6f 6e 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 on . ** page 1
163a1 2e 20 53 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 . So if some oth
163a2 65 72 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 er shared-cache
163a3 63 6c 69 65 6e 74 20 61 6c 72 65 61 64 79 20 68 client already h
163a4 61 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 as a write-lock
163a5 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c . ** on page 1,
163a6 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e the transaction
163a7 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 cannot be opene
163a8 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 d. */. rc = que
163a9 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 rySharedCacheTab
163aa 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 leLock(p, MASTER
163ab 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b _ROOT, READ_LOCK
163ac 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f );. if( SQLITE_
163ad 4f 4b 21 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 OK!=rc ) goto tr
163ae 61 6e 73 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f ans_begun;.. do
163af 20 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c {. /* Call l
163b0 6f 63 6b 42 74 72 65 65 28 29 20 75 6e 74 69 6c ockBtree() until
163b1 20 65 69 74 68 65 72 20 70 42 74 2d 3e 70 50 61 either pBt->pPa
163b2 67 65 31 20 69 73 20 70 6f 70 75 6c 61 74 65 64 ge1 is populated
163b3 20 6f 72 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 or. ** lockB
163b4 74 72 65 65 28 29 20 72 65 74 75 72 6e 73 20 73 tree() returns s
163b5 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 omething other t
163b6 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c han SQLITE_OK. l
163b7 6f 63 6b 42 74 72 65 65 28 29 0a 20 20 20 20 2a ockBtree(). *
163b8 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c * may return SQL
163b9 49 54 45 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 ITE_OK but leave
163ba 20 70 42 74 2d 3e 70 50 61 67 65 31 20 73 65 74 pBt->pPage1 set
163bb 20 74 6f 20 30 20 69 66 20 61 66 74 65 72 0a 20 to 0 if after.
163bc 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 ** reading pa
163bd 67 65 20 31 20 69 74 20 64 69 73 63 6f 76 65 72 ge 1 it discover
163be 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d s that the page-
163bf 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
163c0 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c base . ** fil
163c1 65 20 69 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 e is not pBt->pa
163c2 67 65 53 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 geSize. In this
163c3 63 61 73 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 case lockBtree()
163c4 20 77 69 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 will update.
163c5 20 2a 2a 20 70 42 74 2d 3e 70 61 67 65 53 69 7a ** pBt->pageSiz
163c6 65 20 74 6f 20 74 68 65 20 70 61 67 65 2d 73 69 e to the page-si
163c7 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 6f ze of the file o
163c8 6e 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 n disk.. */.
163c9 20 20 20 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 while( pBt->p
163ca 50 61 67 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 Page1==0 && SQLI
163cb 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 TE_OK==(rc = loc
163cc 6b 42 74 72 65 65 28 70 42 74 29 29 20 29 3b 0a kBtree(pBt)) );.
163cd 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
163ce 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 ITE_OK && wrflag
163cf 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 ){. if( pB
163d0 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 t->readOnly ){.
163d1 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
163d2 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 TE_READONLY;.
163d3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
163d4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
163d5 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 gerBegin(pBt->pP
163d6 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 ager,wrflag>1,sq
163d7 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 lite3TempInMemor
163d8 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 y(p->db));.
163d9 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
163da 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
163db 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 rc = newDataba
163dc 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 se(pBt);.
163dd 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
163de 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63 21 3d . . if( rc!=
163df 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
163e0 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 unlockBtreeIf
163e1 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 Unused(pBt);.
163e2 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d }. }while( rc=
163e3 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 26 20 =SQLITE_BUSY &&
163e4 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
163e5 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 on==TRANS_NONE &
163e6 26 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65 &. btre
163e7 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c eInvokeBusyHandl
163e8 65 72 28 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 er(pBt) );.. if
163e9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
163ea 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 6e ){. if( p->in
163eb 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e Trans==TRANS_NON
163ec 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e E ){. pBt->
163ed 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a nTransaction++;.
163ee 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
163ef 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
163f0 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 73 68 . if( p->sh
163f1 61 72 61 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 arable ){..asser
163f2 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 t( p->lock.pBtre
163f3 65 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e e==p && p->lock.
163f4 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 iTable==1 );.
163f5 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f p->lock.eLo
163f6 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a ck = READ_LOCK;.
163f7 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e p->lock.
163f8 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f pNext = pBt->pLo
163f9 63 6b 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d ck;. pBt-
163fa 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 >pLock = &p->loc
163fb 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 k;. }.#endi
163fc 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 f. }. p->i
163fd 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 nTrans = (wrflag
163fe 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 ?TRANS_WRITE:TRA
163ff 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20 69 66 NS_READ);. if
16400 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 ( p->inTrans>pBt
16401 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 ->inTransaction
16402 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e ){. pBt->in
16403 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d Transaction = p-
16404 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a >inTrans;. }.
16405 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16406 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
16407 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61 67 20 . if( wrflag
16408 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 ){. assert(
16409 20 21 70 42 74 2d 3e 70 57 72 69 74 65 72 20 29 !pBt->pWriter )
1640a 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 57 72 ;. pBt->pWr
1640b 69 74 65 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 iter = p;.
1640c 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 pBt->isExclusive
1640d 20 3d 20 28 75 38 29 28 77 72 66 6c 61 67 3e 31 = (u8)(wrflag>1
1640e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1640f 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 }...trans_begu
16410 6e 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c n:. if( rc==SQL
16411 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 ITE_OK && wrflag
16412 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 ){. /* This
16413 63 61 6c 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 call makes sure
16414 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 68 that the pager h
16415 61 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e as the correct n
16416 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 umber of. **
16417 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e open savepoints.
16418 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 If the second p
16419 61 72 61 6d 65 74 65 72 20 69 73 20 67 72 65 61 arameter is grea
1641a 74 65 72 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 ter than 0 and.
1641b 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f ** the sub-jo
1641c 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 urnal is not alr
1641d 65 61 64 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 eady open, then
1641e 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 it will be opene
1641f 64 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 d here.. */.
16420 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
16421 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e agerOpenSavepoin
16422 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 t(pBt->pPager, p
16423 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 ->db->nSavepoint
16424 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 );. }.. btreeI
16425 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 ntegrity(p);. s
16426 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
16427 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
16428 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
16429 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
1642a 55 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 UUM../*.** Set t
1642b 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
1642c 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 ntries for all c
1642d 68 69 6c 64 72 65 6e 20 6f 66 20 70 61 67 65 20 hildren of page
1642e 70 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a pPage. Also, if.
1642f 2a 2a 20 70 50 61 67 65 20 63 6f 6e 74 61 69 6e ** pPage contain
16430 73 20 63 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 s cells that poi
16431 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 nt to overflow p
16432 61 67 65 73 2c 20 73 65 74 20 74 68 65 20 70 6f ages, set the po
16433 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 inter.** map ent
16434 72 69 65 73 20 66 6f 72 20 74 68 65 20 6f 76 65 ries for the ove
16435 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 20 77 rflow pages as w
16436 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ell..*/.static i
16437 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 nt setChildPtrma
16438 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 ps(MemPage *pPag
16439 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 e){. int i;
1643a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1643b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e /* Coun
1643c 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a ter variable */.
1643d 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 int nCell;
1643e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1643f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
16440 66 20 63 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 f cells in page
16441 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 pPage */. int r
16442 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
16443 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
16444 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a Return code */.
16445 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
16446 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
16447 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 u8 isInitOrig =
16448 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 pPage->isInit;.
16449 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 Pgno pgno = pPa
1644a 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 ge->pgno;.. ass
1644b 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1644c 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 ex_held(pPage->p
1644d 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 Bt->mutex) );.
1644e 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 rc = btreeInitPa
1644f 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69 66 28 ge(pPage);. if(
16450 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
16451 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 {. goto set_c
16452 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 hild_ptrmaps_out
16453 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 ;. }. nCell =
16454 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 pPage->nCell;..
16455 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c for(i=0; i<nCel
16456 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 l; i++){. u8
16457 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c *pCell = findCel
16458 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 l(pPage, i);..
16459 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 ptrmapPutOvflP
1645a 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c tr(pPage, pCell,
1645b 20 26 72 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 &rc);.. if(
1645c 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a !pPage->leaf ){.
1645d 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 Pgno child
1645e 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 Pgno = get4byte(
1645f 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 pCell);. pt
16460 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63 68 69 rmapPut(pBt, chi
16461 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 ldPgno, PTRMAP_B
16462 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 TREE, pgno, &rc)
16463 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 ;. }. }.. i
16464 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
16465 29 7b 0a 20 20 20 20 50 67 6e 6f 20 63 68 69 6c ){. Pgno chil
16466 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 dPgno = get4byte
16467 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
16468 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
16469 38 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 8]);. ptrmapP
1646a 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e ut(pBt, childPgn
1646b 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c o, PTRMAP_BTREE,
1646c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d pgno, &rc);. }
1646d 0a 0a 73 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d ..set_child_ptrm
1646e 61 70 73 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 aps_out:. pPage
1646f 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 ->isInit = isIni
16470 74 4f 72 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 tOrig;. return
16471 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d rc;.}../*.** Som
16472 65 77 68 65 72 65 20 6f 6e 20 70 50 61 67 65 20 ewhere on pPage
16473 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
16474 70 61 67 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 page iFrom. Mod
16475 69 66 79 20 74 68 69 73 20 70 6f 69 6e 74 65 72 ify this pointer
16476 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 so.** that it p
16477 6f 69 6e 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 oints to iTo. Pa
16478 72 61 6d 65 74 65 72 20 65 54 79 70 65 20 64 65 rameter eType de
16479 73 63 72 69 62 65 73 20 74 68 65 20 74 79 70 65 scribes the type
1647a 20 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a of pointer to.*
1647b 2a 20 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 * be modified, a
1647c 73 20 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a s follows:.**.*
1647d 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 * PTRMAP_BTREE:
1647e 20 20 20 20 70 50 61 67 65 20 69 73 20 61 20 62 pPage is a b
1647f 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 tree-page. The p
16480 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 ointer points at
16481 20 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 a child .**
16482 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
16483 61 67 65 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a age of pPage..**
16484 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 .** PTRMAP_OVERF
16485 4c 4f 57 31 3a 20 70 50 61 67 65 20 69 73 20 61 LOW1: pPage is a
16486 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65 btree-page. The
16487 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 pointer points
16488 61 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a at an overflow.*
16489 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1648a 20 20 20 20 70 61 67 65 20 70 6f 69 6e 74 65 64 page pointed
1648b 20 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 to by one of th
1648c 65 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 e cells on pPage
1648d 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f ..**.** PTRMAP_O
1648e 56 45 52 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 VERFLOW2: pPage
1648f 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 is an overflow-p
16490 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 age. The pointer
16491 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6e points at the n
16492 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 ext.**
16493 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f overflo
16494 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 w page in the li
16495 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e st..*/.static in
16496 74 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69 6e t modifyPagePoin
16497 74 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 ter(MemPage *pPa
16498 67 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 ge, Pgno iFrom,
16499 50 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 Pgno iTo, u8 eTy
1649a 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 pe){. assert( s
1649b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1649c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 d(pPage->pBt->mu
1649d 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
1649e 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 ( sqlite3PagerIs
1649f 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d writeable(pPage-
164a0 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 69 >pDbPage) );. i
164a1 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 f( eType==PTRMAP
164a2 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 _OVERFLOW2 ){.
164a3 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 /* The pointer
164a4 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 66 is always the f
164a5 69 72 73 74 20 34 20 62 79 74 65 73 20 6f 66 20 irst 4 bytes of
164a6 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73 the page in this
164a7 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 case. */. i
164a8 66 28 20 67 65 74 34 62 79 74 65 28 70 50 61 67 f( get4byte(pPag
164a9 65 2d 3e 61 44 61 74 61 29 21 3d 69 46 72 6f 6d e->aData)!=iFrom
164aa 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
164ab 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
164ac 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 BKPT;. }.
164ad 70 75 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e put4byte(pPage->
164ae 61 44 61 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d aData, iTo);. }
164af 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 69 73 49 else{. u8 isI
164b0 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67 65 2d nitOrig = pPage-
164b1 3e 69 73 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 >isInit;. int
164b2 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c i;. int nCel
164b3 6c 3b 0a 0a 20 20 20 20 62 74 72 65 65 49 6e 69 l;.. btreeIni
164b4 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 tPage(pPage);.
164b5 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d nCell = pPage-
164b6 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 >nCell;.. for
164b7 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 (i=0; i<nCell; i
164b8 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 ++){. u8 *p
164b9 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
164ba 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 pPage, i);.
164bb 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d if( eType==PTRM
164bc 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a AP_OVERFLOW1 ){.
164bd 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f CellInfo
164be 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 info;. b
164bf 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
164c0 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 (pPage, pCell, &
164c1 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 info);. i
164c2 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f f( info.iOverflo
164c3 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 w ){. i
164c4 66 28 20 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 f( iFrom==get4by
164c5 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
164c6 4f 76 65 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 Overflow]) ){.
164c7 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 put4by
164c8 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 te(&pCell[info.i
164c9 4f 76 65 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b Overflow], iTo);
164ca 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 . bre
164cb 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ak;. }.
164cc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
164cd 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
164ce 66 28 20 67 65 74 34 62 79 74 65 28 70 43 65 6c f( get4byte(pCel
164cf 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 l)==iFrom ){.
164d0 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
164d1 70 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 pCell, iTo);.
164d2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
164d3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
164d4 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 }. . if(
164d5 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 i==nCell ){.
164d6 20 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 if( eType!=PT
164d7 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 RMAP_BTREE || .
164d8 20 20 20 20 20 20 20 20 20 67 65 74 34 62 79 74 get4byt
164d9 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b e(&pPage->aData[
164da 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
164db 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 +8])!=iFrom ){.
164dc 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 return SQ
164dd 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
164de 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 T;. }.
164df 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
164e0 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 ->aData[pPage->h
164e1 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f drOffset+8], iTo
164e2 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 );. }.. pP
164e3 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 age->isInit = is
164e4 49 6e 69 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 InitOrig;. }.
164e5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
164e6 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 ;.}.../*.** Move
164e7 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61 62 61 the open databa
164e8 73 65 20 70 61 67 65 20 70 44 62 50 61 67 65 20 se page pDbPage
164e9 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 to location iFre
164ea 65 50 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a ePage in the .**
164eb 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 70 database. The p
164ec 44 62 50 61 67 65 20 72 65 66 65 72 65 6e 63 65 DbPage reference
164ed 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a remains valid..
164ee 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d **.** The isComm
164ef 69 74 20 66 6c 61 67 20 69 6e 64 69 63 61 74 65 it flag indicate
164f0 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 s that there is
164f1 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d no need to remem
164f2 62 65 72 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 ber that.** the
164f3 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f journal needs to
164f4 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66 be sync()ed bef
164f5 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67 ore database pag
164f6 65 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 e pDbPage->pgno
164f7 0a 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 .** can be writt
164f8 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 en to. The calle
164f9 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 r has already pr
164fa 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 omised not to wr
164fb 69 74 65 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 ite to that.** p
164fc 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 age..*/.static i
164fd 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 nt relocatePage(
164fe 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
164ff 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 , /* B
16500 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 tree */. MemPag
16501 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20 20 e *pDbPage,
16502 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 /* Open page
16503 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 to move */. u8
16504 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 eType,
16505 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 /* Pointer
16506 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74 72 map 'type' entr
16507 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a 2f y for pDbPage */
16508 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 . Pgno iPtrPage
16509 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 , /* P
1650a 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67 65 ointer map 'page
1650b 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20 70 -no' entry for p
1650c 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f DbPage */. Pgno
1650d 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20 20 iFreePage,
1650e 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 /* The loca
1650f 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 tion to move pDb
16510 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 Page to */. int
16511 20 69 73 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 isCommit
16512 20 20 20 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 /* isCommi
16513 74 20 66 6c 61 67 20 70 61 73 73 65 64 20 74 6f t flag passed to
16514 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 sqlite3PagerMov
16515 65 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 epage */.){. Me
16516 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67 65 3b mPage *pPtrPage;
16517 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 /* The page t
16518 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 hat contains a p
16519 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50 61 67 ointer to pDbPag
1651a 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 e */. Pgno iDbP
1651b 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 age = pDbPage->p
1651c 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 gno;. Pager *pP
1651d 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 ager = pBt->pPag
1651e 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 er;. int rc;..
1651f 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d assert( eType==
16520 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 PTRMAP_OVERFLOW2
16521 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 || eType==PTRMA
16522 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a P_OVERFLOW1 || .
16523 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50 54 52 eType==PTR
16524 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 MAP_BTREE || eTy
16525 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 pe==PTRMAP_ROOTP
16526 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 AGE );. assert(
16527 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
16528 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
16529 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62 );. assert( pDb
1652a 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 Page->pBt==pBt )
1652b 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 ;.. /* Move pag
1652c 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d 20 69 e iDbPage from i
1652d 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 ts current locat
1652e 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 ion to page numb
1652f 65 72 20 69 46 72 65 65 50 61 67 65 20 2a 2f 0a er iFreePage */.
16530 20 20 54 52 41 43 45 28 28 22 41 55 54 4f 56 41 TRACE(("AUTOVA
16531 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 CUUM: Moving %d
16532 74 6f 20 66 72 65 65 20 70 61 67 65 20 25 64 20 to free page %d
16533 28 70 74 72 20 70 61 67 65 20 25 64 20 74 79 70 (ptr page %d typ
16534 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 e %d)\n", .
16535 20 69 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 iDbPage, iFreeP
16536 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c 20 65 age, iPtrPage, e
16537 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 Type));. rc = s
16538 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 qlite3PagerMovep
16539 61 67 65 28 70 50 61 67 65 72 2c 20 70 44 62 50 age(pPager, pDbP
1653a 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 age->pDbPage, iF
1653b 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 reePage, isCommi
1653c 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 t);. if( rc!=SQ
1653d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
1653e 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 eturn rc;. }.
1653f 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 pDbPage->pgno =
16540 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a iFreePage;.. /*
16541 20 49 66 20 70 44 62 50 61 67 65 20 77 61 73 20 If pDbPage was
16542 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20 74 68 a btree-page, th
16543 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65 20 63 en it may have c
16544 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64 2f 6f hild pages and/o
16545 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 r cells. ** tha
16546 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 t point to overf
16547 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 low pages. The p
16548 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 ointer map entri
16549 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 es for all these
1654a 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 . ** pages need
1654b 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 2e 0a to be changed..
1654c 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 **. ** If pDb
1654d 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65 72 66 Page is an overf
1654e 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20 74 low page, then t
1654f 68 65 20 66 69 72 73 74 20 34 20 62 79 74 65 73 he first 4 bytes
16550 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20 20 2a may store a. *
16551 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 * pointer to a s
16552 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72 66 6c ubsequent overfl
16553 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68 69 73 ow page. If this
16554 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 is the case, th
16555 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e en. ** the poin
16556 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20 74 6f ter map needs to
16557 20 62 65 20 75 70 64 61 74 65 64 20 66 6f 72 20 be updated for
16558 74 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 6f the subsequent o
16559 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 verflow page..
1655a 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 3d 3d */. if( eType==
1655b 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 PTRMAP_BTREE ||
1655c 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f eType==PTRMAP_RO
1655d 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 72 63 OTPAGE ){. rc
1655e 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 = setChildPtrma
1655f 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 ps(pDbPage);.
16560 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
16561 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 OK ){. retu
16562 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d rn rc;. }. }
16563 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e else{. Pgno n
16564 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 extOvfl = get4by
16565 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44 61 74 te(pDbPage->aDat
16566 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 78 74 a);. if( next
16567 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 Ovfl!=0 ){.
16568 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 ptrmapPut(pBt,
16569 6e 65 78 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 nextOvfl, PTRMAP
1656a 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 _OVERFLOW2, iFre
1656b 65 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 ePage, &rc);.
1656c 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
1656d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
1656e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 return rc;.
1656f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
16570 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74 61 62 /* Fix the datab
16571 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 ase pointer on p
16572 61 67 65 20 69 50 74 72 50 61 67 65 20 74 68 61 age iPtrPage tha
16573 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69 44 62 t pointed at iDb
16574 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 Page so. ** tha
16575 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 69 t it points at i
16576 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 FreePage. Also f
16577 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d ix the pointer m
16578 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a ap entry for. *
16579 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f * iPtrPage.. */
1657a 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d 50 54 . if( eType!=PT
1657b 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b RMAP_ROOTPAGE ){
1657c 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 . rc = btreeG
1657d 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 74 72 etPage(pBt, iPtr
1657e 50 61 67 65 2c 20 26 70 50 74 72 50 61 67 65 2c Page, &pPtrPage,
1657f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 0);. if( rc!
16580 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
16581 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
16582 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
16583 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
16584 70 50 74 72 50 61 67 65 2d 3e 70 44 62 50 61 67 pPtrPage->pDbPag
16585 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
16586 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16587 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
16588 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 PtrPage);.
16589 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1658a 0a 20 20 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 . rc = modify
1658b 50 61 67 65 50 6f 69 6e 74 65 72 28 70 50 74 72 PagePointer(pPtr
1658c 50 61 67 65 2c 20 69 44 62 50 61 67 65 2c 20 69 Page, iDbPage, i
1658d 46 72 65 65 50 61 67 65 2c 20 65 54 79 70 65 29 FreePage, eType)
1658e 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 ;. releasePag
1658f 65 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 e(pPtrPage);.
16590 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
16591 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d OK ){. ptrm
16592 61 70 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 apPut(pBt, iFree
16593 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 Page, eType, iPt
16594 72 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 rPage, &rc);.
16595 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
16596 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 rc;.}../* Forwar
16597 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 d declaration re
16598 71 75 69 72 65 64 20 62 79 20 69 6e 63 72 56 61 quired by incrVa
16599 63 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 cuumStep(). */.s
1659a 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 tatic int alloca
1659b 74 65 42 74 72 65 65 50 61 67 65 28 42 74 53 68 teBtreePage(BtSh
1659c 61 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 ared *, MemPage
1659d 2a 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f **, Pgno *, Pgno
1659e 2c 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 , u8);../*.** Pe
1659f 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 rform a single s
165a0 74 65 70 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d tep of an increm
165a1 65 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 ental-vacuum. If
165a2 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 successful,.**
165a3 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
165a4 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f . If there is no
165a5 20 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 work to do (and
165a6 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a therefore no.**
165a7 20 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e point in callin
165a8 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 g this function
165a9 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 again), return S
165aa 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a QLITE_DONE..**.*
165ab 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 6c * More specificl
165ac 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e y, this function
165ad 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 2d attempts to re-
165ae 6f 72 67 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a organize the .**
165af 20 64 61 74 61 62 61 73 65 20 73 6f 20 74 68 61 database so tha
165b0 74 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 t the last page
165b1 6f 66 20 74 68 65 20 66 69 6c 65 20 63 75 72 72 of the file curr
165b2 65 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 ently in use.**
165b3 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 is no longer in
165b4 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 use..**.** If th
165b5 65 20 6e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 e nFin parameter
165b6 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
165b7 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 is function assu
165b8 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 mes.** that the
165b9 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 caller will keep
165ba 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 calling incrVac
165bb 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a uumStep() until.
165bc 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 ** it returns SQ
165bd 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 LITE_DONE or an
165be 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 error, and that
165bf 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e nFin is the.** n
165c0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 umber of pages t
165c1 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 he database file
165c2 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 will contain af
165c3 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f ter this .** pro
165c4 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 cess is complete
165c5 2e 20 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 . If nFin is ze
165c6 72 6f 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 ro, it is assume
165c7 64 20 74 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 d that.** incrVa
165c8 63 75 75 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 cuumStep() will
165c9 62 65 20 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 be called a fini
165ca 74 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d te amount of tim
165cb 65 73 0a 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 es.** which may
165cc 6f 72 20 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 or may not empty
165cd 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 the freelist.
165ce 41 20 66 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 A full autovacuu
165cf 6d 0a 2a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e m.** has nFin>0.
165d0 20 20 41 20 22 50 52 41 47 4d 41 20 69 6e 63 72 A "PRAGMA incr
165d1 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 emental_vacuum"
165d2 68 61 73 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a has nFin==0..*/.
165d3 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63 72 56 static int incrV
165d4 61 63 75 75 6d 53 74 65 70 28 42 74 53 68 61 72 acuumStep(BtShar
165d5 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 ed *pBt, Pgno nF
165d6 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 in, Pgno iLastPg
165d7 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c ){. Pgno nFreeL
165d8 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f ist; /
165d9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 * Number of page
165da 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 s still on the f
165db 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 ree-list */.. a
165dc 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
165dd 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d utex_held(pBt->m
165de 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
165df 74 28 20 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 t( iLastPg>nFin
165e0 29 3b 0a 0a 20 20 69 66 28 20 21 50 54 52 4d 41 );.. if( !PTRMA
165e1 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 4c P_ISPAGE(pBt, iL
165e2 61 73 74 50 67 29 20 26 26 20 69 4c 61 73 74 50 astPg) && iLastP
165e3 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f g!=PENDING_BYTE_
165e4 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 PAGE(pBt) ){.
165e5 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 int rc;. u8
165e6 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 eType;. Pgno
165e7 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e iPtrPage;.. n
165e8 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74 34 62 FreeList = get4b
165e9 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 yte(&pBt->pPage1
165ea 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 ->aData[36]);.
165eb 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73 74 3d if( nFreeList=
165ec 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 =0 ){. retu
165ed 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a rn SQLITE_DONE;.
165ee 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 }.. rc =
165ef 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 ptrmapGet(pBt, i
165f0 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65 2c 20 LastPg, &eType,
165f1 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 &iPtrPage);.
165f2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
165f3 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 K ){. retur
165f4 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
165f5 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 if( eType==PTRMA
165f6 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 P_ROOTPAGE ){.
165f7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
165f8 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a E_CORRUPT_BKPT;.
165f9 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 }.. if( e
165fa 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 Type==PTRMAP_FRE
165fb 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20 69 EPAGE ){. i
165fc 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 f( nFin==0 ){.
165fd 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 /* Remove
165fe 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68 the page from th
165ff 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 e files free-lis
16600 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 t. This is not r
16601 65 71 75 69 72 65 64 0a 20 20 20 20 20 20 20 20 equired.
16602 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f ** if nFin is no
16603 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 n-zero. In that
16604 63 61 73 65 2c 20 74 68 65 20 66 72 65 65 2d 6c case, the free-l
16605 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 ist will be.
16606 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 ** truncated
16607 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72 20 74 to zero after t
16608 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
16609 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 urns, so it does
1660a 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 n't . **
1660b 6d 61 74 74 65 72 20 69 66 20 69 74 20 73 74 69 matter if it sti
1660c 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 ll contains some
1660d 20 67 61 72 62 61 67 65 20 65 6e 74 72 69 65 73 garbage entries
1660e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1660f 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50 Pgno iFreeP
16610 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 g;. MemPa
16611 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 ge *pFreePg;.
16612 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 rc = alloca
16613 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c teBtreePage(pBt,
16614 20 26 70 46 72 65 65 50 67 2c 20 26 69 46 72 65 &pFreePg, &iFre
16615 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 ePg, iLastPg, 1)
16616 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
16617 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
16618 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
16619 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
1661a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 46 assert( iF
1661b 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 reePg==iLastPg )
1661c 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
1661d 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a ePage(pFreePg);.
1661e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c }. } el
1661f 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 se {. Pgno
16620 69 46 72 65 65 50 67 3b 20 20 20 20 20 20 20 20 iFreePg;
16621 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
16622 20 66 72 65 65 20 70 61 67 65 20 74 6f 20 6d 6f free page to mo
16623 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f ve pLastPg to */
16624 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a . MemPage *
16625 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 pLastPg;..
16626 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
16627 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 e(pBt, iLastPg,
16628 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 &pLastPg, 0);.
16629 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1662a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1662b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
1662c 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 }.. /* If
1662d 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 nFin is zero, t
1662e 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 his loop runs ex
1662f 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 actly once and p
16630 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20 20 20 age pLastPg.
16631 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65 64 20 ** is swapped
16632 77 69 74 68 20 74 68 65 20 66 69 72 73 74 20 66 with the first f
16633 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65 64 20 ree page pulled
16634 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c 69 73 off the free lis
16635 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 t.. **.
16636 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 ** On the othe
16637 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 r hand, if nFin
16638 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
16639 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a zero, then keep.
1663a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 ** looping
1663b 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d 70 61 until a free-pa
1663c 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74 68 69 ge located withi
1663d 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46 69 6e n the first nFin
1663e 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 pages. **
1663f 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 66 of the file is f
16640 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ound.. */.
16641 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 do {.
16642 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72 65 65 MemPage *pFree
16643 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d Pg;. rc =
16644 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 allocateBtreePa
16645 67 65 28 70 42 74 2c 20 26 70 46 72 65 65 50 67 ge(pBt, &pFreePg
16646 2c 20 26 69 46 72 65 65 50 67 2c 20 30 2c 20 30 , &iFreePg, 0, 0
16647 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
16648 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
16649 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
1664a 65 50 61 67 65 28 70 4c 61 73 74 50 67 29 3b 0a ePage(pLastPg);.
1664b 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
1664c 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 rc;. }.
1664d 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 releasePa
1664e 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 ge(pFreePg);.
1664f 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 }while( nFin!
16650 3d 30 20 26 26 20 69 46 72 65 65 50 67 3e 6e 46 =0 && iFreePg>nF
16651 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 in );. asse
16652 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c 61 73 rt( iFreePg<iLas
16653 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 tPg );. .
16654 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16655 50 61 67 65 72 57 72 69 74 65 28 70 4c 61 73 74 PagerWrite(pLast
16656 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 Pg->pDbPage);.
16657 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
16658 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
16659 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 rc = relocatePa
1665a 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50 67 2c ge(pBt, pLastPg,
1665b 20 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 eType, iPtrPage
1665c 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 , iFreePg, nFin!
1665d 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 =0);. }.
1665e 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
1665f 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20 20 69 LastPg);. i
16660 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
16661 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
16662 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 rn rc;. }.
16663 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
16664 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 nFin==0 ){. i
16665 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 LastPg--;. wh
16666 69 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 ile( iLastPg==PE
16667 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
16668 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 pBt)||PTRMAP_ISP
16669 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67 AGE(pBt, iLastPg
1666a 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 50 ) ){. if( P
1666b 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 TRMAP_ISPAGE(pBt
1666c 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 , iLastPg) ){.
1666d 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 MemPage *p
1666e 50 67 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 Pg;. int
1666f 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 rc = btreeGetPag
16670 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 e(pBt, iLastPg,
16671 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 &pPg, 0);.
16672 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
16673 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
16674 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
16675 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 }. rc
16676 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 = sqlite3PagerW
16677 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50 61 67 rite(pPg->pDbPag
16678 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 e);. rele
16679 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a 20 20 asePage(pPg);.
1667a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1667b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1667c 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1667d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1667e 7d 0a 20 20 20 20 20 20 69 4c 61 73 74 50 67 2d }. iLastPg-
1667f 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c -;. }. sql
16680 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
16681 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67 eImage(pBt->pPag
16682 65 72 2c 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 er, iLastPg);.
16683 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
16684 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
16685 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 write-transacti
16686 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 on must be opene
16687 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 d before calling
16688 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a this function..
16689 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61 ** It performs a
1668a 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 single unit of
1668b 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 work towards an
1668c 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 incremental vacu
1668d 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 um..**.** If the
1668e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 incremental vac
1668f 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20 uum is finished
16690 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 after this funct
16691 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 ion has run,.**
16692 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 SQLITE_DONE is r
16693 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69 eturned. If it i
16694 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 s not finished,
16695 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 but no error occ
16696 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 urred,.** SQLITE
16697 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e _OK is returned.
16698 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 Otherwise an SQ
16699 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e Lite error code.
1669a 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 .*/.SQLITE_PRIV
1669b 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
1669c 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 treeIncrVacuum(B
1669d 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 tree *p){. int
1669e 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a rc;. BtShared *
1669f 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 pBt = p->pBt;..
166a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
166a1 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 er(p);. assert(
166a2 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 pBt->inTransact
166a3 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ion==TRANS_WRITE
166a4 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d && p->inTrans==
166a5 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
166a6 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 if( !pBt->autoV
166a7 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 acuum ){. rc
166a8 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 = SQLITE_DONE;.
166a9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 }else{. inva
166aa 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f lidateAllOverflo
166ab 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20 20 20 wCache(pBt);.
166ac 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d rc = incrVacuum
166ad 53 74 65 70 28 70 42 74 2c 20 30 2c 20 70 61 67 Step(pBt, 0, pag
166ae 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
166af 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
166b0 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
166b1 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
166b2 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
166b3 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72 69 6f e is called prio
166b4 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 r to sqlite3Page
166b5 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 rCommit when a t
166b6 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 ransaction.** is
166b7 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e commited for an
166b8 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 auto-vacuum dat
166b9 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 abase..**.** If
166ba 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
166bb 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 urned, then *pnT
166bc 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f 20 74 runc is set to t
166bd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
166be 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 es.** the databa
166bf 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 se file should b
166c0 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 64 e truncated to d
166c1 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 uring the commit
166c2 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e process. .** i.
166c3 65 2e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 e. the database
166c4 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67 61 6e has been reorgan
166c5 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c ized so that onl
166c6 79 20 74 68 65 20 66 69 72 73 74 20 2a 70 6e 54 y the first *pnT
166c7 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 runc.** pages ar
166c8 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 e in use..*/.sta
166c9 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61 63 75 tic int autoVacu
166ca 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 umCommit(BtShare
166cb 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 d *pBt){. int r
166cc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
166cd 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d Pager *pPager =
166ce 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 pBt->pPager;.
166cf 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 VVA_ONLY( int nR
166d0 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ef = sqlite3Page
166d1 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 72 rRefcount(pPager
166d2 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 ) );.. assert(
166d3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 sqlite3_mutex_he
166d4 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 ld(pBt->mutex) )
166d5 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 41 6c ;. invalidateAl
166d6 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 lOverflowCache(p
166d7 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28 70 42 Bt);. assert(pB
166d8 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a t->autoVacuum);.
166d9 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 if( !pBt->incr
166da 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 Vacuum ){. Pg
166db 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 no nFin;
166dc 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 /* Number of pa
166dd 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 ges in database
166de 61 66 74 65 72 20 61 75 74 6f 76 61 63 75 75 6d after autovacuum
166df 69 6e 67 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 ing */. Pgno
166e0 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a nFree; /*
166e1 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 Number of pages
166e2 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 on the freelist
166e3 20 69 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 initially */.
166e4 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 Pgno nPtrmap;
166e5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
166e6 66 20 50 74 72 4d 61 70 20 70 61 67 65 73 20 74 f PtrMap pages t
166e7 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 o be freed */.
166e8 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 Pgno iFree;
166e9 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 /* The next
166ea 20 70 61 67 65 20 74 6f 20 62 65 20 66 72 65 65 page to be free
166eb 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e d */. int nEn
166ec 74 72 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e try; /* N
166ed 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
166ee 20 6f 6e 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 on one ptrmap p
166ef 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 age */. Pgno
166f0 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20 2f 2a nOrig; /*
166f1 20 44 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 Database size b
166f2 65 66 6f 72 65 20 66 72 65 65 69 6e 67 20 2a 2f efore freeing */
166f3 0a 0a 20 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 .. nOrig = pa
166f4 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
166f5 29 3b 0a 20 20 20 20 69 66 28 20 50 54 52 4d 41 );. if( PTRMA
166f6 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 4f P_ISPAGE(pBt, nO
166f7 72 69 67 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 rig) || nOrig==P
166f8 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
166f9 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f (pBt) ){. /
166fa 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 * It is not poss
166fb 69 62 6c 65 20 74 6f 20 63 72 65 61 74 65 20 61 ible to create a
166fc 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 68 database for wh
166fd 69 63 68 20 74 68 65 20 66 69 6e 61 6c 20 70 61 ich the final pa
166fe 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 65 ge. ** is e
166ff 69 74 68 65 72 20 61 20 70 6f 69 6e 74 65 72 2d ither a pointer-
16700 6d 61 70 20 70 61 67 65 20 6f 72 20 74 68 65 20 map page or the
16701 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 pending-byte pag
16702 65 2e 20 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 e. If one.
16703 2a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 ** is encountere
16704 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 d, this indicate
16705 73 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 s corruption..
16706 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 */. ret
16707 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 urn SQLITE_CORRU
16708 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a PT_BKPT;. }..
16709 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 nFree = get4
1670a 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 byte(&pBt->pPage
1670b 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 1->aData[36]);.
1670c 20 20 20 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d nEntry = pBt-
1670d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 >usableSize/5;.
1670e 20 20 20 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 nPtrmap = (nF
1670f 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 ree-nOrig+PTRMAP
16710 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 _PAGENO(pBt, nOr
16711 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 ig)+nEntry)/nEnt
16712 72 79 3b 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e ry;. nFin = n
16713 4f 72 69 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e Orig - nFree - n
16714 50 74 72 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 Ptrmap;. if(
16715 6e 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 nOrig>PENDING_BY
16716 54 45 5f 50 41 47 45 28 70 42 74 29 20 26 26 20 TE_PAGE(pBt) &&
16717 6e 46 69 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 nFin<PENDING_BYT
16718 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 E_PAGE(pBt) ){.
16719 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 nFin--;.
1671a 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 }. while( PT
1671b 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c RMAP_ISPAGE(pBt,
1671c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d nFin) || nFin==
1671d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1671e 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 E(pBt) ){.
1671f 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 nFin--;. }.
16720 20 20 69 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 if( nFin>nOrig
16721 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
16722 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a _CORRUPT_BKPT;..
16723 20 20 20 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f for(iFree=nO
16724 72 69 67 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 rig; iFree>nFin
16725 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b && rc==SQLITE_OK
16726 3b 20 69 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 ; iFree--){.
16727 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75 75 rc = incrVacuu
16728 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c mStep(pBt, nFin,
16729 20 69 46 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 iFree);. }.
1672a 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 if( (rc==SQLI
1672b 54 45 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 TE_DONE || rc==S
1672c 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 QLITE_OK) && nFr
1672d 65 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 ee>0 ){. rc
1672e 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1672f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
16730 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e PagerWrite(pBt->
16731 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
16732 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 ;. put4byte
16733 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 (&pBt->pPage1->a
16734 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 Data[32], 0);.
16735 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 42 put4byte(&pB
16736 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
16737 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 [36], 0);.
16738 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e sqlite3PagerTrun
16739 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 cateImage(pBt->p
1673a 50 61 67 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 Pager, nFin);.
1673b 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d }. if( rc!=
1673c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1673d 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 sqlite3PagerR
1673e 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b ollback(pPager);
1673f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 . }. }.. as
16740 73 65 72 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 sert( nRef==sqli
16741 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 te3PagerRefcount
16742 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65 (pPager) );. re
16743 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 turn rc;.}..#els
16744 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 e /* ifndef SQLI
16745 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
16746 55 4d 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 UM */.# define s
16747 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 78 etChildPtrmaps(x
16748 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 ) SQLITE_OK.#end
16749 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 if../*.** This r
1674a 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 outine does the
1674b 66 69 72 73 74 20 70 68 61 73 65 20 6f 66 20 61 first phase of a
1674c 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 two-phase commi
1674d 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 t. This routine
1674e 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72 6f 6c .** causes a rol
1674f 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f lback journal to
16750 20 62 65 20 63 72 65 61 74 65 64 20 28 69 66 20 be created (if
16751 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 it does not alre
16752 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e ady exist).** an
16753 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 d populated with
16754 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 enough informat
16755 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66 20 61 ion so that if a
16756 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 power loss occu
16757 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 rs.** the databa
16758 73 65 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 se can be restor
16759 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e ed to its origin
1675a 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c 61 79 al state by play
1675b 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 ing back.** the
1675c 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 journal. Then t
1675d 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
1675e 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 he journal are f
1675f 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a lushed out to.**
16760 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66 74 65 the disk. Afte
16761 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 r the journal is
16762 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 safely on oxide
16763 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20 74 6f , the changes to
16764 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 the.** database
16765 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 74 are written int
16766 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 o the database f
16767 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 ile and flushed
16768 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 to oxide..** At
16769 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 the end of this
1676a 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 call, the rollba
1676b 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c ck journal still
1676c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a exists on the.*
1676d 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20 61 72 * disk and we ar
1676e 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 e still holding
1676f 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 all locks, so th
16770 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 e transaction ha
16771 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 s not.** committ
16772 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 ed. See sqlite3
16773 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
16774 54 77 6f 28 29 20 66 6f 72 20 74 68 65 20 73 65 Two() for the se
16775 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 74 68 cond phase of th
16776 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 e.** commit proc
16777 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ess..**.** This
16778 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 call is a no-op
16779 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e if no write-tran
1677a 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 saction is curre
1677b 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e 20 70 ntly active on p
1677c 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 Bt..**.** Otherw
1677d 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20 64 61 ise, sync the da
1677e 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 tabase file for
1677f 74 68 65 20 62 74 72 65 65 20 70 42 74 2e 20 7a the btree pBt. z
16780 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f Master points to
16781 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 .** the name of
16782 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c a master journal
16783 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c file that shoul
16784 64 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 d be written int
16785 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 o the.** individ
16786 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ual journal file
16787 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e , or is NULL, in
16788 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 dicating no mast
16789 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 er journal file
1678a 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61 74 61 .** (single data
1678b 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e base transaction
1678c 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 )..**.** When th
1678d 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 is is called, th
1678e 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c e master journal
1678f 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 should already
16790 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 have been.** cre
16791 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 ated, populated
16792 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 with this journa
16793 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 l pointer and sy
16794 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a nced to disk..**
16795 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20 69 73 .** Once this is
16796 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72 65 74 routine has ret
16797 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 urned, the only
16798 74 68 69 6e 67 20 72 65 71 75 69 72 65 64 20 74 thing required t
16799 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 o commit.** the
1679a 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1679b 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74 61 62 n for this datab
1679c 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f 20 64 ase file is to d
1679d 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 elete the journa
1679e 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 l..*/.SQLITE_PRI
1679f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
167a0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
167a1 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20 63 6f One(Btree *p, co
167a2 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 nst char *zMaste
167a3 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
167a4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 QLITE_OK;. if(
167a5 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e p->inTrans==TRAN
167a6 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20 20 42 S_WRITE ){. B
167a7 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
167a8 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 ->pBt;. sqlit
167a9 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b e3BtreeEnter(p);
167aa 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
167ab 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
167ac 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
167ad 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 oVacuum ){.
167ae 20 72 63 20 3d 20 61 75 74 6f 56 61 63 75 75 6d rc = autoVacuum
167af 43 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 Commit(pBt);.
167b0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 if( rc!=SQLIT
167b1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
167b2 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
167b3 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 e(p);. re
167b4 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d turn rc;. }
167b5 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
167b6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
167b7 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e gerCommitPhaseOn
167b8 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a e(pBt->pPager, z
167b9 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 Master, 0);.
167ba 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
167bb 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 e(p);. }. retu
167bc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
167bd 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
167be 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 called from bot
167bf 68 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 h BtreeCommitPha
167c0 73 65 54 77 6f 28 29 20 61 6e 64 20 42 74 72 65 seTwo() and Btre
167c1 65 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 eRollback().** a
167c2 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e t the conclusion
167c3 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f of a transactio
167c4 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 n..*/.static voi
167c5 64 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 d btreeEndTransa
167c6 63 74 69 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b ction(Btree *p){
167c7 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
167c8 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 = p->pBt;. ass
167c9 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
167ca 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 eHoldsMutex(p) )
167cb 3b 0a 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 ;.. btreeClearH
167cc 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a asContent(pBt);.
167cd 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 if( p->inTrans
167ce 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 >TRANS_NONE && p
167cf 2d 3e 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 ->db->activeVdbe
167d0 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 Cnt>1 ){. /*
167d1 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 If there are oth
167d2 65 72 20 61 63 74 69 76 65 20 73 74 61 74 65 6d er active statem
167d3 65 6e 74 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 ents that belong
167d4 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 to this databas
167d5 65 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c e. ** handle,
167d6 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 61 20 downgrade to a
167d7 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 read-only transa
167d8 63 74 69 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 ction. The other
167d9 20 73 74 61 74 65 6d 65 6e 74 73 0a 20 20 20 20 statements.
167da 2a 2a 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 ** may still be
167db 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 reading from the
167dc 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 database. */.
167dd 20 20 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 downgradeAllS
167de 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c haredCacheTableL
167df 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e ocks(p);. p->
167e0 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f inTrans = TRANS_
167e1 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 READ;. }else{.
167e2 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e /* If the han
167e3 64 6c 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 dle had any kind
167e4 20 6f 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 of transaction
167e5 6f 70 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 open, decrement
167e6 74 68 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e the . ** tran
167e7 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 saction count of
167e8 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65 the shared btre
167e9 65 2e 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 e. If the transa
167ea 63 74 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 ction count .
167eb 20 2a 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73 ** reaches 0, s
167ec 65 74 20 74 68 65 20 73 68 61 72 65 64 20 73 74 et the shared st
167ed 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e ate to TRANS_NON
167ee 45 2e 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 E. The unlockBtr
167ef 65 65 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20 eeIfUnused().
167f0 20 2a 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 ** call below w
167f1 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 ill unlock the p
167f2 61 67 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 ager. */. if
167f3 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 ( p->inTrans!=TR
167f4 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 ANS_NONE ){.
167f5 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 clearAllShared
167f6 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 CacheTableLocks(
167f7 70 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e p);. pBt->n
167f8 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 Transaction--;.
167f9 20 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d if( 0==pBt-
167fa 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b >nTransaction ){
167fb 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e . pBt->in
167fc 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 Transaction = TR
167fd 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 ANS_NONE;.
167fe 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
167ff 53 65 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 Set the current
16800 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 transaction stat
16801 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 e to TRANS_NONE
16802 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a and unlock the .
16803 20 20 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20 ** pager if
16804 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 this call closed
16805 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f the only read o
16806 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 r write transact
16807 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e ion. */. p->
16808 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f inTrans = TRANS_
16809 4e 4f 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b NONE;. unlock
1680a 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 BtreeIfUnused(pB
1680b 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 t);. }.. btree
1680c 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a Integrity(p);.}.
1680d 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 ./*.** Commit th
1680e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 e transaction cu
1680f 72 72 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 rrently in progr
16810 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ess..**.** This
16811 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e routine implemen
16812 74 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 ts the second ph
16813 61 73 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 ase of a 2-phase
16814 20 63 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a commit. The.**
16815 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
16816 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f mitPhaseOne() ro
16817 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 utine does the f
16818 69 72 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 irst phase and s
16819 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f hould.** be invo
1681a 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c ked prior to cal
1681b 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
1681c 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 e. The sqlite3B
1681d 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f treeCommitPhaseO
1681e 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 ne().** routine
1681f 64 69 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b did all the work
16820 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f of writing info
16821 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 rmation out to d
16822 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 isk and flushing
16823 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 the.** contents
16824 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 so that they ar
16825 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 e written onto t
16826 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e he disk platter.
16827 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f All this.** ro
16828 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 utine has to do
16829 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 is delete or tru
1682a 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 ncate or zero th
1682b 65 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a e header in the.
1682c 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 ** the rollback
1682d 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 journal (which c
1682e 61 75 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 auses the transa
1682f 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 ction to commit)
16830 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 and.** drop loc
16831 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 ks..**.** This w
16832 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 ill release the
16833 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 write lock on th
16834 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e e database file.
16835 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 If there.** ar
16836 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 e no active curs
16837 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c ors, it also rel
16838 65 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c eases the read l
16839 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ock..*/.SQLITE_P
1683a 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1683b 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 e3BtreeCommitPha
1683c 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70 29 7b seTwo(Btree *p){
1683d 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
1683e 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 = p->pBt;.. sq
1683f 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
16840 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 p);. btreeInteg
16841 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 rity(p);.. /* I
16842 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68 61 73 f the handle has
16843 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 a write-transac
16844 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 tion open, commi
16845 74 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 t the shared-btr
16846 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 ees . ** transa
16847 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 ction and set th
16848 65 20 73 68 61 72 65 64 20 73 74 61 74 65 20 74 e shared state t
16849 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 o TRANS_READ..
1684a 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 */. if( p->inTr
1684b 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
1684c 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a ){. int rc;.
1684d 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d assert( pBt-
1684e 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d >inTransaction==
1684f 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
16850 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e assert( pBt->
16851 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 nTransaction>0 )
16852 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
16853 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 e3PagerCommitPha
16854 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 seTwo(pBt->pPage
16855 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d r);. if( rc!=
16856 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16857 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c sqlite3BtreeL
16858 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 eave(p);. r
16859 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a eturn rc;. }.
1685a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 pBt->inTrans
1685b 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 action = TRANS_R
1685c 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 EAD;. }.. btre
1685d 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 eEndTransaction(
1685e 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 p);. sqlite3Btr
1685f 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 eeLeave(p);. re
16860 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
16861 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 }../*.** Do both
16862 20 70 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d phases of a com
16863 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 mit..*/.SQLITE_P
16864 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16865 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 e3BtreeCommit(Bt
16866 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 ree *p){. int r
16867 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 c;. sqlite3Btre
16868 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 eEnter(p);. rc
16869 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f = sqlite3BtreeCo
1686a 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 mmitPhaseOne(p,
1686b 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 0);. if( rc==SQ
1686c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 LITE_OK ){. r
1686d 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1686e 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 CommitPhaseTwo(p
1686f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
16870 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 BtreeLeave(p);.
16871 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 return rc;.}..#
16872 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a ifndef NDEBUG./*
16873 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
16874 75 6d 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 umber of write-c
16875 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 ursors open on t
16876 68 69 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 his handle. This
16877 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 is for use.** i
16878 6e 20 61 73 73 65 72 74 28 29 20 65 78 70 72 65 n assert() expre
16879 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 ssions, so it is
1687a 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 only compiled i
1687b 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a f NDEBUG is not.
1687c 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a ** defined..**.*
1687d 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 * For the purpos
1687e 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 es of this routi
1687f 6e 65 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 ne, a write-curs
16880 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 or is any cursor
16881 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 that.** is capa
16882 62 6c 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 ble of writing t
16883 6f 20 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 o the databse.
16884 54 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 That means the c
16885 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 ursor was.** ori
16886 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 ginally opened f
16887 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 or writing and t
16888 68 65 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f he cursor has no
16889 74 20 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a t be disabled.**
1688a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 by having its s
1688b 74 61 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 tate changed to
1688c 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f CURSOR_FAULT..*/
1688d 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e .static int coun
1688e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 tWriteCursors(Bt
1688f 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 Shared *pBt){.
16890 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a BtCursor *pCur;.
16891 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 int r = 0;. f
16892 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 or(pCur=pBt->pCu
16893 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 rsor; pCur; pCur
16894 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 =pCur->pNext){.
16895 20 20 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 if( pCur->wrF
16896 6c 61 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 lag && pCur->eSt
16897 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c ate!=CURSOR_FAUL
16898 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 T ) r++; . }.
16899 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 return r;.}.#end
1689a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 if../*.** This r
1689b 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 outine sets the
1689c 73 74 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f state to CURSOR_
1689d 46 41 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 FAULT and the er
1689e 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 ror.** code to e
1689f 72 72 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 rrCode for every
168a0 20 63 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 cursor on BtSha
168a1 72 65 64 20 74 68 61 74 20 70 42 74 72 65 65 0a red that pBtree.
168a2 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a ** references..*
168a3 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f *.** Every curso
168a4 72 20 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e r is tripped, in
168a5 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 cluding cursors
168a6 74 68 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 that belong.** t
168a7 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 o other database
168a8 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 connections tha
168a9 74 20 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 t happen to be s
168aa 68 61 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 haring.** the ca
168ab 63 68 65 20 77 69 74 68 20 70 42 74 72 65 65 2e che with pBtree.
168ac 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
168ad 69 6e 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 ine gets called
168ae 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 when a rollback
168af 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 occurs..** All c
168b0 75 72 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 ursors using the
168b1 20 73 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 same cache must
168b2 20 62 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 be tripped.** t
168b3 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 o prevent them f
168b4 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 rom trying to us
168b5 65 20 74 68 65 20 62 74 72 65 65 20 61 66 74 65 e the btree afte
168b6 72 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 r.** the rollbac
168b7 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b k. The rollback
168b8 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 may have delete
168b9 64 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d d tables.** or m
168ba 6f 76 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c oved root pages,
168bb 20 73 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 so it is not su
168bc 66 66 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 fficient to.** s
168bd 61 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 ave the state of
168be 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 the cursor. Th
168bf 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 e cursor must be
168c0 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e .** invalidated.
168c1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
168c2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
168c3 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f treeTripAllCurso
168c4 72 73 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 rs(Btree *pBtree
168c5 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a , int errCode){.
168c6 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 BtCursor *p;.
168c7 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
168c8 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f er(pBtree);. fo
168c9 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d r(p=pBtree->pBt-
168ca 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 >pCursor; p; p=p
168cb 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e ->pNext){. in
168cc 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 t i;. sqlite3
168cd 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 BtreeClearCursor
168ce 28 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 (p);. p->eSta
168cf 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c te = CURSOR_FAUL
168d0 54 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 T;. p->skipNe
168d1 78 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 xt = errCode;.
168d2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d for(i=0; i<=p-
168d3 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 >iPage; i++){.
168d4 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
168d5 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 p->apPage[i]);.
168d6 20 20 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 p->apPage[i
168d7 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d ] = 0;. }. }
168d8 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
168d9 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a eave(pBtree);.}.
168da 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 ./*.** Rollback
168db 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 the transaction
168dc 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c in progress. Al
168dd 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 l cursors will b
168de 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 e.** invalided b
168df 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e y this operation
168e0 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 . Any attempt t
168e1 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a o use a cursor.*
168e2 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 * that was open
168e3 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 at the beginning
168e4 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 of this operati
168e5 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a on will result.*
168e6 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a * in an error..*
168e7 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 *.** This will r
168e8 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74 65 elease the write
168e9 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 lock on the dat
168ea 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 abase file. If
168eb 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 there.** are no
168ec 61 63 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 active cursors,
168ed 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 it also releases
168ee 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a the read lock..
168ef 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
168f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
168f1 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 eeRollback(Btree
168f2 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a *p){. int rc;.
168f3 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
168f4 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 = p->pBt;. MemP
168f5 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 age *pPage1;..
168f6 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
168f7 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 r(p);. rc = sav
168f8 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
168f9 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 0, 0);.#ifndef
168fa 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 SQLITE_OMIT_SHAR
168fb 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 ED_CACHE. if( r
168fc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
168fd 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 /* This is a
168fe 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 horrible situat
168ff 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 ion. An IO or ma
16900 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 lloc() error occ
16901 75 72 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 urred whilst.
16902 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 ** trying to sa
16903 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 ve cursor positi
16904 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 ons. If this is
16905 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c an automatic rol
16906 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a lback (as. **
16907 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 the result of a
16908 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c constraint, mal
16909 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 loc() failure or
1690a 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 IO error) then
1690b 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 . ** the cach
1690c 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 e may be interna
1690d 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 lly inconsistent
1690e 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 (not contain va
1690f 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 lid trees) so.
16910 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 ** we cannot s
16911 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 imply return the
16912 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 error to the ca
16913 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 ller. Instead, a
16914 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c bort . ** all
16915 20 71 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 queries that ma
16916 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f y be using any o
16917 66 20 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 f the cursors th
16918 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 at failed to sav
16919 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 e.. */. sq
1691a 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c lite3BtreeTripAl
1691b 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b lCursors(p, rc);
1691c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 . }.#endif. bt
1691d 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b reeIntegrity(p);
1691e 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 .. if( p->inTra
1691f 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 ns==TRANS_WRITE
16920 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a ){. int rc2;.
16921 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 52 41 . assert( TRA
16922 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 NS_WRITE==pBt->i
16923 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a nTransaction );.
16924 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 rc2 = sqlite
16925 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 3PagerRollback(p
16926 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 Bt->pPager);.
16927 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 if( rc2!=SQLITE
16928 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 _OK ){. rc
16929 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 = rc2;. }..
1692a 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 /* The rollbac
1692b 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 k may have destr
1692c 6f 79 65 64 20 74 68 65 20 70 50 61 67 65 31 2d oyed the pPage1-
1692d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 >aData value. S
1692e 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 o. ** call bt
1692f 72 65 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 reeGetPage() on
16930 70 61 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 page 1 again to
16931 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 make. ** sure
16932 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 pPage1->aData i
16933 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e s set correctly.
16934 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 */. if( btre
16935 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c eGetPage(pBt, 1,
16936 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 &pPage1, 0)==SQ
16937 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
16938 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 releasePage(pPa
16939 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ge1);. }.
1693a 61 73 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 assert( countWri
1693b 74 65 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d teCursors(pBt)==
1693c 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 0 );. pBt->in
1693d 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 Transaction = TR
1693e 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 ANS_READ;. }..
1693f 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 btreeEndTransac
16940 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 tion(p);. sqlit
16941 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
16942 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
16943 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 ./*.** Start a s
16944 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e tatement subtran
16945 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 saction. The sub
16946 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 transaction can
16947 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a can be rolled.**
16948 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e back independen
16949 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 tly of the main
1694a 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 transaction. You
1694b 20 6d 75 73 74 20 73 74 61 72 74 20 61 20 74 72 must start a tr
1694c 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 ansaction .** be
1694d 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 fore starting a
1694e 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 subtransaction.
1694f 54 68 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 The subtransacti
16950 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f on is ended auto
16951 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 matically .** if
16952 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 the main transa
16953 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 ction commits or
16954 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a rolls back..**.
16955 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 ** Statement sub
16956 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 transactions are
16957 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 used around ind
16958 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 ividual SQL stat
16959 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 ements.** that a
1695a 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 re contained wit
1695b 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f hin a BEGIN...CO
1695c 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 MMIT block. If
1695d 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 a constraint.**
1695e 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 error occurs wit
1695f 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e hin the statemen
16960 74 2c 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 t, the effect of
16961 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d that one statem
16962 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f ent.** can be ro
16963 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 lled back withou
16964 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c t having to roll
16965 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 back the entire
16966 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a transaction..**.
16967 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 ** A statement s
16968 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 ub-transaction i
16969 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 s implemented as
1696a 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 an anonymous sa
1696b 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 vepoint. The.**
1696c 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 value passed as
1696d 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
1696e 65 74 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 eter is the tota
1696f 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 l number of save
16970 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 points,.** inclu
16971 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f ding the new ano
16972 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 nymous savepoint
16973 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d , open on the B-
16974 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 Tree. i.e. if th
16975 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 ere.** are no ac
16976 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 tive savepoints
16977 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 and no other sta
16978 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 tement-transacti
16979 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 ons open,.** iSt
1697a 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 atement is 1. Th
1697b 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 is anonymous sav
1697c 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 epoint can be re
1697d 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 leased or rolled
1697e 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 back.** using t
1697f 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 he sqlite3BtreeS
16980 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 avepoint() funct
16981 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ion..*/.SQLITE_P
16982 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16983 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 e3BtreeBeginStmt
16984 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 (Btree *p, int i
16985 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e Statement){. in
16986 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 t rc;. BtShared
16987 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a *pBt = p->pBt;.
16988 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e sqlite3BtreeEn
16989 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 ter(p);. assert
1698a 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 ( p->inTrans==TR
1698b 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
1698c 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 ssert( pBt->read
1698d 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Only==0 );. ass
1698e 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e ert( iStatement>
1698f 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 0 );. assert( i
16990 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d Statement>p->db-
16991 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 >nSavepoint );.
16992 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e if( NEVER(p->in
16993 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 Trans!=TRANS_WRI
16994 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f TE || pBt->readO
16995 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d nly) ){. rc =
16996 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c SQLITE_INTERNAL
16997 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 ;. }else{. a
16998 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 ssert( pBt->inTr
16999 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
1699a 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a _WRITE );. /*
1699b 20 41 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 At the pager le
1699c 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 vel, a statement
1699d 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1699e 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 a savepoint with
1699f 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 . ** an index
169a0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c greater than al
169a1 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 l savepoints cre
169a2 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 ated explicitly
169a3 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c using. ** SQL
169a4 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 statements. It
169a5 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 is illegal to op
169a6 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 en, release or r
169a7 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 ollback any.
169a8 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e ** such savepoin
169a9 74 73 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 ts while the sta
169aa 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 tement transacti
169ab 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 on savepoint is
169ac 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 active.. */.
169ad 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
169ae 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e agerOpenSavepoin
169af 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 t(pBt->pPager, i
169b0 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a Statement);. }.
169b1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 sqlite3BtreeLe
169b2 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e ave(p);. return
169b3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 rc;.}../*.** Th
169b4 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
169b5 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 t to this functi
169b6 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 on, op, is alway
169b7 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c s SAVEPOINT_ROLL
169b8 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 BACK.** or SAVEP
169b9 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 OINT_RELEASE. Th
169ba 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 is function eith
169bb 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 er releases or r
169bc 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a olls back the.**
169bd 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 savepoint ident
169be 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 ified by paramet
169bf 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 er iSavepoint, d
169c0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 epending on the
169c1 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e value .** of op.
169c2 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c .**.** Normally,
169c3 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 iSavepoint is g
169c4 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 reater than or e
169c5 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f qual to zero. Ho
169c6 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a wever, if op is.
169c7 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c ** SAVEPOINT_ROL
169c8 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 LBACK, then iSav
169c9 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 epoint may also
169ca 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 be -1. In this c
169cb 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 ase the .** cont
169cc 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 ents of the enti
169cd 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 re transaction a
169ce 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 re rolled back.
169cf 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e This is differen
169d0 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d t.** from a norm
169d1 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 al transaction r
169d2 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c ollback, as no l
169d3 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 ocks are release
169d4 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 d and the.** tra
169d5 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 nsaction remains
169d6 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 open..*/.SQLITE
169d7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
169d8 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 ite3BtreeSavepoi
169d9 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 nt(Btree *p, int
169da 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f op, int iSavepo
169db 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d int){. int rc =
169dc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 SQLITE_OK;. if
169dd 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e ( p && p->inTran
169de 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 s==TRANS_WRITE )
169df 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a {. BtShared *
169e0 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 pBt = p->pBt;.
169e1 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 assert( op==SA
169e2 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 VEPOINT_RELEASE
169e3 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 || op==SAVEPOINT
169e4 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 _ROLLBACK );.
169e5 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f assert( iSavepo
169e6 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 int>=0 || (iSave
169e7 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d point==-1 && op=
169e8 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 =SAVEPOINT_ROLLB
169e9 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 ACK) );. sqli
169ea 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 te3BtreeEnter(p)
169eb 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 ;. rc = sqlit
169ec 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 e3PagerSavepoint
169ed 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 (pBt->pPager, op
169ee 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 , iSavepoint);.
169ef 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
169f0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 E_OK ){. rc
169f1 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 = newDatabase(p
169f2 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 Bt);. }. s
169f3 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
169f4 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 (p);. }. retur
169f5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 n rc;.}../*.** C
169f6 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 reate a new curs
169f7 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 or for the BTree
169f8 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f whose root is o
169f9 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 n the page.** iT
169fa 61 62 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d able. If a read-
169fb 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 only cursor is r
169fc 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 equested, it is
169fd 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 assumed that.**
169fe 74 68 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 the caller alrea
169ff 64 79 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 dy has at least
16a00 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e a read-only tran
16a01 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 saction open.**
16a02 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 on the database
16a03 61 6c 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 already. If a wr
16a04 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 ite-cursor is re
16a05 71 75 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a quested, then.**
16a06 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 the caller is a
16a07 73 73 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 ssumed to have a
16a08 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 n open write tra
16a09 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 nsaction..**.**
16a0a 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 If wrFlag==0, th
16a0b 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 en the cursor ca
16a0c 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 n only be used f
16a0d 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 or reading..** I
16a0e 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 f wrFlag==1, the
16a0f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e n the cursor can
16a10 20 62 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 be used for rea
16a11 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 ding or for.** w
16a12 72 69 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 riting if other
16a13 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 conditions for w
16a14 72 69 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 riting are also
16a15 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 met. These.** a
16a16 72 65 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e re the condition
16a17 73 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d s that must be m
16a18 65 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 et in order for
16a19 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 writing to.** be
16a1a 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 allowed:.**.**
16a1b 31 3a 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 1: The cursor m
16a1c 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 ust have been op
16a1d 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 ened with wrFlag
16a1e 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 ==1.**.** 2: Ot
16a1f 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e her database con
16a20 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 nections that sh
16a21 61 72 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 are the same pag
16a22 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 er cache.**
16a23 62 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f but which are no
16a24 74 20 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e t in the READ_UN
16a25 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 COMMITTED state
16a26 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 may not have.**
16a27 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e cursors open
16a28 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 with wrFlag==0
16a29 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c on the same tabl
16a2a 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a e. Otherwise.**
16a2b 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 the changes
16a2c 20 6d 61 64 65 20 62 79 20 74 68 69 73 20 77 72 made by this wr
16a2d 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 ite cursor would
16a2e 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a be visible to.*
16a2f 2a 20 20 20 20 20 74 68 65 20 72 65 61 64 20 63 * the read c
16a30 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 ursors in the ot
16a31 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e her database con
16a32 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 nection..**.** 3
16a33 3a 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 : The database
16a34 6d 75 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 must be writable
16a35 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e (not on read-on
16a36 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 ly media).**.**
16a37 34 3a 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 4: There must b
16a38 65 20 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e e an active tran
16a39 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e saction..**.** N
16a3a 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f o checking is do
16a3b 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 ne to make sure
16a3c 74 68 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 that page iTable
16a3d 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a really is the.*
16a3e 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 * root page of a
16a3f 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 b-tree. If it
16a40 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 is not, then the
16a41 20 63 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 cursor acquired
16a42 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 .** will not wor
16a43 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a k correctly..**.
16a44 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 ** It is assumed
16a45 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 that the sqlite
16a46 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 3BtreeCursorZero
16a47 28 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c () has been call
16a48 65 64 0a 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f ed.** on pCur to
16a49 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 initialize the
16a4a 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 70 72 69 memory space pri
16a4b 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 or to invoking t
16a4c 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a his routine..*/.
16a4d 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 static int btree
16a4e 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 Cursor(. Btree
16a4f 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
16a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a51 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f /* The btree */
16a52 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 . int iTable,
16a53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a54 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f /* Roo
16a55 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 t page of table
16a56 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 to open */. int
16a57 20 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 wrFlag,
16a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16a59 20 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 /* 1 to writ
16a5a 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e. 0 read-only *
16a5b 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e /. struct KeyIn
16a5c 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 fo *pKeyInfo,
16a5d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 /* Fi
16a5e 72 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 rst arg to compa
16a5f 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a rison function *
16a60 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
16a61 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ur
16a62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
16a63 61 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 ace for new curs
16a64 6f 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 or */.){. BtSha
16a65 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
16a66 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
16a67 20 20 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 /* Shared b-tr
16a68 65 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 ee handle */..
16a69 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
16a6a 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 treeHoldsMutex(p
16a6b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 ) );. assert( w
16a6c 72 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c rFlag==0 || wrFl
16a6d 61 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 ag==1 );.. /* T
16a6e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 he following ass
16a6f 65 72 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 ert statements v
16a70 65 72 69 66 79 20 74 68 61 74 20 69 66 20 74 68 erify that if th
16a71 69 73 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 is is a sharable
16a72 20 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 . ** b-tree da
16a73 74 61 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e tabase, the conn
16a74 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e ection is holdin
16a75 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 g the required t
16a76 61 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a able locks, . *
16a77 2a 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 * and that no ot
16a78 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 her connection h
16a79 61 73 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 as any open curs
16a7a 6f 72 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 or that conflict
16a7b 73 20 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 s with . ** thi
16a7c 73 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 s lock. */. as
16a7d 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 sert( hasSharedC
16a7e 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
16a7f 20 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 iTable, pKeyInf
16a80 6f 21 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 o!=0, wrFlag+1)
16a81 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 );. assert( wrF
16a82 6c 61 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 lag==0 || !hasRe
16a83 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 adConflicts(p, i
16a84 54 61 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 Table) );.. /*
16a85 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 Assert that the
16a86 63 61 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 caller has opene
16a87 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 d the required t
16a88 72 61 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 ransaction. */.
16a89 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
16a8a 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 ans>TRANS_NONE )
16a8b 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c ;. assert( wrFl
16a8c 61 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 ag==0 || p->inTr
16a8d 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
16a8e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 );. assert( pB
16a8f 74 2d 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 t->pPage1 && pBt
16a90 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 ->pPage1->aData
16a91 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 );.. if( NEVER(
16a92 77 72 46 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 wrFlag && pBt->r
16a93 65 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 eadOnly) ){.
16a94 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 return SQLITE_RE
16a95 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 ADONLY;. }. if
16a96 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 ( iTable==1 && p
16a97 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
16a98 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 t)==0 ){. ret
16a99 75 72 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 urn SQLITE_EMPTY
16a9a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 ;. }.. /* Now
16a9b 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 that no other er
16a9c 72 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 rors can occur,
16a9d 66 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 finish filling i
16a9e 6e 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 n the BtCursor.
16a9f 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e ** variables an
16aa0 64 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f d link the curso
16aa1 72 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61 r into the BtSha
16aa2 72 65 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 red list. */.
16aa3 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d pCur->pgnoRoot =
16aa4 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 (Pgno)iTable;.
16aa5 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d pCur->iPage = -
16aa6 31 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 1;. pCur->pKeyI
16aa7 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a nfo = pKeyInfo;.
16aa8 20 20 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d pCur->pBtree =
16aa9 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 p;. pCur->pBt
16aaa 3d 20 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 = pBt;. pCur->w
16aab 72 46 6c 61 67 20 3d 20 28 75 38 29 77 72 46 6c rFlag = (u8)wrFl
16aac 61 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 ag;. pCur->pNex
16aad 74 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 t = pBt->pCursor
16aae 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e ;. if( pCur->pN
16aaf 65 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d ext ){. pCur-
16ab0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 >pNext->pPrev =
16ab1 70 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d pCur;. }. pBt-
16ab2 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b >pCursor = pCur;
16ab3 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 . pCur->eState
16ab4 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 = CURSOR_INVALID
16ab5 3b 0a 20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 ;. pCur->cached
16ab6 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 Rowid = 0;. ret
16ab7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d urn SQLITE_OK;.}
16ab8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16ab9 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16aba 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 Cursor(. Btree
16abb 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 *p,
16abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16abd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 /* The btr
16abe 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 ee */. int iTab
16abf 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 le,
16ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ac1 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 /* Root page
16ac2 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 of table to ope
16ac3 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 n */. int wrFla
16ac4 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g,
16ac5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16ac6 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 /* 1 to write
16ac7 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f . 0 read-only */
16ac8 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 . struct KeyInf
16ac9 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 o *pKeyInfo,
16aca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
16acb 2a 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 * First arg to x
16acc 43 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 Compare() */. B
16acd 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 tCursor *pCur
16ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16acf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 /* Wr
16ad0 69 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 ite new cursor h
16ad1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 ere */.){. int
16ad2 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 rc;. sqlite3Btr
16ad3 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 eeEnter(p);. rc
16ad4 20 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 = btreeCursor(p
16ad5 2c 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 , iTable, wrFlag
16ad6 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 , pKeyInfo, pCur
16ad7 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 );. sqlite3Btre
16ad8 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 eLeave(p);. ret
16ad9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
16ada 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 Return the size
16adb 20 6f 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f of a BtCursor o
16adc 62 6a 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a bject in bytes..
16add 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 **.** This inter
16ade 66 61 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 faces is needed
16adf 73 6f 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 so that users of
16ae0 20 63 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 cursors can pre
16ae1 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 allocate.** suff
16ae2 69 63 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 icient storage t
16ae3 6f 20 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e o hold a cursor.
16ae4 20 20 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f The BtCursor o
16ae5 62 6a 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a bject is opaque.
16ae6 2a 2a 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 ** to users so t
16ae7 68 65 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 hey cannot do th
16ae8 65 20 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 e sizeof() thems
16ae9 65 6c 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 elves - they mus
16aea 74 20 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 t call.** this r
16aeb 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 outine..*/.SQLIT
16aec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
16aed 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 lite3BtreeCursor
16aee 53 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 Size(void){. re
16aef 74 75 72 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 turn ROUND8(size
16af0 6f 66 28 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d of(BtCursor));.}
16af1 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
16af2 7a 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 ze memory that w
16af3 69 6c 6c 20 62 65 20 63 6f 6e 76 65 72 74 65 64 ill be converted
16af4 20 69 6e 74 6f 20 61 20 42 74 43 75 72 73 6f 72 into a BtCursor
16af5 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 object..**.** T
16af6 68 65 20 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 he simple approa
16af7 63 68 20 68 65 72 65 20 77 6f 75 6c 64 20 62 65 ch here would be
16af8 20 74 6f 20 6d 65 6d 73 65 74 28 29 20 74 68 65 to memset() the
16af9 20 65 6e 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a entire object.*
16afa 2a 20 74 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20 * to zero. But
16afb 69 74 20 74 75 72 6e 73 20 6f 75 74 20 74 68 61 it turns out tha
16afc 74 20 74 68 65 20 61 70 50 61 67 65 5b 5d 20 61 t the apPage[] a
16afd 6e 64 20 61 69 49 64 78 5b 5d 20 61 72 72 61 79 nd aiIdx[] array
16afe 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64 s.** do not need
16aff 20 74 6f 20 62 65 20 7a 65 72 6f 65 64 20 61 6e to be zeroed an
16b00 64 20 74 68 65 79 20 61 72 65 20 6c 61 72 67 65 d they are large
16b01 2c 20 73 6f 20 77 65 20 63 61 6e 20 73 61 76 65 , so we can save
16b02 20 61 20 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e a lot.** of run
16b03 2d 74 69 6d 65 20 62 79 20 73 6b 69 70 70 69 6e -time by skippin
16b04 67 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 g the initializa
16b05 74 69 6f 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c tion of those el
16b06 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 ements..*/.SQLIT
16b07 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
16b08 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f qlite3BtreeCurso
16b09 72 5a 65 72 6f 28 42 74 43 75 72 73 6f 72 20 2a rZero(BtCursor *
16b0a 70 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 p){. memset(p,
16b0b 30 2c 20 6f 66 66 73 65 74 6f 66 28 42 74 43 75 0, offsetof(BtCu
16b0c 72 73 6f 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d rsor, iPage));.}
16b0d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
16b0e 63 61 63 68 65 64 20 72 6f 77 69 64 20 76 61 6c cached rowid val
16b0f 75 65 20 6f 66 20 65 76 65 72 79 20 63 75 72 73 ue of every curs
16b10 6f 72 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 or in the same d
16b11 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 atabase file.**
16b12 61 73 20 70 43 75 72 20 61 6e 64 20 68 61 76 69 as pCur and havi
16b13 6e 67 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 ng the same root
16b14 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 page number as
16b15 70 43 75 72 2e 20 20 54 68 65 20 76 61 6c 75 65 pCur. The value
16b16 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 is.** set to iR
16b17 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 owid..**.** Only
16b18 20 70 6f 73 69 74 69 76 65 20 72 6f 77 69 64 20 positive rowid
16b19 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69 values are consi
16b1a 64 65 72 65 64 20 76 61 6c 69 64 20 66 6f 72 20 dered valid for
16b1b 74 68 69 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 this cache..** T
16b1c 68 65 20 63 61 63 68 65 20 69 73 20 69 6e 69 74 he cache is init
16b1d 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c ialized to zero,
16b1e 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20 69 indicating an i
16b1f 6e 76 61 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a nvalid cache..**
16b20 20 41 20 62 74 72 65 65 20 77 69 6c 6c 20 77 6f A btree will wo
16b21 72 6b 20 66 69 6e 65 20 77 69 74 68 20 7a 65 72 rk fine with zer
16b22 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f o or negative ro
16b23 77 69 64 73 2e 20 20 57 65 20 6a 75 73 74 20 63 wids. We just c
16b24 61 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a annot.** cache z
16b25 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 ero or negative
16b26 72 6f 77 69 64 73 2c 20 77 68 69 63 68 20 6d 65 rowids, which me
16b27 61 6e 73 20 74 61 62 6c 65 73 20 74 68 61 74 20 ans tables that
16b28 75 73 65 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e use zero or.** n
16b29 65 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 6d egative rowids m
16b2a 69 67 68 74 20 72 75 6e 20 61 20 6c 69 74 74 6c ight run a littl
16b2b 65 20 73 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 e slower. But i
16b2c 6e 20 70 72 61 63 74 69 63 65 2c 20 7a 65 72 6f n practice, zero
16b2d 0a 2a 2a 20 6f 72 20 6e 65 67 61 74 69 76 65 20 .** or negative
16b2e 72 6f 77 69 64 73 20 61 72 65 20 76 65 72 79 20 rowids are very
16b2f 75 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 uncommon so this
16b30 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 should not be a
16b31 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c problem..*/.SQL
16b32 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 ITE_PRIVATE void
16b33 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 sqlite3BtreeSet
16b34 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 CachedRowid(BtCu
16b35 72 73 6f 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 rsor *pCur, sqli
16b36 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 te3_int64 iRowid
16b37 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 ){. BtCursor *p
16b38 3b 0a 20 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e ;. for(p=pCur->
16b39 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b pBt->pCursor; p;
16b3a 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 p=p->pNext){.
16b3b 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f if( p->pgnoRoo
16b3c 74 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f t==pCur->pgnoRoo
16b3d 74 20 29 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 t ) p->cachedRow
16b3e 69 64 20 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d id = iRowid;. }
16b3f 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
16b40 3e 63 61 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 >cachedRowid==iR
16b41 6f 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a owid );.}../*.**
16b42 20 52 65 74 75 72 6e 20 74 68 65 20 63 61 63 68 Return the cach
16b43 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 ed rowid for the
16b44 20 67 69 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 given cursor.
16b45 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 A negative or ze
16b46 72 6f 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c ro.** return val
16b47 75 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 ue indicates tha
16b48 74 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68 t the rowid cach
16b49 65 20 69 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 e is invalid and
16b4a 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 should be.** ig
16b4b 6e 6f 72 65 64 2e 20 20 49 66 20 74 68 65 20 72 nored. If the r
16b4c 6f 77 69 64 20 63 61 63 68 65 20 68 61 73 20 6e owid cache has n
16b4d 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e ever before been
16b4e 20 73 65 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 set, then a.**
16b4f 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 zero is returned
16b50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
16b51 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 ATE sqlite3_int6
16b52 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 4 sqlite3BtreeGe
16b53 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 tCachedRowid(BtC
16b54 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
16b55 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 return pCur->cac
16b56 68 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a hedRowid;.}../*.
16b57 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f ** Close a curso
16b58 72 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 r. The read loc
16b59 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 k on the databas
16b5a 65 20 66 69 6c 65 20 69 73 20 72 65 6c 65 61 73 e file is releas
16b5b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c ed.** when the l
16b5c 61 73 74 20 63 75 72 73 6f 72 20 69 73 20 63 6c ast cursor is cl
16b5d 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f osed..*/.SQLITE_
16b5e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
16b5f 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 te3BtreeCloseCur
16b60 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 sor(BtCursor *pC
16b61 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 ur){. Btree *pB
16b62 74 72 65 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 tree = pCur->pBt
16b63 72 65 65 3b 0a 20 20 69 66 28 20 70 42 74 72 65 ree;. if( pBtre
16b64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a e ){. int i;.
16b65 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 BtShared *pB
16b66 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 t = pCur->pBt;.
16b67 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 sqlite3BtreeE
16b68 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20 nter(pBtree);.
16b69 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c sqlite3BtreeCl
16b6a 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29 3b earCursor(pCur);
16b6b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 . if( pCur->p
16b6c 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 43 Prev ){. pC
16b6d 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 ur->pPrev->pNext
16b6e 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a = pCur->pNext;.
16b6f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
16b70 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 pBt->pCursor =
16b71 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 pCur->pNext;.
16b72 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d }. if( pCur-
16b73 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 >pNext ){.
16b74 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 pCur->pNext->pPr
16b75 65 76 20 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 ev = pCur->pPrev
16b76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 ;. }. for(
16b77 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 i=0; i<=pCur->iP
16b78 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 age; i++){.
16b79 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 releasePage(pCu
16b7a 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 r->apPage[i]);.
16b7b 20 20 20 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 }. unlockB
16b7c 74 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 treeIfUnused(pBt
16b7d 29 3b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 );. invalidat
16b7e 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 70 eOverflowCache(p
16b7f 43 75 72 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c Cur);. /* sql
16b80 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 29 3b ite3_free(pCur);
16b81 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 */. sqlite3B
16b82 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 treeLeave(pBtree
16b83 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
16b84 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
16b85 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 .** Make sure th
16b86 65 20 42 74 43 75 72 73 6f 72 2a 20 67 69 76 65 e BtCursor* give
16b87 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e n in the argumen
16b88 74 20 68 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a t has a valid.**
16b89 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 BtCursor.info s
16b8a 74 72 75 63 74 75 72 65 2e 20 20 49 66 20 69 74 tructure. If it
16b8b 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 is not already
16b8c 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 valid, call.** b
16b8d 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 treeParseCell()
16b8e 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a to fill it in..*
16b8f 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e *.** BtCursor.in
16b90 66 6f 20 69 73 20 61 20 63 61 63 68 65 20 6f 66 fo is a cache of
16b91 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e the information
16b92 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 in the current
16b93 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 cell..** Using t
16b94 68 69 73 20 63 61 63 68 65 20 72 65 64 75 63 65 his cache reduce
16b95 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
16b96 63 61 6c 6c 73 20 74 6f 20 62 74 72 65 65 50 61 calls to btreePa
16b97 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a rseCell()..**.**
16b98 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 2007-06-25: Th
16b99 65 72 65 20 69 73 20 61 20 62 75 67 20 69 6e 20 ere is a bug in
16b9a 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 some versions of
16b9b 20 4d 53 56 43 20 74 68 61 74 20 63 61 75 73 65 MSVC that cause
16b9c 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 the.** compiler
16b9d 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e 20 67 to crash when g
16b9e 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 etCellInfo() is
16b9f 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 implemented as a
16ba0 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 macro..** But t
16ba1 68 65 72 65 20 69 73 20 61 20 6d 65 61 73 75 72 here is a measur
16ba2 65 61 62 6c 65 20 73 70 65 65 64 20 61 64 76 61 eable speed adva
16ba3 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67 20 74 ntage to using t
16ba4 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a he macro on gcc.
16ba5 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20 63 6f ** (when less co
16ba6 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 mpiler optimizat
16ba7 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 ions like -Os or
16ba8 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20 61 6e -O0 are used an
16ba9 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 d the.** compile
16baa 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 r is not doing a
16bab 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e gressive inlinin
16bac 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65 20 61 g.) So we use a
16bad 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a real function.*
16bae 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 * for MSVC and a
16baf 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65 72 79 macro for every
16bb0 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 thing else. Tic
16bb1 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 ket #2457..*/.#i
16bb2 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 fndef NDEBUG. s
16bb3 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 tatic void asser
16bb4 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 tCellInfo(BtCurs
16bb5 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 or *pCur){. C
16bb6 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 ellInfo info;.
16bb7 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 int iPage = pC
16bb8 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d ur->iPage;. m
16bb9 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 emset(&info, 0,
16bba 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 sizeof(info));.
16bbb 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c btreeParseCel
16bbc 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 l(pCur->apPage[i
16bbd 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 Page], pCur->aiI
16bbe 64 78 5b 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f dx[iPage], &info
16bbf 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d );. assert( m
16bc0 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 emcmp(&info, &pC
16bc1 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 ur->info, sizeof
16bc2 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 (info))==0 );.
16bc3 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e }.#else. #defin
16bc4 65 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f e assertCellInfo
16bc5 28 78 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 (x).#endif.#ifde
16bc6 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 f _MSC_VER. /*
16bc7 55 73 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 Use a real funct
16bc8 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 ion in MSVC to w
16bc9 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 73 20 ork around bugs
16bca 69 6e 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 in that compiler
16bcb 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f . */. static vo
16bcc 69 64 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 id getCellInfo(B
16bcd 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
16bce 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e if( pCur->in
16bcf 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 fo.nSize==0 ){.
16bd0 20 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d int iPage =
16bd1 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 pCur->iPage;.
16bd2 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 btreeParseCe
16bd3 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b ll(pCur->apPage[
16bd4 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 iPage],pCur->aiI
16bd5 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d dx[iPage],&pCur-
16bd6 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 >info);. pC
16bd7 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 ur->validNKey =
16bd8 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
16bd9 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e assertCellIn
16bda 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a fo(pCur);. }.
16bdb 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 }.#else /* if
16bdc 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a not _MSC_VER */.
16bdd 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f /* Use a macro
16bde 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f in all other co
16bdf 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20 mpilers so that
16be0 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 the function is
16be1 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 inlined */.#defi
16be2 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 ne getCellInfo(p
16be3 43 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20 Cur)
16be4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16be5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16be6 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 \. if
16be7 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 ( pCur->info.nSi
16be8 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 ze==0 ){
16be9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16beb 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 \.
16bec 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 int iPage = pCu
16bed 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20 r->iPage;
16bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 \.
16bf1 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c btreeParseCell
16bf2 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 (pCur->apPage[iP
16bf3 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 age],pCur->aiIdx
16bf4 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 [iPage],&pCur->i
16bf5 6e 66 6f 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 nfo); \. pCur
16bf6 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b ->validNKey = 1;
16bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfa 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 \. }else
16bfb 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
16bfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16bff 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 61 73 \. as
16c00 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 sertCellInfo(pCu
16c01 72 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 r);
16c02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16c04 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 \. }.#
16c05 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 endif /* _MSC_VE
16c06 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 R */..#ifndef ND
16c07 45 42 55 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 EBUG /* The nex
16c08 74 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 6f t routine used o
16c09 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73 65 72 nly within asser
16c0a 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a t() statements *
16c0b 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 /./*.** Return t
16c0c 72 75 65 20 69 66 20 74 68 65 20 67 69 76 65 6e rue if the given
16c0d 20 42 74 43 75 72 73 6f 72 20 69 73 20 76 61 6c BtCursor is val
16c0e 69 64 2e 20 20 41 20 76 61 6c 69 64 20 63 75 72 id. A valid cur
16c0f 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 sor is one.** th
16c10 61 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 at is currently
16c11 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f pointing to a ro
16c12 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 w in a (non-empt
16c13 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 y) table..** Thi
16c14 73 20 69 73 20 61 20 76 65 72 69 66 69 63 61 74 s is a verificat
16c15 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 73 20 75 ion routine is u
16c16 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 sed only within
16c17 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 assert() stateme
16c18 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 nts..*/.SQLITE_P
16c19 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
16c1a 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 e3BtreeCursorIsV
16c1b 61 6c 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 alid(BtCursor *p
16c1c 43 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 Cur){. return p
16c1d 43 75 72 20 26 26 20 70 43 75 72 2d 3e 65 53 74 Cur && pCur->eSt
16c1e 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
16c1f 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e D;.}.#endif /* N
16c20 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 DEBUG */../*.**
16c21 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 Set *pSize to th
16c22 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75 e size of the bu
16c23 66 66 65 72 20 6e 65 65 64 65 64 20 74 6f 20 68 ffer needed to h
16c24 6f 6c 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 old the value of
16c25 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 .** the key for
16c26 74 68 65 20 63 75 72 72 65 6e 74 20 65 6e 74 72 the current entr
16c27 79 2e 20 20 49 66 20 74 68 65 20 63 75 72 73 6f y. If the curso
16c28 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e r is not pointin
16c29 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 g.** to a valid
16c2a 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 entry, *pSize is
16c2b 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a set to 0. .**.*
16c2c 2a 20 46 6f 72 20 61 20 74 61 62 6c 65 20 77 69 * For a table wi
16c2d 74 68 20 74 68 65 20 49 4e 54 4b 45 59 20 66 6c th the INTKEY fl
16c2e 61 67 20 73 65 74 2c 20 74 68 69 73 20 72 6f 75 ag set, this rou
16c2f 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 tine returns the
16c30 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 key.** itself,
16c31 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f not the number o
16c32 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b f bytes in the k
16c33 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 ey..**.** The ca
16c34 6c 6c 65 72 20 6d 75 73 74 20 70 6f 73 69 74 69 ller must positi
16c35 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 70 72 on the cursor pr
16c36 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 ior to invoking
16c37 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a this routine..**
16c38 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e .** This routin
16c39 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 e cannot fail.
16c3a 49 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e It always return
16c3b 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a s SQLITE_OK. .*
16c3c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
16c3d 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
16c3e 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f eKeySize(BtCurso
16c3f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 53 r *pCur, i64 *pS
16c40 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ize){. assert(
16c41 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
16c42 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
16c43 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16c44 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
16c45 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 || pCur->eState
16c46 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16c47 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 ;. if( pCur->eS
16c48 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c tate!=CURSOR_VAL
16c49 49 44 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 ID ){. *pSize
16c4a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 = 0;. }else{.
16c4b 20 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 getCellInfo(p
16c4c 43 75 72 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 Cur);. *pSize
16c4d 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b = pCur->info.nK
16c4e 65 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ey;. }. return
16c4f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
16c50 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 *.** Set *pSize
16c51 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 to the number of
16c52 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 69 bytes of data i
16c53 6e 20 74 68 65 20 65 6e 74 72 79 20 74 68 65 0a n the entry the.
16c54 2a 2a 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e ** cursor curren
16c55 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a tly points to..*
16c56 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 *.** The caller
16c57 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74 must guarantee t
16c58 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
16c59 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 s pointing to a
16c5a 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 non-NULL.** vali
16c5b 64 20 65 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 d entry. In oth
16c5c 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 63 61 er words, the ca
16c5d 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 lling procedure
16c5e 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 0a 2a must guarantee.*
16c5f 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f * that the curso
16c60 72 20 68 61 73 20 43 75 72 73 6f 72 2e 65 53 74 r has Cursor.eSt
16c61 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 ate==CURSOR_VALI
16c62 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 D..**.** Failure
16c63 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
16c64 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e . This function
16c65 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 always returns
16c66 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 SQLITE_OK..** It
16c67 20 6d 69 67 68 74 20 6a 75 73 74 20 61 73 20 77 might just as w
16c68 65 6c 6c 20 62 65 20 61 20 70 72 6f 63 65 64 75 ell be a procedu
16c69 72 65 20 28 72 65 74 75 72 6e 69 6e 67 20 76 6f re (returning vo
16c6a 69 64 29 20 62 75 74 20 77 65 20 63 6f 6e 74 69 id) but we conti
16c6b 6e 75 65 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e nue.** to return
16c6c 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 an integer resu
16c6d 6c 74 20 63 6f 64 65 20 66 6f 72 20 68 69 73 74 lt code for hist
16c6e 6f 72 69 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a orical reasons..
16c6f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
16c70 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
16c71 65 65 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 eeDataSize(BtCur
16c72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a sor *pCur, u32 *
16c73 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 pSize){. assert
16c74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16c75 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
16c76 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
16c77 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
16c78 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 );. getCellInf
16c79 6f 28 70 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a o(pCur);. *pSiz
16c7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e e = pCur->info.n
16c7b 44 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 Data;. return S
16c7c 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a QLITE_OK;.}../*.
16c7d 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 70 61 67 ** Given the pag
16c7e 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f e number of an o
16c7f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 verflow page in
16c80 74 68 65 20 64 61 74 61 62 61 73 65 20 28 70 61 the database (pa
16c81 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 rameter.** ovfl)
16c82 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 , this function
16c83 66 69 6e 64 73 20 74 68 65 20 70 61 67 65 20 6e finds the page n
16c84 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 umber of the nex
16c85 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 0a 2a t page in the .*
16c86 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 * linked list of
16c87 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e overflow pages.
16c88 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 If possible, it
16c89 20 75 73 65 73 20 74 68 65 20 61 75 74 6f 2d 76 uses the auto-v
16c8a 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 acuum.** pointer
16c8b 2d 6d 61 70 20 64 61 74 61 20 69 6e 73 74 65 61 -map data instea
16c8c 64 20 6f 66 20 72 65 61 64 69 6e 67 20 74 68 65 d of reading the
16c8d 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 content of page
16c8e 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 ovfl to do so.
16c8f 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 .**.** If an err
16c90 6f 72 20 6f 63 63 75 72 73 20 61 6e 20 53 51 4c or occurs an SQL
16c91 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 ite error code i
16c92 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 s returned. Othe
16c93 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 rwise:.**.** The
16c94 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 page number of
16c95 74 68 65 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f the next overflo
16c96 77 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 w page in the li
16c97 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a nked list is .**
16c98 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 written to *pPg
16c99 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61 67 65 20 noNext. If page
16c9a 6f 76 66 6c 20 69 73 20 74 68 65 20 6c 61 73 74 ovfl is the last
16c9b 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e page in its lin
16c9c 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 ked .** list, *p
16c9d 50 67 6e 6f 4e 65 78 74 20 69 73 20 73 65 74 20 PgnoNext is set
16c9e 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 to zero. .**.**
16c9f 49 66 20 70 70 50 61 67 65 20 69 73 20 6e 6f 74 If ppPage is not
16ca0 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 NULL, and a ref
16ca1 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 erence to the Me
16ca2 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 mPage object cor
16ca3 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f responding.** to
16ca4 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 page number pOv
16ca5 66 6c 20 77 61 73 20 6f 62 74 61 69 6e 65 64 2c fl was obtained,
16ca6 20 74 68 65 6e 20 2a 70 70 50 61 67 65 20 69 73 then *ppPage is
16ca7 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f set to point to
16ca8 20 74 68 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e that.** referen
16ca9 63 65 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 ce. It is the re
16caa 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 sponsibility of
16cab 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 the caller to ca
16cac 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 ll releasePage()
16cad 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 .** on *ppPage t
16cae 6f 20 66 72 65 65 20 74 68 65 20 72 65 66 65 72 o free the refer
16caf 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 ence. In no refe
16cb0 72 65 6e 63 65 20 77 61 73 20 6f 62 74 61 69 6e rence was obtain
16cb1 65 64 20 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 ed (because.** t
16cb2 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 he pointer-map w
16cb3 61 73 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 as used to obtai
16cb4 6e 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 n the value for
16cb5 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 *pPgnoNext), the
16cb6 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20 69 73 20 n.** *ppPage is
16cb7 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a set to zero..*/.
16cb8 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4f 76 static int getOv
16cb9 65 72 66 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 erflowPage(. Bt
16cba 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 Shared *pBt,
16cbb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
16cbc 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
16cbd 2a 2f 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 */. Pgno ovfl,
16cbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16cbf 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 /* Current ove
16cc0 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 rflow page numbe
16cc1 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a r */. MemPage *
16cc2 2a 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 *ppPage,
16cc3 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 /* OUT: MemP
16cc4 61 67 65 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 age handle (may
16cc5 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 be NULL) */. Pg
16cc6 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 no *pPgnoNext
16cc7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 /* OU
16cc8 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 T: Next overflow
16cc9 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a page number */.
16cca 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d ){. Pgno next =
16ccb 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 0;. MemPage *p
16ccc 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 Page = 0;. int
16ccd 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
16cce 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
16ccf 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
16cd0 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
16cd1 73 73 65 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 ssert(pPgnoNext)
16cd2 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 ;..#ifndef SQLIT
16cd3 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
16cd4 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 M. /* Try to fi
16cd5 6e 64 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 nd the next page
16cd6 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 in the overflow
16cd7 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 0a list using the.
16cd8 20 20 2a 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 ** autovacuum
16cd9 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
16cda 73 2e 20 47 75 65 73 73 20 74 68 61 74 20 74 68 s. Guess that th
16cdb 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20 0a e next page in .
16cdc 20 20 2a 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f ** the overflo
16cdd 77 20 6c 69 73 74 20 69 73 20 70 61 67 65 20 6e w list is page n
16cde 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 umber (ovfl+1).
16cdf 49 66 20 74 68 61 74 20 67 75 65 73 73 20 74 75 If that guess tu
16ce0 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f rns . ** out to
16ce1 20 62 65 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 be wrong, fall
16ce2 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 back to loading
16ce3 74 68 65 20 64 61 74 61 20 6f 66 20 70 61 67 65 the data of page
16ce4 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 . ** number ov
16ce5 66 6c 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 fl to determine
16ce6 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 6e 75 the next page nu
16ce7 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 mber.. */. if(
16ce8 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
16ce9 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e ){. Pgno pgn
16cea 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69 47 75 65 o;. Pgno iGue
16ceb 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 ss = ovfl+1;.
16cec 20 75 38 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 u8 eType;..
16ced 77 68 69 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 while( PTRMAP_IS
16cee 50 41 47 45 28 70 42 74 2c 20 69 47 75 65 73 73 PAGE(pBt, iGuess
16cef 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e ) || iGuess==PEN
16cf0 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
16cf1 42 74 29 20 29 7b 0a 20 20 20 20 20 20 69 47 75 Bt) ){. iGu
16cf2 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 ess++;. }..
16cf3 20 20 69 66 28 20 69 47 75 65 73 73 3c 3d 70 61 if( iGuess<=pa
16cf4 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
16cf5 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 ) ){. rc =
16cf6 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 69 ptrmapGet(pBt, i
16cf7 47 75 65 73 73 2c 20 26 65 54 79 70 65 2c 20 26 Guess, &eType, &
16cf8 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 pgno);. if(
16cf9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
16cfa 26 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f & eType==PTRMAP_
16cfb 4f 56 45 52 46 4c 4f 57 32 20 26 26 20 70 67 6e OVERFLOW2 && pgn
16cfc 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 o==ovfl ){.
16cfd 20 20 20 6e 65 78 74 20 3d 20 69 47 75 65 73 73 next = iGuess
16cfe 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 ;. rc = S
16cff 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 QLITE_DONE;.
16d00 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 }. }. }.#e
16d01 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 ndif.. assert(
16d02 6e 65 78 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 next==0 || rc==S
16d03 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 QLITE_DONE );.
16d04 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
16d05 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 K ){. rc = bt
16d06 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 reeGetPage(pBt,
16d07 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 ovfl, &pPage, 0)
16d08 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 ;. assert( rc
16d09 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
16d0a 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 Page==0 );. i
16d0b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
16d0c 20 29 7b 0a 20 20 20 20 20 20 6e 65 78 74 20 3d ){. next =
16d0d 20 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d get4byte(pPage-
16d0e 3e 61 44 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 >aData);. }.
16d0f 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 }.. *pPgnoNext
16d10 20 3d 20 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 = next;. if( p
16d11 70 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 pPage ){. *pp
16d12 50 61 67 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 Page = pPage;.
16d13 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 }else{. relea
16d14 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 sePage(pPage);.
16d15 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d }. return (rc=
16d16 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 =SQLITE_DONE ? S
16d17 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a QLITE_OK : rc);.
16d18 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 }../*.** Copy da
16d19 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 ta from a buffer
16d1a 20 74 6f 20 61 20 70 61 67 65 2c 20 6f 72 20 66 to a page, or f
16d1b 72 6f 6d 20 61 20 70 61 67 65 20 74 6f 20 61 20 rom a page to a
16d1c 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 buffer..**.** pP
16d1d 61 79 6c 6f 61 64 20 69 73 20 61 20 70 6f 69 6e ayload is a poin
16d1e 74 65 72 20 74 6f 20 64 61 74 61 20 73 74 6f 72 ter to data stor
16d1f 65 64 20 6f 6e 20 64 61 74 61 62 61 73 65 20 70 ed on database p
16d20 61 67 65 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 age pDbPage..**
16d21 49 66 20 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 If argument eOp
16d22 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e is false, then n
16d23 42 79 74 65 20 62 79 74 65 73 20 6f 66 20 64 61 Byte bytes of da
16d24 74 61 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a ta are copied.**
16d25 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 from pPayload t
16d26 6f 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 o the buffer poi
16d27 6e 74 65 64 20 61 74 20 62 79 20 70 42 75 66 2e nted at by pBuf.
16d28 20 49 66 20 65 4f 70 20 69 73 20 74 72 75 65 2c If eOp is true,
16d29 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 .** then sqlite3
16d2a 50 61 67 65 72 57 72 69 74 65 28 29 20 69 73 20 PagerWrite() is
16d2b 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 called on pDbPag
16d2c 65 20 61 6e 64 20 6e 42 79 74 65 20 62 79 74 65 e and nByte byte
16d2d 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20 61 72 65 s.** of data are
16d2e 20 63 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 copied from the
16d2f 20 62 75 66 66 65 72 20 70 42 75 66 20 74 6f 20 buffer pBuf to
16d30 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 pPayload..**.**
16d31 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
16d32 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73 urned on success
16d33 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65 , otherwise an e
16d34 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 rror code..*/.st
16d35 61 74 69 63 20 69 6e 74 20 63 6f 70 79 50 61 79 atic int copyPay
16d36 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 load(. void *pP
16d37 61 79 6c 6f 61 64 2c 20 20 20 20 20 20 20 20 20 ayload,
16d38 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 /* Pointer to
16d39 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 page data */. v
16d3a 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 oid *pBuf,
16d3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
16d3c 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 2a 2f ter to buffer */
16d3d 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 . int nByte,
16d3e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16d3f 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
16d40 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 to copy */. int
16d41 20 65 4f 70 2c 20 20 20 20 20 20 20 20 20 20 20 eOp,
16d42 20 20 20 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 /* 0 -> c
16d43 6f 70 79 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 opy from page, 1
16d44 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70 61 67 65 -> copy to page
16d45 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70 44 */. DbPage *pD
16d46 62 50 61 67 65 20 20 20 20 20 20 20 20 20 20 20 bPage
16d47 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 /* Page containi
16d48 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 ng pPayload */.)
16d49 7b 0a 20 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 {. if( eOp ){.
16d4a 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 /* Copy data
16d4b 66 72 6f 6d 20 62 75 66 66 65 72 20 74 6f 20 70 from buffer to p
16d4c 61 67 65 20 28 61 20 77 72 69 74 65 20 6f 70 65 age (a write ope
16d4d 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 ration) */. i
16d4e 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 nt rc = sqlite3P
16d4f 61 67 65 72 57 72 69 74 65 28 70 44 62 50 61 67 agerWrite(pDbPag
16d50 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d e);. if( rc!=
16d51 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
16d52 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
16d53 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 }. memcpy(p
16d54 50 61 79 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e Payload, pBuf, n
16d55 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a Byte);. }else{.
16d56 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 /* Copy data
16d57 20 66 72 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 from page to bu
16d58 66 66 65 72 20 28 61 20 72 65 61 64 20 6f 70 65 ffer (a read ope
16d59 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d ration) */. m
16d5a 65 6d 63 70 79 28 70 42 75 66 2c 20 70 50 61 79 emcpy(pBuf, pPay
16d5b 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 load, nByte);.
16d5c 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
16d5d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 E_OK;.}../*.** T
16d5e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
16d5f 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 used to read or
16d60 6f 76 65 72 77 72 69 74 65 20 70 61 79 6c 6f 61 overwrite payloa
16d61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a d information.**
16d62 20 66 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 for the entry t
16d63 68 61 74 20 74 68 65 20 70 43 75 72 20 63 75 72 hat the pCur cur
16d64 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
16d65 74 6f 2e 20 49 66 20 74 68 65 20 65 4f 70 0a 2a to. If the eOp.*
16d66 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 * parameter is 0
16d67 2c 20 74 68 69 73 20 69 73 20 61 20 72 65 61 64 , this is a read
16d68 20 6f 70 65 72 61 74 69 6f 6e 20 28 64 61 74 61 operation (data
16d69 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 copied into.**
16d6a 62 75 66 66 65 72 20 70 42 75 66 29 2e 20 49 66 buffer pBuf). If
16d6b 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c it is non-zero,
16d6c 20 61 20 77 72 69 74 65 20 28 64 61 74 61 20 63 a write (data c
16d6d 6f 70 69 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 opied from.** bu
16d6e 66 66 65 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a ffer pBuf)..**.*
16d6f 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d * A total of "am
16d70 74 22 20 62 79 74 65 73 20 61 72 65 20 72 65 61 t" bytes are rea
16d71 64 20 6f 72 20 77 72 69 74 74 65 6e 20 62 65 67 d or written beg
16d72 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66 66 73 65 inning at "offse
16d73 74 22 2e 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 t"..** Data is r
16d74 65 61 64 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 ead to or from t
16d75 68 65 20 62 75 66 66 65 72 20 70 42 75 66 2e 0a he buffer pBuf..
16d76 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e **.** The conten
16d77 74 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 20 t being read or
16d78 77 72 69 74 74 65 6e 20 6d 69 67 68 74 20 61 70 written might ap
16d79 70 65 61 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e pear on the main
16d7a 20 70 61 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 page.** or be s
16d7b 63 61 74 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 cattered out on
16d7c 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f multiple overflo
16d7d 77 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 w pages..**.** I
16d7e 66 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 f the BtCursor.i
16d7f 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 sIncrblobHandle
16d80 66 6c 61 67 20 69 73 20 73 65 74 2c 20 61 6e 64 flag is set, and
16d81 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 the current.**
16d82 63 75 72 73 6f 72 20 65 6e 74 72 79 20 75 73 65 cursor entry use
16d83 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 s one or more ov
16d84 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 erflow pages, th
16d85 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 is function.** a
16d86 6c 6c 6f 63 61 74 65 73 20 73 70 61 63 65 20 66 llocates space f
16d87 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f or and lazily po
16d88 70 6c 75 61 74 65 73 20 74 68 65 20 6f 76 65 72 pluates the over
16d89 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 0a flow page-list .
16d8a 2a 2a 20 63 61 63 68 65 20 61 72 72 61 79 20 28 ** cache array (
16d8b 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c BtCursor.aOverfl
16d8c 6f 77 29 2e 20 53 75 62 73 65 71 75 65 6e 74 20 ow). Subsequent
16d8d 63 61 6c 6c 73 20 75 73 65 20 74 68 69 73 0a 2a calls use this.*
16d8e 2a 20 63 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 * cache to make
16d8f 73 65 65 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 seeking to the s
16d90 75 70 70 6c 69 65 64 20 6f 66 66 73 65 74 20 6d upplied offset m
16d91 6f 72 65 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a ore efficient..*
16d92 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 *.** Once an ove
16d93 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 rflow page-list
16d94 63 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 cache has been a
16d95 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20 6d 61 79 llocated, it may
16d96 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 be.** invalidat
16d97 65 64 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 ed if some other
16d98 20 63 75 72 73 6f 72 20 77 72 69 74 65 73 20 74 cursor writes t
16d99 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 o the same table
16d9a 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 , or if.** the c
16d9b 75 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 ursor is moved t
16d9c 6f 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 6f o a different ro
16d9d 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c w. Additionally,
16d9e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a in auto-vacuum.
16d9f 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c ** mode, the fol
16da0 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 lowing events ma
16da1 79 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 y invalidate an
16da2 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 overflow page-li
16da3 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 st cache..**.**
16da4 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 * An increment
16da5 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 al vacuum,.**
16da6 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 * A commit in au
16da7 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 to_vacuum="full"
16da8 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 mode,.** * Cr
16da9 65 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 28 eating a table (
16daa 6d 61 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 may require movi
16dab 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 ng an overflow p
16dac 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 age)..*/.static
16dad 69 6e 74 20 61 63 63 65 73 73 50 61 79 6c 6f 61 int accessPayloa
16dae 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 d(. BtCursor *p
16daf 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 Cur, /* Cur
16db0 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 sor pointing to
16db1 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20 66 72 entry to read fr
16db2 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 om */. u32 offs
16db3 65 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 et, /*
16db4 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 74 68 Begin reading th
16db5 69 73 20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c is far into payl
16db6 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 oad */. u32 amt
16db7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
16db8 20 52 65 61 64 20 74 68 69 73 20 6d 61 6e 79 20 Read this many
16db9 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 bytes */. unsig
16dba 6e 65 64 20 63 68 61 72 20 2a 70 42 75 66 2c 20 ned char *pBuf,
16dbb 2f 2a 20 57 72 69 74 65 20 74 68 65 20 62 79 74 /* Write the byt
16dbc 65 73 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66 es into this buf
16dbd 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f fer */ . int eO
16dbe 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f p /
16dbf 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 * zero to read.
16dc0 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 non-zero to writ
16dc1 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 e. */.){. unsig
16dc2 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f ned char *aPaylo
16dc3 61 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 ad;. int rc = S
16dc4 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 QLITE_OK;. u32
16dc5 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 nKey;. int iIdx
16dc6 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 = 0;. MemPage
16dc7 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 *pPage = pCur->a
16dc8 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
16dc9 65 5d 3b 20 2f 2a 20 42 74 72 65 65 20 70 61 67 e]; /* Btree pag
16dca 65 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74 e of current ent
16dcb 72 79 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 ry */. BtShared
16dcc 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 *pBt = pCur->pB
16dcd 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
16dce 20 20 20 20 2f 2a 20 42 74 72 65 65 20 74 68 69 /* Btree thi
16dcf 73 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 s cursor belongs
16dd0 20 74 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 to */.. assert
16dd1 28 20 70 50 61 67 65 20 29 3b 0a 20 20 61 73 73 ( pPage );. ass
16dd2 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
16dd3 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
16dd4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
16dd5 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
16dd6 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 Page]<pPage->nCe
16dd7 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ll );. assert(
16dd8 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
16dd9 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 (pCur) );.. get
16dda 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a CellInfo(pCur);.
16ddb 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 aPayload = pCu
16ddc 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 r->info.pCell +
16ddd 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 pCur->info.nHead
16dde 65 72 3b 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 er;. nKey = (pP
16ddf 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 age->intKey ? 0
16de0 3a 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 : (int)pCur->inf
16de1 6f 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 o.nKey);.. if(
16de2 4e 45 56 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 NEVER(offset+amt
16de3 20 3e 20 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e > nKey+pCur->in
16de4 66 6f 2e 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c fo.nData) . ||
16de5 20 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d &aPayload[pCur-
16de6 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 >info.nLocal] >
16de7 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 &pPage->aData[pB
16de8 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 t->usableSize].
16de9 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e ){. /* Tryin
16dea 67 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 g to read or wri
16deb 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 te past the end
16dec 6f 66 20 74 68 65 20 64 61 74 61 20 69 73 20 61 of the data is a
16ded 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 n error */. r
16dee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
16def 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a RUPT_BKPT;. }..
16df0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 64 61 /* Check if da
16df1 74 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 2f ta must be read/
16df2 77 72 69 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 written to/from
16df3 74 68 65 20 62 74 72 65 65 20 70 61 67 65 20 69 the btree page i
16df4 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 tself. */. if(
16df5 6f 66 66 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 offset<pCur->inf
16df6 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 o.nLocal ){.
16df7 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 int a = amt;.
16df8 20 69 66 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 if( a+offset>pC
16df9 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 ur->info.nLocal
16dfa 29 7b 0a 20 20 20 20 20 20 61 20 3d 20 70 43 75 ){. a = pCu
16dfb 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d r->info.nLocal -
16dfc 20 6f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 offset;. }.
16dfd 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c rc = copyPayl
16dfe 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 oad(&aPayload[of
16dff 66 73 65 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 fset], pBuf, a,
16e00 65 4f 70 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 eOp, pPage->pDbP
16e01 61 67 65 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 age);. offset
16e02 20 3d 20 30 3b 0a 20 20 20 20 70 42 75 66 20 2b = 0;. pBuf +
16e03 3d 20 61 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 = a;. amt -=
16e04 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 a;. }else{.
16e05 6f 66 66 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e offset -= pCur->
16e06 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d info.nLocal;. }
16e07 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
16e08 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 TE_OK && amt>0 )
16e09 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 33 32 20 {. const u32
16e0a 6f 76 66 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e ovflSize = pBt->
16e0b 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 usableSize - 4;
16e0c 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e 74 65 6e /* Bytes conten
16e0d 74 20 70 65 72 20 6f 76 66 6c 20 70 61 67 65 20 t per ovfl page
16e0e 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 */. Pgno next
16e0f 50 61 67 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 Page;.. nextP
16e10 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 age = get4byte(&
16e11 61 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 aPayload[pCur->i
16e12 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 nfo.nLocal]);..#
16e13 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16e14 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 IT_INCRBLOB.
16e15 2f 2a 20 49 66 20 74 68 65 20 69 73 49 6e 63 72 /* If the isIncr
16e16 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 blobHandle flag
16e17 69 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 42 is set and the B
16e18 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f tCursor.aOverflo
16e19 77 5b 5d 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e w[]. ** has n
16e1a 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 ot been allocate
16e1b 64 2c 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e d, allocate it n
16e1c 6f 77 2e 20 54 68 65 20 61 72 72 61 79 20 69 73 ow. The array is
16e1d 20 73 69 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a sized at. **
16e1e 20 6f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 one entry for e
16e1f 61 63 68 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 ach overflow pag
16e20 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f e in the overflo
16e21 77 20 63 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 w chain. The.
16e22 20 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 ** page number
16e23 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 of the first ove
16e24 72 66 6c 6f 77 20 70 61 67 65 20 69 73 20 73 74 rflow page is st
16e25 6f 72 65 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f ored in aOverflo
16e26 77 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 w[0],. ** etc
16e27 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 69 . A value of 0 i
16e28 6e 20 74 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b n the aOverflow[
16e29 5d 20 61 72 72 61 79 20 6d 65 61 6e 73 20 22 6e ] array means "n
16e2a 6f 74 20 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 ot yet known".
16e2b 20 20 2a 2a 20 28 74 68 65 20 63 61 63 68 65 20 ** (the cache
16e2c 69 73 20 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 is lazily popula
16e2d 74 65 64 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ted).. */.
16e2e 20 69 66 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 if( pCur->isInc
16e2f 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 rblobHandle && !
16e30 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 pCur->aOverflow
16e31 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 ){. int nOv
16e32 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f fl = (pCur->info
16e33 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e .nPayload-pCur->
16e34 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c info.nLocal+ovfl
16e35 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 Size-1)/ovflSize
16e36 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 4f ;. pCur->aO
16e37 76 65 72 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 verflow = (Pgno
16e38 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a *)sqlite3MallocZ
16e39 65 72 6f 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 ero(sizeof(Pgno)
16e3a 2a 6e 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f *nOvfl);. /
16e3b 2a 20 6e 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 * nOvfl is alway
16e3c 73 20 70 6f 73 69 74 69 76 65 2e 20 20 49 66 20 s positive. If
16e3d 69 74 20 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 it were zero, fe
16e3e 74 63 68 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 tchPayload would
16e3f 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 have. ** b
16e40 65 65 6e 20 75 73 65 64 20 69 6e 73 74 65 61 64 een used instead
16e41 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 of this routine
16e42 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 . */. if( A
16e43 4c 57 41 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 LWAYS(nOvfl) &&
16e44 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 !pCur->aOverflow
16e45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
16e46 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
16e47 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
16e48 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 76 65 72 /* If the over
16e49 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 flow page-list c
16e4a 61 63 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c ache has been al
16e4b 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 0a located and the.
16e4c 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 ** entry for
16e4d 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75 69 the first requi
16e4e 72 65 64 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 red overflow pag
16e4f 65 20 69 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 e is valid, skip
16e50 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 . ** directly
16e51 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 to it.. */.
16e52 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 if( pCur->aOv
16e53 65 72 66 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e erflow && pCur->
16e54 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 aOverflow[offset
16e55 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 /ovflSize] ){.
16e56 20 20 20 20 69 49 64 78 20 3d 20 28 6f 66 66 73 iIdx = (offs
16e57 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 et/ovflSize);.
16e58 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 nextPage = p
16e59 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 Cur->aOverflow[i
16e5a 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 Idx];. offs
16e5b 65 74 20 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 et = (offset%ovf
16e5c 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 lSize);. }.#e
16e5d 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b ndif.. for( ;
16e5e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
16e5f 26 20 61 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 & amt>0 && nextP
16e60 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 age; iIdx++){..#
16e61 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
16e62 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 IT_INCRBLOB.
16e63 20 20 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 /* If required
16e64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f , populate the o
16e65 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 verflow page-lis
16e66 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 t cache. */.
16e67 20 20 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 if( pCur->aOve
16e68 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 rflow ){.
16e69 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61 assert(!pCur->a
16e6a 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c Overflow[iIdx] |
16e6b 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f | pCur->aOverflo
16e6c 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 w[iIdx]==nextPag
16e6d 65 29 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72 e);. pCur
16e6e 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 ->aOverflow[iIdx
16e6f 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 ] = nextPage;.
16e70 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
16e71 20 20 20 20 69 66 28 20 6f 66 66 73 65 74 3e 3d if( offset>=
16e72 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 ovflSize ){.
16e73 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 /* The only
16e74 72 65 61 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 reason to read t
16e75 68 69 73 20 70 61 67 65 20 69 73 20 74 6f 20 6f his page is to o
16e76 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 0a 20 btain the page.
16e77 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 ** number
16e78 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 70 61 for the next pa
16e79 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66 6c ge in the overfl
16e7a 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 20 70 61 ow chain. The pa
16e7b 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 ge. ** da
16e7c 74 61 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 ta is not requir
16e7d 65 64 2e 20 53 6f 20 66 69 72 73 74 20 74 72 79 ed. So first try
16e7e 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f to lookup the o
16e7f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 verflow.
16e80 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 ** page-list cac
16e81 68 65 2c 20 69 66 20 61 6e 79 2c 20 74 68 65 6e he, if any, then
16e82 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 fall back to th
16e83 65 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 e getOverflowPag
16e84 65 28 29 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 e(). ** f
16e85 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 unction..
16e86 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
16e87 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
16e88 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 . if( pCu
16e89 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 r->aOverflow &&
16e8a 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
16e8b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 iIdx+1] ){.
16e8c 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 nextPage =
16e8d 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b pCur->aOverflow[
16e8e 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 iIdx+1];.
16e8f 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a } else .#endif.
16e90 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 67 rc = g
16e91 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 etOverflowPage(p
16e92 42 74 2c 20 6e 65 78 74 50 61 67 65 2c 20 30 2c Bt, nextPage, 0,
16e93 20 26 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 &nextPage);.
16e94 20 20 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f offset -= o
16e95 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d vflSize;. }
16e96 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
16e97 20 4e 65 65 64 20 74 6f 20 72 65 61 64 20 74 68 Need to read th
16e98 69 73 20 70 61 67 65 20 70 72 6f 70 65 72 6c 79 is page properly
16e99 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f . It contains so
16e9a 6d 65 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 me of the.
16e9b 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 ** range of da
16e9c 74 61 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 ta that is being
16e9d 20 72 65 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f read (eOp==0) o
16e9e 72 20 77 72 69 74 74 65 6e 20 28 65 4f 70 21 3d r written (eOp!=
16e9f 30 29 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 0).. */.
16ea0 20 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 DbPage *p
16ea1 44 62 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 DbPage;.
16ea2 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 int a = amt;.
16ea3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
16ea4 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 3PagerGet(pBt->p
16ea5 50 61 67 65 72 2c 20 6e 65 78 74 50 61 67 65 2c Pager, nextPage,
16ea6 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 &pDbPage);.
16ea7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
16ea8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
16ea9 20 20 20 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 aPayload = sq
16eaa 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 lite3PagerGetDat
16eab 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 a(pDbPage);.
16eac 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20 3d nextPage =
16ead 20 67 65 74 34 62 79 74 65 28 61 50 61 79 6c 6f get4byte(aPaylo
16eae 61 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ad);. i
16eaf 66 28 20 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 f( a + offset >
16eb0 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 ovflSize ){.
16eb1 20 20 20 20 20 20 20 20 61 20 3d 20 6f 76 66 6c a = ovfl
16eb2 53 69 7a 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 Size - offset;.
16eb3 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
16eb4 20 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50 61 rc = copyPa
16eb5 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64 5b yload(&aPayload[
16eb6 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c offset+4], pBuf,
16eb7 20 61 2c 20 65 4f 70 2c 20 70 44 62 50 61 67 65 a, eOp, pDbPage
16eb8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
16eb9 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 ite3PagerUnref(p
16eba 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 DbPage);.
16ebb 20 20 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 offset = 0;.
16ebc 20 20 20 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 amt -=
16ebd 61 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 a;. pBu
16ebe 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 f += a;.
16ebf 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
16ec0 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 }.. if( rc==S
16ec1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e QLITE_OK && amt>
16ec2 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
16ec3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
16ec4 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 KPT;. }. retur
16ec5 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 n rc;.}../*.** R
16ec6 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 ead part of the
16ec7 6b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 key associated w
16ec8 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e ith cursor pCur.
16ec9 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d Exactly.** "am
16eca 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 t" bytes will be
16ecb 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f transfered into
16ecc 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 pBuf[]. The tr
16ecd 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 ansfer.** begins
16ece 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a at "offset"..**
16ecf 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d .** The caller m
16ed0 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20 ust ensure that
16ed1 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 pCur is pointing
16ed2 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 0a to a valid row.
16ed3 2a 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e ** in the table.
16ed4 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 .**.** Return SQ
16ed5 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
16ed6 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 ss or an error c
16ed7 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 ode if anything
16ed8 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 goes.** wrong.
16ed9 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 An error is retu
16eda 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b rned if "offset+
16edb 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 amt" is larger t
16edc 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c han.** the avail
16edd 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f able payload..*/
16ede 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
16edf 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
16ee0 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43 Key(BtCursor *pC
16ee1 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 ur, u32 offset,
16ee2 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 u32 amt, void *p
16ee3 42 75 66 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Buf){. assert(
16ee4 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 cursorHoldsMutex
16ee5 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 (pCur) );. asse
16ee6 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16ee7 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16ee8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 ;. assert( pCur
16ee9 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 ->iPage>=0 && pC
16eea 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
16eeb 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 >iPage] );. ass
16eec 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ert( pCur->aiIdx
16eed 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 [pCur->iPage]<pC
16eee 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
16eef 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 >iPage]->nCell )
16ef0 3b 0a 20 20 72 65 74 75 72 6e 20 61 63 63 65 73 ;. return acces
16ef1 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f sPayload(pCur, o
16ef2 66 66 73 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 ffset, amt, (uns
16ef3 69 67 6e 65 64 20 63 68 61 72 2a 29 70 42 75 66 igned char*)pBuf
16ef4 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 , 0);.}../*.** R
16ef5 65 61 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 ead part of the
16ef6 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
16ef7 77 69 74 68 20 63 75 72 73 6f 72 20 70 43 75 72 with cursor pCur
16ef8 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 . Exactly.** "a
16ef9 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c 20 62 mt" bytes will b
16efa 65 20 74 72 61 6e 73 66 65 72 65 64 20 69 6e 74 e transfered int
16efb 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 o pBuf[]. The t
16efc 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e ransfer.** begin
16efd 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a s at "offset"..*
16efe 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 *.** Return SQLI
16eff 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 TE_OK on success
16f00 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 or an error cod
16f01 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f e if anything go
16f02 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e es.** wrong. An
16f03 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e error is return
16f04 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d ed if "offset+am
16f05 74 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 t" is larger tha
16f06 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 n.** the availab
16f07 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 le payload..*/.S
16f08 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
16f09 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 t sqlite3BtreeDa
16f0a 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ta(BtCursor *pCu
16f0b 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 r, u32 offset, u
16f0c 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 32 amt, void *pB
16f0d 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a uf){. int rc;..
16f0e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
16f0f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 MIT_INCRBLOB. i
16f10 66 20 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 f ( pCur->eState
16f11 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
16f12 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
16f13 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d QLITE_ABORT;. }
16f14 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
16f15 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
16f16 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 tex(pCur) );. r
16f17 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f c = restoreCurso
16f18 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b rPosition(pCur);
16f19 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
16f1a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 E_OK ){. asse
16f1b 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 rt( pCur->eState
16f1c 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 ==CURSOR_VALID )
16f1d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 ;. assert( pC
16f1e 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 ur->iPage>=0 &&
16f1f 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
16f20 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 r->iPage] );.
16f21 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
16f22 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
16f23 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ]<pCur->apPage[p
16f24 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
16f25 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 ll );. rc = a
16f26 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 ccessPayload(pCu
16f27 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 r, offset, amt,
16f28 70 42 75 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 pBuf, 0);. }.
16f29 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
16f2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 .** Return a poi
16f2b 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 nter to payload
16f2c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d information from
16f2d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 the entry that
16f2e 74 68 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 the .** pCur cur
16f2f 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 sor is pointing
16f30 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 to. The pointer
16f31 20 69 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e is to the begin
16f32 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b ning of.** the k
16f33 65 79 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 ey if skipKey==0
16f34 20 61 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 and it points t
16f35 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 o the beginning
16f36 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b of data if.** sk
16f37 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e ipKey==1. The n
16f38 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
16f39 66 20 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f f available key/
16f3a 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a data is written.
16f3b 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 ** into *pAmt.
16f3c 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 If *pAmt==0, the
16f3d 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
16f3e 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 rned will not be
16f3f 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e .** a valid poin
16f40 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ter..**.** This
16f41 72 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 routine is an op
16f42 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 timization. It
16f43 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 is common for th
16f44 65 20 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 e entire key.**
16f45 61 6e 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 and data to fit
16f46 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 on the local pag
16f47 65 20 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 e and for there
16f48 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f to be no overflo
16f49 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 w.** pages. Whe
16f4a 6e 20 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 n that is so, th
16f4b 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 is routine can b
16f4c 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 e used to access
16f4d 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 the.** key and
16f4e 64 61 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b data without mak
16f4f 69 6e 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 ing a copy. If
16f50 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 the key and/or d
16f51 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e ata spills.** on
16f52 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 to overflow page
16f53 73 2c 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 s, then accessPa
16f54 79 6c 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 yload() must be
16f55 75 73 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 used to reassemb
16f56 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 le.** the key/da
16f57 74 61 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 ta and copy it i
16f58 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 nto a preallocat
16f59 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a ed buffer..**.**
16f5a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 The pointer ret
16f5b 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f urned by this ro
16f5c 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 utine looks dire
16f5d 63 74 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 ctly into the ca
16f5e 63 68 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 ched.** page of
16f5f 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 the database. T
16f60 68 65 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 he data might ch
16f61 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 ange or move the
16f62 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e next time.** an
16f63 79 20 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 y btree routine
16f64 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 is called..*/.st
16f65 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 atic const unsig
16f66 6e 65 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 ned char *fetchP
16f67 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 ayload(. BtCurs
16f68 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f or *pCur, /
16f69 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e * Cursor pointin
16f6a 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 g to entry to re
16f6b 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 ad from */. int
16f6c 20 2a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 *pAmt,
16f6d 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
16f6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 umber of availab
16f6f 6c 65 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f le bytes here */
16f70 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 . int skipKey
16f71 20 20 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 /* read
16f72 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 beginning at dat
16f73 61 20 69 66 20 74 68 69 73 20 69 73 20 74 72 75 a if this is tru
16f74 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e e */.){. unsign
16f75 65 64 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 ed char *aPayloa
16f76 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 d;. MemPage *pP
16f77 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b age;. u32 nKey;
16f78 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a . u32 nLocal;..
16f79 20 20 61 73 73 65 72 74 28 20 70 43 75 72 21 3d assert( pCur!=
16f7a 30 20 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 0 && pCur->iPage
16f7b 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 >=0 && pCur->apP
16f7c 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
16f7d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 );. assert( pCu
16f7e 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
16f7f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 R_VALID );. ass
16f80 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 ert( cursorHolds
16f81 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 Mutex(pCur) );.
16f82 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 pPage = pCur->a
16f83 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
16f84 65 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 e];. assert( pC
16f85 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
16f86 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 iPage]<pPage->nC
16f87 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 ell );. if( NEV
16f88 45 52 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 ER(pCur->info.nS
16f89 69 7a 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 ize==0) ){. b
16f8a 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43 treeParseCell(pC
16f8b 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
16f8c 3e 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 >iPage], pCur->a
16f8d 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 iIdx[pCur->iPage
16f8e 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ],.
16f8f 20 20 20 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 &pCur->inf
16f90 6f 29 3b 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f o);. }. aPaylo
16f91 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e ad = pCur->info.
16f92 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 pCell;. aPayloa
16f93 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e d += pCur->info.
16f94 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28 20 70 nHeader;. if( p
16f95 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a Page->intKey ){.
16f96 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 nKey = 0;.
16f97 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 }else{. nKey
16f98 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 = (int)pCur->inf
16f99 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 o.nKey;. }. if
16f9a 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 ( skipKey ){.
16f9b 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 aPayload += nKe
16f9c 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 y;. nLocal =
16f9d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
16f9e 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 l - nKey;. }els
16f9f 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 e{. nLocal =
16fa0 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 pCur->info.nLoca
16fa1 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e l;. assert( n
16fa2 4c 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 Local<=nKey );.
16fa3 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f }. *pAmt = nLo
16fa4 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 cal;. return aP
16fa5 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a ayload;.}.../*.*
16fa6 2a 20 46 6f 72 20 74 68 65 20 65 6e 74 72 79 20 * For the entry
16fa7 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 that cursor pCur
16fa8 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 is point to, re
16fa9 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 turn as.** many
16faa 62 79 74 65 73 20 6f 66 20 74 68 65 20 6b 65 79 bytes of the key
16fab 20 6f 72 20 64 61 74 61 20 61 73 20 61 72 65 20 or data as are
16fac 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 available on the
16fad 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 local.** b-tree
16fae 20 70 61 67 65 2e 20 20 57 72 69 74 65 20 74 68 page. Write th
16faf 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69 e number of avai
16fb0 6c 61 62 6c 65 20 62 79 74 65 73 20 69 6e 74 6f lable bytes into
16fb1 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 *pAmt..**.** Th
16fb2 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e e pointer return
16fb3 65 64 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2e ed is ephemeral.
16fb4 20 20 54 68 65 20 6b 65 79 2f 64 61 74 61 20 6d The key/data m
16fb5 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 ay move.** or be
16fb6 20 64 65 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 destroyed on th
16fb7 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 e next call to a
16fb8 6e 79 20 42 74 72 65 65 20 72 6f 75 74 69 6e 65 ny Btree routine
16fb9 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 ,.** including c
16fba 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 alls from other
16fbb 74 68 72 65 61 64 73 20 61 67 61 69 6e 73 74 20 threads against
16fbc 74 68 65 20 73 61 6d 65 20 63 61 63 68 65 2e 0a the same cache..
16fbd 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 ** Hence, a mute
16fbe 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 x on the BtShare
16fbf 64 20 73 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 d should be held
16fc0 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
16fc1 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e g.** this routin
16fc2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 e..**.** These r
16fc3 6f 75 74 69 6e 65 73 20 69 73 20 75 73 65 64 20 outines is used
16fc4 74 6f 20 67 65 74 20 71 75 69 63 6b 20 61 63 63 to get quick acc
16fc5 65 73 73 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 ess to key and d
16fc6 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f ata.** in the co
16fc7 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 mmon case where
16fc8 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 no overflow page
16fc9 73 20 61 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 s are used..*/.S
16fca 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f QLITE_PRIVATE co
16fcb 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
16fcc 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 3BtreeKeyFetch(B
16fcd 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 tCursor *pCur, i
16fce 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e nt *pAmt){. con
16fcf 73 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a st void *p = 0;.
16fd0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
16fd1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 3_mutex_held(pCu
16fd2 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d r->pBtree->db->m
16fd3 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 utex) );. asser
16fd4 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
16fd5 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 tex(pCur) );. i
16fd6 66 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e f( ALWAYS(pCur->
16fd7 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 eState==CURSOR_V
16fd8 41 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d ALID) ){. p =
16fd9 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 (const void*)fe
16fda 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c tchPayload(pCur,
16fdb 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 pAmt, 0);. }.
16fdc 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c return p;.}.SQL
16fdd 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 ITE_PRIVATE cons
16fde 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 t void *sqlite3B
16fdf 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74 treeDataFetch(Bt
16fe0 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e Cursor *pCur, in
16fe1 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 t *pAmt){. cons
16fe2 74 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 t void *p = 0;.
16fe3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
16fe4 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 _mutex_held(pCur
16fe5 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 ->pBtree->db->mu
16fe6 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
16fe7 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
16fe8 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 ex(pCur) );. if
16fe9 28 20 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 ( ALWAYS(pCur->e
16fea 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 State==CURSOR_VA
16feb 4c 49 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 LID) ){. p =
16fec 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 (const void*)fet
16fed 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 chPayload(pCur,
16fee 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 pAmt, 1);. }.
16fef 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a return p;.}.../*
16ff0 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
16ff1 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 sor down to a ne
16ff2 77 20 63 68 69 6c 64 20 70 61 67 65 2e 20 20 54 w child page. T
16ff3 68 65 20 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d he newPgno argum
16ff4 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 ent is the.** pa
16ff5 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 ge number of the
16ff6 20 63 68 69 6c 64 20 70 61 67 65 20 74 6f 20 6d child page to m
16ff7 6f 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 ove to..**.** Th
16ff8 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 is function retu
16ff9 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 rns SQLITE_CORRU
16ffa 50 54 20 69 66 20 74 68 65 20 70 61 67 65 2d 68 PT if the page-h
16ffb 65 61 64 65 72 20 66 6c 61 67 73 20 66 69 65 6c eader flags fiel
16ffc 64 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 d of.** the new
16ffd 63 68 69 6c 64 20 70 61 67 65 20 64 6f 65 73 20 child page does
16ffe 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 66 6c not match the fl
16fff 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65 ags field of the
17000 20 70 61 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a parent (i.e..**
17001 20 69 66 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 if an intkey pa
17002 67 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 ge appears to be
17003 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 61 the parent of a
17004 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 non-intkey page
17005 2c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 , or.** vice-ver
17006 73 61 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 sa)..*/.static i
17007 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 nt moveToChild(B
17008 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 tCursor *pCur, u
17009 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 32 newPgno){. i
1700a 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d nt rc;. int i =
1700b 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 pCur->iPage;.
1700c 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67 MemPage *pNewPag
1700d 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 e;. BtShared *p
1700e 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a Bt = pCur->pBt;.
1700f 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f . assert( curso
17010 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 rHoldsMutex(pCur
17011 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
17012 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 Cur->eState==CUR
17013 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 SOR_VALID );. a
17014 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 ssert( pCur->iPa
17015 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f ge<BTCURSOR_MAX_
17016 44 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70 DEPTH );. if( p
17017 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 Cur->iPage>=(BTC
17018 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d URSOR_MAX_DEPTH-
17019 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 1) ){. return
1701a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1701b 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d BKPT;. }. rc =
1701c 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
1701d 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 pBt, newPgno, &p
1701e 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20 NewPage);. if(
1701f 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
17020 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 pCur->apPage[i
17021 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a +1] = pNewPage;.
17022 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b pCur->aiIdx[i+
17023 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 1] = 0;. pCur->
17024 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 iPage++;.. pCur
17025 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 ->info.nSize = 0
17026 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e ;. pCur->validN
17027 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 Key = 0;. if( p
17028 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 NewPage->nCell<1
17029 20 7c 7c 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e || pNewPage->in
1702a 74 4b 65 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 tKey!=pCur->apPa
1702b 67 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b ge[i]->intKey ){
1702c 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1702d 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1702e 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1702f 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e LITE_OK;.}..#ifn
17030 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a def NDEBUG./*.**
17031 20 50 61 67 65 20 70 50 61 72 65 6e 74 20 69 73 Page pParent is
17032 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f an internal (no
17033 6e 2d 6c 65 61 66 29 20 74 72 65 65 20 70 61 67 n-leaf) tree pag
17034 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e e. This function
17035 20 0a 2a 2a 20 61 73 73 65 72 74 73 20 74 68 61 .** asserts tha
17036 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43 t page number iC
17037 68 69 6c 64 20 69 73 20 74 68 65 20 6c 65 66 74 hild is the left
17038 2d 63 68 69 6c 64 20 69 66 20 74 68 65 20 69 49 -child if the iI
17039 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e dx'th.** cell in
1703a 20 70 61 67 65 20 70 50 61 72 65 6e 74 2e 20 4f page pParent. O
1703b 72 2c 20 69 66 20 69 49 64 78 20 69 73 20 65 71 r, if iIdx is eq
1703c 75 61 6c 20 74 6f 20 74 68 65 20 74 6f 74 61 6c ual to the total
1703d 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 number of.** ce
1703e 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 lls in pParent,
1703f 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72 that page number
17040 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 72 iChild is the r
17041 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a ight-child of.**
17042 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 the page..*/.st
17043 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 atic void assert
17044 50 61 72 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 ParentIndex(MemP
17045 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e age *pParent, in
17046 74 20 69 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 t iIdx, Pgno iCh
17047 69 6c 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ild){. assert(
17048 69 49 64 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e iIdx<=pParent->n
17049 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 Cell );. if( iI
1704a 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 dx==pParent->nCe
1704b 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 ll ){. assert
1704c 28 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 ( get4byte(&pPar
1704d 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 ent->aData[pPare
1704e 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d nt->hdrOffset+8]
1704f 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d )==iChild );. }
17050 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
17051 28 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 ( get4byte(findC
17052 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 49 64 ell(pParent, iId
17053 78 29 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 x))==iChild );.
17054 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 }.}.#else.# de
17055 66 69 6e 65 20 61 73 73 65 72 74 50 61 72 65 6e fine assertParen
17056 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 tIndex(x,y,z) .#
17057 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 endif../*.** Mov
17058 65 20 74 68 65 20 63 75 72 73 6f 72 20 75 70 20 e the cursor up
17059 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 to the parent pa
1705a 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e ge..**.** pCur->
1705b 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 74 68 idx is set to th
1705c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 e cell index tha
1705d 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 t contains the p
1705e 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 ointer.** to the
1705f 20 70 61 67 65 20 77 65 20 61 72 65 20 63 6f 6d page we are com
17060 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 ing from. If we
17061 20 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d are coming from
17062 20 74 68 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f the.** right-mo
17063 73 74 20 63 68 69 6c 64 20 70 61 67 65 20 74 68 st child page th
17064 65 6e 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 en pCur->idx is
17065 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 set to one more
17066 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 than.** the larg
17067 65 73 74 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a est cell index..
17068 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d */.static void m
17069 6f 76 65 54 6f 50 61 72 65 6e 74 28 42 74 43 75 oveToParent(BtCu
1706a 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 rsor *pCur){. a
1706b 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c ssert( cursorHol
1706c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b dsMutex(pCur) );
1706d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
1706e 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
1706f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 VALID );. asser
17070 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 t( pCur->iPage>0
17071 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 );. assert( pC
17072 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
17073 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 >iPage] );. ass
17074 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 0a ertParentIndex(.
17075 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 pCur->apPage
17076 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c [pCur->iPage-1],
17077 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 . pCur->aiId
17078 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d x[pCur->iPage-1]
17079 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50 , . pCur->apP
1707a 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
1707b 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 ->pgno. );. re
1707c 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e leasePage(pCur->
1707d 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
1707e 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 ge]);. pCur->iP
1707f 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 age--;. pCur->i
17080 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nfo.nSize = 0;.
17081 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 pCur->validNKey
17082 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d = 0;.}../*.** M
17083 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 ove the cursor t
17084 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 o point to the r
17085 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 74 73 20 oot page of its
17086 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 b-tree structure
17087 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 ..**.** If the t
17088 61 62 6c 65 20 68 61 73 20 61 20 76 69 72 74 75 able has a virtu
17089 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 al root page, th
1708a 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 en the cursor is
1708b 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a moved to point.
1708c 2a 2a 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 ** to the virtua
1708d 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 l root page inst
1708e 65 61 64 20 6f 66 20 74 68 65 20 61 63 74 75 61 ead of the actua
1708f 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 l root page. A t
17090 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 able has a.** vi
17091 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 rtual root page
17092 77 68 65 6e 20 74 68 65 20 61 63 74 75 61 6c 20 when the actual
17093 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e 74 61 69 root page contai
17094 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 ns no cells and
17095 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 a .** single chi
17096 6c 64 20 70 61 67 65 2e 20 54 68 69 73 20 63 61 ld page. This ca
17097 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 n only happen wi
17098 74 68 20 74 68 65 20 74 61 62 6c 65 20 72 6f 6f th the table roo
17099 74 65 64 20 61 74 20 70 61 67 65 20 31 2e 0a 2a ted at page 1..*
1709a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 2d 74 72 *.** If the b-tr
1709b 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20 ee structure is
1709c 65 6d 70 74 79 2c 20 74 68 65 20 63 75 72 73 6f empty, the curso
1709d 72 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 r state is set t
1709e 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 o .** CURSOR_INV
1709f 41 4c 49 44 2e 20 4f 74 68 65 72 77 69 73 65 2c ALID. Otherwise,
170a0 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 73 the cursor is s
170a1 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 et to point to t
170a2 68 65 20 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c he first.** cell
170a3 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 located on the
170a4 72 6f 6f 74 20 28 6f 72 20 76 69 72 74 75 61 6c root (or virtual
170a5 20 72 6f 6f 74 29 20 70 61 67 65 20 61 6e 64 20 root) page and
170a6 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 the cursor state
170a7 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 43 55 .** is set to CU
170a8 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a RSOR_VALID..**.*
170a9 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 * If this functi
170aa 6f 6e 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 on returns succe
170ab 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 ssfully, it may
170ac 62 65 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 be assumed that
170ad 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 the.** page-head
170ae 65 72 20 66 6c 61 67 73 20 69 6e 64 69 63 61 74 er flags indicat
170af 65 20 74 68 61 74 20 74 68 65 20 5b 76 69 72 74 e that the [virt
170b0 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 ual] root-page i
170b1 73 20 74 68 65 20 65 78 70 65 63 74 65 64 20 0a s the expected .
170b2 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 ** kind of b-tre
170b3 65 20 70 61 67 65 20 28 69 2e 65 2e 20 69 66 20 e page (i.e. if
170b4 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 when opening the
170b5 20 63 75 72 73 6f 72 20 74 68 65 20 63 61 6c 6c cursor the call
170b6 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 er did not.** sp
170b7 65 63 69 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 ecify a KeyInfo
170b8 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66 6c structure the fl
170b9 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74 20 ags byte is set
170ba 74 6f 20 30 78 30 35 20 6f 72 20 30 78 30 44 2c to 0x05 or 0x0D,
170bb 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 61 .** indicating a
170bc 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f table b-tree, o
170bd 72 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 20 r if the caller
170be 64 69 64 20 73 70 65 63 69 66 79 20 61 20 4b 65 did specify a Ke
170bf 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 yInfo .** struct
170c0 75 72 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 ure the flags by
170c1 74 65 20 69 73 20 73 65 74 20 74 6f 20 30 78 30 te is set to 0x0
170c2 32 20 6f 72 20 30 78 30 41 2c 20 69 6e 64 69 63 2 or 0x0A, indic
170c3 61 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a ating an index.*
170c4 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 * b-tree)..*/.st
170c5 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 atic int moveToR
170c6 6f 6f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 oot(BtCursor *pC
170c7 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a ur){. MemPage *
170c8 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 pRoot;. int rc
170c9 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 = SQLITE_OK;. B
170ca 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e tree *p = pCur->
170cb 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72 pBtree;. BtShar
170cc 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 ed *pBt = p->pBt
170cd 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 ;.. assert( cur
170ce 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 sorHoldsMutex(pC
170cf 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ur) );. assert(
170d0 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 CURSOR_INVALID
170d1 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 < CURSOR_REQUIRE
170d2 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 SEEK );. assert
170d3 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 ( CURSOR_VALID
170d4 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 < CURSOR_REQUIR
170d5 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 ESEEK );. asser
170d6 74 28 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 t( CURSOR_FAULT
170d7 20 20 3e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 > CURSOR_REQUI
170d8 52 45 53 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 RESEEK );. if(
170d9 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 pCur->eState>=CU
170da 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b RSOR_REQUIRESEEK
170db 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 ){. if( pCur
170dc 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
170dd 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 _FAULT ){.
170de 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b assert( pCur->sk
170df 69 70 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f ipNext!=SQLITE_O
170e0 4b 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 K );. retur
170e1 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 n pCur->skipNext
170e2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
170e3 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 te3BtreeClearCur
170e4 73 6f 72 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a sor(pCur);. }..
170e5 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 if( pCur->iPag
170e6 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 e>=0 ){. int
170e7 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 i;. for(i=1;
170e8 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 i<=pCur->iPage;
170e9 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 i++){. rele
170ea 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 asePage(pCur->ap
170eb 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a Page[i]);. }.
170ec 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 pCur->iPage
170ed 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 = 0;. }else{.
170ee 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 rc = getAndIni
170ef 74 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d tPage(pBt, pCur-
170f0 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 >pgnoRoot, &pCur
170f1 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 ->apPage[0]);.
170f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
170f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 75 _OK ){. pCu
170f4 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 r->eState = CURS
170f5 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 OR_INVALID;.
170f6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
170f7 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61 }. pCur->iPa
170f8 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 ge = 0;.. /*
170f9 49 66 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 If pCur->pKeyInf
170fa 6f 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 o is not NULL, t
170fb 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 74 hen the caller t
170fc 68 61 74 20 6f 70 65 6e 65 64 20 74 68 69 73 20 hat opened this
170fd 63 75 72 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 cursor. ** ex
170fe 70 65 63 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 pected to open i
170ff 74 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d t on an index b-
17100 74 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c tree. Otherwise,
17101 20 69 66 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a if pKeyInfo is.
17102 20 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 ** NULL, the
17103 20 63 61 6c 6c 65 72 20 65 78 70 65 63 74 73 20 caller expects
17104 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 a table b-tree.
17105 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 If this is not t
17106 68 65 20 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 he case,. **
17107 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 return an SQLITE
17108 5f 43 4f 52 52 55 50 54 20 65 72 72 6f 72 2e 20 _CORRUPT error.
17109 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
1710a 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d pCur->apPage[0]-
1710b 3e 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 >intKey==1 || pC
1710c 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 ur->apPage[0]->i
1710d 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 ntKey==0 );.
1710e 69 66 28 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 if( (pCur->pKeyI
1710f 6e 66 6f 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 nfo==0)!=pCur->a
17110 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 pPage[0]->intKey
17111 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
17112 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
17113 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a BKPT;. }. }.
17114 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 . /* Assert tha
17115 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 t the root page
17116 69 73 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 is of the correc
17117 74 20 74 79 70 65 2e 20 54 68 69 73 20 6d 75 73 t type. This mus
17118 74 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 t be the. ** ca
17119 73 65 20 61 73 20 74 68 65 20 63 61 6c 6c 20 74 se as the call t
1711a 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 o this function
1711b 74 68 61 74 20 6c 6f 61 64 65 64 20 74 68 65 20 that loaded the
1711c 72 6f 6f 74 2d 70 61 67 65 20 28 65 69 74 68 65 root-page (eithe
1711d 72 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c r. ** this call
1711e 20 6f 72 20 61 20 70 72 65 76 69 6f 75 73 20 69 or a previous i
1711f 6e 76 6f 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 nvocation) would
17120 20 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 have detected c
17121 6f 72 72 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 orruption . **
17122 69 66 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f if the assumptio
17123 6e 20 77 65 72 65 20 6e 6f 74 20 74 72 75 65 2c n were not true,
17124 20 61 6e 64 20 69 74 20 69 73 20 6e 6f 74 20 70 and it is not p
17125 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 ossible for the
17126 66 6c 61 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 flags . ** byte
17127 20 74 6f 20 68 61 76 65 20 62 65 65 6e 20 6d 6f to have been mo
17128 64 69 66 69 65 64 20 77 68 69 6c 65 20 74 68 69 dified while thi
17129 73 20 63 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 s cursor is hold
1712a 69 6e 67 20 61 20 72 65 66 65 72 65 6e 63 65 0a ing a reference.
1712b 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 ** to the page
1712c 2e 20 20 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 . */. pRoot =
1712d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b pCur->apPage[0];
1712e 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 . assert( pRoot
1712f 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 ->pgno==pCur->pg
17130 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 noRoot );. asse
17131 72 74 28 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 rt( pRoot->isIni
17132 74 20 26 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 t && (pCur->pKey
17133 49 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d Info==0)==pRoot-
17134 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 >intKey );.. pC
17135 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 ur->aiIdx[0] = 0
17136 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e ;. pCur->info.n
17137 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 Size = 0;. pCur
17138 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 ->atLast = 0;.
17139 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1713a 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f = 0;.. if( pRoo
1713b 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 t->nCell==0 && !
1713c 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 pRoot->leaf ){.
1713d 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b Pgno subpage;
1713e 0a 20 20 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e . if( pRoot->
1713f 70 67 6e 6f 21 3d 31 20 29 20 72 65 74 75 72 6e pgno!=1 ) return
17140 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
17141 42 4b 50 54 3b 0a 20 20 20 20 73 75 62 70 61 67 BKPT;. subpag
17142 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 52 e = get4byte(&pR
17143 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 oot->aData[pRoot
17144 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
17145 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 . pCur->eStat
17146 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 e = CURSOR_VALID
17147 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 ;. rc = moveT
17148 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 73 75 62 oChild(pCur, sub
17149 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a page);. }else{.
1714a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 pCur->eState
1714b 20 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c = ((pRoot->nCel
1714c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 l>0)?CURSOR_VALI
1714d 44 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 D:CURSOR_INVALID
1714e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
1714f 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 rc;.}../*.** Mov
17150 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 e the cursor dow
17151 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f n to the left-mo
17152 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 st leaf entry be
17153 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 neath the.** ent
17154 72 79 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 ry to which it i
17155 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
17156 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ting..**.** The
17157 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 left-most leaf i
17158 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20 74 s the one with t
17159 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 he smallest key
1715a 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 - the first.** i
1715b 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 n ascending orde
1715c 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 r..*/.static int
1715d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 moveToLeftmost(
1715e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b BtCursor *pCur){
1715f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 . Pgno pgno;.
17160 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
17161 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 OK;. MemPage *p
17162 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 Page;.. assert(
17163 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 cursorHoldsMute
17164 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 x(pCur) );. ass
17165 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 ert( pCur->eStat
17166 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 e==CURSOR_VALID
17167 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d );. while( rc==
17168 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 SQLITE_OK && !(p
17169 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 Page = pCur->apP
1716a 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d age[pCur->iPage]
1716b 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 )->leaf ){. a
1716c 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 ssert( pCur->aiI
1716d 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c dx[pCur->iPage]<
1716e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pPage->nCell );.
1716f 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 pgno = get4b
17170 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 yte(findCell(pPa
17171 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b ge, pCur->aiIdx[
17172 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a pCur->iPage]));.
17173 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 rc = moveToC
17174 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 hild(pCur, pgno)
17175 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
17176 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 c;.}../*.** Move
17177 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e the cursor down
17178 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f to the right-mo
17179 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20 62 65 st leaf entry be
1717a 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 neath the.** pag
1717b 65 20 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 e to which it is
1717c 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 currently point
1717d 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 ing. Notice the
1717e 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 difference.** b
1717f 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 etween moveToLef
17180 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 tmost() and move
17181 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 ToRightmost().
17182 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 moveToLeftmost()
17183 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20 6c 65 .** finds the le
17184 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 ft-most entry be
17185 6e 65 61 74 68 20 74 68 65 20 2a 65 6e 74 72 79 neath the *entry
17186 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 65 54 6f * whereas moveTo
17187 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 Rightmost().** f
17188 69 6e 64 73 20 74 68 65 20 72 69 67 68 74 2d 6d inds the right-m
17189 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61 74 ost entry beneat
1718a 68 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a h the *page*..**
1718b 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f .** The right-mo
1718c 73 74 20 65 6e 74 72 79 20 69 73 20 74 68 65 20 st entry is the
1718d 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6c 61 72 one with the lar
1718e 67 65 73 74 20 6b 65 79 20 2d 20 74 68 65 20 6c gest key - the l
1718f 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 ast.** key in as
17190 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a cending order..*
17191 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 /.static int mov
17192 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42 74 43 eToRightmost(BtC
17193 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
17194 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 Pgno pgno;. int
17195 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
17196 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
17197 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 e = 0;.. assert
17198 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
17199 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
1719a 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 sert( pCur->eSta
1719b 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 te==CURSOR_VALID
1719c 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d );. while( rc=
1719d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 =SQLITE_OK && !(
1719e 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 pPage = pCur->ap
1719f 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 Page[pCur->iPage
171a0 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ])->leaf ){.
171a1 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
171a2 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 &pPage->aData[pP
171a3 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 age->hdrOffset+8
171a4 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 ]);. pCur->ai
171a5 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
171a6 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b = pPage->nCell;
171a7 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f . rc = moveTo
171a8 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f Child(pCur, pgno
171a9 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d );. }. if( rc=
171aa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
171ab 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 pCur->aiIdx[pC
171ac 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 ur->iPage] = pPa
171ad 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 ge->nCell-1;.
171ae 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a pCur->info.nSiz
171af 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d e = 0;. pCur-
171b0 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
171b1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
171b2 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 .}../* Move the
171b3 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 cursor to the fi
171b4 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 rst entry in the
171b5 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 table. Return
171b6 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 SQLITE_OK.** on
171b7 73 75 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 success. Set *p
171b8 52 65 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 Res to 0 if the
171b9 63 75 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 cursor actually
171ba 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 points to someth
171bb 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 ing.** or set *p
171bc 52 65 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 Res to 1 if the
171bd 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a table is empty..
171be 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
171bf 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
171c0 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 eeFirst(BtCursor
171c1 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 *pCur, int *pRe
171c2 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 s){. int rc;..
171c3 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 assert( cursorH
171c4 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 oldsMutex(pCur)
171c5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c );. assert( sql
171c6 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
171c7 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 pCur->pBtree->db
171c8 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 ->mutex) );. rc
171c9 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 = moveToRoot(pC
171ca 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 ur);. if( rc==S
171cb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
171cc 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 if( pCur->eState
171cd 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 ==CURSOR_INVALID
171ce 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
171cf 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
171d0 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 Cur->iPage]->nCe
171d1 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a ll==0 );. *
171d2 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 pRes = 1;.
171d3 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
171d4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
171d5 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
171d6 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
171d7 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 e]->nCell>0 );.
171d8 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a *pRes = 0;.
171d9 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 rc = moveT
171da 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b oLeftmost(pCur);
171db 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 . }. }. ret
171dc 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f urn rc;.}../* Mo
171dd 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f ve the cursor to
171de 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
171df 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 in the table. R
171e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a eturn SQLITE_OK.
171e1 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 ** on success.
171e2 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 Set *pRes to 0 i
171e3 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 f the cursor act
171e4 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 ually points to
171e5 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 something.** or
171e6 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 set *pRes to 1 i
171e7 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 f the table is e
171e8 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f mpty..*/.SQLITE_
171e9 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
171ea 74 65 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 te3BtreeLast(BtC
171eb 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 ursor *pCur, int
171ec 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 *pRes){. int r
171ed 63 3b 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 c;. . assert( c
171ee 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 ursorHoldsMutex(
171ef 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 pCur) );. asser
171f0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
171f1 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 _held(pCur->pBtr
171f2 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 ee->db->mutex) )
171f3 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 ;.. /* If the c
171f4 75 72 73 6f 72 20 61 6c 72 65 61 64 79 20 70 6f ursor already po
171f5 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 ints to the last
171f6 20 65 6e 74 72 79 2c 20 74 68 69 73 20 69 73 20 entry, this is
171f7 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 a no-op. */. if
171f8 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d ( CURSOR_VALID==
171f9 70 43 75 72 2d 3e 65 53 74 61 74 65 20 26 26 20 pCur->eState &&
171fa 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a pCur->atLast ){.
171fb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
171fc 42 55 47 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 BUG. /* This
171fd 62 6c 6f 63 6b 20 73 65 72 76 65 73 20 74 6f 20 block serves to
171fe 61 73 73 65 72 74 28 29 20 74 68 61 74 20 74 68 assert() that th
171ff 65 20 63 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 e cursor really
17200 64 6f 65 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 does point .
17201 2a 2a 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 ** to the last e
17202 6e 74 72 79 20 69 6e 20 74 68 65 20 62 2d 74 72 ntry in the b-tr
17203 65 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ee. */. int i
17204 69 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b i;. for(ii=0;
17205 20 69 69 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b ii<pCur->iPage;
17206 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 ii++){. as
17207 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 sert( pCur->aiId
17208 78 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 x[ii]==pCur->apP
17209 61 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 age[ii]->nCell )
1720a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
1720b 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b rt( pCur->aiIdx[
1720c 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 pCur->iPage]==pC
1720d 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1720e 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 >iPage]->nCell-1
1720f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
17210 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
17211 72 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 r->iPage]->leaf
17212 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 );.#endif. re
17213 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17214 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 }.. rc = move
17215 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 ToRoot(pCur);.
17216 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
17217 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 43 55 52 K ){. if( CUR
17218 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 SOR_INVALID==pCu
17219 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 r->eState ){.
1721a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d assert( pCur-
1721b 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
1721c 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 age]->nCell==0 )
1721d 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 ;. *pRes =
1721e 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
1721f 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
17220 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
17221 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 _VALID );.
17222 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 *pRes = 0;.
17223 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 rc = moveToRigh
17224 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 tmost(pCur);.
17225 20 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 pCur->atLast
17226 3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 = rc==SQLITE_OK
17227 3f 31 3a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ?1:0;. }. }.
17228 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
17229 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 /* Move the curs
1722a 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f or so that it po
1722b 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 ints to an entry
1722c 20 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a near the key .*
1722d 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 * specified by p
1722e 49 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 IdxKey or intKey
1722f 2e 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 . Return a suc
17230 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a cess code..**.**
17231 20 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c For INTKEY tabl
17232 65 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 es, the intKey p
17233 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 arameter is used
17234 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d . pIdxKey .** m
17235 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f ust be NULL. Fo
17236 72 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 r index tables,
17237 70 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 pIdxKey is used
17238 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 and intKey.** is
17239 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 ignored..**.**
1723a 49 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 If an exact matc
1723b 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 h is not found,
1723c 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 then the cursor
1723d 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 is always.** lef
1723e 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 t pointing at a
1723f 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 leaf page which
17240 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 would hold the e
17241 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 ntry if it.** we
17242 72 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 re present. The
17243 20 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f cursor might po
17244 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 int to an entry
17245 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 that comes.** be
17246 66 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 fore or after th
17247 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 e key..**.** An
17248 69 6e 74 65 67 65 72 20 69 73 20 77 72 69 74 74 integer is writt
17249 65 6e 20 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 en into *pRes wh
1724a 69 63 68 20 69 73 20 74 68 65 20 72 65 73 75 6c ich is the resul
1724b 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e t of.** comparin
1724c 67 20 74 68 65 20 6b 65 79 20 77 69 74 68 20 74 g the key with t
1724d 68 65 20 65 6e 74 72 79 20 74 6f 20 77 68 69 63 he entry to whic
1724e 68 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 h the cursor is
1724f 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 .** pointing. T
17250 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 he meaning of th
17251 65 20 69 6e 74 65 67 65 72 20 77 72 69 74 74 65 e integer writte
17252 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 n into.** *pRes
17253 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a is as follows:.*
17254 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 *.** *pRes<0
17255 20 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 The cursor
17256 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
17257 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 g at an entry th
17258 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 at.**
17259 20 20 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 is smalle
1725a 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 r than intKey/pI
1725b 64 78 4b 65 79 20 6f 72 20 69 66 20 74 68 65 20 dxKey or if the
1725c 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a table is empty.*
1725d 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
1725e 20 20 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f and the curso
1725f 72 20 69 73 20 74 68 65 72 65 66 6f 72 65 20 6c r is therefore l
17260 65 66 74 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 eft point to not
17261 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 hing..**.**
17262 2a 70 52 65 73 3d 3d 30 20 20 20 20 20 54 68 65 *pRes==0 The
17263 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 cursor is left
17264 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 pointing at an e
17265 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 ntry that.**
17266 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 78 ex
17267 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20 69 6e actly matches in
17268 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a tKey/pIdxKey..**
17269 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 .** *pRes>0
1726a 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 The cursor
1726b 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
1726c 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 at an entry tha
1726d 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 t.**
1726e 20 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 is larger
1726f 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 than intKey/pIdx
17270 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 Key..**.*/.SQLIT
17271 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
17272 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f lite3BtreeMoveto
17273 55 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 Unpacked(. BtCu
17274 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 rsor *pCur,
17275 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 /* The curs
17276 6f 72 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a or to be moved *
17277 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f /. UnpackedReco
17278 72 64 20 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 rd *pIdxKey, /*
17279 55 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b Unpacked index k
1727a 65 79 20 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b ey */. i64 intK
1727b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 ey,
1727c 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 6b 65 /* The table ke
1727d 79 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 y */. int biasR
1727e 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 ight,
1727f 2f 2a 20 49 66 20 74 72 75 65 2c 20 62 69 61 73 /* If true, bias
17280 20 74 68 65 20 73 65 61 72 63 68 20 74 6f 20 74 the search to t
17281 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 he high end */.
17282 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 int *pRes
17283 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
17284 74 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 te search result
17285 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 s here */.){. i
17286 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 nt rc;.. assert
17287 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 ( cursorHoldsMut
17288 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 ex(pCur) );. as
17289 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1728a 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 tex_held(pCur->p
1728b 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 Btree->db->mutex
1728c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1728d 52 65 73 20 29 3b 0a 20 20 61 73 73 65 72 74 28 Res );. assert(
1728e 20 28 70 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 (pIdxKey==0)==(
1728f 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d pCur->pKeyInfo==
17290 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 0) );.. /* If t
17291 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 72 he cursor is alr
17292 65 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 eady positioned
17293 61 74 20 74 68 65 20 70 6f 69 6e 74 20 77 65 20 at the point we
17294 61 72 65 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 are trying. **
17295 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e to move to, then
17296 20 6a 75 73 74 20 72 65 74 75 72 6e 20 77 69 74 just return wit
17297 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 hout doing any w
17298 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 ork */. if( pCu
17299 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
1729a 52 5f 56 41 4c 49 44 20 26 26 20 70 43 75 72 2d R_VALID && pCur-
1729b 3e 76 61 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 >validNKey . &
1729c 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 & pCur->apPage[0
1729d 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a ]->intKey . ){.
1729e 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 6e if( pCur->in
1729f 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 fo.nKey==intKey
172a0 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d ){. *pRes =
172a1 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 0;. return
172a2 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
172a3 7d 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e }. if( pCur->
172a4 61 74 4c 61 73 74 20 26 26 20 70 43 75 72 2d 3e atLast && pCur->
172a5 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 info.nKey<intKey
172a6 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 ){. *pRes
172a7 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 = -1;. retu
172a8 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
172a9 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 }. }.. rc =
172aa 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 moveToRoot(pCur)
172ab 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
172ac 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d return rc;. }
172ad 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d . assert( pCur-
172ae 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
172af 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 age] );. assert
172b0 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 ( pCur->apPage[p
172b1 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 Cur->iPage]->isI
172b2 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nit );. assert(
172b3 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 pCur->apPage[pC
172b4 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
172b5 6c 3e 30 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 l>0 || pCur->eSt
172b6 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 ate==CURSOR_INVA
172b7 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 LID );. if( pCu
172b8 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f r->eState==CURSO
172b9 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 R_INVALID ){.
172ba 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 *pRes = -1;.
172bb 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 assert( pCur->a
172bc 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
172bd 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a e]->nCell==0 );.
172be 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
172bf 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 E_OK;. }. asse
172c0 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 rt( pCur->apPage
172c1 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 [0]->intKey || p
172c2 49 64 78 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 IdxKey );. for(
172c3 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 ;;){. int lwr
172c4 2c 20 75 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 , upr;. Pgno
172c5 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 chldPg;. MemP
172c6 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 age *pPage = pCu
172c7 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e r->apPage[pCur->
172c8 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 iPage];. int
172c9 63 3b 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 c;.. /* pPage
172ca 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 ->nCell must be
172cb 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 greater than zer
172cc 6f 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68 o. If this is th
172cd 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 e root-page.
172ce 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20 77 6f ** the cursor wo
172cf 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 49 4e uld have been IN
172d0 56 41 4c 49 44 20 61 62 6f 76 65 20 61 6e 64 20 VALID above and
172d1 74 68 69 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f this for(;;) loo
172d2 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e p. ** not run
172d3 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 . If this is not
172d4 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 the root-page,
172d5 74 68 65 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 then the moveToC
172d6 68 69 6c 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 hild() routine.
172d7 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 ** would have
172d8 20 61 6c 72 65 61 64 79 20 64 65 74 65 63 74 65 already detecte
172d9 64 20 64 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e d db corruption.
172da 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 Similarly, pPag
172db 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 e must. ** be
172dc 20 74 68 65 20 72 69 67 68 74 20 6b 69 6e 64 20 the right kind
172dd 28 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 29 (index or table)
172de 20 6f 66 20 62 2d 74 72 65 65 20 70 61 67 65 2e of b-tree page.
172df 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20 2a Otherwise. *
172e0 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 * a moveToChild(
172e1 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 ) or moveToRoot(
172e2 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 ) call would hav
172e3 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72 75 e detected corru
172e4 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 ption. */. a
172e5 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 ssert( pPage->nC
172e6 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 ell>0 );. ass
172e7 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b ert( pPage->intK
172e8 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 ey==(pIdxKey==0)
172e9 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b );. lwr = 0;
172ea 0a 20 20 20 20 75 70 72 20 3d 20 70 50 61 67 65 . upr = pPage
172eb 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 ->nCell-1;. i
172ec 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a f( biasRight ){.
172ed 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
172ee 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d x[pCur->iPage] =
172ef 20 28 75 31 36 29 75 70 72 3b 0a 20 20 20 20 7d (u16)upr;. }
172f0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 43 75 72 else{. pCur
172f1 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 ->aiIdx[pCur->iP
172f2 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28 75 70 age] = (u16)((up
172f3 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d r+lwr)/2);. }
172f4 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 . for(;;){.
172f5 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 int idx = pC
172f6 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
172f7 69 50 61 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 iPage]; /* Index
172f8 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65 6c 6c of current cell
172f9 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 in pPage */.
172fa 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 u8 *pCell;
172fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
172fc 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
172fd 72 20 74 6f 20 63 75 72 72 65 6e 74 20 63 65 6c r to current cel
172fe 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 l in pPage */..
172ff 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e pCur->info.
17300 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 nSize = 0;.
17301 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
17302 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b 20 l(pPage, idx) +
17303 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
17304 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70 ize;. if( p
17305 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a Page->intKey ){.
17306 20 20 20 20 20 20 20 20 69 36 34 20 6e 43 65 6c i64 nCel
17307 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 lKey;. if
17308 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 ( pPage->hasData
17309 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 ){. u3
1730a 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 2 dummy;.
1730b 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 pCell += getV
1730c 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 arint32(pCell, d
1730d 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d ummy);. }
1730e 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69 . getVari
1730f 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 nt(pCell, (u64*)
17310 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 &nCellKey);.
17311 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 if( nCellKey
17312 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 ==intKey ){.
17313 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 c = 0;.
17314 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e }else if( n
17315 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 CellKey<intKey )
17316 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 {. c =
17317 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 -1;. }els
17318 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 e{. ass
17319 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e ert( nCellKey>in
1731a 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20 tKey );.
1731b 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 c = +1;.
1731c 20 20 7d 0a 20 20 20 20 20 20 20 20 70 43 75 72 }. pCur
1731d 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b ->validNKey = 1;
1731e 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 . pCur->i
1731f 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c nfo.nKey = nCell
17320 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 Key;. }else
17321 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 {. /* The
17322 20 6d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 maximum support
17323 65 64 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 ed page-size is
17324 33 32 37 36 38 20 62 79 74 65 73 2e 20 54 68 69 32768 bytes. Thi
17325 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 20 20 20 s means that.
17326 20 20 20 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 ** the maxi
17327 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 mum number of re
17328 63 6f 72 64 20 62 79 74 65 73 20 73 74 6f 72 65 cord bytes store
17329 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d d on an index B-
1732a 54 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 Tree. **
1732b 70 61 67 65 20 69 73 20 61 74 20 6d 6f 73 74 20 page is at most
1732c 38 31 39 38 20 62 79 74 65 73 2c 20 77 68 69 63 8198 bytes, whic
1732d 68 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 h may be stored
1732e 61 73 20 61 20 32 2d 62 79 74 65 0a 20 20 20 20 as a 2-byte.
1732f 20 20 20 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 ** varint. T
17330 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
17331 69 73 20 75 73 65 64 20 74 6f 20 61 74 74 65 6d is used to attem
17332 70 74 20 74 6f 20 61 76 6f 69 64 20 70 61 72 73 pt to avoid pars
17333 69 6e 67 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 ing . **
17334 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 the entire cell
17335 62 79 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 by checking for
17336 74 68 65 20 63 61 73 65 73 20 77 68 65 72 65 20 the cases where
17337 74 68 65 20 72 65 63 6f 72 64 20 69 73 20 0a 20 the record is .
17338 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 ** stored
17339 20 65 6e 74 69 72 65 6c 79 20 77 69 74 68 69 6e entirely within
1733a 20 74 68 65 20 62 2d 74 72 65 65 20 70 61 67 65 the b-tree page
1733b 20 62 79 20 69 6e 73 70 65 63 74 69 6e 67 20 74 by inspecting t
1733c 68 65 20 66 69 72 73 74 20 0a 20 20 20 20 20 20 he first .
1733d 20 20 2a 2a 20 32 20 62 79 74 65 73 20 6f 66 20 ** 2 bytes of
1733e 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 the cell..
1733f 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 */. int
17340 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 nCell = pCell[0
17341 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 ];. if( !
17342 28 6e 43 65 6c 6c 20 26 20 30 78 38 30 29 20 26 (nCell & 0x80) &
17343 26 20 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e & nCell<=pPage->
17344 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 maxLocal ){.
17345 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 /* This br
17346 61 6e 63 68 20 72 75 6e 73 20 69 66 20 74 68 65 anch runs if the
17347 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 record-size fie
17348 6c 64 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 ld of the cell i
17349 73 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a s a. **
1734a 20 73 69 6e 67 6c 65 20 62 79 74 65 20 76 61 72 single byte var
1734b 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f int and the reco
1734c 72 64 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 rd fits entirely
1734d 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 on the main.
1734e 20 20 20 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 ** b-tree
1734f 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 page. */.
17350 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 c = sqlite3
17351 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 VdbeRecordCompar
17352 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 e(nCell, (void*)
17353 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b &pCell[1], pIdxK
17354 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c ey);. }el
17355 73 65 20 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 se if( !(pCell[1
17356 5d 20 26 20 30 78 38 30 29 20 0a 20 20 20 20 20 ] & 0x80) .
17357 20 20 20 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d && (nCell =
17358 20 28 28 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c ((nCell&0x7f)<<
17359 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 7) + pCell[1])<=
1735a 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a pPage->maxLocal.
1735b 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
1735c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f /* The reco
1735d 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 rd-size field is
1735e 20 61 20 32 20 62 79 74 65 20 76 61 72 69 6e 74 a 2 byte varint
1735f 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20 and the record
17360 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 . ** fi
17361 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 ts entirely on t
17362 68 65 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 he main b-tree p
17363 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 age. */.
17364 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 c = sqlite3Vd
17365 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
17366 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 nCell, (void*)&p
17367 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 Cell[2], pIdxKey
17368 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
17369 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 {. /* T
1736a 68 65 20 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 he record flows
1736b 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 over onto one or
1736c 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 more overflow p
1736d 61 67 65 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 ages. In.
1736e 20 20 20 2a 2a 20 74 68 69 73 20 63 61 73 65 20 ** this case
1736f 74 68 65 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e the whole cell n
17370 65 65 64 73 20 74 6f 20 62 65 20 70 61 72 73 65 eeds to be parse
17371 64 2c 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f d, a buffer allo
17372 63 61 74 65 64 0a 20 20 20 20 20 20 20 20 20 20 cated.
17373 2a 2a 20 61 6e 64 20 61 63 63 65 73 73 50 61 79 ** and accessPay
17374 6c 6f 61 64 28 29 20 75 73 65 64 20 74 6f 20 72 load() used to r
17375 65 74 72 69 65 76 65 20 74 68 65 20 72 65 63 6f etrieve the reco
17376 72 64 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 rd into the.
17377 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20 ** buffer
17378 62 65 66 6f 72 65 20 56 64 62 65 52 65 63 6f 72 before VdbeRecor
17379 64 43 6f 6d 70 61 72 65 28 29 20 63 61 6e 20 62 dCompare() can b
1737a 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 e called. */.
1737b 20 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65 void *pCe
1737c 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 llKey;.
1737d 20 75 38 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c u8 * const pCel
1737e 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 lBody = pCell -
1737f 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 pPage->childPtrS
17380 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20 62 ize;. b
17381 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 treeParseCellPtr
17382 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 (pPage, pCellBod
17383 79 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b y, &pCur->info);
17384 0a 20 20 20 20 20 20 20 20 20 20 6e 43 65 6c 6c . nCell
17385 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e = (int)pCur->in
17386 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 fo.nKey;.
17387 20 20 20 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 pCellKey = sq
17388 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 lite3Malloc( nCe
17389 6c 6c 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ll );.
1738a 69 66 28 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 if( pCellKey==0
1738b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ){. r
1738c 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d c = SQLITE_NOMEM
1738d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f ;. go
1738e 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 to moveto_finish
1738f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
17390 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 63 63 rc = acc
17391 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c essPayload(pCur,
17392 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 0, nCell, (unsi
17393 67 6e 65 64 20 63 68 61 72 2a 29 70 43 65 6c 6c gned char*)pCell
17394 4b 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 Key, 0);.
17395 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 if( rc ){.
17396 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
17397 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b _free(pCellKey);
17398 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 . got
17399 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b o moveto_finish;
1739a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1739b 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 c = sqlit
1739c 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 e3VdbeRecordComp
1739d 61 72 65 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c are(nCell, pCell
1739e 4b 65 79 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 Key, pIdxKey);.
1739f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
173a0 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b _free(pCellKey);
173a1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
173a2 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d }. if( c==
173a3 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 0 ){. if(
173a4 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 pPage->intKey &
173a5 26 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 & !pPage->leaf )
173a6 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 {. lwr
173a7 3d 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 = idx;.
173a8 20 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a upr = lwr - 1;.
173a9 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
173aa 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
173ab 20 20 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 *pRes
173ac 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 = 0;. r
173ad 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
173ae 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f goto mo
173af 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 veto_finish;.
173b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
173b1 20 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a if( c<0 ){.
173b2 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69 64 lwr = id
173b3 78 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 x+1;. }else
173b4 7b 0a 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 {. upr =
173b5 69 64 78 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 idx-1;. }.
173b6 20 20 20 20 20 69 66 28 20 6c 77 72 3e 75 70 72 if( lwr>upr
173b7 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ){. brea
173b8 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
173b9 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
173ba 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 r->iPage] = (u16
173bb 29 28 28 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a )((lwr+upr)/2);.
173bc 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 }. assert
173bd 28 20 6c 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a ( lwr==upr+1 );.
173be 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 assert( pPag
173bf 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 e->isInit );.
173c0 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 if( pPage->leaf
173c1 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 ){. chldPg
173c2 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
173c3 69 66 28 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e if( lwr>=pPage->
173c4 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 nCell ){. c
173c5 68 6c 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 hldPg = get4byte
173c6 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 (&pPage->aData[p
173c7 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b Page->hdrOffset+
173c8 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 8]);. }else{.
173c9 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67 chldPg = g
173ca 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c et4byte(findCell
173cb 28 70 50 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 (pPage, lwr));.
173cc 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6c }. if( chl
173cd 64 50 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 dPg==0 ){.
173ce 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 assert( pCur->ai
173cf 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
173d0 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 <pCur->apPage[pC
173d1 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c ur->iPage]->nCel
173d2 6c 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 l );. *pRes
173d3 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63 20 3d = c;. rc =
173d4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
173d5 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 goto moveto_fi
173d6 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 nish;. }.
173d7 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 pCur->aiIdx[pCur
173d8 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 ->iPage] = (u16)
173d9 6c 77 72 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 lwr;. pCur->i
173da 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nfo.nSize = 0;.
173db 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b pCur->validNK
173dc 65 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d ey = 0;. rc =
173dd 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 moveToChild(pCu
173de 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 r, chldPg);.
173df 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f if( rc ) goto mo
173e0 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d veto_finish;. }
173e1 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a .moveto_finish:.
173e2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
173e3 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 ./*.** Return TR
173e4 55 45 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 UE if the cursor
173e5 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 is not pointing
173e6 20 61 74 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 at an entry of
173e7 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a the table..**.**
173e8 20 54 52 55 45 20 77 69 6c 6c 20 62 65 20 72 65 TRUE will be re
173e9 74 75 72 6e 65 64 20 61 66 74 65 72 20 61 20 63 turned after a c
173ea 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 all to sqlite3Bt
173eb 72 65 65 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a reeNext() moves.
173ec 2a 2a 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 ** past the last
173ed 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 entry in the ta
173ee 62 6c 65 20 6f 72 20 73 71 6c 69 74 65 33 42 74 ble or sqlite3Bt
173ef 72 65 65 50 72 65 76 28 29 20 6d 6f 76 65 73 20 reePrev() moves
173f0 70 61 73 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 past.** the firs
173f1 74 20 65 6e 74 72 79 2e 20 20 54 52 55 45 20 69 t entry. TRUE i
173f2 73 20 61 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 s also returned
173f3 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 if the table is
173f4 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 empty..*/.SQLITE
173f5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
173f6 69 74 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 ite3BtreeEof(BtC
173f7 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 ursor *pCur){.
173f8 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 /* TODO: What if
173f9 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 69 the cursor is i
173fa 6e 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 n CURSOR_REQUIRE
173fb 53 45 45 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 SEEK but all tab
173fc 6c 65 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 le entries. **
173fd 68 61 76 65 20 62 65 65 6e 20 64 65 6c 65 74 65 have been delete
173fe 64 3f 20 54 68 69 73 20 41 50 49 20 77 69 6c 6c d? This API will
173ff 20 6e 65 65 64 20 74 6f 20 63 68 61 6e 67 65 20 need to change
17400 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 to return an err
17401 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 or code. ** as
17402 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c well as the bool
17403 65 61 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 ean result value
17404 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 .. */. return
17405 28 43 55 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 (CURSOR_VALID!=p
17406 43 75 72 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a Cur->eState);.}.
17407 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 ./*.** Advance t
17408 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
17409 20 6e 65 78 74 20 65 6e 74 72 79 20 69 6e 20 74 next entry in t
1740a 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 he database. If
1740b 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 .** successful t
1740c 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e hen set *pRes=0.
1740d 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a If the cursor.
1740e 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 ** was already p
1740f 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c ointing to the l
17410 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 ast entry in the
17411 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 database before
17412 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 .** this routine
17413 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 was called, the
17414 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a n set *pRes=1..*
17415 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
17416 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 int sqlite3Btre
17417 65 4e 65 78 74 28 42 74 43 75 72 73 6f 72 20 2a eNext(BtCursor *
17418 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 pCur, int *pRes)
17419 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e {. int rc;. in
1741a 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 t idx;. MemPage
1741b 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 *pPage;.. asse
1741c 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d rt( cursorHoldsM
1741d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 utex(pCur) );.
1741e 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 rc = restoreCurs
1741f 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 orPosition(pCur)
17420 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 ;. if( rc!=SQLI
17421 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 TE_OK ){. ret
17422 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 urn rc;. }. as
17423 73 65 72 74 28 20 70 52 65 73 21 3d 30 20 29 3b sert( pRes!=0 );
17424 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e . if( CURSOR_IN
17425 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 VALID==pCur->eSt
17426 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 ate ){. *pRes
17427 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e = 1;. return
17428 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a SQLITE_OK;. }.
17429 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 if( pCur->skip
1742a 4e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 Next>0 ){. pC
1742b 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 ur->skipNext = 0
1742c 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b ;. *pRes = 0;
1742d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1742e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 TE_OK;. }. pCu
1742f 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b r->skipNext = 0;
17430 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 .. pPage = pCur
17431 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 ->apPage[pCur->i
17432 50 61 67 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b Page];. idx = +
17433 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 +pCur->aiIdx[pCu
17434 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 r->iPage];. ass
17435 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e ert( pPage->isIn
17436 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 it );. assert(
17437 69 64 78 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c idx<=pPage->nCel
17438 6c 20 29 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e l );.. pCur->in
17439 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 fo.nSize = 0;.
1743a 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 pCur->validNKey
1743b 3d 20 30 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d = 0;. if( idx>=
1743c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a pPage->nCell ){.
1743d 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e if( !pPage->
1743e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 leaf ){. rc
1743f 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 = moveToChild(p
17440 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 Cur, get4byte(&p
17441 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
17442 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
17443 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 );. if( rc
17444 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
17445 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 rc = moveToLe
17446 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 ftmost(pCur);.
17447 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 *pRes = 0;.
17448 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
17449 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 }. do{.
1744a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 if( pCur->iP
1744b 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 age==0 ){.
1744c 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 *pRes = 1;.
1744d 20 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 pCur->eStat
1744e 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c e = CURSOR_INVAL
1744f 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ID;. retu
17450 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
17451 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 }. move
17452 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a ToParent(pCur);.
17453 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 pPage = pC
17454 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
17455 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 >iPage];. }wh
17456 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78 ile( pCur->aiIdx
17457 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 [pCur->iPage]>=p
17458 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 Page->nCell );.
17459 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 *pRes = 0;.
1745a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 if( pPage->int
1745b 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 Key ){. rc
1745c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 = sqlite3BtreeNe
1745d 78 74 28 70 43 75 72 2c 20 70 52 65 73 29 3b 0a xt(pCur, pRes);.
1745e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1745f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc = SQLITE_OK;
17460 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
17461 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 n rc;. }. *pRe
17462 73 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 s = 0;. if( pPa
17463 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 ge->leaf ){.
17464 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17465 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 ;. }. rc = mov
17466 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 eToLeftmost(pCur
17467 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
17468 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 }.../*.** Step t
17469 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 he cursor to the
1746a 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 72 65 back to the pre
1746b 76 69 6f 75 73 20 65 6e 74 72 79 20 69 6e 20 74 vious entry in t
1746c 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 he database. If
1746d 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 74 .** successful t
1746e 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 30 2e hen set *pRes=0.
1746f 20 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 0a If the cursor.
17470 2a 2a 20 77 61 73 20 61 6c 72 65 61 64 79 20 70 ** was already p
17471 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 66 ointing to the f
17472 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 irst entry in th
17473 65 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 e database befor
17474 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e e.** this routin
17475 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 e was called, th
17476 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a en set *pRes=1..
17477 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
17478 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 E int sqlite3Btr
17479 65 65 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 eePrevious(BtCur
1747a 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a sor *pCur, int *
1747b 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b pRes){. int rc;
1747c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
1747d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 e;.. assert( cu
1747e 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 rsorHoldsMutex(p
1747f 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 Cur) );. rc = r
17480 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 estoreCursorPosi
17481 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 tion(pCur);. if
17482 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
17483 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
17484 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 ;. }. pCur->at
17485 4c 61 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 Last = 0;. if(
17486 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d CURSOR_INVALID==
17487 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a pCur->eState ){.
17488 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 *pRes = 1;.
17489 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1748a 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 _OK;. }. if( p
1748b 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 Cur->skipNext<0
1748c 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 ){. pCur->ski
1748d 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a pNext = 0;. *
1748e 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 pRes = 0;. re
1748f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
17490 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 }. pCur->skip
17491 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 Next = 0;.. pPa
17492 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
17493 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
17494 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17495 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 >isInit );. if(
17496 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b !pPage->leaf ){
17497 0a 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70 . int idx = p
17498 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
17499 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 >iPage];. rc
1749a 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
1749b 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69 6e ur, get4byte(fin
1749c 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 dCell(pPage, idx
1749d 29 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 )));. if( rc
1749e 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
1749f 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 rc;. }. rc
174a0 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f = moveToRightmo
174a1 73 74 28 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 st(pCur);. }els
174a2 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 e{. while( pC
174a3 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e ur->aiIdx[pCur->
174a4 69 50 61 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 iPage]==0 ){.
174a5 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 if( pCur->iPa
174a6 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ge==0 ){.
174a7 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 pCur->eState =
174a8 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a CURSOR_INVALID;.
174a9 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 *pRes =
174aa 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 1;. retur
174ab 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 n SQLITE_OK;.
174ac 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 }. moveT
174ad 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 oParent(pCur);.
174ae 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 }. pCur->i
174af 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 nfo.nSize = 0;.
174b0 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b pCur->validNK
174b1 65 79 20 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 ey = 0;.. pCu
174b2 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 r->aiIdx[pCur->i
174b3 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 Page]--;. pPa
174b4 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 ge = pCur->apPag
174b5 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a e[pCur->iPage];.
174b6 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 if( pPage->i
174b7 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d ntKey && !pPage-
174b8 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72 >leaf ){. r
174b9 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
174ba 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20 70 Previous(pCur, p
174bb 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Res);. }else{
174bc 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
174bd 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d TE_OK;. }. }
174be 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 . *pRes = 0;.
174bf 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
174c0 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e .** Allocate a n
174c1 65 77 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 ew page from the
174c2 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a database file..
174c3 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 **.** The new pa
174c4 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 ge is marked as
174c5 64 69 72 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 dirty. (In othe
174c6 72 20 77 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 r words, sqlite3
174c7 50 61 67 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 PagerWrite().**
174c8 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
174c9 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e called on the n
174ca 65 77 20 70 61 67 65 2e 29 20 20 54 68 65 20 6e ew page.) The n
174cb 65 77 20 70 61 67 65 20 68 61 73 20 61 6c 73 6f ew page has also
174cc 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65 72 65 6e .** been referen
174cd 63 65 64 20 61 6e 64 20 74 68 65 20 63 61 6c 6c ced and the call
174ce 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 72 ing routine is r
174cf 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 esponsible for c
174d0 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 alling.** sqlite
174d1 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 3PagerUnref() on
174d2 20 74 68 65 20 6e 65 77 20 70 61 67 65 20 77 68 the new page wh
174d3 65 6e 20 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a en it is done..*
174d4 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 *.** SQLITE_OK i
174d5 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 s returned on su
174d6 63 63 65 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 ccess. Any othe
174d7 72 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 r return value i
174d8 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 ndicates.** an e
174d9 72 72 6f 72 2e 20 20 2a 70 70 50 61 67 65 20 61 rror. *ppPage a
174da 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e nd *pPgno are un
174db 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65 20 65 defined in the e
174dc 76 65 6e 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 vent of an error
174dd 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f ..** Do not invo
174de 6b 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 ke sqlite3PagerU
174df 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 nref() on *ppPag
174e0 65 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 e if an error is
174e1 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a returned..**.**
174e2 20 49 66 20 74 68 65 20 22 6e 65 61 72 62 79 22 If the "nearby"
174e3 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f parameter is no
174e4 74 20 30 2c 20 74 68 65 6e 20 61 20 28 66 65 65 t 0, then a (fee
174e5 62 6c 65 29 20 65 66 66 6f 72 74 20 69 73 20 6d ble) effort is m
174e6 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 ade to .** locat
174e7 65 20 61 20 70 61 67 65 20 63 6c 6f 73 65 20 74 e a page close t
174e8 6f 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 o the page numbe
174e9 72 20 22 6e 65 61 72 62 79 22 2e 20 20 54 68 69 r "nearby". Thi
174ea 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e s can be used in
174eb 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 an.** attempt t
174ec 6f 20 6b 65 65 70 20 72 65 6c 61 74 65 64 20 70 o keep related p
174ed 61 67 65 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 ages close to ea
174ee 63 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 ch other in the
174ef 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a database file,.*
174f0 2a 20 77 68 69 63 68 20 69 6e 20 74 75 72 6e 20 * which in turn
174f1 63 61 6e 20 6d 61 6b 65 20 64 61 74 61 62 61 73 can make databas
174f2 65 20 61 63 63 65 73 73 20 66 61 73 74 65 72 2e e access faster.
174f3 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 .**.** If the "e
174f4 78 61 63 74 22 20 70 61 72 61 6d 65 74 65 72 20 xact" parameter
174f5 69 73 20 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 is not 0, and th
174f6 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 e page-number ne
174f7 61 72 62 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 arby exists .**
174f8 61 6e 79 77 68 65 72 65 20 6f 6e 20 74 68 65 20 anywhere on the
174f9 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 free-list, then
174fa 69 74 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 it is guarenteed
174fb 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 2e to be returned.
174fc 20 54 68 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 This.** is only
174fd 20 75 73 65 64 20 62 79 20 61 75 74 6f 2d 76 61 used by auto-va
174fe 63 75 75 6d 20 64 61 74 61 62 61 73 65 73 20 77 cuum databases w
174ff 68 65 6e 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 hen allocating a
17500 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 new table..*/.s
17501 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 tatic int alloca
17502 74 65 42 74 72 65 65 50 61 67 65 28 0a 20 20 42 teBtreePage(. B
17503 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 tShared *pBt, .
17504 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 MemPage **ppPag
17505 65 2c 20 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e e, . Pgno *pPgn
17506 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 o, . Pgno nearb
17507 79 2c 0a 20 20 75 38 20 65 78 61 63 74 0a 29 7b y,. u8 exact.){
17508 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 . MemPage *pPag
17509 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 e1;. int rc;.
1750a 75 33 32 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 u32 n; /* Nu
1750b 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e mber of pages on
1750c 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f the freelist */
1750d 0a 20 20 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a . u32 k; /*
1750e 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 Number of leave
1750f 73 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f s on the trunk o
17510 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 2a f the freelist *
17511 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 /. MemPage *pTr
17512 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 unk = 0;. MemPa
17513 67 65 20 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d ge *pPrevTrunk =
17514 20 30 3b 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 0;. Pgno mxPag
17515 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 e; /* Total
17516 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 size of the data
17517 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 base file */..
17518 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
17519 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e mutex_held(pBt->
1751a 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 mutex) );. pPag
1751b 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 e1 = pBt->pPage1
1751c 3b 0a 20 20 6d 78 50 61 67 65 20 3d 20 70 61 67 ;. mxPage = pag
1751d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 erPagecount(pBt)
1751e 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 ;. n = get4byte
1751f 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b (&pPage1->aData[
17520 33 36 5d 29 3b 0a 20 20 74 65 73 74 63 61 73 65 36]);. testcase
17521 28 20 6e 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b ( n==mxPage-1 );
17522 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 . if( n>=mxPage
17523 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 ){. return S
17524 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b QLITE_CORRUPT_BK
17525 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e PT;. }. if( n>
17526 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 0 ){. /* Ther
17527 65 20 61 72 65 20 70 61 67 65 73 20 6f 6e 20 74 e are pages on t
17528 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 52 65 he freelist. Re
17529 75 73 65 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 use one of those
1752a 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 pages. */. P
1752b 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 gno iTrunk;.
1752c 75 38 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 u8 searchList =
1752d 30 3b 20 2f 2a 20 49 66 20 74 68 65 20 66 72 65 0; /* If the fre
1752e 65 2d 6c 69 73 74 20 6d 75 73 74 20 62 65 20 73 e-list must be s
1752f 65 61 72 63 68 65 64 20 66 6f 72 20 27 6e 65 61 earched for 'nea
17530 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 rby' */. .
17531 20 2f 2a 20 49 66 20 74 68 65 20 27 65 78 61 63 /* If the 'exac
17532 74 27 20 70 61 72 61 6d 65 74 65 72 20 77 61 73 t' parameter was
17533 20 74 72 75 65 20 61 6e 64 20 61 20 71 75 65 72 true and a quer
17534 79 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 y of the pointer
17535 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 -map. ** show
17536 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 s that the page
17537 27 6e 65 61 72 62 79 27 20 69 73 20 73 6f 6d 65 'nearby' is some
17538 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65 where on the fre
17539 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 e-list, then.
1753a 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 2d 6c ** the entire-l
1753b 69 73 74 20 77 69 6c 6c 20 62 65 20 73 65 61 72 ist will be sear
1753c 63 68 65 64 20 66 6f 72 20 74 68 61 74 20 70 61 ched for that pa
1753d 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 ge.. */.#ifnd
1753e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
1753f 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 UTOVACUUM. if
17540 28 20 65 78 61 63 74 20 26 26 20 6e 65 61 72 62 ( exact && nearb
17541 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 y<=mxPage ){.
17542 20 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 u8 eType;.
17543 20 20 20 61 73 73 65 72 74 28 20 6e 65 61 72 62 assert( nearb
17544 79 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 y>0 );. ass
17545 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 ert( pBt->autoVa
17546 63 75 75 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 cuum );. rc
17547 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 = ptrmapGet(pBt
17548 2c 20 6e 65 61 72 62 79 2c 20 26 65 54 79 70 65 , nearby, &eType
17549 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
1754a 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
1754b 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d if( eType=
1754c 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 =PTRMAP_FREEPAGE
1754d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 65 61 72 ){. sear
1754e 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 chList = 1;.
1754f 20 20 7d 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f }. *pPgno
17550 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d = nearby;. }
17551 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
17552 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 Decrement the fr
17553 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 ee-list count by
17554 20 31 2e 20 53 65 74 20 69 54 72 75 6e 6b 20 74 1. Set iTrunk t
17555 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 o the index of t
17556 68 65 0a 20 20 20 20 2a 2a 20 66 69 72 73 74 20 he. ** first
17557 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 free-list trunk
17558 70 61 67 65 2e 20 69 50 72 65 76 54 72 75 6e 6b page. iPrevTrunk
17559 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e is initially 1.
1755a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d . */. rc =
1755b 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 sqlite3PagerWri
1755c 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 te(pPage1->pDbPa
1755d 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 ge);. if( rc
1755e 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 ) return rc;.
1755f 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 put4byte(&pPage
17560 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 1->aData[36], n-
17561 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 1);.. /* The
17562 63 6f 64 65 20 77 69 74 68 69 6e 20 74 68 69 73 code within this
17563 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c loop is run onl
17564 79 20 6f 6e 63 65 20 69 66 20 74 68 65 20 27 73 y once if the 's
17565 65 61 72 63 68 4c 69 73 74 27 20 76 61 72 69 61 earchList' varia
17566 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f ble. ** is no
17567 74 20 74 72 75 65 2e 20 4f 74 68 65 72 77 69 73 t true. Otherwis
17568 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 e, it runs once
17569 66 6f 72 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 for each trunk-p
1756a 61 67 65 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a age on the. *
1756b 2a 20 66 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 * free-list unti
1756c 6c 20 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 l the page 'near
1756d 62 79 27 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a by' is located..
1756e 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a */. do {.
1756f 20 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b pPrevTrunk
17570 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 = pTrunk;.
17571 20 69 66 28 20 70 50 72 65 76 54 72 75 6e 6b 20 if( pPrevTrunk
17572 29 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e ){. iTrun
17573 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 k = get4byte(&pP
17574 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b revTrunk->aData[
17575 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0]);. }else
17576 7b 0a 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b {. iTrunk
17577 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 = get4byte(&pPa
17578 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b ge1->aData[32]);
17579 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 . }. t
1757a 65 73 74 63 61 73 65 28 20 69 54 72 75 6e 6b 3d estcase( iTrunk=
1757b 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 =mxPage );.
1757c 20 69 66 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 if( iTrunk>mxPa
1757d 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 ge ){. rc
1757e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
1757f 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 T_BKPT;. }e
17580 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 lse{. rc
17581 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 = btreeGetPage(p
17582 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 Bt, iTrunk, &pTr
17583 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d unk, 0);. }
17584 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b . if( rc ){
17585 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 . pTrunk
17586 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 = 0;. got
17587 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
17588 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 age;. }..
17589 20 20 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 k = get4byte
1758a 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
1758b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 4]);. if( k
1758c 3d 3d 30 20 26 26 20 21 73 65 61 72 63 68 4c 69 ==0 && !searchLi
1758d 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a st ){. /*
1758e 20 54 68 65 20 74 72 75 6e 6b 20 68 61 73 20 6e The trunk has n
1758f 6f 20 6c 65 61 76 65 73 20 61 6e 64 20 74 68 65 o leaves and the
17590 20 6c 69 73 74 20 69 73 20 6e 6f 74 20 62 65 69 list is not bei
17591 6e 67 20 73 65 61 72 63 68 65 64 2e 20 0a 20 20 ng searched. .
17592 20 20 20 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 ** So extr
17593 61 63 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 act the trunk pa
17594 67 65 20 69 74 73 65 6c 66 20 61 6e 64 20 75 73 ge itself and us
17595 65 20 69 74 20 61 73 20 74 68 65 20 6e 65 77 6c e it as the newl
17596 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c y . ** al
17597 6c 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a located page */.
17598 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
17599 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b pPrevTrunk==0 );
1759a 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1759b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
1759c 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 pTrunk->pDbPage)
1759d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1759e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f ){. go
1759f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f to end_allocate_
175a0 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a page;. }.
175a1 20 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d *pPgno =
175a2 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 iTrunk;.
175a3 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d memcpy(&pPage1-
175a4 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 >aData[32], &pTr
175a5 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 unk->aData[0], 4
175a6 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 );. *ppPa
175a7 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 ge = pTrunk;.
175a8 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b pTrunk = 0;
175a9 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 . TRACE((
175aa 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 "ALLOCATE: %d tr
175ab 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 unk - %d free pa
175ac 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 ges left\n", *pP
175ad 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 gno, n-1));.
175ae 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 }else if( k>(u
175af 33 32 29 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 32)(pBt->usableS
175b0 69 7a 65 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 ize/4 - 2) ){.
175b1 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f /* Value o
175b2 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61 f k is out of ra
175b3 6e 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63 nge. Database c
175b4 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 orruption */.
175b5 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
175b6 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
175b7 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f goto end_
175b8 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 allocate_page;.#
175b9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
175ba 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
175bb 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65 }else if( se
175bc 61 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72 archList && near
175bd 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 by==iTrunk ){.
175be 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 /* The lis
175bf 74 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63 t is being searc
175c0 68 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75 hed and this tru
175c1 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70 nk page is the p
175c2 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 age. ** t
175c3 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 o allocate, rega
175c4 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
175c5 72 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e r it has leaves.
175c6 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
175c7 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 assert( *pPg
175c8 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 no==iTrunk );.
175c9 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 *ppPage =
175ca 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 pTrunk;.
175cb 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a searchList = 0;.
175cc 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
175cd 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
175ce 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b Trunk->pDbPage);
175cf 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 . if( rc
175d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 ){. got
175d1 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 o end_allocate_p
175d2 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 age;. }.
175d3 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 if( k==0
175d4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
175d5 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a !pPrevTrunk ){.
175d6 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 memc
175d7 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 py(&pPage1->aDat
175d8 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e a[32], &pTrunk->
175d9 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 aData[0], 4);.
175da 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
175db 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 memcp
175dc 79 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 y(&pPrevTrunk->a
175dd 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b Data[0], &pTrunk
175de 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a ->aData[0], 4);.
175df 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
175e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
175e1 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e /* The trun
175e2 6b 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72 k page is requir
175e3 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 ed by the caller
175e4 20 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 but it contains
175e5 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 . ** p
175e6 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d ointers to free-
175e7 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65 list leaves. The
175e8 20 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f first leaf beco
175e9 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 mes a trunk.
175ea 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e ** page in
175eb 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20 this case..
175ec 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
175ed 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 MemPage *pNew
175ee 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 Trunk;.
175ef 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 Pgno iNewTrunk
175f0 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75 = get4byte(&pTru
175f1 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 nk->aData[8]);.
175f2 20 20 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 if( iNe
175f3 77 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b wTrunk>mxPage ){
175f4 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 . rc
175f5 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 = SQLITE_CORRUP
175f6 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 T_BKPT;.
175f7 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c goto end_all
175f8 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 ocate_page;.
175f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
175fa 20 20 74 65 73 74 63 61 73 65 28 20 69 4e 65 77 testcase( iNew
175fb 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b Trunk==mxPage );
175fc 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 . rc =
175fd 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 btreeGetPage(pBt
175fe 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e , iNewTrunk, &pN
175ff 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 ewTrunk, 0);.
17600 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
17601 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17602 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 goto end
17603 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a _allocate_page;.
17604 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
17605 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
17606 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 e3PagerWrite(pNe
17607 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 wTrunk->pDbPage)
17608 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
17609 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1760a 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c . rel
1760b 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72 75 easePage(pNewTru
1760c 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nk);.
1760d 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 goto end_alloca
1760e 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 te_page;.
1760f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 6d }. m
17610 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b emcpy(&pNewTrunk
17611 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 ->aData[0], &pTr
17612 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 unk->aData[0], 4
17613 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 );. put
17614 34 62 79 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 4byte(&pNewTrunk
17615 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 ->aData[4], k-1)
17616 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 ;. memc
17617 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 py(&pNewTrunk->a
17618 44 61 74 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b Data[8], &pTrunk
17619 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d ->aData[12], (k-
1761a 31 29 2a 34 29 3b 0a 20 20 20 20 20 20 20 20 20 1)*4);.
1761b 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 releasePage(pNe
1761c 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 wTrunk);.
1761d 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72 75 if( !pPrevTru
1761e 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 nk ){.
1761f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
17620 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 3PagerIswriteabl
17621 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 e(pPage1->pDbPag
17622 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 e) );.
17623 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 put4byte(&pPag
17624 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 e1->aData[32], i
17625 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 NewTrunk);.
17626 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
17627 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
17628 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
17629 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 PrevTrunk->pDbPa
1762a 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ge);.
1762b 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
1762c 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e goto en
1762d 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b d_allocate_page;
1762e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
1762f 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 put4b
17630 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d yte(&pPrevTrunk-
17631 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 >aData[0], iNewT
17632 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 runk);.
17633 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
17634 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b pTrunk = 0;
17635 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28 . TRACE((
17636 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 "ALLOCATE: %d tr
17637 75 6e 6b 20 2d 20 25 64 20 66 72 65 65 20 70 61 unk - %d free pa
17638 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 ges left\n", *pP
17639 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 gno, n-1));.#end
1763a 69 66 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 if. }else i
1763b 66 28 20 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 f( k>0 ){.
1763c 20 20 2f 2a 20 45 78 74 72 61 63 74 20 61 20 6c /* Extract a l
1763d 65 61 66 20 66 72 6f 6d 20 74 68 65 20 74 72 75 eaf from the tru
1763e 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 nk */. u3
1763f 32 20 63 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 2 closest;.
17640 20 20 20 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 Pgno iPage;.
17641 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 unsigned
17642 63 68 61 72 20 2a 61 44 61 74 61 20 3d 20 70 54 char *aData = pT
17643 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 runk->aData;.
17644 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
17645 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 3PagerWrite(pTru
17646 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 nk->pDbPage);.
17647 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a if( rc ){.
17648 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 goto e
17649 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 nd_allocate_page
1764a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1764b 20 20 20 20 69 66 28 20 6e 65 61 72 62 79 3e 30 if( nearby>0
1764c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33 ){. u3
1764d 32 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 2 i;. i
1764e 6e 74 20 64 69 73 74 3b 0a 20 20 20 20 20 20 20 nt dist;.
1764f 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a closest = 0;.
17650 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20 3d dist =
17651 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61 get4byte(&aData
17652 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 [8]) - nearby;.
17653 20 20 20 20 20 20 20 20 20 69 66 28 20 64 69 73 if( dis
17654 74 3c 30 20 29 20 64 69 73 74 20 3d 20 2d 64 69 t<0 ) dist = -di
17655 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f st;. fo
17656 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 r(i=1; i<k; i++)
17657 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e {. in
17658 74 20 64 32 20 3d 20 67 65 74 34 62 79 74 65 28 t d2 = get4byte(
17659 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d &aData[8+i*4]) -
1765a 20 6e 65 61 72 62 79 3b 0a 20 20 20 20 20 20 20 nearby;.
1765b 20 20 20 20 20 69 66 28 20 64 32 3c 30 20 29 20 if( d2<0 )
1765c 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 d2 = -d2;.
1765d 20 20 20 20 20 20 69 66 28 20 64 32 3c 64 69 73 if( d2<dis
1765e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
1765f 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a closest = i;.
17660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 69 di
17661 73 74 20 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 st = d2;.
17662 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
17663 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 }. }else
17664 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 {. clos
17665 65 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 est = 0;.
17666 20 7d 0a 0a 20 20 20 20 20 20 20 20 69 50 61 67 }.. iPag
17667 65 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44 e = get4byte(&aD
17668 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d ata[8+closest*4]
17669 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 );. testc
1766a 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 ase( iPage==mxPa
1766b 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 ge );. if
1766c 28 20 69 50 61 67 65 3e 6d 78 50 61 67 65 20 29 ( iPage>mxPage )
1766d 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
1766e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1766f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 BKPT;.
17670 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 goto end_allocat
17671 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 e_page;.
17672 7d 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 }. testca
17673 73 65 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 se( iPage==mxPag
17674 65 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 e );. if(
17675 20 21 73 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 !searchList ||
17676 69 50 61 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b iPage==nearby ){
17677 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e . int n
17678 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 oContent;.
17679 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 *pPgno = iPa
1767a 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 54 52 ge;. TR
1767b 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 ACE(("ALLOCATE:
1767c 25 64 20 77 61 73 20 6c 65 61 66 20 25 64 20 6f %d was leaf %d o
1767d 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 f %d on trunk %d
1767e 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
1767f 20 20 20 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 ": %d more fr
17680 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 ee pages\n",.
17681 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 *p
17682 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c Pgno, closest+1,
17683 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f k, pTrunk->pgno
17684 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 , n-1));.
17685 20 20 20 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b if( closest<k
17686 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 -1 ){.
17687 20 20 6d 65 6d 63 70 79 28 26 61 44 61 74 61 5b memcpy(&aData[
17688 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 8+closest*4], &a
17689 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b Data[4+k*4], 4);
1768a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1768b 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 put4byte(
1768c 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b &aData[4], k-1);
1768d 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
1768e 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
1768f 73 77 72 69 74 65 61 62 6c 65 28 70 54 72 75 6e swriteable(pTrun
17690 6b 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 k->pDbPage) );.
17691 20 20 20 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 noConte
17692 6e 74 20 3d 20 21 62 74 72 65 65 47 65 74 48 61 nt = !btreeGetHa
17693 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 sContent(pBt, *p
17694 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 Pgno);.
17695 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 rc = btreeGetPa
17696 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 ge(pBt, *pPgno,
17697 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e ppPage, noConten
17698 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 t);. if
17699 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1769a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ){. r
1769b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1769c 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29 2d Write((*ppPage)-
1769d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
1769e 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
1769f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
176a0 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 releas
176a1 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a ePage(*ppPage);.
176a2 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }.
176a3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
176a4 20 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d searchList =
176a5 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0;. }.
176a6 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 }. rele
176a7 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72 75 asePage(pPrevTru
176a8 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50 72 65 76 nk);. pPrev
176a9 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d Trunk = 0;. }
176aa 77 68 69 6c 65 28 20 73 65 61 72 63 68 4c 69 73 while( searchLis
176ab 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 t );. }else{.
176ac 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 6e /* There are n
176ad 6f 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 66 o pages on the f
176ae 72 65 65 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 reelist, so crea
176af 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 61 74 te a new page at
176b0 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 the. ** end
176b1 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 of the file */.
176b2 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 int nPage = p
176b3 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 agerPagecount(pB
176b4 74 29 3b 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d t);. *pPgno =
176b5 20 6e 50 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 nPage + 1;..
176b6 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e if( *pPgno==PEN
176b7 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
176b8 42 74 29 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 Bt) ){. (*p
176b9 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a Pgno)++;. }..
176ba 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
176bb 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 MIT_AUTOVACUUM.
176bc 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f if( pBt->auto
176bd 56 61 63 75 75 6d 20 26 26 20 50 54 52 4d 41 50 Vacuum && PTRMAP
176be 5f 49 53 50 41 47 45 28 70 42 74 2c 20 2a 70 50 _ISPAGE(pBt, *pP
176bf 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a gno) ){. /*
176c0 20 49 66 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 If *pPgno refer
176c1 73 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d s to a pointer-m
176c2 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 ap page, allocat
176c3 65 20 74 77 6f 20 6e 65 77 20 70 61 67 65 73 0a e two new pages.
176c4 20 20 20 20 20 20 2a 2a 20 61 74 20 74 68 65 20 ** at the
176c5 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 end of the file
176c6 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 instead of one.
176c7 54 68 65 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 The first alloca
176c8 74 65 64 20 70 61 67 65 0a 20 20 20 20 20 20 2a ted page. *
176c9 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 * becomes a new
176ca 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 pointer-map page
176cb 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 , the second is
176cc 75 73 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c used by the call
176cd 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 er.. */.
176ce 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 MemPage *pPg
176cf 3d 20 30 3b 0a 20 20 20 20 20 20 54 52 41 43 45 = 0;. TRACE
176d0 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 (("ALLOCATE: %d
176d1 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 from end of file
176d2 20 28 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 (pointer-map pa
176d3 67 65 29 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 ge)\n", *pPgno))
176d4 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
176d5 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f *pPgno!=PENDING_
176d6 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
176d7 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 ;. rc = btr
176d8 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a eeGetPage(pBt, *
176d9 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b pPgno, &pPg, 0);
176da 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 . if( rc==S
176db 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
176dc 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
176dd 50 61 67 65 72 57 72 69 74 65 28 70 50 67 2d 3e PagerWrite(pPg->
176de 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 pDbPage);.
176df 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 releasePage(pP
176e0 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 g);. }.
176e1 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 if( rc ) retur
176e2 6e 20 72 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 n rc;. (*pP
176e3 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 gno)++;. if
176e4 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e ( *pPgno==PENDIN
176e5 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 G_BYTE_PAGE(pBt)
176e6 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 ){ (*pPgno)++;
176e7 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a }. }.#endif..
176e8 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 assert( *pPg
176e9 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 no!=PENDING_BYTE
176ea 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20 20 _PAGE(pBt) );.
176eb 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 rc = btreeGetP
176ec 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c age(pBt, *pPgno,
176ed 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 ppPage, 0);.
176ee 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e if( rc ) return
176ef 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 rc;. rc = sq
176f0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
176f1 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 (*ppPage)->pDbPa
176f2 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
176f3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
176f4 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
176f5 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a *ppPage);. }.
176f6 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f TRACE(("ALLO
176f7 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e CATE: %d from en
176f8 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 d of file\n", *p
176f9 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 Pgno));. }.. a
176fa 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 ssert( *pPgno!=P
176fb 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
176fc 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c (pBt) );..end_al
176fd 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 locate_page:. r
176fe 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e eleasePage(pTrun
176ff 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 k);. releasePag
17700 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 e(pPrevTrunk);.
17701 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
17702 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 OK ){. if( sq
17703 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 lite3PagerPageRe
17704 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 fcount((*ppPage)
17705 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b 0a ->pDbPage)>1 ){.
17706 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 releasePag
17707 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 e(*ppPage);.
17708 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
17709 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
1770a 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67 65 }. (*ppPage
1770b 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 )->isInit = 0;.
1770c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 }else{. *ppP
1770d 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 age = 0;. }. r
1770e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1770f 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e ** This function
17710 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20 is used to add
17711 70 61 67 65 20 69 50 61 67 65 20 74 6f 20 74 68 page iPage to th
17712 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
17713 66 72 65 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 free-list. .** I
17714 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
17715 74 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f t the page is no
17716 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74 t already a part
17717 20 6f 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 of the free-lis
17718 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c t..**.** The val
17719 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 ue passed as the
1771a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 second argument
1771b 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f to this functio
1771c 6e 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a n is optional..*
1771d 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 * If the caller
1771e 68 61 70 70 65 6e 73 20 74 6f 20 68 61 76 65 20 happens to have
1771f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
17720 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 MemPage object
17721 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e .** correspondin
17722 67 20 74 6f 20 70 61 67 65 20 69 50 61 67 65 20 g to page iPage
17723 68 61 6e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 handy, it may pa
17724 73 73 20 69 74 20 61 73 20 74 68 65 20 73 65 63 ss it as the sec
17725 6f 6e 64 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f ond value. .** O
17726 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 therwise, it may
17727 20 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a pass NULL..**.*
17728 2a 20 49 66 20 61 20 70 6f 69 6e 74 65 72 20 74 * If a pointer t
17729 6f 20 61 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 o a MemPage obje
1772a 63 74 20 69 73 20 70 61 73 73 65 64 20 61 73 20 ct is passed as
1772b 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
1772c 65 6e 74 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 ent,.** its refe
1772d 72 65 6e 63 65 20 63 6f 75 6e 74 20 69 73 20 6e rence count is n
1772e 6f 74 20 61 6c 74 65 72 65 64 20 62 79 20 74 68 ot altered by th
1772f 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a is function..*/.
17730 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 50 static int freeP
17731 61 67 65 32 28 42 74 53 68 61 72 65 64 20 2a 70 age2(BtShared *p
17732 42 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 Bt, MemPage *pMe
17733 6d 50 61 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 mPage, Pgno iPag
17734 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 e){. MemPage *p
17735 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 Trunk = 0;
17736 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 /* Fre
17737 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 e-list trunk pag
17738 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 e */. Pgno iTru
17739 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 nk = 0;
1773a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
1773b 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 ge number of fre
1773c 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67 e-list trunk pag
1773d 65 20 2a 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 e */ . MemPage
1773e 2a 70 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 *pPage1 = pBt->p
1773f 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c Page1; /* L
17740 6f 63 61 6c 20 72 65 66 65 72 65 6e 63 65 20 74 ocal reference t
17741 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 o page 1 */. Me
17742 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 mPage *pPage;
17743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17744 20 20 2f 2a 20 50 61 67 65 20 62 65 69 6e 67 20 /* Page being
17745 66 72 65 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 freed. May be NU
17746 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b LL. */. int rc;
17747 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17748 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17749 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 Return Code */.
1774a 20 69 6e 74 20 6e 46 72 65 65 3b 20 20 20 20 20 int nFree;
1774b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1774c 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 /* Initial
1774d 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 number of pages
1774e 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a on free-list */.
1774f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17750 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 e3_mutex_held(pB
17751 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 t->mutex) );. a
17752 73 73 65 72 74 28 20 69 50 61 67 65 3e 31 20 29 ssert( iPage>1 )
17753 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 4d 65 ;. assert( !pMe
17754 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 mPage || pMemPag
17755 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 e->pgno==iPage )
17756 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d 50 61 67 ;.. if( pMemPag
17757 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d e ){. pPage =
17758 20 70 4d 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 pMemPage;. s
17759 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 70 qlite3PagerRef(p
1775a 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
1775b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 }else{. pPa
1775c 67 65 20 3d 20 62 74 72 65 65 50 61 67 65 4c 6f ge = btreePageLo
1775d 6f 6b 75 70 28 70 42 74 2c 20 69 50 61 67 65 29 okup(pBt, iPage)
1775e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 ;. }.. /* Incr
1775f 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 20 70 ement the free p
17760 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 age count on pPa
17761 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 ge1 */. rc = sq
17762 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
17763 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 pPage1->pDbPage)
17764 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 ;. if( rc ) got
17765 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a o freepage_out;.
17766 20 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 nFree = get4by
17767 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 te(&pPage1->aDat
17768 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 a[36]);. put4by
17769 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 te(&pPage1->aDat
1776a 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b a[36], nFree+1);
1776b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ..#ifdef SQLITE_
1776c 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 SECURE_DELETE.
1776d 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 /* If the SQLITE
1776e 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 63 _SECURE_DELETE c
1776f 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 ompile-time opti
17770 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 on is enabled, t
17771 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 hen. ** always
17772 66 75 6c 6c 79 20 6f 76 65 72 77 72 69 74 65 20 fully overwrite
17773 64 65 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 deleted informat
17774 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a ion with zeros..
17775 20 20 2a 2f 0a 20 20 69 66 28 20 28 21 70 50 61 */. if( (!pPa
17776 67 65 20 26 26 20 28 72 63 20 3d 20 62 74 72 65 ge && (rc = btre
17777 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50 eGetPage(pBt, iP
17778 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 age, &pPage, 0))
17779 29 0a 20 20 20 7c 7c 20 20 20 20 20 20 20 20 20 ). ||
1777a 20 20 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 (rc = sqlite3
1777b 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 PagerWrite(pPage
1777c 2d 3e 70 44 62 50 61 67 65 29 29 0a 20 20 29 7b ->pDbPage)). ){
1777d 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61 . goto freepa
1777e 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 ge_out;. }. me
1777f 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61 74 mset(pPage->aDat
17780 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 a, 0, pPage->pBt
17781 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e ->pageSize);.#en
17782 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 dif.. /* If the
17783 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 database suppor
17784 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 ts auto-vacuum,
17785 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 write an entry i
17786 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 n the pointer-ma
17787 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 p. ** to indica
17788 74 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 te that the page
17789 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 is free.. */.
1778a 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 if( ISAUTOVACUU
1778b 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 M ){. ptrmapP
1778c 75 74 28 70 42 74 2c 20 69 50 61 67 65 2c 20 50 ut(pBt, iPage, P
1778d 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 2c 20 TRMAP_FREEPAGE,
1778e 30 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 0, &rc);. if(
1778f 20 72 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 rc ) goto freep
17790 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 age_out;. }..
17791 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 /* Now manipulat
17792 65 20 74 68 65 20 61 63 74 75 61 6c 20 64 61 74 e the actual dat
17793 61 62 61 73 65 20 66 72 65 65 2d 6c 69 73 74 20 abase free-list
17794 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 72 65 structure. There
17795 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f are two. ** po
17796 73 73 69 62 69 6c 69 74 69 65 73 2e 20 49 66 20 ssibilities. If
17797 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69 73 the free-list is
17798 20 63 75 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 currently empty
17799 2c 20 6f 72 20 69 66 20 74 68 65 20 66 69 72 73 , or if the firs
1779a 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 t. ** trunk pag
1779b 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 e in the free-li
1779c 73 74 20 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e st is full, then
1779d 20 74 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20 this page will
1779e 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 become a. ** ne
1779f 77 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e w free-list trun
177a0 6b 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73 k page. Otherwis
177a1 65 2c 20 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d e, it will becom
177a2 65 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 0a e a leaf of the.
177a3 20 20 2a 2a 20 66 69 72 73 74 20 74 72 75 6e 6b ** first trunk
177a4 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 75 72 page in the cur
177a5 72 65 6e 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 rent free-list.
177a6 54 68 69 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 This block tests
177a7 20 69 66 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 if it. ** is p
177a8 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 ossible to add t
177a9 68 65 20 70 61 67 65 20 61 73 20 61 20 6e 65 77 he page as a new
177aa 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e free-list leaf.
177ab 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 . */. if( nFre
177ac 65 21 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 e!=0 ){. u32
177ad 6e 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 nLeaf;
177ae 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c /* Initial
177af 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 number of leaf
177b0 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 cells on trunk p
177b1 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 age */.. iTru
177b2 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 nk = get4byte(&p
177b3 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d Page1->aData[32]
177b4 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 );. rc = btre
177b5 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 54 eGetPage(pBt, iT
177b6 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 runk, &pTrunk, 0
177b7 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
177b8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
177b9 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
177ba 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 out;. }..
177bb 6e 4c 65 61 66 20 3d 20 67 65 74 34 62 79 74 65 nLeaf = get4byte
177bc 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b (&pTrunk->aData[
177bd 34 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 4]);. assert(
177be 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 pBt->usableSize
177bf 3e 33 32 20 29 3b 0a 20 20 20 20 69 66 28 20 6e >32 );. if( n
177c0 4c 65 61 66 20 3e 20 28 75 33 32 29 70 42 74 2d Leaf > (u32)pBt-
177c1 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 >usableSize/4 -
177c2 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 2 ){. rc =
177c3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
177c4 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 KPT;. goto
177c5 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 freepage_out;.
177c6 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 }. if( nLea
177c7 66 20 3c 20 28 75 33 32 29 70 42 74 2d 3e 75 73 f < (u32)pBt->us
177c8 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 ableSize/4 - 8 )
177c9 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 {. /* In th
177ca 69 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 is case there is
177cb 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 room on the tru
177cc 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e 73 65 72 nk page to inser
177cd 74 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20 t the page.
177ce 20 2a 2a 20 62 65 69 6e 67 20 66 72 65 65 64 20 ** being freed
177cf 61 73 20 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 as a new leaf..
177d0 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a **. **
177d1 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 Note that the t
177d2 72 75 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 runk page is not
177d3 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 really full unt
177d4 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 il it contains.
177d5 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 ** usableSi
177d6 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 ze/4 - 2 entries
177d7 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 , not usableSize
177d8 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 /4 - 8 entries a
177d9 73 20 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 s we have.
177da 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 ** coded. But d
177db 75 65 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 ue to a coding e
177dc 72 72 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 rror in versions
177dd 20 6f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 of SQLite prior
177de 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 to. ** 3.6
177df 2e 30 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 .0, databases wi
177e0 74 68 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e th freelist trun
177e1 6b 20 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 k pages holding
177e2 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 more than.
177e3 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 ** usableSize/4
177e4 2d 20 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c - 8 entries will
177e5 20 62 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 be reported as
177e6 63 6f 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 corrupt. In ord
177e7 65 72 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d er. ** to m
177e8 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 aintain backward
177e9 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 s compatibility
177ea 77 69 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 with older versi
177eb 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 ons of SQLite,.
177ec 20 20 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 ** we will
177ed 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72 65 73 74 continue to rest
177ee 72 69 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 rict the number
177ef 6f 66 20 65 6e 74 72 69 65 73 20 74 6f 20 75 73 of entries to us
177f0 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 ableSize/4 - 8.
177f1 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e ** for now.
177f2 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 At some point
177f3 69 6e 20 74 68 65 20 66 75 74 75 72 65 20 28 6f in the future (o
177f4 6e 63 65 20 65 76 65 72 79 6f 6e 65 20 68 61 73 nce everyone has
177f5 20 75 70 67 72 61 64 65 64 0a 20 20 20 20 20 20 upgraded.
177f6 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c ** to 3.6.0 or l
177f7 61 74 65 72 29 20 77 65 20 73 68 6f 75 6c 64 20 ater) we should
177f8 63 6f 6e 73 69 64 65 72 20 66 69 78 69 6e 67 20 consider fixing
177f9 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 the conditional
177fa 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 above. ** t
177fb 6f 20 72 65 61 64 20 22 75 73 61 62 6c 65 53 69 o read "usableSi
177fc 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65 61 64 20 ze/4-2" instead
177fd 6f 66 20 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 of "usableSize/4
177fe 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 -8".. */.
177ff 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
17800 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e PagerWrite(pTrun
17801 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 k->pDbPage);.
17802 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
17803 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 E_OK ){.
17804 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b put4byte(&pTrunk
17805 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 ->aData[4], nLea
17806 66 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 70 75 f+1);. pu
17807 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e t4byte(&pTrunk->
17808 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d aData[8+nLeaf*4]
17809 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 , iPage);.#ifnde
1780a 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f f SQLITE_SECURE_
1780b 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 69 DELETE. i
1780c 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 f( pPage ){.
1780d 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 sqlite3Pag
1780e 65 72 44 6f 6e 74 57 72 69 74 65 28 70 50 61 67 erDontWrite(pPag
1780f 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 e->pDbPage);.
17810 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }.#endif.
17811 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 rc = btree
17812 53 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 SetHasContent(pB
17813 74 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 t, iPage);.
17814 20 7d 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 }. TRACE((
17815 22 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6c "FREE-PAGE: %d l
17816 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 eaf on trunk pag
17817 65 20 25 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 e %d\n",pPage->p
17818 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f gno,pTrunk->pgno
17819 29 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 ));. goto f
1781a 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 reepage_out;.
1781b 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 }. }.. /* If
1781c 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f control flows to
1781d 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 this point, the
1781e 6e 20 69 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 n it was not pos
1781f 73 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 sible to add the
17820 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20 62 . ** the page b
17821 65 69 6e 67 20 66 72 65 65 64 20 61 73 20 61 20 eing freed as a
17822 6c 65 61 66 20 70 61 67 65 20 6f 66 20 74 68 65 leaf page of the
17823 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 first trunk in
17824 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 the free-list..
17825 20 2a 2a 20 50 6f 73 73 69 62 6c 79 20 62 65 63 ** Possibly bec
17826 61 75 73 65 20 74 68 65 20 66 72 65 65 2d 6c 69 ause the free-li
17827 73 74 20 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 st is empty, or
17828 70 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73 65 possibly because
17829 20 74 68 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 the . ** first
1782a 20 74 72 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 trunk in the fr
1782b 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e ee-list is full.
1782c 20 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 Either way, the
1782d 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65 page being free
1782e 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f d. ** will beco
1782f 6d 65 20 74 68 65 20 6e 65 77 20 66 69 72 73 74 me the new first
17830 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 trunk page in t
17831 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 he free-list..
17832 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d */. if( pPage==
17833 30 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 0 && SQLITE_OK!=
17834 28 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 (rc = btreeGetPa
17835 67 65 28 70 42 74 2c 20 69 50 61 67 65 2c 20 26 ge(pBt, iPage, &
17836 70 50 61 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 pPage, 0)) ){.
17837 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f goto freepage_
17838 6f 75 74 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 out;. }. rc =
17839 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 sqlite3PagerWrit
1783a 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 e(pPage->pDbPage
1783b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c );. if( rc!=SQL
1783c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f ITE_OK ){. go
1783d 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b to freepage_out;
1783e 0a 20 20 7d 0a 20 20 70 75 74 34 62 79 74 65 28 . }. put4byte(
1783f 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 pPage->aData, iT
17840 72 75 6e 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 runk);. put4byt
17841 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b e(&pPage->aData[
17842 34 5d 2c 20 30 29 3b 0a 20 20 70 75 74 34 62 79 4], 0);. put4by
17843 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 te(&pPage1->aDat
17844 61 5b 33 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 a[32], iPage);.
17845 20 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 TRACE(("FREE-PA
17846 47 45 3a 20 25 64 20 6e 65 77 20 74 72 75 6e 6b GE: %d new trunk
17847 20 70 61 67 65 20 72 65 70 6c 61 63 69 6e 67 20 page replacing
17848 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 %d\n", pPage->pg
17849 6e 6f 2c 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 no, iTrunk));..f
1784a 72 65 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 reepage_out:. i
1784b 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 f( pPage ){.
1784c 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 pPage->isInit =
1784d 30 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 0;. }. release
1784e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 Page(pPage);. r
1784f 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75 6e eleasePage(pTrun
17850 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b k);. return rc;
17851 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 .}.static void f
17852 72 65 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 reePage(MemPage
17853 2a 70 50 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 *pPage, int *pRC
17854 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 3d ){. if( (*pRC)=
17855 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17856 20 20 2a 70 52 43 20 3d 20 66 72 65 65 50 61 67 *pRC = freePag
17857 65 32 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 e2(pPage->pBt, p
17858 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e Page, pPage->pgn
17859 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a o);. }.}../*.**
1785a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66 6c Free any overfl
1785b 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 ow pages associa
1785c 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76 ted with the giv
1785d 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 en Cell..*/.stat
1785e 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c ic int clearCell
1785f 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c (MemPage *pPage,
17860 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
17861 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 pCell){. BtShar
17862 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d ed *pBt = pPage-
17863 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f >pBt;. CellInfo
17864 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 info;. Pgno ov
17865 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 flPgno;. int rc
17866 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 ;. int nOvfl;.
17867 20 75 31 36 20 6f 76 66 6c 50 61 67 65 53 69 7a u16 ovflPageSiz
17868 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 e;.. assert( sq
17869 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
1786a 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 (pPage->pBt->mut
1786b 65 78 29 20 29 3b 0a 20 20 62 74 72 65 65 50 61 ex) );. btreePa
1786c 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 rseCellPtr(pPage
1786d 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b , pCell, &info);
1786e 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 . if( info.iOve
1786f 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 rflow==0 ){.
17870 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17871 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f ; /* No overflo
17872 77 20 70 61 67 65 73 2e 20 52 65 74 75 72 6e 20 w pages. Return
17873 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e without doing an
17874 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 ything */. }.
17875 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 ovflPgno = get4b
17876 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e yte(&pCell[info.
17877 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 iOverflow]);. a
17878 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61 62 ssert( pBt->usab
17879 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 leSize > 4 );.
1787a 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 ovflPageSize = p
1787b 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d Bt->usableSize -
1787c 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 4;. nOvfl = (i
1787d 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 nfo.nPayload - i
1787e 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 nfo.nLocal + ovf
1787f 6c 50 61 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f lPageSize - 1)/o
17880 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 vflPageSize;. a
17881 73 73 65 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d ssert( ovflPgno=
17882 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b =0 || nOvfl>0 );
17883 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d . while( nOvfl-
17884 2d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e - ){. Pgno iN
17885 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d ext = 0;. Mem
17886 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b Page *pOvfl = 0;
17887 0a 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e . if( ovflPgn
17888 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e o<2 || ovflPgno>
17889 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 pagerPagecount(p
1788a 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 Bt) ){. /*
1788b 30 20 69 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 0 is not a legal
1788c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 page number and
1788d 20 70 61 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 page 1 cannot b
1788e 65 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f e an . ** o
1788f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 verflow page. Th
17890 65 72 65 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 erefore if ovflP
17891 67 6e 6f 3c 32 20 6f 72 20 70 61 73 74 20 74 68 gno<2 or past th
17892 65 20 65 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 e end of the .
17893 20 20 20 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 ** file the
17894 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 database must be
17895 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 corrupt. */.
17896 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
17897 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
17898 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 }. if( nOv
17899 66 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d fl ){. rc =
1789a 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 getOverflowPage
1789b 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 (pBt, ovflPgno,
1789c 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b &pOvfl, &iNext);
1789d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 . if( rc )
1789e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d return rc;. }
1789f 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 . rc = freePa
178a0 67 65 32 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 ge2(pBt, pOvfl,
178a1 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 ovflPgno);. i
178a2 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 f( pOvfl ){.
178a3 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e sqlite3PagerUn
178a4 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 ref(pOvfl->pDbPa
178a5 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ge);. }. i
178a6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
178a7 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 c;. ovflPgno
178a8 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 = iNext;. }. r
178a9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
178aa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 .}../*.** Create
178ab 20 74 68 65 20 62 79 74 65 20 73 65 71 75 65 6e the byte sequen
178ac 63 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 ce used to repre
178ad 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 sent a cell on p
178ae 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 age pPage.** and
178af 20 77 72 69 74 65 20 74 68 61 74 20 62 79 74 65 write that byte
178b0 20 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 sequence into p
178b1 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f Cell[]. Overflo
178b2 77 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 w pages are.** a
178b3 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69 6c llocated and fil
178b4 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73 73 led in as necess
178b5 61 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e ary. The callin
178b6 67 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 g procedure.** i
178b7 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
178b8 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73 75 r making sure su
178b9 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 68 fficient space h
178ba 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 as been allocate
178bb 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d d.** for pCell[]
178bc 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 ..**.** Note tha
178bd 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 t pCell does not
178be 20 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64 20 necessary need
178bf 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
178c0 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 pPage->aData.**
178c1 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 area. pCell mig
178c2 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 ht point to some
178c3 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 temporary stora
178c4 67 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77 69 ge. The cell wi
178c5 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 ll.** be constru
178c6 63 74 65 64 20 69 6e 20 74 68 69 73 20 74 65 6d cted in this tem
178c7 70 6f 72 61 72 79 20 61 72 65 61 20 74 68 65 6e porary area then
178c8 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50 61 copied into pPa
178c9 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 ge->aData.** lat
178ca 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e er..*/.static in
178cb 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 t fillInCell(.
178cc 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 MemPage *pPage,
178cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
178ce 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20 * The page that
178cf 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65 6c contains the cel
178d0 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 l */. unsigned
178d1 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 char *pCell,
178d2 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 /* Complet
178d3 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63 65 e text of the ce
178d4 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f ll */. const vo
178d5 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b id *pKey, i64 nK
178d6 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 ey, /* The ke
178d7 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 y */. const voi
178d8 64 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 d *pData,int nDa
178d9 74 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61 74 ta, /* The dat
178da 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f a */. int nZero
178db 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
178dc 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 7a /* Extra z
178dd 65 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70 70 ero bytes to app
178de 65 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a end to pData */.
178df 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 int *pnSize
178e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
178e1 20 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20 73 /* Write cell s
178e2 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 ize here */.){.
178e3 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 int nPayload;.
178e4 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b const u8 *pSrc;
178e5 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 . int nSrc, n,
178e6 72 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65 4c rc;. int spaceL
178e7 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a eft;. MemPage *
178e8 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d pOvfl = 0;. Mem
178e9 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 Page *pToRelease
178ea 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 = 0;. unsigned
178eb 20 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 char *pPrior;.
178ec 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
178ed 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 pPayload;. BtSh
178ee 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 ared *pBt = pPag
178ef 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 e->pBt;. Pgno p
178f0 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 gnoOvfl = 0;. i
178f1 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 nt nHeader;. Ce
178f2 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 llInfo info;..
178f3 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
178f4 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 mutex_held(pPage
178f5 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b ->pBt->mutex) );
178f6 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20 69 73 20 .. /* pPage is
178f7 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 not necessarily
178f8 77 72 69 74 65 61 62 6c 65 20 73 69 6e 63 65 20 writeable since
178f9 70 43 65 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 pCell might be a
178fa 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 uxiliary. ** bu
178fb 66 66 65 72 20 73 70 61 63 65 20 74 68 61 74 20 ffer space that
178fc 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d is separate from
178fd 20 74 68 65 20 70 50 61 67 65 20 62 75 66 66 65 the pPage buffe
178fe 72 20 61 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 r area */. asse
178ff 72 74 28 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d rt( pCell<pPage-
17900 3e 61 44 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e >aData || pCell>
17901 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 =&pPage->aData[p
17902 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 Bt->pageSize].
17903 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c || sql
17904 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 ite3PagerIswrite
17905 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 able(pPage->pDbP
17906 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 age) );.. /* Fi
17907 6c 6c 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 ll in the header
17908 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d . */. nHeader =
17909 20 30 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 0;. if( !pPage
1790a 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 ->leaf ){. nH
1790b 65 61 64 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a eader += 4;. }.
1790c 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 if( pPage->has
1790d 44 61 74 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 Data ){. nHea
1790e 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 der += putVarint
1790f 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d (&pCell[nHeader]
17910 2c 20 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a , nData+nZero);.
17911 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 }else{. nDa
17912 74 61 20 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a ta = nZero = 0;.
17913 20 20 7d 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d }. nHeader +=
17914 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c putVarint(&pCel
17915 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 l[nHeader], *(u6
17916 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 4*)&nKey);. btr
17917 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 eeParseCellPtr(p
17918 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e Page, pCell, &in
17919 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 fo);. assert( i
1791a 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 nfo.nHeader==nHe
1791b 61 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 ader );. assert
1791c 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 ( info.nKey==nKe
1791d 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 y );. assert( i
1791e 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 nfo.nData==(u32)
1791f 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b (nData+nZero) );
17920 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e . . /* Fill in
17921 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a the payload */.
17922 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 nPayload = nDa
17923 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 ta + nZero;. if
17924 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 ( pPage->intKey
17925 29 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 44 ){. pSrc = pD
17926 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 ata;. nSrc =
17927 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 nData;. nData
17928 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a = 0;. }else{ .
17929 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 6e 4b if( NEVER(nK
1792a 65 79 3e 30 78 37 66 66 66 66 66 66 66 20 7c 7c ey>0x7fffffff ||
1792b 20 70 4b 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 pKey==0) ){.
1792c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1792d 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1792e 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 }. nPayloa
1792f 64 20 2b 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a d += (int)nKey;.
17930 20 20 20 20 70 53 72 63 20 3d 20 70 4b 65 79 3b pSrc = pKey;
17931 0a 20 20 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 . nSrc = (int
17932 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e )nKey;. }. *pn
17933 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a Size = info.nSiz
17934 65 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20 3d e;. spaceLeft =
17935 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 info.nLocal;.
17936 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c pPayload = &pCel
17937 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 l[nHeader];. pP
17938 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e rior = &pCell[in
17939 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a fo.iOverflow];..
1793a 20 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 while( nPayloa
1793b 64 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 d>0 ){. if( s
1793c 70 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 paceLeft==0 ){.#
1793d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1793e 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
1793f 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 Pgno pgnoPtr
17940 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 map = pgnoOvfl;
17941 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 /* Overflow page
17942 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
17943 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 ry page */.
17944 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
17945 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 cuum ){.
17946 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 67 do{. pg
17947 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 noOvfl++;.
17948 20 20 7d 20 77 68 69 6c 65 28 20 0a 20 20 20 20 } while( .
17949 20 20 20 20 20 20 50 54 52 4d 41 50 5f 49 53 50 PTRMAP_ISP
1794a 41 47 45 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 AGE(pBt, pgnoOvf
1794b 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d l) || pgnoOvfl==
1794c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 PENDING_BYTE_PAG
1794d 45 28 70 42 74 29 20 0a 20 20 20 20 20 20 20 20 E(pBt) .
1794e 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
1794f 66 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c f. rc = all
17950 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
17951 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e Bt, &pOvfl, &pgn
17952 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c oOvfl, pgnoOvfl,
17953 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 0);.#ifndef SQL
17954 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
17955 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 UUM. /* If
17956 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 the database sup
17957 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 ports auto-vacuu
17958 6d 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e m, and the secon
17959 64 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a d or subsequent.
1795a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f ** overflo
1795b 77 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 w page is being
1795c 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 allocated, add a
1795d 6e 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 n entry to the p
1795e 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 ointer-map.
1795f 20 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 ** for that pag
17960 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a e now. . **
17961 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 . ** If thi
17962 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f s is the first o
17963 76 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 verflow page, th
17964 65 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 en write a parti
17965 61 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 al entry .
17966 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 ** to the pointe
17967 72 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 r-map. If we wri
17968 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 te nothing to th
17969 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 is pointer-map s
1796a 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 lot,. ** th
1796b 65 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 en the optimisti
1796c 63 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e c overflow chain
1796d 20 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 processing in c
1796e 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 learCell().
1796f 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 ** may misinter
17970 70 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 pret the uniniti
17971 61 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e alised values an
17972 64 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 d delete the.
17973 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 ** wrong page
17974 73 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 s from the datab
17975 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ase.. */.
17976 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 if( pBt->aut
17977 6f 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 oVacuum && rc==S
17978 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
17979 20 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 u8 eType = (
1797a 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 pgnoPtrmap?PTRMA
1797b 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d P_OVERFLOW2:PTRM
1797c 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 AP_OVERFLOW1);.
1797d 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 ptrmapPut
1797e 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 (pBt, pgnoOvfl,
1797f 65 54 79 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 eType, pgnoPtrma
17980 70 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20 p, &rc);.
17981 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 if( rc ){.
17982 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
17983 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 (pOvfl);.
17984 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
17985 66 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 f. if( rc )
17986 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 {. releas
17987 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 ePage(pToRelease
17988 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
17989 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 n rc;. }..
1798a 20 20 20 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 /* If pToRe
1798b 6c 65 61 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 lease is not zer
1798c 6f 20 74 68 61 6e 20 70 50 72 69 6f 72 20 70 6f o than pPrior po
1798d 69 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 ints into the da
1798e 74 61 20 61 72 65 61 0a 20 20 20 20 20 20 2a 2a ta area. **
1798f 20 6f 66 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 of pToRelease.
17990 20 4d 61 6b 65 20 73 75 72 65 20 70 54 6f 52 65 Make sure pToRe
17991 6c 65 61 73 65 20 69 73 20 73 74 69 6c 6c 20 77 lease is still w
17992 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 riteable. */.
17993 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52 65 assert( pToRe
17994 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 lease==0 || sqli
17995 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
17996 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e ble(pToRelease->
17997 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 pDbPage) );..
17998 20 20 20 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 /* If pPrior
17999 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 is part of the d
1799a 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61 67 ata area of pPag
1799b 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 e, then make sur
1799c 65 20 70 50 61 67 65 0a 20 20 20 20 20 20 2a 2a e pPage. **
1799d 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 is still writea
1799e 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 ble */. ass
1799f 65 72 74 28 20 70 50 72 69 6f 72 3c 70 50 61 67 ert( pPrior<pPag
179a0 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 72 69 e->aData || pPri
179a1 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 or>=&pPage->aDat
179a2 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d a[pBt->pageSize]
179a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 . ||
179a4 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
179a5 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 iteable(pPage->p
179a6 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 DbPage) );..
179a7 20 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f put4byte(pPrio
179a8 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 r, pgnoOvfl);.
179a9 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
179aa 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 pToRelease);.
179ab 20 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 pToRelease =
179ac 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 pOvfl;. pPr
179ad 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 ior = pOvfl->aDa
179ae 74 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 ta;. put4by
179af 74 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 te(pPrior, 0);.
179b0 20 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 pPayload =
179b1 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d &pOvfl->aData[4]
179b2 3b 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65 66 ;. spaceLef
179b3 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 t = pBt->usableS
179b4 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 ize - 4;. }.
179b5 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b n = nPayload;
179b6 0a 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63 65 . if( n>space
179b7 4c 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63 65 Left ) n = space
179b8 4c 65 66 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 Left;.. /* If
179b9 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 6e pToRelease is n
179ba 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 ot zero than pPa
179bb 79 6c 6f 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 yload points int
179bc 6f 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a o the data area.
179bd 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c ** of pToRel
179be 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 ease. Make sure
179bf 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20 73 pToRelease is s
179c0 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 till writeable.
179c1 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 */. assert( p
179c2 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 ToRelease==0 ||
179c3 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
179c4 69 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 iteable(pToRelea
179c5 73 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a se->pDbPage) );.
179c6 0a 20 20 20 20 2f 2a 20 49 66 20 70 50 61 79 6c . /* If pPayl
179c7 6f 61 64 20 69 73 20 70 61 72 74 20 6f 66 20 74 oad is part of t
179c8 68 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 he data area of
179c9 70 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 pPage, then make
179ca 20 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 sure pPage.
179cb 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74 ** is still writ
179cc 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 eable */. ass
179cd 65 72 74 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 ert( pPayload<pP
179ce 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 age->aData || pP
179cf 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e ayload>=&pPage->
179d0 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 aData[pBt->pageS
179d1 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 ize].
179d2 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 || sqlite3Pager
179d3 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
179d4 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a e->pDbPage) );..
179d5 20 20 20 20 69 66 28 20 6e 53 72 63 3e 30 20 29 if( nSrc>0 )
179d6 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 {. if( n>nS
179d7 72 63 20 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 rc ) n = nSrc;.
179d8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 assert( pSr
179d9 63 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 c );. memcp
179da 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 y(pPayload, pSrc
179db 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b , n);. }else{
179dc 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 . memset(pP
179dd 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 ayload, 0, n);.
179de 20 20 20 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 }. nPayloa
179df 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 d -= n;. pPay
179e0 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 load += n;. p
179e1 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 Src += n;. nS
179e2 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 rc -= n;. spa
179e3 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 ceLeft -= n;.
179e4 20 69 66 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a if( nSrc==0 ){.
179e5 20 20 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 nSrc = nDa
179e6 74 61 3b 0a 20 20 20 20 20 20 70 53 72 63 20 3d ta;. pSrc =
179e7 20 70 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 pData;. }.
179e8 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 }. releasePage(
179e9 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 pToRelease);. r
179ea 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
179eb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 .}../*.** Remove
179ec 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 the i-th cell f
179ed 72 6f 6d 20 70 50 61 67 65 2e 20 20 54 68 69 73 rom pPage. This
179ee 20 72 6f 75 74 69 6e 65 20 65 66 66 65 63 74 73 routine effects
179ef 20 70 50 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 pPage only..**
179f0 54 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 The cell content
179f1 20 69 73 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 is not freed or
179f2 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 deallocated. I
179f3 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
179f4 74 0a 2a 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f t.** the cell co
179f5 6e 74 65 6e 74 20 68 61 73 20 62 65 65 6e 20 63 ntent has been c
179f6 6f 70 69 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 opied someplace
179f7 65 6c 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 else. This rout
179f8 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f ine just.** remo
179f9 76 65 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 ves the referenc
179fa 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 e to the cell fr
179fb 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 om pPage..**.**
179fc 22 73 7a 22 20 6d 75 73 74 20 62 65 20 74 68 65 "sz" must be the
179fd 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
179fe 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f in the cell..*/
179ff 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 72 6f .static void dro
17a00 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 pCell(MemPage *p
17a01 50 61 67 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 Page, int idx, i
17a02 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 nt sz, int *pRC)
17a03 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 {. int i;
17a04 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
17a05 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b ter */. int pc;
17a06 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 /* Offs
17a07 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 et to cell conte
17a08 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 nt of cell being
17a09 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 deleted */. u8
17a0a 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a *data; /*
17a0b 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f pPage->aData */
17a0c 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 . u8 *ptr;
17a0d 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f /* Used to mo
17a0e 76 65 20 62 79 74 65 73 20 61 72 6f 75 6e 64 20 ve bytes around
17a0f 77 69 74 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f within data[] */
17a10 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 . int rc;
17a11 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e /* The return
17a12 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 code */. int h
17a13 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 dr; /* Be
17a14 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 ginning of the h
17a15 65 61 64 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 eader. 0 most p
17a16 61 67 65 73 2e 20 20 31 30 30 20 70 61 67 65 20 ages. 100 page
17a17 31 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 1 */.. if( *pRC
17a18 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 ) return;.. as
17a19 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 sert( idx>=0 &&
17a1a 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c idx<pPage->nCell
17a1b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a );. assert( sz
17a1c 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 ==cellSize(pPage
17a1d 2c 20 69 64 78 29 20 29 3b 0a 20 20 61 73 73 65 , idx) );. asse
17a1e 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 rt( sqlite3Pager
17a1f 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 Iswriteable(pPag
17a20 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 e->pDbPage) );.
17a21 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17a22 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
17a23 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
17a24 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 ;. data = pPage
17a25 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74 72 20 3d ->aData;. ptr =
17a26 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 &data[pPage->ce
17a27 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 64 78 llOffset + 2*idx
17a28 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74 32 62 79 ];. pc = get2by
17a29 74 65 28 70 74 72 29 3b 0a 20 20 68 64 72 20 3d te(ptr);. hdr =
17a2a 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
17a2b 74 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 t;. testcase( p
17a2c 63 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 c==get2byte(&dat
17a2d 61 5b 68 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 a[hdr+5]) );. t
17a2e 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d estcase( pc+sz==
17a2f 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 pPage->pBt->usab
17a30 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 leSize );. if(
17a31 70 63 20 3c 20 67 65 74 32 62 79 74 65 28 26 64 pc < get2byte(&d
17a32 61 74 61 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 ata[hdr+5]) || p
17a33 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 c+sz > pPage->pB
17a34 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b t->usableSize ){
17a35 0a 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 . *pRC = SQLI
17a36 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
17a37 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
17a38 0a 20 20 72 63 20 3d 20 66 72 65 65 53 70 61 63 . rc = freeSpac
17a39 65 28 70 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 e(pPage, pc, sz)
17a3a 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 ;. if( rc ){.
17a3b 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 *pRC = rc;.
17a3c 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 return;. }. f
17a3d 6f 72 28 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 or(i=idx+1; i<pP
17a3e 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c age->nCell; i++,
17a3f 20 70 74 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 ptr+=2){. pt
17a40 72 5b 30 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 r[0] = ptr[2];.
17a41 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b ptr[1] = ptr[
17a42 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3];. }. pPage-
17a43 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 >nCell--;. put2
17a44 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
17a45 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 ], pPage->nCell)
17a46 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 ;. pPage->nFree
17a47 20 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 += 2;.}../*.**
17a48 49 6e 73 65 72 74 20 61 20 6e 65 77 20 63 65 6c Insert a new cel
17a49 6c 20 6f 6e 20 70 50 61 67 65 20 61 74 20 63 65 l on pPage at ce
17a4a 6c 6c 20 69 6e 64 65 78 20 22 69 22 2e 20 20 70 ll index "i". p
17a4b 43 65 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 Cell points to t
17a4c 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 he.** content of
17a4d 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a the cell..**.**
17a4e 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e If the cell con
17a4f 74 65 6e 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e tent will fit on
17a50 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 the page, then
17a51 70 75 74 20 69 74 20 74 68 65 72 65 2e 20 20 49 put it there. I
17a52 66 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 f it.** will not
17a53 20 66 69 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 fit, then make
17a54 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 65 a copy of the ce
17a55 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 ll content into
17a56 70 54 65 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d pTemp if.** pTem
17a57 70 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 p is not null.
17a58 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20 70 54 Regardless of pT
17a59 65 6d 70 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 emp, allocate a
17a5a 6e 65 77 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 new entry.** in
17a5b 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 pPage->aOvfl[] a
17a5c 6e 64 20 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 nd make it point
17a5d 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e to the cell con
17a5e 74 65 6e 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 tent (either.**
17a5f 69 6e 20 70 54 65 6d 70 20 6f 72 20 74 68 65 20 in pTemp or the
17a60 6f 72 69 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 original pCell)
17a61 61 6e 64 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 and also record
17a62 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 its index. .** A
17a63 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 llocating a new
17a64 65 6e 74 72 79 20 69 6e 20 70 50 61 67 65 2d 3e entry in pPage->
17a65 61 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 aCell[] implies
17a66 74 68 61 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e that .** pPage->
17a67 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 nOverflow is inc
17a68 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 remented..**.**
17a69 49 66 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d If nSkip is non-
17a6a 7a 65 72 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f zero, then do no
17a6b 74 20 63 6f 70 79 20 74 68 65 20 66 69 72 73 74 t copy the first
17a6c 20 6e 53 6b 69 70 20 62 79 74 65 73 20 6f 66 20 nSkip bytes of
17a6d 74 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 the.** cell. The
17a6e 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 caller will ove
17a6f 72 77 72 69 74 65 20 74 68 65 6d 20 61 66 74 65 rwrite them afte
17a70 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 r this function
17a71 72 65 74 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e returns. If.** n
17a72 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f Skip is non-zero
17a73 2c 20 74 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 , then pCell may
17a74 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e not point to an
17a75 20 69 6e 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 invalid memory
17a76 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 location .** (bu
17a77 74 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 t pCell+nSkip is
17a78 20 61 6c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a always valid)..
17a79 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 */.static void i
17a7a 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d nsertCell(. Mem
17a7b 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f Page *pPage, /
17a7c 2a 20 50 61 67 65 20 69 6e 74 6f 20 77 68 69 63 * Page into whic
17a7d 68 20 77 65 20 61 72 65 20 63 6f 70 79 69 6e 67 h we are copying
17a7e 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 */. int i,
17a7f 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 /* New c
17a80 65 6c 6c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 ell becomes the
17a81 69 2d 74 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 i-th cell of the
17a82 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 page */. u8 *p
17a83 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 Cell, /*
17a84 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e Content of the n
17a85 65 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 ew cell */. int
17a86 20 73 7a 2c 20 20 20 20 20 20 20 20 20 20 20 2f sz, /
17a87 2a 20 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 * Bytes of conte
17a88 6e 74 20 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 nt in pCell */.
17a89 20 75 38 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 u8 *pTemp,
17a8a 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 /* Temp stora
17a8b 67 65 20 73 70 61 63 65 20 66 6f 72 20 70 43 65 ge space for pCe
17a8c 6c 6c 2c 20 69 66 20 6e 65 65 64 65 64 20 2a 2f ll, if needed */
17a8d 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 . Pgno iChild,
17a8e 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a /* If non-z
17a8f 65 72 6f 2c 20 72 65 70 6c 61 63 65 20 66 69 72 ero, replace fir
17a90 73 74 20 34 20 62 79 74 65 73 20 77 69 74 68 20 st 4 bytes with
17a91 74 68 69 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 this value */.
17a92 69 6e 74 20 2a 70 52 43 20 20 20 20 20 20 20 20 int *pRC
17a93 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 /* Read and wr
17a94 69 74 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 ite return code
17a95 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a from here */.){.
17a96 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 int idx;
17a97 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 /* Where to
17a98 77 72 69 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 write new cell c
17a99 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d ontent in data[]
17a9a 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 */. int j;
17a9b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
17a9c 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 counter */. int
17a9d 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f end; /
17a9e 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73 * First byte pas
17a9f 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 t the last cell
17aa0 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b pointer in data[
17aa1 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 ] */. int ins;
17aa2 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 /* Inde
17aa3 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 x in data[] wher
17aa4 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 e new cell point
17aa5 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a er is inserted *
17aa6 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 /. int cellOffs
17aa7 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 et; /* Address
17aa8 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 of first cell p
17aa9 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d ointer in data[]
17aaa 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 */. u8 *data;
17aab 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
17aac 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68 ontent of the wh
17aad 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 ole page */. u8
17aae 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20 *ptr;
17aaf 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 /* Used for movi
17ab0 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 ng information a
17ab1 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 round in data[]
17ab2 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 */.. int nSkip
17ab3 3d 20 28 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 = (iChild ? 4 :
17ab4 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 0);.. if( *pRC
17ab5 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 ) return;.. ass
17ab6 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d ert( i>=0 && i<=
17ab7 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 pPage->nCell+pPa
17ab8 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b ge->nOverflow );
17ab9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 . assert( pPage
17aba 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c ->nCell<=MX_CELL
17abb 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20 (pPage->pBt) &&
17abc 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 MX_CELL(pPage->p
17abd 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 Bt)<=5460 );. a
17abe 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f ssert( pPage->nO
17abf 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 verflow<=ArraySi
17ac0 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 ze(pPage->aOvfl)
17ac1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a );. assert( sz
17ac2 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 ==cellSizePtr(pP
17ac3 61 67 65 2c 20 70 43 65 6c 6c 29 20 29 3b 0a 20 age, pCell) );.
17ac4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
17ac5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 _mutex_held(pPag
17ac6 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 e->pBt->mutex) )
17ac7 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e ;. if( pPage->n
17ac8 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 Overflow || sz+2
17ac9 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b >pPage->nFree ){
17aca 0a 20 20 20 20 69 66 28 20 70 54 65 6d 70 20 29 . if( pTemp )
17acb 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 {. memcpy(p
17acc 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70 43 65 6c Temp+nSkip, pCel
17acd 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 l+nSkip, sz-nSki
17ace 70 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 p);. pCell
17acf 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 = pTemp;. }.
17ad0 20 20 20 69 66 28 20 69 43 68 69 6c 64 20 29 7b if( iChild ){
17ad1 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 . put4byte(
17ad2 70 43 65 6c 6c 2c 20 69 43 68 69 6c 64 29 3b 0a pCell, iChild);.
17ad3 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 }. j = pP
17ad4 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b age->nOverflow++
17ad5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c ;. assert( j<
17ad6 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 (int)(sizeof(pPa
17ad7 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f ge->aOvfl)/sizeo
17ad8 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 f(pPage->aOvfl[0
17ad9 5d 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 ])) );. pPage
17ada 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c ->aOvfl[j].pCell
17adb 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 = pCell;. pP
17adc 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 age->aOvfl[j].id
17add 78 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 7d 65 x = (u16)i;. }e
17ade 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 lse{. int rc
17adf 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 = sqlite3PagerWr
17ae0 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 ite(pPage->pDbPa
17ae1 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 ge);. if( rc!
17ae2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
17ae3 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 *pRC = rc;.
17ae4 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 return;.
17ae5 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 }. assert( s
17ae6 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
17ae7 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 teable(pPage->pD
17ae8 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20 64 61 bPage) );. da
17ae9 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 ta = pPage->aDat
17aea 61 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65 a;. cellOffse
17aeb 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f t = pPage->cellO
17aec 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d ffset;. end =
17aed 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a cellOffset + 2*
17aee 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 pPage->nCell;.
17aef 20 20 69 6e 73 20 3d 20 63 65 6c 6c 4f 66 66 73 ins = cellOffs
17af0 65 74 20 2b 20 32 2a 69 3b 0a 20 20 20 20 72 63 et + 2*i;. rc
17af1 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 = allocateSpace
17af2 28 70 50 61 67 65 2c 20 73 7a 2c 20 26 69 64 78 (pPage, sz, &idx
17af3 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b );. if( rc ){
17af4 20 2a 70 52 43 20 3d 20 72 63 3b 20 72 65 74 75 *pRC = rc; retu
17af5 72 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 rn; }. /* The
17af6 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 29 allocateSpace()
17af7 20 72 6f 75 74 69 6e 65 20 67 75 61 72 61 6e 74 routine guarant
17af8 65 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e ees the followin
17af9 67 20 74 77 6f 20 70 72 6f 70 65 72 74 69 65 73 g two properties
17afa 0a 20 20 20 20 2a 2a 20 69 66 20 69 74 20 72 65 . ** if it re
17afb 74 75 72 6e 73 20 73 75 63 63 65 73 73 20 2a 2f turns success */
17afc 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 . assert( idx
17afd 20 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a 20 20 20 >= end+2 );.
17afe 20 61 73 73 65 72 74 28 20 69 64 78 2b 73 7a 20 assert( idx+sz
17aff 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 <= pPage->pBt->u
17b00 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 sableSize );.
17b01 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b pPage->nCell++;
17b02 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 . pPage->nFre
17b03 65 20 2d 3d 20 28 75 31 36 29 28 32 20 2b 20 73 e -= (u16)(2 + s
17b04 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 z);. memcpy(&
17b05 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c data[idx+nSkip],
17b06 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a pCell+nSkip, sz
17b07 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 69 66 28 -nSkip);. if(
17b08 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 iChild ){.
17b09 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b put4byte(&data[
17b0a 69 64 78 5d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 idx], iChild);.
17b0b 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 65 }. for(j=e
17b0c 6e 64 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a 5d nd, ptr=&data[j]
17b0d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 ; j>ins; j-=2, p
17b0e 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70 74 tr-=2){. pt
17b0f 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a r[0] = ptr[-2];.
17b10 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70 ptr[1] = p
17b11 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 tr[-1];. }.
17b12 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 put2byte(&data
17b13 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20 20 [ins], idx);.
17b14 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b put2byte(&data[
17b15 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 pPage->hdrOffset
17b16 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c +3], pPage->nCel
17b17 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 l);.#ifndef SQLI
17b18 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
17b19 55 4d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 UM. if( pPage
17b1a 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 ->pBt->autoVacuu
17b1b 6d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 m ){. /* Th
17b1c 65 20 63 65 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 e cell may conta
17b1d 69 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 in a pointer to
17b1e 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 an overflow page
17b1f 2e 20 49 66 20 73 6f 2c 20 77 72 69 74 65 0a 20 . If so, write.
17b20 20 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 72 ** the entr
17b21 79 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c y for the overfl
17b22 6f 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 ow page into the
17b23 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 20 20 pointer map..
17b24 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 74 72 */. ptr
17b25 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 mapPutOvflPtr(pP
17b26 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70 52 43 29 age, pCell, pRC)
17b27 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
17b28 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 }.}../*.** Add
17b29 61 20 6c 69 73 74 20 6f 66 20 63 65 6c 6c 73 20 a list of cells
17b2a 74 6f 20 61 20 70 61 67 65 2e 20 20 54 68 65 20 to a page. The
17b2b 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 69 page should be i
17b2c 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74 79 2e 0a nitially empty..
17b2d 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20 61 72 65 ** The cells are
17b2e 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 66 guaranteed to f
17b2f 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a it on the page..
17b30 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 */.static void a
17b31 73 73 65 6d 62 6c 65 50 61 67 65 28 0a 20 20 4d ssemblePage(. M
17b32 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 emPage *pPage,
17b33 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 6f 20 /* The page to
17b34 62 65 20 61 73 73 65 6d 62 6c 69 65 64 20 2a 2f be assemblied */
17b35 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 . int nCell,
17b36 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 /* The numb
17b37 65 72 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 er of cells to a
17b38 64 64 20 74 6f 20 74 68 69 73 20 70 61 67 65 20 dd to this page
17b39 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c */. u8 **apCell
17b3a 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 , /* Pointe
17b3b 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f 64 69 65 rs to cell bodie
17b3c 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61 53 69 7a s */. u16 *aSiz
17b3d 65 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 e /* Size
17b3e 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 2a s of the cells *
17b3f 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 /.){. int i;
17b40 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
17b41 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38 counter */. u8
17b42 20 2a 70 43 65 6c 6c 70 74 72 3b 20 20 20 20 20 *pCellptr;
17b43 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 /* Address of ne
17b44 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 xt cell pointer
17b45 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 62 6f 64 */. int cellbod
17b46 79 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 y; /* Addres
17b47 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 62 s of next cell b
17b48 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f ody */. u8 * co
17b49 6e 73 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 nst data = pPage
17b4a 2d 3e 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 ->aData;
17b4b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 /* Pointer
17b4c 74 6f 20 64 61 74 61 20 66 6f 72 20 70 50 61 67 to data for pPag
17b4d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 e */. const int
17b4e 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 hdr = pPage->hd
17b4f 72 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 rOffset;
17b50 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 /* Offset of
17b51 68 65 61 64 65 72 20 6f 6e 20 70 50 61 67 65 20 header on pPage
17b52 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e */. const int n
17b53 55 73 61 62 6c 65 20 3d 20 70 50 61 67 65 2d 3e Usable = pPage->
17b54 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b pBt->usableSize;
17b55 20 2f 2a 20 55 73 61 62 6c 65 20 73 69 7a 65 20 /* Usable size
17b56 6f 66 20 70 61 67 65 20 2a 2f 0a 0a 20 20 61 73 of page */.. as
17b57 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 sert( pPage->nOv
17b58 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 erflow==0 );. a
17b59 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
17b5a 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d utex_held(pPage-
17b5b 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a >pBt->mutex) );.
17b5c 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3e assert( nCell>
17b5d 3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f =0 && nCell<=MX_
17b5e 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 CELL(pPage->pBt)
17b5f 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 && MX_CELL(pPag
17b60 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b e->pBt)<=5460 );
17b61 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 . assert( sqlit
17b62 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 e3PagerIswriteab
17b63 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 le(pPage->pDbPag
17b64 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 e) );.. /* Chec
17b65 6b 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 k that the page
17b66 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 7a 65 has just been ze
17b67 72 6f 65 64 20 62 79 20 7a 65 72 6f 50 61 67 65 roed by zeroPage
17b68 28 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 () */. assert(
17b69 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 pPage->nCell==0
17b6a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 67 65 74 );. assert( get
17b6b 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
17b6c 35 5d 29 3d 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 5])==nUsable );.
17b6d 0a 20 20 70 43 65 6c 6c 70 74 72 20 3d 20 26 64 . pCellptr = &d
17b6e 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f ata[pPage->cellO
17b6f 66 66 73 65 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d ffset + nCell*2]
17b70 3b 0a 20 20 63 65 6c 6c 62 6f 64 79 20 3d 20 6e ;. cellbody = n
17b71 55 73 61 62 6c 65 3b 0a 20 20 66 6f 72 28 69 3d Usable;. for(i=
17b72 6e 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 nCell-1; i>=0; i
17b73 2d 2d 29 7b 0a 20 20 20 20 70 43 65 6c 6c 70 74 --){. pCellpt
17b74 72 20 2d 3d 20 32 3b 0a 20 20 20 20 63 65 6c 6c r -= 2;. cell
17b75 62 6f 64 79 20 2d 3d 20 61 53 69 7a 65 5b 69 5d body -= aSize[i]
17b76 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 70 ;. put2byte(p
17b77 43 65 6c 6c 70 74 72 2c 20 63 65 6c 6c 62 6f 64 Cellptr, cellbod
17b78 79 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 y);. memcpy(&
17b79 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 data[cellbody],
17b7a 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 apCell[i], aSize
17b7b 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 75 74 32 [i]);. }. put2
17b7c 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 byte(&data[hdr+3
17b7d 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 70 75 74 ], nCell);. put
17b7e 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
17b7f 35 5d 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 5], cellbody);.
17b80 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d pPage->nFree -=
17b81 20 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 (nCell*2 + nUsa
17b82 62 6c 65 20 2d 20 63 65 6c 6c 62 6f 64 79 29 3b ble - cellbody);
17b83 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 . pPage->nCell
17b84 3d 20 28 75 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a = (u16)nCell;.}.
17b85 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
17b86 77 69 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 wing parameters
17b87 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 20 6d 61 determine how ma
17b88 6e 79 20 61 64 6a 61 63 65 6e 74 20 70 61 67 65 ny adjacent page
17b89 73 20 67 65 74 20 69 6e 76 6f 6c 76 65 64 0a 2a s get involved.*
17b8a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69 6e 67 * in a balancing
17b8b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 operation. NN
17b8c 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 is the number of
17b8d 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 neighbors on ei
17b8e 74 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f 66 20 ther side.** of
17b8f 74 68 65 20 70 61 67 65 20 74 68 61 74 20 70 61 the page that pa
17b90 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 68 65 rticipate in the
17b91 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 balancing opera
17b92 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74 68 65 tion. NB is the
17b93 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 .** total number
17b94 20 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 70 of pages that p
17b95 61 72 74 69 63 69 70 61 74 65 2c 20 69 6e 63 6c articipate, incl
17b96 75 64 69 6e 67 20 74 68 65 20 74 61 72 67 65 74 uding the target
17b97 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 page and.** NN
17b98 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 neighbors on eit
17b99 68 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 her side..**.**
17b9a 54 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 The minimum valu
17b9b 65 20 6f 66 20 4e 4e 20 69 73 20 31 20 28 6f 66 e of NN is 1 (of
17b9c 20 63 6f 75 72 73 65 29 2e 20 20 49 6e 63 72 65 course). Incre
17b9d 61 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65 20 31 asing NN above 1
17b9e 0a 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33 29 20 .** (to 2 or 3)
17b9f 67 69 76 65 73 20 61 20 6d 6f 64 65 73 74 20 69 gives a modest i
17ba0 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20 53 45 mprovement in SE
17ba1 4c 45 43 54 20 61 6e 64 20 44 45 4c 45 54 45 20 LECT and DELETE
17ba2 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 performance.** i
17ba3 6e 20 65 78 63 68 61 6e 67 65 20 66 6f 72 20 61 n exchange for a
17ba4 20 6c 61 72 67 65 72 20 64 65 67 72 61 64 61 74 larger degradat
17ba5 69 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20 61 6e ion in INSERT an
17ba6 64 20 55 50 44 41 54 45 20 70 65 72 66 6f 72 6d d UPDATE perform
17ba7 61 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76 61 6c ance..** The val
17ba8 75 65 20 6f 66 20 4e 4e 20 61 70 70 65 61 72 73 ue of NN appears
17ba9 20 74 6f 20 67 69 76 65 20 74 68 65 20 62 65 73 to give the bes
17baa 74 20 72 65 73 75 6c 74 73 20 6f 76 65 72 61 6c t results overal
17bab 6c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4e l..*/.#define NN
17bac 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 1 /
17bad 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 69 67 * Number of neig
17bae 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20 hbors on either
17baf 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 2a 2f side of pPage */
17bb0 0a 23 64 65 66 69 6e 65 20 4e 42 20 28 4e 4e 2a .#define NB (NN*
17bb1 32 2b 31 29 20 20 20 20 20 20 2f 2a 20 54 6f 74 2+1) /* Tot
17bb2 61 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 al pages involve
17bb3 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 65 d in the balance
17bb4 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 */...#ifndef SQ
17bb5 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 LITE_OMIT_QUICKB
17bb6 41 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 ALANCE./*.** Thi
17bb7 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c s version of bal
17bb8 61 6e 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 ance() handles t
17bb9 68 65 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 he common specia
17bba 6c 20 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 l case where.**
17bbb 61 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 a new entry is b
17bbc 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e eing inserted on
17bbd 20 74 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 the extreme rig
17bbe 68 74 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a ht-end of the.**
17bbf 20 74 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 tree, in other
17bc0 77 6f 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 words, when the
17bc1 6e 65 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 new entry will b
17bc2 65 63 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 ecome the larges
17bc3 74 0a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 t.** entry in th
17bc4 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e e tree..**.** In
17bc5 73 74 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 stead of trying
17bc6 74 6f 20 62 61 6c 61 6e 63 65 20 74 68 65 20 33 to balance the 3
17bc7 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 right-most leaf
17bc8 20 70 61 67 65 73 2c 20 6a 75 73 74 20 61 64 64 pages, just add
17bc9 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67 65 20 74 .** a new page t
17bca 6f 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 o the right-hand
17bcb 20 73 69 64 65 20 61 6e 64 20 70 75 74 20 74 68 side and put th
17bcc 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74 72 79 20 e one new entry
17bcd 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 2e in.** that page.
17bce 20 20 54 68 69 73 20 6c 65 61 76 65 73 20 74 68 This leaves th
17bcf 65 20 72 69 67 68 74 20 73 69 64 65 20 6f 66 20 e right side of
17bd0 74 68 65 20 74 72 65 65 20 73 6f 6d 65 77 68 61 the tree somewha
17bd1 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63 65 64 2e t.** unbalanced.
17bd2 20 20 42 75 74 20 6f 64 64 73 20 61 72 65 20 74 But odds are t
17bd3 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 69 hat we will be i
17bd4 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 65 6e 74 nserting new ent
17bd5 72 69 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 65 ries.** at the e
17bd6 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72 77 61 72 nd soon afterwar
17bd7 64 73 20 73 6f 20 74 68 65 20 6e 65 61 72 6c 79 ds so the nearly
17bd8 20 65 6d 70 74 79 20 70 61 67 65 20 77 69 6c 6c empty page will
17bd9 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c quickly.** fill
17bda 20 75 70 2e 20 20 4f 6e 20 61 76 65 72 61 67 65 up. On average
17bdb 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65 20 69 73 ..**.** pPage is
17bdc 20 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 77 the leaf page w
17bdd 68 69 63 68 20 69 73 20 74 68 65 20 72 69 67 68 hich is the righ
17bde 74 2d 6d 6f 73 74 20 70 61 67 65 20 69 6e 20 74 t-most page in t
17bdf 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70 50 61 72 he tree..** pPar
17be0 65 6e 74 20 69 73 20 69 74 73 20 70 61 72 65 6e ent is its paren
17be1 74 2e 20 20 70 50 61 67 65 20 6d 75 73 74 20 68 t. pPage must h
17be2 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 76 65 ave a single ove
17be3 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a 2a 20 77 rflow entry.** w
17be4 68 69 63 68 20 69 73 20 61 6c 73 6f 20 74 68 65 hich is also the
17be5 20 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 right-most entr
17be6 79 20 6f 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a y on the page..*
17be7 2a 0a 2a 2a 20 54 68 65 20 70 53 70 61 63 65 20 *.** The pSpace
17be8 62 75 66 66 65 72 20 69 73 20 75 73 65 64 20 74 buffer is used t
17be9 6f 20 73 74 6f 72 65 20 61 20 74 65 6d 70 6f 72 o store a tempor
17bea 61 72 79 20 63 6f 70 79 20 6f 66 20 74 68 65 20 ary copy of the
17beb 64 69 76 69 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 divider.** cell
17bec 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6e 73 that will be ins
17bed 65 72 74 65 64 20 69 6e 74 6f 20 70 50 61 72 65 erted into pPare
17bee 6e 74 2e 20 53 75 63 68 20 61 20 63 65 6c 6c 20 nt. Such a cell
17bef 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 0a consists of a 4.
17bf0 2a 2a 20 62 79 74 65 20 70 61 67 65 20 6e 75 6d ** byte page num
17bf1 62 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 ber followed by
17bf2 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 a variable lengt
17bf3 68 20 69 6e 74 65 67 65 72 2e 20 49 6e 20 6f 74 h integer. In ot
17bf4 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 61 74 her.** words, at
17bf5 20 6d 6f 73 74 20 31 33 20 62 79 74 65 73 2e 20 most 13 bytes.
17bf6 48 65 6e 63 65 20 74 68 65 20 70 53 70 61 63 65 Hence the pSpace
17bf7 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 buffer must be
17bf8 61 74 0a 2a 2a 20 6c 65 61 73 74 20 31 33 20 62 at.** least 13 b
17bf9 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f ytes in size..*/
17bfa 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
17bfb 6e 63 65 5f 71 75 69 63 6b 28 4d 65 6d 50 61 67 nce_quick(MemPag
17bfc 65 20 2a 70 50 61 72 65 6e 74 2c 20 4d 65 6d 50 e *pParent, MemP
17bfd 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a age *pPage, u8 *
17bfe 70 53 70 61 63 65 29 7b 0a 20 20 42 74 53 68 61 pSpace){. BtSha
17bff 72 65 64 20 2a 63 6f 6e 73 74 20 70 42 74 20 3d red *const pBt =
17c00 20 70 50 61 67 65 2d 3e 70 42 74 3b 20 20 20 20 pPage->pBt;
17c01 2f 2a 20 42 2d 54 72 65 65 20 44 61 74 61 62 61 /* B-Tree Databa
17c02 73 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 se */. MemPage
17c03 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 *pNew;
17c04 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17c05 4e 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 Newly allocated
17c06 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 page */. int rc
17c07 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17c09 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f * Return Code */
17c0a 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b . Pgno pgnoNew;
17c0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17c0c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 /* Page
17c0d 6e 75 6d 62 65 72 20 6f 66 20 70 4e 65 77 20 2a number of pNew *
17c0e 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c /.. assert( sql
17c0f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
17c10 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 pPage->pBt->mute
17c11 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
17c12 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 sqlite3PagerIswr
17c13 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d iteable(pParent-
17c14 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 >pDbPage) );. a
17c15 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f ssert( pPage->nO
17c16 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 verflow==1 );..
17c17 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c if( pPage->nCel
17c18 6c 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 l<=0 ) return SQ
17c19 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 LITE_CORRUPT_BKP
17c1a 54 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 T;.. /* Allocat
17c1b 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 54 68 e a new page. Th
17c1c 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63 is page will bec
17c1d 6f 6d 65 20 74 68 65 20 72 69 67 68 74 2d 73 69 ome the right-si
17c1e 62 6c 69 6e 67 20 6f 66 20 0a 20 20 2a 2a 20 70 bling of . ** p
17c1f 50 61 67 65 2e 20 4d 61 6b 65 20 74 68 65 20 70 Page. Make the p
17c20 61 72 65 6e 74 20 70 61 67 65 20 77 72 69 74 61 arent page writa
17c21 62 6c 65 2c 20 73 6f 20 74 68 61 74 20 74 68 65 ble, so that the
17c22 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c new divider cel
17c23 6c 0a 20 20 2a 2a 20 6d 61 79 20 62 65 20 69 6e l. ** may be in
17c24 73 65 72 74 65 64 2e 20 49 66 20 62 6f 74 68 20 serted. If both
17c25 74 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 these operations
17c26 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2c are successful,
17c27 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 proceed.. */.
17c28 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 rc = allocateBt
17c29 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4e reePage(pBt, &pN
17c2a 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c 20 30 2c ew, &pgnoNew, 0,
17c2b 20 30 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 0);.. if( rc==
17c2c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 SQLITE_OK ){..
17c2d 20 20 75 38 20 2a 70 4f 75 74 20 3d 20 26 70 53 u8 *pOut = &pS
17c2e 70 61 63 65 5b 34 5d 3b 0a 20 20 20 20 75 38 20 pace[4];. u8
17c2f 2a 70 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e *pCell = pPage->
17c30 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a aOvfl[0].pCell;.
17c31 20 20 20 20 75 31 36 20 73 7a 43 65 6c 6c 20 3d u16 szCell =
17c32 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 cellSizePtr(pPa
17c33 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 ge, pCell);.
17c34 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20 20 20 20 u8 *pStop;..
17c35 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
17c36 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
17c37 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29 20 29 pNew->pDbPage) )
17c38 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
17c39 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 age->aData[0]==(
17c3a 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c PTF_INTKEY|PTF_L
17c3b 45 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 EAFDATA|PTF_LEAF
17c3c 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 ) );. zeroPag
17c3d 65 28 70 4e 65 77 2c 20 50 54 46 5f 49 4e 54 4b e(pNew, PTF_INTK
17c3e 45 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c EY|PTF_LEAFDATA|
17c3f 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 20 20 61 PTF_LEAF);. a
17c40 73 73 65 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 ssemblePage(pNew
17c41 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20 26 73 7a , 1, &pCell, &sz
17c42 43 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 Cell);.. /* I
17c43 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 f this is an aut
17c44 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 o-vacuum databas
17c45 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f e, update the po
17c46 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a inter map. **
17c47 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f with entries fo
17c48 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20 r the new page,
17c49 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20 and any pointer
17c4a 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a from the . **
17c4b 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67 cell on the pag
17c4c 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 e to an overflow
17c4d 20 70 61 67 65 2e 20 49 66 20 65 69 74 68 65 72 page. If either
17c4e 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a of these. **
17c4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 66 61 69 6c operations fail
17c50 73 2c 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f s, the return co
17c51 64 65 20 69 73 20 73 65 74 2c 20 62 75 74 20 74 de is set, but t
17c52 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20 20 he contents.
17c53 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 ** of the parent
17c54 20 70 61 67 65 20 61 72 65 20 73 74 69 6c 6c 20 page are still
17c55 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 74 manipulated by t
17c56 68 68 20 63 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 hh code below..
17c57 20 20 20 2a 2a 20 54 68 61 74 20 69 73 20 4f 6b ** That is Ok
17c58 2c 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 , at this point
17c59 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 the parent page
17c5a 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f is guaranteed to
17c5b 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61 72 6b 65 . ** be marke
17c5c 64 20 61 73 20 64 69 72 74 79 2e 20 52 65 74 75 d as dirty. Retu
17c5d 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 rning an error c
17c5e 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65 20 61 ode will cause a
17c5f 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b . ** rollback
17c60 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 63 68 , undoing any ch
17c61 61 6e 67 65 73 20 6d 61 64 65 20 74 6f 20 74 68 anges made to th
17c62 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 e parent page..
17c63 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 */. if( IS
17c64 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 AUTOVACUUM ){.
17c65 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
17c66 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d t, pgnoNew, PTRM
17c67 41 50 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e AP_BTREE, pParen
17c68 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 t->pgno, &rc);.
17c69 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c 3e if( szCell>
17c6a 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 pNew->minLocal )
17c6b 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 {. ptrmap
17c6c 50 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c PutOvflPtr(pNew,
17c6d 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 pCell, &rc);.
17c6e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
17c6f 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 64 /* Create a d
17c70 69 76 69 64 65 72 20 63 65 6c 6c 20 74 6f 20 69 ivider cell to i
17c71 6e 73 65 72 74 20 69 6e 74 6f 20 70 50 61 72 65 nsert into pPare
17c72 6e 74 2e 20 54 68 65 20 64 69 76 69 64 65 72 20 nt. The divider
17c73 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 cell. ** cons
17c74 69 73 74 73 20 6f 66 20 61 20 34 2d 62 79 74 65 ists of a 4-byte
17c75 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 74 68 page number (th
17c76 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 e page number of
17c77 20 70 50 61 67 65 29 20 61 6e 64 0a 20 20 20 20 pPage) and.
17c78 2a 2a 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65 ** a variable le
17c79 6e 67 74 68 20 6b 65 79 20 76 61 6c 75 65 20 28 ngth key value (
17c7a 77 68 69 63 68 20 6d 75 73 74 20 62 65 20 74 68 which must be th
17c7b 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 e same value as
17c7c 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 the. ** large
17c7d 73 74 20 6b 65 79 20 6f 6e 20 70 50 61 67 65 29 st key on pPage)
17c7e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
17c7f 54 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 To find the larg
17c80 65 73 74 20 6b 65 79 20 76 61 6c 75 65 20 6f 6e est key value on
17c81 20 70 50 61 67 65 2c 20 66 69 72 73 74 20 66 69 pPage, first fi
17c82 6e 64 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 nd the right-mos
17c83 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f t . ** cell o
17c84 6e 20 70 50 61 67 65 2e 20 54 68 65 20 66 69 72 n pPage. The fir
17c85 73 74 20 74 77 6f 20 66 69 65 6c 64 73 20 6f 66 st two fields of
17c86 20 74 68 69 73 20 63 65 6c 6c 20 61 72 65 20 74 this cell are t
17c87 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 he . ** recor
17c88 64 2d 6c 65 6e 67 74 68 20 28 61 20 76 61 72 69 d-length (a vari
17c89 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 able length inte
17c8a 67 65 72 20 61 74 20 6d 6f 73 74 20 33 32 2d 62 ger at most 32-b
17c8b 69 74 73 20 69 6e 20 73 69 7a 65 29 0a 20 20 20 its in size).
17c8c 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b 65 79 20 ** and the key
17c8d 76 61 6c 75 65 20 28 61 20 76 61 72 69 61 62 6c value (a variabl
17c8e 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 e length integer
17c8f 2c 20 6d 61 79 20 68 61 76 65 20 61 6e 79 20 76 , may have any v
17c90 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a 20 54 68 alue).. ** Th
17c91 65 20 66 69 72 73 74 20 6f 66 20 74 68 65 20 77 e first of the w
17c92 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 hile(...) loops
17c93 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f 76 65 72 below skips over
17c94 20 74 68 65 20 72 65 63 6f 72 64 2d 6c 65 6e 67 the record-leng
17c95 74 68 0a 20 20 20 20 2a 2a 20 66 69 65 6c 64 2e th. ** field.
17c96 20 54 68 65 20 73 65 63 6f 6e 64 20 77 68 69 6c The second whil
17c97 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 e(...) loop copi
17c98 65 73 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 es the key value
17c99 20 66 72 6f 6d 20 74 68 65 0a 20 20 20 20 2a 2a from the. **
17c9a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 69 cell on pPage i
17c9b 6e 74 6f 20 74 68 65 20 70 53 70 61 63 65 20 62 nto the pSpace b
17c9c 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 uffer.. */.
17c9d 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 pCell = findCe
17c9e 6c 6c 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d ll(pPage, pPage-
17c9f 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 >nCell-1);. p
17ca0 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d Stop = &pCell[9]
17ca1 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 2a 28 ;. while( (*(
17ca2 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30 29 20 26 pCell++)&0x80) &
17ca3 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b & pCell<pStop );
17ca4 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70 43 . pStop = &pC
17ca5 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c ell[9];. whil
17ca6 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b 29 20 3d e( ((*(pOut++) =
17ca7 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26 30 78 38 *(pCell++))&0x8
17ca8 30 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 0) && pCell<pSto
17ca9 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 p );.. /* Ins
17caa 65 72 74 20 74 68 65 20 6e 65 77 20 64 69 76 69 ert the new divi
17cab 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 70 50 der cell into pP
17cac 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 6e arent. */. in
17cad 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 sertCell(pParent
17cae 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c , pParent->nCell
17caf 2c 20 70 53 70 61 63 65 2c 20 28 69 6e 74 29 28 , pSpace, (int)(
17cb0 70 4f 75 74 2d 70 53 70 61 63 65 29 2c 0a 20 20 pOut-pSpace),.
17cb1 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 0,
17cb2 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 pPage->pgno, &rc
17cb3 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 );.. /* Set t
17cb4 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 70 he right-child p
17cb5 6f 69 6e 74 65 72 20 6f 66 20 70 50 61 72 65 6e ointer of pParen
17cb6 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 t to point to th
17cb7 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 e new page. */.
17cb8 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61 put4byte(&pPa
17cb9 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 rent->aData[pPar
17cba 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 ent->hdrOffset+8
17cbb 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a ], pgnoNew);. .
17cbc 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 /* Release t
17cbd 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 he reference to
17cbe 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a 2f the new page. */
17cbf 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 . releasePage
17cc0 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 (pNew);. }.. r
17cc1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
17cc2 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
17cc3 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 20 2a T_QUICKBALANCE *
17cc4 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 /..#if 0./*.** T
17cc5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 his function doe
17cc6 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62 75 74 65 s not contribute
17cc7 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 anything to the
17cc8 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 53 51 operation of SQ
17cc9 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69 73 20 73 Lite..** it is s
17cca 6f 6d 65 74 69 6d 65 73 20 61 63 74 69 76 61 74 ometimes activat
17ccb 65 64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 77 ed temporarily w
17ccc 68 69 6c 65 20 64 65 62 75 67 67 69 6e 67 20 63 hile debugging c
17ccd 6f 64 65 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 ode responsible
17cce 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69 6e 67 20 .** for setting
17ccf 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 pointer-map entr
17cd0 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ies..*/.static i
17cd1 6e 74 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 nt ptrmapCheckPa
17cd2 67 65 73 28 4d 65 6d 50 61 67 65 20 2a 2a 61 70 ges(MemPage **ap
17cd3 50 61 67 65 2c 20 69 6e 74 20 6e 50 61 67 65 29 Page, int nPage)
17cd4 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 {. int i, j;.
17cd5 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50 61 67 65 for(i=0; i<nPage
17cd6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50 67 6e 6f ; i++){. Pgno
17cd7 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b 0a 20 20 n;. u8 e;.
17cd8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 MemPage *pPage
17cd9 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b 0a 20 20 = apPage[i];.
17cda 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 BtShared *pBt
17cdb 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 = pPage->pBt;.
17cdc 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d assert( pPage-
17cdd 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20 20 20 20 >isInit );..
17cde 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 for(j=0; j<pPage
17cdf 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 ->nCell; j++){.
17ce0 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e CellInfo in
17ce1 66 6f 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 3b fo;. u8 *z;
17ce2 0a 20 20 20 20 20 0a 20 20 20 20 20 20 7a 20 3d . . z =
17ce3 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c findCell(pPage,
17ce4 20 6a 29 3b 0a 20 20 20 20 20 20 62 74 72 65 65 j);. btree
17ce5 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 ParseCellPtr(pPa
17ce6 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 ge, z, &info);.
17ce7 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f if( info.iO
17ce8 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 verflow ){.
17ce9 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 Pgno ovfl = g
17cea 65 74 34 62 79 74 65 28 26 7a 5b 69 6e 66 6f 2e et4byte(&z[info.
17ceb 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 iOverflow]);.
17cec 20 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 ptrmapGet(p
17ced 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c 20 26 6e Bt, ovfl, &e, &n
17cee 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 );. asser
17cef 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e t( n==pPage->pgn
17cf0 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 4f o && e==PTRMAP_O
17cf1 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20 20 20 20 VERFLOW1 );.
17cf2 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 }. if( !p
17cf3 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
17cf4 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 Pgno child
17cf5 20 3d 20 67 65 74 34 62 79 74 65 28 7a 29 3b 0a = get4byte(z);.
17cf6 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 ptrmapGe
17cf7 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 t(pBt, child, &e
17cf8 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 , &n);. a
17cf9 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d ssert( n==pPage-
17cfa 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d >pgno && e==PTRM
17cfb 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 AP_BTREE );.
17cfc 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
17cfd 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 ( !pPage->leaf )
17cfe 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69 {. Pgno chi
17cff 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 ld = get4byte(&p
17d00 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 Page->aData[pPag
17d01 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 e->hdrOffset+8])
17d02 3b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 47 65 ;. ptrmapGe
17d03 74 28 70 42 74 2c 20 63 68 69 6c 64 2c 20 26 65 t(pBt, child, &e
17d04 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20 61 73 73 , &n);. ass
17d05 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 ert( n==pPage->p
17d06 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 gno && e==PTRMAP
17d07 5f 42 54 52 45 45 20 29 3b 0a 20 20 20 20 7d 0a _BTREE );. }.
17d08 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a }. return 1;.
17d09 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
17d0a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
17d0b 20 75 73 65 64 20 74 6f 20 63 6f 70 79 20 74 68 used to copy th
17d0c 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 e contents of th
17d0d 65 20 62 2d 74 72 65 65 20 6e 6f 64 65 20 73 74 e b-tree node st
17d0e 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 ored .** on page
17d0f 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 65 20 70 pFrom to page p
17d10 54 6f 2e 20 49 66 20 70 61 67 65 20 70 46 72 6f To. If page pFro
17d11 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c 65 61 66 m was not a leaf
17d12 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 page, then.** t
17d13 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 he pointer-map e
17d14 6e 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 ntries for each
17d15 63 68 69 6c 64 20 70 61 67 65 20 61 72 65 20 75 child page are u
17d16 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 pdated so that t
17d17 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20 70 61 67 he.** parent pag
17d18 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 e stored in the
17d19 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20 70 pointer map is p
17d1a 61 67 65 20 70 54 6f 2e 20 49 66 20 70 46 72 6f age pTo. If pFro
17d1b 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 m contained.** a
17d1c 6e 79 20 63 65 6c 6c 73 20 77 69 74 68 20 6f 76 ny cells with ov
17d1d 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e erflow page poin
17d1e 74 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63 ters, then the c
17d1f 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 69 orresponding poi
17d20 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 nter.** map entr
17d21 69 65 73 20 61 72 65 20 61 6c 73 6f 20 75 70 64 ies are also upd
17d22 61 74 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 ated so that the
17d23 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 parent page is
17d24 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 page pTo..**.**
17d25 49 66 20 70 46 72 6f 6d 20 69 73 20 63 75 72 72 If pFrom is curr
17d26 65 6e 74 6c 79 20 63 61 72 72 79 69 6e 67 20 61 ently carrying a
17d27 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c ny overflow cell
17d28 73 20 28 65 6e 74 72 69 65 73 20 69 6e 20 74 68 s (entries in th
17d29 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 4f 76 e.** MemPage.aOv
17d2a 66 6c 5b 5d 20 61 72 72 61 79 29 2c 20 74 68 65 fl[] array), the
17d2b 79 20 61 72 65 20 6e 6f 74 20 63 6f 70 69 65 64 y are not copied
17d2c 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 to pTo. .**.**
17d2d 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 Before returning
17d2e 2c 20 70 61 67 65 20 70 54 6f 20 69 73 20 72 65 , page pTo is re
17d2f 69 6e 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e initialized usin
17d30 67 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 g btreeInitPage(
17d31 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 )..**.** The per
17d32 66 6f 72 6d 61 6e 63 65 20 6f 66 20 74 68 69 73 formance of this
17d33 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 function is not
17d34 20 63 72 69 74 69 63 61 6c 2e 20 49 74 20 69 73 critical. It is
17d35 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 0a 2a only used by .*
17d36 2a 20 74 68 65 20 62 61 6c 61 6e 63 65 5f 73 68 * the balance_sh
17d37 61 6c 6c 6f 77 65 72 28 29 20 61 6e 64 20 62 61 allower() and ba
17d38 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 70 lance_deeper() p
17d39 72 6f 63 65 64 75 72 65 73 2c 20 6e 65 69 74 68 rocedures, neith
17d3a 65 72 20 6f 66 0a 2a 2a 20 77 68 69 63 68 20 61 er of.** which a
17d3b 72 65 20 63 61 6c 6c 65 64 20 6f 66 74 65 6e 20 re called often
17d3c 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 69 72 under normal cir
17d3d 63 75 6d 73 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 cumstances..*/.s
17d3e 74 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 4e tatic void copyN
17d3f 6f 64 65 43 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 odeContent(MemPa
17d40 67 65 20 2a 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 ge *pFrom, MemPa
17d41 67 65 20 2a 70 54 6f 2c 20 69 6e 74 20 2a 70 52 ge *pTo, int *pR
17d42 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 52 43 29 C){. if( (*pRC)
17d43 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
17d44 20 20 20 42 74 53 68 61 72 65 64 20 2a 20 63 6f BtShared * co
17d45 6e 73 74 20 70 42 74 20 3d 20 70 46 72 6f 6d 2d nst pBt = pFrom-
17d46 3e 70 42 74 3b 0a 20 20 20 20 75 38 20 2a 20 63 >pBt;. u8 * c
17d47 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20 70 46 72 onst aFrom = pFr
17d48 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 75 om->aData;. u
17d49 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f 20 3d 20 8 * const aTo =
17d4a 70 54 6f 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 pTo->aData;.
17d4b 69 6e 74 20 63 6f 6e 73 74 20 69 46 72 6f 6d 48 int const iFromH
17d4c 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68 64 72 4f dr = pFrom->hdrO
17d4d 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 63 ffset;. int c
17d4e 6f 6e 73 74 20 69 54 6f 48 64 72 20 3d 20 28 28 onst iToHdr = ((
17d4f 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 pTo->pgno==1) ?
17d50 31 30 30 20 3a 20 30 29 3b 0a 20 20 20 20 54 45 100 : 0);. TE
17d51 53 54 4f 4e 4c 59 28 69 6e 74 20 72 63 3b 29 0a STONLY(int rc;).
17d52 20 20 20 20 69 6e 74 20 69 44 61 74 61 3b 0a 20 int iData;.
17d53 20 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28 . . assert(
17d54 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74 20 29 pFrom->isInit )
17d55 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 ;. assert( pF
17d56 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54 6f 48 rom->nFree>=iToH
17d57 64 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 dr );. assert
17d58 28 20 67 65 74 32 62 79 74 65 28 26 61 46 72 6f ( get2byte(&aFro
17d59 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29 3c 3d m[iFromHdr+5])<=
17d5a 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 pBt->usableSize
17d5b 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43 6f 70 );. . /* Cop
17d5c 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 y the b-tree nod
17d5d 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 70 e content from p
17d5e 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67 age pFrom to pag
17d5f 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20 69 44 e pTo. */. iD
17d60 61 74 61 20 3d 20 67 65 74 32 62 79 74 65 28 26 ata = get2byte(&
17d61 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 aFrom[iFromHdr+5
17d62 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 ]);. memcpy(&
17d63 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61 46 72 aTo[iData], &aFr
17d64 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74 2d 3e om[iData], pBt->
17d65 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61 74 61 usableSize-iData
17d66 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 );. memcpy(&a
17d67 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61 46 72 To[iToHdr], &aFr
17d68 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20 70 46 om[iFromHdr], pF
17d69 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 rom->cellOffset
17d6a 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65 6c 6c + 2*pFrom->nCell
17d6b 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 69 );. . /* Rei
17d6c 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 20 70 nitialize page p
17d6d 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 To so that the c
17d6e 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 4d ontents of the M
17d6f 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 emPage structure
17d70 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20 74 68 . ** match th
17d71 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68 65 20 e new data. The
17d72 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f initialization o
17d73 66 20 70 54 6f 20 22 63 61 6e 6e 6f 74 22 20 66 f pTo "cannot" f
17d74 61 69 6c 2c 20 61 73 20 74 68 65 0a 20 20 20 20 ail, as the.
17d75 2a 2a 20 64 61 74 61 20 63 6f 70 69 65 64 20 66 ** data copied f
17d76 72 6f 6d 20 70 46 72 6f 6d 20 69 73 20 6b 6e 6f rom pFrom is kno
17d77 77 6e 20 74 6f 20 62 65 20 76 61 6c 69 64 2e 20 wn to be valid.
17d78 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e 69 73 49 */. pTo->isI
17d79 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 54 45 53 nit = 0;. TES
17d7a 54 4f 4e 4c 59 28 72 63 20 3d 20 29 20 62 74 72 TONLY(rc = ) btr
17d7b 65 65 49 6e 69 74 50 61 67 65 28 70 54 6f 29 3b eeInitPage(pTo);
17d7c 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d . assert( rc=
17d7d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 =SQLITE_OK );.
17d7e 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 . /* If this
17d7f 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 is an auto-vacuu
17d80 6d 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 m database, upda
17d81 74 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d te the pointer-m
17d82 61 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a ap entries. *
17d83 2a 20 66 6f 72 20 61 6e 79 20 62 2d 74 72 65 65 * for any b-tree
17d84 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 or overflow pag
17d85 65 73 20 74 68 61 74 20 70 54 6f 20 6e 6f 77 20 es that pTo now
17d86 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 contains the poi
17d87 6e 74 65 72 73 20 74 6f 2e 0a 20 20 20 20 2a 2f nters to.. */
17d88 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 . if( ISAUTOV
17d89 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 2a ACUUM ){. *
17d8a 70 52 43 20 3d 20 73 65 74 43 68 69 6c 64 50 74 pRC = setChildPt
17d8b 72 6d 61 70 73 28 70 54 6f 29 3b 0a 20 20 20 20 rmaps(pTo);.
17d8c 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 }. }.}../*.** T
17d8d 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 64 69 his routine redi
17d8e 73 74 72 69 62 75 74 65 73 20 63 65 6c 6c 73 20 stributes cells
17d8f 6f 6e 20 74 68 65 20 69 50 61 72 65 6e 74 49 64 on the iParentId
17d90 78 27 74 68 20 63 68 69 6c 64 20 6f 66 20 70 50 x'th child of pP
17d91 61 72 65 6e 74 0a 2a 2a 20 28 68 65 72 65 61 66 arent.** (hereaf
17d92 74 65 72 20 22 74 68 65 20 70 61 67 65 22 29 20 ter "the page")
17d93 61 6e 64 20 75 70 20 74 6f 20 32 20 73 69 62 6c and up to 2 sibl
17d94 69 6e 67 73 20 73 6f 20 74 68 61 74 20 61 6c 6c ings so that all
17d95 20 70 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 pages have abou
17d96 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 6d t the.** same am
17d97 6f 75 6e 74 20 6f 66 20 66 72 65 65 20 73 70 61 ount of free spa
17d98 63 65 2e 20 55 73 75 61 6c 6c 79 20 61 20 73 69 ce. Usually a si
17d99 6e 67 6c 65 20 73 69 62 6c 69 6e 67 20 6f 6e 20 ngle sibling on
17d9a 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 either side of t
17d9b 68 65 0a 2a 2a 20 70 61 67 65 20 61 72 65 20 75 he.** page are u
17d9c 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e sed in the balan
17d9d 63 69 6e 67 2c 20 74 68 6f 75 67 68 20 62 6f 74 cing, though bot
17d9e 68 20 73 69 62 6c 69 6e 67 73 20 6d 69 67 68 74 h siblings might
17d9f 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e 65 0a 2a come from one.*
17da0 2a 20 73 69 64 65 20 69 66 20 74 68 65 20 70 61 * side if the pa
17da1 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 ge is the first
17da2 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20 6f 66 or last child of
17da3 20 69 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 its parent. If
17da4 74 68 65 20 70 61 67 65 20 0a 2a 2a 20 68 61 73 the page .** has
17da5 20 66 65 77 65 72 20 74 68 61 6e 20 32 20 73 69 fewer than 2 si
17da6 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74 68 69 6e blings (somethin
17da7 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e 6c 79 g which can only
17da8 20 68 61 70 70 65 6e 20 69 66 20 74 68 65 20 70 happen if the p
17da9 61 67 65 0a 2a 2a 20 69 73 20 61 20 72 6f 6f 74 age.** is a root
17daa 20 70 61 67 65 20 6f 72 20 61 20 63 68 69 6c 64 page or a child
17dab 20 6f 66 20 61 20 72 6f 6f 74 20 70 61 67 65 29 of a root page)
17dac 20 74 68 65 6e 20 61 6c 6c 20 61 76 61 69 6c 61 then all availa
17dad 62 6c 65 20 73 69 62 6c 69 6e 67 73 0a 2a 2a 20 ble siblings.**
17dae 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74 participate in t
17daf 68 65 20 62 61 6c 61 6e 63 69 6e 67 2e 0a 2a 2a he balancing..**
17db0 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f .** The number o
17db1 66 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 74 68 f siblings of th
17db2 65 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20 e page might be
17db3 69 6e 63 72 65 61 73 65 64 20 6f 72 20 64 65 63 increased or dec
17db4 72 65 61 73 65 64 20 62 79 20 0a 2a 2a 20 6f 6e reased by .** on
17db5 65 20 6f 72 20 74 77 6f 20 69 6e 20 61 6e 20 65 e or two in an e
17db6 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 70 61 ffort to keep pa
17db7 67 65 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 20 ges nearly full
17db8 62 75 74 20 6e 6f 74 20 6f 76 65 72 20 66 75 6c but not over ful
17db9 6c 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 l. .**.** Note t
17dba 68 61 74 20 77 68 65 6e 20 74 68 69 73 20 72 6f hat when this ro
17dbb 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c utine is called,
17dbc 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 63 65 6c some of the cel
17dbd 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a ls on the page.*
17dbe 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61 63 74 75 * might not actu
17dbf 61 6c 6c 79 20 62 65 20 73 74 6f 72 65 64 20 69 ally be stored i
17dc0 6e 20 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b n MemPage.aData[
17dc1 5d 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 ]. This can happ
17dc2 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 en.** if the pag
17dc3 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 54 e is overfull. T
17dc4 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 his routine ensu
17dc5 72 65 73 20 74 68 61 74 20 61 6c 6c 20 63 65 6c res that all cel
17dc6 6c 73 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 ls allocated.**
17dc7 74 6f 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 to the page and
17dc8 69 74 73 20 73 69 62 6c 69 6e 67 73 20 66 69 74 its siblings fit
17dc9 20 69 6e 74 6f 20 4d 65 6d 50 61 67 65 2e 61 44 into MemPage.aD
17dca 61 74 61 5b 5d 20 62 65 66 6f 72 65 20 72 65 74 ata[] before ret
17dcb 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e urning..**.** In
17dcc 20 74 68 65 20 63 6f 75 72 73 65 20 6f 66 20 62 the course of b
17dcd 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 70 61 67 alancing the pag
17dce 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69 6e e and its siblin
17dcf 67 73 2c 20 63 65 6c 6c 73 20 6d 61 79 20 62 65 gs, cells may be
17dd0 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 .** inserted int
17dd1 6f 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f o or removed fro
17dd2 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 m the parent pag
17dd3 65 20 28 70 50 61 72 65 6e 74 29 2e 20 44 6f 69 e (pParent). Doi
17dd4 6e 67 20 73 6f 0a 2a 2a 20 6d 61 79 20 63 61 75 ng so.** may cau
17dd5 73 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 se the parent pa
17dd6 67 65 20 74 6f 20 62 65 63 6f 6d 65 20 6f 76 65 ge to become ove
17dd7 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 rfull or underfu
17dd8 6c 6c 2e 20 49 66 20 74 68 69 73 0a 2a 2a 20 68 ll. If this.** h
17dd9 61 70 70 65 6e 73 2c 20 69 74 20 69 73 20 74 68 appens, it is th
17dda 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 e responsibility
17ddb 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 of the caller t
17ddc 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 o invoke the cor
17ddd 72 65 63 74 0a 2a 2a 20 62 61 6c 61 6e 63 69 6e rect.** balancin
17dde 67 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 69 78 g routine to fix
17ddf 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 73 this problem (s
17de0 65 65 20 74 68 65 20 62 61 6c 61 6e 63 65 28 29 ee the balance()
17de1 20 72 6f 75 74 69 6e 65 29 2e 20 0a 2a 2a 0a 2a routine). .**.*
17de2 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e * If this routin
17de3 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 e fails for any
17de4 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74 reason, it might
17de5 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62 leave the datab
17de6 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72 ase.** in a corr
17de7 75 70 74 65 64 20 73 74 61 74 65 2e 20 53 6f 20 upted state. So
17de8 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 if this routine
17de9 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61 62 fails, the datab
17dea 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 ase should.** be
17deb 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a rolled back..**
17dec 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 .** The third ar
17ded 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 gument to this f
17dee 75 6e 63 74 69 6f 6e 2c 20 61 4f 76 66 6c 53 70 unction, aOvflSp
17def 61 63 65 2c 20 69 73 20 61 20 70 6f 69 6e 74 65 ace, is a pointe
17df0 72 20 74 6f 20 61 0a 2a 2a 20 62 75 66 66 65 72 r to a.** buffer
17df1 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 big enough to h
17df2 6f 6c 64 20 6f 6e 65 20 70 61 67 65 2e 20 49 66 old one page. If
17df3 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 while inserting
17df4 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 65 20 cells into the
17df5 70 61 72 65 6e 74 0a 2a 2a 20 70 61 67 65 20 28 parent.** page (
17df6 70 50 61 72 65 6e 74 29 20 74 68 65 20 70 61 72 pParent) the par
17df7 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 ent page becomes
17df8 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 69 73 20 overfull, this
17df9 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 75 73 65 buffer is.** use
17dfa 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 70 d to store the p
17dfb 61 72 65 6e 74 27 73 20 6f 76 65 72 66 6c 6f 77 arent's overflow
17dfc 20 63 65 6c 6c 73 2e 20 42 65 63 61 75 73 65 20 cells. Because
17dfd 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e this function in
17dfe 73 65 72 74 73 0a 2a 2a 20 61 20 6d 61 78 69 6d serts.** a maxim
17dff 75 6d 20 6f 66 20 66 6f 75 72 20 64 69 76 69 64 um of four divid
17e00 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74 68 er cells into th
17e01 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 61 e parent page, a
17e02 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a nd the maximum.*
17e03 2a 20 73 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c * size of a cell
17e04 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 61 stored within a
17e05 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 n internal node
17e06 69 73 20 61 6c 77 61 79 73 20 6c 65 73 73 20 74 is always less t
17e07 68 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66 20 74 68 han 1/4.** of th
17e08 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 74 68 65 e page-size, the
17e09 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 62 75 aOvflSpace[] bu
17e0a 66 66 65 72 20 69 73 20 67 75 61 72 61 6e 74 65 ffer is guarante
17e0b 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65 0a 2a ed to be large.*
17e0c 2a 20 65 6e 6f 75 67 68 20 66 6f 72 20 61 6c 6c * enough for all
17e0d 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e overflow cells.
17e0e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76 66 6c 53 .**.** If aOvflS
17e0f 70 61 63 65 20 69 73 20 73 65 74 20 74 6f 20 61 pace is set to a
17e10 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2c 20 74 null pointer, t
17e11 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 his function ret
17e12 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f urns .** SQLITE_
17e13 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 NOMEM..*/.static
17e14 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e int balance_non
17e15 72 6f 6f 74 28 0a 20 20 4d 65 6d 50 61 67 65 20 root(. MemPage
17e16 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 *pParent,
17e17 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e /* Paren
17e18 74 20 70 61 67 65 20 6f 66 20 73 69 62 6c 69 6e t page of siblin
17e19 67 73 20 62 65 69 6e 67 20 62 61 6c 61 6e 63 65 gs being balance
17e1a 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 65 d */. int iPare
17e1b 6e 74 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 ntIdx,
17e1c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
17e1d 6f 66 20 22 74 68 65 20 70 61 67 65 22 20 69 6e of "the page" in
17e1e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 75 38 pParent */. u8
17e1f 20 2a 61 4f 76 66 6c 53 70 61 63 65 2c 20 20 20 *aOvflSpace,
17e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17e21 20 70 61 67 65 2d 73 69 7a 65 20 62 79 74 65 73 page-size bytes
17e22 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 70 61 of space for pa
17e23 72 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a 20 20 69 rent ovfl */. i
17e24 6e 74 20 69 73 52 6f 6f 74 20 20 20 20 20 20 20 nt isRoot
17e25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
17e26 2a 20 54 72 75 65 20 69 66 20 70 50 61 72 65 6e * True if pParen
17e27 74 20 69 73 20 61 20 72 6f 6f 74 2d 70 61 67 65 t is a root-page
17e28 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 */.){. BtShare
17e29 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 d *pBt;
17e2a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68 6f /* The who
17e2b 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 le database */.
17e2c 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 20 int nCell = 0;
17e2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17e2e 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 Number of cells
17e2f 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a in apCell[] */.
17e30 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 73 20 int nMaxCells
17e31 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f = 0; /
17e32 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 * Allocated size
17e33 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65 of apCell, szCe
17e34 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f 0a 20 20 ll, aFrom. */.
17e35 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b 20 20 20 int nNew = 0;
17e36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e37 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
17e38 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f 0a 20 20 in apNew[] */.
17e39 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20 20 20 20 int nOld;
17e3a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e3b 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 Number of pages
17e3c 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 in apOld[] */.
17e3d 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20 20 20 20 int i, j, k;
17e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
17e3f 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f Loop counters */
17e40 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b 20 20 20 . int nxDiv;
17e41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e42 2f 2a 20 4e 65 78 74 20 64 69 76 69 64 65 72 20 /* Next divider
17e43 73 6c 6f 74 20 69 6e 20 70 50 61 72 65 6e 74 2d slot in pParent-
17e44 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e >aCell[] */. in
17e45 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b t rc = SQLITE_OK
17e46 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ; /* Th
17e47 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f e return code */
17e48 0a 20 20 75 31 36 20 6c 65 61 66 43 6f 72 72 65 . u16 leafCorre
17e49 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 ction;
17e4a 2f 2a 20 34 20 69 66 20 70 50 61 67 65 20 69 73 /* 4 if pPage is
17e4b 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 6e a leaf. 0 if n
17e4c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 ot */. int leaf
17e4d 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 Data;
17e4e 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
17e4f 70 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 pPage is a leaf
17e50 6f 66 20 61 20 4c 45 41 46 44 41 54 41 20 74 72 of a LEAFDATA tr
17e51 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61 62 ee */. int usab
17e52 6c 65 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 leSpace;
17e53 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e /* Bytes in
17e54 20 70 50 61 67 65 20 62 65 79 6f 6e 64 20 74 68 pPage beyond th
17e55 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e e header */. in
17e56 74 20 70 61 67 65 46 6c 61 67 73 3b 20 20 20 20 t pageFlags;
17e57 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
17e58 6c 75 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44 lue of pPage->aD
17e59 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69 6e 74 20 ata[0] */. int
17e5a 73 75 62 74 6f 74 61 6c 3b 20 20 20 20 20 20 20 subtotal;
17e5b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 74 /* Subt
17e5c 6f 74 61 6c 20 6f 66 20 62 79 74 65 73 20 69 6e otal of bytes in
17e5d 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65 20 70 61 cells on one pa
17e5e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 70 61 ge */. int iSpa
17e5f 63 65 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 ce1 = 0;
17e60 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e /* First un
17e61 75 73 65 64 20 62 79 74 65 20 6f 66 20 61 53 70 used byte of aSp
17e62 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 ace1[] */. int
17e63 69 4f 76 66 6c 53 70 61 63 65 20 3d 20 30 3b 20 iOvflSpace = 0;
17e64 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
17e65 74 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 t unused byte of
17e66 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d 20 2a 2f aOvflSpace[] */
17e67 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68 . int szScratch
17e68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
17e69 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74 /* Size of scrat
17e6a 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73 ch memory reques
17e6b 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 ted */. MemPage
17e6c 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20 *apOld[NB];
17e6d 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 /* pPage a
17e6e 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62 nd up to two sib
17e6f 6c 69 6e 67 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 lings */. MemPa
17e70 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42 5d 3b 20 ge *apCopy[NB];
17e71 20 20 20 20 20 20 20 20 2f 2a 20 50 72 69 76 61 /* Priva
17e72 74 65 20 63 6f 70 69 65 73 20 6f 66 20 61 70 4f te copies of apO
17e73 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f 0a 20 20 ld[] pages */.
17e74 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65 77 5b 4e MemPage *apNew[N
17e75 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 B+2]; /*
17e76 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20 pPage and up to
17e77 4e 42 20 73 69 62 6c 69 6e 67 73 20 61 66 74 65 NB siblings afte
17e78 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a 2f 0a 20 r balancing */.
17e79 20 75 38 20 2a 70 52 69 67 68 74 3b 20 20 20 20 u8 *pRight;
17e7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17e7b 20 4c 6f 63 61 74 69 6f 6e 20 69 6e 20 70 61 72 Location in par
17e7c 65 6e 74 20 6f 66 20 72 69 67 68 74 2d 73 69 62 ent of right-sib
17e7d 6c 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a ling pointer */.
17e7e 20 20 75 38 20 2a 61 70 44 69 76 5b 4e 42 2d 31 u8 *apDiv[NB-1
17e7f 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ]; /
17e80 2a 20 44 69 76 69 64 65 72 20 63 65 6c 6c 73 20 * Divider cells
17e81 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20 20 in pParent */.
17e82 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42 2b 32 5d int cntNew[NB+2]
17e83 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
17e84 49 6e 64 65 78 20 69 6e 20 61 43 65 6c 6c 5b 5d Index in aCell[]
17e85 20 6f 66 20 63 65 6c 6c 20 61 66 74 65 72 20 69 of cell after i
17e86 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e -th page */. in
17e87 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 t szNew[NB+2];
17e88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f /* Co
17e89 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f 66 20 63 mbined size of c
17e8a 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e 20 69 2d ells place on i-
17e8b 74 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 th page */. u8
17e8c 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b 20 20 20 **apCell = 0;
17e8d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c /* All
17e8e 20 63 65 6c 6c 73 20 62 65 67 69 6e 20 62 61 6c cells begin bal
17e8f 61 6e 63 65 64 20 2a 2f 0a 20 20 75 31 36 20 2a anced */. u16 *
17e90 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 szCell;
17e91 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c /* Local
17e92 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c size of all cel
17e93 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 2a ls in apCell[] *
17e94 2f 0a 20 20 75 38 20 2a 61 53 70 61 63 65 31 3b /. u8 *aSpace1;
17e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17e96 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63 6f /* Space for co
17e97 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72 73 pies of dividers
17e98 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f cells */. Pgno
17e99 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 pgno;
17e9a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
17e9b 20 76 61 72 20 74 6f 20 73 74 6f 72 65 20 61 20 var to store a
17e9c 70 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 2a page number in *
17e9d 2f 0a 0a 20 20 70 42 74 20 3d 20 70 50 61 72 65 /.. pBt = pPare
17e9e 6e 74 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 nt->pBt;. asser
17e9f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
17ea0 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 _held(pBt->mutex
17ea1 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 ) );. assert( s
17ea2 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 qlite3PagerIswri
17ea3 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e teable(pParent->
17ea4 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 23 69 66 pDbPage) );..#if
17ea5 20 30 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 0. TRACE(("BAL
17ea6 41 4e 43 45 3a 20 62 65 67 69 6e 20 70 61 67 65 ANCE: begin page
17ea7 20 25 64 20 63 68 69 6c 64 20 6f 66 20 25 64 5c %d child of %d\
17ea8 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c n", pPage->pgno,
17ea9 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 29 29 pParent->pgno))
17eaa 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 ;.#endif.. /* A
17eab 74 20 74 68 69 73 20 70 6f 69 6e 74 20 70 50 61 t this point pPa
17eac 72 65 6e 74 20 6d 61 79 20 68 61 76 65 20 61 74 rent may have at
17ead 20 6d 6f 73 74 20 6f 6e 65 20 6f 76 65 72 66 6c most one overfl
17eae 6f 77 20 63 65 6c 6c 2e 20 41 6e 64 20 69 66 0a ow cell. And if.
17eaf 20 20 2a 2a 20 74 68 69 73 20 6f 76 65 72 66 6c ** this overfl
17eb0 6f 77 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 ow cell is prese
17eb1 6e 74 2c 20 69 74 20 6d 75 73 74 20 62 65 20 74 nt, it must be t
17eb2 68 65 20 63 65 6c 6c 20 77 69 74 68 20 0a 20 20 he cell with .
17eb3 2a 2a 20 69 6e 64 65 78 20 69 50 61 72 65 6e 74 ** index iParent
17eb4 49 64 78 2e 20 54 68 69 73 20 73 63 65 6e 61 72 Idx. This scenar
17eb5 69 6f 20 63 6f 6d 65 73 20 61 62 6f 75 74 20 77 io comes about w
17eb6 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f hen this functio
17eb7 6e 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64 n. ** is called
17eb8 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 66 72 (indirectly) fr
17eb9 6f 6d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 om sqlite3BtreeD
17eba 65 6c 65 74 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 elete().. */.
17ebb 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d assert( pParent-
17ebc 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c >nOverflow==0 ||
17ebd 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 pParent->nOverf
17ebe 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 low==1 );. asse
17ebf 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 rt( pParent->nOv
17ec0 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 erflow==0 || pPa
17ec1 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 rent->aOvfl[0].i
17ec2 64 78 3d 3d 69 50 61 72 65 6e 74 49 64 78 20 29 dx==iParentIdx )
17ec3 3b 0a 0a 20 20 69 66 28 20 21 61 4f 76 66 6c 53 ;.. if( !aOvflS
17ec4 70 61 63 65 20 29 7b 0a 20 20 20 20 72 65 74 75 pace ){. retu
17ec5 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
17ec6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 . }.. /* Find
17ec7 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 the sibling page
17ec8 73 20 74 6f 20 62 61 6c 61 6e 63 65 2e 20 41 6c s to balance. Al
17ec9 73 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 63 65 so locate the ce
17eca 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 0a lls in pParent .
17ecb 20 20 2a 2a 20 74 68 61 74 20 64 69 76 69 64 65 ** that divide
17ecc 20 74 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 41 the siblings. A
17ecd 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 n attempt is mad
17ece 65 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 e to find NN sib
17ecf 6c 69 6e 67 73 20 6f 6e 20 0a 20 20 2a 2a 20 65 lings on . ** e
17ed0 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70 50 ither side of pP
17ed1 61 67 65 2e 20 4d 6f 72 65 20 73 69 62 6c 69 6e age. More siblin
17ed2 67 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f gs are taken fro
17ed3 6d 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 m one side, howe
17ed4 76 65 72 2c 20 0a 20 20 2a 2a 20 69 66 20 74 68 ver, . ** if th
17ed5 65 72 65 20 61 72 65 20 66 65 77 65 72 20 74 68 ere are fewer th
17ed6 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f an NN siblings o
17ed7 6e 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 n the other side
17ed8 2e 20 49 66 20 70 50 61 72 65 6e 74 0a 20 20 2a . If pParent. *
17ed9 2a 20 68 61 73 20 4e 42 20 6f 72 20 66 65 77 65 * has NB or fewe
17eda 72 20 63 68 69 6c 64 72 65 6e 20 74 68 65 6e 20 r children then
17edb 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 all children of
17edc 70 50 61 72 65 6e 74 20 61 72 65 20 74 61 6b 65 pParent are take
17edd 6e 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 n. . **. ** T
17ede 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 64 72 his loop also dr
17edf 6f 70 73 20 74 68 65 20 64 69 76 69 64 65 72 20 ops the divider
17ee0 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68 65 20 70 cells from the p
17ee1 61 72 65 6e 74 20 70 61 67 65 2e 20 54 68 69 73 arent page. This
17ee2 0a 20 20 2a 2a 20 77 61 79 2c 20 74 68 65 20 72 . ** way, the r
17ee3 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 65 20 emainder of the
17ee4 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f function does no
17ee5 74 20 68 61 76 65 20 74 6f 20 64 65 61 6c 20 77 t have to deal w
17ee6 69 74 68 20 61 6e 79 0a 20 20 2a 2a 20 6f 76 65 ith any. ** ove
17ee7 72 66 6c 6f 77 20 63 65 6c 6c 73 20 69 6e 20 74 rflow cells in t
17ee8 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2c 20 he parent page,
17ee9 73 69 6e 63 65 20 69 66 20 61 6e 79 20 65 78 69 since if any exi
17eea 73 74 65 64 20 74 68 65 79 20 77 69 6c 6c 0a 20 sted they will.
17eeb 20 2a 2a 20 68 61 76 65 20 61 6c 72 65 61 64 79 ** have already
17eec 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 20 been removed..
17eed 20 2a 2f 0a 20 20 69 20 3d 20 70 50 61 72 65 6e */. i = pParen
17eee 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 2b 20 70 t->nOverflow + p
17eef 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3b 0a 20 Parent->nCell;.
17ef0 20 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 if( i<2 ){.
17ef1 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 20 6e nxDiv = 0;. n
17ef2 4f 6c 64 20 3d 20 69 2b 31 3b 0a 20 20 7d 65 6c Old = i+1;. }el
17ef3 73 65 7b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 33 se{. nOld = 3
17ef4 3b 0a 20 20 20 20 69 66 28 20 69 50 61 72 65 6e ;. if( iParen
17ef5 74 49 64 78 3d 3d 30 20 29 7b 20 20 20 20 20 20 tIdx==0 ){
17ef6 20 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 .
17ef7 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20 20 nxDiv = 0;.
17ef8 20 7d 65 6c 73 65 20 69 66 28 20 69 50 61 72 65 }else if( iPare
17ef9 6e 74 49 64 78 3d 3d 69 20 29 7b 0a 20 20 20 20 ntIdx==i ){.
17efa 20 20 6e 78 44 69 76 20 3d 20 69 2d 32 3b 0a 20 nxDiv = i-2;.
17efb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
17efc 6e 78 44 69 76 20 3d 20 69 50 61 72 65 6e 74 49 nxDiv = iParentI
17efd 64 78 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 dx-1;. }.
17efe 69 20 3d 20 32 3b 0a 20 20 7d 0a 20 20 69 66 28 i = 2;. }. if(
17eff 20 28 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e (i+nxDiv-pParen
17f00 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3d 3d 70 t->nOverflow)==p
17f01 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b Parent->nCell ){
17f02 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 26 70 . pRight = &p
17f03 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 Parent->aData[pP
17f04 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 arent->hdrOffset
17f05 2b 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 +8];. }else{.
17f06 20 20 70 52 69 67 68 74 20 3d 20 66 69 6e 64 43 pRight = findC
17f07 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e ell(pParent, i+n
17f08 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f xDiv-pParent->nO
17f09 76 65 72 66 6c 6f 77 29 3b 0a 20 20 7d 0a 20 20 verflow);. }.
17f0a 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 pgno = get4byte(
17f0b 70 52 69 67 68 74 29 3b 0a 20 20 77 68 69 6c 65 pRight);. while
17f0c 28 20 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ( 1 ){. rc =
17f0d 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 getAndInitPage(p
17f0e 42 74 2c 20 70 67 6e 6f 2c 20 26 61 70 4f 6c 64 Bt, pgno, &apOld
17f0f 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 [i]);. if( rc
17f10 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 ){. memset
17f11 28 61 70 4f 6c 64 2c 20 30 2c 20 28 69 2b 31 29 (apOld, 0, (i+1)
17f12 2a 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 2a *sizeof(MemPage*
17f13 29 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 ));. goto b
17f14 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
17f15 20 20 20 20 7d 0a 20 20 20 20 6e 4d 61 78 43 65 }. nMaxCe
17f16 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 lls += 1+apOld[i
17f17 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 ]->nCell+apOld[i
17f18 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 ]->nOverflow;.
17f19 20 20 69 66 28 20 28 69 2d 2d 29 3d 3d 30 20 29 if( (i--)==0 )
17f1a 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 69 66 28 break;.. if(
17f1b 20 69 2b 6e 78 44 69 76 3d 3d 70 50 61 72 65 6e i+nxDiv==pParen
17f1c 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 t->aOvfl[0].idx
17f1d 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 && pParent->nOve
17f1e 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 61 rflow ){. a
17f1f 70 44 69 76 5b 69 5d 20 3d 20 70 50 61 72 65 6e pDiv[i] = pParen
17f20 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c t->aOvfl[0].pCel
17f21 6c 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 l;. pgno =
17f22 67 65 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 get4byte(apDiv[i
17f23 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b ]);. szNew[
17f24 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 i] = cellSizePtr
17f25 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b (pParent, apDiv[
17f26 69 5d 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 i]);. pPare
17f27 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 nt->nOverflow =
17f28 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 0;. }else{.
17f29 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 66 apDiv[i] = f
17f2a 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c indCell(pParent,
17f2b 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 i+nxDiv-pParent
17f2c 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 ->nOverflow);.
17f2d 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 pgno = get4b
17f2e 79 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 yte(apDiv[i]);.
17f2f 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 szNew[i] =
17f30 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 cellSizePtr(pPar
17f31 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a ent, apDiv[i]);.
17f32 0a 20 20 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 . /* Drop t
17f33 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 he cell from the
17f34 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 61 70 parent page. ap
17f35 44 69 76 5b 69 5d 20 73 74 69 6c 6c 20 70 6f 69 Div[i] still poi
17f36 6e 74 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 nts to. **
17f37 74 68 65 20 63 65 6c 6c 20 77 69 74 68 69 6e 20 the cell within
17f38 74 68 65 20 70 61 72 65 6e 74 2c 20 65 76 65 6e the parent, even
17f39 20 74 68 6f 75 67 68 20 69 74 20 68 61 73 20 62 though it has b
17f3a 65 65 6e 20 64 72 6f 70 70 65 64 2e 0a 20 20 20 een dropped..
17f3b 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 73 61 ** This is sa
17f3c 66 65 20 62 65 63 61 75 73 65 20 64 72 6f 70 70 fe because dropp
17f3d 69 6e 67 20 61 20 63 65 6c 6c 20 6f 6e 6c 79 20 ing a cell only
17f3e 6f 76 65 72 77 72 69 74 65 73 20 74 68 65 20 66 overwrites the f
17f3f 69 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f irst. ** fo
17f40 75 72 20 62 79 74 65 73 20 6f 66 20 69 74 2c 20 ur bytes of it,
17f41 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f and this functio
17f42 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 n does not need
17f43 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 the first.
17f44 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 ** four bytes of
17f45 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c the divider cel
17f46 6c 2e 20 53 6f 20 74 68 65 20 70 6f 69 6e 74 65 l. So the pointe
17f47 72 20 69 73 20 73 61 66 65 20 74 6f 20 75 73 65 r is safe to use
17f48 0a 20 20 20 20 20 20 2a 2a 20 6c 61 74 65 72 20 . ** later
17f49 6f 6e 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 on. . **.
17f4a 20 20 20 20 20 2a 2a 20 55 6e 6c 65 73 73 20 53 ** Unless S
17f4b 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 QLite is compile
17f4c 64 20 69 6e 20 73 65 63 75 72 65 2d 64 65 6c 65 d in secure-dele
17f4d 74 65 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 te mode. In this
17f4e 20 63 61 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20 case,. **
17f4f 74 68 65 20 64 72 6f 70 43 65 6c 6c 28 29 20 72 the dropCell() r
17f50 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 76 65 72 outine will over
17f51 77 72 69 74 65 20 74 68 65 20 65 6e 74 69 72 65 write the entire
17f52 20 63 65 6c 6c 20 77 69 74 68 20 7a 65 72 6f 65 cell with zeroe
17f53 73 2e 0a 20 20 20 20 20 20 2a 2a 20 49 6e 20 74 s.. ** In t
17f54 68 69 73 20 63 61 73 65 2c 20 74 65 6d 70 6f 72 his case, tempor
17f55 61 72 69 6c 79 20 63 6f 70 79 20 74 68 65 20 63 arily copy the c
17f56 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 61 4f 76 ell into the aOv
17f57 66 6c 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 flSpace[].
17f58 2a 2a 20 62 75 66 66 65 72 2e 20 49 74 20 77 69 ** buffer. It wi
17f59 6c 6c 20 62 65 20 63 6f 70 69 65 64 20 6f 75 74 ll be copied out
17f5a 20 61 67 61 69 6e 20 61 73 20 73 6f 6f 6e 20 61 again as soon a
17f5b 73 20 74 68 65 20 61 53 70 61 63 65 5b 5d 20 62 s the aSpace[] b
17f5c 75 66 66 65 72 0a 20 20 20 20 20 20 2a 2a 20 69 uffer. ** i
17f5d 73 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 2a 2f s allocated. */
17f5e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 .#ifdef SQLITE_S
17f5f 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 ECURE_DELETE.
17f60 20 20 20 6d 65 6d 63 70 79 28 26 61 4f 76 66 6c memcpy(&aOvfl
17f61 53 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 Space[apDiv[i]-p
17f62 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 2c 20 Parent->aData],
17f63 61 70 44 69 76 5b 69 5d 2c 20 73 7a 4e 65 77 5b apDiv[i], szNew[
17f64 69 5d 29 3b 0a 20 20 20 20 20 20 61 70 44 69 76 i]);. apDiv
17f65 5b 69 5d 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 [i] = &aOvflSpac
17f66 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 e[apDiv[i]-pPare
17f67 6e 74 2d 3e 61 44 61 74 61 5d 3b 0a 23 65 6e 64 nt->aData];.#end
17f68 69 66 0a 20 20 20 20 20 20 64 72 6f 70 43 65 6c if. dropCel
17f69 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 l(pParent, i+nxD
17f6a 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 iv-pParent->nOve
17f6b 72 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b 69 5d 2c rflow, szNew[i],
17f6c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d &rc);. }. }
17f6d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 .. /* Make nMax
17f6e 43 65 6c 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 Cells a multiple
17f6f 20 6f 66 20 34 20 69 6e 20 6f 72 64 65 72 20 74 of 4 in order t
17f70 6f 20 70 72 65 73 65 72 76 65 20 38 2d 62 79 74 o preserve 8-byt
17f71 65 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 e. ** alignment
17f72 20 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 */. nMaxCells
17f73 3d 20 28 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 = (nMaxCells + 3
17f74 29 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a )&~3;.. /*. **
17f75 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 Allocate space
17f76 66 6f 72 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 for memory struc
17f77 74 75 72 65 73 0a 20 20 2a 2f 0a 20 20 6b 20 3d tures. */. k =
17f78 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b pBt->pageSize +
17f79 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d ROUND8(sizeof(M
17f7a 65 6d 50 61 67 65 29 29 3b 0a 20 20 73 7a 53 63 emPage));. szSc
17f7b 72 61 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e ratch =. n
17f7c 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 MaxCells*sizeof(
17f7d 75 38 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 u8*)
17f7e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 /* ap
17f7f 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e Cell */. + n
17f80 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 MaxCells*sizeof(
17f81 75 31 36 29 20 20 20 20 20 20 20 20 20 20 20 20 u16)
17f82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a /* sz
17f83 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 70 Cell */. + p
17f84 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 20 20 20 Bt->pageSize
17f85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f86 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 /* aS
17f87 70 61 63 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 pace1 */. +
17f88 6b 2a 6e 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 k*nOld;
17f89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17f8a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
17f8b 61 67 65 20 63 6f 70 69 65 73 20 28 61 70 43 6f age copies (apCo
17f8c 70 79 29 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 py) */. apCell
17f8d 3d 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 = sqlite3Scratch
17f8e 4d 61 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 Malloc( szScratc
17f8f 68 20 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 h ); . if( apCe
17f90 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 ll==0 ){. rc
17f91 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a = SQLITE_NOMEM;.
17f92 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 goto balance
17f93 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 _cleanup;. }.
17f94 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 szCell = (u16*)&
17f95 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 apCell[nMaxCells
17f96 5d 3b 0a 20 20 61 53 70 61 63 65 31 20 3d 20 28 ];. aSpace1 = (
17f97 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 u8*)&szCell[nMax
17f98 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 Cells];. assert
17f99 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 ( EIGHT_BYTE_ALI
17f9a 47 4e 4d 45 4e 54 28 61 53 70 61 63 65 31 29 20 GNMENT(aSpace1)
17f9b 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4c 6f );.. /*. ** Lo
17f9c 61 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 ad pointers to a
17f9d 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69 62 6c ll cells on sibl
17f9e 69 6e 67 20 70 61 67 65 73 20 61 6e 64 20 74 68 ing pages and th
17f9f 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a e divider cells.
17fa0 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 6f ** into the lo
17fa1 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61 72 72 cal apCell[] arr
17fa2 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69 65 73 ay. Make copies
17fa3 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20 of the divider
17fa4 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 cells. ** into
17fa5 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 space obtained f
17fa6 72 6f 6d 20 61 53 70 61 63 65 31 5b 5d 20 61 6e rom aSpace1[] an
17fa7 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74 68 65 d remove the the
17fa8 20 64 69 76 69 64 65 72 20 43 65 6c 6c 73 0a 20 divider Cells.
17fa9 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65 6e 74 ** from pParent
17faa 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 .. **. ** If t
17fab 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 he siblings are
17fac 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 2c 20 74 on leaf pages, t
17fad 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70 6f hen the child po
17fae 69 6e 74 65 72 73 20 6f 66 20 74 68 65 0a 20 20 inters of the.
17faf 2a 2a 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 ** divider cells
17fb0 20 61 72 65 20 73 74 72 69 70 70 65 64 20 66 72 are stripped fr
17fb1 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 62 65 66 om the cells bef
17fb2 6f 72 65 20 74 68 65 79 20 61 72 65 20 63 6f 70 ore they are cop
17fb3 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20 61 53 ied. ** into aS
17fb4 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74 68 69 pace1[]. In thi
17fb5 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 s way, all cells
17fb6 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 72 65 in apCell[] are
17fb7 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 63 68 without. ** ch
17fb8 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 49 ild pointers. I
17fb9 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e f siblings are n
17fba 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 ot leaves, then
17fbb 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20 2a 2a all cell in. **
17fbc 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c 75 64 apCell[] includ
17fbd 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 e child pointers
17fbe 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c 20 61 . Either way, a
17fbf 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 ll cells in apCe
17fc0 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20 61 6c ll[]. ** are al
17fc1 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 6c ike.. **. ** l
17fc2 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a 20 20 eafCorrection:
17fc3 34 20 69 66 20 70 50 61 67 65 20 69 73 20 61 20 4 if pPage is a
17fc4 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50 61 67 leaf. 0 if pPag
17fc5 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 2e e is not a leaf.
17fc6 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65 61 66 . ** leaf
17fc7 44 61 74 61 3a 20 20 31 20 69 66 20 70 50 61 67 Data: 1 if pPag
17fc8 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61 74 61 e holds key+data
17fc9 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68 6f 6c and pParent hol
17fca 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a 20 20 ds only keys..
17fcb 2a 2f 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 */. leafCorrect
17fcc 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e ion = apOld[0]->
17fcd 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61 66 44 61 leaf*4;. leafDa
17fce 74 61 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 68 ta = apOld[0]->h
17fcf 61 73 44 61 74 61 3b 0a 20 20 66 6f 72 28 69 3d asData;. for(i=
17fd0 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0; i<nOld; i++){
17fd1 0a 20 20 20 20 69 6e 74 20 6c 69 6d 69 74 3b 0a . int limit;.
17fd2 20 20 20 20 0a 20 20 20 20 2f 2a 20 42 65 66 6f . /* Befo
17fd3 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e re doing anythin
17fd4 67 20 65 6c 73 65 2c 20 74 61 6b 65 20 61 20 63 g else, take a c
17fd5 6f 70 79 20 6f 66 20 74 68 65 20 69 27 74 68 20 opy of the i'th
17fd6 6f 72 69 67 69 6e 61 6c 20 73 69 62 6c 69 6e 67 original sibling
17fd7 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73 74 . ** The rest
17fd8 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f of this functio
17fd9 6e 20 77 69 6c 6c 20 75 73 65 20 64 61 74 61 20 n will use data
17fda 66 72 6f 6d 20 74 68 65 20 63 6f 70 69 65 73 20 from the copies
17fdb 72 61 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 68 rather. ** th
17fdc 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 at the original
17fdd 70 61 67 65 73 20 73 69 6e 63 65 20 74 68 65 20 pages since the
17fde 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 77 original pages w
17fdf 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 0a 20 20 ill be in the.
17fe0 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 ** process of
17fe1 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 being overwritte
17fe2 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 n. */. MemPa
17fe3 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 ge *pOld = apCop
17fe4 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61 67 65 2a y[i] = (MemPage*
17fe5 29 26 61 53 70 61 63 65 31 5b 70 42 74 2d 3e 70 )&aSpace1[pBt->p
17fe6 61 67 65 53 69 7a 65 20 2b 20 6b 2a 69 5d 3b 0a ageSize + k*i];.
17fe7 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2c memcpy(pOld,
17fe8 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65 6f apOld[i], sizeo
17fe9 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 20 f(MemPage));.
17fea 20 70 4f 6c 64 2d 3e 61 44 61 74 61 20 3d 20 28 pOld->aData = (
17feb 76 6f 69 64 2a 29 26 70 4f 6c 64 5b 31 5d 3b 0a void*)&pOld[1];.
17fec 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 6c 64 2d memcpy(pOld-
17fed 3e 61 44 61 74 61 2c 20 61 70 4f 6c 64 5b 69 5d >aData, apOld[i]
17fee 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d 3e 70 61 ->aData, pBt->pa
17fef 67 65 53 69 7a 65 29 3b 0a 0a 20 20 20 20 6c 69 geSize);.. li
17ff0 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c mit = pOld->nCel
17ff1 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f l+pOld->nOverflo
17ff2 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 w;. for(j=0;
17ff3 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29 7b 0a 20 j<limit; j++){.
17ff4 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 assert( nCe
17ff5 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a ll<nMaxCells );.
17ff6 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 apCell[nCe
17ff7 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65 72 66 6c ll] = findOverfl
17ff8 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20 6a 29 3b owCell(pOld, j);
17ff9 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 . szCell[nC
17ffa 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50 ell] = cellSizeP
17ffb 74 72 28 70 4f 6c 64 2c 20 61 70 43 65 6c 6c 5b tr(pOld, apCell[
17ffc 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 20 20 20 6e nCell]);. n
17ffd 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 Cell++;. }.
17ffe 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d 31 20 26 if( i<nOld-1 &
17fff 26 20 21 6c 65 61 66 44 61 74 61 29 7b 0a 20 20 & !leafData){.
18000 20 20 20 20 75 31 36 20 73 7a 20 3d 20 28 75 31 u16 sz = (u1
18001 36 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 6)szNew[i];.
18002 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 u8 *pTemp;.
18003 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c assert( nCell
18004 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 <nMaxCells );.
18005 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c szCell[nCell
18006 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20 20 70 54 ] = sz;. pT
18007 65 6d 70 20 3d 20 26 61 53 70 61 63 65 31 5b 69 emp = &aSpace1[i
18008 53 70 61 63 65 31 5d 3b 0a 20 20 20 20 20 20 69 Space1];. i
18009 53 70 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 Space1 += sz;.
1800a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d assert( sz<=
1800b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 pBt->pageSize/4
1800c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1800d 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d 3e 70 iSpace1<=pBt->p
1800e 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 ageSize );.
1800f 20 6d 65 6d 63 70 79 28 70 54 65 6d 70 2c 20 61 memcpy(pTemp, a
18010 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b 0a 20 20 pDiv[i], sz);.
18011 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c apCell[nCell
18012 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 61 66 43 6f ] = pTemp+leafCo
18013 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 rrection;.
18014 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72 assert( leafCorr
18015 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20 6c 65 61 ection==0 || lea
18016 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 fCorrection==4 )
18017 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e ;. szCell[n
18018 43 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c 6c 5b 6e Cell] = szCell[n
18019 43 65 6c 6c 5d 20 2d 20 6c 65 61 66 43 6f 72 72 Cell] - leafCorr
1801a 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 ection;. if
1801b 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b ( !pOld->leaf ){
1801c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
1801d 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d leafCorrection=
1801e 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 =0 );. as
1801f 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68 64 72 4f sert( pOld->hdrO
18020 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20 20 20 20 ffset==0 );.
18021 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 /* The right
18022 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 pointer of the
18023 63 68 69 6c 64 20 70 61 67 65 20 70 4f 6c 64 20 child page pOld
18024 62 65 63 6f 6d 65 73 20 74 68 65 20 6c 65 66 74 becomes the left
18025 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e . ** poin
18026 74 65 72 20 6f 66 20 74 68 65 20 64 69 76 69 64 ter of the divid
18027 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 er cell */.
18028 20 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c memcpy(apCell
18029 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e [nCell], &pOld->
1802a 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b 0a 20 20 aData[8], 4);.
1802b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1802c 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 assert( leafC
1802d 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a orrection==4 );.
1802e 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 if( szCe
1802f 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29 7b 0a 20 ll[nCell]<4 ){.
18030 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e /* Do n
18031 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20 63 65 6c ot allow any cel
18032 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 ls smaller than
18033 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 20 20 4 bytes. */.
18034 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 szCell[nCe
18035 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 ll] = 4;.
18036 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
18037 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d 0a nCell++;. }.
18038 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 }.. /*. ** F
18039 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 75 igure out the nu
1803a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6e 65 mber of pages ne
1803b 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c 6c eded to hold all
1803c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e 0a 20 20 nCell cells..
1803d 2a 2a 20 53 74 6f 72 65 20 74 68 69 73 20 6e 75 ** Store this nu
1803e 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20 20 41 6c mber in "k". Al
1803f 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a 4e 65 77 so compute szNew
18040 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 [] which is the
18041 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69 7a 65 20 total. ** size
18042 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 of all cells on
18043 74 68 65 20 69 2d 74 68 20 70 61 67 65 20 61 6e the i-th page an
18044 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68 69 63 68 d cntNew[] which
18045 20 69 73 20 74 68 65 20 69 6e 64 65 78 0a 20 20 is the index.
18046 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 6f ** in apCell[] o
18047 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 f the cell that
18048 64 69 76 69 64 65 73 20 70 61 67 65 20 69 20 66 divides page i f
18049 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e 20 20 0a rom page i+1. .
1804a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b 5d 20 73 ** cntNew[k] s
1804b 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e 43 65 6c hould equal nCel
1804c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61 6c l.. **. ** Val
1804d 75 65 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 ues computed by
1804e 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20 20 2a 2a this block:. **
1804f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 . **
18050 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d k: The total num
18051 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67 20 70 ber of sibling p
18052 61 67 65 73 0a 20 20 2a 2a 20 20 20 20 73 7a 4e ages. ** szN
18053 65 77 5b 69 5d 3a 20 53 70 61 63 65 64 20 75 73 ew[i]: Spaced us
18054 65 64 20 6f 6e 20 74 68 65 20 69 2d 74 68 20 73 ed on the i-th s
18055 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a ibling page.. *
18056 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d 3a 20 49 * cntNew[i]: I
18057 6e 64 65 78 20 69 6e 20 61 70 43 65 6c 6c 5b 5d ndex in apCell[]
18058 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d 20 66 6f and szCell[] fo
18059 72 20 74 68 65 20 66 69 72 73 74 20 63 65 6c 6c r the first cell
1805a 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 to. **
1805b 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74 20 the right
1805c 6f 66 20 74 68 65 20 69 2d 74 68 20 73 69 62 6c of the i-th sibl
1805d 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a 2a 20 75 ing page.. ** u
1805e 73 61 62 6c 65 53 70 61 63 65 3a 20 4e 75 6d 62 sableSpace: Numb
1805f 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 er of bytes of s
18060 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65 20 6f pace available o
18061 6e 20 65 61 63 68 20 73 69 62 6c 69 6e 67 2e 0a n each sibling..
18062 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 75 73 61 ** . */. usa
18063 62 6c 65 53 70 61 63 65 20 3d 20 70 42 74 2d 3e bleSpace = pBt->
18064 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 31 32 20 usableSize - 12
18065 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e + leafCorrection
18066 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f 74 61 6c ;. for(subtotal
18067 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b =k=i=0; i<nCell;
18068 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 i++){. asser
18069 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 t( i<nMaxCells )
1806a 3b 0a 20 20 20 20 73 75 62 74 6f 74 61 6c 20 2b ;. subtotal +
1806b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b 20 32 3b = szCell[i] + 2;
1806c 0a 20 20 20 20 69 66 28 20 73 75 62 74 6f 74 61 . if( subtota
1806d 6c 20 3e 20 75 73 61 62 6c 65 53 70 61 63 65 20 l > usableSpace
1806e 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 6b ){. szNew[k
1806f 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20 2d 20 73 ] = subtotal - s
18070 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 zCell[i];.
18071 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69 3b 0a 20 cntNew[k] = i;.
18072 20 20 20 20 20 69 66 28 20 6c 65 61 66 44 61 74 if( leafDat
18073 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20 20 20 20 a ){ i--; }.
18074 20 20 73 75 62 74 6f 74 61 6c 20 3d 20 30 3b 0a subtotal = 0;.
18075 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20 20 20 20 k++;.
18076 20 69 66 28 20 6b 3e 4e 42 2b 31 20 29 7b 20 72 if( k>NB+1 ){ r
18077 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 c = SQLITE_CORRU
18078 50 54 3b 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 PT; goto balance
18079 5f 63 6c 65 61 6e 75 70 3b 20 7d 0a 20 20 20 20 _cleanup; }.
1807a 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77 5b 6b 5d }. }. szNew[k]
1807b 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a 20 20 63 = subtotal;. c
1807c 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43 65 6c 6c ntNew[k] = nCell
1807d 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f 2a 0a 20 ;. k++;.. /*.
1807e 20 2a 2a 20 54 68 65 20 70 61 63 6b 69 6e 67 20 ** The packing
1807f 63 6f 6d 70 75 74 65 64 20 62 79 20 74 68 65 20 computed by the
18080 70 72 65 76 69 6f 75 73 20 62 6c 6f 63 6b 20 69 previous block i
18081 73 20 62 69 61 73 65 64 20 74 6f 77 61 72 64 20 s biased toward
18082 74 68 65 20 73 69 62 6c 69 6e 67 73 0a 20 20 2a the siblings. *
18083 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 73 69 * on the left si
18084 64 65 2e 20 20 54 68 65 20 6c 65 66 74 20 73 69 de. The left si
18085 62 6c 69 6e 67 73 20 61 72 65 20 61 6c 77 61 79 blings are alway
18086 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c 2c 20 77 s nearly full, w
18087 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a 20 72 69 hile the. ** ri
18088 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 ght-most sibling
18089 20 6d 69 67 68 74 20 62 65 20 6e 65 61 72 6c 79 might be nearly
1808a 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20 62 6c empty. This bl
1808b 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61 74 74 65 ock of code atte
1808c 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20 61 64 6a mpts. ** to adj
1808d 75 73 74 20 74 68 65 20 70 61 63 6b 69 6e 67 20 ust the packing
1808e 6f 66 20 73 69 62 6c 69 6e 67 73 20 74 6f 20 67 of siblings to g
1808f 65 74 20 61 20 62 65 74 74 65 72 20 62 61 6c 61 et a better bala
18090 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 nce.. **. ** T
18091 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 his adjustment i
18092 73 20 6d 6f 72 65 20 74 68 61 6e 20 61 6e 20 6f s more than an o
18093 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 ptimization. Th
18094 65 20 70 61 63 6b 69 6e 67 20 61 62 6f 76 65 20 e packing above
18095 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 73 6f might. ** be so
18096 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e 63 65 20 out of balance
18097 61 73 20 74 6f 20 62 65 20 69 6c 6c 65 67 61 6c as to be illegal
18098 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 . For example,
18099 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 the right-most.
1809a 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 ** sibling migh
1809b 74 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 t be completely
1809c 65 6d 70 74 79 2e 20 20 54 68 69 73 20 61 64 6a empty. This adj
1809d 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f 74 20 6f ustment is not o
1809e 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f 0a 20 20 ptional.. */.
1809f 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e 30 3b 20 for(i=k-1; i>0;
180a0 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 73 7a i--){. int sz
180a1 52 69 67 68 74 20 3d 20 73 7a 4e 65 77 5b 69 5d Right = szNew[i]
180a2 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 ; /* Size of si
180a3 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 72 69 67 bling on the rig
180a4 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 7a ht */. int sz
180a5 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b 69 2d 31 Left = szNew[i-1
180a6 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 69 ]; /* Size of si
180a7 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20 6c 65 66 bling on the lef
180a8 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 3b 20 t */. int r;
180a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
180aa 49 6e 64 65 78 20 6f 66 20 72 69 67 68 74 2d 6d Index of right-m
180ab 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c 65 66 74 ost cell in left
180ac 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 20 20 sibling */.
180ad 69 6e 74 20 64 3b 20 20 20 20 20 20 20 20 20 20 int d;
180ae 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
180af 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 20 74 68 first cell to th
180b0 65 20 6c 65 66 74 20 6f 66 20 72 69 67 68 74 20 e left of right
180b1 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 sibling */..
180b2 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 r = cntNew[i-1]
180b3 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20 72 20 2b - 1;. d = r +
180b4 20 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 1 - leafData;.
180b5 20 20 20 61 73 73 65 72 74 28 20 64 3c 6e 4d 61 assert( d<nMa
180b6 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 61 73 xCells );. as
180b7 73 65 72 74 28 20 72 3c 6e 4d 61 78 43 65 6c 6c sert( r<nMaxCell
180b8 73 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 s );. while(
180b9 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 73 7a szRight==0 || sz
180ba 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b 64 5d 2b Right+szCell[d]+
180bb 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a 43 65 6c 2<=szLeft-(szCel
180bc 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20 20 20 20 l[r]+2) ){.
180bd 20 73 7a 52 69 67 68 74 20 2b 3d 20 73 7a 43 65 szRight += szCe
180be 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20 20 20 20 ll[d] + 2;.
180bf 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a 43 65 6c szLeft -= szCel
180c0 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20 20 20 20 l[r] + 2;.
180c1 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d 3b 0a 20 cntNew[i-1]--;.
180c2 20 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b r = cntNew[
180c3 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 20 20 i-1] - 1;.
180c4 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 d = r + 1 - leaf
180c5 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 Data;. }.
180c6 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a 52 69 67 szNew[i] = szRig
180c7 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77 5b 69 2d ht;. szNew[i-
180c8 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a 20 20 7d 1] = szLeft;. }
180c9 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72 20 77 65 .. /* Either we
180ca 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72 20 6d 6f found one or mo
180cb 72 65 20 63 65 6c 6c 73 20 28 63 6e 74 6e 65 77 re cells (cntnew
180cc 5b 30 5d 29 3e 30 29 20 6f 72 20 70 50 61 67 65 [0])>0) or pPage
180cd 20 69 73 0a 20 20 2a 2a 20 61 20 76 69 72 74 75 is. ** a virtu
180ce 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20 20 41 al root page. A
180cf 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 virtual root pa
180d0 67 65 20 69 73 20 77 68 65 6e 20 74 68 65 20 72 ge is when the r
180d1 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a 20 70 61 eal root. ** pa
180d2 67 65 20 69 73 20 70 61 67 65 20 31 20 61 6e 64 ge is page 1 and
180d3 20 77 65 20 61 72 65 20 74 68 65 20 6f 6e 6c 79 we are the only
180d4 20 63 68 69 6c 64 20 6f 66 20 74 68 61 74 20 70 child of that p
180d5 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 age.. */. asse
180d6 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d 3e 30 20 rt( cntNew[0]>0
180d7 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e 70 67 6e || (pParent->pgn
180d8 6f 3d 3d 31 20 26 26 20 70 50 61 72 65 6e 74 2d o==1 && pParent-
180d9 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b 0a 0a 20 >nCell==0) );..
180da 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 TRACE(("BALANCE
180db 3a 20 6f 6c 64 3a 20 25 64 20 25 64 20 25 64 20 : old: %d %d %d
180dc 20 22 2c 0a 20 20 20 20 61 70 4f 6c 64 5b 30 5d ",. apOld[0]
180dd 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 6e 4f 6c ->pgno, . nOl
180de 64 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b 31 5d 2d d>=2 ? apOld[1]-
180df 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20 20 20 20 6e >pgno : 0,. n
180e0 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c 64 5b 32 Old>=3 ? apOld[2
180e1 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20 20 29 29 ]->pgno : 0. ))
180e2 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c ;.. /*. ** All
180e3 6f 63 61 74 65 20 6b 20 6e 65 77 20 70 61 67 65 ocate k new page
180e4 73 2e 20 20 52 65 75 73 65 20 6f 6c 64 20 70 61 s. Reuse old pa
180e5 67 65 73 20 77 68 65 72 65 20 70 6f 73 73 69 62 ges where possib
180e6 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 le.. */. if( a
180e7 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 3c 3d 31 pOld[0]->pgno<=1
180e8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
180e9 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 20 ITE_CORRUPT;.
180ea 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c goto balance_cl
180eb 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 70 61 67 eanup;. }. pag
180ec 65 46 6c 61 67 73 20 3d 20 61 70 4f 6c 64 5b 30 eFlags = apOld[0
180ed 5d 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a 20 20 66 ]->aData[0];. f
180ee 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20 69 2b 2b or(i=0; i<k; i++
180ef 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a ){. MemPage *
180f0 70 4e 65 77 3b 0a 20 20 20 20 69 66 28 20 69 3c pNew;. if( i<
180f1 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 4e nOld ){. pN
180f2 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d 20 3d 20 ew = apNew[i] =
180f3 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20 20 20 20 apOld[i];.
180f4 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 apOld[i] = 0;.
180f5 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
180f6 50 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 2d PagerWrite(pNew-
180f7 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
180f8 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 20 20 69 nNew++;. i
180f9 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c f( rc ) goto bal
180fa 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 ance_cleanup;.
180fb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 }else{. a
180fc 73 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 ssert( i>0 );.
180fd 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 rc = allocat
180fe 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 eBtreePage(pBt,
180ff 26 70 4e 65 77 2c 20 26 70 67 6e 6f 2c 20 70 67 &pNew, &pgno, pg
18100 6e 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 no, 0);. if
18101 28 20 72 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 ( rc ) goto bala
18102 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 nce_cleanup;.
18103 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 70 4e apNew[i] = pN
18104 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b ew;. nNew++
18105 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 ;.. /* Set
18106 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
18107 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65 entry for the ne
18108 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 w sibling page.
18109 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 49 53 41 */. if( ISA
1810a 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 UTOVACUUM ){.
1810b 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 ptrmapPut(p
1810c 42 74 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 Bt, pNew->pgno,
1810d 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 PTRMAP_BTREE, pP
1810e 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 arent->pgno, &rc
1810f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 );. if( r
18110 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c!=SQLITE_OK ){.
18111 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 goto b
18112 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
18113 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
18114 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f }. }. }.. /
18115 2a 20 46 72 65 65 20 61 6e 79 20 6f 6c 64 20 70 * Free any old p
18116 61 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e ages that were n
18117 6f 74 20 72 65 75 73 65 64 20 61 73 20 6e 65 77 ot reused as new
18118 20 70 61 67 65 73 2e 0a 20 20 2a 2f 0a 20 20 77 pages.. */. w
18119 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a hile( i<nOld ){.
1811a 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4f freePage(apO
1811b 6c 64 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 ld[i], &rc);.
1811c 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62 if( rc ) goto b
1811d 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a alance_cleanup;.
1811e 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
1811f 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 61 apOld[i]);. a
18120 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 pOld[i] = 0;.
18121 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a i++;. }.. /*.
18122 20 20 2a 2a 20 50 75 74 20 74 68 65 20 6e 65 77 ** Put the new
18123 20 70 61 67 65 73 20 69 6e 20 61 63 63 65 6e 64 pages in accend
18124 69 6e 67 20 6f 72 64 65 72 2e 20 20 54 68 69 73 ing order. This
18125 20 68 65 6c 70 73 20 74 6f 0a 20 20 2a 2a 20 6b helps to. ** k
18126 65 65 70 20 65 6e 74 72 69 65 73 20 69 6e 20 74 eep entries in t
18127 68 65 20 64 69 73 6b 20 66 69 6c 65 20 69 6e 20 he disk file in
18128 6f 72 64 65 72 20 73 6f 20 74 68 61 74 20 61 20 order so that a
18129 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 scan. ** of the
1812a 20 74 61 62 6c 65 20 69 73 20 61 20 6c 69 6e 65 table is a line
1812b 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 ar scan through
1812c 74 68 65 20 66 69 6c 65 2e 20 20 54 68 61 74 0a the file. That.
1812d 20 20 2a 2a 20 69 6e 20 74 75 72 6e 20 68 65 6c ** in turn hel
1812e 70 73 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 ps the operating
1812f 20 73 79 73 74 65 6d 20 74 6f 20 64 65 6c 69 76 system to deliv
18130 65 72 20 70 61 67 65 73 0a 20 20 2a 2a 20 66 72 er pages. ** fr
18131 6f 6d 20 74 68 65 20 64 69 73 6b 20 6d 6f 72 65 om the disk more
18132 20 72 61 70 69 64 6c 79 2e 0a 20 20 2a 2a 0a 20 rapidly.. **.
18133 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29 20 69 6e ** An O(n^2) in
18134 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 61 6c 67 sertion sort alg
18135 6f 72 69 74 68 6d 20 69 73 20 75 73 65 64 2c 20 orithm is used,
18136 62 75 74 20 73 69 6e 63 65 0a 20 20 2a 2a 20 6e but since. ** n
18137 20 69 73 20 6e 65 76 65 72 20 6d 6f 72 65 20 74 is never more t
18138 68 61 6e 20 4e 42 20 28 61 20 73 6d 61 6c 6c 20 han NB (a small
18139 63 6f 6e 73 74 61 6e 74 29 2c 20 74 68 61 74 20 constant), that
1813a 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e 6f 74 20 should. ** not
1813b 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 be a problem..
1813c 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 4e 42 3d **. ** When NB=
1813d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 20 6f 70 74 =3, this one opt
1813e 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b 65 73 20 imization makes
1813f 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a the database. *
18140 2a 20 61 62 6f 75 74 20 32 35 25 20 66 61 73 74 * about 25% fast
18141 65 72 20 66 6f 72 20 6c 61 72 67 65 20 69 6e 73 er for large ins
18142 65 72 74 69 6f 6e 73 20 61 6e 64 20 64 65 6c 65 ertions and dele
18143 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f tions.. */. fo
18144 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b 20 69 2b r(i=0; i<k-1; i+
18145 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 56 +){. int minV
18146 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e = apNew[i]->pgn
18147 6f 3b 0a 20 20 20 20 69 6e 74 20 6d 69 6e 49 20 o;. int minI
18148 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 = i;. for(j=i
18149 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29 7b 0a 20 +1; j<k; j++){.
1814a 20 20 20 20 20 69 66 28 20 61 70 4e 65 77 5b 6a if( apNew[j
1814b 5d 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69 67 6e 65 ]->pgno<(unsigne
1814c 64 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 d)minV ){.
1814d 20 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 minI = j;.
1814e 20 20 20 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 minV = apNew
1814f 5b 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 [j]->pgno;.
18150 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
18151 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20 20 20 20 minI>i ){.
18152 20 69 6e 74 20 74 3b 0a 20 20 20 20 20 20 4d 65 int t;. Me
18153 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20 20 20 20 mPage *pT;.
18154 20 74 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e 70 t = apNew[i]->p
18155 67 6e 6f 3b 0a 20 20 20 20 20 20 70 54 20 3d 20 gno;. pT =
18156 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 20 20 apNew[i];.
18157 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4e 65 77 apNew[i] = apNew
18158 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20 20 61 70 [minI];. ap
18159 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54 3b 0a New[minI] = pT;.
1815a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52 41 43 }. }. TRAC
1815b 45 28 28 22 6e 65 77 3a 20 25 64 28 25 64 29 20 E(("new: %d(%d)
1815c 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64 %d(%d) %d(%d) %d
1815d 28 25 64 29 20 25 64 28 25 64 29 5c 6e 22 2c 0a (%d) %d(%d)\n",.
1815e 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d 3e 70 67 apNew[0]->pg
1815f 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c 0a 20 20 no, szNew[0],.
18160 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61 70 4e 65 nNew>=2 ? apNe
18161 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 w[1]->pgno : 0,
18162 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b nNew>=2 ? szNew[
18163 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 1] : 0,. nNew
18164 3e 3d 33 20 3f 20 61 70 4e 65 77 5b 32 5d 2d 3e >=3 ? apNew[2]->
18165 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d pgno : 0, nNew>=
18166 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20 3a 20 30 3 ? szNew[2] : 0
18167 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34 20 3f 20 ,. nNew>=4 ?
18168 61 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e 6f 20 3a apNew[3]->pgno :
18169 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f 20 73 7a 0, nNew>=4 ? sz
1816a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20 20 20 20 New[3] : 0,.
1816b 6e 4e 65 77 3e 3d 35 20 3f 20 61 70 4e 65 77 5b nNew>=5 ? apNew[
1816c 34 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 4]->pgno : 0, nN
1816d 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77 5b 34 5d ew>=5 ? szNew[4]
1816e 20 3a 20 30 29 29 3b 0a 0a 20 20 61 73 73 65 72 : 0));.. asser
1816f 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 t( sqlite3PagerI
18170 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 swriteable(pPare
18171 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a nt->pDbPage) );.
18172 20 20 70 75 74 34 62 79 74 65 28 70 52 69 67 68 put4byte(pRigh
18173 74 2c 20 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d t, apNew[nNew-1]
18174 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 ->pgno);.. /*.
18175 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73 74 72 ** Evenly distr
18176 69 62 75 74 65 20 74 68 65 20 64 61 74 61 20 69 ibute the data i
18177 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72 6f 73 n apCell[] acros
18178 73 20 74 68 65 20 6e 65 77 20 70 61 67 65 73 2e s the new pages.
18179 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64 69 76 . ** Insert div
1817a 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f 20 ider cells into
1817b 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63 65 73 pParent as neces
1817c 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a 20 3d sary.. */. j =
1817d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 0;. for(i=0; i
1817e 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nNew; i++){.
1817f 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74 68 65 /* Assemble the
18180 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 new sibling pag
18181 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 e. */. MemPag
18182 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b e *pNew = apNew[
18183 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 i];. assert(
18184 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 j<nMaxCells );.
18185 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 zeroPage(pNew
18186 2c 20 70 61 67 65 46 6c 61 67 73 29 3b 0a 20 20 , pageFlags);.
18187 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 assemblePage(p
18188 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69 5d 2d 6a New, cntNew[i]-j
18189 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 73 , &apCell[j], &s
1818a 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20 20 20 61 zCell[j]);. a
1818b 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 65 ssert( pNew->nCe
1818c 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77 3d 3d 31 ll>0 || (nNew==1
1818d 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d 3d 3d 30 && cntNew[0]==0
1818e 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ) );. assert(
1818f 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66 6c 6f 77 pNew->nOverflow
18190 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 6a 20 3d 20 ==0 );.. j =
18191 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20 20 20 20 cntNew[i];..
18192 2f 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e /* If the siblin
18193 67 20 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 g page assembled
18194 20 61 62 6f 76 65 20 77 61 73 20 6e 6f 74 20 74 above was not t
18195 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 he right-most si
18196 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a 20 69 6e bling,. ** in
18197 73 65 72 74 20 61 20 64 69 76 69 64 65 72 20 63 sert a divider c
18198 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20 70 61 72 ell into the par
18199 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f ent page.. */
1819a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e . assert( i<n
1819b 4e 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e 43 65 6c New-1 || j==nCel
1819c 6c 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3c 6e l );. if( j<n
1819d 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 Cell ){. u8
1819e 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 *pCell;. u
1819f 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 8 *pTemp;.
181a0 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 int sz;.. a
181a1 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c ssert( j<nMaxCel
181a2 6c 73 20 29 3b 0a 20 20 20 20 20 20 70 43 65 6c ls );. pCel
181a3 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 l = apCell[j];.
181a4 20 20 20 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c sz = szCell
181a5 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 [j] + leafCorrec
181a6 74 69 6f 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d tion;. pTem
181a7 70 20 3d 20 26 61 4f 76 66 6c 53 70 61 63 65 5b p = &aOvflSpace[
181a8 69 4f 76 66 6c 53 70 61 63 65 5d 3b 0a 20 20 20 iOvflSpace];.
181a9 20 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 6c 65 if( !pNew->le
181aa 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 af ){. me
181ab 6d 63 70 79 28 26 70 4e 65 77 2d 3e 61 44 61 74 mcpy(&pNew->aDat
181ac 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20 34 29 3b a[8], pCell, 4);
181ad 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
181ae 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20 leafData ){.
181af 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 /* If the t
181b0 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61 ree is a leaf-da
181b1 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65 ta tree, and the
181b2 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65 siblings are le
181b3 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a aves, . *
181b4 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 * then there is
181b5 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 no divider cell
181b6 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73 in apCell[]. Ins
181b7 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65 tead, the divide
181b8 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 r . ** ce
181b9 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 ll consists of t
181ba 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66 he integer key f
181bb 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 or the right-mos
181bc 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20 t cell of .
181bd 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e ** the siblin
181be 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64 g-page assembled
181bf 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20 above only..
181c0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
181c1 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 CellInfo info;.
181c2 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20 j--;.
181c3 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65 btreeParseCe
181c4 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65 llPtr(pNew, apCe
181c5 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 ll[j], &info);.
181c6 20 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 pCell = p
181c7 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 73 7a Temp;. sz
181c8 20 3d 20 34 20 2b 20 70 75 74 56 61 72 69 6e 74 = 4 + putVarint
181c9 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 69 6e 66 6f (&pCell[4], info
181ca 2e 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 .nKey);.
181cb 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20 20 20 20 pTemp = 0;.
181cc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
181cd 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20 20 20 20 pCell -= 4;.
181ce 20 20 20 20 2f 2a 20 4f 62 73 63 75 72 65 20 63 /* Obscure c
181cf 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c 65 61 66 ase for non-leaf
181d0 2d 64 61 74 61 20 74 72 65 65 73 3a 20 49 66 20 -data trees: If
181d1 74 68 65 20 63 65 6c 6c 20 61 74 20 70 43 65 6c the cell at pCel
181d2 6c 20 77 61 73 0a 20 20 20 20 20 20 20 20 2a 2a l was. **
181d3 20 70 72 65 76 69 6f 75 73 6c 79 20 73 74 6f 72 previously stor
181d4 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 6e 6f 64 ed on a leaf nod
181d5 65 2c 20 61 6e 64 20 69 74 73 20 72 65 70 6f 72 e, and its repor
181d6 74 65 64 20 73 69 7a 65 20 77 61 73 20 34 0a 20 ted size was 4.
181d7 20 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73 2c ** bytes,
181d8 20 74 68 65 6e 20 69 74 20 6d 61 79 20 61 63 74 then it may act
181d9 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c 6c 65 72 ually be smaller
181da 20 74 68 61 6e 20 74 68 69 73 20 0a 20 20 20 20 than this .
181db 20 20 20 20 2a 2a 20 28 73 65 65 20 62 74 72 65 ** (see btre
181dc 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c eParseCellPtr(),
181dd 20 34 20 62 79 74 65 73 20 69 73 20 74 68 65 20 4 bytes is the
181de 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20 6f 66 0a minimum size of.
181df 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 63 ** any c
181e0 65 6c 6c 29 2e 20 42 75 74 20 69 74 20 69 73 20 ell). But it is
181e1 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 70 61 73 important to pas
181e2 73 20 74 68 65 20 63 6f 72 72 65 63 74 20 73 69 s the correct si
181e3 7a 65 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a ze to . *
181e4 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 2c 20 * insertCell(),
181e5 73 6f 20 72 65 70 61 72 73 65 20 74 68 65 20 63 so reparse the c
181e6 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20 20 20 20 ell now..
181e7 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e **. ** N
181e8 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 63 61 ote that this ca
181e9 6e 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 69 n never happen i
181ea 6e 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 n an SQLite data
181eb 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c 0a 20 20 file, as all.
181ec 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c 73 20 61 ** cells a
181ed 72 65 20 61 74 20 6c 65 61 73 74 20 34 20 62 79 re at least 4 by
181ee 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20 68 61 70 tes. It only hap
181ef 70 65 6e 73 20 69 6e 20 62 2d 74 72 65 65 73 20 pens in b-trees
181f0 75 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 used. **
181f1 74 6f 20 65 76 61 6c 75 61 74 65 20 22 49 4e 20 to evaluate "IN
181f2 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 61 6e (SELECT ...)" an
181f3 64 20 73 69 6d 69 6c 61 72 20 63 6c 61 75 73 65 d similar clause
181f4 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 s.. */.
181f5 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 6c 6c if( szCell
181f6 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20 20 20 20 [j]==4 ){.
181f7 20 20 20 20 61 73 73 65 72 74 28 6c 65 61 66 43 assert(leafC
181f8 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 orrection==4);.
181f9 20 20 20 20 20 20 20 20 20 73 7a 20 3d 20 63 65 sz = ce
181fa 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e llSizePtr(pParen
181fb 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 t, pCell);.
181fc 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
181fd 20 20 20 69 4f 76 66 6c 53 70 61 63 65 20 2b 3d iOvflSpace +=
181fe 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 sz;. asser
181ff 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 t( sz<=pBt->page
18200 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20 Size/4 );.
18201 61 73 73 65 72 74 28 20 69 4f 76 66 6c 53 70 61 assert( iOvflSpa
18202 63 65 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a ce<=pBt->pageSiz
18203 65 20 29 3b 0a 20 20 20 20 20 20 69 6e 73 65 72 e );. inser
18204 74 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e tCell(pParent, n
18205 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20 73 7a 2c xDiv, pCell, sz,
18206 20 70 54 65 6d 70 2c 20 70 4e 65 77 2d 3e 70 67 pTemp, pNew->pg
18207 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 no, &rc);.
18208 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
18209 4b 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 K ) goto balance
1820a 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 _cleanup;.
1820b 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 assert( sqlite3P
1820c 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 agerIswriteable(
1820d 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 pParent->pDbPage
1820e 29 20 29 3b 0a 0a 20 20 20 20 20 20 6a 2b 2b 3b ) );.. j++;
1820f 0a 20 20 20 20 20 20 6e 78 44 69 76 2b 2b 3b 0a . nxDiv++;.
18210 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 }. }. asse
18211 72 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a rt( j==nCell );.
18212 20 20 61 73 73 65 72 74 28 20 6e 4f 6c 64 3e 30 assert( nOld>0
18213 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4e );. assert( nN
18214 65 77 3e 30 20 29 3b 0a 20 20 69 66 28 20 28 70 ew>0 );. if( (p
18215 61 67 65 46 6c 61 67 73 20 26 20 50 54 46 5f 4c ageFlags & PTF_L
18216 45 41 46 29 3d 3d 30 20 29 7b 0a 20 20 20 20 75 EAF)==0 ){. u
18217 38 20 2a 7a 43 68 69 6c 64 20 3d 20 26 61 70 43 8 *zChild = &apC
18218 6f 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e 61 44 61 opy[nOld-1]->aDa
18219 74 61 5b 38 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 ta[8];. memcp
1821a 79 28 26 61 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d y(&apNew[nNew-1]
1821b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 7a 43 68 69 ->aData[8], zChi
1821c 6c 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a 20 20 69 ld, 4);. }.. i
1821d 66 28 20 69 73 52 6f 6f 74 20 26 26 20 70 50 61 f( isRoot && pPa
1821e 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 rent->nCell==0 &
1821f 26 20 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 & pParent->hdrOf
18220 66 73 65 74 3c 3d 61 70 4e 65 77 5b 30 5d 2d 3e fset<=apNew[0]->
18221 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 2f 2a 20 nFree ){. /*
18222 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 The root page of
18223 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 77 20 the b-tree now
18224 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c contains no cell
18225 73 2e 20 54 68 65 20 6f 6e 6c 79 20 73 69 62 6c s. The only sibl
18226 69 6e 67 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 ing. ** page
18227 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 is the right-chi
18228 6c 64 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 ld of the parent
18229 2e 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 . Copy the conte
1822a 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a nts of the. *
1822b 2a 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 74 * child page int
1822c 6f 20 74 68 65 20 70 61 72 65 6e 74 2c 20 64 65 o the parent, de
1822d 63 72 65 61 73 69 6e 67 20 74 68 65 20 6f 76 65 creasing the ove
1822e 72 61 6c 6c 20 68 65 69 67 68 74 20 6f 66 20 74 rall height of t
1822f 68 65 0a 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 he. ** b-tree
18230 20 73 74 72 75 63 74 75 72 65 20 62 79 20 6f 6e structure by on
18231 65 2e 20 54 68 69 73 20 69 73 20 64 65 73 63 72 e. This is descr
18232 69 62 65 64 20 61 73 20 74 68 65 20 22 62 61 6c ibed as the "bal
18233 61 6e 63 65 2d 73 68 61 6c 6c 6f 77 65 72 22 0a ance-shallower".
18234 20 20 20 20 2a 2a 20 73 75 62 2d 61 6c 67 6f 72 ** sub-algor
18235 69 74 68 6d 20 69 6e 20 73 6f 6d 65 20 64 6f 63 ithm in some doc
18236 75 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20 20 20 20 umentation..
18237 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 **. ** If thi
18238 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 s is an auto-vac
18239 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 74 68 uum database, th
1823a 65 20 63 61 6c 6c 20 74 6f 20 63 6f 70 79 4e 6f e call to copyNo
1823b 64 65 43 6f 6e 74 65 6e 74 28 29 20 0a 20 20 20 deContent() .
1823c 20 2a 2a 20 73 65 74 73 20 61 6c 6c 20 70 6f 69 ** sets all poi
1823d 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 nter-map entries
1823e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 corresponding t
1823f 6f 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 o database image
18240 20 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 66 pages . ** f
18241 6f 72 20 77 68 69 63 68 20 74 68 65 20 70 6f 69 or which the poi
18242 6e 74 65 72 20 69 73 20 73 74 6f 72 65 64 20 77 nter is stored w
18243 69 74 68 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e ithin the conten
18244 74 20 62 65 69 6e 67 20 63 6f 70 69 65 64 2e 0a t being copied..
18245 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
18246 65 20 73 65 63 6f 6e 64 20 61 73 73 65 72 74 20 e second assert
18247 62 65 6c 6f 77 20 76 65 72 69 66 69 65 73 20 74 below verifies t
18248 68 61 74 20 74 68 65 20 63 68 69 6c 64 20 70 61 hat the child pa
18249 67 65 20 69 73 20 64 65 66 72 61 67 6d 65 6e 74 ge is defragment
1824a 65 64 0a 20 20 20 20 2a 2a 20 28 69 74 20 6d 75 ed. ** (it mu
1824b 73 74 20 62 65 2c 20 61 73 20 69 74 20 77 61 73 st be, as it was
1824c 20 6a 75 73 74 20 72 65 63 6f 6e 73 74 72 75 63 just reconstruc
1824d 74 65 64 20 75 73 69 6e 67 20 61 73 73 65 6d 62 ted using assemb
1824e 6c 65 50 61 67 65 28 29 29 2e 20 54 68 69 73 0a lePage()). This.
1824f 20 20 20 20 2a 2a 20 69 73 20 69 6d 70 6f 72 74 ** is import
18250 61 6e 74 20 69 66 20 74 68 65 20 70 61 72 65 6e ant if the paren
18251 74 20 70 61 67 65 20 68 61 70 70 65 6e 73 20 74 t page happens t
18252 6f 20 62 65 20 70 61 67 65 20 31 20 6f 66 20 74 o be page 1 of t
18253 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 he database.
18254 2a 2a 20 69 6d 61 67 65 2e 20 20 2a 2f 0a 20 20 ** image. */.
18255 20 20 61 73 73 65 72 74 28 20 6e 4e 65 77 3d 3d assert( nNew==
18256 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 1 );. assert(
18257 20 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 apNew[0]->nFree
18258 20 3d 3d 20 0a 20 20 20 20 20 20 20 20 28 67 65 == . (ge
18259 74 32 62 79 74 65 28 26 61 70 4e 65 77 5b 30 5d t2byte(&apNew[0]
1825a 2d 3e 61 44 61 74 61 5b 35 5d 29 2d 61 70 4e 65 ->aData[5])-apNe
1825b 77 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 w[0]->cellOffset
1825c 2d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43 65 6c 6c -apNew[0]->nCell
1825d 2a 32 29 20 0a 20 20 20 20 29 3b 0a 20 20 20 20 *2) . );.
1825e 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 copyNodeContent(
1825f 61 70 4e 65 77 5b 30 5d 2c 20 70 50 61 72 65 6e apNew[0], pParen
18260 74 2c 20 26 72 63 29 3b 0a 20 20 20 20 66 72 65 t, &rc);. fre
18261 65 50 61 67 65 28 61 70 4e 65 77 5b 30 5d 2c 20 ePage(apNew[0],
18262 26 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 &rc);. }else if
18263 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 ( ISAUTOVACUUM )
18264 7b 0a 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 {. /* Fix the
18265 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 pointer-map ent
18266 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65 ries for all the
18267 20 63 65 6c 6c 73 20 74 68 61 74 20 77 65 72 65 cells that were
18268 20 73 68 69 66 74 65 64 20 61 72 6f 75 6e 64 2e shifted around.
18269 20 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61 . ** There a
1826a 72 65 20 73 65 76 65 72 61 6c 20 64 69 66 66 65 re several diffe
1826b 72 65 6e 74 20 74 79 70 65 73 20 6f 66 20 70 6f rent types of po
1826c 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 inter-map entrie
1826d 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a 20 s that need to.
1826e 20 20 20 2a 2a 20 62 65 20 64 65 61 6c 74 20 77 ** be dealt w
1826f 69 74 68 20 62 79 20 74 68 69 73 20 72 6f 75 74 ith by this rout
18270 69 6e 65 2e 20 53 6f 6d 65 20 6f 66 20 74 68 65 ine. Some of the
18271 73 65 20 68 61 76 65 20 62 65 65 6e 20 73 65 74 se have been set
18272 20 61 6c 72 65 61 64 79 2c 20 62 75 74 0a 20 20 already, but.
18273 20 20 2a 2a 20 6d 61 6e 79 20 68 61 76 65 20 6e ** many have n
18274 6f 74 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e ot. The followin
18275 67 20 69 73 20 61 20 73 75 6d 6d 61 72 79 3a 0a g is a summary:.
18276 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
18277 31 29 20 54 68 65 20 65 6e 74 72 69 65 73 20 61 1) The entries a
18278 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e ssociated with n
18279 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 ew sibling pages
1827a 20 74 68 61 74 20 77 65 72 65 20 6e 6f 74 0a 20 that were not.
1827b 20 20 20 2a 2a 20 20 20 20 20 20 73 69 62 6c 69 ** sibli
1827c 6e 67 73 20 77 68 65 6e 20 74 68 69 73 20 66 75 ngs when this fu
1827d 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 nction was calle
1827e 64 2e 20 54 68 65 73 65 20 68 61 76 65 20 61 6c d. These have al
1827f 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 20 20 20 ready. **
18280 20 20 62 65 65 6e 20 73 65 74 2e 20 57 65 20 64 been set. We d
18281 6f 6e 27 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 on't need to wor
18282 72 79 20 61 62 6f 75 74 20 6f 6c 64 20 73 69 62 ry about old sib
18283 6c 69 6e 67 73 20 74 68 61 74 20 77 65 72 65 0a lings that were.
18284 20 20 20 20 2a 2a 20 20 20 20 20 20 6d 6f 76 65 ** move
18285 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 d to the free-li
18286 73 74 20 2d 20 74 68 65 20 66 72 65 65 50 61 67 st - the freePag
18287 65 28 29 20 63 6f 64 65 20 68 61 73 20 74 61 6b e() code has tak
18288 65 6e 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 20 en care. **
18289 20 20 20 20 6f 66 20 74 68 6f 73 65 2e 0a 20 20 of those..
1828a 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 32 29 **. ** 2)
1828b 20 54 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 The pointer-map
1828c 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61 entries associa
1828d 74 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 ted with the fir
1828e 73 74 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 st overflow.
1828f 2a 2a 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 ** page in
18290 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 any overflow cha
18291 69 6e 73 20 75 73 65 64 20 62 79 20 6e 65 77 20 ins used by new
18292 64 69 76 69 64 65 72 20 63 65 6c 6c 73 2e 20 54 divider cells. T
18293 68 65 73 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 hese . **
18294 20 20 68 61 76 65 20 61 6c 73 6f 20 61 6c 72 65 have also alre
18295 61 64 79 20 62 65 65 6e 20 74 61 6b 65 6e 20 63 ady been taken c
18296 61 72 65 20 6f 66 20 62 79 20 74 68 65 20 69 6e are of by the in
18297 73 65 72 74 43 65 6c 6c 28 29 20 63 6f 64 65 2e sertCell() code.
18298 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
18299 20 33 29 20 49 66 20 74 68 65 20 73 69 62 6c 69 3) If the sibli
1829a 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74 ng pages are not
1829b 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74 68 leaves, then th
1829c 65 20 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 e child pages of
1829d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 63 65 6c . ** cel
1829e 6c 73 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 ls stored on the
1829f 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 6d sibling pages m
182a0 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 ay need to be up
182a1 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 dated.. **.
182a2 20 20 2a 2a 20 20 20 34 29 20 49 66 20 74 68 65 ** 4) If the
182a3 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 sibling pages a
182a4 72 65 20 6e 6f 74 20 69 6e 74 65 72 6e 61 6c 20 re not internal
182a5 69 6e 74 6b 65 79 20 6e 6f 64 65 73 2c 20 74 68 intkey nodes, th
182a6 65 6e 20 61 6e 79 0a 20 20 20 20 2a 2a 20 20 20 en any. **
182a7 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 overflow page
182a8 73 20 75 73 65 64 20 62 79 20 74 68 65 73 65 20 s used by these
182a9 63 65 6c 6c 73 20 6d 61 79 20 6e 65 65 64 20 74 cells may need t
182aa 6f 20 62 65 20 75 70 64 61 74 65 64 0a 20 20 20 o be updated.
182ab 20 2a 2a 20 20 20 20 20 20 28 69 6e 74 65 72 6e ** (intern
182ac 61 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 20 al intkey nodes
182ad 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 70 6f never contain po
182ae 69 6e 74 65 72 73 20 74 6f 20 6f 76 65 72 66 6c inters to overfl
182af 6f 77 20 70 61 67 65 73 29 2e 0a 20 20 20 20 2a ow pages).. *
182b0 2a 0a 20 20 20 20 2a 2a 20 20 20 35 29 20 49 66 *. ** 5) If
182b1 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 the sibling pag
182b2 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 es are not leave
182b3 73 2c 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e s, then the poin
182b4 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 20 ter-map. **
182b5 20 20 20 20 65 6e 74 72 69 65 73 20 66 6f 72 20 entries for
182b6 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 the right-child
182b7 70 61 67 65 73 20 6f 66 20 65 61 63 68 20 73 69 pages of each si
182b8 62 6c 69 6e 67 20 6d 61 79 20 6e 65 65 64 0a 20 bling may need.
182b9 20 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 62 65 ** to be
182ba 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a updated.. **
182bb 0a 20 20 20 20 2a 2a 20 43 61 73 65 73 20 31 20 . ** Cases 1
182bc 61 6e 64 20 32 20 61 72 65 20 64 65 61 6c 74 20 and 2 are dealt
182bd 77 69 74 68 20 61 62 6f 76 65 20 62 79 20 6f 74 with above by ot
182be 68 65 72 20 63 6f 64 65 2e 20 54 68 65 20 6e 65 her code. The ne
182bf 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 xt. ** block
182c0 64 65 61 6c 73 20 77 69 74 68 20 63 61 73 65 73 deals with cases
182c1 20 33 20 61 6e 64 20 34 20 61 6e 64 20 74 68 65 3 and 4 and the
182c2 20 6f 6e 65 20 61 66 74 65 72 20 74 68 61 74 2c one after that,
182c3 20 63 61 73 65 20 35 2e 20 53 69 6e 63 65 0a 20 case 5. Since.
182c4 20 20 20 2a 2a 20 73 65 74 74 69 6e 67 20 61 20 ** setting a
182c5 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 pointer map entr
182c6 79 20 69 73 20 61 20 72 65 6c 61 74 69 76 65 6c y is a relativel
182c7 79 20 65 78 70 65 6e 73 69 76 65 20 6f 70 65 72 y expensive oper
182c8 61 74 69 6f 6e 2c 20 74 68 69 73 0a 20 20 20 20 ation, this.
182c9 2a 2a 20 63 6f 64 65 20 6f 6e 6c 79 20 73 65 74 ** code only set
182ca 73 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e s pointer map en
182cb 74 72 69 65 73 20 66 6f 72 20 63 68 69 6c 64 20 tries for child
182cc 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 or overflow page
182cd 73 20 74 68 61 74 20 68 61 76 65 0a 20 20 20 20 s that have.
182ce 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6d 6f 76 65 ** actually move
182cf 64 20 62 65 74 77 65 65 6e 20 70 61 67 65 73 2e d between pages.
182d0 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 */. MemPage
182d1 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 30 *pNew = apNew[0
182d2 5d 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a ];. MemPage *
182d3 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 30 5d pOld = apCopy[0]
182d4 3b 0a 20 20 20 20 69 6e 74 20 6e 4f 76 65 72 66 ;. int nOverf
182d5 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 low = pOld->nOve
182d6 72 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 rflow;. int i
182d7 4e 65 78 74 4f 6c 64 20 3d 20 70 4f 6c 64 2d 3e NextOld = pOld->
182d8 6e 43 65 6c 6c 20 2b 20 6e 4f 76 65 72 66 6c 6f nCell + nOverflo
182d9 77 3b 0a 20 20 20 20 69 6e 74 20 69 4f 76 65 72 w;. int iOver
182da 66 6c 6f 77 20 3d 20 28 6e 4f 76 65 72 66 6c 6f flow = (nOverflo
182db 77 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b w ? pOld->aOvfl[
182dc 30 5d 2e 69 64 78 20 3a 20 2d 31 29 3b 0a 20 20 0].idx : -1);.
182dd 20 20 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 j = 0;
182de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182df 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
182e0 27 6f 6c 64 27 20 73 69 62 6c 69 6e 67 20 70 61 'old' sibling pa
182e1 67 65 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b ge */. k = 0;
182e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
182e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
182e4 43 75 72 72 65 6e 74 20 27 6e 65 77 27 20 73 69 Current 'new' si
182e5 62 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 bling page */.
182e6 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 for(i=0; i<nCe
182e7 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 ll; i++){.
182e8 69 6e 74 20 69 73 44 69 76 69 64 65 72 20 3d 20 int isDivider =
182e9 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 0;. while(
182ea 69 3d 3d 69 4e 65 78 74 4f 6c 64 20 29 7b 0a 20 i==iNextOld ){.
182eb 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69 /* Cell i
182ec 20 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d is the cell imm
182ed 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 ediately followi
182ee 6e 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c ng the last cell
182ef 20 6f 6e 20 6f 6c 64 0a 20 20 20 20 20 20 20 20 on old.
182f0 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 ** sibling page
182f1 6a 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e j. If the siblin
182f2 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20 gs are not leaf
182f3 70 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20 pages of an.
182f4 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d ** intkey b-
182f5 74 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20 tree, then cell
182f6 69 20 77 61 73 20 61 20 64 69 76 69 64 65 72 20 i was a divider
182f7 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 cell. */.
182f8 20 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79 5b 2b pOld = apCopy[+
182f9 2b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 +j];. iNe
182fa 78 74 4f 6c 64 20 3d 20 69 20 2b 20 21 6c 65 61 xtOld = i + !lea
182fb 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 6e 43 fData + pOld->nC
182fc 65 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e 4f 76 65 ell + pOld->nOve
182fd 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 69 rflow;. i
182fe 66 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c f( pOld->nOverfl
182ff 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ow ){.
18300 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 nOverflow = pOld
18301 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 ->nOverflow;.
18302 20 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 iOverflow
18303 20 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 = i + !leafData
18304 20 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 + pOld->aOvfl[0
18305 5d 2e 69 64 78 3b 0a 20 20 20 20 20 20 20 20 7d ].idx;. }
18306 0a 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64 . isDivid
18307 65 72 20 3d 20 21 6c 65 61 66 44 61 74 61 3b 20 er = !leafData;
18308 20 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 . }..
18309 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f assert(nOverflo
1830a 77 3e 30 20 7c 7c 20 69 4f 76 65 72 66 6c 6f 77 w>0 || iOverflow
1830b 3c 69 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 <i );. asse
1830c 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 32 20 7c rt(nOverflow<2 |
1830d 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d | pOld->aOvfl[0]
1830e 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 .idx==pOld->aOvf
1830f 6c 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 l[1].idx-1);.
18310 20 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66 assert(nOverf
18311 6c 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64 2d 3e 61 low<3 || pOld->a
18312 4f 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d 70 4f 6c Ovfl[1].idx==pOl
18313 64 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69 64 78 2d d->aOvfl[2].idx-
18314 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 1);. if( i=
18315 3d 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 =iOverflow ){.
18316 20 20 20 20 20 20 69 73 44 69 76 69 64 65 72 20 isDivider
18317 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 = 1;. if(
18318 20 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77 29 3e 30 (--nOverflow)>0
18319 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 4f ){. iO
1831a 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 20 verflow++;.
1831b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 }. }..
1831c 20 20 20 20 69 66 28 20 69 3d 3d 63 6e 74 4e 65 if( i==cntNe
1831d 77 5b 6b 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 w[k] ){.
1831e 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 /* Cell i is the
1831f 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c cell immediatel
18320 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 y following the
18321 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6e 65 77 last cell on new
18322 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c . ** sibl
18323 69 6e 67 20 70 61 67 65 20 6b 2e 20 49 66 20 74 ing page k. If t
18324 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 he siblings are
18325 6e 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f not leaf pages o
18326 66 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 f an. **
18327 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 intkey b-tree, t
18328 68 65 6e 20 63 65 6c 6c 20 69 20 69 73 20 61 20 hen cell i is a
18329 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 20 2a divider cell. *
1832a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d /. pNew =
1832b 20 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a 20 20 20 apNew[++k];.
1832c 20 20 20 20 20 69 66 28 20 21 6c 65 61 66 44 61 if( !leafDa
1832d 74 61 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 ta ) continue;.
1832e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 }. ass
1832f 65 72 74 28 20 6a 3c 6e 4f 6c 64 20 29 3b 0a 20 ert( j<nOld );.
18330 20 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e assert( k<n
18331 4e 65 77 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a New );.. /*
18332 20 49 66 20 74 68 65 20 63 65 6c 6c 20 77 61 73 If the cell was
18333 20 6f 72 69 67 69 6e 61 6c 6c 79 20 64 69 76 69 originally divi
18334 64 65 72 20 63 65 6c 6c 20 28 61 6e 64 20 69 73 der cell (and is
18335 20 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a 20 20 20 not now) or.
18336 20 20 20 2a 2a 20 61 6e 20 6f 76 65 72 66 6c 6f ** an overflo
18337 77 20 63 65 6c 6c 2c 20 6f 72 20 69 66 20 74 68 w cell, or if th
18338 65 20 63 65 6c 6c 20 77 61 73 20 6c 6f 63 61 74 e cell was locat
18339 65 64 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e ed on a differen
1833a 74 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 20 20 t sibling.
1833b 2a 2a 20 70 61 67 65 20 62 65 66 6f 72 65 20 74 ** page before t
1833c 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 he balancing, th
1833d 65 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d en the pointer m
1833e 61 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 ap entries assoc
1833f 69 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 iated. ** w
18340 69 74 68 20 61 6e 79 20 63 68 69 6c 64 20 6f 72 ith any child or
18341 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 overflow pages
18342 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 need to be updat
18343 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 ed. */. if
18344 28 20 69 73 44 69 76 69 64 65 72 20 7c 7c 20 70 ( isDivider || p
18345 4f 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d Old->pgno!=pNew-
18346 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 >pgno ){.
18347 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63 if( !leafCorrec
18348 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 tion ){.
18349 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c ptrmapPut(pBt,
1834a 20 67 65 74 34 62 79 74 65 28 61 70 43 65 6c 6c get4byte(apCell
1834b 5b 69 5d 29 2c 20 50 54 52 4d 41 50 5f 42 54 52 [i]), PTRMAP_BTR
1834c 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 EE, pNew->pgno,
1834d 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a &rc);. }.
1834e 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43 65 if( szCe
1834f 6c 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d 69 6e 4c ll[i]>pNew->minL
18350 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 ocal ){.
18351 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 ptrmapPutOvflP
18352 74 72 28 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b tr(pNew, apCell[
18353 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 i], &rc);.
18354 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
18355 7d 0a 0a 20 20 20 20 69 66 28 20 21 6c 65 61 66 }.. if( !leaf
18356 43 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 Correction ){.
18357 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
18358 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 New; i++){.
18359 20 20 20 75 33 32 20 6b 65 79 20 3d 20 67 65 74 u32 key = get
1835a 34 62 79 74 65 28 26 61 70 4e 65 77 5b 69 5d 2d 4byte(&apNew[i]-
1835b 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20 >aData[8]);.
1835c 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 ptrmapPut(pB
1835d 74 2c 20 6b 65 79 2c 20 50 54 52 4d 41 50 5f 42 t, key, PTRMAP_B
1835e 54 52 45 45 2c 20 61 70 4e 65 77 5b 69 5d 2d 3e TREE, apNew[i]->
1835f 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 pgno, &rc);.
18360 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30 }. }..#if 0
18361 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 74 72 6d . /* The ptrm
18362 61 70 43 68 65 63 6b 50 61 67 65 73 28 29 20 63 apCheckPages() c
18363 6f 6e 74 61 69 6e 73 20 61 73 73 65 72 74 28 29 ontains assert()
18364 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 statements that
18365 20 76 65 72 69 66 79 20 74 68 61 74 0a 20 20 20 verify that.
18366 20 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74 65 72 20 ** all pointer
18367 6d 61 70 20 70 61 67 65 73 20 61 72 65 20 73 65 map pages are se
18368 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 54 68 69 t correctly. Thi
18369 73 20 69 73 20 68 65 6c 70 66 75 6c 20 77 68 69 s is helpful whi
1836a 6c 65 20 0a 20 20 20 20 2a 2a 20 64 65 62 75 67 le . ** debug
1836b 67 69 6e 67 2e 20 54 68 69 73 20 69 73 20 75 73 ging. This is us
1836c 75 61 6c 6c 79 20 64 69 73 61 62 6c 65 64 20 62 ually disabled b
1836d 65 63 61 75 73 65 20 61 20 63 6f 72 72 75 70 74 ecause a corrupt
1836e 20 64 61 74 61 62 61 73 65 20 6d 61 79 0a 20 20 database may.
1836f 20 20 2a 2a 20 63 61 75 73 65 20 61 6e 20 61 73 ** cause an as
18370 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
18371 20 74 6f 20 66 61 69 6c 2e 20 20 2a 2f 0a 20 20 to fail. */.
18372 20 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61 67 ptrmapCheckPag
18373 65 73 28 61 70 4e 65 77 2c 20 6e 4e 65 77 29 3b es(apNew, nNew);
18374 0a 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b . ptrmapCheck
18375 50 61 67 65 73 28 26 70 50 61 72 65 6e 74 2c 20 Pages(&pParent,
18376 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 1);.#endif. }..
18377 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e assert( pParen
18378 74 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 54 t->isInit );. T
18379 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 RACE(("BALANCE:
1837a 66 69 6e 69 73 68 65 64 3a 20 6f 6c 64 3d 25 64 finished: old=%d
1837b 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73 3d 25 64 new=%d cells=%d
1837c 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 6e \n",. n
1837d 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43 65 6c 6c Old, nNew, nCell
1837e 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 ));.. /*. ** C
1837f 6c 65 61 6e 75 70 20 62 65 66 6f 72 65 20 72 65 leanup before re
18380 74 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 62 61 turning.. */.ba
18381 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 lance_cleanup:.
18382 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 sqlite3ScratchF
18383 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20 20 66 ree(apCell);. f
18384 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 or(i=0; i<nOld;
18385 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 i++){. releas
18386 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b ePage(apOld[i]);
18387 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 . }. for(i=0;
18388 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20 i<nNew; i++){.
18389 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 releasePage(ap
1838a 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 New[i]);. }..
1838b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
1838c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
1838d 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 on is called whe
1838e 6e 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 n the root page
1838f 6f 66 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 of a b-tree stru
18390 63 74 75 72 65 20 69 73 0a 2a 2a 20 6f 76 65 72 cture is.** over
18391 66 75 6c 6c 20 28 68 61 73 20 6f 6e 65 20 6f 72 full (has one or
18392 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 more overflow p
18393 61 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e ages)..**.** A n
18394 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 ew child page is
18395 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 allocated and t
18396 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 he contents of t
18397 68 65 20 63 75 72 72 65 6e 74 20 72 6f 6f 74 0a he current root.
18398 2a 2a 20 70 61 67 65 2c 20 69 6e 63 6c 75 64 69 ** page, includi
18399 6e 67 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c ng overflow cell
1839a 73 2c 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e s, are copied in
1839b 74 6f 20 74 68 65 20 63 68 69 6c 64 2e 20 54 68 to the child. Th
1839c 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 e root.** page i
1839d 73 20 74 68 65 6e 20 6f 76 65 72 77 72 69 74 74 s then overwritt
1839e 65 6e 20 74 6f 20 6d 61 6b 65 20 69 74 20 61 6e en to make it an
1839f 20 65 6d 70 74 79 20 70 61 67 65 20 77 69 74 68 empty page with
183a0 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 the right-child
183a1 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 70 6f 69 .** pointer poi
183a2 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 nting to the new
183a3 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 page..**.** Bef
183a4 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 61 ore returning, a
183a5 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 ll pointer-map e
183a6 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f 6e ntries correspon
183a7 64 69 6e 67 20 74 6f 20 70 61 67 65 73 20 0a 2a ding to pages .*
183a8 2a 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 63 * that the new c
183a9 68 69 6c 64 2d 70 61 67 65 20 6e 6f 77 20 63 6f hild-page now co
183aa 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20 ntains pointers
183ab 74 6f 20 61 72 65 20 75 70 64 61 74 65 64 2e 20 to are updated.
183ac 54 68 65 0a 2a 2a 20 65 6e 74 72 79 20 63 6f 72 The.** entry cor
183ad 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 responding to th
183ae 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c e new right-chil
183af 64 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 d pointer of the
183b0 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 69 73 root.** page is
183b1 20 61 6c 73 6f 20 75 70 64 61 74 65 64 2e 0a 2a also updated..*
183b2 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 *.** If successf
183b3 75 6c 2c 20 2a 70 70 43 68 69 6c 64 20 69 73 20 ul, *ppChild is
183b4 73 65 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 set to contain a
183b5 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 reference to th
183b6 65 20 63 68 69 6c 64 20 0a 2a 2a 20 70 61 67 65 e child .** page
183b7 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 and SQLITE_OK i
183b8 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 s returned. In t
183b9 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c his case the cal
183ba 6c 65 72 20 69 73 20 72 65 71 75 69 72 65 64 0a ler is required.
183bb 2a 2a 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 ** to call relea
183bc 73 65 50 61 67 65 28 29 20 6f 6e 20 2a 70 70 43 sePage() on *ppC
183bd 68 69 6c 64 20 65 78 61 63 74 6c 79 20 6f 6e 63 hild exactly onc
183be 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f e. If an error o
183bf 63 63 75 72 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 ccurs,.** an err
183c0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 or code is retur
183c1 6e 65 64 20 61 6e 64 20 2a 70 70 43 68 69 6c 64 ned and *ppChild
183c2 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f is set to 0..*/
183c3 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 .static int bala
183c4 6e 63 65 5f 64 65 65 70 65 72 28 4d 65 6d 50 61 nce_deeper(MemPa
183c5 67 65 20 2a 70 52 6f 6f 74 2c 20 4d 65 6d 50 61 ge *pRoot, MemPa
183c6 67 65 20 2a 2a 70 70 43 68 69 6c 64 29 7b 0a 20 ge **ppChild){.
183c7 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 int rc;
183c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
183c9 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 /* Return value
183ca 66 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 from subprocedur
183cb 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 es */. MemPage
183cc 2a 70 43 68 69 6c 64 20 3d 20 30 3b 20 20 20 20 *pChild = 0;
183cd 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
183ce 72 20 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 r to a new child
183cf 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 page */. Pgno
183d0 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30 3b 20 20 pgnoChild = 0;
183d1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 /* Pag
183d2 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
183d3 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a new child page *
183d4 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 /. BtShared *pB
183d5 74 20 3d 20 70 52 6f 6f 74 2d 3e 70 42 74 3b 20 t = pRoot->pBt;
183d6 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 /* The BTree
183d7 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 52 */.. assert( pR
183d8 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 oot->nOverflow>0
183d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
183da 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
183db 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a (pBt->mutex) );.
183dc 0a 20 20 2f 2a 20 4d 61 6b 65 20 70 52 6f 6f 74 . /* Make pRoot
183dd 2c 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 , the root page
183de 6f 66 20 74 68 65 20 62 2d 74 72 65 65 2c 20 77 of the b-tree, w
183df 72 69 74 61 62 6c 65 2e 20 41 6c 6c 6f 63 61 74 ritable. Allocat
183e0 65 20 61 20 6e 65 77 20 0a 20 20 2a 2a 20 70 61 e a new . ** pa
183e1 67 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 ge that will bec
183e2 6f 6d 65 20 74 68 65 20 6e 65 77 20 72 69 67 68 ome the new righ
183e3 74 2d 63 68 69 6c 64 20 6f 66 20 70 50 61 67 65 t-child of pPage
183e4 2e 20 43 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 . Copy the conte
183e5 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 nts. ** of the
183e6 6e 6f 64 65 20 73 74 6f 72 65 64 20 6f 6e 20 70 node stored on p
183e7 52 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 6e 65 Root into the ne
183e8 77 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 w child page..
183e9 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
183ea 33 50 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 3PagerWrite(pRoo
183eb 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69 t->pDbPage);. i
183ec 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
183ed 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c ){. rc = all
183ee 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 ocateBtreePage(p
183ef 42 74 2c 26 70 43 68 69 6c 64 2c 26 70 67 6e 6f Bt,&pChild,&pgno
183f0 43 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e 70 67 6e Child,pRoot->pgn
183f1 6f 2c 30 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f o,0);. copyNo
183f2 64 65 43 6f 6e 74 65 6e 74 28 70 52 6f 6f 74 2c deContent(pRoot,
183f3 20 70 43 68 69 6c 64 2c 20 26 72 63 29 3b 0a 20 pChild, &rc);.
183f4 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 if( ISAUTOVAC
183f5 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 UUM ){. ptr
183f6 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f mapPut(pBt, pgno
183f7 43 68 69 6c 64 2c 20 50 54 52 4d 41 50 5f 42 54 Child, PTRMAP_BT
183f8 52 45 45 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f REE, pRoot->pgno
183f9 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 , &rc);. }.
183fa 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 }. if( rc ){.
183fb 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 30 3b 0a *ppChild = 0;.
183fc 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
183fd 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 pChild);. ret
183fe 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 urn rc;. }. as
183ff 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 sert( sqlite3Pag
18400 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 43 erIswriteable(pC
18401 68 69 6c 64 2d 3e 70 44 62 50 61 67 65 29 20 29 hild->pDbPage) )
18402 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 ;. assert( sqli
18403 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 te3PagerIswritea
18404 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 ble(pRoot->pDbPa
18405 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ge) );. assert(
18406 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c 6c 3d 3d pChild->nCell==
18407 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a pRoot->nCell );.
18408 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e . TRACE(("BALAN
18409 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74 20 25 64 CE: copy root %d
1840a 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20 70 52 6f into %d\n", pRo
1840b 6f 74 2d 3e 70 67 6e 6f 2c 20 70 43 68 69 6c 64 ot->pgno, pChild
1840c 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20 2f 2a 20 ->pgno));.. /*
1840d 43 6f 70 79 20 74 68 65 20 6f 76 65 72 66 6c 6f Copy the overflo
1840e 77 20 63 65 6c 6c 73 20 66 72 6f 6d 20 70 52 6f w cells from pRo
1840f 6f 74 20 74 6f 20 70 43 68 69 6c 64 20 2a 2f 0a ot to pChild */.
18410 20 20 6d 65 6d 63 70 79 28 70 43 68 69 6c 64 2d memcpy(pChild-
18411 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 61 >aOvfl, pRoot->a
18412 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e 6e 4f 76 Ovfl, pRoot->nOv
18413 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66 28 70 52 erflow*sizeof(pR
18414 6f 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 3b oot->aOvfl[0]));
18415 0a 20 20 70 43 68 69 6c 64 2d 3e 6e 4f 76 65 72 . pChild->nOver
18416 66 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d 3e 6e 4f flow = pRoot->nO
18417 76 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f 2a 20 5a verflow;.. /* Z
18418 65 72 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ero the contents
18419 20 6f 66 20 70 52 6f 6f 74 2e 20 54 68 65 6e 20 of pRoot. Then
1841a 69 6e 73 74 61 6c 6c 20 70 43 68 69 6c 64 20 61 install pChild a
1841b 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c s the right-chil
1841c 64 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50 61 67 65 d. */. zeroPage
1841d 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2d 3e (pRoot, pChild->
1841e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46 5f aData[0] & ~PTF_
1841f 4c 45 41 46 29 3b 0a 20 20 70 75 74 34 62 79 74 LEAF);. put4byt
18420 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b e(&pRoot->aData[
18421 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 pRoot->hdrOffset
18422 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c 64 29 3b +8], pgnoChild);
18423 0a 0a 20 20 2a 70 70 43 68 69 6c 64 20 3d 20 70 .. *ppChild = p
18424 43 68 69 6c 64 3b 0a 20 20 72 65 74 75 72 6e 20 Child;. return
18425 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
18426 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 .** The page tha
18427 74 20 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 t pCur currently
18428 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a points to has j
18429 75 73 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 ust been modifie
1842a 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 d in.** some way
1842b 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 . This function
1842c 66 69 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 figures out if t
1842d 68 69 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e his modification
1842e 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 means the.** tr
1842f 65 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 ee needs to be b
18430 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 alanced, and if
18431 73 6f 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 so calls the app
18432 72 6f 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 ropriate balanci
18433 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 ng .** routine.
18434 42 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e Balancing routin
18435 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 es are:.**.**
18436 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 0a balance_quick().
18437 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 64 65 65 ** balance_dee
18438 70 65 72 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e per().** balan
18439 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a 2a 2f 0a ce_nonroot().*/.
1843a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e static int balan
1843b 63 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 ce(BtCursor *pCu
1843c 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 r){. int rc = S
1843d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 QLITE_OK;. cons
1843e 74 20 69 6e 74 20 6e 4d 69 6e 20 3d 20 70 43 75 t int nMin = pCu
1843f 72 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 r->pBt->usableSi
18440 7a 65 20 2a 20 32 20 2f 20 33 3b 0a 20 20 75 38 ze * 2 / 3;. u8
18441 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 aBalanceQuickSp
18442 61 63 65 5b 31 33 5d 3b 0a 20 20 75 38 20 2a 70 ace[13];. u8 *p
18443 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20 54 45 53 Free = 0;.. TES
18444 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e TONLY( int balan
18445 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 20 ce_quick_called
18446 3d 20 30 20 29 3b 0a 20 20 54 45 53 54 4f 4e 4c = 0 );. TESTONL
18447 59 28 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 Y( int balance_d
18448 65 65 70 65 72 5f 63 61 6c 6c 65 64 20 3d 20 30 eeper_called = 0
18449 20 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20 20 20 20 );.. do {.
1844a 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 int iPage = pCur
1844b 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 4d 65 6d ->iPage;. Mem
1844c 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 Page *pPage = pC
1844d 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65 ur->apPage[iPage
1844e 5d 3b 0a 0a 20 20 20 20 69 66 28 20 69 50 61 67 ];.. if( iPag
1844f 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 e==0 ){. if
18450 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c ( pPage->nOverfl
18451 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ow ){. /*
18452 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f The root page o
18453 66 20 74 68 65 20 62 2d 74 72 65 65 20 69 73 20 f the b-tree is
18454 6f 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 overfull. In thi
18455 73 20 63 61 73 65 20 63 61 6c 6c 20 74 68 65 0a s case call the.
18456 20 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e ** balan
18457 63 65 5f 64 65 65 70 65 72 28 29 20 66 75 6e 63 ce_deeper() func
18458 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61 tion to create a
18459 20 6e 65 77 20 63 68 69 6c 64 20 66 6f 72 20 74 new child for t
1845a 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 he root-page.
1845b 20 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f 70 79 ** and copy
1845c 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e the current con
1845d 74 65 6e 74 73 20 6f 66 20 74 68 65 20 72 6f 6f tents of the roo
1845e 74 2d 70 61 67 65 20 74 6f 20 69 74 2e 20 54 68 t-page to it. Th
1845f 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 78 e. ** nex
18460 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 t iteration of t
18461 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 he do-loop will
18462 62 61 6c 61 6e 63 65 20 74 68 65 20 63 68 69 6c balance the chil
18463 64 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 d page..
18464 2a 2f 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 */ . asse
18465 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 rt( (balance_dee
18466 70 65 72 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 per_called++)==0
18467 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d );. rc =
18468 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 balance_deeper(
18469 70 50 61 67 65 2c 20 26 70 43 75 72 2d 3e 61 70 pPage, &pCur->ap
1846a 50 61 67 65 5b 31 5d 29 3b 0a 20 20 20 20 20 20 Page[1]);.
1846b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1846c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1846d 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 31 pCur->iPage = 1
1846e 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 ;. pCur
1846f 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a ->aiIdx[0] = 0;.
18470 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e pCur->
18471 61 69 49 64 78 5b 31 5d 20 3d 20 30 3b 0a 20 20 aiIdx[1] = 0;.
18472 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
18473 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 2d pCur->apPage[1]-
18474 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20 >nOverflow );.
18475 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 }. }e
18476 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 lse{. bre
18477 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
18478 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 2d }else if( pPage-
18479 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 >nOverflow==0 &&
1847a 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d 6e pPage->nFree<=n
1847b 4d 69 6e 20 29 7b 0a 20 20 20 20 20 20 62 72 65 Min ){. bre
1847c 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ak;. }else{.
1847d 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 20 63 MemPage * c
1847e 6f 6e 73 74 20 70 50 61 72 65 6e 74 20 3d 20 70 onst pParent = p
1847f 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 Cur->apPage[iPag
18480 65 2d 31 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 e-1];. int
18481 63 6f 6e 73 74 20 69 49 64 78 20 3d 20 70 43 75 const iIdx = pCu
18482 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 2d 31 r->aiIdx[iPage-1
18483 5d 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 ];.. rc = s
18484 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 qlite3PagerWrite
18485 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 (pParent->pDbPag
18486 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 e);. if( rc
18487 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 ==SQLITE_OK ){.#
18488 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18489 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a IT_QUICKBALANCE.
1848a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 if( pPag
1848b 65 2d 3e 68 61 73 44 61 74 61 0a 20 20 20 20 20 e->hasData.
1848c 20 20 20 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f && pPage->nO
1848d 76 65 72 66 6c 6f 77 3d 3d 31 0a 20 20 20 20 20 verflow==1.
1848e 20 20 20 20 26 26 20 70 50 61 67 65 2d 3e 61 4f && pPage->aO
1848f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 50 61 67 vfl[0].idx==pPag
18490 65 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20 20 20 20 e->nCell.
18491 20 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 67 && pParent->pg
18492 6e 6f 21 3d 31 0a 20 20 20 20 20 20 20 20 20 26 no!=1. &
18493 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c & pParent->nCell
18494 3d 3d 69 49 64 78 0a 20 20 20 20 20 20 20 20 29 ==iIdx. )
18495 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 {. /* C
18496 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 all balance_quic
18497 6b 28 29 20 74 6f 20 63 72 65 61 74 65 20 61 20 k() to create a
18498 6e 65 77 20 73 69 62 6c 69 6e 67 20 6f 66 20 70 new sibling of p
18499 50 61 67 65 20 6f 6e 20 77 68 69 63 68 0a 20 20 Page on which.
1849a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 73 74 ** to st
1849b 6f 72 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 ore the overflow
1849c 20 63 65 6c 6c 2e 20 62 61 6c 61 6e 63 65 5f 71 cell. balance_q
1849d 75 69 63 6b 28 29 20 69 6e 73 65 72 74 73 20 61 uick() inserts a
1849e 20 6e 65 77 20 63 65 6c 6c 0a 20 20 20 20 20 20 new cell.
1849f 20 20 20 20 2a 2a 20 69 6e 74 6f 20 70 50 61 72 ** into pPar
184a0 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 79 20 63 ent, which may c
184a1 61 75 73 65 20 70 50 61 72 65 6e 74 20 6f 76 65 ause pParent ove
184a2 72 66 6c 6f 77 2e 20 49 66 20 74 68 69 73 0a 20 rflow. If this.
184a3 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70 ** happ
184a4 65 6e 73 2c 20 74 68 65 20 6e 65 78 74 20 69 6e ens, the next in
184a5 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 teration of the
184a6 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c do-loop will bal
184a7 61 6e 63 65 20 70 50 61 72 65 6e 74 20 0a 20 20 ance pParent .
184a8 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 65 ** use e
184a9 69 74 68 65 72 20 62 61 6c 61 6e 63 65 5f 6e 6f ither balance_no
184aa 6e 72 6f 6f 74 28 29 20 6f 72 20 62 61 6c 61 6e nroot() or balan
184ab 63 65 5f 64 65 65 70 65 72 28 29 2e 20 55 6e 74 ce_deeper(). Unt
184ac 69 6c 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 il this.
184ad 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 ** happens, th
184ae 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 e overflow cell
184af 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 is stored in the
184b0 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 aBalanceQuickSp
184b1 61 63 65 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 ace[].
184b2 2a 2a 20 62 75 66 66 65 72 2e 20 0a 20 20 20 20 ** buffer. .
184b3 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 **.
184b4 20 20 20 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 ** The purpos
184b5 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
184b6 6e 67 20 61 73 73 65 72 74 28 29 20 69 73 20 74 ng assert() is t
184b7 6f 20 63 68 65 63 6b 20 74 68 61 74 20 6f 6e 6c o check that onl
184b8 79 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a 2a y a. **
184b9 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 74 6f 20 single call to
184ba 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 balance_quick()
184bb 69 73 20 6d 61 64 65 20 66 6f 72 20 65 61 63 68 is made for each
184bc 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 0a 20 20 call to this.
184bd 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 ** funct
184be 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 77 65 72 ion. If this wer
184bf 65 20 6e 6f 74 20 76 65 72 69 66 69 65 64 2c 20 e not verified,
184c0 61 20 73 75 62 74 6c 65 20 62 75 67 20 69 6e 76 a subtle bug inv
184c1 6f 6c 76 69 6e 67 20 72 65 75 73 65 0a 20 20 20 olving reuse.
184c2 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 ** of the
184c3 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 aBalanceQuickSp
184c4 61 63 65 5b 5d 20 6d 69 67 68 74 20 73 6e 65 61 ace[] might snea
184c5 6b 20 69 6e 2e 0a 20 20 20 20 20 20 20 20 20 20 k in..
184c6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 */. ass
184c7 65 72 74 28 20 28 62 61 6c 61 6e 63 65 5f 71 75 ert( (balance_qu
184c8 69 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29 3d 3d 30 ick_called++)==0
184c9 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 );. rc
184ca 20 3d 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b = balance_quick
184cb 28 70 50 61 72 65 6e 74 2c 20 70 50 61 67 65 2c (pParent, pPage,
184cc 20 61 42 61 6c 61 6e 63 65 51 75 69 63 6b 53 70 aBalanceQuickSp
184cd 61 63 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 ace);. }e
184ce 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 lse.#endif.
184cf 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 2f {. /
184d0 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 * In this case,
184d1 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e call balance_non
184d2 72 6f 6f 74 28 29 20 74 6f 20 72 65 64 69 73 74 root() to redist
184d3 72 69 62 75 74 65 20 63 65 6c 6c 73 0a 20 20 20 ribute cells.
184d4 20 20 20 20 20 20 20 2a 2a 20 62 65 74 77 65 65 ** betwee
184d5 6e 20 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 n pPage and up t
184d6 6f 20 32 20 6f 66 20 69 74 73 20 73 69 62 6c 69 o 2 of its sibli
184d7 6e 67 20 70 61 67 65 73 2e 20 54 68 69 73 20 69 ng pages. This i
184d8 6e 76 6f 6c 76 65 73 0a 20 20 20 20 20 20 20 20 nvolves.
184d9 20 20 2a 2a 20 6d 6f 64 69 66 79 69 6e 67 20 74 ** modifying t
184da 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 he contents of p
184db 50 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61 Parent, which ma
184dc 79 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20 y cause pParent
184dd 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 to. **
184de 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 become overfull
184df 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 54 68 or underfull. Th
184e0 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e e next iteration
184e1 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 0a of the do-loop.
184e2 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 6c ** wil
184e3 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 70 61 l balance the pa
184e4 72 65 6e 74 20 70 61 67 65 20 74 6f 20 63 6f 72 rent page to cor
184e5 72 65 63 74 20 74 68 69 73 2e 0a 20 20 20 20 20 rect this..
184e6 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 20 ** .
184e7 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 ** If the par
184e8 65 6e 74 20 70 61 67 65 20 62 65 63 6f 6d 65 73 ent page becomes
184e9 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68 65 20 6f overfull, the o
184ea 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 6f 72 20 verflow cell or
184eb 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 cells.
184ec 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e ** are stored in
184ed 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 the pSpace buff
184ee 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 69 6d 6d er allocated imm
184ef 65 64 69 61 74 65 6c 79 20 62 65 6c 6f 77 2e 20 ediately below.
184f0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 20 . ** A
184f1 73 75 62 73 65 71 75 65 6e 74 20 69 74 65 72 61 subsequent itera
184f2 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c tion of the do-l
184f3 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c 20 77 69 oop will deal wi
184f4 74 68 20 74 68 69 73 20 62 79 0a 20 20 20 20 20 th this by.
184f5 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 ** calling
184f6 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 balance_nonroot(
184f7 29 20 28 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 ) (balance_deepe
184f8 72 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 r() may be calle
184f9 64 20 66 69 72 73 74 2c 0a 20 20 20 20 20 20 20 d first,.
184fa 20 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 ** but it doe
184fb 73 6e 27 74 20 64 65 61 6c 20 77 69 74 68 20 6f sn't deal with o
184fc 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 2d 20 verflow cells -
184fd 6a 75 73 74 20 6d 6f 76 65 73 20 74 68 65 6d 20 just moves them
184fe 74 6f 20 61 0a 20 20 20 20 20 20 20 20 20 20 2a to a. *
184ff 2a 20 64 69 66 66 65 72 65 6e 74 20 70 61 67 65 * different page
18500 29 2e 20 4f 6e 63 65 20 74 68 69 73 20 73 75 62 ). Once this sub
18501 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 sequent call to
18502 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 balance_nonroot(
18503 29 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ) . **
18504 68 61 73 20 63 6f 6d 70 6c 65 74 65 64 2c 20 69 has completed, i
18505 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 6c t is safe to rel
18506 65 61 73 65 20 74 68 65 20 70 53 70 61 63 65 20 ease the pSpace
18507 62 75 66 66 65 72 20 75 73 65 64 20 62 79 0a 20 buffer used by.
18508 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 ** the
18509 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2c 20 61 previous call, a
1850a 73 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 s the overflow c
1850b 65 6c 6c 20 64 61 74 61 20 77 69 6c 6c 20 68 61 ell data will ha
1850c 76 65 20 62 65 65 6e 20 0a 20 20 20 20 20 20 20 ve been .
1850d 20 20 20 2a 2a 20 63 6f 70 69 65 64 20 65 69 74 ** copied eit
1850e 68 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 her into the bod
1850f 79 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 y of a database
18510 70 61 67 65 20 6f 72 20 69 6e 74 6f 20 74 68 65 page or into the
18511 20 6e 65 77 0a 20 20 20 20 20 20 20 20 20 20 2a new. *
18512 2a 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 * pSpace buffer
18513 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 6c 61 passed to the la
18514 74 74 65 72 20 63 61 6c 6c 20 74 6f 20 62 61 6c tter call to bal
18515 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 0a ance_nonroot()..
18516 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 */.
18517 20 20 20 20 20 20 20 75 38 20 2a 70 53 70 61 63 u8 *pSpac
18518 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d e = sqlite3PageM
18519 61 6c 6c 6f 63 28 70 43 75 72 2d 3e 70 42 74 2d alloc(pCur->pBt-
1851a 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 >pageSize);.
1851b 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e rc = balan
1851c 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50 61 72 65 ce_nonroot(pPare
1851d 6e 74 2c 20 69 49 64 78 2c 20 70 53 70 61 63 65 nt, iIdx, pSpace
1851e 2c 20 69 50 61 67 65 3d 3d 31 29 3b 0a 20 20 20 , iPage==1);.
1851f 20 20 20 20 20 20 20 69 66 28 20 70 46 72 65 65 if( pFree
18520 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
18521 2f 2a 20 49 66 20 70 46 72 65 65 20 69 73 20 6e /* If pFree is n
18522 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e ot NULL, it poin
18523 74 73 20 74 6f 20 74 68 65 20 70 53 70 61 63 65 ts to the pSpace
18524 20 62 75 66 66 65 72 20 75 73 65 64 20 0a 20 20 buffer used .
18525 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 79 20 ** by
18526 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 a previous call
18527 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f to balance_nonro
18528 6f 74 28 29 2e 20 49 74 73 20 63 6f 6e 74 65 6e ot(). Its conten
18529 74 73 20 61 72 65 0a 20 20 20 20 20 20 20 20 20 ts are.
1852a 20 20 20 2a 2a 20 6e 6f 77 20 73 74 6f 72 65 64 ** now stored
1852b 20 65 69 74 68 65 72 20 6f 6e 20 72 65 61 6c 20 either on real
1852c 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f database pages o
1852d 72 20 77 69 74 68 69 6e 20 74 68 65 20 0a 20 20 r within the .
1852e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 65 77 ** new
1852f 20 70 53 70 61 63 65 20 62 75 66 66 65 72 2c 20 pSpace buffer,
18530 73 6f 20 69 74 20 6d 61 79 20 62 65 20 73 61 66 so it may be saf
18531 65 6c 79 20 66 72 65 65 64 20 68 65 72 65 2e 20 ely freed here.
18532 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 */. s
18533 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 qlite3PageFree(p
18534 46 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Free);.
18535 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a }.. /*
18536 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66 The pSpace buff
18537 65 72 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 er will be freed
18538 20 61 66 74 65 72 20 74 68 65 20 6e 65 78 74 20 after the next
18539 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 call to.
1853a 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e ** balance_non
1853b 72 6f 6f 74 28 29 2c 20 6f 72 20 6a 75 73 74 20 root(), or just
1853c 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e 63 before this func
1853d 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c 20 77 68 tion returns, wh
1853e 69 63 68 65 76 65 72 0a 20 20 20 20 20 20 20 20 ichever.
1853f 20 20 2a 2a 20 63 6f 6d 65 73 20 66 69 72 73 74 ** comes first
18540 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 . */. p
18541 46 72 65 65 20 3d 20 70 53 70 61 63 65 3b 0a 20 Free = pSpace;.
18542 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
18543 0a 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 6e .. pPage->n
18544 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 0a 20 Overflow = 0;..
18545 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 /* The next
18546 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 iteration of th
18547 65 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c 61 6e 63 e do-loop balanc
18548 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 es the parent pa
18549 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c ge. */. rel
1854a 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
1854b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50 61 . pCur->iPa
1854c 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 77 ge--;. }. }w
1854d 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
1854e 5f 4f 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 46 _OK );.. if( pF
1854f 72 65 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ree ){. sqlit
18550 65 33 50 61 67 65 46 72 65 65 28 70 46 72 65 65 e3PageFree(pFree
18551 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
18552 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e rc;.}.../*.** In
18553 73 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 sert a new recor
18554 64 20 69 6e 74 6f 20 74 68 65 20 42 54 72 65 65 d into the BTree
18555 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 67 69 . The key is gi
18556 76 65 6e 20 62 79 20 28 70 4b 65 79 2c 6e 4b 65 ven by (pKey,nKe
18557 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 y).** and the da
18558 74 61 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 ta is given by (
18559 70 44 61 74 61 2c 6e 44 61 74 61 29 2e 20 20 54 pData,nData). T
1855a 68 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 he cursor is use
1855b 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20 64 65 66 d only to.** def
1855c 69 6e 65 20 77 68 61 74 20 74 61 62 6c 65 20 74 ine what table t
1855d 68 65 20 72 65 63 6f 72 64 20 73 68 6f 75 6c 64 he record should
1855e 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 be inserted int
1855f 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a o. The cursor.*
18560 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 * is left pointi
18561 6e 67 20 61 74 20 61 20 72 61 6e 64 6f 6d 20 6c ng at a random l
18562 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 ocation..**.** F
18563 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 or an INTKEY tab
18564 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20 6e 4b 65 le, only the nKe
18565 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b y value of the k
18566 65 79 20 69 73 20 75 73 65 64 2e 20 20 70 4b 65 ey is used. pKe
18567 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e y is.** ignored.
18568 20 20 46 6f 72 20 61 20 5a 45 52 4f 44 41 54 41 For a ZERODATA
18569 20 74 61 62 6c 65 2c 20 74 68 65 20 70 44 61 74 table, the pDat
1856a 61 20 61 6e 64 20 6e 44 61 74 61 20 61 72 65 20 a and nData are
1856b 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a both ignored..**
1856c 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52 .** If the seekR
1856d 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 esult parameter
1856e 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 is non-zero, the
1856f 6e 20 61 20 73 75 63 63 65 73 73 66 75 6c 20 63 n a successful c
18570 61 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76 65 74 6f all to.** Moveto
18571 55 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 73 65 Unpacked() to se
18572 65 6b 20 63 75 72 73 6f 72 20 70 43 75 72 20 74 ek cursor pCur t
18573 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 68 o (pKey, nKey) h
18574 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 as already.** be
18575 65 6e 20 70 65 72 66 6f 72 6d 65 64 2e 20 73 65 en performed. se
18576 65 6b 52 65 73 75 6c 74 20 69 73 20 74 68 65 20 ekResult is the
18577 73 65 61 72 63 68 20 72 65 73 75 6c 74 20 72 65 search result re
18578 74 75 72 6e 65 64 20 28 61 20 6e 65 67 61 74 69 turned (a negati
18579 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 66 20 ve.** number if
1857a 70 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 pCur points at a
1857b 6e 20 65 6e 74 72 79 20 74 68 61 74 20 69 73 20 n entry that is
1857c 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 28 70 4b smaller than (pK
1857d 65 79 2c 20 6e 4b 65 79 29 2c 20 6f 72 0a 2a 2a ey, nKey), or.**
1857e 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 a positive valu
1857f 65 20 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 e if pCur points
18580 20 61 74 20 61 6e 20 65 74 72 79 20 74 68 61 74 at an etry that
18581 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 is larger than
18582 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29 .** (pKey, nKey)
18583 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ). .**.** If the
18584 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 61 seekResult para
18585 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 meter is non-zer
18586 6f 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c o, then the call
18587 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 er guarantees th
18588 61 74 0a 2a 2a 20 63 75 72 73 6f 72 20 70 43 75 at.** cursor pCu
18589 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 61 74 r is pointing at
1858a 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 63 6f the existing co
1858b 70 79 20 6f 66 20 61 20 72 6f 77 20 74 68 61 74 py of a row that
1858c 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 is to be.** ove
1858d 72 77 72 69 74 74 65 6e 2e 20 20 49 66 20 74 68 rwritten. If th
1858e 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72 e seekResult par
1858f 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 ameter is 0, the
18590 6e 20 63 75 72 73 6f 72 20 70 43 75 72 20 6d 61 n cursor pCur ma
18591 79 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 6e y.** point to an
18592 79 20 65 6e 74 72 79 20 6f 72 20 74 6f 20 6e 6f y entry or to no
18593 20 65 6e 74 72 79 20 61 74 20 61 6c 6c 20 61 6e entry at all an
18594 64 20 73 6f 20 74 68 69 73 20 66 75 6e 63 74 69 d so this functi
18595 6f 6e 20 68 61 73 20 74 6f 20 73 65 65 6b 0a 2a on has to seek.*
18596 2a 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 66 * the cursor bef
18597 6f 72 65 20 74 68 65 20 6e 65 77 20 6b 65 79 20 ore the new key
18598 63 61 6e 20 62 65 20 69 6e 73 65 72 74 65 64 2e can be inserted.
18599 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1859a 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
1859b 72 65 65 49 6e 73 65 72 74 28 0a 20 20 42 74 43 reeInsert(. BtC
1859c 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 ursor *pCur,
1859d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
1859e 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20 nsert data into
1859f 74 68 65 20 74 61 62 6c 65 20 6f 66 20 74 68 69 the table of thi
185a0 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 63 6f s cursor */. co
185a1 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 nst void *pKey,
185a2 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 i64 nKey, /*
185a3 54 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 6e The key of the n
185a4 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 ew record */. c
185a5 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 onst void *pData
185a6 2c 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 2f 2a , int nData, /*
185a7 20 54 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 The data of the
185a8 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 new record */.
185a9 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 int nZero,
185aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
185ab 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 /* Number of ext
185ac 72 61 20 30 20 62 79 74 65 73 20 74 6f 20 61 70 ra 0 bytes to ap
185ad 70 65 6e 64 20 74 6f 20 64 61 74 61 20 2a 2f 0a pend to data */.
185ae 20 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73 int appendBias
185af 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
185b0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 /* True if this
185b1 20 69 73 20 6c 69 6b 65 6c 79 20 61 6e 20 61 70 is likely an ap
185b2 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 pend */. int se
185b3 65 6b 52 65 73 75 6c 74 20 20 20 20 20 20 20 20 ekResult
185b4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 /* Resu
185b5 6c 74 20 6f 66 20 70 72 69 6f 72 20 4d 6f 76 65 lt of prior Move
185b6 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 63 61 6c toUnpacked() cal
185b7 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 l */.){. int rc
185b8 3b 0a 20 20 69 6e 74 20 6c 6f 63 20 3d 20 73 65 ;. int loc = se
185b9 65 6b 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 ekResult;
185ba 20 20 20 2f 2a 20 2d 31 3a 20 62 65 66 6f 72 65 /* -1: before
185bb 20 64 65 73 69 72 65 64 20 6c 6f 63 61 74 69 6f desired locatio
185bc 6e 20 20 2b 31 3a 20 61 66 74 65 72 20 2a 2f 0a n +1: after */.
185bd 20 20 69 6e 74 20 73 7a 4e 65 77 20 3d 20 30 3b int szNew = 0;
185be 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 . int idx;. Me
185bf 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 mPage *pPage;.
185c0 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d Btree *p = pCur-
185c1 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 >pBtree;. BtSha
185c2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
185c3 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 t;. unsigned ch
185c4 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 ar *oldCell;. u
185c5 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 nsigned char *ne
185c6 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 wCell = 0;.. if
185c7 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d ( pCur->eState==
185c8 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a CURSOR_FAULT ){.
185c9 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 assert( pCur
185ca 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 ->skipNext!=SQLI
185cb 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 TE_OK );. ret
185cc 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 urn pCur->skipNe
185cd 78 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 xt;. }.. asser
185ce 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
185cf 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 tex(pCur) );. a
185d0 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 ssert( pCur->wrF
185d1 6c 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 lag && pBt->inTr
185d2 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 ansaction==TRANS
185d3 5f 57 52 49 54 45 20 26 26 20 21 70 42 74 2d 3e _WRITE && !pBt->
185d4 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 readOnly );. as
185d5 73 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 sert( hasSharedC
185d6 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c acheTableLock(p,
185d7 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
185d8 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 pCur->pKeyInfo!
185d9 3d 30 2c 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 =0, 2) );.. /*
185da 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 Assert that the
185db 63 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e 20 caller has been
185dc 63 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20 74 consistent. If t
185dd 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f his cursor was o
185de 70 65 6e 65 64 0a 20 20 2a 2a 20 65 78 70 65 63 pened. ** expec
185df 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d ting an index b-
185e0 74 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20 63 tree, then the c
185e1 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20 aller should be
185e2 69 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0a 20 inserting blob.
185e3 20 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e 6f ** keys with no
185e4 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 associated data
185e5 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 . If the cursor
185e6 77 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65 63 was opened expec
185e7 74 69 6e 67 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 ting an. ** int
185e8 6b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 20 63 key table, the c
185e9 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20 aller should be
185ea 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 65 67 65 inserting intege
185eb 72 20 6b 65 79 73 20 77 69 74 68 20 61 0a 20 20 r keys with a.
185ec 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 73 73 6f 63 ** blob of assoc
185ed 69 61 74 65 64 20 64 61 74 61 2e 20 20 2a 2f 0a iated data. */.
185ee 20 20 61 73 73 65 72 74 28 20 28 70 4b 65 79 3d assert( (pKey=
185ef 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 =0)==(pCur->pKey
185f0 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f Info==0) );.. /
185f1 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 * If this is an
185f2 69 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 74 61 insert into a ta
185f3 62 6c 65 20 62 2d 74 72 65 65 2c 20 69 6e 76 61 ble b-tree, inva
185f4 6c 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 lidate any incrb
185f5 6c 6f 62 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 lob . ** cursor
185f6 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f s open on the ro
185f7 77 20 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 w being replaced
185f8 20 28 61 73 73 75 6d 69 6e 67 20 74 68 69 73 20 (assuming this
185f9 69 73 20 61 20 72 65 70 6c 61 63 65 0a 20 20 2a is a replace. *
185fa 2a 20 6f 70 65 72 61 74 69 6f 6e 20 2d 20 69 66 * operation - if
185fb 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 it is not, the
185fc 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 6e following is a n
185fd 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 66 28 o-op). */. if(
185fe 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d pCur->pKeyInfo=
185ff 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 =0 ){. invali
18600 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 dateIncrblobCurs
18601 6f 72 73 28 70 2c 20 6e 4b 65 79 2c 20 30 29 3b ors(p, nKey, 0);
18602 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 . }.. /* Save
18603 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 the positions of
18604 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f any other curso
18605 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 rs open on this
18606 74 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a table.. **. **
18607 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 In some cases,
18608 74 68 65 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 the call to btre
18609 65 4d 6f 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 eMoveto() below
1860a 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a is a no-op. For.
1860b 20 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 ** example, wh
1860c 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 64 61 74 en inserting dat
1860d 61 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 a into a table w
1860e 69 74 68 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 ith auto-generat
1860f 65 64 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 ed integer. **
18610 6b 65 79 73 2c 20 74 68 65 20 56 44 42 45 20 6c keys, the VDBE l
18611 61 79 65 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c ayer invokes sql
18612 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 ite3BtreeLast()
18613 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 to figure out th
18614 65 20 0a 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 e . ** integer
18615 6b 65 79 20 74 6f 20 75 73 65 2e 20 49 74 20 74 key to use. It t
18616 68 65 6e 20 63 61 6c 6c 73 20 74 68 69 73 20 66 hen calls this f
18617 75 6e 63 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 unction to actua
18618 6c 6c 79 20 69 6e 73 65 72 74 20 74 68 65 20 0a lly insert the .
18619 20 20 2a 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 ** data into t
1861a 68 65 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 he intkey B-Tree
1861b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 62 . In this case b
1861c 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 72 65 63 treeMoveto() rec
1861d 6f 67 6e 69 7a 65 73 0a 20 20 2a 2a 20 74 68 61 ognizes. ** tha
1861e 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 t the cursor is
1861f 61 6c 72 65 61 64 79 20 77 68 65 72 65 20 69 74 already where it
18620 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e 64 needs to be and
18621 20 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 returns without
18622 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61 6e 79 20 . ** doing any
18623 77 6f 72 6b 2e 20 54 6f 20 61 76 6f 69 64 20 74 work. To avoid t
18624 68 77 61 72 74 69 6e 67 20 74 68 65 73 65 20 6f hwarting these o
18625 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2c 20 69 74 ptimizations, it
18626 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20 is important.
18627 2a 2a 20 6e 6f 74 20 74 6f 20 63 6c 65 61 72 20 ** not to clear
18628 74 68 65 20 63 75 72 73 6f 72 20 68 65 72 65 2e the cursor here.
18629 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 . */. rc = sav
1862a 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c eAllCursors(pBt,
1862b 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c pCur->pgnoRoot,
1862c 20 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 pCur);. if( rc
1862d 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
1862e 69 66 28 20 21 6c 6f 63 20 29 7b 0a 20 20 20 20 if( !loc ){.
1862f 72 63 20 3d 20 62 74 72 65 65 4d 6f 76 65 74 6f rc = btreeMoveto
18630 28 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 (pCur, pKey, nKe
18631 79 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20 26 y, appendBias, &
18632 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 loc);. if( rc
18633 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 ) return rc;.
18634 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 }. assert( pCur
18635 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 ->eState==CURSOR
18636 5f 56 41 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d _VALID || (pCur-
18637 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f >eState==CURSOR_
18638 49 4e 56 41 4c 49 44 20 26 26 20 6c 6f 63 29 20 INVALID && loc)
18639 29 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 );.. pPage = pC
1863a 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d ur->apPage[pCur-
1863b 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 >iPage];. asser
1863c 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 t( pPage->intKey
1863d 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 || nKey>=0 );.
1863e 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e assert( pPage->
1863f 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e leaf || !pPage->
18640 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 54 52 41 intKey );.. TRA
18641 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 CE(("INSERT: tab
18642 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 le=%d nkey=%lld
18643 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 ndata=%d page=%d
18644 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 %s\n",.
18645 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 pCur->pgnoRoot
18646 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 , nKey, nData, p
18647 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 Page->pgno,.
18648 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 loc==0 ? "
18649 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 overwrite" : "ne
1864a 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 w entry"));. as
1864b 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 sert( pPage->isI
1864c 6e 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 nit );. allocat
1864d 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b eTempSpace(pBt);
1864e 0a 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 . newCell = pBt
1864f 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 ->pTmpSpace;. i
18650 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 f( newCell==0 )
18651 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
18652 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c MEM;. rc = fill
18653 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 InCell(pPage, ne
18654 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 wCell, pKey, nKe
18655 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c y, pData, nData,
18656 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b nZero, &szNew);
18657 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f . if( rc ) goto
18658 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 end_insert;. a
18659 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 ssert( szNew==ce
1865a 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c llSizePtr(pPage,
1865b 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 newCell) );. a
1865c 73 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 ssert( szNew<=MX
1865d 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 _CELL_SIZE(pBt)
1865e 29 3b 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d );. idx = pCur-
1865f 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 >aiIdx[pCur->iPa
18660 67 65 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d ge];. if( loc==
18661 30 20 29 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 0 ){. u16 szO
18662 6c 64 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ld;. assert(
18663 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c idx<pPage->nCell
18664 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
18665 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 ite3PagerWrite(p
18666 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a Page->pDbPage);.
18667 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 if( rc ){.
18668 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 goto end_ins
18669 65 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f ert;. }. o
1866a 6c 64 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c ldCell = findCel
1866b 6c 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 l(pPage, idx);.
1866c 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c if( !pPage->l
1866d 65 61 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d eaf ){. mem
1866e 63 70 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 cpy(newCell, old
1866f 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a Cell, 4);. }.
18670 20 20 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c szOld = cell
18671 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f SizePtr(pPage, o
18672 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 ldCell);. rc
18673 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 = clearCell(pPag
18674 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 e, oldCell);.
18675 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c dropCell(pPage,
18676 20 69 64 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 idx, szOld, &rc
18677 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 );. if( rc )
18678 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b goto end_insert;
18679 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 . }else if( loc
1867a 3c 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 <0 && pPage->nCe
1867b 6c 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 ll>0 ){. asse
1867c 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 rt( pPage->leaf
1867d 29 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 );. idx = ++p
1867e 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
1867f 3e 69 50 61 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 >iPage];. }else
18680 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 {. assert( pP
18681 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d age->leaf );. }
18682 0a 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 . insertCell(pP
18683 61 67 65 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c age, idx, newCel
18684 6c 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 l, szNew, 0, 0,
18685 26 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 &rc);. assert(
18686 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c rc!=SQLITE_OK ||
18687 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 pPage->nCell>0
18688 7c 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 || pPage->nOverf
18689 6c 6f 77 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 low>0 );.. /* I
1868a 66 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20 6f f no error has o
1868b 63 63 75 72 65 64 20 61 6e 64 20 70 50 61 67 65 ccured and pPage
1868c 20 68 61 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 has an overflow
1868d 20 63 65 6c 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 cell, call bala
1868e 6e 63 65 28 29 20 0a 20 20 2a 2a 20 74 6f 20 72 nce() . ** to r
1868f 65 64 69 73 74 72 69 62 75 74 65 20 74 68 65 20 edistribute the
18690 63 65 6c 6c 73 20 77 69 74 68 69 6e 20 74 68 65 cells within the
18691 20 74 72 65 65 2e 20 53 69 6e 63 65 20 62 61 6c tree. Since bal
18692 61 6e 63 65 28 29 20 6d 61 79 20 6d 6f 76 65 0a ance() may move.
18693 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2c ** the cursor,
18694 20 7a 65 72 6f 20 74 68 65 20 42 74 43 75 72 73 zero the BtCurs
18695 6f 72 2e 69 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e or.info.nSize an
18696 64 20 42 74 43 75 72 73 6f 72 2e 76 61 6c 69 64 d BtCursor.valid
18697 4e 4b 65 79 0a 20 20 2a 2a 20 76 61 72 69 61 62 NKey. ** variab
18698 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 les.. **. ** P
18699 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 revious versions
1869a 20 6f 66 20 53 51 4c 69 74 65 20 63 61 6c 6c 65 of SQLite calle
1869b 64 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74 d moveToRoot() t
1869c 6f 20 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f o move the curso
1869d 72 0a 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74 r. ** back to t
1869e 68 65 20 72 6f 6f 74 20 70 61 67 65 20 61 73 20 he root page as
1869f 62 61 6c 61 6e 63 65 28 29 20 75 73 65 64 20 74 balance() used t
186a0 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 o invalidate the
186a1 20 63 6f 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f contents. ** o
186a2 66 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 67 f BtCursor.apPag
186a3 65 5b 5d 20 61 6e 64 20 42 74 43 75 72 73 6f 72 e[] and BtCursor
186a4 2e 61 69 49 64 78 5b 5d 2e 20 49 6e 73 74 65 61 .aiIdx[]. Instea
186a5 64 20 6f 66 20 64 6f 69 6e 67 20 74 68 61 74 2c d of doing that,
186a6 0a 20 20 2a 2a 20 73 65 74 20 74 68 65 20 63 75 . ** set the cu
186a7 72 73 6f 72 20 73 74 61 74 65 20 74 6f 20 22 69 rsor state to "i
186a8 6e 76 61 6c 69 64 22 2e 20 54 68 69 73 20 6d 61 nvalid". This ma
186a9 6b 65 73 20 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 kes common inser
186aa 74 20 6f 70 65 72 61 74 69 6f 6e 73 0a 20 20 2a t operations. *
186ab 2a 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 * slightly faste
186ac 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 r.. **. ** The
186ad 72 65 20 69 73 20 61 20 73 75 62 74 6c 65 20 62 re is a subtle b
186ae 75 74 20 69 6d 70 6f 72 74 61 6e 74 20 6f 70 74 ut important opt
186af 69 6d 69 7a 61 74 69 6f 6e 20 68 65 72 65 20 74 imization here t
186b0 6f 6f 2e 20 57 68 65 6e 20 69 6e 73 65 72 74 69 oo. When inserti
186b1 6e 67 0a 20 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 ng. ** multiple
186b2 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 6e records into an
186b3 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 20 75 intkey b-tree u
186b4 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63 75 sing a single cu
186b5 72 73 6f 72 20 28 61 73 20 63 61 6e 0a 20 20 2a rsor (as can. *
186b6 2a 20 68 61 70 70 65 6e 20 77 68 69 6c 65 20 70 * happen while p
186b7 72 6f 63 65 73 73 69 6e 67 20 61 6e 20 22 49 4e rocessing an "IN
186b8 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 SERT INTO ... SE
186b9 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 29 LECT" statement)
186ba 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 64 76 , it. ** is adv
186bb 61 6e 74 61 67 65 6f 75 73 20 74 6f 20 6c 65 61 antageous to lea
186bc 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f ve the cursor po
186bd 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 inting to the la
186be 73 74 20 65 6e 74 72 79 20 69 6e 0a 20 20 2a 2a st entry in. **
186bf 20 74 68 65 20 62 2d 74 72 65 65 20 69 66 20 70 the b-tree if p
186c0 6f 73 73 69 62 6c 65 2e 20 49 66 20 74 68 65 20 ossible. If the
186c1 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 cursor is left p
186c2 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c ointing to the l
186c3 61 73 74 0a 20 20 2a 2a 20 65 6e 74 72 79 20 69 ast. ** entry i
186c4 6e 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 n the table, and
186c5 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e the next row in
186c6 73 65 72 74 65 64 20 68 61 73 20 61 6e 20 69 6e serted has an in
186c7 74 65 67 65 72 20 6b 65 79 0a 20 20 2a 2a 20 6c teger key. ** l
186c8 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6c arger than the l
186c9 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 argest existing
186ca 6b 65 79 2c 20 69 74 20 69 73 20 70 6f 73 73 69 key, it is possi
186cb 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68 ble to insert th
186cc 65 0a 20 20 2a 2a 20 72 6f 77 20 77 69 74 68 6f e. ** row witho
186cd 75 74 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 ut seeking the c
186ce 75 72 73 6f 72 2e 20 54 68 69 73 20 63 61 6e 20 ursor. This can
186cf 62 65 20 61 20 62 69 67 20 70 65 72 66 6f 72 6d be a big perform
186d0 61 6e 63 65 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f ance boost.. */
186d1 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 . pCur->info.nS
186d2 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d ize = 0;. pCur-
186d3 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a >validNKey = 0;.
186d4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
186d5 5f 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f _OK && pPage->nO
186d6 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 72 verflow ){. r
186d7 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 c = balance(pCur
186d8 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 );.. /* Must
186d9 6d 61 6b 65 20 73 75 72 65 20 6e 4f 76 65 72 66 make sure nOverf
186da 6c 6f 77 20 69 73 20 72 65 73 65 74 20 74 6f 20 low is reset to
186db 7a 65 72 6f 20 65 76 65 6e 20 69 66 20 74 68 65 zero even if the
186dc 20 62 61 6c 61 6e 63 65 28 29 0a 20 20 20 20 2a balance(). *
186dd 2a 20 66 61 69 6c 73 2e 20 49 6e 74 65 72 6e 61 * fails. Interna
186de 6c 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 l data structure
186df 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c corruption will
186e0 20 72 65 73 75 6c 74 20 6f 74 68 65 72 77 69 73 result otherwis
186e1 65 2e 20 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c e. . ** Also,
186e2 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 set the cursor
186e3 73 74 61 74 65 20 74 6f 20 69 6e 76 61 6c 69 64 state to invalid
186e4 2e 20 54 68 69 73 20 73 74 6f 70 73 20 73 61 76 . This stops sav
186e5 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 eCursorPosition(
186e6 29 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 ). ** from tr
186e7 79 69 6e 67 20 74 6f 20 73 61 76 65 20 74 68 65 ying to save the
186e8 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f current positio
186e9 6e 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e n of the cursor.
186ea 20 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61 */. pCur->a
186eb 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 pPage[pCur->iPag
186ec 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 e]->nOverflow =
186ed 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 0;. pCur->eSt
186ee 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 ate = CURSOR_INV
186ef 41 4c 49 44 3b 0a 20 20 7d 0a 20 20 61 73 73 65 ALID;. }. asse
186f0 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 rt( pCur->apPage
186f1 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e [pCur->iPage]->n
186f2 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a Overflow==0 );..
186f3 65 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 end_insert:. re
186f4 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
186f5 2a 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 * Delete the ent
186f6 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 ry that the curs
186f7 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 or is pointing t
186f8 6f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a o. The cursor.*
186f9 2a 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 * is left pointi
186fa 6e 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 ng at a arbitrar
186fb 79 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 y location..*/.S
186fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
186fd 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 t sqlite3BtreeDe
186fe 6c 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 lete(BtCursor *p
186ff 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 Cur){. Btree *p
18700 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b = pCur->pBtree;
18701 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
18702 20 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 = p->pBt;
18703 20 20 20 20 20 20 20 20 0a 20 20 69 6e 74 20 72 . int r
18704 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
18705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18706 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a /* Return code *
18707 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 /. MemPage *pPa
18708 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ge;
18709 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 /* Page
1870a 20 74 6f 20 64 65 6c 65 74 65 20 63 65 6c 6c 20 to delete cell
1870b 66 72 6f 6d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e from */. unsign
1870c 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 20 ed char *pCell;
1870d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1870e 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c * Pointer to cel
1870f 6c 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 l to delete */.
18710 20 69 6e 74 20 69 43 65 6c 6c 49 64 78 3b 20 20 int iCellIdx;
18711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18712 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
18713 66 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 f cell to delete
18714 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 44 */. int iCellD
18715 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 epth;
18716 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 /* De
18717 70 74 68 20 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 pth of node cont
18718 61 69 6e 69 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 aining pCell */
18719 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 .. assert( curs
1871a 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 orHoldsMutex(pCu
1871b 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r) );. assert(
1871c 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 pBt->inTransacti
1871d 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 on==TRANS_WRITE
1871e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 );. assert( !pB
1871f 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 t->readOnly );.
18720 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 assert( pCur->w
18721 72 46 6c 61 67 20 29 3b 0a 20 20 61 73 73 65 72 rFlag );. asser
18722 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 t( hasSharedCach
18723 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 eTableLock(p, pC
18724 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 ur->pgnoRoot, pC
18725 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c ur->pKeyInfo!=0,
18726 20 32 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 2) );. assert(
18727 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 !hasReadConflic
18728 74 73 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f ts(p, pCur->pgno
18729 52 6f 6f 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 Root) );.. if(
1872a 4e 45 56 45 52 28 70 43 75 72 2d 3e 61 69 49 64 NEVER(pCur->aiId
1872b 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d x[pCur->iPage]>=
1872c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
1872d 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c r->iPage]->nCell
1872e 29 20 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 70 ) . || NEVER(p
1872f 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 Cur->eState!=CUR
18730 53 4f 52 5f 56 41 4c 49 44 29 0a 20 20 29 7b 0a SOR_VALID). ){.
18731 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
18732 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d E_ERROR; /* Som
18733 65 74 68 69 6e 67 20 68 61 73 20 67 6f 6e 65 20 ething has gone
18734 61 77 72 79 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 awry. */. }..
18735 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
18736 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e delete operation
18737 20 74 6f 20 72 65 6d 6f 76 65 20 61 20 72 6f 77 to remove a row
18738 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d from a table b-
18739 74 72 65 65 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c tree,. ** inval
1873a 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c idate any incrbl
1873b 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 ob cursors open
1873c 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 on the row being
1873d 20 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 deleted. */.
1873e 69 66 28 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e if( pCur->pKeyIn
1873f 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 fo==0 ){. inv
18740 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 alidateIncrblobC
18741 75 72 73 6f 72 73 28 70 2c 20 70 43 75 72 2d 3e ursors(p, pCur->
18742 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 info.nKey, 0);.
18743 20 7d 0a 0a 20 20 69 43 65 6c 6c 44 65 70 74 68 }.. iCellDepth
18744 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a = pCur->iPage;.
18745 20 20 69 43 65 6c 6c 49 64 78 20 3d 20 70 43 75 iCellIdx = pCu
18746 72 2d 3e 61 69 49 64 78 5b 69 43 65 6c 6c 44 65 r->aiIdx[iCellDe
18747 70 74 68 5d 3b 0a 20 20 70 50 61 67 65 20 3d 20 pth];. pPage =
18748 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 pCur->apPage[iCe
18749 6c 6c 44 65 70 74 68 5d 3b 0a 20 20 70 43 65 6c llDepth];. pCel
1874a 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 l = findCell(pPa
1874b 67 65 2c 20 69 43 65 6c 6c 49 64 78 29 3b 0a 0a ge, iCellIdx);..
1874c 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 /* If the page
1874d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1874e 65 6e 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 entry to delete
1874f 69 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 is not a leaf pa
18750 67 65 2c 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 ge, move. ** th
18751 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 e cursor to the
18752 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 69 6e largest entry in
18753 20 74 68 65 20 74 72 65 65 20 74 68 61 74 20 69 the tree that i
18754 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 0a 20 s smaller than.
18755 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 62 65 ** the entry be
18756 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 ing deleted. Thi
18757 73 20 63 65 6c 6c 20 77 69 6c 6c 20 72 65 70 6c s cell will repl
18758 61 63 65 20 74 68 65 20 63 65 6c 6c 20 62 65 69 ace the cell bei
18759 6e 67 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 ng deleted. **
1875a 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 from the interna
1875b 6c 20 6e 6f 64 65 2e 20 54 68 65 20 27 70 72 65 l node. The 'pre
1875c 76 69 6f 75 73 27 20 65 6e 74 72 79 20 69 73 20 vious' entry is
1875d 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 69 6e used for this in
1875e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 74 68 stead. ** of th
1875f 65 20 27 6e 65 78 74 27 20 65 6e 74 72 79 2c 20 e 'next' entry,
18760 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 as the previous
18761 65 6e 74 72 79 20 69 73 20 61 6c 77 61 79 73 20 entry is always
18762 61 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 a part of the.
18763 2a 2a 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 ** sub-tree head
18764 65 64 20 62 79 20 74 68 65 20 63 68 69 6c 64 20 ed by the child
18765 70 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c page of the cell
18766 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 being deleted.
18767 54 68 69 73 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 This makes. **
18768 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 74 72 balancing the tr
18769 65 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 ee following the
1876a 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f delete operatio
1876b 6e 20 65 61 73 69 65 72 2e 20 20 2a 2f 0a 20 20 n easier. */.
1876c 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 if( !pPage->leaf
1876d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 ){. int notU
1876e 73 65 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 sed;. rc = sq
1876f 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f lite3BtreePrevio
18770 75 73 28 70 43 75 72 2c 20 26 6e 6f 74 55 73 65 us(pCur, ¬Use
18771 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 d);. if( rc )
18772 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a return rc;. }.
18773 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 . /* Save the p
18774 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 ositions of any
18775 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 other cursors op
18776 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 en on this table
18777 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b before. ** mak
18778 69 6e 67 20 61 6e 79 20 6d 6f 64 69 66 69 63 61 ing any modifica
18779 74 69 6f 6e 73 2e 20 4d 61 6b 65 20 74 68 65 20 tions. Make the
1877a 70 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 page containing
1877b 74 68 65 20 65 6e 74 72 79 20 74 6f 20 62 65 20 the entry to be
1877c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 77 72 . ** deleted wr
1877d 69 74 61 62 6c 65 2e 20 54 68 65 6e 20 66 72 65 itable. Then fre
1877e 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 e any overflow p
1877f 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 ages associated
18780 77 69 74 68 20 74 68 65 20 0a 20 20 2a 2a 20 65 with the . ** e
18781 6e 74 72 79 20 61 6e 64 20 66 69 6e 61 6c 6c 79 ntry and finally
18782 20 72 65 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c remove the cell
18783 20 69 74 73 65 6c 66 20 66 72 6f 6d 20 77 69 74 itself from wit
18784 68 69 6e 20 74 68 65 20 70 61 67 65 2e 20 20 0a hin the page. .
18785 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 */. rc = save
18786 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 AllCursors(pBt,
18787 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 pCur->pgnoRoot,
18788 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 pCur);. if( rc
18789 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 ) return rc;. r
1878a 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 c = sqlite3Pager
1878b 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 Write(pPage->pDb
1878c 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 Page);. if( rc
1878d 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 ) return rc;. r
1878e 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 c = clearCell(pP
1878f 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 64 age, pCell);. d
18790 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 ropCell(pPage, i
18791 43 65 6c 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a CellIdx, cellSiz
18792 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c ePtr(pPage, pCel
18793 6c 29 2c 20 26 72 63 29 3b 0a 20 20 69 66 28 20 l), &rc);. if(
18794 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a rc ) return rc;.
18795 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c . /* If the cel
18796 6c 20 64 65 6c 65 74 65 64 20 77 61 73 20 6e 6f l deleted was no
18797 74 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 6c t located on a l
18798 65 61 66 20 70 61 67 65 2c 20 74 68 65 6e 20 74 eaf page, then t
18799 68 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 69 he cursor. ** i
1879a 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e s currently poin
1879b 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 72 67 ting to the larg
1879c 65 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 est entry in the
1879d 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 sub-tree headed
1879e 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 63 68 69 . ** by the chi
1879f 6c 64 2d 70 61 67 65 20 6f 66 20 74 68 65 20 63 ld-page of the c
187a0 65 6c 6c 20 74 68 61 74 20 77 61 73 20 6a 75 73 ell that was jus
187a1 74 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 t deleted from a
187a2 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 n internal. **
187a3 6e 6f 64 65 2e 20 54 68 65 20 63 65 6c 6c 20 66 node. The cell f
187a4 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 rom the leaf nod
187a5 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6d 6f e needs to be mo
187a6 76 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 ved to the inter
187a7 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 20 74 6f nal. ** node to
187a8 20 72 65 70 6c 61 63 65 20 74 68 65 20 64 65 6c replace the del
187a9 65 74 65 64 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 eted cell. */.
187aa 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 if( !pPage->lea
187ab 66 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 f ){. MemPage
187ac 20 2a 70 4c 65 61 66 20 3d 20 70 43 75 72 2d 3e *pLeaf = pCur->
187ad 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 apPage[pCur->iPa
187ae 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 ge];. int nCe
187af 6c 6c 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 20 3d ll;. Pgno n =
187b0 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 pCur->apPage[iC
187b1 65 6c 6c 44 65 70 74 68 2b 31 5d 2d 3e 70 67 6e ellDepth+1]->pgn
187b2 6f 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 o;. unsigned
187b3 63 68 61 72 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 char *pTmp;..
187b4 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c pCell = findCel
187b5 6c 28 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e l(pLeaf, pLeaf->
187b6 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 nCell-1);. nC
187b7 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 ell = cellSizePt
187b8 72 28 70 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b r(pLeaf, pCell);
187b9 0a 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f . assert( MX_
187ba 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d CELL_SIZE(pBt)>=
187bb 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 61 6c nCell );.. al
187bc 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 locateTempSpace(
187bd 70 42 74 29 3b 0a 20 20 20 20 70 54 6d 70 20 3d pBt);. pTmp =
187be 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b pBt->pTmpSpace;
187bf 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 .. rc = sqlit
187c0 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 65 e3PagerWrite(pLe
187c1 61 66 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 af->pDbPage);.
187c2 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 insertCell(pPa
187c3 67 65 2c 20 69 43 65 6c 6c 49 64 78 2c 20 70 43 ge, iCellIdx, pC
187c4 65 6c 6c 2d 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 ell-4, nCell+4,
187c5 70 54 6d 70 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 pTmp, n, &rc);.
187c6 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 dropCell(pLea
187c7 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d f, pLeaf->nCell-
187c8 31 2c 20 6e 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 1, nCell, &rc);.
187c9 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
187ca 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f urn rc;. }.. /
187cb 2a 20 42 61 6c 61 6e 63 65 20 74 68 65 20 74 72 * Balance the tr
187cc 65 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79 ee. If the entry
187cd 20 64 65 6c 65 74 65 64 20 77 61 73 20 6c 6f 63 deleted was loc
187ce 61 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 ated on a leaf p
187cf 61 67 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 age,. ** then t
187d0 68 65 20 63 75 72 73 6f 72 20 73 74 69 6c 6c 20 he cursor still
187d1 70 6f 69 6e 74 73 20 74 6f 20 74 68 61 74 20 70 points to that p
187d2 61 67 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 age. In this cas
187d3 65 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a e the first. **
187d4 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 call to balance
187d5 28 29 20 72 65 70 61 69 72 73 20 74 68 65 20 74 () repairs the t
187d6 72 65 65 2c 20 61 6e 64 20 74 68 65 20 69 66 28 ree, and the if(
187d7 2e 2e 2e 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69 ...) condition i
187d8 73 0a 20 20 2a 2a 20 6e 65 76 65 72 20 74 72 75 s. ** never tru
187d9 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 e.. **. ** Oth
187da 65 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 65 erwise, if the e
187db 6e 74 72 79 20 64 65 6c 65 74 65 64 20 77 61 73 ntry deleted was
187dc 20 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 on an internal
187dd 6e 6f 64 65 20 70 61 67 65 2c 20 74 68 65 6e 0a node page, then.
187de 20 20 2a 2a 20 70 43 75 72 20 69 73 20 70 6f 69 ** pCur is poi
187df 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 65 61 nting to the lea
187e0 66 20 70 61 67 65 20 66 72 6f 6d 20 77 68 69 63 f page from whic
187e1 68 20 61 20 63 65 6c 6c 20 77 61 73 20 72 65 6d h a cell was rem
187e2 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 72 65 70 oved to. ** rep
187e3 6c 61 63 65 20 74 68 65 20 63 65 6c 6c 20 64 65 lace the cell de
187e4 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 69 leted from the i
187e5 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 nternal node. Th
187e6 69 73 20 69 73 20 73 6c 69 67 68 74 6c 79 0a 20 is is slightly.
187e7 20 2a 2a 20 74 72 69 63 6b 79 20 61 73 20 74 68 ** tricky as th
187e8 65 20 6c 65 61 66 20 6e 6f 64 65 20 6d 61 79 20 e leaf node may
187e9 62 65 20 75 6e 64 65 72 66 75 6c 6c 2c 20 61 6e be underfull, an
187ea 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e d the internal n
187eb 6f 64 65 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20 ode may. ** be
187ec 65 69 74 68 65 72 20 75 6e 64 65 72 20 6f 72 20 either under or
187ed 6f 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 overfull. In thi
187ee 73 20 63 61 73 65 20 72 75 6e 20 74 68 65 20 62 s case run the b
187ef 61 6c 61 6e 63 69 6e 67 20 61 6c 67 6f 72 69 74 alancing algorit
187f0 68 6d 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c hm. ** on the l
187f1 65 61 66 20 6e 6f 64 65 20 66 69 72 73 74 2e 20 eaf node first.
187f2 49 66 20 74 68 65 20 62 61 6c 61 6e 63 65 20 70 If the balance p
187f3 72 6f 63 65 65 64 73 20 66 61 72 20 65 6e 6f 75 roceeds far enou
187f4 67 68 20 75 70 20 74 68 65 0a 20 20 2a 2a 20 74 gh up the. ** t
187f5 72 65 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 ree that we can
187f6 62 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 79 be sure that any
187f7 20 70 72 6f 62 6c 65 6d 20 69 6e 20 74 68 65 20 problem in the
187f8 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 68 61 internal node ha
187f9 73 0a 20 20 2a 2a 20 62 65 65 6e 20 63 6f 72 72 s. ** been corr
187fa 65 63 74 65 64 2c 20 73 6f 20 62 65 20 69 74 2e ected, so be it.
187fb 20 4f 74 68 65 72 77 69 73 65 2c 20 61 66 74 65 Otherwise, afte
187fc 72 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 r balancing the
187fd 6c 65 61 66 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 leaf node,. **
187fe 77 61 6c 6b 20 74 68 65 20 63 75 72 73 6f 72 20 walk the cursor
187ff 75 70 20 74 68 65 20 74 72 65 65 20 74 6f 20 74 up the tree to t
18800 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 he internal node
18801 20 61 6e 64 20 62 61 6c 61 6e 63 65 20 69 74 20 and balance it
18802 61 73 20 0a 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 as . ** well.
18803 2a 2f 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 */. rc = balanc
18804 65 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 e(pCur);. if( r
18805 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
18806 70 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c pCur->iPage>iCel
18807 6c 44 65 70 74 68 20 29 7b 0a 20 20 20 20 77 68 lDepth ){. wh
18808 69 6c 65 28 20 70 43 75 72 2d 3e 69 50 61 67 65 ile( pCur->iPage
18809 3e 69 43 65 6c 6c 44 65 70 74 68 20 29 7b 0a 20 >iCellDepth ){.
1880a 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 releasePage
1880b 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 (pCur->apPage[pC
1880c 75 72 2d 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 ur->iPage--]);.
1880d 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 62 61 }. rc = ba
1880e 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 20 20 7d lance(pCur);. }
1880f 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 .. if( rc==SQLI
18810 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 TE_OK ){. mov
18811 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 eToRoot(pCur);.
18812 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a }. return rc;.
18813 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 }../*.** Create
18814 61 20 6e 65 77 20 42 54 72 65 65 20 74 61 62 6c a new BTree tabl
18815 65 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a e. Write into *
18816 70 69 54 61 62 6c 65 20 74 68 65 20 70 61 67 65 piTable the page
18817 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 .** number for t
18818 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 he root page of
18819 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a the new table..*
1881a 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 6f 66 *.** The type of
1881b 20 74 79 70 65 20 69 73 20 64 65 74 65 72 6d 69 type is determi
1881c 6e 65 64 20 62 79 20 74 68 65 20 66 6c 61 67 73 ned by the flags
1881d 20 70 61 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c parameter. Onl
1881e 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 y the.** followi
1881f 6e 67 20 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 ng values of fla
18820 67 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 gs are currently
18821 20 69 6e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 in use. Other
18822 76 61 6c 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c values for.** fl
18823 61 67 73 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f ags might not wo
18824 72 6b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 rk:.**.** BT
18825 52 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 REE_INTKEY|BTREE
18826 5f 4c 45 41 46 44 41 54 41 20 20 20 20 20 55 73 _LEAFDATA Us
18827 65 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 ed for SQL table
18828 73 20 77 69 74 68 20 72 6f 77 69 64 20 6b 65 79 s with rowid key
18829 73 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 5a s.** BTREE_Z
1882a 45 52 4f 44 41 54 41 20 20 20 20 20 20 20 20 20 ERODATA
1882b 20 20 20 20 20 20 20 20 20 55 73 65 64 20 66 6f Used fo
1882c 72 20 53 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f r SQL indices.*/
1882d 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 .static int btre
1882e 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 eCreateTable(Btr
1882f 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 ee *p, int *piTa
18830 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b ble, int flags){
18831 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
18832 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d = p->pBt;. Mem
18833 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 Page *pRoot;. P
18834 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 gno pgnoRoot;.
18835 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
18836 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 t( sqlite3BtreeH
18837 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a oldsMutex(p) );.
18838 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 assert( pBt->i
18839 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 nTransaction==TR
1883a 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 ANS_WRITE );. a
1883b 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 ssert( !pBt->rea
1883c 64 4f 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 dOnly );..#ifdef
1883d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
1883e 4f 56 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 OVACUUM. rc = a
1883f 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 llocateBtreePage
18840 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 (pBt, &pRoot, &p
18841 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a gnoRoot, 1, 0);.
18842 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
18843 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 return rc;. }.#
18844 65 6c 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e else. if( pBt->
18845 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 autoVacuum ){.
18846 20 20 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b Pgno pgnoMove;
18847 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 /* Move a
18848 70 61 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b page here to mak
18849 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 e room for the r
1884a 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 oot-page */.
1884b 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f MemPage *pPageMo
1884c 76 65 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 ve; /* The page
1884d 74 6f 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a to move to. */..
1884e 20 20 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 /* Creating
1884f 61 20 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 a new table may
18850 70 72 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 probably require
18851 20 6d 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 moving an exist
18852 69 6e 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 ing database.
18853 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d ** to make room
18854 20 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 for the new tab
18855 6c 65 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 les root page. I
18856 6e 20 63 61 73 65 20 74 68 69 73 20 70 61 67 65 n case this page
18857 20 74 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 turns. ** ou
18858 74 20 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 t to be an overf
18859 6c 6f 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 low page, delete
1885a 20 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 all overflow pa
1885b 67 65 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 ge-map caches.
1885c 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 ** held by ope
1885d 6e 20 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a n cursors.. *
1885e 2f 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 /. invalidate
1885f 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 AllOverflowCache
18860 28 70 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 (pBt);.. /* R
18861 65 61 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ead the value of
18862 20 6d 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 meta[3] from th
18863 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 e database to de
18864 74 65 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 termine where th
18865 65 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 e. ** root pa
18866 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 ge of the new ta
18867 62 6c 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d ble should go. m
18868 65 74 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 eta[3] is the la
18869 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a rgest root-page.
1886a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 ** created s
1886b 6f 20 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 o far, so the ne
1886c 77 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 w root-page is (
1886d 6d 65 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 meta[3]+1)..
1886e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 */. sqlite3Bt
1886f 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 reeGetMeta(p, BT
18870 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 REE_LARGEST_ROOT
18871 5f 50 41 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 _PAGE, &pgnoRoot
18872 29 3b 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b );. pgnoRoot+
18873 2b 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e +;.. /* The n
18874 65 77 20 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 ew root-page may
18875 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 not be allocate
18876 64 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d d on a pointer-m
18877 61 70 20 70 61 67 65 2c 20 6f 72 20 74 68 65 0a ap page, or the.
18878 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 ** PENDING_B
18879 59 54 45 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f YTE page.. */
1887a 0a 20 20 20 20 77 68 69 6c 65 28 20 70 67 6e 6f . while( pgno
1887b 52 6f 6f 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 Root==PTRMAP_PAG
1887c 45 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f ENO(pBt, pgnoRoo
1887d 74 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 t) ||. pg
1887e 6e 6f 52 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f noRoot==PENDING_
1887f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 BYTE_PAGE(pBt) )
18880 7b 0a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 {. pgnoRoot
18881 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 ++;. }. as
18882 73 65 72 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d sert( pgnoRoot>=
18883 33 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 3 );.. /* All
18884 6f 63 61 74 65 20 61 20 70 61 67 65 2e 20 54 68 ocate a page. Th
18885 65 20 70 61 67 65 20 74 68 61 74 20 63 75 72 72 e page that curr
18886 65 6e 74 6c 79 20 72 65 73 69 64 65 73 20 61 74 ently resides at
18887 20 70 67 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 pgnoRoot will.
18888 20 20 20 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 ** be moved t
18889 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 o the allocated
1888a 70 61 67 65 20 28 75 6e 6c 65 73 73 20 74 68 65 page (unless the
1888b 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 allocated page
1888c 68 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 happens. ** t
1888d 6f 20 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f o reside at pgno
1888e 52 6f 6f 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 Root).. */.
1888f 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 rc = allocateB
18890 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 treePage(pBt, &p
18891 50 61 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d PageMove, &pgnoM
18892 6f 76 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 ove, pgnoRoot, 1
18893 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 );. if( rc!=S
18894 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18895 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 return rc;.
18896 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 67 6e 6f }.. if( pgno
18897 4d 6f 76 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 Move!=pgnoRoot )
18898 7b 0a 20 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 {. /* pgnoR
18899 6f 6f 74 20 69 73 20 74 68 65 20 70 61 67 65 20 oot is the page
1889a 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 that will be use
1889b 64 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 d for the root-p
1889c 61 67 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 age of. **
1889d 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 61 the new table (a
1889e 73 73 75 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 ssuming an error
1889f 20 64 69 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e did not occur).
188a0 20 42 75 74 20 77 65 20 77 65 72 65 0a 20 20 20 But we were.
188a1 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 ** allocated
188a2 70 67 6e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 pgnoMove. If req
188a3 75 69 72 65 64 20 28 69 2e 65 2e 20 69 66 20 69 uired (i.e. if i
188a4 74 20 77 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 t was not alloca
188a5 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 ted. ** by
188a6 65 78 74 65 6e 64 69 6e 67 20 74 68 65 20 66 69 extending the fi
188a7 6c 65 29 2c 20 74 68 65 20 63 75 72 72 65 6e 74 le), the current
188a8 20 70 61 67 65 20 61 74 20 70 6f 73 69 74 69 6f page at positio
188a9 6e 20 70 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 n pgnoMove.
188aa 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6a ** is already j
188ab 6f 75 72 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 ournaled..
188ac 2a 2f 0a 20 20 20 20 20 20 75 38 20 65 54 79 70 */. u8 eTyp
188ad 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 50 67 6e e = 0;. Pgn
188ae 6f 20 69 50 74 72 50 61 67 65 20 3d 20 30 3b 0a o iPtrPage = 0;.
188af 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 . releasePa
188b0 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a ge(pPageMove);..
188b1 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 /* Move th
188b2 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79 e page currently
188b3 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 at pgnoRoot to
188b4 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 pgnoMove. */.
188b5 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 rc = btreeGet
188b6 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f Page(pBt, pgnoRo
188b7 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a ot, &pRoot, 0);.
188b8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
188b9 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
188ba 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 return rc;.
188bb 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d }. rc =
188bc 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 ptrmapGet(pBt,
188bd 70 67 6e 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 pgnoRoot, &eType
188be 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 , &iPtrPage);.
188bf 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 if( eType==P
188c0 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c TRMAP_ROOTPAGE |
188c1 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f | eType==PTRMAP_
188c2 46 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 FREEPAGE ){.
188c3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
188c4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 CORRUPT_BKPT;.
188c5 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
188c6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
188c7 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
188c8 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 Page(pRoot);.
188c9 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
188ca 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
188cb 73 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 sert( eType!=PTR
188cc 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a MAP_ROOTPAGE );.
188cd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 assert( eT
188ce 79 70 65 21 3d 50 54 52 4d 41 50 5f 46 52 45 45 ype!=PTRMAP_FREE
188cf 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20 72 63 PAGE );. rc
188d0 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 = relocatePage(
188d1 70 42 74 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70 pBt, pRoot, eTyp
188d2 65 2c 20 69 50 74 72 50 61 67 65 2c 20 70 67 6e e, iPtrPage, pgn
188d3 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 oMove, 0);.
188d4 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f releasePage(pRo
188d5 6f 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f ot);.. /* O
188d6 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 61 btain the page a
188d7 74 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 t pgnoRoot */.
188d8 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
188d9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
188da 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 return rc;.
188db 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 62 }. rc = b
188dc 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c treeGetPage(pBt,
188dd 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f pgnoRoot, &pRoo
188de 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 t, 0);. if(
188df 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
188e0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
188e1 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
188e2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 rc = sqlite3P
188e3 61 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d agerWrite(pRoot-
188e4 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 >pDbPage);.
188e5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
188e6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 OK ){. re
188e7 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 leasePage(pRoot)
188e8 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e ;. return
188e9 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 rc;. }.
188ea 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 }else{. pR
188eb 6f 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b oot = pPageMove;
188ec 0a 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 . } .. /*
188ed 55 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 Update the point
188ee 65 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d er-map and meta-
188ef 64 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 data with the ne
188f0 77 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 w root-page numb
188f1 65 72 2e 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 er. */. ptrma
188f2 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f pPut(pBt, pgnoRo
188f3 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 ot, PTRMAP_ROOTP
188f4 41 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 AGE, 0, &rc);.
188f5 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
188f6 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 releasePage(pR
188f7 6f 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 oot);. retu
188f8 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 rn rc;. }.
188f9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
188fa 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 eeUpdateMeta(p,
188fb 34 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 4, pgnoRoot);.
188fc 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
188fd 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 releasePage(pR
188fe 6f 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 oot);. retu
188ff 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
18900 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
18901 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 allocateBtreePag
18902 65 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 e(pBt, &pRoot, &
18903 70 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b pgnoRoot, 1, 0);
18904 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 . if( rc ) re
18905 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e turn rc;. }.#en
18906 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 dif. assert( sq
18907 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 lite3PagerIswrit
18908 65 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 eable(pRoot->pDb
18909 50 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 Page) );. zeroP
1890a 61 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 age(pRoot, flags
1890b 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 | PTF_LEAF);.
1890c 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
1890d 66 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 f(pRoot->pDbPage
1890e 29 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 );. *piTable =
1890f 28 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 (int)pgnoRoot;.
18910 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
18911 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 K;.}.SQLITE_PRIV
18912 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 ATE int sqlite3B
18913 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 treeCreateTable(
18914 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 Btree *p, int *p
18915 69 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 iTable, int flag
18916 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
18917 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
18918 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 r(p);. rc = btr
18919 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c eeCreateTable(p,
1891a 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 piTable, flags)
1891b 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
1891c 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 Leave(p);. retu
1891d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1891e 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 Erase the given
1891f 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e database page an
18920 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 d all its childr
18921 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 en. Return.** t
18922 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 he page to the f
18923 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 reelist..*/.stat
18924 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 ic int clearData
18925 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 basePage(. BtSh
18926 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 ared *pBt,
18927 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 /* The BTre
18928 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 e that contains
18929 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 the table */. P
1892a 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 gno pgno,
1892b 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d /* Page num
1892c 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a ber to clear */.
1892d 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c int freePageFl
1892e 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c ag, /* Deall
1892f 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72 ocate page if tr
18930 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 ue */. int *pnC
18931 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 hange.){. MemPa
18932 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 ge *pPage;. int
18933 20 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 rc;. unsigned
18934 63 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 char *pCell;. i
18935 6e 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 nt i;.. assert(
18936 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
18937 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 eld(pBt->mutex)
18938 29 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 );. if( pgno>pa
18939 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 gerPagecount(pBt
1893a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 ) ){. return
1893b 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 SQLITE_CORRUPT_B
1893c 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d KPT;. }.. rc =
1893d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 getAndInitPage(
1893e 70 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 pBt, pgno, &pPag
1893f 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 e);. if( rc ) r
18940 65 74 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 eturn rc;. for(
18941 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 i=0; i<pPage->nC
18942 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 ell; i++){. p
18943 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 Cell = findCell(
18944 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 pPage, i);. i
18945 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 f( !pPage->leaf
18946 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c ){. rc = cl
18947 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 earDatabasePage(
18948 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 pBt, get4byte(pC
18949 65 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 ell), 1, pnChang
1894a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 e);. if( rc
1894b 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 ) goto cleardat
1894c 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 abasepage_out;.
1894d 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c }. rc = cl
1894e 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 earCell(pPage, p
1894f 43 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 Cell);. if( r
18950 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 c ) goto clearda
18951 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a tabasepage_out;.
18952 20 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 }. if( !pPage
18953 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63 ->leaf ){. rc
18954 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 = clearDatabase
18955 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 Page(pBt, get4by
18956 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 te(&pPage->aData
18957 5b 38 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 [8]), 1, pnChang
18958 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 e);. if( rc )
18959 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 goto cleardatab
1895a 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d asepage_out;. }
1895b 65 6c 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 else if( pnChang
1895c 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 e ){. assert(
1895d 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 pPage->intKey )
1895e 3b 0a 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 ;. *pnChange
1895f 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b += pPage->nCell;
18960 0a 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 . }. if( freeP
18961 61 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 ageFlag ){. f
18962 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 reePage(pPage, &
18963 72 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 rc);. }else if(
18964 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 (rc = sqlite3Pa
18965 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e gerWrite(pPage->
18966 70 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a pDbPage))==0 ){.
18967 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 zeroPage(pPa
18968 67 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 ge, pPage->aData
18969 5b 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b [0] | PTF_LEAF);
1896a 0a 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 . }..cleardatab
1896b 61 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 asepage_out:. r
1896c 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 eleasePage(pPage
1896d 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
1896e 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 }../*.** Delete
1896f 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 all information
18970 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 from a single ta
18971 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 ble in the datab
18972 61 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a ase. iTable is.
18973 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 ** the page numb
18974 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f er of the root o
18975 66 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 f the table. Af
18976 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
18977 20 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 returns,.** the
18978 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d root page is em
18979 70 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 pty, but still e
1897a 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 xists..**.** Thi
1897b 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 s routine will f
1897c 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f ail with SQLITE_
1897d 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 LOCKED if there
1897e 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 are any open.**
1897f 72 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 read cursors on
18980 74 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e the table. Open
18981 20 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 write cursors a
18982 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a re moved to the.
18983 2a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 ** root of the t
18984 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 able..**.** If p
18985 6e 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e nChange is not N
18986 55 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 ULL, then table
18987 69 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 iTable must be a
18988 6e 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 n intkey table.
18989 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 The.** integer v
1898a 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 alue pointed to
1898b 62 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 by pnChange is i
1898c 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 ncremented by th
1898d 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 e number of.** e
1898e 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 ntries in the ta
1898f 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ble..*/.SQLITE_P
18990 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
18991 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c e3BtreeClearTabl
18992 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 e(Btree *p, int
18993 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 iTable, int *pnC
18994 68 61 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 hange){. int rc
18995 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 ;. BtShared *pB
18996 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 t = p->pBt;. sq
18997 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
18998 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d p);. assert( p-
18999 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
1899a 57 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 WRITE );.. /* I
1899b 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e nvalidate all in
1899c 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f crblob cursors o
1899d 70 65 6e 20 6f 6e 20 74 61 62 6c 65 20 69 54 61 pen on table iTa
1899e 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 69 54 ble (assuming iT
1899f 61 62 6c 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 able. ** is the
189a0 20 72 6f 6f 74 20 6f 66 20 61 20 74 61 62 6c 65 root of a table
189a1 20 62 2d 74 72 65 65 20 2d 20 69 66 20 69 74 20 b-tree - if it
189a2 69 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c is not, the foll
189a3 6f 77 69 6e 67 20 63 61 6c 6c 20 69 73 0a 20 20 owing call is.
189a4 2a 2a 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f ** a no-op). */
189a5 0a 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 . invalidateInc
189a6 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 rblobCursors(p,
189a7 30 2c 20 31 29 3b 0a 0a 20 20 72 63 20 3d 20 73 0, 1);.. rc = s
189a8 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 aveAllCursors(pB
189a9 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c t, (Pgno)iTable,
189aa 20 30 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 0);. if( SQLIT
189ab 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 E_OK==rc ){.
189ac 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 rc = clearDataba
189ad 73 65 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e sePage(pBt, (Pgn
189ae 6f 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e 43 o)iTable, 0, pnC
189af 68 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 hange);. }. sq
189b0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
189b1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b p);. return rc;
189b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 .}../*.** Erase
189b3 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 all information
189b4 69 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 in a table and a
189b5 64 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 dd the root of t
189b6 68 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 he table to.** t
189b7 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78 he freelist. Ex
189b8 63 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f cept, the root o
189b9 66 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20 f the principle
189ba 74 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f table (the one o
189bb 6e 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20 n.** page 1) is
189bc 6e 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74 never added to t
189bd 68 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a he freelist..**.
189be 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
189bf 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 will fail with S
189c0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 QLITE_LOCKED if
189c1 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 there are any op
189c2 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e en.** cursors on
189c3 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a the table..**.*
189c4 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 * If AUTOVACUUM
189c5 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 is enabled and t
189c6 68 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c he page at iTabl
189c7 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 e is not the las
189c8 74 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 t.** root page i
189c9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 n the database f
189ca 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 ile, then the la
189cb 73 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a st root page .**
189cc 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
189cd 20 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 file is moved i
189ce 6e 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 nto the slot for
189cf 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 merly occupied b
189d0 79 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 y.** iTable and
189d1 74 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 that last slot f
189d2 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 ormerly occupied
189d3 20 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f by the last roo
189d4 74 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 t page.** is add
189d5 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 ed to the freeli
189d6 73 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54 st instead of iT
189d7 61 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 able. In this s
189d8 61 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 ay, all.** root
189d9 70 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61 pages are kept a
189da 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 t the beginning
189db 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 of the database
189dc 66 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 file, which.** i
189dd 73 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 s necessary for
189de 41 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f AUTOVACUUM to wo
189df 72 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f rk right. *piMo
189e0 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 ved is set to th
189e1 65 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 e .** page numbe
189e2 72 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62 r that used to b
189e3 65 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 e the last root
189e4 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 page in the file
189e5 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d before.** the m
189e6 6f 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 ove. If no page
189e7 20 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 gets moved, *pi
189e8 4d 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 Moved is set to
189e9 30 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 0..** The last r
189ea 6f 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f oot page is reco
189eb 72 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 rded in meta[3]
189ec 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 and the value of
189ed 0a 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 .** meta[3] is u
189ee 70 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70 pdated by this p
189ef 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 rocedure..*/.sta
189f0 74 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f tic int btreeDro
189f1 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c pTable(Btree *p,
189f2 20 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 6e Pgno iTable, in
189f3 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 t *piMoved){. i
189f4 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 nt rc;. MemPage
189f5 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 *pPage = 0;. B
189f6 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 tShared *pBt = p
189f7 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 ->pBt;.. assert
189f8 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f ( sqlite3BtreeHo
189f9 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 ldsMutex(p) );.
189fa 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
189fb 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 ans==TRANS_WRITE
189fc 20 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 );.. /* It is
189fd 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20 illegal to drop
189fe 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63 a table if any c
189ff 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20 ursors are open
18a00 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 on the. ** data
18a01 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65 base. This is be
18a02 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61 cause in auto-va
18a03 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61 cuum mode the ba
18a04 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e ckend may. ** n
18a05 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 eed to move anot
18a06 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f her root-page to
18a07 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74 fill a gap left
18a08 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a by the deleted.
18a09 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20 ** root page.
18a0a 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f If an open curso
18a0b 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73 r was using this
18a0c 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20 page a problem
18a0d 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75 would . ** occu
18a0e 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 r.. **. ** Thi
18a0f 73 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 s error is caugh
18a10 74 20 6c 6f 6e 67 20 62 65 66 6f 72 65 20 63 6f t long before co
18a11 6e 74 72 6f 6c 20 72 65 61 63 68 65 73 20 74 68 ntrol reaches th
18a12 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 is point.. */.
18a13 20 69 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e if( NEVER(pBt->
18a14 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 pCursor) ){.
18a15 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f sqlite3Connectio
18a16 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 nBlocked(p->db,
18a17 70 42 74 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 pBt->pCursor->pB
18a18 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 tree->db);. r
18a19 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 eturn SQLITE_LOC
18a1a 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b KED_SHAREDCACHE;
18a1b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 62 74 72 . }.. rc = btr
18a1c 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 eeGetPage(pBt, (
18a1d 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 Pgno)iTable, &pP
18a1e 61 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 age, 0);. if( r
18a1f 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 c ) return rc;.
18a20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
18a21 65 65 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 eeClearTable(p,
18a22 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 iTable, 0);. if
18a23 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 ( rc ){. rele
18a24 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a asePage(pPage);.
18a25 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18a26 20 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d }.. *piMoved =
18a27 20 30 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 0;.. if( iTabl
18a28 65 3e 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 e>1 ){.#ifdef SQ
18a29 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
18a2a 43 55 55 4d 0a 20 20 20 20 66 72 65 65 50 61 67 CUUM. freePag
18a2b 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 e(pPage, &rc);.
18a2c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
18a2d 50 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 Page);.#else.
18a2e 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
18a2f 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 cuum ){. Pg
18a30 6e 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a no maxRootPgno;.
18a31 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
18a32 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 eeGetMeta(p, BTR
18a33 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f EE_LARGEST_ROOT_
18a34 50 41 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50 67 PAGE, &maxRootPg
18a35 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 no);.. if(
18a36 69 54 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 iTable==maxRootP
18a37 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f gno ){. /
18a38 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 * If the table b
18a39 65 69 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 eing dropped is
18a3a 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 the table with t
18a3b 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d he largest root-
18a3c 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 page. **
18a3d 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 number in the da
18a3e 74 61 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 tabase, put the
18a3f 72 6f 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 root page on the
18a40 20 66 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 free list. .
18a41 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
18a42 66 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 freePage(pPage,
18a43 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65 &rc);. re
18a44 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 leasePage(pPage)
18a45 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
18a46 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
18a47 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
18a48 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 rc;. }.
18a49 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18a4a 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 /* The table
18a4b 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f being dropped do
18a4c 65 73 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 es not have the
18a4d 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 largest root-pag
18a4e 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d e. ** num
18a4f 62 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 ber in the datab
18a50 61 73 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 ase. So move the
18a51 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 page that does
18a52 69 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 into the .
18a53 20 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 ** gap left by
18a54 20 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f the deleted roo
18a55 74 2d 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 t-page..
18a56 2a 2f 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 */. MemPa
18a57 67 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 ge *pMove;.
18a58 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 releasePage(p
18a59 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 Page);. r
18a5a 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 c = btreeGetPage
18a5b 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e (pBt, maxRootPgn
18a5c 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 o, &pMove, 0);.
18a5d 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 if( rc!=S
18a5e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18a5f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b return rc;
18a60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
18a61 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 rc = relocate
18a62 50 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c Page(pBt, pMove,
18a63 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 PTRMAP_ROOTPAGE
18a64 2c 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b , 0, iTable, 0);
18a65 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 . release
18a66 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 Page(pMove);.
18a67 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
18a68 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
18a69 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 return rc;.
18a6a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
18a6b 20 70 4d 6f 76 65 20 3d 20 30 3b 0a 20 20 20 20 pMove = 0;.
18a6c 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 rc = btreeGe
18a6d 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f tPage(pBt, maxRo
18a6e 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 otPgno, &pMove,
18a6f 30 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 0);. free
18a70 50 61 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29 Page(pMove, &rc)
18a71 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 ;. releas
18a72 65 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 ePage(pMove);.
18a73 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
18a74 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
18a75 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
18a76 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
18a77 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 *piMoved = max
18a78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 RootPgno;.
18a79 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 }.. /* Set
18a7a 74 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f the new 'max-roo
18a7b 74 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e t-page' value in
18a7c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 the database he
18a7d 61 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 ader. This.
18a7e 20 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 ** is the old v
18a7f 61 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c alue less one, l
18a80 65 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 ess one more if
18a81 74 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a that happens to.
18a82 20 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f ** be a ro
18a83 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 ot-page number,
18a84 6c 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 less one again i
18a85 66 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 f that is the.
18a86 20 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 ** PENDING_B
18a87 59 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 YTE_PAGE..
18a88 2a 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 */. maxRoot
18a89 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 77 68 Pgno--;. wh
18a8a 69 6c 65 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f ile( maxRootPgno
18a8b 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 ==PENDING_BYTE_P
18a8c 41 47 45 28 70 42 74 29 0a 20 20 20 20 20 20 20 AGE(pBt).
18a8d 20 20 20 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f || PTRMAP_
18a8e 49 53 50 41 47 45 28 70 42 74 2c 20 6d 61 78 52 ISPAGE(pBt, maxR
18a8f 6f 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 ootPgno) ){.
18a90 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d maxRootPgno-
18a91 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 -;. }.
18a92 20 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 assert( maxRoot
18a93 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 Pgno!=PENDING_BY
18a94 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a TE_PAGE(pBt) );.
18a95 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
18a96 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 te3BtreeUpdateMe
18a97 74 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 ta(p, 4, maxRoot
18a98 50 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Pgno);. }else
18a99 7b 0a 20 20 20 20 20 20 66 72 65 65 50 61 67 65 {. freePage
18a9a 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 (pPage, &rc);.
18a9b 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 releasePage(
18a9c 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 pPage);. }.#e
18a9d 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ndif. }else{.
18a9e 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42 /* If sqlite3B
18a9f 74 72 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61 treeDropTable wa
18aa0 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 s called on page
18aa1 20 31 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 1.. ** This
18aa2 72 65 61 6c 6c 79 20 6e 65 76 65 72 20 73 68 6f really never sho
18aa3 75 6c 64 20 68 61 70 70 65 6e 20 65 78 63 65 70 uld happen excep
18aa4 74 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 20 t in a corrupt.
18aa5 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 ** database.
18aa6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 65 72 6f . */. zero
18aa7 50 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f Page(pPage, PTF_
18aa8 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 INTKEY|PTF_LEAF
18aa9 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 );. releasePa
18aaa 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 ge(pPage);. }.
18aab 20 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a return rc; .}.
18aac 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18aad 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 nt sqlite3BtreeD
18aae 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a ropTable(Btree *
18aaf 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 p, int iTable, i
18ab0 6e 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 nt *piMoved){.
18ab1 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
18ab2 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18ab3 20 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 rc = btreeDrop
18ab4 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c Table(p, iTable,
18ab5 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c piMoved);. sql
18ab6 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 ite3BtreeLeave(p
18ab7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a );. return rc;.
18ab8 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 }.../*.** This f
18ab9 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 unction may only
18aba 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 be called if th
18abb 65 20 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 e b-tree connect
18abc 69 6f 6e 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68 ion already.** h
18abd 61 73 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 as a read or wri
18abe 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te transaction o
18abf 70 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 pen on the datab
18ac0 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 ase..**.** Read
18ac1 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 the meta-informa
18ac2 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 tion out of a da
18ac3 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 tabase file. Me
18ac4 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 ta[0].** is the
18ac5 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 number of free p
18ac6 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 ages currently i
18ac7 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 n the database.
18ac8 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f Meta[1].** thro
18ac9 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 ugh meta[15] are
18aca 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 available for u
18acb 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 se by higher lay
18acc 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a ers. Meta[0].**
18acd 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 is read-only, t
18ace 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 he others are re
18acf 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a ad/write..** .**
18ad0 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 The schema laye
18ad1 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 r numbers meta v
18ad2 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c alues differentl
18ad3 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d y. At the schem
18ad4 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 a.** layer (and
18ad5 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e the SetCookie an
18ad6 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 d ReadCookie opc
18ad7 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 odes) the number
18ad8 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 of.** free page
18ad9 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 s is not visible
18ada 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 . So Cookie[0]
18adb 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d is the same as M
18adc 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 eta[1]..*/.SQLIT
18add 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
18ade 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 qlite3BtreeGetMe
18adf 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 ta(Btree *p, int
18ae0 20 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 idx, u32 *pMeta
18ae1 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 ){. BtShared *p
18ae2 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 Bt = p->pBt;..
18ae3 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
18ae4 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
18ae5 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 p->inTrans>TRANS
18ae6 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 _NONE );. asser
18ae7 74 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 t( SQLITE_OK==qu
18ae8 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
18ae9 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 bleLock(p, MASTE
18aea 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 R_ROOT, READ_LOC
18aeb 4b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 K) );. assert(
18aec 70 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 pBt->pPage1 );.
18aed 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 assert( idx>=0
18aee 26 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 && idx<=15 );..
18aef 20 2a 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 *pMeta = get4by
18af0 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d te(&pBt->pPage1-
18af1 3e 61 44 61 74 61 5b 33 36 20 2b 20 69 64 78 2a >aData[36 + idx*
18af2 34 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75 4]);.. /* If au
18af3 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 64 69 73 to-vacuum is dis
18af4 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 abled in this bu
18af5 69 6c 64 20 61 6e 64 20 74 68 69 73 20 69 73 20 ild and this is
18af6 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 20 an auto-vacuum.
18af7 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 6d 61 ** database, ma
18af8 72 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 rk the database
18af9 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a as read-only. *
18afa 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f /.#ifdef SQLITE_
18afb 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a OMIT_AUTOVACUUM.
18afc 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 45 if( idx==BTREE
18afd 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 _LARGEST_ROOT_PA
18afe 47 45 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 GE && *pMeta>0 )
18aff 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d pBt->readOnly =
18b00 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 1;.#endif.. sq
18b01 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 lite3BtreeLeave(
18b02 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 p);.}../*.** Wri
18b03 74 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 te meta-informat
18b04 69 6f 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 ion back into th
18b05 65 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 e database. Met
18b06 61 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d a[0] is.** read-
18b07 6f 6e 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 only and may not
18b08 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a be written..*/.
18b09 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
18b0a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 nt sqlite3BtreeU
18b0b 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 pdateMeta(Btree
18b0c 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 *p, int idx, u32
18b0d 20 69 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 iMeta){. BtSha
18b0e 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 red *pBt = p->pB
18b0f 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 t;. unsigned ch
18b10 61 72 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 ar *pP1;. int r
18b11 63 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 c;. assert( idx
18b12 3e 3d 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 >=1 && idx<=15 )
18b13 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 ;. sqlite3Btree
18b14 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 Enter(p);. asse
18b15 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d rt( p->inTrans==
18b16 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 TRANS_WRITE );.
18b17 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 assert( pBt->pP
18b18 61 67 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 age1!=0 );. pP1
18b19 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e = pBt->pPage1->
18b1a 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 aData;. rc = sq
18b1b 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 lite3PagerWrite(
18b1c 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 pBt->pPage1->pDb
18b1d 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d Page);. if( rc=
18b1e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
18b1f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 31 5b put4byte(&pP1[
18b20 33 36 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 36 + idx*4], iMe
18b21 74 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c ta);.#ifndef SQL
18b22 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 ITE_OMIT_AUTOVAC
18b23 55 55 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d UUM. if( idx=
18b24 3d 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 =BTREE_INCR_VACU
18b25 55 4d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 UM ){. asse
18b26 72 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 rt( pBt->autoVac
18b27 75 75 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 uum || iMeta==0
18b28 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
18b29 20 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 iMeta==0 || iMe
18b2a 74 61 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 ta==1 );. p
18b2b 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d Bt->incrVacuum =
18b2c 20 28 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 (u8)iMeta;.
18b2d 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 }.#endif. }. s
18b2e 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
18b2f 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 (p);. return rc
18b30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c ;.}..#ifndef SQL
18b31 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f ITE_OMIT_BTREECO
18b32 55 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 UNT./*.** The fi
18b33 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 rst argument, pC
18b34 75 72 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 ur, is a cursor
18b35 6f 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 opened on some b
18b36 2d 74 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 -tree. Count the
18b37 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e .** number of en
18b38 74 72 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 tries in the b-t
18b39 72 65 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 ree and write th
18b3a 65 20 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 e result to *pnE
18b3b 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 ntry..**.** SQLI
18b3c 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 TE_OK is returne
18b3d 64 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 d if the operati
18b3e 6f 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c on is successful
18b3f 6c 79 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a ly executed. .**
18b40 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 Otherwise, if a
18b41 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 n error is encou
18b42 6e 74 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 ntered (i.e. an
18b43 49 4f 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 IO error or data
18b44 62 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 base.** corrupti
18b45 6f 6e 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 on) an SQLite er
18b46 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
18b47 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
18b48 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
18b49 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 te3BtreeCount(Bt
18b4a 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 Cursor *pCur, i6
18b4b 34 20 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 4 *pnEntry){. i
18b4c 36 34 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 64 nEntry = 0;
18b4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b4e 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
18b4f 72 65 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 return in *pnEnt
18b50 72 79 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 ry */. int rc;
18b51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18b52 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
18b53 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 Return code */.
18b54 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 rc = moveToRoot
18b55 28 70 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e (pCur);.. /* Un
18b56 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 less an error oc
18b57 63 75 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 curs, the follow
18b58 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e ing loop runs on
18b59 65 20 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 e iteration for
18b5a 65 61 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 each. ** page i
18b5b 6e 20 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 n the B-Tree str
18b5c 75 63 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c ucture (not incl
18b5d 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 uding overflow p
18b5e 61 67 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 ages). . */. w
18b5f 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
18b60 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 _OK ){. int i
18b61 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 Idx;
18b62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18b63 20 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 Index of child
18b64 6e 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a node in parent *
18b65 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 /. MemPage *p
18b66 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 Page;
18b67 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 /* Curr
18b68 65 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 ent page of the
18b69 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f b-tree */.. /
18b6a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6c * If this is a l
18b6b 65 61 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 eaf page or the
18b6c 74 72 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 tree is not an i
18b6d 6e 74 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 nt-key tree, the
18b6e 6e 20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 n . ** this p
18b6f 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 age contains cou
18b70 6e 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 ntable entries.
18b71 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e Increment the en
18b72 74 72 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 try counter.
18b73 2a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a ** accordingly..
18b74 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 */. pPage
18b75 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b = pCur->apPage[
18b76 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 pCur->iPage];.
18b77 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 if( pPage->lea
18b78 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 f || !pPage->int
18b79 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e Key ){. nEn
18b7a 74 72 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 try += pPage->nC
18b7b 65 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ell;. }..
18b7c 2f 2a 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 /* pPage is a le
18b7d 61 66 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f af node. This lo
18b7e 6f 70 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 op navigates the
18b7f 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 cursor so that
18b80 69 74 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 it . ** point
18b81 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 s to the first i
18b82 6e 74 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 nterior cell tha
18b83 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 t it points to t
18b84 68 65 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 he parent of.
18b85 20 2a 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 ** the next pag
18b86 65 20 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 e in the tree th
18b87 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 at has not yet b
18b88 65 65 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 een visited. The
18b89 0a 20 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 . ** pCur->ai
18b8a 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d Idx[pCur->iPage]
18b8b 20 76 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f value is set to
18b8c 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 the index of th
18b8d 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 e parent cell.
18b8e 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 ** of the page
18b8f 2c 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 , or to the numb
18b90 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 er of cells in t
18b91 68 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e he page if the n
18b92 65 78 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 ext page. **
18b93 74 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20 to visit is the
18b94 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 right-child of i
18b95 74 73 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a ts parent.. *
18b96 2a 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 *. ** If all
18b97 70 61 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 pages in the tre
18b98 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 e have been visi
18b99 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 ted, return SQLI
18b9a 54 45 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 TE_OK to the.
18b9b 20 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 ** caller..
18b9c 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 */. if( pPage
18b9d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 ->leaf ){.
18b9e 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 do {. if(
18b9f 20 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 pCur->iPage==0
18ba0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
18ba1 41 6c 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 All pages of the
18ba2 20 62 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 b-tree have bee
18ba3 6e 20 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 n visited. Retur
18ba4 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 n successfully.
18ba5 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e */. *pn
18ba6 45 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a Entry = nEntry;.
18ba7 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
18ba8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 SQLITE_OK;.
18ba9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 6f }. mo
18baa 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 veToParent(pCur)
18bab 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 20 28 ;. }while (
18bac 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 pCur->aiIdx[pCu
18bad 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d r->iPage]>=pCur-
18bae 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 >apPage[pCur->iP
18baf 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a age]->nCell );..
18bb0 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 pCur->aiId
18bb1 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b x[pCur->iPage]++
18bb2 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 ;. pPage =
18bb3 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 pCur->apPage[pCu
18bb4 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d r->iPage];. }
18bb5 0a 0a 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 .. /* Descend
18bb6 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6e 6f to the child no
18bb7 64 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 de of the cell t
18bb8 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 63 hat the cursor c
18bb9 75 72 72 65 6e 74 6c 79 20 0a 20 20 20 20 2a 2a urrently . **
18bba 20 70 6f 69 6e 74 73 20 61 74 2e 20 54 68 69 73 points at. This
18bbb 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 is the right-ch
18bbc 69 6c 64 20 69 66 20 28 69 49 64 78 3d 3d 70 50 ild if (iIdx==pP
18bbd 61 67 65 2d 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 age->nCell)..
18bbe 20 2a 2f 0a 20 20 20 20 69 49 64 78 20 3d 20 70 */. iIdx = p
18bbf 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d Cur->aiIdx[pCur-
18bc0 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 >iPage];. if(
18bc1 20 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 iIdx==pPage->nC
18bc2 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 ell ){. rc
18bc3 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 = moveToChild(pC
18bc4 75 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 ur, get4byte(&pP
18bc5 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
18bc6 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 ->hdrOffset+8]))
18bc7 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
18bc8 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 rc = moveToCh
18bc9 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 ild(pCur, get4by
18bca 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 te(findCell(pPag
18bcb 65 2c 20 69 49 64 78 29 29 29 3b 0a 20 20 20 20 e, iIdx)));.
18bcc 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 }. }.. /* An e
18bcd 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 rror has occurre
18bce 64 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 d. Return an err
18bcf 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 or code. */. re
18bd0 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 turn rc;.}.#endi
18bd1 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 f../*.** Return
18bd2 74 68 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 the pager associ
18bd3 61 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 ated with a BTre
18bd4 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 e. This routine
18bd5 20 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 is used for.**
18bd6 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 testing and debu
18bd7 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 gging only..*/.S
18bd8 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 QLITE_PRIVATE Pa
18bd9 67 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 ger *sqlite3Btre
18bda 65 50 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 ePager(Btree *p)
18bdb 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 {. return p->pB
18bdc 74 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 t->pPager;.}..#i
18bdd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18bde 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
18bdf 4b 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 K./*.** Append a
18be0 20 6d 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 message to the
18be1 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 error message st
18be2 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ring..*/.static
18be3 76 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 void checkAppend
18be4 4d 73 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 Msg(. Integrity
18be5 43 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 Ck *pCheck,. ch
18be6 61 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e ar *zMsg1,. con
18be7 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 st char *zFormat
18be8 2c 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f ,. ....){. va_
18be9 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 list ap;. if( !
18bea 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 pCheck->mxErr )
18beb 72 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b return;. pCheck
18bec 2d 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 ->mxErr--;. pCh
18bed 65 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 eck->nErr++;. v
18bee 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 a_start(ap, zFor
18bef 6d 61 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 mat);. if( pChe
18bf0 63 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 ck->errMsg.nChar
18bf1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 ){. sqlite3S
18bf2 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 trAccumAppend(&p
18bf3 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 Check->errMsg, "
18bf4 5c 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 \n", 1);. }. i
18bf5 66 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 f( zMsg1 ){.
18bf6 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
18bf7 70 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 ppend(&pCheck->e
18bf8 72 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 rrMsg, zMsg1, -1
18bf9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
18bfa 56 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b VXPrintf(&pCheck
18bfb 2d 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f ->errMsg, 1, zFo
18bfc 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f rmat, ap);. va_
18bfd 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 end(ap);. if( p
18bfe 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 Check->errMsg.ma
18bff 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
18c00 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 pCheck->malloc
18c01 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a Failed = 1;. }.
18c02 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
18c03 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 TE_OMIT_INTEGRIT
18c04 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e Y_CHECK */..#ifn
18c05 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18c06 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a INTEGRITY_CHECK.
18c07 2f 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 /*.** Add 1 to t
18c08 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 he reference cou
18c09 6e 74 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 nt for page iPag
18c0a 65 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 74 e. If this is t
18c0b 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 he second.** ref
18c0c 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 erence to the pa
18c0d 67 65 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 ge, add an error
18c0e 20 6d 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 message to pChe
18c0f 63 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 ck->zErrMsg..**
18c10 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 Return 1 if ther
18c11 65 20 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 e are 2 ore more
18c12 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 references to t
18c13 68 65 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 he page and 0 if
18c14 0a 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 .** if this is t
18c15 68 65 20 66 69 72 73 74 20 72 65 66 65 72 65 6e he first referen
18c16 63 65 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a ce to the page..
18c17 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b **.** Also check
18c18 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e that the page n
18c19 75 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e umber is in boun
18c1a 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ds..*/.static in
18c1b 74 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 t checkRef(Integ
18c1c 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 rityCk *pCheck,
18c1d 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 Pgno iPage, char
18c1e 20 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 *zContext){. i
18c1f 66 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 f( iPage==0 ) re
18c20 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 turn 1;. if( iP
18c21 61 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 age>pCheck->nPag
18c22 65 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 e ){. checkAp
18c23 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
18c24 7a 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c zContext, "inval
18c25 69 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 id page number %
18c26 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 d", iPage);.
18c27 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 return 1;. }.
18c28 69 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 if( pCheck->anRe
18c29 66 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 f[iPage]==1 ){.
18c2a 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
18c2b 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 g(pCheck, zConte
18c2c 78 74 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e xt, "2nd referen
18c2d 63 65 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 ce to page %d",
18c2e 69 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 iPage);. retu
18c2f 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rn 1;. }. retu
18c30 72 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 rn (pCheck->anR
18c31 65 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a ef[iPage]++)>1;.
18c32 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
18c33 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 E_OMIT_AUTOVACUU
18c34 4d 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 M./*.** Check th
18c35 61 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 at the entry in
18c36 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 the pointer-map
18c37 66 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 for page iChild
18c38 6d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 maps to .** page
18c39 20 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 iParent, pointe
18c3a 72 20 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 r type ptrType.
18c3b 49 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 If not, append a
18c3c 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a n error message.
18c3d 2a 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f ** to pCheck..*/
18c3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 .static void che
18c3f 63 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 ckPtrmap(. Inte
18c40 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c grityCk *pCheck,
18c41 20 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 /* Integrity
18c42 63 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f check context */
18c43 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 . Pgno iChild,
18c44 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 /* Chi
18c45 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a ld page number *
18c46 2f 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 /. u8 eType,
18c47 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 /* Ex
18c48 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d pected pointer m
18c49 61 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e ap type */. Pgn
18c4a 6f 20 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 o iParent,
18c4b 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 /* Expected
18c4c 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 pointer map pare
18c4d 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a nt page number *
18c4e 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 /. char *zConte
18c4f 78 74 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f xt /* Co
18c50 6e 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f ntext descriptio
18c51 6e 20 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f n (used for erro
18c52 72 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 r msg) */.){. i
18c53 6e 74 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 nt rc;. u8 ePtr
18c54 6d 61 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 mapType;. Pgno
18c55 69 50 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a iPtrmapParent;..
18c56 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 rc = ptrmapGet
18c57 28 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 (pCheck->pBt, iC
18c58 68 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 hild, &ePtrmapTy
18c59 70 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 pe, &iPtrmapPare
18c5a 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 nt);. if( rc!=S
18c5b 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 QLITE_OK ){.
18c5c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e if( rc==SQLITE_N
18c5d 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 OMEM || rc==SQLI
18c5e 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 TE_IOERR_NOMEM )
18c5f 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 pCheck->mallocF
18c60 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 ailed = 1;. c
18c61 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18c62 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 heck, zContext,
18c63 22 46 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 "Failed to read
18c64 70 74 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 ptrmap key=%d",
18c65 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 iChild);. ret
18c66 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 urn;. }.. if(
18c67 65 50 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 ePtrmapType!=eTy
18c68 70 65 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 pe || iPtrmapPar
18c69 65 6e 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a ent!=iParent ){.
18c6a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18c6b 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 sg(pCheck, zCont
18c6c 65 78 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 ext, . "Bad
18c6d 20 70 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b ptr map entry k
18c6e 65 79 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 ey=%d expected=(
18c6f 25 64 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 %d,%d) got=(%d,%
18c70 64 29 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 d)", . iChi
18c71 6c 64 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 ld, eType, iPare
18c72 6e 74 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c nt, ePtrmapType,
18c73 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b iPtrmapParent);
18c74 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f . }.}.#endif../
18c75 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 *.** Check the i
18c76 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 ntegrity of the
18c77 66 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 freelist or of a
18c78 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 n overflow page
18c79 6c 69 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 list..** Verify
18c7a 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 that the number
18c7b 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 of pages on the
18c7c 6c 69 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 list is N..*/.st
18c7d 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c atic void checkL
18c7e 69 73 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 ist(. Integrity
18c7f 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 Ck *pCheck, /*
18c80 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 Integrity checki
18c81 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
18c82 69 6e 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 int isFreeList,
18c83 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f /* True fo
18c84 72 20 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 r a freelist. F
18c85 61 6c 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f alse for overflo
18c86 77 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 w page list */.
18c87 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 int iPage,
18c88 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e /* Page n
18c89 75 6d 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 umber for first
18c8a 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 page in the list
18c8b 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 */. int N,
18c8c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
18c8d 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f xpected number o
18c8e 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c f pages in the l
18c8f 69 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a ist */. char *z
18c90 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f Context /
18c91 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 * Context for er
18c92 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a ror messages */.
18c93 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e ){. int i;. in
18c94 74 20 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a t expected = N;.
18c95 20 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 int iFirst = i
18c96 50 61 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e Page;. while( N
18c97 2d 2d 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b -- > 0 && pCheck
18c98 2d 3e 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 ->mxErr ){. D
18c99 62 50 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 bPage *pOvflPage
18c9a 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 ;. unsigned c
18c9b 68 61 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a har *pOvflData;.
18c9c 20 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 if( iPage<1
18c9d 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
18c9e 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
18c9f 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 zContext,.
18ca0 20 20 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 "%d of %d pag
18ca1 65 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 es missing from
18ca2 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 overflow list st
18ca3 61 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 arting at %d",.
18ca4 20 20 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 N+1, ex
18ca5 70 65 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b pected, iFirst);
18ca6 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
18ca7 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 }. if( chec
18ca8 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 kRef(pCheck, iPa
18ca9 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 ge, zContext) )
18caa 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 break;. if( s
18cab 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 qlite3PagerGet(p
18cac 43 68 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 Check->pPager, (
18cad 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 Pgno)iPage, &pOv
18cae 66 6c 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 flPage) ){.
18caf 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18cb0 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 pCheck, zContext
18cb1 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 , "failed to get
18cb2 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 page %d", iPage
18cb3 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
18cb4 20 20 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 }. pOvflD
18cb5 61 74 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 ata = (unsigned
18cb6 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 char *)sqlite3Pa
18cb7 67 65 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c gerGetData(pOvfl
18cb8 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 Page);. if( i
18cb9 73 46 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 sFreeList ){.
18cba 20 20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 int n = get4b
18cbb 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 yte(&pOvflData[4
18cbc 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ]);.#ifndef SQLI
18cbd 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18cbe 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 UM. if( pCh
18cbf 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 eck->pBt->autoVa
18cc0 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 cuum ){.
18cc1 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 checkPtrmap(pChe
18cc2 63 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 ck, iPage, PTRMA
18cc3 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a P_FREEPAGE, 0, z
18cc4 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
18cc5 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 }.#endif. i
18cc6 66 28 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 f( n>pCheck->pBt
18cc7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 ->usableSize/4-2
18cc8 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 ){. chec
18cc9 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 kAppendMsg(pChec
18cca 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 k, zContext,.
18ccb 20 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 "freelis
18ccc 74 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f t leaf count too
18ccd 20 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 big on page %d"
18cce 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 , iPage);.
18ccf 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c N--;. }el
18cd0 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 se{. for(
18cd1 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a i=0; i<n; i++){.
18cd2 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 Pgno i
18cd3 46 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 FreePage = get4b
18cd4 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 yte(&pOvflData[8
18cd5 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 +i*4]);.#ifndef
18cd6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f SQLITE_OMIT_AUTO
18cd7 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 VACUUM.
18cd8 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 if( pCheck->pBt
18cd9 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
18cda 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63 chec
18cdb 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 kPtrmap(pCheck,
18cdc 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 iFreePage, PTRMA
18cdd 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a P_FREEPAGE, 0, z
18cde 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
18cdf 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
18ce0 20 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 checkRef(
18ce1 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 pCheck, iFreePag
18ce2 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 e, zContext);.
18ce3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
18ce4 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a N -= n;. }.
18ce5 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 }.#ifndef SQ
18ce6 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 LITE_OMIT_AUTOVA
18ce7 43 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 CUUM. else{.
18ce8 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 /* If this
18ce9 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 database support
18cea 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e s auto-vacuum an
18ceb 64 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 d iPage is not t
18cec 68 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a he last. **
18ced 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 page in this ov
18cee 65 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 erflow list, che
18cef 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e ck that the poin
18cf0 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f ter-map entry fo
18cf1 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 r. ** the f
18cf2 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 ollowing page ma
18cf3 74 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 tches iPage..
18cf4 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
18cf5 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 pCheck->pBt->aut
18cf6 6f 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 oVacuum && N>0 )
18cf7 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 {. i = ge
18cf8 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 t4byte(pOvflData
18cf9 29 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b );. check
18cfa 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 Ptrmap(pCheck, i
18cfb 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f , PTRMAP_OVERFLO
18cfc 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 W2, iPage, zCont
18cfd 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ext);. }.
18cfe 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 }.#endif. i
18cff 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 Page = get4byte(
18d00 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 pOvflData);.
18d01 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 sqlite3PagerUnre
18d02 66 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 f(pOvflPage);.
18d03 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.}.#endif /* SQ
18d04 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 LITE_OMIT_INTEGR
18d05 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 ITY_CHECK */..#i
18d06 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
18d07 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 T_INTEGRITY_CHEC
18d08 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f K./*.** Do vario
18d09 75 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 us sanity checks
18d0a 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 on a single pag
18d0b 65 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 e of a tree. Re
18d0c 74 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 turn.** the tree
18d0d 20 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 depth. Root pa
18d0e 67 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 ges return 0. P
18d0f 61 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 arents of root p
18d10 61 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 ages.** return 1
18d11 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a , and so forth..
18d12 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 ** .** These che
18d13 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a cks are done:.**
18d14 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b .** 1. Mak
18d15 65 20 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c e sure that cell
18d16 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 s and freeblocks
18d17 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a do not overlap.
18d18 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20 ** but
18d19 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c combine to compl
18d1a 65 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 etely cover the
18d1b 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e page..** NO 2.
18d1c 20 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c Make sure cell
18d1d 20 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 keys are in ord
18d1e 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 er..** NO 3.
18d1f 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 Make sure no key
18d20 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 is less than or
18d21 20 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 equal to zLower
18d22 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 Bound..** NO 4
18d23 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 . Make sure no
18d24 6b 65 79 20 69 73 20 67 72 65 61 74 65 72 20 74 key is greater t
18d25 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 han or equal to
18d26 7a 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 zUpperBound..**
18d27 20 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 5. Check t
18d28 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 he integrity of
18d29 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a overflow pages..
18d2a 2a 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 ** 6. Recu
18d2b 72 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 rsively call che
18d2c 63 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c ckTreePage on al
18d2d 6c 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 l children..**
18d2e 20 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 7. Verify t
18d2f 68 61 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 hat the depth of
18d30 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 all children is
18d31 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 the same..**
18d32 20 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 8. Make sure
18d33 20 74 68 69 73 20 70 61 67 65 20 69 73 20 61 74 this page is at
18d34 20 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 least 33% full
18d35 6f 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a or else it is.**
18d36 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 6f the ro
18d37 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a ot of the tree..
18d38 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 */.static int ch
18d39 65 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 eckTreePage(. I
18d3a 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 ntegrityCk *pChe
18d3b 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 ck, /* Context
18d3c 66 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 for the sanity c
18d3d 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 heck */. int iP
18d3e 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 age,
18d3f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f /* Page number o
18d40 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 f the page to ch
18d41 65 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a eck */. char *z
18d42 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f ParentContext /
18d43 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 * Parent context
18d44 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 */.){. MemPage
18d45 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 *pPage;. int i
18d46 2c 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c , rc, depth, d2,
18d47 20 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e pgno, cnt;. in
18d48 74 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 t hdr, cellStart
18d49 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 ;. int nCell;.
18d4a 20 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 u8 *data;. BtS
18d4b 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e hared *pBt;. in
18d4c 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 t usableSize;.
18d4d 63 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 char zContext[10
18d4e 30 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 0];. char *hit
18d4f 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f = 0;.. sqlite3_
18d50 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 snprintf(sizeof(
18d51 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 zContext), zCont
18d52 65 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 ext, "Page %d: "
18d53 2c 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 , iPage);.. /*
18d54 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 Check that the p
18d55 61 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a age exists. */.
18d56 20 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e pBt = pCheck->
18d57 70 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a pBt;. usableSiz
18d58 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 e = pBt->usableS
18d59 69 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 ize;. if( iPage
18d5a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
18d5b 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 if( checkRef(p
18d5c 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 Check, iPage, zP
18d5d 61 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 arentContext) )
18d5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
18d5f 28 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 (rc = btreeGetPa
18d60 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 ge(pBt, (Pgno)iP
18d61 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 age, &pPage, 0))
18d62 21 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b !=0 ){. check
18d63 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b AppendMsg(pCheck
18d64 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 , zContext,.
18d65 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 "unable to ge
18d66 74 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f t the page. erro
18d67 72 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b r code=%d", rc);
18d68 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
18d69 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 4d }.. /* Clear M
18d6a 65 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 74 6f emPage.isInit to
18d6b 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 make sure the c
18d6c 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 orruption detect
18d6d 69 6f 6e 20 63 6f 64 65 20 69 6e 0a 20 20 2a 2a ion code in. **
18d6e 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 btreeInitPage()
18d6f 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 2a is executed. *
18d70 2f 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 /. pPage->isIni
18d71 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 72 63 t = 0;. if( (rc
18d72 20 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 = btreeInitPage
18d73 28 70 50 61 67 65 29 29 21 3d 30 20 29 7b 0a 20 (pPage))!=0 ){.
18d74 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
18d75 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b QLITE_CORRUPT );
18d76 20 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 70 6f /* The only po
18d77 73 73 69 62 6c 65 20 65 72 72 6f 72 20 66 72 6f ssible error fro
18d78 6d 20 49 6e 69 74 50 61 67 65 20 2a 2f 0a 20 20 m InitPage */.
18d79 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 checkAppendMsg
18d7a 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 (pCheck, zContex
18d7b 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 t, .
18d7c 20 20 20 20 20 20 20 22 62 74 72 65 65 49 6e 69 "btreeIni
18d7d 74 50 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 tPage() returns
18d7e 65 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 error code %d",
18d7f 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 rc);. release
18d80 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 Page(pPage);.
18d81 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a return 0;. }..
18d82 20 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 /* Check out a
18d83 6c 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 ll the cells..
18d84 2a 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a */. depth = 0;.
18d85 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 for(i=0; i<pPa
18d86 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 ge->nCell && pCh
18d87 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 eck->mxErr; i++)
18d88 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b {. u8 *pCell;
18d89 0a 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 . u32 sz;.
18d8a 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a CellInfo info;.
18d8b 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 . /* Check pa
18d8c 79 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 yload overflow p
18d8d 61 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ages. */.
18d8e 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
18d8f 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 (sizeof(zContext
18d90 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 ), zContext,.
18d91 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 "On tr
18d92 65 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 ee page %d cell
18d93 25 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 %d: ", iPage, i)
18d94 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 ;. pCell = fi
18d95 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b ndCell(pPage,i);
18d96 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 . btreeParseC
18d97 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 ellPtr(pPage, pC
18d98 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 ell, &info);.
18d99 20 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 sz = info.nData
18d9a 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 ;. if( !pPage
18d9b 2d 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d ->intKey ) sz +=
18d9c 20 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b (int)info.nKey;
18d9d 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d . assert( sz=
18d9e 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 =info.nPayload )
18d9f 3b 0a 20 20 20 20 69 66 28 20 28 73 7a 3e 69 6e ;. if( (sz>in
18da0 66 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 20 fo.nLocal) .
18da1 20 26 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f && (&pCell[info
18da2 2e 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 .iOverflow]<=&pP
18da3 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e age->aData[pBt->
18da4 75 73 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 20 usableSize]).
18da5 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 ){. int nP
18da6 61 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f age = (sz - info
18da7 2e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 .nLocal + usable
18da8 53 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c Size - 5)/(usabl
18da9 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 eSize - 4);.
18daa 20 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 Pgno pgnoOvfl
18dab 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c = get4byte(&pCel
18dac 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 l[info.iOverflow
18dad 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ]);.#ifndef SQLI
18dae 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 TE_OMIT_AUTOVACU
18daf 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 UM. if( pBt
18db0 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a ->autoVacuum ){.
18db1 20 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 checkPtr
18db2 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f map(pCheck, pgno
18db3 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 Ovfl, PTRMAP_OVE
18db4 52 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a RFLOW1, iPage, z
18db5 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
18db6 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 63 }.#endif. c
18db7 68 65 63 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c heckList(pCheck,
18db8 20 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 0, pgnoOvfl, nP
18db9 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a age, zContext);.
18dba 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 }.. /* Ch
18dbb 65 63 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 eck sanity of le
18dbc 66 74 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 ft child page..
18dbd 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 */. if( !p
18dbe 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 Page->leaf ){.
18dbf 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 pgno = get4b
18dc0 79 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e yte(pCell);.#ifn
18dc1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
18dc2 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 AUTOVACUUM.
18dc3 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 if( pBt->autoVa
18dc4 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 cuum ){.
18dc5 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 checkPtrmap(pChe
18dc6 63 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 ck, pgno, PTRMAP
18dc7 5f 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a _BTREE, iPage, z
18dc8 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 Context);.
18dc9 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 64 }.#endif. d
18dca 32 20 3d 20 63 68 65 63 6b 54 72 65 65 50 61 67 2 = checkTreePag
18dcb 65 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 e(pCheck, pgno,
18dcc 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 zContext);.
18dcd 20 69 66 28 20 69 3e 30 20 26 26 20 64 32 21 3d if( i>0 && d2!=
18dce 64 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 20 depth ){.
18dcf 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18dd0 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 pCheck, zContext
18dd1 2c 20 22 43 68 69 6c 64 20 70 61 67 65 20 64 65 , "Child page de
18dd2 70 74 68 20 64 69 66 66 65 72 73 22 29 3b 0a 20 pth differs");.
18dd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 70 }. dep
18dd4 74 68 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 th = d2;. }.
18dd5 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d }. if( !pPage-
18dd6 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e >leaf ){. pgn
18dd7 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 o = get4byte(&pP
18dd8 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 age->aData[pPage
18dd9 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b ->hdrOffset+8]);
18dda 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
18ddb 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f rintf(sizeof(zCo
18ddc 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 ntext), zContext
18ddd 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
18dde 20 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65 "On page
18ddf 20 25 64 20 61 74 20 72 69 67 68 74 20 63 68 69 %d at right chi
18de0 6c 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 ld: ", iPage);.#
18de1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18de2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 IT_AUTOVACUUM.
18de3 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 if( pBt->autoV
18de4 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 acuum ){. c
18de5 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 heckPtrmap(pChec
18de6 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f k, pgno, PTRMAP_
18de7 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 30 29 BTREE, iPage, 0)
18de8 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
18de9 20 20 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 checkTreePage
18dea 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a (pCheck, pgno, z
18deb 43 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a Context);. }. .
18dec 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 /* Check for c
18ded 6f 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65 omplete coverage
18dee 20 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a of the page. *
18def 2f 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 /. data = pPage
18df0 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d ->aData;. hdr =
18df1 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 pPage->hdrOffse
18df2 74 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74 t;. hit = sqlit
18df3 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 e3PageMalloc( pB
18df4 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 t->pageSize );.
18df5 20 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 if( hit==0 ){.
18df6 20 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f pCheck->mallo
18df7 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d cFailed = 1;. }
18df8 65 6c 73 65 7b 0a 20 20 20 20 75 31 36 20 63 6f else{. u16 co
18df9 6e 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 ntentOffset = ge
18dfa 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 t2byte(&data[hdr
18dfb 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +5]);. assert
18dfc 28 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c ( contentOffset<
18dfd 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 =usableSize );
18dfe 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 /* Enforced by b
18dff 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a treeInitPage() *
18e00 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 /. memset(hit
18e01 2b 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 +contentOffset,
18e02 30 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 0, usableSize-co
18e03 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 ntentOffset);.
18e04 20 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c memset(hit, 1,
18e05 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b contentOffset);
18e06 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 . nCell = get
18e07 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 2byte(&data[hdr+
18e08 33 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 3]);. cellSta
18e09 72 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 rt = hdr + 12 -
18e0a 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 4*pPage->leaf;.
18e0b 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 for(i=0; i<nC
18e0c 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 ell; i++){.
18e0d 20 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 int pc = get2by
18e0e 74 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 te(&data[cellSta
18e0f 72 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 rt+i*2]);.
18e10 75 31 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b u16 size = 1024;
18e11 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 . int j;.
18e12 20 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 if( pc<=usab
18e13 6c 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 leSize-4 ){.
18e14 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 size = cellS
18e15 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 izePtr(pPage, &d
18e16 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 ata[pc]);.
18e17 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70 63 2b }. if( (pc+
18e18 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 size-1)>=usableS
18e19 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ize ){. c
18e1a 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 heckAppendMsg(pC
18e1b 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 heck, 0, .
18e1c 20 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f "Corruptio
18e1d 6e 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 n detected in ce
18e1e 6c 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 ll %d on page %d
18e1f 22 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 ",i,iPage,0);.
18e20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
18e21 20 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 for(j=pc+size
18e22 2d 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 -1; j>=pc; j--)
18e23 68 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 hit[j]++;.
18e24 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 }. }. i =
18e25 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 get2byte(&data[h
18e26 64 72 2b 31 5d 29 3b 0a 20 20 20 20 77 68 69 6c dr+1]);. whil
18e27 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 e( i>0 ){.
18e28 69 6e 74 20 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 int size, j;.
18e29 20 20 20 61 73 73 65 72 74 28 20 69 3c 3d 75 73 assert( i<=us
18e2a 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 ableSize-4 );
18e2b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 /* Enforced by
18e2c 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 btreeInitPage()
18e2d 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 20 3d */. size =
18e2e 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b get2byte(&data[
18e2f 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 i+2]);. ass
18e30 65 72 74 28 20 69 2b 73 69 7a 65 3c 3d 75 73 61 ert( i+size<=usa
18e31 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 bleSize ); /* E
18e32 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 nforced by btree
18e33 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 InitPage() */.
18e34 20 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 for(j=i+size
18e35 2d 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 -1; j>=i; j--) h
18e36 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a it[j]++;. j
18e37 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 = get2byte(&dat
18e38 61 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 a[i]);. ass
18e39 65 72 74 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 ert( j==0 || j>i
18e3a 2b 73 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 +size ); /* Enf
18e3b 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e orced by btreeIn
18e3c 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 itPage() */.
18e3d 20 20 61 73 73 65 72 74 28 20 6a 3c 3d 75 73 61 assert( j<=usa
18e3e 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 2f bleSize-4 ); /
18e3f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 * Enforced by bt
18e40 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f reeInitPage() */
18e41 0a 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 . i = j;.
18e42 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e }. for(i=cn
18e43 74 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a t=0; i<usableSiz
18e44 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 e; i++){. i
18e45 66 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a f( hit[i]==0 ){.
18e46 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 cnt++;.
18e47 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 }else if( h
18e48 69 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 it[i]>1 ){.
18e49 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 checkAppendMs
18e4a 67 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 g(pCheck, 0,.
18e4b 20 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 "Multiple
18e4c 20 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 uses for byte %
18e4d 64 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 d of page %d", i
18e4e 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 , iPage);.
18e4f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
18e50 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 . }. if( c
18e51 6e 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 nt!=data[hdr+7]
18e52 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 ){. checkAp
18e53 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 pendMsg(pCheck,
18e54 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 0, . "F
18e55 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 ragmentation of
18e56 25 64 20 62 79 74 65 73 20 72 65 70 6f 72 74 65 %d bytes reporte
18e57 64 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20 d as %d on page
18e58 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63 %d",. c
18e59 6e 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c nt, data[hdr+7],
18e5a 20 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 iPage);. }.
18e5b 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 }. sqlite3Page
18e5c 46 72 65 65 28 68 69 74 29 3b 0a 20 20 72 65 6c Free(hit);. rel
18e5d 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b easePage(pPage);
18e5e 0a 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b . return depth+
18e5f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 1;.}.#endif /* S
18e60 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
18e61 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 RITY_CHECK */..#
18e62 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
18e63 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 IT_INTEGRITY_CHE
18e64 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f CK./*.** This ro
18e65 75 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d utine does a com
18e66 70 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 plete check of t
18e67 68 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 he given BTree f
18e68 69 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 ile. aRoot[] is
18e69 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 .** an array of
18e6a 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 pages numbers we
18e6b 72 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d re each page num
18e6c 62 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 ber is the root
18e6d 70 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 page of.** a tab
18e6e 6c 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 le. nRoot is th
18e6f 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 e number of entr
18e70 69 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a ies in aRoot..**
18e71 0a 2a 2a 20 41 20 72 65 61 64 2d 6f 6e 6c 79 20 .** A read-only
18e72 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 or read-write tr
18e73 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 ansaction must b
18e74 65 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 e opened before
18e75 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 calling.** this
18e76 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
18e77 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 Write the number
18e78 20 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69 of error seen i
18e79 6e 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70 n *pnErr. Excep
18e7a 74 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 t for some memor
18e7b 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 y.** allocation
18e7c 65 72 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f errors, an erro
18e7d 72 20 6d 65 73 73 61 67 65 20 68 65 6c 64 20 69 r message held i
18e7e 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 n memory obtaine
18e7f 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 d from.** malloc
18e80 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 is returned if
18e81 2a 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 *pnErr is non-ze
18e82 72 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d ro. If *pnErr==
18e83 30 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 0 then NULL is.*
18e84 2a 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 * returned. If
18e85 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
18e86 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
18e87 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e , NULL is return
18e88 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ed..*/.SQLITE_PR
18e89 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 IVATE char *sqli
18e8a 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 te3BtreeIntegrit
18e8b 79 43 68 65 63 6b 28 0a 20 20 42 74 72 65 65 20 yCheck(. Btree
18e8c 2a 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62 *p, /* The b
18e8d 74 72 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b tree to be check
18e8e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f ed */. int *aRo
18e8f 6f 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 ot, /* An arra
18e90 79 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 20 y of root pages
18e91 6e 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 69 numbers for indi
18e92 76 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a vidual trees */.
18e93 20 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 int nRoot,
18e94 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
18e95 72 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 ries in aRoot[]
18e96 2a 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 */. int mxErr,
18e97 20 20 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 /* Stop repor
18e98 74 69 6e 67 20 65 72 72 6f 72 73 20 61 66 74 65 ting errors afte
18e99 72 20 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 r this many */.
18e9a 20 69 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 2f int *pnErr /
18e9b 2a 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f * Write number o
18e9c 66 20 65 72 72 6f 72 73 20 73 65 65 6e 20 74 6f f errors seen to
18e9d 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 2a this variable *
18e9e 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 /.){. Pgno i;.
18e9f 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 int nRef;. Int
18ea0 65 67 72 69 74 79 43 6b 20 73 43 68 65 63 6b 3b egrityCk sCheck;
18ea1 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 . BtShared *pBt
18ea2 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 = p->pBt;. cha
18ea3 72 20 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 r zErr[100];..
18ea4 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 sqlite3BtreeEnte
18ea5 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 r(p);. assert(
18ea6 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 p->inTrans>TRANS
18ea7 5f 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e _NONE && pBt->in
18ea8 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e Transaction>TRAN
18ea9 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 6e 52 65 66 S_NONE );. nRef
18eaa 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 = sqlite3PagerR
18eab 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 efcount(pBt->pPa
18eac 67 65 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 70 ger);. sCheck.p
18ead 42 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 Bt = pBt;. sChe
18eae 63 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d ck.pPager = pBt-
18eaf 3e 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 >pPager;. sChec
18eb0 6b 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 k.nPage = pagerP
18eb1 61 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e agecount(sCheck.
18eb2 70 42 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d pBt);. sCheck.m
18eb3 78 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20 xErr = mxErr;.
18eb4 73 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b sCheck.nErr = 0;
18eb5 0a 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 . sCheck.malloc
18eb6 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a 70 Failed = 0;. *p
18eb7 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 nErr = 0;. if(
18eb8 73 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 sCheck.nPage==0
18eb9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
18eba 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 reeLeave(p);.
18ebb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
18ebc 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 sCheck.anRef =
18ebd 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 sqlite3Malloc( (
18ebe 73 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a sCheck.nPage+1)*
18ebf 73 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e sizeof(sCheck.an
18ec0 52 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 Ref[0]) );. if(
18ec1 20 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 !sCheck.anRef )
18ec2 7b 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 {. *pnErr = 1
18ec3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 ;. sqlite3Btr
18ec4 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 eeLeave(p);.
18ec5 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
18ec6 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 for(i=0; i<=sChe
18ec7 63 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 ck.nPage; i++){
18ec8 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 sCheck.anRef[i]
18ec9 3d 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e = 0; }. i = PEN
18eca 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
18ecb 42 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 Bt);. if( i<=sC
18ecc 68 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 heck.nPage ){.
18ecd 20 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 sCheck.anRef[i
18ece 5d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c ] = 1;. }. sql
18ecf 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 ite3StrAccumInit
18ed0 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c (&sCheck.errMsg,
18ed1 20 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 zErr, sizeof(zE
18ed2 72 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 rr), 20000);..
18ed3 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 /* Check the int
18ed4 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 egrity of the fr
18ed5 65 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 eelist. */. ch
18ed6 65 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c eckList(&sCheck,
18ed7 20 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 1, get4byte(&pB
18ed8 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 t->pPage1->aData
18ed9 5b 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 [32]),.
18eda 20 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 get4byte(&pBt
18edb 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b ->pPage1->aData[
18edc 33 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 36]), "Main free
18edd 6c 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 list: ");.. /*
18ede 43 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 Check all the ta
18edf 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 bles.. */. for
18ee0 28 69 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f (i=0; (int)i<nRo
18ee1 6f 74 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 ot && sCheck.mxE
18ee2 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 rr; i++){. if
18ee3 28 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 ( aRoot[i]==0 )
18ee4 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 continue;.#ifnde
18ee5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 f SQLITE_OMIT_AU
18ee6 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 TOVACUUM. if(
18ee7 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d pBt->autoVacuum
18ee8 20 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 && aRoot[i]>1 )
18ee9 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 {. checkPtr
18eea 6d 61 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f map(&sCheck, aRo
18eeb 6f 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f ot[i], PTRMAP_RO
18eec 4f 54 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 OTPAGE, 0, 0);.
18eed 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
18eee 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 checkTreePage(&s
18eef 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c Check, aRoot[i],
18ef0 20 22 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 "List of tree r
18ef1 6f 6f 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 oots: ");. }..
18ef2 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 /* Make sure ev
18ef3 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 ery page in the
18ef4 66 69 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 file is referenc
18ef5 65 64 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d ed. */. for(i=
18ef6 31 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 1; i<=sCheck.nPa
18ef7 67 65 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 ge && sCheck.mxE
18ef8 72 72 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 rr; i++){.#ifdef
18ef9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 SQLITE_OMIT_AUT
18efa 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 OVACUUM. if(
18efb 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d sCheck.anRef[i]=
18efc 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 =0 ){. chec
18efd 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 kAppendMsg(&sChe
18efe 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 ck, 0, "Page %d
18eff 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 is never used",
18f00 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a i);. }.#else.
18f01 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 /* If the da
18f02 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 tabase supports
18f03 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b auto-vacuum, mak
18f04 65 20 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 e sure no tables
18f05 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 contain. **
18f06 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f references to po
18f07 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e inter-map pages.
18f08 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
18f09 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d sCheck.anRef[i]=
18f0a 3d 30 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 =0 && . (P
18f0b 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 TRMAP_PAGENO(pBt
18f0c 2c 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d , i)!=i || !pBt-
18f0d 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a >autoVacuum) ){.
18f0e 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e checkAppen
18f0f 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c dMsg(&sCheck, 0,
18f10 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 "Page %d is nev
18f11 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 er used", i);.
18f12 20 20 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 }. if( sChe
18f13 63 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 ck.anRef[i]!=0 &
18f14 26 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 & . (PTRMA
18f15 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 P_PAGENO(pBt, i)
18f16 3d 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f ==i && pBt->auto
18f17 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 Vacuum) ){.
18f18 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 checkAppendMsg(
18f19 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 &sCheck, 0, "Poi
18f1a 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 nter map page %d
18f1b 20 69 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c is referenced",
18f1c 20 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 i);. }.#endi
18f1d 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 f. }.. /* Make
18f1e 20 73 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 sure this analy
18f1f 73 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 sis did not leav
18f20 65 20 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 e any unref() pa
18f21 67 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 ges.. ** This i
18f22 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f s an internal co
18f23 6e 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b nsistency check;
18f24 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 an integrity ch
18f25 65 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 eck. ** of the
18f26 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e integrity check.
18f27 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 . */. if( NEVE
18f28 52 28 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65 R(nRef != sqlite
18f29 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 3PagerRefcount(p
18f2a 42 74 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a Bt->pPager)) ){.
18f2b 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d checkAppendM
18f2c 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0a sg(&sCheck, 0, .
18f2d 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69 "Outstandi
18f2e 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f ng page count go
18f2f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 es from %d to %d
18f30 20 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61 during this ana
18f31 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 20 6e 52 lysis",. nR
18f32 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 ef, sqlite3Pager
18f33 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 Refcount(pBt->pP
18f34 61 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 7d ager). );. }
18f35 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 70 .. /* Clean up
18f36 20 61 6e 64 20 72 65 70 6f 72 74 20 65 72 72 6f and report erro
18f37 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 rs.. */. sqlit
18f38 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b e3BtreeLeave(p);
18f39 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 . sqlite3_free(
18f3a 73 43 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 sCheck.anRef);.
18f3b 20 69 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c 6c if( sCheck.mall
18f3c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 ocFailed ){.
18f3d 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 sqlite3StrAccumR
18f3e 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 eset(&sCheck.err
18f3f 4d 73 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72 Msg);. *pnErr
18f40 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b 31 = sCheck.nErr+1
18f41 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
18f42 20 20 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73 }. *pnErr = s
18f43 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 Check.nErr;. if
18f44 28 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 ( sCheck.nErr==0
18f45 20 29 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 ) sqlite3StrAcc
18f46 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e umReset(&sCheck.
18f47 65 72 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 72 errMsg);. retur
18f48 6e 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 n sqlite3StrAccu
18f49 6d 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e mFinish(&sCheck.
18f4a 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 errMsg);.}.#endi
18f4b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
18f4c 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b _INTEGRITY_CHECK
18f4d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 */../*.** Retur
18f4e 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e n the full pathn
18f4f 61 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72 ame of the under
18f50 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 lying database f
18f51 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 ile..**.** The p
18f52 61 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 ager filename is
18f53 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f invariant as lo
18f54 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 ng as the pager
18f55 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 is.** open so it
18f56 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 is safe to acce
18f57 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 ss without the B
18f58 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a tShared mutex..*
18f59 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
18f5a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c const char *sql
18f5b 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 ite3BtreeGetFile
18f5c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a name(Btree *p){.
18f5d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 assert( p->pBt
18f5e 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 ->pPager!=0 );.
18f5f 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 return sqlite3P
18f60 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e agerFilename(p->
18f61 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a pBt->pPager);.}.
18f62 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
18f63 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 e pathname of th
18f64 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 e journal file f
18f65 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 or this database
18f66 2e 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 . The return.**
18f67 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f value of this ro
18f68 75 74 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d utine is the sam
18f69 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 e regardless of
18f6a 77 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75 72 whether the jour
18f6b 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 nal file.** has
18f6c 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f 72 20 been created or
18f6d 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 not..**.** The p
18f6e 61 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ager journal fil
18f6f 65 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 ename is invaria
18f70 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 nt as long as th
18f71 65 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 e pager is.** op
18f72 65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 en so it is safe
18f73 20 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f to access witho
18f74 75 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 ut the BtShared
18f75 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 mutex..*/.SQLITE
18f76 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 _PRIVATE const c
18f77 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 har *sqlite3Btre
18f78 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 eGetJournalname(
18f79 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 Btree *p){. ass
18f7a 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 ert( p->pBt->pPa
18f7b 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 ger!=0 );. retu
18f7c 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a rn sqlite3PagerJ
18f7d 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 ournalname(p->pB
18f7e 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f t->pPager);.}../
18f7f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d *.** Return non-
18f80 7a 65 72 6f 20 69 66 20 61 20 74 72 61 6e 73 61 zero if a transa
18f81 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e ction is active.
18f82 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
18f83 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 TE int sqlite3Bt
18f84 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 reeIsInTrans(Btr
18f85 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 ee *p){. assert
18f86 28 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 ( p==0 || sqlite
18f87 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 3_mutex_held(p->
18f88 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 db->mutex) );.
18f89 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d return (p && (p-
18f8a 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f >inTrans==TRANS_
18f8b 57 52 49 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a WRITE));.}../*.*
18f8c 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 * Return non-zer
18f8d 6f 20 69 66 20 61 20 72 65 61 64 20 28 6f 72 20 o if a read (or
18f8e 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 write) transacti
18f8f 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f on is active..*/
18f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
18f91 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 int sqlite3Btree
18f92 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 IsInReadTrans(Bt
18f93 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 ree *p){. asser
18f94 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 t( p );. assert
18f95 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
18f96 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 held(p->db->mute
18f97 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 x) );. return p
18f98 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 ->inTrans!=TRANS
18f99 5f 4e 4f 4e 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 _NONE;.}..SQLITE
18f9a 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
18f9b 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 ite3BtreeIsInBac
18f9c 6b 75 70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 kup(Btree *p){.
18f9d 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 assert( p );.
18f9e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
18f9f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 mutex_held(p->db
18fa0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 ->mutex) );. re
18fa1 74 75 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70 21 turn p->nBackup!
18fa2 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 =0;.}../*.** Thi
18fa3 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 s function retur
18fa4 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
18fa5 61 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 a blob of memory
18fa6 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
18fa7 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 .** a single sha
18fa8 72 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d red-btree. The m
18fa9 65 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 emory is used by
18faa 20 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 client code for
18fab 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 its own.** purp
18fac 6f 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c oses (for exampl
18fad 65 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 e, to store a hi
18fae 67 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 gh-level schema
18faf 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
18fb0 0a 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 .** the shared-b
18fb1 74 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 tree). The btree
18fb2 20 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 layer manages r
18fb3 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e eference countin
18fb4 67 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 g issues..**.**
18fb5 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 The first time t
18fb6 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e his is called on
18fb7 20 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c a shared-btree,
18fb8 20 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 nBytes bytes of
18fb9 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 memory.** are a
18fba 6c 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 llocated, zeroed
18fbb 2c 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 , and returned t
18fbc 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f o the caller. Fo
18fbd 72 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e r each subsequen
18fbe 74 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e t .** call the n
18fbf 42 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 Bytes parameter
18fc0 69 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 is ignored and a
18fc1 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 pointer to the
18fc2 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 same blob.** of
18fc3 6d 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e memory returned.
18fc4 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e .**.** If the n
18fc5 42 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 Bytes parameter
18fc6 69 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f is 0 and the blo
18fc7 62 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 b of memory has
18fc8 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 not yet been.**
18fc9 61 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c allocated, a nul
18fca 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 l pointer is ret
18fcb 75 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c urned. If the bl
18fcc 6f 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 ob has already b
18fcd 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 een.** allocated
18fce 2c 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 , it is returned
18fcf 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a as normal..**.*
18fd0 2a 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 * Just before th
18fd1 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 e shared-btree i
18fd2 73 20 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 s closed, the fu
18fd3 6e 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 nction passed as
18fd4 20 74 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 the .** xFree a
18fd5 72 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 rgument when the
18fd6 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
18fd7 6f 6e 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 on was made is i
18fd8 6e 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a nvoked on the .*
18fd9 2a 20 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 * blob of alloca
18fda 74 65 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 ted memory. This
18fdb 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 function should
18fdc 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 not call sqlite
18fdd 33 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 3_free().** on t
18fde 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 he memory, the b
18fdf 74 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 tree layer does
18fe0 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f that..*/.SQLITE_
18fe1 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 PRIVATE void *sq
18fe2 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 lite3BtreeSchema
18fe3 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e (Btree *p, int n
18fe4 42 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 Bytes, void(*xFr
18fe5 65 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 ee)(void *)){.
18fe6 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 BtShared *pBt =
18fe7 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 p->pBt;. sqlite
18fe8 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 3BtreeEnter(p);.
18fe9 20 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 if( !pBt->pSch
18fea 65 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b ema && nBytes ){
18feb 0a 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d . pBt->pSchem
18fec 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f a = sqlite3Mallo
18fed 63 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 cZero(nBytes);.
18fee 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 pBt->xFreeSch
18fef 65 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d ema = xFree;. }
18ff0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c . sqlite3BtreeL
18ff1 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 eave(p);. retur
18ff2 6e 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a n pBt->pSchema;.
18ff3 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
18ff4 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 SQLITE_LOCKED_SH
18ff5 41 52 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f AREDCACHE if ano
18ff6 74 68 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 ther user of the
18ff7 20 73 61 6d 65 20 73 68 61 72 65 64 20 0a 2a 2a same shared .**
18ff8 20 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72 btree as the ar
18ff9 67 75 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f gument handle ho
18ffa 6c 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 lds an exclusive
18ffb 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a lock on the .**
18ffc 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 sqlite_master t
18ffd 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20 able. Otherwise
18ffe 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 SQLITE_OK..*/.SQ
18fff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19000 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 sqlite3BtreeSch
19001 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 emaLocked(Btree
19002 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 *p){. int rc;.
19003 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
19004 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 _mutex_held(p->d
19005 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
19006 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
19007 28 70 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 (p);. rc = quer
19008 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c ySharedCacheTabl
19009 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f eLock(p, MASTER_
1900a 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 ROOT, READ_LOCK)
1900b 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d ;. assert( rc==
1900c 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d SQLITE_OK || rc=
1900d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 =SQLITE_LOCKED_S
1900e 48 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 HAREDCACHE );.
1900f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 sqlite3BtreeLeav
19010 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
19011 63 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 c;.}...#ifndef S
19012 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 QLITE_OMIT_SHARE
19013 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 D_CACHE./*.** Ob
19014 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 tain a lock on t
19015 68 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 he table whose r
19016 6f 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62 oot page is iTab
19017 2e 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 . The.** lock i
19018 73 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 s a write lock i
19019 66 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 f isWritelock is
1901a 20 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 true or a read
1901b 6c 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 lock.** if it is
1901c 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 false..*/.SQLIT
1901d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1901e 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 lite3BtreeLockTa
1901f 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e ble(Btree *p, in
19020 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 t iTab, u8 isWri
19021 74 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 teLock){. int r
19022 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
19023 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 assert( p->inTr
19024 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 ans!=TRANS_NONE
19025 29 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 );. if( p->shar
19026 61 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c able ){. u8 l
19027 6f 63 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c ockType = READ_L
19028 4f 43 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63 OCK + isWriteLoc
19029 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 52 k;. assert( R
1902a 45 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 EAD_LOCK+1==WRIT
1902b 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 E_LOCK );. as
1902c 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 sert( isWriteLoc
1902d 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c k==0 || isWriteL
1902e 6f 63 6b 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 73 ock==1 );.. s
1902f 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
19030 28 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 (p);. rc = qu
19031 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 erySharedCacheTa
19032 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c bleLock(p, iTab,
19033 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 lockType);.
19034 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
19035 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 K ){. rc =
19036 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 setSharedCacheTa
19037 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c bleLock(p, iTab,
19038 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 lockType);.
19039 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 }. sqlite3Btr
1903a 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a eeLeave(p);. }.
1903b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 return rc;.}.#
1903c 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 endif..#ifndef S
1903d 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 QLITE_OMIT_INCRB
1903e 4c 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 LOB./*.** Argume
1903f 6e 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 nt pCsr must be
19040 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 a cursor opened
19041 66 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 for writing on a
19042 6e 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 n .** INTKEY tab
19043 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 le currently poi
19044 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 nting at a valid
19045 20 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a table entry. .*
19046 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 * This function
19047 6d 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74 modifies the dat
19048 61 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 a stored as part
19049 20 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a of that entry..
1904a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 **.** Only the d
1904b 61 74 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 ata content may
1904c 6f 6e 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 only be modified
1904d 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 , it is not poss
1904e 69 62 6c 65 20 74 6f 20 0a 2a 2a 20 63 68 61 6e ible to .** chan
1904f 67 65 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 ge the length of
19050 20 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 the data stored
19051 2e 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 . If this functi
19052 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 on is called wit
19053 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 h.** parameters
19054 74 68 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20 that attempt to
19055 77 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 write past the e
19056 6e 64 20 6f 66 20 74 68 65 20 65 78 69 73 74 69 nd of the existi
19057 6e 67 20 64 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d ng data,.** no m
19058 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 odifications are
19059 20 6d 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45 made and SQLITE
1905a 5f 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 _CORRUPT is retu
1905b 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f rned..*/.SQLITE_
1905c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
1905d 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 te3BtreePutData(
1905e 42 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 BtCursor *pCsr,
1905f 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 u32 offset, u32
19060 61 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 amt, void *z){.
19061 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 int rc;. asser
19062 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 t( cursorHoldsMu
19063 74 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 tex(pCsr) );. 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 43 73 72 2d 3e utex_held(pCsr->
19066 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 pBtree->db->mute
19067 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 x) );. assert(
19068 70 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 pCsr->isIncrblob
19069 48 61 6e 64 6c 65 20 29 3b 0a 0a 20 20 72 63 20 Handle );.. rc
1906a 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 = restoreCursorP
1906b 6f 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 osition(pCsr);.
1906c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1906d 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e OK ){. return
1906e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 rc;. }. asser
1906f 74 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 t( pCsr->eState!
19070 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 =CURSOR_REQUIRES
19071 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 EEK );. if( pCs
19072 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f r->eState!=CURSO
19073 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 R_VALID ){. r
19074 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f eturn SQLITE_ABO
19075 52 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 RT;. }.. /* Ch
19076 65 63 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74 eck some assumpt
19077 69 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 ions: . ** (a
19078 29 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 ) the cursor is
19079 6f 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 open for writing
1907a 2c 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 ,. ** (b) the
1907b 72 65 20 69 73 20 61 20 72 65 61 64 2f 77 72 69 re is a read/wri
1907c 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f te transaction o
1907d 70 65 6e 2c 0a 20 20 2a 2a 20 20 20 28 63 29 20 pen,. ** (c)
1907e 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 the connection h
1907f 6f 6c 64 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 olds a write-loc
19080 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 28 k on the table (
19081 69 66 20 72 65 71 75 69 72 65 64 29 2c 0a 20 20 if required),.
19082 2a 2a 20 20 20 28 64 29 20 74 68 65 72 65 20 61 ** (d) there a
19083 72 65 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e re no conflictin
19084 67 20 72 65 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e g read-locks, an
19085 64 0a 20 20 2a 2a 20 20 20 28 65 29 20 74 68 65 d. ** (e) the
19086 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 cursor points a
19087 74 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 t a valid row of
19088 20 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 an intKey table
19089 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 .. */. if( !pC
1908a 73 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 sr->wrFlag ){.
1908b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1908c 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 READONLY;. }.
1908d 61 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70 assert( !pCsr->p
1908e 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 Bt->readOnly &&
1908f 70 43 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 pCsr->pBt->inTra
19090 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f nsaction==TRANS_
19091 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 WRITE );. asser
19092 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 t( hasSharedCach
19093 65 54 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d eTableLock(pCsr-
19094 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 >pBtree, pCsr->p
19095 67 6e 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29 gnoRoot, 0, 2) )
19096 3b 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 ;. assert( !has
19097 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 ReadConflicts(pC
19098 73 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72 sr->pBtree, pCsr
19099 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 ->pgnoRoot) );.
1909a 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61 assert( pCsr->a
1909b 70 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 pPage[pCsr->iPag
1909c 65 5d 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 e]->intKey );..
1909d 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 return accessPa
1909e 79 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 yload(pCsr, offs
1909f 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e et, amt, (unsign
190a0 65 64 20 63 68 61 72 20 2a 29 7a 2c 20 31 29 3b ed char *)z, 1);
190a1 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 .}../* .** Set a
190a2 20 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 flag on this cu
190a3 72 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 rsor to cache th
190a4 65 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 e locations of p
190a5 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a ages from the .*
190a6 2a 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 * overflow list
190a7 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 for the current
190a8 72 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 row. This is use
190a9 64 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 d by cursors ope
190aa 6e 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 ned.** for incre
190ab 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f mental blob IO o
190ac 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 nly..**.** This
190ad 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 function sets a
190ae 66 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 flag only. The a
190af 63 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 ctual page locat
190b0 69 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 ion cache.** (st
190b1 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 ored in BtCursor
190b2 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 .aOverflow[]) is
190b3 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 allocated and u
190b4 73 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a sed by function.
190b5 2a 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 ** accessPayload
190b6 28 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 () (the worker f
190b7 75 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 unction for sqli
190b8 74 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61 te3BtreeData() a
190b9 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 nd.** sqlite3Btr
190ba 65 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f eePutData())..*/
190bb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
190bc 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 void sqlite3Btre
190bd 65 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 eCacheOverflow(B
190be 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a tCursor *pCur){.
190bf 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 assert( cursor
190c0 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 HoldsMutex(pCur)
190c1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 );. assert( sq
190c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
190c3 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 (pCur->pBtree->d
190c4 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
190c5 73 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 ssert(!pCur->isI
190c6 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a ncrblobHandle);.
190c7 20 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e assert(!pCur->
190c8 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 aOverflow);. pC
190c9 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 ur->isIncrblobHa
190ca 6e 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 ndle = 1;.}.#end
190cb 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a if../***********
190cc 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 *** End of btree
190cd 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
190ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190d0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **/./***********
190d1 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 *** Begin file b
190d2 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a ackup.c ********
190d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190d5 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a **/./*.** 2009 J
190d6 61 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 anuary 28.**.**
190d7 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
190d8 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
190d9 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
190da 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
190db 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
190dc 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
190dd 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
190de 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
190df 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
190e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
190e1 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
190e2 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
190e3 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
190e4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
190e5 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
190e6 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
190e7 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
190e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
190ec 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 ********.** This
190ed 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 file contains t
190ee 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f he implementatio
190ef 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 n of the sqlite3
190f0 5f 62 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 2a _backup_XXX() .*
190f1 2a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 * API functions
190f2 61 6e 64 20 74 68 65 20 72 65 6c 61 74 65 64 20 and the related
190f3 66 65 61 74 75 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a features..*/../*
190f4 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 Macro to find t
190f5 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 he minimum of tw
190f6 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 o numeric values
190f7 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e ..*/.#ifndef MIN
190f8 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c .# define MIN(x,
190f9 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a y) ((x)<(y)?(x):
190fa 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a (y)).#endif../*.
190fb 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c ** Structure all
190fc 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 ocated for each
190fd 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e backup operation
190fe 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 ..*/.struct sqli
190ff 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73 te3_backup {. s
19100 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b qlite3* pDestDb;
19101 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 /* Desti
19102 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 nation database
19103 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 handle */. Btre
19104 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20 e *pDest;
19105 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 /* Destinat
19106 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20 ion b-tree file
19107 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 53 63 */. u32 iDestSc
19108 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a hema; /*
19109 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61 Original schema
1910a 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69 cookie in desti
1910b 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 nation */. int
1910c 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20 bDestLocked;
1910d 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 /* True onc
1910e 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 e a write-transa
1910f 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e ction is open on
19110 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e pDest */.. Pgn
19111 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20 o iNext;
19112 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 /* Page nu
19113 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 mber of the next
19114 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20 source page to
19115 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 copy */. sqlite
19116 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 20 20 3* pSrcDb;
19117 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 /* Source dat
19118 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a abase handle */.
19119 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20 Btree *pSrc;
1911a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f /* So
1911b 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65 urce b-tree file
1911c 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20 */.. int rc;
1911d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1911e 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73 /* Backup proces
1911f 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a s error code */.
19120 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20 . /* These two
19121 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 65 variables are se
19122 74 20 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20 t by every call
19123 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29 to backup_step()
19124 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20 . They are. **
19125 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f read by calls to
19126 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e backup_remainin
19127 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70 g() and backup_p
19128 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f agecount().. */
19129 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69 . Pgno nRemaini
1912a 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ng; /* N
1912b 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c umber of pages l
1912c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
1912d 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74 Pgno nPagecount
1912e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
1912f 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 al number of pag
19130 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20 es to copy */..
19131 20 69 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b int isAttached;
19132 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
19133 65 20 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61 e once backup ha
19134 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 s been registere
19135 64 20 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a d with pager */.
19136 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
19137 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 *pNext; /* Ne
19138 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69 xt backup associ
19139 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 65 ated with source
1913a 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a pager */.};../*
1913b 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 54 .** THREAD SAFET
1913c 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 Y NOTES:.**.**
1913d 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 65 Once it has bee
1913e 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 n created using
1913f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61 backup_init(), a
19140 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f single sqlite3_
19141 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75 backup.** stru
19142 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 63 cture may be acc
19143 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 72 essed via two gr
19144 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73 oups of thread-s
19145 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 afe entry points
19146 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 :.**.** * Vi
19147 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 a the sqlite3_ba
19148 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66 ckup_XXX() API f
19149 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73 unction backup_s
1914a 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20 tep() and .**
1914b 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73 backup_finis
1914c 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20 h(). Both these
1914d 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e functions obtain
1914e 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
1914f 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61 base.** ha
19150 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74 ndle mutex and t
19151 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 he mutex associa
19152 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 ted with the sou
19153 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a rce BtShared .**
19154 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65 structure
19155 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e , in that order.
19156 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61 .**.** * Via
19157 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 74 the BackupUpdat
19158 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65 e() and BackupRe
19159 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e start() function
1915a 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20 s, which are.**
1915b 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79 invoked by
1915c 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 the pager layer
1915d 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f to report vario
1915e 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 73 us state changes
1915f 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 in.** the
19160 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 6f page cache asso
19161 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19162 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e source database.
19163 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20 The mutex.**
19164 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 associated w
19165 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64 ith the source d
19166 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 64 atabase BtShared
19167 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 structure will
19168 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20 always .**
19169 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69 be held when ei
1916a 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 75 ther of these fu
1916b 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f nctions are invo
1916c 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 ked..**.** The
1916d 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62 other sqlite3_b
1916e 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 ackup_XXX() API
1916f 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75 functions, backu
19170 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e p_remaining() an
19171 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61 d.** backup_pa
19172 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f gecount() are no
19173 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 75 t thread-safe fu
19174 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79 nctions. If they
19175 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20 are called.**
19176 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65 while some othe
19177 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c r thread is call
19178 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28 ing backup_step(
19179 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69 ) or backup_fini
1917a 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76 sh(),.** the v
1917b 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d alues returned m
1917c 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54 ay be invalid. T
1917d 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 here is no way f
1917e 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 or a call to.**
1917f 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 BackupUpdate()
19180 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 72 or BackupRestar
19181 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65 t() to interfere
19182 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d with backup_rem
19183 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72 aining().** or
19184 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e backup_pagecoun
19185 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70 t()..**.** Dep
19186 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51 ending on the SQ
19187 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69 Lite configurati
19188 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 on, the database
19189 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a handles and/or.
1918a 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 6f ** the Btree o
1918b 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 20 bjects may have
1918c 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65 their own mutexe
1918d 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 6c s that require l
1918e 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e ocking..** Non
1918f 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 -sharable Btrees
19190 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 (in-memory data
19191 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c bases for exampl
19192 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a e), do not have.
19193 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 ** associated
19194 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a mutexes..*/../*.
19195 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e ** Return a poin
19196 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ter correspondin
19197 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44 g to database zD
19198 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 b (i.e. "main",
19199 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f "temp").** in co
1919a 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 nnection handle
1919b 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 64 pDb. If such a d
1919c 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 atabase cannot b
1919d 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a e found, return.
1919e 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 ** a NULL pointe
1919f 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65 r and write an e
191a0 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 rror message to
191a1 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 pErrorDb..**.**
191a2 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 61 If the "temp" da
191a3 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 73 tabase is reques
191a4 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64 ted, it may need
191a5 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79 to be opened by
191a6 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69 this .** functi
191a7 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 on. If an error
191a8 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69 occurs while doi
191a9 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20 ng so, return 0
191aa 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a and write an .**
191ab 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 error message t
191ac 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73 o pErrorDb..*/.s
191ad 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e tatic Btree *fin
191ae 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a dBtree(sqlite3 *
191af 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65 pErrorDb, sqlite
191b0 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68 3 *pDb, const ch
191b1 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20 ar *zDb){. int
191b2 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 i = sqlite3FindD
191b3 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b bName(pDb, zDb);
191b4 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a .. if( i==1 ){.
191b5 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 Parse *pPars
191b6 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 e;. int rc =
191b7 30 3b 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20 0;. pParse =
191b8 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f sqlite3StackAllo
191b9 63 5a 65 72 6f 28 70 45 72 72 6f 72 44 62 2c 20 cZero(pErrorDb,
191ba 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 sizeof(*pParse))
191bb 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 ;. if( pParse
191bc 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
191bd 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 ite3Error(pError
191be 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d Db, SQLITE_NOMEM
191bf 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 , "out of memory
191c0 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ");. rc = S
191c1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
191c2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 }else{. pP
191c3 61 72 73 65 2d 3e 64 62 20 3d 20 70 44 62 3b 0a arse->db = pDb;.
191c4 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
191c5 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 3OpenTempDatabas
191c6 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 e(pParse) ){.
191c7 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
191c8 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a rClear(pParse);.
191c9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
191ca 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 70 rror(pErrorDb, p
191cb 50 61 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c Parse->rc, "%s",
191cc 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 pParse->zErrMsg
191cd 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 );. rc =
191ce 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
191cf 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
191d0 74 65 33 53 74 61 63 6b 46 72 65 65 28 70 45 72 te3StackFree(pEr
191d1 72 6f 72 44 62 2c 20 70 50 61 72 73 65 29 3b 0a rorDb, pParse);.
191d2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
191d3 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
191d4 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 0;. }. }..
191d5 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20 if( i<0 ){.
191d6 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72 sqlite3Error(pEr
191d7 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 rorDb, SQLITE_ER
191d8 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61 ROR, "unknown da
191d9 74 61 62 61 73 65 20 25 73 22 2c 20 7a 44 62 29 tabase %s", zDb)
191da 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a ;. return 0;.
191db 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 44 }.. return pD
191dc 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 7d b->aDb[i].pBt;.}
191dd 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 ../*.** Create a
191de 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 n sqlite3_backup
191df 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79 process to copy
191e0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 the contents of
191e1 20 7a 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20 zSrcDb from.**
191e2 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c connection handl
191e3 65 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73 e pSrcDb to zDes
191e4 74 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20 tDb in pDestDb.
191e5 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72 If successful, r
191e6 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 eturn.** a point
191e7 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71 er to the new sq
191e8 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a lite3_backup obj
191e9 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e ect..**.** If an
191ea 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e error occurs, N
191eb 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 ULL is returned
191ec 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 and an error cod
191ed 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 e and error mess
191ee 61 67 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e age.** stored in
191ef 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
191f0 20 70 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c pDestDb..*/.SQL
191f1 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
191f2 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f backup *sqlite3_
191f3 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 backup_init(. s
191f4 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c qlite3* pDestDb,
191f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
191f6 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
191f7 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a to write to */.
191f8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
191f9 65 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20 estDb,
191fa 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 /* Name
191fb 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 68 of database with
191fc 69 6e 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20 in pDestDb */.
191fd 73 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c sqlite3* pSrcDb,
191fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
191ff 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
19200 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 e connection to
19201 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63 read from */. c
19202 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44 onst char *zSrcD
19203 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b
19204 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 /* Name of
19205 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 database within
19206 70 53 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73 pSrcDb */.){. s
19207 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 qlite3_backup *p
19208 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19209 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
1920a 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a return */.. /*
1920b 20 4c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65 Lock the source
1920c 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 database handle
1920d 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f . The destinatio
1920e 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 n database. **
1920f 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f handle is not lo
19210 63 6b 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75 cked in this rou
19211 74 69 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20 tine, but it is
19212 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73 locked in. ** s
19213 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
19214 65 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69 ep(). The user i
19215 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e s required to en
19216 73 75 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a sure that no. *
19217 2a 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61 * other thread a
19218 63 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74 ccesses the dest
19219 69 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66 ination handle f
1921a 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a or the duration.
1921b 20 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b ** of the back
1921c 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41 up operation. A
1921d 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 ny attempt to us
1921e 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f e the destinatio
1921f 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 n. ** database
19220 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 connection while
19221 20 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 a backup is in
19222 70 72 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75 progress may cau
19223 73 65 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e se. ** a malfun
19224 63 74 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c ction or a deadl
19225 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ock.. */. sqli
19226 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
19227 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a pSrcDb->mutex);.
19228 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
19229 65 6e 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d enter(pDestDb->m
1922a 75 74 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53 utex);.. if( pS
1922b 72 63 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b rcDb==pDestDb ){
1922c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f . sqlite3Erro
1922d 72 28 0a 20 20 20 20 20 20 20 20 70 44 65 73 74 r(. pDest
1922e 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 Db, SQLITE_ERROR
1922f 2c 20 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65 , "source and de
19230 73 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62 stination must b
19231 65 20 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20 e distinct".
19232 29 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 );. p = 0;.
19233 7d 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 }else {. /* A
19234 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f llocate space fo
19235 72 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f r a new sqlite3_
19236 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 2a 2f backup object */
19237 0a 20 20 20 20 70 20 3d 20 28 73 71 6c 69 74 65 . p = (sqlite
19238 33 5f 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 74 3_backup *)sqlit
19239 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 e3_malloc(sizeof
1923a 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29 (sqlite3_backup)
1923b 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b );. if( !p ){
1923c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 . sqlite3Er
1923d 72 6f 72 28 70 44 65 73 74 44 62 2c 20 53 51 4c ror(pDestDb, SQL
1923e 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 ITE_NOMEM, 0);.
1923f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
19240 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e f the allocation
19241 20 73 75 63 63 65 65 64 65 64 2c 20 70 6f 70 75 succeeded, popu
19242 6c 61 74 65 20 74 68 65 20 6e 65 77 20 6f 62 6a late the new obj
19243 65 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20 ect. */. if( p
19244 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c ){. memset(p,
19245 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 0, sizeof(sqlit
19246 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 e3_backup));.
19247 20 70 2d 3e 70 53 72 63 20 3d 20 66 69 6e 64 42 p->pSrc = findB
19248 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70 53 tree(pDestDb, pS
19249 72 63 44 62 2c 20 7a 53 72 63 44 62 29 3b 0a 20 rcDb, zSrcDb);.
1924a 20 20 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69 p->pDest = fi
1924b 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 2c ndBtree(pDestDb,
1924c 20 70 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44 pDestDb, zDestD
1924d 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74 b);. p->pDest
1924e 44 62 20 3d 20 70 44 65 73 74 44 62 3b 0a 20 20 Db = pDestDb;.
1924f 20 20 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53 p->pSrcDb = pS
19250 72 63 44 62 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 rcDb;. p->iNe
19251 78 74 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 69 xt = 1;. p->i
19252 73 41 74 74 61 63 68 65 64 20 3d 20 30 3b 0a 0a sAttached = 0;..
19253 20 20 20 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53 if( 0==p->pS
19254 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73 rc || 0==p->pDes
19255 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e t ){. /* On
19256 65 20 28 6f 72 20 62 6f 74 68 29 20 6f 66 20 74 e (or both) of t
19257 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 he named databas
19258 65 73 20 64 69 64 20 6e 6f 74 20 65 78 69 73 74 es did not exist
19259 2e 20 41 6e 20 65 72 72 6f 72 20 68 61 73 0a 20 . An error has.
1925a 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 ** already
1925b 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74 been written int
1925c 6f 20 74 68 65 20 70 44 65 73 74 44 62 20 68 61 o the pDestDb ha
1925d 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 61 74 20 69 ndle. All that i
1925e 73 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2a 20 s left. **
1925f 74 6f 20 64 6f 20 68 65 72 65 20 69 73 20 66 72 to do here is fr
19260 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 ee the sqlite3_b
19261 61 63 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e ackup structure.
19262 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
19263 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b sqlite3_free(p);
19264 0a 20 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 . p = 0;.
19265 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20 }. }. if( p
19266 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e ){. p->pSrc->
19267 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a nBackup++;. }..
19268 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
19269 6c 65 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d leave(pDestDb->m
1926a 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 utex);. sqlite3
1926b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72 _mutex_leave(pSr
1926c 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 cDb->mutex);. r
1926d 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn p;.}../*.*
1926e 2a 20 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73 * Argument rc is
1926f 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 an SQLite error
19270 20 63 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72 code. Return tr
19271 75 65 20 69 66 20 74 68 69 73 20 65 72 72 6f 72 ue if this error
19272 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 is .** consider
19273 65 64 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f ed fatal if enco
19274 75 6e 74 65 72 65 64 20 64 75 72 69 6e 67 20 61 untered during a
19275 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f backup operatio
19276 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a n. All errors.**
19277 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 are considered
19278 66 61 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72 fatal except for
19279 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 SQLITE_BUSY and
1927a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a SQLITE_LOCKED..
1927b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 */.static int is
1927c 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72 FatalError(int r
1927d 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 c){. return (rc
1927e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 !=SQLITE_OK && r
1927f 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26 c!=SQLITE_BUSY &
19280 26 20 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c & ALWAYS(rc!=SQL
19281 49 54 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a ITE_LOCKED));.}.
19282 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 ./*.** Parameter
19283 20 7a 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73 zSrcData points
19284 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e to a buffer con
19285 74 61 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61 taining the data
19286 20 66 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53 for .** page iS
19287 72 63 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f rcPg from the so
19288 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 urce database. C
19289 6f 70 79 20 74 68 69 73 20 64 61 74 61 20 69 6e opy this data in
1928a 74 6f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69 to the .** desti
1928b 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e nation database.
1928c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 .*/.static int b
1928d 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c ackupOnePage(sql
1928e 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20 ite3_backup *p,
1928f 50 67 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e Pgno iSrcPg, con
19290 73 74 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29 st u8 *zSrcData)
19291 7b 0a 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 {. Pager * cons
19292 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73 t pDestPager = s
19293 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 qlite3BtreePager
19294 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f (p->pDest);. co
19295 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a nst int nSrcPgsz
19296 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
19297 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 etPageSize(p->pS
19298 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74 rc);. int nDest
19299 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 Pgsz = sqlite3Bt
1929a 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 reeGetPageSize(p
1929b 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 ->pDest);. cons
1929c 74 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49 t int nCopy = MI
1929d 4e 28 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73 N(nSrcPgsz, nDes
1929e 74 50 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20 tPgsz);. const
1929f 69 36 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29 i64 iEnd = (i64)
192a0 69 53 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63 iSrcPg*(i64)nSrc
192a1 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 20 Pgsz;.. int rc
192a2 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 = SQLITE_OK;. i
192a3 36 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65 64 iOff;.. asse
192a4 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b rt( p->bDestLock
192a5 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 ed );. assert(
192a6 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d !isFatalError(p-
192a7 3e 72 63 29 20 29 3b 0a 20 20 61 73 73 65 72 74 >rc) );. assert
192a8 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e ( iSrcPg!=PENDIN
192a9 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 G_BYTE_PAGE(p->p
192aa 53 72 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 61 Src->pBt) );. a
192ab 73 73 65 72 74 28 20 7a 53 72 63 44 61 74 61 20 ssert( zSrcData
192ac 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 74 );.. /* Catch t
192ad 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 he case where th
192ae 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 e destination is
192af 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 an in-memory da
192b0 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a 20 tabase and the.
192b1 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 73 20 6f ** page sizes o
192b2 66 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 f the source and
192b3 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 66 destination dif
192b4 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 fer. . */. if(
192b5 20 6e 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 74 nSrcPgsz!=nDest
192b6 50 67 73 7a 20 26 26 20 73 71 6c 69 74 65 33 50 Pgsz && sqlite3P
192b7 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69 agerIsMemdb(sqli
192b8 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d te3BtreePager(p-
192b9 3e 70 44 65 73 74 29 29 20 29 7b 0a 20 20 20 20 >pDest)) ){.
192ba 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 rc = SQLITE_READ
192bb 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ONLY;. }.. /*
192bc 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f This loop runs o
192bd 6e 63 65 20 66 6f 72 20 65 61 63 68 20 64 65 73 nce for each des
192be 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 70 tination page sp
192bf 61 6e 6e 65 64 20 62 79 20 74 68 65 20 73 6f 75 anned by the sou
192c0 72 63 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e 20 rce . ** page.
192c1 46 6f 72 20 65 61 63 68 20 69 74 65 72 61 74 69 For each iterati
192c2 6f 6e 2c 20 76 61 72 69 61 62 6c 65 20 69 4f 66 on, variable iOf
192c3 66 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 f is set to the
192c4 62 79 74 65 20 6f 66 66 73 65 74 0a 20 20 2a 2a byte offset. **
192c5 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 of the destinat
192c6 69 6f 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 ion page.. */.
192c7 20 66 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d 28 for(iOff=iEnd-(
192c8 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 63 i64)nSrcPgsz; rc
192c9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
192ca 4f 66 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b 3d Off<iEnd; iOff+=
192cb 6e 44 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 20 nDestPgsz){.
192cc 44 62 50 61 67 65 20 2a 70 44 65 73 74 50 67 20 DbPage *pDestPg
192cd 3d 20 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 44 = 0;. Pgno iD
192ce 65 73 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66 est = (Pgno)(iOf
192cf 66 2f 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b 0a f/nDestPgsz)+1;.
192d0 20 20 20 20 69 66 28 20 69 44 65 73 74 3d 3d 50 if( iDest==P
192d1 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 ENDING_BYTE_PAGE
192d2 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 (p->pDest->pBt)
192d3 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
192d4 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 if( SQLITE_OK==(
192d5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 rc = sqlite3Page
192d6 72 47 65 74 28 70 44 65 73 74 50 61 67 65 72 2c rGet(pDestPager,
192d7 20 69 44 65 73 74 2c 20 26 70 44 65 73 74 50 67 iDest, &pDestPg
192d8 29 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54 )). && SQLIT
192d9 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 E_OK==(rc = sqli
192da 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 te3PagerWrite(pD
192db 65 73 74 50 67 29 29 0a 20 20 20 20 29 7b 0a 20 estPg)). ){.
192dc 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a const u8 *z
192dd 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69 In = &zSrcData[i
192de 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a 20 Off%nSrcPgsz];.
192df 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44 61 u8 *zDestDa
192e0 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 ta = sqlite3Page
192e1 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50 67 rGetData(pDestPg
192e2 29 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 75 );. u8 *zOu
192e3 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61 5b 69 t = &zDestData[i
192e4 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b 0a Off%nDestPgsz];.
192e5 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 . /* Copy t
192e6 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 he data from the
192e7 20 73 6f 75 72 63 65 20 70 61 67 65 20 69 6e 74 source page int
192e8 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f o the destinatio
192e9 6e 20 70 61 67 65 2e 0a 20 20 20 20 20 20 2a 2a n page.. **
192ea 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 Then clear the
192eb 42 74 72 65 65 20 6c 61 79 65 72 20 4d 65 6d 50 Btree layer MemP
192ec 61 67 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 2e age.isInit flag.
192ed 20 42 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 6c Both this modul
192ee 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74 e. ** and t
192ef 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 75 73 he pager code us
192f0 65 20 74 68 69 73 20 74 72 69 63 6b 20 28 63 6c e this trick (cl
192f1 65 61 72 69 6e 67 20 74 68 65 20 66 69 72 73 74 earing the first
192f2 20 62 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 6f byte. ** o
192f3 66 20 74 68 65 20 70 61 67 65 20 27 65 78 74 72 f the page 'extr
192f4 61 27 20 73 70 61 63 65 20 74 6f 20 69 6e 76 61 a' space to inva
192f5 6c 69 64 61 74 65 20 74 68 65 20 42 74 72 65 65 lidate the Btree
192f6 20 6c 61 79 65 72 73 0a 20 20 20 20 20 20 2a 2a layers. **
192f7 20 63 61 63 68 65 64 20 70 61 72 73 65 20 6f 66 cached parse of
192f8 20 74 68 65 20 70 61 67 65 29 2e 20 4d 65 6d 50 the page). MemP
192f9 61 67 65 2e 69 73 49 6e 69 74 20 69 73 20 6d 61 age.isInit is ma
192fa 72 6b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 22 rked . ** "
192fb 4d 55 53 54 20 42 45 20 46 49 52 53 54 22 20 66 MUST BE FIRST" f
192fc 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e or this purpose.
192fd 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
192fe 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 6e memcpy(zOut, zIn
192ff 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 , nCopy);.
19300 28 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 ((u8 *)sqlite3Pa
19301 67 65 72 47 65 74 45 78 74 72 61 28 70 44 65 73 gerGetExtra(pDes
19302 74 50 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 20 tPg))[0] = 0;.
19303 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50 }. sqlite3P
19304 61 67 65 72 55 6e 72 65 66 28 70 44 65 73 74 50 agerUnref(pDestP
19305 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 g);. }.. retur
19306 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 n rc;.}../*.** I
19307 66 20 70 46 69 6c 65 20 69 73 20 63 75 72 72 65 f pFile is curre
19308 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e ntly larger than
19309 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68 iSize bytes, th
1930a 65 6e 20 74 72 75 6e 63 61 74 65 20 69 74 20 74 en truncate it t
1930b 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 69 o.** exactly iSi
1930c 7a 65 20 62 79 74 65 73 2e 20 49 66 20 70 46 69 ze bytes. If pFi
1930d 6c 65 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 72 le is not larger
1930e 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65 than iSize byte
1930f 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 s, then.** this
19310 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f function is a no
19311 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 -op..**.** Retur
19312 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 n SQLITE_OK if e
19313 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 verything is suc
19314 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 cessful, or an S
19315 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20 QLite error .**
19316 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 code if an error
19317 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 occurs..*/.stat
19318 69 63 20 69 6e 74 20 62 61 63 6b 75 70 54 72 75 ic int backupTru
19319 6e 63 61 74 65 46 69 6c 65 28 73 71 6c 69 74 65 ncateFile(sqlite
1931a 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 3_file *pFile, i
1931b 36 34 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 34 64 iSize){. i64
1931c 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 74 iCurrent;. int
1931d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 rc = sqlite3OsF
1931e 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26 ileSize(pFile, &
1931f 69 43 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 28 iCurrent);. if(
19320 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
19321 26 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65 & iCurrent>iSize
19322 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
19323 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 ite3OsTruncate(p
19324 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20 File, iSize);.
19325 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
19326 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 ../*.** Register
19327 20 74 68 69 73 20 62 61 63 6b 75 70 20 6f 62 6a this backup obj
19328 65 63 74 20 77 69 74 68 20 74 68 65 20 61 73 73 ect with the ass
19329 6f 63 69 61 74 65 64 20 73 6f 75 72 63 65 20 70 ociated source p
1932a 61 67 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c 6c ager for.** call
1932b 62 61 63 6b 73 20 77 68 65 6e 20 70 61 67 65 73 backs when pages
1932c 20 61 72 65 20 63 68 61 6e 67 65 64 20 6f 72 20 are changed or
1932d 74 68 65 20 63 61 63 68 65 20 69 6e 76 61 6c 69 the cache invali
1932e 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 dated..*/.static
1932f 20 76 6f 69 64 20 61 74 74 61 63 68 42 61 63 6b void attachBack
19330 75 70 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33 upObject(sqlite3
19331 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 _backup *p){. s
19332 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a qlite3_backup **
19333 70 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 pp;. assert( sq
19334 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d lite3BtreeHoldsM
19335 75 74 65 78 28 70 2d 3e 70 53 72 63 29 20 29 3b utex(p->pSrc) );
19336 0a 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 . pp = sqlite3P
19337 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 agerBackupPtr(sq
19338 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 lite3BtreePager(
19339 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d 3e p->pSrc));. p->
1933a 70 4e 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 2a pNext = *pp;. *
1933b 70 70 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 41 pp = p;. p->isA
1933c 74 74 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a 0a ttached = 1;.}..
1933d 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 65 /*.** Copy nPage
1933e 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 pages from the
1933f 73 6f 75 72 63 65 20 62 2d 74 72 65 65 20 74 6f source b-tree to
19340 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
19341 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
19342 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b int sqlite3_back
19343 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f up_step(sqlite3_
19344 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e backup *p, int n
19345 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b Page){. int rc;
19346 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 .. sqlite3_mute
19347 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 x_enter(p->pSrcD
19348 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c b->mutex);. sql
19349 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 ite3BtreeEnter(p
1934a 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70 ->pSrc);. if( p
1934b 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
1934c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 sqlite3_mutex_e
1934d 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d nter(p->pDestDb-
1934e 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 >mutex);. }..
1934f 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66 rc = p->rc;. if
19350 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 ( !isFatalError(
19351 72 63 29 20 29 7b 0a 20 20 20 20 50 61 67 65 72 rc) ){. Pager
19352 20 2a 20 63 6f 6e 73 74 20 70 53 72 63 50 61 67 * const pSrcPag
19353 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 er = sqlite3Btre
19354 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 3b ePager(p->pSrc);
19355 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 /* Source p
19356 61 67 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 65 ager */. Page
19357 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50 r * const pDestP
19358 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 ager = sqlite3Bt
19359 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73 reePager(p->pDes
1935a 74 29 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 61 t); /* Dest pa
1935b 67 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ger */. int i
1935c 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1935d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1935e 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 Iterator variab
1935f 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 le */. int nS
19360 72 63 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 20 rcPage = -1;
19361 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19362 53 69 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 64 Size of source d
19363 62 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 b in pages */.
19364 20 20 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e int bCloseTran
19365 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 s = 0;
19366 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
19367 73 72 63 20 64 62 20 72 65 71 75 69 72 65 73 20 src db requires
19368 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20 unlocking */..
19369 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 /* If the sour
1936a 63 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72 ce pager is curr
1936b 65 6e 74 6c 79 20 69 6e 20 61 20 77 72 69 74 65 ently in a write
1936c 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65 -transaction, re
1936d 74 75 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49 turn. ** SQLI
1936e 54 45 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74 TE_BUSY immediat
1936f 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ely.. */.
19370 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26 if( p->pDestDb &
19371 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e & p->pSrc->pBt->
19372 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 inTransaction==T
19373 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 RANS_WRITE ){.
19374 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
19375 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b BUSY;. }else{
19376 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
19377 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20 TE_OK;. }..
19378 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65 /* Lock the de
19379 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 stination databa
1937a 73 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 se, if it is not
1937b 20 6c 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 2e locked already.
1937c 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 */. if( SQLI
1937d 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e TE_OK==rc && p->
1937e 62 44 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20 bDestLocked==0.
1937f 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b && SQLITE_OK
19380 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 ==(rc = sqlite3B
19381 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70 treeBeginTrans(p
19382 2d 3e 70 44 65 73 74 2c 20 32 29 29 20 0a 20 20 ->pDest, 2)) .
19383 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44 ){. p->bD
19384 65 73 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 estLocked = 1;.
19385 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
19386 65 47 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 73 eGetMeta(p->pDes
19387 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f t, BTREE_SCHEMA_
19388 56 45 52 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65 VERSION, &p->iDe
19389 73 74 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d stSchema);. }
1938a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 .. /* If ther
1938b 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61 e is no open rea
1938c 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e d-transaction on
1938d 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 the source data
1938e 62 61 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a base, open. *
1938f 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 * one now. If a
19390 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f transaction is o
19391 70 65 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e pened here, then
19392 20 69 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 it will be clos
19393 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 ed. ** before
19394 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 this function e
19395 78 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 xits.. */.
19396 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
19397 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 OK && 0==sqlite3
19398 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 BtreeIsInReadTra
19399 6e 73 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20 ns(p->pSrc) ){.
1939a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
1939b 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 3BtreeBeginTrans
1939c 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20 (p->pSrc, 0);.
1939d 20 20 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 bCloseTrans
1939e 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 = 1;. }. .
1939f 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68 /* Now that th
193a0 65 72 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f ere is a read-lo
193a1 63 6b 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 ck on the source
193a2 20 64 61 74 61 62 61 73 65 2c 20 71 75 65 72 79 database, query
193a3 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72 the. ** sour
193a4 63 65 20 70 61 67 65 72 20 66 6f 72 20 74 68 65 ce pager for the
193a5 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 number of pages
193a6 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
193a7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
193a8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
193a9 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c {. rc = sql
193aa 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 ite3PagerPagecou
193ab 6e 74 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e nt(pSrcPager, &n
193ac 53 72 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a SrcPage);. }.
193ad 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e for(ii=0; (n
193ae 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61 Page<0 || ii<nPa
193af 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c ge) && p->iNext<
193b0 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 =(Pgno)nSrcPage
193b1 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 && !rc; ii++){.
193b2 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 const Pgno
193b3 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78 iSrcPg = p->iNex
193b4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t;
193b5 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 /* Source pag
193b6 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 e number */.
193b7 20 20 69 66 28 20 69 53 72 63 50 67 21 3d 50 45 if( iSrcPg!=PE
193b8 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 NDING_BYTE_PAGE(
193b9 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b p->pSrc->pBt) ){
193ba 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20 . DbPage
193bb 2a 70 53 72 63 50 67 3b 20 20 20 20 20 20 20 20 *pSrcPg;
193bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
193bd 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 /* Source p
193be 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 age object */.
193bf 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 rc = sqlit
193c0 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50 e3PagerGet(pSrcP
193c1 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70 ager, iSrcPg, &p
193c2 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 SrcPg);.
193c3 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
193c4 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 K ){. r
193c5 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 c = backupOnePag
193c6 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c e(p, iSrcPg, sql
193c7 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 ite3PagerGetData
193c8 28 70 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20 (pSrcPg));.
193c9 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 sqlite3Page
193ca 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a rUnref(pSrcPg);.
193cb 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
193cc 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74 }. p->iNext
193cd 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
193ce 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
193cf 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67 ){. p->nPag
193d0 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67 ecount = nSrcPag
193d1 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d e;. p->nRem
193d2 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67 aining = nSrcPag
193d3 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20 e+1-p->iNext;.
193d4 20 20 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74 if( p->iNext
193d5 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20 >(Pgno)nSrcPage
193d6 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
193d7 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 SQLITE_DONE;.
193d8 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 2d }else if( !p-
193d9 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 >isAttached ){.
193da 20 20 20 20 20 20 20 61 74 74 61 63 68 42 61 63 attachBac
193db 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b 0a 20 20 kupObject(p);.
193dc 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 }. }. .
193dd 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 /* Update the
193de 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20 schema version
193df 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73 field in the des
193e0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 tination databas
193e1 65 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 e. This. ** i
193e2 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 s to make sure t
193e3 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 2d 76 hat the schema-v
193e4 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64 6f ersion really do
193e5 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20 20 20 es change in.
193e6 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77 68 65 ** the case whe
193e7 72 65 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e re the source an
193e8 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 d destination da
193e9 74 61 62 61 73 65 73 20 68 61 76 65 20 74 68 65 tabases have the
193ea 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68 . ** same sch
193eb 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20 ema version..
193ec 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d */. if( rc==
193ed 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 20 SQLITE_DONE .
193ee 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 && (rc = sqlit
193ef 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 e3BtreeUpdateMet
193f0 61 28 70 2d 3e 70 44 65 73 74 2c 31 2c 70 2d 3e a(p->pDest,1,p->
193f1 69 44 65 73 74 53 63 68 65 6d 61 2b 31 29 29 3d iDestSchema+1))=
193f2 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 29 =SQLITE_OK. )
193f3 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e {. const in
193f4 74 20 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d t nSrcPagesize =
193f5 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 sqlite3BtreeGet
193f6 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 PageSize(p->pSrc
193f7 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 );. const i
193f8 6e 74 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65 nt nDestPagesize
193f9 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 = sqlite3BtreeG
193fa 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 etPageSize(p->pD
193fb 65 73 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 est);. int
193fc 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20 nDestTruncate;.
193fd 20 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 . if( p->p
193fe 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 20 20 DestDb ){.
193ff 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
19400 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d 3e ternalSchema(p->
19401 70 44 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 20 pDestDb, 0);.
19402 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53 }.. /* S
19403 65 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 et nDestTruncate
19404 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e 75 to the final nu
19405 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
19406 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
19407 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 . ** databa
19408 73 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61 se. The complica
19409 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 tion here is tha
1940a 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f t the destinatio
1940b 6e 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 n page. **
1940c 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66 size may be diff
1940d 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75 erent to the sou
1940e 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a rce page size. .
1940f 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a **. *
19410 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 * If the source
19411 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 6d 61 page size is sma
19412 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 ller than the de
19413 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 stination page s
19414 69 7a 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 72 ize, . ** r
19415 6f 75 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 73 ound up. In this
19416 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 case the call t
19417 6f 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 o sqlite3OsTrunc
19418 61 74 65 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c ate() below will
19419 0a 20 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68 . ** fix th
1941a 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
1941b 6c 65 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69 le. However it i
1941c 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 s important to c
1941d 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c all. ** sql
1941e 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 ite3PagerTruncat
1941f 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73 6f eImage() here so
19420 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73 20 that any pages
19421 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a in the . **
19422 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c destination fil
19423 65 20 74 68 61 74 20 6c 69 65 20 62 65 79 6f 6e e that lie beyon
19424 64 20 74 68 65 20 6e 44 65 73 74 54 72 75 6e 63 d the nDestTrunc
19425 61 74 65 20 70 61 67 65 20 6d 61 72 6b 20 61 72 ate page mark ar
19426 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e e. ** journ
19427 61 6c 6c 65 64 20 62 79 20 50 61 67 65 72 43 6f alled by PagerCo
19428 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 62 mmitPhaseOne() b
19429 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 64 efore they are d
1942a 65 73 74 72 6f 79 65 64 0a 20 20 20 20 20 20 2a estroyed. *
1942b 2a 20 62 79 20 74 68 65 20 66 69 6c 65 20 74 72 * by the file tr
1942c 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 uncation..
1942d 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 */. if( nSr
1942e 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 cPagesize<nDestP
1942f 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 agesize ){.
19430 20 20 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 6e int ratio = n
19431 44 65 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 72 DestPagesize/nSr
19432 63 50 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 20 cPagesize;.
19433 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 nDestTruncate
19434 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74 = (nSrcPage+rat
19435 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20 io-1)/ratio;.
19436 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 72 if( nDestTr
19437 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e uncate==(int)PEN
19438 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
19439 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b ->pDest->pBt) ){
1943a 0a 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 74 . nDest
1943b 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 20 Truncate--;.
1943c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
1943d 65 7b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74 e{. nDest
1943e 54 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50 Truncate = nSrcP
1943f 61 67 65 20 2a 20 28 6e 53 72 63 50 61 67 65 73 age * (nSrcPages
19440 69 7a 65 2f 6e 44 65 73 74 50 61 67 65 73 69 7a ize/nDestPagesiz
19441 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 e);. }.
19442 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 sqlite3PagerTr
19443 75 6e 63 61 74 65 49 6d 61 67 65 28 70 44 65 73 uncateImage(pDes
19444 74 50 61 67 65 72 2c 20 6e 44 65 73 74 54 72 75 tPager, nDestTru
19445 6e 63 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 69 ncate);.. i
19446 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c f( nSrcPagesize<
19447 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b nDestPagesize ){
19448 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 . /* If t
19449 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73 he source page-s
1944a 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 ize is smaller t
1944b 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 han the destinat
1944c 69 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20 ion page-size,.
1944d 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 78 ** two ex
1944e 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20 6e tra things may n
1944f 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a 20 eed to happen:.
19450 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
19451 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 73 ** * The des
19452 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 65 tination may nee
19453 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65 d to be truncate
19454 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a d, and. *
19455 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a *. ** *
19456 20 44 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 Data stored on
19457 74 68 65 20 70 61 67 65 73 20 69 6d 6d 65 64 69 the pages immedi
19458 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 ately following
19459 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
1945a 20 20 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 pending-byte
1945b 20 70 61 67 65 20 69 6e 20 74 68 65 20 73 6f 75 page in the sou
1945c 72 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 rce database may
1945d 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 20 20 need to be.
1945e 20 20 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 65 ** copie
1945f 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69 d into the desti
19460 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e nation database.
19461 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 . */.
19462 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 53 const i64 iS
19463 69 7a 65 20 3d 20 28 69 36 34 29 6e 53 72 63 50 ize = (i64)nSrcP
19464 61 67 65 73 69 7a 65 20 2a 20 28 69 36 34 29 6e agesize * (i64)n
19465 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 20 SrcPage;.
19466 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20 sqlite3_file *
19467 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71 const pFile = sq
19468 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70 lite3PagerFile(p
19469 44 65 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 20 DestPager);..
1946a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 assert( pFi
1946b 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 le );. as
1946c 73 65 72 74 28 20 28 69 36 34 29 6e 44 65 73 74 sert( (i64)nDest
1946d 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29 6e 44 Truncate*(i64)nD
1946e 65 73 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 69 estPagesize >= i
1946f 53 69 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 20 Size || (.
19470 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 nDestTru
19471 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e ncate==(int)(PEN
19472 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 DING_BYTE_PAGE(p
19473 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29 ->pDest->pBt)-1)
19474 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 69 . && i
19475 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 Size>=PENDING_BY
19476 54 45 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 4e TE && iSize<=PEN
19477 44 49 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 50 DING_BYTE+nDestP
19478 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 agesize.
19479 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ));. if(
1947a 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d SQLITE_OK==(rc =
1947b 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d sqlite3PagerCom
1947c 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73 mitPhaseOne(pDes
1947d 74 50 61 67 65 72 2c 20 30 2c 20 31 29 29 0a 20 tPager, 0, 1)).
1947e 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 && SQLIT
1947f 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 6b E_OK==(rc = back
19480 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 70 upTruncateFile(p
19481 46 69 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 20 File, iSize)).
19482 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45 && SQLITE
19483 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 _OK==(rc = sqlit
19484 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65 73 e3PagerSync(pDes
19485 74 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 tPager)).
19486 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36 ){. i6
19487 34 20 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20 4 iOff;.
19488 20 20 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 4e i64 iEnd = MIN
19489 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20 (PENDING_BYTE +
1948a 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2c 20 69 nDestPagesize, i
1948b 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Size);.
1948c 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 20 20 for(.
1948d 20 20 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f 42 iOff=PENDING_B
1948e 59 54 45 2b 6e 53 72 63 50 61 67 65 73 69 7a 65 YTE+nSrcPagesize
1948f 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 ; . r
19490 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
19491 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 20 iOff<iEnd; .
19492 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e 53 iOff+=nS
19493 72 63 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20 rcPagesize.
19494 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
19495 20 20 20 20 50 67 48 64 72 20 2a 70 53 72 63 50 PgHdr *pSrcP
19496 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 g = 0;.
19497 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 const Pgno iS
19498 72 63 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 69 rcPg = (Pgno)((i
19499 4f 66 66 2f 6e 53 72 63 50 61 67 65 73 69 7a 65 Off/nSrcPagesize
1949a 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 )+1);.
1949b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 rc = sqlite3Pa
1949c 67 65 72 47 65 74 28 70 53 72 63 50 61 67 65 72 gerGet(pSrcPager
1949d 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 50 , iSrcPg, &pSrcP
1949e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 g);.
1949f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
194a0 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 K ){.
194a1 20 20 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 73 u8 *zData = s
194a2 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 qlite3PagerGetDa
194a3 74 61 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 ta(pSrcPg);.
194a4 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
194a5 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 46 qlite3OsWrite(pF
194a6 69 6c 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 63 ile, zData, nSrc
194a7 50 61 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 3b Pagesize, iOff);
194a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 . }.
194a9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
194aa 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 72 e3PagerUnref(pSr
194ab 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 cPg);.
194ac 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
194ad 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
194ae 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 rc = sqlite3Pag
194af 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 erCommitPhaseOne
194b0 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20 (pDestPager, 0,
194b1 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 0);. }. .
194b2 20 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 63 /* Finish c
194b3 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72 ommitting the tr
194b4 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 65 ansaction to the
194b5 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 destination dat
194b6 61 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20 abase. */.
194b7 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 if( SQLITE_OK==r
194b8 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 c. && SQLI
194b9 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c TE_OK==(rc = sql
194ba 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 ite3BtreeCommitP
194bb 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74 haseTwo(p->pDest
194bc 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 )). ){.
194bd 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
194be 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 DONE;. }.
194bf 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 }. . /* If
194c0 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 20 bCloseTrans is
194c1 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20 true, then this
194c2 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 function opened
194c3 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 a read transacti
194c4 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 on. ** on the
194c5 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 source database
194c6 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61 64 . Close the read
194c7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 72 transaction her
194c8 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20 20 e. There is.
194c9 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 68 ** no need to ch
194ca 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76 eck the return v
194cb 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74 72 alues of the btr
194cc 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65 2c ee methods here,
194cd 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d 6d as. ** "comm
194ce 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d 6f itting" a read-o
194cf 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 nly transaction
194d0 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20 cannot fail..
194d1 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c 6f */. if( bClo
194d2 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 seTrans ){.
194d3 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72 TESTONLY( int r
194d4 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53 54 c2 );. TEST
194d5 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73 ONLY( rc2 = ) s
194d6 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 qlite3BtreeCommi
194d7 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72 tPhaseOne(p->pSr
194d8 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45 53 c, 0);. TES
194d9 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20 TONLY( rc2 |= )
194da 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
194db 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53 itPhaseTwo(p->pS
194dc 72 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 rc);. asser
194dd 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f t( rc2==SQLITE_O
194de 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 K );. }. .
194df 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
194e0 7d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 }. if( p->pDest
194e1 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Db ){. sqlite
194e2 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3_mutex_leave(p-
194e3 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 >pDestDb->mutex)
194e4 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 ;. }. sqlite3B
194e5 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 treeLeave(p->pSr
194e6 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 c);. sqlite3_mu
194e7 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 72 tex_leave(p->pSr
194e8 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 cDb->mutex);. r
194e9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
194ea 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 72 ** Release all r
194eb 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 esources associa
194ec 74 65 64 20 77 69 74 68 20 61 6e 20 73 71 6c 69 ted with an sqli
194ed 74 65 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e 64 te3_backup* hand
194ee 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
194ef 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 I int sqlite3_ba
194f0 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69 ckup_finish(sqli
194f1 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a te3_backup *p){.
194f2 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 sqlite3_backup
194f3 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 **pp;
194f4 20 20 20 20 20 20 20 2f 2a 20 50 74 72 20 74 6f /* Ptr to
194f5 20 68 65 61 64 20 6f 66 20 70 61 67 65 72 73 20 head of pagers
194f6 62 61 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a 20 backup list */.
194f7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a sqlite3_mutex *
194f8 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 mutex;
194f9 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 /* Mutex t
194fa 6f 20 70 72 6f 74 65 63 74 20 73 6f 75 72 63 65 o protect source
194fb 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 database */. i
194fc 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 nt rc;
194fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
194fe 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 /* Value to
194ff 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 return */.. /*
19500 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 Enter the mutexe
19501 73 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20 s */. if( p==0
19502 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f ) return SQLITE_
19503 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 OK;. sqlite3_mu
19504 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 tex_enter(p->pSr
19505 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 cDb->mutex);. s
19506 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
19507 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 6d 75 74 (p->pSrc);. mut
19508 65 78 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e ex = p->pSrcDb->
19509 6d 75 74 65 78 3b 0a 20 20 69 66 28 20 70 2d 3e mutex;. if( p->
1950a 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 pDestDb ){. s
1950b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 qlite3_mutex_ent
1950c 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d er(p->pDestDb->m
1950d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a utex);. }.. /*
1950e 20 44 65 74 61 63 68 20 74 68 69 73 20 62 61 63 Detach this bac
1950f 6b 75 70 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 kup from the sou
19510 72 63 65 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 rce pager. */.
19511 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 if( p->pDestDb )
19512 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e {. p->pSrc->n
19513 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 20 20 Backup--;. }.
19514 69 66 28 20 70 2d 3e 69 73 41 74 74 61 63 68 65 if( p->isAttache
19515 64 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71 d ){. pp = sq
19516 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 lite3PagerBackup
19517 50 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65 Ptr(sqlite3Btree
19518 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b Pager(p->pSrc));
19519 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21 . while( *pp!
1951a 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d =p ){. pp =
1951b 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a &(*pp)->pNext;.
1951c 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 }. *pp =
1951d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 p->pNext;. }..
1951e 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 /* If a transac
1951f 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f 70 tion is still op
19520 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2c en on the Btree,
19521 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 2a roll it back. *
19522 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 /. sqlite3Btree
19523 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 73 Rollback(p->pDes
19524 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 t);.. /* Set th
19525 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 e error code of
19526 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 the destination
19527 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e database handle.
19528 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e 72 */. rc = (p->r
19529 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 c==SQLITE_DONE)
1952a 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 2d ? SQLITE_OK : p-
1952b 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 >rc;. sqlite3Er
1952c 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c 20 ror(p->pDestDb,
1952d 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 78 rc, 0);.. /* Ex
1952e 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 61 it the mutexes a
1952f 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63 6b nd free the back
19530 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75 63 up context struc
19531 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70 ture. */. if( p
19532 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 ->pDestDb ){.
19533 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
19534 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d eave(p->pDestDb-
19535 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73 >mutex);. }. s
19536 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 qlite3BtreeLeave
19537 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 (p->pSrc);. if(
19538 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 p->pDestDb ){.
19539 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 sqlite3_free(
1953a 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 p);. }. sqlite
1953b 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 3_mutex_leave(mu
1953c 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 tex);. return r
1953d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
1953e 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
1953f 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20 pages still to
19540 62 65 20 62 61 63 6b 65 64 20 75 70 20 61 73 20 be backed up as
19541 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 of the most rece
19542 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 nt.** call to sq
19543 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
19544 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 p()..*/.SQLITE_A
19545 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
19546 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 ackup_remaining(
19547 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a sqlite3_backup *
19548 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e p){. return p->
19549 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f nRemaining;.}../
1954a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1954b 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 total number of
1954c 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75 pages in the sou
1954d 72 63 65 20 64 61 74 61 62 61 73 65 20 61 73 20 rce database as
1954e 6f 66 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a 20 of the most .**
1954f 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 recent call to s
19550 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 qlite3_backup_st
19551 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ep()..*/.SQLITE_
19552 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
19553 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 backup_pagecount
19554 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 (sqlite3_backup
19555 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d *p){. return p-
19556 3e 6e 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a 0a >nPagecount;.}..
19557 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
19558 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ion is called af
19559 74 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 ter the contents
1955a 20 6f 66 20 70 61 67 65 20 69 50 61 67 65 20 6f of page iPage o
1955b 66 20 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 20 f the.** source
1955c 64 61 74 61 62 61 73 65 20 68 61 76 65 20 62 65 database have be
1955d 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 20 en modified. If
1955e 70 61 67 65 20 69 50 61 67 65 20 68 61 73 20 61 page iPage has a
1955f 6c 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20 lready been .**
19560 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 copied into the
19561 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 destination data
19562 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64 base, then the d
19563 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20 74 ata written to t
19564 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f he.** destinatio
19565 6e 20 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64 n is now invalid
19566 61 74 65 64 2e 20 54 68 65 20 64 65 73 74 69 6e ated. The destin
19567 61 74 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50 ation copy of iP
19568 61 67 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 age needs.** to
19569 62 65 20 75 70 64 61 74 65 64 20 77 69 74 68 20 be updated with
1956a 74 68 65 20 6e 65 77 20 64 61 74 61 20 62 65 66 the new data bef
1956b 6f 72 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f ore the backup o
1956c 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 peration is.** c
1956d 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 omplete..**.** I
1956e 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 t is assumed tha
1956f 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f t the mutex asso
19570 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
19571 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a BtShared object.
19572 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 ** corresponding
19573 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64 to the source d
19574 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20 atabase is held
19575 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 when this functi
19576 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e on is.** called.
19577 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19578 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 TE void sqlite3B
19579 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69 ackupUpdate(sqli
1957a 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 te3_backup *pBac
1957b 6b 75 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c kup, Pgno iPage,
1957c 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61 const u8 *aData
1957d 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 ){. sqlite3_bac
1957e 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 kup *p;
1957f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
19580 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
19581 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 /. for(p=pBacku
19582 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 p; p; p=p->pNext
19583 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
19584 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
19585 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e d(p->pSrc->pBt->
19586 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66 mutex) );. if
19587 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 ( !isFatalError(
19588 70 2d 3e 72 63 29 20 26 26 20 69 50 61 67 65 3c p->rc) && iPage<
19589 70 2d 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 p->iNext ){.
1958a 20 20 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 20 /* The backup
1958b 70 72 6f 63 65 73 73 20 70 20 68 61 73 20 61 6c process p has al
1958c 72 65 61 64 79 20 63 6f 70 69 65 64 20 70 61 67 ready copied pag
1958d 65 20 69 50 61 67 65 2e 20 42 75 74 20 6e 6f 77 e iPage. But now
1958e 20 69 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 it. ** has
1958f 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62 been modified b
19590 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 y a transaction
19591 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 on the source pa
19592 67 65 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 20 ger. Copy.
19593 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 ** the new data
19594 69 6e 74 6f 20 74 68 65 20 62 61 63 6b 75 70 2e into the backup.
19595 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
19596 69 6e 74 20 72 63 20 3d 20 62 61 63 6b 75 70 4f int rc = backupO
19597 6e 65 50 61 67 65 28 70 2c 20 69 50 61 67 65 2c nePage(p, iPage,
19598 20 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 61 aData);. a
19599 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 ssert( rc!=SQLIT
1959a 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53 51 E_BUSY && rc!=SQ
1959b 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20 LITE_LOCKED );.
1959c 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1959d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1959e 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
1959f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
195a0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74 }../*.** Restart
195a1 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 the backup proc
195a2 65 73 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c ess. This is cal
195a3 6c 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67 led when the pag
195a4 65 72 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 65 er layer.** dete
195a5 63 74 73 20 74 68 61 74 20 74 68 65 20 64 61 74 cts that the dat
195a6 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d abase has been m
195a7 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 65 78 odified by an ex
195a8 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0a ternal database.
195a9 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 ** connection. I
195aa 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 n this case ther
195ab 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b e is no way of k
195ac 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20 nowing which of
195ad 74 68 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 the.** pages tha
195ae 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69 t have been copi
195af 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 ed into the dest
195b0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 ination database
195b1 20 61 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 76 are still .** v
195b2 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68 20 61 alid and which a
195b3 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 65 re not, so the e
195b4 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20 6e 65 ntire process ne
195b5 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 eds to be restar
195b6 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 ted..**.** It is
195b7 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 assumed that th
195b8 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 e mutex associat
195b9 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68 ed with the BtSh
195ba 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 ared object.** c
195bb 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 orresponding to
195bc 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 the source datab
195bd 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e ase is held when
195be 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
195bf 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a s.** called..*/.
195c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
195c1 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 oid sqlite3Backu
195c2 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33 pRestart(sqlite3
195c3 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 _backup *pBackup
195c4 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 ){. sqlite3_bac
195c5 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20 kup *p;
195c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 /* Ite
195c7 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a rator variable *
195c8 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75 /. for(p=pBacku
195c9 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 p; p; p=p->pNext
195ca 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 ){. assert( s
195cb 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
195cc 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e d(p->pSrc->pBt->
195cd 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 2d mutex) );. p-
195ce 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a >iNext = 1;. }.
195cf 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
195d0 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a E_OMIT_VACUUM./*
195d1 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f 6d .** Copy the com
195d2 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 plete content of
195d3 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42 pBtFrom into pB
195d4 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 74 tTo. A transact
195d5 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 ion.** must be a
195d6 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20 66 ctive for both f
195d7 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 iles..**.** The
195d8 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 6f size of file pTo
195d9 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 20 may be reduced
195da 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f by this operatio
195db 6e 2e 20 49 66 20 61 6e 79 74 68 69 6e 67 20 0a n. If anything .
195dc 2a 2a 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74 ** goes wrong, t
195dd 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f he transaction o
195de 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20 n pTo is rolled
195df 62 61 63 6b 2e 20 49 66 20 73 75 63 63 65 73 73 back. If success
195e0 66 75 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 61 ful, the .** tra
195e1 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d nsaction is comm
195e2 69 74 74 65 64 20 62 65 66 6f 72 65 20 72 65 74 itted before ret
195e3 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 urning..*/.SQLIT
195e4 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
195e5 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 lite3BtreeCopyFi
195e6 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 le(Btree *pTo, B
195e7 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 tree *pFrom){.
195e8 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 int rc;. sqlite
195e9 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71 3_backup b;. sq
195ea 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 lite3BtreeEnter(
195eb 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 pTo);. sqlite3B
195ec 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29 treeEnter(pFrom)
195ed 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 61 ;.. /* Set up a
195ee 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 n sqlite3_backup
195ef 20 6f 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 33 object. sqlite3
195f0 5f 62 61 63 6b 75 70 2e 70 44 65 73 74 44 62 20 _backup.pDestDb
195f1 6d 75 73 74 20 62 65 20 73 65 74 0a 20 20 2a 2a must be set. **
195f2 20 74 6f 20 30 2e 20 54 68 69 73 20 69 73 20 75 to 0. This is u
195f3 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 sed by the imple
195f4 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71 mentations of sq
195f5 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 lite3_backup_ste
195f6 70 28 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c p(). ** and sql
195f7 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 ite3_backup_fini
195f8 73 68 28 29 20 74 6f 20 64 65 74 65 63 74 20 74 sh() to detect t
195f9 68 61 74 20 74 68 65 79 20 61 72 65 20 62 65 69 hat they are bei
195fa 6e 67 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 66 ng called. ** f
195fb 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f rom this functio
195fc 6e 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20 n, not directly
195fd 62 79 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a by the user.. *
195fe 2f 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 30 /. memset(&b, 0
195ff 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 20 , sizeof(b));.
19600 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72 6f 6d b.pSrcDb = pFrom
19601 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 3d ->db;. b.pSrc =
19602 20 70 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 73 pFrom;. b.pDes
19603 74 20 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e 65 t = pTo;. b.iNe
19604 78 74 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 78 xt = 1;.. /* 0x
19605 37 46 46 46 46 46 46 46 20 69 73 20 74 68 65 20 7FFFFFFF is the
19606 68 61 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 74 hard limit for t
19607 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
19608 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 es in a database
19609 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 70 . ** file. By p
1960a 61 73 73 69 6e 67 20 74 68 69 73 20 61 73 20 74 assing this as t
1960b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 he number of pag
1960c 65 73 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 20 es to copy to.
1960d 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 ** sqlite3_backu
1960e 70 5f 73 74 65 70 28 29 2c 20 77 65 20 63 61 6e p_step(), we can
1960f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
19610 74 68 65 20 63 6f 70 79 20 66 69 6e 69 73 68 65 the copy finishe
19611 73 20 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 61 s . ** within a
19612 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 6e single call (un
19613 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 less an error oc
19614 63 75 72 73 29 2e 20 54 68 65 20 61 73 73 65 72 curs). The asser
19615 74 28 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 t() statement.
19616 2a 2a 20 63 68 65 63 6b 73 20 74 68 69 73 20 61 ** checks this a
19617 73 73 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d 3e ssumption - (p->
19618 72 63 29 20 73 68 6f 75 6c 64 20 62 65 20 73 65 rc) should be se
19619 74 20 74 6f 20 65 69 74 68 65 72 20 53 51 4c 49 t to either SQLI
1961a 54 45 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f 72 TE_DONE . ** or
1961b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a an error code..
1961c 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 */. sqlite3_b
1961d 61 63 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30 ackup_step(&b, 0
1961e 78 37 46 46 46 46 46 46 46 29 3b 0a 20 20 61 73 x7FFFFFFF);. as
1961f 73 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49 sert( b.rc!=SQLI
19620 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 TE_OK );. rc =
19621 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 sqlite3_backup_f
19622 69 6e 69 73 68 28 26 62 29 3b 0a 20 20 69 66 28 inish(&b);. if(
19623 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
19624 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d 3e {. pTo->pBt->
19625 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 pageSizeFixed =
19626 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 0;. }.. sqlite
19627 33 42 74 72 65 65 4c 65 61 76 65 28 70 46 72 6f 3BtreeLeave(pFro
19628 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 m);. sqlite3Btr
19629 65 65 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 20 eeLeave(pTo);.
1962a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
1962b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1962c 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a IT_VACUUM */../*
1962d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e ************* En
1962e 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a 2a d of backup.c **
1962f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19631 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
19632 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 ************* Be
19633 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 6d gin file vdbemem
19634 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
19635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a ************/./*
19637 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a .** 2004 May 26.
19638 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 **.** The author
19639 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 disclaims copyr
1963a 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 ight to this sou
1963b 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c rce code. In pl
1963c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 ace of.** a lega
1963d 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 l notice, here i
1963e 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a s a blessing:.**
1963f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 .** May you d
19640 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 o good and not e
19641 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 vil..** May y
19642 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e ou find forgiven
19643 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 ess for yourself
19644 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 and forgive oth
19645 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 ers..** May y
19646 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c ou share freely,
19647 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f never taking mo
19648 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 re than you give
19649 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ..**.***********
1964a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1964b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1964c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1964d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a **************.*
1964e 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
1964f 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 ontains code use
19650 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 22 to manipulate "
19651 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e 20 Mem" structure.
19652 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72 A "Mem".** stor
19653 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75 es a single valu
19654 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 e in the VDBE.
19655 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 65 Mem is an opaque
19656 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69 62 structure visib
19657 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69 le.** only withi
19658 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e 74 n the VDBE. Int
19659 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 erface routines
1965a 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 75 refer to a Mem u
1965b 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65 sing the.** name
1965c 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2f sqlite_value.*/
1965d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c ../*.** Call sql
1965e 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e ite3VdbeMemExpan
1965f 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 dBlob() on the s
19660 75 70 70 6c 69 65 64 20 76 61 6c 75 65 20 28 74 upplied value (t
19661 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 ype Mem*).** P i
19662 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 f required..*/.#
19663 64 65 66 69 6e 65 20 65 78 70 61 6e 64 42 6c 6f define expandBlo
19664 62 28 50 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 b(P) (((P)->flag
19665 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 s&MEM_Zero)?sqli
19666 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 te3VdbeMemExpand
19667 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a Blob(P):0)../*.*
19668 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 * If pMem is an
19669 6f 62 6a 65 63 74 20 77 69 74 68 20 61 20 76 61 object with a va
1966a 6c 69 64 20 73 74 72 69 6e 67 20 72 65 70 72 65 lid string repre
1966b 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68 69 73 20 sentation, this
1966c 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 routine.** ensur
1966d 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 es the internal
1966e 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 encoding for the
1966f 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e string represen
19670 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 tation is.** 'de
19671 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e 65 20 6f siredEnc', one o
19672 66 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 f SQLITE_UTF8, S
19673 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 QLITE_UTF16LE or
19674 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2e SQLITE_UTF16BE.
19675 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 .**.** If pMem i
19676 73 20 6e 6f 74 20 61 20 73 74 72 69 6e 67 20 6f s not a string o
19677 62 6a 65 63 74 2c 20 6f 72 20 74 68 65 20 65 6e bject, or the en
19678 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 73 74 coding of the st
19679 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65 73 65 6e ring.** represen
1967a 74 61 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64 tation is alread
1967b 79 20 73 74 6f 72 65 64 20 75 73 69 6e 67 20 74 y stored using t
1967c 68 65 20 72 65 71 75 65 73 74 65 64 20 65 6e 63 he requested enc
1967d 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 69 73 oding, then this
1967e 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 61 .** routine is a
1967f 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 no-op..**.** SQ
19680 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 LITE_OK is retur
19681 6e 65 64 20 69 66 20 74 68 65 20 63 6f 6e 76 65 ned if the conve
19682 72 73 69 6f 6e 20 69 73 20 73 75 63 63 65 73 73 rsion is success
19683 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72 65 71 75 ful (or not requ
19684 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 ired)..** SQLITE
19685 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65 20 72 65 _NOMEM may be re
19686 74 75 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c turned if a mall
19687 6f 63 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e oc() fails durin
19688 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 g conversion.**
19689 62 65 74 77 65 65 6e 20 66 6f 72 6d 61 74 73 2e between formats.
1968a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1968b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1968c 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 beChangeEncoding
1968d 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 (Mem *pMem, int
1968e 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69 desiredEnc){. i
1968f 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 nt rc;. assert(
19690 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 (pMem->flags&ME
19691 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a M_RowSet)==0 );.
19692 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65 assert( desire
19693 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 dEnc==SQLITE_UTF
19694 38 20 7c 7c 20 64 65 73 69 72 65 64 45 6e 63 3d 8 || desiredEnc=
19695 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a =SQLITE_UTF16LE.
19696 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 || de
19697 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 siredEnc==SQLITE
19698 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 _UTF16BE );. if
19699 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 ( !(pMem->flags&
1969a 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70 4d 65 6d MEM_Str) || pMem
1969b 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 6e ->enc==desiredEn
1969c 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 c ){. return
1969d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 SQLITE_OK;. }.
1969e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
1969f 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
196a0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
196a1 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 >db->mutex) );.#
196a2 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 ifdef SQLITE_OMI
196a3 54 5f 55 54 46 31 36 0a 20 20 72 65 74 75 72 6e T_UTF16. return
196a4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 23 SQLITE_ERROR;.#
196a5 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65 6d 54 72 else.. /* MemTr
196a6 61 6e 73 6c 61 74 65 28 29 20 6d 61 79 20 72 65 anslate() may re
196a7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
196a8 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 r SQLITE_NOMEM.
196a9 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 If NOMEM is retu
196aa 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 rned,. ** then
196ab 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 the encoding of
196ac 74 68 65 20 76 61 6c 75 65 20 6d 61 79 20 6e 6f the value may no
196ad 74 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a t have changed..
196ae 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 */. rc = sqli
196af 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c te3VdbeMemTransl
196b0 61 74 65 28 70 4d 65 6d 2c 20 28 75 38 29 64 65 ate(pMem, (u8)de
196b1 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73 siredEnc);. ass
196b2 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ert(rc==SQLITE_O
196b3 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 K || rc==SQLI
196b4 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 61 73 73 TE_NOMEM);. ass
196b5 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f ert(rc==SQLITE_O
196b6 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e K || pMem->en
196b7 63 21 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a c!=desiredEnc);.
196b8 20 20 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c assert(rc==SQL
196b9 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 70 4d 65 ITE_NOMEM || pMe
196ba 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65 64 45 m->enc==desiredE
196bb 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 nc);. return rc
196bc 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a ;.#endif.}../*.*
196bd 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 4d 65 6d * Make sure pMem
196be 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 ->z points to a
196bf 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 writable allocat
196c0 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 ion of at least
196c1 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a .** n bytes..**.
196c2 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72 79 ** If the memory
196c3 20 63 65 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 cell currently
196c4 63 6f 6e 74 61 69 6e 73 20 73 74 72 69 6e 67 20 contains string
196c5 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a 2a 2a 20 or blob data.**
196c6 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72 and the third ar
196c7 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f gument passed to
196c8 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 this function i
196c9 73 20 74 72 75 65 2c 20 74 68 65 20 0a 2a 2a 20 s true, the .**
196ca 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 20 current content
196cb 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 70 of the cell is p
196cc 72 65 73 65 72 76 65 64 2e 20 4f 74 68 65 72 77 reserved. Otherw
196cd 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a 2a 20 62 ise, it may.** b
196ce 65 20 64 69 73 63 61 72 64 65 64 2e 20 20 0a 2a e discarded. .*
196cf 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
196d0 6f 6e 20 73 65 74 73 20 74 68 65 20 4d 45 4d 5f on sets the MEM_
196d1 44 79 6e 20 66 6c 61 67 20 61 6e 64 20 63 6c 65 Dyn flag and cle
196d2 61 72 73 20 61 6e 79 20 78 44 65 6c 20 63 61 6c ars any xDel cal
196d3 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20 61 6c 73 lback..** It als
196d4 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f 45 70 68 o clears MEM_Eph
196d5 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74 61 74 69 em and MEM_Stati
196d6 63 2e 20 49 66 20 74 68 65 20 70 72 65 73 65 72 c. If the preser
196d7 76 65 20 66 6c 61 67 20 69 73 20 0a 2a 2a 20 6e ve flag is .** n
196d8 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e 20 69 73 ot set, Mem.n is
196d9 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 zeroed..*/.SQLI
196da 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
196db 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f qlite3VdbeMemGro
196dc 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 w(Mem *pMem, int
196dd 20 6e 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 n, int preserve
196de 29 7b 0a 20 20 61 73 73 65 72 74 28 20 31 20 3e ){. assert( 1 >
196df 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 7a 4d =. ((pMem->zM
196e0 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a alloc && pMem->z
196e1 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 Malloc==pMem->z)
196e2 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20 20 20 20 ? 1 : 0) +.
196e3 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d (((pMem->flags&M
196e4 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d 2d 3e 78 EM_Dyn)&&pMem->x
196e5 44 65 6c 29 20 3f 20 31 20 3a 20 30 29 20 2b 20 Del) ? 1 : 0) +
196e6 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 . ((pMem->fla
196e7 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 20 3f 20 gs&MEM_Ephem) ?
196e8 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20 28 28 1 : 0) + . ((
196e9 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f pMem->flags&MEM_
196ea 53 74 61 74 69 63 29 20 3f 20 31 20 3a 20 30 29 Static) ? 1 : 0)
196eb 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 . );. assert(
196ec 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d (pMem->flags&MEM
196ed 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 0a _RowSet)==0 );..
196ee 20 20 69 66 28 20 6e 3c 33 32 20 29 20 6e 20 3d if( n<32 ) n =
196ef 20 33 32 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 32;. if( sqlit
196f0 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 e3DbMallocSize(p
196f1 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a Mem->db, pMem->z
196f2 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a 20 20 20 Malloc)<n ){.
196f3 20 69 66 28 20 70 72 65 73 65 72 76 65 20 26 26 if( preserve &&
196f4 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e pMem->z==pMem->
196f5 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 20 zMalloc ){.
196f6 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d pMem->z = pMem-
196f7 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 >zMalloc = sqlit
196f8 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 e3DbReallocOrFre
196f9 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d e(pMem->db, pMem
196fa 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 70 ->z, n);. p
196fb 72 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20 reserve = 0;.
196fc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
196fd 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d lite3DbFree(pMem
196fe 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c ->db, pMem->zMal
196ff 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d loc);. pMem
19700 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 ->zMalloc = sqli
19701 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 te3DbMallocRaw(p
19702 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a 20 20 20 Mem->db, n);.
19703 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d }. }.. if( pM
19704 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73 65 72 76 em->z && preserv
19705 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c e && pMem->zMall
19706 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 oc && pMem->z!=p
19707 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a Mem->zMalloc ){.
19708 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d memcpy(pMem-
19709 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65 6d 2d 3e >zMalloc, pMem->
1970a 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 7d z, pMem->n);. }
1970b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 . if( pMem->fla
1970c 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 4d gs&MEM_Dyn && pM
1970d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 em->xDel ){.
1970e 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76 6f 69 64 pMem->xDel((void
1970f 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 *)(pMem->z));.
19710 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 }.. pMem->z =
19711 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 pMem->zMalloc;.
19712 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 if( pMem->z==0
19713 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
19714 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
19715 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d }else{. pMem
19716 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
19717 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 _Ephem|MEM_Stati
19718 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e c);. }. pMem->
19719 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 xDel = 0;. retu
1971a 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f 20 53 51 rn (pMem->z ? SQ
1971b 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 LITE_OK : SQLITE
1971c 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a _NOMEM);.}../*.*
1971d 2a 20 4d 61 6b 65 20 74 68 65 20 67 69 76 65 6e * Make the given
1971e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d 45 4d 5f Mem object MEM_
1971f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 Dyn. In other w
19720 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74 20 73 6f ords, make it so
19721 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20 54 45 58 .** that any TEX
19722 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e 74 65 6e T or BLOB conten
19723 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 6d t is stored in m
19724 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 emory obtained f
19725 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e rom.** malloc().
19726 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 In this way, w
19727 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 e know that the
19728 6d 65 6d 6f 72 79 20 69 73 20 73 61 66 65 20 74 memory is safe t
19729 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 o be.** overwrit
1972a 74 65 6e 20 6f 72 20 61 6c 74 65 72 65 64 2e 0a ten or altered..
1972b 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c **.** Return SQL
1972c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 ITE_OK on succes
1972d 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 s or SQLITE_NOME
1972e 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c M if malloc fail
1972f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 s..*/.SQLITE_PRI
19730 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
19731 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 VdbeMemMakeWrite
19732 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b able(Mem *pMem){
19733 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61 73 73 65 . int f;. asse
19734 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
19735 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
19736 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
19737 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19738 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
19739 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 &MEM_RowSet)==0
1973a 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62 28 );. expandBlob(
1973b 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d 65 pMem);. f = pMe
1973c 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 m->flags;. if(
1973d 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f (f&(MEM_Str|MEM_
1973e 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d 3e Blob)) && pMem->
1973f 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 z!=pMem->zMalloc
19740 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 ){. if( sqli
19741 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19742 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20 32 Mem, pMem->n + 2
19743 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 , 1) ){. re
19744 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
19745 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 M;. }. pMe
19746 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 m->z[pMem->n] =
19747 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 0;. pMem->z[p
19748 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a 20 Mem->n+1] = 0;.
19749 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
1974a 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a = MEM_Term;. }.
1974b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1974c 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 _OK;.}../*.** If
1974d 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a 20 the given Mem*
1974e 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 has a zero-fille
1974f 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74 20 d tail, turn it
19750 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72 79 into an ordinary
19751 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64 20 .** blob stored
19752 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 in dynamically a
19753 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 0a llocated space..
19754 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 */.#ifndef SQLIT
19755 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a E_OMIT_INCRBLOB.
19756 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19757 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 nt sqlite3VdbeMe
19758 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 mExpandBlob(Mem
19759 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d *pMem){. if( pM
1975a 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f em->flags & MEM_
1975b 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 Zero ){. int
1975c 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65 72 nByte;. asser
1975d 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d t( pMem->flags&M
1975e 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 61 EM_Blob );. a
1975f 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
19760 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d ags&MEM_RowSet)=
19761 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =0 );. assert
19762 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c ( pMem->db==0 ||
19763 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
19764 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 eld(pMem->db->mu
19765 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 tex) );.. /*
19766 53 65 74 20 6e 42 79 74 65 20 74 6f 20 74 68 65 Set nByte to the
19767 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
19768 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
19769 72 65 20 74 68 65 20 65 78 70 61 6e 64 65 64 20 re the expanded
1976a 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20 6e 42 79 blob. */. nBy
1976b 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2b 20 70 te = pMem->n + p
1976c 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 Mem->u.nZero;.
1976d 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30 20 29 if( nByte<=0 )
1976e 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d 20 {. nByte =
1976f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 1;. }. if(
19770 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 sqlite3VdbeMemG
19771 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c row(pMem, nByte,
19772 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 1) ){. ret
19773 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
19774 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65 6d ;. }.. mem
19775 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 set(&pMem->z[pMe
19776 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d 2d 3e m->n], 0, pMem->
19777 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 70 4d u.nZero);. pM
19778 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 em->n += pMem->u
19779 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70 4d 65 6d .nZero;. pMem
1977a 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d ->flags &= ~(MEM
1977b 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72 6d 29 3b _Zero|MEM_Term);
1977c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 . }. return SQ
1977d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 LITE_OK;.}.#endi
1977e 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 f.../*.** Make s
1977f 75 72 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 ure the given Me
19780 6d 20 69 73 20 5c 75 30 30 30 30 20 74 65 72 6d m is \u0000 term
19781 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 inated..*/.SQLIT
19782 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19783 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 lite3VdbeMemNulT
19784 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20 2a 70 4d erminate(Mem *pM
19785 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 em){. assert( p
19786 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
19787 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19788 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
19789 29 20 29 3b 0a 20 20 69 66 28 20 28 70 4d 65 6d ) );. if( (pMem
1978a 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 54 65 ->flags & MEM_Te
1978b 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d 65 6d 2d rm)!=0 || (pMem-
1978c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 >flags & MEM_Str
1978d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 )==0 ){. retu
1978e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 rn SQLITE_OK;
1978f 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f 20 64 6f /* Nothing to do
19790 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 */. }. if( sq
19791 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
19792 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 (pMem, pMem->n+2
19793 2c 20 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 , 1) ){. retu
19794 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
19795 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 7a 5b 70 . }. pMem->z[p
19796 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 70 Mem->n] = 0;. p
19797 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 Mem->z[pMem->n+1
19798 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 66 ] = 0;. pMem->f
19799 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d lags |= MEM_Term
1979a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 ;. return SQLIT
1979b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 E_OK;.}../*.** A
1979c 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f 20 74 68 dd MEM_Str to th
1979d 65 20 73 65 74 20 6f 66 20 72 65 70 72 65 73 65 e set of represe
1979e 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 ntations for the
1979f 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20 4e 75 6d given Mem. Num
197a0 62 65 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 bers.** are conv
197a1 65 72 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 erted using sqli
197a2 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 2e 20 te3_snprintf().
197a3 20 43 6f 6e 76 65 72 74 69 6e 67 20 61 20 42 4c Converting a BL
197a4 4f 42 20 74 6f 20 61 20 73 74 72 69 6e 67 0a 2a OB to a string.*
197a5 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a * is a no-op..**
197a6 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 72 65 70 .** Existing rep
197a7 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 4d 45 4d resentations MEM
197a8 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52 65 61 _Int and MEM_Rea
197a9 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69 6e 76 61 l are *not* inva
197aa 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 lidated..**.** A
197ab 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c 75 65 20 MEM_Null value
197ac 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 61 will never be pa
197ad 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e ssed to this fun
197ae 63 74 69 6f 6e 2e 20 54 68 69 73 20 66 75 6e 63 ction. This func
197af 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73 65 64 20 tion is.** used
197b0 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 76 for converting v
197b1 61 6c 75 65 73 20 74 6f 20 74 65 78 74 20 66 6f alues to text fo
197b2 72 20 72 65 74 75 72 6e 69 6e 67 20 74 6f 20 74 r returning to t
197b3 68 65 20 75 73 65 72 20 28 69 2e 65 2e 20 76 69 he user (i.e. vi
197b4 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c a.** sqlite3_val
197b5 75 65 5f 74 65 78 74 28 29 29 2c 20 6f 72 20 66 ue_text()), or f
197b6 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 or ensuring that
197b7 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 75 73 values to be us
197b8 65 64 20 61 73 20 62 74 72 65 65 0a 2a 2a 20 6b ed as btree.** k
197b9 65 79 73 20 61 72 65 20 73 74 72 69 6e 67 73 2e eys are strings.
197ba 20 49 6e 20 74 68 65 20 66 6f 72 6d 65 72 20 63 In the former c
197bb 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 ase a NULL point
197bc 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 er is returned t
197bd 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e 64 20 74 he.** user and t
197be 68 65 20 6c 61 74 65 72 20 69 73 20 61 6e 20 69 he later is an i
197bf 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72 61 6d 6d nternal programm
197c0 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 ing error..*/.SQ
197c1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
197c2 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
197c3 74 72 69 6e 67 69 66 79 28 4d 65 6d 20 2a 70 4d tringify(Mem *pM
197c4 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b 0a 20 20 em, int enc){.
197c5 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
197c6 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20 3d 20 70 OK;. int fg = p
197c7 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f Mem->flags;. co
197c8 6e 73 74 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 nst int nByte =
197c9 33 32 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 32;.. assert( p
197ca 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
197cb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
197cc 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
197cd 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 ) );. assert( !
197ce 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29 20 29 3b (fg&MEM_Zero) );
197cf 0a 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 . assert( !(fg&
197d0 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
197d1 62 29 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 b)) );. assert(
197d2 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d fg&(MEM_Int|MEM
197d3 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61 73 73 65 _Real) );. asse
197d4 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
197d5 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 &MEM_RowSet)==0
197d6 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 );. assert( EIG
197d7 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e HT_BYTE_ALIGNMEN
197d8 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a 20 20 69 T(pMem) );... i
197d9 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 f( sqlite3VdbeMe
197da 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 mGrow(pMem, nByt
197db 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 e, 0) ){. ret
197dc 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
197dd 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 ;. }.. /* For
197de 61 20 52 65 61 6c 20 6f 72 20 49 6e 74 65 67 65 a Real or Intege
197df 72 2c 20 75 73 65 20 73 71 6c 69 74 65 33 5f 6d r, use sqlite3_m
197e0 70 72 69 6e 74 66 28 29 20 74 6f 20 70 72 6f 64 printf() to prod
197e1 75 63 65 20 74 68 65 20 55 54 46 2d 38 0a 20 20 uce the UTF-8.
197e2 2a 2a 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 ** string repres
197e3 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
197e4 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20 69 66 20 value. Then, if
197e5 74 68 65 20 72 65 71 75 69 72 65 64 20 65 6e 63 the required enc
197e6 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73 20 55 54 oding. ** is UT
197e7 46 2d 31 36 6c 65 20 6f 72 20 55 54 46 2d 31 36 F-16le or UTF-16
197e8 62 65 20 64 6f 20 61 20 74 72 61 6e 73 6c 61 74 be do a translat
197e9 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 ion.. ** . **
197ea 46 49 58 20 4d 45 3a 20 49 74 20 77 6f 75 6c 64 FIX ME: It would
197eb 20 62 65 20 62 65 74 74 65 72 20 69 66 20 73 71 be better if sq
197ec 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 lite3_snprintf()
197ed 20 63 6f 75 6c 64 20 64 6f 20 55 54 46 2d 31 36 could do UTF-16
197ee 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 66 67 20 .. */. if( fg
197ef 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
197f0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 sqlite3_snprint
197f1 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a f(nByte, pMem->z
197f2 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e , "%lld", pMem->
197f3 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 u.i);. }else{.
197f4 20 20 20 61 73 73 65 72 74 28 20 66 67 20 26 20 assert( fg &
197f5 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 MEM_Real );.
197f6 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
197f7 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c (nByte, pMem->z,
197f8 20 22 25 21 2e 31 35 67 22 2c 20 70 4d 65 6d 2d "%!.15g", pMem-
197f9 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d >r);. }. pMem-
197fa 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c >n = sqlite3Strl
197fb 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 en30(pMem->z);.
197fc 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c pMem->enc = SQL
197fd 49 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d ITE_UTF8;. pMem
197fe 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 ->flags |= MEM_S
197ff 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 tr|MEM_Term;. s
19800 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
19801 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 Encoding(pMem, e
19802 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 nc);. return rc
19803 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 ;.}../*.** Memor
19804 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 y cell pMem cont
19805 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 ains the context
19806 20 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 of an aggregate
19807 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 function..** Th
19808 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 is routine calls
19809 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 the finalize me
1980a 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 thod for that fu
1980b 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 nction. The.**
1980c 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 result of the ag
1980d 67 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 gregate is store
1980e 64 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d d back into pMem
1980f 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 ..**.** Return S
19810 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 QLITE_ERROR if t
19811 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 he finalizer rep
19812 6f 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 orts an error.
19813 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 SQLITE_OK.** oth
19814 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 erwise..*/.SQLIT
19815 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19816 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 lite3VdbeMemFina
19817 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 lize(Mem *pMem,
19818 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b FuncDef *pFunc){
19819 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 . int rc = SQLI
1981a 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 41 4c 57 TE_OK;. if( ALW
1981b 41 59 53 28 70 46 75 6e 63 20 26 26 20 70 46 75 AYS(pFunc && pFu
1981c 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 29 20 29 nc->xFinalize) )
1981d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f {. sqlite3_co
1981e 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 61 ntext ctx;. a
1981f 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c ssert( (pMem->fl
19820 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 ags & MEM_Null)!
19821 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d 70 4d 65 =0 || pFunc==pMe
19822 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a 20 20 20 m->u.pDef );.
19823 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 assert( pMem->d
19824 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f b==0 || sqlite3_
19825 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d mutex_held(pMem-
19826 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 >db->mutex) );.
19827 20 20 20 6d 65 6d 73 65 74 28 26 63 74 78 2c 20 memset(&ctx,
19828 30 2c 20 73 69 7a 65 6f 66 28 63 74 78 29 29 3b 0, sizeof(ctx));
19829 0a 20 20 20 20 63 74 78 2e 73 2e 66 6c 61 67 73 . ctx.s.flags
1982a 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1982b 20 63 74 78 2e 73 2e 64 62 20 3d 20 70 4d 65 6d ctx.s.db = pMem
1982c 2d 3e 64 62 3b 0a 20 20 20 20 63 74 78 2e 70 4d ->db;. ctx.pM
1982d 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20 20 20 63 em = pMem;. c
1982e 74 78 2e 70 46 75 6e 63 20 3d 20 70 46 75 6e 63 tx.pFunc = pFunc
1982f 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e 78 46 69 ;. pFunc->xFi
19830 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b 0a 20 20 nalize(&ctx);.
19831 20 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 4d assert( 0==(pM
19832 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 em->flags&MEM_Dy
19833 6e 29 20 26 26 20 21 70 4d 65 6d 2d 3e 78 44 65 n) && !pMem->xDe
19834 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 l );. sqlite3
19835 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c DbFree(pMem->db,
19836 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b pMem->zMalloc);
19837 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d . memcpy(pMem
19838 2c 20 26 63 74 78 2e 73 2c 20 73 69 7a 65 6f 66 , &ctx.s, sizeof
19839 28 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 (ctx.s));. rc
1983a 20 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a = ctx.isError;.
1983b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b }. return rc;
1983c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 .}../*.** If the
1983d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 63 6f 6e memory cell con
1983e 74 61 69 6e 73 20 61 20 73 74 72 69 6e 67 20 76 tains a string v
1983f 61 6c 75 65 20 74 68 61 74 20 6d 75 73 74 20 62 alue that must b
19840 65 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 69 6e e freed by.** in
19841 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74 65 72 6e voking an extern
19842 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20 66 72 65 al callback, fre
19843 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c 6c 69 6e e it now. Callin
19844 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a g this function.
19845 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66 72 65 65 ** does not free
19846 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 any Mem.zMalloc
19847 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53 51 4c 49 buffer..*/.SQLI
19848 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19849 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1984a 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 4d 65 leaseExternal(Me
1984b 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 m *p){. assert(
1984c 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c p->db==0 || sql
1984d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1984e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b p->db->mutex) );
1984f 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e . testcase( p->
19850 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 20 flags & MEM_Agg
19851 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 );. testcase( p
19852 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 ->flags & MEM_Dy
19853 6e 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 n );. testcase(
19854 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f p->flags & MEM_
19855 52 6f 77 53 65 74 20 29 3b 0a 20 20 74 65 73 74 RowSet );. test
19856 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 case( p->flags &
19857 20 4d 45 4d 5f 46 72 61 6d 65 20 29 3b 0a 20 20 MEM_Frame );.
19858 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 if( p->flags&(ME
19859 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 M_Agg|MEM_Dyn|ME
1985a 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f 46 72 61 M_RowSet|MEM_Fra
1985b 6d 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 me) ){. if( p
1985c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67 67 20 ->flags&MEM_Agg
1985d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1985e 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 VdbeMemFinalize(
1985f 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20 p, p->u.pDef);.
19860 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d assert( (p-
19861 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 >flags & MEM_Agg
19862 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 )==0 );. sq
19863 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19864 61 73 65 28 70 29 3b 0a 20 20 20 20 7d 65 6c 73 ase(p);. }els
19865 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d e if( p->flags&M
19866 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e 78 44 65 EM_Dyn && p->xDe
19867 6c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 l ){. asser
19868 74 28 20 28 70 2d 3e 66 6c 61 67 73 26 4d 45 4d t( (p->flags&MEM
19869 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
1986a 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f p->xDel((vo
1986b 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 id *)p->z);.
1986c 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 p->xDel = 0;.
1986d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e }else if( p->
1986e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 flags&MEM_RowSet
1986f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
19870 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 2d 3e 3RowSetClear(p->
19871 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 u.pRowSet);.
19872 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 }else if( p->fla
19873 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a gs&MEM_Frame ){.
19874 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
19875 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 29 3b 0a eMemSetNull(p);.
19876 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a }. }.}../*.
19877 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 79 20 6d ** Release any m
19878 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68 emory held by th
19879 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d 61 79 20 e Mem. This may
1987a 6c 65 61 76 65 20 74 68 65 20 4d 65 6d 20 69 6e leave the Mem in
1987b 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 an.** inconsist
1987c 65 6e 74 20 73 74 61 74 65 2c 20 66 6f 72 20 65 ent state, for e
1987d 78 61 6d 70 6c 65 20 77 69 74 68 20 28 4d 65 6d xample with (Mem
1987e 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a 20 28 4d .z==0) and.** (M
1987f 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f em.type==SQLITE_
19880 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TEXT)..*/.SQLITE
19881 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19882 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19883 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 73 ase(Mem *p){. s
19884 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19885 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 29 3b easeExternal(p);
19886 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
19887 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c (p->db, p->zMall
19888 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d 20 30 3b oc);. p->z = 0;
19889 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 . p->zMalloc =
1988a 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30 0;. p->xDel = 0
1988b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 ;.}../*.** Conve
1988c 72 74 20 61 20 36 34 2d 62 69 74 20 49 45 45 45 rt a 64-bit IEEE
1988d 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20 61 20 36 double into a 6
1988e 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 4-bit signed int
1988f 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 eger..** If the
19890 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f 20 6c 61 double is too la
19891 72 67 65 2c 20 72 65 74 75 72 6e 20 30 78 38 30 rge, return 0x80
19892 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2e 0a 00000000000000..
19893 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79 73 74 65 **.** Most syste
19894 6d 73 20 61 70 70 65 61 72 20 74 6f 20 64 6f 20 ms appear to do
19895 74 68 69 73 20 73 69 6d 70 6c 79 20 62 79 20 61 this simply by a
19896 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76 61 72 69 ssigning.** vari
19897 61 62 6c 65 73 20 61 6e 64 20 77 69 74 68 6f 75 ables and withou
19898 74 20 74 68 65 20 65 78 74 72 61 20 72 61 6e 67 t the extra rang
19899 65 20 74 65 73 74 73 2e 20 20 42 75 74 0a 2a 2a e tests. But.**
1989a 20 74 68 65 72 65 20 61 72 65 20 72 65 70 6f 72 there are repor
1989b 74 73 20 74 68 61 74 20 77 69 6e 64 6f 77 73 20 ts that windows
1989c 74 68 72 6f 77 73 20 61 6e 20 65 78 70 65 63 74 throws an expect
1989d 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65 20 66 6c ion.** if the fl
1989e 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c oating point val
1989f 75 65 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e ue is out of ran
198a0 67 65 2e 20 28 53 65 65 20 74 69 63 6b 65 74 20 ge. (See ticket
198a1 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65 63 61 75 #2880.).** Becau
198a2 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 63 6f 6d se we do not com
198a3 70 6c 65 74 65 6c 79 20 75 6e 64 65 72 73 74 61 pletely understa
198a4 6e 64 20 74 68 65 20 70 72 6f 62 6c 65 6d 2c 20 nd the problem,
198a5 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61 6b 65 20 we will.** take
198a6 74 68 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 the conservative
198a7 20 61 70 70 72 6f 61 63 68 20 61 6e 64 20 61 6c approach and al
198a8 77 61 79 73 20 64 6f 20 72 61 6e 67 65 20 74 65 ways do range te
198a9 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 61 74 sts.** before at
198aa 74 65 6d 70 74 69 6e 67 20 74 68 65 20 63 6f 6e tempting the con
198ab 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 version..*/.stat
198ac 69 63 20 69 36 34 20 64 6f 75 62 6c 65 54 6f 49 ic i64 doubleToI
198ad 6e 74 36 34 28 64 6f 75 62 6c 65 20 72 29 7b 0a nt64(double r){.
198ae 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e 79 20 63 /*. ** Many c
198af 6f 6d 70 69 6c 65 72 73 20 77 65 20 65 6e 63 6f ompilers we enco
198b0 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20 64 65 66 unter do not def
198b1 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f ine constants fo
198b2 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69 6e 69 6d r the. ** minim
198b3 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75 6d 20 36 um and maximum 6
198b4 34 2d 62 69 74 20 69 6e 74 65 67 65 72 73 2c 20 4-bit integers,
198b5 6f 72 20 74 68 65 79 20 64 65 66 69 6e 65 20 74 or they define t
198b6 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f 6e 73 69 hem. ** inconsi
198b7 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64 20 6d 61 stently. And ma
198b8 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64 65 72 73 ny do not unders
198b9 74 61 6e 64 20 74 68 65 20 22 4c 4c 22 20 6e 6f tand the "LL" no
198ba 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53 6f 20 tation.. ** So
198bb 77 65 20 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 we define our ow
198bc 6e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 61 6e n static constan
198bd 74 73 20 68 65 72 65 20 75 73 69 6e 67 20 6e 6f ts here using no
198be 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61 72 67 65 thing. ** large
198bf 72 20 74 68 61 6e 20 61 20 33 32 2d 62 69 74 20 r than a 32-bit
198c0 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 integer constant
198c1 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 .. */. static
198c2 63 6f 6e 73 74 20 69 36 34 20 6d 61 78 49 6e 74 const i64 maxInt
198c3 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 = LARGEST_INT64
198c4 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 ;. static const
198c5 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d 20 53 4d i64 minInt = SM
198c6 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b 0a 0a 20 ALLEST_INT64;..
198c7 20 69 66 28 20 72 3c 28 64 6f 75 62 6c 65 29 6d if( r<(double)m
198c8 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 inInt ){. ret
198c9 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d 65 urn minInt;. }e
198ca 6c 73 65 20 69 66 28 20 72 3e 28 64 6f 75 62 6c lse if( r>(doubl
198cb 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20 20 20 20 e)maxInt ){.
198cc 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20 63 6f 72 /* minInt is cor
198cd 72 65 63 74 20 68 65 72 65 20 2d 20 6e 6f 74 20 rect here - not
198ce 6d 61 78 49 6e 74 2e 20 20 49 74 20 74 75 72 6e maxInt. It turn
198cf 73 20 6f 75 74 20 74 68 61 74 20 61 73 73 69 67 s out that assig
198d0 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61 20 76 65 ning. ** a ve
198d1 72 79 20 6c 61 72 67 65 20 70 6f 73 69 74 69 76 ry large positiv
198d2 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 69 e number to an i
198d3 6e 74 65 67 65 72 20 72 65 73 75 6c 74 73 20 69 nteger results i
198d4 6e 20 61 20 76 65 72 79 20 6c 61 72 67 65 0a 20 n a very large.
198d5 20 20 20 2a 2a 20 6e 65 67 61 74 69 76 65 20 69 ** negative i
198d6 6e 74 65 67 65 72 2e 20 20 54 68 69 73 20 6d 61 nteger. This ma
198d7 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c 20 62 75 kes no sense, bu
198d8 74 20 69 74 20 69 73 20 77 68 61 74 20 78 38 36 t it is what x86
198d9 20 68 61 72 64 77 61 72 65 0a 20 20 20 20 2a 2a hardware. **
198da 20 64 6f 65 73 20 73 6f 20 66 6f 72 20 63 6f 6d does so for com
198db 70 61 74 69 62 69 6c 69 74 79 20 77 65 20 77 69 patibility we wi
198dc 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d 65 20 69 ll do the same i
198dd 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a 2f 0a 20 n software. */.
198de 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 return minInt
198df 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
198e0 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a 20 20 eturn (i64)r;.
198e1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 }.}../*.** Retur
198e2 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 69 n some kind of i
198e3 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77 68 69 nteger value whi
198e4 63 68 20 69 73 20 74 68 65 20 62 65 73 74 20 77 ch is the best w
198e5 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74 20 72 e can do.** at r
198e6 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 epresenting the
198e7 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d 65 6d value that *pMem
198e8 20 64 65 73 63 72 69 62 65 73 20 61 73 20 61 6e describes as an
198e9 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 integer..** If
198ea 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 pMem is an integ
198eb 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c er, then the val
198ec 75 65 20 69 73 20 65 78 61 63 74 2e 20 20 49 66 ue is exact. If
198ed 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20 66 6c pMem is.** a fl
198ee 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74 68 65 oating-point the
198ef 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 n the value retu
198f0 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e 74 65 rned is the inte
198f1 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 ger part..** If
198f2 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69 6e 67 pMem is a string
198f3 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 77 or blob, then w
198f4 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65 6d 70 e make an attemp
198f5 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a 2a 20 t to convert.**
198f6 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65 67 65 it into a intege
198f7 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 61 r and return tha
198f8 74 2e 20 20 49 66 20 70 4d 65 6d 20 72 65 70 72 t. If pMem repr
198f9 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20 61 6e 20 esents an.** an
198fa 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 SQL-NULL value,
198fb 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 return 0..**.**
198fc 49 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e If pMem represen
198fd 74 73 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 ts a string valu
198fe 65 2c 20 69 74 73 20 65 6e 63 6f 64 69 6e 67 20 e, its encoding
198ff 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67 65 64 might be changed
19900 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19901 41 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 56 ATE i64 sqlite3V
19902 64 62 65 49 6e 74 56 61 6c 75 65 28 4d 65 6d 20 dbeIntValue(Mem
19903 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c *pMem){. int fl
19904 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ags;. assert( p
19905 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 Mem->db==0 || sq
19906 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 lite3_mutex_held
19907 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 (pMem->db->mutex
19908 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 ) );. assert( E
19909 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d IGHT_BYTE_ALIGNM
1990a 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 66 ENT(pMem) );. f
1990b 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 lags = pMem->fla
1990c 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 gs;. if( flags
1990d 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
1990e 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75 2e return pMem->u.
1990f 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 i;. }else if( f
19910 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 lags & MEM_Real
19911 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 6f ){. return do
19912 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d ubleToInt64(pMem
19913 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 ->r);. }else if
19914 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 ( flags & (MEM_S
19915 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a tr|MEM_Blob) ){.
19916 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20 i64 value;.
19917 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c pMem->flags |
19918 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 = MEM_Str;. i
19919 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 f( sqlite3VdbeCh
1991a 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 angeEncoding(pMe
1991b 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a m, SQLITE_UTF8).
1991c 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 || sqlite
1991d 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 3VdbeMemNulTermi
1991e 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 nate(pMem) ){.
1991f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
19920 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
19921 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 pMem->z );. s
19922 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d 65 qlite3Atoi64(pMe
19923 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 m->z, &value);.
19924 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 3b return value;
19925 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 . }else{. re
19926 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f turn 0;. }.}../
19927 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
19928 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61 74 best representat
19929 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61 74 ion of pMem that
1992a 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74 6f we can get into
1992b 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20 49 a.** double. I
1992c 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61 64 f pMem is alread
1992d 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61 6e y a double or an
1992e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e integer, return
1992f 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 its.** value.
19930 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69 6e If it is a strin
19931 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 g or blob, try t
19932 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 o convert it to
19933 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66 20 a double..** If
19934 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72 65 it is a NULL, re
19935 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51 4c turn 0.0..*/.SQL
19936 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75 62 ITE_PRIVATE doub
19937 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 le sqlite3VdbeRe
19938 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 alValue(Mem *pMe
19939 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d m){. assert( pM
1993a 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
1993b 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
1993c 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
1993d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 );. assert( EI
1993e 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
1993f 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20 69 66 NT(pMem) );. if
19940 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 ( pMem->flags &
19941 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
19942 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 72 3b 0a return pMem->r;.
19943 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d }else if( pMem
19944 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
19945 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
19946 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e (double)pMem->u.
19947 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 i;. }else if( p
19948 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 Mem->flags & (ME
19949 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 M_Str|MEM_Blob)
1994a 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c ){. /* (doubl
1994b 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 e)0 In case of S
1994c 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 QLITE_OMIT_FLOAT
1994d 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a ING_POINT... */.
1994e 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d double val =
1994f 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20 20 (double)0;.
19950 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d pMem->flags |= M
19951 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 EM_Str;. if(
19952 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
19953 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 eEncoding(pMem,
19954 53 51 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 SQLITE_UTF8).
19955 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 || sqlite3Vd
19956 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
19957 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 e(pMem) ){.
19958 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e /* (double)0 In
19959 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f case of SQLITE_
1995a 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f OMIT_FLOATING_PO
1995b 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 INT... */.
1995c 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65 29 30 return (double)0
1995d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 ;. }. asse
1995e 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 rt( pMem->z );.
1995f 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 70 sqlite3AtoF(p
19960 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a 20 Mem->z, &val);.
19961 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 return val;.
19962 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 28 }else{. /* (
19963 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 double)0 In case
19964 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f of SQLITE_OMIT_
19965 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e FLOATING_POINT..
19966 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 . */. return
19967 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 0a 7d (double)0;. }.}
19968 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 ../*.** The MEM
19969 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 structure is alr
1996a 65 61 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e eady a MEM_Real.
1996b 20 20 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 Try to also ma
1996c 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 ke it a.** MEM_I
1996d 6e 74 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f nt if we can..*/
1996e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1996f 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
19970 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 IntegerAffinity(
19971 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 Mem *pMem){. as
19972 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 sert( pMem->flag
19973 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a s & MEM_Real );.
19974 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
19975 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19976 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 Set)==0 );. ass
19977 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 ert( pMem->db==0
19978 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 || sqlite3_mute
19979 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d x_held(pMem->db-
1997a 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 >mutex) );. ass
1997b 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
1997c 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 ALIGNMENT(pMem)
1997d 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 );.. pMem->u.i
1997e 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 = doubleToInt64(
1997f 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20 2f 2a 20 pMem->r);.. /*
19980 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65 20 76 61 Only mark the va
19981 6c 75 65 20 61 73 20 61 6e 20 69 6e 74 65 67 65 lue as an intege
19982 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 r if. **. **
19983 20 20 28 31 29 20 74 68 65 20 72 6f 75 6e 64 2d (1) the round-
19984 74 72 69 70 20 63 6f 6e 76 65 72 73 69 6f 6e 20 trip conversion
19985 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65 61 6c 20 real->int->real
19986 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61 6e 64 0a is a no-op, and.
19987 20 20 2a 2a 20 20 20 20 28 32 29 20 54 68 65 20 ** (2) The
19988 69 6e 74 65 67 65 72 20 69 73 20 6e 65 69 74 68 integer is neith
19989 65 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 6e er the largest n
1998a 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 0a or the smallest.
1998b 20 20 2a 2a 20 20 20 20 20 20 20 20 70 6f 73 73 ** poss
1998c 69 62 6c 65 20 69 6e 74 65 67 65 72 20 28 74 69 ible integer (ti
1998d 63 6b 65 74 20 23 33 39 32 32 29 0a 20 20 2a 2a cket #3922). **
1998e 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 . ** The second
1998f 20 61 6e 64 20 74 68 69 72 64 20 74 65 72 6d 73 and third terms
19990 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e in the followin
19991 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 6e g conditional en
19992 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74 68 65 20 forces. ** the
19993 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74 69 6f 6e second condition
19994 20 75 6e 64 65 72 20 74 68 65 20 61 73 73 75 6d under the assum
19995 70 74 69 6f 6e 20 74 68 61 74 20 61 64 64 69 74 ption that addit
19996 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 63 61 75 ion overflow cau
19997 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75 65 73 20 ses. ** values
19998 74 6f 20 77 72 61 70 20 61 72 6f 75 6e 64 2e 20 to wrap around.
19999 20 4f 6e 20 78 38 36 20 68 61 72 64 77 61 72 65 On x86 hardware
1999a 2c 20 74 68 65 20 74 68 69 72 64 20 74 65 72 6d , the third term
1999b 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20 is always. **
1999c 74 72 75 65 20 61 6e 64 20 63 6f 75 6c 64 20 62 true and could b
1999d 65 20 6f 6d 69 74 74 65 64 2e 20 20 42 75 74 20 e omitted. But
1999e 77 65 20 6c 65 61 76 65 20 69 74 20 69 6e 20 62 we leave it in b
1999f 65 63 61 75 73 65 20 6f 74 68 65 72 0a 20 20 2a ecause other. *
199a0 2a 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 * architectures
199a1 6d 69 67 68 74 20 62 65 68 61 76 65 20 64 69 66 might behave dif
199a2 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a 2f 0a 20 ferently.. */.
199a3 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d 3d 28 64 if( pMem->r==(d
199a4 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 20 ouble)pMem->u.i
199a5 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e 53 4d 41 && pMem->u.i>SMA
199a6 4c 4c 45 53 54 5f 49 4e 54 36 34 0a 20 20 20 20 LLEST_INT64.
199a7 20 20 26 26 20 41 4c 57 41 59 53 28 70 4d 65 6d && ALWAYS(pMem
199a8 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54 5f 49 4e ->u.i<LARGEST_IN
199a9 54 36 34 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d T64) ){. pMem
199aa 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 49 ->flags |= MEM_I
199ab 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a nt;. }.}../*.**
199ac 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 74 6f Convert pMem to
199ad 20 74 79 70 65 20 69 6e 74 65 67 65 72 2e 20 20 type integer.
199ae 49 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 Invalidate any p
199af 72 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 rior representat
199b0 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ions..*/.SQLITE_
199b1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
199b2 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
199b3 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b rify(Mem *pMem){
199b4 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d . assert( pMem-
199b5 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 >db==0 || sqlite
199b6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 3_mutex_held(pMe
199b7 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b m->db->mutex) );
199b8 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
199b9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
199ba 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 wSet)==0 );. as
199bb 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 sert( EIGHT_BYTE
199bc 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 _ALIGNMENT(pMem)
199bd 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 );.. pMem->u.i
199be 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e = sqlite3VdbeIn
199bf 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 tValue(pMem);.
199c0 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 MemSetTypeFlag(p
199c1 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 Mem, MEM_Int);.
199c2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
199c3 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 K;.}../*.** Conv
199c4 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 ert pMem so that
199c5 20 69 74 20 69 73 20 6f 66 20 74 79 70 65 20 4d it is of type M
199c6 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49 6e 76 61 EM_Real..** Inva
199c7 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 lidate any prior
199c8 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
199c9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
199ca 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
199cb 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d 65 dbeMemRealify(Me
199cc 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 m *pMem){. asse
199cd 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
199ce 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
199cf 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
199d0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
199d1 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
199d2 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 LIGNMENT(pMem) )
199d3 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 73 ;.. pMem->r = s
199d4 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
199d5 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d lue(pMem);. Mem
199d6 53 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d SetTypeFlag(pMem
199d7 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a 20 20 72 , MEM_Real);. r
199d8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
199d9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 .}../*.** Conver
199da 74 20 70 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 t pMem so that i
199db 74 20 68 61 73 20 74 79 70 65 73 20 4d 45 4d 5f t has types MEM_
199dc 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49 6e 74 20 Real or MEM_Int
199dd 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49 6e 76 61 or both..** Inva
199de 6c 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 lidate any prior
199df 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 representations
199e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
199e1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
199e2 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 4d dbeMemNumerify(M
199e3 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 64 6f 75 em *pMem){. dou
199e4 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 69 36 ble r1, r2;. i6
199e5 34 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 28 4 i;. assert( (
199e6 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d pMem->flags & (M
199e7 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c EM_Int|MEM_Real|
199e8 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30 20 29 3b MEM_Null))==0 );
199e9 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
199ea 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 42 ->flags & (MEM_B
199eb 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 21 3d 30 lob|MEM_Str))!=0
199ec 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d );. assert( pM
199ed 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c em->db==0 || sql
199ee 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 ite3_mutex_held(
199ef 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 pMem->db->mutex)
199f0 20 29 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 );. r1 = sqlit
199f1 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 e3VdbeRealValue(
199f2 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20 64 6f 75 pMem);. i = dou
199f3 62 6c 65 54 6f 49 6e 74 36 34 28 72 31 29 3b 0a bleToInt64(r1);.
199f4 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 69 r2 = (double)i
199f5 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72 32 20 29 ;. if( r1==r2 )
199f6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
199f7 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 eMemIntegerify(p
199f8 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 Mem);. }else{.
199f9 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 72 31 3b pMem->r = r1;
199fa 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 . MemSetTypeF
199fb 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 lag(pMem, MEM_Re
199fc 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 al);. }. retur
199fd 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
199fe 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 /*.** Delete any
199ff 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 previous value
19a00 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 and set the valu
19a01 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 e stored in *pMe
19a02 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 m to NULL..*/.SQ
19a03 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19a04 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
19a05 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a 70 4d 65 SetNull(Mem *pMe
19a06 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e m){. if( pMem->
19a07 66 6c 61 67 73 20 26 20 4d 45 4d 5f 46 72 61 6d flags & MEM_Fram
19a08 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 e ){. sqlite3
19a09 56 64 62 65 46 72 61 6d 65 44 65 6c 65 74 65 28 VdbeFrameDelete(
19a0a 70 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d 65 29 3b pMem->u.pFrame);
19a0b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d . }. if( pMem-
19a0c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19a0d 53 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 Set ){. sqlit
19a0e 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 4d e3RowSetClear(pM
19a0f 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a em->u.pRowSet);.
19a10 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 }. MemSetType
19a11 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e Flag(pMem, MEM_N
19a12 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 ull);. pMem->ty
19a13 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c pe = SQLITE_NULL
19a14 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 ;.}../*.** Delet
19a15 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 e any previous v
19a16 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 alue and set the
19a17 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20 42 value to be a B
19a18 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a 2a LOB of length.**
19a19 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c n containing al
19a1a 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 l zeros..*/.SQLI
19a1b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19a1c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19a1d 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a 70 tZeroBlob(Mem *p
19a1e 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 73 Mem, int n){. s
19a1f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
19a20 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d ease(pMem);. pM
19a21 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f em->flags = MEM_
19a22 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 Blob|MEM_Zero;.
19a23 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
19a24 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65 LITE_BLOB;. pMe
19a25 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 m->n = 0;. if(
19a26 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70 n<0 ) n = 0;. p
19a27 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e Mem->u.nZero = n
19a28 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 ;. pMem->enc =
19a29 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 0a 23 69 SQLITE_UTF8;..#i
19a2a 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19a2b 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 73 71 6c 69 _INCRBLOB. sqli
19a2c 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
19a2d 4d 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20 20 69 66 Mem, n, 0);. if
19a2e 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 ( pMem->z ){.
19a2f 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 pMem->n = n;.
19a30 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a memset(pMem->z
19a31 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 23 65 6e , 0, n);. }.#en
19a32 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c dif.}../*.** Del
19a33 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 ete any previous
19a34 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 value and set t
19a35 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 he value stored
19a36 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c in *pMem to val,
19a37 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 .** manifest typ
19a38 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f 0a 53 51 e INTEGER..*/.SQ
19a39 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
19a3a 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
19a3b 53 65 74 49 6e 74 36 34 28 4d 65 6d 20 2a 70 4d SetInt64(Mem *pM
19a3c 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b 0a 20 20 em, i64 val){.
19a3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
19a3e 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 lease(pMem);. p
19a3f 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61 6c 3b 0a Mem->u.i = val;.
19a40 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
19a41 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d 65 6d 2d MEM_Int;. pMem-
19a42 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 >type = SQLITE_I
19a43 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a NTEGER;.}../*.**
19a44 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 Delete any prev
19a45 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 ious value and s
19a46 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f et the value sto
19a47 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 red in *pMem to
19a48 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 val,.** manifest
19a49 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a 2f 0a 53 type REAL..*/.S
19a4a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
19a4b 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 id sqlite3VdbeMe
19a4c 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65 6d 20 2a mSetDouble(Mem *
19a4d 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20 76 61 6c pMem, double val
19a4e 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 ){. if( sqlite3
19a4f 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b 0a 20 20 IsNaN(val) ){.
19a50 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
19a51 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 SetNull(pMem);.
19a52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 }else{. sqli
19a53 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 te3VdbeMemReleas
19a54 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 e(pMem);. pMe
19a55 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20 20 20 20 m->r = val;.
19a56 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
19a57 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70 4d 65 6d M_Real;. pMem
19a58 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f ->type = SQLITE_
19a59 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a FLOAT;. }.}../*
19a5a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 .** Delete any p
19a5b 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e revious value an
19a5c 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 d set the value
19a5d 6f 66 20 70 4d 65 6d 20 74 6f 20 62 65 20 61 6e of pMem to be an
19a5e 0a 2a 2a 20 65 6d 70 74 79 20 62 6f 6f 6c 65 61 .** empty boolea
19a5f 6e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 n index..*/.SQLI
19a60 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
19a61 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19a62 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a 70 4d 65 tRowSet(Mem *pMe
19a63 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 m){. sqlite3 *d
19a64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 b = pMem->db;.
19a65 61 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b assert( db!=0 );
19a66 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d . assert( (pMem
19a67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
19a68 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 wSet)==0 );. sq
19a69 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19a6a 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 ase(pMem);. pMe
19a6b 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71 6c m->zMalloc = sql
19a6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
19a6d 64 62 2c 20 36 34 29 3b 0a 20 20 69 66 28 20 64 db, 64);. if( d
19a6e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
19a6f 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ){. pMem->fla
19a70 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
19a71 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 }else{. asse
19a72 72 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f rt( pMem->zMallo
19a73 63 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 c );. pMem->u
19a74 2e 70 52 6f 77 53 65 74 20 3d 20 73 71 6c 69 74 .pRowSet = sqlit
19a75 65 33 52 6f 77 53 65 74 49 6e 69 74 28 64 62 2c e3RowSetInit(db,
19a76 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 pMem->zMalloc,
19a77 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
19a78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19a79 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
19a7a 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 bMallocSize(db,
19a7b 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 3b pMem->zMalloc));
19a7c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
19a7d 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 21 3d 30 20 m->u.pRowSet!=0
19a7e 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 );. pMem->fla
19a7f 67 73 20 3d 20 4d 45 4d 5f 52 6f 77 53 65 74 3b gs = MEM_RowSet;
19a80 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
19a81 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
19a82 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63 6f 6e 74 Mem object cont
19a83 61 69 6e 73 20 61 20 54 45 58 54 20 6f 72 20 42 ains a TEXT or B
19a84 4c 4f 42 20 74 68 61 74 20 69 73 0a 2a 2a 20 74 LOB that is.** t
19a85 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68 6f 73 65 oo large - whose
19a86 20 73 69 7a 65 20 65 78 63 65 65 64 73 20 53 51 size exceeds SQ
19a87 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2e LITE_MAX_LENGTH.
19a88 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19a89 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19a8a 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d 65 6d 20 beMemTooBig(Mem
19a8b 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 *p){. assert( p
19a8c 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 ->db!=0 );. if(
19a8d 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d p->flags & (MEM
19a8e 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 _Str|MEM_Blob) )
19a8f 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d {. int n = p-
19a90 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 >n;. if( p->f
19a91 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 lags & MEM_Zero
19a92 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 70 2d ){. n += p-
19a93 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 0a >u.nZero;. }.
19a94 20 20 20 20 72 65 74 75 72 6e 20 6e 3e 70 2d 3e return n>p->
19a95 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
19a96 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b E_LIMIT_LENGTH];
19a97 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
19a98 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 .}../*.** Size
19a99 6f 66 20 73 74 72 75 63 74 20 4d 65 6d 20 6e 6f of struct Mem no
19a9a 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 t including the
19a9b 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d 65 6d 62 Mem.zMalloc memb
19a9c 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d er..*/.#define M
19a9d 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73 69 7a 65 EMCELLSIZE (size
19a9e 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a 29 30 29 _t)(&(((Mem *)0)
19a9f 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a ->zMalloc))../*.
19aa0 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68 61 6c 6c ** Make an shall
19aa1 6f 77 20 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d ow copy of pFrom
19aa2 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f into pTo. Prio
19aa3 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a r contents of.**
19aa4 20 70 54 6f 20 61 72 65 20 66 72 65 65 64 2e 20 pTo are freed.
19aa5 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a 20 66 69 The pFrom->z fi
19aa6 65 6c 64 20 69 73 20 6e 6f 74 20 64 75 70 6c 69 eld is not dupli
19aa7 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a 20 70 46 cated. If.** pF
19aa8 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65 64 2c 20 rom->z is used,
19aa9 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70 6f 69 6e then pTo->z poin
19aaa 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 ts to the same t
19aab 68 69 6e 67 20 61 73 20 70 46 72 6f 6d 2d 3e 7a hing as pFrom->z
19aac 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73 20 67 65 .** and flags ge
19aad 74 73 20 73 72 63 54 79 70 65 20 28 65 69 74 68 ts srcType (eith
19aae 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20 6f 72 20 er MEM_Ephem or
19aaf 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 2a 2f 0a MEM_Static)..*/.
19ab0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
19ab1 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d oid sqlite3VdbeM
19ab2 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65 emShallowCopy(Me
19ab3 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 m *pTo, const Me
19ab4 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74 20 73 72 m *pFrom, int sr
19ab5 63 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 cType){. assert
19ab6 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 20 ( (pFrom->flags
19ab7 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 & MEM_RowSet)==0
19ab8 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 );. sqlite3Vdb
19ab9 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
19aba 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 nal(pTo);. memc
19abb 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 4d py(pTo, pFrom, M
19abc 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20 20 70 EMCELLSIZE);. p
19abd 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 To->xDel = 0;.
19abe 69 66 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 if( (pFrom->flag
19abf 73 26 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 7c 7c s&MEM_Dyn)!=0 ||
19ac0 20 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d pFrom->z==pFrom
19ac1 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 ->zMalloc ){.
19ac2 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e pTo->flags &= ~
19ac3 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 61 (MEM_Dyn|MEM_Sta
19ac4 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a tic|MEM_Ephem);.
19ac5 20 20 20 20 61 73 73 65 72 74 28 20 73 72 63 54 assert( srcT
19ac6 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65 6d 20 7c ype==MEM_Ephem |
19ac7 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 53 | srcType==MEM_S
19ac8 74 61 74 69 63 20 29 3b 0a 20 20 20 20 70 54 6f tatic );. pTo
19ac9 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72 63 54 79 ->flags |= srcTy
19aca 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a pe;. }.}../*.**
19acb 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20 63 6f 70 Make a full cop
19acc 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 y of pFrom into
19acd 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 pTo. Prior cont
19ace 65 6e 74 73 20 6f 66 20 70 54 6f 20 61 72 65 0a ents of pTo are.
19acf 2a 2a 20 66 72 65 65 64 20 62 65 66 6f 72 65 20 ** freed before
19ad0 74 68 65 20 63 6f 70 79 20 69 73 20 6d 61 64 65 the copy is made
19ad1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
19ad2 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19ad3 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d 20 2a dbeMemCopy(Mem *
19ad4 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a pTo, const Mem *
19ad5 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 pFrom){. int rc
19ad6 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 = SQLITE_OK;..
19ad7 20 61 73 73 65 72 74 28 20 28 70 46 72 6f 6d 2d assert( (pFrom-
19ad8 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
19ad9 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 73 71 6c Set)==0 );. sql
19ada 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
19adb 73 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b seExternal(pTo);
19adc 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 . memcpy(pTo, p
19add 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a From, MEMCELLSIZ
19ade 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 E);. pTo->flags
19adf 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 &= ~MEM_Dyn;..
19ae0 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 if( pTo->flags&
19ae1 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
19ae2 62 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d b) ){. if( 0=
19ae3 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d =(pFrom->flags&M
19ae4 45 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 EM_Static) ){.
19ae5 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c pTo->flags |
19ae6 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 = MEM_Ephem;.
19ae7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
19ae8 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 dbeMemMakeWritea
19ae9 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a ble(pTo);. }.
19aea 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 }.. return rc
19aeb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 ;.}../*.** Trans
19aec 66 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 fer the contents
19aed 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f of pFrom to pTo
19aee 2e 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 . Any existing v
19aef 61 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a alue in pTo is.*
19af0 2a 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f * freed. If pFro
19af1 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d m contains ephem
19af2 65 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 eral data, a cop
19af3 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a y is made..**.**
19af4 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 pFrom contains
19af5 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e an SQL NULL when
19af6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
19af7 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 turns..*/.SQLITE
19af8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
19af9 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 lite3VdbeMemMove
19afa 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a (Mem *pTo, Mem *
19afb 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 pFrom){. assert
19afc 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c ( pFrom->db==0 |
19afd 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
19afe 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e held(pFrom->db->
19aff 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19b00 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c rt( pTo->db==0 |
19b01 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f | sqlite3_mutex_
19b02 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 held(pTo->db->mu
19b03 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 tex) );. assert
19b04 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c ( pFrom->db==0 |
19b05 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 | pTo->db==0 ||
19b06 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e pFrom->db==pTo->
19b07 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 db );.. sqlite3
19b08 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19b09 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 To);. memcpy(pT
19b0a 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 o, pFrom, sizeof
19b0b 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d (Mem));. pFrom-
19b0c 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c >flags = MEM_Nul
19b0d 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c l;. pFrom->xDel
19b0e 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a = 0;. pFrom->z
19b0f 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f Malloc = 0;.}../
19b10 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 *.** Change the
19b11 76 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 value of a Mem t
19b12 6f 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 o be a string or
19b13 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 a BLOB..**.** T
19b14 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 he memory manage
19b15 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 ment strategy de
19b16 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c pends on the val
19b17 75 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a ue of the xDel.*
19b18 2a 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 * parameter. If
19b19 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 the value passed
19b1a 20 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 is SQLITE_TRANS
19b1b 49 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a IENT, then the .
19b1c 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 ** string is cop
19b1d 69 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 ied into a (poss
19b1e 69 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 ibly existing) b
19b1f 75 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 uffer managed by
19b20 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 the .** Mem str
19b21 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 ucture. Otherwis
19b22 65 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 e, any existing
19b23 62 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 buffer is freed
19b24 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 and the.** point
19b25 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2a 0a 2a 2a er copied..**.**
19b26 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 If the string i
19b27 73 20 74 6f 6f 20 6c 61 72 67 65 20 28 69 66 20 s too large (if
19b28 69 74 20 65 78 63 65 65 64 73 20 74 68 65 20 53 it exceeds the S
19b29 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
19b2a 54 48 0a 2a 2a 20 73 69 7a 65 20 6c 69 6d 69 74 TH.** size limit
19b2b 29 20 74 68 65 6e 20 6e 6f 20 6d 65 6d 6f 72 79 ) then no memory
19b2c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 63 63 75 allocation occu
19b2d 72 73 2e 20 20 49 66 20 74 68 65 20 73 74 72 69 rs. If the stri
19b2e 6e 67 20 63 61 6e 20 62 65 0a 2a 2a 20 73 74 6f ng can be.** sto
19b2f 72 65 64 20 77 69 74 68 6f 75 74 20 61 6c 6c 6f red without allo
19b30 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 74 cating memory, t
19b31 68 65 6e 20 69 74 20 69 73 2e 20 20 49 66 20 61 hen it is. If a
19b32 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
19b33 6f 6e 0a 2a 2a 20 69 73 20 72 65 71 75 69 72 65 on.** is require
19b34 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 d to store the s
19b35 74 72 69 6e 67 2c 20 74 68 65 6e 20 76 61 6c 75 tring, then valu
19b36 65 20 6f 66 20 70 4d 65 6d 20 69 73 20 75 6e 63 e of pMem is unc
19b37 68 61 6e 67 65 64 2e 20 20 49 6e 0a 2a 2a 20 65 hanged. In.** e
19b38 69 74 68 65 72 20 63 61 73 65 2c 20 53 51 4c 49 ither case, SQLI
19b39 54 45 5f 54 4f 4f 42 49 47 20 69 73 20 72 65 74 TE_TOOBIG is ret
19b3a 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 urned..*/.SQLITE
19b3b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19b3c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
19b3d 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 r(. Mem *pMem,
19b3e 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f /* Memo
19b3f 72 79 20 63 65 6c 6c 20 74 6f 20 73 65 74 20 74 ry cell to set t
19b40 6f 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 2a o string value *
19b41 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a /. const char *
19b42 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e z, /* Strin
19b43 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 g pointer */. i
19b44 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 nt n,
19b45 20 20 20 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 /* Bytes in s
19b46 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 tring, or negati
19b47 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 ve */. u8 enc,
19b48 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
19b49 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 ncoding of z. 0
19b4a 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 for BLOBs */.
19b4b 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 void (*xDel)(voi
19b4c 64 2a 29 20 2f 2a 20 44 65 73 74 72 75 63 74 6f d*) /* Destructo
19b4d 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b r function */.){
19b4e 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 6e . int nByte = n
19b4f 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 ; /* New va
19b50 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e 6e 20 lue for pMem->n
19b51 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b */. int iLimit;
19b52 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 /* Maxi
19b53 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 mum allowed stri
19b54 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69 7a 65 20 ng or blob size
19b55 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 20 3d */. u16 flags =
19b56 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 0; /* New
19b57 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d 3e value for pMem->
19b58 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61 73 73 65 flags */.. asse
19b59 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 rt( pMem->db==0
19b5a 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 || sqlite3_mutex
19b5b 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e _held(pMem->db->
19b5c 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 mutex) );. asse
19b5d 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 rt( (pMem->flags
19b5e 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d & MEM_RowSet)==
19b5f 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 7a 20 0 );.. /* If z
19b60 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 is a NULL pointe
19b61 72 2c 20 73 65 74 20 70 4d 65 6d 20 74 6f 20 63 r, set pMem to c
19b62 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e 55 ontain an SQL NU
19b63 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 21 7a 20 LL. */. if( !z
19b64 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
19b65 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 beMemSetNull(pMe
19b66 6d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 m);. return S
19b67 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 QLITE_OK;. }..
19b68 20 69 66 28 20 70 4d 65 6d 2d 3e 64 62 20 29 7b if( pMem->db ){
19b69 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 4d . iLimit = pM
19b6a 65 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 em->db->aLimit[S
19b6b 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
19b6c 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 TH];. }else{.
19b6d 20 20 69 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 iLimit = SQLIT
19b6e 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b 0a 20 20 E_MAX_LENGTH;.
19b6f 7d 0a 20 20 66 6c 61 67 73 20 3d 20 28 65 6e 63 }. flags = (enc
19b70 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a 4d 45 4d ==0?MEM_Blob:MEM
19b71 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 _Str);. if( nBy
19b72 74 65 3c 30 20 29 7b 0a 20 20 20 20 61 73 73 65 te<0 ){. asse
19b73 72 74 28 20 65 6e 63 21 3d 30 20 29 3b 0a 20 20 rt( enc!=0 );.
19b74 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 if( enc==SQLIT
19b75 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 E_UTF8 ){.
19b76 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 for(nByte=0; nBy
19b77 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 7a 5b te<=iLimit && z[
19b78 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65 2b 2b 29 nByte]; nByte++)
19b79 7b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 {}. }else{.
19b7a 20 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b for(nByte=0;
19b7b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 nByte<=iLimit &
19b7c 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c 20 7a 5b & (z[nByte] | z[
19b7d 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42 79 74 65 nByte+1]); nByte
19b7e 2b 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a 20 20 20 +=2){}. }.
19b7f 20 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 flags |= MEM_Te
19b80 72 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 rm;. }.. /* Th
19b81 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 e following bloc
19b82 6b 20 73 65 74 73 20 74 68 65 20 6e 65 77 20 76 k sets the new v
19b83 61 6c 75 65 73 20 6f 66 20 4d 65 6d 2e 7a 20 61 alues of Mem.z a
19b84 6e 64 20 4d 65 6d 2e 78 44 65 6c 2e 20 49 74 0a nd Mem.xDel. It.
19b85 20 20 2a 2a 20 61 6c 73 6f 20 73 65 74 73 20 61 ** also sets a
19b86 20 66 6c 61 67 20 69 6e 20 6c 6f 63 61 6c 20 76 flag in local v
19b87 61 72 69 61 62 6c 65 20 22 66 6c 61 67 73 22 20 ariable "flags"
19b88 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 to indicate the
19b89 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d 61 6e 61 memory. ** mana
19b8a 67 65 6d 65 6e 74 20 28 6f 6e 65 20 6f 66 20 4d gement (one of M
19b8b 45 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d 5f 53 74 EM_Dyn or MEM_St
19b8c 61 74 69 63 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 atic).. */. if
19b8d 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 54 ( xDel==SQLITE_T
19b8e 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20 20 20 20 RANSIENT ){.
19b8f 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20 6e 42 79 int nAlloc = nBy
19b90 74 65 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 te;. if( flag
19b91 73 26 4d 45 4d 5f 54 65 72 6d 20 29 7b 0a 20 20 s&MEM_Term ){.
19b92 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d 20 28 65 nAlloc += (e
19b93 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f nc==SQLITE_UTF8?
19b94 31 3a 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 1:2);. }.
19b95 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 if( nByte>iLimit
19b96 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19b97 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a SQLITE_TOOBIG;.
19b98 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
19b99 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
19b9a 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63 2c 20 30 (pMem, nAlloc, 0
19b9b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
19b9c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
19b9d 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 }. memcpy
19b9e 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c (pMem->z, z, nAl
19b9f 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 loc);. }else if
19ba0 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 44 ( xDel==SQLITE_D
19ba1 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 73 71 YNAMIC ){. sq
19ba2 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 lite3VdbeMemRele
19ba3 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 ase(pMem);. p
19ba4 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 Mem->zMalloc = p
19ba5 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a Mem->z = (char *
19ba6 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 )z;. pMem->xD
19ba7 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b el = 0;. }else{
19ba8 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
19ba9 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 MemRelease(pMem)
19baa 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 ;. pMem->z =
19bab 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 70 (char *)z;. p
19bac 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c Mem->xDel = xDel
19bad 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 28 ;. flags |= (
19bae 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45 5f 53 54 (xDel==SQLITE_ST
19baf 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61 74 69 63 ATIC)?MEM_Static
19bb0 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 7d 0a 0a :MEM_Dyn);. }..
19bb1 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 pMem->n = nByt
19bb2 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 e;. pMem->flags
19bb3 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 4d 65 6d = flags;. pMem
19bb4 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d 3d 30 20 ->enc = (enc==0
19bb5 3f 20 53 51 4c 49 54 45 5f 55 54 46 38 20 3a 20 ? SQLITE_UTF8 :
19bb6 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 enc);. pMem->ty
19bb7 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 pe = (enc==0 ? S
19bb8 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20 53 51 4c QLITE_BLOB : SQL
19bb9 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23 69 66 6e ITE_TEXT);..#ifn
19bba 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
19bbb 55 54 46 31 36 0a 20 20 69 66 28 20 70 4d 65 6d UTF16. if( pMem
19bbc 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 ->enc!=SQLITE_UT
19bbd 46 38 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 F8 && sqlite3Vdb
19bbe 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 70 4d eMemHandleBom(pM
19bbf 65 6d 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 em) ){. retur
19bc0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a n SQLITE_NOMEM;.
19bc1 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 }.#endif.. if
19bc2 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 ( nByte>iLimit )
19bc3 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c {. return SQL
19bc4 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 7d 0a ITE_TOOBIG;. }.
19bc5 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
19bc6 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f _OK;.}../*.** Co
19bc7 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 mpare the values
19bc8 20 63 6f 6e 74 61 69 6e 65 64 20 62 79 20 74 68 contained by th
19bc9 65 20 74 77 6f 20 6d 65 6d 6f 72 79 20 63 65 6c e two memory cel
19bca 6c 73 2c 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a ls, returning.**
19bcb 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 20 negative, zero
19bcc 6f 72 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 or positive if p
19bcd 4d 65 6d 31 20 69 73 20 6c 65 73 73 20 74 68 61 Mem1 is less tha
19bce 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 n, equal to, or
19bcf 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 greater.** than
19bd0 70 4d 65 6d 32 2e 20 53 6f 72 74 69 6e 67 20 6f pMem2. Sorting o
19bd1 72 64 65 72 20 69 73 20 4e 55 4c 4c 27 73 20 66 rder is NULL's f
19bd2 69 72 73 74 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 irst, followed b
19bd3 79 20 6e 75 6d 62 65 72 73 20 28 69 6e 74 65 67 y numbers (integ
19bd4 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 61 6c 73 ers.** and reals
19bd5 29 20 73 6f 72 74 65 64 20 6e 75 6d 65 72 69 63 ) sorted numeric
19bd6 61 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 ally, followed b
19bd7 79 20 74 65 78 74 20 6f 72 64 65 72 65 64 20 62 y text ordered b
19bd8 79 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 0a y the collating.
19bd9 2a 2a 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c ** sequence pCol
19bda 6c 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20 62 6c l and finally bl
19bdb 6f 62 27 73 20 6f 72 64 65 72 65 64 20 62 79 20 ob's ordered by
19bdc 6d 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 memcmp()..**.**
19bdd 54 77 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 Two NULL values
19bde 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65 are considered e
19bdf 71 75 61 6c 20 62 79 20 74 68 69 73 20 66 75 6e qual by this fun
19be0 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ction..*/.SQLITE
19be1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
19be2 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 63 ite3MemCompare(c
19be3 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d 31 2c onst Mem *pMem1,
19be4 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65 6d const Mem *pMem
19be5 32 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c 53 65 71 2, const CollSeq
19be6 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69 6e 74 20 *pColl){. int
19be7 72 63 3b 0a 20 20 69 6e 74 20 66 31 2c 20 66 32 rc;. int f1, f2
19be8 3b 0a 20 20 69 6e 74 20 63 6f 6d 62 69 6e 65 64 ;. int combined
19be9 5f 66 6c 61 67 73 3b 0a 0a 20 20 66 31 20 3d 20 _flags;.. f1 =
19bea 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 pMem1->flags;.
19beb 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 f2 = pMem2->flag
19bec 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c s;. combined_fl
19bed 61 67 73 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 ags = f1|f2;. a
19bee 73 73 65 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 ssert( (combined
19bef 5f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 _flags & MEM_Row
19bf0 53 65 74 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f Set)==0 );. . /
19bf1 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 * If one value i
19bf2 73 20 4e 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 s NULL, it is le
19bf3 73 73 20 74 68 61 6e 20 74 68 65 20 6f 74 68 65 ss than the othe
19bf4 72 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 r. If both value
19bf5 73 0a 20 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c s. ** are NULL,
19bf6 20 72 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a return 0.. */.
19bf7 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 if( combined_f
19bf8 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b lags&MEM_Null ){
19bf9 0a 20 20 20 20 72 65 74 75 72 6e 20 28 66 32 26 . return (f2&
19bfa 4d 45 4d 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 MEM_Null) - (f1&
19bfb 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a MEM_Null);. }..
19bfc 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 /* If one valu
19bfd 65 20 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e e is a number an
19bfe 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e d the other is n
19bff 6f 74 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69 ot, the number i
19c00 73 20 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 s less.. ** If
19c01 62 6f 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73 both are numbers
19c02 2c 20 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61 , compare as rea
19c03 6c 73 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72 ls if one is a r
19c04 65 61 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 eal, or as integ
19c05 65 72 73 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 ers. ** if both
19c06 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 values are inte
19c07 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 gers.. */. if(
19c08 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 combined_flags&
19c09 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 (MEM_Int|MEM_Rea
19c0a 6c 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 28 l) ){. if( !(
19c0b 66 31 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f f1&(MEM_Int|MEM_
19c0c 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 Real)) ){.
19c0d 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
19c0e 20 20 20 20 69 66 28 20 21 28 66 32 26 28 4d 45 if( !(f2&(ME
19c0f 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 M_Int|MEM_Real))
19c10 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e ){. return
19c11 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 -1;. }. i
19c12 66 28 20 28 66 31 20 26 20 66 32 20 26 20 4d 45 f( (f1 & f2 & ME
19c13 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 M_Int)==0 ){.
19c14 20 20 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 double r1, r2
19c15 3b 0a 20 20 20 20 20 20 69 66 28 20 28 66 31 26 ;. if( (f1&
19c16 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a MEM_Real)==0 ){.
19c17 20 20 20 20 20 20 20 20 72 31 20 3d 20 28 64 6f r1 = (do
19c18 75 62 6c 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b uble)pMem1->u.i;
19c19 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
19c1a 20 20 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 r1 = pMem1
19c1b 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ->r;. }.
19c1c 20 20 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 if( (f2&MEM_R
19c1d 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 eal)==0 ){.
19c1e 20 20 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 r2 = (double)
19c1f 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 pMem2->u.i;.
19c20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
19c21 20 72 32 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a r2 = pMem2->r;.
19c22 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
19c23 28 20 72 31 3c 72 32 20 29 20 72 65 74 75 72 6e ( r1<r2 ) return
19c24 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 -1;. if( r
19c25 31 3e 72 32 20 29 20 72 65 74 75 72 6e 20 31 3b 1>r2 ) return 1;
19c26 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
19c27 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
19c28 20 20 61 73 73 65 72 74 28 20 66 31 26 4d 45 4d assert( f1&MEM
19c29 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 61 73 _Int );. as
19c2a 73 65 72 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 sert( f2&MEM_Int
19c2b 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d );. if( pM
19c2c 65 6d 31 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 em1->u.i < pMem2
19c2d 2d 3e 75 2e 69 20 29 20 72 65 74 75 72 6e 20 2d ->u.i ) return -
19c2e 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 1;. if( pMe
19c2f 6d 31 2d 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d m1->u.i > pMem2-
19c30 3e 75 2e 69 20 29 20 72 65 74 75 72 6e 20 31 3b >u.i ) return 1;
19c31 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
19c32 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
19c33 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 If one value is
19c34 20 61 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 a string and th
19c35 65 20 6f 74 68 65 72 20 69 73 20 61 20 62 6c 6f e other is a blo
19c36 62 2c 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 b, the string is
19c37 20 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 less.. ** If b
19c38 6f 74 68 20 61 72 65 20 73 74 72 69 6e 67 73 2c oth are strings,
19c39 20 63 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 74 compare using t
19c3a 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e he collating fun
19c3b 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 ctions.. */. i
19c3c 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 f( combined_flag
19c3d 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 s&MEM_Str ){.
19c3e 20 69 66 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 if( (f1 & MEM_S
19c3f 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 tr)==0 ){.
19c40 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
19c41 20 20 20 20 69 66 28 20 28 66 32 20 26 20 4d 45 if( (f2 & ME
19c42 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 M_Str)==0 ){.
19c43 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 return -1;.
19c44 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 }.. assert(
19c45 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 pMem1->enc==pMe
19c46 6d 32 2d 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 m2->enc );. a
19c47 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e ssert( pMem1->en
19c48 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c c==SQLITE_UTF8 |
19c49 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 | . p
19c4a 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 Mem1->enc==SQLIT
19c4b 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 E_UTF16LE || pMe
19c4c 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f m1->enc==SQLITE_
19c4d 55 54 46 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 UTF16BE );..
19c4e 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e /* The collation
19c4f 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62 sequence must b
19c50 65 20 64 65 66 69 6e 65 64 20 61 74 20 74 68 69 e defined at thi
19c51 73 20 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 s point, even if
19c52 0a 20 20 20 20 2a 2a 20 74 68 65 20 75 73 65 72 . ** the user
19c53 20 64 65 6c 65 74 65 73 20 74 68 65 20 63 6f 6c deletes the col
19c54 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 lation sequence
19c55 61 66 74 65 72 20 74 68 65 20 76 64 62 65 20 70 after the vdbe p
19c56 72 6f 67 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a rogram is. **
19c57 20 63 6f 6d 70 69 6c 65 64 20 28 74 68 69 73 20 compiled (this
19c58 77 61 73 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 was not always t
19c59 68 65 20 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f he case).. */
19c5a 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 43 . assert( !pC
19c5b 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 oll || pColl->xC
19c5c 6d 70 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 mp );.. if( p
19c5d 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 Coll ){. if
19c5e 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 ( pMem1->enc==pC
19c5f 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 oll->enc ){.
19c60 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e /* The strin
19c61 67 73 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 gs are already i
19c62 6e 20 74 68 65 20 63 6f 72 72 65 63 74 20 65 6e n the correct en
19c63 63 6f 64 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 coding. Call th
19c64 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d e. ** com
19c65 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e parison function
19c66 20 64 69 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 directly */.
19c67 20 20 20 20 20 72 65 74 75 72 6e 20 70 43 6f 6c return pCol
19c68 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 l->xCmp(pColl->p
19c69 55 73 65 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d User,pMem1->n,pM
19c6a 65 6d 31 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c em1->z,pMem2->n,
19c6b 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 pMem2->z);.
19c6c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
19c6d 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 const void *v1,
19c6e 2a 76 32 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 *v2;. int
19c6f 20 6e 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 n1, n2;.
19c70 20 4d 65 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 Mem c1;.
19c71 20 4d 65 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 Mem c2;.
19c72 20 6d 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 memset(&c1, 0,
19c73 73 69 7a 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 sizeof(c1));.
19c74 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 32 2c memset(&c2,
19c75 20 30 2c 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0, sizeof(c2));
19c76 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
19c77 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
19c78 70 79 28 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d py(&c1, pMem1, M
19c79 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 EM_Ephem);.
19c7a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19c7b 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 mShallowCopy(&c2
19c7c 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 , pMem2, MEM_Eph
19c7d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 76 31 20 em);. v1
19c7e 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 = sqlite3ValueTe
19c7f 78 74 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 xt((sqlite3_valu
19c80 65 2a 29 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 e*)&c1, pColl->e
19c81 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 nc);. n1
19c82 3d 20 76 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 = v1==0 ? 0 : c1
19c83 2e 6e 3b 0a 20 20 20 20 20 20 20 20 76 32 20 3d .n;. v2 =
19c84 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 sqlite3ValueTex
19c85 74 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 t((sqlite3_value
19c86 2a 29 26 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e *)&c2, pColl->en
19c87 63 29 3b 0a 20 20 20 20 20 20 20 20 6e 32 20 3d c);. n2 =
19c88 20 76 32 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e v2==0 ? 0 : c2.
19c89 6e 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 n;. rc =
19c8a 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c pColl->xCmp(pCol
19c8b 6c 2d 3e 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 l->pUser, n1, v1
19c8c 2c 20 6e 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 , n2, v2);.
19c8d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19c8e 6d 52 65 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 mRelease(&c1);.
19c8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
19c90 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 beMemRelease(&c2
19c91 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 );. retur
19c92 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 n rc;. }.
19c93 20 20 7d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 }. /* If a
19c94 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 NULL pointer was
19c95 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 63 passed as the c
19c96 6f 6c 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c ollate function,
19c97 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 fall through.
19c98 20 20 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 ** to the blob
19c99 20 63 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65 case and use me
19c9a 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a mcmp(). */. }.
19c9b 20 0a 20 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 . /* Both valu
19c9c 65 73 20 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 es must be blobs
19c9d 2e 20 20 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 . Compare using
19c9e 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 memcmp(). */.
19c9f 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 rc = memcmp(pMe
19ca0 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c m1->z, pMem2->z,
19ca1 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 (pMem1->n>pMem2
19ca2 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d ->n)?pMem2->n:pM
19ca3 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 em1->n);. if( r
19ca4 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d c==0 ){. rc =
19ca5 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d pMem1->n - pMem
19ca6 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 2->n;. }. retu
19ca7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
19ca8 4d 6f 76 65 20 64 61 74 61 20 6f 75 74 20 6f 66 Move data out of
19ca9 20 61 20 62 74 72 65 65 20 6b 65 79 20 6f 72 20 a btree key or
19caa 64 61 74 61 20 66 69 65 6c 64 20 61 6e 64 20 69 data field and i
19cab 6e 74 6f 20 61 20 4d 65 6d 20 73 74 72 75 63 74 nto a Mem struct
19cac 75 72 65 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 ure..** The data
19cad 20 6f 72 20 6b 65 79 20 69 73 20 74 61 6b 65 6e or key is taken
19cae 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 from the entry
19caf 74 68 61 74 20 70 43 75 72 20 69 73 20 63 75 72 that pCur is cur
19cb0 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a rently pointing.
19cb1 2a 2a 20 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 ** to. offset a
19cb2 6e 64 20 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 nd amt determine
19cb3 20 77 68 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 what portion of
19cb4 20 74 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 the data or key
19cb5 20 74 6f 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a to retrieve..**
19cb6 20 6b 65 79 20 69 73 20 74 72 75 65 20 74 6f 20 key is true to
19cb7 67 65 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66 get the key or f
19cb8 61 6c 73 65 20 74 6f 20 67 65 74 20 64 61 74 61 alse to get data
19cb9 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 . The result is
19cba 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f written.** into
19cbb 20 74 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e the pMem elemen
19cbc 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 t..**.** The pMe
19cbd 6d 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 m structure is a
19cbe 73 73 75 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 ssumed to be uni
19cbf 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 nitialized. Any
19cc0 20 70 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a prior content.*
19cc1 2a 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e * is overwritten
19cc2 20 77 69 74 68 6f 75 74 20 62 65 69 6e 67 20 66 without being f
19cc3 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 reed..**.** If t
19cc4 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c his routine fail
19cc5 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e s for any reason
19cc6 20 28 6d 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 (malloc returns
19cc7 20 4e 55 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a NULL or unable.
19cc8 2a 2a 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 ** to read from
19cc9 74 68 65 20 64 69 73 6b 29 20 74 68 65 6e 20 74 the disk) then t
19cca 68 65 20 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 he pMem is left
19ccb 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 in an inconsiste
19ccc 6e 74 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c nt state..*/.SQL
19ccd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
19cce 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
19ccf 6f 6d 42 74 72 65 65 28 0a 20 20 42 74 43 75 72 omBtree(. BtCur
19cd0 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 sor *pCur, /*
19cd1 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 Cursor pointing
19cd2 61 74 20 72 65 63 6f 72 64 20 74 6f 20 72 65 74 at record to ret
19cd3 72 69 65 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 rieve. */. int
19cd4 6f 66 66 73 65 74 2c 20 20 20 20 20 20 20 2f 2a offset, /*
19cd5 20 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 Offset from the
19cd6 20 73 74 61 72 74 20 6f 66 20 64 61 74 61 20 74 start of data t
19cd7 6f 20 72 65 74 75 72 6e 20 62 79 74 65 73 20 66 o return bytes f
19cd8 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d rom. */. int am
19cd9 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e t, /* N
19cda 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 umber of bytes t
19cdb 6f 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 o return. */. i
19cdc 6e 74 20 6b 65 79 2c 20 20 20 20 20 20 20 20 20 nt key,
19cdd 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 72 65 74 /* If true, ret
19cde 72 69 65 76 65 20 66 72 6f 6d 20 74 68 65 20 62 rieve from the b
19cdf 74 72 65 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 tree key, not da
19ce0 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d ta. */. Mem *pM
19ce1 65 6d 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 em /* OU
19ce2 54 3a 20 52 65 74 75 72 6e 20 64 61 74 61 20 69 T: Return data i
19ce3 6e 20 74 68 69 73 20 4d 65 6d 20 73 74 72 75 63 n this Mem struc
19ce4 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 ture. */.){. ch
19ce5 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 ar *zData;
19ce6 20 20 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 /* Data from t
19ce7 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a he btree layer *
19ce8 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c /. int availabl
19ce9 65 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 e = 0; /* Numbe
19cea 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c r of bytes avail
19ceb 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 able on the loca
19cec 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0a l btree page */.
19ced 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
19cee 45 5f 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 E_OK; /* Return
19cef 63 6f 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 code */.. asser
19cf0 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 t( sqlite3BtreeC
19cf1 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 ursorIsValid(pCu
19cf2 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 r) );.. /* Note
19cf3 3a 20 74 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 : the calls to B
19cf4 74 72 65 65 4b 65 79 46 65 74 63 68 28 29 20 61 treeKeyFetch() a
19cf5 6e 64 20 44 61 74 61 46 65 74 63 68 28 29 20 62 nd DataFetch() b
19cf6 65 6c 6f 77 20 61 73 73 65 72 74 28 29 20 0a 20 elow assert() .
19cf7 20 2a 2a 20 74 68 61 74 20 62 6f 74 68 20 74 68 ** that both th
19cf8 65 20 42 74 53 68 61 72 65 64 20 61 6e 64 20 64 e BtShared and d
19cf9 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d atabase handle m
19cfa 75 74 65 78 65 73 20 61 72 65 20 68 65 6c 64 2e utexes are held.
19cfb 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 */. assert( (p
19cfc 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Mem->flags & MEM
19cfd 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 _RowSet)==0 );.
19cfe 20 69 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 if( key ){.
19cff 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 zData = (char *)
19d00 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 sqlite3BtreeKeyF
19d01 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 etch(pCur, &avai
19d02 6c 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b lable);. }else{
19d03 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 . zData = (ch
19d04 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 ar *)sqlite3Btre
19d05 65 44 61 74 61 46 65 74 63 68 28 70 43 75 72 2c eDataFetch(pCur,
19d06 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 &available);.
19d07 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 }. assert( zDat
19d08 61 21 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f a!=0 );.. if( o
19d09 66 66 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c ffset+amt<=avail
19d0a 61 62 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 able && (pMem->f
19d0b 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 lags&MEM_Dyn)==0
19d0c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
19d0d 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d dbeMemRelease(pM
19d0e 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a em);. pMem->z
19d0f 20 3d 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 = &zData[offset
19d10 5d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 ];. pMem->fla
19d11 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 gs = MEM_Blob|ME
19d12 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 M_Ephem;. }else
19d13 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d if( SQLITE_OK==
19d14 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 (rc = sqlite3Vdb
19d15 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 eMemGrow(pMem, a
19d16 6d 74 2b 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 mt+2, 0)) ){.
19d17 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
19d18 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c EM_Blob|MEM_Dyn|
19d19 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d MEM_Term;. pM
19d1a 65 6d 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 em->enc = 0;.
19d1b 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 pMem->type = SQ
19d1c 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 LITE_BLOB;. i
19d1d 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 f( key ){.
19d1e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
19d1f 65 4b 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65 eKey(pCur, offse
19d20 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 t, amt, pMem->z)
19d21 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
19d22 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 rc = sqlite3B
19d23 74 72 65 65 44 61 74 61 28 70 43 75 72 2c 20 6f treeData(pCur, o
19d24 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d ffset, amt, pMem
19d25 2d 3e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ->z);. }.
19d26 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 pMem->z[amt] = 0
19d27 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d ;. pMem->z[am
19d28 74 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 t+1] = 0;. if
19d29 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
19d2a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
19d2b 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
19d2c 4d 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a Mem);. }. }.
19d2d 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b pMem->n = amt;
19d2e 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d .. return rc;.}
19d2f 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 ../* This functi
19d30 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c on is only avail
19d31 61 62 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c able internally,
19d32 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 it is not part
19d33 6f 66 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e of the.** extern
19d34 61 6c 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 al API. It works
19d35 20 69 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61 in a similar wa
19d36 79 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c y to sqlite3_val
19d37 75 65 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 ue_text(),.** ex
19d38 63 65 70 74 20 74 68 65 20 64 61 74 61 20 72 65 cept the data re
19d39 74 75 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65 turned is in the
19d3a 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 encoding specif
19d3b 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e ied by the secon
19d3c 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 d.** parameter,
19d3d 77 68 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e which must be on
19d3e 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 e of SQLITE_UTF1
19d3f 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 6BE, SQLITE_UTF1
19d40 36 4c 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 6LE or.** SQLITE
19d41 5f 55 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 _UTF8..**.** (20
19d42 30 36 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20 06-02-16:) The
19d43 65 6e 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65 enc value can be
19d44 20 6f 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49 or-ed with SQLI
19d45 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 TE_UTF16_ALIGNED
19d46 2e 0a 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20 ..** If that is
19d47 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 the case, then t
19d48 68 65 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 he result must b
19d49 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 e aligned on an
19d4a 65 76 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 even byte.** bou
19d4b 6e 64 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ndary..*/.SQLITE
19d4c 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 _PRIVATE const v
19d4d 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 oid *sqlite3Valu
19d4e 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 eText(sqlite3_va
19d4f 6c 75 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e lue* pVal, u8 en
19d50 63 29 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20 c){. if( !pVal
19d51 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 ) return 0;.. a
19d52 73 73 65 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d ssert( pVal->db=
19d53 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 =0 || sqlite3_mu
19d54 74 65 78 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 tex_held(pVal->d
19d55 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 b->mutex) );. a
19d56 73 73 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d ssert( (enc&3)==
19d57 28 65 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 (enc&~SQLITE_UTF
19d58 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 16_ALIGNED) );.
19d59 20 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e assert( (pVal->
19d5a 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 flags & MEM_RowS
19d5b 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 et)==0 );.. if(
19d5c 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d pVal->flags&MEM
19d5d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 _Null ){. ret
19d5e 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 urn 0;. }. ass
19d5f 65 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e ert( (MEM_Blob>>
19d60 33 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 3) == MEM_Str );
19d61 0a 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c . pVal->flags |
19d62 3d 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 = (pVal->flags &
19d63 20 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 MEM_Blob)>>3;.
19d64 20 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c expandBlob(pVal
19d65 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 );. if( pVal->f
19d66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a lags&MEM_Str ){.
19d67 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
19d68 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 hangeEncoding(pV
19d69 61 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 al, enc & ~SQLIT
19d6a 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 E_UTF16_ALIGNED)
19d6b 3b 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 ;. if( (enc &
19d6c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c SQLITE_UTF16_AL
19d6d 49 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d IGNED)!=0 && 1==
19d6e 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f (1&SQLITE_PTR_TO
19d6f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 _INT(pVal->z)) )
19d70 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
19d71 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 (pVal->flags & (
19d72 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 MEM_Ephem|MEM_St
19d73 61 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 atic))!=0 );.
19d74 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 if( sqlite3Vd
19d75 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
19d76 6c 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 le(pVal)!=SQLITE
19d77 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 _OK ){. r
19d78 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d eturn 0;. }
19d79 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
19d7a 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d e3VdbeMemNulTerm
19d7b 69 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d inate(pVal);. }
19d7c 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
19d7d 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d ( (pVal->flags&M
19d7e 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 EM_Blob)==0 );.
19d7f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
19d80 6d 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c mStringify(pVal,
19d81 20 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 enc);. asser
19d82 74 28 20 30 3d 3d 28 31 26 53 51 4c 49 54 45 5f t( 0==(1&SQLITE_
19d83 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d PTR_TO_INT(pVal-
19d84 3e 7a 29 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73 >z)) );. }. as
19d85 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d sert(pVal->enc==
19d86 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 (enc & ~SQLITE_U
19d87 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c TF16_ALIGNED) ||
19d88 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20 pVal->db==0.
19d89 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56 || pV
19d8a 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 al->db->mallocFa
19d8b 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56 iled );. if( pV
19d8c 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 al->enc==(enc &
19d8d 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c ~SQLITE_UTF16_AL
19d8e 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65 IGNED) ){. re
19d8f 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20 turn pVal->z;.
19d90 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 }else{. retur
19d91 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a n 0;. }.}../*.*
19d92 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 * Create a new s
19d93 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a qlite3_value obj
19d94 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ect..*/.SQLITE_P
19d95 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 RIVATE sqlite3_v
19d96 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c alue *sqlite3Val
19d97 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64 ueNew(sqlite3 *d
19d98 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73 b){. Mem *p = s
19d99 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
19d9a 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 ro(db, sizeof(*p
19d9b 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 ));. if( p ){.
19d9c 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 p->flags = ME
19d9d 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74 M_Null;. p->t
19d9e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c ype = SQLITE_NUL
19d9f 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64 L;. p->db = d
19da0 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 b;. }. return
19da1 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 p;.}../*.** Crea
19da2 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 te a new sqlite3
19da3 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63 _value object, c
19da4 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 ontaining the va
19da5 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a lue of pExpr..**
19da6 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f .** This only wo
19da7 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d rks for very sim
19da8 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 ple expressions
19da9 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 that consist of
19daa 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 one constant.**
19dab 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c token (i.e. "5",
19dac 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69 "5.1", "'a stri
19dad 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 ng'"). If the ex
19dae 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20 pression can.**
19daf 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 be converted dir
19db0 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c ectly into a val
19db1 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c ue, then the val
19db2 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 ue is allocated
19db3 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 and.** a pointer
19db4 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56 written to *ppV
19db5 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69 al. The caller i
19db6 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f s responsible fo
19db7 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a r deallocating.*
19db8 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 20 70 * the value by p
19db9 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c assing it to sql
19dba 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29 20 ite3ValueFree()
19dbb 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65 later on. If the
19dbc 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 expression.** c
19dbd 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 annot be convert
19dbe 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 ed to a value, t
19dbf 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65 hen *ppVal is se
19dc0 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 t to NULL..*/.SQ
19dc1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
19dc2 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f sqlite3ValueFro
19dc3 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 mExpr(. sqlite3
19dc4 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 *db,
19dc5 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
19dc6 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
19dc7 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 . Expr *pExpr,
19dc8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
19dc9 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 The expression t
19dca 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 20 o evaluate */.
19dcb 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 u8 enc,
19dcc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 /* Enc
19dcd 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a oding to use */.
19dce 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 20 u8 affinity,
19dcf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
19dd0 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a ffinity to use *
19dd1 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 /. sqlite3_valu
19dd2 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f 2a e **ppVal /*
19dd3 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 76 Write the new v
19dd4 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a alue here */.){.
19dd5 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 72 int op;. char
19dd6 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 71 *zVal = 0;. sq
19dd7 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
19dd8 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 70 l = 0;.. if( !p
19dd9 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 56 Expr ){. *ppV
19dda 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 al = 0;. retu
19ddb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 rn SQLITE_OK;.
19ddc 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e }. op = pExpr->
19ddd 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b op;. if( op==TK
19dde 5f 52 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 _REGISTER ){.
19ddf 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 32 op = pExpr->op2
19de0 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 ; /* This only
19de1 68 61 70 70 65 6e 73 20 77 69 74 68 20 53 51 4c happens with SQL
19de2 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 ITE_ENABLE_STAT2
19de3 20 2a 2f 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6f */. }.. if( o
19de4 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 p==TK_STRING ||
19de5 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 op==TK_FLOAT ||
19de6 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 op==TK_INTEGER )
19de7 7b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c {. pVal = sql
19de8 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 ite3ValueNew(db)
19de9 3b 0a 20 20 20 20 69 66 28 20 70 56 61 6c 3d 3d ;. if( pVal==
19dea 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0 ) goto no_mem;
19deb 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 . if( ExprHas
19dec 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
19ded 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 7b 0a EP_IntValue) ){.
19dee 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
19def 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 56 61 eMemSetInt64(pVa
19df0 6c 2c 20 28 69 36 34 29 70 45 78 70 72 2d 3e 75 l, (i64)pExpr->u
19df1 2e 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 7d 65 .iValue);. }e
19df2 6c 73 65 7b 0a 20 20 20 20 20 20 7a 56 61 6c 20 lse{. zVal
19df3 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
19df4 70 28 64 62 2c 20 70 45 78 70 72 2d 3e 75 2e 7a p(db, pExpr->u.z
19df5 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 69 66 Token);. if
19df6 28 20 7a 56 61 6c 3d 3d 30 20 29 20 67 6f 74 6f ( zVal==0 ) goto
19df7 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 73 no_mem;. s
19df8 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 qlite3ValueSetSt
19df9 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c r(pVal, -1, zVal
19dfa 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 , SQLITE_UTF8, S
19dfb 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a QLITE_DYNAMIC);.
19dfc 20 20 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b if( op==TK
19dfd 5f 46 4c 4f 41 54 20 29 20 70 56 61 6c 2d 3e 74 _FLOAT ) pVal->t
19dfe 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f ype = SQLITE_FLO
19dff 41 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 AT;. }. if
19e00 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 ( (op==TK_INTEGE
19e01 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 R || op==TK_FLOA
19e02 54 20 29 20 26 26 20 61 66 66 69 6e 69 74 79 3d T ) && affinity=
19e03 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 =SQLITE_AFF_NONE
19e04 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
19e05 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 3ValueApplyAffin
19e06 69 74 79 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 ity(pVal, SQLITE
19e07 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 53 51 _AFF_NUMERIC, SQ
19e08 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 LITE_UTF8);.
19e09 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
19e0a 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 ite3ValueApplyAf
19e0b 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 61 66 66 finity(pVal, aff
19e0c 69 6e 69 74 79 2c 20 53 51 4c 49 54 45 5f 55 54 inity, SQLITE_UT
19e0d 46 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 F8);. }. i
19e0e 66 28 20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 f( enc!=SQLITE_U
19e0f 54 46 38 20 29 7b 0a 20 20 20 20 20 20 73 71 6c TF8 ){. sql
19e10 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
19e11 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 coding(pVal, enc
19e12 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
19e13 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e if( op==TK_UMIN
19e14 55 53 20 29 20 7b 0a 20 20 20 20 69 66 28 20 53 US ) {. if( S
19e15 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 QLITE_OK==sqlite
19e16 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 64 3ValueFromExpr(d
19e17 62 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 65 b,pExpr->pLeft,e
19e18 6e 63 2c 61 66 66 69 6e 69 74 79 2c 26 70 56 61 nc,affinity,&pVa
19e19 6c 29 20 29 7b 0a 20 20 20 20 20 20 70 56 61 6c l) ){. pVal
19e1a 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a 20 70 56 61 ->u.i = -1 * pVa
19e1b 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 2f 2a l->u.i;. /*
19e1c 20 28 64 6f 75 62 6c 65 29 2d 31 20 49 6e 20 63 (double)-1 In c
19e1d 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d ase of SQLITE_OM
19e1e 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e IT_FLOATING_POIN
19e1f 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 56 T... */. pV
19e20 61 6c 2d 3e 72 20 3d 20 28 64 6f 75 62 6c 65 29 al->r = (double)
19e21 2d 31 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 -1 * pVal->r;.
19e22 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 }. }.#ifndef
19e23 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 SQLITE_OMIT_BLOB
19e24 5f 4c 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 _LITERAL. else
19e25 69 66 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 if( op==TK_BLOB
19e26 29 7b 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b ){. int nVal;
19e27 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 . assert( pEx
19e28 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d pr->u.zToken[0]=
19e29 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 ='x' || pExpr->u
19e2a 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 .zToken[0]=='X'
19e2b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 );. assert( p
19e2c 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 Expr->u.zToken[1
19e2d 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 70 ]=='\'' );. p
19e2e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c Val = sqlite3Val
19e2f 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 69 ueNew(db);. i
19e30 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74 6f 20 f( !pVal ) goto
19e31 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7a 56 61 6c no_mem;. zVal
19e32 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e 7a 54 6f = &pExpr->u.zTo
19e33 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 6e 56 61 6c ken[2];. nVal
19e34 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
19e35 33 30 28 7a 56 61 6c 29 2d 31 3b 0a 20 20 20 20 30(zVal)-1;.
19e36 61 73 73 65 72 74 28 20 7a 56 61 6c 5b 6e 56 61 assert( zVal[nVa
19e37 6c 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 l]=='\'' );.
19e38 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
19e39 74 53 74 72 28 70 56 61 6c 2c 20 73 71 6c 69 74 tStr(pVal, sqlit
19e3a 65 33 48 65 78 54 6f 42 6c 6f 62 28 64 62 2c 20 e3HexToBlob(db,
19e3b 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 zVal, nVal), nVa
19e3c 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20 l/2,.
19e3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0,
19e3e 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
19e3f 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
19e40 69 66 28 20 70 56 61 6c 20 29 7b 0a 20 20 20 20 if( pVal ){.
19e41 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 sqlite3VdbeMemSt
19e42 6f 72 65 54 79 70 65 28 70 56 61 6c 29 3b 0a 20 oreType(pVal);.
19e43 20 7d 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 }. *ppVal = pV
19e44 61 6c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c al;. return SQL
19e45 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a ITE_OK;..no_mem:
19e46 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 . db->mallocFai
19e47 6c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 led = 1;. sqlit
19e48 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 e3DbFree(db, zVa
19e49 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c l);. sqlite3Val
19e4a 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 ueFree(pVal);.
19e4b 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 *ppVal = 0;. re
19e4c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 turn SQLITE_NOME
19e4d 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e M;.}../*.** Chan
19e4e 67 65 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 ge the string va
19e4f 6c 75 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 lue of an sqlite
19e50 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 3_value object.*
19e51 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
19e52 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c void sqlite3Val
19e53 75 65 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 ueSetStr(. sqli
19e54 74 65 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 te3_value *v,
19e55 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 /* Value to be
19e56 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c set */. int n,
19e57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e58 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 /* Length of str
19e59 69 6e 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 ing z */. const
19e5a 20 76 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 void *z,
19e5b 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 /* Text of the
19e5c 6e 65 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 new string */.
19e5d 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 u8 enc,
19e5e 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e /* Encodin
19e5f 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f g to use */. vo
19e60 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a id (*xDel)(void*
19e61 29 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f ) /* Destructo
19e62 72 20 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 r for the string
19e63 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 */.){. if( v )
19e64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
19e65 65 74 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 etStr((Mem *)v,
19e66 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 z, n, enc, xDel)
19e67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
19e68 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 an sqlite3_value
19e69 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 object.*/.SQLIT
19e6a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
19e6b 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 qlite3ValueFree(
19e6c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 sqlite3_value *v
19e6d 29 7b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 ){. if( !v ) re
19e6e 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 turn;. sqlite3V
19e6f 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d dbeMemRelease((M
19e70 65 6d 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 em *)v);. sqlit
19e71 65 33 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 e3DbFree(((Mem*)
19e72 76 29 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f v)->db, v);.}../
19e73 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
19e74 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
19e75 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 in the sqlite3_v
19e76 61 6c 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 alue object assu
19e77 6d 69 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 ming.** that it
19e78 75 73 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e uses the encodin
19e79 67 20 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 g "enc".*/.SQLIT
19e7a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
19e7b 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 lite3ValueBytes(
19e7c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
19e7d 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 Val, u8 enc){.
19e7e 4d 65 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 Mem *p = (Mem*)p
19e7f 56 61 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 Val;. if( (p->f
19e80 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 lags & MEM_Blob)
19e81 21 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 !=0 || sqlite3Va
19e82 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e lueText(pVal, en
19e83 63 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d c) ){. if( p-
19e84 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 >flags & MEM_Zer
19e85 6f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 o ){. retur
19e86 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e 75 2e 6e 5a n p->n + p->u.nZ
19e87 65 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ero;. }else{.
19e88 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e return p->
19e89 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 n;. }. }. r
19e8a 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a eturn 0;.}../***
19e8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
19e8c 6f 66 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a of vdbemem.c ***
19e8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
19e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 *********** Begi
19e91 6e 20 66 69 6c 65 20 76 64 62 65 61 75 78 2e 63 n file vdbeaux.c
19e92 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
19e93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19e94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a **********/./*.*
19e95 2a 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 * 2003 September
19e96 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6.**.** The aut
19e97 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f hor disclaims co
19e98 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 pyright to this
19e99 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e source code. In
19e9a 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c place of.** a l
19e9b 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 egal notice, her
19e9c 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a e is a blessing:
19e9d 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f .**.** May yo
19e9e 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f u do good and no
19e9f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 t evil..** Ma
19ea0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 y you find forgi
19ea1 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 veness for yours
19ea2 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 elf and forgive
19ea3 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 others..** Ma
19ea4 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 y you share free
19ea5 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 ly, never taking
19ea6 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 more than you g
19ea7 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a ive..**.********
19ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19ea9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
19eac 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 *.** This file c
19ead 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 ontains code use
19eae 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 2c 20 d for creating,
19eaf 64 65 73 74 72 6f 79 69 6e 67 2c 20 61 6e 64 20 destroying, and
19eb0 70 6f 70 75 6c 61 74 69 6e 67 0a 2a 2a 20 61 20 populating.** a
19eb1 56 44 42 45 20 28 6f 72 20 61 6e 20 22 73 71 6c VDBE (or an "sql
19eb2 69 74 65 33 5f 73 74 6d 74 22 20 61 73 20 69 74 ite3_stmt" as it
19eb3 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 74 68 65 is known to the
19eb4 20 6f 75 74 73 69 64 65 20 77 6f 72 6c 64 2e 29 outside world.)
19eb5 20 20 50 72 69 6f 72 0a 2a 2a 20 74 6f 20 76 65 Prior.** to ve
19eb6 72 73 69 6f 6e 20 32 2e 38 2e 37 2c 20 61 6c 6c rsion 2.8.7, all
19eb7 20 74 68 69 73 20 63 6f 64 65 20 77 61 73 20 63 this code was c
19eb8 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 ombined into the
19eb9 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20 66 vdbe.c source f
19eba 69 6c 65 2e 0a 2a 2a 20 42 75 74 20 74 68 61 74 ile..** But that
19ebb 20 66 69 6c 65 20 77 61 73 20 67 65 74 74 69 6e file was gettin
19ebc 67 20 74 6f 6f 20 62 69 67 20 73 6f 20 74 68 69 g too big so thi
19ebd 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 77 65 s subroutines we
19ebe 72 65 20 73 70 6c 69 74 20 6f 75 74 2e 0a 2a 2f re split out..*/
19ebf 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 ..../*.** When d
19ec0 65 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 ebugging the cod
19ec1 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 e generator in a
19ec2 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 symbolic debugg
19ec3 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 er, one can.** s
19ec4 65 74 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 et the sqlite3Vd
19ec5 62 65 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 beAddopTrace to
19ec6 31 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 1 and all opcode
19ec7 73 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 s will be printe
19ec8 64 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 d.** as they are
19ec9 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e added to the in
19eca 73 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d struction stream
19ecb 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 ..*/.#ifdef SQLI
19ecc 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f TE_DEBUG.SQLITE_
19ecd 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
19ece 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 te3VdbeAddopTrac
19ecf 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a e = 0;.#endif...
19ed0 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e /*.** Create a n
19ed1 65 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 ew virtual datab
19ed2 61 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 ase engine..*/.S
19ed3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 QLITE_PRIVATE Vd
19ed4 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 be *sqlite3VdbeC
19ed5 72 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 reate(sqlite3 *d
19ed6 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 b){. Vdbe *p;.
19ed7 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 p = sqlite3DbMa
19ed8 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
19ed9 65 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 eof(Vdbe) );. i
19eda 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
19edb 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 0;. p->db = db
19edc 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 ;. if( db->pVdb
19edd 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 e ){. db->pVd
19ede 62 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 be->pPrev = p;.
19edf 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 }. p->pNext =
19ee0 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e db->pVdbe;. p->
19ee1 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d pPrev = 0;. db-
19ee2 3e 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d >pVdbe = p;. p-
19ee3 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 >magic = VDBE_MA
19ee4 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 GIC_INIT;. retu
19ee5 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 rn p;.}../*.** R
19ee6 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 emember the SQL
19ee7 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 string for a pre
19ee8 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e pared statement.
19ee9 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19eea 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
19eeb 64 62 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a dbeSetSql(Vdbe *
19eec 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a p, const char *z
19eed 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 , int n, int isP
19eee 72 65 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73 repareV2){. ass
19eef 65 72 74 28 20 69 73 50 72 65 70 61 72 65 56 32 ert( isPrepareV2
19ef0 3d 3d 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65 ==1 || isPrepare
19ef1 56 32 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 V2==0 );. if( p
19ef2 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 ==0 ) return;.#i
19ef3 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
19ef4 5f 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 _TRACE. if( !is
19ef5 50 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75 PrepareV2 ) retu
19ef6 72 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 rn;.#endif. ass
19ef7 65 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 ert( p->zSql==0
19ef8 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 );. p->zSql = s
19ef9 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 qlite3DbStrNDup(
19efa 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 p->db, z, n);.
19efb 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d p->isPrepareV2 =
19efc 20 28 75 38 29 69 73 50 72 65 70 61 72 65 56 32 (u8)isPrepareV2
19efd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
19efe 6e 20 74 68 65 20 53 51 4c 20 61 73 73 6f 63 69 n the SQL associ
19eff 61 74 65 64 20 77 69 74 68 20 61 20 70 72 65 70 ated with a prep
19f00 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a ared statement.*
19f01 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e /.SQLITE_API con
19f02 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 st char *sqlite3
19f03 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d _sql(sqlite3_stm
19f04 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 t *pStmt){. Vdb
19f05 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 e *p = (Vdbe *)p
19f06 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 Stmt;. return (
19f07 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3f p->isPrepareV2 ?
19f08 20 70 2d 3e 7a 53 71 6c 20 3a 20 30 29 3b 0a 7d p->zSql : 0);.}
19f09 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 61 6c 6c ../*.** Swap all
19f0a 20 63 6f 6e 74 65 6e 74 20 62 65 74 77 65 65 6e content between
19f0b 20 74 77 6f 20 56 44 42 45 20 73 74 72 75 63 74 two VDBE struct
19f0c 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ures..*/.SQLITE_
19f0d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
19f0e 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 ite3VdbeSwap(Vdb
19f0f 65 20 2a 70 41 2c 20 56 64 62 65 20 2a 70 42 29 e *pA, Vdbe *pB)
19f10 7b 0a 20 20 56 64 62 65 20 74 6d 70 2c 20 2a 70 {. Vdbe tmp, *p
19f11 54 6d 70 3b 0a 20 20 63 68 61 72 20 2a 7a 54 6d Tmp;. char *zTm
19f12 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70 41 3b 0a p;. tmp = *pA;.
19f13 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a 20 20 2a *pA = *pB;. *
19f14 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70 54 6d 70 pB = tmp;. pTmp
19f15 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b 0a 20 20 = pA->pNext;.
19f16 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70 42 2d 3e pA->pNext = pB->
19f17 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e 70 4e 65 pNext;. pB->pNe
19f18 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20 70 54 6d xt = pTmp;. pTm
19f19 70 20 3d 20 70 41 2d 3e 70 50 72 65 76 3b 0a 20 p = pA->pPrev;.
19f1a 20 70 41 2d 3e 70 50 72 65 76 20 3d 20 70 42 2d pA->pPrev = pB-
19f1b 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d 3e 70 50 >pPrev;. pB->pP
19f1c 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20 20 7a 54 rev = pTmp;. zT
19f1d 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c 3b 0a 20 mp = pA->zSql;.
19f1e 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70 42 2d 3e pA->zSql = pB->
19f1f 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a 53 71 6c zSql;. pB->zSql
19f20 20 3d 20 7a 54 6d 70 3b 0a 20 20 70 42 2d 3e 69 = zTmp;. pB->i
19f21 73 50 72 65 70 61 72 65 56 32 20 3d 20 70 41 2d sPrepareV2 = pA-
19f22 3e 69 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a >isPrepareV2;.}.
19f23 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
19f24 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 EBUG./*.** Turn
19f25 74 72 61 63 69 6e 67 20 6f 6e 20 6f 72 20 6f 66 tracing on or of
19f26 66 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 f.*/.SQLITE_PRIV
19f27 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
19f28 56 64 62 65 54 72 61 63 65 28 56 64 62 65 20 2a VdbeTrace(Vdbe *
19f29 70 2c 20 46 49 4c 45 20 2a 74 72 61 63 65 29 7b p, FILE *trace){
19f2a 0a 20 20 70 2d 3e 74 72 61 63 65 20 3d 20 74 72 . p->trace = tr
19f2b 61 63 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f ace;.}.#endif../
19f2c 2a 0a 2a 2a 20 52 65 73 69 7a 65 20 74 68 65 20 *.** Resize the
19f2d 56 64 62 65 2e 61 4f 70 20 61 72 72 61 79 20 73 Vdbe.aOp array s
19f2e 6f 20 74 68 61 74 20 69 74 20 69 73 20 61 74 20 o that it is at
19f2f 6c 65 61 73 74 20 6f 6e 65 20 6f 70 20 6c 61 72 least one op lar
19f30 67 65 72 20 74 68 61 6e 20 0a 2a 2a 20 69 74 20 ger than .** it
19f31 77 61 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e was..**.** If an
19f32 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 out-of-memory e
19f33 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c rror occurs whil
19f34 65 20 72 65 73 69 7a 69 6e 67 20 74 68 65 20 61 e resizing the a
19f35 72 72 61 79 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 rray, return.**
19f36 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 6e SQLITE_NOMEM. In
19f37 20 74 68 69 73 20 63 61 73 65 20 56 64 62 65 2e this case Vdbe.
19f38 61 4f 70 20 61 6e 64 20 56 64 62 65 2e 6e 4f 70 aOp and Vdbe.nOp
19f39 41 6c 6c 6f 63 20 72 65 6d 61 69 6e 20 0a 2a 2a Alloc remain .**
19f3a 20 75 6e 63 68 61 6e 67 65 64 20 28 74 68 69 73 unchanged (this
19f3b 20 69 73 20 73 6f 20 74 68 61 74 20 61 6e 79 20 is so that any
19f3c 6f 70 63 6f 64 65 73 20 61 6c 72 65 61 64 79 20 opcodes already
19f3d 61 6c 6c 6f 63 61 74 65 64 20 63 61 6e 20 62 65 allocated can be
19f3e 20 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 64 .** correctly d
19f3f 65 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 eallocated along
19f40 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f with the rest o
19f41 66 20 74 68 65 20 56 64 62 65 29 2e 0a 2a 2f 0a f the Vdbe)..*/.
19f42 73 74 61 74 69 63 20 69 6e 74 20 67 72 6f 77 4f static int growO
19f43 70 41 72 72 61 79 28 56 64 62 65 20 2a 70 29 7b pArray(Vdbe *p){
19f44 0a 20 20 56 64 62 65 4f 70 20 2a 70 4e 65 77 3b . VdbeOp *pNew;
19f45 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 70 . int nNew = (p
19f46 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3f 20 70 2d 3e ->nOpAlloc ? p->
19f47 6e 4f 70 41 6c 6c 6f 63 2a 32 20 3a 20 28 69 6e nOpAlloc*2 : (in
19f48 74 29 28 31 30 32 34 2f 73 69 7a 65 6f 66 28 4f t)(1024/sizeof(O
19f49 70 29 29 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 p)));. pNew = s
19f4a 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 qlite3DbRealloc(
19f4b 70 2d 3e 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 6e p->db, p->aOp, n
19f4c 4e 65 77 2a 73 69 7a 65 6f 66 28 4f 70 29 29 3b New*sizeof(Op));
19f4d 0a 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 . if( pNew ){.
19f4e 20 20 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 3d p->nOpAlloc =
19f4f 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
19f50 53 69 7a 65 28 70 2d 3e 64 62 2c 20 70 4e 65 77 Size(p->db, pNew
19f51 29 2f 73 69 7a 65 6f 66 28 4f 70 29 3b 0a 20 20 )/sizeof(Op);.
19f52 20 20 70 2d 3e 61 4f 70 20 3d 20 70 4e 65 77 3b p->aOp = pNew;
19f53 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 . }. return (p
19f54 4e 65 77 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 New ? SQLITE_OK
19f55 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b : SQLITE_NOMEM);
19f56 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 .}../*.** Add a
19f57 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 new instruction
19f58 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 to the list of i
19f59 6e 73 74 72 75 63 74 69 6f 6e 73 20 63 75 72 72 nstructions curr
19f5a 65 6e 74 20 69 6e 20 74 68 65 0a 2a 2a 20 56 44 ent in the.** VD
19f5b 42 45 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 BE. Return the
19f5c 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e address of the n
19f5d 65 77 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a ew instruction..
19f5e 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73 **.** Parameters
19f5f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 20 20 20 20 :.**.** p
19f60 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74 Point
19f61 65 72 20 74 6f 20 74 68 65 20 56 44 42 45 0a 2a er to the VDBE.*
19f62 2a 0a 2a 2a 20 20 20 20 6f 70 20 20 20 20 20 20 *.** op
19f63 20 20 20 20 20 20 20 20 54 68 65 20 6f 70 63 6f The opco
19f64 64 65 20 66 6f 72 20 74 68 69 73 20 69 6e 73 74 de for this inst
19f65 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 ruction.**.**
19f66 20 70 31 2c 20 70 32 2c 20 70 33 20 20 20 20 20 p1, p2, p3
19f67 20 4f 70 65 72 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 Operands.**.**
19f68 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 56 Use the sqlite3V
19f69 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
19f6a 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69 ) function to fi
19f6b 78 20 61 6e 20 61 64 64 72 65 73 73 20 61 6e 64 x an address and
19f6c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 56 .** the sqlite3V
19f6d 64 62 65 43 68 61 6e 67 65 50 34 28 29 20 66 75 dbeChangeP4() fu
19f6e 6e 63 74 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 nction to change
19f6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
19f70 65 20 50 34 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e e P4.** operand.
19f71 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
19f72 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
19f73 62 65 41 64 64 4f 70 33 28 56 64 62 65 20 2a 70 beAddOp3(Vdbe *p
19f74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 , int op, int p1
19f75 2c 20 69 6e 74 20 70 32 2c 20 69 6e 74 20 70 33 , int p2, int p3
19f76 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 56 64 ){. int i;. Vd
19f77 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a 20 20 69 20 beOp *pOp;.. i
19f78 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 61 73 73 65 = p->nOp;. asse
19f79 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 rt( p->magic==VD
19f7a 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b BE_MAGIC_INIT );
19f7b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 30 20 . assert( op>0
19f7c 26 26 20 6f 70 3c 30 78 66 66 20 29 3b 0a 20 20 && op<0xff );.
19f7d 69 66 28 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c if( p->nOpAlloc<
19f7e 3d 69 20 29 7b 0a 20 20 20 20 69 66 28 20 67 72 =i ){. if( gr
19f7f 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a owOpArray(p) ){.
19f80 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
19f81 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d 3e 6e }. }. p->n
19f82 4f 70 2b 2b 3b 0a 20 20 70 4f 70 20 3d 20 26 70 Op++;. pOp = &p
19f83 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 70 4f 70 2d ->aOp[i];. pOp-
19f84 3e 6f 70 63 6f 64 65 20 3d 20 28 75 38 29 6f 70 >opcode = (u8)op
19f85 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 3d 20 30 3b ;. pOp->p5 = 0;
19f86 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 31 3b . pOp->p1 = p1;
19f87 0a 20 20 70 4f 70 2d 3e 70 32 20 3d 20 70 32 3b . pOp->p2 = p2;
19f88 0a 20 20 70 4f 70 2d 3e 70 33 20 3d 20 70 33 3b . pOp->p3 = p3;
19f89 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 . pOp->p4.p = 0
19f8a 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 ;. pOp->p4type
19f8b 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 = P4_NOTUSED;.
19f8c 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a p->expired = 0;.
19f8d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 #ifdef SQLITE_DE
19f8e 42 55 47 0a 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d BUG. pOp->zComm
19f8f 65 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 ent = 0;. if( s
19f90 71 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 qlite3VdbeAddopT
19f91 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 race ) sqlite3Vd
19f92 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 69 2c 20 bePrintOp(0, i,
19f93 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 23 65 6e &p->aOp[i]);.#en
19f94 64 69 66 0a 23 69 66 64 65 66 20 56 44 42 45 5f dif.#ifdef VDBE_
19f95 50 52 4f 46 49 4c 45 0a 20 20 70 4f 70 2d 3e 63 PROFILE. pOp->c
19f96 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 70 4f 70 ycles = 0;. pOp
19f97 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 ->cnt = 0;.#endi
19f98 66 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a f. return i;.}.
19f99 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
19f9a 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 nt sqlite3VdbeAd
19f9b 64 4f 70 30 28 56 64 62 65 20 2a 70 2c 20 69 6e dOp0(Vdbe *p, in
19f9c 74 20 6f 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 t op){. return
19f9d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
19f9e 33 28 70 2c 20 6f 70 2c 20 30 2c 20 30 2c 20 30 3(p, op, 0, 0, 0
19f9f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 );.}.SQLITE_PRIV
19fa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
19fa1 64 62 65 41 64 64 4f 70 31 28 56 64 62 65 20 2a dbeAddOp1(Vdbe *
19fa2 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 p, int op, int p
19fa3 31 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 1){. return sql
19fa4 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 ite3VdbeAddOp3(p
19fa5 2c 20 6f 70 2c 20 70 31 2c 20 30 2c 20 30 29 3b , op, p1, 0, 0);
19fa6 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 .}.SQLITE_PRIVAT
19fa7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
19fa8 65 41 64 64 4f 70 32 28 56 64 62 65 20 2a 70 2c eAddOp2(Vdbe *p,
19fa9 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 70 31 2c int op, int p1,
19faa 20 69 6e 74 20 70 32 29 7b 0a 20 20 72 65 74 75 int p2){. retu
19fab 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 rn sqlite3VdbeAd
19fac 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 dOp3(p, op, p1,
19fad 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a p2, 0);.}.../*.*
19fae 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 * Add an opcode
19faf 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 that includes th
19fb0 65 20 70 34 20 76 61 6c 75 65 20 61 73 20 61 20 e p4 value as a
19fb1 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 pointer..*/.SQLI
19fb2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
19fb3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
19fb4 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 (. Vdbe *p,
19fb5 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 /* Add t
19fb6 68 65 20 6f 70 63 6f 64 65 20 74 6f 20 74 68 69 he opcode to thi
19fb7 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 s VM */. int op
19fb8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
19fb9 20 54 68 65 20 6e 65 77 20 6f 70 63 6f 64 65 20 The new opcode
19fba 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 20 20 20 */. int p1,
19fbb 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
19fbc 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 P1 operand */.
19fbd 69 6e 74 20 70 32 2c 20 20 20 20 20 20 20 20 20 int p2,
19fbe 20 20 20 20 2f 2a 20 54 68 65 20 50 32 20 6f 70 /* The P2 op
19fbf 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 erand */. int p
19fc0 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 3, /
19fc1 2a 20 54 68 65 20 50 33 20 6f 70 65 72 61 6e 64 * The P3 operand
19fc2 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
19fc3 20 2a 7a 50 34 2c 20 20 20 20 2f 2a 20 54 68 65 *zP4, /* The
19fc4 20 50 34 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 P4 operand */.
19fc5 20 69 6e 74 20 70 34 74 79 70 65 20 20 20 20 20 int p4type
19fc6 20 20 20 20 20 2f 2a 20 50 34 20 6f 70 65 72 61 /* P4 opera
19fc7 6e 64 20 74 79 70 65 20 2a 2f 0a 29 7b 0a 20 20 nd type */.){.
19fc8 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 int addr = sqlit
19fc9 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20 e3VdbeAddOp3(p,
19fca 6f 70 2c 20 70 31 2c 20 70 32 2c 20 70 33 29 3b op, p1, p2, p3);
19fcb 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
19fcc 61 6e 67 65 50 34 28 70 2c 20 61 64 64 72 2c 20 angeP4(p, addr,
19fcd 7a 50 34 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 zP4, p4type);.
19fce 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a return addr;.}..
19fcf 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 /*.** Add an opc
19fd0 6f 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 ode that include
19fd1 73 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 s the p4 value a
19fd2 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f s an integer..*/
19fd3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
19fd4 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
19fd5 64 64 4f 70 34 49 6e 74 28 0a 20 20 56 64 62 65 ddOp4Int(. Vdbe
19fd6 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 *p,
19fd7 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64 /* Add the opcod
19fd8 65 20 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a e to this VM */.
19fd9 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 int op,
19fda 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 /* The new
19fdb 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 opcode */. int
19fdc 20 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 p1,
19fdd 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61 /* The P1 opera
19fde 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20 nd */. int p2,
19fdf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
19fe0 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f he P2 operand */
19fe1 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20 . int p3,
19fe2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 /* The P3
19fe3 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e operand */. in
19fe4 74 20 70 34 20 20 20 20 20 20 20 20 20 20 20 20 t p4
19fe5 20 20 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72 /* The P4 oper
19fe6 61 6e 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65 and as an intege
19fe7 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 r */.){. int ad
19fe8 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
19fe9 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 AddOp3(p, op, p1
19fea 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c , p2, p3);. sql
19feb 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
19fec 28 70 2c 20 61 64 64 72 2c 20 53 51 4c 49 54 45 (p, addr, SQLITE
19fed 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 70 34 29 2c _INT_TO_PTR(p4),
19fee 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 72 65 P4_INT32);. re
19fef 74 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a turn addr;.}../*
19ff0 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 .** Create a new
19ff1 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 symbolic label
19ff2 66 6f 72 20 61 6e 20 69 6e 73 74 72 75 63 74 69 for an instructi
19ff3 6f 6e 20 74 68 61 74 20 68 61 73 20 79 65 74 20 on that has yet
19ff4 74 6f 20 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 to be.** coded.
19ff5 20 54 68 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 The symbolic la
19ff6 62 65 6c 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 bel is really ju
19ff7 73 74 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 st a negative nu
19ff8 6d 62 65 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 mber. The.** la
19ff9 62 65 6c 20 63 61 6e 20 62 65 20 75 73 65 64 20 bel can be used
19ffa 61 73 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 as the P2 value
19ffb 6f 66 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e of an operation.
19ffc 20 20 4c 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a Later, when.**
19ffd 20 74 68 65 20 6c 61 62 65 6c 20 69 73 20 72 65 the label is re
19ffe 73 6f 6c 76 65 64 20 74 6f 20 61 20 73 70 65 63 solved to a spec
19fff 69 66 69 63 20 61 64 64 72 65 73 73 2c 20 74 68 ific address, th
1a000 65 20 56 44 42 45 20 77 69 6c 6c 20 73 63 61 6e e VDBE will scan
1a001 0a 2a 2a 20 74 68 72 6f 75 67 68 20 69 74 73 20 .** through its
1a002 6f 70 65 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 operation list a
1a003 6e 64 20 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 nd change all va
1a004 6c 75 65 73 20 6f 66 20 50 32 20 77 68 69 63 68 lues of P2 which
1a005 20 6d 61 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 match.** the la
1a006 62 65 6c 20 69 6e 74 6f 20 74 68 65 20 72 65 73 bel into the res
1a007 6f 6c 76 65 64 20 61 64 64 72 65 73 73 2e 0a 2a olved address..*
1a008 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 6b 6e *.** The VDBE kn
1a009 6f 77 73 20 74 68 61 74 20 61 20 50 32 20 76 61 ows that a P2 va
1a00a 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 20 62 lue is a label b
1a00b 65 63 61 75 73 65 20 6c 61 62 65 6c 73 20 61 72 ecause labels ar
1a00c 65 0a 2a 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 e.** always nega
1a00d 74 69 76 65 20 61 6e 64 20 50 32 20 76 61 6c 75 tive and P2 valu
1a00e 65 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74 es are suppose t
1a00f 6f 20 62 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 o be non-negativ
1a010 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e e..** Hence, a n
1a011 65 67 61 74 69 76 65 20 50 32 20 76 61 6c 75 65 egative P2 value
1a012 20 69 73 20 61 20 6c 61 62 65 6c 20 74 68 61 74 is a label that
1a013 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 20 72 has yet to be r
1a014 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a esolved..**.** Z
1a015 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 20 ero is returned
1a016 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 if a malloc() fa
1a017 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ils..*/.SQLITE_P
1a018 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 RIVATE int sqlit
1a019 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
1a01a 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 Vdbe *p){. int
1a01b 69 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 i;. i = p->nLab
1a01c 65 6c 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 el++;. assert(
1a01d 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d p->magic==VDBE_M
1a01e 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 AGIC_INIT );. i
1a01f 66 28 20 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 f( i>=p->nLabelA
1a020 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 lloc ){. int
1a021 6e 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c n = p->nLabelAll
1a022 6f 63 2a 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d oc*2 + 5;. p-
1a023 3e 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 >aLabel = sqlite
1a024 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 3DbReallocOrFree
1a025 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 (p->db, p->aLabe
1a026 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 l,.
1a027 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a028 20 20 20 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 n*size
1a029 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 of(p->aLabel[0])
1a02a 29 3b 0a 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c );. p->nLabel
1a02b 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 Alloc = sqlite3D
1a02c 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 bMallocSize(p->d
1a02d 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 b, p->aLabel)/si
1a02e 7a 65 6f 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 zeof(p->aLabel[0
1a02f 5d 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d ]);. }. if( p-
1a030 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 >aLabel ){. p
1a031 2d 3e 61 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 ->aLabel[i] = -1
1a032 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d ;. }. return -
1a033 31 2d 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 1-i;.}../*.** Re
1a034 73 6f 6c 76 65 20 6c 61 62 65 6c 20 22 78 22 20 solve label "x"
1a035 74 6f 20 62 65 20 74 68 65 20 61 64 64 72 65 73 to be the addres
1a036 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e s of the next in
1a037 73 74 72 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 struction to.**
1a038 62 65 20 69 6e 73 65 72 74 65 64 2e 20 20 54 68 be inserted. Th
1a039 65 20 70 61 72 61 6d 65 74 65 72 20 22 78 22 20 e parameter "x"
1a03a 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 6f must have been o
1a03b 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 btained from.**
1a03c 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 a prior call to
1a03d 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
1a03e 61 62 65 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 abel()..*/.SQLIT
1a03f 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a040 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
1a041 65 4c 61 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 eLabel(Vdbe *p,
1a042 69 6e 74 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 int x){. int j
1a043 3d 20 2d 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 = -1-x;. assert
1a044 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 ( p->magic==VDBE
1a045 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 _MAGIC_INIT );.
1a046 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 assert( j>=0 &&
1a047 20 6a 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a j<p->nLabel );.
1a048 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 if( p->aLabel
1a049 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c ){. p->aLabel
1a04a 5b 6a 5d 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 [j] = p->nOp;.
1a04b 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 }.}..#ifdef SQLI
1a04c 54 45 5f 44 45 42 55 47 20 2f 2a 20 73 71 6c 69 TE_DEBUG /* sqli
1a04d 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72 te3AssertMayAbor
1a04e 74 28 29 20 6c 6f 67 69 63 20 2a 2f 0a 0a 2f 2a t() logic */../*
1a04f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1a050 67 20 74 79 70 65 20 61 6e 64 20 66 75 6e 63 74 g type and funct
1a051 69 6f 6e 20 61 72 65 20 75 73 65 64 20 74 6f 20 ion are used to
1a052 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 iterate through
1a053 61 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 69 all opcodes.** i
1a054 6e 20 61 20 56 64 62 65 20 6d 61 69 6e 20 70 72 n a Vdbe main pr
1a055 6f 67 72 61 6d 20 61 6e 64 20 65 61 63 68 20 6f ogram and each o
1a056 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 f the sub-progra
1a057 6d 73 20 28 74 72 69 67 67 65 72 73 29 20 69 74 ms (triggers) it
1a058 20 6d 61 79 20 0a 2a 2a 20 69 6e 76 6f 6b 65 20 may .** invoke
1a059 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 directly or indi
1a05a 72 65 63 74 6c 79 2e 20 49 74 20 73 68 6f 75 6c rectly. It shoul
1a05b 64 20 62 65 20 75 73 65 64 20 61 73 20 66 6f 6c d be used as fol
1a05c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 lows:.**.** Op
1a05d 20 2a 70 4f 70 3b 0a 2a 2a 20 20 20 56 64 62 65 *pOp;.** Vdbe
1a05e 4f 70 49 74 65 72 20 73 49 74 65 72 3b 0a 2a 2a OpIter sIter;.**
1a05f 0a 2a 2a 20 20 20 6d 65 6d 73 65 74 28 26 73 49 .** memset(&sI
1a060 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 ter, 0, sizeof(s
1a061 49 74 65 72 29 29 3b 0a 2a 2a 20 20 20 73 49 74 Iter));.** sIt
1a062 65 72 2e 76 20 3d 20 76 3b 20 20 20 20 20 20 20 er.v = v;
1a063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a064 20 20 20 20 20 2f 2f 20 76 20 69 73 20 6f 66 20 // v is of
1a065 74 79 70 65 20 56 64 62 65 2a 20 0a 2a 2a 20 20 type Vdbe* .**
1a066 20 77 68 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f while( (pOp = o
1a067 70 49 74 65 72 4e 65 78 74 28 26 73 49 74 65 72 pIterNext(&sIter
1a068 29 29 20 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 )) ){.** //
1a069 44 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 Do something wit
1a06a 68 20 70 4f 70 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 h pOp.** }.**
1a06b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a06c 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 v->db, sIter.apS
1a06d 75 62 29 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 70 65 ub);.** .*/.type
1a06e 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 4f def struct VdbeO
1a06f 70 49 74 65 72 20 56 64 62 65 4f 70 49 74 65 72 pIter VdbeOpIter
1a070 3b 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70 49 ;.struct VdbeOpI
1a071 74 65 72 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b ter {. Vdbe *v;
1a072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a073 20 20 20 2f 2a 20 56 64 62 65 20 74 6f 20 69 74 /* Vdbe to it
1a074 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 erate through th
1a075 65 20 6f 70 63 6f 64 65 73 20 6f 66 20 2a 2f 0a e opcodes of */.
1a076 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 61 SubProgram **a
1a077 70 53 75 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 pSub; /*
1a078 41 72 72 61 79 20 6f 66 20 73 75 62 70 72 6f 67 Array of subprog
1a079 72 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 rams */. int nS
1a07a 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ub;
1a07b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1a07c 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 53 f entries in apS
1a07d 75 62 20 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64 ub */. int iAdd
1a07e 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1a07f 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
1a080 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f next instructio
1a081 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 n to return */.
1a082 20 69 6e 74 20 69 53 75 62 3b 20 20 20 20 20 20 int iSub;
1a083 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 /* 0
1a084 20 3d 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2c = main program,
1a085 20 31 20 3d 20 66 69 72 73 74 20 73 75 62 2d 70 1 = first sub-p
1a086 72 6f 67 72 61 6d 20 65 74 63 2e 20 2a 2f 0a 7d rogram etc. */.}
1a087 3b 0a 73 74 61 74 69 63 20 4f 70 20 2a 6f 70 49 ;.static Op *opI
1a088 74 65 72 4e 65 78 74 28 56 64 62 65 4f 70 49 74 terNext(VdbeOpIt
1a089 65 72 20 2a 70 29 7b 0a 20 20 56 64 62 65 20 2a er *p){. Vdbe *
1a08a 76 20 3d 20 70 2d 3e 76 3b 0a 20 20 4f 70 20 2a v = p->v;. Op *
1a08b 70 52 65 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a pRet = 0;. Op *
1a08c 61 4f 70 3b 0a 20 20 69 6e 74 20 6e 4f 70 3b 0a aOp;. int nOp;.
1a08d 0a 20 20 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d . if( p->iSub<=
1a08e 70 2d 3e 6e 53 75 62 20 29 7b 0a 0a 20 20 20 20 p->nSub ){..
1a08f 69 66 28 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29 if( p->iSub==0 )
1a090 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 76 2d {. aOp = v-
1a091 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 >aOp;. nOp
1a092 3d 20 76 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 65 = v->nOp;. }e
1a093 6c 73 65 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d lse{. aOp =
1a094 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75 p->apSub[p->iSu
1a095 62 2d 31 5d 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 b-1]->aOp;.
1a096 20 6e 4f 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b nOp = p->apSub[
1a097 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e 6e 4f 70 3b p->iSub-1]->nOp;
1a098 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 . }. asser
1a099 74 28 20 70 2d 3e 69 41 64 64 72 3c 6e 4f 70 20 t( p->iAddr<nOp
1a09a 29 3b 0a 0a 20 20 20 20 70 52 65 74 20 3d 20 26 );.. pRet = &
1a09b 61 4f 70 5b 70 2d 3e 69 41 64 64 72 5d 3b 0a 20 aOp[p->iAddr];.
1a09c 20 20 20 70 2d 3e 69 41 64 64 72 2b 2b 3b 0a 20 p->iAddr++;.
1a09d 20 20 20 69 66 28 20 70 2d 3e 69 41 64 64 72 3d if( p->iAddr=
1a09e 3d 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 2d =nOp ){. p-
1a09f 3e 69 53 75 62 2b 2b 3b 0a 20 20 20 20 20 20 70 >iSub++;. p
1a0a0 2d 3e 69 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 ->iAddr = 0;.
1a0a1 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 70 52 }. . if( pR
1a0a2 65 74 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 et->p4type==P4_S
1a0a3 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 UBPROGRAM ){.
1a0a4 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 int nByte = (
1a0a5 70 2d 3e 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f p->nSub+1)*sizeo
1a0a6 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a f(SubProgram*);.
1a0a7 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 int j;.
1a0a8 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d for(j=0; j<p-
1a0a9 3e 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 >nSub; j++){.
1a0aa 20 20 20 20 20 69 66 28 20 70 2d 3e 61 70 53 75 if( p->apSu
1a0ab 62 5b 6a 5d 3d 3d 70 52 65 74 2d 3e 70 34 2e 70 b[j]==pRet->p4.p
1a0ac 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b Program ) break;
1a0ad 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
1a0ae 66 28 20 6a 3d 3d 70 2d 3e 6e 53 75 62 20 29 7b f( j==p->nSub ){
1a0af 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 . p->apSu
1a0b0 62 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 b = sqlite3DbRea
1a0b1 6c 6c 6f 63 4f 72 46 72 65 65 28 76 2d 3e 64 62 llocOrFree(v->db
1a0b2 2c 20 70 2d 3e 61 70 53 75 62 2c 20 6e 42 79 74 , p->apSub, nByt
1a0b3 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
1a0b4 21 70 2d 3e 61 70 53 75 62 20 29 7b 0a 20 20 20 !p->apSub ){.
1a0b5 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 30 3b pRet = 0;
1a0b6 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1a0b7 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 70 53 p->apS
1a0b8 75 62 5b 70 2d 3e 6e 53 75 62 2b 2b 5d 20 3d 20 ub[p->nSub++] =
1a0b9 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61 pRet->p4.pProgra
1a0ba 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 m;. }.
1a0bb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a }. }. }..
1a0bc 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d return pRet;.}
1a0bd 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 69 66 ../*.** Check if
1a0be 20 74 68 65 20 70 72 6f 67 72 61 6d 20 73 74 6f the program sto
1a0bf 72 65 64 20 69 6e 20 74 68 65 20 56 4d 20 61 73 red in the VM as
1a0c0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 50 sociated with pP
1a0c1 61 72 73 65 20 6d 61 79 0a 2a 2a 20 74 68 72 6f arse may.** thro
1a0c2 77 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 w an ABORT excep
1a0c3 74 69 6f 6e 20 28 63 61 75 73 69 6e 67 20 74 68 tion (causing th
1a0c4 65 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 75 74 e statement, but
1a0c5 20 6e 6f 74 20 65 6e 74 69 72 65 20 74 72 61 6e not entire tran
1a0c6 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 saction.** to be
1a0c7 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54 rolled back). T
1a0c8 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 his condition is
1a0c9 20 74 72 75 65 20 69 66 20 74 68 65 20 6d 61 69 true if the mai
1a0ca 6e 20 70 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79 n program or any
1a0cb 0a 2a 2a 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 .** sub-programs
1a0cc 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66 contains any of
1a0cd 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
1a0ce 2a 2a 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 **.** * OP_Ha
1a0cf 6c 74 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 lt with P1=SQLIT
1a0d0 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 E_CONSTRAINT and
1a0d1 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a P2=OE_Abort..**
1a0d2 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e * OP_HaltIfN
1a0d3 75 6c 6c 20 77 69 74 68 20 50 31 3d 53 51 4c 49 ull with P1=SQLI
1a0d4 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e TE_CONSTRAINT an
1a0d5 64 20 50 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a d P2=OE_Abort..*
1a0d6 2a 20 20 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f * * OP_Destro
1a0d7 79 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 y.** * OP_VUp
1a0d8 64 61 74 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f date.** * OP_
1a0d9 56 52 65 6e 61 6d 65 0a 2a 2a 20 20 20 2a 20 20 VRename.** *
1a0da 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 77 69 74 OP_FkCounter wit
1a0db 68 20 50 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61 h P2==0 (immedia
1a0dc 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 te foreign key c
1a0dd 6f 6e 73 74 72 61 69 6e 74 29 0a 2a 2a 0a 2a 2a onstraint).**.**
1a0de 20 54 68 65 6e 20 63 68 65 63 6b 20 74 68 61 74 Then check that
1a0df 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61 the value of Pa
1a0e0 72 73 65 2e 6d 61 79 41 62 6f 72 74 20 69 73 20 rse.mayAbort is
1a0e1 74 72 75 65 20 69 66 20 61 6e 0a 2a 2a 20 41 42 true if an.** AB
1a0e2 4f 52 54 20 6d 61 79 20 62 65 20 74 68 72 6f 77 ORT may be throw
1a0e3 6e 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 n, or false othe
1a0e4 72 77 69 73 65 2e 20 52 65 74 75 72 6e 20 74 72 rwise. Return tr
1a0e5 75 65 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a ue if it does.**
1a0e6 20 6d 61 74 63 68 2c 20 6f 72 20 66 61 6c 73 65 match, or false
1a0e7 20 6f 74 68 65 72 77 69 73 65 2e 20 54 68 69 73 otherwise. This
1a0e8 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 74 function is int
1a0e9 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 ended to be used
1a0ea 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 as.** part of a
1a0eb 6e 20 61 73 73 65 72 74 20 73 74 61 74 65 6d 65 n assert stateme
1a0ec 6e 74 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c nt in the compil
1a0ed 65 72 2e 20 53 69 6d 69 6c 61 72 20 74 6f 3a 0a er. Similar to:.
1a0ee 2a 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 **.** assert(
1a0ef 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 sqlite3VdbeAsser
1a0f0 74 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 tMayAbort(pParse
1a0f1 2d 3e 70 56 64 62 65 2c 20 70 50 61 72 73 65 2d ->pVdbe, pParse-
1a0f2 3e 6d 61 79 41 62 6f 72 74 29 20 29 3b 0a 2a 2f >mayAbort) );.*/
1a0f3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1a0f4 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 int sqlite3VdbeA
1a0f5 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 56 64 ssertMayAbort(Vd
1a0f6 62 65 20 2a 76 2c 20 69 6e 74 20 6d 61 79 41 62 be *v, int mayAb
1a0f7 6f 72 74 29 7b 0a 20 20 69 6e 74 20 68 61 73 41 ort){. int hasA
1a0f8 62 6f 72 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a bort = 0;. Op *
1a0f9 70 4f 70 3b 0a 20 20 56 64 62 65 4f 70 49 74 65 pOp;. VdbeOpIte
1a0fa 72 20 73 49 74 65 72 3b 0a 20 20 6d 65 6d 73 65 r sIter;. memse
1a0fb 74 28 26 73 49 74 65 72 2c 20 30 2c 20 73 69 7a t(&sIter, 0, siz
1a0fc 65 6f 66 28 73 49 74 65 72 29 29 3b 0a 20 20 73 eof(sIter));. s
1a0fd 49 74 65 72 2e 76 20 3d 20 76 3b 0a 0a 20 20 77 Iter.v = v;.. w
1a0fe 68 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 hile( (pOp = opI
1a0ff 74 65 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 terNext(&sIter))
1a100 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f !=0 ){. int o
1a101 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 pcode = pOp->opc
1a102 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 ode;. if( opc
1a103 6f 64 65 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 ode==OP_Destroy
1a104 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 || opcode==OP_VU
1a105 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d pdate || opcode=
1a106 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 0a 23 69 66 =OP_VRename .#if
1a107 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1a108 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 _FOREIGN_KEY.
1a109 20 20 7c 7c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 || (opcode==OP
1a10a 5f 46 6b 43 6f 75 6e 74 65 72 20 26 26 20 70 4f _FkCounter && pO
1a10b 70 2d 3e 70 31 3d 3d 30 20 26 26 20 70 4f 70 2d p->p1==0 && pOp-
1a10c 3e 70 32 3d 3d 31 29 20 0a 23 65 6e 64 69 66 0a >p2==1) .#endif.
1a10d 20 20 20 20 20 7c 7c 20 28 28 6f 70 63 6f 64 65 || ((opcode
1a10e 3d 3d 4f 50 5f 48 61 6c 74 20 7c 7c 20 6f 70 63 ==OP_Halt || opc
1a10f 6f 64 65 3d 3d 4f 50 5f 48 61 6c 74 49 66 4e 75 ode==OP_HaltIfNu
1a110 6c 6c 29 20 0a 20 20 20 20 20 20 26 26 20 28 70 ll) . && (p
1a111 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 Op->p1==SQLITE_C
1a112 4f 4e 53 54 52 41 49 4e 54 20 26 26 20 70 4f 70 ONSTRAINT && pOp
1a113 2d 3e 70 32 3d 3d 4f 45 5f 41 62 6f 72 74 29 29 ->p2==OE_Abort))
1a114 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 68 61 . ){. ha
1a115 73 41 62 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 sAbort = 1;.
1a116 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1a117 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 }. sqlite3DbFr
1a118 65 65 28 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e ee(v->db, sIter.
1a119 61 70 53 75 62 29 3b 0a 0a 20 20 2f 2a 20 52 65 apSub);.. /* Re
1a11a 74 75 72 6e 20 74 72 75 65 20 69 66 20 68 61 73 turn true if has
1a11b 41 62 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e Abort==mayAbort.
1a11c 20 4f 72 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 Or if a malloc
1a11d 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 2e failure occured.
1a11e 0a 20 20 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20 . ** If malloc
1a11f 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 failed, then the
1a120 20 77 68 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62 while() loop ab
1a121 6f 76 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 ove may not have
1a122 20 69 74 65 72 61 74 65 64 0a 20 20 2a 2a 20 74 iterated. ** t
1a123 68 72 6f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 hrough all opcod
1a124 65 73 20 61 6e 64 20 68 61 73 41 62 6f 72 74 20 es and hasAbort
1a125 6d 61 79 20 62 65 20 73 65 74 20 69 6e 63 6f 72 may be set incor
1a126 72 65 63 74 6c 79 2e 20 52 65 74 75 72 6e 0a 20 rectly. Return.
1a127 20 2a 2a 20 74 72 75 65 20 66 6f 72 20 74 68 69 ** true for thi
1a128 73 20 63 61 73 65 20 74 6f 20 70 72 65 76 65 6e s case to preven
1a129 74 20 74 68 65 20 61 73 73 65 72 74 28 29 20 69 t the assert() i
1a12a 6e 20 74 68 65 20 63 61 6c 6c 65 72 73 20 66 72 n the callers fr
1a12b 61 6d 65 0a 20 20 2a 2a 20 66 72 6f 6d 20 66 61 ame. ** from fa
1a12c 69 6c 69 6e 67 2e 20 20 2a 2f 0a 20 20 72 65 74 iling. */. ret
1a12d 75 72 6e 20 28 20 76 2d 3e 64 62 2d 3e 6d 61 6c urn ( v->db->mal
1a12e 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 68 61 73 locFailed || has
1a12f 41 62 6f 72 74 3d 3d 6d 61 79 41 62 6f 72 74 20 Abort==mayAbort
1a130 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
1a131 51 4c 49 54 45 5f 44 45 42 55 47 20 2d 20 74 68 QLITE_DEBUG - th
1a132 65 20 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d e sqlite3AssertM
1a133 61 79 41 62 6f 72 74 28 29 20 66 75 6e 63 74 69 ayAbort() functi
1a134 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f on */../*.** Loo
1a135 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 70 72 p through the pr
1a136 6f 67 72 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f ogram looking fo
1a137 72 20 50 32 20 76 61 6c 75 65 73 20 74 68 61 74 r P2 values that
1a138 20 61 72 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a are negative.**
1a139 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 on jump instruc
1a13a 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 73 75 63 tions. Each suc
1a13b 68 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 h value is a lab
1a13c 65 6c 2e 20 20 52 65 73 6f 6c 76 65 20 74 68 65 el. Resolve the
1a13d 0a 2a 2a 20 6c 61 62 65 6c 20 62 79 20 73 65 74 .** label by set
1a13e 74 69 6e 67 20 74 68 65 20 50 32 20 76 61 6c 75 ting the P2 valu
1a13f 65 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63 74 e to its correct
1a140 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e non-zero value.
1a141 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
1a142 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e ine is called on
1a143 63 65 20 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 ce after all opc
1a144 6f 64 65 73 20 68 61 76 65 20 62 65 65 6e 20 69 odes have been i
1a145 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 nserted..**.** V
1a146 61 72 69 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e ariable *pMaxFun
1a147 63 41 72 67 73 20 69 73 20 73 65 74 20 74 6f 20 cArgs is set to
1a148 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 the maximum valu
1a149 65 20 6f 66 20 61 6e 79 20 50 32 20 61 72 67 75 e of any P2 argu
1a14a 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f ment .** to an O
1a14b 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 P_Function, OP_A
1a14c 67 67 53 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 ggStep or OP_VFi
1a14d 6c 74 65 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 lter opcode. Thi
1a14e 73 20 69 73 20 75 73 65 64 20 62 79 20 0a 2a 2a s is used by .**
1a14f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
1a150 52 65 61 64 79 28 29 20 74 6f 20 73 69 7a 65 20 Ready() to size
1a151 74 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d the Vdbe.apArg[]
1a152 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 array..**.** Th
1a153 65 20 4f 70 2e 6f 70 66 6c 61 67 73 20 66 69 65 e Op.opflags fie
1a154 6c 64 20 69 73 20 73 65 74 20 6f 6e 20 61 6c 6c ld is set on all
1a155 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a 73 74 61 opcodes..*/.sta
1a156 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 tic void resolve
1a157 50 32 56 61 6c 75 65 73 28 56 64 62 65 20 2a 70 P2Values(Vdbe *p
1a158 2c 20 69 6e 74 20 2a 70 4d 61 78 46 75 6e 63 41 , int *pMaxFuncA
1a159 72 67 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 rgs){. int i;.
1a15a 20 69 6e 74 20 6e 4d 61 78 41 72 67 73 20 3d 20 int nMaxArgs =
1a15b 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 3b 0a 20 *pMaxFuncArgs;.
1a15c 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 6e 74 20 Op *pOp;. int
1a15d 2a 61 4c 61 62 65 6c 20 3d 20 70 2d 3e 61 4c 61 *aLabel = p->aLa
1a15e 62 65 6c 3b 0a 20 20 70 2d 3e 72 65 61 64 4f 6e bel;. p->readOn
1a15f 6c 79 20 3d 20 31 3b 0a 20 20 66 6f 72 28 70 4f ly = 1;. for(pO
1a160 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e p=p->aOp, i=p->n
1a161 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c Op-1; i>=0; i--,
1a162 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20 pOp++){. u8
1a163 6f 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 opcode = pOp->op
1a164 63 6f 64 65 3b 0a 0a 20 20 20 20 70 4f 70 2d 3e code;.. pOp->
1a165 6f 70 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 opflags = sqlite
1a166 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 3OpcodeProperty[
1a167 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28 opcode];. if(
1a168 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 opcode==OP_Func
1a169 74 69 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d tion || opcode==
1a16a 4f 50 5f 41 67 67 53 74 65 70 20 29 7b 0a 20 20 OP_AggStep ){.
1a16b 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e if( pOp->p5>
1a16c 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 nMaxArgs ) nMaxA
1a16d 72 67 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 rgs = pOp->p5;.
1a16e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 }else if( opc
1a16f 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73 61 63 74 ode==OP_Transact
1a170 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70 32 21 3d ion && pOp->p2!=
1a171 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 65 0 ){. p->re
1a172 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23 69 66 6e adOnly = 0;.#ifn
1a173 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1a174 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 VIRTUALTABLE.
1a175 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 }else if( opcod
1a176 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 29 7b e==OP_VUpdate ){
1a177 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e . if( pOp->
1a178 70 32 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d p2>nMaxArgs ) nM
1a179 61 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70 32 axArgs = pOp->p2
1a17a 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1a17b 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 opcode==OP_VFilt
1a17c 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 er ){. int
1a17d 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 n;. assert(
1a17e 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 p->nOp - i >= 3
1a17f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
1a180 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 ( pOp[-1].opcode
1a181 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a ==OP_Integer );.
1a182 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 n = pOp[-1
1a183 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 ].p1;. if(
1a184 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 n>nMaxArgs ) nMa
1a185 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 xArgs = n;.#endi
1a186 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 f. }.. if(
1a187 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 (pOp->opflags &
1a188 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 21 3d 30 20 OPFLG_JUMP)!=0
1a189 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a && pOp->p2<0 ){.
1a18a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31 assert( -1
1a18b 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62 -pOp->p2<p->nLab
1a18c 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d el );. pOp-
1a18d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d >p2 = aLabel[-1-
1a18e 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a pOp->p2];. }.
1a18f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
1a190 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c ree(p->db, p->aL
1a191 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62 abel);. p->aLab
1a192 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78 el = 0;.. *pMax
1a193 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41 FuncArgs = nMaxA
1a194 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 rgs;.}../*.** Re
1a195 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 turn the address
1a196 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 of the next ins
1a197 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 truction to be i
1a198 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 nserted..*/.SQLI
1a199 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1a19a 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
1a19b 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a tAddr(Vdbe *p){.
1a19c 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
1a19d 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
1a19e 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 NIT );. return
1a19f 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a p->nOp;.}../*.**
1a1a0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 This function r
1a1a1 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 eturns a pointer
1a1a2 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66 to the array of
1a1a3 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 69 61 opcodes associa
1a1a4 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20 ted with.** the
1a1a5 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74 Vdbe passed as t
1a1a6 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e he first argumen
1a1a7 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 61 6c t. It is the cal
1a1a8 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c lers responsibil
1a1a9 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e 67 ity.** to arrang
1a1aa 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 72 6e e for the return
1a1ab 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 20 65 ed array to be e
1a1ac 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20 ventually freed
1a1ad 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 64 using the .** vd
1a1ae 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 29 20 beFreeOpArray()
1a1af 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 function..**.**
1a1b0 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 Before returning
1a1b1 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 74 , *pnOp is set t
1a1b2 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 o the number of
1a1b3 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72 entries in the r
1a1b4 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 79 eturned.** array
1a1b5 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 72 . Also, *pnMaxAr
1a1b6 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 g is set to the
1a1b7 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 63 75 larger of its cu
1a1b8 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 rrent value and
1a1b9 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f .** the number o
1a1ba 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 f entries in the
1a1bb 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 Vdbe.apArg[] ar
1a1bc 72 61 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 ray required to
1a1bd 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a 2a 20 execute the .**
1a1be 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 61 6d returned program
1a1bf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a1c0 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 ATE VdbeOp *sqli
1a1c1 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72 te3VdbeTakeOpArr
1a1c2 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 ay(Vdbe *p, int
1a1c3 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61 *pnOp, int *pnMa
1a1c4 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 xArg){. VdbeOp
1a1c5 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 *aOp = p->aOp;.
1a1c6 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 26 20 assert( aOp &&
1a1c7 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 !p->db->mallocFa
1a1c8 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 iled );.. /* Ch
1a1c9 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65 33 eck that sqlite3
1a1ca 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29 20 VdbeUsesBtree()
1a1cb 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 6f was not called o
1a1cc 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 61 n this VM */. a
1a1cd 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 78 ssert( p->aMutex
1a1ce 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a 20 .nMutex==0 );..
1a1cf 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 resolveP2Values
1a1d0 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20 (p, pnMaxArg);.
1a1d1 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b *pnOp = p->nOp;
1a1d2 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 . p->aOp = 0;.
1a1d3 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a return aOp;.}..
1a1d4 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c /*.** Add a whol
1a1d5 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74 e list of operat
1a1d6 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72 ions to the oper
1a1d7 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65 ation stack. Re
1a1d8 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72 turn the.** addr
1a1d9 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 ess of the first
1a1da 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64 operation added
1a1db 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a1dc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1a1dd 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 dbeAddOpList(Vdb
1a1de 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 e *p, int nOp, V
1a1df 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 dbeOpList const
1a1e0 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64 *aOp){. int add
1a1e1 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e r;. assert( p->
1a1e2 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 magic==VDBE_MAGI
1a1e3 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 C_INIT );. if(
1a1e4 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 p->nOp + nOp > p
1a1e5 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72 ->nOpAlloc && gr
1a1e6 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a owOpArray(p) ){.
1a1e7 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 return 0;.
1a1e8 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f }. addr = p->nO
1a1e9 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 p;. if( ALWAYS(
1a1ea 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e nOp>0) ){. in
1a1eb 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c t i;. VdbeOpL
1a1ec 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d ist const *pIn =
1a1ed 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d aOp;. for(i=
1a1ee 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70 0; i<nOp; i++, p
1a1ef 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 In++){. int
1a1f0 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20 p2 = pIn->p2;.
1a1f1 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75 VdbeOp *pOu
1a1f2 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 t = &p->aOp[i+ad
1a1f3 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d dr];. pOut-
1a1f4 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f >opcode = pIn->o
1a1f5 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75 pcode;. pOu
1a1f6 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b t->p1 = pIn->p1;
1a1f7 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 . if( p2<0
1a1f8 26 26 20 28 73 71 6c 69 74 65 33 4f 70 63 6f 64 && (sqlite3Opcod
1a1f9 65 50 72 6f 70 65 72 74 79 5b 70 4f 75 74 2d 3e eProperty[pOut->
1a1fa 6f 70 63 6f 64 65 5d 20 26 20 4f 50 46 4c 47 5f opcode] & OPFLG_
1a1fb 4a 55 4d 50 29 21 3d 30 20 29 7b 0a 20 20 20 20 JUMP)!=0 ){.
1a1fc 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 61 pOut->p2 = a
1a1fd 64 64 72 20 2b 20 41 44 44 52 28 70 32 29 3b 0a ddr + ADDR(p2);.
1a1fe 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1a1ff 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20 pOut->p2 =
1a200 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p2;. }.
1a201 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49 6e pOut->p3 = pIn
1a202 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f 75 74 ->p3;. pOut
1a203 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f ->p4type = P4_NO
1a204 54 55 53 45 44 3b 0a 20 20 20 20 20 20 70 4f 75 TUSED;. pOu
1a205 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 t->p4.p = 0;.
1a206 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30 3b pOut->p5 = 0;
1a207 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
1a208 45 42 55 47 0a 20 20 20 20 20 20 70 4f 75 74 2d EBUG. pOut-
1a209 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 >zComment = 0;.
1a20a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1a20b 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 VdbeAddopTrace )
1a20c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a20d 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20 3VdbePrintOp(0,
1a20e 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70 5b i+addr, &p->aOp[
1a20f 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 20 20 i+addr]);.
1a210 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 }.#endif. }.
1a211 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 p->nOp += nOp
1a212 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 ;. }. return a
1a213 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 ddr;.}../*.** Ch
1a214 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f ange the value o
1a215 66 20 74 68 65 20 50 31 20 6f 70 65 72 61 6e 64 f the P1 operand
1a216 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 for a specific
1a217 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 instruction..**
1a218 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1a219 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 useful when a la
1a21a 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c rge program is l
1a21b 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 oaded from a.**
1a21c 73 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69 static array usi
1a21d 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 ng sqlite3VdbeAd
1a21e 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 dOpList but we w
1a21f 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a ant to make a.**
1a220 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 few minor chang
1a221 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 es to the progra
1a222 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 m..*/.SQLITE_PRI
1a223 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a224 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 3VdbeChangeP1(Vd
1a225 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
1a226 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 int val){. ass
1a227 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 ert( p!=0 );. a
1a228 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 ssert( addr>=0 )
1a229 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 ;. if( p->nOp>a
1a22a 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f ddr ){. p->aO
1a22b 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61 6c p[addr].p1 = val
1a22c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
1a22d 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
1a22e 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e of the P2 operan
1a22f 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 d for a specific
1a230 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a instruction..**
1a231 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
1a232 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74 74 useful for sett
1a233 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74 69 ing a jump desti
1a234 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 nation..*/.SQLIT
1a235 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
1a236 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1a237 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 P2(Vdbe *p, int
1a238 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a addr, int val){.
1a239 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
1a23a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 ;. assert( addr
1a23b 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e >=0 );. if( p->
1a23c 6e 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 nOp>addr ){.
1a23d 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32 20 p->aOp[addr].p2
1a23e 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a = val;. }.}../*
1a23f 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 .** Change the v
1a240 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20 6f alue of the P3 o
1a241 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 perand for a spe
1a242 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f cific instructio
1a243 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 n..*/.SQLITE_PRI
1a244 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1a245 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56 64 3VdbeChangeP3(Vd
1a246 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c be *p, int addr,
1a247 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73 int val){. ass
1a248 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 ert( p!=0 );. a
1a249 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29 ssert( addr>=0 )
1a24a 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61 ;. if( p->nOp>a
1a24b 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f ddr ){. p->aO
1a24c 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76 61 6c p[addr].p3 = val
1a24d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
1a24e 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 hange the value
1a24f 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 61 6e of the P5 operan
1a250 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 d for the most r
1a251 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 65 64 ecently.** added
1a252 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 operation..*/.S
1a253 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
1a254 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 id sqlite3VdbeCh
1a255 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70 2c 20 angeP5(Vdbe *p,
1a256 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 u8 val){. asser
1a257 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 t( p!=0 );. if(
1a258 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 p->aOp ){. a
1a259 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
1a25a 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d );. p->aOp[p-
1a25b 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c >nOp-1].p5 = val
1a25c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 ;. }.}../*.** C
1a25d 68 61 6e 67 65 20 74 68 65 20 50 32 20 6f 70 65 hange the P2 ope
1a25e 72 61 6e 64 20 6f 66 20 69 6e 73 74 72 75 63 74 rand of instruct
1a25f 69 6f 6e 20 61 64 64 72 20 73 6f 20 74 68 61 74 ion addr so that
1a260 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a it points to.**
1a261 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 the address of
1a262 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1a263 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 tion to be coded
1a264 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1a265 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
1a266 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64 62 VdbeJumpHere(Vdb
1a267 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b e *p, int addr){
1a268 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 . sqlite3VdbeCh
1a269 61 6e 67 65 50 32 28 70 2c 20 61 64 64 72 2c 20 angeP2(p, addr,
1a26a 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a p->nOp);.}.../*.
1a26b 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 ** If the input
1a26c 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 FuncDef structur
1a26d 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 e is ephemeral,
1a26e 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20 20 49 then free it. I
1a26f 66 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44 65 66 f.** the FuncDef
1a270 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c is not ephermal
1a271 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e , then do nothin
1a272 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 g..*/.static voi
1a273 64 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 d freeEphemeralF
1a274 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 unction(sqlite3
1a275 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 *db, FuncDef *pD
1a276 65 66 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 ef){. if( ALWAY
1a277 53 28 70 44 65 66 29 20 26 26 20 28 70 44 65 66 S(pDef) && (pDef
1a278 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
1a279 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30 20 _FUNC_EPHEM)!=0
1a27a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 ){. sqlite3Db
1a27b 46 72 65 65 28 64 62 2c 20 70 44 65 66 29 3b 0a Free(db, pDef);.
1a27c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c }.}../*.** Del
1a27d 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 20 69 ete a P4 value i
1a27e 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a f necessary..*/.
1a27f 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 static void free
1a280 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 P4(sqlite3 *db,
1a281 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f 69 64 int p4type, void
1a282 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 34 20 *p4){. if( p4
1a283 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 ){. switch( p
1a284 34 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 4type ){. c
1a285 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20 20 ase P4_REAL:.
1a286 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 case P4_INT64
1a287 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f :. case P4_
1a288 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 20 63 MPRINTF:. c
1a289 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a 0a ase P4_DYNAMIC:.
1a28a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 case P4_KE
1a28b 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61 73 YINFO:. cas
1a28c 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a 20 e P4_INTARRAY:.
1a28d 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 case P4_KEY
1a28e 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b 0a INFO_HANDOFF: {.
1a28f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
1a290 62 46 72 65 65 28 64 62 2c 20 70 34 29 3b 0a 20 bFree(db, p4);.
1a291 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1a292 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 }. case
1a293 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b 0a P4_VDBEFUNC: {.
1a294 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e 63 VdbeFunc
1a295 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 *pVdbeFunc = (V
1a296 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20 20 dbeFunc *)p4;.
1a297 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 freeEpheme
1a298 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 ralFunction(db,
1a299 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 pVdbeFunc->pFunc
1a29a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1a29b 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 e3VdbeDeleteAuxD
1a29c 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20 30 ata(pVdbeFunc, 0
1a29d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
1a29e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 64 e3DbFree(db, pVd
1a29f 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 beFunc);.
1a2a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
1a2a1 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 46 55 case P4_FU
1a2a2 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 20 NCDEF: {.
1a2a3 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 freeEphemeralFu
1a2a4 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e 63 nction(db, (Func
1a2a5 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 Def*)p4);.
1a2a6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1a2a7 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4d . case P4_M
1a2a8 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 EM: {. sq
1a2a9 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 28 lite3ValueFree((
1a2aa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 70 sqlite3_value*)p
1a2ab 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 4);. brea
1a2ac 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
1a2ad 20 63 61 73 65 20 50 34 5f 56 54 41 42 20 3a 20 case P4_VTAB :
1a2ae 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1a2af 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54 61 3VtabUnlock((VTa
1a2b0 62 6c 65 20 2a 29 70 34 29 3b 0a 20 20 20 20 20 ble *)p4);.
1a2b1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1a2b2 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a2b3 53 55 42 50 52 4f 47 52 41 4d 20 3a 20 7b 0a 20 SUBPROGRAM : {.
1a2b4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a2b5 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 beProgramDelete(
1a2b6 64 62 2c 20 28 53 75 62 50 72 6f 67 72 61 6d 20 db, (SubProgram
1a2b7 2a 29 70 34 2c 20 31 29 3b 0a 20 20 20 20 20 20 *)p4, 1);.
1a2b8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1a2b9 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a . }. }.}../*
1a2ba 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 73 70 61 .** Free the spa
1a2bb 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 ce allocated for
1a2bc 20 61 4f 70 20 61 6e 64 20 61 6e 79 20 70 34 20 aOp and any p4
1a2bd 76 61 6c 75 65 73 20 61 6c 6c 6f 63 61 74 65 64 values allocated
1a2be 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 63 6f for the.** opco
1a2bf 64 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 des contained wi
1a2c0 74 68 69 6e 2e 20 49 66 20 61 4f 70 20 69 73 20 thin. If aOp is
1a2c1 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 69 73 20 61 not NULL it is a
1a2c2 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 ssumed to contai
1a2c3 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 69 65 n .** nOp entrie
1a2c4 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f s. .*/.static vo
1a2c5 69 64 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 id vdbeFreeOpArr
1a2c6 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ay(sqlite3 *db,
1a2c7 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f 70 Op *aOp, int nOp
1a2c8 29 7b 0a 20 20 69 66 28 20 61 4f 70 20 29 7b 0a ){. if( aOp ){.
1a2c9 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 Op *pOp;.
1a2ca 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70 4f for(pOp=aOp; pO
1a2cb 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f 70 p<&aOp[nOp]; pOp
1a2cc 2b 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 ++){. freeP
1a2cd 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 4(db, pOp->p4typ
1a2ce 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 23 e, pOp->p4.p);.#
1a2cf 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1a2d0 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 UG. sqlite3
1a2d1 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e DbFree(db, pOp->
1a2d2 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64 69 zComment);.#endi
1a2d3 66 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d f . }. }
1a2d4 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1a2d5 28 64 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a (db, aOp);.}../*
1a2d6 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 .** Decrement th
1a2d7 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20 74 e ref-count on t
1a2d8 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74 he SubProgram st
1a2d9 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 ructure passed a
1a2da 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 s the.** second
1a2db 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68 65 argument. If the
1a2dc 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 ref-count reach
1a2dd 65 73 20 7a 65 72 6f 2c 20 66 72 65 65 20 74 68 es zero, free th
1a2de 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a e structure..**.
1a2df 2a 2a 20 54 68 65 20 61 72 72 61 79 20 6f 66 20 ** The array of
1a2e0 56 44 42 45 20 6f 70 63 6f 64 65 73 20 73 74 6f VDBE opcodes sto
1a2e1 72 65 64 20 61 73 20 53 75 62 50 72 6f 67 72 61 red as SubProgra
1a2e2 6d 2e 61 4f 70 20 69 73 20 66 72 65 65 64 20 69 m.aOp is freed i
1a2e3 66 0a 2a 2a 20 65 69 74 68 65 72 20 74 68 65 20 f.** either the
1a2e4 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68 65 ref-count reache
1a2e5 73 20 7a 65 72 6f 20 6f 72 20 70 61 72 61 6d 65 s zero or parame
1a2e6 74 65 72 20 66 72 65 65 6f 70 20 69 73 20 6e 6f ter freeop is no
1a2e7 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 n-zero..**.** Si
1a2e8 6e 63 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 nce the array of
1a2e9 20 6f 70 63 6f 64 65 73 20 70 6f 69 6e 74 65 64 opcodes pointed
1a2ea 20 74 6f 20 62 79 20 53 75 62 50 72 6f 67 72 61 to by SubProgra
1a2eb 6d 2e 61 4f 70 20 6d 61 79 20 64 69 72 65 63 74 m.aOp may direct
1a2ec 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 65 63 ly.** or indirec
1a2ed 74 6c 79 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 tly contain a re
1a2ee 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 53 ference to the S
1a2ef 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 ubProgram struct
1a2f0 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 42 ure itself..** B
1a2f1 79 20 70 61 73 73 69 6e 67 20 61 20 6e 6f 6e 2d y passing a non-
1a2f2 7a 65 72 6f 20 66 72 65 65 6f 70 20 70 61 72 61 zero freeop para
1a2f3 6d 65 74 65 72 2c 20 74 68 65 20 63 61 6c 6c 65 meter, the calle
1a2f4 72 20 6d 61 79 20 65 6e 73 75 72 65 20 74 68 61 r may ensure tha
1a2f5 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f 67 t all.** SubProg
1a2f6 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 ram structures a
1a2f7 6e 64 20 74 68 65 69 72 20 61 4f 70 20 61 72 72 nd their aOp arr
1a2f8 61 79 73 20 61 72 65 20 66 72 65 65 64 2c 20 65 ays are freed, e
1a2f9 76 65 6e 20 77 68 65 6e 20 74 68 65 72 65 0a 2a ven when there.*
1a2fa 2a 20 61 72 65 20 73 75 63 68 20 63 69 72 63 75 * are such circu
1a2fb 6c 61 72 20 72 65 66 65 72 65 6e 63 65 73 2e 0a lar references..
1a2fc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a2fd 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a2fe 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 beProgramDelete(
1a2ff 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 75 62 sqlite3 *db, Sub
1a300 50 72 6f 67 72 61 6d 20 2a 70 2c 20 69 6e 74 20 Program *p, int
1a301 66 72 65 65 6f 70 29 7b 0a 20 20 69 66 28 20 70 freeop){. if( p
1a302 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 ){. assert(
1a303 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 p->nRef>0 );.
1a304 20 69 66 28 20 66 72 65 65 6f 70 20 7c 7c 20 70 if( freeop || p
1a305 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20 20 ->nRef==1 ){.
1a306 20 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e Op *aOp = p->
1a307 61 4f 70 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f aOp;. p->aO
1a308 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 76 64 62 p = 0;. vdb
1a309 65 46 72 65 65 4f 70 41 72 72 61 79 28 64 62 2c eFreeOpArray(db,
1a30a 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 aOp, p->nOp);.
1a30b 20 20 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 30 3b p->nOp = 0;
1a30c 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 . }. p->nR
1a30d 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d ef--;. if( p-
1a30e 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 >nRef==0 ){.
1a30f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1a310 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 db, p);. }.
1a311 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}.../*.** Chan
1a312 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74 61 ge N opcodes sta
1a313 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74 6f rting at addr to
1a314 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 No-ops..*/.SQLI
1a315 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a316 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1a317 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70 2c eToNoop(Vdbe *p,
1a318 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e int addr, int N
1a319 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 20 ){. if( p->aOp
1a31a 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 ){. VdbeOp *p
1a31b 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 Op = &p->aOp[add
1a31c 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 r];. sqlite3
1a31d 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 *db = p->db;.
1a31e 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 while( N-- ){.
1a31f 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 freeP4(db,
1a320 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 pOp->p4type, pOp
1a321 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d ->p4.p);. m
1a322 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 emset(pOp, 0, si
1a323 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 zeof(pOp[0]));.
1a324 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 pOp->opcode
1a325 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 = OP_Noop;.
1a326 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 pOp++;. }.
1a327 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e }.}../*.** Chan
1a328 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ge the value of
1a329 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 66 the P4 operand f
1a32a 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69 6e or a specific in
1a32b 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 struction..** Th
1a32c 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 is routine is us
1a32d 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 eful when a larg
1a32e 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 e program is loa
1a32f 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 ded from a.** st
1a330 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e 67 atic array using
1a331 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1a332 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61 6e pList but we wan
1a333 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 t to make a.** f
1a334 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 ew minor changes
1a335 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d 2e to the program.
1a336 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 .**.** If n>=0 t
1a337 68 65 6e 20 74 68 65 20 50 34 20 6f 70 65 72 61 hen the P4 opera
1a338 6e 64 20 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d nd is dynamic, m
1a339 65 61 6e 69 6e 67 20 74 68 61 74 20 61 20 63 6f eaning that a co
1a33a 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 py of.** the str
1a33b 69 6e 67 20 69 73 20 6d 61 64 65 20 69 6e 74 6f ing is made into
1a33c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
1a33d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 from sqlite3_ma
1a33e 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c lloc()..** A val
1a33f 75 65 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 ue of n==0 means
1a340 20 63 6f 70 79 20 62 79 74 65 73 20 6f 66 20 7a copy bytes of z
1a341 50 34 20 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 P4 up to and inc
1a342 6c 75 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 luding the.** fi
1a343 72 73 74 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 rst null byte.
1a344 49 66 20 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 If n>0 then copy
1a345 20 6e 2b 31 20 62 79 74 65 73 20 6f 66 20 7a 50 n+1 bytes of zP
1a346 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 4..**.** If n==P
1a347 34 5f 4b 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 4_KEYINFO it mea
1a348 6e 73 20 74 68 61 74 20 7a 50 34 20 69 73 20 61 ns that zP4 is a
1a349 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 pointer to a Ke
1a34a 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e yInfo structure.
1a34b 0a 2a 2a 20 41 20 63 6f 70 79 20 69 73 20 6d 61 .** A copy is ma
1a34c 64 65 20 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 de of the KeyInf
1a34d 6f 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f o structure into
1a34e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 memory obtained
1a34f 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 from.** sqlite3
1a350 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 _malloc, to be f
1a351 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 reed when the Vd
1a352 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e be is finalized.
1a353 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 .** n==P4_KEYINF
1a354 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 O_HANDOFF indica
1a355 74 65 73 20 74 68 61 74 20 7a 50 34 20 70 6f 69 tes that zP4 poi
1a356 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f nts to a KeyInfo
1a357 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 structure.** st
1a358 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 ored in memory t
1a359 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 hat the caller h
1a35a 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d as obtained from
1a35b 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e sqlite3_malloc.
1a35c 20 54 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 The .** caller
1a35d 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 should not free
1a35e 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 the allocation,
1a35f 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 it will be freed
1a360 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 when the Vdbe i
1a361 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a s.** finalized..
1a362 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c ** .** Other val
1a363 75 65 73 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 ues of n (P4_STA
1a364 54 49 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 TIC, P4_COLLSEQ
1a365 65 74 63 2e 29 20 69 6e 64 69 63 61 74 65 20 74 etc.) indicate t
1a366 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a hat zP4 points.*
1a367 2a 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 * to a string or
1a368 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 structure that
1a369 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f is guaranteed to
1a36a 20 65 78 69 73 74 20 66 6f 72 20 74 68 65 20 6c exist for the l
1a36b 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 ifetime of.** th
1a36c 65 20 56 64 62 65 2e 20 49 6e 20 74 68 65 73 65 e Vdbe. In these
1a36d 20 63 61 73 65 73 20 77 65 20 63 61 6e 20 6a 75 cases we can ju
1a36e 73 74 20 63 6f 70 79 20 74 68 65 20 70 6f 69 6e st copy the poin
1a36f 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 ter..**.** If ad
1a370 64 72 3c 30 20 74 68 65 6e 20 63 68 61 6e 67 65 dr<0 then change
1a371 20 50 34 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 P4 on the most
1a372 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 recently inserte
1a373 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a d instruction..*
1a374 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a375 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 void sqlite3Vdb
1a376 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 20 2a eChangeP4(Vdbe *
1a377 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e p, int addr, con
1a378 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e st char *zP4, in
1a379 74 20 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b t n){. Op *pOp;
1a37a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1a37b 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 assert( p!=0 )
1a37c 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a ;. db = p->db;.
1a37d 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 assert( p->mag
1a37e 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 ic==VDBE_MAGIC_I
1a37f 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e NIT );. if( p->
1a380 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 aOp==0 || db->ma
1a381 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1a382 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b 45 59 if ( n!=P4_KEY
1a383 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f 56 54 INFO && n!=P4_VT
1a384 41 42 20 29 20 7b 0a 20 20 20 20 20 20 66 72 65 AB ) {. fre
1a385 65 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 eP4(db, n, (void
1a386 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 *)*(char**)&zP4)
1a387 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 ;. }. retu
1a388 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 rn;. }. assert
1a389 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20 ( p->nOp>0 );.
1a38a 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d 3e assert( addr<p->
1a38b 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64 64 nOp );. if( add
1a38c 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72 20 r<0 ){. addr
1a38d 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 = p->nOp - 1;.
1a38e 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f }. pOp = &p->aO
1a38f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 65 50 p[addr];. freeP
1a390 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 4(db, pOp->p4typ
1a391 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 e, pOp->p4.p);.
1a392 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a pOp->p4.p = 0;.
1a393 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54 33 if( n==P4_INT3
1a394 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74 65 2 ){. /* Note
1a395 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20 73 : this cast is s
1a396 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68 65 afe, because the
1a397 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f 69 origin data poi
1a398 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a 20 20 nt was an int.
1a399 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63 61 ** that was ca
1a39a 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20 63 st to a (const c
1a39b 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20 70 har *). */. p
1a39c 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49 54 Op->p4.i = SQLIT
1a39d 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50 34 E_PTR_TO_INT(zP4
1a39e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 );. pOp->p4ty
1a39f 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a 20 pe = P4_INT32;.
1a3a0 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 3d 3d }else if( zP4==
1a3a1 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 0 ){. pOp->p4
1a3a2 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70 2d .p = 0;. pOp-
1a3a3 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 >p4type = P4_NOT
1a3a4 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 USED;. }else if
1a3a5 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 ( n==P4_KEYINFO
1a3a6 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a ){. KeyInfo *
1a3a7 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69 6e pKeyInfo;. in
1a3a8 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65 3b t nField, nByte;
1a3a9 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20 28 .. nField = (
1a3aa 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d 3e (KeyInfo*)zP4)->
1a3ab 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79 74 nField;. nByt
1a3ac 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 e = sizeof(*pKey
1a3ad 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64 2d Info) + (nField-
1a3ae 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49 6e 1)*sizeof(pKeyIn
1a3af 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b 20 fo->aColl[0]) +
1a3b0 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65 79 nField;. pKey
1a3b1 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d 61 Info = sqlite3Ma
1a3b2 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 lloc( nByte );.
1a3b3 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 pOp->p4.pKeyI
1a3b4 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a nfo = pKeyInfo;.
1a3b5 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66 6f if( pKeyInfo
1a3b6 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 61 53 ){. u8 *aS
1a3b7 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 ortOrder;.
1a3b8 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2c memcpy(pKeyInfo,
1a3b9 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20 20 zP4, nByte);.
1a3ba 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20 3d aSortOrder =
1a3bb 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 pKeyInfo->aSort
1a3bc 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66 28 Order;. if(
1a3bd 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 aSortOrder ){.
1a3be 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 2d pKeyInfo-
1a3bf 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 >aSortOrder = (u
1a3c0 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 70 nsigned char*)&p
1a3c1 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e KeyInfo->aColl[n
1a3c2 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20 20 Field];.
1a3c3 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f 2d memcpy(pKeyInfo-
1a3c4 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53 6f >aSortOrder, aSo
1a3c5 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64 29 rtOrder, nField)
1a3c6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
1a3c7 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
1a3c8 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d 65 _KEYINFO;. }e
1a3c9 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64 62 lse{. p->db
1a3ca 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1a3cb 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 1;. pOp->p
1a3cc 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 4type = P4_NOTUS
1a3cd 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 ED;. }. }els
1a3ce 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 e if( n==P4_KEYI
1a3cf 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a 20 NFO_HANDOFF ){.
1a3d0 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 pOp->p4.p = (
1a3d1 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 void*)zP4;. p
1a3d2 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f Op->p4type = P4_
1a3d3 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73 65 KEYINFO;. }else
1a3d4 20 69 66 28 20 6e 3d 3d 50 34 5f 56 54 41 42 20 if( n==P4_VTAB
1a3d5 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 ){. pOp->p4.p
1a3d6 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 = (void*)zP4;.
1a3d7 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d pOp->p4type =
1a3d8 20 50 34 5f 56 54 41 42 3b 0a 20 20 20 20 73 71 P4_VTAB;. sq
1a3d9 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 28 56 lite3VtabLock((V
1a3da 54 61 62 6c 65 20 2a 29 7a 50 34 29 3b 0a 20 20 Table *)zP4);.
1a3db 20 20 61 73 73 65 72 74 28 20 28 28 56 54 61 62 assert( ((VTab
1a3dc 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d 70 le *)zP4)->db==p
1a3dd 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 65 20 ->db );. }else
1a3de 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20 70 if( n<0 ){. p
1a3df 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 Op->p4.p = (void
1a3e0 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e *)zP4;. pOp->
1a3e1 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e 65 64 p4type = (signed
1a3e2 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73 65 char)n;. }else
1a3e3 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 {. if( n==0 )
1a3e4 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c n = sqlite3Strl
1a3e5 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 20 70 en30(zP4);. p
1a3e6 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69 74 Op->p4.z = sqlit
1a3e7 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 e3DbStrNDup(p->d
1a3e8 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20 20 b, zP4, n);.
1a3e9 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 pOp->p4type = P4
1a3ea 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d 0a _DYNAMIC;. }.}.
1a3eb 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a .#ifndef NDEBUG.
1a3ec 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 /*.** Change the
1a3ed 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20 comment on the
1a3ee 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c the most recentl
1a3ef 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74 y coded instruct
1a3f0 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73 65 ion. Or.** inse
1a3f1 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20 61 rt a No-op and a
1a3f2 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74 dd the comment t
1a3f3 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74 72 o that new instr
1a3f4 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a uction. This.**
1a3f5 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65 20 makes the code
1a3f6 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20 64 easier to read d
1a3f7 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67 2e uring debugging.
1a3f8 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20 68 None of this h
1a3f9 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20 70 appens.** in a p
1a3fa 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64 2e roduction build.
1a3fb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a3fc 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1a3fd 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 dbeComment(Vdbe
1a3fe 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *p, const char *
1a3ff 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 zFormat, ...){.
1a400 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 va_list ap;. i
1a401 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a f( !p ) return;.
1a402 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 assert( p->nOp
1a403 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 >0 || p->aOp==0
1a404 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e );. assert( p->
1a405 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 aOp==0 || p->aOp
1a406 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d [p->nOp-1].zComm
1a407 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d ent==0 || p->db-
1a408 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b >mallocFailed );
1a409 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b . if( p->nOp ){
1a40a 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d . char **pz =
1a40b 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d &p->aOp[p->nOp-
1a40c 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 1].zComment;.
1a40d 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 va_start(ap, zF
1a40e 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 ormat);. sqli
1a40f 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c te3DbFree(p->db,
1a410 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d *pz);. *pz =
1a411 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 sqlite3VMPrintf
1a412 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c (p->db, zFormat,
1a413 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 ap);. va_end
1a414 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 (ap);. }.}.SQLI
1a415 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a416 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 sqlite3VdbeNoopC
1a417 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 omment(Vdbe *p,
1a418 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 const char *zFor
1a419 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f mat, ...){. va_
1a41a 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 list ap;. if( !
1a41b 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 p ) return;. sq
1a41c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 lite3VdbeAddOp0(
1a41d 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61 p, OP_Noop);. a
1a41e 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 ssert( p->nOp>0
1a41f 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a || p->aOp==0 );.
1a420 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 assert( p->aOp
1a421 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d ==0 || p->aOp[p-
1a422 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 >nOp-1].zComment
1a423 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 ==0 || p->db->ma
1a424 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
1a425 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 if( p->nOp ){.
1a426 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 char **pz = &p
1a427 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e ->aOp[p->nOp-1].
1a428 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 zComment;. va
1a429 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d _start(ap, zForm
1a42a 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 at);. sqlite3
1a42b 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 DbFree(p->db, *p
1a42c 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 z);. *pz = sq
1a42d 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d lite3VMPrintf(p-
1a42e 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 >db, zFormat, ap
1a42f 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 );. va_end(ap
1a430 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 );. }.}.#endif
1a431 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f /* NDEBUG */../
1a432 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1a433 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 69 76 opcode for a giv
1a434 65 6e 20 61 64 64 72 65 73 73 2e 20 20 49 66 20 en address. If
1a435 74 68 65 20 61 64 64 72 65 73 73 20 69 73 20 2d the address is -
1a436 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 1, then.** retur
1a437 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e n the most recen
1a438 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63 tly inserted opc
1a439 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 ode..**.** If a
1a43a 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1a43b 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 n error has occu
1a43c 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 rred prior to th
1a43d 65 20 63 61 6c 6c 69 6e 67 20 6f 66 20 74 68 69 e calling of thi
1a43e 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 74 68 s.** routine, th
1a43f 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 en a pointer to
1a440 61 20 64 75 6d 6d 79 20 56 64 62 65 4f 70 20 77 a dummy VdbeOp w
1a441 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e ill be returned.
1a442 20 20 54 68 61 74 20 6f 70 63 6f 64 65 0a 2a 2a That opcode.**
1a443 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 is readable and
1a444 20 77 72 69 74 61 62 6c 65 2c 20 62 75 74 20 69 writable, but i
1a445 74 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74 2e t has no effect.
1a446 20 20 54 68 65 20 72 65 74 75 72 6e 20 6f 66 20 The return of
1a447 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f 64 a dummy.** opcod
1a448 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61 6c e allows the cal
1a449 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66 75 l to continue fu
1a44a 6e 63 74 69 6f 6e 69 6e 67 20 61 66 74 65 72 20 nctioning after
1a44b 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 69 74 68 a OOM fault with
1a44c 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 6f out.** having to
1a44d 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 check to see if
1a44e 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 6f 6d the return from
1a44f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1a450 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72 a valid pointer
1a451 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74 68 ..**.** About th
1a452 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f e #ifdef SQLITE_
1a453 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f 72 OMIT_TRACE: Nor
1a454 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75 74 mally, this rout
1a455 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c ine is never cal
1a456 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70 2d led.** unless p-
1a457 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 69 73 >nOp>0. This is
1a458 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 65 20 because in the
1a459 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c 49 54 absense of SQLIT
1a45a 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a 2a E_OMIT_TRACE,.**
1a45b 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e 73 an OP_Trace ins
1a45c 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 77 61 truction is alwa
1a45d 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 20 73 ys inserted by s
1a45e 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 29 20 qlite3VdbeGet()
1a45f 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61 20 as soon as.** a
1a460 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 65 61 new VDBE is crea
1a461 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 65 20 ted. So we are
1a462 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 64 72 free to set addr
1a463 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69 74 to p->nOp-1 wit
1a464 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74 hout.** having t
1a465 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 o double-check t
1a466 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 o make sure that
1a467 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e the result is n
1a468 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 75 74 on-negative. But
1a469 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d .** if SQLITE_OM
1a46a 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 66 69 IT_TRACE is defi
1a46b 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 61 63 ned, the OP_Trac
1a46c 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 6e 64 e is omitted and
1a46d 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a 2a we do need to.*
1a46e 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 6c 75 * check the valu
1a46f 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62 65 e of p->nOp-1 be
1a470 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 2e fore continuing.
1a471 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1a472 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 TE VdbeOp *sqlit
1a473 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62 65 e3VdbeGetOp(Vdbe
1a474 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a *p, int addr){.
1a475 20 20 73 74 61 74 69 63 20 56 64 62 65 4f 70 20 static VdbeOp
1a476 64 75 6d 6d 79 3b 0a 20 20 61 73 73 65 72 74 28 dummy;. assert(
1a477 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f p->magic==VDBE_
1a478 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 MAGIC_INIT );.
1a479 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 23 69 if( addr<0 ){.#i
1a47a 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 fdef SQLITE_OMIT
1a47b 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 70 _TRACE. if( p
1a47c 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 65 74 75 72 ->nOp==0 ) retur
1a47d 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69 66 n &dummy;.#endif
1a47e 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e . addr = p->n
1a47f 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61 73 Op - 1;. }. as
1a480 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20 26 sert( (addr>=0 &
1a481 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20 7c & addr<p->nOp) |
1a482 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 | p->db->mallocF
1a483 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
1a484 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a485 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ed ){. return
1a486 20 26 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73 65 &dummy;. }else
1a487 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 2d {. return &p-
1a488 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 7d 0a >aOp[addr];. }.
1a489 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 }..#if !defined(
1a48a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
1a48b 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 AIN) || !defined
1a48c 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20 20 (NDEBUG) \.
1a48d 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f || defined(VDBE_
1a48e 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 PROFILE) || defi
1a48f 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
1a490 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 )./*.** Compute
1a491 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 a string that de
1a492 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20 70 scribes the P4 p
1a493 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e 20 arameter for an
1a494 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20 7a opcode..** Use z
1a495 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65 71 Temp for any req
1a496 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79 20 uired temporary
1a497 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a 2f buffer space..*/
1a498 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 69 .static char *di
1a499 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70 2c splayP4(Op *pOp,
1a49a 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69 6e char *zTemp, in
1a49b 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61 72 t nTemp){. char
1a49c 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 *zP4 = zTemp;.
1a49d 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e 3d assert( nTemp>=
1a49e 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 20 );. switch(
1a49f 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a 20 pOp->p4type ){.
1a4a0 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e case P4_KEYIN
1a4a1 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20 63 FO_STATIC:. c
1a4a2 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a 20 ase P4_KEYINFO:
1a4a3 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a {. int i, j
1a4a4 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 ;. KeyInfo
1a4a5 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d *pKeyInfo = pOp-
1a4a6 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 >p4.pKeyInfo;.
1a4a7 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a4a8 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1a4a9 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22 2c p, "keyinfo(%d",
1a4aa 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c pKeyInfo->nFiel
1a4ab 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73 71 d);. i = sq
1a4ac 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 lite3Strlen30(zT
1a4ad 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 emp);. for(
1a4ae 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d j=0; j<pKeyInfo-
1a4af 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 >nField; j++){.
1a4b0 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a CollSeq *
1a4b1 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f pColl = pKeyInfo
1a4b2 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 ->aColl[j];.
1a4b3 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b if( pColl ){
1a4b4 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e . int n
1a4b5 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
1a4b6 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 30(pColl->zName)
1a4b7 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1a4b8 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 i+n>nTemp-6 ){.
1a4b9 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 memcp
1a4ba 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e 2e y(&zTemp[i],",..
1a4bb 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 .",4);.
1a4bc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1a4bd 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
1a4be 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c 27 zTemp[i++] = ','
1a4bf 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1a4c0 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f pKeyInfo->aSortO
1a4c1 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e 66 6f rder && pKeyInfo
1a4c2 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d 20 ->aSortOrder[j]
1a4c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a ){. z
1a4c4 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27 3b Temp[i++] = '-';
1a4c5 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
1a4c6 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a memcpy(&z
1a4c7 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d 3e Temp[i], pColl->
1a4c8 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20 20 zName,n+1);.
1a4c9 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20 20 i += n;.
1a4ca 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1a4cb 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a 20 i+4<nTemp-6 ){.
1a4cc 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 memcpy(
1a4cd 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c 22 &zTemp[i],",nil"
1a4ce 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 ,4);. i
1a4cf 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d += 4;. }
1a4d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a . }. z
1a4d1 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27 3b Temp[i++] = ')';
1a4d2 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d 20 . zTemp[i]
1a4d3 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 = 0;. asser
1a4d4 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20 20 t( i<nTemp );.
1a4d5 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1a4d6 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 4f 4c . case P4_COL
1a4d7 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 43 6f LSEQ: {. Co
1a4d8 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 llSeq *pColl = p
1a4d9 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 Op->p4.pColl;.
1a4da 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a4db 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1a4dc 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32 30 p, "collseq(%.20
1a4dd 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d s)", pColl->zNam
1a4de 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b e);. break;
1a4df 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1a4e0 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 P4_FUNCDEF: {.
1a4e1 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 FuncDef *pDe
1a4e2 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e f = pOp->p4.pFun
1a4e3 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 c;. sqlite3
1a4e4 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c _snprintf(nTemp,
1a4e5 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 29 22 zTemp, "%s(%d)"
1a4e6 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 70 , pDef->zName, p
1a4e7 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20 20 Def->nArg);.
1a4e8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 break;. }.
1a4e9 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36 34 case P4_INT64
1a4ea 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 : {. sqlite
1a4eb 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 3_snprintf(nTemp
1a4ec 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c , zTemp, "%lld",
1a4ed 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29 3b *pOp->p4.pI64);
1a4ee 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1a4ef 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f }. case P4_
1a4f0 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20 73 INT32: {. s
1a4f1 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a4f2 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 nTemp, zTemp, "%
1a4f3 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b 0a d", pOp->p4.i);.
1a4f4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1a4f5 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 52 }. case P4_R
1a4f6 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c EAL: {. sql
1a4f7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 ite3_snprintf(nT
1a4f8 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 emp, zTemp, "%.1
1a4f9 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 6g", *pOp->p4.pR
1a4fa 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 eal);. brea
1a4fb 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 k;. }. cas
1a4fc 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 e P4_MEM: {.
1a4fd 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 4f Mem *pMem = pO
1a4fe 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20 20 p->p4.pMem;.
1a4ff 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d assert( (pMem-
1a500 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1a501 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 l)==0 );. i
1a502 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 f( pMem->flags &
1a503 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 MEM_Str ){.
1a504 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d 3e zP4 = pMem->
1a505 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 z;. }else i
1a506 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 f( pMem->flags &
1a507 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 MEM_Int ){.
1a508 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 sqlite3_snpr
1a509 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d intf(nTemp, zTem
1a50a 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d 2d p, "%lld", pMem-
1a50b 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c >u.i);. }el
1a50c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 se if( pMem->fla
1a50d 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b gs & MEM_Real ){
1a50e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a50f 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c _snprintf(nTemp,
1a510 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c zTemp, "%.16g",
1a511 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20 20 pMem->r);.
1a512 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1a513 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c assert( pMem->fl
1a514 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 ags & MEM_Blob )
1a515 3b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 ;. zP4 =
1a516 22 28 62 6c 6f 62 29 22 3b 0a 20 20 20 20 20 20 "(blob)";.
1a517 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
1a518 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
1a519 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1a51a 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20 50 TABLE. case P
1a51b 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 20 20 4_VTAB: {.
1a51c 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1a51d 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 tab = pOp->p4.pV
1a51e 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 tab->pVtab;.
1a51f 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a520 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c tf(nTemp, zTemp,
1a521 20 22 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 "vtab:%p:%p", p
1a522 56 74 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f Vtab, pVtab->pMo
1a523 64 75 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 65 dule);. bre
1a524 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
1a525 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 . case P4_INT
1a526 41 52 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 ARRAY: {. s
1a527 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 qlite3_snprintf(
1a528 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 nTemp, zTemp, "i
1a529 6e 74 61 72 72 61 79 22 29 3b 0a 20 20 20 20 20 ntarray");.
1a52a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1a52b 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52 4f case P4_SUBPRO
1a52c 47 52 41 4d 3a 20 7b 0a 20 20 20 20 20 20 73 71 GRAM: {. sq
1a52d 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e lite3_snprintf(n
1a52e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 70 72 Temp, zTemp, "pr
1a52f 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20 62 ogram");. b
1a530 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
1a531 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
1a532 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a zP4 = pOp->p4.z
1a533 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 34 3d ;. if( zP4=
1a534 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50 =0 ){. zP
1a535 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20 4 = zTemp;.
1a536 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b zTemp[0] = 0;
1a537 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
1a538 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34 }. assert( zP4
1a539 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 !=0 );. return
1a53a 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f zP4;.}.#endif../
1a53b 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f 20 *.** Declare to
1a53c 74 68 65 20 56 64 62 65 20 74 68 61 74 20 74 68 the Vdbe that th
1a53d 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 20 61 e BTree object a
1a53e 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20 t db->aDb[i] is
1a53f 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f used..*/.SQLITE_
1a540 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
1a541 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 ite3VdbeUsesBtre
1a542 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 e(Vdbe *p, int i
1a543 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 20 ){. int mask;.
1a544 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 assert( i>=0 &&
1a545 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26 26 i<p->db->nDb &&
1a546 20 69 3c 73 69 7a 65 6f 66 28 75 33 32 29 2a 38 i<sizeof(u32)*8
1a547 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c );. assert( i<
1a548 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62 (int)sizeof(p->b
1a549 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20 treeMask)*8 );.
1a54a 20 6d 61 73 6b 20 3d 20 28 28 75 33 32 29 31 29 mask = ((u32)1)
1a54b 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e 62 <<i;. if( (p->b
1a54c 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 treeMask & mask)
1a54d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62 74 ==0 ){. p->bt
1a54e 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b 3b reeMask |= mask;
1a54f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1a550 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 eMutexArrayInser
1a551 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70 2d t(&p->aMutex, p-
1a552 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 >db->aDb[i].pBt)
1a553 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64 65 ;. }.}...#if de
1a554 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 fined(VDBE_PROFI
1a555 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 LE) || defined(S
1a556 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a QLITE_DEBUG)./*.
1a557 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67 6c ** Print a singl
1a558 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73 20 e opcode. This
1a559 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 routine is used
1a55a 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f 6e for debugging on
1a55b 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ly..*/.SQLITE_PR
1a55c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 IVATE void sqlit
1a55d 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 e3VdbePrintOp(FI
1a55e 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70 63 LE *pOut, int pc
1a55f 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63 68 , Op *pOp){. ch
1a560 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72 20 ar *zP4;. char
1a561 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61 74 zPtr[50];. stat
1a562 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a ic const char *z
1a563 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20 25 Format1 = "%4d %
1a564 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 34 64 -13s %4d %4d %4d
1a565 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e 22 %-4s %.2X %s\n"
1a566 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30 20 ;. if( pOut==0
1a567 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74 3b ) pOut = stdout;
1a568 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61 79 . zP4 = display
1a569 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73 69 P4(pOp, zPtr, si
1a56a 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20 66 zeof(zPtr));. f
1a56b 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46 6f printf(pOut, zFo
1a56c 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20 20 rmat1, pc, .
1a56d 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 4e sqlite3OpcodeN
1a56e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 ame(pOp->opcode)
1a56f 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e , pOp->p1, pOp->
1a570 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50 34 p2, pOp->p3, zP4
1a571 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64 65 , pOp->p5,.#ifde
1a572 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1a573 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 pOp->zComme
1a574 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 nt ? pOp->zComme
1a575 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20 20 nt : "".#else.
1a576 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20 20 "".#endif.
1a577 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75 74 );. fflush(pOut
1a578 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a );.}.#endif../*.
1a579 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61 72 ** Release an ar
1a57a 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c 65 ray of N Mem ele
1a57b 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 ments.*/.static
1a57c 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d 41 void releaseMemA
1a57d 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e 74 rray(Mem *p, int
1a57e 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 N){. if( p &&
1a57f 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 45 N ){. Mem *pE
1a580 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 nd;. sqlite3
1a581 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 *db = p->db;.
1a582 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 u8 malloc_faile
1a583 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 d = db->mallocFa
1a584 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70 45 iled;. for(pE
1a585 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e 64 nd=&p[N]; p<pEnd
1a586 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 ; p++){. as
1a587 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d 70 sert( (&p[1])==p
1a588 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d 3d End || p[0].db==
1a589 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20 20 p[1].db );..
1a58a 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 /* This block
1a58b 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e 6c is really an inl
1a58c 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 ined version of
1a58d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 sqlite3VdbeMemRe
1a58e 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a 2a lease(). **
1a58f 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76 61 that takes adva
1a590 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61 63 ntage of the fac
1a591 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 t that the memor
1a592 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73 20 y cell value is
1a593 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 . ** being
1a594 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74 65 set to NULL afte
1a595 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79 20 r releasing any
1a596 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63 65 dynamic resource
1a597 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 s.. **.
1a598 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66 69 ** The justifi
1a599 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c 69 cation for dupli
1a59a 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20 74 cating code is t
1a59b 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f hat according to
1a59c 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 67 . ** callg
1a59d 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73 65 rind, this cause
1a59e 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 73 74 s a certain test
1a59f 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 68 65 case to hit the
1a5a0 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20 20 CPU 4.7 .
1a5a1 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73 20 ** percent less
1a5a2 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63 20 (x86 linux, gcc
1a5a3 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20 2d version 4.1.2, -
1a5a4 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20 20 O6) than if .
1a5a5 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d ** sqlite3Mem
1a5a6 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 20 63 Release() were c
1a5a7 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65 2e alled from here.
1a5a8 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73 20 With -O2, this
1a5a9 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20 74 jumps. ** t
1a5aa 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20 54 o 6.6 percent. T
1a5ab 68 65 20 74 65 73 74 20 63 61 73 65 20 69 73 20 he test case is
1a5ac 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20 72 inserting 1000 r
1a5ad 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 ows into a table
1a5ae 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 . ** with
1a5af 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e 67 no indexes using
1a5b0 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61 72 a single prepar
1a5b1 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d ed INSERT statem
1a5b2 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20 20 ent, bind() .
1a5b3 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74 28 ** and reset(
1a5b4 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20 67 ). Inserts are g
1a5b5 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74 72 rouped into a tr
1a5b6 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20 20 ansaction..
1a5b7 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d */. if( p-
1a5b8 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67 7c >flags&(MEM_Agg|
1a5b9 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61 6d MEM_Dyn|MEM_Fram
1a5ba 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29 7b e|MEM_RowSet) ){
1a5bb 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a5bc 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 VdbeMemRelease(p
1a5bd 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
1a5be 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b f( p->zMalloc ){
1a5bf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1a5c0 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 4d DbFree(db, p->zM
1a5c1 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 alloc);.
1a5c2 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a p->zMalloc = 0;.
1a5c3 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70 }.. p
1a5c4 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 ->flags = MEM_Nu
1a5c5 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 ll;. }. db
1a5c6 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1a5c7 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a malloc_failed;.
1a5c8 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c }.}../*.** Del
1a5c9 65 74 65 20 61 20 56 64 62 65 46 72 61 6d 65 20 ete a VdbeFrame
1a5ca 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73 20 63 object and its c
1a5cb 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46 72 61 ontents. VdbeFra
1a5cc 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65 0a 2a me objects are.*
1a5cd 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 * allocated by t
1a5ce 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70 he OP_Program op
1a5cf 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65 33 56 code in sqlite3V
1a5d0 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 dbeExec()..*/.SQ
1a5d1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1a5d2 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 d sqlite3VdbeFra
1a5d3 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 meDelete(VdbeFra
1a5d4 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b me *p){. int i;
1a5d5 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20 56 . Mem *aMem = V
1a5d6 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b 0a dbeFrameMem(p);.
1a5d7 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 VdbeCursor **a
1a5d8 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72 73 pCsr = (VdbeCurs
1a5d9 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e 6e or **)&aMem[p->n
1a5da 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f 72 ChildMem];. for
1a5db 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 69 6c (i=0; i<p->nChil
1a5dc 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 dCsr; i++){.
1a5dd 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
1a5de 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70 43 73 ursor(p->v, apCs
1a5df 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 6c r[i]);. }. rel
1a5e0 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61 4d 65 easeMemArray(aMe
1a5e1 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 29 m, p->nChildMem)
1a5e2 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1a5e3 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b 0a e(p->v->db, p);.
1a5e4 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }..#ifndef SQLIT
1a5e5 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f E_OMIT_EXPLAIN./
1a5e6 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 73 74 *.** Give a list
1a5e7 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f 67 72 ing of the progr
1a5e8 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 am in the virtua
1a5e9 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a l machine..**.**
1a5ea 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 The interface i
1a5eb 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 73 71 s the same as sq
1a5ec 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e lite3VdbeExec().
1a5ed 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 But instead of
1a5ee 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 .** running the
1a5ef 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65 73 code, it invokes
1a5f0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e the callback on
1a5f1 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 73 74 ce for each inst
1a5f2 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 ruction..** This
1a5f3 20 66 65 61 74 75 72 65 20 69 73 20 75 73 65 64 feature is used
1a5f4 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22 45 to implement "E
1a5f5 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 XPLAIN"..**.** W
1a5f6 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d hen p->explain==
1a5f7 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 1, each instruct
1a5f8 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e 20 20 ion is listed.
1a5f9 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c 61 When.** p->expla
1a5fa 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f 45 in==2, only OP_E
1a5fb 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 74 69 xplain instructi
1a5fc 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64 20 61 ons are listed a
1a5fd 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72 65 20 nd these.** are
1a5fe 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66 66 65 shown in a diffe
1a5ff 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70 2d rent format. p-
1a600 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20 75 >explain==2 is u
1a601 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 sed to implement
1a602 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55 45 52 .** EXPLAIN QUER
1a603 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 Y PLAN..*/.SQLIT
1a604 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
1a605 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 0a 20 lite3VdbeList(.
1a606 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 20 20 Vdbe *p
1a607 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
1a608 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 e VDBE */.){. i
1a609 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 nt nRow;
1a60a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a60b 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d /* Total num
1a60c 62 65 72 20 6f 66 20 72 6f 77 73 20 74 6f 20 72 ber of rows to r
1a60d 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e eturn */. int n
1a60e 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Sub = 0;
1a60f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a610 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 75 62 /* Number of sub
1a611 2d 76 64 62 65 73 20 73 65 65 6e 20 73 6f 20 66 -vdbes seen so f
1a612 61 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 ar */. SubProgr
1a613 61 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b 20 am **apSub = 0;
1a614 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a615 41 72 72 61 79 20 6f 66 20 73 75 62 2d 76 64 62 Array of sub-vdb
1a616 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53 75 es */. Mem *pSu
1a617 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 b = 0;. sqlite3
1a618 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 *db = p->db;.
1a619 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 int i;. int rc
1a61a 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d = SQLITE_OK;. M
1a61b 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 em *pMem = p->pR
1a61c 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 esultSet = &p->a
1a61d 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65 72 Mem[1];.. asser
1a61e 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 3b t( p->explain );
1a61f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 . assert( p->ma
1a620 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic==VDBE_MAGIC_
1a621 52 55 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 RUN );. assert(
1a622 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 db->magic==SQLI
1a623 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b TE_MAGIC_BUSY );
1a624 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 . assert( p->rc
1a625 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 ==SQLITE_OK || p
1a626 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ->rc==SQLITE_BUS
1a627 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 Y || p->rc==SQLI
1a628 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20 2f TE_NOMEM );.. /
1a629 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 * Even though th
1a62a 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 6e is opcode does n
1a62b 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20 73 ot use dynamic s
1a62c 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a 20 trings for. **
1a62d 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73 75 the result, resu
1a62e 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 lt columns may b
1a62f 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69 66 ecome dynamic if
1a630 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73 0a the user calls.
1a631 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c ** sqlite3_col
1a632 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63 61 umn_text16(), ca
1a633 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61 74 using a translat
1a634 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65 6e ion to UTF-16 en
1a635 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 72 coding.. */. r
1a636 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 eleaseMemArray(p
1a637 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 28 20 Mem, 8);.. if(
1a638 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f p->rc==SQLITE_NO
1a639 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 MEM ){. /* Th
1a63a 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20 is happens if a
1a63b 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20 malloc() inside
1a63c 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
1a63d 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 3_column_text()
1a63e 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 or. ** sqlite
1a63f 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 3_column_text16(
1a640 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20 ) failed. */.
1a641 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c db->mallocFail
1a642 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 ed = 1;. retu
1a643 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b rn SQLITE_ERROR;
1a644 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 . }.. /* Figur
1a645 65 20 6f 75 74 20 74 6f 74 61 6c 20 6e 75 6d 62 e out total numb
1a646 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 er of rows that
1a647 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 will be returned
1a648 20 62 79 20 74 68 69 73 20 0a 20 20 2a 2a 20 45 by this . ** E
1a649 58 50 4c 41 49 4e 20 70 72 6f 67 72 61 6d 2e 20 XPLAIN program.
1a64a 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d 3e */. nRow = p->
1a64b 6e 4f 70 3b 0a 20 20 69 66 28 20 70 2d 3e 65 78 nOp;. if( p->ex
1a64c 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 plain==1 ){.
1a64d 70 53 75 62 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b pSub = &p->aMem[
1a64e 39 5d 3b 0a 20 20 20 20 69 66 28 20 70 53 75 62 9];. if( pSub
1a64f 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 ->flags&MEM_Blob
1a650 20 29 7b 0a 20 20 20 20 20 20 6e 53 75 62 20 3d ){. nSub =
1a651 20 70 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66 28 pSub->n/sizeof(
1a652 56 64 62 65 2a 29 3b 0a 20 20 20 20 20 20 61 70 Vdbe*);. ap
1a653 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 Sub = (SubProgra
1a654 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 m **)pSub->z;.
1a655 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b }. for(i=0;
1a656 20 69 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a 20 i<nSub; i++){.
1a657 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70 53 nRow += apS
1a658 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 ub[i]->nOp;.
1a659 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20 }. }.. do{.
1a65a 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 i = p->pc++;.
1a65b 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20 26 }while( i<nRow &
1a65c 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 & p->explain==2
1a65d 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 && p->aOp[i].opc
1a65e 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 ode!=OP_Explain
1a65f 29 3b 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f 77 );. if( i>=nRow
1a660 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 ){. p->rc =
1a661 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 SQLITE_OK;. r
1a662 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b c = SQLITE_DONE;
1a663 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d . }else if( db-
1a664 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 >u1.isInterrupte
1a665 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d d ){. p->rc =
1a666 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 SQLITE_INTERRUP
1a667 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 T;. rc = SQLI
1a668 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 TE_ERROR;. sq
1a669 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1a66a 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1a66b 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 "%s", sqlite3Err
1a66c 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d Str(p->rc));. }
1a66d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a else{. char *
1a66e 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a z;. Op *pOp;.
1a66f 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f 70 if( i<p->nOp
1a670 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 ){. pOp =
1a671 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20 &p->aOp[i];.
1a672 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 }else{. int
1a673 20 6a 3b 0a 20 20 20 20 20 20 69 20 2d 3d 20 70 j;. i -= p
1a674 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 66 6f 72 ->nOp;. for
1a675 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 75 62 5b 6a (j=0; i>=apSub[j
1a676 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20 20 ]->nOp; j++){.
1a677 20 20 20 20 20 20 69 20 2d 3d 20 61 70 53 75 62 i -= apSub
1a678 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 [j]->nOp;.
1a679 7d 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26 61 }. pOp = &a
1a67a 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b pSub[j]->aOp[i];
1a67b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1a67c 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a ->explain==1 ){.
1a67d 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1a67e 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1a67f 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1a680 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a SQLITE_INTEGER;.
1a681 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 pMem->u.i
1a682 3d 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 = i;
1a683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a684 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20 63 /* Program c
1a685 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 ounter */.
1a686 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20 20 pMem++;. .
1a687 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1a688 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53 74 EM_Static|MEM_St
1a689 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 r|MEM_Term;.
1a68a 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 pMem->z = (cha
1a68b 72 2a 29 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 r*)sqlite3Opcode
1a68c 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65 Name(pOp->opcode
1a68d 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a 2f ); /* Opcode */
1a68e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1a68f 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 Mem->z!=0 );.
1a690 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c pMem->n = sql
1a691 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 ite3Strlen30(pMe
1a692 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 m->z);. pMe
1a693 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 m->type = SQLITE
1a694 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 _TEXT;. pMe
1a695 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f m->enc = SQLITE_
1a696 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d UTF8;. pMem
1a697 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 ++;.. if( p
1a698 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 Op->p4type==P4_S
1a699 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 UBPROGRAM ){.
1a69a 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d int nByte =
1a69b 20 28 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 (nSub+1)*sizeof
1a69c 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 (SubProgram*);.
1a69d 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 int j;.
1a69e 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
1a69f 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 <nSub; j++){.
1a6a0 20 20 20 20 20 20 20 69 66 28 20 61 70 53 75 62 if( apSub
1a6a1 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50 72 [j]==pOp->p4.pPr
1a6a2 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 ogram ) break;.
1a6a3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1a6a4 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26 20 if( j==nSub &&
1a6a5 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 SQLITE_OK==sqlit
1a6a6 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 53 e3VdbeMemGrow(pS
1a6a7 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20 29 7b ub, nByte, 1) ){
1a6a8 0a 20 20 20 20 20 20 20 20 20 20 61 70 53 75 62 . apSub
1a6a9 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a = (SubProgram *
1a6aa 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 20 *)pSub->z;.
1a6ab 20 20 20 20 20 61 70 53 75 62 5b 6e 53 75 62 2b apSub[nSub+
1a6ac 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 +] = pOp->p4.pPr
1a6ad 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 20 20 ogram;.
1a6ae 20 70 53 75 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 pSub->flags |=
1a6af 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20 20 MEM_Blob;.
1a6b0 20 20 20 20 70 53 75 62 2d 3e 6e 20 3d 20 6e 53 pSub->n = nS
1a6b1 75 62 2a 73 69 7a 65 6f 66 28 53 75 62 50 72 6f ub*sizeof(SubPro
1a6b2 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 20 20 gram*);.
1a6b3 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a }. }. }.
1a6b4 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 . pMem->flags
1a6b5 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 = MEM_Int;.
1a6b6 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d pMem->u.i = pOp-
1a6b7 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 >p1;
1a6b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1a6b9 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d P1 */. pMem-
1a6ba 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 >type = SQLITE_I
1a6bb 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d NTEGER;. pMem
1a6bc 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 ++;.. pMem->f
1a6bd 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1a6be 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 pMem->u.i =
1a6bf 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 20 20 pOp->p2;
1a6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6c1 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20 70 /* P2 */. p
1a6c2 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1a6c3 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 TE_INTEGER;.
1a6c4 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 pMem++;.. if(
1a6c5 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 p->explain==1 )
1a6c6 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c {. pMem->fl
1a6c7 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 ags = MEM_Int;.
1a6c8 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d pMem->u.i =
1a6c9 20 70 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20 20 pOp->p3;
1a6ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a6cb 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20 20 /* P3 */.
1a6cc 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1a6cd 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 QLITE_INTEGER;.
1a6ce 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20 pMem++;.
1a6cf 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 }.. if( sqli
1a6d0 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 te3VdbeMemGrow(p
1a6d1 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20 20 Mem, 32, 0) ){
1a6d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 /* P4
1a6d3 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1a6d4 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 p->db->mallocFa
1a6d5 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65 iled );. re
1a6d6 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1a6d7 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 R;. }. pMe
1a6d8 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 m->flags = MEM_D
1a6d9 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 yn|MEM_Str|MEM_T
1a6da 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69 73 erm;. z = dis
1a6db 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65 6d playP4(pOp, pMem
1a6dc 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69 66 ->z, 32);. if
1a6dd 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b 0a ( z!=pMem->z ){.
1a6de 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1a6df 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 6d 2c eMemSetStr(pMem,
1a6e0 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 z, -1, SQLITE_U
1a6e1 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c TF8, 0);. }el
1a6e2 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
1a6e3 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a ( pMem->z!=0 );.
1a6e4 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 pMem->n =
1a6e5 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 sqlite3Strlen30(
1a6e6 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 pMem->z);.
1a6e7 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 pMem->enc = SQLI
1a6e8 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a 20 TE_UTF8;. }.
1a6e9 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1a6ea 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 SQLITE_TEXT;.
1a6eb 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66 pMem++;.. if
1a6ec 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 ( p->explain==1
1a6ed 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c ){. if( sql
1a6ee 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 ite3VdbeMemGrow(
1a6ef 70 4d 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a 20 pMem, 4, 0) ){.
1a6f0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1a6f1 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ->db->mallocFail
1a6f2 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 ed );. re
1a6f3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f turn SQLITE_ERRO
1a6f4 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 R;. }.
1a6f5 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d pMem->flags = M
1a6f6 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d EM_Dyn|MEM_Str|M
1a6f7 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 EM_Term;. p
1a6f8 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20 20 Mem->n = 2;.
1a6f9 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e sqlite3_snprin
1a6fa 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 tf(3, pMem->z, "
1a6fb 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29 3b %.2x", pOp->p5);
1a6fc 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20 20 /* P5 */.
1a6fd 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 pMem->type = S
1a6fe 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 QLITE_TEXT;.
1a6ff 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 pMem->enc = SQ
1a700 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 LITE_UTF8;.
1a701 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66 64 pMem++;. .#ifd
1a702 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a ef SQLITE_DEBUG.
1a703 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 7a if( pOp->z
1a704 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 Comment ){.
1a705 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1a706 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 MEM_Str|MEM_Ter
1a707 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d m;. pMem-
1a708 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 >z = pOp->zComme
1a709 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d nt;. pMem
1a70a 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 ->n = sqlite3Str
1a70b 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a len30(pMem->z);.
1a70c 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e pMem->en
1a70d 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
1a70e 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 . pMem->t
1a70f 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 ype = SQLITE_TEX
1a710 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 T;. }else.#
1a711 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 endif. {.
1a712 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1a713 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20 20 s = MEM_Null;
1a714 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a715 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 2a /* Comment *
1a716 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e /. pMem->
1a717 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 type = SQLITE_NU
1a718 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 LL;. }.
1a719 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43 6f }.. p->nResCo
1a71a 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70 2d lumn = 8 - 5*(p-
1a71b 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20 20 >explain-1);.
1a71c 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f p->rc = SQLITE_
1a71d 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c OK;. rc = SQL
1a71e 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20 72 ITE_ROW;. }. r
1a71f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
1a720 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1a721 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23 69 T_EXPLAIN */..#i
1a722 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1a723 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 G./*.** Print th
1a724 65 20 53 51 4c 20 74 68 61 74 20 77 61 73 20 75 e SQL that was u
1a725 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 sed to generate
1a726 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a a VDBE program..
1a727 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1a728 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1a729 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 20 bePrintSql(Vdbe
1a72a 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d *p){. int nOp =
1a72b 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f p->nOp;. VdbeO
1a72c 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e 4f p *pOp;. if( nO
1a72d 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 p<1 ) return;.
1a72e 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d pOp = &p->aOp[0]
1a72f 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 ;. if( pOp->opc
1a730 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 ode==OP_Trace &&
1a731 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b pOp->p4.z!=0 ){
1a732 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1a733 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a *z = pOp->p4.z;.
1a734 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 while( sqlit
1a735 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29 20 e3Isspace(*z) )
1a736 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 z++;. printf(
1a737 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20 7a "SQL: [%s]\n", z
1a738 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a );. }.}.#endif.
1a739 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
1a73a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 LITE_OMIT_TRACE)
1a73b 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
1a73c 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 TE_ENABLE_IOTRAC
1a73d 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 E)./*.** Print a
1a73e 6e 20 49 4f 54 52 41 43 45 20 6d 65 73 73 61 67 n IOTRACE messag
1a73f 65 20 73 68 6f 77 69 6e 67 20 53 51 4c 20 63 6f e showing SQL co
1a740 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 ntent..*/.SQLITE
1a741 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1a742 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 lite3VdbeIOTrace
1a743 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 Sql(Vdbe *p){.
1a744 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 int nOp = p->nOp
1a745 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b ;. VdbeOp *pOp;
1a746 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6f . if( sqlite3Io
1a747 54 72 61 63 65 3d 3d 30 20 29 20 72 65 74 75 72 Trace==0 ) retur
1a748 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 n;. if( nOp<1 )
1a749 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d return;. pOp =
1a74a 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 &p->aOp[0];. i
1a74b 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1a74c 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d OP_Trace && pOp-
1a74d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 >p4.z!=0 ){.
1a74e 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63 68 int i, j;. ch
1a74f 61 72 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20 20 ar z[1000];.
1a750 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 sqlite3_snprintf
1a751 28 73 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20 22 (sizeof(z), z, "
1a752 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b %s", pOp->p4.z);
1a753 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73 71 . for(i=0; sq
1a754 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 lite3Isspace(z[i
1a755 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 ]); i++){}. f
1a756 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b or(j=0; z[i]; i+
1a757 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 +){. if( sq
1a758 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b 69 lite3Isspace(z[i
1a759 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 ]) ){. if
1a75a 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29 7b ( z[i-1]!=' ' ){
1a75b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b . z[j++
1a75c 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 ] = ' ';.
1a75d 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1a75e 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d z[j++] =
1a75f 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 z[i];. }.
1a760 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d 20 }. z[j] =
1a761 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 6f 0;. sqlite3Io
1a762 54 72 61 63 65 28 22 53 51 4c 20 25 73 5c 6e 22 Trace("SQL %s\n"
1a763 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 , z);. }.}.#end
1a764 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d if /* !SQLITE_OM
1a765 49 54 5f 54 52 41 43 45 20 26 26 20 53 51 4c 49 IT_TRACE && SQLI
1a766 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 TE_ENABLE_IOTRAC
1a767 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f E */../*.** Allo
1a768 63 61 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 cate space from
1a769 61 20 66 69 78 65 64 20 73 69 7a 65 20 62 75 66 a fixed size buf
1a76a 66 65 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20 70 fer. Make *pp p
1a76b 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 61 oint to the.** a
1a76c 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e 20 llocated space.
1a76d 20 28 4e 6f 74 65 3a 20 20 70 70 20 69 73 20 61 (Note: pp is a
1a76e 20 63 68 61 72 2a 20 72 61 74 68 65 72 20 74 68 char* rather th
1a76f 61 6e 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a 2a an a void** to.*
1a770 2a 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 * work around th
1a771 65 20 70 6f 69 6e 74 65 72 20 61 6c 69 61 73 69 e pointer aliasi
1a772 6e 67 20 72 75 6c 65 73 20 6f 66 20 43 2e 29 20 ng rules of C.)
1a773 20 2a 70 70 20 73 68 6f 75 6c 64 20 69 6e 69 74 *pp should init
1a774 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72 6f ially.** be zero
1a775 2e 20 20 49 66 20 2a 70 70 20 69 73 20 6e 6f 74 . If *pp is not
1a776 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e zero, that mean
1a777 73 20 74 68 61 74 20 74 68 65 20 73 70 61 63 65 s that the space
1a778 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 has already.**
1a779 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 been allocated a
1a77a 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 nd this routine
1a77b 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a is a noop..**.**
1a77c 20 6e 42 79 74 65 20 69 73 20 74 68 65 20 6e 75 nByte is the nu
1a77d 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1a77e 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e 0a 2a space needed..*
1a77f 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f 69 *.** *ppFrom poi
1a780 6e 74 20 74 6f 20 61 76 61 69 6c 61 62 6c 65 20 nt to available
1a781 73 70 61 63 65 20 61 6e 64 20 70 45 6e 64 20 70 space and pEnd p
1a782 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 65 6e 64 oints to the end
1a783 20 6f 66 20 74 68 65 0a 2a 2a 20 61 76 61 69 6c of the.** avail
1a784 61 62 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a 2a able space..**.*
1a785 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 20 63 * *pnByte is a c
1a786 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e 75 ounter of the nu
1a787 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 mber of bytes of
1a788 20 73 70 61 63 65 20 74 68 61 74 20 68 61 76 65 space that have
1a789 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61 6c failed.** to al
1a78a 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65 72 locate. If ther
1a78b 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 65 6e e is insufficien
1a78c 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46 72 t space in *ppFr
1a78d 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 om to satisfy th
1a78e 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74 68 e.** request, th
1a78f 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e en increment *pn
1a790 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f 75 Byte by the amou
1a791 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 nt of the reques
1a792 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 t..*/.static voi
1a793 64 20 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20 20 d allocSpace(.
1a794 63 68 61 72 20 2a 70 70 2c 20 20 20 20 20 20 20 char *pp,
1a795 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 /* IN/OUT:
1a796 53 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e 74 Set *pp to point
1a797 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 to allocated bu
1a798 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 ffer */. int nB
1a799 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f yte, /
1a79a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1a79b 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f s to allocate */
1a79c 0a 20 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c 20 . u8 **ppFrom,
1a79d 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 /* IN/OU
1a79e 54 3a 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f 6d T: Allocate from
1a79f 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75 38 *ppFrom */. u8
1a7a0 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 *pEnd,
1a7a1 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1a7a2 20 31 20 62 79 74 65 20 70 61 73 74 20 74 68 65 1 byte past the
1a7a3 20 65 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d 20 end of *ppFrom
1a7a4 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 buffer */. int
1a7a5 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20 20 20 *pnByte
1a7a6 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69 6f /* If allocatio
1a7a7 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64 65 n cannot be made
1a7a8 2c 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e 42 , increment *pnB
1a7a9 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 yte */.){. asse
1a7aa 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 rt( EIGHT_BYTE_A
1a7ab 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f 6d LIGNMENT(*ppFrom
1a7ac 29 20 29 3b 0a 20 20 69 66 28 20 28 2a 28 76 6f ) );. if( (*(vo
1a7ad 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 7b 0a 20 id**)pp)==0 ){.
1a7ae 20 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 nByte = ROUND
1a7af 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66 8(nByte);. if
1a7b0 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42 79 ( &(*ppFrom)[nBy
1a7b1 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0a 20 te] <= pEnd ){.
1a7b2 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70 70 *(void**)pp
1a7b3 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 46 72 = (void *)*ppFr
1a7b4 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 72 6f om;. *ppFro
1a7b5 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 m += nByte;.
1a7b6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70 6e }else{. *pn
1a7b7 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 Byte += nByte;.
1a7b8 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
1a7b9 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72 74 * Prepare a virt
1a7ba 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72 20 ual machine for
1a7bb 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69 73 execution. This
1a7bc 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67 73 involves things
1a7bd 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c 6f such.** as allo
1a7be 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70 61 cating stack spa
1a7bf 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a ce and initializ
1a7c0 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d 20 ing the program
1a7c1 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74 65 counter..** Afte
1a7c2 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20 62 r the VDBE has b
1a7c3 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63 61 e prepped, it ca
1a7c4 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62 79 n be executed by
1a7c5 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 one or more.**
1a7c6 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
1a7c7 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a 2a VdbeExec(). .**
1a7c8 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 .** This is the
1a7c9 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76 65 only way to move
1a7ca 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44 42 a VDBE from VDB
1a7cb 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f 0a E_MAGIC_INIT to.
1a7cc 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 ** VDBE_MAGIC_RU
1a7cd 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 N..**.** This fu
1a7ce 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 nction may be ca
1a7cf 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f lled more than o
1a7d0 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 nce on a single
1a7d1 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
1a7d2 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 63 61 .** The first ca
1a7d3 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 6c 65 ll is made while
1a7d4 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 53 compiling the S
1a7d5 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 53 75 QL statement. Su
1a7d6 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c bsequent.** call
1a7d7 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 70 61 s are made as pa
1a7d8 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73 rt of the proces
1a7d9 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 20 61 s of resetting a
1a7da 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 statement to be
1a7db 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 64 20 .** re-executed
1a7dc 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f 20 (from a call to
1a7dd 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 29 sqlite3_reset())
1a7de 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65 6d . The nVar, nMem
1a7df 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61 6e , nCursor .** an
1a7e0 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 72 61 d isExplain para
1a7e1 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 meters are only
1a7e2 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 20 76 passed correct v
1a7e3 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 74 20 alues the first
1a7e4 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e 63 time.** the func
1a7e5 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 tion is called.
1a7e6 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 On subsequent ca
1a7e7 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74 65 lls, from sqlite
1a7e8 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 72 0a 3_reset(), nVar.
1a7e9 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d 31 20 ** is passed -1
1a7ea 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f and nMem, nCurso
1a7eb 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 r and isExplain
1a7ec 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 20 7a are all passed z
1a7ed 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ero..*/.SQLITE_P
1a7ee 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1a7ef 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 te3VdbeMakeReady
1a7f0 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 (. Vdbe *p,
1a7f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7f2 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a /* The VDBE *
1a7f3 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20 /. int nVar,
1a7f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7f5 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1a7f6 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53 '?' see in the S
1a7f7 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a QL statement */.
1a7f8 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20 int nMem,
1a7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a7fa 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 /* Number of me
1a7fb 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c mory cells to al
1a7fc 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 locate */. int
1a7fd 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20 nCursor,
1a7fe 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1a7ff 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 mber of cursors
1a800 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 to allocate */.
1a801 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 int nArg,
1a802 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a803 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 /* Maximum numbe
1a804 72 20 6f 66 20 61 72 67 73 20 69 6e 20 53 75 62 r of args in Sub
1a805 50 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69 6e Programs */. in
1a806 74 20 69 73 45 78 70 6c 61 69 6e 2c 20 20 20 20 t isExplain,
1a807 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1a808 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50 4c True if the EXPL
1a809 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 73 20 AIN keywords is
1a80a 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 present */. int
1a80b 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c usesStmtJournal
1a80c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1a80d 72 75 65 20 74 6f 20 73 65 74 20 56 64 62 65 2e rue to set Vdbe.
1a80e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 usesStmtJournal
1a80f 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 */.){. int n;.
1a810 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1a811 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 ->db;.. assert(
1a812 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 p!=0 );. asser
1a813 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 t( p->magic==VDB
1a814 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a E_MAGIC_INIT );.
1a815 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 . /* There shou
1a816 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f ld be at least o
1a817 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f 0a ne opcode.. */.
1a818 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 assert( p->nOp
1a819 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 >0 );.. /* Set
1a81a 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44 42 the magic to VDB
1a81b 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f 6e E_MAGIC_RUN soon
1a81c 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c er rather than l
1a81d 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d 61 ater. */. p->ma
1a81e 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 gic = VDBE_MAGIC
1a81f 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 _RUN;.. /* For
1a820 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71 75 each cursor requ
1a821 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f 63 ired, also alloc
1a822 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c ate a memory cel
1a823 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20 63 l. Memory. ** c
1a824 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43 75 ells (nMem+1-nCu
1a825 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 rsor)..nMem, inc
1a826 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65 76 lusive, will nev
1a827 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 er be used by.
1a828 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 ** the vdbe prog
1a829 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68 65 ram. Instead the
1a82a 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 6c y are used to al
1a82b 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
1a82c 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f 72 . ** VdbeCursor
1a82d 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63 74 /BtCursor struct
1a82e 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20 6f ures. The blob o
1a82f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 f memory associa
1a830 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20 63 ted with . ** c
1a831 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72 65 ursor 0 is store
1a832 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c d in memory cell
1a833 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63 65 nMem. Memory ce
1a834 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a 2a ll (nMem-1). **
1a835 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f 62 stores the blob
1a836 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 of memory assoc
1a837 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f iated with curso
1a838 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a 20 r 1, etc.. **.
1a839 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61 6c ** See also: al
1a83a 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e 0a locateCursor()..
1a83b 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20 6e */. nMem += n
1a83c 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41 6c Cursor;.. /* Al
1a83d 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 locate space for
1a83e 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 memory register
1a83f 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 s, SQL variables
1a840 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 61 , VDBE cursors a
1a841 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72 61 nd . ** an arra
1a842 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51 4c y to marshal SQL
1a843 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 function argume
1a844 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73 20 nts in. This is
1a845 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20 20 only done the.
1a846 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 ** first time th
1a847 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 is function is c
1a848 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76 65 alled for a give
1a849 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65 6e n VDBE, not when
1a84a 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69 6e it is. ** bein
1a84b 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73 71 g called from sq
1a84c 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74 6f lite3_reset() to
1a84d 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74 75 reset the virtu
1a84e 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a 2f al machine.. */
1a84f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20 26 . if( nVar>=0 &
1a850 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61 6c & ALWAYS(db->mal
1a851 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 29 20 29 7b locFailed==0) ){
1a852 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20 3d 20 . u8 *zCsr =
1a853 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70 2d (u8 *)&p->aOp[p-
1a854 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a 7a >nOp];. u8 *z
1a855 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e End = (u8 *)&p->
1a856 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d aOp[p->nOpAlloc]
1a857 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b ;. int nByte;
1a858 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56 61 . resolveP2Va
1a859 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b 0a lues(p, &nArg);.
1a85a 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a p->usesStmtJ
1a85b 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 ournal = (u8)use
1a85c 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 sStmtJournal;.
1a85d 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e 20 if( isExplain
1a85e 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20 20 && nMem<10 ){.
1a85f 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a 20 nMem = 10;.
1a860 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 }. memset(
1a861 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a 43 zCsr, 0, zEnd-zC
1a862 73 72 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d sr);. zCsr +=
1a863 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 30 29 (zCsr - (u8*)0)
1a864 26 37 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 &7;. assert(
1a865 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e EIGHT_BYTE_ALIGN
1a866 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0a 0a 20 MENT(zCsr) );..
1a867 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 6e 42 do {. nB
1a868 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 yte = 0;. a
1a869 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a llocSpace((char*
1a86a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d 2a )&p->aMem, nMem*
1a86b 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 sizeof(Mem), &zC
1a86c 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 sr, zEnd, &nByte
1a86d 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 );. allocSp
1a86e 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 ace((char*)&p->a
1a86f 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f 66 Var, nVar*sizeof
1a870 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a 45 (Mem), &zCsr, zE
1a871 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 nd, &nByte);.
1a872 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 allocSpace((c
1a873 68 61 72 2a 29 26 70 2d 3e 61 70 41 72 67 2c 20 har*)&p->apArg,
1a874 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d 2a nArg*sizeof(Mem*
1a875 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 ), &zCsr, zEnd,
1a876 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 &nByte);. a
1a877 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a llocSpace((char*
1a878 29 26 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61 72 )&p->azVar, nVar
1a879 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c 20 *sizeof(char*),
1a87a 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 &zCsr, zEnd, &nB
1a87b 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f yte);. allo
1a87c 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 cSpace((char*)&p
1a87d 2d 3e 61 70 43 73 72 2c 20 0a 20 20 20 20 20 20 ->apCsr, .
1a87e 20 20 20 20 20 20 20 20 20 20 20 6e 43 75 72 73 nCurs
1a87f 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65 43 75 or*sizeof(VdbeCu
1a880 72 73 6f 72 2a 29 2c 20 26 7a 43 73 72 2c 20 7a rsor*), &zCsr, z
1a881 45 6e 64 2c 20 26 6e 42 79 74 65 0a 20 20 20 20 End, &nByte.
1a882 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e );. if( n
1a883 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 Byte ){.
1a884 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 6c 69 74 p->pFree = sqlit
1a885 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 e3DbMallocZero(d
1a886 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 b, nByte);.
1a887 20 7d 0a 20 20 20 20 20 20 7a 43 73 72 20 3d 20 }. zCsr =
1a888 70 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 20 20 p->pFree;.
1a889 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 42 79 zEnd = &zCsr[nBy
1a88a 74 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 te];. }while(
1a88b 20 6e 42 79 74 65 20 26 26 20 21 64 62 2d 3e 6d nByte && !db->m
1a88c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 0a allocFailed );..
1a88d 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d p->nCursor =
1a88e 20 28 75 31 36 29 6e 43 75 72 73 6f 72 3b 0a 20 (u16)nCursor;.
1a88f 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20 29 if( p->aVar )
1a890 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 {. p->nVar
1a891 3d 20 28 79 6e 56 61 72 29 6e 56 61 72 3b 0a 20 = (ynVar)nVar;.
1a892 20 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c for(n=0; n<
1a893 6e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 nVar; n++){.
1a894 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 p->aVar[n].f
1a895 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1a896 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72 . p->aVar
1a897 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 [n].db = db;.
1a898 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1a899 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 f( p->aMem ){.
1a89a 20 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 p->aMem--;
1a89b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a89c 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f /* aMem[] go
1a89d 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 es from 1..nMem
1a89e 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d */. p->nMem
1a89f 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 = nMem;
1a8a0 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20 /*
1a8a1 20 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d not from 0..nM
1a8a2 65 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f em-1 */. fo
1a8a3 72 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 r(n=1; n<=nMem;
1a8a4 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d n++){. p-
1a8a5 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d >aMem[n].flags =
1a8a6 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 MEM_Null;.
1a8a7 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 p->aMem[n].db
1a8a8 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 = db;. }.
1a8a9 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 }. }.#ifdef
1a8aa 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 SQLITE_DEBUG. f
1a8ab 6f 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 or(n=1; n<p->nMe
1a8ac 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 m; n++){. ass
1a8ad 65 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e ert( p->aMem[n].
1a8ae 64 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 db==db );. }.#e
1a8af 6e 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 ndif.. p->pc =
1a8b0 2d 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 -1;. p->rc = SQ
1a8b1 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 LITE_OK;. p->er
1a8b2 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 rorAction = OE_A
1a8b3 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 bort;. p->expla
1a8b4 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b in |= isExplain;
1a8b5 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 . p->magic = VD
1a8b6 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 BE_MAGIC_RUN;.
1a8b7 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a p->nChange = 0;.
1a8b8 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 p->cacheCtr =
1a8b9 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 1;. p->minWrite
1a8ba 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 FileFormat = 255
1a8bb 3b 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e ;. p->iStatemen
1a8bc 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 t = 0;.#ifdef VD
1a8bd 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 BE_PROFILE. {.
1a8be 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
1a8bf 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
1a8c0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e i++){. p->
1a8c1 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a aOp[i].cnt = 0;.
1a8c2 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e p->aOp[i].
1a8c3 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 cycles = 0;.
1a8c4 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a }. }.#endif.}..
1a8c5 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 /*.** Close a VD
1a8c6 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 BE cursor and re
1a8c7 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 lease all the re
1a8c8 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 sources that cur
1a8c9 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 sor .** happens
1a8ca 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 to hold..*/.SQLI
1a8cb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a8cc 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 sqlite3VdbeFreeC
1a8cd 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 ursor(Vdbe *p, V
1a8ce 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b dbeCursor *pCx){
1a8cf 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b . if( pCx==0 ){
1a8d0 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d . return;. }
1a8d1 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 . if( pCx->pBt
1a8d2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 ){. sqlite3Bt
1a8d3 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 reeClose(pCx->pB
1a8d4 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 t);. /* The p
1a8d5 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c Cx->pCursor will
1a8d6 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 be close automa
1a8d7 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 tically, if it e
1a8d8 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a xists, by. **
1a8d9 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e the call above.
1a8da 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 */. }else if(
1a8db 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a pCx->pCursor ){.
1a8dc 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1a8dd 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d CloseCursor(pCx-
1a8de 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 >pCursor);. }.#
1a8df 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1a8e0 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1a8e1 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 if( pCx->pVtab
1a8e2 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 Cursor ){. sq
1a8e3 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1a8e4 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d r *pVtabCursor =
1a8e5 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f pCx->pVtabCurso
1a8e6 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c r;. const sql
1a8e7 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f ite3_module *pMo
1a8e8 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 dule = pCx->pMod
1a8e9 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 ule;. p->inVt
1a8ea 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 abMethod = 1;.
1a8eb 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 (void)sqlite3S
1a8ec 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b afetyOff(p->db);
1a8ed 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 . pModule->xC
1a8ee 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 lose(pVtabCursor
1a8ef 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c );. (void)sql
1a8f0 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e ite3SafetyOn(p->
1a8f1 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 db);. p->inVt
1a8f2 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 abMethod = 0;.
1a8f3 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a }.#endif.}../*.*
1a8f4 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 * Copy the value
1a8f5 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 s stored in the
1a8f6 56 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 VdbeFrame struct
1a8f7 75 72 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e ure to its Vdbe.
1a8f8 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 This.** is used
1a8f9 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 , for example, w
1a8fa 68 65 6e 20 61 20 74 72 69 67 67 65 72 20 73 75 hen a trigger su
1a8fb 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c b-program is hal
1a8fc 74 65 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a ted to restore.*
1a8fd 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 * control to the
1a8fe 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a main program..*
1a8ff 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1a900 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1a901 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 FrameRestore(Vdb
1a902 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b eFrame *pFrame){
1a903 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 . Vdbe *v = pFr
1a904 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 ame->v;. v->aOp
1a905 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a = pFrame->aOp;.
1a906 20 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d v->nOp = pFram
1a907 65 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 e->nOp;. v->aMe
1a908 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d m = pFrame->aMem
1a909 3b 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 ;. v->nMem = pF
1a90a 72 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d rame->nMem;. v-
1a90b 3e 61 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d >apCsr = pFrame-
1a90c 3e 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 >apCsr;. v->nCu
1a90d 72 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e rsor = pFrame->n
1a90e 43 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d Cursor;. v->db-
1a90f 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72 >lastRowid = pFr
1a910 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a ame->lastRowid;.
1a911 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 v->nChange = p
1a912 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a Frame->nChange;.
1a913 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d return pFrame-
1a914 3e 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c >pc;.}../*.** Cl
1a915 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e ose all cursors.
1a916 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 .**.** Also rele
1a917 61 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 ase any dynamic
1a918 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 memory held by t
1a919 68 65 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62 he VM in the Vdb
1a91a 65 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a e.aMem memory .*
1a91b 2a 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 * cell array. Th
1a91c 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 is is necessary
1a91d 61 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 as the memory ce
1a91e 6c 6c 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e ll array may con
1a91f 74 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 tain.** pointers
1a920 20 74 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62 to VdbeFrame ob
1a921 6a 65 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79 jects, which may
1a922 20 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e in turn contain
1a923 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 pointers to.**
1a924 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f open cursors..*/
1a925 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f .static void clo
1a926 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 seAllCursors(Vdb
1a927 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e e *p){. if( p->
1a928 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 pFrame ){. Vd
1a929 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 beFrame *pFrame
1a92a 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 = p->pFrame;.
1a92b 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 for(pFrame=p->p
1a92c 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 Frame; pFrame->p
1a92d 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 Parent; pFrame=p
1a92e 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b Frame->pParent);
1a92f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1a930 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 FrameRestore(pFr
1a931 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 ame);. }. p->p
1a932 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e Frame = 0;. p->
1a933 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 nFrame = 0;.. i
1a934 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 f( p->apCsr ){.
1a935 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f int i;. fo
1a936 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 r(i=0; i<p->nCur
1a937 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 sor; i++){.
1a938 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 VdbeCursor *pC
1a939 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 = p->apCsr[i];.
1a93a 20 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 if( pC ){.
1a93b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
1a93c 62 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 beFreeCursor(p,
1a93d 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e pC);. p->
1a93e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 apCsr[i] = 0;.
1a93f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1a940 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b if( p->aMem ){
1a941 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 . releaseMemA
1a942 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d rray(&p->aMem[1]
1a943 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a , p->nMem);. }.
1a944 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 }../*.** Clean u
1a945 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 p the VM after e
1a946 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 xecution..**.**
1a947 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c This routine wil
1a948 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 l automatically
1a949 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 close any cursor
1a94a 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 s, lists, and/or
1a94b 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 .** sorters that
1a94c 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e were left open.
1a94d 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 It also delete
1a94e 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a s the values of.
1a94f 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 ** variables in
1a950 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 the aVar[] array
1a951 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1a952 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 Cleanup(Vdbe *p
1a953 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 ){. sqlite3 *db
1a954 20 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 = p->db;..#ifde
1a955 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 f SQLITE_DEBUG.
1a956 20 2f 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 /* Execute asse
1a957 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 rt() statements
1a958 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 to ensure that t
1a959 68 65 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 he Vdbe.apCsr[]
1a95a 61 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 and . ** Vdbe.a
1a95b 4d 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 Mem[] arrays hav
1a95c 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 e already been c
1a95d 6c 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 leaned up. */.
1a95e 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
1a95f 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 0; i<p->nCursor;
1a960 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d i++) assert( p-
1a961 3e 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e >apCsr==0 || p->
1a962 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 apCsr[i]==0 );.
1a963 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e for(i=1; i<=p->
1a964 6e 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 nMem; i++) asser
1a965 74 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c t( p->aMem==0 ||
1a966 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 p->aMem[i].flag
1a967 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 s==MEM_Null );.#
1a968 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 endif.. sqlite3
1a969 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1a96a 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 rrMsg);. p->zEr
1a96b 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 rMsg = 0;. p->p
1a96c 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d ResultSet = 0;.}
1a96d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 ../*.** Set the
1a96e 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 number of result
1a96f 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 columns that wi
1a970 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 ll be returned b
1a971 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 y this SQL.** st
1a972 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 atement. This is
1a973 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 now set at comp
1a974 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 ile time, rather
1a975 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 than during.**
1a976 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 execution of the
1a977 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f vdbe program so
1a978 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f that sqlite3_co
1a979 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e lumn_count() can
1a97a 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e .** be called on
1a97b 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e an SQL statemen
1a97c 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 t before sqlite3
1a97d 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 _step()..*/.SQLI
1a97e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1a97f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 sqlite3VdbeSetNu
1a980 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 mCols(Vdbe *p, i
1a981 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a nt nResColumn){.
1a982 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b Mem *pColName;
1a983 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 . int n;. sqli
1a984 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b te3 *db = p->db;
1a985 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 .. releaseMemAr
1a986 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c ray(p->aColName,
1a987 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 p->nResColumn*C
1a988 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c OLNAME_N);. sql
1a989 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1a98a 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e ->aColName);. n
1a98b 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f = nResColumn*CO
1a98c 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 LNAME_N;. p->nR
1a98d 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 esColumn = (u16)
1a98e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d nResColumn;. p-
1a98f 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c >aColName = pCol
1a990 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c Name = (Mem*)sql
1a991 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
1a992 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 (db, sizeof(Mem)
1a993 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 *n );. if( p->a
1a994 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 ColName==0 ) ret
1a995 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d urn;. while( n-
1a996 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f - > 0 ){. pCo
1a997 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d lName->flags = M
1a998 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f EM_Null;. pCo
1a999 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 lName->db = p->d
1a99a 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b b;. pColName+
1a99b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 +;. }.}../*.**
1a99c 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 Set the name of
1a99d 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d the idx'th colum
1a99e 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 n to be returned
1a99f 20 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 by the SQL stat
1a9a0 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 ement..** zName
1a9a1 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 must be a pointe
1a9a2 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 r to a nul termi
1a9a3 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a nated string..**
1a9a4 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 .** This call mu
1a9a5 73 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 st be made after
1a9a6 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 a call to sqlit
1a9a7 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 e3VdbeSetNumCols
1a9a8 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 ()..**.** The fi
1a9a9 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 nal parameter, x
1a9aa 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 Del, must be one
1a9ab 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d of SQLITE_DYNAM
1a9ac 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 IC, SQLITE_STATI
1a9ad 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 C.** or SQLITE_T
1a9ae 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 RANSIENT. If it
1a9af 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 is SQLITE_DYNAMI
1a9b0 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 C, then the buff
1a9b1 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f er pointed.** to
1a9b2 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 by zName will b
1a9b3 65 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 e freed by sqlit
1a9b4 65 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 e3DbFree() when
1a9b5 74 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 the vdbe is dest
1a9b6 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 royed..*/.SQLITE
1a9b7 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1a9b8 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 ite3VdbeSetColNa
1a9b9 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 me(. Vdbe *p,
1a9ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9bb 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 /* Vdbe b
1a9bc 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 eing configured
1a9bd 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 */. int idx,
1a9be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9bf 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1a9c0 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 f column zName a
1a9c1 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 pplies to */. i
1a9c2 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 nt var,
1a9c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a9c4 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f /* One of the CO
1a9c5 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 LNAME_* constant
1a9c6 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 s */. const cha
1a9c7 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 r *zName,
1a9c8 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 /* Point
1a9c9 65 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e er to buffer con
1a9ca 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a taining name */.
1a9cb 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
1a9cc 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 oid*)
1a9cd 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e /* Memory man
1a9ce 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 agement strategy
1a9cf 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b for zName */.){
1a9d0 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d . int rc;. Mem
1a9d1 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 *pColName;. as
1a9d2 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 sert( idx<p->nRe
1a9d3 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 sColumn );. ass
1a9d4 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 ert( var<COLNAME
1a9d5 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 _N );. if( p->d
1a9d6 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1a9d7 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
1a9d8 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 zName || xDel!=S
1a9d9 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b QLITE_DYNAMIC );
1a9da 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1a9db 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 TE_NOMEM;. }.
1a9dc 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e assert( p->aColN
1a9dd 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c ame!=0 );. pCol
1a9de 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c Name = &(p->aCol
1a9df 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e Name[idx+var*p->
1a9e0 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 nResColumn]);.
1a9e1 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 rc = sqlite3Vdbe
1a9e2 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 MemSetStr(pColNa
1a9e3 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 me, zName, -1, S
1a9e4 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c QLITE_UTF8, xDel
1a9e5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 );. assert( rc!
1a9e6 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 =0 || !zName ||
1a9e7 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 (pColName->flags
1a9e8 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b &MEM_Term)!=0 );
1a9e9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
1a9ea 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 ./*.** A read or
1a9eb 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1a9ec 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f on may or may no
1a9ed 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 t be active on d
1a9ee 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a atabase handle.*
1a9ef 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 * db. If a trans
1a9f0 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 action is active
1a9f1 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 , commit it. If
1a9f2 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 there is a.** wr
1a9f3 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 ite-transaction
1a9f4 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 spanning more th
1a9f5 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 an one database
1a9f6 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 file, this routi
1a9f7 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 ne.** takes care
1a9f8 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a of the master j
1a9f9 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e ournal trickery.
1a9fa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 .*/.static int v
1a9fb 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 dbeCommit(sqlite
1a9fc 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 3 *db, Vdbe *p){
1a9fd 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1a9fe 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 nTrans = 0; /*
1a9ff 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 Number of databa
1aa00 73 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 ses with an acti
1aa01 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 ve write-transac
1aa02 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 tion */. int rc
1aa03 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1aa04 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 int needXcommit
1aa05 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c = 0;..#ifdef SQL
1aa06 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1aa07 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 TABLE. /* With
1aa08 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c this option, sql
1aa09 69 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 69 ite3VtabSync() i
1aa0a 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 s defined to be
1aa0b 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c simply . ** SQL
1aa0c 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e ITE_OK so p is n
1aa0d 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 ot used. . */.
1aa0e 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 UNUSED_PARAMETE
1aa0f 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 R(p);.#endif..
1aa10 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 /* Before doing
1aa11 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 anything else, c
1aa12 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 all the xSync()
1aa13 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 callback for any
1aa14 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f . ** virtual mo
1aa15 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 dule tables writ
1aa16 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e ten in this tran
1aa17 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 saction. This ha
1aa18 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e s to. ** be don
1aa19 65 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 e before determi
1aa1a 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d ning whether a m
1aa1b 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1aa1c 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 le is . ** requ
1aa1d 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e ired, as an xSyn
1aa1e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 c() callback may
1aa1f 20 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 add an attached
1aa20 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 database. ** t
1aa21 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f o the transactio
1aa22 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 n.. */. rc = s
1aa23 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 qlite3VtabSync(d
1aa24 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b b, &p->zErrMsg);
1aa25 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 . if( rc!=SQLIT
1aa26 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 E_OK ){. retu
1aa27 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a rn rc;. }.. /*
1aa28 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 This loop deter
1aa29 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 mines (a) if the
1aa2a 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f commit hook sho
1aa2b 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 uld be invoked a
1aa2c 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 nd. ** (b) how
1aa2d 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 many database fi
1aa2e 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 les have open wr
1aa2f 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 ite transactions
1aa30 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c , not . ** incl
1aa31 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 uding the temp d
1aa32 61 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 atabase. (b) is
1aa33 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 important becaus
1aa34 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a e if more than .
1aa35 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 ** one databas
1aa36 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 e file has an op
1aa37 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 en write transac
1aa38 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a tion, a master j
1aa39 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 ournal. ** file
1aa3a 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 is required for
1aa3b 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 an atomic commi
1aa3c 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 t.. */ . for(i
1aa3d 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1aa3e 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65 65 20 ++){ . Btree
1aa3f 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 *pBt = db->aDb[i
1aa40 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 73 ].pBt;. if( s
1aa41 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 qlite3BtreeIsInT
1aa42 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 rans(pBt) ){.
1aa43 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d needXcommit =
1aa44 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 1;. if( i!
1aa45 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 =1 ) nTrans++;.
1aa46 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 }. }.. /* I
1aa47 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 f there are any
1aa48 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f write-transactio
1aa49 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b ns at all, invok
1aa4a 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f e the commit hoo
1aa4b 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 k */. if( needX
1aa4c 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 commit && db->xC
1aa4d 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b ommitCallback ){
1aa4e 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 . (void)sqlit
1aa4f 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b e3SafetyOff(db);
1aa50 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 . rc = db->xC
1aa51 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 ommitCallback(db
1aa52 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 ->pCommitArg);.
1aa53 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
1aa54 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 SafetyOn(db);.
1aa55 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
1aa56 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1aa57 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 CONSTRAINT;.
1aa58 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 }. }.. /* The
1aa59 73 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f simple case - no
1aa5a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 more than one d
1aa5b 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f atabase file (no
1aa5c 74 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 t counting the.
1aa5d 20 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73 ** TEMP databas
1aa5e 65 29 20 68 61 73 20 61 20 74 72 61 6e 73 61 63 e) has a transac
1aa5f 74 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54 tion active. T
1aa60 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
1aa61 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 for the. ** mas
1aa62 74 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a ter-journal.. *
1aa63 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 *. ** If the re
1aa64 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 turn value of sq
1aa65 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c lite3BtreeGetFil
1aa66 65 6e 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72 ename() is a zer
1aa67 6f 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 o length. ** st
1aa68 72 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 ring, it means t
1aa69 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 he main database
1aa6a 20 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 is :memory: or
1aa6b 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e a temp file. In
1aa6c 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 . ** that case
1aa6d 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f we do not suppo
1aa6e 72 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d rt atomic multi-
1aa6f 66 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f file commits, so
1aa70 20 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 use the . ** s
1aa71 69 6d 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20 imple case then
1aa72 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 too.. */. if(
1aa73 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 0==sqlite3Strlen
1aa74 33 30 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 30(sqlite3BtreeG
1aa75 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 etFilename(db->a
1aa76 44 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c Db[0].pBt)). |
1aa77 7c 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b | nTrans<=1. ){
1aa78 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 . for(i=0; rc
1aa79 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
1aa7a 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a <db->nDb; i++){.
1aa7b 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 Btree *pBt
1aa7c 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 = db->aDb[i].pB
1aa7d 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 t;. if( pBt
1aa7e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1aa7f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1aa80 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c mitPhaseOne(pBt,
1aa81 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 0);. }.
1aa82 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 }.. /* Do th
1aa83 65 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 e commit only if
1aa84 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 all databases s
1aa85 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 uccessfully comp
1aa86 6c 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 lete phase 1. .
1aa87 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 ** If one of
1aa88 74 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 the BtreeCommitP
1aa89 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 haseOne() calls
1aa8a 66 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 fails, this indi
1aa8b 63 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 cates an. **
1aa8c 49 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 IO error while d
1aa8d 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 eleting or trunc
1aa8e 61 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 ating a journal
1aa8f 66 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 file. It is unli
1aa90 6b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 kely,. ** but
1aa91 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 could happen. I
1aa92 6e 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e n this case aban
1aa93 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 don processing a
1aa94 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 nd return the er
1aa95 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ror.. */.
1aa96 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c for(i=0; rc==SQL
1aa97 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e ITE_OK && i<db->
1aa98 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nDb; i++){.
1aa99 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 Btree *pBt = db
1aa9a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 ->aDb[i].pBt;.
1aa9b 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 if( pBt ){.
1aa9c 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1aa9d 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 te3BtreeCommitPh
1aa9e 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 aseTwo(pBt);.
1aa9f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
1aaa0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1aaa1 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1aaa2 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 3VtabCommit(db);
1aaa3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
1aaa4 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 The complex cas
1aaa5 65 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d e - There is a m
1aaa6 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d ulti-file write-
1aaa7 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 transaction acti
1aaa8 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 ve.. ** This re
1aaa9 71 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 quires a master
1aaaa 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 journal file to
1aaab 65 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 ensure the trans
1aaac 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 action is. ** c
1aaad 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c ommitted atomicl
1aaae 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 y.. */.#ifndef
1aaaf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b SQLITE_OMIT_DISK
1aab0 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 IO. else{. s
1aab1 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 qlite3_vfs *pVfs
1aab2 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 = db->pVfs;.
1aab3 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 int needSync =
1aab4 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 0;. char *zMa
1aab5 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 ster = 0; /* F
1aab6 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 ile-name for the
1aab7 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 master journal
1aab8 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 */. char cons
1aab9 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 t *zMainFile = s
1aaba 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 qlite3BtreeGetFi
1aabb 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 lename(db->aDb[0
1aabc 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 ].pBt);. sqli
1aabd 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 te3_file *pMaste
1aabe 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f r = 0;. i64 o
1aabf 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 ffset = 0;. i
1aac0 6e 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 nt res;.. /*
1aac1 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 Select a master
1aac2 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
1aac3 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 e */. do {.
1aac4 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b u32 iRandom;
1aac5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 . sqlite3Db
1aac6 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 Free(db, zMaster
1aac7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1aac8 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 _randomness(size
1aac9 6f 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 of(iRandom), &iR
1aaca 61 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d andom);. zM
1aacb 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d aster = sqlite3M
1aacc 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d Printf(db, "%s-m
1aacd 6a 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c j%08X", zMainFil
1aace 65 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 e, iRandom&0x7ff
1aacf 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 fffff);. if
1aad0 28 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 ( !zMaster ){.
1aad1 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c return SQL
1aad2 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ITE_NOMEM;.
1aad3 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 }. rc = sq
1aad4 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 lite3OsAccess(pV
1aad5 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c fs, zMaster, SQL
1aad6 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 ITE_ACCESS_EXIST
1aad7 53 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 S, &res);. }w
1aad8 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 hile( rc==SQLITE
1aad9 5f 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 _OK && res );.
1aada 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1aadb 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 _OK ){. /*
1aadc 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 Open the master
1aadd 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 journal. */.
1aade 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 rc = sqlite3Os
1aadf 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c OpenMalloc(pVfs,
1aae0 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 zMaster, &pMast
1aae1 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 er, . S
1aae2 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 QLITE_OPEN_READW
1aae3 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e RITE|SQLITE_OPEN
1aae4 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 _CREATE|.
1aae5 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 SQLITE_OPEN_E
1aae6 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f XCLUSIVE|SQLITE_
1aae7 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 OPEN_MASTER_JOUR
1aae8 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a NAL, 0. );.
1aae9 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 }. if( rc
1aaea 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
1aaeb 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
1aaec 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b ee(db, zMaster);
1aaed 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 . return rc
1aaee 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a ;. }. . /*
1aaef 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 Write the name
1aaf0 6f 66 20 65 61 63 68 20 64 61 74 61 62 61 73 65 of each database
1aaf1 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 file in the tra
1aaf2 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 nsaction into th
1aaf3 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 e new. ** mas
1aaf4 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 ter journal file
1aaf5 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 . If an error oc
1aaf6 63 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 curs at this poi
1aaf7 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 nt close. **
1aaf8 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d and delete the m
1aaf9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 aster journal fi
1aafa 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 le. All the indi
1aafb 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 vidual journal f
1aafc 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c iles. ** stil
1aafd 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 l have 'null' as
1aafe 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 the master jour
1aaff 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 nal pointer, so
1ab00 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 they will roll.
1ab01 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 ** back indep
1ab02 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 endently if a fa
1ab03 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 ilure occurs..
1ab04 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 */. for(i=0
1ab05 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b ; i<db->nDb; i++
1ab06 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a ){. Btree *
1ab07 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d pBt = db->aDb[i]
1ab08 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 .pBt;. if(
1ab09 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b i==1 ) continue;
1ab0a 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 /* Ignore the
1ab0b 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a TEMP database *
1ab0c 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 /. if( sqli
1ab0d 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e te3BtreeIsInTran
1ab0e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 s(pBt) ){.
1ab0f 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 char const *zF
1ab10 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 ile = sqlite3Btr
1ab11 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 eeGetJournalname
1ab12 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 (pBt);. i
1ab13 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 f( zFile[0]==0 )
1ab14 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 continue; /* I
1ab15 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 gnore :memory: d
1ab16 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 atabases */.
1ab17 20 20 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e if( !needSyn
1ab18 63 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 c && !sqlite3Btr
1ab19 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 eeSyncDisabled(p
1ab1a 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Bt) ){.
1ab1b 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 needSync = 1;.
1ab1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1ab1d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 rc = sqlite3OsW
1ab1e 72 69 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 rite(pMaster, zF
1ab1f 69 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c ile, sqlite3Strl
1ab20 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f en30(zFile)+1, o
1ab21 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 ffset);.
1ab22 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 offset += sqlite
1ab23 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 3Strlen30(zFile)
1ab24 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +1;. if(
1ab25 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc!=SQLITE_OK ){
1ab26 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1ab27 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d e3OsCloseFree(pM
1ab28 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 aster);.
1ab29 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 sqlite3OsDelet
1ab2a 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c e(pVfs, zMaster,
1ab2b 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 0);. s
1ab2c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ab2d 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 zMaster);.
1ab2e 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a return rc;.
1ab2f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1ab30 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 }. }.. /*
1ab31 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 Sync the master
1ab32 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 journal file. If
1ab33 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 the IOCAP_SEQUE
1ab34 4e 54 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 NTIAL device.
1ab35 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 ** flag is set
1ab36 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 this is not requ
1ab37 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 ired.. */.
1ab38 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 if( needSync .
1ab39 20 20 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 && 0==(sqlit
1ab3a 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 e3OsDeviceCharac
1ab3b 74 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 teristics(pMaste
1ab3c 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f r)&SQLITE_IOCAP_
1ab3d 53 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 SEQUENTIAL).
1ab3e 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 && SQLITE_OK!=(
1ab3f 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 rc = sqlite3OsSy
1ab40 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 nc(pMaster, SQLI
1ab41 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 TE_SYNC_NORMAL))
1ab42 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 . ){. sq
1ab43 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 lite3OsCloseFree
1ab44 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 (pMaster);.
1ab45 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 sqlite3OsDelete
1ab46 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 (pVfs, zMaster,
1ab47 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0);. sqlite
1ab48 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 3DbFree(db, zMas
1ab49 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 ter);. retu
1ab4a 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 rn rc;. }..
1ab4b 20 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 /* Sync all th
1ab4c 65 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c e db files invol
1ab4d 76 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 ved in the trans
1ab4e 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 action. The same
1ab4f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 call. ** set
1ab50 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 s the master jou
1ab51 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 rnal pointer in
1ab52 65 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 each individual
1ab53 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 journal. If.
1ab54 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 ** an error occu
1ab55 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 rs here, do not
1ab56 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 delete the maste
1ab57 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a r journal file..
1ab58 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 **. ** If
1ab59 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 the error occur
1ab5a 73 20 64 75 72 69 6e 67 20 74 68 65 20 66 69 72 s during the fir
1ab5b 73 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a st call to. *
1ab5c 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f * sqlite3BtreeCo
1ab5d 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 mmitPhaseOne(),
1ab5e 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 then there is a
1ab5f 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a chance that the.
1ab60 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f ** master jo
1ab61 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 urnal file will
1ab62 62 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 be orphaned. But
1ab63 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 we cannot delet
1ab64 65 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 e it,. ** in
1ab65 63 61 73 65 20 74 68 65 20 6d 61 73 74 65 72 20 case the master
1ab66 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d journal file nam
1ab67 65 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e e was written in
1ab68 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 to the journal.
1ab69 20 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 ** file befor
1ab6a 65 20 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63 e the failure oc
1ab6b 63 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 curred.. */.
1ab6c 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d for(i=0; rc==
1ab6d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 SQLITE_OK && i<d
1ab6e 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 b->nDb; i++){ .
1ab6f 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 Btree *pBt
1ab70 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 = db->aDb[i].pBt
1ab71 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 ;. if( pBt
1ab72 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 ){. rc =
1ab73 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d sqlite3BtreeComm
1ab74 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 itPhaseOne(pBt,
1ab75 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 zMaster);.
1ab76 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 }. }. sqli
1ab77 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 te3OsCloseFree(p
1ab78 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 Master);. if(
1ab79 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc!=SQLITE_OK )
1ab7a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 {. sqlite3D
1ab7b 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 bFree(db, zMaste
1ab7c 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e r);. return
1ab7d 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 rc;. }..
1ab7e 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61 /* Delete the ma
1ab7f 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c ster journal fil
1ab80 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 e. This commits
1ab81 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e the transaction.
1ab82 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f After. ** do
1ab83 69 6e 67 20 74 68 69 73 20 74 68 65 20 64 69 72 ing this the dir
1ab84 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64 ectory is synced
1ab85 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e again before an
1ab86 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 y individual.
1ab87 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ** transaction
1ab88 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65 files are delete
1ab89 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 d.. */. rc
1ab8a 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 = sqlite3OsDele
1ab8b 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 te(pVfs, zMaster
1ab8c 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 1);. sqlite
1ab8d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 3DbFree(db, zMas
1ab8e 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 ter);. zMaste
1ab8f 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 r = 0;. if( r
1ab90 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 c ){. retur
1ab91 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n rc;. }..
1ab92 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e /* All files an
1ab93 64 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61 d directories ha
1ab94 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ve already been
1ab95 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66 synced, so the f
1ab96 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 ollowing. **
1ab97 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 calls to sqlite3
1ab98 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 BtreeCommitPhase
1ab99 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 Two() are only c
1ab9a 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 losing files and
1ab9b 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 . ** deleting
1ab9c 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a or truncating j
1ab9d 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 ournals. If some
1ab9e 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 thing goes wrong
1ab9f 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 while. ** th
1aba0 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 is is happening
1aba1 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 we don't really
1aba2 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72 care. The integr
1aba3 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a ity of the. *
1aba4 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 * transaction is
1aba5 20 61 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74 already guarant
1aba6 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 eed, but some st
1aba7 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e ray 'cold' journ
1aba8 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 als. ** may b
1aba9 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 e lying around.
1abaa 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 Returning an err
1abab 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 or code won't he
1abac 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 lp matters..
1abad 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73 */. disable_s
1abae 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f imulated_io_erro
1abaf 72 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 rs();. sqlite
1abb0 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 3BeginBenignMall
1abb1 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d oc();. for(i=
1abb2 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 0; i<db->nDb; i+
1abb3 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 +){ . Btree
1abb4 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b *pBt = db->aDb[
1abb5 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 i].pBt;. if
1abb6 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 ( pBt ){.
1abb7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d sqlite3BtreeCom
1abb8 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 29 mitPhaseTwo(pBt)
1abb9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1abba 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 sqlite3EndBe
1abbb 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 nignMalloc();.
1abbc 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 enable_simulat
1abbd 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a ed_io_errors();.
1abbe 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 . sqlite3Vtab
1abbf 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a Commit(db);. }.
1abc0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e #endif.. return
1abc1 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 rc;.}../* .** T
1abc2 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 his routine chec
1abc3 6b 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 ks that the sqli
1abc4 74 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e te3.activeVdbeCn
1abc5 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 t count variable
1abc6 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 .** matches the
1abc7 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 number of vdbe's
1abc8 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c in the list sql
1abc9 69 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 ite3.pVdbe that
1abca 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 are.** currently
1abcb 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 active. An asse
1abcc 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 rtion fails if t
1abcd 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f he two counts do
1abce 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 not match..** T
1abcf 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e his is an intern
1abd0 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e al self-check on
1abd1 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 ly - it is not a
1abd2 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 n essential proc
1abd3 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a essing.** step..
1abd4 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 **.** This is a
1abd5 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20 no-op if NDEBUG
1abd6 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 is defined..*/.#
1abd7 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 ifndef NDEBUG.st
1abd8 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 atic void checkA
1abd9 63 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c ctiveVdbeCnt(sql
1abda 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 ite3 *db){. Vdb
1abdb 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 e *p;. int cnt
1abdc 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 = 0;. int nWrit
1abdd 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d e = 0;. p = db-
1abde 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 >pVdbe;. while(
1abdf 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d p ){. if( p-
1abe0 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 >magic==VDBE_MAG
1abe1 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e IC_RUN && p->pc>
1abe2 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b =0 ){. cnt+
1abe3 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e +;. if( p->
1abe4 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 readOnly==0 ) nW
1abe5 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 rite++;. }.
1abe6 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a p = p->pNext;.
1abe7 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e }. assert( cn
1abe8 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62 t==db->activeVdb
1abe9 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 eCnt );. assert
1abea 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 ( nWrite==db->wr
1abeb 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a iteVdbeCnt );.}.
1abec 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68 #else.#define ch
1abed 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 eckActiveVdbeCnt
1abee 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a (x).#endif../*.*
1abef 2a 20 46 6f 72 20 65 76 65 72 79 20 42 74 72 65 * For every Btre
1abf0 65 20 74 68 61 74 20 69 6e 20 64 61 74 61 62 61 e that in databa
1abf1 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 se connection db
1abf2 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 which .** has b
1abf3 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 een modified, "t
1abf4 72 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 rip" or invalida
1abf5 74 65 20 65 61 63 68 20 63 75 72 73 6f 72 20 69 te each cursor i
1abf6 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20 n.** that Btree
1abf7 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 might have been
1abf8 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74 modified so that
1abf9 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 the cursor.** c
1abfa 61 6e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 an never be used
1abfb 20 61 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61 again. This ha
1abfc 70 70 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c ppens when a rol
1abfd 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 lback.*** occurs
1abfe 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 . We have to tr
1abff 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 ip all the other
1ac00 20 63 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a cursors, even.*
1ac01 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 * cursor from ot
1ac02 68 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 her VMs in diffe
1ac03 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f rent database co
1ac04 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f nnections,.** so
1ac05 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 that none of th
1ac06 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 em try to use th
1ac07 65 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20 e data at which
1ac08 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 they.** were poi
1ac09 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20 nting and which
1ac0a 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65 65 now may have bee
1ac0b 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a n changed due.**
1ac0c 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b to the rollback
1ac0d 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 ..**.** Remember
1ac0e 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b that a rollback
1ac0f 20 63 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c can delete tabl
1ac10 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a es complete and.
1ac11 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 ** reorder rootp
1ac12 61 67 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20 ages. So it is
1ac13 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a not sufficient j
1ac14 75 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 ust to save.** t
1ac15 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 he state of the
1ac16 63 75 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65 cursor. We have
1ac17 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 to invalidate t
1ac18 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 he cursor.** so
1ac19 74 68 61 74 20 69 74 20 69 73 20 6e 65 76 65 72 that it is never
1ac1a 20 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a used again..*/.
1ac1b 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 static void inva
1ac1c 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d lidateCursorsOnM
1ac1d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71 odifiedBtrees(sq
1ac1e 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e lite3 *db){. in
1ac1f 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
1ac20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b i<db->nDb; i++){
1ac21 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 . Btree *p =
1ac22 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a db->aDb[i].pBt;.
1ac23 20 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c if( p && sql
1ac24 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 ite3BtreeIsInTra
1ac25 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73 ns(p) ){. s
1ac26 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 qlite3BtreeTripA
1ac27 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c llCursors(p, SQL
1ac28 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 ITE_ABORT);.
1ac29 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 }. }.}../*.** I
1ac2a 66 20 74 68 65 20 56 64 62 65 20 70 61 73 73 65 f the Vdbe passe
1ac2b 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 d as the first a
1ac2c 72 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 rgument opened a
1ac2d 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 statement-trans
1ac2e 61 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 action,.** close
1ac2f 20 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e it now. Argumen
1ac30 74 20 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69 t eOp must be ei
1ac31 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 ther SAVEPOINT_R
1ac32 4f 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 OLLBACK or.** SA
1ac33 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e VEPOINT_RELEASE.
1ac34 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f If it is SAVEPO
1ac35 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 INT_ROLLBACK, th
1ac36 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 en the statement
1ac37 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 .** transaction
1ac38 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 is rolled back.
1ac39 49 66 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f If eOp is SAVEPO
1ac3a 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 INT_RELEASE, the
1ac3b 6e 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d n the .** statem
1ac3c 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1ac3d 69 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a is commtted..**.
1ac3e 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f ** If an IO erro
1ac3f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c r occurs, an SQL
1ac40 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 ITE_IOERR_XXX er
1ac41 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 ror code is retu
1ac42 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 rned. .** Otherw
1ac43 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a ise SQLITE_OK..*
1ac44 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1ac45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 int sqlite3Vdbe
1ac46 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 CloseStatement(V
1ac47 64 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 dbe *p, int eOp)
1ac48 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e {. sqlite3 *con
1ac49 73 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 st db = p->db;.
1ac4a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 int rc = SQLITE
1ac4b 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d _OK;.. /* If p-
1ac4c 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67 >iStatement is g
1ac4d 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f reater than zero
1ac4e 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64 62 65 , then this Vdbe
1ac4f 20 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 opened a . **
1ac50 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1ac51 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c ction that shoul
1ac52 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65 d be closed here
1ac53 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 . The only excep
1ac54 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 tion. ** is tha
1ac55 74 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 t an IO error ma
1ac56 79 20 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20 y have occured,
1ac57 63 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 causing an emerg
1ac58 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 ency rollback..
1ac59 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 ** In this case
1ac5a 20 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 (db->nStatement
1ac5b 3d 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20 ==0), and there
1ac5c 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f is nothing to do
1ac5d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d .. */. if( db-
1ac5e 3e 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 >nStatement && p
1ac5f 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a ->iStatement ){.
1ac60 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 int i;. c
1ac61 6f 6e 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f onst int iSavepo
1ac62 69 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d int = p->iStatem
1ac63 65 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 ent-1;.. asse
1ac64 72 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 rt( eOp==SAVEPOI
1ac65 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 NT_ROLLBACK || e
1ac66 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 Op==SAVEPOINT_RE
1ac67 4c 45 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65 LEASE);. asse
1ac68 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 rt( db->nStateme
1ac69 6e 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 nt>0 );. asse
1ac6a 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e rt( p->iStatemen
1ac6b 74 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 t==(db->nStateme
1ac6c 6e 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e nt+db->nSavepoin
1ac6d 74 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 t) );.. for(i
1ac6e 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 =0; i<db->nDb; i
1ac6f 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 ++){ . int
1ac70 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b rc2 = SQLITE_OK;
1ac71 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 . Btree *pB
1ac72 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 t = db->aDb[i].p
1ac73 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 Bt;. if( pB
1ac74 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 t ){. if(
1ac75 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f eOp==SAVEPOINT_
1ac76 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 ROLLBACK ){.
1ac77 20 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 rc2 = sqli
1ac78 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e te3BtreeSavepoin
1ac79 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 t(pBt, SAVEPOINT
1ac7a 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 _ROLLBACK, iSave
1ac7b 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 point);.
1ac7c 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 }. if( rc
1ac7d 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 2==SQLITE_OK ){.
1ac7e 20 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 rc2 =
1ac7f 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 sqlite3BtreeSave
1ac80 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 point(pBt, SAVEP
1ac81 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 OINT_RELEASE, iS
1ac82 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 avepoint);.
1ac83 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 }. if(
1ac84 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1ac85 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
1ac86 20 72 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a rc2;. }.
1ac87 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1ac88 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 db->nStatement
1ac89 2d 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 --;. p->iStat
1ac8a 65 6d 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 ement = 0;..
1ac8b 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d /* If the statem
1ac8c 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 ent transaction
1ac8d 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 is being rolled
1ac8e 62 61 63 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f back, also resto
1ac8f 72 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 re the . ** d
1ac90 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 atabase handles
1ac91 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 deferred constra
1ac92 69 6e 74 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 int counter to t
1ac93 68 65 20 76 61 6c 75 65 20 69 74 20 68 61 64 20 he value it had
1ac94 77 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 when . ** the
1ac95 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 statement trans
1ac96 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 action was opene
1ac97 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 d. */. if( e
1ac98 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f Op==SAVEPOINT_RO
1ac99 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 LLBACK ){.
1ac9a 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e db->nDeferredCon
1ac9b 73 20 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 s = p->nStmtDefC
1ac9c 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ons;. }. }.
1ac9d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1ac9e 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 *.** If SQLite i
1ac9f 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 s compiled to su
1aca0 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 pport shared-cac
1aca1 68 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 he mode and to b
1aca2 65 20 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a e threadsafe,.**
1aca3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 this routine ob
1aca4 74 61 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20 tains the mutex
1aca5 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1aca6 65 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74 each BtShared st
1aca7 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 ructure.** that
1aca8 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 may be accessed
1aca9 62 79 20 74 68 65 20 56 4d 20 70 61 73 73 65 64 by the VM passed
1acaa 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
1acab 20 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a In doing so it.
1acac 2a 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68 ** sets the BtSh
1acad 61 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f ared.db member o
1acae 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 f each of the Bt
1acaf 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 Shared structure
1acb0 73 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 s, ensuring.** t
1acb1 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 hat the correct
1acb2 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c busy-handler cal
1acb3 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 lback is invoked
1acb4 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a if required..**
1acb5 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 .** If SQLite is
1acb6 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 not threadsafe
1acb7 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74 but does support
1acb8 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f shared-cache mo
1acb9 64 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 de, then.** sqli
1acba 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1acbb 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f () is invoked to
1acbc 20 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65 set the BtShare
1acbd 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a d.db variables.*
1acbe 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 * of all of BtSh
1acbf 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73 20 ared structures
1acc0 61 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 74 accessible via t
1acc1 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 he database hand
1acc2 6c 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 le .** associate
1acc3 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f d with the VM. O
1acc4 66 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 f course only a
1acc5 73 75 62 73 65 74 20 6f 66 20 74 68 65 73 65 20 subset of these
1acc6 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 structures.** wi
1acc7 6c 6c 20 62 65 20 61 63 63 65 73 73 65 64 20 62 ll be accessed b
1acc8 79 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 y the VM, and we
1acc9 20 63 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e could use Vdbe.
1acca 62 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 btreeMask to fig
1accb 75 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 ure.** that subs
1accc 65 74 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72 et out, but ther
1accd 65 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 e is no advantag
1acce 65 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a e to doing so..*
1accf 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 *.** If SQLite i
1acd0 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 s not threadsafe
1acd1 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 and does not su
1acd2 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 pport shared-cac
1acd3 68 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a he mode, this.**
1acd4 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e function is a n
1acd5 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 o-op..*/.#ifndef
1acd6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1acd7 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 RED_CACHE.SQLITE
1acd8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1acd9 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 lite3VdbeMutexAr
1acda 72 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 rayEnter(Vdbe *p
1acdb 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 ){.#if SQLITE_TH
1acdc 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 READSAFE. sqlit
1acdd 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 e3BtreeMutexArra
1acde 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 yEnter(&p->aMute
1acdf 78 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 x);.#else. sqli
1ace0 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c te3BtreeEnterAll
1ace1 28 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a (p->db);.#endif.
1ace2 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1ace3 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 This function is
1ace4 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 called when a t
1ace5 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 ransaction opene
1ace6 64 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 d by the databas
1ace7 65 20 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 e .** handle ass
1ace8 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 ociated with the
1ace9 20 56 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e VM passed as an
1acea 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 62 6f argument is abo
1aceb 75 74 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d ut to be .** com
1acec 6d 69 74 74 65 64 2e 20 49 66 20 74 68 65 72 65 mitted. If there
1aced 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 are outstanding
1acee 20 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 deferred foreig
1acef 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 n key constraint
1acf0 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 .** violations,
1acf1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 return SQLITE_ER
1acf2 52 4f 52 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ROR. Otherwise,
1acf3 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a SQLITE_OK..**.**
1acf4 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 If there are ou
1acf5 74 73 74 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f tstanding FK vio
1acf6 6c 61 74 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 lations and this
1acf7 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e function return
1acf8 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 s .** SQLITE_ERR
1acf9 4f 52 2c 20 73 65 74 20 74 68 65 20 72 65 73 75 OR, set the resu
1acfa 6c 74 20 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 lt of the VM to
1acfb 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e SQLITE_CONSTRAIN
1acfc 54 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 T and write.** a
1acfd 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
1acfe 74 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 74 75 to it. Then retu
1acff 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e rn SQLITE_ERROR.
1ad00 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
1ad01 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f TE_OMIT_FOREIGN_
1ad02 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 KEY.SQLITE_PRIVA
1ad03 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 TE int sqlite3Vd
1ad04 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a beCheckFk(Vdbe *
1ad05 70 2c 20 69 6e 74 20 64 65 66 65 72 72 65 64 29 p, int deferred)
1ad06 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 {. sqlite3 *db
1ad07 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 = p->db;. if( (
1ad08 64 65 66 65 72 72 65 64 20 26 26 20 64 62 2d 3e deferred && db->
1ad09 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 nDeferredCons>0)
1ad0a 20 7c 7c 20 28 21 64 65 66 65 72 72 65 64 20 26 || (!deferred &
1ad0b 26 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 & p->nFkConstrai
1ad0c 6e 74 3e 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e nt>0) ){. p->
1ad0d 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 rc = SQLITE_CONS
1ad0e 54 52 41 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 TRAINT;. p->e
1ad0f 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f rrorAction = OE_
1ad10 41 62 6f 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 Abort;. sqlit
1ad11 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e e3SetString(&p->
1ad12 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f zErrMsg, db, "fo
1ad13 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 reign key constr
1ad14 61 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 aint failed");.
1ad15 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 return SQLITE
1ad16 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 _ERROR;. }. re
1ad17 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a turn SQLITE_OK;.
1ad18 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1ad19 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 This routine is
1ad1a 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 called the when
1ad1b 61 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 a VDBE tries to
1ad1c 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 halt. If the VD
1ad1d 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 BE.** has made c
1ad1e 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e hanges and is in
1ad1f 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 autocommit mode
1ad20 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 , then commit th
1ad21 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 ose.** changes.
1ad22 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 If a rollback i
1ad23 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 s needed, then d
1ad24 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a o the rollback..
1ad25 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1ad26 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 ne is the only w
1ad27 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 ay to move the s
1ad28 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f tate of a VM fro
1ad29 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 m.** SQLITE_MAGI
1ad2a 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f C_RUN to SQLITE_
1ad2b 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 MAGIC_HALT. It
1ad2c 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a is harmless to.*
1ad2d 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 * call this on a
1ad2e 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 VM that is in t
1ad2f 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f he SQLITE_MAGIC_
1ad30 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a HALT state..**.*
1ad31 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f * Return an erro
1ad32 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 r code. If the
1ad33 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 commit could not
1ad34 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 complete becaus
1ad35 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e e of.** lock con
1ad36 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 tention, return
1ad37 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 SQLITE_BUSY. If
1ad38 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 SQLITE_BUSY is
1ad39 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 returned, it.**
1ad3a 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 means the close
1ad3b 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 did not happen a
1ad3c 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 nd needs to be r
1ad3d 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 epeated..*/.SQLI
1ad3e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1ad3f 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 qlite3VdbeHalt(V
1ad40 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 dbe *p){. int r
1ad41 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 c;
1ad42 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 /* Us
1ad43 65 64 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e ed to store tran
1ad44 73 69 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 sient return cod
1ad45 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 es */. sqlite3
1ad46 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 *db = p->db;..
1ad47 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e /* This function
1ad48 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f contains the lo
1ad49 67 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 gic that determi
1ad4a 6e 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 nes if a stateme
1ad4b 6e 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 nt or. ** trans
1ad4c 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 action will be c
1ad4d 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c ommitted or roll
1ad4e 65 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 ed back as a res
1ad4f 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 ult of the. **
1ad50 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 execution of thi
1ad51 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e s virtual machin
1ad52 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 e. . **. ** If
1ad53 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c any of the foll
1ad54 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 owing errors occ
1ad55 75 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 ur:. **. **
1ad56 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 SQLITE_NOMEM.
1ad57 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 ** SQLITE_I
1ad58 4f 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 OERR. ** SQ
1ad59 4c 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 LITE_FULL. **
1ad5a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 SQLITE_INTERR
1ad5b 55 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 UPT. **. ** Th
1ad5c 65 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 en the internal
1ad5d 63 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 cache might have
1ad5e 20 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e been left in an
1ad5f 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 inconsistent.
1ad60 2a 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 ** state. We ne
1ad61 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ed to rollback t
1ad62 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 he statement tra
1ad63 6e 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 nsaction, if the
1ad64 72 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 re is. ** one,
1ad65 6f 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 or the complete
1ad66 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 transaction if t
1ad67 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 here is no state
1ad68 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e ment transaction
1ad69 2e 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d .. */.. if( p-
1ad6a 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
1ad6b 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d d ){. p->rc =
1ad6c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1ad6d 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 }. closeAllCur
1ad6e 73 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70 sors(p);. if( p
1ad6f 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 ->magic!=VDBE_MA
1ad70 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 GIC_RUN ){. r
1ad71 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1ad72 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 . }. checkActi
1ad73 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a veVdbeCnt(db);..
1ad74 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f /* No commit o
1ad75 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 r rollback neede
1ad76 64 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d d if the program
1ad77 20 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a never started *
1ad78 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 /. if( p->pc>=0
1ad79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b ){. int mrc;
1ad7a 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 /* Primary er
1ad7b 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d ror code from p-
1ad7c 3e 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 >rc */. int e
1ad7d 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b StatementOp = 0;
1ad7e 0a 20 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 . int isSpeci
1ad7f 61 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 alError;
1ad80 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 /* Set to tr
1ad81 75 65 20 69 66 20 61 20 27 73 70 65 63 69 61 6c ue if a 'special
1ad82 27 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 ' error */..
1ad83 2f 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 /* Lock all btre
1ad84 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 es used by the s
1ad85 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 tatement */.
1ad86 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 sqlite3VdbeMutex
1ad87 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a ArrayEnter(p);..
1ad88 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 /* Check for
1ad89 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 one of the spec
1ad8a 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 ial errors */.
1ad8b 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 mrc = p->rc &
1ad8c 30 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 0xff;. assert
1ad8d 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f ( p->rc!=SQLITE_
1ad8e 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b IOERR_BLOCKED );
1ad8f 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 /* This error
1ad90 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 no longer exists
1ad91 20 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 */. isSpecia
1ad92 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 lError = mrc==SQ
1ad93 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 LITE_NOMEM || mr
1ad94 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a c==SQLITE_IOERR.
1ad95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ad96 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c || mrc==SQL
1ad97 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c ITE_INTERRUPT ||
1ad98 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c mrc==SQLITE_FUL
1ad99 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 L;. if( isSpe
1ad9a 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 cialError ){.
1ad9b 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 /* If the que
1ad9c 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 ry was read-only
1ad9d 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 , we need do no
1ad9e 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e rollback at all.
1ad9f 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 Otherwise,.
1ada0 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 ** proceed wit
1ada1 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 h the special ha
1ada2 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f ndling.. */
1ada3 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 . if( !p->r
1ada4 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d eadOnly || mrc!=
1ada5 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 SQLITE_INTERRUPT
1ada6 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1ada7 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d (mrc==SQLITE_NOM
1ada8 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 EM || mrc==SQLIT
1ada9 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 E_FULL) && p->us
1adaa 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b esStmtJournal ){
1adab 0a 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 . eStat
1adac 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f ementOp = SAVEPO
1adad 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 INT_ROLLBACK;.
1adae 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
1adaf 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 /* We are
1adb0 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 forced to roll
1adb1 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 back the active
1adb2 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 transaction. Bef
1adb3 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 ore doing.
1adb4 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 ** so, abort
1adb5 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 any other state
1adb6 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c ments this handl
1adb7 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 e currently has
1adb8 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 active..
1adb9 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 */. i
1adba 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 nvalidateCursors
1adbb 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 OnModifiedBtrees
1adbc 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 (db);.
1adbd 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 sqlite3RollbackA
1adbe 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 ll(db);.
1adbf 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 sqlite3CloseSa
1adc0 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 vepoints(db);.
1adc1 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f db->auto
1adc2 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 Commit = 1;.
1adc3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1adc4 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 }.. /* Chec
1adc5 6b 20 66 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 k for immediate
1adc6 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c foreign key viol
1adc7 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 ations. */. i
1adc8 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 f( p->rc==SQLITE
1adc9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c _OK ){. sql
1adca 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 ite3VdbeCheckFk(
1adcb 70 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a p, 0);. }. .
1adcc 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 /* If the au
1adcd 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 to-commit flag i
1adce 73 20 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 s set and this i
1adcf 73 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 s the only activ
1add0 65 20 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a e writer . **
1add1 20 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 VM, then we do
1add2 65 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 either a commit
1add3 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 or rollback of t
1add4 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
1add5 61 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a action. . **.
1add6 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 ** Note: Thi
1add7 73 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e s block also run
1add8 73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 s if one of the
1add9 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 special errors h
1adda 61 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 andled . ** a
1addb 62 6f 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 bove has occurre
1addc 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 d. . */. i
1addd 66 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 f( !sqlite3VtabI
1adde 6e 53 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 nSync(db) .
1addf 26 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 && db->autoCommi
1ade0 74 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 t . && db->w
1ade1 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d riteVdbeCnt==(p-
1ade2 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 >readOnly==0) .
1ade3 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ){. if(
1ade4 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b p->rc==SQLITE_OK
1ade5 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 || (p->errorAct
1ade6 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 ion==OE_Fail &&
1ade7 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 !isSpecialError)
1ade8 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
1ade9 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b sqlite3VdbeCheck
1adea 46 6b 28 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 Fk(p, 1) ){.
1adeb 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 sqlite3Btr
1adec 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 eeMutexArrayLeav
1aded 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 e(&p->aMutex);.
1adee 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 return
1adef 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1adf0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1adf1 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d /* The auto-comm
1adf2 69 74 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c it flag is true,
1adf3 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 the vdbe progra
1adf4 6d 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c m was successful
1adf5 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 . ** or
1adf6 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 hit an 'OR FAIL'
1adf7 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 constraint and
1adf8 74 68 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 there are no def
1adf9 65 72 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 erred foreign.
1adfa 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e ** key con
1adfb 73 74 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 straints to hold
1adfc 20 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 up the transact
1adfd 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 ion. This means
1adfe 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 a commit .
1adff 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 ** is required
1ae00 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 . */. rc
1ae01 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 = vdbeCommit(db
1ae02 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 , p);. if
1ae03 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 ( rc==SQLITE_BUS
1ae04 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 Y ){. s
1ae05 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 qlite3BtreeMutex
1ae06 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 ArrayLeave(&p->a
1ae07 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 Mutex);.
1ae08 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1ae09 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 BUSY;. }e
1ae0a 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 lse if( rc!=SQLI
1ae0b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1ae0c 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 p->rc = rc;.
1ae0d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
1ae0e 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b RollbackAll(db);
1ae0f 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
1ae10 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 db->nD
1ae11 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b eferredCons = 0;
1ae12 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
1ae13 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c e3CommitInternal
1ae14 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 Changes(db);.
1ae15 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c }. }el
1ae16 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
1ae17 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 te3RollbackAll(d
1ae18 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 b);. }.
1ae19 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 db->nStatement
1ae1a 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 0;. }else
1ae1b 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 if( eStatementOp
1ae1c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 ==0 ){. if(
1ae1d 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f p->rc==SQLITE_O
1ae1e 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 K || p->errorAct
1ae1f 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a ion==OE_Fail ){.
1ae20 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 eStateme
1ae21 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 ntOp = SAVEPOINT
1ae22 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 _RELEASE;.
1ae23 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 }else if( p->err
1ae24 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f orAction==OE_Abo
1ae25 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 rt ){. eS
1ae26 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 tatementOp = SAV
1ae27 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b EPOINT_ROLLBACK;
1ae28 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1ae29 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 invalidate
1ae2a 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 CursorsOnModifie
1ae2b 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 dBtrees(db);.
1ae2c 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c sqlite3Roll
1ae2d 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 backAll(db);.
1ae2e 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 sqlite3Clos
1ae2f 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b eSavepoints(db);
1ae30 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 . db->aut
1ae31 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 oCommit = 1;.
1ae32 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
1ae33 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 /* If eStateme
1ae34 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f ntOp is non-zero
1ae35 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 , then a stateme
1ae36 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e nt transaction n
1ae37 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 eeds to. ** b
1ae38 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 e committed or r
1ae39 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c olled back. Call
1ae3a 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 sqlite3VdbeClos
1ae3b 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a eStatement() to.
1ae3c 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 ** do so. If
1ae3d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 this operation
1ae3e 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 returns an error
1ae3f 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e , and the curren
1ae40 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 t statement.
1ae41 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 ** error code is
1ae42 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 SQLITE_OK or SQ
1ae43 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c LITE_CONSTRAINT,
1ae44 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 then set the er
1ae45 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 ror. ** code
1ae46 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 to the new value
1ae47 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
1ae48 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b eStatementOp ){
1ae49 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1ae4a 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 te3VdbeCloseStat
1ae4b 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d ement(p, eStatem
1ae4c 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 entOp);. if
1ae4d 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d ( rc && (p->rc==
1ae4e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e SQLITE_OK || p->
1ae4f 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 rc==SQLITE_CONST
1ae50 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 RAINT) ){.
1ae51 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
1ae52 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1ae53 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d ree(db, p->zErrM
1ae54 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e sg);. p->
1ae55 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 zErrMsg = 0;.
1ae56 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 }. }. .
1ae57 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 /* If this was
1ae58 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 an INSERT, UPDA
1ae59 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 TE or DELETE and
1ae5a 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 no statement tr
1ae5b 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a ansaction. **
1ae5c 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 has been rolled
1ae5d 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 back, update th
1ae5e 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 e database conne
1ae5f 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 ction change-cou
1ae60 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 nter. . */.
1ae61 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 if( p->changeC
1ae62 6e 74 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 ntOn ){. if
1ae63 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d ( eStatementOp!=
1ae64 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 SAVEPOINT_ROLLBA
1ae65 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 CK ){. sq
1ae66 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e lite3VdbeSetChan
1ae67 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e ges(db, p->nChan
1ae68 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 ge);. }else
1ae69 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
1ae6a 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
1ae6b 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a db, 0);. }.
1ae6c 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 p->nChange
1ae6d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 = 0;. }. .
1ae6e 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f /* Rollback o
1ae6f 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 r commit any sch
1ae70 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 ema changes that
1ae71 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 occurred. */.
1ae72 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c if( p->rc!=SQL
1ae73 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c ITE_OK && db->fl
1ae74 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 ags&SQLITE_Inter
1ae75 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 nChanges ){.
1ae76 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e sqlite3ResetIn
1ae77 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1ae78 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 0);. db->f
1ae79 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 lags = (db->flag
1ae7a 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 s | SQLITE_Inter
1ae7b 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d nChanges);. }
1ae7c 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 .. /* Release
1ae7d 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 the locks */.
1ae7e 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 sqlite3BtreeMu
1ae7f 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 texArrayLeave(&p
1ae80 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a ->aMutex);. }..
1ae81 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 /* We have suc
1ae82 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 cessfully halted
1ae83 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 and closed the
1ae84 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 VM. Record this
1ae85 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 fact. */. if(
1ae86 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 p->pc>=0 ){.
1ae87 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e db->activeVdbeCn
1ae88 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d t--;. if( !p-
1ae89 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 >readOnly ){.
1ae8a 20 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 db->writeVdbe
1ae8b 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 Cnt--;. }.
1ae8c 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 assert( db->act
1ae8d 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e iveVdbeCnt>=db->
1ae8e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a writeVdbeCnt );.
1ae8f 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d }. p->magic =
1ae90 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 VDBE_MAGIC_HALT
1ae91 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 ;. checkActiveV
1ae92 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 dbeCnt(db);. if
1ae93 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 ( p->db->mallocF
1ae94 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e ailed ){. p->
1ae95 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 rc = SQLITE_NOME
1ae96 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 M;. }.. /* If
1ae97 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 the auto-commit
1ae98 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 flag is set to t
1ae99 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f rue, then any lo
1ae9a 63 6b 73 20 74 68 61 74 20 77 65 72 65 20 68 65 cks that were he
1ae9b 6c 64 0a 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 ld. ** by conne
1ae9c 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f ction db have no
1ae9d 77 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e w been released.
1ae9e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e Call sqlite3Con
1ae9f 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 nectionUnlocked(
1aea0 29 20 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b ) . ** to invok
1aea1 65 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 75 e any required u
1aea2 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c nlock-notify cal
1aea3 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 lbacks.. */. i
1aea4 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 f( db->autoCommi
1aea5 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
1aea6 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b ConnectionUnlock
1aea7 65 64 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 ed(db);. }.. a
1aea8 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 ssert( db->activ
1aea9 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 eVdbeCnt>0 || db
1aeaa 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 ->autoCommit==0
1aeab 7c 7c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e || db->nStatemen
1aeac 74 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e t==0 );. return
1aead 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a SQLITE_OK;.}...
1aeae 2f 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 /*.** Each VDBE
1aeaf 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 holds the result
1aeb0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 of the most rec
1aeb1 65 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 ent sqlite3_step
1aeb2 28 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d () call.** in p-
1aeb3 3e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 >rc. This routi
1aeb4 6e 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73 ne sets that res
1aeb5 75 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 ult back to SQLI
1aeb6 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 TE_OK..*/.SQLITE
1aeb7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1aeb8 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 lite3VdbeResetSt
1aeb9 65 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 epResult(Vdbe *p
1aeba 29 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c ){. p->rc = SQL
1aebb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ITE_OK;.}../*.**
1aebc 20 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 Clean up a VDBE
1aebd 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e after execution
1aebe 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 but do not dele
1aebf 74 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 te the VDBE just
1aec0 20 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 yet..** Write a
1aec1 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 ny error message
1aec2 73 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 s into *pzErrMsg
1aec3 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 . Return the re
1aec4 73 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a sult code..**.**
1aec5 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 After this rout
1aec6 69 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 ine is run, the
1aec7 56 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 VDBE should be r
1aec8 65 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 eady to be execu
1aec9 74 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a ted.** again..**
1aeca 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 .** To look at i
1aecb 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 t another way, t
1aecc 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 his routine rese
1aecd 74 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 ts the state of
1aece 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d the.** virtual m
1aecf 61 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 achine from VDBE
1aed0 5f 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 _MAGIC_RUN or VD
1aed1 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 BE_MAGIC_HALT ba
1aed2 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 ck to.** VDBE_MA
1aed3 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c GIC_INIT..*/.SQL
1aed4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1aed5 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 sqlite3VdbeReset
1aed6 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c (Vdbe *p){. sql
1aed7 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d ite3 *db;. db =
1aed8 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 p->db;.. /* If
1aed9 20 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 the VM did not
1aeda 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f run to completio
1aedb 6e 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 n or if it encou
1aedc 6e 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 ntered an. ** e
1aedd 72 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 rror, then it mi
1aede 67 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 ght not have bee
1aedf 6e 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c n halted properl
1aee0 79 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a y. So halt. **
1aee1 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 it now.. */.
1aee2 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1aee3 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c etyOn(db);. sql
1aee4 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1aee5 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 . (void)sqlite3
1aee6 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a SafetyOff(db);..
1aee7 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 /* If the VDBE
1aee8 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e has be run even
1aee9 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e partially, then
1aeea 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 transfer the er
1aeeb 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e ror code. ** an
1aeec 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 d error message
1aeed 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e from the VDBE in
1aeee 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 to the main data
1aeef 62 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 base structure.
1aef0 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 But. ** if the
1aef1 20 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 VDBE has just b
1aef2 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 een set to run b
1aef3 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 ut has not actua
1aef4 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 lly executed any
1aef5 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f . ** instructio
1aef6 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 ns yet, leave th
1aef7 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1aef8 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f error informatio
1aef9 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a n unchanged.. *
1aefa 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 /. if( p->pc>=0
1aefb 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a ){. if( p->z
1aefc 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 ErrMsg ){.
1aefd 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 sqlite3BeginBeni
1aefe 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 gnMalloc();.
1aeff 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 sqlite3ValueSe
1af00 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 tStr(db->pErr,-1
1af01 2c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 ,p->zErrMsg,SQLI
1af02 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 TE_UTF8,SQLITE_T
1af03 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 RANSIENT);.
1af04 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 sqlite3EndBenig
1af05 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 nMalloc();.
1af06 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 db->errCode = p
1af07 2d 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 ->rc;. sqli
1af08 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d te3DbFree(db, p-
1af09 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 >zErrMsg);.
1af0a 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b p->zErrMsg = 0;
1af0b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
1af0c 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 ->rc ){. sq
1af0d 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 lite3Error(db, p
1af0e 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 ->rc, 0);. }e
1af0f 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 lse{. sqlit
1af10 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 e3Error(db, SQLI
1af11 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d TE_OK, 0);. }
1af12 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e . }else if( p->
1af13 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 rc && p->expired
1af14 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 ){. /* The e
1af15 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 xpired flag was
1af16 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 set on the VDBE
1af17 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 before the first
1af18 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 call. ** to
1af19 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 sqlite3_step().
1af1a 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 For consistency
1af1b 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 (since sqlite3_s
1af1c 74 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a tep() was. **
1af1d 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 called), set th
1af1e 65 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 e database error
1af1f 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 in this case as
1af20 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 well.. */.
1af21 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 sqlite3Error(d
1af22 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 b, p->rc, 0);.
1af23 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 sqlite3ValueSe
1af24 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d tStr(db->pErr, -
1af25 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 1, p->zErrMsg, S
1af26 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 QLITE_UTF8, SQLI
1af27 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 TE_TRANSIENT);.
1af28 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1af29 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1af2a 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 ;. p->zErrMsg
1af2b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 = 0;. }.. /*
1af2c 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f Reclaim all memo
1af2d 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56 ry used by the V
1af2e 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e DBE. */. Clean
1af2f 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 up(p);.. /* Sav
1af30 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f e profiling info
1af31 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 rmation from thi
1af32 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f s VDBE run.. */
1af33 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
1af34 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c FILE. {. FIL
1af35 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 E *out = fopen("
1af36 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 vdbe_profile.out
1af37 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 ", "a");. if(
1af38 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e out ){. in
1af39 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e t i;. fprin
1af3a 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 tf(out, "---- ")
1af3b 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b ;. for(i=0;
1af3c 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b i<p->nOp; i++){
1af3d 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 . fprintf
1af3e 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d (out, "%02x", p-
1af3f 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b >aOp[i].opcode);
1af40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 . }. f
1af41 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 printf(out, "\n"
1af42 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 );. for(i=0
1af43 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 ; i<p->nOp; i++)
1af44 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 {. fprint
1af45 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c f(out, "%6d %10l
1af46 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 ld %8lld ",.
1af47 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d p->aOp[i]
1af48 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 .cnt,.
1af49 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 p->aOp[i].cycle
1af4a 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d s,. p-
1af4b 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 >aOp[i].cnt>0 ?
1af4c 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 p->aOp[i].cycles
1af4d 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a /p->aOp[i].cnt :
1af4e 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 0. );.
1af4f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1af50 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c ePrintOp(out, i,
1af51 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 &p->aOp[i]);.
1af52 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f }. fclo
1af53 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 se(out);. }.
1af54 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d }.#endif. p->m
1af55 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 agic = VDBE_MAGI
1af56 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e C_INIT;. return
1af57 20 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 p->rc & db->err
1af58 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 Mask;.}. ./*.**
1af59 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c Clean up and del
1af5a 65 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 ete a VDBE after
1af5b 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 execution. Ret
1af5c 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 urn an integer w
1af5d 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 hich is.** the r
1af5e 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 esult code. Wri
1af5f 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 te any error mes
1af60 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a sage text into *
1af61 70 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c pzErrMsg..*/.SQL
1af62 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1af63 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c sqlite3VdbeFinal
1af64 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 ize(Vdbe *p){.
1af65 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
1af66 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 OK;. if( p->mag
1af67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 ic==VDBE_MAGIC_R
1af68 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d UN || p->magic==
1af69 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 VDBE_MAGIC_HALT
1af6a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1af6b 74 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b te3VdbeReset(p);
1af6c 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 . assert( (rc
1af6d 20 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 & p->db->errMas
1af6e 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 k)==rc );. }.
1af6f 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 sqlite3VdbeDelet
1af70 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 e(p);. return r
1af71 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c c;.}../*.** Call
1af72 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 the destructor
1af73 66 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 for each auxdata
1af74 20 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 entry in pVdbeF
1af75 75 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a unc for which.**
1af76 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
1af77 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 ng bit in mask i
1af78 73 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 s clear. Auxdat
1af79 61 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 a entries beyond
1af7a 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 31.** are alway
1af7b 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f s destroyed. To
1af7c 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 destroy all aux
1af7d 64 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 data entries, ca
1af7e 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 ll this.** routi
1af7f 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e ne with mask==0.
1af80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1af81 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 TE void sqlite3V
1af82 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 dbeDeleteAuxData
1af83 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 (VdbeFunc *pVdbe
1af84 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b Func, int mask){
1af85 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 . int i;. for(
1af86 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 i=0; i<pVdbeFunc
1af87 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 ->nAux; i++){.
1af88 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 struct AuxData
1af89 20 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 *pAux = &pVdbeF
1af8a 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 unc->apAux[i];.
1af8b 20 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 if( (i>31 ||
1af8c 21 28 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 !(mask&(((u32)1)
1af8d 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e <<i))) && pAux->
1af8e 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 pAux ){. if
1af8f 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 ( pAux->xDelete
1af90 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d ){. pAux-
1af91 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 >xDelete(pAux->p
1af92 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Aux);. }.
1af93 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d pAux->pAux =
1af94 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
1af95 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
1af96 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f entire VDBE..*/
1af97 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1af98 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 void sqlite3Vdbe
1af99 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b Delete(Vdbe *p){
1af9a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1af9b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d . if( NEVER(p==
1af9c 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 0) ) return;. d
1af9d 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 b = p->db;. if(
1af9e 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 p->pPrev ){.
1af9f 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 p->pPrev->pNext
1afa0 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d = p->pNext;. }
1afa1 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1afa2 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 ( db->pVdbe==p )
1afa3 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 ;. db->pVdbe
1afa4 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a = p->pNext;. }.
1afa5 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 if( p->pNext )
1afa6 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e {. p->pNext->
1afa7 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 pPrev = p->pPrev
1afa8 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d ;. }. releaseM
1afa9 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c emArray(p->aVar,
1afaa 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c p->nVar);. rel
1afab 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e easeMemArray(p->
1afac 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 aColName, p->nRe
1afad 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f sColumn*COLNAME_
1afae 4e 29 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70 N);. vdbeFreeOp
1afaf 41 72 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 Array(db, p->aOp
1afb0 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c , p->nOp);. sql
1afb1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1afb2 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c ->aLabel);. sql
1afb3 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1afb4 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 ->aColName);. s
1afb5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1afb6 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e p->zSql);. p->
1afb7 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 magic = VDBE_MAG
1afb8 49 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 IC_DEAD;. sqlit
1afb9 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
1afba 70 46 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 pFree);. sqlite
1afbb 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 3DbFree(db, p);.
1afbc 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 }../*.** Make su
1afbd 72 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 20 re the cursor p
1afbe 69 73 20 72 65 61 64 79 20 74 6f 20 72 65 61 64 is ready to read
1afbf 20 6f 72 20 77 72 69 74 65 20 74 68 65 20 72 6f or write the ro
1afc0 77 20 74 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a w to which it.**
1afc1 20 77 61 73 20 6c 61 73 74 20 70 6f 73 69 74 69 was last positi
1afc2 6f 6e 65 64 2e 20 20 52 65 74 75 72 6e 20 61 6e oned. Return an
1afc3 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 error code if a
1afc4 6e 20 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 n OOM fault or I
1afc5 2f 4f 20 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 /O error.** prev
1afc6 65 6e 74 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 ents us from pos
1afc7 69 74 69 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 itioning the cur
1afc8 73 6f 72 20 74 6f 20 69 74 73 20 63 6f 72 72 65 sor to its corre
1afc9 63 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a ct position..**.
1afca 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f ** If a MoveTo o
1afcb 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 peration is pend
1afcc 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 65 6e ing on the given
1afcd 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f cursor, then do
1afce 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 that.** MoveTo
1afcf 6e 6f 77 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 now. If no move
1afd0 20 69 73 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 is pending, che
1afd1 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 ck to see if the
1afd2 20 72 6f 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a row has been.**
1afd3 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f deleted out fro
1afd4 6d 20 75 6e 64 65 72 20 74 68 65 20 63 75 72 73 m under the curs
1afd5 6f 72 20 61 6e 64 20 69 66 20 69 74 20 68 61 73 or and if it has
1afd6 2c 20 6d 61 72 6b 20 74 68 65 20 72 6f 77 20 61 , mark the row a
1afd7 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e s.** a NULL row.
1afd8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 .**.** If the cu
1afd9 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 rsor is already
1afda 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 pointing to the
1afdb 63 6f 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20 correct row and
1afdc 74 68 61 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 that row has.**
1afdd 6e 6f 74 20 62 65 65 6e 20 64 65 6c 65 74 65 64 not been deleted
1afde 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 out from under
1afdf 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 6e the cursor, then
1afe0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
1afe1 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c a no-op..*/.SQL
1afe2 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
1afe3 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1afe4 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 rMoveto(VdbeCurs
1afe5 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d or *p){. if( p-
1afe6 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 >deferredMoveto
1afe7 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 ){. int res,
1afe8 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 rc;.#ifdef SQLIT
1afe9 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74 65 72 E_TEST. exter
1afea 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 n int sqlite3_se
1afeb 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 arch_count;.#end
1afec 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 if. assert( p
1afed 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 ->isTable );.
1afee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1afef 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 eeMovetoUnpacked
1aff0 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 (p->pCursor, 0,
1aff1 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c p->movetoTarget,
1aff2 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 0, &res);. i
1aff3 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1aff4 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f c;. p->lastRo
1aff5 77 69 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 wid = p->movetoT
1aff6 61 72 67 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f arget;. p->ro
1aff7 77 69 64 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 widIsValid = ALW
1aff8 41 59 53 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 AYS(res==0) ?1:0
1aff9 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 ;. if( NEVER(
1affa 72 65 73 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 res<0) ){.
1affb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1affc 65 4e 65 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 eNext(p->pCursor
1affd 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 , &res);. i
1affe 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 f( rc ) return r
1afff 63 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 c;. }.#ifdef
1b000 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 SQLITE_TEST.
1b001 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 sqlite3_search_c
1b002 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 ount++;.#endif.
1b003 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f p->deferredMo
1b004 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d veto = 0;. p-
1b005 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 >cacheStatus = C
1b006 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 ACHE_STALE;. }e
1b007 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 lse if( ALWAYS(p
1b008 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 ->pCursor) ){.
1b009 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a int hasMoved;.
1b00a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c int rc = sql
1b00b 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 ite3BtreeCursorH
1b00c 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 asMoved(p->pCurs
1b00d 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a or, &hasMoved);.
1b00e 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 if( rc ) ret
1b00f 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 urn rc;. if(
1b010 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 hasMoved ){.
1b011 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 p->cacheStatus
1b012 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a = CACHE_STALE;.
1b013 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 p->nullRow
1b014 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
1b015 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1b016 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 OK;.}../*.** The
1b017 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 following funct
1b018 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 ions:.**.** sqli
1b019 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1b01a 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 e().** sqlite3Vd
1b01b 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1b01c 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ).** sqlite3Vdbe
1b01d 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 SerialLen().** s
1b01e 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b01f 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 Put().** sqlite3
1b020 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a VdbeSerialGet().
1b021 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 **.** encapsulat
1b022 65 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 e the code that
1b023 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 serializes value
1b024 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e s for storage in
1b025 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 SQLite.** data
1b026 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 and index record
1b027 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a s. Each serializ
1b028 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 ed value consist
1b029 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 s of a.** 'seria
1b02a 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c l-type' and a bl
1b02b 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 ob of data. The
1b02c 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 serial type is a
1b02d 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 n 8-byte unsigne
1b02e 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 d.** integer, st
1b02f 6f 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 ored as a varint
1b030 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 ..**.** In an SQ
1b031 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 Lite index recor
1b032 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 d, the serial ty
1b033 70 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 pe is stored dir
1b034 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 ectly before.**
1b035 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 the blob of data
1b036 20 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 that it corresp
1b037 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 onds to. In a ta
1b038 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 ble record, all
1b039 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 serial.** types
1b03a 61 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 are stored at th
1b03b 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 e start of the r
1b03c 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 ecord, and the b
1b03d 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a lobs of data at.
1b03e 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 ** the end. Henc
1b03f 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e e these function
1b040 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c s allow the call
1b041 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 er to handle the
1b042 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 .** serial-type
1b043 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 and data blob se
1b044 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 perately..**.**
1b045 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 The following ta
1b046 62 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 ble describes th
1b047 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 e various storag
1b048 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 e classes for da
1b049 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 ta:.**.** seri
1b04a 61 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 al type b
1b04b 79 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 ytes of data
1b04c 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d type.** ----
1b04d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d ---------- -
1b04e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 --------------
1b04f 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d --------------
1b050 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 -.** 0
1b051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b052 30 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 0 NUL
1b053 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 L.** 1
1b054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b055 31 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 1 sig
1b056 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 ned integer.**
1b057 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 2
1b058 20 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 2
1b059 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e signed in
1b05a 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 teger.** 3
1b05b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b05c 20 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 3
1b05d 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a signed integer.
1b05e 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20 ** 4
1b05f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20 4
1b060 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 signe
1b061 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 d integer.**
1b062 20 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20 5
1b063 20 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20 6
1b064 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 signed inte
1b065 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 ger.** 6
1b066 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b067 20 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73 8 s
1b068 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a igned integer.**
1b069 20 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20 7
1b06a 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 8
1b06b 20 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c IEEE fl
1b06c 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 oat.** 8
1b06d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b06e 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 0 I
1b06f 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 nteger constant
1b070 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 0.** 9
1b071 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b072 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 0 Int
1b073 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a eger constant 1.
1b074 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 ** 10,11
1b075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b076 20 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72 reser
1b077 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f ved for expansio
1b078 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e n.** N>=12 an
1b079 64 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d d even (N-
1b07a 31 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 12)/2 BLO
1b07b 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e B.** N>=13 an
1b07c 64 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d d odd (N-
1b07d 31 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 13)/2 tex
1b07e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e t.**.** The 8 an
1b07f 64 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61 d 9 types were a
1b080 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 dded in 3.3.0, f
1b081 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 ile format 4. P
1b082 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a rior versions.**
1b083 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 of SQLite will
1b084 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 not understand t
1b085 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 hose serial type
1b086 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 s..*/../*.** Ret
1b087 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 urn the serial-t
1b088 79 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 ype for the valu
1b089 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d e stored in pMem
1b08a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1b08b 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 ATE u32 sqlite3V
1b08c 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 dbeSerialType(Me
1b08d 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c m *pMem, int fil
1b08e 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 e_format){. int
1b08f 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 flags = pMem->f
1b090 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a lags;. int n;..
1b091 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f if( flags&MEM_
1b092 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 Null ){. retu
1b093 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
1b094 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b flags&MEM_Int ){
1b095 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f . /* Figure o
1b096 75 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 ut whether to us
1b097 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 e 1, 2, 4, 6 or
1b098 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 8 bytes. */.#
1b099 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 define MAX_6BYTE
1b09a 20 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38 ((((i64)0x00008
1b09b 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 000)<<32)-1).
1b09c 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 i64 i = pMem->u
1b09d 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 .i;. u64 u;.
1b09e 20 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d if( file_form
1b09f 61 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d at>=4 && (i&1)==
1b0a0 69 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 i ){. retur
1b0a1 6e 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 n 8+(u32)i;.
1b0a2 7d 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 }. u = i<0 ?
1b0a3 2d 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 -i : i;. if(
1b0a4 75 3c 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 u<=127 ) return
1b0a5 31 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 1;. if( u<=32
1b0a6 37 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 767 ) return 2;.
1b0a7 20 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 if( u<=83886
1b0a8 30 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 07 ) return 3;.
1b0a9 20 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 if( u<=214748
1b0aa 33 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 3647 ) return 4;
1b0ab 0a 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f . if( u<=MAX_
1b0ac 36 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 6BYTE ) return 5
1b0ad 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a ;. return 6;.
1b0ae 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 }. if( flags&
1b0af 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 MEM_Real ){.
1b0b0 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 return 7;. }.
1b0b1 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 assert( pMem->db
1b0b2 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c ->mallocFailed |
1b0b3 7c 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 | flags&(MEM_Str
1b0b4 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 |MEM_Blob) );.
1b0b5 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 n = pMem->n;. i
1b0b6 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a f( flags & MEM_Z
1b0b7 65 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 ero ){. n +=
1b0b8 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 pMem->u.nZero;.
1b0b9 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d }. assert( n>=
1b0ba 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 0 );. return ((
1b0bb 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c n*2) + 12 + ((fl
1b0bc 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 ags&MEM_Str)!=0)
1b0bd 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 );.}../*.** Retu
1b0be 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 rn the length of
1b0bf 20 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 the data corres
1b0c0 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 ponding to the s
1b0c1 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 upplied serial-t
1b0c2 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ype..*/.SQLITE_P
1b0c3 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 RIVATE u32 sqlit
1b0c4 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1b0c5 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 Len(u32 serial_t
1b0c6 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 ype){. if( seri
1b0c7 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 al_type>=12 ){.
1b0c8 20 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 return (seria
1b0c9 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 l_type-12)/2;.
1b0ca 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 }else{. stati
1b0cb 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 c const u8 aSize
1b0cc 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 [] = { 0, 1, 2,
1b0cd 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 3, 4, 6, 8, 8, 0
1b0ce 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 , 0, 0, 0 };.
1b0cf 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 return aSize[se
1b0d0 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a rial_type];. }.
1b0d1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 }../*.** If we a
1b0d2 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 re on an archite
1b0d3 63 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 cture with mixed
1b0d4 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 -endian floating
1b0d5 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a .** points (ex:
1b0d6 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 ARM7) then swap
1b0d7 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 the lower 4 byt
1b0d8 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 es with the .**
1b0d9 75 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 upper 4 bytes.
1b0da 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c Return the resul
1b0db 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 t..**.** For mos
1b0dc 74 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c t architectures,
1b0dd 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 this is a no-op
1b0de 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a ..**.** (later):
1b0df 20 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 It is reported
1b0e0 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 to me that the
1b0e1 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f mixed-endian pro
1b0e2 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 blem.** on ARM7
1b0e3 69 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 is an issue with
1b0e4 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 GCC, not with t
1b0e5 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 he ARM7 chip. I
1b0e6 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 t seems.** that
1b0e7 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f early versions o
1b0e8 66 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 f GCC stored the
1b0e9 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 two words of a
1b0ea 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 64-bit.** float
1b0eb 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 in the wrong ord
1b0ec 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 er. And that er
1b0ed 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f ror has been pro
1b0ee 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 pagated.** ever
1b0ef 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d since. The blam
1b0f0 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 e is not necessa
1b0f1 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 rily with GCC, t
1b0f2 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 hough..** GCC mi
1b0f3 67 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f ght have just co
1b0f4 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 pying the proble
1b0f5 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 m from a prior c
1b0f6 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d ompiler..** I am
1b0f7 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 also told that
1b0f8 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f newer versions o
1b0f9 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f f GCC that follo
1b0fa 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a w a different.**
1b0fb 20 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74 ABI get the byt
1b0fc 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a e order right..*
1b0fd 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 *.** Developers
1b0fe 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 using SQLite on
1b0ff 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 an ARM7 should c
1b100 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 ompile and run t
1b101 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 heir.** applicat
1b102 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 ion using -DSQLI
1b103 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 TE_DEBUG=1 at le
1b104 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 ast once. With
1b105 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 DEBUG.** enabled
1b106 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 , some asserts b
1b107 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 elow will ensure
1b108 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f that the byte o
1b109 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 rder of.** float
1b10a 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 ing point values
1b10b 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a is correct..**.
1b10c 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 ** (2007-08-30)
1b10d 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 Frank van Vugt
1b10e 68 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73 has studied this
1b10f 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 problem closely
1b110 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 .** and has send
1b111 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f his findings to
1b112 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 the SQLite deve
1b113 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a lopers. Frank.*
1b114 2a 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f * writes that so
1b115 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 me Linux kernels
1b116 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 offer floating
1b117 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a point hardware.*
1b118 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 * emulation that
1b119 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 uses only 32-bi
1b11a 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 t mantissas inst
1b11b 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a ead of a full .*
1b11c 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 * 48-bits as req
1b11d 75 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45 uired by the IEE
1b11e 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 E standard. (Th
1b11f 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e is is the.** CON
1b120 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 FIG_FPE_FASTFPE
1b121 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 option.) On suc
1b122 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 h systems, float
1b123 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 ing point.** byt
1b124 65 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d e swapping becom
1b125 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 es very complica
1b126 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 ted. To avoid p
1b127 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 roblems,.** the
1b128 6e 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73 necessary byte s
1b129 77 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 wapping is carri
1b12a 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 ed out using a 6
1b12b 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 4-bit integer.**
1b12c 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 rather than a 6
1b12d 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 4-bit float. Fr
1b12e 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 ank assures us t
1b12f 68 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 hat the code her
1b130 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 e.** works for h
1b131 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 im. We, the dev
1b132 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f elopers, have no
1b133 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 way to independ
1b134 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 ently.** verify
1b135 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 this, but Frank
1b136 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 seems to know wh
1b137 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 at he is talking
1b138 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 about.** so we
1b139 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 trust him..*/.#i
1b13a 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 fdef SQLITE_MIXE
1b13b 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 D_ENDIAN_64BIT_F
1b13c 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 LOAT.static u64
1b13d 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e floatSwap(u64 in
1b13e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 ){. union {.
1b13f 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 u64 r;. u32
1b140 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 i[2];. } u;. u
1b141 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 32 t;.. u.r = i
1b142 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b n;. t = u.i[0];
1b143 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b . u.i[0] = u.i[
1b144 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 1];. u.i[1] = t
1b145 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a ;. return u.r;.
1b146 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d }.# define swapM
1b147 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 ixedEndianFloat(
1b148 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 X) X = floatSwa
1b149 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 p(X).#else.# def
1b14a 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 ine swapMixedEnd
1b14b 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 ianFloat(X).#end
1b14c 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 if../*.** Write
1b14d 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 the serialized d
1b14e 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 ata blob for the
1b14f 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e value stored in
1b150 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 pMem into .** b
1b151 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 uf. It is assume
1b152 64 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 d that the calle
1b153 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 r has allocated
1b154 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 sufficient space
1b155 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 ..** Return the
1b156 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 number of bytes
1b157 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e written..**.** n
1b158 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e Buf is the amoun
1b159 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 t of space left
1b15a 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 in buf[]. nBuf
1b15b 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a must always be.*
1b15c 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 * large enough t
1b15d 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 o hold the entir
1b15e 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 e field. Except
1b15f 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 , if the field i
1b160 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 s.** a blob with
1b161 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 a zero-filled t
1b162 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 ail, then buf[]
1b163 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 might be just th
1b164 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 e right.** size
1b165 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 to hold everythi
1b166 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 ng except for th
1b167 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 e zero-filled ta
1b168 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a il. If buf[].**
1b169 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f is only big eno
1b16a 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 ugh to hold the
1b16b 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c non-zero prefix,
1b16c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 then only write
1b16d 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 that.** prefix
1b16e 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 into buf[]. But
1b16f 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 if buf[] is lar
1b170 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c ge enough to hol
1b171 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 d both the.** pr
1b172 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 efix and the tai
1b173 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 l then write the
1b174 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 prefix and set
1b175 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a the tail to all.
1b176 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 ** zeros..**.**
1b177 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 Return the numbe
1b178 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 r of bytes actua
1b179 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f lly written into
1b17a 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d buf[]. The num
1b17b 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 ber.** of bytes
1b17c 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c in the zero-fill
1b17d 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 ed tail is inclu
1b17e 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 ded in the retur
1b17f 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 n value only.**
1b180 69 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 if those bytes w
1b181 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 ere zeroed in bu
1b182 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f f[]..*/ .SQLITE_
1b183 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 PRIVATE u32 sqli
1b184 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 te3VdbeSerialPut
1b185 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 (u8 *buf, int nB
1b186 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 uf, Mem *pMem, i
1b187 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b nt file_format){
1b188 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 . u32 serial_ty
1b189 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 pe = sqlite3Vdbe
1b18a 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c SerialType(pMem,
1b18b 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 file_format);.
1b18c 20 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 u32 len;.. /*
1b18d 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c Integer and Real
1b18e 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c */. if( serial
1b18f 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 _type<=7 && seri
1b190 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 al_type>0 ){.
1b191 20 75 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20 u64 v;. u32
1b192 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 i;. if( seria
1b193 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 l_type==7 ){.
1b194 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f assert( sizeo
1b195 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 f(v)==sizeof(pMe
1b196 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d m->r) );. m
1b197 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d emcpy(&v, &pMem-
1b198 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a >r, sizeof(v));.
1b199 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 swapMixedE
1b19a 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 ndianFloat(v);.
1b19b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1b19c 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 v = pMem->u.i;.
1b19d 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 }. len = i
1b19e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1b19f 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 rialTypeLen(seri
1b1a0 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 al_type);. as
1b1a1 73 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 sert( len<=(u32)
1b1a2 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c nBuf );. whil
1b1a3 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 e( i-- ){.
1b1a4 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 buf[i] = (u8)(v&
1b1a5 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 0xFF);. v >
1b1a6 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 >= 8;. }.
1b1a7 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a return len;. }.
1b1a8 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 . /* String or
1b1a9 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 blob */. if( se
1b1aa 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b rial_type>=12 ){
1b1ab 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 . assert( pMe
1b1ac 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 m->n + ((pMem->f
1b1ad 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 lags & MEM_Zero)
1b1ae 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 ?pMem->u.nZero:0
1b1af 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d ). =
1b1b0 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 = (int)sqlite3Vd
1b1b1 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1b1b2 73 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a serial_type) );.
1b1b3 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d assert( pMem
1b1b4 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 ->n<=nBuf );.
1b1b5 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a len = pMem->n;.
1b1b6 20 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 memcpy(buf,
1b1b7 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 pMem->z, len);.
1b1b8 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 if( pMem->fla
1b1b9 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b gs & MEM_Zero ){
1b1ba 0a 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d . len += pM
1b1bb 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 em->u.nZero;.
1b1bc 20 20 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e assert( nBuf>
1b1bd 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 =0 );. if(
1b1be 6c 65 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20 len > (u32)nBuf
1b1bf 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d ){. len =
1b1c0 20 28 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 (u32)nBuf;.
1b1c1 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 }. memset
1b1c2 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 (&buf[pMem->n],
1b1c3 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0, len-pMem->n);
1b1c4 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 . }. retur
1b1c5 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a n len;. }.. /*
1b1c6 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e NULL or constan
1b1c7 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 ts 0 or 1 */. r
1b1c8 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
1b1c9 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 * Deserialize th
1b1ca 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e e data blob poin
1b1cb 74 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 ted to by buf as
1b1cc 20 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 serial type ser
1b1cd 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 ial_type.** and
1b1ce 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1b1cf 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 in pMem. Retur
1b1d0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 n the number of
1b1d1 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a bytes read..*/ .
1b1d2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 SQLITE_PRIVATE u
1b1d3 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 32 sqlite3VdbeSe
1b1d4 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 rialGet(. const
1b1d5 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a unsigned char *
1b1d6 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 buf, /* Buff
1b1d7 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a er to deserializ
1b1d8 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 e from */. u32
1b1d9 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 serial_type,
1b1da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 /* Ser
1b1db 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 ial type to dese
1b1dc 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d rialize */. Mem
1b1dd 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 *pMem
1b1de 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 /* Me
1b1df 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 mory cell to wri
1b1e0 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f te value into */
1b1e1 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 .){. switch( se
1b1e2 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 rial_type ){.
1b1e3 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 case 10: /* R
1b1e4 65 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 eserved for futu
1b1e5 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 re use */. ca
1b1e6 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 se 11: /* Rese
1b1e7 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 rved for future
1b1e8 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 use */. case
1b1e9 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0: { /* NULL */
1b1ea 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 . pMem->fla
1b1eb 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1b1ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1b1ed 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 }. case 1: {
1b1ee 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 /* 1-byte signed
1b1ef 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 integer */.
1b1f0 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 pMem->u.i = (s
1b1f1 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 igned char)buf[0
1b1f2 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 ];. pMem->f
1b1f3 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1b1f4 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a return 1;.
1b1f5 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 }. case 2
1b1f6 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 : { /* 2-byte si
1b1f7 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
1b1f8 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 pMem->u.i
1b1f9 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 = (((signed char
1b1fa 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 )buf[0])<<8) | b
1b1fb 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 uf[1];. pMe
1b1fc 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 m->flags = MEM_I
1b1fd 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e nt;. return
1b1fe 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 2;. }. ca
1b1ff 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 se 3: { /* 3-byt
1b200 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 e signed integer
1b201 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e */. pMem->
1b202 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 u.i = (((signed
1b203 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 char)buf[0])<<16
1b204 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 ) | (buf[1]<<8)
1b205 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 | buf[2];.
1b206 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 pMem->flags = ME
1b207 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 M_Int;. ret
1b208 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 urn 3;. }.
1b209 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d case 4: { /* 4-
1b20a 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 byte signed inte
1b20b 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 ger */. pMe
1b20c 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d m->u.i = (buf[0]
1b20d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c <<24) | (buf[1]<
1b20e 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c <16) | (buf[2]<<
1b20f 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 8) | buf[3];.
1b210 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d pMem->flags =
1b211 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 MEM_Int;.
1b212 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a return 4;. }.
1b213 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a case 5: { /*
1b214 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6-byte signed i
1b215 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 nteger */.
1b216 75 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 u64 x = (((signe
1b217 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c d char)buf[0])<<
1b218 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 8) | buf[1];.
1b219 20 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b u32 y = (buf[
1b21a 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 2]<<24) | (buf[3
1b21b 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d ]<<16) | (buf[4]
1b21c 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 <<8) | buf[5];.
1b21d 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 x = (x<<32)
1b21e 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d | y;. pMem
1b21f 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 ->u.i = *(i64*)&
1b220 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 x;. pMem->f
1b221 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1b222 20 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a return 6;.
1b223 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 }. case 6
1b224 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 : /* 8-byte si
1b225 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a gned integer */.
1b226 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a case 7: { /*
1b227 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 IEEE floating p
1b228 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 oint */. u6
1b229 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 4 x;. u32 y
1b22a 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e ;.#if !defined(N
1b22b 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e DEBUG) && !defin
1b22c 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 ed(SQLITE_OMIT_F
1b22d 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 LOATING_POINT).
1b22e 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 /* Verify t
1b22f 68 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 hat integers and
1b230 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 floating point
1b231 76 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 values use the s
1b232 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 ame. ** byt
1b233 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 e order. Or, th
1b234 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 at if SQLITE_MIX
1b235 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f ED_ENDIAN_64BIT_
1b236 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a FLOAT is. *
1b237 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 * defined that 6
1b238 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 4-bit floating p
1b239 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c oint values real
1b23a 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 ly are mixed.
1b23b 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 ** endian..
1b23c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 */. sta
1b23d 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 tic const u64 t1
1b23e 20 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30 = ((u64)0x3ff00
1b23f 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 000)<<32;.
1b240 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 static const dou
1b241 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 ble r1 = 1.0;.
1b242 20 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b u64 t2 = t1;
1b243 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 . swapMixed
1b244 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b EndianFloat(t2);
1b245 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 . assert( s
1b246 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f izeof(r1)==sizeo
1b247 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 f(t2) && memcmp(
1b248 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 &r1, &t2, sizeof
1b249 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 (r1))==0 );.#end
1b24a 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 if.. x = (b
1b24b 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 uf[0]<<24) | (bu
1b24c 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 f[1]<<16) | (buf
1b24d 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d [2]<<8) | buf[3]
1b24e 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 ;. y = (buf
1b24f 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b [4]<<24) | (buf[
1b250 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5]<<16) | (buf[6
1b251 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a ]<<8) | buf[7];.
1b252 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 x = (x<<32
1b253 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 ) | y;. if(
1b254 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 serial_type==6
1b255 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d ){. pMem-
1b256 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 >u.i = *(i64*)&x
1b257 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e ;. pMem->
1b258 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b flags = MEM_Int;
1b259 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1b25a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 assert( si
1b25b 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 zeof(x)==8 && si
1b25c 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 zeof(pMem->r)==8
1b25d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 );. swap
1b25e 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 MixedEndianFloat
1b25f 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d (x);. mem
1b260 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 cpy(&pMem->r, &x
1b261 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 , sizeof(x));.
1b262 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b263 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e s = sqlite3IsNaN
1b264 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f (pMem->r) ? MEM_
1b265 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b Null : MEM_Real;
1b266 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 . }. r
1b267 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 eturn 8;. }.
1b268 20 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a case 8: /*
1b269 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 Integer 0 */.
1b26a 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 case 9: { /*
1b26b 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 Integer 1 */.
1b26c 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 pMem->u.i = s
1b26d 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 erial_type-8;.
1b26e 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 pMem->flags
1b26f 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 = MEM_Int;.
1b270 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
1b271 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a . default: {.
1b272 20 20 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 u32 len =
1b273 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 (serial_type-12)
1b274 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e /2;. pMem->
1b275 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b z = (char *)buf;
1b276 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d . pMem->n =
1b277 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d len;. pMem
1b278 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 ->xDel = 0;.
1b279 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 if( serial_typ
1b27a 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 e&0x01 ){.
1b27b 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b27c 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 MEM_Str | MEM_Ep
1b27d 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 hem;. }else
1b27e 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e {. pMem->
1b27f 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 flags = MEM_Blob
1b280 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 | MEM_Ephem;.
1b281 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 }. retu
1b282 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 rn len;. }.
1b283 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
1b284 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 ../*.** Given th
1b285 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f e nKey-byte enco
1b286 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 ding of a record
1b287 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 in pKey[], pars
1b288 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 e the.** record
1b289 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 into a UnpackedR
1b28a 65 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e ecord structure.
1b28b 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
1b28c 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 er to.** that st
1b28d 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 ructure..**.** T
1b28e 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 he calling funct
1b28f 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 ion might provid
1b290 65 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 e szSpace bytes
1b291 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 of memory.** spa
1b292 63 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 ce at pSpace. T
1b293 68 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 his space can be
1b294 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 used to hold th
1b295 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 e returned.** VD
1b296 62 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 beParsedRecord s
1b297 74 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69 tructure if it i
1b298 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 s large enough.
1b299 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 If it is.** not
1b29a 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 big enough, spa
1b29b 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 ce is obtained f
1b29c 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c rom sqlite3_mall
1b29d 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 oc()..**.** The
1b29e 72 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 returned structu
1b29f 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f re should be clo
1b2a0 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f sed by a call to
1b2a1 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 .** sqlite3VdbeD
1b2a2 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 eleteUnpackedRec
1b2a3 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 ord()..*/ .SQLIT
1b2a4 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b E_PRIVATE Unpack
1b2a5 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 edRecord *sqlite
1b2a6 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 3VdbeRecordUnpac
1b2a7 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b k(. KeyInfo *pK
1b2a8 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 eyInfo, /* I
1b2a9 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1b2aa 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d the record form
1b2ab 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 at */. int nKey
1b2ac 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1b2ad 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 * Size of the bi
1b2ae 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 nary record */.
1b2af 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 const void *pKe
1b2b0 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 y, /* The b
1b2b1 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a inary record */.
1b2b2 20 20 63 68 61 72 20 2a 70 53 70 61 63 65 2c 20 char *pSpace,
1b2b3 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c /* Unal
1b2b4 69 67 6e 65 64 20 73 70 61 63 65 20 61 76 61 69 igned space avai
1b2b5 6c 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 lable to hold th
1b2b6 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e e object */. in
1b2b7 74 20 73 7a 53 70 61 63 65 20 20 20 20 20 20 20 t szSpace
1b2b8 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1b2b9 70 53 70 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 pSpace[] in byte
1b2ba 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 s */.){. const
1b2bb 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 unsigned char *a
1b2bc 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 Key = (const uns
1b2bd 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 igned char *)pKe
1b2be 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 y;. UnpackedRec
1b2bf 6f 72 64 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 ord *p; /* The
1b2c0 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 unpacked record
1b2c1 74 68 61 74 20 77 65 20 77 69 6c 6c 20 72 65 74 that we will ret
1b2c2 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 urn */. int nBy
1b2c3 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 te; /*
1b2c4 4d 65 6d 6f 72 79 20 73 70 61 63 65 20 6e 65 65 Memory space nee
1b2c5 64 65 64 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 ded to hold p, i
1b2c6 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 n bytes */. int
1b2c7 20 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 d;. u32 idx;.
1b2c8 20 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 u16 u;
1b2c9 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 /* Unsigned
1b2ca 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f loop counter */
1b2cb 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 . u32 szHdr;.
1b2cc 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 Mem *pMem;. int
1b2cd 20 6e 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 nOff;
1b2ce 20 2f 2a 20 49 6e 63 72 65 61 73 65 20 70 53 70 /* Increase pSp
1b2cf 61 63 65 20 62 79 20 74 68 69 73 20 6d 75 63 68 ace by this much
1b2d0 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e to 8-byte align
1b2d1 20 69 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 it */. . /*.
1b2d2 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 73 ** We want to s
1b2d3 68 69 66 74 20 74 68 65 20 70 6f 69 6e 74 65 72 hift the pointer
1b2d4 20 70 53 70 61 63 65 20 75 70 20 73 75 63 68 20 pSpace up such
1b2d5 74 68 61 74 20 69 74 20 69 73 20 38 2d 62 79 74 that it is 8-byt
1b2d6 65 20 61 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 e aligned.. **
1b2d7 54 68 75 73 2c 20 77 65 20 6e 65 65 64 20 74 6f Thus, we need to
1b2d8 20 63 61 6c 63 75 6c 61 74 65 20 61 20 76 61 6c calculate a val
1b2d9 75 65 2c 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 ue, nOff, betwee
1b2da 6e 20 30 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 n 0 and 7, to sh
1b2db 69 66 74 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e ift . ** it by.
1b2dc 20 20 49 66 20 70 53 70 61 63 65 20 69 73 20 61 If pSpace is a
1b2dd 6c 72 65 61 64 79 20 38 2d 62 79 74 65 20 61 6c lready 8-byte al
1b2de 69 67 6e 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 igned, nOff shou
1b2df 6c 64 20 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f ld be zero.. */
1b2e0 0a 20 20 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 . nOff = (8 - (
1b2e1 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e SQLITE_PTR_TO_IN
1b2e2 54 28 70 53 70 61 63 65 29 20 26 20 37 29 29 20 T(pSpace) & 7))
1b2e3 26 20 37 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d & 7;. pSpace +=
1b2e4 20 6e 4f 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 nOff;. szSpace
1b2e5 20 2d 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 -= nOff;. nByt
1b2e6 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f e = ROUND8(sizeo
1b2e7 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 f(UnpackedRecord
1b2e8 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 )) + sizeof(Mem)
1b2e9 2a 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 *(pKeyInfo->nFie
1b2ea 6c 64 2b 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 ld+1);. if( nBy
1b2eb 74 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 te>szSpace ){.
1b2ec 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d p = sqlite3DbM
1b2ed 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 allocRaw(pKeyInf
1b2ee 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 o->db, nByte);.
1b2ef 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 if( p==0 ) re
1b2f0 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 turn 0;. p->f
1b2f1 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f lags = UNPACKED_
1b2f2 4e 45 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 NEED_FREE | UNPA
1b2f3 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f CKED_NEED_DESTRO
1b2f4 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 Y;. }else{.
1b2f5 70 20 3d 20 28 55 6e 70 61 63 6b 65 64 52 65 63 p = (UnpackedRec
1b2f6 6f 72 64 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 ord*)pSpace;.
1b2f7 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 p->flags = UNPA
1b2f8 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f CKED_NEED_DESTRO
1b2f9 59 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 Y;. }. p->pKey
1b2fa 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b Info = pKeyInfo;
1b2fb 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 . p->nField = p
1b2fc 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 KeyInfo->nField
1b2fd 2b 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d + 1;. p->aMem =
1b2fe 20 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 pMem = (Mem*)&(
1b2ff 28 63 68 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 (char*)p)[ROUND8
1b300 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 (sizeof(Unpacked
1b301 52 65 63 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 Record))];. ass
1b302 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f ert( EIGHT_BYTE_
1b303 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 ALIGNMENT(pMem)
1b304 29 3b 0a 20 20 69 64 78 20 3d 20 67 65 74 56 61 );. idx = getVa
1b305 72 69 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 rint32(aKey, szH
1b306 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 dr);. d = szHdr
1b307 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 ;. u = 0;. whi
1b308 6c 65 28 20 69 64 78 3c 73 7a 48 64 72 20 26 26 le( idx<szHdr &&
1b309 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 u<p->nField &&
1b30a 64 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 d<=nKey ){. u
1b30b 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 32 serial_type;.
1b30c 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 . idx += getV
1b30d 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 arint32(&aKey[id
1b30e 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 x], serial_type)
1b30f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 ;. pMem->enc
1b310 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b = pKeyInfo->enc;
1b311 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 . pMem->db =
1b312 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 pKeyInfo->db;.
1b313 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 pMem->flags =
1b314 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 0;. pMem->zMa
1b315 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 lloc = 0;. d
1b316 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 += sqlite3VdbeSe
1b317 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d rialGet(&aKey[d]
1b318 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 , serial_type, p
1b319 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b Mem);. pMem++
1b31a 3b 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 ;. u++;. }.
1b31b 20 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 assert( u<=pKey
1b31c 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 Info->nField + 1
1b31d 20 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 );. p->nField
1b31e 3d 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 = u;. return (v
1b31f 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a oid*)p;.}../*.**
1b320 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 This routine de
1b321 73 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 stroys a Unpacke
1b322 64 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a dRecord object..
1b323 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b324 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 E void sqlite3Vd
1b325 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 beDeleteUnpacked
1b326 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 Record(UnpackedR
1b327 65 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 ecord *p){. int
1b328 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b i;. Mem *pMem;
1b329 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 .. assert( p!=0
1b32a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d );. assert( p-
1b32b 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 >flags & UNPACKE
1b32c 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 D_NEED_DESTROY )
1b32d 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 ;. for(i=0, pMe
1b32e 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e m=p->aMem; i<p->
1b32f 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 nField; i++, pMe
1b330 6d 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 m++){. /* The
1b331 20 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 unpacked record
1b332 20 69 73 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 is always const
1b333 72 75 63 74 65 64 20 62 79 20 74 68 65 0a 20 20 ructed by the.
1b334 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 ** sqlite3Vdbe
1b335 55 6e 70 61 63 6b 52 65 63 6f 72 64 28 29 20 66 UnpackRecord() f
1b336 75 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 unction above, w
1b337 68 69 63 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 hich makes all.
1b338 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e ** strings an
1b339 64 20 62 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 d blobs static.
1b33a 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 And none of the
1b33b 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 elements are.
1b33c 20 20 2a 2a 20 65 76 65 72 20 74 72 61 6e 73 66 ** ever transf
1b33d 6f 72 6d 65 64 2c 20 73 6f 20 74 68 65 72 65 20 ormed, so there
1b33e 69 73 20 6e 65 76 65 72 20 61 6e 79 74 68 69 6e is never anythin
1b33f 67 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 g to delete..
1b340 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 */. if( NEVE
1b341 52 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 R(pMem->zMalloc)
1b342 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 ) sqlite3VdbeMe
1b343 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a mRelease(pMem);.
1b344 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 }. if( p->fla
1b345 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 gs & UNPACKED_NE
1b346 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 73 ED_FREE ){. s
1b347 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e qlite3DbFree(p->
1b348 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 pKeyInfo->db, p)
1b349 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 ;. }.}../*.** T
1b34a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d his function com
1b34b 70 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 pares the two ta
1b34c 62 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 ble rows or inde
1b34d 78 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 x records.** spe
1b34e 63 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 cified by {nKey1
1b34f 2c 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b , pKey1} and pPK
1b350 65 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 ey2. It returns
1b351 20 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 a negative, zer
1b352 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 o.** or positive
1b353 20 69 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 integer if key1
1b354 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 is less than, e
1b355 71 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 qual to or .** g
1b356 72 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 reater than key2
1b357 2e 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 . The {nKey1, p
1b358 4b 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 Key1} key must b
1b359 65 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 e a blob.** crea
1b35a 74 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b ted by th OP_Mak
1b35b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f eRecord opcode o
1b35c 66 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 f the VDBE. The
1b35d 20 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d pPKey2.** key m
1b35e 75 73 74 20 62 65 20 61 20 70 61 72 73 65 64 20 ust be a parsed
1b35f 6b 65 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 key such as obta
1b360 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c ined from.** sql
1b361 69 74 65 33 56 64 62 65 50 61 72 73 65 52 65 63 ite3VdbeParseRec
1b362 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 ord..**.** Key1
1b363 61 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 and Key2 do not
1b364 68 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 have to contain
1b365 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 the same number
1b366 6f 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 of fields..** Th
1b367 65 20 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 e key with fewer
1b368 20 66 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c fields is usual
1b369 6c 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 ly compares less
1b36a 20 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f than the .** lo
1b36b 6e 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 nger key. Howev
1b36c 65 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b er if the UNPACK
1b36d 45 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 ED_INCRKEY flags
1b36e 20 69 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 in pPKey2 is se
1b36f 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d t.** and the com
1b370 6d 6f 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 mon prefixes are
1b371 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 equal, then key
1b372 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 1 is less than k
1b373 65 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 ey2..** Or if th
1b374 65 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 e UNPACKED_MATCH
1b375 5f 50 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 _PREFIX flag is
1b376 73 65 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 set and the pref
1b377 69 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 ixes are.** equa
1b378 6c 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 l, then the keys
1b379 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 are considered
1b37a 74 6f 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a to be equal and.
1b37b 2a 2a 20 74 68 65 20 70 61 72 74 73 20 62 65 79 ** the parts bey
1b37c 6f 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 ond the common p
1b37d 72 65 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 refix are ignore
1b37e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 d..**.** If the
1b37f 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f UNPACKED_IGNORE_
1b380 52 4f 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 ROWID flag is se
1b381 74 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 t, then the last
1b382 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 byte of.** the
1b383 68 65 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 header of pKey1
1b384 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 is ignored. It
1b385 69 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 is assumed that
1b386 70 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 pKey1 is.** an i
1b387 6e 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 ndex key, and th
1b388 75 73 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 us ends with a r
1b389 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 owid value. The
1b38a 20 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 last byte.** of
1b38b 20 74 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c the header will
1b38c 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 therefore be th
1b38d 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 e serial type of
1b38e 20 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f the rowid:.** o
1b38f 6e 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 ne of 1, 2, 3, 4
1b390 2c 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 , 5, 6, 8, or 9
1b391 2d 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 - the integer se
1b392 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 rial types..** T
1b393 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f he serial type o
1b394 66 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 f the final rowi
1b395 64 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 d will always be
1b396 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a a single byte..
1b397 2a 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 ** By ignoring t
1b398 68 69 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 his last byte of
1b399 20 74 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 the header, we
1b39a 66 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 force the compar
1b39b 69 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 ison.** to ignor
1b39c 65 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 e the rowid at t
1b39d 68 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a he end of key1..
1b39e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
1b39f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 E int sqlite3Vdb
1b3a0 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a eRecordCompare(.
1b3a1 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e int nKey1, con
1b3a2 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 st void *pKey1,
1b3a3 2f 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 /* Left key */.
1b3a4 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1b3a5 2a 70 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f *pPKey2 /
1b3a6 2a 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 * Right key */.)
1b3a7 7b 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 {. int d1;
1b3a8 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 /* Offset
1b3a9 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 into aKey[] of
1b3aa 6e 65 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e next data elemen
1b3ab 74 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b t */. u32 idx1;
1b3ac 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 /* Off
1b3ad 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 set into aKey[]
1b3ae 6f 66 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 of next header e
1b3af 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 lement */. u32
1b3b0 73 7a 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f szHdr1; /
1b3b1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 * Number of byte
1b3b2 73 20 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 s in header */.
1b3b3 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e int i = 0;. in
1b3b4 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 t nField;. int
1b3b5 72 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 rc = 0;. const
1b3b6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 unsigned char *a
1b3b7 4b 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e Key1 = (const un
1b3b8 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b signed char *)pK
1b3b9 65 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a ey1;. KeyInfo *
1b3ba 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 pKeyInfo;. Mem
1b3bb 6d 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 mem1;.. pKeyInf
1b3bc 6f 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 o = pPKey2->pKey
1b3bd 49 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 Info;. mem1.enc
1b3be 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 = pKeyInfo->enc
1b3bf 3b 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b ;. mem1.db = pK
1b3c0 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 2f 2a eyInfo->db;. /*
1b3c1 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b mem1.flags = 0;
1b3c2 20 20 2f 2f 20 57 69 6c 6c 20 62 65 20 69 6e 69 // Will be ini
1b3c3 74 69 61 6c 69 7a 65 64 20 62 79 20 73 71 6c 69 tialized by sqli
1b3c4 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 te3VdbeSerialGet
1b3c5 28 29 20 2a 2f 0a 20 20 56 56 41 5f 4f 4e 4c 59 () */. VVA_ONLY
1b3c6 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20 3d ( mem1.zMalloc =
1b3c7 20 30 3b 20 29 20 2f 2a 20 4f 6e 6c 79 20 6e 65 0; ) /* Only ne
1b3c8 65 64 65 64 20 62 79 20 61 73 73 65 72 74 28 29 eded by assert()
1b3c9 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 0a statements */..
1b3ca 20 20 2f 2a 20 43 6f 6d 70 69 6c 65 72 73 20 6d /* Compilers m
1b3cb 61 79 20 63 6f 6d 70 6c 61 69 6e 20 74 68 61 74 ay complain that
1b3cc 20 6d 65 6d 31 2e 75 2e 69 20 69 73 20 70 6f 74 mem1.u.i is pot
1b3cd 65 6e 74 69 61 6c 6c 79 20 75 6e 69 6e 69 74 69 entially uniniti
1b3ce 61 6c 69 7a 65 64 2e 0a 20 20 2a 2a 20 57 65 20 alized.. ** We
1b3cf 63 6f 75 6c 64 20 69 6e 69 74 69 61 6c 69 7a 65 could initialize
1b3d0 20 69 74 2c 20 61 73 20 73 68 6f 77 6e 20 68 65 it, as shown he
1b3d1 72 65 2c 20 74 6f 20 73 69 6c 65 6e 63 65 20 74 re, to silence t
1b3d2 68 6f 73 65 20 63 6f 6d 70 6c 61 69 6e 74 73 2e hose complaints.
1b3d3 0a 20 20 2a 2a 20 42 75 74 20 69 6e 20 66 61 63 . ** But in fac
1b3d4 74 2c 20 6d 65 6d 31 2e 75 2e 69 20 77 69 6c 6c t, mem1.u.i will
1b3d5 20 6e 65 76 65 72 20 61 63 74 75 61 6c 6c 79 20 never actually
1b3d6 62 65 20 75 73 65 64 20 69 6e 69 74 69 61 6c 69 be used initiali
1b3d7 7a 65 64 2c 20 61 6e 64 20 64 6f 69 6e 67 20 0a zed, and doing .
1b3d8 20 20 2a 2a 20 74 68 65 20 75 6e 6e 65 63 65 73 ** the unneces
1b3d9 73 61 72 79 20 69 6e 69 74 69 61 6c 69 7a 61 74 sary initializat
1b3da 69 6f 6e 20 68 61 73 20 61 20 6d 65 61 73 75 72 ion has a measur
1b3db 61 62 6c 65 20 6e 65 67 61 74 69 76 65 20 70 65 able negative pe
1b3dc 72 66 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 69 rformance. ** i
1b3dd 6d 70 61 63 74 2c 20 73 69 6e 63 65 20 74 68 69 mpact, since thi
1b3de 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 76 s routine is a v
1b3df 65 72 79 20 68 69 67 68 20 72 75 6e 6e 65 72 2e ery high runner.
1b3e0 20 20 41 6e 64 20 73 6f 2c 20 77 65 20 63 68 6f And so, we cho
1b3e1 6f 73 65 0a 20 20 2a 2a 20 74 6f 20 69 67 6e 6f ose. ** to igno
1b3e2 72 65 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 re the compiler
1b3e3 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 6c 65 61 warnings and lea
1b3e4 76 65 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 ve this variable
1b3e5 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a uninitialized..
1b3e6 20 20 2a 2f 0a 20 20 2f 2a 20 20 6d 65 6d 31 2e */. /* mem1.
1b3e7 75 2e 69 20 3d 20 30 3b 20 20 2f 2f 20 6e 6f 74 u.i = 0; // not
1b3e8 20 6e 65 65 64 65 64 2c 20 68 65 72 65 20 74 6f needed, here to
1b3e9 20 73 69 6c 65 6e 63 65 20 63 6f 6d 70 69 6c 65 silence compile
1b3ea 72 20 77 61 72 6e 69 6e 67 20 2a 2f 0a 20 20 0a r warning */. .
1b3eb 20 20 69 64 78 31 20 3d 20 67 65 74 56 61 72 69 idx1 = getVari
1b3ec 6e 74 33 32 28 61 4b 65 79 31 2c 20 73 7a 48 64 nt32(aKey1, szHd
1b3ed 72 31 29 3b 0a 20 20 64 31 20 3d 20 73 7a 48 64 r1);. d1 = szHd
1b3ee 72 31 3b 0a 20 20 69 66 28 20 70 50 4b 65 79 32 r1;. if( pPKey2
1b3ef 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b ->flags & UNPACK
1b3f0 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 ED_IGNORE_ROWID
1b3f1 29 7b 0a 20 20 20 20 73 7a 48 64 72 31 2d 2d 3b ){. szHdr1--;
1b3f2 0a 20 20 7d 0a 20 20 6e 46 69 65 6c 64 20 3d 20 . }. nField =
1b3f3 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1b3f4 3b 0a 20 20 77 68 69 6c 65 28 20 69 64 78 31 3c ;. while( idx1<
1b3f5 73 7a 48 64 72 31 20 26 26 20 69 3c 70 50 4b 65 szHdr1 && i<pPKe
1b3f6 79 32 2d 3e 6e 46 69 65 6c 64 20 29 7b 0a 20 20 y2->nField ){.
1b3f7 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 u32 serial_typ
1b3f8 65 31 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 e1;.. /* Read
1b3f9 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 the serial type
1b3fa 73 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 65 s for the next e
1b3fb 6c 65 6d 65 6e 74 20 69 6e 20 65 61 63 68 20 6b lement in each k
1b3fc 65 79 2e 20 2a 2f 0a 20 20 20 20 69 64 78 31 20 ey. */. idx1
1b3fd 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 += getVarint32(
1b3fe 61 4b 65 79 31 2b 69 64 78 31 2c 20 73 65 72 69 aKey1+idx1, seri
1b3ff 61 6c 5f 74 79 70 65 31 20 29 3b 0a 20 20 20 20 al_type1 );.
1b400 69 66 28 20 64 31 3e 3d 6e 4b 65 79 31 20 26 26 if( d1>=nKey1 &&
1b401 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1b402 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61 6c alTypeLen(serial
1b403 5f 74 79 70 65 31 29 3e 30 20 29 20 62 72 65 61 _type1)>0 ) brea
1b404 6b 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 k;.. /* Extra
1b405 63 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f ct the values to
1b406 20 62 65 20 63 6f 6d 70 61 72 65 64 2e 0a 20 20 be compared..
1b407 20 20 2a 2f 0a 20 20 20 20 64 31 20 2b 3d 20 73 */. d1 += s
1b408 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c qlite3VdbeSerial
1b409 47 65 74 28 26 61 4b 65 79 31 5b 64 31 5d 2c 20 Get(&aKey1[d1],
1b40a 73 65 72 69 61 6c 5f 74 79 70 65 31 2c 20 26 6d serial_type1, &m
1b40b 65 6d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f em1);.. /* Do
1b40c 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a the comparison.
1b40d 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 */. rc =
1b40e 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 sqlite3MemCompar
1b40f 65 28 26 6d 65 6d 31 2c 20 26 70 50 4b 65 79 32 e(&mem1, &pPKey2
1b410 2d 3e 61 4d 65 6d 5b 69 5d 2c 0a 20 20 20 20 20 ->aMem[i],.
1b411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b412 20 20 20 20 20 20 69 3c 6e 46 69 65 6c 64 20 3f i<nField ?
1b413 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c pKeyInfo->aColl
1b414 5b 69 5d 20 3a 20 30 29 3b 0a 20 20 20 20 69 66 [i] : 0);. if
1b415 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 ( rc!=0 ){.
1b416 20 61 73 73 65 72 74 28 20 6d 65 6d 31 2e 7a 4d assert( mem1.zM
1b417 61 6c 6c 6f 63 3d 3d 30 20 29 3b 20 20 2f 2a 20 alloc==0 ); /*
1b418 53 65 65 20 63 6f 6d 6d 65 6e 74 20 62 65 6c 6f See comment belo
1b419 77 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 w */.. /* I
1b41a 6e 76 65 72 74 20 74 68 65 20 72 65 73 75 6c 74 nvert the result
1b41b 20 69 66 20 77 65 20 61 72 65 20 75 73 69 6e 67 if we are using
1b41c 20 44 45 53 43 20 73 6f 72 74 20 6f 72 64 65 72 DESC sort order
1b41d 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 . */. if( p
1b41e 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 KeyInfo->aSortOr
1b41f 64 65 72 20 26 26 20 69 3c 6e 46 69 65 6c 64 20 der && i<nField
1b420 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f && pKeyInfo->aSo
1b421 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 rtOrder[i] ){.
1b422 20 20 20 20 20 20 72 63 20 3d 20 2d 72 63 3b 0a rc = -rc;.
1b423 20 20 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20 }. .
1b424 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52 45 /* If the PRE
1b425 46 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67 20 FIX_SEARCH flag
1b426 69 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20 66 is set and all f
1b427 69 65 6c 64 73 20 65 78 63 65 70 74 20 74 68 65 ields except the
1b428 20 66 69 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 final. **
1b429 72 6f 77 69 64 20 66 69 65 6c 64 20 77 65 72 65 rowid field were
1b42a 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65 equal, then cle
1b42b 61 72 20 74 68 65 20 50 52 45 46 49 58 5f 53 45 ar the PREFIX_SE
1b42c 41 52 43 48 20 66 6c 61 67 20 61 6e 64 20 73 65 ARCH flag and se
1b42d 74 20 0a 20 20 20 20 20 20 2a 2a 20 70 50 4b 65 t . ** pPKe
1b42e 79 32 2d 3e 72 6f 77 69 64 20 74 6f 20 74 68 65 y2->rowid to the
1b42f 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f value of the ro
1b430 77 69 64 20 66 69 65 6c 64 20 69 6e 20 28 70 4b wid field in (pK
1b431 65 79 31 2c 20 6e 4b 65 79 31 29 2e 0a 20 20 20 ey1, nKey1)..
1b432 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 75 73 ** This is us
1b433 65 64 20 62 79 20 74 68 65 20 4f 50 5f 49 73 55 ed by the OP_IsU
1b434 6e 69 71 75 65 20 6f 70 63 6f 64 65 2e 0a 20 20 nique opcode..
1b435 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 */. if(
1b436 20 28 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 (pPKey2->flags
1b437 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 & UNPACKED_PREFI
1b438 58 5f 53 45 41 52 43 48 29 20 26 26 20 69 3d 3d X_SEARCH) && i==
1b439 28 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d (pPKey2->nField-
1b43a 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 1) ){. as
1b43b 73 65 72 74 28 20 69 64 78 31 3d 3d 73 7a 48 64 sert( idx1==szHd
1b43c 72 31 20 26 26 20 72 63 20 29 3b 0a 20 20 20 20 r1 && rc );.
1b43d 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31 assert( mem1
1b43e 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 .flags & MEM_Int
1b43f 20 29 3b 0a 20 20 20 20 20 20 20 20 70 50 4b 65 );. pPKe
1b440 79 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55 4e y2->flags &= ~UN
1b441 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 PACKED_PREFIX_SE
1b442 41 52 43 48 3b 0a 20 20 20 20 20 20 20 20 70 50 ARCH;. pP
1b443 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65 Key2->rowid = me
1b444 6d 31 2e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 0a m1.u.i;. }.
1b445 20 20 20 20 0a 20 20 20 20 20 20 72 65 74 75 72 . retur
1b446 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 n rc;. }.
1b447 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e i++;. }.. /* N
1b448 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 o memory allocat
1b449 69 6f 6e 20 69 73 20 65 76 65 72 20 75 73 65 64 ion is ever used
1b44a 20 6f 6e 20 6d 65 6d 31 2e 20 20 50 72 6f 76 65 on mem1. Prove
1b44b 20 74 68 69 73 20 75 73 69 6e 67 0a 20 20 2a 2a this using. **
1b44c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 the following a
1b44d 73 73 65 72 74 28 29 2e 20 20 49 66 20 74 68 65 ssert(). If the
1b44e 20 61 73 73 65 72 74 28 29 20 66 61 69 6c 73 2c assert() fails,
1b44f 20 69 74 20 69 6e 64 69 63 61 74 65 73 20 61 0a it indicates a.
1b450 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 6c 65 61 6b ** memory leak
1b451 20 61 6e 64 20 61 20 6e 65 65 64 20 74 6f 20 63 and a need to c
1b452 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d all sqlite3VdbeM
1b453 65 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 emRelease(&mem1)
1b454 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
1b455 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30 mem1.zMalloc==0
1b456 20 29 3b 0a 0a 20 20 2f 2a 20 72 63 3d 3d 30 20 );.. /* rc==0
1b457 68 65 72 65 20 6d 65 61 6e 73 20 74 68 61 74 20 here means that
1b458 6f 6e 65 20 6f 66 20 74 68 65 20 6b 65 79 73 20 one of the keys
1b459 72 61 6e 20 6f 75 74 20 6f 66 20 66 69 65 6c 64 ran out of field
1b45a 73 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 20 74 s and. ** all t
1b45b 68 65 20 66 69 65 6c 64 73 20 75 70 20 74 6f 20 he fields up to
1b45c 74 68 61 74 20 70 6f 69 6e 74 20 77 65 72 65 20 that point were
1b45d 65 71 75 61 6c 2e 20 49 66 20 74 68 65 20 55 4e equal. If the UN
1b45e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 0a 20 PACKED_INCRKEY.
1b45f 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2c ** flag is set,
1b460 20 74 68 65 6e 20 62 72 65 61 6b 20 74 68 65 20 then break the
1b461 74 69 65 20 62 79 20 74 72 65 61 74 69 6e 67 20 tie by treating
1b462 6b 65 79 32 20 61 73 20 6c 61 72 67 65 72 2e 0a key2 as larger..
1b463 20 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43 ** If the UPAC
1b464 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 KED_PREFIX_MATCH
1b465 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 flag is set, th
1b466 65 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d en keys with com
1b467 6d 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 20 2a mon prefixes. *
1b468 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 * are considered
1b469 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 4f to be equal. O
1b46a 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 6c 6f therwise, the lo
1b46b 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68 65 20 nger key is the
1b46c 0a 20 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 . ** larger. A
1b46d 73 20 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 s it happens, th
1b46e 65 20 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c e pPKey2 will al
1b46f 77 61 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 ways be the long
1b470 65 72 0a 20 20 2a 2a 20 69 66 20 74 68 65 72 65 er. ** if there
1b471 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 is a difference
1b472 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 .. */. assert(
1b473 20 72 63 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 rc==0 );. if(
1b474 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 pPKey2->flags &
1b475 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 UNPACKED_INCRKEY
1b476 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 2d 31 3b ){. rc = -1;
1b477 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 4b . }else if( pPK
1b478 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 ey2->flags & UNP
1b479 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 ACKED_PREFIX_MAT
1b47a 43 48 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61 CH ){. /* Lea
1b47b 76 65 20 72 63 3d 3d 30 20 2a 2f 0a 20 20 7d 65 ve rc==0 */. }e
1b47c 6c 73 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 lse if( idx1<szH
1b47d 64 72 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 dr1 ){. rc =
1b47e 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 1;. }. return
1b47f 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 rc;.}. ../*.** p
1b480 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e Cur points at an
1b481 20 69 6e 64 65 78 20 65 6e 74 72 79 20 63 72 65 index entry cre
1b482 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 4f ated using the O
1b483 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 P_MakeRecord opc
1b484 6f 64 65 2e 0a 2a 2a 20 52 65 61 64 20 74 68 65 ode..** Read the
1b485 20 72 6f 77 69 64 20 28 74 68 65 20 6c 61 73 74 rowid (the last
1b486 20 66 69 65 6c 64 20 69 6e 20 74 68 65 20 72 65 field in the re
1b487 63 6f 72 64 29 20 61 6e 64 20 73 74 6f 72 65 20 cord) and store
1b488 69 74 20 69 6e 20 2a 72 6f 77 69 64 2e 0a 2a 2a it in *rowid..**
1b489 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f Return SQLITE_O
1b48a 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 K if everything
1b48b 77 6f 72 6b 73 2c 20 6f 72 20 61 6e 20 65 72 72 works, or an err
1b48c 6f 72 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 or code otherwis
1b48d 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75 72 20 6d 69 e..**.** pCur mi
1b48e 67 68 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 ght be pointing
1b48f 74 6f 20 74 65 78 74 20 6f 62 74 61 69 6e 65 64 to text obtained
1b490 20 66 72 6f 6d 20 61 20 63 6f 72 72 75 70 74 20 from a corrupt
1b491 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1b492 2a 20 53 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 * So the content
1b493 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74 cannot be trust
1b494 65 64 2e 20 20 44 6f 20 61 70 70 72 6f 70 72 69 ed. Do appropri
1b495 61 74 65 20 63 68 65 63 6b 73 20 6f 6e 20 74 68 ate checks on th
1b496 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 e content..*/.SQ
1b497 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
1b498 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 sqlite3VdbeIdxR
1b499 6f 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64 62 owid(sqlite3 *db
1b49a 2c 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 , BtCursor *pCur
1b49b 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a 20 , i64 *rowid){.
1b49c 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 i64 nCellKey =
1b49d 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 0;. int rc;. u
1b49e 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20 32 szHdr;
1b49f 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
1b4a0 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 header */. u32
1b4a1 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a typeRowid; /*
1b4a2 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 Serial type of
1b4a3 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 75 the rowid */. u
1b4a4 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20 32 lenRowid;
1b4a5 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 /* Size of the
1b4a6 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d rowid */. Mem m
1b4a7 2c 20 76 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 , v;.. UNUSED_P
1b4a8 41 52 41 4d 45 54 45 52 28 64 62 29 3b 0a 0a 20 ARAMETER(db);..
1b4a9 20 2f 2a 20 47 65 74 20 74 68 65 20 73 69 7a 65 /* Get the size
1b4aa 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e of the index en
1b4ab 74 72 79 2e 20 20 4f 6e 6c 79 20 69 6e 64 69 63 try. Only indic
1b4ac 65 73 20 65 6e 74 72 69 65 73 20 6f 66 20 6c 65 es entries of le
1b4ad 73 73 0a 20 20 2a 2a 20 74 68 61 6e 20 32 47 69 ss. ** than 2Gi
1b4ae 42 20 61 72 65 20 73 75 70 70 6f 72 74 20 2d 20 B are support -
1b4af 61 6e 79 74 68 69 6e 67 20 6c 61 72 67 65 20 6d anything large m
1b4b0 75 73 74 20 62 65 20 64 61 74 61 62 61 73 65 20 ust be database
1b4b1 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 2a 2a corruption.. **
1b4b2 20 41 6e 79 20 63 6f 72 72 75 70 74 69 6f 6e 20 Any corruption
1b4b3 69 73 20 64 65 74 65 63 74 65 64 20 69 6e 20 73 is detected in s
1b4b4 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 qlite3BtreeParse
1b4b5 43 65 6c 6c 50 74 72 28 29 2c 20 74 68 6f 75 67 CellPtr(), thoug
1b4b6 68 2c 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 h, so. ** this
1b4b7 63 6f 64 65 20 63 61 6e 20 73 61 66 65 6c 79 20 code can safely
1b4b8 61 73 73 75 6d 65 20 74 68 61 74 20 6e 43 65 6c assume that nCel
1b4b9 6c 4b 65 79 20 69 73 20 33 32 2d 62 69 74 73 20 lKey is 32-bits
1b4ba 20 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 . */. assert(
1b4bb 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1b4bc 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 sorIsValid(pCur)
1b4bd 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 );. rc = sqlit
1b4be 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 e3BtreeKeySize(p
1b4bf 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b Cur, &nCellKey);
1b4c0 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
1b4c1 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 20 QLITE_OK );
1b4c2 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61 79 /* pCur is alway
1b4c3 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69 s valid so KeySi
1b4c4 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a ze cannot fail *
1b4c5 2f 0a 20 20 61 73 73 65 72 74 28 20 28 6e 43 65 /. assert( (nCe
1b4c6 6c 6c 4b 65 79 20 26 20 53 51 4c 49 54 45 5f 4d llKey & SQLITE_M
1b4c7 41 58 5f 55 33 32 29 3d 3d 28 75 36 34 29 6e 43 AX_U32)==(u64)nC
1b4c8 65 6c 6c 4b 65 79 20 29 3b 0a 0a 20 20 2f 2a 20 ellKey );.. /*
1b4c9 52 65 61 64 20 69 6e 20 74 68 65 20 63 6f 6d 70 Read in the comp
1b4ca 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 lete content of
1b4cb 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 the index entry
1b4cc 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 */. memset(&m,
1b4cd 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 0, sizeof(m));.
1b4ce 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1b4cf 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 eMemFromBtree(pC
1b4d0 75 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c ur, 0, (int)nCel
1b4d1 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 lKey, 1, &m);.
1b4d2 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 if( rc ){. re
1b4d3 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 turn rc;. }..
1b4d4 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 65 6e 74 /* The index ent
1b4d5 72 79 20 6d 75 73 74 20 62 65 67 69 6e 20 77 69 ry must begin wi
1b4d6 74 68 20 61 20 68 65 61 64 65 72 20 73 69 7a 65 th a header size
1b4d7 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 */. (void)getV
1b4d8 61 72 69 6e 74 33 32 28 28 75 38 2a 29 6d 2e 7a arint32((u8*)m.z
1b4d9 2c 20 73 7a 48 64 72 29 3b 0a 20 20 74 65 73 74 , szHdr);. test
1b4da 63 61 73 65 28 20 73 7a 48 64 72 3d 3d 33 20 29 case( szHdr==3 )
1b4db 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 73 7a ;. testcase( sz
1b4dc 48 64 72 3d 3d 6d 2e 6e 20 29 3b 0a 20 20 69 66 Hdr==m.n );. if
1b4dd 28 20 75 6e 6c 69 6b 65 6c 79 28 73 7a 48 64 72 ( unlikely(szHdr
1b4de 3c 33 20 7c 7c 20 28 69 6e 74 29 73 7a 48 64 72 <3 || (int)szHdr
1b4df 3e 6d 2e 6e 29 20 29 7b 0a 20 20 20 20 67 6f 74 >m.n) ){. got
1b4e0 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 o idx_rowid_corr
1b4e1 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f uption;. }.. /
1b4e2 2a 20 54 68 65 20 6c 61 73 74 20 66 69 65 6c 64 * The last field
1b4e3 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 73 68 of the index sh
1b4e4 6f 75 6c 64 20 62 65 20 61 6e 20 69 6e 74 65 67 ould be an integ
1b4e5 65 72 20 2d 20 74 68 65 20 52 4f 57 49 44 2e 0a er - the ROWID..
1b4e6 20 20 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 ** Verify that
1b4e7 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 the last entry
1b4e8 72 65 61 6c 6c 79 20 69 73 20 61 6e 20 69 6e 74 really is an int
1b4e9 65 67 65 72 2e 20 2a 2f 0a 20 20 28 76 6f 69 64 eger. */. (void
1b4ea 29 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38 )getVarint32((u8
1b4eb 2a 29 26 6d 2e 7a 5b 73 7a 48 64 72 2d 31 5d 2c *)&m.z[szHdr-1],
1b4ec 20 74 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 typeRowid);. t
1b4ed 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b4ee 69 64 3d 3d 31 20 29 3b 0a 20 20 74 65 73 74 63 id==1 );. testc
1b4ef 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b4f0 32 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 2 );. testcase(
1b4f1 20 74 79 70 65 52 6f 77 69 64 3d 3d 33 20 29 3b typeRowid==3 );
1b4f2 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b4f3 65 52 6f 77 69 64 3d 3d 34 20 29 3b 0a 20 20 74 eRowid==4 );. t
1b4f4 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 estcase( typeRow
1b4f5 69 64 3d 3d 35 20 29 3b 0a 20 20 74 65 73 74 63 id==5 );. testc
1b4f6 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d ase( typeRowid==
1b4f7 36 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 6 );. testcase(
1b4f8 20 74 79 70 65 52 6f 77 69 64 3d 3d 38 20 29 3b typeRowid==8 );
1b4f9 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 . testcase( typ
1b4fa 65 52 6f 77 69 64 3d 3d 39 20 29 3b 0a 20 20 69 eRowid==9 );. i
1b4fb 66 28 20 75 6e 6c 69 6b 65 6c 79 28 74 79 70 65 f( unlikely(type
1b4fc 52 6f 77 69 64 3c 31 20 7c 7c 20 74 79 70 65 52 Rowid<1 || typeR
1b4fd 6f 77 69 64 3e 39 20 7c 7c 20 74 79 70 65 52 6f owid>9 || typeRo
1b4fe 77 69 64 3d 3d 37 29 20 29 7b 0a 20 20 20 20 67 wid==7) ){. g
1b4ff 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f oto idx_rowid_co
1b500 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 rruption;. }.
1b501 6c 65 6e 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 lenRowid = sqlit
1b502 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 e3VdbeSerialType
1b503 4c 65 6e 28 74 79 70 65 52 6f 77 69 64 29 3b 0a Len(typeRowid);.
1b504 20 20 74 65 73 74 63 61 73 65 28 20 28 75 33 32 testcase( (u32
1b505 29 6d 2e 6e 3d 3d 73 7a 48 64 72 2b 6c 65 6e 52 )m.n==szHdr+lenR
1b506 6f 77 69 64 20 29 3b 0a 20 20 69 66 28 20 75 6e owid );. if( un
1b507 6c 69 6b 65 6c 79 28 28 75 33 32 29 6d 2e 6e 3c likely((u32)m.n<
1b508 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 29 20 szHdr+lenRowid)
1b509 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f ){. goto idx_
1b50a 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e rowid_corruption
1b50b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 65 74 63 ;. }.. /* Fetc
1b50c 68 20 74 68 65 20 69 6e 74 65 67 65 72 20 6f 66 h the integer of
1b50d 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 f the end of the
1b50e 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f index record */
1b50f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 . sqlite3VdbeSe
1b510 72 69 61 6c 47 65 74 28 28 75 38 2a 29 26 6d 2e rialGet((u8*)&m.
1b511 7a 5b 6d 2e 6e 2d 6c 65 6e 52 6f 77 69 64 5d 2c z[m.n-lenRowid],
1b512 20 74 79 70 65 52 6f 77 69 64 2c 20 26 76 29 3b typeRowid, &v);
1b513 0a 20 20 2a 72 6f 77 69 64 20 3d 20 76 2e 75 2e . *rowid = v.u.
1b514 69 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 i;. sqlite3Vdbe
1b515 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a MemRelease(&m);.
1b516 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1b517 4f 4b 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 OK;.. /* Jump h
1b518 65 72 65 20 69 66 20 64 61 74 61 62 61 73 65 20 ere if database
1b519 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 corruption is de
1b51a 74 65 63 74 65 64 20 61 66 74 65 72 20 6d 20 68 tected after m h
1b51b 61 73 20 62 65 65 6e 0a 20 20 2a 2a 20 61 6c 6c as been. ** all
1b51c 6f 63 61 74 65 64 2e 20 20 46 72 65 65 20 74 68 ocated. Free th
1b51d 65 20 6d 20 6f 62 6a 65 63 74 20 61 6e 64 20 72 e m object and r
1b51e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 eturn SQLITE_COR
1b51f 52 55 50 54 2e 20 2a 2f 0a 69 64 78 5f 72 6f 77 RUPT. */.idx_row
1b520 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3a 0a 20 id_corruption:.
1b521 20 74 65 73 74 63 61 73 65 28 20 6d 2e 7a 4d 61 testcase( m.zMa
1b522 6c 6c 6f 63 21 3d 30 20 29 3b 0a 20 20 73 71 6c lloc!=0 );. sql
1b523 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 ite3VdbeMemRelea
1b524 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e se(&m);. return
1b525 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1b526 42 4b 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 BKPT;.}../*.** C
1b527 6f 6d 70 61 72 65 20 74 68 65 20 6b 65 79 20 6f ompare the key o
1b528 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 f the index entr
1b529 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70 43 y that cursor pC
1b52a 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 is pointing to
1b52b 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 6b against.** the k
1b52c 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70 55 6e ey string in pUn
1b52d 70 61 63 6b 65 64 2e 20 20 57 72 69 74 65 20 69 packed. Write i
1b52e 6e 74 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 nto *pRes a numb
1b52f 65 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 er.** that is ne
1b530 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 gative, zero, or
1b531 20 70 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 positive if pC
1b532 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 is less than, eq
1b533 75 61 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 ual to,.** or gr
1b534 65 61 74 65 72 20 74 68 61 6e 20 70 55 6e 70 61 eater than pUnpa
1b535 63 6b 65 64 2e 20 20 52 65 74 75 72 6e 20 53 51 cked. Return SQ
1b536 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 LITE_OK on succe
1b537 73 73 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 ss..**.** pUnpac
1b538 6b 65 64 20 69 73 20 65 69 74 68 65 72 20 63 72 ked is either cr
1b539 65 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 20 eated without a
1b53a 72 6f 77 69 64 20 6f 72 20 69 73 20 74 72 75 6e rowid or is trun
1b53b 63 61 74 65 64 20 73 6f 20 74 68 61 74 20 69 74 cated so that it
1b53c 0a 2a 2a 20 6f 6d 69 74 73 20 74 68 65 20 72 6f .** omits the ro
1b53d 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 2e 20 wid at the end.
1b53e 20 54 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 The rowid at th
1b53f 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 e end of the ind
1b540 65 78 20 65 6e 74 72 79 0a 2a 2a 20 69 73 20 69 ex entry.** is i
1b541 67 6e 6f 72 65 64 20 61 73 20 77 65 6c 6c 2e 20 gnored as well.
1b542 20 48 65 6e 63 65 2c 20 74 68 69 73 20 72 6f 75 Hence, this rou
1b543 74 69 6e 65 20 6f 6e 6c 79 20 63 6f 6d 70 61 72 tine only compar
1b544 65 73 20 74 68 65 20 70 72 65 66 69 78 65 73 20 es the prefixes
1b545 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79 73 20 .** of the keys
1b546 70 72 69 6f 72 20 74 6f 20 74 68 65 20 66 69 6e prior to the fin
1b547 61 6c 20 72 6f 77 69 64 2c 20 6e 6f 74 20 74 68 al rowid, not th
1b548 65 20 65 6e 74 69 72 65 20 6b 65 79 2e 0a 2a 2f e entire key..*/
1b549 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1b54a 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 49 int sqlite3VdbeI
1b54b 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 0a 20 20 dxKeyCompare(.
1b54c 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 2c 20 VdbeCursor *pC,
1b54d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1b54e 68 65 20 63 75 72 73 6f 72 20 74 6f 20 63 6f 6d he cursor to com
1b54f 70 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a pare against */.
1b550 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
1b551 20 2a 70 55 6e 70 61 63 6b 65 64 2c 20 20 2f 2a *pUnpacked, /*
1b552 20 55 6e 70 61 63 6b 65 64 20 76 65 72 73 69 6f Unpacked versio
1b553 6e 20 6f 66 20 6b 65 79 20 74 6f 20 63 6f 6d 70 n of key to comp
1b554 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 are against */.
1b555 20 69 6e 74 20 2a 72 65 73 20 20 20 20 20 20 20 int *res
1b556 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1b557 57 72 69 74 65 20 74 68 65 20 63 6f 6d 70 61 72 Write the compar
1b558 69 73 6f 6e 20 72 65 73 75 6c 74 20 68 65 72 65 ison result here
1b559 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20 6e 43 65 */.){. i64 nCe
1b55a 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 llKey = 0;. int
1b55b 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 rc;. BtCursor
1b55c 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70 43 75 72 *pCur = pC->pCur
1b55d 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b 0a 0a 20 sor;. Mem m;..
1b55e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1b55f 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c BtreeCursorIsVal
1b560 69 64 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 id(pCur) );. rc
1b561 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b = sqlite3BtreeK
1b562 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 eySize(pCur, &nC
1b563 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 ellKey);. asser
1b564 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b t( rc==SQLITE_OK
1b565 20 29 3b 20 20 20 20 2f 2a 20 70 43 75 72 20 69 ); /* pCur i
1b566 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73 s always valid s
1b567 6f 20 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 o KeySize cannot
1b568 20 66 61 69 6c 20 2a 2f 0a 20 20 2f 2a 20 6e 43 fail */. /* nC
1b569 65 6c 6c 4b 65 79 20 77 69 6c 6c 20 61 6c 77 61 ellKey will alwa
1b56a 79 73 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 ys be between 0
1b56b 61 6e 64 20 30 78 66 66 66 66 66 66 66 66 20 62 and 0xffffffff b
1b56c 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 73 61 ecause of the sa
1b56d 79 0a 20 20 2a 2a 20 74 68 61 74 20 62 74 72 65 y. ** that btre
1b56e 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 eParseCellPtr()
1b56f 61 6e 64 20 73 71 6c 69 74 65 33 47 65 74 56 61 and sqlite3GetVa
1b570 72 69 6e 74 33 32 28 29 20 61 72 65 20 69 6d 70 rint32() are imp
1b571 6c 65 6d 65 6e 74 65 64 20 2a 2f 0a 20 20 69 66 lemented */. if
1b572 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 7c 7c ( nCellKey<=0 ||
1b573 20 6e 43 65 6c 6c 4b 65 79 3e 30 78 37 66 66 66 nCellKey>0x7fff
1b574 66 66 66 66 20 29 7b 0a 20 20 20 20 2a 72 65 73 ffff ){. *res
1b575 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e = 0;. return
1b576 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b SQLITE_CORRUPT;
1b577 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d . }. memset(&m
1b578 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29 29 3b , 0, sizeof(m));
1b579 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
1b57a 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1b57b 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1b57c 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 (int)nCellKey, 1
1b57d 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 , &m);. if( rc
1b57e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 ){. return rc
1b57f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
1b580 70 55 6e 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 pUnpacked->flags
1b581 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f & UNPACKED_IGNO
1b582 52 45 5f 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 RE_ROWID );. *r
1b583 65 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 es = sqlite3Vdbe
1b584 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e RecordCompare(m.
1b585 6e 2c 20 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 n, m.z, pUnpacke
1b586 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 d);. sqlite3Vdb
1b587 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b eMemRelease(&m);
1b588 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1b589 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 _OK;.}../*.** Th
1b58a 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 is routine sets
1b58b 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 the value to be
1b58c 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62 73 returned by subs
1b58d 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a equent calls to.
1b58e 2a 2a 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 ** sqlite3_chang
1b58f 65 73 28 29 20 6f 6e 20 74 68 65 20 64 61 74 61 es() on the data
1b590 62 61 73 65 20 68 61 6e 64 6c 65 20 27 64 62 27 base handle 'db'
1b591 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 . .*/.SQLITE_PRI
1b592 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
1b593 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
1b594 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 sqlite3 *db, int
1b595 20 6e 43 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 nChange){. ass
1b596 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b597 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 ex_held(db->mute
1b598 78 29 20 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 x) );. db->nCha
1b599 6e 67 65 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 nge = nChange;.
1b59a 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 db->nTotalChang
1b59b 65 20 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a e += nChange;.}.
1b59c 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 ./*.** Set a fla
1b59d 67 20 69 6e 20 74 68 65 20 76 64 62 65 20 74 6f g in the vdbe to
1b59e 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e update the chan
1b59f 67 65 20 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 ge counter when
1b5a0 69 74 20 69 73 20 66 69 6e 61 6c 69 73 65 64 0a it is finalised.
1b5a1 2a 2a 20 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a ** or reset..*/.
1b5a2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
1b5a3 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 oid sqlite3VdbeC
1b5a4 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 ountChanges(Vdbe
1b5a5 20 2a 76 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 *v){. v->chang
1b5a6 65 43 6e 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f eCntOn = 1;.}../
1b5a7 2a 0a 2a 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 *.** Mark every
1b5a8 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 prepared stateme
1b5a9 6e 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 nt associated wi
1b5aa 74 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f th a database co
1b5ab 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 nnection.** as e
1b5ac 78 70 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e xpired..**.** An
1b5ad 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 expired stateme
1b5ae 6e 74 20 6d 65 61 6e 73 20 74 68 61 74 20 72 65 nt means that re
1b5af 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 compilation of t
1b5b0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 0a he statement is.
1b5b1 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 ** recommend. S
1b5b2 74 61 74 65 6d 65 6e 74 73 20 65 78 70 69 72 65 tatements expire
1b5b3 20 77 68 65 6e 20 74 68 69 6e 67 73 20 68 61 70 when things hap
1b5b4 70 65 6e 20 74 68 61 74 20 6d 61 6b 65 20 74 68 pen that make th
1b5b5 65 69 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 eir.** programs
1b5b6 6f 62 73 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 obsolete. Remov
1b5b7 69 6e 67 20 75 73 65 72 2d 64 65 66 69 6e 65 64 ing user-defined
1b5b8 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f functions or co
1b5b9 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 llating.** seque
1b5ba 6e 63 65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e nces, or changin
1b5bb 67 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 g an authorizati
1b5bc 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 on function are
1b5bd 74 68 65 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 the types of.**
1b5be 74 68 69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 things that make
1b5bf 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d prepared statem
1b5c0 65 6e 74 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a ents obsolete..*
1b5c1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1b5c2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
1b5c3 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65 irePreparedState
1b5c4 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 ments(sqlite3 *d
1b5c5 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 b){. Vdbe *p;.
1b5c6 20 66 6f 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 for(p = db->pVd
1b5c7 62 65 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 be; p; p=p->pNex
1b5c8 74 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 t){. p->expir
1b5c9 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f ed = 1;. }.}../
1b5ca 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 *.** Return the
1b5cb 64 61 74 61 62 61 73 65 20 61 73 73 6f 63 69 61 database associa
1b5cc 74 65 64 20 77 69 74 68 20 74 68 65 20 56 64 62 ted with the Vdb
1b5cd 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
1b5ce 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 VATE sqlite3 *sq
1b5cf 6c 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 lite3VdbeDb(Vdbe
1b5d0 20 2a 76 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 *v){. return v
1b5d1 2d 3e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 ->db;.}../*.** R
1b5d2 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 eturn a pointer
1b5d3 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 to an sqlite3_va
1b5d4 6c 75 65 20 73 74 72 75 63 74 75 72 65 20 63 6f lue structure co
1b5d5 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c ntaining the val
1b5d6 75 65 20 62 6f 75 6e 64 0a 2a 2a 20 70 61 72 61 ue bound.** para
1b5d7 6d 65 74 65 72 20 69 56 61 72 20 6f 66 20 56 4d meter iVar of VM
1b5d8 20 76 2e 20 45 78 63 65 70 74 2c 20 69 66 20 74 v. Except, if t
1b5d9 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 he value is an S
1b5da 51 4c 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 QL NULL, return
1b5db 0a 2a 2a 20 30 20 69 6e 73 74 65 61 64 2e 20 55 .** 0 instead. U
1b5dc 6e 6c 65 73 73 20 69 74 20 69 73 20 4e 55 4c 4c nless it is NULL
1b5dd 2c 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 , apply affinity
1b5de 20 61 66 66 20 28 6f 6e 65 20 6f 66 20 74 68 65 aff (one of the
1b5df 20 53 51 4c 49 54 45 5f 41 46 46 5f 2a 0a 2a 2a SQLITE_AFF_*.**
1b5e0 20 63 6f 6e 73 74 61 6e 74 73 29 20 74 6f 20 74 constants) to t
1b5e1 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 he value before
1b5e2 72 65 74 75 72 6e 69 6e 67 20 69 74 2e 0a 2a 2a returning it..**
1b5e3 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 .** The returned
1b5e4 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 66 value must be f
1b5e5 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c reed by the call
1b5e6 65 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 er using sqlite3
1b5e7 56 61 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2f 0a ValueFree()..*/.
1b5e8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 SQLITE_PRIVATE s
1b5e9 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 qlite3_value *sq
1b5ea 6c 69 74 65 33 56 64 62 65 47 65 74 56 61 6c 75 lite3VdbeGetValu
1b5eb 65 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 e(Vdbe *v, int i
1b5ec 56 61 72 2c 20 75 38 20 61 66 66 29 7b 0a 20 20 Var, u8 aff){.
1b5ed 61 73 73 65 72 74 28 20 69 56 61 72 3e 30 20 29 assert( iVar>0 )
1b5ee 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 ;. if( v ){.
1b5ef 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 76 2d Mem *pMem = &v-
1b5f0 3e 61 56 61 72 5b 69 56 61 72 2d 31 5d 3b 0a 20 >aVar[iVar-1];.
1b5f1 20 20 20 69 66 28 20 30 3d 3d 28 70 4d 65 6d 2d if( 0==(pMem-
1b5f2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1b5f3 6c 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 l) ){. sqli
1b5f4 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 74 20 te3_value *pRet
1b5f5 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 = sqlite3ValueNe
1b5f6 77 28 76 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 w(v->db);.
1b5f7 69 66 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20 if( pRet ){.
1b5f8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1b5f9 65 6d 43 6f 70 79 28 28 4d 65 6d 20 2a 29 70 52 emCopy((Mem *)pR
1b5fa 65 74 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 et, pMem);.
1b5fb 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 sqlite3ValueA
1b5fc 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 52 65 pplyAffinity(pRe
1b5fd 74 2c 20 61 66 66 2c 20 53 51 4c 49 54 45 5f 55 t, aff, SQLITE_U
1b5fe 54 46 38 29 3b 0a 20 20 20 20 20 20 20 20 73 71 TF8);. sq
1b5ff 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 lite3VdbeMemStor
1b600 65 54 79 70 65 28 28 4d 65 6d 20 2a 29 70 52 65 eType((Mem *)pRe
1b601 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
1b602 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 20 return pRet;.
1b603 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
1b604 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f n 0;.}../*.** Co
1b605 6e 66 69 67 75 72 65 20 53 51 4c 20 76 61 72 69 nfigure SQL vari
1b606 61 62 6c 65 20 69 56 61 72 20 73 6f 20 74 68 61 able iVar so tha
1b607 74 20 62 69 6e 64 69 6e 67 20 61 20 6e 65 77 20 t binding a new
1b608 76 61 6c 75 65 20 74 6f 20 69 74 20 73 69 67 6e value to it sign
1b609 61 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 als.** to sqlite
1b60a 33 5f 72 65 6f 70 74 69 6d 69 7a 65 28 29 20 74 3_reoptimize() t
1b60b 68 61 74 20 72 65 2d 70 72 65 70 61 72 69 6e 67 hat re-preparing
1b60c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d the statement m
1b60d 61 79 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 ay result.** in
1b60e 61 20 62 65 74 74 65 72 20 71 75 65 72 79 20 70 a better query p
1b60f 6c 61 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 lan..*/.SQLITE_P
1b610 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1b611 74 65 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 te3VdbeSetVarmas
1b612 6b 28 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 k(Vdbe *v, int i
1b613 56 61 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 Var){. assert(
1b614 69 56 61 72 3e 30 20 29 3b 0a 20 20 69 66 28 20 iVar>0 );. if(
1b615 69 56 61 72 3e 33 32 20 29 7b 0a 20 20 20 20 76 iVar>32 ){. v
1b616 2d 3e 65 78 70 6d 61 73 6b 20 3d 20 30 78 66 66 ->expmask = 0xff
1b617 66 66 66 66 66 66 3b 0a 20 20 7d 65 6c 73 65 7b ffffff;. }else{
1b618 0a 20 20 20 20 76 2d 3e 65 78 70 6d 61 73 6b 20 . v->expmask
1b619 7c 3d 20 28 28 75 33 32 29 31 20 3c 3c 20 28 69 |= ((u32)1 << (i
1b61a 56 61 72 2d 31 29 29 3b 0a 20 20 7d 0a 7d 0a 0a Var-1));. }.}..
1b61b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1b61c 45 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63 End of vdbeaux.c
1b61d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1b61e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b61f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1b620 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1b621 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 Begin file vdbea
1b622 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a pi.c ***********
1b623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b624 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1b625 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 /*.** 2004 May 2
1b626 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6.**.** The auth
1b627 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1b628 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1b629 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1b62a 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1b62b 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1b62c 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1b62d 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1b62e 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1b62f 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1b630 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1b631 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1b632 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1b633 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1b634 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1b635 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1b636 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1b637 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1b638 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b639 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b63a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b63b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b63c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
1b63d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 contains code u
1b63e 73 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 se to implement
1b63f 41 50 49 73 20 74 68 61 74 20 61 72 65 20 70 61 APIs that are pa
1b640 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 rt of the.** VDB
1b641 45 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 E..*/..#ifndef S
1b642 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 QLITE_OMIT_DEPRE
1b643 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75 CATED./*.** Retu
1b644 72 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 rn TRUE (non-zer
1b645 6f 29 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d o) of the statem
1b646 65 6e 74 20 73 75 70 70 6c 69 65 64 20 61 73 20 ent supplied as
1b647 61 6e 20 61 72 67 75 6d 65 6e 74 20 6e 65 65 64 an argument need
1b648 73 0a 2a 2a 20 74 6f 20 62 65 20 72 65 63 6f 6d s.** to be recom
1b649 70 69 6c 65 64 2e 20 20 41 20 73 74 61 74 65 6d piled. A statem
1b64a 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 ent needs to be
1b64b 72 65 63 6f 6d 70 69 6c 65 64 20 77 68 65 6e 65 recompiled whene
1b64c 76 65 72 20 74 68 65 0a 2a 2a 20 65 78 65 63 75 ver the.** execu
1b64d 74 69 6f 6e 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 tion environment
1b64e 20 63 68 61 6e 67 65 73 20 69 6e 20 61 20 77 61 changes in a wa
1b64f 79 20 74 68 61 74 20 77 6f 75 6c 64 20 61 6c 74 y that would alt
1b650 65 72 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a er the program.*
1b651 2a 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 70 * that sqlite3_p
1b652 72 65 70 61 72 65 28 29 20 67 65 6e 65 72 61 74 repare() generat
1b653 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 es. For example
1b654 2c 20 69 66 20 6e 65 77 20 66 75 6e 63 74 69 6f , if new functio
1b655 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c 6c 61 74 69 ns or.** collati
1b656 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61 72 65 ng sequences are
1b657 20 72 65 67 69 73 74 65 72 65 64 20 6f 72 20 69 registered or i
1b658 66 20 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 20 f an authorizer
1b659 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 function is.** a
1b65a 64 64 65 64 20 6f 72 20 63 68 61 6e 67 65 64 2e dded or changed.
1b65b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1b65c 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 nt sqlite3_expir
1b65d 65 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 ed(sqlite3_stmt
1b65e 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 *pStmt){. Vdbe
1b65f 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d *p = (Vdbe*)pStm
1b660 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 t;. return p==0
1b661 20 7c 7c 20 70 2d 3e 65 78 70 69 72 65 64 3b 0a || p->expired;.
1b662 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 }.#endif../*.**
1b663 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
1b664 75 74 69 6e 65 20 64 65 73 74 72 6f 79 73 20 61 utine destroys a
1b665 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1b666 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65 64 that is created
1b667 20 62 79 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 by.** the sqlit
1b668 65 33 5f 63 6f 6d 70 69 6c 65 28 29 20 72 6f 75 e3_compile() rou
1b669 74 69 6e 65 2e 20 54 68 65 20 69 6e 74 65 67 65 tine. The intege
1b66a 72 20 72 65 74 75 72 6e 65 64 20 69 73 20 61 6e r returned is an
1b66b 20 53 51 4c 49 54 45 5f 0a 2a 2a 20 73 75 63 63 SQLITE_.** succ
1b66c 65 73 73 2f 66 61 69 6c 75 72 65 20 63 6f 64 65 ess/failure code
1b66d 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 that describes
1b66e 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 78 the result of ex
1b66f 65 63 75 74 69 6e 67 20 74 68 65 20 76 69 72 74 ecuting the virt
1b670 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 2e 0a ual.** machine..
1b671 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1b672 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72 6f ne sets the erro
1b673 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69 6e r code and strin
1b674 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a g returned by.**
1b675 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 sqlite3_errcode
1b676 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d (), sqlite3_errm
1b677 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 sg() and sqlite3
1b678 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f 0a _errmsg16()..*/.
1b679 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b67a 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1b67b 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1b67c 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a tmt){. int rc;.
1b67d 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 if( pStmt==0 )
1b67e 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1b67f 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 E_OK;. }else{.
1b680 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 Vdbe *v = (Vd
1b681 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 be*)pStmt;. s
1b682 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e qlite3 *db = v->
1b683 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 db;.#if SQLITE_T
1b684 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 HREADSAFE. sq
1b685 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 lite3_mutex *mut
1b686 65 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 ex = v->db->mute
1b687 78 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 x;.#endif. sq
1b688 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 lite3_mutex_ente
1b689 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 r(mutex);. rc
1b68a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 = sqlite3VdbeFi
1b68b 6e 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 72 nalize(v);. r
1b68c 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 c = sqlite3ApiEx
1b68d 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 it(db, rc);.
1b68e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1b68f 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 7d 0a ave(mutex);. }.
1b690 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1b691 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e 61 74 65 20 /*.** Terminate
1b692 74 68 65 20 63 75 72 72 65 6e 74 20 65 78 65 63 the current exec
1b693 75 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 ution of an SQL
1b694 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 statement and re
1b695 73 65 74 20 69 74 0a 2a 2a 20 62 61 63 6b 20 74 set it.** back t
1b696 6f 20 69 74 73 20 73 74 61 72 74 69 6e 67 20 73 o its starting s
1b697 74 61 74 65 20 73 6f 20 74 68 61 74 20 69 74 20 tate so that it
1b698 63 61 6e 20 62 65 20 72 65 75 73 65 64 2e 20 41 can be reused. A
1b699 20 73 75 63 63 65 73 73 20 63 6f 64 65 20 66 72 success code fr
1b69a 6f 6d 0a 2a 2a 20 74 68 65 20 70 72 69 6f 72 20 om.** the prior
1b69b 65 78 65 63 75 74 69 6f 6e 20 69 73 20 72 65 74 execution is ret
1b69c 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 urned..**.** Thi
1b69d 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 s routine sets t
1b69e 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e he error code an
1b69f 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 d string returne
1b6a0 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f d by.** sqlite3_
1b6a1 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 errcode(), sqlit
1b6a2 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 e3_errmsg() and
1b6a3 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 sqlite3_errmsg16
1b6a4 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ()..*/.SQLITE_AP
1b6a5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 I int sqlite3_re
1b6a6 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 set(sqlite3_stmt
1b6a7 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 *pStmt){. int
1b6a8 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d rc;. if( pStmt=
1b6a9 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 =0 ){. rc = S
1b6aa 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 QLITE_OK;. }els
1b6ab 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d e{. Vdbe *v =
1b6ac 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe*)pStmt;.
1b6ad 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 sqlite3_mutex
1b6ae 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d 3e 6d 75 _enter(v->db->mu
1b6af 74 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 tex);. rc = s
1b6b0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 qlite3VdbeReset(
1b6b1 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 v);. sqlite3V
1b6b2 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 dbeMakeReady(v,
1b6b3 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 -1, 0, 0, 0, 0,
1b6b4 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 0);. assert(
1b6b5 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65 72 (rc & (v->db->er
1b6b6 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a 20 rMask))==rc );.
1b6b7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 rc = sqlite3A
1b6b8 70 69 45 78 69 74 28 76 2d 3e 64 62 2c 20 72 63 piExit(v->db, rc
1b6b9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d );. sqlite3_m
1b6ba 75 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 utex_leave(v->db
1b6bb 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 ->mutex);. }.
1b6bc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1b6bd 0a 2a 2a 20 53 65 74 20 61 6c 6c 20 74 68 65 20 .** Set all the
1b6be 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20 74 68 parameters in th
1b6bf 65 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 e compiled SQL s
1b6c0 74 61 74 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c tatement to NULL
1b6c1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b6c2 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 int sqlite3_clea
1b6c3 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 r_bindings(sqlit
1b6c4 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b e3_stmt *pStmt){
1b6c5 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 . int i;. int
1b6c6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a rc = SQLITE_OK;.
1b6c7 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 Vdbe *p = (Vdb
1b6c8 65 2a 29 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 e*)pStmt;.#if SQ
1b6c9 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a LITE_THREADSAFE.
1b6ca 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 sqlite3_mutex
1b6cb 2a 6d 75 74 65 78 20 3d 20 28 28 56 64 62 65 2a *mutex = ((Vdbe*
1b6cc 29 70 53 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 )pStmt)->db->mut
1b6cd 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c ex;.#endif. sql
1b6ce 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 ite3_mutex_enter
1b6cf 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 (mutex);. for(i
1b6d0 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 =0; i<p->nVar; i
1b6d1 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ++){. sqlite3
1b6d2 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 VdbeMemRelease(&
1b6d3 70 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 p->aVar[i]);.
1b6d4 20 70 2d 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 p->aVar[i].flag
1b6d5 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1b6d6 7d 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 }. if( p->isPre
1b6d7 70 61 72 65 56 32 20 26 26 20 70 2d 3e 65 78 70 pareV2 && p->exp
1b6d8 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70 2d 3e 65 mask ){. p->e
1b6d9 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a xpired = 1;. }.
1b6da 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1b6db 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 leave(mutex);.
1b6dc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f return rc;.}.../
1b6dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c ************ sql
1b6df 69 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a ite3_value_ ***
1b6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b6e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1b6e2 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f The following ro
1b6e3 75 74 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 utines extract i
1b6e4 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 nformation from
1b6e5 61 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 a Mem or sqlite3
1b6e6 5f 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 _value.** struct
1b6e7 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ure..*/.SQLITE_A
1b6e8 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1b6e9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f qlite3_value_blo
1b6ea 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 b(sqlite3_value
1b6eb 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 *pVal){. Mem *p
1b6ec 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 = (Mem*)pVal;.
1b6ed 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 if( p->flags &
1b6ee 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 (MEM_Blob|MEM_St
1b6ef 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 r) ){. sqlite
1b6f0 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 3VdbeMemExpandBl
1b6f1 6f 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c ob(p);. p->fl
1b6f2 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b ags &= ~MEM_Str;
1b6f3 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d . p->flags |=
1b6f4 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 MEM_Blob;. r
1b6f5 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 eturn p->z;. }e
1b6f6 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 lse{. return
1b6f7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1b6f8 78 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a xt(pVal);. }.}.
1b6f9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 SQLITE_API int s
1b6fa 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 qlite3_value_byt
1b6fb 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 es(sqlite3_value
1b6fc 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 *pVal){. retur
1b6fd 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 n sqlite3ValueBy
1b6fe 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 tes(pVal, SQLITE
1b6ff 5f 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 _UTF8);.}.SQLITE
1b700 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1b701 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 _value_bytes16(s
1b702 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 qlite3_value *pV
1b703 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 al){. return sq
1b704 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 lite3ValueBytes(
1b705 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 pVal, SQLITE_UTF
1b706 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 16NATIVE);.}.SQL
1b707 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 ITE_API double s
1b708 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 qlite3_value_dou
1b709 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 ble(sqlite3_valu
1b70a 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 e *pVal){. retu
1b70b 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 rn sqlite3VdbeRe
1b70c 61 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 alValue((Mem*)pV
1b70d 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 al);.}.SQLITE_AP
1b70e 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 I int sqlite3_va
1b70f 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f lue_int(sqlite3_
1b710 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 value *pVal){.
1b711 72 65 74 75 72 6e 20 28 69 6e 74 29 73 71 6c 69 return (int)sqli
1b712 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1b713 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 (Mem*)pVal);.}.S
1b714 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 QLITE_API sqlite
1b715 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 76 _int64 sqlite3_v
1b716 61 6c 75 65 5f 69 6e 74 36 34 28 73 71 6c 69 74 alue_int64(sqlit
1b717 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b e3_value *pVal){
1b718 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1b719 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 3VdbeIntValue((M
1b71a 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c em*)pVal);.}.SQL
1b71b 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e ITE_API const un
1b71c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c signed char *sql
1b71d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1b71e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b71f 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 Val){. return (
1b720 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 const unsigned c
1b721 68 61 72 20 2a 29 73 71 6c 69 74 65 33 56 61 6c har *)sqlite3Val
1b722 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c ueText(pVal, SQL
1b723 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66 ITE_UTF8);.}.#if
1b724 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b725 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1b726 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1b727 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1b728 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 16(sqlite3_value
1b729 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 * pVal){. retur
1b72a 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 n sqlite3ValueTe
1b72b 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f xt(pVal, SQLITE_
1b72c 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a UTF16NATIVE);.}.
1b72d 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 SQLITE_API const
1b72e 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 void *sqlite3_v
1b72f 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 alue_text16be(sq
1b730 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 lite3_value *pVa
1b731 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c l){. return sql
1b732 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 ite3ValueText(pV
1b733 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 al, SQLITE_UTF16
1b734 42 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 BE);.}.SQLITE_AP
1b735 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1b736 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1b737 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 16le(sqlite3_val
1b738 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 ue *pVal){. ret
1b739 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 urn sqlite3Value
1b73a 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 Text(pVal, SQLIT
1b73b 45 5f 55 54 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 E_UTF16LE);.}.#e
1b73c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1b73d 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c MIT_UTF16 */.SQL
1b73e 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1b73f 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 73 te3_value_type(s
1b740 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 qlite3_value* pV
1b741 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 al){. return pV
1b742 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a al->type;.}../**
1b743 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b744 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 ********** sqlit
1b745 65 33 5f 72 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a e3_result_ ****
1b746 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1b747 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 ***********.** T
1b748 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 he following rou
1b749 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 tines are used b
1b74a 79 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 y user-defined f
1b74b 75 6e 63 74 69 6f 6e 73 20 74 6f 20 73 70 65 63 unctions to spec
1b74c 69 66 79 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 ify.** the funct
1b74d 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a ion result..**.*
1b74e 2a 20 54 68 65 20 73 65 74 53 74 72 4f 72 45 72 * The setStrOrEr
1b74f 72 6f 72 28 29 20 66 75 6e 74 69 6f 6e 20 63 61 ror() funtion ca
1b750 6c 6c 73 20 73 71 6c 69 74 65 33 56 64 62 65 4d lls sqlite3VdbeM
1b751 65 6d 53 65 74 53 74 72 28 29 20 74 6f 20 73 74 emSetStr() to st
1b752 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c ore the.** resul
1b753 74 20 61 73 20 61 20 73 74 72 69 6e 67 20 6f 72 t as a string or
1b754 20 62 6c 6f 62 20 62 75 74 20 69 66 20 74 68 65 blob but if the
1b755 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 string or blob
1b756 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20 69 74 is too large, it
1b757 0a 2a 2a 20 74 68 65 6e 20 73 65 74 73 20 74 68 .** then sets th
1b758 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 e error code to
1b759 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f SQLITE_TOOBIG.*/
1b75a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 .static void set
1b75b 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 ResultStrOrError
1b75c 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 (. sqlite3_cont
1b75d 65 78 74 20 2a 70 43 74 78 2c 20 20 2f 2a 20 46 ext *pCtx, /* F
1b75e 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 unction context
1b75f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 */. const char
1b760 2a 7a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 *z, /*
1b761 53 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a String pointer *
1b762 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 /. int n,
1b763 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 /* B
1b764 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67 2c 20 ytes in string,
1b765 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a 20 or negative */.
1b766 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20 u8 enc,
1b767 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f /* Enco
1b768 64 69 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f ding of z. 0 fo
1b769 72 20 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 r BLOBs */. voi
1b76a 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 d (*xDel)(void*)
1b76b 20 20 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 /* Destruct
1b76c 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 or function */.)
1b76d 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 {. if( sqlite3V
1b76e 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 dbeMemSetStr(&pC
1b76f 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 tx->s, z, n, enc
1b770 2c 20 78 44 65 6c 29 3d 3d 53 51 4c 49 54 45 5f , xDel)==SQLITE_
1b771 54 4f 4f 42 49 47 20 29 7b 0a 20 20 20 20 73 71 TOOBIG ){. sq
1b772 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 lite3_result_err
1b773 6f 72 5f 74 6f 6f 62 69 67 28 70 43 74 78 29 3b or_toobig(pCtx);
1b774 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 . }.}.SQLITE_AP
1b775 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b776 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20 73 71 esult_blob(. sq
1b777 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b778 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f Ctx, . const vo
1b779 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c id *z, . int n,
1b77a 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 . void (*xDel)
1b77b 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 (void *).){. as
1b77c 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 sert( n>=0 );.
1b77d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f assert( sqlite3_
1b77e 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d mutex_held(pCtx-
1b77f 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b >s.db->mutex) );
1b780 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f . setResultStrO
1b781 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 rError(pCtx, z,
1b782 6e 2c 20 30 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 n, 0, xDel);.}.S
1b783 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 QLITE_API void s
1b784 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f qlite3_result_do
1b785 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e uble(sqlite3_con
1b786 74 65 78 74 20 2a 70 43 74 78 2c 20 64 6f 75 62 text *pCtx, doub
1b787 6c 65 20 72 56 61 6c 29 7b 0a 20 20 61 73 73 65 le rVal){. asse
1b788 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 rt( sqlite3_mute
1b789 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 x_held(pCtx->s.d
1b78a 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 b->mutex) );. s
1b78b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 qlite3VdbeMemSet
1b78c 44 6f 75 62 6c 65 28 26 70 43 74 78 2d 3e 73 2c Double(&pCtx->s,
1b78d 20 72 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 rVal);.}.SQLITE
1b78e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 _API void sqlite
1b78f 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 3_result_error(s
1b790 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b791 70 43 74 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 pCtx, const char
1b792 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 *z, int n){. a
1b793 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b794 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1b795 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1b796 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 pCtx->isError
1b797 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a = SQLITE_ERROR;.
1b798 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b799 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
1b79a 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 z, n, SQLITE_UT
1b79b 46 38 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 F8, SQLITE_TRANS
1b79c 49 45 4e 54 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 IENT);.}.#ifndef
1b79d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1b79e 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 16.SQLITE_API vo
1b79f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c id sqlite3_resul
1b7a0 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 t_error16(sqlite
1b7a1 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 3_context *pCtx,
1b7a2 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 const void *z,
1b7a3 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 int n){. assert
1b7a4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b7a5 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b7a6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 >mutex) );. pCt
1b7a7 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c x->isError = SQL
1b7a8 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c ITE_ERROR;. sql
1b7a9 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 ite3VdbeMemSetSt
1b7aa 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e r(&pCtx->s, z, n
1b7ab 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 , SQLITE_UTF16NA
1b7ac 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 54 52 41 TIVE, SQLITE_TRA
1b7ad 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 NSIENT);.}.#endi
1b7ae 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 f.SQLITE_API voi
1b7af 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 d sqlite3_result
1b7b0 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e _int(sqlite3_con
1b7b1 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 text *pCtx, int
1b7b2 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 iVal){. assert(
1b7b3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b7b4 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b7b5 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
1b7b6 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
1b7b7 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 28 69 36 64(&pCtx->s, (i6
1b7b8 34 29 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 4)iVal);.}.SQLIT
1b7b9 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b7ba 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 e3_result_int64(
1b7bb 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b7bc 2a 70 43 74 78 2c 20 69 36 34 20 69 56 61 6c 29 *pCtx, i64 iVal)
1b7bd 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 {. assert( sqli
1b7be 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 te3_mutex_held(p
1b7bf 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 Ctx->s.db->mutex
1b7c0 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 ) );. sqlite3Vd
1b7c1 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 beMemSetInt64(&p
1b7c2 43 74 78 2d 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d Ctx->s, iVal);.}
1b7c3 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 .SQLITE_API void
1b7c4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f sqlite3_result_
1b7c5 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e null(sqlite3_con
1b7c6 74 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 text *pCtx){. a
1b7c7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d ssert( sqlite3_m
1b7c8 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e utex_held(pCtx->
1b7c9 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a s.db->mutex) );.
1b7ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b7cb 53 65 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 SetNull(&pCtx->s
1b7cc 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b7cd 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b7ce 75 6c 74 5f 74 65 78 74 28 0a 20 20 73 71 6c 69 ult_text(. sqli
1b7cf 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 te3_context *pCt
1b7d0 78 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 x, . const char
1b7d1 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 *z, . int n,.
1b7d2 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1b7d3 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 id *).){. asser
1b7d4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 t( sqlite3_mutex
1b7d5 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 _held(pCtx->s.db
1b7d6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 ->mutex) );. se
1b7d7 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f tResultStrOrErro
1b7d8 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 r(pCtx, z, n, SQ
1b7d9 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29 LITE_UTF8, xDel)
1b7da 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 ;.}.#ifndef SQLI
1b7db 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 TE_OMIT_UTF16.SQ
1b7dc 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b7dd 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
1b7de 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 t16(. sqlite3_c
1b7df 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 ontext *pCtx, .
1b7e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 const void *z,
1b7e1 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 . int n, . voi
1b7e2 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a d (*xDel)(void *
1b7e3 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 ).){. assert( s
1b7e4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c qlite3_mutex_hel
1b7e5 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 d(pCtx->s.db->mu
1b7e6 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 tex) );. setRes
1b7e7 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 ultStrOrError(pC
1b7e8 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 tx, z, n, SQLITE
1b7e9 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78 44 _UTF16NATIVE, xD
1b7ea 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 el);.}.SQLITE_AP
1b7eb 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 I void sqlite3_r
1b7ec 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 0a esult_text16be(.
1b7ed 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 sqlite3_contex
1b7ee 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 t *pCtx, . cons
1b7ef 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e t void *z, . in
1b7f0 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 t n, . void (*x
1b7f1 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a Del)(void *).){.
1b7f2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1b7f3 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1b7f4 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1b7f5 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74 );. setResultSt
1b7f6 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a rOrError(pCtx, z
1b7f7 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 , n, SQLITE_UTF1
1b7f8 36 42 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 6BE, xDel);.}.SQ
1b7f9 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 LITE_API void sq
1b7fa 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 lite3_result_tex
1b7fb 74 31 36 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 t16le(. sqlite3
1b7fc 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b7fd 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a . const void *z
1b7fe 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 , . int n, . v
1b7ff 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1b800 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 *).){. assert(
1b801 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b802 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b803 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 mutex) );. setR
1b804 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 esultStrOrError(
1b805 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 pCtx, z, n, SQLI
1b806 54 45 5f 55 54 46 31 36 4c 45 2c 20 78 44 65 6c TE_UTF16LE, xDel
1b807 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 );.}.#endif /* S
1b808 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 QLITE_OMIT_UTF16
1b809 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 */.SQLITE_API v
1b80a 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b80b 6c 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 lt_value(sqlite3
1b80c 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 _context *pCtx,
1b80d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1b80e 56 61 6c 75 65 29 7b 0a 20 20 61 73 73 65 72 74 Value){. assert
1b80f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f ( sqlite3_mutex_
1b810 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d held(pCtx->s.db-
1b811 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c >mutex) );. sql
1b812 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 ite3VdbeMemCopy(
1b813 26 70 43 74 78 2d 3e 73 2c 20 70 56 61 6c 75 65 &pCtx->s, pValue
1b814 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1b815 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 void sqlite3_res
1b816 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c ult_zeroblob(sql
1b817 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 ite3_context *pC
1b818 74 78 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 tx, int n){. as
1b819 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b81a 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b81b 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b81c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 sqlite3VdbeMemS
1b81d 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 43 74 78 etZeroBlob(&pCtx
1b81e 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 ->s, n);.}.SQLIT
1b81f 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 E_API void sqlit
1b820 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f e3_result_error_
1b821 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e code(sqlite3_con
1b822 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 text *pCtx, int
1b823 65 72 72 43 6f 64 65 29 7b 0a 20 20 70 43 74 78 errCode){. pCtx
1b824 2d 3e 69 73 45 72 72 6f 72 20 3d 20 65 72 72 43 ->isError = errC
1b825 6f 64 65 3b 0a 20 20 69 66 28 20 70 43 74 78 2d ode;. if( pCtx-
1b826 3e 73 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e >s.flags & MEM_N
1b827 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ull ){. sqlit
1b828 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1b829 26 70 43 74 78 2d 3e 73 2c 20 73 71 6c 69 74 65 &pCtx->s, sqlite
1b82a 33 45 72 72 53 74 72 28 65 72 72 43 6f 64 65 29 3ErrStr(errCode)
1b82b 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20 20 , -1, .
1b82c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b82d 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c SQLITE_UTF8, SQL
1b82e 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d ITE_STATIC);. }
1b82f 0a 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 .}../* Force an
1b830 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 SQLITE_TOOBIG er
1b831 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 ror. */.SQLITE_A
1b832 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f PI void sqlite3_
1b833 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f result_error_too
1b834 62 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 big(sqlite3_cont
1b835 65 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 ext *pCtx){. as
1b836 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1b837 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1b838 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1b839 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d pCtx->isError =
1b83a 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a SQLITE_TOOBIG;.
1b83b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b83c 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c SetStr(&pCtx->s,
1b83d 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 "string or blob
1b83e 20 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a too big", -1, .
1b83f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1b840 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 SQLITE_UT
1b841 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 F8, SQLITE_STATI
1b842 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c C);.}../* An SQL
1b843 49 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e ITE_NOMEM error.
1b844 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 */.SQLITE_API v
1b845 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 oid sqlite3_resu
1b846 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 lt_error_nomem(s
1b847 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1b848 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 pCtx){. assert(
1b849 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 sqlite3_mutex_h
1b84a 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e eld(pCtx->s.db->
1b84b 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 mutex) );. sqli
1b84c 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1b84d 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 l(&pCtx->s);. p
1b84e 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 Ctx->isError = S
1b84f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 QLITE_NOMEM;. p
1b850 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f Ctx->s.db->mallo
1b851 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a cFailed = 1;.}..
1b852 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 /*.** Execute th
1b853 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d e statement pStm
1b854 74 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 t, either until
1b855 61 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 a row of data is
1b856 20 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 ready, the.** s
1b857 74 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 tatement is comp
1b858 6c 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 letely executed
1b859 6f 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 or an error occu
1b85a 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 rs..**.** This r
1b85b 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 outine implement
1b85c 73 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 s the bulk of th
1b85d 65 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 e logic behind t
1b85e 68 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 he sqlite_step()
1b85f 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e .** API. The on
1b860 6c 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 ly thing omitted
1b861 20 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 is the automati
1b862 63 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 c recompile if a
1b863 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e .** schema chan
1b864 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e ge has occurred.
1b865 20 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 That detail is
1b866 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a handled by the.
1b867 2a 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 ** outer sqlite3
1b868 5f 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20 _step() wrapper
1b869 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 procedure..*/.st
1b86a 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 atic int sqlite3
1b86b 53 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 Step(Vdbe *p){.
1b86c 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
1b86d 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 int rc;.. asser
1b86e 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d t(p);. if( p->m
1b86f 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 agic!=VDBE_MAGIC
1b870 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 _RUN ){. retu
1b871 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 rn SQLITE_MISUSE
1b872 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 ;. }.. /* Asse
1b873 72 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 rt that malloc()
1b874 20 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 has not failed
1b875 2a 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b */. db = p->db;
1b876 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f . if( db->mallo
1b877 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 cFailed ){. r
1b878 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
1b879 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 EM;. }.. if( p
1b87a 2d 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 ->pc<=0 && p->ex
1b87b 70 69 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 pired ){. if(
1b87c 20 41 4c 57 41 59 53 28 70 2d 3e 72 63 3d 3d 53 ALWAYS(p->rc==S
1b87d 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 QLITE_OK || p->r
1b87e 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 c==SQLITE_SCHEMA
1b87f 29 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 ) ){. p->rc
1b880 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 = SQLITE_SCHEMA
1b881 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d ;. }. rc =
1b882 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1b883 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 goto end_of_s
1b884 74 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 tep;. }. if( s
1b885 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1b886 62 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 b) ){. p->rc
1b887 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b = SQLITE_MISUSE;
1b888 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 . return SQLI
1b889 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 TE_MISUSE;. }.
1b88a 20 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a if( p->pc<0 ){.
1b88b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 /* If there
1b88c 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 are no other sta
1b88d 74 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c tements currentl
1b88e 79 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a y running, then.
1b88f 20 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 ** reset the
1b890 20 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e interrupt flag.
1b891 20 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 This prevents
1b892 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 a call to sqlite
1b893 33 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 3_interrupt.
1b894 2a 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 ** from interrup
1b895 74 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 ting a statement
1b896 20 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 that has not ye
1b897 74 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a t started.. *
1b898 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 /. if( db->ac
1b899 74 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 tiveVdbeCnt==0 )
1b89a 7b 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 {. db->u1.i
1b89b 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 sInterrupted = 0
1b89c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 ;. }.. ass
1b89d 65 72 74 28 20 64 62 2d 3e 77 72 69 74 65 56 64 ert( db->writeVd
1b89e 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 beCnt>0 || db->a
1b89f 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 utoCommit==0 ||
1b8a0 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e db->nDeferredCon
1b8a1 73 3d 3d 30 20 29 3b 0a 0a 23 69 66 6e 64 65 66 s==0 );..#ifndef
1b8a2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 SQLITE_OMIT_TRA
1b8a3 43 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 CE. if( db->x
1b8a4 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e Profile && !db->
1b8a5 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 init.busy ){.
1b8a6 20 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a double rNow;.
1b8a7 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 sqlite3OsC
1b8a8 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 urrentTime(db->p
1b8a9 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 Vfs, &rNow);.
1b8aa 20 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 p->startTime
1b8ab 3d 20 28 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 = (u64)((rNow -
1b8ac 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e (int)rNow)*3600.
1b8ad 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30 0*24.0*100000000
1b8ae 30 2e 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 0.0);. }.#end
1b8af 69 66 0a 0a 20 20 20 20 64 62 2d 3e 61 63 74 69 if.. db->acti
1b8b0 76 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 veVdbeCnt++;.
1b8b1 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 if( p->readOnly
1b8b2 3d 3d 30 20 29 20 64 62 2d 3e 77 72 69 74 65 56 ==0 ) db->writeV
1b8b3 64 62 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 70 2d dbeCnt++;. p-
1b8b4 3e 70 63 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 >pc = 0;. }.#if
1b8b5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1b8b6 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 _EXPLAIN. if( p
1b8b7 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 ->explain ){.
1b8b8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 rc = sqlite3Vdb
1b8b9 65 4c 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 eList(p);. }els
1b8ba 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 e.#endif /* SQLI
1b8bb 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 TE_OMIT_EXPLAIN
1b8bc 2a 2f 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 */. {. rc =
1b8bd 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 sqlite3VdbeExec(
1b8be 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 p);. }.. if( s
1b8bf 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1b8c0 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 db) ){. rc =
1b8c1 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1b8c2 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }..#ifndef SQLI
1b8c3 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 TE_OMIT_TRACE.
1b8c4 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 /* Invoke the pr
1b8c5 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 ofile callback i
1b8c6 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 f there is one.
1b8c7 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 */. if( rc!=SQ
1b8c8 4c 49 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e LITE_ROW && db->
1b8c9 78 50 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d xProfile && !db-
1b8ca 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d >init.busy && p-
1b8cb 3e 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 75 >zSql ){. dou
1b8cc 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 ble rNow;. u6
1b8cd 34 20 65 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 4 elapseTime;..
1b8ce 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 sqlite3OsCurr
1b8cf 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 entTime(db->pVfs
1b8d0 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c , &rNow);. el
1b8d1 61 70 73 65 54 69 6d 65 20 3d 20 28 75 36 34 29 apseTime = (u64)
1b8d2 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e ((rNow - (int)rN
1b8d3 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a ow)*3600.0*24.0*
1b8d4 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b 0a 20 1000000000.0);.
1b8d5 20 20 20 65 6c 61 70 73 65 54 69 6d 65 20 2d 3d elapseTime -=
1b8d6 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 3b 0a 20 p->startTime;.
1b8d7 20 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 28 db->xProfile(
1b8d8 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 2c db->pProfileArg,
1b8d9 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c 61 70 73 65 p->zSql, elapse
1b8da 54 69 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 Time);. }.#endi
1b8db 66 0a 0a 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 f.. db->errCode
1b8dc 20 3d 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c = rc;. if( SQL
1b8dd 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 ITE_NOMEM==sqlit
1b8de 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c e3ApiExit(p->db,
1b8df 20 70 2d 3e 72 63 29 20 29 7b 0a 20 20 20 20 70 p->rc) ){. p
1b8e0 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f ->rc = SQLITE_NO
1b8e1 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64 5f 6f 66 5f MEM;. }.end_of_
1b8e2 73 74 65 70 3a 0a 20 20 2f 2a 20 41 74 20 74 68 step:. /* At th
1b8e3 69 73 20 70 6f 69 6e 74 20 6c 6f 63 61 6c 20 76 is point local v
1b8e4 61 72 69 61 62 6c 65 20 72 63 20 68 6f 6c 64 73 ariable rc holds
1b8e5 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 the value that
1b8e6 73 68 6f 75 6c 64 20 62 65 20 0a 20 20 2a 2a 20 should be . **
1b8e7 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 69 73 returned if this
1b8e8 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 63 statement was c
1b8e9 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 74 68 ompiled using th
1b8ea 65 20 6c 65 67 61 63 79 20 0a 20 20 2a 2a 20 73 e legacy . ** s
1b8eb 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 qlite3_prepare()
1b8ec 20 69 6e 74 65 72 66 61 63 65 2e 20 41 63 63 6f interface. Acco
1b8ed 72 64 69 6e 67 20 74 6f 20 74 68 65 20 64 6f 63 rding to the doc
1b8ee 73 2c 20 74 68 69 73 20 63 61 6e 20 6f 6e 6c 79 s, this can only
1b8ef 0a 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 . ** be one of
1b8f0 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 74 68 the values in th
1b8f1 65 20 66 69 72 73 74 20 61 73 73 65 72 74 28 29 e first assert()
1b8f2 20 62 65 6c 6f 77 2e 20 56 61 72 69 61 62 6c 65 below. Variable
1b8f3 20 70 2d 3e 72 63 20 0a 20 20 2a 2a 20 63 6f 6e p->rc . ** con
1b8f4 74 61 69 6e 73 20 74 68 65 20 76 61 6c 75 65 20 tains the value
1b8f5 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 72 65 that would be re
1b8f6 74 75 72 6e 65 64 20 69 66 20 73 71 6c 69 74 65 turned if sqlite
1b8f7 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 0a 20 20 3_finalize() .
1b8f8 2a 2a 20 77 65 72 65 20 63 61 6c 6c 65 64 20 6f ** were called o
1b8f9 6e 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 20 n statement p..
1b8fa 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 */. assert( rc
1b8fb 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 20 7c 7c ==SQLITE_ROW ||
1b8fc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 rc==SQLITE_DONE
1b8fd 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 || rc==SQLITE
1b8fe 5f 45 52 52 4f 52 20 0a 20 20 20 20 20 20 20 7c _ERROR . |
1b8ff 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 | rc==SQLITE_BUS
1b900 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f Y || rc==SQLITE_
1b901 4d 49 53 55 53 45 0a 20 20 29 3b 0a 20 20 61 73 MISUSE. );. as
1b902 73 65 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c sert( p->rc!=SQL
1b903 49 54 45 5f 52 4f 57 20 26 26 20 70 2d 3e 72 63 ITE_ROW && p->rc
1b904 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b !=SQLITE_DONE );
1b905 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65 70 . if( p->isPrep
1b906 61 72 65 56 32 20 26 26 20 72 63 21 3d 53 51 4c areV2 && rc!=SQL
1b907 49 54 45 5f 52 4f 57 20 26 26 20 72 63 21 3d 53 ITE_ROW && rc!=S
1b908 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 QLITE_DONE ){.
1b909 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73 74 61 /* If this sta
1b90a 74 65 6d 65 6e 74 20 77 61 73 20 70 72 65 70 61 tement was prepa
1b90b 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 red using sqlite
1b90c 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 2c 20 3_prepare_v2(),
1b90d 61 6e 64 20 61 6e 0a 20 20 20 20 2a 2a 20 65 72 and an. ** er
1b90e 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64 2c ror has occured,
1b90f 20 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 65 then return the
1b910 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e 20 70 error code in p
1b911 2d 3e 72 63 20 74 6f 20 74 68 65 0a 20 20 20 20 ->rc to the.
1b912 2a 2a 20 63 61 6c 6c 65 72 2e 20 53 65 74 20 74 ** caller. Set t
1b913 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 6e he error code in
1b914 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1b915 6e 64 6c 65 20 74 6f 20 74 68 65 20 73 61 6d 65 ndle to the same
1b916 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 20 0a value.. */ .
1b917 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 65 72 72 rc = db->err
1b918 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 Code = p->rc;.
1b919 7d 0a 20 20 72 65 74 75 72 6e 20 28 72 63 26 64 }. return (rc&d
1b91a 62 2d 3e 65 72 72 4d 61 73 6b 29 3b 0a 7d 0a 0a b->errMask);.}..
1b91b 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 /*.** This is th
1b91c 65 20 74 6f 70 2d 6c 65 76 65 6c 20 69 6d 70 6c e top-level impl
1b91d 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 ementation of sq
1b91e 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20 43 lite3_step(). C
1b91f 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74 65 33 53 74 all.** sqlite3St
1b920 65 70 28 29 20 74 6f 20 64 6f 20 6d 6f 73 74 20 ep() to do most
1b921 6f 66 20 74 68 65 20 77 6f 72 6b 2e 20 20 49 66 of the work. If
1b922 20 61 20 73 63 68 65 6d 61 20 65 72 72 6f 72 20 a schema error
1b923 6f 63 63 75 72 73 2c 0a 2a 2a 20 63 61 6c 6c 20 occurs,.** call
1b924 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 sqlite3Reprepare
1b925 28 29 20 61 6e 64 20 74 72 79 20 61 67 61 69 6e () and try again
1b926 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b927 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 int sqlite3_step
1b928 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1b929 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 Stmt){. int rc
1b92a 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b = SQLITE_MISUSE;
1b92b 0a 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a . if( pStmt ){.
1b92c 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b int cnt = 0;
1b92d 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 28 . Vdbe *v = (
1b92e 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 Vdbe*)pStmt;.
1b92f 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 sqlite3 *db = v
1b930 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 ->db;. sqlite
1b931 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 3_mutex_enter(db
1b932 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 77 68 ->mutex);. wh
1b933 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74 ile( (rc = sqlit
1b934 65 33 53 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 e3Step(v))==SQLI
1b935 54 45 5f 53 43 48 45 4d 41 0a 20 20 20 20 20 20 TE_SCHEMA.
1b936 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 && cnt++ <
1b937 35 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 5. &&
1b938 28 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 70 (rc = sqlite3Rep
1b939 72 65 70 61 72 65 28 76 29 29 3d 3d 53 51 4c 49 repare(v))==SQLI
1b93a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 TE_OK ){. s
1b93b 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 53 74 qlite3_reset(pSt
1b93c 6d 74 29 3b 0a 20 20 20 20 20 20 76 2d 3e 65 78 mt);. v->ex
1b93d 70 69 72 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d pired = 0;. }
1b93e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c . if( rc==SQL
1b93f 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20 41 4c ITE_SCHEMA && AL
1b940 57 41 59 53 28 76 2d 3e 69 73 50 72 65 70 61 72 WAYS(v->isPrepar
1b941 65 56 32 29 20 26 26 20 41 4c 57 41 59 53 28 64 eV2) && ALWAYS(d
1b942 62 2d 3e 70 45 72 72 29 20 29 7b 0a 20 20 20 20 b->pErr) ){.
1b943 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20 6f /* This case o
1b944 63 63 75 72 73 20 61 66 74 65 72 20 66 61 69 6c ccurs after fail
1b945 69 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c 65 ing to recompile
1b946 20 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65 6e an sql statemen
1b947 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 t. . ** The
1b948 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 error message f
1b949 72 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d 70 rom the SQL comp
1b94a 69 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 iler has already
1b94b 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20 20 been loaded .
1b94c 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 ** into the
1b94d 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e database handle.
1b94e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 This block copi
1b94f 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 es the error mes
1b950 73 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 sage . ** f
1b951 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 rom the database
1b952 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68 65 handle into the
1b953 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 73 statement and s
1b954 65 74 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e ets the statemen
1b955 74 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67 72 t. ** progr
1b956 61 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30 20 am counter to 0
1b957 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 77 to ensure that w
1b958 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e hen the statemen
1b959 74 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 t is . ** f
1b95a 69 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73 65 inalized or rese
1b95b 74 20 74 68 65 20 70 61 72 73 65 72 20 65 72 72 t the parser err
1b95c 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 61 76 or message is av
1b95d 61 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20 20 ailable via.
1b95e 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 ** sqlite3_err
1b95f 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65 msg() and sqlite
1b960 33 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20 20 3_errcode()..
1b961 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 */. cons
1b962 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 28 t char *zErr = (
1b963 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c const char *)sql
1b964 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 ite3_value_text(
1b965 64 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20 20 db->pErr); .
1b966 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
1b967 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29 3b db, v->zErrMsg);
1b968 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e . if( !db->
1b969 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1b96a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d v->zErrM
1b96b 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 sg = sqlite3DbSt
1b96c 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b 0a rDup(db, zErr);.
1b96d 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 } else {.
1b96e 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d 73 v->zErrMs
1b96f 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 76 g = 0;. v
1b970 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f ->rc = SQLITE_NO
1b971 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 MEM;. }.
1b972 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 }. rc = sqli
1b973 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 te3ApiExit(db, r
1b974 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f c);. sqlite3_
1b975 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e mutex_leave(db->
1b976 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
1b977 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a turn rc;.}../*.*
1b978 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 75 73 * Extract the us
1b979 65 72 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 er data from a s
1b97a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 qlite3_context s
1b97b 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 tructure and ret
1b97c 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 urn a.** pointer
1b97d 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 to it..*/.SQLIT
1b97e 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 E_API void *sqli
1b97f 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73 71 te3_user_data(sq
1b980 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 lite3_context *p
1b981 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 ){. assert( p &
1b982 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 & p->pFunc );.
1b983 72 65 74 75 72 6e 20 70 2d 3e 70 46 75 6e 63 2d return p->pFunc-
1b984 3e 70 55 73 65 72 44 61 74 61 3b 0a 7d 0a 0a 2f >pUserData;.}../
1b985 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
1b986 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d 20 user data from
1b987 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a sqlite3_contex
1b988 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 t structure and
1b989 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69 6e return a.** poin
1b98a 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53 51 ter to it..*/.SQ
1b98b 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 LITE_API sqlite3
1b98c 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 *sqlite3_contex
1b98d 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 t_db_handle(sqli
1b98e 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b te3_context *p){
1b98f 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20 . assert( p &&
1b990 70 2d 3e 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 p->pFunc );. re
1b991 74 75 72 6e 20 70 2d 3e 73 2e 64 62 3b 0a 7d 0a turn p->s.db;.}.
1b992 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f ./*.** The follo
1b993 77 69 6e 67 20 69 73 20 74 68 65 20 69 6d 70 6c wing is the impl
1b994 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e ementation of an
1b995 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 SQL function th
1b996 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 66 61 69 at always.** fai
1b997 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 ls with an error
1b998 20 6d 65 73 73 61 67 65 20 73 74 61 74 69 6e 67 message stating
1b999 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 that the functi
1b99a 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20 74 68 on is used in th
1b99b 65 0a 2a 2a 20 77 72 6f 6e 67 20 63 6f 6e 74 65 e.** wrong conte
1b99c 78 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 xt. The sqlite3
1b99d 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 _overload_functi
1b99e 6f 6e 28 29 20 41 50 49 20 6d 69 67 68 74 20 63 on() API might c
1b99f 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 53 51 4c 20 onstruct.** SQL
1b9a0 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73 function that us
1b9a1 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 e this routine s
1b9a2 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 o that the funct
1b9a3 69 6f 6e 73 20 77 69 6c 6c 20 65 78 69 73 74 0a ions will exist.
1b9a4 2a 2a 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f ** for name reso
1b9a5 6c 75 74 69 6f 6e 20 62 75 74 20 61 72 65 20 61 lution but are a
1b9a6 63 74 75 61 6c 6c 79 20 6f 76 65 72 6c 6f 61 64 ctually overload
1b9a7 65 64 20 62 79 20 74 68 65 20 78 46 69 6e 64 46 ed by the xFindF
1b9a8 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 65 74 68 6f unction.** metho
1b9a9 64 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62 d of virtual tab
1b9aa 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 les..*/.SQLITE_P
1b9ab 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1b9ac 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 te3InvalidFuncti
1b9ad 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f on(. sqlite3_co
1b9ae 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 ntext *context,
1b9af 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e /* The function
1b9b0 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 74 65 78 74 calling context
1b9b1 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 */. int NotUse
1b9b2 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d,
1b9b3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
1b9b4 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 guments to the f
1b9b5 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c unction */. sql
1b9b6 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 ite3_value **Not
1b9b7 55 73 65 64 32 20 20 20 2f 2a 20 56 61 6c 75 65 Used2 /* Value
1b9b8 20 6f 66 20 65 61 63 68 20 61 72 67 75 6d 65 6e of each argumen
1b9b9 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 t */.){. const
1b9ba 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 63 6f char *zName = co
1b9bb 6e 74 65 78 74 2d 3e 70 46 75 6e 63 2d 3e 7a 4e ntext->pFunc->zN
1b9bc 61 6d 65 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 ame;. char *zEr
1b9bd 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 r;. UNUSED_PARA
1b9be 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 METER2(NotUsed,
1b9bf 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 7a 45 72 NotUsed2);. zEr
1b9c0 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 r = sqlite3_mpri
1b9c1 6e 74 66 28 0a 20 20 20 20 20 20 22 75 6e 61 62 ntf(. "unab
1b9c2 6c 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 le to use functi
1b9c3 6f 6e 20 25 73 20 69 6e 20 74 68 65 20 72 65 71 on %s in the req
1b9c4 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 22 2c uested context",
1b9c5 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 zName);. sqlit
1b9c6 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 e3_result_error(
1b9c7 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d context, zErr, -
1b9c8 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 1);. sqlite3_fr
1b9c9 65 65 28 7a 45 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a ee(zErr);.}../*.
1b9ca 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f 72 20 72 ** Allocate or r
1b9cb 65 74 75 72 6e 20 74 68 65 20 61 67 67 72 65 67 eturn the aggreg
1b9cc 61 74 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 ate context for
1b9cd 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e a user function.
1b9ce 20 20 41 20 6e 65 77 0a 2a 2a 20 63 6f 6e 74 65 A new.** conte
1b9cf 78 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 xt is allocated
1b9d0 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61 6c on the first cal
1b9d1 6c 2e 20 20 53 75 62 73 65 71 75 65 6e 74 20 63 l. Subsequent c
1b9d2 61 6c 6c 73 20 72 65 74 75 72 6e 20 74 68 65 0a alls return the.
1b9d3 2a 2a 20 73 61 6d 65 20 63 6f 6e 74 65 78 74 20 ** same context
1b9d4 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 that was returne
1b9d5 64 20 6f 6e 20 70 72 69 6f 72 20 63 61 6c 6c 73 d on prior calls
1b9d6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1b9d7 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 void *sqlite3_ag
1b9d8 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 gregate_context(
1b9d9 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1b9da 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a *p, int nByte){.
1b9db 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 61 Mem *pMem;. a
1b9dc 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 ssert( p && p->p
1b9dd 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 Func && p->pFunc
1b9de 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 61 73 73 ->xStep );. ass
1b9df 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 ert( sqlite3_mut
1b9e0 65 78 5f 68 65 6c 64 28 70 2d 3e 73 2e 64 62 2d ex_held(p->s.db-
1b9e1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65 >mutex) );. pMe
1b9e2 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b 0a 20 20 69 m = p->pMem;. i
1b9e3 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 f( (pMem->flags
1b9e4 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20 29 7b & MEM_Agg)==0 ){
1b9e5 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3d 3d . if( nByte==
1b9e6 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
1b9e7 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 e3VdbeMemRelease
1b9e8 45 78 74 65 72 6e 61 6c 28 70 4d 65 6d 29 3b 0a External(pMem);.
1b9e9 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 pMem->flag
1b9ea 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 s = MEM_Null;.
1b9eb 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 30 3b pMem->z = 0;
1b9ec 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1b9ed 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1b9ee 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74 65 Grow(pMem, nByte
1b9ef 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d , 0);. pMem
1b9f0 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 41 67 ->flags = MEM_Ag
1b9f1 67 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 g;. pMem->u
1b9f2 2e 70 44 65 66 20 3d 20 70 2d 3e 70 46 75 6e 63 .pDef = p->pFunc
1b9f3 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d ;. if( pMem
1b9f4 2d 3e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 6d ->z ){. m
1b9f5 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 emset(pMem->z, 0
1b9f6 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 , nByte);.
1b9f7 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 }. }. }. re
1b9f8 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 4d 65 6d turn (void*)pMem
1b9f9 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ->z;.}../*.** Re
1b9fa 74 75 72 6e 20 74 68 65 20 61 75 78 69 6c 61 72 turn the auxilar
1b9fb 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2c 20 y data pointer,
1b9fc 69 66 20 61 6e 79 2c 20 66 6f 72 20 74 68 65 20 if any, for the
1b9fd 69 41 72 67 27 74 68 20 61 72 67 75 6d 65 6e 74 iArg'th argument
1b9fe 20 74 6f 0a 2a 2a 20 74 68 65 20 75 73 65 72 2d to.** the user-
1b9ff 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 64 function defined
1ba00 20 62 79 20 70 43 74 78 2e 0a 2a 2f 0a 53 51 4c by pCtx..*/.SQL
1ba01 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 ITE_API void *sq
1ba02 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 lite3_get_auxdat
1ba03 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 a(sqlite3_contex
1ba04 74 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 41 72 t *pCtx, int iAr
1ba05 67 29 7b 0a 20 20 56 64 62 65 46 75 6e 63 20 2a g){. VdbeFunc *
1ba06 70 56 64 62 65 46 75 6e 63 3b 0a 0a 20 20 61 73 pVdbeFunc;.. as
1ba07 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 sert( sqlite3_mu
1ba08 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 tex_held(pCtx->s
1ba09 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 .db->mutex) );.
1ba0a 20 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 pVdbeFunc = pCt
1ba0b 78 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 x->pVdbeFunc;.
1ba0c 69 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c if( !pVdbeFunc |
1ba0d 7c 20 69 41 72 67 3e 3d 70 56 64 62 65 46 75 6e | iArg>=pVdbeFun
1ba0e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69 41 72 67 3c c->nAux || iArg<
1ba0f 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1ba10 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 0;. }. return
1ba11 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 pVdbeFunc->apAux
1ba12 5b 69 41 72 67 5d 2e 70 41 75 78 3b 0a 7d 0a 0a [iArg].pAux;.}..
1ba13 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 61 75 /*.** Set the au
1ba14 78 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e xilary data poin
1ba15 74 65 72 20 61 6e 64 20 64 65 6c 65 74 65 20 66 ter and delete f
1ba16 75 6e 63 74 69 6f 6e 2c 20 66 6f 72 20 74 68 65 unction, for the
1ba17 20 69 41 72 67 27 74 68 0a 2a 2a 20 61 72 67 75 iArg'th.** argu
1ba18 6d 65 6e 74 20 74 6f 20 74 68 65 20 75 73 65 72 ment to the user
1ba19 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 65 -function define
1ba1a 64 20 62 79 20 70 43 74 78 2e 20 41 6e 79 20 70 d by pCtx. Any p
1ba1b 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 69 73 revious value is
1ba1c 0a 2a 2a 20 64 65 6c 65 74 65 64 20 62 79 20 63 .** deleted by c
1ba1d 61 6c 6c 69 6e 67 20 74 68 65 20 64 65 6c 65 74 alling the delet
1ba1e 65 20 66 75 6e 63 74 69 6f 6e 20 73 70 65 63 69 e function speci
1ba1f 66 69 65 64 20 77 68 65 6e 20 69 74 20 77 61 73 fied when it was
1ba20 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f set..*/.SQLITE_
1ba21 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 API void sqlite3
1ba22 5f 73 65 74 5f 61 75 78 64 61 74 61 28 0a 20 20 _set_auxdata(.
1ba23 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 sqlite3_context
1ba24 2a 70 43 74 78 2c 20 0a 20 20 69 6e 74 20 69 41 *pCtx, . int iA
1ba25 72 67 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 75 rg, . void *pAu
1ba26 78 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 x, . void (*xDe
1ba27 6c 65 74 65 29 28 76 6f 69 64 2a 29 0a 29 7b 0a lete)(void*).){.
1ba28 20 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 struct AuxData
1ba29 20 2a 70 41 75 78 44 61 74 61 3b 0a 20 20 56 64 *pAuxData;. Vd
1ba2a 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e beFunc *pVdbeFun
1ba2b 63 3b 0a 20 20 69 66 28 20 69 41 72 67 3c 30 20 c;. if( iArg<0
1ba2c 29 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 0a ) goto failed;..
1ba2d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 assert( sqlite
1ba2e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 3_mutex_held(pCt
1ba2f 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 x->s.db->mutex)
1ba30 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d );. pVdbeFunc =
1ba31 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63 pCtx->pVdbeFunc
1ba32 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75 ;. if( !pVdbeFu
1ba33 6e 63 20 7c 7c 20 70 56 64 62 65 46 75 6e 63 2d nc || pVdbeFunc-
1ba34 3e 6e 41 75 78 3c 3d 69 41 72 67 20 29 7b 0a 20 >nAux<=iArg ){.
1ba35 20 20 20 69 6e 74 20 6e 41 75 78 20 3d 20 28 70 int nAux = (p
1ba36 56 64 62 65 46 75 6e 63 20 3f 20 70 56 64 62 65 VdbeFunc ? pVdbe
1ba37 46 75 6e 63 2d 3e 6e 41 75 78 20 3a 20 30 29 3b Func->nAux : 0);
1ba38 0a 20 20 20 20 69 6e 74 20 6e 4d 61 6c 6c 6f 63 . int nMalloc
1ba39 20 3d 20 73 69 7a 65 6f 66 28 56 64 62 65 46 75 = sizeof(VdbeFu
1ba3a 6e 63 29 20 2b 20 73 69 7a 65 6f 66 28 73 74 72 nc) + sizeof(str
1ba3b 75 63 74 20 41 75 78 44 61 74 61 29 2a 69 41 72 uct AuxData)*iAr
1ba3c 67 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 g;. pVdbeFunc
1ba3d 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c = sqlite3DbReal
1ba3e 6c 6f 63 28 70 43 74 78 2d 3e 73 2e 64 62 2c 20 loc(pCtx->s.db,
1ba3f 70 56 64 62 65 46 75 6e 63 2c 20 6e 4d 61 6c 6c pVdbeFunc, nMall
1ba40 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 21 70 56 oc);. if( !pV
1ba41 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 20 dbeFunc ){.
1ba42 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 goto failed;.
1ba43 20 20 7d 0a 20 20 20 20 70 43 74 78 2d 3e 70 56 }. pCtx->pV
1ba44 64 62 65 46 75 6e 63 20 3d 20 70 56 64 62 65 46 dbeFunc = pVdbeF
1ba45 75 6e 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 unc;. memset(
1ba46 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75 &pVdbeFunc->apAu
1ba47 78 5b 6e 41 75 78 5d 2c 20 30 2c 20 73 69 7a 65 x[nAux], 0, size
1ba48 6f 66 28 73 74 72 75 63 74 20 41 75 78 44 61 74 of(struct AuxDat
1ba49 61 29 2a 28 69 41 72 67 2b 31 2d 6e 41 75 78 29 a)*(iArg+1-nAux)
1ba4a 29 3b 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 );. pVdbeFunc
1ba4b 2d 3e 6e 41 75 78 20 3d 20 69 41 72 67 2b 31 3b ->nAux = iArg+1;
1ba4c 0a 20 20 20 20 70 56 64 62 65 46 75 6e 63 2d 3e . pVdbeFunc->
1ba4d 70 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 46 pFunc = pCtx->pF
1ba4e 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20 70 41 75 78 unc;. }.. pAux
1ba4f 44 61 74 61 20 3d 20 26 70 56 64 62 65 46 75 6e Data = &pVdbeFun
1ba50 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 3b 0a c->apAux[iArg];.
1ba51 20 20 69 66 28 20 70 41 75 78 44 61 74 61 2d 3e if( pAuxData->
1ba52 70 41 75 78 20 26 26 20 70 41 75 78 44 61 74 61 pAux && pAuxData
1ba53 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20 ->xDelete ){.
1ba54 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 pAuxData->xDele
1ba55 74 65 28 70 41 75 78 44 61 74 61 2d 3e 70 41 75 te(pAuxData->pAu
1ba56 78 29 3b 0a 20 20 7d 0a 20 20 70 41 75 78 44 61 x);. }. pAuxDa
1ba57 74 61 2d 3e 70 41 75 78 20 3d 20 70 41 75 78 3b ta->pAux = pAux;
1ba58 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 . pAuxData->xDe
1ba59 6c 65 74 65 20 3d 20 78 44 65 6c 65 74 65 3b 0a lete = xDelete;.
1ba5a 20 20 72 65 74 75 72 6e 3b 0a 0a 66 61 69 6c 65 return;..faile
1ba5b 64 3a 0a 20 20 69 66 28 20 78 44 65 6c 65 74 65 d:. if( xDelete
1ba5c 20 29 7b 0a 20 20 20 20 78 44 65 6c 65 74 65 28 ){. xDelete(
1ba5d 70 41 75 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 pAux);. }.}..#i
1ba5e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1ba5f 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a T_DEPRECATED./*.
1ba60 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
1ba61 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 mber of times th
1ba62 65 20 53 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 e Step function
1ba63 6f 66 20 61 20 61 67 67 72 65 67 61 74 65 20 68 of a aggregate h
1ba64 61 73 20 62 65 65 6e 20 0a 2a 2a 20 63 61 6c 6c as been .** call
1ba65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ed..**.** This f
1ba66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65 unction is depre
1ba67 63 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 75 cated. Do not u
1ba68 73 65 20 69 74 20 66 6f 72 20 6e 65 77 20 63 6f se it for new co
1ba69 64 65 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 de. It is.** pr
1ba6a 6f 76 69 64 65 20 6f 6e 6c 79 20 74 6f 20 61 76 ovide only to av
1ba6b 6f 69 64 20 62 72 65 61 6b 69 6e 67 20 6c 65 67 oid breaking leg
1ba6c 61 63 79 20 63 6f 64 65 2e 20 20 4e 65 77 20 61 acy code. New a
1ba6d 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1ba6e 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 n.** implementat
1ba6f 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6b 65 65 70 ions should keep
1ba70 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 75 6e 74 their own count
1ba71 73 20 77 69 74 68 69 6e 20 74 68 65 69 72 20 61 s within their a
1ba72 67 67 72 65 67 61 74 65 0a 2a 2a 20 63 6f 6e 74 ggregate.** cont
1ba73 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 ext..*/.SQLITE_A
1ba74 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 PI int sqlite3_a
1ba75 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 ggregate_count(s
1ba76 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a qlite3_context *
1ba77 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20 p){. assert( p
1ba78 26 26 20 70 2d 3e 70 4d 65 6d 20 26 26 20 70 2d && p->pMem && p-
1ba79 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 >pFunc && p->pFu
1ba7a 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 nc->xStep );. r
1ba7b 65 74 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e eturn p->pMem->n
1ba7c 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a ;.}.#endif../*.*
1ba7d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d * Return the num
1ba7e 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
1ba7f 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 n the result set
1ba80 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 for the stateme
1ba81 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c nt pStmt..*/.SQL
1ba82 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1ba83 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 te3_column_count
1ba84 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1ba85 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 Stmt){. Vdbe *p
1ba86 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 Vm = (Vdbe *)pSt
1ba87 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d mt;. return pVm
1ba88 20 3f 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 ? pVm->nResColu
1ba89 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a mn : 0;.}../*.**
1ba8a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1ba8b 65 72 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 er of values ava
1ba8c 69 6c 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 ilable from the
1ba8d 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 current row of t
1ba8e 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 he.** currently
1ba8f 65 78 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d executing statem
1ba90 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 ent pStmt..*/.SQ
1ba91 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1ba92 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 ite3_data_count(
1ba93 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1ba94 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 tmt){. Vdbe *pV
1ba95 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d m = (Vdbe *)pStm
1ba96 74 3b 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 t;. if( pVm==0
1ba97 7c 7c 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 || pVm->pResultS
1ba98 65 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 et==0 ) return 0
1ba99 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e ;. return pVm->
1ba9a 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a nResColumn;.}...
1ba9b 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 /*.** Check to s
1ba9c 65 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f ee if column iCo
1ba9d 6c 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 l of the given s
1ba9e 74 61 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 tatement is vali
1ba9f 64 2e 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c d. If.** it is,
1baa0 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 return a pointe
1baa1 72 20 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 r to the Mem for
1baa2 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 the value of th
1baa3 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 at column..** If
1baa4 20 69 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c iCol is not val
1baa5 69 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 id, return a poi
1baa6 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 nter to a Mem wh
1baa7 69 63 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a ich has a value.
1baa8 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 ** of NULL..*/.s
1baa9 74 61 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d tatic Mem *colum
1baaa 6e 4d 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d nMem(sqlite3_stm
1baab 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 t *pStmt, int i)
1baac 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 {. Vdbe *pVm;.
1baad 20 69 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d int vals;. Mem
1baae 20 2a 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d *pOut;.. pVm =
1baaf 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a (Vdbe *)pStmt;.
1bab0 20 20 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d if( pVm && pVm
1bab1 2d 3e 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 ->pResultSet!=0
1bab2 26 26 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f && i<pVm->nResCo
1bab3 6c 75 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a lumn && i>=0 ){.
1bab4 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1bab5 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d x_enter(pVm->db-
1bab6 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c >mutex);. val
1bab7 73 20 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 s = sqlite3_data
1bab8 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 _count(pStmt);.
1bab9 20 20 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e pOut = &pVm->
1baba 70 52 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 pResultSet[i];.
1babb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 }else{. /* I
1babc 66 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 f the value pass
1babd 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
1babe 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 75 74 argument is out
1babf 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72 of range, retur
1bac0 6e 0a 20 20 20 20 2a 2a 20 61 20 70 6f 69 6e 74 n. ** a point
1bac1 65 72 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 er to the follow
1bac2 69 6e 67 20 73 74 61 74 69 63 20 4d 65 6d 20 6f ing static Mem o
1bac3 62 6a 65 63 74 20 77 68 69 63 68 20 63 6f 6e 74 bject which cont
1bac4 61 69 6e 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 ains the. **
1bac5 76 61 6c 75 65 20 53 51 4c 20 4e 55 4c 4c 2e 20 value SQL NULL.
1bac6 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 Even though the
1bac7 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 63 6f Mem structure co
1bac8 6e 74 61 69 6e 73 20 61 6e 20 65 6c 65 6d 65 6e ntains an elemen
1bac9 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 79 70 65 t. ** of type
1baca 20 69 36 34 2c 20 6f 6e 20 63 65 72 74 61 69 6e i64, on certain
1bacb 20 61 72 63 68 69 74 65 63 74 75 72 65 20 28 78 architecture (x
1bacc 38 36 29 20 77 69 74 68 20 63 65 72 74 61 69 6e 86) with certain
1bacd 20 63 6f 6d 70 69 6c 65 72 0a 20 20 20 20 2a 2a compiler. **
1bace 20 73 77 69 74 63 68 65 73 20 28 2d 4f 73 29 2c switches (-Os),
1bacf 20 67 63 63 20 6d 61 79 20 61 6c 69 67 6e 20 74 gcc may align t
1bad0 68 69 73 20 4d 65 6d 20 6f 62 6a 65 63 74 20 6f his Mem object o
1bad1 6e 20 61 20 34 2d 62 79 74 65 20 62 6f 75 6e 64 n a 4-byte bound
1bad2 61 72 79 0a 20 20 20 20 2a 2a 20 69 6e 73 74 65 ary. ** inste
1bad3 61 64 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 ad of an 8-byte
1bad4 6f 6e 65 2e 20 54 68 69 73 20 61 6c 6c 20 77 6f one. This all wo
1bad5 72 6b 73 20 66 69 6e 65 2c 20 65 78 63 65 70 74 rks fine, except
1bad6 20 74 68 61 74 20 77 68 65 6e 0a 20 20 20 20 2a that when. *
1bad7 2a 20 72 75 6e 6e 69 6e 67 20 77 69 74 68 20 53 * running with S
1bad8 51 4c 49 54 45 5f 44 45 42 55 47 20 64 65 66 69 QLITE_DEBUG defi
1bad9 6e 65 64 20 74 68 65 20 53 51 4c 69 74 65 20 63 ned the SQLite c
1bada 6f 64 65 20 73 6f 6d 65 74 69 6d 65 73 20 61 73 ode sometimes as
1badb 73 65 72 74 28 29 73 0a 20 20 20 20 2a 2a 20 74 sert()s. ** t
1badc 68 61 74 20 61 20 4d 65 6d 20 73 74 72 75 63 74 hat a Mem struct
1badd 75 72 65 20 69 73 20 6c 6f 63 61 74 65 64 20 6f ure is located o
1bade 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e n an 8-byte boun
1badf 64 61 72 79 2e 20 54 6f 20 70 72 65 76 65 6e 74 dary. To prevent
1bae0 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 61 73 73 . ** this ass
1bae1 65 72 74 28 29 20 66 72 6f 6d 20 66 61 69 6c 69 ert() from faili
1bae2 6e 67 2c 20 77 68 65 6e 20 62 75 69 6c 64 69 6e ng, when buildin
1bae3 67 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 g with SQLITE_DE
1bae4 42 55 47 20 64 65 66 69 6e 65 64 0a 20 20 20 20 BUG defined.
1bae5 2a 2a 20 75 73 69 6e 67 20 67 63 63 2c 20 66 6f ** using gcc, fo
1bae6 72 63 65 20 6e 75 6c 6c 4d 65 6d 20 74 6f 20 62 rce nullMem to b
1bae7 65 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 e 8-byte aligned
1bae8 20 75 73 69 6e 67 20 74 68 65 20 6d 61 67 69 63 using the magic
1bae9 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f 61 74 74 72 al. ** __attr
1baea 69 62 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 ibute__((aligned
1baeb 28 38 29 29 29 20 6d 61 63 72 6f 2e 20 20 2a 2f (8))) macro. */
1baec 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 . static cons
1baed 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65 6d 20 0a 23 t Mem nullMem .#
1baee 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
1baef 45 5f 44 45 42 55 47 29 20 26 26 20 64 65 66 69 E_DEBUG) && defi
1baf0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 20 20 ned(__GNUC__).
1baf1 20 20 20 20 5f 5f 61 74 74 72 69 62 75 74 65 5f __attribute_
1baf2 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29 20 _((aligned(8)))
1baf3 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 3d 20 .#endif. =
1baf4 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c 65 29 30 2c {{0}, (double)0,
1baf5 20 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 0, "", 0, MEM_N
1baf6 75 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c ull, SQLITE_NULL
1baf7 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 20 20 , 0, 0, 0 };..
1baf8 20 20 69 66 28 20 70 56 6d 20 26 26 20 41 4c 57 if( pVm && ALW
1baf9 41 59 53 28 70 56 6d 2d 3e 64 62 29 20 29 7b 0a AYS(pVm->db) ){.
1bafa 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 sqlite3_mu
1bafb 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 tex_enter(pVm->d
1bafc 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 b->mutex);.
1bafd 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 sqlite3Error(pV
1bafe 6d 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 m->db, SQLITE_RA
1baff 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 NGE, 0);. }.
1bb00 20 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 pOut = (Mem*)
1bb01 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 &nullMem;. }.
1bb02 72 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a return pOut;.}..
1bb03 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 /*.** This funct
1bb04 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 ion is called af
1bb05 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 ter invoking an
1bb06 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 sqlite3_value_XX
1bb07 58 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 X function on a
1bb08 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 .** column value
1bb09 20 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 (i.e. a value r
1bb0a 65 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 eturned by evalu
1bb0b 61 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 ating an SQL exp
1bb0c 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a ression in the.*
1bb0d 2a 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 * select list of
1bb0e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d a SELECT statem
1bb0f 65 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 ent) that may ca
1bb10 75 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 use a malloc() f
1bb11 61 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d ailure. If .** m
1bb12 61 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c alloc() has fail
1bb13 65 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 ed, the threads
1bb14 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 mallocFailed fla
1bb15 67 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 g is cleared and
1bb16 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 the result.** c
1bb17 6f 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 ode of statement
1bb18 20 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 pStmt set to SQ
1bb19 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a LITE_NOMEM..**.*
1bb1a 2a 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 * Specifically,
1bb1b 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 this is called f
1bb1c 72 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a rom within:.**.*
1bb1d 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f * sqlite3_co
1bb1e 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 lumn_int().**
1bb1f 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e sqlite3_column
1bb20 5f 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 _int64().**
1bb21 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 sqlite3_column_t
1bb22 65 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c ext().** sql
1bb23 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 ite3_column_text
1bb24 31 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 16().** sqli
1bb25 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 te3_column_real(
1bb26 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 ).** sqlite3
1bb27 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a _column_bytes().
1bb28 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 ** sqlite3_c
1bb29 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a olumn_bytes16().
1bb2a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f **.** But not fo
1bb2b 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e r sqlite3_column
1bb2c 5f 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e _blob(), which n
1bb2d 65 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f ever calls mallo
1bb2e 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 c()..*/.static v
1bb2f 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 oid columnMalloc
1bb30 46 61 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f Failure(sqlite3_
1bb31 73 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 stmt *pStmt).{.
1bb32 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 /* If malloc()
1bb33 66 61 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e failed during an
1bb34 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 encoding conver
1bb35 73 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 sion within an.
1bb36 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** sqlite3_colu
1bb37 6d 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e mn_XXX API, then
1bb38 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 set the return
1bb39 63 6f 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 code of the stat
1bb3a 65 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 ement to. ** SQ
1bb3b 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 LITE_NOMEM. The
1bb3c 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 next call to _st
1bb3d 65 70 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 ep() (if any) wi
1bb3e 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ll return SQLITE
1bb3f 5f 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 _ERROR. ** and
1bb40 5f 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c _finalize() will
1bb41 20 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 return NOMEM..
1bb42 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 */. Vdbe *p =
1bb43 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1bb44 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d if( p ){. p-
1bb45 3e 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 >rc = sqlite3Api
1bb46 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 Exit(p->db, p->r
1bb47 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f c);. sqlite3_
1bb48 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1bb49 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d b->mutex);. }.}
1bb4a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ../*************
1bb4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 ***************
1bb4c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 sqlite3_column_
1bb4d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***************
1bb4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bb4f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e .** The followin
1bb50 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 g routines are u
1bb51 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c sed to access el
1bb52 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 ements of the cu
1bb53 72 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 rrent row.** in
1bb54 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a the result set..
1bb55 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f */.SQLITE_API co
1bb56 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 nst void *sqlite
1bb57 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 3_column_blob(sq
1bb58 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bb59 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e t, int i){. con
1bb5a 73 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 st void *val;.
1bb5b 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 val = sqlite3_va
1bb5c 6c 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e lue_blob( column
1bb5d 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a Mem(pStmt,i) );.
1bb5e 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 /* Even though
1bb5f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 there is no enc
1bb60 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e oding conversion
1bb61 2c 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d , value_blob() m
1bb62 69 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 ight. ** need t
1bb63 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 o call malloc()
1bb64 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 to expand the re
1bb65 73 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c sult of a zerobl
1bb66 6f 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 ob() . ** expre
1bb67 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 ssion. . */. c
1bb68 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1bb69 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1bb6a 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 urn val;.}.SQLIT
1bb6b 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bb6c 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 3_column_bytes(s
1bb6d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bb6e 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e mt, int i){. in
1bb6f 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f t val = sqlite3_
1bb70 76 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c value_bytes( col
1bb71 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1bb72 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1bb73 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1bb74 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d . return val;.}
1bb75 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1bb76 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 sqlite3_column_b
1bb77 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 ytes16(sqlite3_s
1bb78 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bb79 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 i){. int val =
1bb7a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 sqlite3_value_by
1bb7b 74 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d tes16( columnMem
1bb7c 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1bb7d 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1bb7e 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1bb7f 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 urn val;.}.SQLIT
1bb80 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c E_API double sql
1bb81 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 ite3_column_doub
1bb82 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 le(sqlite3_stmt
1bb83 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a *pStmt, int i){.
1bb84 20 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 double val = s
1bb85 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 qlite3_value_dou
1bb86 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 ble( columnMem(p
1bb87 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1bb88 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1bb89 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1bb8a 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1bb8b 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1bb8c 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 column_int(sqlit
1bb8d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1bb8e 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 int i){. int va
1bb8f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 l = sqlite3_valu
1bb90 65 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d e_int( columnMem
1bb91 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 (pStmt,i) );. c
1bb92 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 olumnMallocFailu
1bb93 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 re(pStmt);. ret
1bb94 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 urn val;.}.SQLIT
1bb95 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 E_API sqlite_int
1bb96 36 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 64 sqlite3_colum
1bb97 6e 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f n_int64(sqlite3_
1bb98 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bb99 20 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e i){. sqlite_in
1bb9a 74 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 t64 val = sqlite
1bb9b 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 3_value_int64( c
1bb9c 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 olumnMem(pStmt,i
1bb9d 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c ) );. columnMal
1bb9e 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 locFailure(pStmt
1bb9f 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b );. return val;
1bba0 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f .}.SQLITE_API co
1bba1 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 nst unsigned cha
1bba2 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d r *sqlite3_colum
1bba3 6e 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 n_text(sqlite3_s
1bba4 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bba5 69 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 i){. const unsi
1bba6 67 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d gned char *val =
1bba7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 sqlite3_value_t
1bba8 65 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 ext( columnMem(p
1bba9 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c Stmt,i) );. col
1bbaa 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 umnMallocFailure
1bbab 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 (pStmt);. retur
1bbac 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f n val;.}.SQLITE_
1bbad 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 API sqlite3_valu
1bbae 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d e *sqlite3_colum
1bbaf 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f n_value(sqlite3_
1bbb0 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bbb1 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 i){. Mem *pOut
1bbb2 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 = columnMem(pSt
1bbb3 6d 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f mt, i);. if( pO
1bbb4 75 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 ut->flags&MEM_St
1bbb5 61 74 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 atic ){. pOut
1bbb6 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f ->flags &= ~MEM_
1bbb7 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 Static;. pOut
1bbb8 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 ->flags |= MEM_E
1bbb9 70 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 phem;. }. colu
1bbba 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 mnMallocFailure(
1bbbb 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e pStmt);. return
1bbbc 20 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 (sqlite3_value
1bbbd 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 *)pOut;.}.#ifnde
1bbbe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
1bbbf 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 F16.SQLITE_API c
1bbc0 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1bbc1 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 e3_column_text16
1bbc2 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 (sqlite3_stmt *p
1bbc3 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 Stmt, int i){.
1bbc4 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 const void *val
1bbc5 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f = sqlite3_value_
1bbc6 74 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 text16( columnMe
1bbc7 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 m(pStmt,i) );.
1bbc8 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c columnMallocFail
1bbc9 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 ure(pStmt);. re
1bbca 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 turn val;.}.#end
1bbcb 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bbcc 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 T_UTF16 */.SQLIT
1bbcd 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bbce 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 3_column_type(sq
1bbcf 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bbd0 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 t, int i){. int
1bbd1 20 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 iType = sqlite3
1bbd2 5f 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c _value_type( col
1bbd3 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 umnMem(pStmt,i)
1bbd4 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f );. columnMallo
1bbd5 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b cFailure(pStmt);
1bbd6 0a 20 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b . return iType;
1bbd7 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f .}../* The follo
1bbd8 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 wing function is
1bbd9 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e experimental an
1bbda 64 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 d subject to cha
1bbdb 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 nge or.** remova
1bbdc 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 l */./*int sqlit
1bbdd 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 e3_column_numeri
1bbde 63 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 c_type(sqlite3_s
1bbdf 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bbe0 69 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 i){.** return s
1bbe1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d qlite3_value_num
1bbe2 65 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d eric_type( colum
1bbe3 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b nMem(pStmt,i) );
1bbe4 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 .**}.*/../*.** C
1bbe5 6f 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 onvert the N-th
1bbe6 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 element of pStmt
1bbe7 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 ->pColName[] int
1bbe8 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 o a string using
1bbe9 0a 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e .** xFunc() then
1bbea 20 72 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 return that str
1bbeb 69 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 ing. If N is ou
1bbec 74 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 t of range, retu
1bbed 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 rn 0..**.** Ther
1bbee 65 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 e are up to 5 na
1bbef 6d 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c mes for each col
1bbf0 75 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 umn. useType de
1bbf1 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a termines which.*
1bbf2 2a 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e * name is return
1bbf3 65 64 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 ed. Here are th
1bbf4 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 e names:.**.**
1bbf5 20 20 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 0 The col
1bbf6 75 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 umn name as it s
1bbf7 68 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 hould be display
1bbf8 65 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a ed for output.**
1bbf9 20 20 20 20 31 20 20 20 20 20 20 54 68 65 20 64 1 The d
1bbfa 61 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 atatype name for
1bbfb 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 the column.**
1bbfc 20 20 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 2 The nam
1bbfd 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 e of the databas
1bbfe 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d e that the colum
1bbff 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a n derives from.*
1bc00 2a 20 20 20 20 33 20 20 20 20 20 20 54 68 65 20 * 3 The
1bc01 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1bc02 65 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d e that the colum
1bc03 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a n derives from.*
1bc04 2a 20 20 20 20 34 20 20 20 20 20 20 54 68 65 20 * 4 The
1bc05 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c name of the tabl
1bc06 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 e column that th
1bc07 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 e result column
1bc08 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a derives from.**.
1bc09 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 ** If the result
1bc0a 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 is not a simple
1bc0b 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 column referenc
1bc0c 65 20 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 e (if it is an e
1bc0d 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 xpression.** or
1bc0e 61 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e a constant) then
1bc0f 20 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 useTypes 2, 3,
1bc10 61 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c and 4 return NUL
1bc11 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e L..*/.static con
1bc12 73 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e st void *columnN
1bc13 61 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 ame(. sqlite3_s
1bc14 74 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e tmt *pStmt,. in
1bc15 74 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 t N,. const voi
1bc16 64 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a d *(*xFunc)(Mem*
1bc17 29 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 ),. int useType
1bc18 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 .){. const void
1bc19 20 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 *ret = 0;. Vdb
1bc1a 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 e *p = (Vdbe *)p
1bc1b 53 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 Stmt;. int n;.
1bc1c 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
1bc1d 2d 3e 64 62 3b 0a 20 20 0a 20 20 61 73 73 65 72 ->db;. . asser
1bc1e 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 6e 20 t( db!=0 );. n
1bc1f 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e = sqlite3_column
1bc20 5f 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 _count(pStmt);.
1bc21 20 69 66 28 20 4e 3c 6e 20 26 26 20 4e 3e 3d 30 if( N<n && N>=0
1bc22 20 29 7b 0a 20 20 20 20 4e 20 2b 3d 20 75 73 65 ){. N += use
1bc23 54 79 70 65 2a 6e 3b 0a 20 20 20 20 73 71 6c 69 Type*n;. sqli
1bc24 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1bc25 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 db->mutex);.
1bc26 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c assert( db->mall
1bc27 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 ocFailed==0 );.
1bc28 20 20 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 ret = xFunc(&
1bc29 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b p->aColName[N]);
1bc2a 0a 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f . /* A mallo
1bc2b 63 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 c may have faile
1bc2c 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 d inside of the
1bc2d 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66 xFunc() call. If
1bc2e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 this. ** is
1bc2f 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61 72 20 the case, clear
1bc30 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 the mallocFailed
1bc31 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75 72 6e flag and return
1bc32 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 NULL.. */.
1bc33 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1bc34 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 Failed ){.
1bc35 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1bc36 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 20 = 0;. ret
1bc37 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 = 0;. }. s
1bc38 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bc39 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 ve(db->mutex);.
1bc3a 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 74 3b }. return ret;
1bc3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1bc3c 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 the name of the
1bc3d 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 Nth column of t
1bc3e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72 65 he result set re
1bc3f 74 75 72 6e 65 64 20 62 79 20 53 51 4c 0a 2a 2a turned by SQL.**
1bc40 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 statement pStmt
1bc41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bc42 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
1bc43 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 te3_column_name(
1bc44 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1bc45 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 tmt, int N){. r
1bc46 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 eturn columnName
1bc47 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e (. pStmt, N
1bc48 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a , (const void*(*
1bc49 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f )(Mem*))sqlite3_
1bc4a 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e value_text, COLN
1bc4b 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 69 66 AME_NAME);.}.#if
1bc4c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1bc4d 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 _UTF16.SQLITE_AP
1bc4e 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 I const void *sq
1bc4f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d lite3_column_nam
1bc50 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 e16(sqlite3_stmt
1bc51 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b *pStmt, int N){
1bc52 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e . return column
1bc53 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d Name(. pStm
1bc54 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 t, N, (const voi
1bc55 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 d*(*)(Mem*))sqli
1bc56 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 te3_value_text16
1bc57 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b , COLNAME_NAME);
1bc58 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1bc59 20 43 6f 6e 73 74 72 61 69 6e 74 3a 20 20 49 66 Constraint: If
1bc5a 20 79 6f 75 20 68 61 76 65 20 45 4e 41 42 4c 45 you have ENABLE
1bc5b 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 _COLUMN_METADATA
1bc5c 20 74 68 65 6e 20 79 6f 75 20 6d 75 73 74 0a 2a then you must.*
1bc5d 2a 20 6e 6f 74 20 64 65 66 69 6e 65 20 4f 4d 49 * not define OMI
1bc5e 54 5f 44 45 43 4c 54 59 50 45 2e 0a 2a 2f 0a 23 T_DECLTYPE..*/.#
1bc5f 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 if defined(SQLIT
1bc60 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 E_OMIT_DECLTYPE)
1bc61 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 && defined(SQLI
1bc62 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e TE_ENABLE_COLUMN
1bc63 5f 4d 45 54 41 44 41 54 41 29 0a 23 20 65 72 72 _METADATA).# err
1bc64 6f 72 20 22 4d 75 73 74 20 6e 6f 74 20 64 65 66 or "Must not def
1bc65 69 6e 65 20 62 6f 74 68 20 53 51 4c 49 54 45 5f ine both SQLITE_
1bc66 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 5c 0a OMIT_DECLTYPE \.
1bc67 20 20 20 20 20 20 20 20 20 61 6e 64 20 53 51 4c and SQL
1bc68 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d ITE_ENABLE_COLUM
1bc69 4e 5f 4d 45 54 41 44 41 54 41 22 0a 23 65 6e 64 N_METADATA".#end
1bc6a 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 if..#ifndef SQLI
1bc6b 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 TE_OMIT_DECLTYPE
1bc6c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bc6d 65 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 e column declara
1bc6e 74 69 6f 6e 20 74 79 70 65 20 28 69 66 20 61 70 tion type (if ap
1bc6f 70 6c 69 63 61 62 6c 65 29 20 6f 66 20 74 68 65 plicable) of the
1bc70 20 27 69 27 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 'i'th column.**
1bc71 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 of the result s
1bc72 65 74 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d et of SQL statem
1bc73 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 ent pStmt..*/.SQ
1bc74 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
1bc75 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
1bc76 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c umn_decltype(sql
1bc77 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bc78 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bc79 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bc7a 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bc7b 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bc7c 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bc7d 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 ue_text, COLNAME
1bc7e 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 69 _DECLTYPE);.}.#i
1bc7f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1bc80 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 T_UTF16.SQLITE_A
1bc81 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 PI const void *s
1bc82 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 qlite3_column_de
1bc83 63 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 cltype16(sqlite3
1bc84 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1bc85 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 t N){. return c
1bc86 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 olumnName(.
1bc87 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 pStmt, N, (cons
1bc88 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 t void*(*)(Mem*)
1bc89 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1bc8a 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 ext16, COLNAME_D
1bc8b 45 43 4c 54 59 50 45 29 3b 0a 7d 0a 23 65 6e 64 ECLTYPE);.}.#end
1bc8c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1bc8d 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 T_UTF16 */.#endi
1bc8e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1bc8f 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a 0a 23 69 _DECLTYPE */..#i
1bc90 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
1bc91 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 LE_COLUMN_METADA
1bc92 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 TA./*.** Return
1bc93 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1bc94 64 61 74 61 62 61 73 65 20 66 72 6f 6d 20 77 68 database from wh
1bc95 69 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c ich a result col
1bc96 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 umn derives..**
1bc97 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 NULL is returned
1bc98 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 if the result c
1bc99 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 olumn is an expr
1bc9a 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 ession or consta
1bc9b 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e nt or.** anythin
1bc9c 67 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 g else which is
1bc9d 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 not an unabiguou
1bc9e 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 s reference to a
1bc9f 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e database column
1bca0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bca1 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 const char *sqli
1bca2 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 te3_column_datab
1bca3 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 ase_name(sqlite3
1bca4 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e _stmt *pStmt, in
1bca5 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 t N){. return c
1bca6 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 olumnName(.
1bca7 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 pStmt, N, (cons
1bca8 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 t void*(*)(Mem*)
1bca9 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 )sqlite3_value_t
1bcaa 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 ext, COLNAME_DAT
1bcab 41 42 41 53 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 ABASE);.}.#ifnde
1bcac 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 f SQLITE_OMIT_UT
1bcad 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 F16.SQLITE_API c
1bcae 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 onst void *sqlit
1bcaf 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 e3_column_databa
1bcb0 73 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 se_name16(sqlite
1bcb1 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 3_stmt *pStmt, i
1bcb2 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 nt N){. return
1bcb3 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 columnName(.
1bcb4 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e pStmt, N, (con
1bcb5 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a st void*(*)(Mem*
1bcb6 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f ))sqlite3_value_
1bcb7 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f text16, COLNAME_
1bcb8 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 65 6e DATABASE);.}.#en
1bcb9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1bcba 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a IT_UTF16 */../*.
1bcbb 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 ** Return the na
1bcbc 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 me of the table
1bcbd 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 from which a res
1bcbe 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 ult column deriv
1bcbf 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 es..** NULL is r
1bcc0 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 eturned if the r
1bcc1 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 esult column is
1bcc2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 an expression or
1bcc3 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 constant or.**
1bcc4 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 anything else wh
1bcc5 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e ich is not an un
1bcc6 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e abiguous referen
1bcc7 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 ce to a database
1bcc8 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 column..*/.SQLI
1bcc9 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 TE_API const cha
1bcca 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d r *sqlite3_colum
1bccb 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c n_table_name(sql
1bccc 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bccd 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bcce 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bccf 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bcd0 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bcd1 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bcd2 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 ue_text, COLNAME
1bcd3 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 69 66 6e 64 _TABLE);.}.#ifnd
1bcd4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 ef SQLITE_OMIT_U
1bcd5 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 TF16.SQLITE_API
1bcd6 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 const void *sqli
1bcd7 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 te3_column_table
1bcd8 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f _name16(sqlite3_
1bcd9 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 stmt *pStmt, int
1bcda 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f N){. return co
1bcdb 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 lumnName(.
1bcdc 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 pStmt, N, (const
1bcdd 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 void*(*)(Mem*))
1bcde 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 sqlite3_value_te
1bcdf 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 xt16, COLNAME_TA
1bce0 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f BLE);.}.#endif /
1bce1 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 * SQLITE_OMIT_UT
1bce2 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 F16 */../*.** Re
1bce3 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 turn the name of
1bce4 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d the table colum
1bce5 6e 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 72 n from which a r
1bce6 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 esult column der
1bce7 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 ives..** NULL is
1bce8 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 returned if the
1bce9 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 result column i
1bcea 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 s an expression
1bceb 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a or constant or.*
1bcec 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 * anything else
1bced 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 which is not an
1bcee 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66 65 72 unabiguous refer
1bcef 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61 62 61 ence to a databa
1bcf0 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 se column..*/.SQ
1bcf1 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 LITE_API const c
1bcf2 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c har *sqlite3_col
1bcf3 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 28 umn_origin_name(
1bcf4 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 sqlite3_stmt *pS
1bcf5 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 tmt, int N){. r
1bcf6 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 eturn columnName
1bcf7 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e (. pStmt, N
1bcf8 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a , (const void*(*
1bcf9 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f )(Mem*))sqlite3_
1bcfa 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e value_text, COLN
1bcfb 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 AME_COLUMN);.}.#
1bcfc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1bcfd 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f IT_UTF16.SQLITE_
1bcfe 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a API const void *
1bcff 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f sqlite3_column_o
1bd00 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c rigin_name16(sql
1bd01 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bd02 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 , int N){. retu
1bd03 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 rn columnName(.
1bd04 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 pStmt, N, (
1bd05 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d const void*(*)(M
1bd06 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c em*))sqlite3_val
1bd07 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 ue_text16, COLNA
1bd08 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 ME_COLUMN);.}.#e
1bd09 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1bd0a 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23 65 6e MIT_UTF16 */.#en
1bd0b 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
1bd0c 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 ABLE_COLUMN_META
1bd0d 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a DATA */.../*****
1bd0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 ********** sqlit
1bd10 65 33 5f 62 69 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a e3_bind_ ******
1bd11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bd12 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 *****.** .** Rou
1bd13 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 61 74 tines used to at
1bd14 74 61 63 68 20 76 61 6c 75 65 73 20 74 6f 20 77 tach values to w
1bd15 69 6c 64 63 61 72 64 73 20 69 6e 20 61 20 63 6f ildcards in a co
1bd16 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 mpiled SQL state
1bd17 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 ment..*/./*.** U
1bd18 6e 62 69 6e 64 20 74 68 65 20 76 61 6c 75 65 20 nbind the value
1bd19 62 6f 75 6e 64 20 74 6f 20 76 61 72 69 61 62 6c bound to variabl
1bd1a 65 20 69 20 69 6e 20 76 69 72 74 75 61 6c 20 6d e i in virtual m
1bd1b 61 63 68 69 6e 65 20 70 2e 20 54 68 69 73 20 69 achine p. This i
1bd1c 73 20 74 68 65 20 0a 2a 2a 20 74 68 65 20 73 61 s the .** the sa
1bd1d 6d 65 20 61 73 20 62 69 6e 64 69 6e 67 20 61 20 me as binding a
1bd1e 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 74 68 NULL value to th
1bd1f 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 e column. If the
1bd20 20 22 69 22 20 70 61 72 61 6d 65 74 65 72 20 69 "i" parameter i
1bd21 73 0a 2a 2a 20 6f 75 74 20 6f 66 20 72 61 6e 67 s.** out of rang
1bd22 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 52 e, then SQLITE_R
1bd23 41 4e 47 45 20 69 73 20 72 65 74 75 72 6e 65 64 ANGE is returned
1bd24 2e 20 4f 74 68 65 77 69 73 65 20 53 51 4c 49 54 . Othewise SQLIT
1bd25 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 75 E_OK..**.** A su
1bd26 63 63 65 73 73 66 75 6c 20 65 76 61 6c 75 61 74 ccessful evaluat
1bd27 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 ion of this rout
1bd28 69 6e 65 20 61 63 71 75 69 72 65 73 20 74 68 65 ine acquires the
1bd29 20 6d 75 74 65 78 20 6f 6e 20 70 2e 0a 2a 2a 20 mutex on p..**
1bd2a 74 68 65 20 6d 75 74 65 78 20 69 73 20 72 65 6c the mutex is rel
1bd2b 65 61 73 65 64 20 69 66 20 61 6e 79 20 6b 69 6e eased if any kin
1bd2c 64 20 6f 66 20 65 72 72 6f 72 20 6f 63 63 75 72 d of error occur
1bd2d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 72 72 s..**.** The err
1bd2e 6f 72 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 or code stored i
1bd2f 6e 20 64 61 74 61 62 61 73 65 20 70 2d 3e 64 62 n database p->db
1bd30 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 is overwritten
1bd31 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 0a with the return.
1bd32 2a 2a 20 76 61 6c 75 65 20 69 6e 20 61 6e 79 20 ** value in any
1bd33 63 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 case..*/.static
1bd34 69 6e 74 20 76 64 62 65 55 6e 62 69 6e 64 28 56 int vdbeUnbind(V
1bd35 64 62 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a dbe *p, int i){.
1bd36 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 Mem *pVar;. i
1bd37 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e f( p==0 ) return
1bd38 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a SQLITE_MISUSE;.
1bd39 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bd3a 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 enter(p->db->mut
1bd3b 65 78 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 ex);. if( p->ma
1bd3c 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f gic!=VDBE_MAGIC_
1bd3d 52 55 4e 20 7c 7c 20 70 2d 3e 70 63 3e 3d 30 20 RUN || p->pc>=0
1bd3e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
1bd3f 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 ror(p->db, SQLIT
1bd40 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 E_MISUSE, 0);.
1bd41 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f sqlite3_mutex_
1bd42 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 leave(p->db->mut
1bd43 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 ex);. return
1bd44 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 SQLITE_MISUSE;.
1bd45 20 7d 0a 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 }. if( i<1 ||
1bd46 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 i>p->nVar ){.
1bd47 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d sqlite3Error(p-
1bd48 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 >db, SQLITE_RANG
1bd49 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 E, 0);. sqlit
1bd4a 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 e3_mutex_leave(p
1bd4b 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 ->db->mutex);.
1bd4c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1bd4d 52 41 4e 47 45 3b 0a 20 20 7d 0a 20 20 69 2d 2d RANGE;. }. i--
1bd4e 3b 0a 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 ;. pVar = &p->a
1bd4f 56 61 72 5b 69 5d 3b 0a 20 20 73 71 6c 69 74 65 Var[i];. sqlite
1bd50 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1bd51 70 56 61 72 29 3b 0a 20 20 70 56 61 72 2d 3e 66 pVar);. pVar->f
1bd52 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b lags = MEM_Null;
1bd53 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 . sqlite3Error(
1bd54 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b p->db, SQLITE_OK
1bd55 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 , 0);.. /* If t
1bd56 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f 6e he bit correspon
1bd57 64 69 6e 67 20 74 6f 20 74 68 69 73 20 76 61 72 ding to this var
1bd58 69 61 62 6c 65 20 69 6e 20 56 64 62 65 2e 65 78 iable in Vdbe.ex
1bd59 70 6d 61 73 6b 20 69 73 20 73 65 74 2c 20 74 68 pmask is set, th
1bd5a 65 6e 20 0a 20 20 2a 2a 20 62 69 6e 64 69 6e 67 en . ** binding
1bd5b 20 61 20 6e 65 77 20 76 61 6c 75 65 20 74 6f 20 a new value to
1bd5c 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e this variable in
1bd5d 76 61 6c 69 64 61 74 65 73 20 74 68 65 20 63 75 validates the cu
1bd5e 72 72 65 6e 74 20 71 75 65 72 79 20 70 6c 61 6e rrent query plan
1bd5f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e .. */. if( p->
1bd60 69 73 50 72 65 70 61 72 65 56 32 20 26 26 0a 20 isPrepareV2 &&.
1bd61 20 20 20 20 28 28 69 3c 33 32 20 26 26 20 70 2d ((i<32 && p-
1bd62 3e 65 78 70 6d 61 73 6b 20 26 20 28 28 75 33 32 >expmask & ((u32
1bd63 29 31 20 3c 3c 20 69 29 29 20 7c 7c 20 70 2d 3e )1 << i)) || p->
1bd64 65 78 70 6d 61 73 6b 3d 3d 30 78 66 66 66 66 66 expmask==0xfffff
1bd65 66 66 66 29 0a 20 20 29 7b 0a 20 20 20 20 70 2d fff). ){. p-
1bd66 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 >expired = 1;.
1bd67 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 }. return SQLIT
1bd68 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 E_OK;.}../*.** B
1bd69 69 6e 64 20 61 20 74 65 78 74 20 6f 72 20 42 4c ind a text or BL
1bd6a 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 OB value..*/.sta
1bd6b 74 69 63 20 69 6e 74 20 62 69 6e 64 54 65 78 74 tic int bindText
1bd6c 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 (. sqlite3_stmt
1bd6d 20 2a 70 53 74 6d 74 2c 20 20 20 2f 2a 20 54 68 *pStmt, /* Th
1bd6e 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 e statement to b
1bd6f 69 6e 64 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 ind against */.
1bd70 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 int i,
1bd71 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
1bd72 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 of the paramete
1bd73 72 20 74 6f 20 62 69 6e 64 20 2a 2f 0a 20 20 63 r to bind */. c
1bd74 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61 onst void *zData
1bd75 2c 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 , /* Pointer
1bd76 20 74 6f 20 74 68 65 20 64 61 74 61 20 74 6f 20 to the data to
1bd77 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 69 6e be bound */. in
1bd78 74 20 6e 44 61 74 61 2c 20 20 20 20 20 20 20 20 t nData,
1bd79 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1bd7a 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 f bytes of data
1bd7b 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a 20 to be bound */.
1bd7c 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f void (*xDel)(vo
1bd7d 69 64 2a 29 2c 20 20 20 2f 2a 20 44 65 73 74 72 id*), /* Destr
1bd7e 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 64 61 uctor for the da
1bd7f 74 61 20 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 ta */. u8 encod
1bd80 69 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 2f ing /
1bd81 2a 20 45 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 * Encoding for t
1bd82 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 he data */.){.
1bd83 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 Vdbe *p = (Vdbe
1bd84 2a 29 70 53 74 6d 74 3b 0a 20 20 4d 65 6d 20 2a *)pStmt;. Mem *
1bd85 70 56 61 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a pVar;. int rc;.
1bd86 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 . rc = vdbeUnbi
1bd87 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 nd(p, i);. if(
1bd88 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b rc==SQLITE_OK ){
1bd89 0a 20 20 20 20 69 66 28 20 7a 44 61 74 61 21 3d . if( zData!=
1bd8a 30 20 29 7b 0a 20 20 20 20 20 20 70 56 61 72 20 0 ){. pVar
1bd8b 3d 20 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 3b = &p->aVar[i-1];
1bd8c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1bd8d 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 te3VdbeMemSetStr
1bd8e 28 70 56 61 72 2c 20 7a 44 61 74 61 2c 20 6e 44 (pVar, zData, nD
1bd8f 61 74 61 2c 20 65 6e 63 6f 64 69 6e 67 2c 20 78 ata, encoding, x
1bd90 44 65 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 Del);. if(
1bd91 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 rc==SQLITE_OK &&
1bd92 20 65 6e 63 6f 64 69 6e 67 21 3d 30 20 29 7b 0a encoding!=0 ){.
1bd93 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c rc = sql
1bd94 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e ite3VdbeChangeEn
1bd95 63 6f 64 69 6e 67 28 70 56 61 72 2c 20 45 4e 43 coding(pVar, ENC
1bd96 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 (p->db));.
1bd97 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 }. sqlite3E
1bd98 72 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 rror(p->db, rc,
1bd99 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 0);. rc = s
1bd9a 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d qlite3ApiExit(p-
1bd9b 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20 20 7d 0a >db, rc);. }.
1bd9c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1bd9d 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
1bd9e 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
1bd9f 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a urn rc;.}.../*.*
1bda0 2a 20 42 69 6e 64 20 61 20 62 6c 6f 62 20 76 61 * Bind a blob va
1bda1 6c 75 65 20 74 6f 20 61 6e 20 53 51 4c 20 73 74 lue to an SQL st
1bda2 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 atement variable
1bda3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 ..*/.SQLITE_API
1bda4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1bda5 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 _blob(. sqlite3
1bda6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 _stmt *pStmt, .
1bda7 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 int i, . const
1bda8 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 void *zData, .
1bda9 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 int nData, . v
1bdaa 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1bdab 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 *).){. return b
1bdac 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 indText(pStmt, i
1bdad 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 , zData, nData,
1bdae 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 xDel, 0);.}.SQLI
1bdaf 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1bdb0 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73 e3_bind_double(s
1bdb1 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 qlite3_stmt *pSt
1bdb2 6d 74 2c 20 69 6e 74 20 69 2c 20 64 6f 75 62 6c mt, int i, doubl
1bdb3 65 20 72 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 e rValue){. int
1bdb4 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d rc;. Vdbe *p =
1bdb5 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a (Vdbe *)pStmt;.
1bdb6 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e rc = vdbeUnbin
1bdb7 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 d(p, i);. if( r
1bdb8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1bdb9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1bdba 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 2d 3e emSetDouble(&p->
1bdbb 61 56 61 72 5b 69 2d 31 5d 2c 20 72 56 61 6c 75 aVar[i-1], rValu
1bdbc 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f e);. sqlite3_
1bdbd 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 mutex_leave(p->d
1bdbe 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 b->mutex);. }.
1bdbf 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 return rc;.}.SQ
1bdc0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c LITE_API int sql
1bdc1 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 ite3_bind_int(sq
1bdc2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 2c 20 69 lite3_stmt *p, i
1bdc3 6e 74 20 69 2c 20 69 6e 74 20 69 56 61 6c 75 65 nt i, int iValue
1bdc4 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 ){. return sqli
1bdc5 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 te3_bind_int64(p
1bdc6 2c 20 69 2c 20 28 69 36 34 29 69 56 61 6c 75 65 , i, (i64)iValue
1bdc7 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 );.}.SQLITE_API
1bdc8 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 int sqlite3_bind
1bdc9 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 _int64(sqlite3_s
1bdca 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 tmt *pStmt, int
1bdcb 69 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 i, sqlite_int64
1bdcc 69 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 iValue){. int r
1bdcd 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 c;. Vdbe *p = (
1bdce 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 Vdbe *)pStmt;.
1bdcf 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 rc = vdbeUnbind(
1bdd0 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d p, i);. if( rc=
1bdd1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1bdd2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1bdd3 53 65 74 49 6e 74 36 34 28 26 70 2d 3e 61 56 61 SetInt64(&p->aVa
1bdd4 72 5b 69 2d 31 5d 2c 20 69 56 61 6c 75 65 29 3b r[i-1], iValue);
1bdd5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 . sqlite3_mut
1bdd6 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e ex_leave(p->db->
1bdd7 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 mutex);. }. re
1bdd8 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 turn rc;.}.SQLIT
1bdd9 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bdda 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c 69 3_bind_null(sqli
1bddb 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bddc 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 72 int i){. int r
1bddd 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 c;. Vdbe *p = (
1bdde 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 Vdbe*)pStmt;. r
1bddf 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 c = vdbeUnbind(p
1bde0 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d , i);. if( rc==
1bde1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1bde2 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1bde3 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 eave(p->db->mute
1bde4 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e x);. }. return
1bde5 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 rc;.}.SQLITE_AP
1bde6 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 I int sqlite3_bi
1bde7 6e 64 5f 74 65 78 74 28 20 0a 20 20 73 71 6c 69 nd_text( . sqli
1bde8 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c te3_stmt *pStmt,
1bde9 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f . int i, . co
1bdea 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74 61 2c nst char *zData,
1bdeb 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a . int nData, .
1bdec 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 void (*xDel)(v
1bded 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 oid*).){. retur
1bdee 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74 n bindText(pStmt
1bdef 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 , i, zData, nDat
1bdf0 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f a, xDel, SQLITE_
1bdf1 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 UTF8);.}.#ifndef
1bdf2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1bdf3 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 16.SQLITE_API in
1bdf4 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 t sqlite3_bind_t
1bdf5 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74 65 33 ext16(. sqlite3
1bdf6 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 _stmt *pStmt, .
1bdf7 20 69 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 int i, . const
1bdf8 20 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 0a 20 void *zData, .
1bdf9 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 int nData, . v
1bdfa 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 oid (*xDel)(void
1bdfb 2a 29 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 *).){. return b
1bdfc 69 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 indText(pStmt, i
1bdfd 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 , zData, nData,
1bdfe 78 44 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 xDel, SQLITE_UTF
1bdff 31 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 23 65 6e 16NATIVE);.}.#en
1be00 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1be01 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 IT_UTF16 */.SQLI
1be02 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1be03 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73 71 e3_bind_value(sq
1be04 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1be05 74 2c 20 69 6e 74 20 69 2c 20 63 6f 6e 73 74 20 t, int i, const
1be06 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 sqlite3_value *p
1be07 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 Value){. int rc
1be08 3b 0a 20 20 73 77 69 74 63 68 28 20 70 56 61 6c ;. switch( pVal
1be09 75 65 2d 3e 74 79 70 65 20 29 7b 0a 20 20 20 20 ue->type ){.
1be0a 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 case SQLITE_INTE
1be0b 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 GER: {. rc
1be0c 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 = sqlite3_bind_i
1be0d 6e 74 36 34 28 70 53 74 6d 74 2c 20 69 2c 20 70 nt64(pStmt, i, p
1be0e 56 61 6c 75 65 2d 3e 75 2e 69 29 3b 0a 20 20 20 Value->u.i);.
1be0f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
1be10 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f case SQLITE_
1be11 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 FLOAT: {. r
1be12 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 c = sqlite3_bind
1be13 5f 64 6f 75 62 6c 65 28 70 53 74 6d 74 2c 20 69 _double(pStmt, i
1be14 2c 20 70 56 61 6c 75 65 2d 3e 72 29 3b 0a 20 20 , pValue->r);.
1be15 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
1be16 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 . case SQLITE
1be17 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 _BLOB: {. i
1be18 66 28 20 70 56 61 6c 75 65 2d 3e 66 6c 61 67 73 f( pValue->flags
1be19 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 & MEM_Zero ){.
1be1a 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 rc = sqli
1be1b 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f te3_bind_zeroblo
1be1c 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c b(pStmt, i, pVal
1be1d 75 65 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 ue->u.nZero);.
1be1e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1be1f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f rc = sqlite3_
1be20 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c bind_blob(pStmt,
1be21 20 69 2c 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 i, pValue->z, p
1be22 56 61 6c 75 65 2d 3e 6e 2c 53 51 4c 49 54 45 5f Value->n,SQLITE_
1be23 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 TRANSIENT);.
1be24 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b }. break;
1be25 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
1be26 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 SQLITE_TEXT: {.
1be27 20 20 20 20 20 72 63 20 3d 20 62 69 6e 64 54 65 rc = bindTe
1be28 78 74 28 70 53 74 6d 74 2c 69 2c 20 20 70 56 61 xt(pStmt,i, pVa
1be29 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e lue->z, pValue->
1be2a 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 n, SQLITE_TRANSI
1be2b 45 4e 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 ENT,.
1be2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1be2d 20 20 20 70 56 61 6c 75 65 2d 3e 65 6e 63 29 3b pValue->enc);
1be2e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1be2f 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
1be30 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 {. rc = sq
1be31 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 lite3_bind_null(
1be32 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 pStmt, i);.
1be33 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1be34 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1be35 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1be36 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 sqlite3_bind_zer
1be37 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 oblob(sqlite3_st
1be38 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 mt *pStmt, int i
1be39 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 , int n){. int
1be3a 72 63 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 rc;. Vdbe *p =
1be3b 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 (Vdbe *)pStmt;.
1be3c 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 rc = vdbeUnbind
1be3d 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20 72 63 (p, i);. if( rc
1be3e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1be3f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 sqlite3VdbeMe
1be40 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26 70 2d mSetZeroBlob(&p-
1be41 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 6e 29 3b 0a >aVar[i-1], n);.
1be42 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 sqlite3_mute
1be43 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d x_leave(p->db->m
1be44 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 utex);. }. ret
1be45 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
1be46 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 Return the numb
1be47 65 72 20 6f 66 20 77 69 6c 64 63 61 72 64 73 20 er of wildcards
1be48 74 68 61 74 20 63 61 6e 20 62 65 20 70 6f 74 65 that can be pote
1be49 6e 74 69 61 6c 6c 79 20 62 6f 75 6e 64 20 74 6f ntially bound to
1be4a 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
1be4b 65 20 69 73 20 61 64 64 65 64 20 74 6f 20 73 75 e is added to su
1be4c 70 70 6f 72 74 20 44 42 44 3a 3a 53 51 4c 69 74 pport DBD::SQLit
1be4d 65 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 e. .*/.SQLITE_A
1be4e 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 PI int sqlite3_b
1be4f 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f ind_parameter_co
1be50 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 unt(sqlite3_stmt
1be51 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65 *pStmt){. Vdbe
1be52 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 *p = (Vdbe*)pSt
1be53 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f mt;. return p ?
1be54 20 70 2d 3e 6e 56 61 72 20 3a 20 30 3b 0a 7d 0a p->nVar : 0;.}.
1be55 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 ./*.** Create a
1be56 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 76 61 72 mapping from var
1be57 69 61 62 6c 65 20 6e 75 6d 62 65 72 73 20 74 6f iable numbers to
1be58 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 73 0a variable names.
1be59 2a 2a 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 ** in the Vdbe.a
1be5a 7a 56 61 72 5b 5d 20 61 72 72 61 79 2c 20 69 66 zVar[] array, if
1be5b 20 73 75 63 68 20 61 20 6d 61 70 70 69 6e 67 20 such a mapping
1be5c 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 does not already
1be5d 0a 2a 2a 20 65 78 69 73 74 2e 0a 2a 2f 0a 73 74 .** exist..*/.st
1be5e 61 74 69 63 20 76 6f 69 64 20 63 72 65 61 74 65 atic void create
1be5f 56 61 72 4d 61 70 28 56 64 62 65 20 2a 70 29 7b VarMap(Vdbe *p){
1be60 0a 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56 61 72 . if( !p->okVar
1be61 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 ){. int j;.
1be62 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 Op *pOp;.
1be63 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e sqlite3_mutex_en
1be64 74 65 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ter(p->db->mutex
1be65 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 61 );. /* The ra
1be66 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68 65 72 ce condition her
1be67 65 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 e is harmless.
1be68 49 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 63 If two threads c
1be69 61 6c 6c 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 all this. **
1be6a 72 6f 75 74 69 6e 65 20 6f 6e 20 74 68 65 20 73 routine on the s
1be6b 61 6d 65 20 56 64 62 65 20 61 74 20 74 68 65 20 ame Vdbe at the
1be6c 73 61 6d 65 20 74 69 6d 65 2c 20 74 68 65 79 20 same time, they
1be6d 62 6f 74 68 20 6d 69 67 68 74 20 65 6e 64 0a 20 both might end.
1be6e 20 20 20 2a 2a 20 75 70 20 69 6e 69 74 69 61 6c ** up initial
1be6f 69 7a 69 6e 67 20 74 68 65 20 56 64 62 65 2e 61 izing the Vdbe.a
1be70 7a 56 61 72 5b 5d 20 61 72 72 61 79 2e 20 20 54 zVar[] array. T
1be71 68 61 74 20 69 73 20 61 20 6c 69 74 74 6c 65 20 hat is a little
1be72 65 78 74 72 61 0a 20 20 20 20 2a 2a 20 77 6f 72 extra. ** wor
1be73 6b 20 62 75 74 20 69 74 20 72 65 73 75 6c 74 73 k but it results
1be74 20 69 6e 20 74 68 65 20 73 61 6d 65 20 61 6e 73 in the same ans
1be75 77 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 wer.. */.
1be76 66 6f 72 28 6a 3d 30 2c 20 70 4f 70 3d 70 2d 3e for(j=0, pOp=p->
1be77 61 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a aOp; j<p->nOp; j
1be78 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 ++, pOp++){.
1be79 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 if( pOp->opcod
1be7a 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 20 29 e==OP_Variable )
1be7b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1be7c 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 ( pOp->p1>0 && p
1be7d 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 Op->p1<=p->nVar
1be7e 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 7a );. p->az
1be7f 56 61 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d Var[pOp->p1-1] =
1be80 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 pOp->p4.z;.
1be81 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d }. }. p-
1be82 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a 20 20 20 20 >okVar = 1;.
1be83 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1be84 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 ave(p->db->mutex
1be85 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 );. }.}../*.**
1be86 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 Return the name
1be87 6f 66 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 of a wildcard pa
1be88 72 61 6d 65 74 65 72 2e 20 20 52 65 74 75 72 6e rameter. Return
1be89 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 69 6e 64 NULL if the ind
1be8a 65 78 0a 2a 2a 20 69 73 20 6f 75 74 20 6f 66 20 ex.** is out of
1be8b 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 range or if the
1be8c 77 69 6c 64 63 61 72 64 20 69 73 20 75 6e 6e 61 wildcard is unna
1be8d 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 med..**.** The r
1be8e 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20 esult is always
1be8f 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 UTF-8..*/.SQLITE
1be90 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 _API const char
1be91 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 *sqlite3_bind_pa
1be92 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c rameter_name(sql
1be93 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1be94 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56 64 62 65 , int i){. Vdbe
1be95 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 *p = (Vdbe*)pSt
1be96 6d 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c mt;. if( p==0 |
1be97 7c 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 | i<1 || i>p->nV
1be98 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ar ){. return
1be99 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 0;. }. create
1be9a 56 61 72 4d 61 70 28 70 29 3b 0a 20 20 72 65 74 VarMap(p);. ret
1be9b 75 72 6e 20 70 2d 3e 61 7a 56 61 72 5b 69 2d 31 urn p->azVar[i-1
1be9c 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 ];.}../*.** Give
1be9d 6e 20 61 20 77 69 6c 64 63 61 72 64 20 70 61 72 n a wildcard par
1be9e 61 6d 65 74 65 72 20 6e 61 6d 65 2c 20 72 65 74 ameter name, ret
1be9f 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 urn the index of
1bea0 20 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a the variable.**
1bea1 20 77 69 74 68 20 74 68 61 74 20 6e 61 6d 65 2e with that name.
1bea2 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f If there is no
1bea3 20 76 61 72 69 61 62 6c 65 20 77 69 74 68 20 74 variable with t
1bea4 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2c 0a 2a he given name,.*
1bea5 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 * return 0..*/.S
1bea6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 QLITE_API int sq
1bea7 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d lite3_bind_param
1bea8 65 74 65 72 5f 69 6e 64 65 78 28 73 71 6c 69 74 eter_index(sqlit
1bea9 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 e3_stmt *pStmt,
1beaa 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d const char *zNam
1beab 65 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 e){. Vdbe *p =
1beac 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 (Vdbe*)pStmt;.
1bead 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d int i;. if( p==
1beae 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 0 ){. return
1beaf 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65 56 0;. }. createV
1beb0 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66 28 arMap(p); . if(
1beb1 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f zName ){. fo
1beb2 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61 72 r(i=0; i<p->nVar
1beb3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f ; i++){. co
1beb4 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 2d nst char *z = p-
1beb5 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20 20 >azVar[i];.
1beb6 20 69 66 28 20 7a 20 26 26 20 73 74 72 63 6d 70 if( z && strcmp
1beb7 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a (z,zName)==0 ){.
1beb8 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 return i
1beb9 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 +1;. }.
1beba 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 }. }. return 0
1bebb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 ;.}../*.** Trans
1bebc 66 65 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 fer all bindings
1bebd 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 from the first
1bebe 73 74 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 statement over t
1bebf 6f 20 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f o the second..*/
1bec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1bec1 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 int sqlite3Trans
1bec2 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 ferBindings(sqli
1bec3 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 te3_stmt *pFromS
1bec4 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d tmt, sqlite3_stm
1bec5 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 t *pToStmt){. V
1bec6 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 dbe *pFrom = (Vd
1bec7 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 be*)pFromStmt;.
1bec8 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 Vdbe *pTo = (Vd
1bec9 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 be*)pToStmt;. i
1beca 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
1becb 70 54 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e pTo->db==pFrom->
1becc 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 db );. assert(
1becd 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d pTo->nVar==pFrom
1bece 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 ->nVar );. sqli
1becf 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 te3_mutex_enter(
1bed0 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b pTo->db->mutex);
1bed1 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 . for(i=0; i<pF
1bed2 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b rom->nVar; i++){
1bed3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1bed4 4d 65 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 MemMove(&pTo->aV
1bed5 61 72 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 ar[i], &pFrom->a
1bed6 56 61 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 Var[i]);. }. s
1bed7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1bed8 76 65 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 ve(pTo->db->mute
1bed9 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c x);. return SQL
1beda 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 ITE_OK;.}..#ifnd
1bedb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 ef SQLITE_OMIT_D
1bedc 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 EPRECATED./*.**
1bedd 44 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 Deprecated exter
1bede 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 nal interface.
1bedf 49 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 Internal/core SQ
1bee0 4c 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f Lite code.** sho
1bee1 75 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 uld call sqlite3
1bee2 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 TransferBindings
1bee3 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 ..**.** Is is mi
1bee4 73 75 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 suse to call thi
1bee5 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 s routine with s
1bee6 74 61 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 tatements from d
1bee7 69 66 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 ifferent.** data
1bee8 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 base connections
1bee9 2e 20 20 42 75 74 20 61 73 20 74 68 69 73 20 69 . But as this i
1beea 73 20 61 20 64 65 70 72 65 63 61 74 65 64 20 69 s a deprecated i
1beeb 6e 74 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 nterface, we.**
1beec 77 69 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 will not bother
1beed 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 to check for tha
1beee 74 20 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a t condition..**.
1beef 2a 2a 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 ** If the two st
1bef0 61 74 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e atements contain
1bef1 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d a different num
1bef2 62 65 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c ber of bindings,
1bef3 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 then.** an SQLI
1bef4 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 TE_ERROR is retu
1bef5 72 6e 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 rned. Nothing e
1bef6 6c 73 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 lse can go wrong
1bef7 2c 20 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a , so otherwise.*
1bef8 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 * SQLITE_OK is r
1bef9 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 eturned..*/.SQLI
1befa 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1befb 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 e3_transfer_bind
1befc 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d ings(sqlite3_stm
1befd 74 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 t *pFromStmt, sq
1befe 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 lite3_stmt *pToS
1beff 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 tmt){. Vdbe *pF
1bf00 72 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 rom = (Vdbe*)pFr
1bf01 6f 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a omStmt;. Vdbe *
1bf02 70 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f pTo = (Vdbe*)pTo
1bf03 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f Stmt;. if( pFro
1bf04 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 m->nVar!=pTo->nV
1bf05 61 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e ar ){. return
1bf06 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1bf07 20 7d 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73 }. if( pTo->is
1bf08 50 72 65 70 61 72 65 56 32 20 26 26 20 70 54 6f PrepareV2 && pTo
1bf09 2d 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 ->expmask ){.
1bf0a 20 70 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20 pTo->expired =
1bf0b 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72 1;. }. if( pFr
1bf0c 6f 6d 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 om->isPrepareV2
1bf0d 26 26 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73 && pFrom->expmas
1bf0e 6b 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e k ){. pFrom->
1bf0f 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d expired = 1;. }
1bf10 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 . return sqlite
1bf11 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 3TransferBinding
1bf12 73 28 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f s(pFromStmt, pTo
1bf13 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a Stmt);.}.#endif.
1bf14 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 ./*.** Return th
1bf15 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 e sqlite3* datab
1bf16 61 73 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 ase handle to wh
1bf17 69 63 68 20 74 68 65 20 70 72 65 70 61 72 65 64 ich the prepared
1bf18 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e statement given
1bf19 0a 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d .** in the argum
1bf1a 65 6e 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 ent belongs. Th
1bf1b 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 is is the same d
1bf1c 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 atabase handle t
1bf1d 68 61 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 hat was.** the f
1bf1e 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
1bf1f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 the sqlite3_pre
1bf20 70 61 72 65 28 29 20 74 68 61 74 20 77 61 73 20 pare() that was
1bf21 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a used to create.*
1bf22 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 * the statement
1bf23 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 in the first pla
1bf24 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 ce..*/.SQLITE_AP
1bf25 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 I sqlite3 *sqlit
1bf26 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c e3_db_handle(sql
1bf27 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 ite3_stmt *pStmt
1bf28 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d ){. return pStm
1bf29 74 20 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d t ? ((Vdbe*)pStm
1bf2a 74 29 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f t)->db : 0;.}../
1bf2b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f *.** Return a po
1bf2c 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 inter to the nex
1bf2d 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 t prepared state
1bf2e 6d 65 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74 ment after pStmt
1bf2f 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 associated.** w
1bf30 69 74 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e ith database con
1bf31 6e 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 nection pDb. If
1bf32 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 pStmt is NULL,
1bf33 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 return the first
1bf34 0a 2a 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 .** prepared sta
1bf35 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 tement for the d
1bf36 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
1bf37 6f 6e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c on. Return NULL
1bf38 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 if there.** are
1bf39 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c no more..*/.SQL
1bf3a 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f ITE_API sqlite3_
1bf3b 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 stmt *sqlite3_ne
1bf3c 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 xt_stmt(sqlite3
1bf3d 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 *pDb, sqlite3_st
1bf3e 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 mt *pStmt){. sq
1bf3f 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 lite3_stmt *pNex
1bf40 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 t;. sqlite3_mut
1bf41 65 78 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 ex_enter(pDb->mu
1bf42 74 65 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d tex);. if( pStm
1bf43 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 t==0 ){. pNex
1bf44 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d t = (sqlite3_stm
1bf45 74 2a 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 t*)pDb->pVdbe;.
1bf46 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 }else{. pNex
1bf47 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d t = (sqlite3_stm
1bf48 74 2a 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74 t*)((Vdbe*)pStmt
1bf49 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 )->pNext;. }.
1bf4a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 sqlite3_mutex_le
1bf4b 61 76 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b ave(pDb->mutex);
1bf4c 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b . return pNext;
1bf4d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
1bf4e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 the value of a
1bf4f 73 74 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 status counter f
1bf50 6f 72 20 61 20 70 72 65 70 61 72 65 64 20 73 74 or a prepared st
1bf51 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 atement.*/.SQLIT
1bf52 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 E_API int sqlite
1bf53 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 3_stmt_status(sq
1bf54 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d lite3_stmt *pStm
1bf55 74 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 t, int op, int r
1bf56 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 esetFlag){. Vdb
1bf57 65 20 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 e *pVdbe = (Vdbe
1bf58 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 *)pStmt;. int v
1bf59 20 3d 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 = pVdbe->aCount
1bf5a 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 er[op-1];. if(
1bf5b 72 65 73 65 74 46 6c 61 67 20 29 20 70 56 64 62 resetFlag ) pVdb
1bf5c 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 e->aCounter[op-1
1bf5d 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 ] = 0;. return
1bf5e 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a v;.}../*********
1bf5f 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 ***** End of vdb
1bf60 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a eapi.c *********
1bf61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf63 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
1bf64 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
1bf65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a vdbe.c ********
1bf66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf68 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
1bf69 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
1bf6a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
1bf6b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
1bf6c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
1bf6d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
1bf6e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
1bf6f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
1bf70 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
1bf71 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
1bf72 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
1bf73 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
1bf74 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
1bf75 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
1bf76 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
1bf77 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
1bf78 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
1bf79 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
1bf7a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
1bf7b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
1bf7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1bf7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1bf80 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 The code in this
1bf81 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 file implements
1bf82 20 65 78 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f execution metho
1bf83 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 d of the .** Vir
1bf84 74 75 61 6c 20 44 61 74 61 62 61 73 65 20 45 6e tual Database En
1bf85 67 69 6e 65 20 28 56 44 42 45 29 2e 20 20 41 20 gine (VDBE). A
1bf86 73 65 70 61 72 61 74 65 20 66 69 6c 65 20 28 22 separate file ("
1bf87 76 64 62 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 vdbeaux.c").** h
1bf88 61 6e 64 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 andles housekeep
1bf89 69 6e 67 20 64 65 74 61 69 6c 73 20 73 75 63 68 ing details such
1bf8a 20 61 73 20 63 72 65 61 74 69 6e 67 20 61 6e 64 as creating and
1bf8b 20 64 65 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 deleting.** VDB
1bf8c 45 20 69 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 E instances. Th
1bf8d 69 73 20 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c is file is solel
1bf8e 79 20 69 6e 74 65 72 65 73 74 65 64 20 69 6e 20 y interested in
1bf8f 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 executing.** the
1bf90 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a VDBE program..*
1bf91 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 *.** In the exte
1bf92 72 6e 61 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 rnal interface,
1bf93 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 an "sqlite3_stmt
1bf94 2a 22 20 69 73 20 61 6e 20 6f 70 61 71 75 65 20 *" is an opaque
1bf95 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 pointer.** to a
1bf96 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 VDBE..**.** The
1bf97 53 51 4c 20 70 61 72 73 65 72 20 67 65 6e 65 72 SQL parser gener
1bf98 61 74 65 73 20 61 20 70 72 6f 67 72 61 6d 20 77 ates a program w
1bf99 68 69 63 68 20 69 73 20 74 68 65 6e 20 65 78 65 hich is then exe
1bf9a 63 75 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 cuted by.** the
1bf9b 56 44 42 45 20 74 6f 20 64 6f 20 74 68 65 20 77 VDBE to do the w
1bf9c 6f 72 6b 20 6f 66 20 74 68 65 20 53 51 4c 20 73 ork of the SQL s
1bf9d 74 61 74 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 tatement. VDBE
1bf9e 70 72 6f 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a programs are .**
1bf9f 20 73 69 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d similar in form
1bfa0 20 74 6f 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e to assembly lan
1bfa1 67 75 61 67 65 2e 20 20 54 68 65 20 70 72 6f 67 guage. The prog
1bfa2 72 61 6d 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a ram consists of.
1bfa3 2a 2a 20 61 20 6c 69 6e 65 61 72 20 73 65 71 75 ** a linear sequ
1bfa4 65 6e 63 65 20 6f 66 20 6f 70 65 72 61 74 69 6f ence of operatio
1bfa5 6e 73 2e 20 20 45 61 63 68 20 6f 70 65 72 61 74 ns. Each operat
1bfa6 69 6f 6e 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 ion has an opcod
1bfa7 65 20 0a 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 e .** and 5 oper
1bfa8 61 6e 64 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 ands. Operands
1bfa9 50 31 2c 20 50 32 2c 20 61 6e 64 20 50 33 20 61 P1, P2, and P3 a
1bfaa 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 re integers. Op
1bfab 65 72 61 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 erand P4 .** is
1bfac 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 a null-terminate
1bfad 64 20 73 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 d string. Opera
1bfae 6e 64 20 50 35 20 69 73 20 61 6e 20 75 6e 73 69 nd P5 is an unsi
1bfaf 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 2e 0a gned character..
1bfb0 2a 2a 20 46 65 77 20 6f 70 63 6f 64 65 73 20 75 ** Few opcodes u
1bfb1 73 65 20 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 se all 5 operand
1bfb2 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 s..**.** Computa
1bfb3 74 69 6f 6e 20 72 65 73 75 6c 74 73 20 61 72 65 tion results are
1bfb4 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 stored on a set
1bfb5 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6e 75 of registers nu
1bfb6 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 mbered beginning
1bfb7 0a 2a 2a 20 77 69 74 68 20 31 20 61 6e 64 20 67 .** with 1 and g
1bfb8 6f 69 6e 67 20 75 70 20 74 6f 20 56 64 62 65 2e oing up to Vdbe.
1bfb9 6e 4d 65 6d 2e 20 20 45 61 63 68 20 72 65 67 69 nMem. Each regi
1bfba 73 74 65 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a ster can store.*
1bfbb 2a 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 * either an inte
1bfbc 67 65 72 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d ger, a null-term
1bfbd 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2c 20 61 inated string, a
1bfbe 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a floating point.
1bfbf 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 ** number, or th
1bfc0 65 20 53 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c e SQL "NULL" val
1bfc1 75 65 2e 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 ue. An implicit
1bfc2 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d conversion from
1bfc3 20 6f 6e 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 one.** type to
1bfc4 74 68 65 20 6f 74 68 65 72 20 6f 63 63 75 72 73 the other occurs
1bfc5 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a as necessary..*
1bfc6 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 * .** Most of th
1bfc7 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 e code in this f
1bfc8 69 6c 65 20 69 73 20 74 61 6b 65 6e 20 75 70 20 ile is taken up
1bfc9 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 by the sqlite3Vd
1bfca 62 65 45 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 beExec().** func
1bfcb 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73 20 tion which does
1bfcc 74 68 65 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 the work of inte
1bfcd 72 70 72 65 74 69 6e 67 20 61 20 56 44 42 45 20 rpreting a VDBE
1bfce 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 program..** But
1bfcf 6f 74 68 65 72 20 72 6f 75 74 69 6e 65 73 20 61 other routines a
1bfd0 72 65 20 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 re also provided
1bfd1 20 74 6f 20 68 65 6c 70 20 69 6e 20 62 75 69 6c to help in buil
1bfd2 64 69 6e 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f ding up.** a pro
1bfd3 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e gram instruction
1bfd4 20 62 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e by instruction.
1bfd5 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 .**.** Various s
1bfd6 63 72 69 70 74 73 20 73 63 61 6e 20 74 68 69 73 cripts scan this
1bfd7 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 6e 20 source file in
1bfd8 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 order to generat
1bfd9 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 e HTML.** docume
1bfda 6e 74 61 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 ntation, headers
1bfdb 20 66 69 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 files, or other
1bfdc 20 64 65 72 69 76 65 64 20 66 69 6c 65 73 2e 20 derived files.
1bfdd 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a The formatting.
1bfde 2a 2a 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 ** of the code i
1bfdf 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 2c 20 n this file is,
1bfe0 74 68 65 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 therefore, impor
1bfe1 74 61 6e 74 2e 20 20 53 65 65 20 6f 74 68 65 72 tant. See other
1bfe2 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 comments.** in
1bfe3 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 this file for de
1bfe4 74 61 69 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f tails. If in do
1bfe5 75 62 74 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 ubt, do not devi
1bfe6 61 74 65 20 66 72 6f 6d 20 65 78 69 73 74 69 6e ate from existin
1bfe7 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 g.** commenting
1bfe8 61 6e 64 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 and indentation
1bfe9 70 72 61 63 74 69 63 65 73 20 77 68 65 6e 20 63 practices when c
1bfea 68 61 6e 67 69 6e 67 20 6f 72 20 61 64 64 69 6e hanging or addin
1bfeb 67 20 63 6f 64 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a g code..*/../*.*
1bfec 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 * The following
1bfed 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1bfee 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1bfef 76 65 72 79 20 74 69 6d 65 20 61 20 63 75 72 73 very time a curs
1bff0 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 or.** moves, eit
1bff1 68 65 72 20 62 79 20 74 68 65 20 4f 50 5f 53 65 her by the OP_Se
1bff2 65 6b 58 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f ekXX, OP_Next, o
1bff3 72 20 4f 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 r OP_Prev opcode
1bff4 73 2e 20 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 s. The test.**
1bff5 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74 procedures use t
1bff6 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
1bff7 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 to make sure tha
1bff8 74 20 69 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a t indices are.**
1bff9 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 working correct
1bffa 6c 79 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 ly. This variab
1bffb 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 le has no functi
1bffc 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f on other than to
1bffd 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 .** help verify
1bffe 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 the correct oper
1bfff 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 ation of the lib
1c000 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 rary..*/.#ifdef
1c001 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 SQLITE_TEST.SQLI
1c002 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 TE_API int sqlit
1c003 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 e3_search_count
1c004 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a = 0;.#endif../*.
1c005 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 67 6c 6f ** When this glo
1c006 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 bal variable is
1c007 70 6f 73 69 74 69 76 65 2c 20 69 74 20 67 65 74 positive, it get
1c008 73 20 64 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e s decremented on
1c009 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 ce before.** eac
1c00a 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e h instruction in
1c00b 20 74 68 65 20 56 44 42 45 2e 20 20 57 68 65 6e the VDBE. When
1c00c 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 reaches zero, t
1c00d 68 65 20 75 31 2e 69 73 49 6e 74 65 72 72 75 70 he u1.isInterrup
1c00e 74 65 64 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 ted.** field of
1c00f 74 68 65 20 73 71 6c 69 74 65 33 20 73 74 72 75 the sqlite3 stru
1c010 63 74 75 72 65 20 69 73 20 73 65 74 20 69 6e 20 cture is set in
1c011 6f 72 64 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 order to simulat
1c012 65 20 61 6e 64 20 69 6e 74 65 72 72 75 70 74 2e e and interrupt.
1c013 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 .**.** This faci
1c014 6c 69 74 79 20 69 73 20 75 73 65 64 20 66 6f 72 lity is used for
1c015 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 testing purpose
1c016 73 20 6f 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 s only. It does
1c017 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a not function.**
1c018 20 69 6e 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 in an ordinary
1c019 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 build..*/.#ifdef
1c01a 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c SQLITE_TEST.SQL
1c01b 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 ITE_API int sqli
1c01c 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f te3_interrupt_co
1c01d 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a unt = 0;.#endif.
1c01e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 ./*.** The next
1c01f 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1c020 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 is incremented e
1c021 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f ach type the OP_
1c022 53 6f 72 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 Sort opcode.** i
1c023 73 20 65 78 65 63 75 74 65 64 2e 20 20 54 68 65 s executed. The
1c024 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 test procedures
1c025 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d use this inform
1c026 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 ation to make su
1c027 72 65 20 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 re that.** sorti
1c028 6e 67 20 69 73 20 6f 63 63 75 72 72 69 6e 67 20 ng is occurring
1c029 6f 72 20 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 or not occurring
1c02a 20 61 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 at appropriate
1c02b 74 69 6d 65 73 2e 20 20 20 54 68 69 73 20 76 61 times. This va
1c02c 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f riable.** has no
1c02d 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 function other
1c02e 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 than to help ver
1c02f 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
1c030 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
1c031 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a .** library..*/.
1c032 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1c033 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
1c034 74 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 t sqlite3_sort_c
1c035 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 ount = 0;.#endif
1c036 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ../*.** The next
1c037 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1c038 20 72 65 63 6f 72 64 73 20 74 68 65 20 73 69 7a records the siz
1c039 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 e of the largest
1c03a 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 MEM_Blob.** or
1c03b 4d 45 4d 5f 53 74 72 20 74 68 61 74 20 68 61 73 MEM_Str that has
1c03c 20 62 65 65 6e 20 75 73 65 64 20 62 79 20 61 20 been used by a
1c03d 56 44 42 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 VDBE opcode. Th
1c03e 65 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 e test procedure
1c03f 73 0a 2a 2a 20 75 73 65 20 74 68 69 73 20 69 6e s.** use this in
1c040 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b formation to mak
1c041 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 e sure that the
1c042 7a 65 72 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 zero-blob functi
1c043 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f onality.** is wo
1c044 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e rking correctly.
1c045 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 This variable
1c046 20 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e has no function
1c047 20 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a other than to.*
1c048 2a 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 * help verify th
1c049 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 e correct operat
1c04a 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 ion of the libra
1c04b 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 ry..*/.#ifdef SQ
1c04c 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 LITE_TEST.SQLITE
1c04d 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 _API int sqlite3
1c04e 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 _max_blobsize =
1c04f 30 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 0;.static void u
1c050 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 pdateMaxBlobsize
1c051 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 (Mem *p){. if(
1c052 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d (p->flags & (MEM
1c053 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 _Str|MEM_Blob))!
1c054 3d 30 20 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 =0 && p->n>sqlit
1c055 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 e3_max_blobsize
1c056 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d ){. sqlite3_m
1c057 61 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d ax_blobsize = p-
1c058 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 >n;. }.}.#endif
1c059 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 ../*.** The next
1c05a 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 global variable
1c05b 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 is incremented
1c05c 65 61 63 68 20 74 79 70 65 20 74 68 65 20 4f 50 each type the OP
1c05d 5f 46 6f 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a _Found opcode.**
1c05e 20 69 73 20 65 78 65 63 75 74 65 64 2e 20 54 68 is executed. Th
1c05f 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 is is used to te
1c060 73 74 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f st whether or no
1c061 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 t the foreign ke
1c062 79 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 y.** operation i
1c063 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 mplemented using
1c064 20 4f 50 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20 OP_FkIsZero is
1c065 77 6f 72 6b 69 6e 67 2e 20 54 68 69 73 20 76 61 working. This va
1c066 72 69 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f riable.** has no
1c067 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 function other
1c068 74 68 61 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 than to help ver
1c069 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 ify the correct
1c06a 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 operation of the
1c06b 0a 2a 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a .** library..*/.
1c06c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 #ifdef SQLITE_TE
1c06d 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e ST.SQLITE_API in
1c06e 74 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f t sqlite3_found_
1c06f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 count = 0;.#endi
1c070 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 f../*.** Test a
1c071 72 65 67 69 73 74 65 72 20 74 6f 20 73 65 65 20 register to see
1c072 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68 if it exceeds th
1c073 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 e current maximu
1c074 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 m blob size..**
1c075 49 66 20 69 74 20 64 6f 65 73 2c 20 72 65 63 6f If it does, reco
1c076 72 64 20 74 68 65 20 6e 65 77 20 6d 61 78 69 6d rd the new maxim
1c077 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f um blob size..*/
1c078 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c .#if defined(SQL
1c079 49 54 45 5f 54 45 53 54 29 20 26 26 20 21 64 65 ITE_TEST) && !de
1c07a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1c07b 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a T_BUILTIN_TEST).
1c07c 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f # define UPDATE_
1c07d 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 MAX_BLOBSIZE(P)
1c07e 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 updateMaxBlobsi
1c07f 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 ze(P).#else.# de
1c080 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f fine UPDATE_MAX_
1c081 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 BLOBSIZE(P).#end
1c082 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 if../*.** Conver
1c083 74 20 74 68 65 20 67 69 76 65 6e 20 72 65 67 69 t the given regi
1c084 73 74 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 ster into a stri
1c085 6e 67 20 69 66 20 69 74 20 69 73 6e 27 74 20 6f ng if it isn't o
1c086 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 ne.** already. R
1c087 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 eturn non-zero i
1c088 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 f a malloc() fai
1c089 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 ls..*/.#define S
1c08a 74 72 69 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 tringify(P, enc)
1c08b 20 5c 0a 20 20 20 69 66 28 28 28 50 29 2d 3e 66 \. if(((P)->f
1c08c 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 lags&(MEM_Str|ME
1c08d 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 M_Blob))==0 && s
1c08e 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 qlite3VdbeMemStr
1c08f 69 6e 67 69 66 79 28 50 2c 65 6e 63 29 29 20 5c ingify(P,enc)) \
1c090 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f . { goto no_
1c091 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e mem; }../*.** An
1c092 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e ephemeral strin
1c093 67 20 76 61 6c 75 65 20 28 73 69 67 6e 69 66 69 g value (signifi
1c094 65 64 20 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 ed by the MEM_Ep
1c095 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 hem flag) contai
1c096 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 ns.** a pointer
1c097 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 to a dynamically
1c098 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 69 6e allocated strin
1c099 67 20 77 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 g where some oth
1c09a 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 er entity.** is
1c09b 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 responsible for
1c09c 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 deallocating tha
1c09d 74 20 73 74 72 69 6e 67 2e 20 20 42 65 63 61 75 t string. Becau
1c09e 73 65 20 74 68 65 20 72 65 67 69 73 74 65 72 0a se the register.
1c09f 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 ** does not cont
1c0a0 72 6f 6c 20 74 68 65 20 73 74 72 69 6e 67 2c 20 rol the string,
1c0a1 69 74 20 6d 69 67 68 74 20 62 65 20 64 65 6c 65 it might be dele
1c0a2 74 65 64 20 77 69 74 68 6f 75 74 20 74 68 65 20 ted without the
1c0a3 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 register.** know
1c0a4 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ing it..**.** Th
1c0a5 69 73 20 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 is routine conve
1c0a6 72 74 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c rts an ephemeral
1c0a7 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 string into a d
1c0a8 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 ynamically alloc
1c0a9 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 ated.** string t
1c0aa 68 61 74 20 74 68 65 20 72 65 67 69 73 74 65 72 hat the register
1c0ab 20 69 74 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 itself controls
1c0ac 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 . In other word
1c0ad 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 s, it.** convert
1c0ae 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 s an MEM_Ephem s
1c0af 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 tring into an ME
1c0b0 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f M_Dyn string..*/
1c0b1 0a 23 64 65 66 69 6e 65 20 44 65 65 70 68 65 6d .#define Deephem
1c0b2 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 eralize(P) \.
1c0b3 69 66 28 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 if( ((P)->flags&
1c0b4 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a MEM_Ephem)!=0 \.
1c0b5 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 && sqlite
1c0b6 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 3VdbeMemMakeWrit
1c0b7 65 61 62 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f eable(P) ){ goto
1c0b8 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a no_mem;}../*.**
1c0b9 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 Call sqlite3Vdb
1c0ba 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 eMemExpandBlob()
1c0bb 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69 65 64 on the supplied
1c0bc 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d 65 6d value (type Mem
1c0bd 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71 75 69 *).** P if requi
1c0be 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 red..*/.#define
1c0bf 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 ExpandBlob(P) ((
1c0c0 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a (P)->flags&MEM_Z
1c0c1 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64 62 65 ero)?sqlite3Vdbe
1c0c2 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 MemExpandBlob(P)
1c0c3 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d :0)../*.** Argum
1c0c4 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 ent pMem points
1c0c5 61 74 20 61 20 72 65 67 69 73 74 65 72 20 74 68 at a register th
1c0c6 61 74 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65 at will be passe
1c0c7 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 d to a.** user-d
1c0c8 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 efined function
1c0c9 6f 72 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 or returned to t
1c0ca 68 65 20 75 73 65 72 20 61 73 20 74 68 65 20 72 he user as the r
1c0cb 65 73 75 6c 74 20 6f 66 20 61 20 71 75 65 72 79 esult of a query
1c0cc 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e ..** This routin
1c0cd 65 20 73 65 74 73 20 74 68 65 20 70 4d 65 6d 2d e sets the pMem-
1c0ce 3e 74 79 70 65 20 76 61 72 69 61 62 6c 65 20 75 >type variable u
1c0cf 73 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 sed by the sqlit
1c0d0 65 33 5f 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a e3_value_*() .**
1c0d1 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 routines..*/.SQ
1c0d2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
1c0d3 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d d sqlite3VdbeMem
1c0d4 53 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 StoreType(Mem *p
1c0d5 4d 65 6d 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 Mem){. int flag
1c0d6 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b s = pMem->flags;
1c0d7 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d . if( flags & M
1c0d8 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 EM_Null ){. p
1c0d9 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1c0da 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 TE_NULL;. }. e
1c0db 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 lse if( flags &
1c0dc 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 MEM_Int ){. p
1c0dd 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 Mem->type = SQLI
1c0de 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a TE_INTEGER;. }.
1c0df 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 else if( flags
1c0e0 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 & MEM_Real ){.
1c0e1 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 pMem->type =
1c0e2 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 SQLITE_FLOAT;.
1c0e3 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 }. else if( fla
1c0e4 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a gs & MEM_Str ){.
1c0e5 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d pMem->type =
1c0e6 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 SQLITE_TEXT;.
1c0e7 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d }else{. pMem-
1c0e8 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 >type = SQLITE_B
1c0e9 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a LOB;. }.}../*.*
1c0ea 2a 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 * Allocate VdbeC
1c0eb 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 ursor number iCu
1c0ec 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 r. Return a poi
1c0ed 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 nter to it. Ret
1c0ee 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 urn NULL.** if w
1c0ef 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d e run out of mem
1c0f0 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 ory..*/.static V
1c0f1 64 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 dbeCursor *alloc
1c0f2 61 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 ateCursor(. Vdb
1c0f3 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 e *p,
1c0f4 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 /* The virtua
1c0f5 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 l machine */. i
1c0f6 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 nt iCur,
1c0f7 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
1c0f8 20 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 the new VdbeCur
1c0f9 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 sor */. int nFi
1c0fa 65 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f eld, /
1c0fb 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c * Number of fiel
1c0fc 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ds in the table
1c0fd 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e or index */. in
1c0fe 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 t iDb,
1c0ff 20 20 20 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 /* When data
1c100 62 61 73 65 20 74 68 65 20 63 75 72 73 6f 72 20 base the cursor
1c101 62 65 6c 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d belongs to, or -
1c102 31 20 2a 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 1 */. int isBtr
1c103 65 65 43 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 eeCursor /*
1c104 54 72 75 65 20 66 6f 72 20 42 2d 54 72 65 65 2e True for B-Tree.
1c105 20 20 46 61 6c 73 65 20 66 6f 72 20 70 73 65 75 False for pseu
1c106 64 6f 2d 74 61 62 6c 65 20 6f 72 20 76 74 61 62 do-table or vtab
1c107 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 */.){. /* Find
1c108 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1c109 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 that will be us
1c10a 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 ed to store the
1c10b 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 blob of memory.
1c10c 20 2a 2a 20 72 65 71 75 69 72 65 64 20 66 6f 72 ** required for
1c10d 20 74 68 69 73 20 56 64 62 65 43 75 72 73 6f 72 this VdbeCursor
1c10e 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 69 structure. It i
1c10f 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 s convenient to
1c110 75 73 65 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 use a . ** vdbe
1c111 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 memory cell to
1c112 6d 61 6e 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 manage the memor
1c113 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 y allocation req
1c114 75 69 72 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a uired for a. **
1c115 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72 75 VdbeCursor stru
1c116 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 66 6f cture for the fo
1c117 6c 6c 6f 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a llowing reasons:
1c118 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 . **. ** * S
1c119 6f 6d 65 74 69 6d 65 73 20 63 75 72 73 6f 72 20 ometimes cursor
1c11a 6e 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 numbers are used
1c11b 20 66 6f 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 for a couple of
1c11c 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 different. **
1c11d 20 20 20 20 70 75 72 70 6f 73 65 73 20 69 6e 20 purposes in
1c11e 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 a vdbe program.
1c11f 54 68 65 20 64 69 66 66 65 72 65 6e 74 20 75 73 The different us
1c120 65 73 20 6d 69 67 68 74 20 72 65 71 75 69 72 65 es might require
1c121 0a 20 20 2a 2a 20 20 20 20 20 64 69 66 66 65 72 . ** differ
1c122 65 6e 74 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 ent sized alloca
1c123 74 69 6f 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 tions. Memory ce
1c124 6c 6c 73 20 70 72 6f 76 69 64 65 20 67 72 6f 77 lls provide grow
1c125 61 62 6c 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c able. ** al
1c126 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a locations.. **.
1c127 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 ** * When us
1c128 69 6e 67 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 ing ENABLE_MEMOR
1c129 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 Y_MANAGEMENT, me
1c12a 6d 6f 72 79 20 63 65 6c 6c 20 62 75 66 66 65 72 mory cell buffer
1c12b 73 20 63 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 s can. ** b
1c12c 65 20 66 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 e freed lazily v
1c12d 69 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 ia the sqlite3_r
1c12e 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 elease_memory()
1c12f 41 50 49 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 API. This. **
1c130 20 20 20 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 minimizes the
1c131 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f number of mallo
1c132 63 20 63 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 c calls made by
1c133 74 68 65 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a the system.. **
1c134 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c . ** Memory cel
1c135 6c 73 20 66 6f 72 20 63 75 72 73 6f 72 73 20 61 ls for cursors a
1c136 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 re allocated at
1c137 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 61 the top of the a
1c138 64 64 72 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 ddress. ** spac
1c139 65 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 e. Memory cell (
1c13a 70 2d 3e 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 p->nMem) corresp
1c13b 6f 6e 64 73 20 74 6f 20 63 75 72 73 6f 72 20 30 onds to cursor 0
1c13c 2e 20 53 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a . Space for. **
1c13d 20 63 75 72 73 6f 72 20 31 20 69 73 20 6d 61 6e cursor 1 is man
1c13e 61 67 65 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 aged by memory c
1c13f 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c ell (p->nMem-1),
1c140 20 65 74 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d etc.. */. Mem
1c141 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 *pMem = &p->aMe
1c142 6d 5b 70 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b m[p->nMem-iCur];
1c143 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 .. int nByte;.
1c144 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1c145 20 3d 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 = 0;. nByte =
1c146 0a 20 20 20 20 20 20 52 4f 55 4e 44 38 28 73 69 . ROUND8(si
1c147 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 zeof(VdbeCursor)
1c148 29 20 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 ) + . (isBt
1c149 72 65 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 reeCursor?sqlite
1c14a 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 3BtreeCursorSize
1c14b 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 ():0) + . 2
1c14c 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 *nField*sizeof(u
1c14d 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 32);.. assert(
1c14e 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 iCur<p->nCursor
1c14f 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 );. if( p->apCs
1c150 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 r[iCur] ){. s
1c151 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 qlite3VdbeFreeCu
1c152 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 rsor(p, p->apCsr
1c153 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e [iCur]);. p->
1c154 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b apCsr[iCur] = 0;
1c155 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 . }. if( SQLIT
1c156 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 E_OK==sqlite3Vdb
1c157 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e eMemGrow(pMem, n
1c158 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 Byte, 0) ){.
1c159 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d p->apCsr[iCur] =
1c15a 20 70 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 pCx = (VdbeCurs
1c15b 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 or*)pMem->z;.
1c15c 20 6d 65 6d 73 65 74 28 70 43 78 2c 20 30 2c 20 memset(pCx, 0,
1c15d 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f sizeof(VdbeCurso
1c15e 72 29 29 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 r));. pCx->iD
1c15f 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 43 78 b = iDb;. pCx
1c160 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c ->nField = nFiel
1c161 64 3b 0a 20 20 20 20 69 66 28 20 6e 46 69 65 6c d;. if( nFiel
1c162 64 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e d ){. pCx->
1c163 61 54 79 70 65 20 3d 20 28 75 33 32 20 2a 29 26 aType = (u32 *)&
1c164 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 pMem->z[ROUND8(s
1c165 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
1c166 29 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ))];. }. i
1c167 66 28 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 f( isBtreeCursor
1c168 20 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 ){. pCx->p
1c169 43 75 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73 Cursor = (BtCurs
1c16a 6f 72 2a 29 0a 20 20 20 20 20 20 20 20 20 20 26 or*). &
1c16b 70 4d 65 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 pMem->z[ROUND8(s
1c16c 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 izeof(VdbeCursor
1c16d 29 29 2b 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 ))+2*nField*size
1c16e 6f 66 28 75 33 32 29 5d 3b 0a 20 20 20 20 20 20 of(u32)];.
1c16f 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1c170 6f 72 5a 65 72 6f 28 70 43 78 2d 3e 70 43 75 72 orZero(pCx->pCur
1c171 73 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a sor);. }. }.
1c172 20 20 72 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a return pCx;.}.
1c173 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f ./*.** Try to co
1c174 6e 76 65 72 74 20 61 20 76 61 6c 75 65 20 69 6e nvert a value in
1c175 74 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 to a numeric rep
1c176 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 resentation if w
1c177 65 20 63 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 e can.** do so w
1c178 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 ithout loss of i
1c179 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 nformation. In
1c17a 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 other words, if
1c17b 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f the string.** lo
1c17c 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 oks like a numbe
1c17d 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 20 69 6e r, convert it in
1c17e 74 6f 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 to a number. If
1c17f 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 it does not.**
1c180 6c 6f 6f 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 look like a numb
1c181 65 72 2c 20 6c 65 61 76 65 20 69 74 20 61 6c 6f er, leave it alo
1c182 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ne..*/.static vo
1c183 69 64 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 id applyNumericA
1c184 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 ffinity(Mem *pRe
1c185 63 29 7b 0a 20 20 69 66 28 20 28 70 52 65 63 2d c){. if( (pRec-
1c186 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 >flags & (MEM_Re
1c187 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 al|MEM_Int))==0
1c188 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 61 6c 6e ){. int realn
1c189 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 um;. sqlite3V
1c18a 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 dbeMemNulTermina
1c18b 74 65 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 te(pRec);. if
1c18c 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d ( (pRec->flags&M
1c18d 45 4d 5f 53 74 72 29 0a 20 20 20 20 20 20 20 20 EM_Str).
1c18e 20 26 26 20 73 71 6c 69 74 65 33 49 73 4e 75 6d && sqlite3IsNum
1c18f 62 65 72 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 ber(pRec->z, &re
1c190 61 6c 6e 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 alnum, pRec->enc
1c191 29 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 ) ){. i64 v
1c192 61 6c 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 alue;. sqli
1c193 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 te3VdbeChangeEnc
1c194 6f 64 69 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 oding(pRec, SQLI
1c195 54 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 20 20 TE_UTF8);.
1c196 69 66 28 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 if( !realnum &&
1c197 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 52 sqlite3Atoi64(pR
1c198 65 63 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 ec->z, &value) )
1c199 7b 0a 20 20 20 20 20 20 20 20 70 52 65 63 2d 3e {. pRec->
1c19a 75 2e 69 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 u.i = value;.
1c19b 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 MemSetTypeF
1c19c 6c 61 67 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e lag(pRec, MEM_In
1c19d 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b t);. }else{
1c19e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
1c19f 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 VdbeMemRealify(p
1c1a0 52 65 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 Rec);. }.
1c1a1 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a }. }.}../*.**
1c1a2 20 50 72 6f 63 65 73 73 69 6e 67 20 69 73 20 64 Processing is d
1c1a3 65 74 65 72 6d 69 6e 65 20 62 79 20 74 68 65 20 etermine by the
1c1a4 61 66 66 69 6e 69 74 79 20 70 61 72 61 6d 65 74 affinity paramet
1c1a5 65 72 3a 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 er:.**.** SQLITE
1c1a6 5f 41 46 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a _AFF_INTEGER:.**
1c1a7 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c SQLITE_AFF_REAL
1c1a8 3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f :.** SQLITE_AFF_
1c1a9 4e 55 4d 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 NUMERIC:.** T
1c1aa 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 ry to convert pR
1c1ab 65 63 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 ec to an integer
1c1ac 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1c1ad 6f 72 20 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 or a .** floa
1c1ae 74 69 6e 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 ting-point repre
1c1af 73 65 6e 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 sentation if an
1c1b0 69 6e 74 65 67 65 72 20 72 65 70 72 65 73 65 6e integer represen
1c1b1 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 tation.** is
1c1b2 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e not possible. N
1c1b3 6f 74 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 ote that the int
1c1b4 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 eger representat
1c1b5 69 6f 6e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 ion is.** alw
1c1b6 61 79 73 20 70 72 65 66 65 72 72 65 64 2c 20 65 ays preferred, e
1c1b7 76 65 6e 20 69 66 20 74 68 65 20 61 66 66 69 6e ven if the affin
1c1b8 69 74 79 20 69 73 20 52 45 41 4c 2c 20 62 65 63 ity is REAL, bec
1c1b9 61 75 73 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e ause.** an in
1c1ba 74 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 teger representa
1c1bb 74 69 6f 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 tion is more spa
1c1bc 63 65 20 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 ce efficient on
1c1bd 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 disk..**.** SQLI
1c1be 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 TE_AFF_TEXT:.**
1c1bf 20 20 20 43 6f 6e 76 65 72 74 20 70 52 65 63 20 Convert pRec
1c1c0 74 6f 20 61 20 74 65 78 74 20 72 65 70 72 65 73 to a text repres
1c1c1 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 entation..**.**
1c1c2 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a SQLITE_AFF_NONE:
1c1c3 0a 2a 2a 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 .** No-op. p
1c1c4 52 65 63 20 69 73 20 75 6e 63 68 61 6e 67 65 64 Rec is unchanged
1c1c5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1c1c6 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 0a applyAffinity(.
1c1c7 20 20 4d 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 Mem *pRec,
1c1c8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c /* The val
1c1c9 75 65 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 ue to apply affi
1c1ca 6e 69 74 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 nity to */. cha
1c1cb 72 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 r affinity,
1c1cc 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79 /* The affinity
1c1cd 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64 20 2a to be applied *
1c1ce 2f 0a 20 20 75 38 20 65 6e 63 20 20 20 20 20 20 /. u8 enc
1c1cf 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 /* Use t
1c1d0 68 69 73 20 74 65 78 74 20 65 6e 63 6f 64 69 6e his text encodin
1c1d1 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 g */.){. if( af
1c1d2 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 finity==SQLITE_A
1c1d3 46 46 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 2f FF_TEXT ){. /
1c1d4 2a 20 4f 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 * Only attempt t
1c1d5 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f he conversion to
1c1d6 20 54 45 58 54 20 69 66 20 74 68 65 72 65 20 69 TEXT if there i
1c1d7 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 s an integer or
1c1d8 72 65 61 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 real. ** repr
1c1d9 65 73 65 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 esentation (blob
1c1da 20 61 6e 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 and NULL do not
1c1db 20 67 65 74 20 63 6f 6e 76 65 72 74 65 64 29 20 get converted)
1c1dc 62 75 74 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 but no string.
1c1dd 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 ** representat
1c1de 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ion.. */.
1c1df 69 66 28 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c if( 0==(pRec->fl
1c1e0 61 67 73 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 ags&MEM_Str) &&
1c1e1 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 (pRec->flags&(ME
1c1e2 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 M_Real|MEM_Int))
1c1e3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1c1e4 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 3VdbeMemStringif
1c1e5 79 28 70 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 y(pRec, enc);.
1c1e6 20 20 7d 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c }. pRec->fl
1c1e7 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 ags &= ~(MEM_Rea
1c1e8 6c 7c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 l|MEM_Int);. }e
1c1e9 6c 73 65 20 69 66 28 20 61 66 66 69 6e 69 74 79 lse if( affinity
1c1ea 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e !=SQLITE_AFF_NON
1c1eb 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 E ){. assert(
1c1ec 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1c1ed 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c E_AFF_INTEGER ||
1c1ee 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 affinity==SQLIT
1c1ef 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 E_AFF_REAL.
1c1f0 20 20 20 20 20 20 20 20 7c 7c 20 61 66 66 69 6e || affin
1c1f1 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f ity==SQLITE_AFF_
1c1f2 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 61 NUMERIC );. a
1c1f3 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e pplyNumericAffin
1c1f4 69 74 79 28 70 52 65 63 29 3b 0a 20 20 20 20 69 ity(pRec);. i
1c1f5 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 f( pRec->flags &
1c1f6 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 MEM_Real ){.
1c1f7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e sqlite3VdbeIn
1c1f8 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 52 tegerAffinity(pR
1c1f9 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d ec);. }. }.}
1c1fa 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 ../*.** Try to c
1c1fb 6f 6e 76 65 72 74 20 74 68 65 20 74 79 70 65 20 onvert the type
1c1fc 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 of a function ar
1c1fd 67 75 6d 65 6e 74 20 6f 72 20 61 20 72 65 73 75 gument or a resu
1c1fe 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 lt column.** int
1c1ff 6f 20 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 o a numeric repr
1c200 65 73 65 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 esentation. Use
1c201 20 65 69 74 68 65 72 20 49 4e 54 45 47 45 52 20 either INTEGER
1c202 6f 72 20 52 45 41 4c 20 77 68 69 63 68 65 76 65 or REAL whicheve
1c203 72 0a 2a 2a 20 69 73 20 61 70 70 72 6f 70 72 69 r.** is appropri
1c204 61 74 65 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 ate. But only d
1c205 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e o the conversion
1c206 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 if it is possib
1c207 6c 65 20 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f le without.** lo
1c208 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f ss of informatio
1c209 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 n and return the
1c20a 20 72 65 76 69 73 65 64 20 74 79 70 65 20 6f 66 revised type of
1c20b 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a the argument..*
1c20c 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 *.** This is an
1c20d 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 EXPERIMENTAL api
1c20e 20 61 6e 64 20 69 73 20 73 75 62 6a 65 63 74 20 and is subject
1c20f 74 6f 20 63 68 61 6e 67 65 20 6f 72 20 72 65 6d to change or rem
1c210 6f 76 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f oval..*/.SQLITE_
1c211 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f API int sqlite3_
1c212 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 value_numeric_ty
1c213 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 pe(sqlite3_value
1c214 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a *pVal){. Mem *
1c215 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 pMem = (Mem*)pVa
1c216 6c 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 l;. applyNumeri
1c217 63 41 66 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b cAffinity(pMem);
1c218 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 . sqlite3VdbeMe
1c219 6d 53 74 6f 72 65 54 79 70 65 28 70 4d 65 6d 29 mStoreType(pMem)
1c21a 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d ;. return pMem-
1c21b 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 >type;.}../*.**
1c21c 45 78 70 6f 72 74 65 64 20 76 65 72 73 69 6f 6e Exported version
1c21d 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e 69 74 of applyAffinit
1c21e 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f y(). This one wo
1c21f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 rks on sqlite3_v
1c220 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 alue*, .** not t
1c221 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a he internal Mem*
1c222 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 type..*/.SQLITE
1c223 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 _PRIVATE void sq
1c224 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 lite3ValueApplyA
1c225 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 ffinity(. sqlit
1c226 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 e3_value *pVal,
1c227 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20 . u8 affinity,
1c228 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 . u8 enc.){. a
1c229 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28 4d 65 pplyAffinity((Me
1c22a 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 m *)pVal, affini
1c22b 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 ty, enc);.}..#if
1c22c 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1c22d 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e ./*.** Write a n
1c22e 69 63 65 20 73 74 72 69 6e 67 20 72 65 70 72 65 ice string repre
1c22f 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 sentation of the
1c230 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c contents of cel
1c231 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 l pMem.** into b
1c232 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 uffer zBuf, leng
1c233 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 th nBuf..*/.SQLI
1c234 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
1c235 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 sqlite3VdbeMemPr
1c236 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 ettyPrint(Mem *p
1c237 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 Mem, char *zBuf)
1c238 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 20 3d {. char *zCsr =
1c239 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d zBuf;. int f =
1c23a 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 pMem->flags;..
1c23b 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 static const ch
1c23c 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d ar *const encnam
1c23d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 es[] = {"(X)", "
1c23e 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 (8)", "(16LE)",
1c23f 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 "(16BE)"};.. if
1c240 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a ( f&MEM_Blob ){.
1c241 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 int i;. c
1c242 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28 20 66 har c;. if( f
1c243 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 & MEM_Dyn ){.
1c244 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 c = 'z';.
1c245 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c246 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f (MEM_Static|MEM_
1c247 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 Ephem))==0 );.
1c248 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 }else if( f &
1c249 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 MEM_Static ){.
1c24a 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 c = 't';.
1c24b 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20 assert( (f &
1c24c 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 (MEM_Dyn|MEM_Eph
1c24d 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d em))==0 );. }
1c24e 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d else if( f & MEM
1c24f 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 _Ephem ){.
1c250 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 c = 'e';. a
1c251 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d ssert( (f & (MEM
1c252 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 _Static|MEM_Dyn)
1c253 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 )==0 );. }els
1c254 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 73 27 e{. c = 's'
1c255 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1c256 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 ite3_snprintf(10
1c257 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 0, zCsr, "%c", c
1c258 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 );. zCsr += s
1c259 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a qlite3Strlen30(z
1c25a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 Csr);. sqlite
1c25b 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c25c 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 zCsr, "%d[", pMe
1c25d 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 m->n);. zCsr
1c25e 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c25f 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 30(zCsr);. fo
1c260 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 r(i=0; i<16 && i
1c261 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a <pMem->n; i++){.
1c262 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e sqlite3_sn
1c263 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 printf(100, zCsr
1c264 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e 74 29 , "%02X", ((int)
1c265 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 pMem->z[i] & 0xF
1c266 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 F));. zCsr
1c267 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c268 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 30(zCsr);. }.
1c269 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 for(i=0; i<1
1c26a 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 6 && i<pMem->n;
1c26b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 i++){. char
1c26c 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b z = pMem->z[i];
1c26d 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33 32 20 . if( z<32
1c26e 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 || z>126 ) *zCsr
1c26f 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 ++ = '.';.
1c270 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a else *zCsr++ = z
1c271 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c ;. }.. sql
1c272 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 ite3_snprintf(10
1c273 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 0, zCsr, "]%s",
1c274 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 encnames[pMem->e
1c275 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b nc]);. zCsr +
1c276 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 = sqlite3Strlen3
1c277 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28 0(zCsr);. if(
1c278 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b f & MEM_Zero ){
1c279 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 . sqlite3_s
1c27a 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 nprintf(100, zCs
1c27b 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 r,"+%dz",pMem->u
1c27c 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a .nZero);. z
1c27d 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 Csr += sqlite3St
1c27e 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 rlen30(zCsr);.
1c27f 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 }. *zCsr =
1c280 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 '\0';. }else if
1c281 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b ( f & MEM_Str ){
1c282 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 . int j, k;.
1c283 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 zBuf[0] = ' '
1c284 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 ;. if( f & ME
1c285 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a M_Dyn ){. z
1c286 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 Buf[1] = 'z';.
1c287 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 assert( (f &
1c288 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d (MEM_Static|MEM
1c289 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 _Ephem))==0 );.
1c28a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 }else if( f &
1c28b 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 MEM_Static ){.
1c28c 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 zBuf[1] = '
1c28d 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 t';. assert
1c28e 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c ( (f & (MEM_Dyn|
1c28f 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 MEM_Ephem))==0 )
1c290 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
1c291 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b f & MEM_Ephem ){
1c292 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d . zBuf[1] =
1c293 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65 'e';. asse
1c294 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 rt( (f & (MEM_St
1c295 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d atic|MEM_Dyn))==
1c296 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0 );. }else{.
1c297 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 zBuf[1] =
1c298 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 's';. }. k
1c299 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 = 2;. sqlite
1c29a 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 3_snprintf(100,
1c29b 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 &zBuf[k], "%d",
1c29c 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 pMem->n);. k
1c29d 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c29e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 30(&zBuf[k]);.
1c29f 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b zBuf[k++] = '[
1c2a0 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 ';. for(j=0;
1c2a1 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e j<15 && j<pMem->
1c2a2 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 n; j++){. u
1c2a3 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 8 c = pMem->z[j]
1c2a4 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 30 ;. if( c>=0
1c2a5 78 32 30 20 26 26 20 63 3c 30 78 37 66 20 29 7b x20 && c<0x7f ){
1c2a6 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b . zBuf[k+
1c2a7 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 +] = c;. }e
1c2a8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 lse{. zBu
1c2a9 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 f[k++] = '.';.
1c2aa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1c2ab 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b zBuf[k++] = ']';
1c2ac 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 . sqlite3_snp
1c2ad 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75 66 5b rintf(100,&zBuf[
1c2ae 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 k], encnames[pMe
1c2af 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 m->enc]);. k
1c2b0 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e += sqlite3Strlen
1c2b1 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 30(&zBuf[k]);.
1c2b2 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b zBuf[k++] = 0;
1c2b3 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 . }.}.#endif..#
1c2b4 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
1c2b5 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 UG./*.** Print t
1c2b6 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 72 65 he value of a re
1c2b7 67 69 73 74 65 72 20 66 6f 72 20 74 72 61 63 69 gister for traci
1c2b8 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a ng purposes:.*/.
1c2b9 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 static void memT
1c2ba 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45 20 2a racePrint(FILE *
1c2bb 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 out, Mem *p){.
1c2bc 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d if( p->flags & M
1c2bd 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 EM_Null ){. f
1c2be 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 printf(out, " NU
1c2bf 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 LL");. }else if
1c2c0 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d ( (p->flags & (M
1c2c1 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 EM_Int|MEM_Str))
1c2c2 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 ==(MEM_Int|MEM_S
1c2c3 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e tr) ){. fprin
1c2c4 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c tf(out, " si:%ll
1c2c5 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d d", p->u.i);. }
1c2c6 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 else if( p->flag
1c2c7 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 s & MEM_Int ){.
1c2c8 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 fprintf(out,
1c2c9 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e " i:%lld", p->u.
1c2ca 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 i);.#ifndef SQLI
1c2cb 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
1c2cc 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69 _POINT. }else i
1c2cd 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 f( p->flags & ME
1c2ce 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 M_Real ){. fp
1c2cf 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 rintf(out, " r:%
1c2d0 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69 g", p->r);.#endi
1c2d1 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d f. }else if( p-
1c2d2 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 >flags & MEM_Row
1c2d3 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72 69 6e Set ){. fprin
1c2d4 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77 73 65 tf(out, " (rowse
1c2d5 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 t)");. }else{.
1c2d6 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30 char zBuf[200
1c2d7 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
1c2d8 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 beMemPrettyPrint
1c2d9 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 (p, zBuf);. f
1c2da 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 printf(out, " ")
1c2db 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 ;. fprintf(ou
1c2dc 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a t, "%s", zBuf);.
1c2dd 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 }.}.static voi
1c2de 64 20 72 65 67 69 73 74 65 72 54 72 61 63 65 28 d registerTrace(
1c2df 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 FILE *out, int i
1c2e0 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 Reg, Mem *p){.
1c2e1 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 fprintf(out, "RE
1c2e2 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 G[%d] = ", iReg)
1c2e3 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72 69 6e ;. memTracePrin
1c2e4 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 t(out, p);. fpr
1c2e5 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b intf(out, "\n");
1c2e6 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 .}.#endif..#ifde
1c2e7 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 f SQLITE_DEBUG.#
1c2e8 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 define REGISTE
1c2e9 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69 66 28 R_TRACE(R,M) if(
1c2ea 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73 74 65 p->trace)registe
1c2eb 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c rTrace(p->trace,
1c2ec 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 R,M).#else.# de
1c2ed 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54 52 fine REGISTER_TR
1c2ee 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a ACE(R,M).#endif.
1c2ef 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 ..#ifdef VDBE_PR
1c2f0 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 OFILE../* .** hw
1c2f1 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 time.h contains
1c2f2 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 inline assembler
1c2f3 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d code for implem
1c2f4 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d enting .** high-
1c2f5 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 performance timi
1c2f6 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a ng routines..*/.
1c2f7 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1c2f8 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 Include hwtime.h
1c2f9 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f in the middle o
1c2fa 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a f vdbe.c *******
1c2fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1c2fc 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 /**************
1c2fd 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d Begin file hwtim
1c2fe 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.h ************
1c2ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1c301 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 /*.** 2008 May 2
1c302 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 7.**.** The auth
1c303 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 or disclaims cop
1c304 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 yright to this s
1c305 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 ource code. In
1c306 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 place of.** a le
1c307 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 gal notice, here
1c308 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a is a blessing:.
1c309 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 **.** May you
1c30a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 do good and not
1c30b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 evil..** May
1c30c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 you find forgiv
1c30d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 eness for yourse
1c30e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f lf and forgive o
1c30f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 thers..** May
1c310 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c you share freel
1c311 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 y, never taking
1c312 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 more than you gi
1c313 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a ve..**.*********
1c314 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c315 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c316 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c317 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c318 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 *****.**.** This
1c319 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 file contains i
1c31a 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 nline asm code f
1c31b 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 or retrieving "h
1c31c 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 igh-performance"
1c31d 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 .** counters for
1c31e 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e x86 class CPUs.
1c31f 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 .*/.#ifndef _HWT
1c320 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f IME_H_.#define _
1c321 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a HWTIME_H_../*.**
1c322 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 The following r
1c323 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b outine only work
1c324 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 s on pentium-cla
1c325 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 ss (or newer) pr
1c326 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 ocessors..** It
1c327 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f uses the RDTSC o
1c328 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 pcode to read th
1c329 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 e cycle count va
1c32a 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a lue out of the.*
1c32b 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 * processor and
1c32c 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c returns that val
1c32d 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 ue. This can be
1c32e 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 used for high-r
1c32f 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e es.** profiling.
1c330 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 .*/.#if (defined
1c331 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 (__GNUC__) || de
1c332 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 fined(_MSC_VER))
1c333 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 && \. (def
1c334 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 ined(i386) || de
1c335 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 fined(__i386__)
1c336 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 || defined(_M_IX
1c337 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 86)).. #if defi
1c338 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 ned(__GNUC__)..
1c339 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 __inline__ sqli
1c33a 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 te_uint64 sqlite
1c33b 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 3Hwtime(void){.
1c33c 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 unsigned int
1c33d 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f lo, hi;. __
1c33e 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 asm__ __volatile
1c33f 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d __ ("rdtsc" : "=
1c340 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 a" (lo), "=d" (h
1c341 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e i));. return
1c342 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 (sqlite_uint64)
1c343 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 hi << 32 | lo;.
1c344 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 }.. #elif defi
1c345 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 ned(_MSC_VER)..
1c346 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 __declspec(nake
1c347 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 d) __inline sqli
1c348 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 te_uint64 __cdec
1c349 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 l sqlite3Hwtime(
1c34a 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 void){. __as
1c34b 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 m {. rdts
1c34c 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 c. ret
1c34d 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c ; return val
1c34e 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 ue at EDX:EAX.
1c34f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 }. }.. #end
1c350 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e if..#elif (defin
1c351 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 ed(__GNUC__) &&
1c352 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 defined(__x86_64
1c353 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 __)).. __inline
1c354 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 __ sqlite_uint64
1c355 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 sqlite3Hwtime(v
1c356 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 oid){. unsi
1c357 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 gned long val;.
1c358 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 __asm__ __v
1c359 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 olatile__ ("rdts
1c35a 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 c" : "=A" (val))
1c35b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 ;. return v
1c35c 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 al;. }. .#elif
1c35d 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f (defined(__GNUC_
1c35e 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f _) && defined(__
1c35f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c ppc__)).. __inl
1c360 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e ine__ sqlite_uin
1c361 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d t64 sqlite3Hwtim
1c362 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 e(void){. u
1c363 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e nsigned long lon
1c364 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 g retval;.
1c365 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 unsigned long ju
1c366 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f nk;. __asm_
1c367 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 _ __volatile__ (
1c368 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 "\n\. 1
1c369 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 : mftbu %
1c36a 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 1\n\.
1c36b 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 mftb %
1c36c 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 L0\n\.
1c36d 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 mftbu
1c36e 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 %0\n\.
1c36f 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 cmpw
1c370 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 %0,%1\n\.
1c371 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 bne
1c372 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 1b".
1c373 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 : "=r"
1c374 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 (retval), "=r" (
1c375 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 junk));. re
1c376 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d turn retval;. }
1c377 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f ..#else.. #erro
1c378 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 r Need implement
1c379 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 ation of sqlite3
1c37a 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 Hwtime() for you
1c37b 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f r platform... /
1c37c 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c *. ** To compil
1c37d 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d e without implem
1c37e 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 enting sqlite3Hw
1c37f 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 time() for your
1c380 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 platform,. ** y
1c381 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 ou can remove th
1c382 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 e above #error a
1c383 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f nd use the follo
1c384 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 wing. ** stub f
1c385 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 unction. You wi
1c386 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 ll lose timing s
1c387 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a upport for many.
1c388 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 ** of the debu
1c389 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e gging and testin
1c38a 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 g utilities, but
1c38b 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 it should at.
1c38c 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 ** least compile
1c38d 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 and run.. */.S
1c38e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 QLITE_PRIVATE
1c38f 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 sqlite_uint64 sq
1c390 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 lite3Hwtime(void
1c391 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 ){ return ((sqli
1c392 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a te_uint64)0); }.
1c393 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 .#endif..#endif
1c394 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 /* !defined(_HWT
1c395 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a IME_H_) */../***
1c396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 *********** End
1c397 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a of hwtime.h ****
1c398 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c399 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c39a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a **********/./***
1c39b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 *********** Cont
1c39c 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 inuing where we
1c39d 6c 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 left off in vdbe
1c39e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .c *************
1c39f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e **********/..#en
1c3a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 dif../*.** The C
1c3a1 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 HECK_FOR_INTERRU
1c3a2 50 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 PT macro defined
1c3a3 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 here looks to s
1c3a4 65 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c ee if the.** sql
1c3a5 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 ite3_interrupt()
1c3a6 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 routine has bee
1c3a7 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 n called. If it
1c3a8 20 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a has been, then.
1c3a9 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 ** processing of
1c3aa 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 the VDBE progra
1c3ab 6d 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 m is interrupted
1c3ac 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 ..**.** This mac
1c3ad 72 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 ro added to ever
1c3ae 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 y instruction th
1c3af 61 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 at does a jump i
1c3b0 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d n order to.** im
1c3b1 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 plement a loop.
1c3b2 20 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20 This test used
1c3b3 74 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 to be on every s
1c3b4 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f ingle instructio
1c3b5 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d n,.** but that m
1c3b6 65 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 eant we more tes
1c3b7 74 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 ting that we nee
1c3b8 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 ded. By only te
1c3b9 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 sting the.** fla
1c3ba 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 g on jump instru
1c3bb 63 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 ctions, we get a
1c3bc 20 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 (small) speed i
1c3bd 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 mprovement..*/.#
1c3be 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 define CHECK_FOR
1c3bf 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 _INTERRUPT \.
1c3c0 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 if( db->u1.isInt
1c3c1 65 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 errupted ) goto
1c3c2 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 abort_due_to_int
1c3c3 65 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 errupt;..#ifdef
1c3c4 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 SQLITE_DEBUG.sta
1c3c5 74 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 tic int fileExis
1c3c6 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ts(sqlite3 *db,
1c3c7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c const char *zFil
1c3c8 65 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 e){. int res =
1c3c9 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 0;. int rc = SQ
1c3ca 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 LITE_OK;.#ifdef
1c3cb 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a SQLITE_TEST. /*
1c3cc 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72 65 If we are curre
1c3cd 6e 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 ntly testing IO
1c3ce 65 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 errors, then do
1c3cf 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 not call OsAcces
1c3d0 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 s() to. ** test
1c3d1 20 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 for the presenc
1c3d2 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 e of zFile. This
1c3d3 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 is because any
1c3d4 49 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 IO error that.
1c3d5 2a 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 ** occurs here w
1c3d6 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 ill not be repor
1c3d7 74 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 ted, causing the
1c3d8 20 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 test to fail..
1c3d9 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 */. extern int
1c3da 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
1c3db 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 r_pending;. if(
1c3dc 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f sqlite3_io_erro
1c3dd 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 r_pending<=0 ).#
1c3de 65 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 endif. rc = s
1c3df 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 qlite3OsAccess(d
1c3e0 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 b->pVfs, zFile,
1c3e1 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 SQLITE_ACCESS_EX
1c3e2 49 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 ISTS, &res);. r
1c3e3 65 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 eturn (res && rc
1c3e4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a ==SQLITE_OK);.}.
1c3e5 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
1c3e6 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 NDEBUG./*.** Thi
1c3e7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e s function is on
1c3e8 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 ly called from w
1c3e9 69 74 68 69 6e 20 61 6e 20 61 73 73 65 72 74 28 ithin an assert(
1c3ea 29 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 ) expression. It
1c3eb 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68 61 74 20 .** checks that
1c3ec 74 68 65 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 the sqlite3.nTra
1c3ed 6e 73 61 63 74 69 6f 6e 20 76 61 72 69 61 62 6c nsaction variabl
1c3ee 65 20 69 73 20 63 6f 72 72 65 63 74 6c 79 20 73 e is correctly s
1c3ef 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d et to.** the num
1c3f0 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 ber of non-trans
1c3f1 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 action savepoint
1c3f2 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 s currently in t
1c3f3 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 he .** linked li
1c3f4 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 20 73 st starting at s
1c3f5 71 6c 69 74 65 33 2e 70 53 61 76 65 70 6f 69 6e qlite3.pSavepoin
1c3f6 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a t..** .** Usage:
1c3f7 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 .**.** asser
1c3f8 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f 69 6e t( checkSavepoin
1c3f9 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f tCount(db) );.*/
1c3fa 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 .static int chec
1c3fb 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 kSavepointCount(
1c3fc 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 sqlite3 *db){.
1c3fd 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 int n = 0;. Sav
1c3fe 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 epoint *p;. for
1c3ff 28 70 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e (p=db->pSavepoin
1c400 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 t; p; p=p->pNext
1c401 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 ) n++;. assert(
1c402 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f n==(db->nSavepo
1c403 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e int + db->isTran
1c404 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 sactionSavepoint
1c405 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b ) );. return 1;
1c406 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
1c407 20 45 78 65 63 75 74 65 20 61 73 20 6d 75 63 68 Execute as much
1c408 20 6f 66 20 61 20 56 44 42 45 20 70 72 6f 67 72 of a VDBE progr
1c409 61 6d 20 61 73 20 77 65 20 63 61 6e 20 74 68 65 am as we can the
1c40a 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 n return..**.**
1c40b 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 sqlite3VdbeMakeR
1c40c 65 61 64 79 28 29 20 6d 75 73 74 20 62 65 20 63 eady() must be c
1c40d 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 74 68 69 alled before thi
1c40e 73 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 s routine in ord
1c40f 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 er to.** close t
1c410 68 65 20 70 72 6f 67 72 61 6d 20 77 69 74 68 20 he program with
1c411 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 a final OP_Halt
1c412 61 6e 64 20 74 6f 20 73 65 74 20 75 70 20 74 68 and to set up th
1c413 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 e callbacks.** a
1c414 6e 64 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 nd the error mes
1c415 73 61 67 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a sage pointer..**
1c416 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 61 20 72 .** Whenever a r
1c417 6f 77 20 6f 72 20 72 65 73 75 6c 74 20 64 61 74 ow or result dat
1c418 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 a is available,
1c419 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
1c41a 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f l either.** invo
1c41b 6b 65 20 74 68 65 20 72 65 73 75 6c 74 20 63 61 ke the result ca
1c41c 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 llback (if there
1c41d 20 69 73 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 is one) or retu
1c41e 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 rn with.** SQLIT
1c41f 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 E_ROW..**.** If
1c420 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 an attempt is ma
1c421 64 65 20 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 de to open a loc
1c422 6b 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 ked database, th
1c423 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a en this routine.
1c424 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65 72 20 69 ** will either i
1c425 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 63 nvoke the busy c
1c426 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68 65 72 allback (if ther
1c427 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 e is one) or it
1c428 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 will.** return S
1c429 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a QLITE_BUSY..**.*
1c42a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
1c42b 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d curs, an error m
1c42c 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65 essage is writte
1c42d 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 n to memory obta
1c42e 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c ined.** from sql
1c42f 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e ite3_malloc() an
1c430 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 d p->zErrMsg is
1c431 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f made to point to
1c432 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a that memory..**
1c433 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 The error code
1c434 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e is stored in p->
1c435 72 63 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 rc and this rout
1c436 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 ine returns SQLI
1c437 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 TE_ERROR..**.**
1c438 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 If the callback
1c439 65 76 65 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e ever returns non
1c43a 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 -zero, then the
1c43b 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a program exits.**
1c43c 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 immediately. T
1c43d 68 65 72 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 here will be no
1c43e 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 75 error message bu
1c43f 74 20 74 68 65 20 70 2d 3e 72 63 20 66 69 65 6c t the p->rc fiel
1c440 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 d is.** set to S
1c441 51 4c 49 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 QLITE_ABORT and
1c442 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
1c443 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f l return SQLITE_
1c444 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d ERROR..**.** A m
1c445 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e emory allocation
1c446 20 65 72 72 6f 72 20 63 61 75 73 65 73 20 70 2d error causes p-
1c447 3e 72 63 20 74 6f 20 62 65 20 73 65 74 20 74 6f >rc to be set to
1c448 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e SQLITE_NOMEM an
1c449 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e d this.** routin
1c44a 65 20 74 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 e to return SQLI
1c44b 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 TE_ERROR..**.**
1c44c 4f 74 68 65 72 20 66 61 74 61 6c 20 65 72 72 6f Other fatal erro
1c44d 72 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 rs return SQLITE
1c44e 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 _ERROR..**.** Af
1c44f 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ter this routine
1c450 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20 73 has finished, s
1c451 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 qlite3VdbeFinali
1c452 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a ze() should be.*
1c453 2a 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 * used to clean
1c454 75 70 20 74 68 65 20 6d 65 73 73 20 74 68 61 74 up the mess that
1c455 20 77 61 73 20 6c 65 66 74 20 62 65 68 69 6e 64 was left behind
1c456 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
1c457 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 ATE int sqlite3V
1c458 64 62 65 45 78 65 63 28 0a 20 20 56 64 62 65 20 dbeExec(. Vdbe
1c459 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *p
1c45a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
1c45b 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 E */.){. int pc
1c45c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c45d 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 /* The prog
1c45e 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 ram counter */.
1c45f 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f Op *aOp = p->aO
1c460 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 p; /* C
1c461 6f 70 79 20 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f opy of p->aOp */
1c462 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20 . Op *pOp;
1c463 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c464 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 Current operati
1c465 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d on */. int rc =
1c466 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 SQLITE_OK;
1c467 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 /* Value to r
1c468 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 eturn */. sqlit
1c469 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 e3 *db = p->db;
1c46a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 /* The dat
1c46b 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 abase */. u8 re
1c46c 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 setSchemaOnFault
1c46d 20 3d 20 30 3b 20 2f 2a 20 52 65 73 65 74 20 73 = 0; /* Reset s
1c46e 63 68 65 6d 61 20 61 66 74 65 72 20 61 6e 20 65 chema after an e
1c46f 72 72 6f 72 20 69 66 20 74 72 75 65 20 2a 2f 0a rror if true */.
1c470 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20 u8 encoding =
1c471 45 4e 43 28 64 62 29 3b 20 20 20 20 20 2f 2a 20 ENC(db); /*
1c472 54 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 The database enc
1c473 6f 64 69 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66 oding */.#ifndef
1c474 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f SQLITE_OMIT_PRO
1c475 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 GRESS_CALLBACK.
1c476 20 75 38 20 63 68 65 63 6b 50 72 6f 67 72 65 73 u8 checkProgres
1c477 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 s; /* T
1c478 72 75 65 20 69 66 20 70 72 6f 67 72 65 73 73 20 rue if progress
1c479 63 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 65 6e callbacks are en
1c47a 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e abled */. int n
1c47b 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b ProgressOps = 0;
1c47c 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 /* Opcodes
1c47d 20 65 78 65 63 75 74 65 64 20 73 69 6e 63 65 20 executed since
1c47e 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 progress callbac
1c47f 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d k. */.#endif. M
1c480 65 6d 20 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d em *aMem = p->aM
1c481 65 6d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 em; /* Cop
1c482 79 20 6f 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a y of p->aMem */.
1c483 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b Mem *pIn1 = 0;
1c484 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c485 31 73 74 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 1st input operan
1c486 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 d */. Mem *pIn2
1c487 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
1c488 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f /* 2nd input o
1c489 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 perand */. Mem
1c48a 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 *pIn3 = 0;
1c48b 20 20 20 20 20 20 20 2f 2a 20 33 72 64 20 69 6e /* 3rd in
1c48c 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 put operand */.
1c48d 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 Mem *pOut = 0;
1c48e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
1c48f 75 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f utput operand */
1c490 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 . int iCompare
1c491 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
1c492 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 Result of last
1c493 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 OP_Compare opera
1c494 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 tion */. int *a
1c495 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 Permute = 0;
1c496 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 /* Permutat
1c497 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 ion of columns f
1c498 6f 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f or OP_Compare */
1c499 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f .#ifdef VDBE_PRO
1c49a 46 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 FILE. u64 start
1c49b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1c49c 20 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 /* CPU clock c
1c49d 6f 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 ount at start of
1c49e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 opcode */. int
1c49f 20 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 origPc;
1c4a0 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 /* Progr
1c4a1 61 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 am counter at st
1c4a2 61 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f art of opcode */
1c4a3 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a .#endif. /*****
1c4a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c4a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a ***************.
1c4a8 20 20 2a 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c ** Automatical
1c4a9 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 ly generated cod
1c4aa 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 e. **. ** The
1c4ab 66 6f 6c 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 following union
1c4ac 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 is automatically
1c4ad 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 generated by th
1c4ae 65 0a 20 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 e. ** vdbe-comp
1c4af 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70 74 2e ress.tcl script.
1c4b0 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 The purpose of
1c4b1 20 74 68 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 this union is t
1c4b2 6f 0a 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 o. ** reduce th
1c4b3 65 20 61 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 e amount of stac
1c4b4 6b 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 k space required
1c4b5 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f by this functio
1c4b6 6e 2e 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d n.. ** See comm
1c4b7 65 6e 74 73 20 69 6e 20 74 68 65 20 76 64 62 65 ents in the vdbe
1c4b8 2d 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 -compress.tcl sc
1c4b9 72 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 ript for details
1c4ba 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 .. */. union v
1c4bb 64 62 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 dbeExecUnion {.
1c4bc 20 20 20 73 74 72 75 63 74 20 4f 50 5f 59 69 65 struct OP_Yie
1c4bd 6c 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ld_stack_vars {.
1c4be 20 20 20 20 20 20 69 6e 74 20 70 63 44 65 73 74 int pcDest
1c4bf 3b 0a 20 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 ;. } aa;.
1c4c0 73 74 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62 struct OP_Variab
1c4c1 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a le_stack_vars {.
1c4c2 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 int p1;
1c4c3 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 /* Variab
1c4c4 6c 65 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 le to copy from
1c4c5 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b */. int p2;
1c4c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c4c7 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f ister to copy to
1c4c8 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b */. int n;
1c4c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c4ca 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c mber of values l
1c4cb 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
1c4cc 20 20 20 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 Mem *pVar;
1c4cd 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 /* Value b
1c4ce 65 69 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64 eing transferred
1c4cf 20 2a 2f 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20 */. } ab;.
1c4d0 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65 struct OP_Move
1c4d1 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c4d2 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f char *zMallo
1c4d3 63 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 c; /* Holding
1c4d4 76 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c variable for all
1c4d5 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f ocated memory */
1c4d6 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 . int n;
1c4d7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 /* Numbe
1c4d8 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 6c r of registers l
1c4d9 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 eft to copy */.
1c4da 20 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 int p1;
1c4db 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 /* Registe
1c4dc 72 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a r to copy from *
1c4dd 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 /. int p2;
1c4de 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 /* Regi
1c4df 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 ster to copy to
1c4e0 2a 2f 0a 20 20 20 20 7d 20 61 63 3b 0a 20 20 20 */. } ac;.
1c4e1 20 73 74 72 75 63 74 20 4f 50 5f 52 65 73 75 6c struct OP_Resul
1c4e2 74 52 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 tRow_stack_vars
1c4e3 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 {. Mem *pMe
1c4e4 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a m;. int i;.
1c4e5 20 20 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 } ad;. st
1c4e6 72 75 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 ruct OP_Concat_s
1c4e7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c4e8 20 20 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 20 i64 nByte;.
1c4e9 20 7d 20 61 65 3b 0a 20 20 20 20 73 74 72 75 63 } ae;. struc
1c4ea 74 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73 t OP_Remainder_s
1c4eb 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c4ec 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
1c4ed 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 /* Combined ME
1c4ee 4d 5f 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 M_* flags from b
1c4ef 6f 74 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 oth inputs */.
1c4f0 20 20 20 20 69 36 34 20 69 41 3b 20 20 20 20 20 i64 iA;
1c4f1 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 /* Integer v
1c4f2 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 alue of left ope
1c4f3 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 rand */. i6
1c4f4 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 4 iB; /*
1c4f5 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f Integer value o
1c4f6 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 f right operand
1c4f7 2a 2f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 */. double
1c4f8 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c rA; /* Real
1c4f9 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f value of left o
1c4fa 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 perand */.
1c4fb 64 6f 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 double rB;
1c4fc 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 /* Real value of
1c4fd 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a right operand *
1c4fe 2f 0a 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20 /. } af;.
1c4ff 73 74 72 75 63 74 20 4f 50 5f 46 75 6e 63 74 69 struct OP_Functi
1c500 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a on_stack_vars {.
1c501 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 int i;.
1c502 20 20 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 Mem *pArg;.
1c503 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 sqlite3_cont
1c504 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73 ext ctx;. s
1c505 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 qlite3_value **a
1c506 70 56 61 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 pVal;. int
1c507 6e 3b 0a 20 20 20 20 7d 20 61 67 3b 0a 20 20 20 n;. } ag;.
1c508 20 73 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74 struct OP_Shift
1c509 52 69 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 Right_stack_vars
1c50a 20 7b 0a 20 20 20 20 20 20 69 36 34 20 61 3b 0a {. i64 a;.
1c50b 20 20 20 20 20 20 69 36 34 20 62 3b 0a 20 20 20 i64 b;.
1c50c 20 7d 20 61 68 3b 0a 20 20 20 20 73 74 72 75 63 } ah;. struc
1c50d 74 20 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 t OP_Ge_stack_va
1c50e 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 rs {. int r
1c50f 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f es; /
1c510 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 * Result of the
1c511 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 comparison of pI
1c512 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20 n1 against pIn3
1c513 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 */. char af
1c514 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 finity; /*
1c515 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 Affinity to use
1c516 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a for comparison *
1c517 2f 0a 20 20 20 20 7d 20 61 69 3b 0a 20 20 20 20 /. } ai;.
1c518 73 74 72 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72 struct OP_Compar
1c519 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 e_stack_vars {.
1c51a 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 int n;.
1c51b 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 int i;. i
1c51c 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 nt p1;. int
1c51d 20 70 32 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 p2;. const
1c51e 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e KeyInfo *pKeyIn
1c51f 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 fo;. int id
1c520 78 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 x;. CollSeq
1c521 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 *pColl; /* C
1c522 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1c523 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 e to use on this
1c524 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69 term */. i
1c525 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 nt bRev;
1c526 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 /* True for DE
1c527 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 SCENDING sort or
1c528 64 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b der */. } aj;
1c529 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f . struct OP_O
1c52a 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 r_stack_vars {.
1c52b 20 20 20 20 20 69 6e 74 20 76 31 3b 20 20 20 20 int v1;
1c52c 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a /* Left operand:
1c52d 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 0==FALSE, 1==T
1c52e 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 RUE, 2==UNKNOWN
1c52f 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 or NULL */.
1c530 20 69 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 int v2; /* R
1c531 69 67 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d ight operand: 0=
1c532 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c =FALSE, 1==TRUE,
1c533 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 2==UNKNOWN or N
1c534 55 4c 4c 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b ULL */. } ak;
1c535 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c536 66 4e 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 fNot_stack_vars
1c537 7b 0a 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 {. int c;.
1c538 20 20 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72 } al;. str
1c539 75 63 74 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 uct OP_Column_st
1c53a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c53b 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 u32 payloadSize
1c53c 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 ; /* Number of
1c53d 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 bytes in the re
1c53e 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 cord */. i6
1c53f 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 4 payloadSize64;
1c540 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c541 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1c542 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 d */. int p
1c543 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 1; /*
1c544 20 50 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 P1 value of the
1c545 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 opcode */.
1c546 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 int p2;
1c547 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 /* column nu
1c548 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 mber to retrieve
1c549 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 */. VdbeCu
1c54a 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 rsor *pC; /*
1c54b 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
1c54c 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a */. char *z
1c54d 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 Rec; /* P
1c54e 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 ointer to comple
1c54f 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a te record-data *
1c550 2f 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 /. BtCursor
1c551 20 2a 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 *pCrsr; /* Th
1c552 65 20 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a e BTree cursor *
1c553 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 54 79 /. u32 *aTy
1c554 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 pe; /* aT
1c555 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 ype[i] holds the
1c556 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 numeric type of
1c557 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e the i-th column
1c558 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 */. u32 *a
1c559 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 Offset; /*
1c55a 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 aOffset[i] is of
1c55b 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 fset to start of
1c55c 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 data for i-th c
1c55d 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 olumn */. i
1c55e 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
1c55f 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 /* number of f
1c560 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 ields in the rec
1c561 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 ord */. int
1c562 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 len;
1c563 2f 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 /* The length of
1c564 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 the serialized
1c565 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c data for the col
1c566 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 umn */. int
1c567 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
1c568 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 /* Loop counter
1c569 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a */. char *z
1c56a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 Data; /* P
1c56b 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 art of the recor
1c56c 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 d being decoded
1c56d 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 */. Mem *pD
1c56e 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 est; /* W
1c56f 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 here to write th
1c570 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75 e extracted valu
1c571 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73 e */. Mem s
1c572 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a Mem; /*
1c573 20 46 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 For storing the
1c574 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 record being de
1c575 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 coded */. u
1c576 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 8 *zIdx;
1c577 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 /* Index into
1c578 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 header */.
1c579 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 u8 *zEndHdr;
1c57a 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1c57b 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65 first byte afte
1c57c 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a r the header */.
1c57d 20 20 20 20 20 20 75 33 32 20 6f 66 66 73 65 74 u32 offset
1c57e 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 ; /* Offs
1c57f 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 et into the data
1c580 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6f 66 */. u64 of
1c581 66 73 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 fset64; /*
1c582 36 34 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 64-bit offset.
1c583 36 34 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 64 bits needed t
1c584 6f 20 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 o catch overflow
1c585 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a */. int sz
1c586 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 Hdr; /*
1c587 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 Size of the head
1c588 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 er size field at
1c589 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 start of record
1c58a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 76 */. int av
1c58b 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 ail; /*
1c58c 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 Number of bytes
1c58d 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 of available dat
1c58e 61 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a a */. Mem *
1c58f 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a pReg; /*
1c590 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 PseudoTable inp
1c591 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 ut register */.
1c592 20 20 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72 } am;. str
1c593 75 63 74 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f uct OP_Affinity_
1c594 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c595 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c596 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 Affinity; /* T
1c597 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 he affinity to b
1c598 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20 e applied */.
1c599 20 20 20 63 68 61 72 20 63 41 66 66 3b 20 20 20 char cAff;
1c59a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
1c59b 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 single characte
1c59c 72 20 6f 66 20 61 66 66 69 6e 69 74 79 20 2a 2f r of affinity */
1c59d 0a 20 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 . } an;. s
1c59e 74 72 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 truct OP_MakeRec
1c59f 6f 72 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b ord_stack_vars {
1c5a0 0a 20 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 . u8 *zNewR
1c5a1 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a ecord; /*
1c5a2 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c A buffer to hol
1c5a3 64 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 d the data for t
1c5a4 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f he new record */
1c5a5 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 . Mem *pRec
1c5a6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c5a7 20 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 The new record
1c5a8 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61 */. u64 nDa
1c5a9 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
1c5aa 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1c5ab 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 es of data space
1c5ac 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 */. int nH
1c5ad 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 dr;
1c5ae 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1c5af 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 tes of header sp
1c5b0 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 ace */. i64
1c5b1 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 nByte;
1c5b2 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 /* Data spac
1c5b3 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 e required for t
1c5b4 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 his record */.
1c5b5 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 int nZero;
1c5b6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 /* Nu
1c5b7 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 mber of zero byt
1c5b8 65 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 es at the end of
1c5b9 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 the record */.
1c5ba 20 20 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 int nVarint
1c5bb 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1c5bc 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 umber of bytes i
1c5bd 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 n a varint */.
1c5be 20 20 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 u32 serial_t
1c5bf 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 ype; /* Ty
1c5c0 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 pe field */.
1c5c1 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 Mem *pData0;
1c5c2 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 /* Firs
1c5c3 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f t field to be co
1c5c4 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 mbined into the
1c5c5 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 record */.
1c5c6 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 Mem *pLast;
1c5c7 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 /* Last f
1c5c8 69 65 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f ield of the reco
1c5c9 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 rd */. int
1c5ca 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 nField;
1c5cb 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c5cc 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 fields in the re
1c5cd 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 cord */. ch
1c5ce 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 ar *zAffinity;
1c5cf 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 /* The affi
1c5d0 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 nity string for
1c5d1 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1c5d2 20 20 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 int file_for
1c5d3 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 mat; /* Fi
1c5d4 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 le format to use
1c5d5 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f for encoding */
1c5d6 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
1c5d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1c5d8 20 53 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a Space used in z
1c5d9 4e 65 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 NewRecord[] */.
1c5da 20 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 int len;
1c5db 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
1c5dc 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64 ength of a field
1c5dd 20 2a 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 */. } ao;.
1c5de 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e struct OP_Coun
1c5df 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c5e0 20 20 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b i64 nEntry;
1c5e1 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 . BtCursor
1c5e2 2a 70 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 *pCrsr;. } ap
1c5e3 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c5e4 53 61 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f Savepoint_stack_
1c5e5 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 vars {. int
1c5e6 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 p1;
1c5e7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c5e8 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 Value of P1 oper
1c5e9 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 and */. cha
1c5ea 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 r *zName;
1c5eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c5ec 4e 61 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e Name of savepoin
1c5ed 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e t */. int n
1c5ee 4e 61 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65 Name;. Save
1c5ef 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 point *pNew;.
1c5f0 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 Savepoint *pS
1c5f1 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 avepoint;.
1c5f2 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b Savepoint *pTmp;
1c5f3 0a 20 20 20 20 20 20 69 6e 74 20 69 53 61 76 65 . int iSave
1c5f4 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 point;. int
1c5f5 20 69 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 ii;. } aq;.
1c5f6 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74 struct OP_Aut
1c5f7 6f 43 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 oCommit_stack_va
1c5f8 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 rs {. int d
1c5f9 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1c5fa 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c ;. int iRol
1c5fb 6c 62 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 lback;. int
1c5fc 20 74 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d turnOnAC;. }
1c5fd 20 61 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 ar;. struct
1c5fe 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 OP_Transaction_s
1c5ff 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c600 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 Btree *pBt;.
1c601 20 20 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75 } as;. stru
1c602 63 74 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 ct OP_ReadCookie
1c603 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c604 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 int iMeta;.
1c605 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 int iDb;.
1c606 20 20 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b int iCookie;
1c607 0a 20 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 . } at;. s
1c608 74 72 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b truct OP_SetCook
1c609 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ie_stack_vars {.
1c60a 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 Db *pDb;.
1c60b 20 20 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 } au;. str
1c60c 75 63 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f uct OP_VerifyCoo
1c60d 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b kie_stack_vars {
1c60e 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61 . int iMeta
1c60f 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 ;. Btree *p
1c610 42 74 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 Bt;. } av;.
1c611 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e struct OP_Open
1c612 57 72 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 Write_stack_vars
1c613 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 {. int nFi
1c614 65 6c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e eld;. KeyIn
1c615 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 fo *pKeyInfo;.
1c616 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 int p2;.
1c617 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 int iDb;.
1c618 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 int wrFlag;.
1c619 20 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 Btree *pX;.
1c61a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a VdbeCursor *
1c61b 70 43 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a pCur;. Db *
1c61c 70 44 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 pDb;. } aw;.
1c61d 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 struct OP_Ope
1c61e 6e 45 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b nEphemeral_stack
1c61f 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c620 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 beCursor *pCx;.
1c621 20 20 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 } ax;. str
1c622 75 63 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 uct OP_OpenPseud
1c623 6f 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 o_stack_vars {.
1c624 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c625 2a 70 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a *pCx;. } ay;.
1c626 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c627 65 6b 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 ekGt_stack_vars
1c628 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b {. int res;
1c629 0a 20 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 . int oc;.
1c62a 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 VdbeCursor
1c62b 2a 70 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 *pC;. Unpac
1c62c 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 kedRecord r;.
1c62d 20 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 int nField;.
1c62e 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 i64 iKey;
1c62f 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 /* The rowid
1c630 20 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 we are to seek
1c631 74 6f 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a to */. } az;.
1c632 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 struct OP_Se
1c633 65 6b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ek_stack_vars {.
1c634 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c635 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a *pC;. } ba;.
1c636 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f struct OP_Fo
1c637 75 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b und_stack_vars {
1c638 0a 20 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61 . int alrea
1c639 64 79 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 dyExists;.
1c63a 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c63b 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 int res;.
1c63c 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 UnpackedRec
1c63d 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 ord *pIdxKey;.
1c63e 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f UnpackedReco
1c63f 72 64 20 72 3b 0a 20 20 20 20 20 20 63 68 61 72 rd r;. char
1c640 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 aTempRec[ROUND8
1c641 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 (sizeof(Unpacked
1c642 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f Record)) + sizeo
1c643 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 f(Mem)*3 + 7];.
1c644 20 20 20 7d 20 62 62 3b 0a 20 20 20 20 73 74 72 } bb;. str
1c645 75 63 74 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f uct OP_IsUnique_
1c646 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c647 20 20 20 75 31 36 20 69 69 3b 0a 20 20 20 20 20 u16 ii;.
1c648 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 VdbeCursor *pCx
1c649 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c64a 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75 *pCrsr;. u
1c64b 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 16 nField;.
1c64c 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20 Mem *aMx;.
1c64d 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1c64e 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 r;
1c64f 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e /* B-Tree in
1c650 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a dex search key *
1c651 2f 0a 20 20 20 20 20 20 69 36 34 20 52 3b 20 20 /. i64 R;
1c652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1c653 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
1c654 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 wid stored in re
1c655 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 gister P3 */.
1c656 20 7d 20 62 63 3b 0a 20 20 20 20 73 74 72 75 63 } bc;. struc
1c657 74 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 t OP_NotExists_s
1c658 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c659 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1c65a 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 ;. BtCursor
1c65b 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 *pCrsr;. i
1c65c 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36 nt res;. u6
1c65d 34 20 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64 4 iKey;. } bd
1c65e 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c65f 4e 65 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 NewRowid_stack_v
1c660 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 ars {. i64
1c661 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 v;
1c662 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f /* The new ro
1c663 77 69 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 wid */. Vdb
1c664 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 eCursor *pC;
1c665 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 /* Cursor of
1c666 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 table to get th
1c667 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 e new rowid */.
1c668 20 20 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 int res;
1c669 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
1c66a 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 esult of an sqli
1c66b 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a te3BtreeLast() *
1c66c 2f 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b /. int cnt;
1c66d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c66e 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d * Counter to lim
1c66f 69 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 it the number of
1c670 20 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 searches */.
1c671 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 Mem *pMem;
1c672 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
1c673 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 ister holding la
1c674 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20 rgest rowid for
1c675 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f AUTOINCREMENT */
1c676 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 . VdbeFrame
1c677 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a *pFrame; /*
1c678 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 Root frame of V
1c679 44 42 45 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b DBE */. } be;
1c67a 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c67b 6e 73 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 nsertInt_stack_v
1c67c 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 ars {. Mem
1c67d 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a *pData; /*
1c67e 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e MEM cell holdin
1c67f 67 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 g data for the r
1c680 65 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 ecord to be inse
1c681 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 rted */. Me
1c682 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 m *pKey;
1c683 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 /* MEM cell hold
1c684 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65 ing key for the
1c685 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 record */.
1c686 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 i64 iKey;
1c687 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 /* The intege
1c688 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 r ROWID or key f
1c689 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f or the record to
1c68a 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a be inserted */.
1c68b 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c68c 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f *pC; /* Curso
1c68d 72 20 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 r to table into
1c68e 77 68 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 which insert is
1c68f 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 written */.
1c690 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 int nZero;
1c691 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1c692 7a 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 zero-bytes to ap
1c693 70 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e pend */. in
1c694 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 t seekResult;
1c695 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69 /* Result of pri
1c696 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20 or seek or 0 if
1c697 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 no USESEEKRESULT
1c698 20 66 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 flag */. c
1c699 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 onst char *zDb;
1c69a 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d /* database nam
1c69b 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 e - used by the
1c69c 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 update hook */.
1c69d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1c69e 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 *zTbl; /* Table
1c69f 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 name - used by t
1c6a0 68 65 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a he opdate hook *
1c6a1 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 /. int op;
1c6a2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 /* Opc
1c6a3 6f 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 ode for update h
1c6a4 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 ook: SQLITE_UPDA
1c6a5 54 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 TE or SQLITE_INS
1c6a6 45 52 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b ERT */. } bf;
1c6a7 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44 . struct OP_D
1c6a8 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 elete_stack_vars
1c6a9 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 {. i64 iKe
1c6aa 79 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 y;. VdbeCur
1c6ab 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 sor *pC;. } b
1c6ac 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 g;. struct OP
1c6ad 5f 52 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f 76 _RowData_stack_v
1c6ae 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 ars {. Vdbe
1c6af 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 Cursor *pC;.
1c6b0 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1c6b1 72 3b 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a r;. u32 n;.
1c6b2 20 20 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 i64 n64;.
1c6b3 20 20 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 72 } bh;. str
1c6b4 75 63 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61 uct OP_Rowid_sta
1c6b5 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c6b6 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c6b7 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 i64 v;.
1c6b8 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 sqlite3_vtab
1c6b9 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f *pVtab;. co
1c6ba 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 nst sqlite3_modu
1c6bb 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 le *pModule;.
1c6bc 20 7d 20 62 69 3b 0a 20 20 20 20 73 74 72 75 63 } bi;. struc
1c6bd 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 t OP_NullRow_sta
1c6be 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c6bf 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c6c0 20 20 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74 } bj;. st
1c6c1 72 75 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61 ruct OP_Last_sta
1c6c2 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c6c3 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a VdbeCursor *pC;.
1c6c4 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a BtCursor *
1c6c5 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 pCrsr;. int
1c6c6 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a res;. } bk;.
1c6c7 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 struct OP_Re
1c6c8 77 69 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 wind_stack_vars
1c6c9 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 {. VdbeCurs
1c6ca 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 or *pC;. Bt
1c6cb 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1c6cc 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 int res;.
1c6cd 20 20 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 75 } bl;. stru
1c6ce 63 74 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b ct OP_Next_stack
1c6cf 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c6d0 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1c6d1 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 BtCursor *pC
1c6d2 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 rsr;. int r
1c6d3 65 73 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20 es;. } bm;.
1c6d4 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 49 struct OP_IdxI
1c6d5 6e 73 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 73 nsert_stack_vars
1c6d6 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 {. VdbeCur
1c6d7 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 sor *pC;. B
1c6d8 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c6d9 20 20 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a int nKey;.
1c6da 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 const char
1c6db 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e *zKey;. } bn
1c6dc 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c6dd 49 64 78 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f IdxDelete_stack_
1c6de 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 vars {. Vdb
1c6df 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 eCursor *pC;.
1c6e0 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 BtCursor *pCr
1c6e1 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 sr;. int re
1c6e2 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 s;. Unpacke
1c6e3 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d dRecord r;. }
1c6e4 20 62 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 bo;. struct
1c6e5 4f 50 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 63 OP_IdxRowid_stac
1c6e6 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 k_vars {. B
1c6e7 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1c6e8 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 VdbeCursor
1c6e9 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20 *pC;. i64
1c6ea 72 6f 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b rowid;. } bp;
1c6eb 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c6ec 64 78 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 20 dxGE_stack_vars
1c6ed 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 {. VdbeCurs
1c6ee 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e or *pC;. in
1c6ef 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 t res;. Unp
1c6f0 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 ackedRecord r;.
1c6f1 20 20 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 72 } bq;. str
1c6f2 75 63 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73 uct OP_Destroy_s
1c6f3 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c6f4 20 20 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 int iMoved;.
1c6f5 20 20 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 int iCnt;.
1c6f6 20 20 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b Vdbe *pVdbe;
1c6f7 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a . int iDb;.
1c6f8 20 20 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 74 } br;. st
1c6f9 72 75 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 ruct OP_Clear_st
1c6fa 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c6fb 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20 int nChange;.
1c6fc 20 20 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75 } bs;. stru
1c6fd 63 74 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c ct OP_CreateTabl
1c6fe 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 e_stack_vars {.
1c6ff 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 int pgno;.
1c700 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a int flags;.
1c701 20 20 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 Db *pDb;.
1c702 20 20 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 72 } bt;. str
1c703 75 63 74 20 4f 50 5f 50 61 72 73 65 53 63 68 65 uct OP_ParseSche
1c704 6d 61 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ma_stack_vars {.
1c705 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 int iDb;.
1c706 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
1c707 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 20 *zMaster;.
1c708 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 char *zSql;.
1c709 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 InitData initD
1c70a 61 74 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20 ata;. } bu;.
1c70b 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 74 struct OP_Int
1c70c 65 67 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76 egrityCk_stack_v
1c70d 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 ars {. int
1c70e 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e nRoot; /* N
1c70f 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 umber of tables
1c710 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 to check. (Numb
1c711 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 er of root pages
1c712 2e 29 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 .) */. int
1c713 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 *aRoot; /* A
1c714 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 rray of rootpage
1c715 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 numbers for tab
1c716 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 les to be checke
1c717 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a d */. int j
1c718 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f ; /* Lo
1c719 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
1c71a 20 20 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 int nErr;
1c71b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1c71c 20 65 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 errors reported
1c71d 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a */. char *
1c71e 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 z; /* Tex
1c71f 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 t of the error r
1c720 65 70 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d eport */. M
1c721 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f em *pnErr; /
1c722 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 * Register keepi
1c723 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f ng track of erro
1c724 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a rs remaining */.
1c725 20 20 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 74 } bv;. st
1c726 72 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 52 65 ruct OP_RowSetRe
1c727 61 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a ad_stack_vars {.
1c728 20 20 20 20 20 20 69 36 34 20 76 61 6c 3b 0a 20 i64 val;.
1c729 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 73 74 72 } bw;. str
1c72a 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 uct OP_RowSetTes
1c72b 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 t_stack_vars {.
1c72c 20 20 20 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 int iSet;.
1c72d 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b int exists;
1c72e 0a 20 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 73 . } bx;. s
1c72f 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d truct OP_Program
1c730 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 _stack_vars {.
1c731 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 int nMem;
1c732 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1c733 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 umber of memory
1c734 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75 registers for su
1c735 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 b-program */.
1c736 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 int nByte;
1c737 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1c738 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 tes of runtime s
1c739 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f pace required fo
1c73a 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f r sub-program */
1c73b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b . Mem *pRt;
1c73c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1c73d 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c * Register to al
1c73e 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 locate runtime s
1c73f 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 pace */. Me
1c740 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 m *pMem;
1c741 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f /* Used to
1c742 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 iterate through
1c743 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f memory cells */
1c744 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 . Mem *pEnd
1c745 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1c746 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 * Last memory ce
1c747 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20 ll in new array
1c748 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 72 61 */. VdbeFra
1c749 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 me *pFrame;
1c74a 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61 /* New vdbe fra
1c74b 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e me to execute in
1c74c 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 50 72 6f */. SubPro
1c74d 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 gram *pProgram;
1c74e 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d /* Sub-program
1c74f 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 to execute */.
1c750 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 void *t;
1c751 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1c752 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e Token identifyin
1c753 67 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 g trigger */.
1c754 20 7d 20 62 79 3b 0a 20 20 20 20 73 74 72 75 63 } by;. struc
1c755 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b t OP_Param_stack
1c756 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 _vars {. Vd
1c757 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b beFrame *pFrame;
1c758 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b . Mem *pIn;
1c759 0a 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73 . } bz;. s
1c75a 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f truct OP_MemMax_
1c75b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 stack_vars {.
1c75c 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 Mem *pIn1;.
1c75d 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 VdbeFrame *p
1c75e 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 63 61 3b Frame;. } ca;
1c75f 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 . struct OP_A
1c760 67 67 53 74 65 70 5f 73 74 61 63 6b 5f 76 61 72 ggStep_stack_var
1c761 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b s {. int n;
1c762 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 . int i;.
1c763 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 Mem *pMem;.
1c764 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a Mem *pRec;.
1c765 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f sqlite3_co
1c766 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20 ntext ctx;.
1c767 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1c768 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d 20 63 62 *apVal;. } cb
1c769 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f ;. struct OP_
1c76a 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76 AggFinal_stack_v
1c76b 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 ars {. Mem
1c76c 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 63 63 3b *pMem;. } cc;
1c76d 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 . struct OP_I
1c76e 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f ncrVacuum_stack_
1c76f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72 vars {. Btr
1c770 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63 ee *pBt;. } c
1c771 64 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 d;. struct OP
1c772 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61 _VBegin_stack_va
1c773 72 73 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c rs {. VTabl
1c774 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20 e *pVTab;. }
1c775 63 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f ce;. struct O
1c776 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 P_VOpen_stack_va
1c777 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 rs {. VdbeC
1c778 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 ursor *pCur;.
1c779 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f sqlite3_vtab_
1c77a 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 cursor *pVtabCur
1c77b 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 sor;. sqlit
1c77c 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1c77d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f sqlite3_mo
1c77e 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c77f 20 20 20 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 } cf;. str
1c780 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73 uct OP_VFilter_s
1c781 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c782 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 int nArg;.
1c783 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 int iQuery;.
1c784 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 const sqlite
1c785 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 3_module *pModul
1c786 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 e;. Mem *pQ
1c787 75 65 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20 uery;. Mem
1c788 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71 *pArgc;. sq
1c789 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f lite3_vtab_curso
1c78a 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a r *pVtabCursor;.
1c78b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c78c 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c78d 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c78e 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 r;. int res
1c78f 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 ;. int i;.
1c790 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 Mem **apArg
1c791 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 ;. } cg;.
1c792 73 74 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d struct OP_VColum
1c793 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 n_stack_vars {.
1c794 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 sqlite3_vta
1c795 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 b *pVtab;.
1c796 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f const sqlite3_mo
1c797 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 dule *pModule;.
1c798 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b Mem *pDest;
1c799 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 . sqlite3_c
1c79a 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b ontext sContext;
1c79b 0a 20 20 20 20 7d 20 63 68 3b 0a 20 20 20 20 73 . } ch;. s
1c79c 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73 truct OP_VNext_s
1c79d 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c79e 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1c79f 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e pVtab;. con
1c7a0 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1c7a1 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 e *pModule;.
1c7a2 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 int res;.
1c7a3 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 VdbeCursor *pCu
1c7a4 72 3b 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 20 r;. } ci;.
1c7a5 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61 struct OP_VRena
1c7a6 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a me_stack_vars {.
1c7a7 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 sqlite3_vt
1c7a8 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 ab *pVtab;.
1c7a9 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20 Mem *pName;.
1c7aa 20 7d 20 63 6a 3b 0a 20 20 20 20 73 74 72 75 63 } cj;. struc
1c7ab 74 20 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61 t OP_VUpdate_sta
1c7ac 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 ck_vars {.
1c7ad 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 sqlite3_vtab *pV
1c7ae 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 tab;. sqlit
1c7af 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1c7b0 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 le;. int nA
1c7b1 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b rg;. int i;
1c7b2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e . sqlite_in
1c7b3 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20 t64 rowid;.
1c7b4 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 Mem **apArg;.
1c7b5 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20 Mem *pX;.
1c7b6 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 72 75 63 } ck;. struc
1c7b7 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73 t OP_Pagecount_s
1c7b8 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 tack_vars {.
1c7b9 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 int p1;.
1c7ba 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 int nPage;.
1c7bb 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a Pager *pPager;.
1c7bc 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 } cl;. st
1c7bd 72 75 63 74 20 4f 50 5f 54 72 61 63 65 5f 73 74 ruct OP_Trace_st
1c7be 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 ack_vars {.
1c7bf 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20 char *zTrace;.
1c7c0 20 20 20 7d 20 63 6d 3b 0a 20 20 7d 20 75 3b 0a } cm;. } u;.
1c7c1 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74 /* End automat
1c7c2 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 ically generated
1c7c3 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a code. ********
1c7c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c7c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 ************/..
1c7c8 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 assert( p->magi
1c7c9 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1c7ca 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 N ); /* sqlite3
1c7cb 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73 _step() verifies
1c7cc 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 this */. asser
1c7cd 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 t( db->magic==SQ
1c7ce 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 LITE_MAGIC_BUSY
1c7cf 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1c7d0 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 MutexArrayEnter(
1c7d1 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d p);. if( p->rc=
1c7d2 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b =SQLITE_NOMEM ){
1c7d3 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1c7d4 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 pens if a malloc
1c7d5 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c () inside a call
1c7d6 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 to sqlite3_colu
1c7d7 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20 mn_text() or.
1c7d8 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 ** sqlite3_colu
1c7d9 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c mn_text16() fail
1c7da 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f ed. */. goto
1c7db 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 no_mem;. }. a
1c7dc 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 ssert( p->rc==SQ
1c7dd 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 LITE_OK || p->rc
1c7de 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b ==SQLITE_BUSY );
1c7df 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 . p->rc = SQLIT
1c7e0 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 E_OK;. assert(
1c7e1 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b p->explain==0 );
1c7e2 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 . p->pResultSet
1c7e3 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 = 0;. db->busy
1c7e4 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 Handler.nBusy =
1c7e5 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 0;. CHECK_FOR_I
1c7e6 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 NTERRUPT;. sqli
1c7e7 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71 te3VdbeIOTraceSq
1c7e8 6c 28 70 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 l(p);.#ifndef SQ
1c7e9 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 LITE_OMIT_PROGRE
1c7ea 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 63 68 SS_CALLBACK. ch
1c7eb 65 63 6b 50 72 6f 67 72 65 73 73 20 3d 20 64 62 eckProgress = db
1c7ec 2d 3e 78 50 72 6f 67 72 65 73 73 21 3d 30 3b 0a ->xProgress!=0;.
1c7ed 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 #endif.#ifdef SQ
1c7ee 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c LITE_DEBUG. sql
1c7ef 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d ite3BeginBenignM
1c7f0 61 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 alloc();. if( p
1c7f1 2d 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 ->pc==0 . && (
1c7f2 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 (p->db->flags &
1c7f3 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 SQLITE_VdbeListi
1c7f4 6e 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 ng) || fileExist
1c7f5 73 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c s(db, "vdbe_expl
1c7f6 61 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 ain")). ){.
1c7f7 69 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 int i;. print
1c7f8 66 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 f("VDBE Program
1c7f9 4c 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 Listing:\n");.
1c7fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c7fb 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f ntSql(p);. fo
1c7fc 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b r(i=0; i<p->nOp;
1c7fd 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c i++){. sql
1c7fe 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 ite3VdbePrintOp(
1c7ff 73 74 64 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b stdout, i, &aOp[
1c800 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 i]);. }. }.
1c801 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73 28 if( fileExists(
1c802 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63 65 22 db, "vdbe_trace"
1c803 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 ) ){. p->trac
1c804 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a e = stdout;. }.
1c805 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 sqlite3EndBeni
1c806 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 gnMalloc();.#end
1c807 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 if. for(pc=p->p
1c808 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b c; rc==SQLITE_OK
1c809 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 ; pc++){. ass
1c80a 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20 70 63 ert( pc>=0 && pc
1c80b 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 <p->nOp );. i
1c80c 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
1c80d 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 led ) goto no_me
1c80e 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 m;.#ifdef VDBE_P
1c80f 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 ROFILE. origP
1c810 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74 61 72 c = pc;. star
1c811 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74 69 6d t = sqlite3Hwtim
1c812 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 e();.#endif.
1c813 70 4f 70 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a pOp = &aOp[pc];.
1c814 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c . /* Only all
1c815 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51 ow tracing if SQ
1c816 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65 LITE_DEBUG is de
1c817 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 fined.. */.#i
1c818 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
1c819 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 G. if( p->tra
1c81a 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ce ){. if(
1c81b 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 pc==0 ){.
1c81c 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78 printf("VDBE Ex
1c81d 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e ecution Trace:\n
1c81e 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ");. sqli
1c81f 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 te3VdbePrintSql(
1c820 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
1c821 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 sqlite3VdbePri
1c822 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 ntOp(p->trace, p
1c823 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 c, pOp);. }.
1c824 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d if( p->trace=
1c825 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 =0 && pc==0 ){.
1c826 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 sqlite3Begi
1c827 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b nBenignMalloc();
1c828 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45 . if( fileE
1c829 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f xists(db, "vdbe_
1c82a 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20 sqltrace") ){.
1c82b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1c82c 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 ePrintSql(p);.
1c82d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 }. sqli
1c82e 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c te3EndBenignMall
1c82f 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 oc();. }.#end
1c830 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f if. .. /
1c831 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 * Check to see i
1c832 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d f we need to sim
1c833 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75 ulate an interru
1c834 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 pt. This only h
1c835 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 appens. ** if
1c836 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69 we have a speci
1c837 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 al test build..
1c838 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c */.#ifdef SQL
1c839 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28 ITE_TEST. if(
1c83a 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 sqlite3_interru
1c83b 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 pt_count>0 ){.
1c83c 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 sqlite3_inte
1c83d 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 rrupt_count--;.
1c83e 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1c83f 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 _interrupt_count
1c840 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ==0 ){. s
1c841 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 qlite3_interrupt
1c842 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 (db);. }.
1c843 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e }.#endif..#ifn
1c844 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1c845 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 PROGRESS_CALLBAC
1c846 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 K. /* Call th
1c847 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 e progress callb
1c848 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e ack if it is con
1c849 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20 figured and the
1c84a 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a required number.
1c84b 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f ** of VDBE o
1c84c 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65 ps have been exe
1c84d 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69 cuted (either si
1c84e 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74 nce this invocat
1c84f 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 ion of. ** sq
1c850 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20 lite3VdbeExec()
1c851 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69 or since last ti
1c852 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 me the progress
1c853 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c callback was cal
1c854 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 led).. ** If
1c855 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c the progress cal
1c856 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f lback returns no
1c857 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65 n-zero, exit the
1c858 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 virtual machine
1c859 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 with. ** a r
1c85a 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 eturn code SQLIT
1c85b 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a E_ABORT.. */.
1c85c 20 20 20 20 69 66 28 20 63 68 65 63 6b 50 72 6f if( checkPro
1c85d 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69 gress ){. i
1c85e 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 f( db->nProgress
1c85f 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 Ops==nProgressOp
1c860 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 s ){. int
1c861 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66 prc;. if
1c862 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f ( sqlite3SafetyO
1c863 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 ff(db) ) goto ab
1c864 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1c865 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d e;. prc =
1c866 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62 db->xProgress(db
1c867 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b ->pProgressArg);
1c868 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
1c869 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1c86a 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1c86b 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1c86c 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20 if( prc!=0
1c86d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 ){. rc
1c86e 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 = SQLITE_INTERRU
1c86f 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f PT;. go
1c870 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
1c871 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 lt;. }.
1c872 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1c873 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a ps = 0;. }.
1c874 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f nProgressO
1c875 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 ps++;. }.#end
1c876 69 66 0a 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e if.. /* On an
1c877 79 20 6f 70 63 6f 64 65 20 77 69 74 68 20 74 68 y opcode with th
1c878 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c 61 73 e "out2-prerelas
1c879 65 22 20 74 61 67 2c 20 66 72 65 65 20 61 6e 79 e" tag, free any
1c87a 0a 20 20 20 20 2a 2a 20 65 78 74 65 72 6e 61 6c . ** external
1c87b 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 75 74 allocations out
1c87c 20 6f 66 20 6d 65 6d 5b 70 32 5d 20 61 6e 64 20 of mem[p2] and
1c87d 73 65 74 20 6d 65 6d 5b 70 32 5d 20 74 6f 20 62 set mem[p2] to b
1c87e 65 0a 20 20 20 20 2a 2a 20 61 6e 20 75 6e 64 65 e. ** an unde
1c87f 66 69 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 fined integer.
1c880 4f 70 63 6f 64 65 73 20 77 69 6c 6c 20 65 69 74 Opcodes will eit
1c881 68 65 72 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 her fill in the
1c882 69 6e 74 65 67 65 72 0a 20 20 20 20 2a 2a 20 76 integer. ** v
1c883 61 6c 75 65 20 6f 72 20 63 6f 6e 76 65 72 74 20 alue or convert
1c884 6d 65 6d 5b 70 32 5d 20 74 6f 20 61 20 64 69 66 mem[p2] to a dif
1c885 66 65 72 65 6e 74 20 74 79 70 65 2e 0a 20 20 20 ferent type..
1c886 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
1c887 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 3d 3d 73 71 pOp->opflags==sq
1c888 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 lite3OpcodePrope
1c889 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d rty[pOp->opcode]
1c88a 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d );. if( pOp-
1c88b 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 >opflags & OPFLG
1c88c 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 _OUT2_PRERELEASE
1c88d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 ){. assert
1c88e 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 ( pOp->p2>0 );.
1c88f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 assert( pOp
1c890 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b ->p2<=p->nMem );
1c891 0a 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 61 . pOut = &a
1c892 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1c893 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1c894 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1c895 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 l(pOut);. p
1c896 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d Out->flags = MEM
1c897 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 _Int;. }..
1c898 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b /* Sanity check
1c899 69 6e 67 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65 ing on other ope
1c89a 72 61 6e 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 rands */.#ifdef
1c89b 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 SQLITE_DEBUG.
1c89c 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 if( (pOp->opfla
1c89d 67 73 20 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 gs & OPFLG_IN1)!
1c89e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 =0 ){. asse
1c89f 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b rt( pOp->p1>0 );
1c8a0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c8a1 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 Op->p1<=p->nMem
1c8a2 29 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 );. REGISTE
1c8a3 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c R_TRACE(pOp->p1,
1c8a4 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29 &aMem[pOp->p1])
1c8a5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
1c8a6 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 (pOp->opflags &
1c8a7 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b OPFLG_IN2)!=0 ){
1c8a8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1c8a9 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 Op->p2>0 );.
1c8aa 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c8ab 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 2<=p->nMem );.
1c8ac 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 REGISTER_TRA
1c8ad 43 45 28 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 CE(pOp->p2, &aMe
1c8ae 6d 5b 70 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 m[pOp->p2]);.
1c8af 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4f 70 2d }. if( (pOp-
1c8b0 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 >opflags & OPFLG
1c8b1 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 _IN3)!=0 ){.
1c8b2 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1c8b3 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 3>0 );. ass
1c8b4 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d ert( pOp->p3<=p-
1c8b5 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 52 >nMem );. R
1c8b6 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f EGISTER_TRACE(pO
1c8b7 70 2d 3e 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 p->p3, &aMem[pOp
1c8b8 2d 3e 70 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 ->p3]);. }.
1c8b9 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c if( (pOp->opfl
1c8ba 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 ags & OPFLG_OUT2
1c8bb 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 )!=0 ){. as
1c8bc 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 sert( pOp->p2>0
1c8bd 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1c8be 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 pOp->p2<=p->nMe
1c8bf 6d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 m );. }. i
1c8c0 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 f( (pOp->opflags
1c8c1 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d & OPFLG_OUT3)!=
1c8c2 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 0 ){. asser
1c8c3 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a t( pOp->p3>0 );.
1c8c4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f assert( pO
1c8c5 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1c8c6 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 ;. }.#endif.
1c8c7 20 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f . switch( pO
1c8c8 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a p->opcode ){../*
1c8c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c8cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
1c8ce 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 What follows is
1c8cf 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 63 68 a massive switch
1c8d0 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 72 65 statement where
1c8d1 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 6c 65 each case imple
1c8d2 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 61 72 ments a.** separ
1c8d3 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ate instruction
1c8d4 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d in the virtual m
1c8d5 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 20 66 achine. If we f
1c8d6 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 6c 0a ollow the usual.
1c8d7 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63 ** indentation c
1c8d8 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68 onventions, each
1c8d9 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 65 20 case should be
1c8da 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 73 70 indented by 6 sp
1c8db 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 aces. But.** th
1c8dc 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 20 77 at is a lot of w
1c8dd 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e 20 74 asted space on t
1c8de 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20 he left margin.
1c8df 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 69 74 So the code wit
1c8e0 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 74 63 hin.** the switc
1c8e1 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c h statement will
1c8e2 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f 6e 76 break with conv
1c8e3 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c ention and be fl
1c8e4 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65 ush-left. Anothe
1c8e5 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 r.** big comment
1c8e6 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 (similar to thi
1c8e7 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b s one) will mark
1c8e8 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 the point in th
1c8e9 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20 e code where.**
1c8ea 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 we transition ba
1c8eb 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 ck to normal ind
1c8ec 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 entation..**.**
1c8ed 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f The formatting o
1c8ee 66 20 65 61 63 68 20 63 61 73 65 20 69 73 20 69 f each case is i
1c8ef 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 20 6d mportant. The m
1c8f0 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 akefile for SQLi
1c8f1 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 te.** generates
1c8f2 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f 70 63 two C files "opc
1c8f3 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 odes.h" and "opc
1c8f4 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 6e 6e odes.c" by scann
1c8f5 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65 ing this.** file
1c8f6 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e looking for lin
1c8f7 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 es that begin wi
1c8f8 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 th "case OP_".
1c8f9 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 The opcodes.h fi
1c8fa 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 les.** will be f
1c8fb 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66 69 illed with #defi
1c8fc 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75 6e nes that give un
1c8fd 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61 6c ique integer val
1c8fe 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f ues to each.** o
1c8ff 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f 70 pcode and the op
1c900 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73 20 codes.c file is
1c901 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20 61 filled with an a
1c902 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73 20 rray of strings
1c903 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73 74 where.** each st
1c904 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 6d 62 ring is the symb
1c905 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 74 68 olic name for th
1c906 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 e corresponding
1c907 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 65 0a opcode. If the.
1c908 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d 65 6e ** case statemen
1c909 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 t is followed by
1c90a 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 a comment of th
1c90b 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20 e form "/# same
1c90c 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 as ... #/".** th
1c90d 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73 at comment is us
1c90e 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
1c90f 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 the particular v
1c910 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f alue of the opco
1c911 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 de..**.** Other
1c912 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 65 20 keywords in the
1c913 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c comment that fol
1c914 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 20 61 lows each case a
1c915 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f re used to.** co
1c916 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 46 4c nstruct the OPFL
1c917 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 76 61 G_INITIALIZER va
1c918 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 61 6c lue that initial
1c919 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f 70 65 izes opcodePrope
1c91a 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 rty[]..** Keywor
1c91b 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c ds include: in1,
1c91c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f in2, in3, out2_
1c91d 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 74 32 prerelease, out2
1c91e 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20 , out3. See.**
1c91f 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 the mkopcodeh.aw
1c920 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 64 64 k script for add
1c921 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 itional informat
1c922 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d ion..**.** Docum
1c923 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20 56 entation about V
1c924 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20 67 DBE opcodes is g
1c925 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61 6e enerated by scan
1c926 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a 2a ning this file.*
1c927 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74 * for lines of t
1c928 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63 hat contain "Opc
1c929 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e ode:". That lin
1c92a 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65 71 e and all subseq
1c92b 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 uent.** comment
1c92c 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 69 lines are used i
1c92d 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f 6e n the generation
1c92e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 of the opcode.h
1c92f 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f tml documentatio
1c930 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a n.** file..**.**
1c931 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 SUMMARY:.**.**
1c932 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20 69 Formatting i
1c933 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 s important to s
1c934 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61 6e cripts that scan
1c935 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 this file..**
1c936 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61 74 Do not deviat
1c937 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 e from the forma
1c938 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72 72 tting style curr
1c939 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a ently in use..**
1c93a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
1c93b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c93c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c93d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1c93e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a **************/.
1c93f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74 ./* Opcode: Got
1c940 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a o * P2 * * *.**.
1c941 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f ** An unconditio
1c942 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 nal jump to addr
1c943 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e ess P2..** The n
1c944 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ext instruction
1c945 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 62 65 executed will be
1c946 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 .** the one at
1c947 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 74 68 index P2 from th
1c948 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a e beginning of.*
1c949 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a * the program..*
1c94a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 /.case OP_Goto:
1c94b 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1c94c 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b jump */. CHECK
1c94d 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a _FOR_INTERRUPT;.
1c94e 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1c94f 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 1;. break;.}..
1c950 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 /* Opcode: Gosu
1c951 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a b P1 P2 * * *.**
1c952 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75 .** Write the cu
1c953 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 6f 6e rrent address on
1c954 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a to register P1.*
1c955 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 * and then jump
1c956 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a to address P2..*
1c957 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a /.case OP_Gosub:
1c958 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a { /*
1c959 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 jump, in1 */.
1c95a 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1c95b 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1c95c 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 (pIn1->flags &
1c95d 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 MEM_Dyn)==0 );.
1c95e 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d pIn1->flags = M
1c95f 45 4d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e EM_Int;. pIn1->
1c960 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 u.i = pc;. REGI
1c961 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1c962 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 p1, pIn1);. pc
1c963 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1c964 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1c965 63 6f 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 code: Return P1
1c966 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a * * * *.**.** J
1c967 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ump to the next
1c968 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 instruction afte
1c969 72 20 74 68 65 20 61 64 64 72 65 73 73 20 69 6e r the address in
1c96a 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f register P1..*/
1c96b 0a 63 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a .case OP_Return:
1c96c 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1c96d 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 in1 */. pIn1 =
1c96e 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1c96f 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1c970 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 flags & MEM_Int
1c971 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 );. pc = (int)p
1c972 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 In1->u.i;. brea
1c973 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1c974 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a Yield P1 * * *
1c975 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 *.**.** Swap th
1c976 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 e program counte
1c977 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 r with the value
1c978 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
1c979 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c .*/.case OP_Yiel
1c97a 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 d: {
1c97b 2f 2a 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 /* in1 */.#if 0
1c97c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1c97d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1c97e 2e 61 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 .aa */. int pcD
1c97f 65 73 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c est;.#endif /* l
1c980 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1c981 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a oved into u.aa *
1c982 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d /. pIn1 = &aMem
1c983 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1c984 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 ert( (pIn1->flag
1c985 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 s & MEM_Dyn)==0
1c986 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 );. pIn1->flags
1c987 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e = MEM_Int;. u.
1c988 61 61 2e 70 63 44 65 73 74 20 3d 20 28 69 6e 74 aa.pcDest = (int
1c989 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 )pIn1->u.i;. pI
1c98a 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 n1->u.i = pc;.
1c98b 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 REGISTER_TRACE(p
1c98c 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 Op->p1, pIn1);.
1c98d 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 pc = u.aa.pcDes
1c98e 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f t;. break;.}../
1c98f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 * Opcode: HaltI
1c990 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50 33 20 fNull P1 P2 P3
1c991 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b P4 *.**.** Check
1c992 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1c993 67 69 73 74 65 72 20 50 33 2e 20 20 49 66 20 69 gister P3. If i
1c994 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 s is NULL then H
1c995 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 alt using.** par
1c996 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c 20 61 ameter P1, P2, a
1c997 6e 64 20 50 34 20 61 73 20 69 66 20 74 68 69 73 nd P4 as if this
1c998 20 77 65 72 65 20 61 20 48 61 6c 74 20 69 6e 73 were a Halt ins
1c999 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 truction. If th
1c99a 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 e.** value in re
1c99b 67 69 73 74 65 72 20 50 33 20 69 73 20 6e 6f 74 gister P3 is not
1c99c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 NULL, then this
1c99d 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f routine is a no
1c99e 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f -op..*/.case OP_
1c99f 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 HaltIfNull: {
1c9a0 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 70 /* in3 */. p
1c9a1 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In3 = &aMem[pOp-
1c9a2 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e >p3];. if( (pIn
1c9a3 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 3->flags & MEM_N
1c9a4 75 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b ull)==0 ) break;
1c9a5 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 . /* Fall throu
1c9a6 67 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 gh into OP_Halt
1c9a7 2a 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a */.}../* Opcode:
1c9a8 20 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50 Halt P1 P2 * P
1c9a9 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 4 *.**.** Exit i
1c9aa 6d 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c mmediately. All
1c9ab 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 open cursors, e
1c9ac 74 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a tc are closed.**
1c9ad 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a automatically..
1c9ae 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 **.** P1 is the
1c9af 72 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75 result code retu
1c9b0 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f rned by sqlite3_
1c9b1 65 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f exec(), sqlite3_
1c9b2 72 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 reset(),.** or s
1c9b3 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 qlite3_finalize(
1c9b4 29 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c ). For a normal
1c9b5 20 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 halt, this shou
1c9b6 6c 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 ld be SQLITE_OK
1c9b7 28 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f (0)..** For erro
1c9b8 72 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f rs, it can be so
1c9b9 6d 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 me other value.
1c9ba 20 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50 If P1!=0 then P
1c9bb 32 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 2 will determine
1c9bc 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e .** whether or n
1c9bd 6f 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 ot to rollback t
1c9be 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 he current trans
1c9bf 61 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 action. Do not
1c9c0 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 rollback.** if P
1c9c1 32 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 2==OE_Fail. Do t
1c9c2 68 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 he rollback if P
1c9c3 32 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 2==OE_Rollback.
1c9c4 20 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 If P2==OE_Abort
1c9c5 2c 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f ,.** then back o
1c9c6 75 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 ut all changes t
1c9c7 68 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 hat have occurre
1c9c8 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78 d during this ex
1c9c9 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a ecution of the.*
1c9ca 2a 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e * VDBE, but do n
1c9cb 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 ot rollback the
1c9cc 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a transaction. .**
1c9cd 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 .** If P4 is not
1c9ce 20 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 null then it is
1c9cf 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
1c9d0 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 e string..**.**
1c9d1 54 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c There is an impl
1c9d2 69 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22 ied "Halt 0 0 0"
1c9d3 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 instruction ins
1c9d4 65 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72 erted at the ver
1c9d5 79 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 y end of.** ever
1c9d6 79 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 y program. So a
1c9d7 20 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c jump past the l
1c9d8 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ast instruction
1c9d9 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a of the program.*
1c9da 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 * is the same as
1c9db 20 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e executing Halt.
1c9dc 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 .*/.case OP_Halt
1c9dd 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 : {. if( pOp->p
1c9de 31 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 1==SQLITE_OK &&
1c9df 70 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 p->pFrame ){.
1c9e0 20 2f 2a 20 48 61 6c 74 20 74 68 65 20 73 75 62 /* Halt the sub
1c9e1 2d 70 72 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e -program. Return
1c9e2 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 control to the
1c9e3 70 61 72 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f parent frame. */
1c9e4 0a 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a . VdbeFrame *
1c9e5 70 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 pFrame = p->pFra
1c9e6 6d 65 3b 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d me;. p->pFram
1c9e7 65 20 3d 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 e = pFrame->pPar
1c9e8 65 6e 74 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61 ent;. p->nFra
1c9e9 6d 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 me--;. sqlite
1c9ea 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 3VdbeSetChanges(
1c9eb 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b db, p->nChange);
1c9ec 0a 20 20 20 20 70 63 20 3d 20 73 71 6c 69 74 65 . pc = sqlite
1c9ed 33 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 3VdbeFrameRestor
1c9ee 65 28 70 46 72 61 6d 65 29 3b 0a 20 20 20 20 69 e(pFrame);. i
1c9ef 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 f( pOp->p2==OE_I
1c9f0 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 2f gnore ){. /
1c9f1 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 * Instruction pc
1c9f2 20 69 73 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 is the OP_Progr
1c9f3 61 6d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 am that invoked
1c9f4 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 the sub-program
1c9f5 0a 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e . ** curren
1c9f6 74 6c 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64 tly being halted
1c9f7 2e 20 49 66 20 74 68 65 20 70 32 20 69 6e 73 74 . If the p2 inst
1c9f8 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 ruction of this
1c9f9 4f 50 5f 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a OP_Halt. **
1c9fa 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
1c9fb 73 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 set to OE_Ignore
1c9fc 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70 , then the sub-p
1c9fd 72 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69 rogram is throwi
1c9fe 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49 ng. ** an I
1c9ff 47 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e GNORE exception.
1ca00 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 In this case ju
1ca01 6d 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 mp to the addres
1ca02 73 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 s specified.
1ca03 20 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20 6f ** as the p2 o
1ca04 66 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50 f the calling OP
1ca05 5f 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 _Program. */.
1ca06 20 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b pc = p->aOp[
1ca07 70 63 5d 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a pc].p2-1;. }.
1ca08 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 aOp = p->aOp
1ca09 3b 0a 20 20 20 20 61 4d 65 6d 20 3d 20 70 2d 3e ;. aMem = p->
1ca0a 61 4d 65 6d 3b 0a 20 20 20 20 62 72 65 61 6b 3b aMem;. break;
1ca0b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 . }.. p->rc =
1ca0c 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 pOp->p1;. p->er
1ca0d 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 rorAction = (u8)
1ca0e 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 pOp->p2;. p->pc
1ca0f 20 3d 20 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 = pc;. if( pOp
1ca10 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 ->p4.z ){. sq
1ca11 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1ca12 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1ca13 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 "%s", pOp->p4.z)
1ca14 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c ;. }. rc = sql
1ca15 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b ite3VdbeHalt(p);
1ca16 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 . assert( rc==S
1ca17 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 QLITE_BUSY || rc
1ca18 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 ==SQLITE_OK || r
1ca19 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 c==SQLITE_ERROR
1ca1a 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c );. if( rc==SQL
1ca1b 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 ITE_BUSY ){.
1ca1c 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c p->rc = rc = SQL
1ca1d 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 ITE_BUSY;. }els
1ca1e 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 e{. assert( r
1ca1f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c==SQLITE_OK ||
1ca20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f p->rc==SQLITE_CO
1ca21 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 NSTRAINT );.
1ca22 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 assert( rc==SQLI
1ca23 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 TE_OK || db->nDe
1ca24 66 65 72 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a ferredCons>0 );.
1ca25 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f rc = p->rc ?
1ca26 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 SQLITE_ERROR :
1ca27 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d SQLITE_DONE;. }
1ca28 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 . goto vdbe_ret
1ca29 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 urn;.}../* Opcod
1ca2a 65 3a 20 49 6e 74 65 67 65 72 20 50 31 20 50 32 e: Integer P1 P2
1ca2b 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 * * *.**.** The
1ca2c 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 32-bit integer
1ca2d 76 61 6c 75 65 20 50 31 20 69 73 20 77 72 69 74 value P1 is writ
1ca2e 74 65 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 ten into registe
1ca2f 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 r P2..*/.case OP
1ca30 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20 20 20 20 _Integer: {
1ca31 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1ca32 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 elease */. pOut
1ca33 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b ->u.i = pOp->p1;
1ca34 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ca35 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 Opcode: Int64 *
1ca36 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * P4 *.**.**
1ca37 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1ca38 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 to a 64-bit inte
1ca39 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 ger value..** Wr
1ca3a 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 ite that value i
1ca3b 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e nto register P2.
1ca3c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 .*/.case OP_Int6
1ca3d 34 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 4: { /
1ca3e 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1ca3f 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 e */. assert( p
1ca40 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 Op->p4.pI64!=0 )
1ca41 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 ;. pOut->u.i =
1ca42 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 *pOp->p4.pI64;.
1ca43 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1ca44 63 6f 64 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 code: Real * P2
1ca45 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1ca46 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 is a pointer to
1ca47 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e a 64-bit floatin
1ca48 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a g point value..*
1ca49 2a 20 57 72 69 74 65 20 74 68 61 74 20 76 61 6c * Write that val
1ca4a 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 ue into register
1ca4b 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P2..*/.case OP_
1ca4c 52 65 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 Real: {
1ca4d 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1ca4e 5f 46 4c 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 _FLOAT, out2-pre
1ca4f 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 release */. pOu
1ca50 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 t->flags = MEM_R
1ca51 65 61 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 21 eal;. assert( !
1ca52 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f sqlite3IsNaN(*pO
1ca53 70 2d 3e 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a p->p4.pReal) );.
1ca54 20 20 70 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 pOut->r = *pOp
1ca55 2d 3e 70 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 ->p4.pReal;. br
1ca56 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1ca57 65 3a 20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 e: String8 * P2
1ca58 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 * P4 *.**.** P4
1ca59 70 6f 69 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 points to a nul
1ca5a 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 terminated UTF-8
1ca5b 20 73 74 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 string. This op
1ca5c 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 code is transfor
1ca5d 6d 65 64 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 med .** into an
1ca5e 4f 50 5f 53 74 72 69 6e 67 20 62 65 66 6f 72 65 OP_String before
1ca5f 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 20 it is executed
1ca60 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69 for the first ti
1ca61 6d 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 me..*/.case OP_S
1ca62 74 72 69 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 tring8: {
1ca63 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f /* same as TK_
1ca64 53 54 52 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 STRING, out2-pre
1ca65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 release */. ass
1ca66 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d ert( pOp->p4.z!=
1ca67 30 20 29 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 0 );. pOp->opco
1ca68 64 65 20 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a de = OP_String;.
1ca69 20 20 70 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 pOp->p1 = sqli
1ca6a 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d te3Strlen30(pOp-
1ca6b 3e 70 34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 >p4.z);..#ifndef
1ca6c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 SQLITE_OMIT_UTF
1ca6d 31 36 0a 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 16. if( encodin
1ca6e 67 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 g!=SQLITE_UTF8 )
1ca6f 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1ca70 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 e3VdbeMemSetStr(
1ca71 70 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c pOut, pOp->p4.z,
1ca72 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 -1, SQLITE_UTF8
1ca73 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 , SQLITE_STATIC)
1ca74 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 ;. if( rc==SQ
1ca75 4c 49 54 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f LITE_TOOBIG ) go
1ca76 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 to too_big;.
1ca77 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 if( SQLITE_OK!=s
1ca78 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1ca79 45 6e 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 Encoding(pOut, e
1ca7a 6e 63 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 ncoding) ) goto
1ca7b 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 no_mem;. asse
1ca7c 72 74 28 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f rt( pOut->zMallo
1ca7d 63 3d 3d 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 c==pOut->z );.
1ca7e 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e assert( pOut->
1ca7f 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 flags & MEM_Dyn
1ca80 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 );. pOut->zMa
1ca81 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f lloc = 0;. pO
1ca82 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d ut->flags |= MEM
1ca83 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 _Static;. pOu
1ca84 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d t->flags &= ~MEM
1ca85 5f 44 79 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f _Dyn;. if( pO
1ca86 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 p->p4type==P4_DY
1ca87 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 NAMIC ){. s
1ca88 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1ca89 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 pOp->p4.z);.
1ca8a 20 7d 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 }. pOp->p4ty
1ca8b 70 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b pe = P4_DYNAMIC;
1ca8c 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d . pOp->p4.z =
1ca8d 20 70 4f 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f pOut->z;. pO
1ca8e 70 2d 3e 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b p->p1 = pOut->n;
1ca8f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 . }.#endif. if
1ca90 28 20 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c ( pOp->p1>db->aL
1ca91 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1ca92 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1ca93 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 goto too_big;.
1ca94 20 7d 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 }. /* Fall thr
1ca95 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1ca96 20 63 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 case, OP_String
1ca97 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f */.}. ./* Opco
1ca98 64 65 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 de: String P1 P2
1ca99 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 * P4 *.**.** Th
1ca9a 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 e string value P
1ca9b 34 20 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 4 of length P1 (
1ca9c 62 79 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 bytes) is stored
1ca9d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e in register P2.
1ca9e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 .*/.case OP_Stri
1ca9f 6e 67 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f ng: { /
1caa0 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 * out2-prereleas
1caa1 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 e */. assert( p
1caa2 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 Op->p4.z!=0 );.
1caa3 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d pOut->flags = M
1caa4 45 4d 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 EM_Str|MEM_Stati
1caa5 63 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f c|MEM_Term;. pO
1caa6 75 74 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e ut->z = pOp->p4.
1caa7 7a 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 z;. pOut->n = p
1caa8 4f 70 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e Op->p1;. pOut->
1caa9 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a enc = encoding;.
1caaa 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1caab 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 BSIZE(pOut);. b
1caac 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1caad 64 65 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 de: Null * P2 *
1caae 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1caaf 61 20 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 a NULL into regi
1cab0 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 ster P2..*/.case
1cab1 20 4f 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 OP_Null: {
1cab2 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1cab3 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f erelease */. pO
1cab4 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f ut->flags = MEM_
1cab5 4e 75 6c 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Null;. break;.}
1cab6 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c .../* Opcode: Bl
1cab7 6f 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a ob P1 P2 * P4.**
1cab8 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f .** P4 points to
1cab9 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 a blob of data
1caba 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 P1 bytes long.
1cabb 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c Store this.** bl
1cabc 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ob in register P
1cabd 32 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 2. This instruct
1cabe 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 ion is not coded
1cabf 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 directly.** by
1cac0 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e the compiler. In
1cac1 73 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 stead, the compi
1cac2 6c 65 72 20 6c 61 79 65 72 20 73 70 65 63 69 66 ler layer specif
1cac3 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 ies.** an OP_Hex
1cac4 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 Blob opcode, wit
1cac5 68 20 74 68 65 20 68 65 78 20 73 74 72 69 6e 67 h the hex string
1cac6 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
1cac7 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 of.** the blob a
1cac8 73 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 s P4. This opcod
1cac9 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 e is transformed
1caca 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a to an OP_Blob.*
1cacb 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 * the first time
1cacc 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e it is executed.
1cacd 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 .*/.case OP_Blob
1cace 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1cacf 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1cad0 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 lease */. asser
1cad1 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 t( pOp->p1 <= SQ
1cad2 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 LITE_MAX_LENGTH
1cad3 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
1cad4 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 MemSetStr(pOut,
1cad5 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e pOp->p4.z, pOp->
1cad6 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 p1, 0, 0);. pOu
1cad7 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e t->enc = encodin
1cad8 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f g;. UPDATE_MAX_
1cad9 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1cada 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1cadb 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65 20 pcode: Variable
1cadc 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a P1 P2 P3 P4 *.**
1cadd 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 .** Transfer the
1cade 20 76 61 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 values of bound
1cadf 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 2e 2e parameters P1..
1cae0 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 72 65 67 P1+P3-1 into reg
1cae1 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32 isters.** P2..P2
1cae2 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 +P3-1..**.** If
1cae3 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 the parameter is
1cae4 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 69 74 73 named, then its
1cae5 20 6e 61 6d 65 20 61 70 70 65 61 72 73 20 69 6e name appears in
1cae6 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a P4 and P3==1..*
1cae7 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20 69 * The P4 value i
1cae8 73 20 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 s used by sqlite
1cae9 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 3_bind_parameter
1caea 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65 _name()..*/.case
1caeb 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a OP_Variable: {.
1caec 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1caed 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1caee 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69 into u.ab */. i
1caef 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 nt p1;
1caf0 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63 /* Variable to c
1caf1 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e opy from */. in
1caf2 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 2f t p2; /
1caf3 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f * Register to co
1caf4 70 79 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e py to */. int n
1caf5 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e ; /* N
1caf6 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 umber of values
1caf7 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a left to copy */.
1caf8 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 Mem *pVar;
1caf9 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e /* Value bein
1cafa 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f g transferred */
1cafb 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1cafc 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1cafd 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20 into u.ab */..
1cafe 20 75 2e 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e u.ab.p1 = pOp->
1caff 70 31 20 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70 p1 - 1;. u.ab.p
1cb00 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2 = pOp->p2;. u
1cb01 2e 61 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b .ab.n = pOp->p3;
1cb02 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e . assert( u.ab.
1cb03 70 31 3e 3d 30 20 26 26 20 75 2e 61 62 2e 70 31 p1>=0 && u.ab.p1
1cb04 2b 75 2e 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 +u.ab.n<=p->nVar
1cb05 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1cb06 61 62 2e 70 32 3e 3d 31 20 26 26 20 75 2e 61 62 ab.p2>=1 && u.ab
1cb07 2e 70 32 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d .p2+u.ab.n-1<=p-
1cb08 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 >nMem );. asser
1cb09 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 t( pOp->p4.z==0
1cb0a 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 7c 7c || pOp->p3==1 ||
1cb0b 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 0a 0a pOp->p3==0 );..
1cb0c 20 20 77 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d while( u.ab.n-
1cb0d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 75 2e 61 - > 0 ){. u.a
1cb0e 62 2e 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 b.pVar = &p->aVa
1cb0f 72 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20 r[u.ab.p1++];.
1cb10 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 if( sqlite3Vdb
1cb11 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e eMemTooBig(u.ab.
1cb12 70 56 61 72 29 20 29 7b 0a 20 20 20 20 20 20 67 pVar) ){. g
1cb13 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 oto too_big;.
1cb14 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 61 }. pOut = &a
1cb15 4d 65 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a Mem[u.ab.p2++];.
1cb16 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1cb17 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 emReleaseExterna
1cb18 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75 l(pOut);. pOu
1cb19 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e t->flags = MEM_N
1cb1a 75 6c 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 ull;. sqlite3
1cb1b 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f VdbeMemShallowCo
1cb1c 70 79 28 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56 py(pOut, u.ab.pV
1cb1d 61 72 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b ar, MEM_Static);
1cb1e 0a 20 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f . UPDATE_MAX_
1cb1f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a BLOBSIZE(pOut);.
1cb20 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1cb21 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 /* Opcode: Move
1cb22 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a P1 P2 P3 * *.**.
1cb23 2a 2a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 ** Move the valu
1cb24 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 es in register P
1cb25 31 2e 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 1..P1+P3-1 over
1cb26 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 into.** register
1cb27 73 20 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 s P2..P2+P3-1.
1cb28 52 65 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 Registers P1..P1
1cb29 2b 50 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 +P1-1 are.** lef
1cb2a 74 20 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c t holding a NULL
1cb2b 2e 20 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f . It is an erro
1cb2c 72 20 66 6f 72 20 72 65 67 69 73 74 65 72 20 72 r for register r
1cb2d 61 6e 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b anges.** P1..P1+
1cb2e 50 33 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b P3-1 and P2..P2+
1cb2f 50 33 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e P3-1 to overlap.
1cb30 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 .*/.case OP_Move
1cb31 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1cb32 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1cb33 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f ved into u.ac */
1cb34 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 . char *zMalloc
1cb35 3b 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 ; /* Holding v
1cb36 61 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f ariable for allo
1cb37 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a cated memory */.
1cb38 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 int n;
1cb39 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1cb3a 72 65 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 registers left t
1cb3b 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 o copy */. int
1cb3c 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 p1; /*
1cb3d 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 Register to copy
1cb3e 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 from */. int p
1cb3f 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2; /* R
1cb40 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 egister to copy
1cb41 74 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 to */.#endif /*
1cb42 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1cb43 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 moved into u.ac
1cb44 2a 2f 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70 */.. u.ac.n = p
1cb45 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70 Op->p3;. u.ac.p
1cb46 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 1 = pOp->p1;. u
1cb47 2e 61 63 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 .ac.p2 = pOp->p2
1cb48 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 ;. assert( u.ac
1cb49 2e 6e 3e 30 20 26 26 20 75 2e 61 63 2e 70 31 3e .n>0 && u.ac.p1>
1cb4a 30 20 26 26 20 75 2e 61 63 2e 70 32 3e 30 20 29 0 && u.ac.p2>0 )
1cb4b 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 ;. assert( u.ac
1cb4c 2e 70 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 .p1+u.ac.n<=u.ac
1cb4d 2e 70 32 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75 .p2 || u.ac.p2+u
1cb4e 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29 .ac.n<=u.ac.p1 )
1cb4f 3b 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 ;.. pIn1 = &aMe
1cb50 6d 5b 75 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f m[u.ac.p1];. pO
1cb51 75 74 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 63 2e ut = &aMem[u.ac.
1cb52 70 32 5d 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e p2];. while( u.
1cb53 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 ac.n-- ){. as
1cb54 73 65 72 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65 sert( pOut<=&aMe
1cb55 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 m[p->nMem] );.
1cb56 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d assert( pIn1<=
1cb57 26 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 &aMem[p->nMem] )
1cb58 3b 0a 20 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c ;. u.ac.zMall
1cb59 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c oc = pOut->zMall
1cb5a 6f 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d oc;. pOut->zM
1cb5b 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 alloc = 0;. s
1cb5c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1cb5d 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 e(pOut, pIn1);.
1cb5e 20 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 pIn1->zMalloc
1cb5f 20 3d 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b = u.ac.zMalloc;
1cb60 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 . REGISTER_TR
1cb61 41 43 45 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70 ACE(u.ac.p2++, p
1cb62 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b Out);. pIn1++
1cb63 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 ;. pOut++;.
1cb64 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1cb65 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 Opcode: Copy P1
1cb66 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1cb67 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 72 Make a copy of r
1cb68 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20 egister P1 into
1cb69 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1cb6a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 ** This instruct
1cb6b 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65 65 70 ion makes a deep
1cb6c 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
1cb6d 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61 74 65 ue. A duplicate
1cb6e 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66 20 61 .** is made of a
1cb6f 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f ny string or blo
1cb70 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 b constant. See
1cb71 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a also OP_SCopy..
1cb72 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a */.case OP_Copy:
1cb73 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f { /
1cb74 2a 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 * in1, out2 */.
1cb75 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn1 = &aMem[pO
1cb76 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d p->p1];. pOut =
1cb77 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b &aMem[pOp->p2];
1cb78 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21 . assert( pOut!
1cb79 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 =pIn1 );. sqlit
1cb7a 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 e3VdbeMemShallow
1cb7b 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c Copy(pOut, pIn1,
1cb7c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 MEM_Ephem);. D
1cb7d 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f eephemeralize(pO
1cb7e 75 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f ut);. REGISTER_
1cb7f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 TRACE(pOp->p2, p
1cb80 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Out);. break;.}
1cb81 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f ../* Opcode: SCo
1cb82 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a py P1 P2 * * *.*
1cb83 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c *.** Make a shal
1cb84 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69 low copy of regi
1cb85 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 ster P1 into reg
1cb86 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
1cb87 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1cb88 20 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 makes a shallow
1cb89 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c copy of the val
1cb8a 75 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 ue. If the valu
1cb8b 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 e.** is a string
1cb8c 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 or blob, then t
1cb8d 68 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 he copy is only
1cb8e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
1cb8f 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 .** original and
1cb90 20 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72 hence if the or
1cb91 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 iginal changes s
1cb92 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e o will the copy.
1cb93 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68 .** Worse, if th
1cb94 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65 e original is de
1cb95 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63 allocated, the c
1cb96 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 opy becomes inva
1cb97 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 lid..** Thus the
1cb98 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 program must gu
1cb99 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 arantee that the
1cb9a 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e original will n
1cb9b 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 ot change.** dur
1cb9c 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65 ing the lifetime
1cb9d 20 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 of the copy. U
1cb9e 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 se OP_Copy to ma
1cb9f 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a ke a complete.**
1cba0 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f copy..*/.case O
1cba1 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 P_SCopy: {
1cba2 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 6f 75 /* in1, ou
1cba3 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 t2 */. pIn1 = &
1cba4 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1cba5 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f pOut = &aMem[pO
1cba6 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 p->p2];. assert
1cba7 28 20 70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a ( pOut!=pIn1 );.
1cba8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1cba9 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 ShallowCopy(pOut
1cbaa 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 , pIn1, MEM_Ephe
1cbab 6d 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 m);. REGISTER_T
1cbac 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f RACE(pOp->p2, pO
1cbad 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a ut);. break;.}.
1cbae 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 ./* Opcode: Resu
1cbaf 6c 74 52 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 ltRow P1 P2 * *
1cbb0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 *.**.** The regi
1cbb1 73 74 65 72 73 20 50 31 20 74 68 72 6f 75 67 68 sters P1 through
1cbb2 20 50 31 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e P1+P2-1 contain
1cbb3 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 a single row of
1cbb4 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20 54 68 69 .** results. Thi
1cbb5 73 20 6f 70 63 6f 64 65 20 63 61 75 73 65 73 20 s opcode causes
1cbb6 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 the sqlite3_step
1cbb7 28 29 20 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 () call to termi
1cbb8 6e 61 74 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 nate.** with an
1cbb9 53 51 4c 49 54 45 5f 52 4f 57 20 72 65 74 75 72 SQLITE_ROW retur
1cbba 6e 20 63 6f 64 65 20 61 6e 64 20 69 74 20 73 65 n code and it se
1cbbb 74 73 20 75 70 20 74 68 65 20 73 71 6c 69 74 65 ts up the sqlite
1cbbc 33 5f 73 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 3_stmt.** struct
1cbbd 75 72 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 ure to provide a
1cbbe 63 63 65 73 73 20 74 6f 20 74 68 65 20 74 6f 70 ccess to the top
1cbbf 20 50 31 20 76 61 6c 75 65 73 20 61 73 20 74 68 P1 values as th
1cbc0 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e e result.** row.
1cbc1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 .*/.case OP_Resu
1cbc2 6c 74 52 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 ltRow: {.#if 0
1cbc3 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1cbc4 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1cbc5 61 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 ad */. Mem *pMe
1cbc6 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64 m;. int i;.#end
1cbc7 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1cbc8 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1cbc9 20 75 2e 61 64 20 2a 2f 0a 20 20 61 73 73 65 72 u.ad */. asser
1cbca 74 28 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e t( p->nResColumn
1cbcb 3d 3d 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 ==pOp->p2 );. a
1cbcc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 ssert( pOp->p1>0
1cbcd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f );. assert( pO
1cbce 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 p->p1+pOp->p2<=p
1cbcf 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f ->nMem+1 );.. /
1cbd0 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d * If this statem
1cbd1 65 6e 74 20 68 61 73 20 76 69 6f 6c 61 74 65 64 ent has violated
1cbd2 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 immediate forei
1cbd3 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1cbd4 74 73 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 ts, do. ** not
1cbd5 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 return the numbe
1cbd6 72 20 6f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69 r of rows modifi
1cbd7 65 64 2e 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52 ed. And do not R
1cbd8 45 4c 45 41 53 45 20 74 68 65 20 73 74 61 74 65 ELEASE the state
1cbd9 6d 65 6e 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 ment. ** transa
1cbda 63 74 69 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20 ction. It needs
1cbdb 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 to be rolled bac
1cbdc 6b 2e 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c k. */. if( SQL
1cbdd 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 ITE_OK!=(rc = sq
1cbde 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
1cbdf 28 70 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 61 (p, 0)) ){. a
1cbe0 73 73 65 72 74 28 20 64 62 2d 3e 66 6c 61 67 73 ssert( db->flags
1cbe1 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 &SQLITE_CountRow
1cbe2 73 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 s );. assert(
1cbe3 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 p->usesStmtJour
1cbe4 6e 61 6c 20 29 3b 0a 20 20 20 20 62 72 65 61 6b nal );. break
1cbe5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 ;. }.. /* If t
1cbe6 68 65 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 he SQLITE_CountR
1cbe7 6f 77 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 ows flag is set
1cbe8 69 6e 20 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 in sqlite3.flags
1cbe9 20 6d 61 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a mask, then. **
1cbea 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 DML statements
1cbeb 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f invoke this opco
1cbec 64 65 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 de to return the
1cbed 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a number of rows.
1cbee 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f ** modified to
1cbef 20 74 68 65 20 75 73 65 72 2e 20 54 68 69 73 20 the user. This
1cbf0 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
1cbf1 74 68 61 74 20 61 20 56 4d 20 74 68 61 74 0a 20 that a VM that.
1cbf2 20 2a 2a 20 6f 70 65 6e 73 20 61 20 73 74 61 74 ** opens a stat
1cbf3 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f ement transactio
1cbf4 6e 20 6d 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 n may invoke thi
1cbf5 73 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 s opcode.. **.
1cbf6 20 2a 2a 20 49 6e 20 63 61 73 65 20 74 68 69 73 ** In case this
1cbf7 20 69 73 20 73 75 63 68 20 61 20 73 74 61 74 65 is such a state
1cbf8 6d 65 6e 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 ment, close any
1cbf9 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1cbfa 63 74 69 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 ction. ** opene
1cbfb 64 20 62 79 20 74 68 69 73 20 56 4d 20 62 65 66 d by this VM bef
1cbfc 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 63 6f ore returning co
1cbfd 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 ntrol to the use
1cbfe 72 2e 20 54 68 69 73 20 69 73 20 74 6f 0a 20 20 r. This is to.
1cbff 2a 2a 20 65 6e 73 75 72 65 20 74 68 61 74 20 73 ** ensure that s
1cc00 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 tatement-transac
1cc01 74 69 6f 6e 73 20 61 72 65 20 61 6c 77 61 79 73 tions are always
1cc02 20 6e 65 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 nested, not ove
1cc03 72 6c 61 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 rlapping.. ** I
1cc04 66 20 74 68 65 20 6f 70 65 6e 20 73 74 61 74 65 f the open state
1cc05 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e ment-transaction
1cc06 20 69 73 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 is not closed h
1cc07 65 72 65 2c 20 74 68 65 6e 20 74 68 65 20 75 73 ere, then the us
1cc08 65 72 0a 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 er. ** may step
1cc09 20 61 6e 6f 74 68 65 72 20 56 4d 20 74 68 61 74 another VM that
1cc0a 20 6f 70 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 opens its own s
1cc0b 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 tatement transac
1cc0c 74 69 6f 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 tion. This. **
1cc0d 6d 61 79 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 may lead to over
1cc0e 6c 61 70 70 69 6e 67 20 73 74 61 74 65 6d 65 6e lapping statemen
1cc0f 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a t transactions..
1cc10 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 **. ** The st
1cc11 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 atement transact
1cc12 69 6f 6e 20 69 73 20 6e 65 76 65 72 20 61 20 74 ion is never a t
1cc13 6f 70 2d 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 op-level transac
1cc14 74 69 6f 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a tion. Hence. *
1cc15 2a 20 74 68 65 20 52 45 4c 45 41 53 45 20 63 61 * the RELEASE ca
1cc16 6c 6c 20 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 ll below can nev
1cc17 65 72 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 er fail.. */.
1cc18 61 73 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74 assert( p->iStat
1cc19 65 6d 65 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e ement==0 || db->
1cc1a 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 flags&SQLITE_Cou
1cc1b 6e 74 52 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d ntRows );. rc =
1cc1c 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 sqlite3VdbeClos
1cc1d 65 53 74 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 eStatement(p, SA
1cc1e 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 VEPOINT_RELEASE)
1cc1f 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
1cc20 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a !=SQLITE_OK) ){.
1cc21 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a break;. }..
1cc22 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 /* Invalidate
1cc23 61 6c 6c 20 65 70 68 65 6d 65 72 61 6c 20 63 75 all ephemeral cu
1cc24 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 73 20 rsor row caches
1cc25 2a 2f 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 */. p->cacheCtr
1cc26 20 3d 20 28 70 2d 3e 63 61 63 68 65 43 74 72 20 = (p->cacheCtr
1cc27 2b 20 32 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 + 2)|1;.. /* Ma
1cc28 6b 65 20 73 75 72 65 20 74 68 65 20 72 65 73 75 ke sure the resu
1cc29 6c 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 lts of the curre
1cc2a 6e 74 20 72 6f 77 20 61 72 65 20 5c 30 30 30 20 nt row are \000
1cc2b 74 65 72 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 terminated. **
1cc2c 61 6e 64 20 68 61 76 65 20 61 6e 20 61 73 73 69 and have an assi
1cc2d 67 6e 65 64 20 74 79 70 65 2e 20 20 54 68 65 20 gned type. The
1cc2e 72 65 73 75 6c 74 73 20 61 72 65 20 64 65 2d 65 results are de-e
1cc2f 70 68 65 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a phemeralized as.
1cc30 20 20 2a 2a 20 61 73 20 73 69 64 65 20 65 66 66 ** as side eff
1cc31 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 ect.. */. u.ad
1cc32 2e 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 .pMem = p->pResu
1cc33 6c 74 53 65 74 20 3d 20 26 61 4d 65 6d 5b 70 4f ltSet = &aMem[pO
1cc34 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e p->p1];. for(u.
1cc35 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70 ad.i=0; u.ad.i<p
1cc36 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b Op->p2; u.ad.i++
1cc37 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
1cc38 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 beMemNulTerminat
1cc39 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 e(&u.ad.pMem[u.a
1cc3a 64 2e 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 d.i]);. sqlit
1cc3b 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 e3VdbeMemStoreTy
1cc3c 70 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e pe(&u.ad.pMem[u.
1cc3d 61 64 2e 69 5d 29 3b 0a 20 20 20 20 52 45 47 49 ad.i]);. REGI
1cc3e 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e STER_TRACE(pOp->
1cc3f 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64 p1+u.ad.i, &u.ad
1cc40 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a .pMem[u.ad.i]);.
1cc41 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 }. if( db->ma
1cc42 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 llocFailed ) got
1cc43 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 o no_mem;.. /*
1cc44 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f Return SQLITE_RO
1cc45 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d W. */. p->pc =
1cc46 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 pc + 1;. rc =
1cc47 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f SQLITE_ROW;. go
1cc48 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a to vdbe_return;.
1cc49 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f }../* Opcode: Co
1cc4a 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 2a 20 ncat P1 P2 P3 *
1cc4b 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 *.**.** Add the
1cc4c 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 65 72 text in register
1cc4d 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 P1 onto the end
1cc4e 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a of the text in.
1cc4f 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 20 61 ** register P2 a
1cc50 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 nd store the res
1cc51 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 ult in register
1cc52 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 P3..** If either
1cc53 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 74 65 the P1 or P2 te
1cc54 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e xt are NULL then
1cc55 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 store NULL in P
1cc56 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 3..**.** P3 =
1cc57 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 P2 || P1.**.** I
1cc58 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 t is illegal for
1cc59 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65 P1 and P3 to be
1cc5a 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 the same regist
1cc5b 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a er. Sometimes,.*
1cc5c 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 20 73 * if P3 is the s
1cc5d 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 73 20 ame register as
1cc5e 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e P2, the implemen
1cc5f 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a tation is able.*
1cc60 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d * to avoid a mem
1cc61 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f cpy()..*/.case O
1cc62 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 P_Concat: {
1cc63 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1cc64 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c TK_CONCAT, in1,
1cc65 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 in2, out3 */.#i
1cc66 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1cc67 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1cc68 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34 to u.ae */. i64
1cc69 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f nByte;.#endif /
1cc6a 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1cc6b 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1cc6c 65 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 e */.. pIn1 = &
1cc6d 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1cc6e 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn2 = &aMem[pO
1cc6f 70 2d 3e 70 32 5d 3b 0a 20 20 70 4f 75 74 20 3d p->p2];. pOut =
1cc70 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b &aMem[pOp->p3];
1cc71 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21 . assert( pIn1!
1cc72 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28 =pOut );. if( (
1cc73 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 pIn1->flags | pI
1cc74 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d n2->flags) & MEM
1cc75 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c _Null ){. sql
1cc76 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 ite3VdbeMemSetNu
1cc77 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 ll(pOut);. br
1cc78 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 eak;. }. if( E
1cc79 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 xpandBlob(pIn1)
1cc7a 7c 7c 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 || ExpandBlob(pI
1cc7b 6e 32 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 n2) ) goto no_me
1cc7c 6d 3b 0a 20 20 53 74 72 69 6e 67 69 66 79 28 70 m;. Stringify(p
1cc7d 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a In1, encoding);.
1cc7e 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 Stringify(pIn2
1cc7f 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 , encoding);. u
1cc80 2e 61 65 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 .ae.nByte = pIn1
1cc81 2d 3e 6e 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 ->n + pIn2->n;.
1cc82 20 69 66 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e if( u.ae.nByte>
1cc83 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
1cc84 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 E_LIMIT_LENGTH]
1cc85 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1cc86 62 69 67 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 big;. }. MemSe
1cc87 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 tTypeFlag(pOut,
1cc88 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 MEM_Str);. if(
1cc89 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1cc8a 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e ow(pOut, (int)u.
1cc8b 61 65 2e 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 ae.nByte+2, pOut
1cc8c 3d 3d 70 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 ==pIn2) ){. g
1cc8d 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1cc8e 20 20 69 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 if( pOut!=pIn2
1cc8f 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 ){. memcpy(p
1cc90 4f 75 74 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c Out->z, pIn2->z,
1cc91 20 70 49 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 pIn2->n);. }.
1cc92 20 6d 65 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a memcpy(&pOut->z
1cc93 5b 70 49 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d [pIn2->n], pIn1-
1cc94 3e 7a 2c 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 >z, pIn1->n);.
1cc95 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 pOut->z[u.ae.nBy
1cc96 74 65 5d 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d te] = 0;. pOut-
1cc97 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d >z[u.ae.nByte+1]
1cc98 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c = 0;. pOut->fl
1cc99 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b ags |= MEM_Term;
1cc9a 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e . pOut->n = (in
1cc9b 74 29 75 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 t)u.ae.nByte;.
1cc9c 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f pOut->enc = enco
1cc9d 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d ding;. UPDATE_M
1cc9e 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 AX_BLOBSIZE(pOut
1cc9f 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1cca0 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 20 50 31 * Opcode: Add P1
1cca1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a P2 P3 * *.**.**
1cca2 20 41 64 64 20 74 68 65 20 76 61 6c 75 65 20 69 Add the value i
1cca3 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f n register P1 to
1cca4 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cca5 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 gister P2.** and
1cca6 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c store the resul
1cca7 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1cca8 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1cca9 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1ccaa 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1ccab 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1ccac 4d 75 6c 74 69 70 6c 79 20 50 31 20 50 32 20 50 Multiply P1 P2 P
1ccad 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 3 * *.**.**.** M
1ccae 75 6c 74 69 70 6c 79 20 74 68 65 20 76 61 6c 75 ultiply the valu
1ccaf 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1ccb0 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e by the value in
1ccb1 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 register P2.**
1ccb2 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 and store the re
1ccb3 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 sult in register
1ccb4 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 P3..** If eithe
1ccb5 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c r input is NULL,
1ccb6 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e the result is N
1ccb7 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 ULL..*/./* Opcod
1ccb8 65 3a 20 53 75 62 74 72 61 63 74 20 50 31 20 50 e: Subtract P1 P
1ccb9 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 2 P3 * *.**.** S
1ccba 75 62 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 ubtract the valu
1ccbb 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 e in register P1
1ccbc 20 66 72 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 from the value
1ccbd 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a in register P2.*
1ccbe 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 * and store the
1ccbf 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 result in regist
1ccc0 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 er P3..** If eit
1ccc1 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c her input is NUL
1ccc2 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 L, the result is
1ccc3 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 NULL..*/./* Opc
1ccc4 6f 64 65 3a 20 44 69 76 69 64 65 20 50 31 20 50 ode: Divide P1 P
1ccc5 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 2 P3 * *.**.** D
1ccc6 69 76 69 64 65 20 74 68 65 20 76 61 6c 75 65 20 ivide the value
1ccc7 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 in register P1 b
1ccc8 79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 y the value in r
1ccc9 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e egister P2.** an
1ccca 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 d store the resu
1cccb 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 lt in register P
1cccc 33 20 28 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 3 (P3=P2/P1). If
1cccd 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a the value in .*
1ccce 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 * register P1 is
1cccf 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 zero, then the
1ccd0 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 result is NULL.
1ccd1 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 If either input
1ccd2 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 is .** NULL, the
1ccd3 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1ccd4 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 .*/./* Opcode: R
1ccd5 65 6d 61 69 6e 64 65 72 20 50 31 20 50 32 20 50 emainder P1 P2 P
1ccd6 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 3 * *.**.** Comp
1ccd7 75 74 65 20 74 68 65 20 72 65 6d 61 69 6e 64 65 ute the remainde
1ccd8 72 20 61 66 74 65 72 20 69 6e 74 65 67 65 72 20 r after integer
1ccd9 64 69 76 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 division of the
1ccda 76 61 6c 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 value in.** regi
1ccdb 73 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 ster P1 by the v
1ccdc 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 alue in register
1ccdd 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68 P2 and store th
1ccde 65 20 72 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 e result in P3.
1ccdf 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 .** If the value
1cce0 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 in register P2
1cce1 69 73 20 7a 65 72 6f 20 74 68 65 20 72 65 73 75 is zero the resu
1cce2 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 lt is NULL..** I
1cce3 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 f either operand
1cce4 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 is NULL, the re
1cce5 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f sult is NULL..*/
1cce6 0a 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 .case OP_Add:
1cce7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cce8 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c /* same as TK_PL
1cce9 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 US, in1, in2, ou
1ccea 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 t3 */.case OP_Su
1cceb 62 74 72 61 63 74 3a 20 20 20 20 20 20 20 20 20 btract:
1ccec 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cced 54 4b 5f 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 TK_MINUS, in1, i
1ccee 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1ccef 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 OP_Multiply:
1ccf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1ccf1 6d 65 20 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 me as TK_STAR, i
1ccf2 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f n1, in2, out3 */
1ccf3 0a 63 61 73 65 20 4f 50 5f 44 69 76 69 64 65 3a .case OP_Divide:
1ccf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ccf5 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c /* same as TK_SL
1ccf6 41 53 48 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f ASH, in1, in2, o
1ccf7 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 ut3 */.case OP_R
1ccf8 65 6d 61 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 emainder: {
1ccf9 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1ccfa 20 54 4b 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e TK_REM, in1, in
1ccfb 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 2, out3 */.#if 0
1ccfc 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1ccfd 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1ccfe 75 2e 61 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c u.af */. int fl
1ccff 61 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d ags; /* Com
1cd00 62 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 bined MEM_* flag
1cd01 73 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 s from both inpu
1cd02 74 73 20 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 ts */. i64 iA;
1cd03 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 /* Integ
1cd04 65 72 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 er value of left
1cd05 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 operand */. i6
1cd06 34 20 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 4 iB; /*
1cd07 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f Integer value o
1cd08 66 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 f right operand
1cd09 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 */. double rA;
1cd0a 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c /* Real val
1cd0b 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 ue of left opera
1cd0c 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 nd */. double r
1cd0d 42 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 B; /* Real
1cd0e 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f value of right o
1cd0f 70 65 72 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 perand */.#endif
1cd10 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1cd11 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1cd12 2e 61 66 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d .af */.. pIn1 =
1cd13 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b &aMem[pOp->p1];
1cd14 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 . applyNumericA
1cd15 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 ffinity(pIn1);.
1cd16 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn2 = &aMem[pO
1cd17 70 2d 3e 70 32 5d 3b 0a 20 20 61 70 70 6c 79 4e p->p2];. applyN
1cd18 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 umericAffinity(p
1cd19 49 6e 32 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 In2);. pOut = &
1cd1a 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 aMem[pOp->p3];.
1cd1b 20 75 2e 61 66 2e 66 6c 61 67 73 20 3d 20 70 49 u.af.flags = pI
1cd1c 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 n1->flags | pIn2
1cd1d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 ->flags;. if( (
1cd1e 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d u.af.flags & MEM
1cd1f 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f _Null)!=0 ) goto
1cd20 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 arithmetic_resu
1cd21 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 lt_is_null;. if
1cd22 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 ( (pIn1->flags &
1cd23 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d pIn2->flags & M
1cd24 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 EM_Int)==MEM_Int
1cd25 20 29 7b 0a 20 20 20 20 75 2e 61 66 2e 69 41 20 ){. u.af.iA
1cd26 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 = pIn1->u.i;.
1cd27 20 75 2e 61 66 2e 69 42 20 3d 20 70 49 6e 32 2d u.af.iB = pIn2-
1cd28 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74 63 68 >u.i;. switch
1cd29 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1cd2a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 . case OP_A
1cd2b 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66 dd: u.af
1cd2c 2e 69 42 20 2b 3d 20 75 2e 61 66 2e 69 41 3b 20 .iB += u.af.iA;
1cd2d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
1cd2e 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 case OP_Subtr
1cd2f 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 act: u.af.iB
1cd30 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 -= u.af.iA;
1cd31 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 break;. c
1cd32 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a ase OP_Multiply:
1cd33 20 20 20 20 75 2e 61 66 2e 69 42 20 2a 3d 20 75 u.af.iB *= u
1cd34 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72 .af.iA; br
1cd35 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 eak;. case
1cd36 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 OP_Divide: {.
1cd37 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 if( u.af.iA
1cd38 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 ==0 ) goto arith
1cd39 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f metic_result_is_
1cd3a 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a null;. /*
1cd3b 20 44 69 76 69 64 69 6e 67 20 74 68 65 20 6c 61 Dividing the la
1cd3c 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65 20 6e rgest possible n
1cd3d 65 67 61 74 69 76 65 20 36 34 2d 62 69 74 20 69 egative 64-bit i
1cd3e 6e 74 65 67 65 72 20 28 31 3c 3c 36 33 29 20 62 nteger (1<<63) b
1cd3f 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 y. ** -1
1cd40 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 returns an integ
1cd41 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 er too large to
1cd42 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 store in a 64-bi
1cd43 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a t data-type. On.
1cd44 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 ** some
1cd45 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 architectures, t
1cd46 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f he value overflo
1cd47 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f ws to (1<<63). O
1cd48 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 n others,.
1cd49 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 ** a SIGFPE is
1cd4a 20 69 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c issued. The fol
1cd4b 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 lowing statement
1cd4c 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 normalizes this
1cd4d 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 . ** beha
1cd4e 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c vior so that all
1cd4f 20 61 72 63 68 69 74 65 63 74 75 72 65 73 20 62 architectures b
1cd50 65 68 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 ehave as if inte
1cd51 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f ger. ** o
1cd52 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 verflow occurred
1cd53 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
1cd54 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 if( u.af.iA
1cd55 3d 3d 2d 31 20 26 26 20 75 2e 61 66 2e 69 42 3d ==-1 && u.af.iB=
1cd56 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 =SMALLEST_INT64
1cd57 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 ) u.af.iA = 1;.
1cd58 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 2f u.af.iB /
1cd59 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 = u.af.iA;.
1cd5a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
1cd5b 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a }. default:
1cd5c 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 {. if( u
1cd5d 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f .af.iA==0 ) goto
1cd5e 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 arithmetic_resu
1cd5f 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 lt_is_null;.
1cd60 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d if( u.af.iA=
1cd61 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 =-1 ) u.af.iA =
1cd62 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 1;. u.af.
1cd63 69 42 20 25 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 iB %= u.af.iA;.
1cd64 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1cd65 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1cd66 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 66 pOut->u.i = u.af
1cd67 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 .iB;. MemSetT
1cd68 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1cd69 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b M_Int);. }else{
1cd6a 0a 20 20 20 20 75 2e 61 66 2e 72 41 20 3d 20 73 . u.af.rA = s
1cd6b 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 qlite3VdbeRealVa
1cd6c 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20 20 75 lue(pIn1);. u
1cd6d 2e 61 66 2e 72 42 20 3d 20 73 71 6c 69 74 65 33 .af.rB = sqlite3
1cd6e 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 VdbeRealValue(pI
1cd6f 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 n2);. switch(
1cd70 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a pOp->opcode ){.
1cd71 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 case OP_Ad
1cd72 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61 66 2e d: u.af.
1cd73 72 42 20 2b 3d 20 75 2e 61 66 2e 72 41 3b 20 20 rB += u.af.rA;
1cd74 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1cd75 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 case OP_Subtra
1cd76 63 74 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2d ct: u.af.rB -
1cd77 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 = u.af.rA;
1cd78 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 break;. ca
1cd79 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 se OP_Multiply:
1cd7a 20 20 20 75 2e 61 66 2e 72 42 20 2a 3d 20 75 2e u.af.rB *= u.
1cd7b 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 af.rA; bre
1cd7c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f ak;. case O
1cd7d 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 P_Divide: {.
1cd7e 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 /* (double)0
1cd7f 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 In case of SQLI
1cd80 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 TE_OMIT_FLOATING
1cd81 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 _POINT... */.
1cd82 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 72 41 if( u.af.rA
1cd83 3d 3d 28 64 6f 75 62 6c 65 29 30 20 29 20 67 6f ==(double)0 ) go
1cd84 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 to arithmetic_re
1cd85 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 sult_is_null;.
1cd86 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2f 3d u.af.rB /=
1cd87 20 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20 20 20 u.af.rA;.
1cd88 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
1cd89 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 . default:
1cd8a 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 {. u.af.i
1cd8b 41 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 41 A = (i64)u.af.rA
1cd8c 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 ;. u.af.i
1cd8d 42 20 3d 20 28 69 36 34 29 75 2e 61 66 2e 72 42 B = (i64)u.af.rB
1cd8e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e ;. if( u.
1cd8f 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 af.iA==0 ) goto
1cd90 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c arithmetic_resul
1cd91 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 t_is_null;.
1cd92 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d if( u.af.iA==
1cd93 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 -1 ) u.af.iA = 1
1cd94 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 ;. u.af.r
1cd95 42 20 3d 20 28 64 6f 75 62 6c 65 29 28 75 2e 61 B = (double)(u.a
1cd96 66 2e 69 42 20 25 20 75 2e 61 66 2e 69 41 29 3b f.iB % u.af.iA);
1cd97 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
1cd98 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1cd99 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e if( sqlite3IsN
1cd9a 61 4e 28 75 2e 61 66 2e 72 42 29 20 29 7b 0a 20 aN(u.af.rB) ){.
1cd9b 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74 68 6d goto arithm
1cd9c 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e etic_result_is_n
1cd9d 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 ull;. }. p
1cd9e 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 66 2e 72 42 Out->r = u.af.rB
1cd9f 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1cda0 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 Flag(pOut, MEM_R
1cda1 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20 28 75 eal);. if( (u
1cda2 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f .af.flags & MEM_
1cda3 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 Real)==0 ){.
1cda4 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 sqlite3VdbeInt
1cda5 65 67 65 72 41 66 66 69 6e 69 74 79 28 70 4f 75 egerAffinity(pOu
1cda6 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 t);. }. }.
1cda7 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 break;..arithmet
1cda8 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c ic_result_is_nul
1cda9 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 l:. sqlite3Vdbe
1cdaa 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 MemSetNull(pOut)
1cdab 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a ;. break;.}../*
1cdac 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 Opcode: CollSeq
1cdad 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 * * P4.**.** P4
1cdae 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1cdaf 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 a CollSeq struc
1cdb0 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 63 t. If the next c
1cdb1 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20 66 75 all to a user fu
1cdb2 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 nction.** or agg
1cdb3 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73 71 6c regate calls sql
1cdb4 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 ite3GetFuncCollS
1cdb5 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 eq(), this colla
1cdb6 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 tion sequence wi
1cdb7 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 ll.** be returne
1cdb8 64 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 d. This is used
1cdb9 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 by the built-in
1cdba 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 min(), max() and
1cdbb 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e nullif().** fun
1cdbc 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 ctions..**.** Th
1cdbd 65 20 69 6e 74 65 72 66 61 63 65 20 75 73 65 64 e interface used
1cdbe 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e by the implemen
1cdbf 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 tation of the af
1cdc0 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e orementioned fun
1cdc1 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 ctions.** to ret
1cdc2 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 rieve the collat
1cdc3 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 65 74 ion sequence set
1cdc4 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20 by this opcode
1cdc5 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 is not available
1cdc6 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e .** publicly, on
1cdc7 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 ly to user funct
1cdc8 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20 ions defined in
1cdc9 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 func.c..*/.case
1cdca 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 OP_CollSeq: {.
1cdcb 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 assert( pOp->p4t
1cdcc 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 ype==P4_COLLSEQ
1cdcd 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f );. break;.}../
1cdce 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 * Opcode: Functi
1cdcf 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 on P1 P2 P3 P4 P
1cdd0 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 5.**.** Invoke a
1cdd1 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 user function (
1cdd2 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 P4 is a pointer
1cdd3 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 to a Function st
1cdd4 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 ructure that.**
1cdd5 64 65 66 69 6e 65 73 20 74 68 65 20 66 75 6e 63 defines the func
1cdd6 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20 61 72 tion) with P5 ar
1cdd7 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 guments taken fr
1cdd8 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 20 61 om register P2 a
1cdd9 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 nd.** successors
1cdda 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 . The result of
1cddb 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 the function is
1cddc 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 stored in regis
1cddd 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 ter P3..** Regis
1cdde 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 ter P3 must not
1cddf 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 be one of the fu
1cde0 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a nction inputs..*
1cde1 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33 32 2d *.** P1 is a 32-
1cde2 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 bit bitmask indi
1cde3 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20 6f cating whether o
1cde4 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67 75 6d r not each argum
1cde5 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 ent to the .** f
1cde6 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65 74 65 unction was dete
1cde7 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e rmined to be con
1cde8 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 stant at compile
1cde9 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20 66 69 time. If the fi
1cdea 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 rst.** argument
1cdeb 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 was constant the
1cdec 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20 69 73 n bit 0 of P1 is
1cded 20 73 65 74 2e 20 54 68 69 73 20 69 73 20 75 73 set. This is us
1cdee 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a ed to determine.
1cdef 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74 61 20 ** whether meta
1cdf0 64 61 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 data associated
1cdf1 77 69 74 68 20 61 20 75 73 65 72 20 66 75 6e 63 with a user func
1cdf2 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 tion argument us
1cdf3 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 ing the.** sqlit
1cdf4 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 e3_set_auxdata()
1cdf5 20 41 50 49 20 6d 61 79 20 62 65 20 73 61 66 65 API may be safe
1cdf6 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e 74 69 ly retained unti
1cdf7 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e l the next.** in
1cdf8 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 vocation of this
1cdf9 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 opcode..**.** S
1cdfa 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 ee also: AggStep
1cdfb 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f and AggFinal.*/
1cdfc 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f .case OP_Functio
1cdfd 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c n: {.#if 0 /* l
1cdfe 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1cdff 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a oved into u.ag *
1ce00 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d /. int i;. Mem
1ce01 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 *pArg;. sqlite
1ce02 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 3_context ctx;.
1ce03 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a sqlite3_value *
1ce04 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b *apVal;. int n;
1ce05 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1ce06 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1ce07 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 into u.ag */..
1ce08 20 75 2e 61 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70 u.ag.n = pOp->p
1ce09 35 3b 0a 20 20 75 2e 61 67 2e 61 70 56 61 6c 20 5;. u.ag.apVal
1ce0a 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 = p->apArg;. as
1ce0b 73 65 72 74 28 20 75 2e 61 67 2e 61 70 56 61 6c sert( u.ag.apVal
1ce0c 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b || u.ag.n==0 );
1ce0d 0a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 67 .. assert( u.ag
1ce0e 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 .n==0 || (pOp->p
1ce0f 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2>0 && pOp->p2+u
1ce10 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 .ag.n<=p->nMem+1
1ce11 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 ) );. assert( p
1ce12 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c Op->p3<pOp->p2 |
1ce13 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e | pOp->p3>=pOp->
1ce14 70 32 2b 75 2e 61 67 2e 6e 20 29 3b 0a 20 20 75 p2+u.ag.n );. u
1ce15 2e 61 67 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d .ag.pArg = &aMem
1ce16 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 [pOp->p2];. for
1ce17 28 75 2e 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e (u.ag.i=0; u.ag.
1ce18 69 3c 75 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 i<u.ag.n; u.ag.i
1ce19 2b 2b 2c 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 ++, u.ag.pArg++)
1ce1a 7b 0a 20 20 20 20 75 2e 61 67 2e 61 70 56 61 6c {. u.ag.apVal
1ce1b 5b 75 2e 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e [u.ag.i] = u.ag.
1ce1c 70 41 72 67 3b 0a 20 20 20 20 73 71 6c 69 74 65 pArg;. sqlite
1ce1d 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 3VdbeMemStoreTyp
1ce1e 65 28 75 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 e(u.ag.pArg);.
1ce1f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1ce20 28 70 4f 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 (pOp->p2, u.ag.p
1ce21 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 Arg);. }.. ass
1ce22 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 ert( pOp->p4type
1ce23 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 ==P4_FUNCDEF ||
1ce24 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1ce25 56 44 42 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 VDBEFUNC );. if
1ce26 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1ce27 34 5f 46 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 4_FUNCDEF ){.
1ce28 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 u.ag.ctx.pFunc
1ce29 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b = pOp->p4.pFunc;
1ce2a 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 . u.ag.ctx.pV
1ce2b 64 62 65 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d dbeFunc = 0;. }
1ce2c 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 else{. u.ag.c
1ce2d 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 28 tx.pVdbeFunc = (
1ce2e 56 64 62 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 VdbeFunc*)pOp->p
1ce2f 34 2e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 4.pVdbeFunc;.
1ce30 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 u.ag.ctx.pFunc
1ce31 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 = u.ag.ctx.pVdbe
1ce32 46 75 6e 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d Func->pFunc;. }
1ce33 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1ce34 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 >p3>0 && pOp->p3
1ce35 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 <=p->nMem );. p
1ce36 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d Out = &aMem[pOp-
1ce37 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78 >p3];. u.ag.ctx
1ce38 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e .s.flags = MEM_N
1ce39 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e ull;. u.ag.ctx.
1ce3a 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61 s.db = db;. u.a
1ce3b 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 g.ctx.s.xDel = 0
1ce3c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a ;. u.ag.ctx.s.z
1ce3d 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f Malloc = 0;.. /
1ce3e 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c * The output cel
1ce3f 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61 l may already ha
1ce40 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f ve a buffer allo
1ce41 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a cated. Move. **
1ce42 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 the pointer to
1ce43 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e u.ag.ctx.s so in
1ce44 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 case the user-f
1ce45 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a unction can use.
1ce46 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79 ** the already
1ce47 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 allocated buffe
1ce48 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c r instead of all
1ce49 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e ocating a new on
1ce4a 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 e.. */. sqlite
1ce4b 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 3VdbeMemMove(&u.
1ce4c 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b ag.ctx.s, pOut);
1ce4d 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 . MemSetTypeFla
1ce4e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d g(&u.ag.ctx.s, M
1ce4f 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61 EM_Null);.. u.a
1ce50 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 g.ctx.isError =
1ce51 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 0;. if( u.ag.ct
1ce52 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 x.pFunc->flags &
1ce53 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 SQLITE_FUNC_NEE
1ce54 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 DCOLL ){. ass
1ce55 65 72 74 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a ert( pOp>aOp );.
1ce56 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b assert( pOp[
1ce57 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 -1].p4type==P4_C
1ce58 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73 OLLSEQ );. as
1ce59 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 sert( pOp[-1].op
1ce5a 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 code==OP_CollSeq
1ce5b 20 29 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 );. u.ag.ctx
1ce5c 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d .pColl = pOp[-1]
1ce5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 .p4.pColl;. }.
1ce5e 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1ce5f 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1ce60 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1ce61 73 75 73 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 suse;. (*u.ag.c
1ce62 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 tx.pFunc->xFunc)
1ce63 28 26 75 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 (&u.ag.ctx, u.ag
1ce64 2e 6e 2c 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b .n, u.ag.apVal);
1ce65 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1ce66 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 fetyOn(db) ){.
1ce67 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1ce68 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74 Release(&u.ag.ct
1ce69 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 x.s);. goto a
1ce6a 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 bort_due_to_misu
1ce6b 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 se;. }. if( db
1ce6c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
1ce6d 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 {. /* Even th
1ce6e 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 ough a malloc()
1ce6f 68 61 73 20 66 61 69 6c 65 64 2c 20 74 68 65 20 has failed, the
1ce70 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f implementation o
1ce71 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 f the. ** use
1ce72 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 r function may h
1ce73 61 76 65 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 ave called an sq
1ce74 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 lite3_result_XXX
1ce75 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 () function.
1ce76 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 20 76 ** to return a v
1ce77 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 alue. The follow
1ce78 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 ing call release
1ce79 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 0a s any resources.
1ce7a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 ** associate
1ce7b 64 20 77 69 74 68 20 73 75 63 68 20 61 20 76 61 d with such a va
1ce7c 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 lue.. **.
1ce7d 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d ** Note: Maybe M
1ce7e 65 6d 52 65 6c 65 61 73 65 28 29 20 73 68 6f 75 emRelease() shou
1ce7f 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20 ld be called if
1ce80 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 sqlite3SafetyOn(
1ce81 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 ). ** fails a
1ce82 6c 73 6f 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 lso (the if(...)
1ce83 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 statement above
1ce84 29 2e 20 42 75 74 20 69 66 20 70 65 6f 70 6c 65 ). But if people
1ce85 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 are. ** misu
1ce86 73 69 6e 67 20 73 71 6c 69 74 65 2c 20 74 68 65 sing sqlite, the
1ce87 79 20 68 61 76 65 20 62 69 67 67 65 72 20 70 72 y have bigger pr
1ce88 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 oblems than a le
1ce89 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 aked value..
1ce8a 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 */. sqlite3Vd
1ce8b 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e beMemRelease(&u.
1ce8c 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 ag.ctx.s);. g
1ce8d 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1ce8e 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 . /* If any aux
1ce8f 69 6c 69 61 72 79 20 64 61 74 61 20 66 75 6e 63 iliary data func
1ce90 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 tions have been
1ce91 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73 20 75 called by this u
1ce92 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 ser function,.
1ce93 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 ** immediately c
1ce94 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 all the destruct
1ce95 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 or for any non-s
1ce96 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 tatic values..
1ce97 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 */. if( u.ag.ct
1ce98 78 2e 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 x.pVdbeFunc ){.
1ce99 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 sqlite3VdbeDe
1ce9a 6c 65 74 65 41 75 78 44 61 74 61 28 75 2e 61 67 leteAuxData(u.ag
1ce9b 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 .ctx.pVdbeFunc,
1ce9c 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f pOp->p1);. pO
1ce9d 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63 20 p->p4.pVdbeFunc
1ce9e 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 = u.ag.ctx.pVdbe
1ce9f 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 Func;. pOp->p
1cea0 34 74 79 70 65 20 3d 20 50 34 5f 56 44 42 45 46 4type = P4_VDBEF
1cea1 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 UNC;. }.. /* I
1cea2 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 f the function r
1cea3 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 eturned an error
1cea4 2c 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 , throw an excep
1cea5 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e tion */. if( u.
1cea6 61 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 29 ag.ctx.isError )
1cea7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 {. sqlite3Set
1cea8 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1cea9 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 sg, db, "%s", sq
1ceaa 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 lite3_value_text
1ceab 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a (&u.ag.ctx.s));.
1ceac 20 20 20 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 rc = u.ag.ct
1cead 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a x.isError;. }..
1ceae 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 72 65 /* Copy the re
1ceaf 73 75 6c 74 20 6f 66 20 74 68 65 20 66 75 6e 63 sult of the func
1ceb0 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 tion into regist
1ceb1 65 72 20 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 er P3 */. sqlit
1ceb2 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f e3VdbeChangeEnco
1ceb3 64 69 6e 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 ding(&u.ag.ctx.s
1ceb4 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 , encoding);. s
1ceb5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 qlite3VdbeMemMov
1ceb6 65 28 70 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 e(pOut, &u.ag.ct
1ceb7 78 2e 73 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 x.s);. if( sqli
1ceb8 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 te3VdbeMemTooBig
1ceb9 28 70 4f 75 74 29 20 29 7b 0a 20 20 20 20 67 6f (pOut) ){. go
1ceba 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a to too_big;. }.
1cebb 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 REGISTER_TRACE
1cebc 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b (pOp->p3, pOut);
1cebd 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1cebe 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 OBSIZE(pOut);.
1cebf 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1cec0 6f 64 65 3a 20 42 69 74 41 6e 64 20 50 31 20 50 ode: BitAnd P1 P
1cec1 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 2 P3 * *.**.** T
1cec2 61 6b 65 20 74 68 65 20 62 69 74 2d 77 69 73 65 ake the bit-wise
1cec3 20 41 4e 44 20 6f 66 20 74 68 65 20 76 61 6c 75 AND of the valu
1cec4 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 es in register P
1cec5 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 1 and P2 and.**
1cec6 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 store the result
1cec7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e in register P3.
1cec8 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e .** If either in
1cec9 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 put is NULL, the
1ceca 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e result is NULL.
1cecb 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 .*/./* Opcode: B
1cecc 69 74 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 itOr P1 P2 P3 *
1cecd 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 *.**.** Take the
1cece 20 62 69 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 bit-wise OR of
1cecf 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 the values in re
1ced0 67 69 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 gister P1 and P2
1ced1 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 and.** store th
1ced2 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 e result in regi
1ced3 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 ster P3..** If e
1ced4 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e ither input is N
1ced5 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 ULL, the result
1ced6 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f is NULL..*/./* O
1ced7 70 63 6f 64 65 3a 20 53 68 69 66 74 4c 65 66 74 pcode: ShiftLeft
1ced8 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1ced9 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e .** Shift the in
1ceda 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 teger value in r
1cedb 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 egister P2 to th
1cedc 65 20 6c 65 66 74 20 62 79 20 74 68 65 0a 2a 2a e left by the.**
1cedd 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 number of bits
1cede 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 specified by the
1cedf 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 integer in regi
1cee0 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 ser P1..** Store
1cee1 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 the result in r
1cee2 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 egister P3..** I
1cee3 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 f either input i
1cee4 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 s NULL, the resu
1cee5 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f lt is NULL..*/./
1cee6 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 52 * Opcode: ShiftR
1cee7 69 67 68 74 20 50 31 20 50 32 20 50 33 20 2a 20 ight P1 P2 P3 *
1cee8 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 *.**.** Shift th
1cee9 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 e integer value
1ceea 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 in register P2 t
1ceeb 6f 20 74 68 65 20 72 69 67 68 74 20 62 79 20 74 o the right by t
1ceec 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 he.** number of
1ceed 62 69 74 73 20 73 70 65 63 69 66 69 65 64 20 62 bits specified b
1ceee 79 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e y the integer in
1ceef 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1cef0 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c Store the resul
1cef1 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 t in register P3
1cef2 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69 ..** If either i
1cef3 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 nput is NULL, th
1cef4 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c e result is NULL
1cef5 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 ..*/.case OP_Bit
1cef6 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 And:
1cef7 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cef8 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 TK_BITAND, in1,
1cef9 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 in2, out3 */.cas
1cefa 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 e OP_BitOr:
1cefb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cefc 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 same as TK_BITOR
1cefd 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1cefe 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 */.case OP_Shif
1ceff 74 4c 65 66 74 3a 20 20 20 20 20 20 20 20 20 20 tLeft:
1cf00 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1cf01 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 K_LSHIFT, in1, i
1cf02 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 n2, out3 */.case
1cf03 20 4f 50 5f 53 68 69 66 74 52 69 67 68 74 3a 20 OP_ShiftRight:
1cf04 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 { /* s
1cf05 61 6d 65 20 61 73 20 54 4b 5f 52 53 48 49 46 54 ame as TK_RSHIFT
1cf06 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 , in1, in2, out3
1cf07 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f */.#if 0 /* lo
1cf08 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1cf09 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f ved into u.ah */
1cf0a 0a 20 20 69 36 34 20 61 3b 0a 20 20 69 36 34 20 . i64 a;. i64
1cf0b 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 b;.#endif /* loc
1cf0c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1cf0d 65 64 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a ed into u.ah */.
1cf0e 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b . pIn1 = &aMem[
1cf0f 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 pOp->p1];. pIn2
1cf10 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 = &aMem[pOp->p2
1cf11 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 ];. pOut = &aMe
1cf12 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 m[pOp->p3];. if
1cf13 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c ( (pIn1->flags |
1cf14 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 pIn2->flags) &
1cf15 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1cf16 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1cf17 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 tNull(pOut);.
1cf18 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e break;. }. u.
1cf19 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 56 64 ah.a = sqlite3Vd
1cf1a 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 beIntValue(pIn2)
1cf1b 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c ;. u.ah.b = sql
1cf1c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 ite3VdbeIntValue
1cf1d 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 (pIn1);. switch
1cf1e 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b ( pOp->opcode ){
1cf1f 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 . case OP_Bit
1cf20 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 2e 61 And: u.ah.a
1cf21 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 &= u.ah.b;
1cf22 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 break;. case
1cf23 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 OP_BitOr:
1cf24 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62 u.ah.a |= u.ah.b
1cf25 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 ; break;.
1cf26 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 case OP_ShiftLe
1cf27 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d ft: u.ah.a <<=
1cf28 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 65 61 u.ah.b; brea
1cf29 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 k;. default:
1cf2a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 assert( pOp->op
1cf2b 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69 code==OP_ShiftRi
1cf2c 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20 ght );.
1cf2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cf2e 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e u.ah.a >>= u.ah.
1cf2f 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d b; break;. }
1cf30 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 . pOut->u.i = u
1cf31 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54 .ah.a;. MemSetT
1cf32 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 ypeFlag(pOut, ME
1cf33 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b M_Int);. break;
1cf34 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 .}../* Opcode: A
1cf35 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a ddImm P1 P2 * *
1cf36 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 *.** .** Add th
1cf37 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f e constant P2 to
1cf38 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 the value in re
1cf39 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 gister P1..** Th
1cf3a 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 e result is alwa
1cf3b 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a ys an integer..*
1cf3c 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e *.** To force an
1cf3d 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 65 y register to be
1cf3e 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 an integer, jus
1cf3f 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 t add 0..*/.case
1cf40 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 OP_AddImm: {
1cf41 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 /* in1
1cf42 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1cf43 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71 m[pOp->p1];. sq
1cf44 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 lite3VdbeMemInte
1cf45 67 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 gerify(pIn1);.
1cf46 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 pIn1->u.i += pOp
1cf47 2d 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d ->p2;. break;.}
1cf48 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 ../* Opcode: Mus
1cf49 74 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a tBeInt P1 P2 * *
1cf4a 20 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 *.** .** Force
1cf4b 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1cf4c 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 ister P1 to be a
1cf4d 6e 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 n integer. If t
1cf4e 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 he value.** in P
1cf4f 31 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 1 is not an inte
1cf50 67 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 ger and cannot b
1cf51 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f e converted into
1cf52 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 an integer.** w
1cf53 69 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 ithout data loss
1cf54 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 , then jump imme
1cf55 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f diately to P2, o
1cf56 72 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 r if P2==0.** ra
1cf57 69 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 ise an SQLITE_MI
1cf58 53 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e SMATCH exception
1cf59 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 ..*/.case OP_Mus
1cf5a 74 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 tBeInt: {
1cf5b 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1cf5c 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 1 */. pIn1 = &a
1cf5d 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1cf5e 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 applyAffinity(pI
1cf5f 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e n1, SQLITE_AFF_N
1cf60 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 UMERIC, encoding
1cf61 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e );. if( (pIn1->
1cf62 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 flags & MEM_Int)
1cf63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 ==0 ){. if( p
1cf64 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 Op->p2==0 ){.
1cf65 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d rc = SQLITE_M
1cf66 49 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 ISMATCH;. g
1cf67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1cf68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 _error;. }els
1cf69 65 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f e{. pc = pO
1cf6a 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d p->p2 - 1;. }
1cf6b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 . }else{. Me
1cf6c 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e mSetTypeFlag(pIn
1cf6d 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 1, MEM_Int);. }
1cf6e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1cf6f 4f 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 Opcode: RealAffi
1cf70 6e 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a nity P1 * * * *.
1cf71 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 **.** If registe
1cf72 72 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e r P1 holds an in
1cf73 74 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 teger convert it
1cf74 20 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 to a real value
1cf75 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 ..**.** This opc
1cf76 6f 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e ode is used when
1cf77 20 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f extracting info
1cf78 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 rmation from a c
1cf79 6f 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 olumn that.** ha
1cf7a 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e s REAL affinity.
1cf7b 20 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 Such column va
1cf7c 6c 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 lues may still b
1cf7d 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 e stored as.** i
1cf7e 6e 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 ntegers, for spa
1cf7f 63 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 ce efficiency, b
1cf80 75 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74 ut after extract
1cf81 69 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d ion we want them
1cf82 0a 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 .** to have only
1cf83 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a a real value..*
1cf84 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 /.case OP_RealAf
1cf85 66 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 finity: {
1cf86 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e /* in
1cf87 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 1 */. pIn1 = &a
1cf88 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1cf89 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 if( pIn1->flags
1cf8a 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 & MEM_Int ){.
1cf8b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 sqlite3VdbeMemR
1cf8c 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 ealify(pIn1);.
1cf8d 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 }. break;.}..#i
1cf8e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1cf8f 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 T_CAST./* Opcode
1cf90 3a 20 54 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 : ToText P1 * *
1cf91 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 * *.**.** Force
1cf92 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1cf93 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 74 ister P1 to be t
1cf94 65 78 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 ext..** If the v
1cf95 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c alue is numeric,
1cf96 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1cf97 20 73 74 72 69 6e 67 20 75 73 69 6e 67 20 74 68 string using th
1cf98 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 e.** equivalent
1cf99 6f 66 20 70 72 69 6e 74 66 28 29 2e 20 20 42 6c of printf(). Bl
1cf9a 6f 62 20 76 61 6c 75 65 73 20 61 72 65 20 75 6e ob values are un
1cf9b 63 68 61 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 changed and.** a
1cf9c 72 65 20 61 66 74 65 72 77 61 72 64 73 20 73 69 re afterwards si
1cf9d 6d 70 6c 79 20 69 6e 74 65 72 70 72 65 74 65 64 mply interpreted
1cf9e 20 61 73 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 as text..**.**
1cf9f 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 A NULL value is
1cfa0 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 not changed by t
1cfa1 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 his routine. It
1cfa2 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a remains NULL..*
1cfa3 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 /.case OP_ToText
1cfa4 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 : {
1cfa5 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1cfa6 54 4b 5f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 TK_TO_TEXT, in1
1cfa7 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 */. pIn1 = &aMe
1cfa8 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 m[pOp->p1];. if
1cfa9 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
1cfaa 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b MEM_Null ) break
1cfab 3b 0a 20 20 61 73 73 65 72 74 28 20 4d 45 4d 5f ;. assert( MEM_
1cfac 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e Str==(MEM_Blob>>
1cfad 33 29 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 3) );. pIn1->fl
1cfae 61 67 73 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c ags |= (pIn1->fl
1cfaf 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 ags&MEM_Blob)>>3
1cfb0 3b 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 ;. applyAffinit
1cfb1 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 y(pIn1, SQLITE_A
1cfb2 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e FF_TEXT, encodin
1cfb3 67 29 3b 0a 20 20 72 63 20 3d 20 45 78 70 61 6e g);. rc = Expan
1cfb4 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 dBlob(pIn1);. a
1cfb5 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 ssert( pIn1->fla
1cfb6 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 gs & MEM_Str ||
1cfb7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1cfb8 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 );. pIn1->flag
1cfb9 73 20 26 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d s &= ~(MEM_Int|M
1cfba 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 EM_Real|MEM_Blob
1cfbb 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 |MEM_Zero);. UP
1cfbc 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a DATE_MAX_BLOBSIZ
1cfbd 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b E(pIn1);. break
1cfbe 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1cfbf 54 6f 42 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 ToBlob P1 * * *
1cfc0 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1cfc1 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1cfc2 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 20 42 ter P1 to be a B
1cfc3 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 LOB..** If the v
1cfc4 61 6c 75 65 20 69 73 20 6e 75 6d 65 72 69 63 2c alue is numeric,
1cfc5 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 convert it to a
1cfc6 20 73 74 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a string first..*
1cfc7 2a 20 53 74 72 69 6e 67 73 20 61 72 65 20 73 69 * Strings are si
1cfc8 6d 70 6c 79 20 72 65 69 6e 74 65 72 70 72 65 74 mply reinterpret
1cfc9 65 64 20 61 73 20 62 6c 6f 62 73 20 77 69 74 68 ed as blobs with
1cfca 20 6e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f no change.** to
1cfcb 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
1cfcc 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 data..**.** A NU
1cfcd 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1cfce 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1cfcf 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1cfd0 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1cfd1 73 65 20 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 se OP_ToBlob: {
1cfd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cfd3 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 /* same as TK_T
1cfd4 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 O_BLOB, in1 */.
1cfd5 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f pIn1 = &aMem[pO
1cfd6 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70 49 p->p1];. if( pI
1cfd7 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1cfd8 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 Null ) break;.
1cfd9 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1cfda 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 & MEM_Blob)==0
1cfdb 29 7b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69 ){. applyAffi
1cfdc 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 nity(pIn1, SQLIT
1cfdd 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f E_AFF_TEXT, enco
1cfde 64 69 6e 67 29 3b 0a 20 20 20 20 61 73 73 65 72 ding);. asser
1cfdf 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 t( pIn1->flags &
1cfe0 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e MEM_Str || db->
1cfe1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a mallocFailed );.
1cfe2 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c MemSetTypeFl
1cfe3 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f ag(pIn1, MEM_Blo
1cfe4 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 b);. }else{.
1cfe5 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 pIn1->flags &=
1cfe6 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e ~(MEM_TypeMask&~
1cfe7 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 MEM_Blob);. }.
1cfe8 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 UPDATE_MAX_BLOB
1cfe9 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 SIZE(pIn1);. br
1cfea 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1cfeb 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 e: ToNumeric P1
1cfec 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f * * * *.**.** Fo
1cfed 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e rce the value in
1cfee 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 register P1 to
1cfef 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69 74 68 be numeric (eith
1cff0 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 er an.** integer
1cff1 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 or a floating-p
1cff2 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a oint number.).**
1cff3 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 If the value is
1cff4 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 text or blob, t
1cff5 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 ry to convert it
1cff6 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74 68 65 to an using the
1cff7 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f .** equivalent o
1cff8 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 f atoi() or atof
1cff9 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 () and store 0 i
1cffa 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 f no such conver
1cffb 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 sion .** is poss
1cffc 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 ible..**.** A NU
1cffd 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 LL value is not
1cffe 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 changed by this
1cfff 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d routine. It rem
1d000 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 ains NULL..*/.ca
1d001 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a se OP_ToNumeric:
1d002 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
1d003 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d004 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e K_TO_NUMERIC, in
1d005 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 1 */. pIn1 = &a
1d006 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1d007 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 if( (pIn1->flags
1d008 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d & (MEM_Null|MEM
1d009 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d _Int|MEM_Real))=
1d00a 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1d00b 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 3VdbeMemNumerify
1d00c 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 (pIn1);. }. br
1d00d 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a eak;.}.#endif /*
1d00e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
1d00f 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a T */../* Opcode:
1d010 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 ToInt P1 * * *
1d011 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 *.**.** Force th
1d012 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 e value in regis
1d013 74 65 72 20 50 31 20 62 65 20 61 6e 20 69 6e 74 ter P1 be an int
1d014 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65 eger. If.** The
1d015 20 76 61 6c 75 65 20 69 73 20 63 75 72 72 65 6e value is curren
1d016 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 tly a real numbe
1d017 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72 61 63 r, drop its frac
1d018 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 tional part..**
1d019 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 If the value is
1d01a 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 text or blob, tr
1d01b 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 y to convert it
1d01c 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 75 73 to an integer us
1d01d 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 ing the.** equiv
1d01e 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 alent of atoi()
1d01f 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e and store 0 if n
1d020 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f o such conversio
1d021 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a n is possible..*
1d022 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 *.** A NULL valu
1d023 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 e is not changed
1d024 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 by this routine
1d025 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 . It remains NU
1d026 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 LL..*/.case OP_T
1d027 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 oInt: {
1d028 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d029 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 as TK_TO_INT, i
1d02a 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 n1 */. pIn1 = &
1d02b 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 aMem[pOp->p1];.
1d02c 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 if( (pIn1->flag
1d02d 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 s & MEM_Null)==0
1d02e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
1d02f 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 dbeMemIntegerify
1d030 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 (pIn1);. }. br
1d031 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 eak;.}..#ifndef
1d032 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 SQLITE_OMIT_CAST
1d033 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 ./* Opcode: ToRe
1d034 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a al P1 * * * *.**
1d035 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 .** Force the va
1d036 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 lue in register
1d037 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 P1 to be a float
1d038 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 ing point number
1d039 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75 ..** If The valu
1d03a 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 e is currently a
1d03b 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 n integer, conve
1d03c 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 rt it..** If the
1d03d 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f value is text o
1d03e 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 r blob, try to c
1d03f 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 onvert it to an
1d040 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 integer using th
1d041 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 e.** equivalent
1d042 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 of atoi() and st
1d043 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 ore 0.0 if no su
1d044 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 ch conversion is
1d045 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a possible..**.**
1d046 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 A NULL value is
1d047 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 not changed by
1d048 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 this routine. I
1d049 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a t remains NULL..
1d04a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 */.case OP_ToRea
1d04b 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 l: {
1d04c 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 /* same as
1d04d 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 TK_TO_REAL, in1
1d04e 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d */. pIn1 = &aM
1d04f 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 em[pOp->p1];. i
1d050 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 f( (pIn1->flags
1d051 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 & MEM_Null)==0 )
1d052 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
1d053 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 eMemRealify(pIn1
1d054 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a );. }. break;.
1d055 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
1d056 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a TE_OMIT_CAST */.
1d057 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 ./* Opcode: Lt P
1d058 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1d059 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 .** Compare the
1d05a 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 values in regist
1d05b 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49 er P1 and P3. I
1d05c 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31 f reg(P3)<reg(P1
1d05d 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 ) then.** jump t
1d05e 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a o address P2. .
1d05f 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c **.** If the SQL
1d060 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 ITE_JUMPIFNULL b
1d061 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 it of P5 is set
1d062 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50 and either reg(P
1d063 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 1) or.** reg(P3)
1d064 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 is NULL then ta
1d065 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 ke the jump. If
1d066 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 the SQLITE_JUMP
1d067 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 IFNULL .** bit i
1d068 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c s clear then fal
1d069 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72 l thru if either
1d06a 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c operand is NULL
1d06b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 ..**.** The SQLI
1d06c 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 TE_AFF_MASK port
1d06d 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62 ion of P5 must b
1d06e 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 e an affinity ch
1d06f 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c aracter -.** SQL
1d070 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 ITE_AFF_TEXT, SQ
1d071 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 LITE_AFF_INTEGER
1d072 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 , and so forth.
1d073 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 An attempt is ma
1d074 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 de .** to coerce
1d075 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63 both inputs acc
1d076 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61 ording to this a
1d077 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74 ffinity before t
1d078 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e he.** comparison
1d079 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65 is made. If the
1d07a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b SQLITE_AFF_MASK
1d07b 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e is 0x00, then n
1d07c 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 umeric.** affini
1d07d 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 ty is used. Note
1d07e 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 that the affini
1d07f 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 ty conversions a
1d080 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 re stored.** bac
1d081 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 k into the input
1d082 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d083 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f d P3. So this o
1d084 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a pcode can cause.
1d085 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68 ** persistent ch
1d086 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65 anges to registe
1d087 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a rs P1 and P3..**
1d088 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e .** Once any con
1d089 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61 versions have ta
1d08a 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e ken place, and n
1d08b 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20 either value is
1d08c 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 NULL, .** the va
1d08d 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65 lues are compare
1d08e 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 d. If both value
1d08f 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e s are blobs then
1d090 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 memcmp() is.**
1d091 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e used to determin
1d092 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 e the results of
1d093 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e the comparison.
1d094 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
1d095 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68 .** are text, th
1d096 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 en the appropria
1d097 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e te collating fun
1d098 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 ction specified
1d099 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65 in.** P4 is use
1d09a 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 d to do the comp
1d09b 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 arison. If P4 i
1d09c 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20 s not specified
1d09d 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 then.** memcmp()
1d09e 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 is used to comp
1d09f 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e are text string.
1d0a0 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 If both values
1d0a1 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c are.** numeric,
1d0a2 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 then a numeric
1d0a3 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73 comparison is us
1d0a4 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76 ed. If the two v
1d0a5 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 alues.** are of
1d0a6 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c different types,
1d0a7 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 then numbers ar
1d0a8 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
1d0a9 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 s than.** string
1d0aa 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72 s and strings ar
1d0ab 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 e considered les
1d0ac 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a s than blobs..**
1d0ad 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 .** If the SQLIT
1d0ae 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66 E_STOREP2 bit of
1d0af 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e P5 is set, then
1d0b0 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 do not jump. I
1d0b1 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 nstead,.** store
1d0b2 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c a boolean resul
1d0b3 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20 t (either 0, or
1d0b4 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 1, or NULL) in r
1d0b5 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f egister P2..*/./
1d0b6 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 * Opcode: Ne P1
1d0b7 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a P2 P3 P4 P5.**.*
1d0b8 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 * This works jus
1d0b9 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 t like the Lt op
1d0ba 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 code except that
1d0bb 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b the jump is tak
1d0bc 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 en if.** the ope
1d0bd 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 rands in registe
1d0be 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 rs P1 and P3 are
1d0bf 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65 not equal. See
1d0c0 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 the Lt opcode f
1d0c1 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c or.** additional
1d0c2 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a information..**
1d0c3 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 .** If SQLITE_NU
1d0c4 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50 LLEQ is set in P
1d0c5 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 5 then the resul
1d0c6 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 t of comparison
1d0c7 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72 is always either
1d0c8 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73 .** true or fals
1d0c9 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e e and is never N
1d0ca 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70 ULL. If both op
1d0cb 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 erands are NULL
1d0cc 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a then the result.
1d0cd 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e ** of comparison
1d0ce 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20 65 is false. If e
1d0cf 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d0d0 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 NULL then the r
1d0d1 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a esult is true..*
1d0d2 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 65 * If neither ope
1d0d3 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 rand is NULL the
1d0d4 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74 the result is t
1d0d5 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f he same as it wo
1d0d6 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65 uld be if.** the
1d0d7 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 SQLITE_NULLEQ f
1d0d8 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64 lag were omitted
1d0d9 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 from P5..*/./*
1d0da 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32 Opcode: Eq P1 P2
1d0db 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 P3 P4 P5.**.**
1d0dc 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 This works just
1d0dd 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f like the Lt opco
1d0de 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 de except that t
1d0df 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e he jump is taken
1d0e0 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 if.** the opera
1d0e1 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 nds in registers
1d0e2 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65 P1 and P3 are e
1d0e3 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 qual..** See the
1d0e4 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 Lt opcode for a
1d0e5 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
1d0e6 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ation..**.** If
1d0e7 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 SQLITE_NULLEQ is
1d0e8 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20 set in P5 then
1d0e9 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f the result of co
1d0ea 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 mparison is alwa
1d0eb 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 ys either.** tru
1d0ec 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69 e or false and i
1d0ed 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 s never NULL. I
1d0ee 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 f both operands
1d0ef 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 are NULL then th
1d0f0 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 e result.** of c
1d0f1 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75 omparison is tru
1d0f2 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70 e. If either op
1d0f3 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 erand is NULL th
1d0f4 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 en the result is
1d0f5 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65 false..** If ne
1d0f6 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 ither operand is
1d0f7 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65 NULL the the re
1d0f8 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65 sult is the same
1d0f9 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 as it would be
1d0fa 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 if.** the SQLITE
1d0fb 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 _NULLEQ flag wer
1d0fc 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 e omitted from P
1d0fd 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 5..*/./* Opcode:
1d0fe 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 Le P1 P2 P3 P4
1d0ff 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f P5.**.** This wo
1d100 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 rks just like th
1d101 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 e Lt opcode exce
1d102 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 pt that the jump
1d103 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 is taken if.**
1d104 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d105 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65 egister P3 is le
1d106 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c ss than or equal
1d107 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 to the content
1d108 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 of.** register P
1d109 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 1. See the Lt o
1d10a 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 pcode for additi
1d10b 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e onal information
1d10c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1d10d 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 Gt P1 P2 P3 P4 P
1d10e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 5.**.** This wor
1d10f 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 ks just like the
1d110 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 Lt opcode excep
1d111 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 t that the jump
1d112 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 is taken if.** t
1d113 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1d114 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 gister P3 is gre
1d115 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f ater than the co
1d116 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 ntent of.** regi
1d117 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 ster P1. See th
1d118 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 e Lt opcode for
1d119 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 additional infor
1d11a 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 mation..*/./* Op
1d11b 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50 code: Ge P1 P2 P
1d11c 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 3 P4 P5.**.** Th
1d11d 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 is works just li
1d11e 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 ke the Lt opcode
1d11f 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 except that the
1d120 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 jump is taken i
1d121 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 f.** the content
1d122 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 of register P3
1d123 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
1d124 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 or equal to the
1d125 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 content of.** re
1d126 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 gister P1. See
1d127 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f the Lt opcode fo
1d128 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 r additional inf
1d129 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 ormation..*/.cas
1d12a 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20 e OP_Eq:
1d12b 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 /* same a
1d12c 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 s TK_EQ, jump, i
1d12d 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 n1, in3 */.case
1d12e 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20 OP_Ne:
1d12f 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d130 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 TK_NE, jump, in1
1d131 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 , in3 */.case OP
1d132 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 _Lt:
1d133 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b /* same as TK
1d134 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 _LT, jump, in1,
1d135 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c in3 */.case OP_L
1d136 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e:
1d137 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c /* same as TK_L
1d138 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e E, jump, in1, in
1d139 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 3 */.case OP_Gt:
1d13a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1d13b 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c * same as TK_GT,
1d13c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 jump, in1, in3
1d13d 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b */.case OP_Ge: {
1d13e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1d13f 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a same as TK_GE, j
1d140 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f ump, in1, in3 */
1d141 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d142 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d143 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20 into u.ai */.
1d144 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 20 int res;
1d145 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 /* Result of
1d146 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
1d147 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20 of pIn1 against
1d148 70 49 6e 33 20 2a 2f 0a 20 20 63 68 61 72 20 61 pIn3 */. char a
1d149 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a ffinity; /*
1d14a 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 Affinity to use
1d14b 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 for comparison
1d14c 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 */.#endif /* loc
1d14d 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1d14e 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a ed into u.ai */.
1d14f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b . pIn1 = &aMem[
1d150 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 pOp->p1];. pIn3
1d151 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1d152 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e ];. if( (pIn1->
1d153 66 6c 61 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c flags | pIn3->fl
1d154 61 67 73 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b ags)&MEM_Null ){
1d155 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62 . /* One or b
1d156 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 oth operands are
1d157 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28 NULL */. if(
1d158 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 pOp->p5 & SQLIT
1d159 45 5f 4e 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20 E_NULLEQ ){.
1d15a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e /* If SQLITE_N
1d15b 55 4c 4c 45 51 20 69 73 20 73 65 74 20 28 77 68 ULLEQ is set (wh
1d15c 69 63 68 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 ich will only ha
1d15d 70 70 65 6e 20 69 66 20 74 68 65 20 6f 70 65 72 ppen if the oper
1d15e 61 74 6f 72 20 69 73 0a 20 20 20 20 20 20 2a 2a ator is. **
1d15f 20 4f 50 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29 OP_Eq or OP_Ne)
1d160 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a then take the j
1d161 75 6d 70 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e ump or not depen
1d162 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a ding on whether.
1d163 20 20 20 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 ** or not
1d164 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 both operands ar
1d165 65 20 6e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f e null.. */
1d166 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1d167 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 Op->opcode==OP_E
1d168 71 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 q || pOp->opcode
1d169 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 ==OP_Ne );.
1d16a 20 75 2e 61 69 2e 72 65 73 20 3d 20 28 70 49 6e u.ai.res = (pIn
1d16b 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 33 2d 1->flags & pIn3-
1d16c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c >flags & MEM_Nul
1d16d 6c 29 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 l)==0;. }else
1d16e 7b 0a 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 {. /* SQLIT
1d16f 45 5f 4e 55 4c 4c 45 51 20 69 73 20 63 6c 65 61 E_NULLEQ is clea
1d170 72 20 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f r and at least o
1d171 6e 65 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 ne operand is NU
1d172 4c 4c 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 LL,. ** the
1d173 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 n the result is
1d174 61 6c 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20 always NULL..
1d175 20 20 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 ** The jump i
1d176 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 s taken if the S
1d177 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c QLITE_JUMPIFNULL
1d178 20 62 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 bit is set..
1d179 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 */. if(
1d17a 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 pOp->p5 & SQLITE
1d17b 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 _STOREP2 ){.
1d17c 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d pOut = &aMem
1d17d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 [pOp->p2];.
1d17e 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 MemSetTypeFla
1d17f 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c g(pOut, MEM_Null
1d180 29 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 );. REGIS
1d181 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 TER_TRACE(pOp->p
1d182 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 2, pOut);.
1d183 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 }else if( pOp->p
1d184 35 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 5 & SQLITE_JUMPI
1d185 46 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 FNULL ){.
1d186 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b pc = pOp->p2-1;
1d187 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 . }. b
1d188 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 reak;. }. }e
1d189 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 lse{. /* Neit
1d18a 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e her operand is N
1d18b 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61 ULL. Do a compa
1d18c 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e rison. */. u.
1d18d 61 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70 4f ai.affinity = pO
1d18e 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 41 p->p5 & SQLITE_A
1d18f 46 46 5f 4d 41 53 4b 3b 0a 20 20 20 20 69 66 28 FF_MASK;. if(
1d190 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 29 u.ai.affinity )
1d191 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66 66 {. applyAff
1d192 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 69 inity(pIn1, u.ai
1d193 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 .affinity, encod
1d194 69 6e 67 29 3b 0a 20 20 20 20 20 20 61 70 70 6c ing);. appl
1d195 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 yAffinity(pIn3,
1d196 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 u.ai.affinity, e
1d197 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 ncoding);.
1d198 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1d199 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d iled ) goto no_m
1d19a 65 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 em;. }.. a
1d19b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1d19c 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c pe==P4_COLLSEQ |
1d19d 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d | pOp->p4.pColl=
1d19e 3d 30 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 =0 );. Expand
1d19f 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 20 20 Blob(pIn1);.
1d1a0 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 ExpandBlob(pIn3)
1d1a1 3b 0a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d ;. u.ai.res =
1d1a2 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 sqlite3MemCompa
1d1a3 72 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 re(pIn3, pIn1, p
1d1a4 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 Op->p4.pColl);.
1d1a5 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 }. switch( pOp
1d1a6 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 ->opcode ){.
1d1a7 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20 75 case OP_Eq: u
1d1a8 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 .ai.res = u.ai.r
1d1a9 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61 6b es==0; break
1d1aa 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 ;. case OP_Ne
1d1ab 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 : u.ai.res =
1d1ac 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20 20 u.ai.res!=0;
1d1ad 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 break;. case
1d1ae 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69 2e OP_Lt: u.ai.
1d1af 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 30 res = u.ai.res<0
1d1b0 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 ; break;.
1d1b1 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 case OP_Le:
1d1b2 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 u.ai.res = u.ai
1d1b3 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 .res<=0; bre
1d1b4 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f ak;. case OP_
1d1b5 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 Gt: u.ai.res
1d1b6 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20 20 = u.ai.res>0;
1d1b7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 break;. de
1d1b8 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e 61 fault: u.a
1d1b9 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 i.res = u.ai.res
1d1ba 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a >=0; break;.
1d1bb 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e }.. if( pOp->
1d1bc 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 p5 & SQLITE_STOR
1d1bd 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 EP2 ){. pOut
1d1be 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d = &aMem[pOp->p2]
1d1bf 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 ;. MemSetType
1d1c0 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 Flag(pOut, MEM_I
1d1c1 6e 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 nt);. pOut->u
1d1c2 2e 69 20 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20 .i = u.ai.res;.
1d1c3 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 REGISTER_TRAC
1d1c4 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 E(pOp->p2, pOut)
1d1c5 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e ;. }else if( u.
1d1c6 61 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 63 ai.res ){. pc
1d1c7 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 = pOp->p2-1;.
1d1c8 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1d1c9 20 4f 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 Opcode: Permuta
1d1ca 74 69 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a tion * * * P4 *.
1d1cb 2a 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 **.** Set the pe
1d1cc 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20 62 rmutation used b
1d1cd 79 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 y the OP_Compare
1d1ce 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20 operator to be
1d1cf 74 68 65 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 the array.** of
1d1d0 69 6e 74 65 67 65 72 73 20 69 6e 20 50 34 2e 0a integers in P4..
1d1d1 2a 2a 0a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 **.** The permut
1d1d2 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 ation is only va
1d1d3 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 lid until the ne
1d1d4 78 74 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f xt OP_Permutatio
1d1d5 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a n, OP_Compare,.*
1d1d6 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 * OP_Halt, or OP
1d1d7 5f 52 65 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 _ResultRow. Typ
1d1d8 69 63 61 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 ically the OP_Pe
1d1d9 72 6d 75 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 rmutation should
1d1da 20 6f 63 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 occur.** immedi
1d1db 61 74 65 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 ately prior to t
1d1dc 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a he OP_Compare..*
1d1dd 2f 0a 63 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 /.case OP_Permut
1d1de 61 74 69 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 ation: {. asser
1d1df 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d t( pOp->p4type==
1d1e0 50 34 5f 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 P4_INTARRAY );.
1d1e1 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1d1e2 2e 61 69 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 .ai );. aPermut
1d1e3 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a e = pOp->p4.ai;.
1d1e4 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1d1e5 70 63 6f 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 pcode: Compare P
1d1e6 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1d1e7 2a 2a 20 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 ** Compare to ve
1d1e8 63 74 6f 72 73 20 6f 66 20 72 65 67 69 73 74 65 ctors of registe
1d1e9 72 73 20 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 rs in reg(P1)..r
1d1ea 65 67 28 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c eg(P1+P3-1) (all
1d1eb 20 74 68 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 this.** one "A"
1d1ec 29 20 61 6e 64 20 69 6e 20 72 65 67 28 50 32 29 ) and in reg(P2)
1d1ed 2e 2e 72 65 67 28 50 32 2b 50 33 2d 31 29 20 28 ..reg(P2+P3-1) (
1d1ee 22 42 22 29 2e 20 20 53 61 76 65 20 74 68 65 20 "B"). Save the
1d1ef 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 result of.** the
1d1f0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 comparison for
1d1f1 75 73 65 20 62 79 20 74 68 65 20 6e 65 78 74 20 use by the next
1d1f2 4f 50 5f 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 OP_Jump instruct
1d1f3 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 ..**.** P4 is a
1d1f4 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1d1f5 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 63 e that defines c
1d1f6 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
1d1f7 65 73 20 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f es and sort.** o
1d1f8 72 64 65 72 73 20 66 6f 72 20 74 68 65 20 63 6f rders for the co
1d1f9 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 mparison. The p
1d1fa 65 72 6d 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 ermutation appli
1d1fb 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a es to registers.
1d1fc 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 ** only. The Ke
1d1fd 79 49 6e 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 yInfo elements a
1d1fe 72 65 20 75 73 65 64 20 73 65 71 75 65 6e 74 69 re used sequenti
1d1ff 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ally..**.** The
1d200 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 comparison is a
1d201 73 6f 72 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c sort comparison,
1d202 20 73 6f 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 so NULLs compar
1d203 65 20 65 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c e equal,.** NULL
1d204 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 s are less than
1d205 6e 75 6d 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 numbers, numbers
1d206 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 73 are less than s
1d207 74 72 69 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 trings,.** and s
1d208 74 72 69 6e 67 73 20 61 72 65 20 6c 65 73 73 20 trings are less
1d209 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 than blobs..*/.c
1d20a 61 73 65 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 ase OP_Compare:
1d20b 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 {.#if 0 /* loca
1d20c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1d20d 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 d into u.aj */.
1d20e 20 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b int n;. int i;
1d20f 0a 20 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 . int p1;. int
1d210 20 70 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 p2;. const Key
1d211 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a Info *pKeyInfo;.
1d212 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c int idx;. Col
1d213 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 lSeq *pColl;
1d214 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
1d215 75 65 6e 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 uence to use on
1d216 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 this term */. i
1d217 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 nt bRev;
1d218 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 /* True for DE
1d219 53 43 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 SCENDING sort or
1d21a 64 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a der */.#endif /*
1d21b 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d21c 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a moved into u.aj
1d21d 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 */.. u.aj.n =
1d21e 70 4f 70 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e pOp->p3;. u.aj.
1d21f 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e pKeyInfo = pOp->
1d220 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 p4.pKeyInfo;. a
1d221 73 73 65 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 ssert( u.aj.n>0
1d222 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 );. assert( u.a
1d223 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b j.pKeyInfo!=0 );
1d224 0a 20 20 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 . u.aj.p1 = pOp
1d225 2d 3e 70 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 ->p1;. u.aj.p2
1d226 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53 = pOp->p2;.#if S
1d227 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 QLITE_DEBUG. if
1d228 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 ( aPermute ){.
1d229 20 20 69 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b int k, mx = 0;
1d22a 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c . for(k=0; k<
1d22b 75 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 u.aj.n; k++) if(
1d22c 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 aPermute[k]>mx
1d22d 29 20 6d 78 20 3d 20 61 50 65 72 6d 75 74 65 5b ) mx = aPermute[
1d22e 6b 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 k];. assert(
1d22f 75 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 u.aj.p1>0 && u.a
1d230 6a 2e 70 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d j.p1+mx<=p->nMem
1d231 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 +1 );. assert
1d232 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 ( u.aj.p2>0 && u
1d233 2e 61 6a 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d .aj.p2+mx<=p->nM
1d234 65 6d 2b 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b em+1 );. }else{
1d235 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 . assert( u.a
1d236 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 j.p1>0 && u.aj.p
1d237 31 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 1+u.aj.n<=p->nMe
1d238 6d 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 m+1 );. asser
1d239 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 t( u.aj.p2>0 &&
1d23a 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d u.aj.p2+u.aj.n<=
1d23b 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d p->nMem+1 );. }
1d23c 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
1d23d 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72 E_DEBUG */. for
1d23e 28 75 2e 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e (u.aj.i=0; u.aj.
1d23f 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 i<u.aj.n; u.aj.i
1d240 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64 ++){. u.aj.id
1d241 78 20 3d 20 61 50 65 72 6d 75 74 65 20 3f 20 61 x = aPermute ? a
1d242 50 65 72 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 Permute[u.aj.i]
1d243 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45 : u.aj.i;. RE
1d244 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 GISTER_TRACE(u.a
1d245 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 j.p1+u.aj.idx, &
1d246 61 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 aMem[u.aj.p1+u.a
1d247 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 j.idx]);. REG
1d248 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a ISTER_TRACE(u.aj
1d249 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61 .p2+u.aj.idx, &a
1d24a 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a Mem[u.aj.p2+u.aj
1d24b 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 .idx]);. asse
1d24c 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61 6a 2e rt( u.aj.i<u.aj.
1d24d 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 pKeyInfo->nField
1d24e 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70 43 6f );. u.aj.pCo
1d24f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e ll = u.aj.pKeyIn
1d250 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a 2e 69 fo->aColl[u.aj.i
1d251 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52 65 76 ];. u.aj.bRev
1d252 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f = u.aj.pKeyInfo
1d253 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75 2e 61 ->aSortOrder[u.a
1d254 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61 j.i];. iCompa
1d255 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 re = sqlite3MemC
1d256 6f 6d 70 61 72 65 28 26 61 4d 65 6d 5b 75 2e 61 ompare(&aMem[u.a
1d257 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 j.p1+u.aj.idx],
1d258 26 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e &aMem[u.aj.p2+u.
1d259 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43 aj.idx], u.aj.pC
1d25a 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 oll);. if( iC
1d25b 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20 ompare ){.
1d25c 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20 if( u.aj.bRev )
1d25d 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d iCompare = -iCom
1d25e 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61 pare;. brea
1d25f 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 k;. }. }. a
1d260 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 Permute = 0;. b
1d261 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d262 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50 de: Jump P1 P2 P
1d263 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 3 * *.**.** Jump
1d264 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74 to the instruct
1d265 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50 ion at address P
1d266 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70 1, P2, or P3 dep
1d267 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 ending on whethe
1d268 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 r.** in the most
1d269 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 recent OP_Compa
1d26a 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 re instruction t
1d26b 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73 he P1 vector was
1d26c 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 less than.** eq
1d26d 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 ual to, or great
1d26e 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76 er than the P2 v
1d26f 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76 ector, respectiv
1d270 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ely..*/.case OP_
1d271 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20 Jump: {
1d272 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 /* jump */.
1d273 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 if( iCompare<0
1d274 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d ){. pc = pOp-
1d275 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 >p1 - 1;. }else
1d276 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 if( iCompare==0
1d277 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 ){. pc = pOp
1d278 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 ->p2 - 1;. }els
1d279 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d e{. pc = pOp-
1d27a 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 >p3 - 1;. }. b
1d27b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1d27c 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33 de: And P1 P2 P3
1d27d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 * *.**.** Take
1d27e 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 the logical AND
1d27f 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e of the values in
1d280 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e registers P1 an
1d281 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 d P2 and.** writ
1d282 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 e the result int
1d283 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a o register P3..*
1d284 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 *.** If either P
1d285 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61 1 or P2 is 0 (fa
1d286 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65 lse) then the re
1d287 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69 sult is 0 even i
1d288 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69 f.** the other i
1d289 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 nput is NULL. A
1d28a 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f NULL and true o
1d28b 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 r two NULLs give
1d28c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 .** a NULL outpu
1d28d 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a t..*/./* Opcode:
1d28e 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a Or P1 P2 P3 * *
1d28f 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 .**.** Take the
1d290 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 logical OR of th
1d291 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 e values in regi
1d292 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 ster P1 and P2 a
1d293 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 nd.** store the
1d294 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74 answer in regist
1d295 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 er P3..**.** If
1d296 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 either P1 or P2
1d297 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 is nonzero (true
1d298 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c ) then the resul
1d299 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a t is 1 (true).**
1d29a 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68 even if the oth
1d29b 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c er input is NULL
1d29c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 . A NULL and fa
1d29d 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 lse or two NULLs
1d29e 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 .** give a NULL
1d29f 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 output..*/.case
1d2a0 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20 OP_And:
1d2a1 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 /* same as
1d2a2 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 TK_AND, in1, in2
1d2a3 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f , out3 */.case O
1d2a4 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20 P_Or: {
1d2a5 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 /* same as T
1d2a6 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 K_OR, in1, in2,
1d2a7 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f out3 */.#if 0 /
1d2a8 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d2a9 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d2aa 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20 k */. int v1;
1d2ab 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e /* Left operan
1d2ac 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d d: 0==FALSE, 1=
1d2ad 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 =TRUE, 2==UNKNOW
1d2ae 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 N or NULL */. i
1d2af 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 nt v2; /* Rig
1d2b0 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 ht operand: 0==F
1d2b1 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 ALSE, 1==TRUE, 2
1d2b2 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c ==UNKNOWN or NUL
1d2b3 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c L */.#endif /* l
1d2b4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d2b5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a oved into u.ak *
1d2b6 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 /.. pIn1 = &aMe
1d2b7 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 m[pOp->p1];. if
1d2b8 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 ( pIn1->flags &
1d2b9 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1d2ba 75 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d u.ak.v1 = 2;. }
1d2bb 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 else{. u.ak.v
1d2bc 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 1 = sqlite3VdbeI
1d2bd 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 ntValue(pIn1)!=0
1d2be 3b 0a 20 20 7d 0a 20 20 70 49 6e 32 20 3d 20 26 ;. }. pIn2 = &
1d2bf 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 aMem[pOp->p2];.
1d2c0 20 69 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 if( pIn2->flags
1d2c1 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 & MEM_Null ){.
1d2c2 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a u.ak.v2 = 2;.
1d2c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 }else{. u.a
1d2c4 6b 2e 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64 k.v2 = sqlite3Vd
1d2c5 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 beIntValue(pIn2)
1d2c6 21 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 !=0;. }. if( p
1d2c7 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 Op->opcode==OP_A
1d2c8 6e 64 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 nd ){. static
1d2c9 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 const unsigned
1d2ca 63 68 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d char and_logic[]
1d2cb 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c = { 0, 0, 0, 0,
1d2cc 20 31 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 1, 2, 0, 2, 2 }
1d2cd 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 ;. u.ak.v1 =
1d2ce 61 6e 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 and_logic[u.ak.v
1d2cf 31 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 1*3+u.ak.v2];.
1d2d0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 }else{. stati
1d2d1 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 c const unsigned
1d2d2 20 63 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d char or_logic[]
1d2d3 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c = { 0, 1, 2, 1,
1d2d4 20 31 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 1, 1, 2, 1, 2 }
1d2d5 3b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 ;. u.ak.v1 =
1d2d6 6f 72 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 or_logic[u.ak.v1
1d2d7 2a 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d *3+u.ak.v2];. }
1d2d8 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b . pOut = &aMem[
1d2d9 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 pOp->p3];. if(
1d2da 75 2e 61 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 u.ak.v1==2 ){.
1d2db 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 MemSetTypeFlag
1d2dc 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 (pOut, MEM_Null)
1d2dd 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 ;. }else{. p
1d2de 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e Out->u.i = u.ak.
1d2df 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 v1;. MemSetTy
1d2e0 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d peFlag(pOut, MEM
1d2e1 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 _Int);. }. bre
1d2e2 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1d2e3 3a 20 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 : Not P1 P2 * *
1d2e4 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 *.**.** Interpre
1d2e5 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 t the value in r
1d2e6 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 20 egister P1 as a
1d2e7 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 boolean value.
1d2e8 53 74 6f 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f Store the.** boo
1d2e9 6c 65 61 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 lean complement
1d2ea 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 in register P2.
1d2eb 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e If the value in
1d2ec 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 register P1 is
1d2ed 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 .** NULL, then a
1d2ee 20 4e 55 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 NULL is stored
1d2ef 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f in P2..*/.case O
1d2f0 50 5f 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 P_Not: {
1d2f1 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 /* same
1d2f2 61 73 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 2c 20 as TK_NOT, in1,
1d2f3 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d out2 */. pIn1 =
1d2f4 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b &aMem[pOp->p1];
1d2f5 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b . pOut = &aMem[
1d2f6 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 pOp->p2];. if(
1d2f7 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1d2f8 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 M_Null ){. sq
1d2f9 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e lite3VdbeMemSetN
1d2fa 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c ull(pOut);. }el
1d2fb 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 se{. sqlite3V
1d2fc 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 dbeMemSetInt64(p
1d2fd 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64 62 Out, !sqlite3Vdb
1d2fe 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 eIntValue(pIn1))
1d2ff 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1d300 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 ../* Opcode: Bit
1d301 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Not P1 P2 * * *.
1d302 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 **.** Interpret
1d303 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1d304 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61 6e egister P1 as an
1d305 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72 65 integer. Store
1d306 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d the.** ones-com
1d307 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 plement of the P
1d308 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 1 value into reg
1d309 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50 31 ister P2. If P1
1d30a 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c holds.** a NULL
1d30b 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 then store a NU
1d30c 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 LL in P2..*/.cas
1d30d 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 e OP_BitNot: {
1d30e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 /* sa
1d30f 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c me as TK_BITNOT,
1d310 20 69 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 in1, out2 */.
1d311 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1d312 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 ->p1];. pOut =
1d313 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a &aMem[pOp->p2];.
1d314 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 if( pIn1->flag
1d315 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a s & MEM_Null ){.
1d316 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d317 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b emSetNull(pOut);
1d318 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 . }else{. sq
1d319 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 lite3VdbeMemSetI
1d31a 6e 74 36 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 nt64(pOut, ~sqli
1d31b 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 te3VdbeIntValue(
1d31c 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 pIn1));. }. br
1d31d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1d31e 65 3a 20 49 66 20 50 31 20 50 32 20 50 33 20 2a e: If P1 P2 P3 *
1d31f 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d320 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
1d321 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d322 69 73 20 74 72 75 65 2e 20 20 54 68 65 20 76 61 is true. The va
1d323 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e lue is.** is con
1d324 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 sidered true if
1d325 69 74 20 69 73 20 6e 75 6d 65 72 69 63 20 61 6e it is numeric an
1d326 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 d non-zero. If
1d327 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 the value.** in
1d328 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 P1 is NULL then
1d329 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
1d32a 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a P3 is true..*/.
1d32b 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 /* Opcode: IfNot
1d32c 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a P1 P2 P3 * *.**
1d32d 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 .** Jump to P2 i
1d32e 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 f the value in r
1d32f 65 67 69 73 74 65 72 20 50 31 20 69 73 20 46 61 egister P1 is Fa
1d330 6c 73 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 lse. The value
1d331 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 is.** is conside
1d332 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20 68 red true if it h
1d333 61 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61 6c as a numeric val
1d334 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 ue of zero. If
1d335 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 the value.** in
1d336 50 31 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 P1 is NULL then
1d337 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 take the jump if
1d338 20 50 33 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a P3 is true..*/.
1d339 63 61 73 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 case OP_If:
1d33a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a /* j
1d33b 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 ump, in1 */.case
1d33c 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 OP_IfNot: {
1d33d 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c /* jump,
1d33e 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f in1 */.#if 0 /
1d33f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1d340 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 s moved into u.a
1d341 6c 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 l */. int c;.#e
1d342 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1d343 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1d344 74 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 70 49 6e to u.al */. pIn
1d345 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 1 = &aMem[pOp->p
1d346 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 1];. if( pIn1->
1d347 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c flags & MEM_Null
1d348 20 29 7b 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d ){. u.al.c =
1d349 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 pOp->p3;. }els
1d34a 65 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 e{.#ifdef SQLITE
1d34b 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 _OMIT_FLOATING_P
1d34c 4f 49 4e 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20 OINT. u.al.c
1d34d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 = sqlite3VdbeInt
1d34e 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a Value(pIn1)!=0;.
1d34f 23 65 6c 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63 #else. u.al.c
1d350 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 = sqlite3VdbeRe
1d351 61 6c 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 alValue(pIn1)!=0
1d352 2e 30 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 .0;.#endif. i
1d353 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d f( pOp->opcode==
1d354 4f 50 5f 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e OP_IfNot ) u.al.
1d355 63 20 3d 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d c = !u.al.c;. }
1d356 0a 20 20 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b . if( u.al.c ){
1d357 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 . pc = pOp->p
1d358 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 2-1;. }. break
1d359 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1d35a 49 73 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a IsNull P1 P2 * *
1d35b 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f *.**.** Jump to
1d35c 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 P2 if the value
1d35d 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 in register P1
1d35e 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 is NULL..*/.case
1d35f 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 OP_IsNull: {
1d360 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 /* same
1d361 20 61 73 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a as TK_ISNULL, j
1d362 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 ump, in1 */. pI
1d363 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e n1 = &aMem[pOp->
1d364 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 p1];. if( (pIn1
1d365 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 ->flags & MEM_Nu
1d366 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 63 ll)!=0 ){. pc
1d367 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1d368 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d369 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 /* Opcode: NotNu
1d36a 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a ll P1 P2 * * *.*
1d36b 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 *.** Jump to P2
1d36c 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 if the value in
1d36d 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6e register P1 is n
1d36e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 ot NULL. .*/.ca
1d36f 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b se OP_NotNull: {
1d370 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 /* s
1d371 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c ame as TK_NOTNUL
1d372 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a L, jump, in1 */.
1d373 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1d374 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28 Op->p1];. if( (
1d375 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1d376 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 M_Null)==0 ){.
1d377 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1d378 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1d379 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 .}../* Opcode: C
1d37a 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33 20 50 olumn P1 P2 P3 P
1d37b 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 4 P5.**.** Inter
1d37c 70 72 65 74 20 74 68 65 20 64 61 74 61 20 74 68 pret the data th
1d37d 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69 at cursor P1 poi
1d37e 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74 72 75 nts to as a stru
1d37f 63 74 75 72 65 20 62 75 69 6c 74 20 75 73 69 6e cture built usin
1d380 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 g.** the MakeRec
1d381 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ord instruction.
1d382 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b 65 52 (See the MakeR
1d383 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 ecord opcode for
1d384 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 additional.** i
1d385 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 nformation about
1d386 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 the format of t
1d387 68 65 20 64 61 74 61 2e 29 20 20 45 78 74 72 61 he data.) Extra
1d388 63 74 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c ct the P2-th col
1d389 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 umn.** from this
1d38a 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74 68 65 record. If the
1d38b 72 65 20 61 72 65 20 6c 65 73 73 20 74 68 61 74 re are less that
1d38c 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 (P2+1) .** valu
1d38d 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 es in the record
1d38e 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c , extract a NULL
1d38f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 ..**.** The valu
1d390 65 20 65 78 74 72 61 63 74 65 64 20 69 73 20 73 e extracted is s
1d391 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1d392 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 r P3..**.** If t
1d393 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 he column contai
1d394 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20 50 32 ns fewer than P2
1d395 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 fields, then ex
1d396 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f tract a NULL. O
1d397 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50 34 20 r,.** if the P4
1d398 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 50 34 argument is a P4
1d399 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76 61 6c _MEM use the val
1d39a 75 65 20 6f 66 20 74 68 65 20 50 34 20 61 72 67 ue of the P4 arg
1d39b 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 ument as.** the
1d39c 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 result..**.** If
1d39d 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 the OPFLAG_CLEA
1d39e 52 43 41 43 48 45 20 62 69 74 20 69 73 20 73 65 RCACHE bit is se
1d39f 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31 20 69 t on P5 and P1 i
1d3a0 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 s a pseudo-table
1d3a1 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e cursor,.** then
1d3a2 20 74 68 65 20 63 61 63 68 65 20 6f 66 20 74 68 the cache of th
1d3a3 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 73 65 e cursor is rese
1d3a4 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74 72 61 t prior to extra
1d3a5 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e cting the column
1d3a6 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 4f ..** The first O
1d3a7 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74 P_Column against
1d3a8 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 a pseudo-table
1d3a9 61 66 74 65 72 20 74 68 65 20 76 61 6c 75 65 20 after the value
1d3aa 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a of the content.*
1d3ab 2a 20 72 65 67 69 73 74 65 72 20 68 61 73 20 63 * register has c
1d3ac 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20 68 61 hanged should ha
1d3ad 76 65 20 74 68 69 73 20 62 69 74 20 73 65 74 2e ve this bit set.
1d3ae 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 .*/.case OP_Colu
1d3af 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 mn: {.#if 0 /*
1d3b0 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d3b1 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20 moved into u.am
1d3b2 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f 61 64 */. u32 payload
1d3b3 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 Size; /* Numbe
1d3b4 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 r of bytes in th
1d3b5 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36 e record */. i6
1d3b6 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 4 payloadSize64;
1d3b7 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1d3b8 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 tes in the recor
1d3b9 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 d */. int p1;
1d3ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 /* P1
1d3bb 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63 value of the opc
1d3bc 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b ode */. int p2;
1d3bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 /* c
1d3be 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 olumn number to
1d3bf 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 retrieve */. Vd
1d3c0 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 beCursor *pC;
1d3c1 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 /* The VDBE cur
1d3c2 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a sor */. char *z
1d3c3 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 Rec; /* P
1d3c4 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 ointer to comple
1d3c5 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a te record-data *
1d3c6 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 /. BtCursor *pC
1d3c7 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 rsr; /* The BT
1d3c8 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 ree cursor */.
1d3c9 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 u32 *aType;
1d3ca 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 /* aType[i] h
1d3cb 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63 olds the numeric
1d3cc 20 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74 type of the i-t
1d3cd 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 h column */. u3
1d3ce 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20 20 20 2 *aOffset;
1d3cf 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 /* aOffset[i] i
1d3d0 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74 61 72 s offset to star
1d3d1 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20 69 2d t of data for i-
1d3d2 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 th column */. i
1d3d3 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 nt nField;
1d3d4 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 /* number of f
1d3d5 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 ields in the rec
1d3d6 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e ord */. int len
1d3d7 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 ; /* T
1d3d8 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 he length of the
1d3d9 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 serialized data
1d3da 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 for the column
1d3db 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
1d3dc 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
1d3dd 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 counter */. cha
1d3de 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 r *zData;
1d3df 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65 20 72 /* Part of the r
1d3e0 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f ecord being deco
1d3e1 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 ded */. Mem *pD
1d3e2 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 est; /* W
1d3e3 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 here to write th
1d3e4 65 20 65 78 74 72 61 63 74 65 64 20 76 61 6c 75 e extracted valu
1d3e5 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b e */. Mem sMem;
1d3e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 /* For
1d3e7 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65 63 storing the rec
1d3e8 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 ord being decode
1d3e9 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b d */. u8 *zIdx;
1d3ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 /* Ind
1d3eb 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 2a ex into header *
1d3ec 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b /. u8 *zEndHdr;
1d3ed 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 /* Pointe
1d3ee 72 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 r to first byte
1d3ef 61 66 74 65 72 20 74 68 65 20 68 65 61 64 65 72 after the header
1d3f0 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 */. u32 offset
1d3f1 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 ; /* Offs
1d3f2 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 et into the data
1d3f3 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73 65 74 */. u64 offset
1d3f4 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 64; /* 64-b
1d3f5 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20 62 it offset. 64 b
1d3f6 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63 61 its needed to ca
1d3f7 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a tch overflow */.
1d3f8 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 int szHdr;
1d3f9 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 /* Size of
1d3fa 74 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 the header size
1d3fb 66 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f field at start o
1d3fc 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e f record */. in
1d3fd 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 20 t avail;
1d3fe 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 /* Number of by
1d3ff 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 tes of available
1d400 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a data */. Mem *
1d401 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a pReg; /*
1d402 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 PseudoTable inp
1d403 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 ut register */.#
1d404 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1d405 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1d406 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20 nto u.am */...
1d407 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 u.am.p1 = pOp->p
1d408 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70 1;. u.am.p2 = p
1d409 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70 Op->p2;. u.am.p
1d40a 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 C = 0;. memset(
1d40b 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73 &u.am.sMem, 0, s
1d40c 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29 izeof(u.am.sMem)
1d40d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 );. assert( u.a
1d40e 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 m.p1<p->nCursor
1d40f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1d410 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p3>0 && pOp->p
1d411 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 3<=p->nMem );.
1d412 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26 61 4d u.am.pDest = &aM
1d413 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d em[pOp->p3];. M
1d414 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 75 2e emSetTypeFlag(u.
1d415 61 6d 2e 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 am.pDest, MEM_Nu
1d416 6c 6c 29 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 ll);. u.am.zRec
1d417 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 = 0;.. /* This
1d418 20 62 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 block sets the
1d419 76 61 72 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 variable u.am.pa
1d41a 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62 65 20 yloadSize to be
1d41b 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 the total number
1d41c 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 of. ** bytes i
1d41d 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 n the record..
1d41e 2a 2a 0a 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 **. ** u.am.zRe
1d41f 63 20 69 73 20 73 65 74 20 74 6f 20 62 65 20 74 c is set to be t
1d420 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 he complete text
1d421 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 of the record i
1d422 66 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c f it is availabl
1d423 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 e.. ** The comp
1d424 6c 65 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 lete record text
1d425 20 69 73 20 61 6c 77 61 79 73 20 61 76 61 69 6c is always avail
1d426 61 62 6c 65 20 66 6f 72 20 70 73 65 75 64 6f 2d able for pseudo-
1d427 74 61 62 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 tables. ** If t
1d428 68 65 20 72 65 63 6f 72 64 20 69 73 20 73 74 6f he record is sto
1d429 72 65 64 20 69 6e 20 61 20 63 75 72 73 6f 72 2c red in a cursor,
1d42a 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 the complete re
1d42b 63 6f 72 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d cord text. ** m
1d42c 69 67 68 74 20 62 65 20 61 76 61 69 6c 61 62 6c ight be availabl
1d42d 65 20 69 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 e in the u.am.p
1d42e 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1d42f 4f 72 20 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 Or it might not
1d430 62 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 be.. ** If the
1d431 64 61 74 61 20 69 73 20 75 6e 61 76 61 69 6c 61 data is unavaila
1d432 62 6c 65 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 ble, u.am.zRec
1d433 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a is set to NULL..
1d434 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 **. ** We als
1d435 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 o compute the nu
1d436 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
1d437 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 20 20 in the record.
1d438 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a For cursors,. *
1d439 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 * the number of
1d43a 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 columns is store
1d43b 64 20 69 6e 20 74 68 65 20 56 64 62 65 43 75 72 d in the VdbeCur
1d43c 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 sor.nField eleme
1d43d 6e 74 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e nt.. */. u.am.
1d43e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e pC = p->apCsr[u.
1d43f 61 6d 2e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 am.p1];. assert
1d440 28 20 75 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a ( u.am.pC!=0 );.
1d441 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
1d442 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1d443 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e . assert( u.am.
1d444 70 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d pC->pVtabCursor=
1d445 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 =0 );.#endif. u
1d446 2e 61 6d 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d .am.pCrsr = u.am
1d447 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 .pC->pCursor;.
1d448 69 66 28 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d if( u.am.pCrsr!=
1d449 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 0 ){. /* The
1d44a 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 record is stored
1d44b 20 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a in a B-Tree */.
1d44c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1d44d 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f VdbeCursorMoveto
1d44e 28 75 2e 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 (u.am.pC);. i
1d44f 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f f( rc ) goto abo
1d450 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b rt_due_to_error;
1d451 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 . if( u.am.pC
1d452 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 ->nullRow ){.
1d453 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 u.am.payloadS
1d454 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c ize = 0;. }el
1d455 73 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e se if( u.am.pC->
1d456 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e cacheStatus==p->
1d457 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 cacheCtr ){.
1d458 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 u.am.payloadSi
1d459 7a 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 ze = u.am.pC->pa
1d45a 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 yloadSize;.
1d45b 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 u.am.zRec = (ch
1d45c 61 72 2a 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f ar*)u.am.pC->aRo
1d45d 77 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 w;. }else if(
1d45e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d45f 78 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 x ){. asser
1d460 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 t( sqlite3BtreeC
1d461 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 ursorIsValid(u.a
1d462 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20 20 20 m.pCrsr) );.
1d463 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1d464 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e reeKeySize(u.am.
1d465 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 pCrsr, &u.am.pay
1d466 6c 6f 61 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 loadSize64);.
1d467 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 assert( rc==S
1d468 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a QLITE_OK ); /*
1d469 20 54 72 75 65 20 62 65 63 61 75 73 65 20 6f 66 True because of
1d46a 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 CursorMoveto()
1d46b 63 61 6c 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 call above */.
1d46c 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 /* sqlite3Bt
1d46d 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 reeParseCellPtr(
1d46e 29 20 75 73 65 73 20 67 65 74 56 61 72 69 6e 74 ) uses getVarint
1d46f 33 32 28 29 20 74 6f 20 65 78 74 72 61 63 74 20 32() to extract
1d470 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 the. ** pay
1d471 6c 6f 61 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 load size, so it
1d472 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 is impossible f
1d473 6f 72 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 or u.am.payloadS
1d474 69 7a 65 36 34 20 74 6f 20 62 65 0a 20 20 20 20 ize64 to be.
1d475 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e ** larger than
1d476 20 33 32 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 32 bits. */.
1d477 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 61 6d assert( (u.am
1d478 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 .payloadSize64 &
1d479 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 SQLITE_MAX_U32)
1d47a 3d 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c ==(u64)u.am.payl
1d47b 6f 61 64 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 oadSize64 );.
1d47c 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 u.am.payloadS
1d47d 69 7a 65 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e ize = (u32)u.am.
1d47e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 payloadSize64;.
1d47f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1d480 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1d481 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 treeCursorIsVali
1d482 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b d(u.am.pCrsr) );
1d483 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1d484 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65 te3BtreeDataSize
1d485 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.am.pCrsr, &u.
1d486 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b am.payloadSize);
1d487 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 . assert( r
1d488 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 c==SQLITE_OK );
1d489 20 20 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 /* DataSize()
1d48a 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 cannot fail */.
1d48b 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
1d48c 20 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f u.am.pC->pseudo
1d48d 54 61 62 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 TableReg>0 ){.
1d48e 20 20 75 2e 61 6d 2e 70 52 65 67 20 3d 20 26 61 u.am.pReg = &a
1d48f 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 Mem[u.am.pC->pse
1d490 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20 udoTableReg];.
1d491 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 assert( u.am.p
1d492 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d Reg->flags & MEM
1d493 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61 _Blob );. u.a
1d494 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 m.payloadSize =
1d495 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20 u.am.pReg->n;.
1d496 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e u.am.zRec = u.
1d497 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20 am.pReg->z;.
1d498 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d499 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26 atus = (pOp->p5&
1d49a 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 OPFLAG_CLEARCACH
1d49b 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45 E) ? CACHE_STALE
1d49c 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a : p->cacheCtr;.
1d49d 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d49e 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 .payloadSize==0
1d49f 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 || u.am.zRec!=0
1d4a0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1d4a1 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 /* Consider the
1d4a2 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a row to be NULL *
1d4a3 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f /. u.am.paylo
1d4a4 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a adSize = 0;. }.
1d4a5 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61 . /* If u.am.pa
1d4a6 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 yloadSize is 0,
1d4a7 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 then just store
1d4a8 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 a NULL */. if(
1d4a9 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d4aa 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ==0 ){. asser
1d4ab 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 t( u.am.pDest->f
1d4ac 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b lags&MEM_Null );
1d4ad 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c . goto op_col
1d4ae 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 umn_out;. }. a
1d4af 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69 ssert( db->aLimi
1d4b0 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c t[SQLITE_LIMIT_L
1d4b1 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69 ENGTH]>=0 );. i
1d4b2 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 f( u.am.payloadS
1d4b3 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61 ize > (u32)db->a
1d4b4 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d Limit[SQLITE_LIM
1d4b5 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 IT_LENGTH] ){.
1d4b6 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a goto too_big;.
1d4b7 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65 }.. u.am.nFie
1d4b8 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46 ld = u.am.pC->nF
1d4b9 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20 ield;. assert(
1d4ba 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69 u.am.p2<u.am.nFi
1d4bb 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 eld );.. /* Rea
1d4bc 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20 d and parse the
1d4bd 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53 table header. S
1d4be 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 tore the results
1d4bf 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20 of the parse.
1d4c0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f ** into the reco
1d4c1 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20 rd header cache
1d4c2 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 fields of the cu
1d4c3 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 rsor.. */. u.a
1d4c4 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70 m.aType = u.am.p
1d4c5 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20 C->aType;. if(
1d4c6 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d4c7 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 atus==p->cacheCt
1d4c8 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f r ){. u.am.aO
1d4c9 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d ffset = u.am.pC-
1d4ca 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 >aOffset;. }els
1d4cb 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e e{. assert(u.
1d4cc 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75 am.aType);. u
1d4cd 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20 .am.avail = 0;.
1d4ce 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 u.am.pC->aOff
1d4cf 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73 set = u.am.aOffs
1d4d0 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65 et = &u.am.aType
1d4d1 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20 [u.am.nField];.
1d4d2 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c u.am.pC->payl
1d4d3 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 oadSize = u.am.p
1d4d4 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 ayloadSize;.
1d4d5 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 u.am.pC->cacheSt
1d4d6 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 atus = p->cacheC
1d4d7 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 tr;.. /* Figu
1d4d8 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 re out how many
1d4d9 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65 bytes are in the
1d4da 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 header */. i
1d4db 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d4dc 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 u.am.zData
1d4dd 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20 = u.am.zRec;.
1d4de 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
1d4df 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e f( u.am.pC->isIn
1d4e0 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75 dex ){. u
1d4e1 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 .am.zData = (cha
1d4e2 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b r*)sqlite3BtreeK
1d4e3 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72 eyFetch(u.am.pCr
1d4e4 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29 sr, &u.am.avail)
1d4e5 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
1d4e6 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 u.am.zDat
1d4e7 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 a = (char*)sqlit
1d4e8 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68 e3BtreeDataFetch
1d4e9 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e (u.am.pCrsr, &u.
1d4ea 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 am.avail);.
1d4eb 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b }. /* If K
1d4ec 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65 eyFetch()/DataFe
1d4ed 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f tch() managed to
1d4ee 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20 get the entire
1d4ef 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a payload,. *
1d4f0 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f * save the paylo
1d4f1 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70 ad in the u.am.p
1d4f2 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20 C->aRow cache.
1d4f3 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75 That will save u
1d4f4 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 s from. **
1d4f5 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 having to make a
1d4f6 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 dditional calls
1d4f7 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e to fetch the con
1d4f8 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a tent portion of.
1d4f9 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63 ** the rec
1d4fa 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 ord.. */.
1d4fb 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d assert( u.am
1d4fc 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20 .avail>=0 );.
1d4fd 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c if( u.am.payl
1d4fe 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 oadSize <= (u32)
1d4ff 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20 u.am.avail ){.
1d500 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 u.am.zRec
1d501 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 = u.am.zData;.
1d502 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 u.am.pC->a
1d503 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e Row = (u8*)u.am.
1d504 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c zData;. }el
1d505 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d se{. u.am
1d506 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20 .pC->aRow = 0;.
1d507 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1d508 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e /* The followin
1d509 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65 g assert is true
1d50a 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63 in all cases ac
1d50b 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a cept when. **
1d50c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1d50d 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72 le has been corr
1d50e 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79 upted externally
1d50f 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65 .. ** asse
1d510 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 rt( u.am.zRec!=0
1d511 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d512 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 u.am.payloadSize
1d513 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d || u.am.avail>=
1d514 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 9 ); */. u.am
1d515 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69 .szHdr = getVari
1d516 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a nt32((u8*)u.am.z
1d517 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65 Data, u.am.offse
1d518 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 t);.. /* Make
1d519 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20 sure a corrupt
1d51a 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 database has not
1d51b 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65 given us an ove
1d51c 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20 rsize header..
1d51d 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77 ** Do this now
1d51e 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65 to avoid an ove
1d51f 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c rsize memory all
1d520 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a ocation.. **.
1d521 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 ** Type entr
1d522 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65 ies can be betwe
1d523 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73 en 1 and 5 bytes
1d524 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e each. But 4 an
1d525 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 d 5 byte. **
1d526 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63 types use so muc
1d527 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61 h data space tha
1d528 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 t there can only
1d529 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20 be 4096 and 32
1d52a 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 of. ** them,
1d52b 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 respectively. S
1d52c 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 o the maximum he
1d52d 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75 ader length resu
1d52e 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a lts from a. *
1d52f 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f * 3-byte type fo
1d530 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61 r each of the ma
1d531 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63 ximum of 32768 c
1d532 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65 olumns plus thre
1d533 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62 e. ** extra b
1d534 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61 ytes for the hea
1d535 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c der length itsel
1d536 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20 f. 32768*3 + 3
1d537 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a = 98307.. */.
1d538 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66 if( u.am.off
1d539 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20 set > 98307 ){.
1d53a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
1d53b 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 _CORRUPT_BKPT;.
1d53c 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c goto op_col
1d53d 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a umn_out;. }..
1d53e 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69 /* Compute i
1d53f 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e n u.am.len the n
1d540 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f umber of bytes o
1d541 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74 f data we need t
1d542 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a o read in order.
1d543 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e ** to get u.
1d544 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76 am.nField type v
1d545 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66 alues. u.am.off
1d546 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20 set is an upper
1d547 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20 bound on this.
1d548 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e But. ** u.am.
1d549 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20 nField might be
1d54a 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65 significantly le
1d54b 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 ss than the true
1d54c 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
1d54d 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 ns. ** in the
1d54e 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74 table, and in t
1d54f 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d hat case, 5*u.am
1d550 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20 .nField+3 might
1d551 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 be smaller than
1d552 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20 u.am.offset..
1d553 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d ** We want to m
1d554 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e inimize u.am.len
1d555 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d in order to lim
1d556 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 it the size of t
1d557 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a he memory. **
1d558 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 allocation, esp
1d559 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 ecially if a cor
1d55a 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69 rupt database fi
1d55b 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e le has caused u.
1d55c 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a am.offset. **
1d55d 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64 to be oversized
1d55e 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69 . Offset is limi
1d55f 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f ted to 98307 abo
1d560 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d ve. But 98307 m
1d561 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c ight. ** stil
1d562 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 l exceed Robson
1d563 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f memory allocatio
1d564 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 n limits on some
1d565 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e configurations.
1d566 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65 . ** On syste
1d567 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74 ms that cannot t
1d568 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65 olerate large me
1d569 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 mory allocations
1d56a 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b , u.am.nField*5+
1d56b 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69 3. ** will li
1d56c 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61 kely be much sma
1d56d 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e ller since u.am.
1d56e 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65 nField will like
1d56f 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a ly be less than.
1d570 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e ** 20 or so.
1d571 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74 This insures t
1d572 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 hat Robson memor
1d573 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d y allocation lim
1d574 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e its are. ** n
1d575 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e ot exceeded even
1d576 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74 for corrupt dat
1d577 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20 abase files..
1d578 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e */. u.am.len
1d579 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 = u.am.nField*5
1d57a 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e + 3;. if( u.
1d57b 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e am.len > (int)u.
1d57c 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d am.offset ) u.am
1d57d 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d .len = (int)u.am
1d57e 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a .offset;.. /*
1d57f 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20 The KeyFetch()
1d580 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61 or DataFetch() a
1d581 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e bove are fast an
1d582 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65 d will get the e
1d583 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 ntire. ** rec
1d584 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f ord header in mo
1d585 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74 st cases. But t
1d586 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f hey will fail to
1d587 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 get the complet
1d588 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 e. ** record
1d589 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65 header if the re
1d58a 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73 cord header does
1d58b 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69 not fit on a si
1d58c 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a ngle page. **
1d58d 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20 in the B-Tree.
1d58e 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65 When that happe
1d58f 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56 ns, use sqlite3V
1d590 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 dbeMemFromBtree(
1d591 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 ) to. ** acqu
1d592 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 ire the complete
1d593 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20 header text..
1d594 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e */. if( !u.
1d595 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e am.zRec && u.am.
1d596 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29 avail<u.am.len )
1d597 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 {. u.am.sMe
1d598 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 m.flags = 0;.
1d599 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20 u.am.sMem.db
1d59a 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 = 0;. rc =
1d59b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 sqlite3VdbeMemFr
1d59c 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 omBtree(u.am.pCr
1d59d 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c sr, 0, u.am.len,
1d59e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 u.am.pC->isInde
1d59f 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a x, &u.am.sMem);.
1d5a0 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 if( rc!=SQ
1d5a1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1d5a2 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d goto op_colum
1d5a3 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 n_out;. }.
1d5a4 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 u.am.zData
1d5a5 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 = u.am.sMem.z;.
1d5a6 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45 }. u.am.zE
1d5a7 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75 ndHdr = (u8 *)&u
1d5a8 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c .am.zData[u.am.l
1d5a9 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49 en];. u.am.zI
1d5aa 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d dx = (u8 *)&u.am
1d5ab 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64 .zData[u.am.szHd
1d5ac 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e r];.. /* Scan
1d5ad 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 the header and
1d5ae 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 use it to fill i
1d5af 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65 n the u.am.aType
1d5b0 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 [] and u.am.aOff
1d5b1 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 set[]. ** arr
1d5b2 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65 ays. u.am.aType
1d5b3 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f [u.am.i] will co
1d5b4 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69 ntain the type i
1d5b5 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75 nteger for the u
1d5b6 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20 .am.i-th. **
1d5b7 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e column and u.am.
1d5b8 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 aOffset[u.am.i]
1d5b9 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 will contain the
1d5ba 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f u.am.offset fro
1d5bb 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a m the beginning.
1d5bc 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 ** of the re
1d5bd 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72 cord to the star
1d5be 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f t of the data fo
1d5bf 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20 r the u.am.i-th
1d5c0 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 column. */.
1d5c1 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 u.am.offset64
1d5c2 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20 = u.am.offset;.
1d5c3 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b for(u.am.i=0;
1d5c4 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69 u.am.i<u.am.nFi
1d5c5 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a eld; u.am.i++){.
1d5c6 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a if( u.am.z
1d5c7 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 Idx<u.am.zEndHdr
1d5c8 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d ){. u.am
1d5c9 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d .aOffset[u.am.i]
1d5ca 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66 = (u32)u.am.off
1d5cb 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75 set64;. u
1d5cc 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56 .am.zIdx += getV
1d5cd 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64 arint32(u.am.zId
1d5ce 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e x, u.am.aType[u.
1d5cf 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 am.i]);.
1d5d0 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d u.am.offset64 +=
1d5d1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 sqlite3VdbeSeri
1d5d2 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 alTypeLen(u.am.a
1d5d3 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 Type[u.am.i]);.
1d5d4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1d5d5 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69 /* If u.am.i
1d5d6 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e is less that u.
1d5d7 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 am.nField, then
1d5d8 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66 there are less f
1d5d9 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20 ields in this.
1d5da 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 ** record
1d5db 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d than SetNumColum
1d5dc 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65 ns indicated the
1d5dd 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 re are columns i
1d5de 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a n the. **
1d5df 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20 table. Set the
1d5e0 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20 u.am.offset for
1d5e1 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e any extra column
1d5e2 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e s not present in
1d5e3 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 . ** the
1d5e4 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 record to 0. Thi
1d5e5 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c s tells code bel
1d5e6 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 ow to store a NU
1d5e7 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e LL. ** in
1d5e8 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61 stead of deseria
1d5e9 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66 lizing a value f
1d5ea 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a rom the record..
1d5eb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
1d5ec 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b u.am.aOffset[
1d5ed 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20 u.am.i] = 0;.
1d5ee 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 }. }. s
1d5ef 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c qlite3VdbeMemRel
1d5f0 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29 ease(&u.am.sMem)
1d5f1 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e ;. u.am.sMem.
1d5f2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c flags = MEM_Null
1d5f3 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 ;.. /* If we
1d5f4 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 have read more h
1d5f5 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 eader data than
1d5f6 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e was contained in
1d5f7 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 the header,.
1d5f8 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e ** or if the en
1d5f9 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 d of the last fi
1d5fa 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 eld appears to b
1d5fb 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f e past the end o
1d5fc 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 f the. ** rec
1d5fd 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 ord, or if the e
1d5fe 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 nd of the last f
1d5ff 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 ield appears to
1d600 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e be before the en
1d601 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 d. ** of the
1d602 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c record (when all
1d603 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 fields present)
1d604 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 , then we must b
1d605 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a e dealing. **
1d606 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 with a corrupt
1d607 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f database.. */
1d608 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a . if( (u.am.z
1d609 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48 Idx > u.am.zEndH
1d60a 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73 dr)|| (u.am.offs
1d60b 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c et64 > u.am.payl
1d60c 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c oadSize). ||
1d60d 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61 (u.am.zIdx==u.a
1d60e 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61 m.zEndHdr && u.a
1d60f 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34 m.offset64!=(u64
1d610 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a )u.am.payloadSiz
1d611 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d e) ){. rc =
1d612 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f SQLITE_CORRUPT_
1d613 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f BKPT;. goto
1d614 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a op_column_out;.
1d615 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
1d616 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 Get the column i
1d617 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75 nformation. If u
1d618 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d .am.aOffset[u.am
1d619 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f .p2] is non-zero
1d61a 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65 , then. ** dese
1d61b 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 rialize the valu
1d61c 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 e from the recor
1d61d 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73 d. If u.am.aOffs
1d61e 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a et[u.am.p2] is z
1d61f 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 ero,. ** then t
1d620 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f here are not eno
1d621 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68 ugh fields in th
1d622 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69 e record to sati
1d623 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 sfy the. ** req
1d624 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63 uest. In this c
1d625 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c ase, set the val
1d626 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34 ue NULL or to P4
1d627 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61 if P4 is. ** a
1d628 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 pointer to a Me
1d629 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20 m object.. */.
1d62a 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65 if( u.am.aOffse
1d62b 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20 t[u.am.p2] ){.
1d62c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 assert( rc==SQ
1d62d 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 LITE_OK );. i
1d62e 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a f( u.am.zRec ){.
1d62f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
1d630 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 eMemReleaseExter
1d631 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b nal(u.am.pDest);
1d632 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
1d633 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20 beSerialGet((u8
1d634 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61 *)&u.am.zRec[u.a
1d635 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 m.aOffset[u.am.p
1d636 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 2]], u.am.aType[
1d637 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 u.am.p2], u.am.p
1d638 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 Dest);. }else
1d639 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e {. u.am.len
1d63a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 = sqlite3VdbeSe
1d63b 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d rialTypeLen(u.am
1d63c 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29 .aType[u.am.p2])
1d63d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d63e 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d dbeMemMove(&u.am
1d63f 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73 .sMem, u.am.pDes
1d640 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 t);. rc = s
1d641 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f qlite3VdbeMemFro
1d642 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 mBtree(u.am.pCrs
1d643 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b r, u.am.aOffset[
1d644 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c u.am.p2], u.am.l
1d645 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 en, u.am.pC->isI
1d646 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d ndex, &u.am.sMem
1d647 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 );. if( rc!
1d648 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1d649 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f goto op_co
1d64a 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 lumn_out;.
1d64b 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 }. u.am.zDa
1d64c 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a ta = u.am.sMem.z
1d64d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
1d64e 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 dbeSerialGet((u8
1d64f 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e *)u.am.zData, u.
1d650 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 am.aType[u.am.p2
1d651 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a ], u.am.pDest);.
1d652 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70 }. u.am.p
1d653 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f Dest->enc = enco
1d654 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 ding;. }else{.
1d655 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 if( pOp->p4ty
1d656 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 pe==P4_MEM ){.
1d657 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1d658 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e emShallowCopy(u.
1d659 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 am.pDest, pOp->p
1d65a 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 4.pMem, MEM_Stat
1d65b 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ic);. }else{.
1d65c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1d65d 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 am.pDest->flags&
1d65e 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 MEM_Null );.
1d65f 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 }. }.. /* If w
1d660 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c e dynamically al
1d661 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f located space to
1d662 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28 hold the data (
1d663 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 in the. ** sqli
1d664 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 te3VdbeMemFromBt
1d665 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 ree() call above
1d666 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 ) then transfer
1d667 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a control of that.
1d668 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 ** dynamically
1d669 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 allocated space
1d66a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61 over to the u.a
1d66b 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72 m.pDest structur
1d66c 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65 e.. ** This pre
1d66d 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 vents a memory c
1d66e 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 opy.. */. if(
1d66f 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f u.am.sMem.zMallo
1d670 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 c ){. assert(
1d671 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e u.am.sMem.z==u.
1d672 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 am.sMem.zMalloc
1d673 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 );. assert( !
1d674 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 (u.am.pDest->fla
1d675 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b gs & MEM_Dyn) );
1d676 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75 . assert( !(u
1d677 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 .am.pDest->flags
1d678 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d & (MEM_Blob|MEM
1d679 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70 _Str)) || u.am.p
1d67a 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d Dest->z==u.am.sM
1d67b 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d em.z );. u.am
1d67c 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d .pDest->flags &=
1d67d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d ~(MEM_Ephem|MEM
1d67e 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e _Static);. u.
1d67f 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 am.pDest->flags
1d680 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 |= MEM_Term;.
1d681 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d u.am.pDest->z =
1d682 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 u.am.sMem.z;.
1d683 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d u.am.pDest->zM
1d684 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65 alloc = u.am.sMe
1d685 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a m.zMalloc;. }..
1d686 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1d687 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 beMemMakeWriteab
1d688 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a le(u.am.pDest);.
1d689 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a .op_column_out:.
1d68a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f UPDATE_MAX_BLO
1d68b 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74 BSIZE(u.am.pDest
1d68c 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 );. REGISTER_TR
1d68d 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61 ACE(pOp->p3, u.a
1d68e 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61 m.pDest);. brea
1d68f 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1d690 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20 Affinity P1 P2
1d691 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 * P4 *.**.** App
1d692 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f ly affinities to
1d693 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72 a range of P2 r
1d694 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e egisters startin
1d695 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a g with P1..**.**
1d696 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 P4 is a string
1d697 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61 that is P2 chara
1d698 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 cters long. The
1d699 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66 nth character of
1d69a 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 the.** string i
1d69b 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c ndicates the col
1d69c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61 umn affinity tha
1d69d 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 t should be used
1d69e 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 for the nth.**
1d69f 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 memory cell in t
1d6a0 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 he range..*/.cas
1d6a1 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b e OP_Affinity: {
1d6a2 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1d6a3 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1d6a4 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 into u.an */.
1d6a5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66 const char *zAff
1d6a6 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 inity; /* The
1d6a7 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61 affinity to be a
1d6a8 70 70 6c 69 65 64 20 2a 2f 0a 20 20 63 68 61 72 pplied */. char
1d6a9 20 63 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 cAff;
1d6aa 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 /* A single
1d6ab 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 61 66 character of af
1d6ac 66 69 6e 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 finity */.#endif
1d6ad 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d6ae 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d6af 2e 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a .an */.. u.an.z
1d6b0 41 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e Affinity = pOp->
1d6b1 70 34 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 p4.z;. assert(
1d6b2 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 21 3d u.an.zAffinity!=
1d6b3 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 0 );. assert( u
1d6b4 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 5b 70 4f .an.zAffinity[pO
1d6b5 70 2d 3e 70 32 5d 3d 3d 30 20 29 3b 0a 20 20 70 p->p2]==0 );. p
1d6b6 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In1 = &aMem[pOp-
1d6b7 3e 70 31 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 >p1];. while( (
1d6b8 75 2e 61 6e 2e 63 41 66 66 20 3d 20 2a 28 75 2e u.an.cAff = *(u.
1d6b9 61 6e 2e 7a 41 66 66 69 6e 69 74 79 2b 2b 29 29 an.zAffinity++))
1d6ba 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 !=0 ){. asser
1d6bb 74 28 20 70 49 6e 31 20 3c 3d 20 26 70 2d 3e 61 t( pIn1 <= &p->a
1d6bc 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a Mem[p->nMem] );.
1d6bd 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 ExpandBlob(p
1d6be 49 6e 31 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 In1);. applyA
1d6bf 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e ffinity(pIn1, u.
1d6c0 61 6e 2e 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e an.cAff, encodin
1d6c1 67 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a g);. pIn1++;.
1d6c2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a }. break;.}..
1d6c3 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52 /* Opcode: MakeR
1d6c4 65 63 6f 72 64 20 50 31 20 50 32 20 50 33 20 50 ecord P1 P2 P3 P
1d6c5 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 4 *.**.** Conver
1d6c6 74 20 50 32 20 72 65 67 69 73 74 65 72 73 20 62 t P2 registers b
1d6c7 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 31 eginning with P1
1d6c8 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 65 into a single e
1d6c9 6e 74 72 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65 ntry.** suitable
1d6ca 20 66 6f 72 20 75 73 65 20 61 73 20 61 20 64 61 for use as a da
1d6cb 74 61 20 72 65 63 6f 72 64 20 69 6e 20 61 20 64 ta record in a d
1d6cc 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 atabase table or
1d6cd 20 61 73 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 as a key.** in
1d6ce 61 6e 20 69 6e 64 65 78 2e 20 20 54 68 65 20 64 an index. The d
1d6cf 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 66 6f etails of the fo
1d6d0 72 6d 61 74 20 61 72 65 20 69 72 72 65 6c 65 76 rmat are irrelev
1d6d1 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a ant as long as.*
1d6d2 2a 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 * the OP_Column
1d6d3 6f 70 63 6f 64 65 20 63 61 6e 20 64 65 63 6f 64 opcode can decod
1d6d4 65 20 74 68 65 20 72 65 63 6f 72 64 20 6c 61 74 e the record lat
1d6d5 65 72 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 er..** Refer to
1d6d6 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d source code comm
1d6d7 65 6e 74 73 20 66 6f 72 20 74 68 65 20 64 65 74 ents for the det
1d6d8 61 69 6c 73 20 6f 66 20 74 68 65 20 72 65 63 6f ails of the reco
1d6d9 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a rd.** format..**
1d6da 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 .** P4 may be a
1d6db 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 50 string that is P
1d6dc 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 2 characters lon
1d6dd 67 2e 20 20 54 68 65 20 6e 74 68 20 63 68 61 72 g. The nth char
1d6de 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 acter of the.**
1d6df 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73 string indicates
1d6e0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 the column affi
1d6e1 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 nity that should
1d6e2 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 be used for the
1d6e3 20 6e 74 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 nth.** field of
1d6e4 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a the index key..
1d6e5 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e **.** The mappin
1d6e6 67 20 66 72 6f 6d 20 63 68 61 72 61 63 74 65 72 g from character
1d6e7 20 74 6f 20 61 66 66 69 6e 69 74 79 20 69 73 20 to affinity is
1d6e8 67 69 76 65 6e 20 62 79 20 74 68 65 20 53 51 4c given by the SQL
1d6e9 49 54 45 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 ITE_AFF_.** macr
1d6ea 6f 73 20 64 65 66 69 6e 65 64 20 69 6e 20 73 71 os defined in sq
1d6eb 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a liteInt.h..**.**
1d6ec 20 49 66 20 50 34 20 69 73 20 4e 55 4c 4c 20 74 If P4 is NULL t
1d6ed 68 65 6e 20 61 6c 6c 20 69 6e 64 65 78 20 66 69 hen all index fi
1d6ee 65 6c 64 73 20 68 61 76 65 20 74 68 65 20 61 66 elds have the af
1d6ef 66 69 6e 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a finity NONE..*/.
1d6f0 63 61 73 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f case OP_MakeReco
1d6f1 72 64 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 rd: {.#if 0 /*
1d6f2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1d6f3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 moved into u.ao
1d6f4 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 */. u8 *zNewRec
1d6f5 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ord; /* A
1d6f6 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 buffer to hold
1d6f7 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 the data for the
1d6f8 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 new record */.
1d6f9 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 Mem *pRec;
1d6fa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e /* The n
1d6fb 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 ew record */. u
1d6fc 36 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 64 nData;
1d6fd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1d6fe 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 of bytes of data
1d6ff 20 73 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 space */. int
1d700 6e 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 nHdr;
1d701 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
1d702 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72 20 bytes of header
1d703 73 70 61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e space */. i64 n
1d704 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 Byte;
1d705 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 /* Data space
1d706 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 required for thi
1d707 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e s record */. in
1d708 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 t nZero;
1d709 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1d70a 66 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 f zero bytes at
1d70b 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 the end of the r
1d70c 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e ecord */. int n
1d70d 56 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 Varint;
1d70e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d70f 79 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 ytes in a varint
1d710 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61 6c */. u32 serial
1d711 5f 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 _type; /*
1d712 54 79 70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 Type field */.
1d713 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 Mem *pData0;
1d714 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 /* First
1d715 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 field to be comb
1d716 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 ined into the re
1d717 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 cord */. Mem *p
1d718 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 Last;
1d719 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c 64 20 6f /* Last field o
1d71a 66 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a f the record */.
1d71b 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 int nField;
1d71c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1d71d 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 er of fields in
1d71e 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 the record */.
1d71f 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b char *zAffinity;
1d720 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 /* The af
1d721 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f finity string fo
1d722 72 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a r the record */.
1d723 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 int file_forma
1d724 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 t; /* File
1d725 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 format to use f
1d726 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 or encoding */.
1d727 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 int i;
1d728 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 /* Space
1d729 20 75 73 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 used in zNewRec
1d72a 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c ord[] */. int l
1d72b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 en;
1d72c 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 /* Length of a
1d72d 20 66 69 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 field */.#endif
1d72e 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1d72f 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1d730 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 .ao */.. /* Ass
1d731 75 6d 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 uming the record
1d732 20 63 6f 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c contains N fiel
1d733 64 73 2c 20 74 68 65 20 72 65 63 6f 72 64 20 66 ds, the record f
1d734 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a ormat looks. **
1d735 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a like this:. **
1d736 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d . ** ----------
1d737 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d738 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d739 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d73a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 --------------.
1d73b 20 2a 2a 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c ** | hdr-size |
1d73c 20 74 79 70 65 20 30 20 7c 20 74 79 70 65 20 31 type 0 | type 1
1d73d 20 7c 20 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d | ... | type N-
1d73e 31 20 7c 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 1 | data0 | ...
1d73f 7c 20 64 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a | data N-1 |. *
1d740 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d * --------------
1d741 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d742 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d743 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
1d744 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a ----------. **.
1d745 20 20 2a 2a 20 44 61 74 61 28 30 29 20 69 73 20 ** Data(0) is
1d746 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 taken from regis
1d747 74 65 72 20 50 31 2e 20 20 44 61 74 61 28 31 29 ter P1. Data(1)
1d748 20 63 6f 6d 65 73 20 66 72 6f 6d 20 72 65 67 69 comes from regi
1d749 73 74 65 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61 ster P1+1. ** a
1d74a 6e 64 20 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a nd so froth.. *
1d74b 2a 0a 20 20 2a 2a 20 45 61 63 68 20 74 79 70 65 *. ** Each type
1d74c 20 66 69 65 6c 64 20 69 73 20 61 20 76 61 72 69 field is a vari
1d74d 6e 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 nt representing
1d74e 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 the serial type
1d74f 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 of the. ** corr
1d750 65 73 70 6f 6e 64 69 6e 67 20 64 61 74 61 20 65 esponding data e
1d751 6c 65 6d 65 6e 74 20 28 73 65 65 20 73 71 6c 69 lement (see sqli
1d752 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 te3VdbeSerialTyp
1d753 65 28 29 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68 e()). The. ** h
1d754 64 72 2d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 dr-size field is
1d755 20 61 6c 73 6f 20 61 20 76 61 72 69 6e 74 20 77 also a varint w
1d756 68 69 63 68 20 69 73 20 74 68 65 20 6f 66 66 73 hich is the offs
1d757 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 et from the begi
1d758 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68 nning. ** of th
1d759 65 20 72 65 63 6f 72 64 20 74 6f 20 64 61 74 61 e record to data
1d75a 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 0.. */. u.ao.n
1d75b 44 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20 20 Data = 0;
1d75c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 /* Number of b
1d75d 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61 ytes of data spa
1d75e 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 ce */. u.ao.nHd
1d75f 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 r = 0;
1d760 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1d761 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 es of header spa
1d762 63 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 ce */. u.ao.nBy
1d763 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 te = 0;
1d764 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 /* Data space re
1d765 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 quired for this
1d766 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f record */. u.ao
1d767 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 .nZero = 0;
1d768 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1d769 20 7a 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 zero bytes at t
1d76a 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 he end of the re
1d76b 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e cord */. u.ao.n
1d76c 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b Field = pOp->p1;
1d76d 0a 20 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 . u.ao.zAffinit
1d76e 79 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 y = pOp->p4.z;.
1d76f 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46 assert( u.ao.nF
1d770 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ield>0 && pOp->p
1d771 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2>0 && pOp->p2+u
1d772 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e .ao.nField<=p->n
1d773 4d 65 6d 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e Mem+1 );. u.ao.
1d774 70 44 61 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75 pData0 = &aMem[u
1d775 2e 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 .ao.nField];. u
1d776 2e 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 .ao.nField = pOp
1d777 2d 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 ->p2;. u.ao.pLa
1d778 73 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 st = &u.ao.pData
1d779 30 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 0[u.ao.nField-1]
1d77a 3b 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f ;. u.ao.file_fo
1d77b 72 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 rmat = p->minWri
1d77c 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 teFileFormat;..
1d77d 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 /* Loop through
1d77e 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 the elements th
1d77f 61 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 at will make up
1d780 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 the record to fi
1d781 67 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f gure. ** out ho
1d782 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 w much space is
1d783 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 required for the
1d784 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a new record.. *
1d785 2f 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 /. for(u.ao.pRe
1d786 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 c=u.ao.pData0; u
1d787 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 .ao.pRec<=u.ao.p
1d788 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b Last; u.ao.pRec+
1d789 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f +){. if( u.ao
1d78a 2e 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 .zAffinity ){.
1d78b 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 applyAffinit
1d78c 79 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 y(u.ao.pRec, u.a
1d78d 6f 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f o.zAffinity[u.ao
1d78e 2e 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 .pRec-u.ao.pData
1d78f 30 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 0], encoding);.
1d790 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 }. if( u.a
1d791 6f 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 o.pRec->flags&ME
1d792 4d 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 M_Zero && u.ao.p
1d793 52 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 Rec->n>0 ){.
1d794 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d sqlite3VdbeMem
1d795 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e ExpandBlob(u.ao.
1d796 70 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 pRec);. }.
1d797 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 u.ao.serial_typ
1d798 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 e = sqlite3VdbeS
1d799 65 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 erialType(u.ao.p
1d79a 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 Rec, u.ao.file_f
1d79b 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f ormat);. u.ao
1d79c 2e 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 .len = sqlite3Vd
1d79d 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 beSerialTypeLen(
1d79e 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 u.ao.serial_type
1d79f 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 );. u.ao.nDat
1d7a0 61 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 a += u.ao.len;.
1d7a1 20 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 u.ao.nHdr +=
1d7a2 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e sqlite3VarintLen
1d7a3 28 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 (u.ao.serial_typ
1d7a4 65 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f e);. if( u.ao
1d7a5 2e 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d .pRec->flags & M
1d7a6 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 EM_Zero ){.
1d7a7 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 /* Only pure ze
1d7a8 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 ro-filled BLOBs
1d7a9 63 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 can be input to
1d7aa 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 this Opcode..
1d7ab 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 ** We do not
1d7ac 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 allow blobs with
1d7ad 20 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 a prefix and a
1d7ae 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c zero-filled tail
1d7af 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e . */. u.ao.
1d7b0 6e 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 nZero += u.ao.pR
1d7b1 65 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 ec->u.nZero;.
1d7b2 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e }else if( u.ao.
1d7b3 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 len ){. u.a
1d7b4 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 o.nZero = 0;.
1d7b5 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 }. }.. /* Add
1d7b6 20 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 the initial hea
1d7b7 64 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 der varint and t
1d7b8 6f 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f otal the size */
1d7b9 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 . u.ao.nHdr +=
1d7ba 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 u.ao.nVarint = s
1d7bb 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1d7bc 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 u.ao.nHdr);. if
1d7bd 28 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 ( u.ao.nVarint<s
1d7be 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 qlite3VarintLen(
1d7bf 75 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 u.ao.nHdr) ){.
1d7c0 20 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 u.ao.nHdr++;.
1d7c1 20 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 }. u.ao.nByte
1d7c2 3d 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f = u.ao.nHdr+u.ao
1d7c3 2e 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 .nData-u.ao.nZer
1d7c4 6f 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 o;. if( u.ao.nB
1d7c5 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 yte>db->aLimit[S
1d7c6 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 QLITE_LIMIT_LENG
1d7c7 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 TH] ){. goto
1d7c8 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 too_big;. }..
1d7c9 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 /* Make sure the
1d7ca 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 output register
1d7cb 20 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 has a buffer la
1d7cc 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 rge enough to st
1d7cd 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 ore. ** the new
1d7ce 20 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 record. The out
1d7cf 70 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f put register (pO
1d7d0 70 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c p->p3) is not al
1d7d1 6c 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 lowed to. ** be
1d7d2 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 one of the inpu
1d7d3 74 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 t registers (bec
1d7d4 61 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 ause the followi
1d7d5 6e 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 ng call to. **
1d7d6 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 sqlite3VdbeMemGr
1d7d7 6f 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 ow() could clobb
1d7d8 65 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 er the value bef
1d7d9 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e ore it is used).
1d7da 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 . */. assert(
1d7db 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 pOp->p3<pOp->p1
1d7dc 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d || pOp->p3>=pOp-
1d7dd 3e 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 >p1+pOp->p2 );.
1d7de 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f pOut = &aMem[pO
1d7df 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 73 71 p->p3];. if( sq
1d7e0 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 lite3VdbeMemGrow
1d7e1 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 6f (pOut, (int)u.ao
1d7e2 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 .nByte, 0) ){.
1d7e3 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 goto no_mem;.
1d7e4 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77 52 65 }. u.ao.zNewRe
1d7e5 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 cord = (u8 *)pOu
1d7e6 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 t->z;.. /* Writ
1d7e7 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a e the record */.
1d7e8 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74 56 61 u.ao.i = putVa
1d7e9 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e 65 77 rint32(u.ao.zNew
1d7ea 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e 48 64 Record, u.ao.nHd
1d7eb 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 r);. for(u.ao.p
1d7ec 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b Rec=u.ao.pData0;
1d7ed 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f u.ao.pRec<=u.ao
1d7ee 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 .pLast; u.ao.pRe
1d7ef 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 73 c++){. u.ao.s
1d7f0 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c erial_type = sql
1d7f1 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 ite3VdbeSerialTy
1d7f2 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e pe(u.ao.pRec, u.
1d7f3 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b ao.file_format);
1d7f4 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 70 . u.ao.i += p
1d7f5 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e 61 6f utVarint32(&u.ao
1d7f6 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f .zNewRecord[u.ao
1d7f7 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69 61 6c .i], u.ao.serial
1d7f8 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 _type); /*
1d7f9 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 serial type */.
1d7fa 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 }. for(u.ao.pR
1d7fb 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 ec=u.ao.pData0;
1d7fc 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e u.ao.pRec<=u.ao.
1d7fd 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 pLast; u.ao.pRec
1d7fe 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 ++){ /* serial
1d7ff 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e 61 6f data */. u.ao
1d800 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 .i += sqlite3Vdb
1d801 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e 61 6f eSerialPut(&u.ao
1d802 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f .zNewRecord[u.ao
1d803 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61 6f 2e .i], (int)(u.ao.
1d804 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c 20 75 nByte-u.ao.i), u
1d805 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e 66 69 .ao.pRec,u.ao.fi
1d806 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a le_format);. }.
1d807 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f 2e 69 assert( u.ao.i
1d808 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29 3b 0a ==u.ao.nByte );.
1d809 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1d80a 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c p3>0 && pOp->p3<
1d80b 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f =p->nMem );. pO
1d80c 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61 ut->n = (int)u.a
1d80d 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d o.nByte;. pOut-
1d80e 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f >flags = MEM_Blo
1d80f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70 b | MEM_Dyn;. p
1d810 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 Out->xDel = 0;.
1d811 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 if( u.ao.nZero
1d812 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e ){. pOut->u.n
1d813 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a 65 72 Zero = u.ao.nZer
1d814 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 o;. pOut->fla
1d815 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a gs |= MEM_Zero;.
1d816 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 }. pOut->enc
1d817 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 = SQLITE_UTF8;
1d818 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20 62 /* In case the b
1d819 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f 6e 76 lob is ever conv
1d81a 65 72 74 65 64 20 74 6f 20 74 65 78 74 20 2a 2f erted to text */
1d81b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1d81c 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 E(pOp->p3, pOut)
1d81d 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ;. UPDATE_MAX_B
1d81e 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 LOBSIZE(pOut);.
1d81f 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1d820 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31 20 50 code: Count P1 P
1d821 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 2 * * *.**.** St
1d822 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f ore the number o
1d823 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20 69 6e f entries (an in
1d824 74 65 67 65 72 20 76 61 6c 75 65 29 20 69 6e 20 teger value) in
1d825 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 the table or ind
1d826 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79 ex .** opened by
1d827 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20 72 65 cursor P1 in re
1d828 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66 gister P2.*/.#if
1d829 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1d82a 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61 73 65 _BTREECOUNT.case
1d82b 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20 OP_Count: {
1d82c 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 /* out2-pre
1d82d 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 release */.#if 0
1d82e 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 /* local varia
1d82f 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1d830 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20 6e 45 u.ap */. i64 nE
1d831 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73 6f 72 ntry;. BtCursor
1d832 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69 66 20 *pCrsr;.#endif
1d833 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1d834 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1d835 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e 70 43 ap */.. u.ap.pC
1d836 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 rsr = p->apCsr[p
1d837 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 Op->p1]->pCursor
1d838 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70 43 72 ;. if( u.ap.pCr
1d839 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 sr ){. rc = s
1d83a 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 qlite3BtreeCount
1d83b 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26 75 2e (u.ap.pCrsr, &u.
1d83c 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65 ap.nEntry);. }e
1d83d 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e 6e 45 lse{. u.ap.nE
1d83e 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 ntry = 0;. }.
1d83f 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70 pOut->u.i = u.ap
1d840 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b .nEntry;. break
1d841 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f ;.}.#endif../* O
1d842 70 63 6f 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 pcode: Savepoint
1d843 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a P1 * * P4 *.**.
1d844 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 ** Open, release
1d845 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 or rollback the
1d846 20 73 61 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 savepoint named
1d847 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 50 34 by parameter P4
1d848 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f , depending.** o
1d849 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 n the value of P
1d84a 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 1. To open a new
1d84b 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d savepoint, P1==
1d84c 30 2e 20 54 6f 20 72 65 6c 65 61 73 65 20 28 63 0. To release (c
1d84d 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 ommit) an.** exi
1d84e 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c sting savepoint,
1d84f 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f P1==1, or to ro
1d850 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69 73 74 69 llback an existi
1d851 6e 67 20 73 61 76 65 70 6f 69 6e 74 20 50 31 3d ng savepoint P1=
1d852 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 =2..*/.case OP_S
1d853 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 avepoint: {.#if
1d854 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1d855 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d856 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70 u.aq */. int p
1d857 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 1;
1d858 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
1d859 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e lue of P1 operan
1d85a 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 d */. char *zNa
1d85b 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 me;
1d85c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f /* Name o
1d85d 66 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 f savepoint */.
1d85e 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61 int nName;. Sa
1d85f 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 vepoint *pNew;.
1d860 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 Savepoint *pSav
1d861 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f epoint;. Savepo
1d862 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74 int *pTmp;. int
1d863 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69 iSavepoint;. i
1d864 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a nt ii;.#endif /*
1d865 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1d866 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 moved into u.aq
1d867 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d */.. u.aq.p1 =
1d868 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 pOp->p1;. u.aq
1d869 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 .zName = pOp->p4
1d86a 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 .z;.. /* Assert
1d86b 20 74 68 61 74 20 74 68 65 20 75 2e 61 71 2e 70 that the u.aq.p
1d86c 31 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 76 1 parameter is v
1d86d 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 alid. Also that
1d86e 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f if there is no o
1d86f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 pen. ** transac
1d870 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 tion, then there
1d871 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 cannot be any s
1d872 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a avepoints.. */.
1d873 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 assert( db->pS
1d874 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 avepoint==0 || d
1d875 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 b->autoCommit==0
1d876 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1d877 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1d878 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d _BEGIN||u.aq.p1=
1d879 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 =SAVEPOINT_RELEA
1d87a 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 SE||u.aq.p1==SAV
1d87b 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1d87c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d );. assert( db-
1d87d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 >pSavepoint || d
1d87e 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e b->isTransaction
1d87f 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a Savepoint==0 );.
1d880 20 20 61 73 73 65 72 74 28 20 63 68 65 63 6b 53 assert( checkS
1d881 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 avepointCount(db
1d882 29 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71 ) );.. if( u.aq
1d883 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 .p1==SAVEPOINT_B
1d884 45 47 49 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 EGIN ){. if(
1d885 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 db->writeVdbeCnt
1d886 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 >0 ){. /* A
1d887 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 63 new savepoint c
1d888 61 6e 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 annot be created
1d889 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 63 if there are ac
1d88a 74 69 76 65 20 77 72 69 74 65 0a 20 20 20 20 20 tive write.
1d88b 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 28 ** statements (
1d88c 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 i.e. open read/w
1d88d 72 69 74 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c rite incremental
1d88e 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a blob handles)..
1d88f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
1d890 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1d891 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1d892 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 "cannot open sa
1d893 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 vepoint - ".
1d894 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 "SQL stateme
1d895 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 nts in progress"
1d896 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 );. rc = SQ
1d897 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d LITE_BUSY;. }
1d898 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 71 else{. u.aq
1d899 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 .nName = sqlite3
1d89a 53 74 72 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e Strlen30(u.aq.zN
1d89b 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 ame);.. /*
1d89c 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 61 76 Create a new sav
1d89d 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 epoint structure
1d89e 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e . */. u.aq.
1d89f 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 pNew = sqlite3Db
1d8a0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 MallocRaw(db, si
1d8a1 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e 74 29 2b zeof(Savepoint)+
1d8a2 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20 u.aq.nName+1);.
1d8a3 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e 70 4e if( u.aq.pN
1d8a4 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e ew ){. u.
1d8a5 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d aq.pNew->zName =
1d8a6 20 28 63 68 61 72 20 2a 29 26 75 2e 61 71 2e 70 (char *)&u.aq.p
1d8a7 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 New[1];.
1d8a8 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70 4e 65 77 memcpy(u.aq.pNew
1d8a9 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e ->zName, u.aq.zN
1d8aa 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b ame, u.aq.nName+
1d8ab 31 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 1);.. /*
1d8ac 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6f If there is no o
1d8ad 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c pen transaction,
1d8ae 20 74 68 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 then mark this
1d8af 61 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20 20 as a special.
1d8b0 20 20 20 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 ** "transac
1d8b1 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e tion savepoint".
1d8b2 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 */. if(
1d8b3 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 db->autoCommit )
1d8b4 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e {. db->
1d8b5 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a autoCommit = 0;.
1d8b6 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 db->is
1d8b7 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 TransactionSavep
1d8b8 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 oint = 1;.
1d8b9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1d8ba 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e db->nSavepoin
1d8bb 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a t++;. }..
1d8bc 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 /* Link
1d8bd 74 68 65 20 6e 65 77 20 73 61 76 65 70 6f 69 6e the new savepoin
1d8be 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 t into the datab
1d8bf 61 73 65 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 ase handle's lis
1d8c0 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e t. */. u.
1d8c1 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d aq.pNew->pNext =
1d8c2 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b db->pSavepoint;
1d8c3 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 . db->pSa
1d8c4 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 vepoint = u.aq.p
1d8c5 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 New;. u.a
1d8c6 71 2e 70 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65 q.pNew->nDeferre
1d8c7 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 dCons = db->nDef
1d8c8 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 erredCons;.
1d8c9 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 }. }. }else
1d8ca 7b 0a 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 {. u.aq.iSave
1d8cb 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 point = 0;..
1d8cc 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 /* Find the name
1d8cd 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 d savepoint. If
1d8ce 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 there is no such
1d8cf 20 73 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e savepoint, then
1d8d0 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 an. ** an er
1d8d1 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 ror is returned
1d8d2 74 6f 20 74 68 65 20 75 73 65 72 2e 20 20 2a 2f to the user. */
1d8d3 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20 20 20 . for(.
1d8d4 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 u.aq.pSavepoint
1d8d5 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 = db->pSavepoint
1d8d6 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61 ;. u.aq.pSa
1d8d7 76 65 70 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 vepoint && sqlit
1d8d8 65 33 53 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 e3StrICmp(u.aq.p
1d8d9 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 Savepoint->zName
1d8da 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 , u.aq.zName);.
1d8db 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 u.aq.pSavep
1d8dc 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 oint = u.aq.pSav
1d8dd 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 epoint->pNext.
1d8de 20 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e ){. u.aq.
1d8df 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 iSavepoint++;.
1d8e0 20 20 7d 0a 20 20 20 20 69 66 28 20 21 75 2e 61 }. if( !u.a
1d8e1 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a q.pSavepoint ){.
1d8e2 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 sqlite3Set
1d8e3 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1d8e4 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 sg, db, "no such
1d8e5 20 73 61 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c savepoint: %s",
1d8e6 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 u.aq.zName);.
1d8e7 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1d8e8 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 ERROR;. }else
1d8e9 20 69 66 28 0a 20 20 20 20 20 20 20 20 64 62 2d if(. db-
1d8ea 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 >writeVdbeCnt>0
1d8eb 7c 7c 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 || (u.aq.p1==SAV
1d8ec 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 EPOINT_ROLLBACK
1d8ed 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 && db->activeVdb
1d8ee 65 43 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 eCnt>1). ){.
1d8ef 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f /* It is no
1d8f0 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 t possible to re
1d8f1 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 lease (commit) a
1d8f2 20 73 61 76 65 70 6f 69 6e 74 20 69 66 20 74 68 savepoint if th
1d8f3 65 72 65 20 61 72 65 0a 20 20 20 20 20 20 2a 2a ere are. **
1d8f4 20 61 63 74 69 76 65 20 77 72 69 74 65 20 73 74 active write st
1d8f5 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 atements. It is
1d8f6 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 not possible to
1d8f7 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61 76 65 70 rollback a savep
1d8f8 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 oint. ** if
1d8f9 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 there are any a
1d8fa 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 ctive statements
1d8fb 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a at all.. *
1d8fc 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 /. sqlite3S
1d8fd 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 etString(&p->zEr
1d8fe 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 rMsg, db,.
1d8ff 20 20 22 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 "cannot %s sav
1d900 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 epoint - SQL sta
1d901 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 tements in progr
1d902 65 73 73 22 2c 0a 20 20 20 20 20 20 20 20 28 75 ess",. (u
1d903 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e .aq.p1==SAVEPOIN
1d904 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f T_ROLLBACK ? "ro
1d905 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 llback": "releas
1d906 65 22 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 e"). );.
1d907 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 rc = SQLITE_B
1d908 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a USY;. }else{.
1d909 0a 20 20 20 20 20 20 2f 2a 20 44 65 74 65 72 6d . /* Determ
1d90a 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e ine whether or n
1d90b 6f 74 20 74 68 69 73 20 69 73 20 61 20 74 72 61 ot this is a tra
1d90c 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 nsaction savepoi
1d90d 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 nt. If so,.
1d90e 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 69 73 20 ** and this is
1d90f 61 20 52 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e a RELEASE comman
1d910 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 d, then the curr
1d911 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a ent transaction.
1d912 20 20 20 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d ** is comm
1d913 69 74 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a itted.. */.
1d914 20 20 20 20 20 20 69 6e 74 20 69 73 54 72 61 6e int isTran
1d915 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 saction = u.aq.p
1d916 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1d917 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73 54 72 61 ==0 && db->isTra
1d918 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e nsactionSavepoin
1d919 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 54 t;. if( isT
1d91a 72 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 75 2e ransaction && u.
1d91b 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1d91c 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 _RELEASE ){.
1d91d 20 20 20 20 69 66 28 20 28 72 63 20 3d 20 73 71 if( (rc = sq
1d91e 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b lite3VdbeCheckFk
1d91f 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f (p, 1))!=SQLITE_
1d920 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 OK ){.
1d921 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e goto vdbe_return
1d922 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1d923 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1d924 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 it = 1;.
1d925 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48 if( sqlite3VdbeH
1d926 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 alt(p)==SQLITE_B
1d927 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20 USY ){.
1d928 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 p->pc = pc;.
1d929 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 db->autoC
1d92a 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 ommit = 0;.
1d92b 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 p->rc = rc
1d92c 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 = SQLITE_BUSY;.
1d92d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 goto vd
1d92e 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 be_return;.
1d92f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d }. db-
1d930 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 >isTransactionSa
1d931 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 vepoint = 0;.
1d932 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b rc = p->rc;
1d933 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1d934 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65 u.aq.iSave
1d935 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 point = db->nSav
1d936 65 70 6f 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 epoint - u.aq.iS
1d937 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 avepoint - 1;.
1d938 20 20 20 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 for(u.aq.i
1d939 69 3d 30 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d i=0; u.aq.ii<db-
1d93a 3e 6e 44 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 >nDb; u.aq.ii++)
1d93b 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d {. rc =
1d93c 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 sqlite3BtreeSav
1d93d 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 epoint(db->aDb[u
1d93e 2e 61 71 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 .aq.ii].pBt, u.a
1d93f 71 2e 70 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 q.p1, u.aq.iSave
1d940 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 point);.
1d941 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
1d942 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 _OK ){.
1d943 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1d944 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1d945 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1d946 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e }. if( u.
1d947 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 aq.p1==SAVEPOINT
1d948 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 _ROLLBACK && (db
1d949 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 ->flags&SQLITE_I
1d94a 6e 74 65 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 nternChanges)!=0
1d94b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 ){. sq
1d94c 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1d94d 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1d94e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
1d94f 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 ite3ResetInterna
1d950 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a lSchema(db, 0);.
1d951 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1d952 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 67 61 }.. /* Rega
1d953 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 rdless of whethe
1d954 72 20 74 68 69 73 20 69 73 20 61 20 52 45 4c 45 r this is a RELE
1d955 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c ASE or ROLLBACK,
1d956 20 64 65 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 destroy all.
1d957 20 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 ** savepoints
1d958 20 6e 65 73 74 65 64 20 69 6e 73 69 64 65 20 6f nested inside o
1d959 66 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 f the savepoint
1d95a 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f being operated o
1d95b 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c n. */. whil
1d95c 65 28 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e e( db->pSavepoin
1d95d 74 21 3d 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 t!=u.aq.pSavepoi
1d95e 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e nt ){. u.
1d95f 61 71 2e 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 aq.pTmp = db->pS
1d960 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 avepoint;.
1d961 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 db->pSavepoint
1d962 20 3d 20 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e = u.aq.pTmp->pN
1d963 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ext;. sql
1d964 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 ite3DbFree(db, u
1d965 2e 61 71 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 .aq.pTmp);.
1d966 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e db->nSavepoin
1d967 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 t--;. }..
1d968 20 20 20 20 2f 2a 20 49 66 20 69 74 20 69 73 20 /* If it is
1d969 61 20 52 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 a RELEASE, then
1d96a 64 65 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 destroy the save
1d96b 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 point being oper
1d96c 61 74 65 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a ated on. **
1d96d 20 74 6f 6f 2e 20 49 66 20 69 74 20 69 73 20 61 too. If it is a
1d96e 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68 ROLLBACK TO, th
1d96f 65 6e 20 73 65 74 20 74 68 65 20 6e 75 6d 62 65 en set the numbe
1d970 72 20 6f 66 20 64 65 66 65 72 72 65 64 0a 20 20 r of deferred.
1d971 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e ** constrain
1d972 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 t violations pre
1d973 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 sent in the data
1d974 62 61 73 65 20 74 6f 20 74 68 65 20 76 61 6c 75 base to the valu
1d975 65 20 73 74 6f 72 65 64 0a 20 20 20 20 20 20 2a e stored. *
1d976 2a 20 77 68 65 6e 20 74 68 65 20 73 61 76 65 70 * when the savep
1d977 6f 69 6e 74 20 77 61 73 20 63 72 65 61 74 65 64 oint was created
1d978 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 . */. if(
1d979 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 u.aq.p1==SAVEPOI
1d97a 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 NT_RELEASE ){.
1d97b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1d97c 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 aq.pSavepoint==d
1d97d 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 29 3b b->pSavepoint );
1d97e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 70 53 61 . db->pSa
1d97f 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 vepoint = u.aq.p
1d980 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 Savepoint->pNext
1d981 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
1d982 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 3DbFree(db, u.aq
1d983 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 .pSavepoint);.
1d984 20 20 20 20 20 20 69 66 28 20 21 69 73 54 72 61 if( !isTra
1d985 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 nsaction ){.
1d986 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 db->nSavep
1d987 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 oint--;.
1d988 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
1d989 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 db->nDefe
1d98a 72 72 65 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e rredCons = u.aq.
1d98b 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66 pSavepoint->nDef
1d98c 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 erredCons;.
1d98d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
1d98e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1d98f 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 ode: AutoCommit
1d990 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1d991 2a 20 53 65 74 20 74 68 65 20 64 61 74 61 62 61 * Set the databa
1d992 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 se auto-commit f
1d993 6c 61 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 lag to P1 (1 or
1d994 30 29 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 0). If P2 is tru
1d995 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 e, roll.** back
1d996 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 any currently ac
1d997 74 69 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 tive btree trans
1d998 61 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 actions. If ther
1d999 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 e are any active
1d99a 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 .** VMs (apart f
1d99b 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 rom this one), t
1d99c 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 hen a ROLLBACK f
1d99d 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 ails. A COMMIT
1d99e 66 61 69 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 fails if.** ther
1d99f 65 20 61 72 65 20 61 63 74 69 76 65 20 77 72 69 e are active wri
1d9a0 74 69 6e 67 20 56 4d 73 20 6f 72 20 61 63 74 69 ting VMs or acti
1d9a1 76 65 20 56 4d 73 20 74 68 61 74 20 75 73 65 20 ve VMs that use
1d9a2 73 68 61 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a shared cache..**
1d9a3 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 .** This instruc
1d9a4 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 tion causes the
1d9a5 56 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 VM to halt..*/.c
1d9a6 61 73 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 ase OP_AutoCommi
1d9a7 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c t: {.#if 0 /* l
1d9a8 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1d9a9 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a oved into u.ar *
1d9aa 2f 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 /. int desiredA
1d9ab 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 utoCommit;. int
1d9ac 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e iRollback;. in
1d9ad 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 t turnOnAC;.#end
1d9ae 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1d9af 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1d9b0 20 75 2e 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 u.ar */.. u.ar
1d9b1 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1d9b2 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 it = pOp->p1;.
1d9b3 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d u.ar.iRollback =
1d9b4 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 pOp->p2;. u.ar
1d9b5 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 .turnOnAC = u.ar
1d9b6 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d .desiredAutoComm
1d9b7 69 74 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 it && !db->autoC
1d9b8 6f 6d 6d 69 74 3b 0a 20 20 61 73 73 65 72 74 28 ommit;. assert(
1d9b9 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 u.ar.desiredAut
1d9ba 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e oCommit==1 || u.
1d9bb 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f ar.desiredAutoCo
1d9bc 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 mmit==0 );. ass
1d9bd 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 ert( u.ar.desire
1d9be 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c dAutoCommit==1 |
1d9bf 7c 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b | u.ar.iRollback
1d9c0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ==0 );. assert(
1d9c1 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 db->activeVdbeC
1d9c2 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c nt>0 ); /* At l
1d9c3 65 61 73 74 20 74 68 69 73 20 6f 6e 65 20 56 4d east this one VM
1d9c4 20 69 73 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 is active */..
1d9c5 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e if( u.ar.turnOn
1d9c6 41 43 20 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c AC && u.ar.iRoll
1d9c7 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 back && db->acti
1d9c8 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 veVdbeCnt>1 ){.
1d9c9 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e /* If this in
1d9ca 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d struction implem
1d9cb 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 ents a ROLLBACK
1d9cc 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 and other VMs ar
1d9cd 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 e. ** still r
1d9ce 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 unning, and a tr
1d9cf 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1d9d0 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 ive, return an e
1d9d1 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a rror indicating.
1d9d2 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 ** that the
1d9d3 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 other VMs must c
1d9d4 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 omplete first..
1d9d5 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
1d9d6 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1d9d7 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e ErrMsg, db, "can
1d9d8 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 not rollback tra
1d9d9 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 nsaction - ".
1d9da 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d "SQL statem
1d9db 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 ents in progress
1d9dc 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ");. rc = SQL
1d9dd 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 ITE_BUSY;. }els
1d9de 65 20 69 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f e if( u.ar.turnO
1d9df 6e 41 43 20 26 26 20 21 75 2e 61 72 2e 69 52 6f nAC && !u.ar.iRo
1d9e0 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 llback && db->wr
1d9e1 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a iteVdbeCnt>0 ){.
1d9e2 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 /* If this i
1d9e3 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 nstruction imple
1d9e4 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 ments a COMMIT a
1d9e5 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 nd other VMs are
1d9e6 20 77 72 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 writing. **
1d9e7 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 return an error
1d9e8 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 indicating that
1d9e9 74 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 the other VMs mu
1d9ea 73 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 st complete firs
1d9eb 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 t.. */. sq
1d9ec 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 lite3SetString(&
1d9ed 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 p->zErrMsg, db,
1d9ee 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 "cannot commit t
1d9ef 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 ransaction - ".
1d9f0 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 "SQL stat
1d9f1 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 ements in progre
1d9f2 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 ss");. rc = S
1d9f3 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 QLITE_BUSY;. }e
1d9f4 6c 73 65 20 69 66 28 20 75 2e 61 72 2e 64 65 73 lse if( u.ar.des
1d9f5 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d iredAutoCommit!=
1d9f6 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 db->autoCommit )
1d9f7 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 72 2e 69 {. if( u.ar.i
1d9f8 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 Rollback ){.
1d9f9 20 20 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 assert( u.ar.d
1d9fa 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 esiredAutoCommit
1d9fb 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==1 );. sql
1d9fc 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 ite3RollbackAll(
1d9fd 64 62 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 db);. db->a
1d9fe 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 utoCommit = 1;.
1d9ff 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 }else if( (rc
1da00 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 = sqlite3VdbeCh
1da01 65 63 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 eckFk(p, 1))!=SQ
1da02 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1da03 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 goto vdbe_retur
1da04 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 n;. }else{.
1da05 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d db->autoComm
1da06 69 74 20 3d 20 28 75 38 29 75 2e 61 72 2e 64 65 it = (u8)u.ar.de
1da07 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b siredAutoCommit;
1da08 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
1da09 65 33 56 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 e3VdbeHalt(p)==S
1da0a 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 QLITE_BUSY ){.
1da0b 20 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 p->pc = pc
1da0c 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 ;. db->au
1da0d 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 toCommit = (u8)(
1da0e 31 2d 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 1-u.ar.desiredAu
1da0f 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 toCommit);.
1da10 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1da11 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
1da12 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 goto vdbe_r
1da13 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 eturn;. }.
1da14 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
1da15 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d db->nStatement=
1da16 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 =0 );. sqlite
1da17 33 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 3CloseSavepoints
1da18 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 2d (db);. if( p-
1da19 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 >rc==SQLITE_OK )
1da1a 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c {. rc = SQL
1da1b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 ITE_DONE;. }e
1da1c 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1da1d 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
1da1e 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 }. goto vdb
1da1f 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 e_return;. }els
1da20 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 e{. sqlite3Se
1da21 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 tString(&p->zErr
1da22 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 Msg, db,.
1da23 20 28 21 75 2e 61 72 2e 64 65 73 69 72 65 64 41 (!u.ar.desiredA
1da24 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e utoCommit)?"cann
1da25 6f 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73 ot start a trans
1da26 61 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 action within a
1da27 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 transaction":(.
1da28 20 20 20 20 20 20 20 28 75 2e 61 72 2e 69 52 6f (u.ar.iRo
1da29 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 llback)?"cannot
1da2a 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 rollback - no tr
1da2b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 ansaction is act
1da2c 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20 20 20 ive":.
1da2d 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 "cannot
1da2e 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 commit - no tra
1da2f 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 nsaction is acti
1da30 76 65 22 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d ve"));.. rc =
1da31 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1da32 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f }. break;.}../
1da33 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 * Opcode: Transa
1da34 63 74 69 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 ction P1 P2 * *
1da35 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 *.**.** Begin a
1da36 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 transaction. Th
1da37 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e e transaction en
1da38 64 73 20 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 ds when a Commit
1da39 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 or Rollback.**
1da3a 6f 70 63 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e opcode is encoun
1da3b 74 65 72 65 64 2e 20 20 44 65 70 65 6e 64 69 6e tered. Dependin
1da3c 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 g on the ON CONF
1da3d 4c 49 43 54 20 73 65 74 74 69 6e 67 2c 20 74 68 LICT setting, th
1da3e 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e e.** transaction
1da3f 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 72 might also be r
1da40 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20 61 6e olled back if an
1da41 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e error is encoun
1da42 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 tered..**.** P1
1da43 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 is the index of
1da44 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c the database fil
1da45 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 74 e on which the t
1da46 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a ransaction is.**
1da47 20 73 74 61 72 74 65 64 2e 20 20 49 6e 64 65 78 started. Index
1da48 20 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 0 is the main d
1da49 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 atabase file and
1da4a 20 69 6e 64 65 78 20 31 20 69 73 20 74 68 65 0a index 1 is the.
1da4b 2a 2a 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72 ** file used for
1da4c 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1da4d 73 2e 20 20 49 6e 64 69 63 65 73 20 6f 66 20 32 s. Indices of 2
1da4e 20 6f 72 20 6d 6f 72 65 20 61 72 65 20 75 73 65 or more are use
1da4f 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 d for.** attache
1da50 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a d databases..**.
1da51 2a 2a 20 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d ** If P2 is non-
1da52 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 zero, then a wri
1da53 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 te-transaction i
1da54 73 20 73 74 61 72 74 65 64 2e 20 20 41 20 52 45 s started. A RE
1da55 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a SERVED lock is.*
1da56 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 * obtained on th
1da57 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 e database file
1da58 77 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72 61 when a write-tra
1da59 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 nsaction is star
1da5a 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 ted. No.** othe
1da5b 72 20 70 72 6f 63 65 73 73 20 63 61 6e 20 73 74 r process can st
1da5c 61 72 74 20 61 6e 6f 74 68 65 72 20 77 72 69 74 art another writ
1da5d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 e transaction wh
1da5e 69 6c 65 20 74 68 69 73 20 74 72 61 6e 73 61 63 ile this transac
1da5f 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 tion is.** under
1da60 77 61 79 2e 20 20 53 74 61 72 74 69 6e 67 20 61 way. Starting a
1da61 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 write transacti
1da62 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74 65 73 20 on also creates
1da63 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e a rollback journ
1da64 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 al. A.** write t
1da65 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 ransaction must
1da66 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 be started befor
1da67 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 63 61 e any changes ca
1da68 6e 20 62 65 20 6d 61 64 65 20 74 6f 20 74 68 65 n be made to the
1da69 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 49 .** database. I
1da6a 66 20 50 32 20 69 73 20 32 20 6f 72 20 67 72 65 f P2 is 2 or gre
1da6b 61 74 65 72 20 74 68 65 6e 20 61 6e 20 45 58 43 ater then an EXC
1da6c 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 LUSIVE lock is a
1da6d 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 lso obtained.**
1da6e 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a on the file..**.
1da6f 2a 2a 20 49 66 20 61 20 77 72 69 74 65 2d 74 72 ** If a write-tr
1da70 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61 ansaction is sta
1da71 72 74 65 64 20 61 6e 64 20 74 68 65 20 56 64 62 rted and the Vdb
1da72 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 e.usesStmtJourna
1da73 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75 l flag is.** tru
1da74 65 20 28 74 68 69 73 20 66 6c 61 67 20 69 73 20 e (this flag is
1da75 73 65 74 20 69 66 20 74 68 65 20 56 64 62 65 20 set if the Vdbe
1da76 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20 may modify more
1da77 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64 than one row and
1da78 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e may.** throw an
1da79 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e ABORT exception
1da7a 29 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 ), a statement t
1da7b 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 ransaction may a
1da7c 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a lso be opened..*
1da7d 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69 63 61 * More specifica
1da7e 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 lly, a statement
1da7f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 transaction is
1da80 6f 70 65 6e 65 64 20 69 66 66 20 74 68 65 20 64 opened iff the d
1da81 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 atabase.** conne
1da82 63 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 ction is current
1da83 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f ly not in autoco
1da84 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 mmit mode, or if
1da85 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 there are other
1da86 0a 2a 2a 20 61 63 74 69 76 65 20 73 74 61 74 65 .** active state
1da87 6d 65 6e 74 73 2e 20 41 20 73 74 61 74 65 6d 65 ments. A stateme
1da88 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 nt transaction a
1da89 6c 6c 6f 77 73 20 74 68 65 20 61 66 66 65 63 74 llows the affect
1da8a 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20 56 44 42 s of this.** VDB
1da8b 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 E to be rolled b
1da8c 61 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72 72 ack after an err
1da8d 6f 72 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e or without havin
1da8e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 g to roll back t
1da8f 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 he.** entire tra
1da90 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 nsaction. If no
1da91 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 error is encount
1da92 65 72 65 64 2c 20 74 68 65 20 73 74 61 74 65 6d ered, the statem
1da93 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a ent transaction.
1da94 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 ** will automati
1da95 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65 cally commit whe
1da96 6e 20 74 68 65 20 56 44 42 45 20 68 61 6c 74 73 n the VDBE halts
1da97 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 ..**.** If P2 is
1da98 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 zero, then a re
1da99 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 ad-lock is obtai
1da9a 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 ned on the datab
1da9b 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 ase file..*/.cas
1da9c 65 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e e OP_Transaction
1da9d 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f : {.#if 0 /* lo
1da9e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1da9f 76 65 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f ved into u.as */
1daa0 0a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 . Btree *pBt;.#
1daa1 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1daa2 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1daa3 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61 nto u.as */.. a
1daa4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1daa5 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 0 && pOp->p1<db-
1daa6 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1daa7 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1daa8 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 & (1<<pOp->p1))!
1daa9 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74 =0 );. u.as.pBt
1daaa 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e = db->aDb[pOp->
1daab 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 p1].pBt;.. if(
1daac 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20 20 20 20 u.as.pBt ){.
1daad 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1daae 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 73 eBeginTrans(u.as
1daaf 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a .pBt, pOp->p2);.
1dab0 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
1dab1 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 TE_BUSY ){.
1dab2 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 p->pc = pc;.
1dab3 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 p->rc = rc =
1dab4 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 SQLITE_BUSY;.
1dab5 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 goto vdbe_ret
1dab6 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 urn;. }. i
1dab7 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b f( rc!=SQLITE_OK
1dab8 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 ){. goto a
1dab9 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f bort_due_to_erro
1daba 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 r;. }.. if
1dabb 28 20 70 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e ( pOp->p2 && p->
1dabc 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a usesStmtJournal.
1dabd 20 20 20 20 20 26 26 20 28 64 62 2d 3e 61 75 74 && (db->aut
1dabe 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 oCommit==0 || db
1dabf 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e ->activeVdbeCnt>
1dac0 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 1). ){.
1dac1 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 assert( sqlite3B
1dac2 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 75 2e treeIsInTrans(u.
1dac3 61 73 2e 70 42 74 29 20 29 3b 0a 20 20 20 20 20 as.pBt) );.
1dac4 20 69 66 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 if( p->iStateme
1dac5 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 nt==0 ){.
1dac6 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 assert( db->nSt
1dac7 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 atement>=0 && db
1dac8 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 ->nSavepoint>=0
1dac9 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e );. db->n
1daca 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20 Statement++;.
1dacb 20 20 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 p->iStateme
1dacc 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f nt = db->nSavepo
1dacd 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 int + db->nState
1dace 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ment;. }.
1dacf 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1dad0 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 75 BtreeBeginStmt(u
1dad1 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61 .as.pBt, p->iSta
1dad2 74 65 6d 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 tement);..
1dad3 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 63 75 72 /* Store the cur
1dad4 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 rent value of th
1dad5 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c e database handl
1dad6 65 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 es deferred cons
1dad7 74 72 61 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 traint. **
1dad8 63 6f 75 6e 74 65 72 2e 20 49 66 20 74 68 65 20 counter. If the
1dad9 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 statement transa
1dada 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62 ction needs to b
1dadb 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20 e rolled back,.
1dadc 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 ** the valu
1dadd 65 20 6f 66 20 74 68 69 73 20 63 6f 75 6e 74 65 e of this counte
1dade 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 r needs to be re
1dadf 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a stored too. */.
1dae0 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65 p->nStmtDe
1dae1 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 fCons = db->nDef
1dae2 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d erredCons;. }
1dae3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1dae4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 ./* Opcode: Read
1dae5 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
1dae6 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 * *.**.** Read c
1dae7 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20 ookie number P3
1dae8 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31 from database P1
1dae9 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e and write it in
1daea 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a to register P2..
1daeb 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 ** P3==1 is the
1daec 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 schema version.
1daed 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20 64 61 P3==2 is the da
1daee 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a tabase format..*
1daef 2a 20 50 33 3d 3d 33 20 69 73 20 74 68 65 20 72 * P3==3 is the r
1daf0 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 ecommended pager
1daf1 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 cache size, and
1daf2 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d so forth. P1==
1daf3 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 0 is.** the main
1daf4 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1daf5 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
1daf6 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73 database file us
1daf7 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 ed to store.** t
1daf8 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e emporary tables.
1daf9 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 .**.** There mus
1dafa 74 20 62 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b t be a read-lock
1dafb 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 on the database
1dafc 20 28 65 69 74 68 65 72 20 61 20 74 72 61 6e 73 (either a trans
1dafd 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 action.** must b
1dafe 65 20 73 74 61 72 74 65 64 20 6f 72 20 74 68 65 e started or the
1daff 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 re must be an op
1db00 65 6e 20 63 75 72 73 6f 72 29 20 62 65 66 6f 72 en cursor) befor
1db01 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e 67 20 74 e.** executing t
1db02 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e his instruction.
1db03 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 .*/.case OP_Read
1db04 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 Cookie: {
1db05 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d /* out2-
1db06 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 prerelease */.#i
1db07 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1db08 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1db09 74 6f 20 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 to u.at */. int
1db0a 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 iMeta;. int iD
1db0b 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 b;. int iCookie
1db0c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1db0d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1db0e 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a d into u.at */..
1db0f 20 20 75 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 u.at.iDb = pOp
1db10 2d 3e 70 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f ->p1;. u.at.iCo
1db11 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a okie = pOp->p3;.
1db12 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1db13 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 3<SQLITE_N_BTREE
1db14 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 _META );. asser
1db15 74 28 20 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 t( u.at.iDb>=0 &
1db16 26 20 75 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e & u.at.iDb<db->n
1db17 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 Db );. assert(
1db18 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 db->aDb[u.at.iDb
1db19 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 ].pBt!=0 );. as
1db1a 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d sert( (p->btreeM
1db1b 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 ask & (1<<u.at.i
1db1c 44 62 29 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 Db))!=0 );.. sq
1db1d 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 lite3BtreeGetMet
1db1e 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 a(db->aDb[u.at.i
1db1f 44 62 5d 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 Db].pBt, u.at.iC
1db20 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a 29 26 75 ookie, (u32 *)&u
1db21 2e 61 74 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f .at.iMeta);. pO
1db22 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 ut->u.i = u.at.i
1db23 4d 65 74 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d Meta;. break;.}
1db24 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 ../* Opcode: Set
1db25 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20 Cookie P1 P2 P3
1db26 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 * *.**.** Write
1db27 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 the content of r
1db28 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65 egister P3 (inte
1db29 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e rpreted as an in
1db2a 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 teger).** into c
1db2b 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20 ookie number P2
1db2c 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 20 of database P1.
1db2d 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 73 63 P2==1 is the sc
1db2e 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a hema version. .
1db2f 2a 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 ** P2==2 is the
1db30 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e database format.
1db31 20 50 32 3d 3d 33 20 69 73 20 74 68 65 20 72 65 P2==3 is the re
1db32 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 commended pager
1db33 63 61 63 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 cache .** size,
1db34 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 and so forth. P
1db35 31 3d 3d 30 20 69 73 20 74 68 65 20 6d 61 69 6e 1==0 is the main
1db36 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 database file a
1db37 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20 nd P1==1 is the
1db38 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c .** database fil
1db39 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 e used to store
1db3a 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
1db3b 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 ..**.** A transa
1db3c 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 ction must be st
1db3d 61 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 arted before exe
1db3e 63 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f cuting this opco
1db3f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 de..*/.case OP_S
1db40 65 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 etCookie: {
1db41 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 /* in3 */.#if
1db42 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1db43 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1db44 20 75 2e 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 u.au */. Db *p
1db45 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f Db;.#endif /* lo
1db46 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1db47 76 65 64 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f ved into u.au */
1db48 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1db49 70 32 3c 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 p2<SQLITE_N_BTRE
1db4a 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73 73 65 E_META );. asse
1db4b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1db4c 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 & pOp->p1<db->nD
1db4d 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 b );. assert( (
1db4e 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 p->btreeMask & (
1db4f 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 1<<pOp->p1))!=0
1db50 29 3b 0a 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 );. u.au.pDb =
1db51 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 &db->aDb[pOp->p1
1db52 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 ];. assert( u.a
1db53 75 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b u.pDb->pBt!=0 );
1db54 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b . pIn3 = &aMem[
1db55 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 73 71 6c 69 pOp->p3];. sqli
1db56 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 te3VdbeMemIntege
1db57 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a rify(pIn3);. /*
1db58 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 See note about
1db59 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67 20 6f index shifting o
1db5a 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 n OP_ReadCookie
1db5b 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 */. rc = sqlite
1db5c 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 3BtreeUpdateMeta
1db5d 28 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20 (u.au.pDb->pBt,
1db5e 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 pOp->p2, (int)pI
1db5f 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 n3->u.i);. if(
1db60 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53 pOp->p2==BTREE_S
1db61 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b CHEMA_VERSION ){
1db62 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 . /* When the
1db63 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 schema cookie c
1db64 68 61 6e 67 65 73 2c 20 72 65 63 6f 72 64 20 74 hanges, record t
1db65 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e he new cookie in
1db66 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 ternally */.
1db67 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d u.au.pDb->pSchem
1db68 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 a->schema_cookie
1db69 20 3d 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e = (int)pIn3->u.
1db6a 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 i;. db->flags
1db6b 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 |= SQLITE_Inter
1db6c 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 nChanges;. }els
1db6d 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 e if( pOp->p2==B
1db6e 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 TREE_FILE_FORMAT
1db6f 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 ){. /* Recor
1db70 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 d changes in the
1db71 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a file format */.
1db72 20 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 u.au.pDb->pS
1db73 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d chema->file_form
1db74 61 74 20 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 at = (u8)pIn3->u
1db75 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f .i;. }. if( pO
1db76 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 p->p1==1 ){.
1db77 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c /* Invalidate al
1db78 6c 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 l prepared state
1db79 6d 65 6e 74 73 20 77 68 65 6e 65 76 65 72 20 74 ments whenever t
1db7a 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 he TEMP database
1db7b 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 . ** schema i
1db7c 73 20 63 68 61 6e 67 65 64 2e 20 20 54 69 63 6b s changed. Tick
1db7d 65 74 20 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 et #1644 */.
1db7e 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 sqlite3ExpirePre
1db7f 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 paredStatements(
1db80 64 62 29 3b 0a 20 20 20 20 70 2d 3e 65 78 70 69 db);. p->expi
1db81 72 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 red = 0;. }. b
1db82 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1db83 64 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 de: VerifyCookie
1db84 20 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 P1 P2 *.**.** C
1db85 68 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f heck the value o
1db86 66 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 f global databas
1db87 65 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 e parameter numb
1db88 65 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 er 0 (the.** sch
1db89 65 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 ema version) and
1db8a 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 make sure it is
1db8b 20 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a equal to P2. .
1db8c 2a 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74 ** P1 is the dat
1db8d 61 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69 abase number whi
1db8e 63 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20 ch is 0 for the
1db8f 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 main database fi
1db90 6c 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 le.** and 1 for
1db91 74 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 the file holding
1db92 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1db93 73 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 s and some highe
1db94 72 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 r number.** for
1db95 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 auxiliary databa
1db96 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 ses..**.** The c
1db97 6f 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74 ookie changes it
1db98 73 20 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72 s value whenever
1db99 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
1db9a 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a hema changes..**
1db9b 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 This operation
1db9c 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 is used to detec
1db9d 74 20 77 68 65 6e 20 74 68 61 74 20 74 68 65 20 t when that the
1db9e 63 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67 cookie has chang
1db9f 65 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 ed.** and that t
1dba0 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 he current proce
1dba1 73 73 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65 ss needs to rere
1dba2 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a ad the schema..*
1dba3 2a 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72 *.** Either a tr
1dba4 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 ansaction needs
1dba5 74 6f 20 68 61 76 65 20 62 65 65 6e 20 73 74 61 to have been sta
1dba6 72 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 rted or an OP_Op
1dba7 65 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 en needs.** to b
1dba8 65 20 65 78 65 63 75 74 65 64 20 28 74 6f 20 65 e executed (to e
1dba9 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 20 stablish a read
1dbaa 6c 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69 lock) before thi
1dbab 73 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 s opcode is.** i
1dbac 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 nvoked..*/.case
1dbad 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a OP_VerifyCookie:
1dbae 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1dbaf 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1dbb0 65 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a ed into u.av */.
1dbb1 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 int iMeta;. B
1dbb2 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 tree *pBt;.#endi
1dbb3 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1dbb4 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1dbb5 75 2e 61 76 20 2a 2f 0a 20 20 61 73 73 65 72 74 u.av */. assert
1dbb6 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1dbb7 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 pOp->p1<db->nDb
1dbb8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d );. assert( (p-
1dbb9 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c >btreeMask & (1<
1dbba 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b <pOp->p1))!=0 );
1dbbb 0a 20 20 75 2e 61 76 2e 70 42 74 20 3d 20 64 62 . u.av.pBt = db
1dbbc 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 ->aDb[pOp->p1].p
1dbbd 42 74 3b 0a 20 20 69 66 28 20 75 2e 61 76 2e 70 Bt;. if( u.av.p
1dbbe 42 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 Bt ){. sqlite
1dbbf 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 75 2e 3BtreeGetMeta(u.
1dbc0 61 76 2e 70 42 74 2c 20 42 54 52 45 45 5f 53 43 av.pBt, BTREE_SC
1dbc1 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 HEMA_VERSION, (u
1dbc2 33 32 20 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61 32 *)&u.av.iMeta
1dbc3 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
1dbc4 75 2e 61 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a u.av.iMeta = 0;.
1dbc5 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 76 2e 69 }. if( u.av.i
1dbc6 4d 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b Meta!=pOp->p2 ){
1dbc7 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
1dbc8 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 ee(db, p->zErrMs
1dbc9 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d g);. p->zErrM
1dbca 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 sg = sqlite3DbSt
1dbcb 72 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 61 rDup(db, "databa
1dbcc 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 se schema has ch
1dbcd 61 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 anged");. /*
1dbce 49 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f If the schema-co
1dbcf 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 okie from the da
1dbd0 74 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63 tabase file matc
1dbd1 68 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 hes the cookie.
1dbd2 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 ** stored wit
1dbd3 68 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 h the in-memory
1dbd4 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f representation o
1dbd5 66 20 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f f the schema, do
1dbd6 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f . ** not relo
1dbd7 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 72 ad the schema fr
1dbd8 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 om the database
1dbd9 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 file.. **.
1dbda 20 2a 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74 ** If virtual-t
1dbdb 61 62 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65 ables are in use
1dbdc 2c 20 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 , this is not ju
1dbdd 73 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 st an optimizati
1dbde 6f 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e on.. ** Often
1dbdf 2c 20 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 , v-tables store
1dbe0 20 74 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f their data in o
1dbe1 74 68 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c ther SQLite tabl
1dbe2 65 73 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a es, which. **
1dbe3 20 61 72 65 20 71 75 65 72 69 65 64 20 66 72 6f are queried fro
1dbe4 6d 20 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29 m within xNext()
1dbe5 20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62 and other v-tab
1dbe6 6c 65 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 le methods using
1dbe7 0a 20 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64 . ** prepared
1dbe8 20 71 75 65 72 69 65 73 2e 20 49 66 20 73 75 63 queries. If suc
1dbe9 68 20 61 20 71 75 65 72 79 20 69 73 20 6f 75 74 h a query is out
1dbea 2d 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 -of-date, we do
1dbeb 6e 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 not want to.
1dbec 2a 2a 20 64 69 73 63 61 72 64 20 74 68 65 20 64 ** discard the d
1dbed 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20 atabase schema,
1dbee 61 73 20 74 68 65 20 75 73 65 72 20 63 6f 64 65 as the user code
1dbef 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 implementing th
1dbf0 65 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 e. ** v-table
1dbf1 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62 would have to b
1dbf2 65 20 72 65 61 64 79 20 66 6f 72 20 74 68 65 20 e ready for the
1dbf3 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 sqlite3_vtab str
1dbf4 75 63 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20 ucture itself.
1dbf5 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c ** to be inval
1dbf6 69 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20 idated whenever
1dbf7 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 sqlite3_step() i
1dbf8 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 s called from wi
1dbf9 74 68 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d thin. ** a v-
1dbfa 74 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 table method..
1dbfb 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d */. if( db-
1dbfc 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 >aDb[pOp->p1].pS
1dbfd 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f chema->schema_co
1dbfe 6f 6b 69 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61 okie!=u.av.iMeta
1dbff 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1dc00 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 3ResetInternalSc
1dc01 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 hema(db, pOp->p1
1dc02 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 );. }.. sq
1dc03 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 lite3ExpirePrepa
1dc04 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 redStatements(db
1dc05 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 );. rc = SQLI
1dc06 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 TE_SCHEMA;. }.
1dc07 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1dc08 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 code: OpenRead P
1dc09 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 1 P2 P3 P4 P5.**
1dc0a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d .** Open a read-
1dc0b 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 only cursor for
1dc0c 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 the database tab
1dc0d 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 le whose root pa
1dc0e 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 ge is.** P2 in a
1dc0f 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 database file.
1dc10 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 The database fi
1dc11 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 le is determined
1dc12 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d by P3. .** P3==
1dc13 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 0 means the main
1dc14 20 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 database, P3==1
1dc15 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 means the datab
1dc16 61 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a ase used for .**
1dc17 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 temporary table
1dc18 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e s, and P3>1 mean
1dc19 73 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 s used the corre
1dc1a 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 sponding attache
1dc1b 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 d.** database.
1dc1c 47 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 Give the new cur
1dc1d 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 sor an identifie
1dc1e 72 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 r of P1. The P1
1dc1f 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 .** values need
1dc20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 not be contiguou
1dc21 73 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c s but all P1 val
1dc22 75 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d ues should be sm
1dc23 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a all integers..**
1dc24 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 It is an error
1dc25 66 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 for P1 to be neg
1dc26 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 ative..**.** If
1dc27 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 P5!=0 then use t
1dc28 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 he content of re
1dc29 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65 gister P2 as the
1dc2a 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a root page, not.
1dc2b 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ** the value of
1dc2c 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a P2 itself..**.**
1dc2d 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 There will be a
1dc2e 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 read lock on th
1dc2f 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 e database whene
1dc30 76 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a ver there is an.
1dc31 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 ** open cursor.
1dc32 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 If the database
1dc33 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 was unlocked pr
1dc34 69 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 ior to this inst
1dc35 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 ruction.** then
1dc36 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 a read lock is a
1dc37 63 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20 cquired as part
1dc38 6f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 of this instruct
1dc39 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 ion. A read.**
1dc3a 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 lock allows othe
1dc3b 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 r processes to r
1dc3c 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 ead the database
1dc3d 20 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a but prohibits.*
1dc3e 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 * any other proc
1dc3f 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 ess from modifyi
1dc40 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e ng the database.
1dc41 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 The read lock
1dc42 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 is.** released w
1dc43 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 hen all cursors
1dc44 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 are closed. If
1dc45 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1dc46 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 attempts.** to
1dc47 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 get a read lock
1dc48 62 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 but fails, the s
1dc49 63 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 cript terminates
1dc4a 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 with an.** SQLI
1dc4b 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f TE_BUSY error co
1dc4c 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 de..**.** The P4
1dc4d 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 value may be ei
1dc4e 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 ther an integer
1dc4f 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 (P4_INT32) or a
1dc50 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 pointer to.** a
1dc51 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
1dc52 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 e (P4_KEYINFO).
1dc53 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 If it is a point
1dc54 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 er to a KeyInfo
1dc55 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 .** structure, t
1dc56 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 hen said structu
1dc57 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 re defines the c
1dc58 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 ontent and colla
1dc59 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 ting .** sequenc
1dc5a 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 e of the index b
1dc5b 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 eing opened. Oth
1dc5c 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 erwise, if P4 is
1dc5d 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 an integer .**
1dc5e 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 value, it is set
1dc5f 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f to the number o
1dc60 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 f columns in the
1dc61 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 table..**.** Se
1dc62 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 e also OpenWrite
1dc63 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ..*/./* Opcode:
1dc64 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 OpenWrite P1 P2
1dc65 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f P3 P4 P5.**.** O
1dc66 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 pen a read/write
1dc67 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 cursor named P1
1dc68 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 on the table or
1dc69 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f index whose roo
1dc6a 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e t.** page is P2.
1dc6b 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 Or if P5!=0 us
1dc6c 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 e the content of
1dc6d 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 register P2 to
1dc6e 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 find the.** root
1dc6f 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 page..**.** The
1dc70 20 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 P4 value may be
1dc71 20 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 either an integ
1dc72 65 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 er (P4_INT32) or
1dc73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a a pointer to.**
1dc74 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 a KeyInfo struc
1dc75 74 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f ture (P4_KEYINFO
1dc76 29 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f ). If it is a po
1dc77 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e inter to a KeyIn
1dc78 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 fo .** structure
1dc79 2c 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 , then said stru
1dc7a 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 cture defines th
1dc7b 65 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f e content and co
1dc7c 6c 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 llating .** sequ
1dc7d 65 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 ence of the inde
1dc7e 78 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 x being opened.
1dc7f 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 Otherwise, if P4
1dc80 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a is an integer .
1dc81 2a 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 ** value, it is
1dc82 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
1dc83 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
1dc84 74 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f the table, or to
1dc85 20 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 the.** largest
1dc86 69 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c index of any col
1dc87 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 umn of the table
1dc88 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c that is actuall
1dc89 79 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 y used..**.** Th
1dc8a 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 is instruction w
1dc8b 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f orks just like O
1dc8c 70 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74 penRead except t
1dc8d 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65 hat it opens the
1dc8e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 cursor.** in re
1dc8f 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 ad/write mode.
1dc90 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c For a given tabl
1dc91 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20 e, there can be
1dc92 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 one or more read
1dc93 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 -only.** cursors
1dc94 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61 or a single rea
1dc95 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62 d/write cursor b
1dc96 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a ut not both..**.
1dc97 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e ** See also Open
1dc98 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 Read..*/.case OP
1dc99 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 _OpenRead:.case
1dc9a 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a OP_OpenWrite: {.
1dc9b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1dc9c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1dc9d 69 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69 into u.aw */. i
1dc9e 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79 nt nField;. Key
1dc9f 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a Info *pKeyInfo;.
1dca0 20 20 69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20 int p2;. int
1dca1 69 44 62 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 iDb;. int wrFla
1dca2 67 3b 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a g;. Btree *pX;.
1dca3 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1dca4 75 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 ur;. Db *pDb;.#
1dca5 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1dca6 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1dca7 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 69 nto u.aw */.. i
1dca8 66 28 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b f( p->expired ){
1dca9 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 . rc = SQLITE
1dcaa 5f 41 42 4f 52 54 3b 0a 20 20 20 20 62 72 65 61 _ABORT;. brea
1dcab 6b 3b 0a 20 20 7d 0a 0a 20 20 75 2e 61 77 2e 6e k;. }.. u.aw.n
1dcac 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61 Field = 0;. u.a
1dcad 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a w.pKeyInfo = 0;.
1dcae 20 20 75 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d u.aw.p2 = pOp-
1dcaf 3e 70 32 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20 >p2;. u.aw.iDb
1dcb0 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 = pOp->p3;. ass
1dcb1 65 72 74 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30 ert( u.aw.iDb>=0
1dcb2 20 26 26 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d && u.aw.iDb<db-
1dcb3 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 >nDb );. assert
1dcb4 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1dcb5 26 20 28 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29 & (1<<u.aw.iDb))
1dcb6 21 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44 !=0 );. u.aw.pD
1dcb7 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61 b = &db->aDb[u.a
1dcb8 77 2e 69 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70 w.iDb];. u.aw.p
1dcb9 58 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42 X = u.aw.pDb->pB
1dcba 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 t;. assert( u.a
1dcbb 77 2e 70 58 21 3d 30 20 29 3b 0a 20 20 69 66 28 w.pX!=0 );. if(
1dcbc 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 pOp->opcode==OP
1dcbd 5f 4f 70 65 6e 57 72 69 74 65 20 29 7b 0a 20 20 _OpenWrite ){.
1dcbe 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 u.aw.wrFlag =
1dcbf 31 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 77 2e 1;. if( u.aw.
1dcc0 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 pDb->pSchema->fi
1dcc1 6c 65 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d le_format < p->m
1dcc2 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 inWriteFileForma
1dcc3 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 t ){. p->mi
1dcc4 6e 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 nWriteFileFormat
1dcc5 20 3d 20 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 = u.aw.pDb->pSc
1dcc6 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 hema->file_forma
1dcc7 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 t;. }. }else
1dcc8 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61 {. u.aw.wrFla
1dcc9 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 g = 0;. }. if(
1dcca 20 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 pOp->p5 ){.
1dccb 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 32 3e assert( u.aw.p2>
1dccc 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1dccd 20 75 2e 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 u.aw.p2<=p->nMe
1dcce 6d 20 29 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 m );. pIn2 =
1dccf 26 61 4d 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a &aMem[u.aw.p2];.
1dcd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1dcd1 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e emIntegerify(pIn
1dcd2 32 29 3b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20 2);. u.aw.p2
1dcd3 3d 20 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 = (int)pIn2->u.i
1dcd4 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61 ;. /* The u.a
1dcd5 77 2e 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79 w.p2 value alway
1dcd6 73 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70 s comes from a p
1dcd7 72 69 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61 rior OP_CreateTa
1dcd8 62 6c 65 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20 ble opcode and.
1dcd9 20 20 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64 ** that opcod
1dcda 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 73 65 e will always se
1dcdb 74 20 74 68 65 20 75 2e 61 77 2e 70 32 20 76 61 t the u.aw.p2 va
1dcdc 6c 75 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65 lue to 2 or more
1dcdd 20 6f 72 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20 or else fail..
1dcde 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 77 ** If there w
1dcdf 65 72 65 20 61 20 66 61 69 6c 75 72 65 2c 20 74 ere a failure, t
1dce0 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 he prepared stat
1dce1 65 6d 65 6e 74 20 77 6f 75 6c 64 20 68 61 76 65 ement would have
1dce2 20 68 61 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62 halted. ** b
1dce3 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 efore reaching t
1dce4 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e his instruction.
1dce5 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 */. if( NEVE
1dce6 52 28 75 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b R(u.aw.p2<2) ) {
1dce7 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 . rc = SQLI
1dce8 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b TE_CORRUPT_BKPT;
1dce9 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 . goto abor
1dcea 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1dceb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 }. }. if(
1dcec 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1dced 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75 KEYINFO ){. u
1dcee 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 .aw.pKeyInfo = p
1dcef 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b Op->p4.pKeyInfo;
1dcf0 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e . u.aw.pKeyIn
1dcf1 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d fo->enc = ENC(p-
1dcf2 3e 64 62 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e >db);. u.aw.n
1dcf3 46 69 65 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65 Field = u.aw.pKe
1dcf4 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b yInfo->nField+1;
1dcf5 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 . }else if( pOp
1dcf6 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 ->p4type==P4_INT
1dcf7 33 32 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e 32 ){. u.aw.n
1dcf8 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e Field = pOp->p4.
1dcf9 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 i;. }. assert(
1dcfa 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 pOp->p1>=0 );.
1dcfb 20 75 2e 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c u.aw.pCur = all
1dcfc 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 ocateCursor(p, p
1dcfd 4f 70 2d 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69 Op->p1, u.aw.nFi
1dcfe 65 6c 64 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31 eld, u.aw.iDb, 1
1dcff 29 3b 0a 20 20 69 66 28 20 75 2e 61 77 2e 70 43 );. if( u.aw.pC
1dd00 75 72 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f ur==0 ) goto no_
1dd01 6d 65 6d 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 mem;. u.aw.pCur
1dd02 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
1dd03 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1dd04 65 65 43 75 72 73 6f 72 28 75 2e 61 77 2e 70 58 eeCursor(u.aw.pX
1dd05 2c 20 75 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e , u.aw.p2, u.aw.
1dd06 77 72 46 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65 wrFlag, u.aw.pKe
1dd07 79 49 6e 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72 yInfo, u.aw.pCur
1dd08 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e ->pCursor);. u.
1dd09 61 77 2e 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 aw.pCur->pKeyInf
1dd0a 6f 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 o = u.aw.pKeyInf
1dd0b 6f 3b 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69 o;.. /* Since i
1dd0c 74 20 70 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65 t performs no me
1dd0d 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
1dd0e 6f 72 20 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20 or IO, the only
1dd0f 76 61 6c 75 65 73 20 74 68 61 74 0a 20 20 2a 2a values that. **
1dd10 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 sqlite3BtreeCur
1dd11 73 6f 72 28 29 20 6d 61 79 20 72 65 74 75 72 6e sor() may return
1dd12 20 61 72 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 are SQLITE_EMPT
1dd13 59 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e Y and SQLITE_OK.
1dd14 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50 . ** SQLITE_EMP
1dd15 54 59 20 69 73 20 6f 6e 6c 79 20 72 65 74 75 72 TY is only retur
1dd16 6e 65 64 20 77 68 65 6e 20 61 74 74 65 6d 70 74 ned when attempt
1dd17 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 ing to open the
1dd18 74 61 62 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65 table. ** roote
1dd19 64 20 61 74 20 70 61 67 65 20 31 20 6f 66 20 61 d at page 1 of a
1dd1a 20 7a 65 72 6f 2d 62 79 74 65 20 64 61 74 61 62 zero-byte datab
1dd1b 61 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 ase. */. asser
1dd1c 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d t( rc==SQLITE_EM
1dd1d 50 54 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 PTY || rc==SQLIT
1dd1e 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 E_OK );. if( rc
1dd1f 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 ==SQLITE_EMPTY )
1dd20 7b 0a 20 20 20 20 75 2e 61 77 2e 70 43 75 72 2d {. u.aw.pCur-
1dd21 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 >pCursor = 0;.
1dd22 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1dd23 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 ;. }.. /* Set
1dd24 74 68 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69 the VdbeCursor.i
1dd25 73 54 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 sTable and isInd
1dd26 65 78 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72 ex variables. Pr
1dd27 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 evious versions
1dd28 6f 66 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 of. ** SQLite u
1dd29 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20 sed to check if
1dd2a 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c the root-page fl
1dd2b 61 67 73 20 77 65 72 65 20 73 61 6e 65 20 61 74 ags were sane at
1dd2c 20 74 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a this point. **
1dd2d 20 61 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61 and report data
1dd2e 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 base corruption
1dd2f 69 66 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74 if they were not
1dd30 2c 20 62 75 74 20 74 68 69 73 20 63 68 65 63 6b , but this check
1dd31 20 68 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20 has. ** since
1dd32 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62 moved into the b
1dd33 74 72 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a tree layer. */.
1dd34 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 u.aw.pCur->isT
1dd35 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 able = pOp->p4ty
1dd36 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a pe!=P4_KEYINFO;.
1dd37 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49 u.aw.pCur->isI
1dd38 6e 64 65 78 20 3d 20 21 75 2e 61 77 2e 70 43 75 ndex = !u.aw.pCu
1dd39 72 2d 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 r->isTable;. br
1dd3a 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1dd3b 65 3a 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c e: OpenEphemeral
1dd3c 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a P1 P2 * P4 *.**
1dd3d 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 .** Open a new c
1dd3e 75 72 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72 ursor P1 to a tr
1dd3f 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a ansient table..*
1dd40 2a 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 * The cursor is
1dd41 61 6c 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65 always opened re
1dd42 61 64 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66 ad/write even if
1dd43 20 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 .** the main da
1dd44 74 61 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f tabase is read-o
1dd45 6e 6c 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69 nly. The transi
1dd46 65 6e 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a ent or virtual.*
1dd47 2a 20 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74 * table is delet
1dd48 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 ed automatically
1dd49 20 77 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 when the cursor
1dd4a 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a is closed..**.*
1dd4b 2a 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 * P2 is the numb
1dd4c 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e er of columns in
1dd4d 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 the virtual tab
1dd4e 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f le..** The curso
1dd4f 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 r points to a BT
1dd50 72 65 65 20 74 61 62 6c 65 20 69 66 20 50 34 3d ree table if P4=
1dd51 3d 30 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65 =0 and to a BTre
1dd52 65 20 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34 e index.** if P4
1dd53 20 69 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 is not 0. If P
1dd54 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 4 is not NULL, i
1dd55 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 t points to a Ke
1dd56 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a yInfo structure.
1dd57 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 ** that defines
1dd58 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 the format of ke
1dd59 79 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e ys in the index.
1dd5a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f .**.** This opco
1dd5b 64 65 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c de was once call
1dd5c 65 64 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 ed OpenTemp. Bu
1dd5d 74 20 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a t that created.*
1dd5e 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 * confusion beca
1dd5f 75 73 65 20 74 68 65 20 74 65 72 6d 20 22 74 65 use the term "te
1dd60 6d 70 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 mp table", might
1dd61 20 72 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a refer either.**
1dd62 20 74 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65 to a TEMP table
1dd63 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65 at the SQL leve
1dd64 6c 2c 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 l, or to a table
1dd65 20 6f 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 opened by.** th
1dd66 69 73 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e is opcode. Then
1dd67 20 74 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 this opcode was
1dd68 20 63 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 call OpenVirtua
1dd69 6c 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 l. But.** that
1dd6a 63 72 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f created confusio
1dd6b 6e 20 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65 n with the whole
1dd6c 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 virtual-table i
1dd6d 64 65 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f dea..*/.case OP_
1dd6e 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b OpenEphemeral: {
1dd6f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c .#if 0 /* local
1dd70 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dd71 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 into u.ax */.
1dd72 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b VdbeCursor *pCx;
1dd73 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1dd74 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1dd75 20 69 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 into u.ax */.
1dd76 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 static const int
1dd77 20 6f 70 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20 openFlags =.
1dd78 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 SQLITE_OPEN_R
1dd79 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 EADWRITE |.
1dd7a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 SQLITE_OPEN_CRE
1dd7b 41 54 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 ATE |. SQLI
1dd7c 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 TE_OPEN_EXCLUSIV
1dd7d 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 E |. SQLITE
1dd7e 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c _OPEN_DELETEONCL
1dd7f 4f 53 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 OSE |. SQLI
1dd80 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e TE_OPEN_TRANSIEN
1dd81 54 5f 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 T_DB;.. assert(
1dd82 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 pOp->p1>=0 );.
1dd83 20 75 2e 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f u.ax.pCx = allo
1dd84 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f cateCursor(p, pO
1dd85 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 p->p1, pOp->p2,
1dd86 2d 31 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e -1, 1);. if( u.
1dd87 61 78 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f ax.pCx==0 ) goto
1dd88 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e no_mem;. u.ax.
1dd89 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 pCx->nullRow = 1
1dd8a 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
1dd8b 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c BtreeFactory(db,
1dd8c 20 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 0, 1, SQLITE_DE
1dd8d 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 FAULT_TEMP_CACHE
1dd8e 5f 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 _SIZE, openFlags
1dd8f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1dd90 20 20 20 20 20 20 20 20 20 20 20 20 20 26 75 2e &u.
1dd91 61 78 2e 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 ax.pCx->pBt);.
1dd92 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1dd93 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 K ){. rc = sq
1dd94 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 lite3BtreeBeginT
1dd95 72 61 6e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70 rans(u.ax.pCx->p
1dd96 42 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 Bt, 1);. }. if
1dd97 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1dd98 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 ){. /* If a t
1dd99 72 61 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69 ransient index i
1dd9a 73 20 72 65 71 75 69 72 65 64 2c 20 63 72 65 61 s required, crea
1dd9b 74 65 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 te it by calling
1dd9c 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 . ** sqlite3B
1dd9d 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 treeCreateTable(
1dd9e 29 20 77 69 74 68 20 74 68 65 20 42 54 52 45 45 ) with the BTREE
1dd9f 5f 5a 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62 _ZERODATA flag b
1dda0 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 efore. ** ope
1dda1 6e 69 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72 ning it. If a tr
1dda2 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73 ansient table is
1dda3 20 72 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20 required, just
1dda4 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 use the. ** a
1dda5 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 utomatically cre
1dda6 61 74 65 64 20 74 61 62 6c 65 20 77 69 74 68 20 ated table with
1dda7 72 6f 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20 root-page 1 (an
1dda8 49 4e 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 INTKEY table)..
1dda9 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f */. if( pO
1ddaa 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 p->p4.pKeyInfo )
1ddab 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f {. int pgno
1ddac 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1ddad 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1ddae 4b 45 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 KEYINFO );.
1ddaf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1ddb0 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e eeCreateTable(u.
1ddb1 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 ax.pCx->pBt, &pg
1ddb2 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 no, BTREE_ZERODA
1ddb3 54 41 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 TA);. if( r
1ddb4 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
1ddb5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
1ddb6 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f pgno==MASTER_ROO
1ddb7 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72 T+1 );. r
1ddb8 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1ddb9 43 75 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d Cursor(u.ax.pCx-
1ddba 3e 70 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20 >pBt, pgno, 1,.
1ddbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ddbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
1ddbd 4b 65 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 KeyInfo*)pOp->p4
1ddbe 2e 7a 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 .z, u.ax.pCx->pC
1ddbf 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 ursor);.
1ddc0 75 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e u.ax.pCx->pKeyIn
1ddc1 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 fo = pOp->p4.pKe
1ddc2 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 75 yInfo;. u
1ddc3 2e 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 .ax.pCx->pKeyInf
1ddc4 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e o->enc = ENC(p->
1ddc5 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 db);. }.
1ddc6 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 u.ax.pCx->isT
1ddc7 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 able = 0;. }e
1ddc8 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
1ddc9 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 sqlite3BtreeCurs
1ddca 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 or(u.ax.pCx->pBt
1ddcb 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 , MASTER_ROOT, 1
1ddcc 2c 20 30 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 , 0, u.ax.pCx->p
1ddcd 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 75 Cursor);. u
1ddce 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 .ax.pCx->isTable
1ddcf 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
1ddd0 20 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e u.ax.pCx->isIn
1ddd1 64 65 78 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d dex = !u.ax.pCx-
1ddd2 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 >isTable;. brea
1ddd3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1ddd4 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50 OpenPseudo P1 P
1ddd5 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 2 P3 * *.**.** O
1ddd6 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 pen a new cursor
1ddd7 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 that points to
1ddd8 61 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 a fake table tha
1ddd9 74 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e t contains a sin
1ddda 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 gle.** row of da
1dddb 74 61 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 ta. The content
1dddc 20 6f 66 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 of that one row
1dddd 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 in the content
1ddde 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 of memory.** reg
1dddf 69 73 74 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 ister P2. In ot
1dde0 68 65 72 20 77 6f 72 64 73 2c 20 63 75 72 73 6f her words, curso
1dde1 72 20 50 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 r P1 becomes an
1dde2 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 0a 2a alias for the .*
1dde3 2a 20 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 * MEM_Blob conte
1dde4 6e 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 nt contained in
1dde5 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a register P2..**.
1dde6 2a 2a 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c ** A pseudo-tabl
1dde7 65 20 63 72 65 61 74 65 64 20 62 79 20 74 68 69 e created by thi
1dde8 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 s opcode is used
1dde9 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 to hold the a s
1ddea 69 6e 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 ingle.** row out
1ddeb 70 75 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 put from the sor
1ddec 74 65 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 ter so that the
1dded 72 6f 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d row can be decom
1ddee 70 6f 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e posed into.** in
1ddef 64 69 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 dividual columns
1ddf0 20 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f using the OP_Co
1ddf1 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 lumn opcode. Th
1ddf2 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f e OP_Column opco
1ddf3 64 65 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c de.** is the onl
1ddf4 79 20 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 y cursor opcode
1ddf5 74 68 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20 that works with
1ddf6 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a a pseudo-table..
1ddf7 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20 **.** P3 is the
1ddf8 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 number of fields
1ddf9 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20 in the records
1ddfa 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f that will be sto
1ddfb 72 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 red by.** the ps
1ddfc 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 eudo-table..*/.c
1ddfd 61 73 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 ase OP_OpenPseud
1ddfe 6f 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c o: {.#if 0 /* l
1ddff 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1de00 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a oved into u.ay *
1de01 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1de02 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c pCx;.#endif /* l
1de03 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1de04 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a oved into u.ay *
1de05 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1de06 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 ->p1>=0 );. u.a
1de07 79 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 y.pCx = allocate
1de08 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 Cursor(p, pOp->p
1de09 31 2c 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 1, pOp->p3, -1,
1de0a 30 29 3b 0a 20 20 69 66 28 20 75 2e 61 79 2e 70 0);. if( u.ay.p
1de0b 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f Cx==0 ) goto no_
1de0c 6d 65 6d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d mem;. u.ay.pCx-
1de0d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 >nullRow = 1;.
1de0e 75 2e 61 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f u.ay.pCx->pseudo
1de0f 54 61 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e TableReg = pOp->
1de10 70 32 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e p2;. u.ay.pCx->
1de11 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 isTable = 1;. u
1de12 2e 61 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 .ay.pCx->isIndex
1de13 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d = 0;. break;.}
1de14 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f ../* Opcode: Clo
1de15 73 65 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a se P1 * * * *.**
1de16 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 .** Close a curs
1de17 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 or previously op
1de18 65 6e 65 64 20 61 73 20 50 31 2e 20 20 49 66 20 ened as P1. If
1de19 50 31 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 P1 is not.** cur
1de1a 72 65 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 rently open, thi
1de1b 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 s instruction is
1de1c 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 a no-op..*/.cas
1de1d 65 20 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 e OP_Close: {.
1de1e 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1de1f 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1de20 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 >nCursor );. sq
1de21 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 lite3VdbeFreeCur
1de22 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b sor(p, p->apCsr[
1de23 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e pOp->p1]);. p->
1de24 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d apCsr[pOp->p1] =
1de25 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0;. break;.}..
1de26 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 /* Opcode: SeekG
1de27 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a e P1 P2 P3 P4 *.
1de28 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 **.** If cursor
1de29 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 P1 refers to an
1de2a 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 SQL table (B-Tre
1de2b 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 e that uses inte
1de2c 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 ger keys), .** u
1de2d 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 se the value in
1de2e 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 74 register P3 as t
1de2f 68 65 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73 he key. If curs
1de30 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a or P1 refers .**
1de31 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 to an SQL index
1de32 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 , then P3 is the
1de33 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 first in an arr
1de34 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 ay of P4 registe
1de35 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 rs .** that are
1de36 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 used as an unpac
1de37 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a ked index key. .
1de38 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e **.** Reposition
1de39 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 cursor P1 so th
1de3a 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f at it points to
1de3b 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e the smallest en
1de3c 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 try that .** is
1de3d 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 greater than or
1de3e 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 equal to the key
1de3f 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1de40 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1de41 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e .** greater than
1de42 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 or equal to the
1de43 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e key and P2 is n
1de44 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 ot zero, then ju
1de45 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 mp to P2..**.**
1de46 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c See also: Found,
1de47 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 NotFound, Disti
1de48 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 nct, SeekLt, See
1de49 6b 47 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f kGt, SeekLe.*/./
1de4a 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 * Opcode: SeekGt
1de4b 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a P1 P2 P3 P4 *.*
1de4c 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 *.** If cursor P
1de4d 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 1 refers to an S
1de4e 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 QL table (B-Tree
1de4f 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 that uses integ
1de50 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 er keys), .** us
1de51 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 e the value in r
1de52 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 egister P3 as a
1de53 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 key. If cursor P
1de54 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 1 refers .** to
1de55 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 an SQL index, th
1de56 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 en P3 is the fir
1de57 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f st in an array o
1de58 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a f P4 registers .
1de59 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 ** that are used
1de5a 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 as an unpacked
1de5b 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a index key. .**.*
1de5c 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 * Reposition cur
1de5d 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 sor P1 so that
1de5e 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
1de5f 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 smallest entry
1de60 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 that .** is grea
1de61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79 ter than the key
1de62 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 value. If there
1de63 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 are no records
1de64 67 72 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a greater than .**
1de65 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 the key and P2
1de66 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 is not zero, the
1de67 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1de68 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f .** See also: Fo
1de69 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 und, NotFound, D
1de6a 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c istinct, SeekLt,
1de6b 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a SeekGe, SeekLe.
1de6c 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 */./* Opcode: Se
1de6d 65 6b 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 ekLt P1 P2 P3 P4
1de6e 20 2a 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 * .**.** If cur
1de6f 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 74 6f sor P1 refers to
1de70 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 an SQL table (B
1de71 2d 54 72 65 65 20 74 68 61 74 20 75 73 65 73 20 -Tree that uses
1de72 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a integer keys), .
1de73 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 ** use the value
1de74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 in register P3
1de75 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 as a key. If cur
1de76 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a sor P1 refers .*
1de77 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 * to an SQL inde
1de78 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 x, then P3 is th
1de79 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 e first in an ar
1de7a 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 ray of P4 regist
1de7b 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 ers .** that are
1de7c 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 used as an unpa
1de7d 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 cked index key.
1de7e 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f .**.** Repositio
1de7f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 n cursor P1 so t
1de80 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 hat it points t
1de81 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e o the largest en
1de82 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 try that .** is
1de83 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 less than the ke
1de84 79 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 y value. If ther
1de85 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 e are no records
1de86 20 6c 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 less than .** t
1de87 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 he key and P2 is
1de88 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 not zero, then
1de89 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a jump to P2..**.*
1de8a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e * See also: Foun
1de8b 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 d, NotFound, Dis
1de8c 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 tinct, SeekGt, S
1de8d 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f eekGe, SeekLe.*/
1de8e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b ./* Opcode: Seek
1de8f 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a Le P1 P2 P3 P4 *
1de90 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 .**.** If cursor
1de91 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e P1 refers to an
1de92 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 SQL table (B-Tr
1de93 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 ee that uses int
1de94 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 eger keys), .**
1de95 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e use the value in
1de96 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20 register P3 as
1de97 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 a key. If cursor
1de98 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 P1 refers .** t
1de99 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 o an SQL index,
1de9a 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 then P3 is the f
1de9b 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 irst in an array
1de9c 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 of P4 registers
1de9d 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 .** that are us
1de9e 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 ed as an unpacke
1de9f 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a d index key. .**
1dea0 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 .** Reposition c
1dea1 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 ursor P1 so that
1dea2 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 it points to th
1dea3 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 e largest entry
1dea4 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 that .** is less
1dea5 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
1dea6 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e o the key value.
1dea7 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f If there are no
1dea8 20 72 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 records .** les
1dea9 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 s than or equal
1deaa 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 to the key and P
1deab 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 2 is not zero, t
1deac 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a hen jump to P2..
1dead 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1deae 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c Found, NotFound,
1deaf 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 Distinct, SeekG
1deb0 74 2c 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c t, SeekGe, SeekL
1deb1 74 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 t.*/.case OP_See
1deb2 6b 4c 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kLt: /*
1deb3 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1deb4 65 20 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 e OP_SeekLe:
1deb5 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1deb6 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 3 */.case OP_See
1deb7 6b 47 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 kGe: /*
1deb8 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 jump, in3 */.cas
1deb9 65 20 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 e OP_SeekGt: {
1deba 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e /* jump, in
1debb 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 3 */.#if 0 /* l
1debc 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1debd 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a oved into u.az *
1debe 2f 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 /. int res;. i
1debf 6e 74 20 6f 63 3b 0a 20 20 56 64 62 65 43 75 72 nt oc;. VdbeCur
1dec0 73 6f 72 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63 sor *pC;. Unpac
1dec1 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 69 kedRecord r;. i
1dec2 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34 nt nField;. i64
1dec3 20 69 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 iKey; /* T
1dec4 68 65 20 72 6f 77 69 64 20 77 65 20 61 72 65 20 he rowid we are
1dec5 74 6f 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65 to seek to */.#e
1dec6 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1dec7 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1dec8 74 6f 20 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73 to u.az */.. as
1dec9 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1deca 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1decb 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 Cursor );. asse
1decc 72 74 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 rt( pOp->p2!=0 )
1decd 3b 0a 20 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d ;. u.az.pC = p-
1dece 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1decf 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e . assert( u.az.
1ded0 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1ded1 74 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75 t( u.az.pC->pseu
1ded2 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b doTableReg==0 );
1ded3 0a 20 20 61 73 73 65 72 74 28 20 4f 50 5f 53 65 . assert( OP_Se
1ded4 65 6b 4c 65 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c ekLe == OP_SeekL
1ded5 74 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 t+1 );. assert(
1ded6 20 4f 50 5f 53 65 65 6b 47 65 20 3d 3d 20 4f 50 OP_SeekGe == OP
1ded7 5f 53 65 65 6b 4c 74 2b 32 20 29 3b 0a 20 20 61 _SeekLt+2 );. a
1ded8 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 47 74 ssert( OP_SeekGt
1ded9 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 33 20 == OP_SeekLt+3
1deda 29 3b 0a 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 );. if( u.az.pC
1dedb 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a ->pCursor!=0 ){.
1dedc 20 20 20 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f u.az.oc = pO
1dedd 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 75 p->opcode;. u
1dede 2e 61 7a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .az.pC->nullRow
1dedf 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 = 0;. if( u.a
1dee0 7a 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b z.pC->isTable ){
1dee1 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e . /* The in
1dee2 70 75 74 20 76 61 6c 75 65 20 69 6e 20 50 33 20 put value in P3
1dee3 6d 69 67 68 74 20 62 65 20 6f 66 20 61 6e 79 20 might be of any
1dee4 74 79 70 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 type: integer, r
1dee5 65 61 6c 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 eal, string,.
1dee6 20 20 20 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e ** blob, or N
1dee7 55 4c 4c 2e 20 20 42 75 74 20 69 74 20 6e 65 65 ULL. But it nee
1dee8 64 73 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 ds to be an inte
1dee9 67 65 72 20 62 65 66 6f 72 65 20 77 65 20 63 61 ger before we ca
1deea 6e 20 64 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 n do. ** th
1deeb 65 20 73 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 e seek, so cover
1deec 74 20 69 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 t it. */. p
1deed 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d In3 = &aMem[pOp-
1deee 3e 70 33 5d 3b 0a 20 20 20 20 20 20 61 70 70 6c >p3];. appl
1deef 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 yNumericAffinity
1def0 28 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e (pIn3);. u.
1def1 61 7a 2e 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 az.iKey = sqlite
1def2 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 3VdbeIntValue(pI
1def3 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e n3);. u.az.
1def4 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 pC->rowidIsValid
1def5 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 = 0;.. /*
1def6 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 If the P3 value
1def7 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e could not be con
1def8 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 verted into an i
1def9 6e 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20 nteger without.
1defa 20 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 ** loss of
1defb 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 information, the
1defc 6e 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 n special proces
1defd 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 sing is required
1defe 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ... */. if(
1deff 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 (pIn3->flags &
1df00 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 MEM_Int)==0 ){.
1df01 20 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 if( (pIn3
1df02 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 ->flags & MEM_Re
1df03 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 al)==0 ){.
1df04 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 /* If the P3
1df05 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 value cannot be
1df06 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 converted into
1df07 61 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 any kind of a nu
1df08 6d 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 mber,.
1df09 2a 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b ** then the seek
1df0a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 is not possible
1df0b 2c 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 , so jump to P2
1df0c 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 */. pc
1df0d 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1df0e 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1df0f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1df10 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 /* If we reach
1df11 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 this point, the
1df12 6e 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d n the P3 value m
1df13 75 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e ust be a floatin
1df14 67 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 g. ** poi
1df15 6e 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 nt number. */.
1df16 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 assert( (p
1df17 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d In3->flags & MEM
1df18 5f 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 _Real)!=0 );..
1df19 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 69 if( u.az.i
1df1a 4b 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e Key==SMALLEST_IN
1df1b 54 36 34 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c T64 && (pIn3->r<
1df1c 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 (double)u.az.iKe
1df1d 79 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 y || pIn3->r>0)
1df1e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ){. /*
1df1f 54 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 The P3 value is
1df20 74 6f 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 too large in mag
1df21 6e 69 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 nitude to be exp
1df22 72 65 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 ressed as an.
1df23 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 ** intege
1df24 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 r. */.
1df25 75 2e 61 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20 u.az.res = 1;.
1df26 20 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 if( pIn3
1df27 2d 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 ->r<0 ){.
1df28 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 if( u.az.oc
1df29 3e 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 20 20 >=OP_SeekGe ){
1df2a 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d assert( u.az.oc=
1df2b 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e =OP_SeekGe || u.
1df2c 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 az.oc==OP_SeekGt
1df2d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
1df2e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1df2f 72 65 65 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 reeFirst(u.az.pC
1df30 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a ->pCursor, &u.az
1df31 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 .res);.
1df32 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1df33 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 ITE_OK ) goto ab
1df34 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 ort_due_to_error
1df35 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
1df36 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
1df37 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
1df38 20 75 2e 61 7a 2e 6f 63 3c 3d 4f 50 5f 53 65 65 u.az.oc<=OP_See
1df39 6b 4c 65 20 29 7b 20 20 61 73 73 65 72 74 28 20 kLe ){ assert(
1df3a 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1df3b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f Lt || u.az.oc==O
1df3c 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 P_SeekLe );.
1df3d 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 rc = s
1df3e 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 qlite3BtreeLast(
1df3f 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.az.pC->pCursor
1df40 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 , &u.az.res);.
1df41 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 if(
1df42 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 rc!=SQLITE_OK )
1df43 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 goto abort_due_t
1df44 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 o_error;.
1df45 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1df46 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 }. if(
1df47 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 u.az.res ){.
1df48 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70 4f pc = pO
1df49 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 p->p2 - 1;.
1df4a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
1df4b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
1df4c 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f }else if( u.az.o
1df4d 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 c==OP_SeekLt ||
1df4e 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b u.az.oc==OP_Seek
1df4f 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 Ge ){.
1df50 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 6c 69 /* Use the ceili
1df51 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f ng() function to
1df52 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 convert real->i
1df53 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 nt */.
1df54 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 if( pIn3->r > (d
1df55 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 ouble)u.az.iKey
1df56 29 20 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 ) u.az.iKey++;.
1df57 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
1df58 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 74 /* Use t
1df59 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 he floor() funct
1df5a 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 ion to convert r
1df5b 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 eal->int */.
1df5c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1df5d 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 az.oc==OP_SeekLe
1df5e 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f || u.az.oc==OP_
1df5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 SeekGt );.
1df60 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 if( pIn3->r
1df61 3c 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 < (double)u.az.i
1df62 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d Key ) u.az.iKey-
1df63 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 -;. }.
1df64 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 }. rc =
1df65 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 sqlite3BtreeMove
1df66 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e toUnpacked(u.az.
1df67 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 pC->pCursor, 0,
1df68 28 75 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 (u64)u.az.iKey,
1df69 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 0, &u.az.res);.
1df6a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c if( rc!=SQL
1df6b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 ITE_OK ){.
1df6c 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 goto abort_due
1df6d 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 _to_error;.
1df6e 20 7d 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 }. if( u.a
1df6f 7a 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 z.res==0 ){.
1df70 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 u.az.pC->row
1df71 69 64 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 idIsValid = 1;.
1df72 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e u.az.pC->
1df73 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a lastRowid = u.az
1df74 2e 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20 .iKey;. }.
1df75 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1df76 75 2e 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f u.az.nField = pO
1df77 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 p->p4.i;. a
1df78 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1df79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a pe==P4_INT32 );.
1df7a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1df7b 61 7a 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20 az.nField>0 );.
1df7c 20 20 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 u.az.r.pKey
1df7d 49 6e 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e Info = u.az.pC->
1df7e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 pKeyInfo;.
1df7f 75 2e 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 u.az.r.nField =
1df80 28 75 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 (u16)u.az.nField
1df81 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ;.. /* The
1df82 6e 65 78 74 20 6c 69 6e 65 20 6f 66 20 63 6f 64 next line of cod
1df83 65 20 63 6f 6d 70 75 74 65 73 20 61 73 20 66 6f e computes as fo
1df84 6c 6c 6f 77 73 2c 20 6f 6e 6c 79 20 66 61 73 74 llows, only fast
1df85 65 72 3a 0a 20 20 20 20 20 20 2a 2a 20 20 20 69 er:. ** i
1df86 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 f( u.az.oc==OP_S
1df87 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 eekGt || u.az.oc
1df88 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 ==OP_SeekLe ){.
1df89 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a ** u.az
1df8a 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 .r.flags = UNPAC
1df8b 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 KED_INCRKEY;.
1df8c 20 20 20 2a 2a 20 20 20 7d 65 6c 73 65 7b 0a 20 ** }else{.
1df8d 20 20 20 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a ** u.az
1df8e 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 .r.flags = 0;.
1df8f 20 20 20 20 2a 2a 20 20 20 7d 0a 20 20 20 20 20 ** }.
1df90 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 */. u.az.r
1df91 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 .flags = UNPACKE
1df92 44 5f 49 4e 43 52 4b 45 59 20 2a 20 28 31 20 26 D_INCRKEY * (1 &
1df93 20 28 75 2e 61 7a 2e 6f 63 20 2d 20 4f 50 5f 53 (u.az.oc - OP_S
1df94 65 65 6b 4c 74 29 29 3b 0a 20 20 20 20 20 20 61 eekLt));. a
1df95 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 21 3d ssert( u.az.oc!=
1df96 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e 61 OP_SeekGt || u.a
1df97 7a 2e 72 2e 66 6c 61 67 73 3d 3d 55 4e 50 41 43 z.r.flags==UNPAC
1df98 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 3b 0a 20 KED_INCRKEY );.
1df99 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 assert( u.a
1df9a 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 4c 65 20 z.oc!=OP_SeekLe
1df9b 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d || u.az.r.flags=
1df9c 3d 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 =UNPACKED_INCRKE
1df9d 59 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 Y );. asser
1df9e 74 28 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f 53 t( u.az.oc!=OP_S
1df9f 65 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e 72 2e eekGe || u.az.r.
1dfa0 66 6c 61 67 73 3d 3d 30 20 29 3b 0a 20 20 20 20 flags==0 );.
1dfa1 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f assert( u.az.o
1dfa2 63 21 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 c!=OP_SeekLt ||
1dfa3 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d 3d 30 20 u.az.r.flags==0
1dfa4 29 3b 0a 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 );.. u.az.r
1dfa5 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f .aMem = &aMem[pO
1dfa6 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 45 78 p->p3];. Ex
1dfa7 70 61 6e 64 42 6c 6f 62 28 75 2e 61 7a 2e 72 2e pandBlob(u.az.r.
1dfa8 61 4d 65 6d 29 3b 0a 20 20 20 20 20 20 72 63 20 aMem);. rc
1dfa9 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1dfaa 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 vetoUnpacked(u.a
1dfab 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 z.pC->pCursor, &
1dfac 75 2e 61 7a 2e 72 2c 20 30 2c 20 30 2c 20 26 75 u.az.r, 0, 0, &u
1dfad 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 .az.res);.
1dfae 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f if( rc!=SQLITE_O
1dfaf 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 K ){. got
1dfb0 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 o abort_due_to_e
1dfb1 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rror;. }.
1dfb2 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 u.az.pC->row
1dfb3 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 idIsValid = 0;.
1dfb4 20 20 20 7d 0a 20 20 20 20 75 2e 61 7a 2e 70 43 }. u.az.pC
1dfb5 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f ->deferredMoveto
1dfb6 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 7a 2e 70 = 0;. u.az.p
1dfb7 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d C->cacheStatus =
1dfb8 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23 69 CACHE_STALE;.#i
1dfb9 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 fdef SQLITE_TEST
1dfba 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65 61 . sqlite3_sea
1dfbb 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e rch_count++;.#en
1dfbc 64 69 66 0a 20 20 20 20 69 66 28 20 75 2e 61 7a dif. if( u.az
1dfbd 2e 6f 63 3e 3d 4f 50 5f 53 65 65 6b 47 65 20 29 .oc>=OP_SeekGe )
1dfbe 7b 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e { assert( u.az.
1dfbf 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 7c 7c oc==OP_SeekGe ||
1dfc0 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 u.az.oc==OP_See
1dfc1 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 kGt );. if(
1dfc2 20 75 2e 61 7a 2e 72 65 73 3c 30 20 7c 7c 20 28 u.az.res<0 || (
1dfc3 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 u.az.res==0 && u
1dfc4 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 .az.oc==OP_SeekG
1dfc5 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 t) ){. rc
1dfc6 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e = sqlite3BtreeN
1dfc7 65 78 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 ext(u.az.pC->pCu
1dfc8 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 rsor, &u.az.res)
1dfc9 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 ;. if( rc
1dfca 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f !=SQLITE_OK ) go
1dfcb 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1dfcc 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 75 error;. u
1dfcd 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 .az.pC->rowidIsV
1dfce 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 alid = 0;.
1dfcf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 }else{. u
1dfd0 2e 61 7a 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 .az.res = 0;.
1dfd1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
1dfd2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e assert( u.
1dfd3 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 az.oc==OP_SeekLt
1dfd4 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f || u.az.oc==OP_
1dfd5 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 SeekLe );.
1dfd6 69 66 28 20 75 2e 61 7a 2e 72 65 73 3e 30 20 7c if( u.az.res>0 |
1dfd7 7c 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 | (u.az.res==0 &
1dfd8 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 & u.az.oc==OP_Se
1dfd9 65 6b 4c 74 29 20 29 7b 0a 20 20 20 20 20 20 20 ekLt) ){.
1dfda 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1dfdb 65 65 50 72 65 76 69 6f 75 73 28 75 2e 61 7a 2e eePrevious(u.az.
1dfdc 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e pC->pCursor, &u.
1dfdd 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 az.res);.
1dfde 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1dfdf 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f OK ) goto abort_
1dfe0 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 due_to_error;.
1dfe1 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 u.az.pC->r
1dfe2 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b owidIsValid = 0;
1dfe3 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 . }else{.
1dfe4 20 20 20 20 20 20 2f 2a 20 75 2e 61 7a 2e 72 65 /* u.az.re
1dfe5 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67 61 74 s might be negat
1dfe6 69 76 65 20 62 65 63 61 75 73 65 20 74 68 65 20 ive because the
1dfe7 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 20 table is empty.
1dfe8 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20 20 20 Check to.
1dfe9 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68 69 73 ** see if this
1dfea 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 is the case..
1dfeb 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
1dfec 20 75 2e 61 7a 2e 72 65 73 20 3d 20 73 71 6c 69 u.az.res = sqli
1dfed 74 65 33 42 74 72 65 65 45 6f 66 28 75 2e 61 7a te3BtreeEof(u.az
1dfee 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 .pC->pCursor);.
1dfef 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1dff0 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 assert( pOp->p2
1dff1 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 75 2e >0 );. if( u.
1dff2 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 az.res ){.
1dff3 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1dff4 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b ;. }. }else{
1dff5 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 . /* This hap
1dff6 70 65 6e 73 20 77 68 65 6e 20 61 74 74 65 6d 70 pens when attemp
1dff7 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 ting to open the
1dff8 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 sqlite3_master
1dff9 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 table. ** for
1dffa 20 72 65 61 64 20 61 63 63 65 73 73 20 72 65 74 read access ret
1dffb 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 urns SQLITE_EMPT
1dffc 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 Y. In this case
1dffd 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 always. ** ta
1dffe 6b 65 20 74 68 65 20 6a 75 6d 70 20 28 73 69 6e ke the jump (sin
1dfff 63 65 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 ce there are no
1e000 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 records in the t
1e001 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 able).. */.
1e002 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d pc = pOp->p2 -
1e003 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 1;. }. break;
1e004 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 .}../* Opcode: S
1e005 65 65 6b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a eek P1 P2 * * *.
1e006 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 6f **.** P1 is an o
1e007 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73 6f 72 pen table cursor
1e008 20 61 6e 64 20 50 32 20 69 73 20 61 20 72 6f 77 and P2 is a row
1e009 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41 72 72 id integer. Arr
1e00a 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31 20 74 ange.** for P1 t
1e00b 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74 20 69 o move so that i
1e00c 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 t points to the
1e00d 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79 20 50 rowid given by P
1e00e 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 2..**.** This is
1e00f 20 61 63 74 75 61 6c 6c 79 20 61 20 64 65 66 65 actually a defe
1e010 72 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f 74 68 rred seek. Noth
1e011 69 6e 67 20 61 63 74 75 61 6c 6c 79 20 68 61 70 ing actually hap
1e012 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 pens until.** th
1e013 65 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 e cursor is used
1e014 20 74 6f 20 72 65 61 64 20 61 20 72 65 63 6f 72 to read a recor
1e015 64 2e 20 20 54 68 61 74 20 77 61 79 2c 20 69 66 d. That way, if
1e016 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f 63 63 no reads.** occ
1e017 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73 73 61 ur, no unnecessa
1e018 72 79 20 49 2f 4f 20 68 61 70 70 65 6e 73 2e 0a ry I/O happens..
1e019 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 3a */.case OP_Seek:
1e01a 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a { /* in2 */.
1e01b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1e01c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e01d 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20 20 56 into u.ba */. V
1e01e 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 dbeCursor *pC;.#
1e01f 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 endif /* local v
1e020 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e021 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 20 20 61 nto u.ba */.. a
1e022 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d ssert( pOp->p1>=
1e023 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 0 && pOp->p1<p->
1e024 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 nCursor );. u.b
1e025 61 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b a.pC = p->apCsr[
1e026 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 pOp->p1];. asse
1e027 72 74 28 20 75 2e 62 61 2e 70 43 21 3d 30 20 29 rt( u.ba.pC!=0 )
1e028 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 ;. if( ALWAYS(u
1e029 2e 62 61 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 .ba.pC->pCursor!
1e02a 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 =0) ){. asser
1e02b 74 28 20 75 2e 62 61 2e 70 43 2d 3e 69 73 54 61 t( u.ba.pC->isTa
1e02c 62 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62 61 2e ble );. u.ba.
1e02d 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b pC->nullRow = 0;
1e02e 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 . pIn2 = &aMe
1e02f 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 m[pOp->p2];.
1e030 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 u.ba.pC->movetoT
1e031 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56 arget = sqlite3V
1e032 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 dbeIntValue(pIn2
1e033 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e );. u.ba.pC->
1e034 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1e035 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 64 ;. u.ba.pC->d
1e036 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1e037 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 1;. }. break;.
1e038 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a }. ../* Opcode:
1e039 20 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20 Found P1 P2 P3
1e03a 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 P4 *.**.** If P4
1e03b 3d 3d 30 20 74 68 65 6e 20 72 65 67 69 73 74 65 ==0 then registe
1e03c 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f r P3 holds a blo
1e03d 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 b constructed by
1e03e 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 49 66 MakeRecord. If
1e03f 0a 2a 2a 20 50 34 3e 30 20 74 68 65 6e 20 72 65 .** P4>0 then re
1e040 67 69 73 74 65 72 20 50 33 20 69 73 20 74 68 65 gister P3 is the
1e041 20 66 69 72 73 74 20 6f 66 20 50 34 20 72 65 67 first of P4 reg
1e042 69 73 74 65 72 73 20 74 68 61 74 20 66 6f 72 6d isters that form
1e043 20 61 6e 20 75 6e 70 61 63 6b 65 64 0a 2a 2a 20 an unpacked.**
1e044 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 43 75 record..**.** Cu
1e045 72 73 6f 72 20 50 31 20 69 73 20 6f 6e 20 61 6e rsor P1 is on an
1e046 20 69 6e 64 65 78 20 62 74 72 65 65 2e 20 20 49 index btree. I
1e047 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 64 65 f the record ide
1e048 6e 74 69 66 69 65 64 20 62 79 20 50 33 20 61 6e ntified by P3 an
1e049 64 20 50 34 0a 2a 2a 20 69 73 20 61 20 70 72 65 d P4.** is a pre
1e04a 66 69 78 20 6f 66 20 61 6e 79 20 65 6e 74 72 79 fix of any entry
1e04b 20 69 6e 20 50 31 20 74 68 65 6e 20 61 20 6a 75 in P1 then a ju
1e04c 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32 mp is made to P2
1e04d 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 6c 65 and.** P1 is le
1e04e 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 ft pointing at t
1e04f 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 he matching entr
1e050 79 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a y..*/./* Opcode:
1e051 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32 20 NotFound P1 P2
1e052 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 P3 P4 *.**.** If
1e053 20 50 34 3d 3d 30 20 74 68 65 6e 20 72 65 67 69 P4==0 then regi
1e054 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 ster P3 holds a
1e055 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 blob constructed
1e056 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 by MakeRecord.
1e057 20 49 66 0a 2a 2a 20 50 34 3e 30 20 74 68 65 6e If.** P4>0 then
1e058 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 register P3 is
1e059 74 68 65 20 66 69 72 73 74 20 6f 66 20 50 34 20 the first of P4
1e05a 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20 66 registers that f
1e05b 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 0a orm an unpacked.
1e05c 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 0a 2a ** record..** .*
1e05d 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f * Cursor P1 is o
1e05e 6e 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 65 n an index btree
1e05f 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 . If the record
1e060 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 50 identified by P
1e061 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69 73 20 6e 3 and P4.** is n
1e062 6f 74 20 74 68 65 20 70 72 65 66 69 78 20 6f 66 ot the prefix of
1e063 20 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 any entry in P1
1e064 20 74 68 65 6e 20 61 20 6a 75 6d 70 20 69 73 20 then a jump is
1e065 6d 61 64 65 20 74 6f 20 50 32 2e 20 20 49 66 20 made to P2. If
1e066 50 31 20 0a 2a 2a 20 64 6f 65 73 20 63 6f 6e 74 P1 .** does cont
1e067 61 69 6e 20 61 6e 20 65 6e 74 72 79 20 77 68 6f ain an entry who
1e068 73 65 20 70 72 65 66 69 78 20 6d 61 74 63 68 65 se prefix matche
1e069 73 20 74 68 65 20 50 33 2f 50 34 20 72 65 63 6f s the P3/P4 reco
1e06a 72 64 20 74 68 65 6e 20 63 6f 6e 74 72 6f 6c 0a rd then control.
1e06b 2a 2a 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 ** falls through
1e06c 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 to the next ins
1e06d 74 72 75 63 74 69 6f 6e 20 61 6e 64 20 50 31 20 truction and P1
1e06e 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 is left pointing
1e06f 20 61 74 20 74 68 65 0a 2a 2a 20 6d 61 74 63 68 at the.** match
1e070 69 6e 67 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a ing entry..**.**
1e071 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 See also: Found
1e072 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 49 73 55 , NotExists, IsU
1e073 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 nique.*/.case OP
1e074 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20 20 20 20 20 _NotFound:
1e075 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f /* jump, in3 */
1e076 0a 63 61 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20 .case OP_Found:
1e077 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 { /* jump
1e078 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 , in3 */.#if 0
1e079 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e07a 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e07b 62 62 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 65 bb */. int alre
1e07c 61 64 79 45 78 69 73 74 73 3b 0a 20 20 56 64 62 adyExists;. Vdb
1e07d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 eCursor *pC;. i
1e07e 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b nt res;. Unpack
1e07f 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 edRecord *pIdxKe
1e080 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 y;. UnpackedRec
1e081 6f 72 64 20 72 3b 0a 20 20 63 68 61 72 20 61 54 ord r;. char aT
1e082 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69 empRec[ROUND8(si
1e083 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 zeof(UnpackedRec
1e084 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d ord)) + sizeof(M
1e085 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 23 65 6e 64 em)*3 + 7];.#end
1e086 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e087 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e088 20 75 2e 62 62 20 2a 2f 0a 0a 23 69 66 64 65 66 u.bb */..#ifdef
1e089 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 SQLITE_TEST. s
1e08a 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75 qlite3_found_cou
1e08b 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 nt++;.#endif..
1e08c 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 u.bb.alreadyExis
1e08d 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 ts = 0;. assert
1e08e 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e08f 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e090 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 or );. assert(
1e091 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f pOp->p4type==P4_
1e092 49 4e 54 33 32 20 29 3b 0a 20 20 75 2e 62 62 2e INT32 );. u.bb.
1e093 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f pC = p->apCsr[pO
1e094 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e095 28 20 75 2e 62 62 2e 70 43 21 3d 30 20 29 3b 0a ( u.bb.pC!=0 );.
1e096 20 20 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 pIn3 = &aMem[p
1e097 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 41 Op->p3];. if( A
1e098 4c 57 41 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 LWAYS(u.bb.pC->p
1e099 43 75 72 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 Cursor!=0) ){..
1e09a 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 62 2e assert( u.bb.
1e09b 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 pC->isTable==0 )
1e09c 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 ;. if( pOp->p
1e09d 34 2e 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 75 4.i>0 ){. u
1e09e 2e 62 62 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d .bb.r.pKeyInfo =
1e09f 20 75 2e 62 62 2e 70 43 2d 3e 70 4b 65 79 49 6e u.bb.pC->pKeyIn
1e0a0 66 6f 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 72 fo;. u.bb.r
1e0a1 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 .nField = pOp->p
1e0a2 34 2e 69 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 4.i;. u.bb.
1e0a3 72 2e 61 4d 65 6d 20 3d 20 70 49 6e 33 3b 0a 20 r.aMem = pIn3;.
1e0a4 20 20 20 20 20 75 2e 62 62 2e 72 2e 66 6c 61 67 u.bb.r.flag
1e0a5 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 s = UNPACKED_PRE
1e0a6 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20 20 FIX_MATCH;.
1e0a7 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20 u.bb.pIdxKey =
1e0a8 26 75 2e 62 62 2e 72 3b 0a 20 20 20 20 7d 65 6c &u.bb.r;. }el
1e0a9 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 se{. assert
1e0aa 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 ( pIn3->flags &
1e0ab 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 MEM_Blob );.
1e0ac 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e ExpandBlob(pIn
1e0ad 33 29 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 70 3);. u.bb.p
1e0ae 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 IdxKey = sqlite3
1e0af 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b VdbeRecordUnpack
1e0b0 28 75 2e 62 62 2e 70 43 2d 3e 70 4b 65 79 49 6e (u.bb.pC->pKeyIn
1e0b1 66 6f 2c 20 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e fo, pIn3->n, pIn
1e0b2 33 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20 3->z,.
1e0b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e0b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 2e u.
1e0b5 62 62 2e 61 54 65 6d 70 52 65 63 2c 20 73 69 7a bb.aTempRec, siz
1e0b6 65 6f 66 28 75 2e 62 62 2e 61 54 65 6d 70 52 65 eof(u.bb.aTempRe
1e0b7 63 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 c));. if( u
1e0b8 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d 30 20 29 .bb.pIdxKey==0 )
1e0b9 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6e {. goto n
1e0ba 6f 5f 6d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 o_mem;. }.
1e0bb 20 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 u.bb.pIdxKe
1e0bc 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 y->flags |= UNPA
1e0bd 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 CKED_PREFIX_MATC
1e0be 48 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 H;. }. rc
1e0bf 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f = sqlite3BtreeMo
1e0c0 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 vetoUnpacked(u.b
1e0c1 62 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 b.pC->pCursor, u
1e0c2 2e 62 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 .bb.pIdxKey, 0,
1e0c3 30 2c 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20 0, &u.bb.res);.
1e0c4 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 69 if( pOp->p4.i
1e0c5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ==0 ){. sql
1e0c6 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e ite3VdbeDeleteUn
1e0c7 70 61 63 6b 65 64 52 65 63 6f 72 64 28 75 2e 62 packedRecord(u.b
1e0c8 62 2e 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 b.pIdxKey);.
1e0c9 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 }. if( rc!=SQ
1e0ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 LITE_OK ){.
1e0cb 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
1e0cc 20 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 u.bb.alreadyEx
1e0cd 69 73 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73 ists = (u.bb.res
1e0ce 3d 3d 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 ==0);. u.bb.p
1e0cf 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e0d0 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e o = 0;. u.bb.
1e0d1 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e0d2 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1e0d3 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 }. if( pOp->op
1e0d4 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 code==OP_Found )
1e0d5 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61 {. if( u.bb.a
1e0d6 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70 lreadyExists ) p
1e0d7 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1e0d8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1e0d9 28 20 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 ( !u.bb.alreadyE
1e0da 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 xists ) pc = pOp
1e0db 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 ->p2 - 1;. }.
1e0dc 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1e0dd 6f 64 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 ode: IsUnique P1
1e0de 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a P2 P3 P4 *.**.*
1e0df 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f * Cursor P1 is o
1e0e0 70 65 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 pen on an index
1e0e1 62 2d 74 72 65 65 20 2d 20 74 68 61 74 20 69 73 b-tree - that is
1e0e2 20 74 6f 20 73 61 79 2c 20 61 20 62 74 72 65 65 to say, a btree
1e0e3 20 77 68 69 63 68 0a 2a 2a 20 6e 6f 20 64 61 74 which.** no dat
1e0e4 61 20 61 6e 64 20 77 68 65 72 65 20 74 68 65 20 a and where the
1e0e5 6b 65 79 20 61 72 65 20 72 65 63 6f 72 64 73 20 key are records
1e0e6 67 65 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f generated by OP_
1e0e7 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 0a MakeRecord with.
1e0e8 2a 2a 20 74 68 65 20 6c 69 73 74 20 66 69 65 6c ** the list fiel
1e0e9 64 20 62 65 69 6e 67 20 74 68 65 20 69 6e 74 65 d being the inte
1e0ea 67 65 72 20 52 4f 57 49 44 20 6f 66 20 74 68 65 ger ROWID of the
1e0eb 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 entry that the
1e0ec 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 72 index.** entry r
1e0ed 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 efers to..**.**
1e0ee 54 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20 The P3 register
1e0ef 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 74 65 contains an inte
1e0f0 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 ger record numbe
1e0f1 72 2e 20 43 61 6c 6c 20 74 68 69 73 20 72 65 63 r. Call this rec
1e0f2 6f 72 64 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 ord .** number R
1e0f3 2e 20 52 65 67 69 73 74 65 72 20 50 34 20 69 73 . Register P4 is
1e0f4 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 the first in a
1e0f5 73 65 74 20 6f 66 20 4e 20 63 6f 6e 74 69 67 75 set of N contigu
1e0f6 6f 75 73 20 72 65 67 69 73 74 65 72 73 0a 2a 2a ous registers.**
1e0f7 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61 6e that make up an
1e0f8 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 unpacked index
1e0f9 6b 65 79 20 74 68 61 74 20 63 61 6e 20 62 65 20 key that can be
1e0fa 75 73 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 used with cursor
1e0fb 20 50 31 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 P1..** The valu
1e0fc 65 20 6f 66 20 4e 20 63 61 6e 20 62 65 20 69 6e e of N can be in
1e0fd 66 65 72 72 65 64 20 66 72 6f 6d 20 74 68 65 20 ferred from the
1e0fe 63 75 72 73 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 cursor. N includ
1e0ff 65 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 es the rowid.**
1e100 76 61 6c 75 65 20 61 70 70 65 6e 64 65 64 20 74 value appended t
1e101 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 o the end of the
1e102 20 69 6e 64 65 78 20 72 65 63 6f 72 64 2e 20 54 index record. T
1e103 68 69 73 20 72 6f 77 69 64 20 76 61 6c 75 65 20 his rowid value
1e104 6d 61 79 0a 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f may.** or may no
1e105 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 t be the same as
1e106 20 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 R..**.** If any
1e107 20 6f 66 20 74 68 65 20 4e 20 72 65 67 69 73 74 of the N regist
1e108 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 ers beginning wi
1e109 74 68 20 72 65 67 69 73 74 65 72 20 50 34 20 63 th register P4 c
1e10a 6f 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a ontains a NULL.*
1e10b 2a 20 76 61 6c 75 65 2c 20 6a 75 6d 70 20 69 6d * value, jump im
1e10c 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e mediately to P2.
1e10d 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 .**.** Otherwise
1e10e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 , this instructi
1e10f 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 63 75 72 on checks if cur
1e110 73 6f 72 20 50 31 20 63 6f 6e 74 61 69 6e 73 20 sor P1 contains
1e111 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 77 68 65 72 an entry.** wher
1e112 65 20 74 68 65 20 66 69 72 73 74 20 28 4e 2d 31 e the first (N-1
1e113 29 20 66 69 65 6c 64 73 20 6d 61 74 63 68 20 62 ) fields match b
1e114 75 74 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c ut the rowid val
1e115 75 65 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a ue at the end.**
1e116 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e of the index en
1e117 74 72 79 20 69 73 20 6e 6f 74 20 52 2e 20 49 66 try is not R. If
1e118 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 there is no suc
1e119 68 20 65 6e 74 72 79 2c 20 63 6f 6e 74 72 6f 6c h entry, control
1e11a 20 6a 75 6d 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 jumps.** to ins
1e11b 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 truction P2. Oth
1e11c 65 72 77 69 73 65 2c 20 74 68 65 20 72 6f 77 69 erwise, the rowi
1e11d 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 d of the conflic
1e11e 74 69 6e 67 20 69 6e 64 65 78 0a 2a 2a 20 65 6e ting index.** en
1e11f 74 72 79 20 69 73 20 63 6f 70 69 65 64 20 74 6f try is copied to
1e120 20 72 65 67 69 73 74 65 72 20 50 33 20 61 6e 64 register P3 and
1e121 20 63 6f 6e 74 72 6f 6c 20 66 61 6c 6c 73 20 74 control falls t
1e122 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 hrough to the ne
1e123 78 74 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f xt.** instructio
1e124 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 n..**.** See als
1e125 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 o: NotFound, Not
1e126 45 78 69 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f Exists, Found.*/
1e127 0a 63 61 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 .case OP_IsUniqu
1e128 65 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a e: { /* j
1e129 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 ump, in3 */.#if
1e12a 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e12b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e12c 20 75 2e 62 63 20 2a 2f 0a 20 20 75 31 36 20 69 u.bc */. u16 i
1e12d 69 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 i;. VdbeCursor
1e12e 2a 70 43 78 3b 0a 20 20 42 74 43 75 72 73 6f 72 *pCx;. BtCursor
1e12f 20 2a 70 43 72 73 72 3b 0a 20 20 75 31 36 20 6e *pCrsr;. u16 n
1e130 46 69 65 6c 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d Field;. Mem *aM
1e131 78 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 x;. UnpackedRec
1e132 6f 72 64 20 72 3b 20 20 20 20 20 20 20 20 20 20 ord r;
1e133 20 20 20 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 /* B-Tre
1e134 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 6b e index search k
1e135 65 79 20 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20 ey */. i64 R;
1e136 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e137 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f /* Ro
1e138 77 69 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 wid stored in re
1e139 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e gister P3 */.#en
1e13a 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e13b 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e13c 6f 20 75 2e 62 63 20 2a 2f 0a 0a 20 20 70 49 6e o u.bc */.. pIn
1e13d 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 3 = &aMem[pOp->p
1e13e 33 5d 3b 0a 20 20 75 2e 62 63 2e 61 4d 78 20 3d 3];. u.bc.aMx =
1e13f 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e 69 &aMem[pOp->p4.i
1e140 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 ];. /* Assert t
1e141 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 6f hat the values o
1e142 66 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 20 f parameters P1
1e143 61 6e 64 20 50 34 20 61 72 65 20 69 6e 20 72 61 and P4 are in ra
1e144 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 nge. */. assert
1e145 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 ( pOp->p4type==P
1e146 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 4_INT32 );. ass
1e147 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 3e 30 ert( pOp->p4.i>0
1e148 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d 70 && pOp->p4.i<=p
1e149 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 ->nMem );. asse
1e14a 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e14b 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e14c 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 rsor );.. /* Fi
1e14d 6e 64 20 74 68 65 20 69 6e 64 65 78 20 63 75 72 nd the index cur
1e14e 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 70 sor. */. u.bc.p
1e14f 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f Cx = p->apCsr[pO
1e150 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1e151 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 65 66 65 ( u.bc.pCx->defe
1e152 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1e153 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e 73 65 65 . u.bc.pCx->see
1e154 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 75 kResult = 0;. u
1e155 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 65 53 74 .bc.pCx->cacheSt
1e156 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 atus = CACHE_STA
1e157 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 72 73 72 LE;. u.bc.pCrsr
1e158 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 43 75 = u.bc.pCx->pCu
1e159 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 rsor;.. /* If a
1e15a 6e 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 ny of the values
1e15b 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65 20 are NULL, take
1e15c 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20 75 the jump. */. u
1e15d 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 62 .bc.nField = u.b
1e15e 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d c.pCx->pKeyInfo-
1e15f 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 28 75 >nField;. for(u
1e160 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 63 2e 69 .bc.ii=0; u.bc.i
1e161 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 3b 20 75 i<u.bc.nField; u
1e162 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 20 20 69 .bc.ii++){. i
1e163 66 28 20 75 2e 62 63 2e 61 4d 78 5b 75 2e 62 63 f( u.bc.aMx[u.bc
1e164 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d .ii].flags & MEM
1e165 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 _Null ){. p
1e166 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1e167 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43 72 73 . u.bc.pCrs
1e168 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 r = 0;. bre
1e169 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
1e16a 61 73 73 65 72 74 28 20 28 75 2e 62 63 2e 61 4d assert( (u.bc.aM
1e16b 78 5b 75 2e 62 63 2e 6e 46 69 65 6c 64 5d 2e 66 x[u.bc.nField].f
1e16c 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 lags & MEM_Null)
1e16d 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 75 2e ==0 );.. if( u.
1e16e 62 63 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20 bc.pCrsr!=0 ){.
1e16f 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 /* Populate t
1e170 68 65 20 69 6e 64 65 78 20 73 65 61 72 63 68 20 he index search
1e171 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 75 2e 62 63 key. */. u.bc
1e172 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e .r.pKeyInfo = u.
1e173 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f bc.pCx->pKeyInfo
1e174 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 6e 46 69 ;. u.bc.r.nFi
1e175 65 6c 64 20 3d 20 75 2e 62 63 2e 6e 46 69 65 6c eld = u.bc.nFiel
1e176 64 20 2b 20 31 3b 0a 20 20 20 20 75 2e 62 63 2e d + 1;. u.bc.
1e177 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b r.flags = UNPACK
1e178 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 ED_PREFIX_SEARCH
1e179 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 61 4d 65 ;. u.bc.r.aMe
1e17a 6d 20 3d 20 75 2e 62 63 2e 61 4d 78 3b 0a 0a 20 m = u.bc.aMx;..
1e17b 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 /* Extract th
1e17c 65 20 76 61 6c 75 65 20 6f 66 20 75 2e 62 63 2e e value of u.bc.
1e17d 52 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 R from register
1e17e 50 33 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 P3. */. sqlit
1e17f 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 e3VdbeMemInteger
1e180 69 66 79 28 70 49 6e 33 29 3b 0a 20 20 20 20 75 ify(pIn3);. u
1e181 2e 62 63 2e 52 20 3d 20 70 49 6e 33 2d 3e 75 2e .bc.R = pIn3->u.
1e182 69 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 i;.. /* Searc
1e183 68 20 74 68 65 20 42 2d 54 72 65 65 20 69 6e 64 h the B-Tree ind
1e184 65 78 2e 20 49 66 20 6e 6f 20 63 6f 6e 66 6c 69 ex. If no confli
1e185 63 74 69 6e 67 20 72 65 63 6f 72 64 20 69 73 20 cting record is
1e186 66 6f 75 6e 64 2c 20 6a 75 6d 70 0a 20 20 20 20 found, jump.
1e187 2a 2a 20 74 6f 20 50 32 2e 20 4f 74 68 65 72 77 ** to P2. Otherw
1e188 69 73 65 2c 20 63 6f 70 79 20 74 68 65 20 72 6f ise, copy the ro
1e189 77 69 64 20 6f 66 20 74 68 65 20 63 6f 6e 66 6c wid of the confl
1e18a 69 63 74 69 6e 67 20 72 65 63 6f 72 64 20 74 6f icting record to
1e18b 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65 72 . ** register
1e18c 20 50 33 20 61 6e 64 20 66 61 6c 6c 20 74 68 72 P3 and fall thr
1e18d 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 ough to the next
1e18e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 2a instruction. *
1e18f 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 /. rc = sqlit
1e190 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 e3BtreeMovetoUnp
1e191 61 63 6b 65 64 28 75 2e 62 63 2e 70 43 72 73 72 acked(u.bc.pCrsr
1e192 2c 20 26 75 2e 62 63 2e 72 2c 20 30 2c 20 30 2c , &u.bc.r, 0, 0,
1e193 20 26 75 2e 62 63 2e 70 43 78 2d 3e 73 65 65 6b &u.bc.pCx->seek
1e194 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69 66 28 Result);. if(
1e195 20 28 75 2e 62 63 2e 72 2e 66 6c 61 67 73 20 26 (u.bc.r.flags &
1e196 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 UNPACKED_PREFIX
1e197 5f 53 45 41 52 43 48 29 20 7c 7c 20 75 2e 62 63 _SEARCH) || u.bc
1e198 2e 72 2e 72 6f 77 69 64 3d 3d 75 2e 62 63 2e 52 .r.rowid==u.bc.R
1e199 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 ){. pc = p
1e19a 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 Op->p2 - 1;.
1e19b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e }else{. pIn
1e19c 33 2d 3e 75 2e 69 20 3d 20 75 2e 62 63 2e 72 2e 3->u.i = u.bc.r.
1e19d 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 7d rowid;. }. }
1e19e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1e19f 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 69 73 74 Opcode: NotExist
1e1a0 73 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a s P1 P2 P3 * *.*
1e1a1 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 63 6f 6e *.** Use the con
1e1a2 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 tent of register
1e1a3 20 50 33 20 61 73 20 61 20 69 6e 74 65 67 65 72 P3 as a integer
1e1a4 20 6b 65 79 2e 20 20 49 66 20 61 20 72 65 63 6f key. If a reco
1e1a5 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 rd .** with that
1e1a6 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 65 78 key does not ex
1e1a7 69 73 74 20 69 6e 20 74 61 62 6c 65 20 6f 66 20 ist in table of
1e1a8 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f P1, then jump to
1e1a9 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20 P2. .** If the
1e1aa 72 65 63 6f 72 64 20 64 6f 65 73 20 65 78 69 73 record does exis
1e1ab 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 t, then fall thr
1e1ac 75 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 u. The cursor i
1e1ad 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69 6e 74 s left .** point
1e1ae 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63 6f 72 ing to the recor
1e1af 64 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a d if it exists..
1e1b0 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 **.** The differ
1e1b1 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 ence between thi
1e1b2 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20 s operation and
1e1b3 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68 61 74 NotFound is that
1e1b4 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74 69 this.** operati
1e1b5 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 6b on assumes the k
1e1b6 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 ey is an integer
1e1b7 20 61 6e 64 20 74 68 61 74 20 50 31 20 69 73 20 and that P1 is
1e1b8 61 20 74 61 62 6c 65 20 77 68 65 72 65 61 73 0a a table whereas.
1e1b9 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73 73 75 ** NotFound assu
1e1ba 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62 6c 6f mes key is a blo
1e1bb 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 b constructed fr
1e1bc 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20 61 6e om MakeRecord an
1e1bd 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20 69 6e d.** P1 is an in
1e1be 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 dex..**.** See a
1e1bf 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 lso: Found, NotF
1e1c0 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65 0a 2a ound, IsUnique.*
1e1c1 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45 78 69 /.case OP_NotExi
1e1c2 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a sts: { /*
1e1c3 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 jump, in3 */.#i
1e1c4 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e1c5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e1c6 74 6f 20 75 2e 62 64 20 2a 2f 0a 20 20 56 64 62 to u.bd */. Vdb
1e1c7 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1e1c8 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1e1c9 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 75 36 34 int res;. u64
1e1ca 20 69 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f 2a iKey;.#endif /*
1e1cb 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e1cc 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 64 moved into u.bd
1e1cd 20 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20 26 61 */.. pIn3 = &a
1e1ce 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1e1cf 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c assert( pIn3->fl
1e1d0 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b ags & MEM_Int );
1e1d1 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e1d2 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e1d3 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e1d4 20 75 2e 62 64 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bd.pC = p->ap
1e1d5 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e1d6 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 21 assert( u.bd.pC!
1e1d7 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1e1d8 75 2e 62 64 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bd.pC->isTable
1e1d9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e );. assert( u.
1e1da 62 64 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 bd.pC->pseudoTab
1e1db 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 75 2e leReg==0 );. u.
1e1dc 62 64 2e 70 43 72 73 72 20 3d 20 75 2e 62 64 2e bd.pCrsr = u.bd.
1e1dd 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 pC->pCursor;. i
1e1de 66 28 20 75 2e 62 64 2e 70 43 72 73 72 21 3d 30 f( u.bd.pCrsr!=0
1e1df 20 29 7b 0a 20 20 20 20 75 2e 62 64 2e 72 65 73 ){. u.bd.res
1e1e0 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 69 = 0;. u.bd.i
1e1e1 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b Key = pIn3->u.i;
1e1e2 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1e1e3 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 3BtreeMovetoUnpa
1e1e4 63 6b 65 64 28 75 2e 62 64 2e 70 43 72 73 72 2c cked(u.bd.pCrsr,
1e1e5 20 30 2c 20 75 2e 62 64 2e 69 4b 65 79 2c 20 30 0, u.bd.iKey, 0
1e1e6 2c 20 26 75 2e 62 64 2e 72 65 73 29 3b 0a 20 20 , &u.bd.res);.
1e1e7 20 20 75 2e 62 64 2e 70 43 2d 3e 6c 61 73 74 52 u.bd.pC->lastR
1e1e8 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 owid = pIn3->u.i
1e1e9 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 72 ;. u.bd.pC->r
1e1ea 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 75 2e owidIsValid = u.
1e1eb 62 64 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a bd.res==0 ?1:0;.
1e1ec 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6e 75 6c u.bd.pC->nul
1e1ed 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 2e lRow = 0;. u.
1e1ee 62 64 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 bd.pC->cacheStat
1e1ef 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e1f0 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 64 ;. u.bd.pC->d
1e1f1 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1e1f2 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 64 2e 0;. if( u.bd.
1e1f3 72 65 73 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 res!=0 ){.
1e1f4 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 pc = pOp->p2 - 1
1e1f5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1e1f6 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 u.bd.pC->rowidIs
1e1f7 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 20 20 Valid==0 );.
1e1f8 7d 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 }. u.bd.pC->s
1e1f9 65 65 6b 52 65 73 75 6c 74 20 3d 20 75 2e 62 64 eekResult = u.bd
1e1fa 2e 72 65 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 .res;. }else{.
1e1fb 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 /* This happe
1e1fc 6e 73 20 77 68 65 6e 20 61 6e 20 61 74 74 65 6d ns when an attem
1e1fd 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 72 65 61 pt to open a rea
1e1fe 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 0a d cursor on the.
1e1ff 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 ** sqlite_ma
1e200 73 74 65 72 20 74 61 62 6c 65 20 72 65 74 75 72 ster table retur
1e201 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e ns SQLITE_EMPTY.
1e202 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20 3d . */. pc =
1e203 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 pOp->p2 - 1;.
1e204 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 assert( u.bd.p
1e205 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d C->rowidIsValid=
1e206 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 64 2e 70 =0 );. u.bd.p
1e207 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 C->seekResult =
1e208 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 0;. }. break;.
1e209 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 }../* Opcode: Se
1e20a 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20 2a quence P1 P2 * *
1e20b 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 *.**.** Find th
1e20c 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65 e next available
1e20d 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 sequence number
1e20e 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a for cursor P1..
1e20f 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 71 ** Write the seq
1e210 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e 74 uence number int
1e211 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a o register P2..*
1e212 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20 6e * The sequence n
1e213 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75 72 umber on the cur
1e214 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 sor is increment
1e215 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a 2a ed after this.**
1e216 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 0a instruction. .
1e217 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75 65 */.case OP_Seque
1e218 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 nce: {
1e219 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 /* out2-prerele
1e21a 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 ase */. assert(
1e21b 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 pOp->p1>=0 && p
1e21c 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f Op->p1<p->nCurso
1e21d 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 r );. assert( p
1e21e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e21f 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 !=0 );. pOut->u
1e220 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f .i = p->apCsr[pO
1e221 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f 75 6e 74 p->p1]->seqCount
1e222 2b 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a ++;. break;.}..
1e223 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 ./* Opcode: NewR
1e224 6f 77 69 64 20 50 31 20 50 32 20 50 33 20 2a 20 owid P1 P2 P3 *
1e225 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 *.**.** Get a ne
1e226 77 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 w integer record
1e227 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 number (a.k.a "
1e228 72 6f 77 69 64 22 29 20 75 73 65 64 20 61 73 20 rowid") used as
1e229 74 68 65 20 6b 65 79 20 74 6f 20 61 20 74 61 62 the key to a tab
1e22a 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 le..** The recor
1e22b 64 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 d number is not
1e22c 70 72 65 76 69 6f 75 73 6c 79 20 75 73 65 64 20 previously used
1e22d 61 73 20 61 20 6b 65 79 20 69 6e 20 74 68 65 20 as a key in the
1e22e 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c database.** tabl
1e22f 65 20 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 e that cursor P1
1e230 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 points to. The
1e231 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 new record numb
1e232 65 72 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a er is written.**
1e233 20 77 72 69 74 74 65 6e 20 74 6f 20 72 65 67 69 written to regi
1e234 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 ster P2..**.** I
1e235 66 20 50 33 3e 30 20 74 68 65 6e 20 50 33 20 69 f P3>0 then P3 i
1e236 73 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 s a register in
1e237 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f the root frame o
1e238 66 20 74 68 69 73 20 56 44 42 45 20 74 68 61 74 f this VDBE that
1e239 20 68 6f 6c 64 73 20 0a 2a 2a 20 74 68 65 20 6c holds .** the l
1e23a 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c argest previousl
1e23b 79 20 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f y generated reco
1e23c 72 64 20 6e 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 rd number. No ne
1e23d 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 w record numbers
1e23e 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 are.** allowed
1e23f 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 to be less than
1e240 74 68 69 73 20 76 61 6c 75 65 2e 20 57 68 65 6e this value. When
1e241 20 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 this value reac
1e242 68 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c hes its maximum,
1e243 20 0a 2a 2a 20 61 20 53 51 4c 49 54 45 5f 46 55 .** a SQLITE_FU
1e244 4c 4c 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 LL error is gene
1e245 72 61 74 65 64 2e 20 54 68 65 20 50 33 20 72 65 rated. The P3 re
1e246 67 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 gister is update
1e247 64 20 77 69 74 68 20 74 68 65 20 27 0a 2a 2a 20 d with the '.**
1e248 67 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 generated record
1e249 20 6e 75 6d 62 65 72 2e 20 54 68 69 73 20 50 33 number. This P3
1e24a 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 mechanism is us
1e24b 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 ed to help imple
1e24c 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f ment the.** AUTO
1e24d 49 4e 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 INCREMENT featur
1e24e 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 e..*/.case OP_Ne
1e24f 77 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 wRowid: {
1e250 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 /* out2-prer
1e251 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 elease */.#if 0
1e252 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e253 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e254 2e 62 65 20 2a 2f 0a 20 20 69 36 34 20 76 3b 20 .be */. i64 v;
1e255 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e256 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 /* The new rowid
1e257 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 */. VdbeCursor
1e258 20 2a 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 *pC; /*
1e259 43 75 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20 Cursor of table
1e25a 74 6f 20 67 65 74 20 74 68 65 20 6e 65 77 20 72 to get the new r
1e25b 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 owid */. int re
1e25c 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 s;
1e25d 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e /* Result of an
1e25e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 sqlite3BtreeLas
1e25f 74 28 29 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 t() */. int cnt
1e260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1e261 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 /* Counter to li
1e262 6d 69 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f mit the number o
1e263 66 20 73 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 f searches */.
1e264 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 Mem *pMem;
1e265 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
1e266 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65 er holding large
1e267 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54 st rowid for AUT
1e268 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 OINCREMENT */.
1e269 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d VdbeFrame *pFram
1e26a 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 e; /* Root f
1e26b 72 61 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a rame of VDBE */.
1e26c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e26d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e26e 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 into u.be */..
1e26f 75 2e 62 65 2e 76 20 3d 20 30 3b 0a 20 20 75 2e u.be.v = 0;. u.
1e270 62 65 2e 72 65 73 20 3d 20 30 3b 0a 20 20 61 73 be.res = 0;. as
1e271 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e272 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e && pOp->p1<p->n
1e273 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 65 Cursor );. u.be
1e274 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 .pC = p->apCsr[p
1e275 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 Op->p1];. asser
1e276 74 28 20 75 2e 62 65 2e 70 43 21 3d 30 20 29 3b t( u.be.pC!=0 );
1e277 0a 20 20 69 66 28 20 4e 45 56 45 52 28 75 2e 62 . if( NEVER(u.b
1e278 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 e.pC->pCursor==0
1e279 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 ) ){. /* The
1e27a 7a 65 72 6f 20 69 6e 69 74 69 61 6c 69 7a 61 74 zero initializat
1e27b 69 6f 6e 20 61 62 6f 76 65 20 69 73 20 61 6c 6c ion above is all
1e27c 20 74 68 61 74 20 69 73 20 6e 65 65 64 65 64 20 that is needed
1e27d 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 */. }else{.
1e27e 2f 2a 20 54 68 65 20 6e 65 78 74 20 72 6f 77 69 /* The next rowi
1e27f 64 20 6f 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 d or record numb
1e280 65 72 20 28 64 69 66 66 65 72 65 6e 74 20 74 65 er (different te
1e281 72 6d 73 20 66 6f 72 20 74 68 65 20 73 61 6d 65 rms for the same
1e282 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67 29 20 69 . ** thing) i
1e283 73 20 6f 62 74 61 69 6e 65 64 20 69 6e 20 61 20 s obtained in a
1e284 74 77 6f 2d 73 74 65 70 20 61 6c 67 6f 72 69 74 two-step algorit
1e285 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a hm.. **. *
1e286 2a 20 46 69 72 73 74 20 77 65 20 61 74 74 65 6d * First we attem
1e287 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6c pt to find the l
1e288 61 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 argest existing
1e289 72 6f 77 69 64 20 61 6e 64 20 61 64 64 20 6f 6e rowid and add on
1e28a 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 61 74 e. ** to that
1e28b 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 6c 61 . But if the la
1e28c 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 72 rgest existing r
1e28d 6f 77 69 64 20 69 73 20 61 6c 72 65 61 64 79 20 owid is already
1e28e 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20 20 20 20 the maximum.
1e28f 2a 2a 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 ** positive inte
1e290 67 65 72 2c 20 77 65 20 68 61 76 65 20 74 6f 20 ger, we have to
1e291 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 fall through to
1e292 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a the second. *
1e293 2a 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20 * probabilistic
1e294 61 6c 67 6f 72 69 74 68 6d 0a 20 20 20 20 2a 2a algorithm. **
1e295 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f . ** The seco
1e296 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 nd algorithm is
1e297 74 6f 20 73 65 6c 65 63 74 20 61 20 72 6f 77 69 to select a rowi
1e298 64 20 61 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 d at random and
1e299 73 65 65 20 69 66 0a 20 20 20 20 2a 2a 20 69 74 see if. ** it
1e29a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 already exists
1e29b 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 in the table. I
1e29c 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 f it does not ex
1e29d 69 73 74 2c 20 77 65 20 68 61 76 65 0a 20 20 20 ist, we have.
1e29e 20 2a 2a 20 73 75 63 63 65 65 64 65 64 2e 20 20 ** succeeded.
1e29f 49 66 20 74 68 65 20 72 61 6e 64 6f 6d 20 72 6f If the random ro
1e2a0 77 69 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 wid does exist,
1e2a1 77 65 20 73 65 6c 65 63 74 20 61 20 6e 65 77 20 we select a new
1e2a2 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 one. ** and t
1e2a3 72 79 20 61 67 61 69 6e 2c 20 75 70 20 74 6f 20 ry again, up to
1e2a4 31 30 30 20 74 69 6d 65 73 2e 0a 20 20 20 20 2a 100 times.. *
1e2a5 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e /. assert( u.
1e2a6 62 65 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 be.pC->isTable )
1e2a7 3b 0a 20 20 20 20 75 2e 62 65 2e 63 6e 74 20 3d ;. u.be.cnt =
1e2a8 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 0;..#ifdef SQLI
1e2a9 54 45 5f 33 32 42 49 54 5f 52 4f 57 49 44 0a 23 TE_32BIT_ROWID.#
1e2aa 20 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f define MAX_RO
1e2ab 57 49 44 20 30 78 37 66 66 66 66 66 66 66 0a 23 WID 0x7fffffff.#
1e2ac 65 6c 73 65 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 else. /* Some
1e2ad 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c compilers compl
1e2ae 61 69 6e 20 61 62 6f 75 74 20 63 6f 6e 73 74 61 ain about consta
1e2af 6e 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 nts of the form
1e2b0 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 66 0x7fffffffffffff
1e2b1 66 66 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 ff.. ** Other
1e2b2 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f 75 74 s complain about
1e2b3 20 30 78 37 66 66 66 66 66 66 66 66 66 66 66 66 0x7ffffffffffff
1e2b4 66 66 66 66 4c 4c 2e 20 20 54 68 65 20 66 6f 6c ffffLL. The fol
1e2b5 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 73 65 65 lowing macro see
1e2b6 6d 73 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f ms. ** to pro
1e2b7 76 69 64 65 20 74 68 65 20 63 6f 6e 73 74 61 6e vide the constan
1e2b8 74 20 77 68 69 6c 65 20 6d 61 6b 69 6e 67 20 61 t while making a
1e2b9 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 20 68 61 70 ll compilers hap
1e2ba 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 20 20 20 64 py.. */.# d
1e2bb 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 efine MAX_ROWID
1e2bc 20 28 69 36 34 29 28 20 28 28 28 75 36 34 29 30 (i64)( (((u64)0
1e2bd 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 20 x7fffffff)<<32)
1e2be 7c 20 28 75 36 34 29 30 78 66 66 66 66 66 66 66 | (u64)0xfffffff
1e2bf 66 20 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 f ).#endif..
1e2c0 69 66 28 20 21 75 2e 62 65 2e 70 43 2d 3e 75 73 if( !u.be.pC->us
1e2c1 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a eRandomRowid ){.
1e2c2 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 73 u.be.v = s
1e2c3 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61 qlite3BtreeGetCa
1e2c4 63 68 65 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 chedRowid(u.be.p
1e2c5 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 C->pCursor);.
1e2c6 20 20 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 30 if( u.be.v==0
1e2c7 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d ){. rc =
1e2c8 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 sqlite3BtreeLas
1e2c9 74 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 t(u.be.pC->pCurs
1e2ca 6f 72 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b 0a or, &u.be.res);.
1e2cb 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d if( rc!=
1e2cc 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 SQLITE_OK ){.
1e2cd 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 goto abor
1e2ce 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a t_due_to_error;.
1e2cf 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
1e2d0 20 20 69 66 28 20 75 2e 62 65 2e 72 65 73 20 29 if( u.be.res )
1e2d1 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 {. u.be
1e2d2 2e 76 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 .v = 1;.
1e2d3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
1e2d4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 assert( sqlite3
1e2d5 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c BtreeCursorIsVal
1e2d6 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 id(u.be.pC->pCur
1e2d7 73 6f 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 sor) );.
1e2d8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 rc = sqlite3Bt
1e2d9 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 65 2e reeKeySize(u.be.
1e2da 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e pC->pCursor, &u.
1e2db 62 65 2e 76 29 3b 0a 20 20 20 20 20 20 20 20 20 be.v);.
1e2dc 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c assert( rc==SQL
1e2dd 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 ITE_OK ); /* C
1e2de 61 6e 6e 6f 74 20 66 61 69 6c 20 66 6f 6c 6c 6f annot fail follo
1e2df 77 69 6e 67 20 42 74 72 65 65 4c 61 73 74 28 29 wing BtreeLast()
1e2e0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 */. if
1e2e1 28 20 75 2e 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f ( u.be.v==MAX_RO
1e2e2 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 WID ){.
1e2e3 20 20 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 u.be.pC->useR
1e2e4 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20 31 3b 0a andomRowid = 1;.
1e2e5 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b }else{
1e2e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 . u.b
1e2e7 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 e.v++;.
1e2e8 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1e2e9 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 }..#ifndef SQ
1e2ea 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e LITE_OMIT_AUTOIN
1e2eb 43 52 45 4d 45 4e 54 0a 20 20 20 20 20 20 69 66 CREMENT. if
1e2ec 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 ( pOp->p3 ){.
1e2ed 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 /* Assert t
1e2ee 68 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 hat P3 is a vali
1e2ef 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a d memory cell. *
1e2f0 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 /. assert
1e2f1 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 ( pOp->p3>0 );.
1e2f2 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 46 if( p->pF
1e2f3 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 rame ){.
1e2f4 20 20 66 6f 72 28 75 2e 62 65 2e 70 46 72 61 6d for(u.be.pFram
1e2f5 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 e=p->pFrame; u.b
1e2f6 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e e.pFrame->pParen
1e2f7 74 3b 20 75 2e 62 65 2e 70 46 72 61 6d 65 3d 75 t; u.be.pFrame=u
1e2f8 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61 72 .be.pFrame->pPar
1e2f9 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ent);.
1e2fa 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 50 /* Assert that P
1e2fb 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 3 is a valid mem
1e2fc 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 ory cell. */.
1e2fd 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
1e2fe 4f 70 2d 3e 70 33 3c 3d 75 2e 62 65 2e 70 46 72 Op->p3<=u.be.pFr
1e2ff 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 ame->nMem );.
1e300 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d u.be.pMem
1e301 20 3d 20 26 75 2e 62 65 2e 70 46 72 61 6d 65 2d = &u.be.pFrame-
1e302 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a >aMem[pOp->p3];.
1e303 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }else{.
1e304 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 /* Asse
1e305 72 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20 rt that P3 is a
1e306 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c valid memory cel
1e307 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 l. */.
1e308 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c assert( pOp->p3<
1e309 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 =p->nMem );.
1e30a 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 u.be.pMem
1e30b 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d = &aMem[pOp->p3]
1e30c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
1e30d 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 REGISTER_TR
1e30e 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 ACE(pOp->p3, u.b
1e30f 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 e.pMem);.
1e310 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 sqlite3VdbeMemI
1e311 6e 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e 70 ntegerify(u.be.p
1e312 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 73 Mem);. as
1e313 73 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65 6d sert( (u.be.pMem
1e314 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e ->flags & MEM_In
1e315 74 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 6d t)!=0 ); /* mem
1e316 28 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 6e (P3) holds an in
1e317 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 teger */.
1e318 20 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e if( u.be.pMem->
1e319 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c u.i==MAX_ROWID |
1e31a 7c 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 | u.be.pC->useRa
1e31b 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 ndomRowid ){.
1e31c 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 rc = SQLI
1e31d 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 TE_FULL;.
1e31e 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1e31f 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1e320 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
1e321 28 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70 4d ( u.be.v<u.be.pM
1e322 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 em->u.i+1 ){.
1e323 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 u.be.v =
1e324 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b u.be.pMem->u.i +
1e325 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 1;. }.
1e326 20 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 2d u.be.pMem-
1e327 3e 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 >u.i = u.be.v;.
1e328 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 }.#endif..
1e329 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1e32a 65 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 eSetCachedRowid(
1e32b 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 u.be.pC->pCursor
1e32c 2c 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f 57 , u.be.v<MAX_ROW
1e32d 49 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a 20 ID ? u.be.v+1 :
1e32e 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 0);. }. if
1e32f 28 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 ( u.be.pC->useRa
1e330 6e 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 ndomRowid ){.
1e331 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e assert( pOp->
1e332 70 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65 20 p3==0 ); /* We
1e333 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61 6e cannot be in ran
1e334 64 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20 69 dom rowid mode i
1e335 66 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 f this is.
1e336 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e337 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55 54 ** an AUT
1e338 4f 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65 OINCREMENT table
1e339 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65 2e . */. u.be.
1e33a 76 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 v = db->lastRowi
1e33b 64 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63 6e d;. u.be.cn
1e33c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b t = 0;. do{
1e33d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 . if( u.b
1e33e 65 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e 62 e.cnt==0 && (u.b
1e33f 65 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d 75 e.v&0xffffff)==u
1e340 2e 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20 20 .be.v ){.
1e341 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 u.be.v++;.
1e342 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1e343 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 sqlite3_ra
1e344 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 ndomness(sizeof(
1e345 75 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e 76 u.be.v), &u.be.v
1e346 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
1e347 20 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75 2e u.be.cnt<5 ) u.
1e348 62 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66 66 be.v &= 0xffffff
1e349 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
1e34a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e34b 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1e34c 6b 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 ked(u.be.pC->pCu
1e34d 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 2e rsor, 0, (u64)u.
1e34e 62 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e 72 be.v, 0, &u.be.r
1e34f 65 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 62 es);. u.b
1e350 65 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d e.cnt++;. }
1e351 77 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74 3c while( u.be.cnt<
1e352 31 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 100 && rc==SQLIT
1e353 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 73 E_OK && u.be.res
1e354 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ==0 );. if(
1e355 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 rc==SQLITE_OK &
1e356 26 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 7b & u.be.res==0 ){
1e357 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 . rc = SQ
1e358 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 LITE_FULL;.
1e359 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 goto abort_du
1e35a 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 e_to_error;.
1e35b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e }. }. u.
1e35c 62 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 be.pC->rowidIsVa
1e35d 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 lid = 0;. u.b
1e35e 65 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f e.pC->deferredMo
1e35f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e veto = 0;. u.
1e360 62 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 be.pC->cacheStat
1e361 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 us = CACHE_STALE
1e362 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e ;. }. pOut->u.
1e363 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 62 72 i = u.be.v;. br
1e364 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e365 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 32 20 e: Insert P1 P2
1e366 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57 P3 P4 P5.**.** W
1e367 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e rite an entry in
1e368 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20 to the table of
1e369 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65 cursor P1. A ne
1e36a 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72 w entry is.** cr
1e36b 65 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73 eated if it does
1e36c 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 n't already exis
1e36d 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f t or the data fo
1e36e 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a r an existing.**
1e36f 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72 entry is overwr
1e370 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61 itten. The data
1e371 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 4d 45 is the value ME
1e372 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e M_Blob stored in
1e373 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e 75 6d register.** num
1e374 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 79 20 ber P2. The key
1e375 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 is stored in reg
1e376 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 6b 65 ister P3. The ke
1e377 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 20 4d y must.** be a M
1e378 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 EM_Int..**.** If
1e379 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 the OPFLAG_NCHA
1e37a 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35 20 69 NGE flag of P5 i
1e37b 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 s set, then the
1e37c 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 row change count
1e37d 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 is.** increment
1e37e 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f ed (otherwise no
1e37f 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50 46 4c t). If the OPFL
1e380 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 6c 61 AG_LASTROWID fla
1e381 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 0a g of P5 is set,.
1e382 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 69 73 ** then rowid is
1e383 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75 62 73 stored for subs
1e384 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20 62 79 equent return by
1e385 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f the.** sqlite3_
1e386 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 last_insert_rowi
1e387 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 6f 74 d() function (ot
1e388 68 65 72 77 69 73 65 20 69 74 20 69 73 20 75 6e herwise it is un
1e389 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a modified)..**.**
1e38a 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 55 If the OPFLAG_U
1e38b 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 SESEEKRESULT fla
1e38c 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 g of P5 is set a
1e38d 6e 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 nd if the result
1e38e 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20 of.** the last
1e38f 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 20 28 seek operation (
1e390 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20 77 61 OP_NotExists) wa
1e391 73 20 61 20 73 75 63 63 65 73 73 2c 20 74 68 65 s a success, the
1e392 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74 n this.** operat
1e393 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 ion will not att
1e394 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65 empt to find the
1e395 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 6f 77 appropriate row
1e396 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a 2a 2a before doing.**
1e397 20 74 68 65 20 69 6e 73 65 72 74 20 62 75 74 20 the insert but
1e398 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f 76 65 will instead ove
1e399 72 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74 rwrite the row t
1e39a 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 hat the cursor i
1e39b 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 70 s.** currently p
1e39c 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50 72 65 ointing to. Pre
1e39d 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70 72 69 sumably, the pri
1e39e 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 or OP_NotExists
1e39f 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20 61 6c opcode.** has al
1e3a0 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64 ready positioned
1e3a1 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 72 72 the cursor corr
1e3a2 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69 73 20 ectly. This is
1e3a3 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a an optimization.
1e3a4 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 20 70 ** that boosts p
1e3a5 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 61 76 erformance by av
1e3a6 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61 6e 74 oiding redundant
1e3a7 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 seeks..**.** If
1e3a8 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 55 50 the OPFLAG_ISUP
1e3a9 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 65 74 DATE flag is set
1e3aa 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f , then this opco
1e3ab 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e de is part of an
1e3ac 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61 .** UPDATE opera
1e3ad 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 tion. Otherwise
1e3ae 20 28 69 66 20 74 68 65 20 66 6c 61 67 20 69 73 (if the flag is
1e3af 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74 68 69 clear) then thi
1e3b0 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 70 s opcode.** is p
1e3b1 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 art of an INSERT
1e3b2 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65 operation. The
1e3b3 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 6f difference is o
1e3b4 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f nly important to
1e3b5 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 68 .** the update h
1e3b6 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d ook..**.** Param
1e3b7 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 6e eter P4 may poin
1e3b8 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f t to a string co
1e3b9 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 ntaining the tab
1e3ba 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d le-name, or.** m
1e3bb 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69 ay be NULL. If i
1e3bc 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 t is not NULL, t
1e3bd 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d 68 hen the update-h
1e3be 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33 ook .** (sqlite3
1e3bf 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b .xUpdateCallback
1e3c0 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c ) is invoked fol
1e3c1 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 73 lowing a success
1e3c2 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a ful insert..**.*
1e3c3 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a * (WARNING/TODO:
1e3c4 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 75 If P1 is a pseu
1e3c5 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 32 do-cursor and P2
1e3c6 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a is dynamically.
1e3c7 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 ** allocated, th
1e3c8 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 en ownership of
1e3c9 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 65 P2 is transferre
1e3ca 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d d to the pseudo-
1e3cb 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65 cursor.** and re
1e3cc 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d 65 gister P2 become
1e3cd 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 66 s ephemeral. If
1e3ce 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63 the cursor is c
1e3cf 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76 hanged, the.** v
1e3d0 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 alue of register
1e3d1 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 68 P2 will then ch
1e3d2 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 ange. Make sure
1e3d3 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a this does not.*
1e3d4 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62 * cause any prob
1e3d5 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 lems.).**.** Thi
1e3d6 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e s instruction on
1e3d7 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c ly works on tabl
1e3d8 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c es. The equival
1e3d9 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a ent instruction.
1e3da 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 69 ** for indices i
1e3db 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a s OP_IdxInsert..
1e3dc 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e */./* Opcode: In
1e3dd 73 65 72 74 49 6e 74 20 50 31 20 50 32 20 50 33 sertInt P1 P2 P3
1e3de 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 P4 P5.**.** Thi
1e3df 73 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 s works exactly
1e3e0 6c 69 6b 65 20 4f 50 5f 49 6e 73 65 72 74 20 65 like OP_Insert e
1e3e1 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6b xcept that the k
1e3e2 65 79 20 69 73 20 74 68 65 0a 2a 2a 20 69 6e 74 ey is the.** int
1e3e3 65 67 65 72 20 76 61 6c 75 65 20 50 33 2c 20 6e eger value P3, n
1e3e4 6f 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 ot the value of
1e3e5 74 68 65 20 69 6e 74 65 67 65 72 20 73 74 6f 72 the integer stor
1e3e6 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 ed in register P
1e3e7 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 3..*/.case OP_In
1e3e8 73 65 72 74 3a 20 0a 63 61 73 65 20 4f 50 5f 49 sert: .case OP_I
1e3e9 6e 73 65 72 74 49 6e 74 3a 20 7b 0a 23 69 66 20 nsertInt: {.#if
1e3ea 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e3eb 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e3ec 20 75 2e 62 66 20 2a 2f 0a 20 20 4d 65 6d 20 2a u.bf */. Mem *
1e3ed 70 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 pData; /*
1e3ee 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 MEM cell holding
1e3ef 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 data for the re
1e3f0 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 cord to be inser
1e3f1 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4b ted */. Mem *pK
1e3f2 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 45 ey; /* ME
1e3f3 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 6b M cell holding k
1e3f4 65 79 20 20 66 6f 72 20 74 68 65 20 72 65 63 6f ey for the reco
1e3f5 72 64 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 rd */. i64 iKey
1e3f6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ; /* The
1e3f7 20 69 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f integer ROWID o
1e3f8 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 r key for the re
1e3f9 63 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 cord to be inser
1e3fa 74 65 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 ted */. VdbeCur
1e3fb 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43 75 sor *pC; /* Cu
1e3fc 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 69 6e rsor to table in
1e3fd 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 74 20 to which insert
1e3fe 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 is written */.
1e3ff 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 int nZero;
1e400 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a /* Number of z
1e401 65 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 ero-bytes to app
1e402 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 73 65 65 end */. int see
1e403 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 52 65 kResult; /* Re
1e404 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 73 65 sult of prior se
1e405 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20 55 53 ek or 0 if no US
1e406 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 ESEEKRESULT flag
1e407 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1e408 20 2a 7a 44 62 3b 20 20 2f 2a 20 64 61 74 61 62 *zDb; /* datab
1e409 61 73 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 ase name - used
1e40a 62 79 20 74 68 65 20 75 70 64 61 74 65 20 68 6f by the update ho
1e40b 6f 6b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 ok */. const ch
1e40c 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61 62 ar *zTbl; /* Tab
1e40d 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 le name - used b
1e40e 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 6f 6f y the opdate hoo
1e40f 6b 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 k */. int op;
1e410 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f /* Opco
1e411 64 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f de for update ho
1e412 6f 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54 ok: SQLITE_UPDAT
1e413 45 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45 E or SQLITE_INSE
1e414 52 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 RT */.#endif /*
1e415 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 local variables
1e416 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 moved into u.bf
1e417 2a 2f 0a 0a 20 20 75 2e 62 66 2e 70 44 61 74 61 */.. u.bf.pData
1e418 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 = &aMem[pOp->p2
1e419 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 ];. assert( pOp
1e41a 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e41b 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e41c 0a 20 20 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e . u.bf.pC = p->
1e41d 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e41e 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 assert( u.bf.p
1e41f 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 C!=0 );. assert
1e420 28 20 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 ( u.bf.pC->pCurs
1e421 6f 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 or!=0 );. asser
1e422 74 28 20 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75 t( u.bf.pC->pseu
1e423 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b doTableReg==0 );
1e424 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e . assert( u.bf.
1e425 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 pC->isTable );.
1e426 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 REGISTER_TRACE(
1e427 70 4f 70 2d 3e 70 32 2c 20 75 2e 62 66 2e 70 44 pOp->p2, u.bf.pD
1e428 61 74 61 29 3b 0a 0a 20 20 69 66 28 20 70 4f 70 ata);.. if( pOp
1e429 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 ->opcode==OP_Ins
1e42a 65 72 74 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e ert ){. u.bf.
1e42b 70 4b 65 79 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pKey = &aMem[pOp
1e42c 2d 3e 70 33 5d 3b 0a 20 20 20 20 61 73 73 65 72 ->p3];. asser
1e42d 74 28 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c t( u.bf.pKey->fl
1e42e 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b ags & MEM_Int );
1e42f 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 . REGISTER_TR
1e430 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 ACE(pOp->p3, u.b
1e431 66 2e 70 4b 65 79 29 3b 0a 20 20 20 20 75 2e 62 f.pKey);. u.b
1e432 66 2e 69 4b 65 79 20 3d 20 75 2e 62 66 2e 70 4b f.iKey = u.bf.pK
1e433 65 79 2d 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 ey->u.i;. }else
1e434 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f {. assert( pO
1e435 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e p->opcode==OP_In
1e436 73 65 72 74 49 6e 74 20 29 3b 0a 20 20 20 20 75 sertInt );. u
1e437 2e 62 66 2e 69 4b 65 79 20 3d 20 70 4f 70 2d 3e .bf.iKey = pOp->
1e438 70 33 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 p3;. }.. if( p
1e439 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f Op->p5 & OPFLAG_
1e43a 4e 43 48 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 NCHANGE ) p->nCh
1e43b 61 6e 67 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f ange++;. if( pO
1e43c 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c p->p5 & OPFLAG_L
1e43d 41 53 54 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c ASTROWID ) db->l
1e43e 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 62 66 2e astRowid = u.bf.
1e43f 69 4b 65 79 3b 0a 20 20 69 66 28 20 75 2e 62 66 iKey;. if( u.bf
1e440 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 .pData->flags &
1e441 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 MEM_Null ){.
1e442 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 u.bf.pData->z =
1e443 30 3b 0a 20 20 20 20 75 2e 62 66 2e 70 44 61 74 0;. u.bf.pDat
1e444 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 a->n = 0;. }els
1e445 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 e{. assert( u
1e446 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 .bf.pData->flags
1e447 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d & (MEM_Blob|MEM
1e448 5f 53 74 72 29 20 29 3b 0a 20 20 7d 0a 20 20 75 _Str) );. }. u
1e449 2e 62 66 2e 73 65 65 6b 52 65 73 75 6c 74 20 3d .bf.seekResult =
1e44a 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 ((pOp->p5 & OPF
1e44b 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c LAG_USESEEKRESUL
1e44c 54 29 20 3f 20 75 2e 62 66 2e 70 43 2d 3e 73 65 T) ? u.bf.pC->se
1e44d 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 3b 0a 20 ekResult : 0);.
1e44e 20 69 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d if( u.bf.pData-
1e44f 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 >flags & MEM_Zer
1e450 6f 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a o ){. u.bf.nZ
1e451 65 72 6f 20 3d 20 75 2e 62 66 2e 70 44 61 74 61 ero = u.bf.pData
1e452 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c ->u.nZero;. }el
1e453 73 65 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 se{. u.bf.nZe
1e454 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 ro = 0;. }. sq
1e455 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 lite3BtreeSetCac
1e456 68 65 64 52 6f 77 69 64 28 75 2e 62 66 2e 70 43 hedRowid(u.bf.pC
1e457 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 ->pCursor, 0);.
1e458 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 rc = sqlite3Btr
1e459 65 65 49 6e 73 65 72 74 28 75 2e 62 66 2e 70 43 eeInsert(u.bf.pC
1e45a 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e ->pCursor, 0, u.
1e45b 62 66 2e 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 bf.iKey,.
1e45c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e45d 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a u.bf.pData->z
1e45e 2c 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 2c , u.bf.pData->n,
1e45f 20 75 2e 62 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 u.bf.nZero,.
1e460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e461 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 pOp->p5 &
1e462 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 2c 20 OPFLAG_APPEND,
1e463 75 2e 62 66 2e 73 65 65 6b 52 65 73 75 6c 74 0a u.bf.seekResult.
1e464 20 20 29 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e );. u.bf.pC->
1e465 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 rowidIsValid = 0
1e466 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 64 65 66 ;. u.bf.pC->def
1e467 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b erredMoveto = 0;
1e468 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 63 61 63 68 . u.bf.pC->cach
1e469 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f eStatus = CACHE_
1e46a 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 STALE;.. /* Inv
1e46b 6f 6b 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 oke the update-h
1e46c 6f 6f 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e ook if required.
1e46d 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 */. if( rc==SQ
1e46e 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 LITE_OK && db->x
1e46f 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 UpdateCallback &
1e470 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 & pOp->p4.z ){.
1e471 20 20 20 75 2e 62 66 2e 7a 44 62 20 3d 20 64 62 u.bf.zDb = db
1e472 2d 3e 61 44 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 ->aDb[u.bf.pC->i
1e473 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 Db].zName;. u
1e474 2e 62 66 2e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e .bf.zTbl = pOp->
1e475 70 34 2e 7a 3b 0a 20 20 20 20 75 2e 62 66 2e 6f p4.z;. u.bf.o
1e476 70 20 3d 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 p = ((pOp->p5 &
1e477 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 29 OPFLAG_ISUPDATE)
1e478 20 3f 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 ? SQLITE_UPDATE
1e479 20 3a 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 : SQLITE_INSERT
1e47a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 );. assert( u
1e47b 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 .bf.pC->isTable
1e47c 29 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 );. db->xUpda
1e47d 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 teCallback(db->p
1e47e 55 70 64 61 74 65 41 72 67 2c 20 75 2e 62 66 2e UpdateArg, u.bf.
1e47f 6f 70 2c 20 75 2e 62 66 2e 7a 44 62 2c 20 75 2e op, u.bf.zDb, u.
1e480 62 66 2e 7a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b bf.zTbl, u.bf.iK
1e481 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 ey);. assert(
1e482 20 75 2e 62 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 u.bf.pC->iDb>=0
1e483 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b );. }. break;
1e484 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 .}../* Opcode: D
1e485 65 6c 65 74 65 20 50 31 20 50 32 20 2a 20 50 34 elete P1 P2 * P4
1e486 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 *.**.** Delete
1e487 74 68 65 20 72 65 63 6f 72 64 20 61 74 20 77 68 the record at wh
1e488 69 63 68 20 74 68 65 20 50 31 20 63 75 72 73 6f ich the P1 curso
1e489 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 r is currently p
1e48a 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 ointing..**.** T
1e48b 68 65 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 62 he cursor will b
1e48c 65 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 e left pointing
1e48d 61 74 20 65 69 74 68 65 72 20 74 68 65 20 6e 65 at either the ne
1e48e 78 74 20 6f 72 20 74 68 65 20 70 72 65 76 69 6f xt or the previo
1e48f 75 73 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20 us.** record in
1e490 74 68 65 20 74 61 62 6c 65 2e 20 49 66 20 69 74 the table. If it
1e491 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e is left pointin
1e492 67 20 61 74 20 74 68 65 20 6e 65 78 74 20 72 65 g at the next re
1e493 63 6f 72 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 cord, then.** th
1e494 65 20 6e 65 78 74 20 4e 65 78 74 20 69 6e 73 74 e next Next inst
1e495 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 ruction will be
1e496 61 20 6e 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20 a no-op. Hence
1e497 69 74 20 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65 it is OK to dele
1e498 74 65 0a 2a 2a 20 61 20 72 65 63 6f 72 64 20 66 te.** a record f
1e499 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 4e 65 rom within an Ne
1e49a 78 74 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 xt loop..**.** I
1e49b 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 f the OPFLAG_NCH
1e49c 41 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 32 20 ANGE flag of P2
1e49d 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 is set, then the
1e49e 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e row change coun
1e49f 74 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e t is.** incremen
1e4a0 74 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e ted (otherwise n
1e4a1 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 ot)..**.** P1 mu
1e4a2 73 74 20 6e 6f 74 20 62 65 20 70 73 65 75 64 6f st not be pseudo
1e4a3 2d 74 61 62 6c 65 2e 20 20 49 74 20 68 61 73 20 -table. It has
1e4a4 74 6f 20 62 65 20 61 20 72 65 61 6c 20 74 61 62 to be a real tab
1e4a5 6c 65 20 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 le with.** multi
1e4a6 70 6c 65 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 ple rows..**.**
1e4a7 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c If P4 is not NUL
1e4a8 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 L, then it is th
1e4a9 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 e name of the ta
1e4aa 62 6c 65 20 74 68 61 74 20 50 31 20 69 73 0a 2a ble that P1 is.*
1e4ab 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 * pointing to.
1e4ac 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 The update hook
1e4ad 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c will be invoked,
1e4ae 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a if it exists..*
1e4af 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e * If P4 is not N
1e4b0 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 50 31 20 ULL then the P1
1e4b1 63 75 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 cursor must have
1e4b2 20 62 65 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64 been positioned
1e4b3 0a 2a 2a 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 .** using OP_Not
1e4b4 46 6f 75 6e 64 20 70 72 69 6f 72 20 74 6f 20 69 Found prior to i
1e4b5 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 6f 70 63 nvoking this opc
1e4b6 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ode..*/.case OP_
1e4b7 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 Delete: {.#if 0
1e4b8 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1e4b9 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1e4ba 2e 62 67 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 .bg */. i64 iKe
1e4bb 79 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 y;. VdbeCursor
1e4bc 2a 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c *pC;.#endif /* l
1e4bd 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e4be 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a oved into u.bg *
1e4bf 2f 0a 0a 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d /.. u.bg.iKey =
1e4c0 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 0;. assert( pO
1e4c1 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d p->p1>=0 && pOp-
1e4c2 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 >p1<p->nCursor )
1e4c3 3b 0a 20 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d ;. u.bg.pC = p-
1e4c4 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b >apCsr[pOp->p1];
1e4c5 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e . assert( u.bg.
1e4c6 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 pC!=0 );. asser
1e4c7 74 28 20 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 t( u.bg.pC->pCur
1e4c8 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e sor!=0 ); /* On
1e4c9 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 72 65 61 ly valid for rea
1e4ca 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 l tables, no pse
1e4cb 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 udotables */..
1e4cc 2f 2a 20 49 66 20 74 68 65 20 75 70 64 61 74 65 /* If the update
1e4cd 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e -hook will be in
1e4ce 76 6f 6b 65 64 2c 20 73 65 74 20 75 2e 62 67 2e voked, set u.bg.
1e4cf 69 4b 65 79 20 74 6f 20 74 68 65 20 72 6f 77 69 iKey to the rowi
1e4d0 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f d of the. ** ro
1e4d1 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e w being deleted.
1e4d2 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e . */. if( db->
1e4d3 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 xUpdateCallback
1e4d4 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a && pOp->p4.z ){.
1e4d5 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 assert( u.bg
1e4d6 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a .pC->isTable );.
1e4d7 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 assert( u.bg
1e4d8 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 .pC->rowidIsVali
1e4d9 64 20 29 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 d ); /* lastRow
1e4da 69 64 20 73 65 74 20 62 79 20 70 72 65 76 69 6f id set by previo
1e4db 75 73 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a us OP_NotFound *
1e4dc 2f 0a 20 20 20 20 75 2e 62 67 2e 69 4b 65 79 20 /. u.bg.iKey
1e4dd 3d 20 75 2e 62 67 2e 70 43 2d 3e 6c 61 73 74 52 = u.bg.pC->lastR
1e4de 6f 77 69 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 owid;. }.. /*
1e4df 54 68 65 20 4f 50 5f 44 65 6c 65 74 65 20 6f 70 The OP_Delete op
1e4e0 63 6f 64 65 20 61 6c 77 61 79 73 20 66 6f 6c 6c code always foll
1e4e1 6f 77 73 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 ows an OP_NotExi
1e4e2 73 74 73 20 6f 72 20 4f 50 5f 4c 61 73 74 20 6f sts or OP_Last o
1e4e3 72 0a 20 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e r. ** OP_Column
1e4e4 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62 on the same tab
1e4e5 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 79 20 69 le without any i
1e4e6 6e 74 65 72 76 65 6e 69 6e 67 20 6f 70 65 72 61 ntervening opera
1e4e7 74 69 6f 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 tions that. **
1e4e8 6d 69 67 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e might move or in
1e4e9 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 validate the cur
1e4ea 73 6f 72 2e 20 20 48 65 6e 63 65 20 63 75 72 73 sor. Hence curs
1e4eb 6f 72 20 75 2e 62 67 2e 70 43 20 69 73 20 61 6c or u.bg.pC is al
1e4ec 77 61 79 73 20 70 6f 69 6e 74 69 6e 67 0a 20 20 ways pointing.
1e4ed 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 74 6f ** to the row to
1e4ee 20 62 65 20 64 65 6c 65 74 65 64 20 61 6e 64 20 be deleted and
1e4ef 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 the sqlite3VdbeC
1e4f0 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70 ursorMoveto() op
1e4f1 65 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c eration. ** bel
1e4f2 6f 77 20 69 73 20 61 6c 77 61 79 73 20 61 20 6e ow is always a n
1e4f3 6f 2d 6f 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20 o-op and cannot
1e4f4 66 61 69 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72 fail. We will r
1e4f5 75 6e 20 69 74 20 61 6e 79 68 6f 77 2c 20 74 68 un it anyhow, th
1e4f6 6f 75 67 68 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 ough,. ** to gu
1e4f7 61 72 64 20 61 67 61 69 6e 73 74 20 66 75 74 75 ard against futu
1e4f8 72 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 re changes to th
1e4f9 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 e code generator
1e4fa 2e 0a 20 20 2a 2a 2f 0a 20 20 61 73 73 65 72 74 .. **/. assert
1e4fb 28 20 75 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72 ( u.bg.pC->defer
1e4fc 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a redMoveto==0 );.
1e4fd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 rc = sqlite3Vd
1e4fe 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 beCursorMoveto(u
1e4ff 2e 62 67 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e .bg.pC);. if( N
1e500 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f EVER(rc!=SQLITE_
1e501 4f 4b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 OK) ) goto abort
1e502 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a _due_to_error;..
1e503 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 sqlite3BtreeSe
1e504 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62 tCachedRowid(u.b
1e505 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 g.pC->pCursor, 0
1e506 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 );. rc = sqlite
1e507 33 42 74 72 65 65 44 65 6c 65 74 65 28 75 2e 62 3BtreeDelete(u.b
1e508 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a g.pC->pCursor);.
1e509 20 20 75 2e 62 67 2e 70 43 2d 3e 63 61 63 68 65 u.bg.pC->cache
1e50a 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e50b 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f TALE;.. /* Invo
1e50c 6b 65 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f ke the update-ho
1e50d 6f 6b 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 ok if required.
1e50e 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c */. if( rc==SQL
1e50f 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 ITE_OK && db->xU
1e510 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 pdateCallback &&
1e511 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 pOp->p4.z ){.
1e512 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 const char *zD
1e513 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75 2e 62 67 b = db->aDb[u.bg
1e514 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b .pC->iDb].zName;
1e515 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 . const char
1e516 2a 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e *zTbl = pOp->p4.
1e517 7a 3b 0a 20 20 20 20 64 62 2d 3e 78 55 70 64 61 z;. db->xUpda
1e518 74 65 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 teCallback(db->p
1e519 55 70 64 61 74 65 41 72 67 2c 20 53 51 4c 49 54 UpdateArg, SQLIT
1e51a 45 5f 44 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a E_DELETE, zDb, z
1e51b 54 62 6c 2c 20 75 2e 62 67 2e 69 4b 65 79 29 3b Tbl, u.bg.iKey);
1e51c 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e51d 67 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a g.pC->iDb>=0 );.
1e51e 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 }. if( pOp->p
1e51f 32 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 2 & OPFLAG_NCHAN
1e520 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b GE ) p->nChange+
1e521 2b 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a +;. break;.}./*
1e522 20 4f 70 63 6f 64 65 3a 20 52 65 73 65 74 43 6f Opcode: ResetCo
1e523 75 6e 74 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a unt * * * * *.**
1e524 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 .** The value of
1e525 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e the change coun
1e526 74 65 72 20 69 73 20 63 6f 70 69 65 64 20 74 6f ter is copied to
1e527 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 the database ha
1e528 6e 64 6c 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 ndle.** change c
1e529 6f 75 6e 74 65 72 20 28 72 65 74 75 72 6e 65 64 ounter (returned
1e52a 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 by subsequent c
1e52b 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f alls to sqlite3_
1e52c 63 68 61 6e 67 65 73 28 29 29 2e 0a 2a 2a 20 54 changes())..** T
1e52d 68 65 6e 20 74 68 65 20 56 4d 73 20 69 6e 74 65 hen the VMs inte
1e52e 72 6e 61 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e rnal change coun
1e52f 74 65 72 20 72 65 73 65 74 73 20 74 6f 20 30 2e ter resets to 0.
1e530 0a 2a 2a 20 54 68 69 73 20 69 73 20 75 73 65 64 .** This is used
1e531 20 62 79 20 74 72 69 67 67 65 72 20 70 72 6f 67 by trigger prog
1e532 72 61 6d 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 rams..*/.case OP
1e533 5f 52 65 73 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 _ResetCount: {.
1e534 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 sqlite3VdbeSetC
1e535 68 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 hanges(db, p->nC
1e536 68 61 6e 67 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 hange);. p->nCh
1e537 61 6e 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 ange = 0;. brea
1e538 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e539 20 52 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a RowData P1 P2 *
1e53a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 * *.**.** Write
1e53b 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 into register P
1e53c 32 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 2 the complete r
1e53d 6f 77 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 ow data for curs
1e53e 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 or P1..** There
1e53f 69 73 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 is no interpreta
1e540 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 tion of the data
1e541 2e 20 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 . .** It is jus
1e542 74 20 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 t copied onto th
1e543 65 20 50 32 20 72 65 67 69 73 74 65 72 20 65 78 e P2 register ex
1e544 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 actly as .** it
1e545 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 is found in the
1e546 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a database file..*
1e547 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 *.** If the P1 c
1e548 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f ursor must be po
1e549 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 inting to a vali
1e54a 64 20 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c d row (not a NUL
1e54b 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 L row).** of a r
1e54c 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 eal table, not a
1e54d 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a pseudo-table..*
1e54e 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 /./* Opcode: Row
1e54f 4b 65 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a Key P1 P2 * * *.
1e550 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f **.** Write into
1e551 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68 65 register P2 the
1e552 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 complete row ke
1e553 79 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e y for cursor P1.
1e554 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 .** There is no
1e555 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f interpretation o
1e556 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a f the data. .**
1e557 20 54 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 The key is copi
1e558 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 ed onto the P3 r
1e559 65 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 egister exactly
1e55a 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 as .** it is fou
1e55b 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 nd in the databa
1e55c 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 se file..**.** I
1e55d 66 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 f the P1 cursor
1e55e 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 must be pointing
1e55f 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 to a valid row
1e560 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 (not a NULL row)
1e561 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 .** of a real ta
1e562 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 ble, not a pseud
1e563 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 o-table..*/.case
1e564 20 4f 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 OP_RowKey:.case
1e565 20 4f 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 OP_RowData: {.#
1e566 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e567 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e568 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 20 20 56 64 nto u.bh */. Vd
1e569 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 beCursor *pC;.
1e56a 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b BtCursor *pCrsr;
1e56b 0a 20 20 75 33 32 20 6e 3b 0a 20 20 69 36 34 20 . u32 n;. i64
1e56c 6e 36 34 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c n64;.#endif /* l
1e56d 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e56e 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a oved into u.bh *
1e56f 2f 0a 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 /.. pOut = &aMe
1e570 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f m[pOp->p2];.. /
1e571 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b * Note that RowK
1e572 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 ey and RowData a
1e573 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c re really exactl
1e574 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 y the same instr
1e575 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 uction */. asse
1e576 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e577 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e578 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e 70 rsor );. u.bh.p
1e579 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e57a 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e57b 20 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c u.bh.pC->isTabl
1e57c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 e || pOp->opcode
1e57d 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 ==OP_RowKey );.
1e57e 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 assert( u.bh.pC
1e57f 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 ->isIndex || pOp
1e580 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 ->opcode==OP_Row
1e581 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 Data );. assert
1e582 28 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b 0a ( u.bh.pC!=0 );.
1e583 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 assert( u.bh.p
1e584 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b C->nullRow==0 );
1e585 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e . assert( u.bh.
1e586 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 pC->pseudoTableR
1e587 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 eg==0 );. asser
1e588 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 t( u.bh.pC->pCur
1e589 73 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 68 sor!=0 );. u.bh
1e58a 2e 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 43 .pCrsr = u.bh.pC
1e58b 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 ->pCursor;. ass
1e58c 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1e58d 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 eCursorIsValid(u
1e58e 2e 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a 20 .bh.pCrsr) );..
1e58f 20 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b 65 /* The OP_RowKe
1e590 79 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 61 y and OP_RowData
1e591 20 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 20 opcodes always
1e592 66 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 69 follow OP_NotExi
1e593 73 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52 sts or. ** OP_R
1e594 65 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 69 ewind/Op_Next wi
1e595 74 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 6e th no intervenin
1e596 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 g instructions t
1e597 68 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c 69 hat might invali
1e598 64 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 date. ** the cu
1e599 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 65 rsor. Hence the
1e59a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 following sqlit
1e59b 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 e3VdbeCursorMove
1e59c 74 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c 77 to() call is alw
1e59d 61 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 ays. ** a no-op
1e59e 20 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 66 and can never f
1e59f 61 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 61 ail. But we lea
1e5a0 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 61 ve it in place a
1e5a1 73 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a 2f s a safety.. */
1e5a2 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e . assert( u.bh.
1e5a3 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 pC->deferredMove
1e5a4 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 to==0 );. rc =
1e5a5 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f sqlite3VdbeCurso
1e5a6 72 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 29 rMoveto(u.bh.pC)
1e5a7 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 ;. if( NEVER(rc
1e5a8 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 !=SQLITE_OK) ) g
1e5a9 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f oto abort_due_to
1e5aa 5f 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 75 _error;.. if( u
1e5ab 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 .bh.pC->isIndex
1e5ac 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 ){. assert( !
1e5ad 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 u.bh.pC->isTable
1e5ae 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c );. rc = sql
1e5af 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 ite3BtreeKeySize
1e5b0 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e (u.bh.pCrsr, &u.
1e5b1 62 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 73 bh.n64);. ass
1e5b2 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f ert( rc==SQLITE_
1e5b3 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 OK ); /* True
1e5b4 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 because of Curs
1e5b5 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 orMoveto() call
1e5b6 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 28 above */. if(
1e5b7 20 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c u.bh.n64>db->aL
1e5b8 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1e5b9 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 T_LENGTH] ){.
1e5ba 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1e5bb 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 2e . }. u.bh.
1e5bc 6e 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e 36 n = (u32)u.bh.n6
1e5bd 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4;. }else{.
1e5be 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 rc = sqlite3Btre
1e5bf 65 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e 70 eDataSize(u.bh.p
1e5c0 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a Crsr, &u.bh.n);.
1e5c1 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d assert( rc==
1e5c2 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 SQLITE_OK );
1e5c3 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 /* DataSize() ca
1e5c4 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 nnot fail */.
1e5c5 20 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 32 if( u.bh.n>(u32
1e5c6 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 )db->aLimit[SQLI
1e5c7 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d TE_LIMIT_LENGTH]
1e5c8 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 ){. goto t
1e5c9 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 oo_big;. }.
1e5ca 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 }. if( sqlite3V
1e5cb 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c dbeMemGrow(pOut,
1e5cc 20 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a 20 u.bh.n, 0) ){.
1e5cd 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a goto no_mem;.
1e5ce 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 }. pOut->n =
1e5cf 75 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 u.bh.n;. MemSet
1e5d0 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d TypeFlag(pOut, M
1e5d1 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 EM_Blob);. if(
1e5d2 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 u.bh.pC->isIndex
1e5d3 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c ){. rc = sql
1e5d4 69 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62 ite3BtreeKey(u.b
1e5d5 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 h.pCrsr, 0, u.bh
1e5d6 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 .n, pOut->z);.
1e5d7 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 }else{. rc =
1e5d8 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 sqlite3BtreeData
1e5d9 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c 20 (u.bh.pCrsr, 0,
1e5da 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 u.bh.n, pOut->z)
1e5db 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e ;. }. pOut->en
1e5dc 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b c = SQLITE_UTF8;
1e5dd 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 /* In case the
1e5de 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 blob is ever ca
1e5df 73 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 st to text */.
1e5e0 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 UPDATE_MAX_BLOBS
1e5e1 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 IZE(pOut);. bre
1e5e2 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e5e3 3a 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 : Rowid P1 P2 *
1e5e4 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 * *.**.** Store
1e5e5 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 in register P2 a
1e5e6 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 n integer which
1e5e7 69 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 is the key of th
1e5e8 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68 e table entry th
1e5e9 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72 at.** P1 is curr
1e5ea 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a ently point to..
1e5eb 2a 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20 **.** P1 can be
1e5ec 65 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61 either an ordina
1e5ed 72 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69 ry table or a vi
1e5ee 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 rtual table. Th
1e5ef 65 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 ere used to.** b
1e5f0 65 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f e a separate OP_
1e5f1 56 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f VRowid opcode fo
1e5f2 72 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75 r use with virtu
1e5f3 61 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74 al tables, but t
1e5f4 68 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 his.** one opcod
1e5f5 65 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20 e now works for
1e5f6 62 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73 both table types
1e5f7 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 ..*/.case OP_Row
1e5f8 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 id: {
1e5f9 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1e5fa 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1e5fb 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e5fc 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e5fd 20 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 43 u.bi */. VdbeC
1e5fe 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 ursor *pC;. i64
1e5ff 20 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 v;. sqlite3_vt
1e600 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e ab *pVtab;. con
1e601 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c st sqlite3_modul
1e602 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 e *pModule;.#end
1e603 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 if /* local vari
1e604 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e605 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 65 u.bi */.. asse
1e606 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 rt( pOp->p1>=0 &
1e607 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 & pOp->p1<p->nCu
1e608 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e 70 rsor );. u.bi.p
1e609 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 C = p->apCsr[pOp
1e60a 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1e60b 20 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a 20 u.bi.pC!=0 );.
1e60c 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 assert( u.bi.pC
1e60d 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 ->pseudoTableReg
1e60e 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 ==0 );. if( u.b
1e60f 69 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b i.pC->nullRow ){
1e610 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 . pOut->flags
1e611 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 = MEM_Null;.
1e612 20 62 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 20 break;. }else
1e613 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 64 65 66 if( u.bi.pC->def
1e614 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 erredMoveto ){.
1e615 20 20 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 u.bi.v = u.bi
1e616 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 .pC->movetoTarge
1e617 74 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 t;.#ifndef SQLIT
1e618 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1e619 42 4c 45 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 BLE. }else if(
1e61a 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 u.bi.pC->pVtabCu
1e61b 72 73 6f 72 20 29 7b 0a 20 20 20 20 75 2e 62 69 rsor ){. u.bi
1e61c 2e 70 56 74 61 62 20 3d 20 75 2e 62 69 2e 70 43 .pVtab = u.bi.pC
1e61d 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 ->pVtabCursor->p
1e61e 56 74 61 62 3b 0a 20 20 20 20 75 2e 62 69 2e 70 Vtab;. u.bi.p
1e61f 4d 6f 64 75 6c 65 20 3d 20 75 2e 62 69 2e 70 56 Module = u.bi.pV
1e620 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 tab->pModule;.
1e621 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 assert( u.bi.p
1e622 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 20 29 Module->xRowid )
1e623 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 ;. if( sqlite
1e624 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 3SafetyOff(db) )
1e625 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1e626 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 to_misuse;. r
1e627 63 20 3d 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 c = u.bi.pModule
1e628 2d 3e 78 52 6f 77 69 64 28 75 2e 62 69 2e 70 43 ->xRowid(u.bi.pC
1e629 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 ->pVtabCursor, &
1e62a 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 73 71 6c u.bi.v);. sql
1e62b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1e62c 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
1e62d 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 62 p->zErrMsg = u.b
1e62e 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 i.pVtab->zErrMsg
1e62f 3b 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 61 62 ;. u.bi.pVtab
1e630 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 ->zErrMsg = 0;.
1e631 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 if( sqlite3Sa
1e632 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1e633 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1e634 69 73 75 73 65 3b 0a 23 65 6e 64 69 66 20 2f 2a isuse;.#endif /*
1e635 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
1e636 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 20 20 7d TUALTABLE */. }
1e637 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 else{. assert
1e638 28 20 75 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 ( u.bi.pC->pCurs
1e639 6f 72 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 or!=0 );. rc
1e63a 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
1e63b 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 69 2e 70 sorMoveto(u.bi.p
1e63c 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 C);. if( rc )
1e63d 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1e63e 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 69 66 to_error;. if
1e63f 28 20 75 2e 62 69 2e 70 43 2d 3e 72 6f 77 69 64 ( u.bi.pC->rowid
1e640 49 73 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 20 IsValid ){.
1e641 20 75 2e 62 69 2e 76 20 3d 20 75 2e 62 69 2e 70 u.bi.v = u.bi.p
1e642 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 C->lastRowid;.
1e643 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 }else{. r
1e644 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e645 4b 65 79 53 69 7a 65 28 75 2e 62 69 2e 70 43 2d KeySize(u.bi.pC-
1e646 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 69 2e >pCursor, &u.bi.
1e647 76 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 v);. assert
1e648 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc==SQLITE_OK
1e649 29 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20 73 6f ); /* Always so
1e64a 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 because of Curs
1e64b 6f 72 4d 6f 76 65 74 6f 28 29 20 61 62 6f 76 65 orMoveto() above
1e64c 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 */. }. }.
1e64d 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 69 pOut->u.i = u.bi
1e64e 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a .v;. break;.}..
1e64f 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 /* Opcode: NullR
1e650 6f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a ow P1 * * * *.**
1e651 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 .** Move the cur
1e652 73 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c sor P1 to a null
1e653 20 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f row. Any OP_Co
1e654 6c 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a lumn operations.
1e655 2a 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 ** that occur wh
1e656 69 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 ile the cursor i
1e657 73 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f s on the null ro
1e658 77 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a w will always.**
1e659 20 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a write a NULL..*
1e65a 2f 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f /.case OP_NullRo
1e65b 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c w: {.#if 0 /* l
1e65c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e65d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a oved into u.bj *
1e65e 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e65f 70 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f pC;.#endif /* lo
1e660 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f cal variables mo
1e661 76 65 64 20 69 6e 74 6f 20 75 2e 62 6a 20 2a 2f ved into u.bj */
1e662 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d .. assert( pOp-
1e663 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 >p1>=0 && pOp->p
1e664 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 1<p->nCursor );.
1e665 20 20 75 2e 62 6a 2e 70 43 20 3d 20 70 2d 3e 61 u.bj.pC = p->a
1e666 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 pCsr[pOp->p1];.
1e667 20 61 73 73 65 72 74 28 20 75 2e 62 6a 2e 70 43 assert( u.bj.pC
1e668 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 !=0 );. u.bj.pC
1e669 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 ->nullRow = 1;.
1e66a 20 75 2e 62 6a 2e 70 43 2d 3e 72 6f 77 69 64 49 u.bj.pC->rowidI
1e66b 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 69 66 sValid = 0;. if
1e66c 28 20 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 ( u.bj.pC->pCurs
1e66d 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 or ){. sqlite
1e66e 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 3BtreeClearCurso
1e66f 72 28 75 2e 62 6a 2e 70 43 2d 3e 70 43 75 72 73 r(u.bj.pC->pCurs
1e670 6f 72 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b or);. }. break
1e671 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e672 4c 61 73 74 20 50 31 20 50 32 20 2a 20 2a 20 2a Last P1 P2 * * *
1e673 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 .**.** The next
1e674 75 73 65 20 6f 66 20 74 68 65 20 52 6f 77 69 64 use of the Rowid
1e675 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 or Column or Ne
1e676 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 xt instruction f
1e677 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 or P1 .** will r
1e678 65 66 65 72 20 74 6f 20 74 68 65 20 6c 61 73 74 efer to the last
1e679 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61 entry in the da
1e67a 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 tabase table or
1e67b 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 index..** If the
1e67c 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 table or index
1e67d 69 73 20 65 6d 70 74 79 20 61 6e 64 20 50 32 3e is empty and P2>
1e67e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 0, then jump imm
1e67f 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a ediately to P2..
1e680 2a 2a 20 49 66 20 50 32 20 69 73 20 30 20 6f 72 ** If P2 is 0 or
1e681 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 if the table or
1e682 20 69 6e 64 65 78 20 69 73 20 6e 6f 74 20 65 6d index is not em
1e683 70 74 79 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 pty, fall throug
1e684 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c h.** to the foll
1e685 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f owing instructio
1e686 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 61 n..*/.case OP_La
1e687 73 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 st: { /*
1e688 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f jump */.#if 0 /
1e689 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1e68a 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 s moved into u.b
1e68b 6b 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f k */. VdbeCurso
1e68c 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f r *pC;. BtCurso
1e68d 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 r *pCrsr;. int
1e68e 72 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c res;.#endif /* l
1e68f 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e690 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6b 20 2a oved into u.bk *
1e691 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 /.. assert( pOp
1e692 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e ->p1>=0 && pOp->
1e693 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b p1<p->nCursor );
1e694 0a 20 20 75 2e 62 6b 2e 70 43 20 3d 20 70 2d 3e . u.bk.pC = p->
1e695 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a apCsr[pOp->p1];.
1e696 20 20 61 73 73 65 72 74 28 20 75 2e 62 6b 2e 70 assert( u.bk.p
1e697 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 C!=0 );. u.bk.p
1e698 43 72 73 72 20 3d 20 75 2e 62 6b 2e 70 43 2d 3e Crsr = u.bk.pC->
1e699 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 pCursor;. if( u
1e69a 2e 62 6b 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a .bk.pCrsr==0 ){.
1e69b 20 20 20 20 75 2e 62 6b 2e 72 65 73 20 3d 20 31 u.bk.res = 1
1e69c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 ;. }else{. r
1e69d 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 c = sqlite3Btree
1e69e 4c 61 73 74 28 75 2e 62 6b 2e 70 43 72 73 72 2c Last(u.bk.pCrsr,
1e69f 20 26 75 2e 62 6b 2e 72 65 73 29 3b 0a 20 20 7d &u.bk.res);. }
1e6a0 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 6e 75 6c 6c . u.bk.pC->null
1e6a1 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6b 2e 72 Row = (u8)u.bk.r
1e6a2 65 73 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 64 es;. u.bk.pC->d
1e6a3 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 eferredMoveto =
1e6a4 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d 3e 72 6f 0;. u.bk.pC->ro
1e6a5 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a widIsValid = 0;.
1e6a6 20 20 75 2e 62 6b 2e 70 43 2d 3e 63 61 63 68 65 u.bk.pC->cache
1e6a7 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e6a8 54 41 4c 45 3b 0a 20 20 69 66 28 20 70 4f 70 2d TALE;. if( pOp-
1e6a9 3e 70 32 3e 30 20 26 26 20 75 2e 62 6b 2e 72 65 >p2>0 && u.bk.re
1e6aa 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f s ){. pc = pO
1e6ab 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 p->p2 - 1;. }.
1e6ac 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f break;.}.../* O
1e6ad 70 63 6f 64 65 3a 20 53 6f 72 74 20 50 31 20 50 pcode: Sort P1 P
1e6ae 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 2 * * *.**.** Th
1e6af 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20 65 is opcode does e
1e6b0 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 xactly the same
1e6b1 74 68 69 6e 67 20 61 73 20 4f 50 5f 52 65 77 69 thing as OP_Rewi
1e6b2 6e 64 20 65 78 63 65 70 74 20 74 68 61 74 0a 2a nd except that.*
1e6b3 2a 20 69 74 20 69 6e 63 72 65 6d 65 6e 74 73 20 * it increments
1e6b4 61 6e 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 20 an undocumented
1e6b5 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 global variable
1e6b6 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 used for testing
1e6b7 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72 74 69 6e 67 20 ..**.** Sorting
1e6b8 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 64 20 is accomplished
1e6b9 62 79 20 77 72 69 74 69 6e 67 20 72 65 63 6f 72 by writing recor
1e6ba 64 73 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e ds into a sortin
1e6bb 67 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 6e g index,.** then
1e6bc 20 72 65 77 69 6e 64 69 6e 67 20 74 68 61 74 20 rewinding that
1e6bd 69 6e 64 65 78 20 61 6e 64 20 70 6c 61 79 69 6e index and playin
1e6be 67 20 69 74 20 62 61 63 6b 20 66 72 6f 6d 20 62 g it back from b
1e6bf 65 67 69 6e 6e 69 6e 67 20 74 6f 0a 2a 2a 20 65 eginning to.** e
1e6c0 6e 64 2e 20 20 57 65 20 75 73 65 20 74 68 65 20 nd. We use the
1e6c1 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 20 69 OP_Sort opcode i
1e6c2 6e 73 74 65 61 64 20 6f 66 20 4f 50 5f 52 65 77 nstead of OP_Rew
1e6c3 69 6e 64 20 74 6f 20 64 6f 20 74 68 65 0a 2a 2a ind to do the.**
1e6c4 20 72 65 77 69 6e 64 69 6e 67 20 73 6f 20 74 68 rewinding so th
1e6c5 61 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 76 61 at the global va
1e6c6 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 69 riable will be i
1e6c7 6e 63 72 65 6d 65 6e 74 65 64 20 61 6e 64 0a 2a ncremented and.*
1e6c8 2a 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65 73 * regression tes
1e6c9 74 73 20 63 61 6e 20 64 65 74 65 72 6d 69 6e 65 ts can determine
1e6ca 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 whether or not
1e6cb 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73 the optimizer is
1e6cc 0a 2a 2a 20 63 6f 72 72 65 63 74 6c 79 20 6f 70 .** correctly op
1e6cd 74 69 6d 69 7a 69 6e 67 20 6f 75 74 20 73 6f 72 timizing out sor
1e6ce 74 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 ts..*/.case OP_S
1e6cf 6f 72 74 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a ort: { /*
1e6d0 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 64 65 66 20 jump */.#ifdef
1e6d1 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 SQLITE_TEST. sq
1e6d2 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 lite3_sort_count
1e6d3 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 ++;. sqlite3_se
1e6d4 61 72 63 68 5f 63 6f 75 6e 74 2d 2d 3b 0a 23 65 arch_count--;.#e
1e6d5 6e 64 69 66 0a 20 20 70 2d 3e 61 43 6f 75 6e 74 ndif. p->aCount
1e6d6 65 72 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 er[SQLITE_STMTST
1e6d7 41 54 55 53 5f 53 4f 52 54 2d 31 5d 2b 2b 3b 0a ATUS_SORT-1]++;.
1e6d8 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 /* Fall throug
1e6d9 68 20 69 6e 74 6f 20 4f 50 5f 52 65 77 69 6e 64 h into OP_Rewind
1e6da 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65 3a */.}./* Opcode:
1e6db 20 52 65 77 69 6e 64 20 50 31 20 50 32 20 2a 20 Rewind P1 P2 *
1e6dc 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 * *.**.** The ne
1e6dd 78 74 20 75 73 65 20 6f 66 20 74 68 65 20 52 6f xt use of the Ro
1e6de 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e 20 6f 72 wid or Column or
1e6df 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f Next instructio
1e6e0 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20 77 69 6c n for P1 .** wil
1e6e1 6c 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 66 l refer to the f
1e6e2 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 irst entry in th
1e6e3 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 e database table
1e6e4 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 or index..** If
1e6e5 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e the table or in
1e6e6 64 65 78 20 69 73 20 65 6d 70 74 79 20 61 6e 64 dex is empty and
1e6e7 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75 6d 70 P2>0, then jump
1e6e8 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 immediately to
1e6e9 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 P2..** If P2 is
1e6ea 30 20 6f 72 20 69 66 20 74 68 65 20 74 61 62 6c 0 or if the tabl
1e6eb 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 6e 6f e or index is no
1e6ec 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20 74 68 t empty, fall th
1e6ed 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 rough.** to the
1e6ee 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 following instru
1e6ef 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f ction..*/.case O
1e6f0 50 5f 52 65 77 69 6e 64 3a 20 7b 20 20 20 20 20 P_Rewind: {
1e6f1 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 /* jump */.#i
1e6f2 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1e6f3 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e6f4 74 6f 20 75 2e 62 6c 20 2a 2f 0a 20 20 56 64 62 to u.bl */. Vdb
1e6f5 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 eCursor *pC;. B
1e6f6 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a tCursor *pCrsr;.
1e6f7 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 int res;.#endi
1e6f8 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e6f9 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e6fa 75 2e 62 6c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 u.bl */.. asser
1e6fb 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 t( pOp->p1>=0 &&
1e6fc 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 pOp->p1<p->nCur
1e6fd 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6c 2e 70 43 sor );. u.bl.pC
1e6fe 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d = p->apCsr[pOp-
1e6ff 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 >p1];. assert(
1e700 75 2e 62 6c 2e 70 43 21 3d 30 20 29 3b 0a 20 20 u.bl.pC!=0 );.
1e701 69 66 28 20 28 75 2e 62 6c 2e 70 43 72 73 72 20 if( (u.bl.pCrsr
1e702 3d 20 75 2e 62 6c 2e 70 43 2d 3e 70 43 75 72 73 = u.bl.pC->pCurs
1e703 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 63 or)!=0 ){. rc
1e704 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 = sqlite3BtreeF
1e705 69 72 73 74 28 75 2e 62 6c 2e 70 43 72 73 72 2c irst(u.bl.pCrsr,
1e706 20 26 75 2e 62 6c 2e 72 65 73 29 3b 0a 20 20 20 &u.bl.res);.
1e707 20 75 2e 62 6c 2e 70 43 2d 3e 61 74 46 69 72 73 u.bl.pC->atFirs
1e708 74 20 3d 20 75 2e 62 6c 2e 72 65 73 3d 3d 30 20 t = u.bl.res==0
1e709 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 ?1:0;. u.bl.p
1e70a 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 C->deferredMovet
1e70b 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6c 2e o = 0;. u.bl.
1e70c 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 pC->cacheStatus
1e70d 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 = CACHE_STALE;.
1e70e 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 72 6f 77 69 u.bl.pC->rowi
1e70f 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 dIsValid = 0;.
1e710 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 6c 2e }else{. u.bl.
1e711 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 75 res = 1;. }. u
1e712 2e 62 6c 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 .bl.pC->nullRow
1e713 3d 20 28 75 38 29 75 2e 62 6c 2e 72 65 73 3b 0a = (u8)u.bl.res;.
1e714 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e715 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 70 2>0 && pOp->p2<p
1e716 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 75 ->nOp );. if( u
1e717 2e 62 6c 2e 72 65 73 20 29 7b 0a 20 20 20 20 70 .bl.res ){. p
1e718 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b c = pOp->p2 - 1;
1e719 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1e71a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 ./* Opcode: Next
1e71b 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a P1 P2 * * *.**.
1e71c 2a 2a 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f ** Advance curso
1e71d 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 r P1 so that it
1e71e 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 points to the ne
1e71f 78 74 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 xt key/data pair
1e720 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 in its.** table
1e721 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 or index. If t
1e722 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 here are no more
1e723 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 key/value pairs
1e724 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 then fall throu
1e725 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c gh.** to the fol
1e726 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 lowing instructi
1e727 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 on. But if the
1e728 63 75 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 cursor advance w
1e729 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a as successful,.*
1e72a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 * jump immediate
1e72b 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ly to P2..**.**
1e72c 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 The P1 cursor mu
1e72d 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c st be for a real
1e72e 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 table, not a ps
1e72f 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a eudo-table..**.*
1e730 2a 20 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 * See also: Prev
1e731 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 .*/./* Opcode: P
1e732 72 65 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a rev P1 P2 * * *.
1e733 2a 2a 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 **.** Back up cu
1e734 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 rsor P1 so that
1e735 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 it points to the
1e736 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 previous key/da
1e737 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a ta pair in its.*
1e738 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 * table or index
1e739 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e . If there is n
1e73a 6f 20 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 o previous key/v
1e73b 61 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 alue pairs then
1e73c 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 fall through.**
1e73d 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 to the following
1e73e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 instruction. B
1e73f 75 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 ut if the cursor
1e740 20 62 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 backup was succ
1e741 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 essful,.** jump
1e742 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 immediately to P
1e743 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 2..**.** The P1
1e744 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 cursor must be f
1e745 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c or a real table,
1e746 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 not a pseudo-ta
1e747 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ble..*/.case OP_
1e748 50 72 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f Prev: /
1e749 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f * jump */.case O
1e74a 50 5f 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 P_Next: {
1e74b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 /* jump */.#if
1e74c 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e74d 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e74e 20 75 2e 62 6d 20 2a 2f 0a 20 20 56 64 62 65 43 u.bm */. VdbeC
1e74f 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 ursor *pC;. BtC
1e750 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 ursor *pCrsr;.
1e751 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69 66 20 int res;.#endif
1e752 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1e753 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1e754 62 6d 20 2a 2f 0a 0a 20 20 43 48 45 43 4b 5f 46 bm */.. CHECK_F
1e755 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 OR_INTERRUPT;.
1e756 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e assert( pOp->p1>
1e757 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d =0 && pOp->p1<p-
1e758 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e >nCursor );. u.
1e759 62 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 bm.pC = p->apCsr
1e75a 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 [pOp->p1];. if(
1e75b 20 75 2e 62 6d 2e 70 43 3d 3d 30 20 29 7b 0a 20 u.bm.pC==0 ){.
1e75c 20 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 53 65 break; /* Se
1e75d 65 20 74 69 63 6b 65 74 20 23 32 32 37 33 20 2a e ticket #2273 *
1e75e 2f 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 72 /. }. u.bm.pCr
1e75f 73 72 20 3d 20 75 2e 62 6d 2e 70 43 2d 3e 70 43 sr = u.bm.pC->pC
1e760 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 ursor;. if( u.b
1e761 6d 2e 70 43 72 73 72 3d 3d 30 20 29 7b 0a 20 20 m.pCrsr==0 ){.
1e762 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 u.bm.pC->nullR
1e763 6f 77 20 3d 20 31 3b 0a 20 20 20 20 62 72 65 61 ow = 1;. brea
1e764 6b 3b 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 72 65 k;. }. u.bm.re
1e765 73 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 s = 1;. assert(
1e766 20 75 2e 62 6d 2e 70 43 2d 3e 64 65 66 65 72 72 u.bm.pC->deferr
1e767 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 edMoveto==0 );.
1e768 20 72 63 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 rc = pOp->opcod
1e769 65 3d 3d 4f 50 5f 4e 65 78 74 20 3f 20 73 71 6c e==OP_Next ? sql
1e76a 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 75 2e ite3BtreeNext(u.
1e76b 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e 62 6d 2e bm.pCrsr, &u.bm.
1e76c 72 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 res) :.
1e76d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1e76e 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1e76f 65 50 72 65 76 69 6f 75 73 28 75 2e 62 6d 2e 70 ePrevious(u.bm.p
1e770 43 72 73 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29 Crsr, &u.bm.res)
1e771 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 6e 75 6c ;. u.bm.pC->nul
1e772 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6d 2e lRow = (u8)u.bm.
1e773 72 65 73 3b 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e res;. u.bm.pC->
1e774 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 cacheStatus = CA
1e775 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66 28 CHE_STALE;. if(
1e776 20 75 2e 62 6d 2e 72 65 73 3d 3d 30 20 29 7b 0a u.bm.res==0 ){.
1e777 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1e778 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 70 4f - 1;. if( pO
1e779 70 2d 3e 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e p->p5 ) p->aCoun
1e77a 74 65 72 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b ter[pOp->p5-1]++
1e77b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
1e77c 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 TEST. sqlite3
1e77d 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b _search_count++;
1e77e 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 75 2e .#endif. }. u.
1e77f 62 6d 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 bm.pC->rowidIsVa
1e780 6c 69 64 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b lid = 0;. break
1e781 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e782 49 64 78 49 6e 73 65 72 74 20 50 31 20 50 32 20 IdxInsert P1 P2
1e783 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 52 65 P3 * P5.**.** Re
1e784 67 69 73 74 65 72 20 50 32 20 68 6f 6c 64 73 20 gister P2 holds
1e785 61 20 53 51 4c 20 69 6e 64 65 78 20 6b 65 79 20 a SQL index key
1e786 6d 61 64 65 20 75 73 69 6e 67 20 74 68 65 0a 2a made using the.*
1e787 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 * MakeRecord ins
1e788 74 72 75 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 tructions. This
1e789 20 6f 70 63 6f 64 65 20 77 72 69 74 65 73 20 74 opcode writes t
1e78a 68 61 74 20 6b 65 79 0a 2a 2a 20 69 6e 74 6f 20 hat key.** into
1e78b 74 68 65 20 69 6e 64 65 78 20 50 31 2e 20 20 44 the index P1. D
1e78c 61 74 61 20 66 6f 72 20 74 68 65 20 65 6e 74 72 ata for the entr
1e78d 79 20 69 73 20 6e 69 6c 2e 0a 2a 2a 0a 2a 2a 20 y is nil..**.**
1e78e 50 33 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 P3 is a flag tha
1e78f 74 20 70 72 6f 76 69 64 65 73 20 61 20 68 69 6e t provides a hin
1e790 74 20 74 6f 20 74 68 65 20 62 2d 74 72 65 65 20 t to the b-tree
1e791 6c 61 79 65 72 20 74 68 61 74 20 74 68 69 73 0a layer that this.
1e792 2a 2a 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b ** insert is lik
1e793 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 ely to be an app
1e794 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 end..**.** This
1e795 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 6c 79 instruction only
1e796 20 77 6f 72 6b 73 20 66 6f 72 20 69 6e 64 69 63 works for indic
1e797 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c es. The equival
1e798 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a ent instruction.
1e799 2a 2a 20 66 6f 72 20 74 61 62 6c 65 73 20 69 73 ** for tables is
1e79a 20 4f 50 5f 49 6e 73 65 72 74 2e 0a 2a 2f 0a 63 OP_Insert..*/.c
1e79b 61 73 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 ase OP_IdxInsert
1e79c 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e : { /* in
1e79d 32 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 2 */.#if 0 /* l
1e79e 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e79f 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e 20 2a oved into u.bn *
1e7a0 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e7a1 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a pC;. BtCursor *
1e7a2 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 6e 4b 65 pCrsr;. int nKe
1e7a3 79 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 y;. const char
1e7a4 2a 7a 4b 65 79 3b 0a 23 65 6e 64 69 66 20 2f 2a *zKey;.#endif /*
1e7a5 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1e7a6 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6e moved into u.bn
1e7a7 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
1e7a8 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 Op->p1>=0 && pOp
1e7a9 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 ->p1<p->nCursor
1e7aa 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 20 3d 20 70 );. u.bn.pC = p
1e7ab 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1e7ac 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e ;. assert( u.bn
1e7ad 2e 70 43 21 3d 30 20 29 3b 0a 20 20 70 49 6e 32 .pC!=0 );. pIn2
1e7ae 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 = &aMem[pOp->p2
1e7af 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e ];. assert( pIn
1e7b0 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 2->flags & MEM_B
1e7b1 6c 6f 62 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 lob );. u.bn.pC
1e7b2 72 73 72 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 rsr = u.bn.pC->p
1e7b3 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c Cursor;. if( AL
1e7b4 57 41 59 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 WAYS(u.bn.pCrsr!
1e7b5 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 =0) ){. asser
1e7b6 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 t( u.bn.pC->isTa
1e7b7 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 ble==0 );. rc
1e7b8 20 3d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 = ExpandBlob(pI
1e7b9 6e 32 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d n2);. if( rc=
1e7ba 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1e7bb 20 20 20 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 u.bn.nKey =
1e7bc 70 49 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 pIn2->n;. u
1e7bd 2e 62 6e 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d .bn.zKey = pIn2-
1e7be 3e 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 >z;. rc = s
1e7bf 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 qlite3BtreeInser
1e7c0 74 28 75 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e t(u.bn.pCrsr, u.
1e7c1 62 6e 2e 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b bn.zKey, u.bn.nK
1e7c2 65 79 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f ey, "", 0, 0, pO
1e7c3 70 2d 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20 p->p3,.
1e7c4 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 ((pOp->p5 & OPF
1e7c5 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c LAG_USESEEKRESUL
1e7c6 54 29 20 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 T) ? u.bn.pC->se
1e7c7 65 6b 52 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 ekResult : 0).
1e7c8 20 20 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 );. ass
1e7c9 65 72 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 ert( u.bn.pC->de
1e7ca 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 ferredMoveto==0
1e7cb 29 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 );. u.bn.pC
1e7cc 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 ->cacheStatus =
1e7cd 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 CACHE_STALE;.
1e7ce 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1e7cf 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 }../* Opcode: Id
1e7d0 78 44 65 6c 65 74 65 20 50 31 20 50 32 20 50 33 xDelete P1 P2 P3
1e7d1 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 * *.**.** The c
1e7d2 6f 6e 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67 ontent of P3 reg
1e7d3 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 isters starting
1e7d4 61 74 20 72 65 67 69 73 74 65 72 20 50 32 20 66 at register P2 f
1e7d5 6f 72 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b orm.** an unpack
1e7d6 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 ed index key. Th
1e7d7 69 73 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 is opcode remove
1e7d8 73 20 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f s that entry fro
1e7d9 6d 20 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 m the .** index
1e7da 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 opened by cursor
1e7db 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f P1..*/.case OP_
1e7dc 49 64 78 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 IdxDelete: {.#if
1e7dd 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1e7de 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e7df 6f 20 75 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 o u.bo */. Vdbe
1e7e0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 Cursor *pC;. Bt
1e7e1 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 Cursor *pCrsr;.
1e7e2 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 int res;. Unpa
1e7e3 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 ckedRecord r;.#e
1e7e4 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1e7e5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1e7e6 74 6f 20 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 to u.bo */.. as
1e7e7 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 sert( pOp->p3>0
1e7e8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 );. assert( pOp
1e7e9 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 ->p2>0 && pOp->p
1e7ea 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 2+pOp->p3<=p->nM
1e7eb 65 6d 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 em+1 );. assert
1e7ec 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 ( pOp->p1>=0 &&
1e7ed 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 pOp->p1<p->nCurs
1e7ee 6f 72 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 or );. u.bo.pC
1e7ef 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e = p->apCsr[pOp->
1e7f0 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e7f1 2e 62 6f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 .bo.pC!=0 );. u
1e7f2 2e 62 6f 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f .bo.pCrsr = u.bo
1e7f3 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 .pC->pCursor;.
1e7f4 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e if( ALWAYS(u.bo.
1e7f5 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 pCrsr!=0) ){.
1e7f6 20 75 2e 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f u.bo.r.pKeyInfo
1e7f7 20 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 = u.bo.pC->pKey
1e7f8 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 Info;. u.bo.r
1e7f9 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 .nField = (u16)p
1e7fa 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f Op->p3;. u.bo
1e7fb 2e 72 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 .r.flags = 0;.
1e7fc 20 20 75 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 u.bo.r.aMem =
1e7fd 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a &aMem[pOp->p2];.
1e7fe 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e7ff 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 BtreeMovetoUnpac
1e800 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 2c 20 ked(u.bo.pCrsr,
1e801 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c 20 26 &u.bo.r, 0, 0, &
1e802 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 20 69 u.bo.res);. i
1e803 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
1e804 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d 30 20 && u.bo.res==0
1e805 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 ){. rc = sq
1e806 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 lite3BtreeDelete
1e807 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a 20 20 (u.bo.pCrsr);.
1e808 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1e809 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 72 65 u.bo.pC->deferre
1e80a 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 dMoveto==0 );.
1e80b 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 68 65 u.bo.pC->cache
1e80c 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 Status = CACHE_S
1e80d 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61 TALE;. }. brea
1e80e 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1e80f 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20 IdxRowid P1 P2
1e810 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1e811 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 e into register
1e812 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 P2 an integer wh
1e813 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20 ich is the last
1e814 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63 entry in the rec
1e815 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e ord at.** the en
1e816 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b d of the index k
1e817 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 ey pointed to by
1e818 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69 cursor P1. Thi
1e819 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64 s integer should
1e81a 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 be.** the rowid
1e81b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e of the table en
1e81c 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69 try to which thi
1e81d 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f s index entry po
1e81e 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 ints..**.** See
1e81f 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b also: Rowid, Mak
1e820 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65 eRecord..*/.case
1e821 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20 OP_IdxRowid: {
1e822 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1e823 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1e824 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1e825 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e826 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a ed into u.bp */.
1e827 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 BtCursor *pCrs
1e828 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 r;. VdbeCursor
1e829 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 69 64 *pC;. i64 rowid
1e82a 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1e82b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1e82c 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a d into u.bp */..
1e82d 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 assert( pOp->p
1e82e 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 1>=0 && pOp->p1<
1e82f 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 p->nCursor );.
1e830 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70 43 u.bp.pC = p->apC
1e831 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 sr[pOp->p1];. a
1e832 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21 3d ssert( u.bp.pC!=
1e833 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 72 73 0 );. u.bp.pCrs
1e834 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 43 75 r = u.bp.pC->pCu
1e835 72 73 6f 72 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c rsor;. pOut->fl
1e836 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a ags = MEM_Null;.
1e837 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 if( ALWAYS(u.b
1e838 70 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a 20 p.pCrsr!=0) ){.
1e839 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 rc = sqlite3V
1e83a 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 dbeCursorMoveto(
1e83b 75 2e 62 70 2e 70 43 29 3b 0a 20 20 20 20 69 66 u.bp.pC);. if
1e83c 28 20 4e 45 56 45 52 28 72 63 29 20 29 20 67 6f ( NEVER(rc) ) go
1e83d 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1e83e 65 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72 error;. asser
1e83f 74 28 20 75 2e 62 70 2e 70 43 2d 3e 64 65 66 65 t( u.bp.pC->defe
1e840 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b rredMoveto==0 );
1e841 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 . assert( u.b
1e842 70 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 p.pC->isTable==0
1e843 20 29 3b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 );. if( !u.b
1e844 70 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b p.pC->nullRow ){
1e845 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
1e846 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 te3VdbeIdxRowid(
1e847 64 62 2c 20 75 2e 62 70 2e 70 43 72 73 72 2c 20 db, u.bp.pCrsr,
1e848 26 75 2e 62 70 2e 72 6f 77 69 64 29 3b 0a 20 20 &u.bp.rowid);.
1e849 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 if( rc!=SQLI
1e84a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 TE_OK ){.
1e84b 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f goto abort_due_
1e84c 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 to_error;.
1e84d 7d 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e }. pOut->u.
1e84e 69 20 3d 20 75 2e 62 70 2e 72 6f 77 69 64 3b 0a i = u.bp.rowid;.
1e84f 20 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 pOut->flag
1e850 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 s = MEM_Int;.
1e851 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a }. }. break;.
1e852 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 }../* Opcode: Id
1e853 78 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20 xGE P1 P2 P3 P4
1e854 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 P5.**.** The P4
1e855 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 register values
1e856 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 beginning with P
1e857 33 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 3 form an unpack
1e858 65 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 ed index .** key
1e859 20 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 that omits the
1e85a 52 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 ROWID. Compare
1e85b 74 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 this key value a
1e85c 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 gainst the index
1e85d 20 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 .** that P1 is
1e85e 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 currently pointi
1e85f 6e 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 ng to, ignoring
1e860 74 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 the ROWID on the
1e861 20 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a P1 index..**.**
1e862 20 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 If the P1 index
1e863 20 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 entry is greate
1e864 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 r than or equal
1e865 74 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 to the key value
1e866 0a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f .** then jump to
1e867 20 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 P2. Otherwise
1e868 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 fall through to
1e869 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 the next instruc
1e86a 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 tion..**.** If P
1e86b 35 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 5 is non-zero th
1e86c 65 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 en the key value
1e86d 20 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 is increased by
1e86e 20 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 an epsilon .**
1e86f 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d prior to the com
1e870 70 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d parison. This m
1e871 61 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 ake the opcode w
1e872 6f 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 ork like IdxGT e
1e873 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 xcept.** that if
1e874 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 the key from re
1e875 67 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70 gister P3 is a p
1e876 72 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 refix of the key
1e877 20 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a in the cursor,.
1e878 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 ** the result is
1e879 20 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 false whereas i
1e87a 74 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 t would be true
1e87b 77 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f with IdxGT..*/./
1e87c 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 * Opcode: IdxLT
1e87d 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a P1 P2 P3 * P5.**
1e87e 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 .** The P4 regis
1e87f 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e ter values begin
1e880 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 ning with P3 for
1e881 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e m an unpacked in
1e882 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 dex .** key that
1e883 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 omits the ROWID
1e884 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 . Compare this
1e885 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 key value agains
1e886 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 t the index .**
1e887 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 that P1 is curre
1e888 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f ntly pointing to
1e889 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 , ignoring the R
1e88a 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 OWID on the P1 i
1e88b 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ndex..**.** If t
1e88c 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 he P1 index entr
1e88d 79 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 y is less than t
1e88e 68 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 he key value the
1e88f 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a n jump to P2..**
1e890 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 Otherwise fall
1e891 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e through to the n
1e892 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e ext instruction.
1e893 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 .**.** If P5 is
1e894 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 non-zero then th
1e895 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 e key value is i
1e896 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 ncreased by an e
1e897 70 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a psilon prior .**
1e898 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 to the comparis
1e899 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 on. This makes
1e89a 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 the opcode work
1e89b 6c 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 like IdxLE..*/.c
1e89c 61 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 ase OP_IdxLT:
1e89d 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a /* jump *
1e89e 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45 3a /.case OP_IdxGE:
1e89f 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d { /* jum
1e8a0 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c p */.#if 0 /* l
1e8a1 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1e8a2 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a oved into u.bq *
1e8a3 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a /. VdbeCursor *
1e8a4 70 43 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 pC;. int res;.
1e8a5 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 UnpackedRecord
1e8a6 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 r;.#endif /* loc
1e8a7 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1e8a8 65 64 20 69 6e 74 6f 20 75 2e 62 71 20 2a 2f 0a ed into u.bq */.
1e8a9 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1e8aa 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 p1>=0 && pOp->p1
1e8ab 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 <p->nCursor );.
1e8ac 20 75 2e 62 71 2e 70 43 20 3d 20 70 2d 3e 61 70 u.bq.pC = p->ap
1e8ad 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Csr[pOp->p1];.
1e8ae 61 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 21 assert( u.bq.pC!
1e8af 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 =0 );. if( ALWA
1e8b0 59 53 28 75 2e 62 71 2e 70 43 2d 3e 70 43 75 72 YS(u.bq.pC->pCur
1e8b1 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61 sor!=0) ){. a
1e8b2 73 73 65 72 74 28 20 75 2e 62 71 2e 70 43 2d 3e ssert( u.bq.pC->
1e8b3 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d deferredMoveto==
1e8b4 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
1e8b5 20 70 4f 70 2d 3e 70 35 3d 3d 30 20 7c 7c 20 70 pOp->p5==0 || p
1e8b6 4f 70 2d 3e 70 35 3d 3d 31 20 29 3b 0a 20 20 20 Op->p5==1 );.
1e8b7 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 assert( pOp->p4
1e8b8 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 type==P4_INT32 )
1e8b9 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 70 4b 65 ;. u.bq.r.pKe
1e8ba 79 49 6e 66 6f 20 3d 20 75 2e 62 71 2e 70 43 2d yInfo = u.bq.pC-
1e8bb 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 >pKeyInfo;. u
1e8bc 2e 62 71 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 .bq.r.nField = (
1e8bd 75 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 u16)pOp->p4.i;.
1e8be 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 if( pOp->p5 )
1e8bf 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 2e 66 {. u.bq.r.f
1e8c0 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f lags = UNPACKED_
1e8c1 49 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b INCRKEY | UNPACK
1e8c2 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b ED_IGNORE_ROWID;
1e8c3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1e8c4 20 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d u.bq.r.flags =
1e8c5 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 UNPACKED_IGNORE
1e8c6 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 _ROWID;. }.
1e8c7 20 20 75 2e 62 71 2e 72 2e 61 4d 65 6d 20 3d 20 u.bq.r.aMem =
1e8c8 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a &aMem[pOp->p3];.
1e8c9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
1e8ca 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 VdbeIdxKeyCompar
1e8cb 65 28 75 2e 62 71 2e 70 43 2c 20 26 75 2e 62 71 e(u.bq.pC, &u.bq
1e8cc 2e 72 2c 20 26 75 2e 62 71 2e 72 65 73 29 3b 0a .r, &u.bq.res);.
1e8cd 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 if( pOp->opc
1e8ce 6f 64 65 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 7b ode==OP_IdxLT ){
1e8cf 0a 20 20 20 20 20 20 75 2e 62 71 2e 72 65 73 20 . u.bq.res
1e8d0 3d 20 2d 75 2e 62 71 2e 72 65 73 3b 0a 20 20 20 = -u.bq.res;.
1e8d1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 }else{. as
1e8d2 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 sert( pOp->opcod
1e8d3 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20 e==OP_IdxGE );.
1e8d4 20 20 20 20 20 75 2e 62 71 2e 72 65 73 2b 2b 3b u.bq.res++;
1e8d5 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 . }. if( u
1e8d6 2e 62 71 2e 72 65 73 3e 30 20 29 7b 0a 20 20 20 .bq.res>0 ){.
1e8d7 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 pc = pOp->p2
1e8d8 2d 20 31 20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a - 1 ;. }. }.
1e8d9 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1e8da 70 63 6f 64 65 3a 20 44 65 73 74 72 6f 79 20 50 pcode: Destroy P
1e8db 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 1 P2 P3 * *.**.*
1e8dc 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 * Delete an enti
1e8dd 72 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c re database tabl
1e8de 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 e or index whose
1e8df 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 root page in th
1e8e0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 e database.** fi
1e8e1 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 le is given by P
1e8e2 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 1..**.** The tab
1e8e3 6c 65 20 62 65 69 6e 67 20 64 65 73 74 72 6f 79 le being destroy
1e8e4 65 64 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69 ed is in the mai
1e8e5 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 n database file
1e8e6 69 66 20 50 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a if P3==0. If.**
1e8e7 20 50 33 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 P3==1 then the
1e8e8 74 61 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 table to be clea
1e8e9 72 20 69 73 20 69 6e 20 74 68 65 20 61 75 78 69 r is in the auxi
1e8ea 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 liary database f
1e8eb 69 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 ile.** that is u
1e8ec 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62 sed to store tab
1e8ed 6c 65 73 20 63 72 65 61 74 65 20 75 73 69 6e 67 les create using
1e8ee 20 43 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52 CREATE TEMPORAR
1e8ef 59 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 Y TABLE..**.** I
1e8f0 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 f AUTOVACUUM is
1e8f1 65 6e 61 62 6c 65 64 20 74 68 65 6e 20 69 74 20 enabled then it
1e8f2 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 is possible that
1e8f3 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 20 70 61 another root pa
1e8f4 67 65 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 6d ge.** might be m
1e8f5 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6e 65 oved into the ne
1e8f6 77 6c 79 20 64 65 6c 65 74 65 64 20 72 6f 6f 74 wly deleted root
1e8f7 20 70 61 67 65 20 69 6e 20 6f 72 64 65 72 20 74 page in order t
1e8f8 6f 20 6b 65 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f o keep all.** ro
1e8f9 6f 74 20 70 61 67 65 73 20 63 6f 6e 74 69 67 75 ot pages contigu
1e8fa 6f 75 73 20 61 74 20 74 68 65 20 62 65 67 69 6e ous at the begin
1e8fb 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 ning of the data
1e8fc 62 61 73 65 2e 20 20 54 68 65 20 66 6f 72 6d 65 base. The forme
1e8fd 72 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 r.** value of th
1e8fe 65 20 72 6f 6f 74 20 70 61 67 65 20 74 68 61 74 e root page that
1e8ff 20 6d 6f 76 65 64 20 2d 20 69 74 73 20 76 61 6c moved - its val
1e900 75 65 20 62 65 66 6f 72 65 20 74 68 65 20 6d 6f ue before the mo
1e901 76 65 20 6f 63 63 75 72 72 65 64 20 2d 0a 2a 2a ve occurred -.**
1e902 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 is stored in re
1e903 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 6e gister P2. If n
1e904 6f 20 70 61 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d o page .** movem
1e905 65 6e 74 20 77 61 73 20 72 65 71 75 69 72 65 64 ent was required
1e906 20 28 62 65 63 61 75 73 65 20 74 68 65 20 74 61 (because the ta
1e907 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 ble being droppe
1e908 64 20 77 61 73 20 61 6c 72 65 61 64 79 20 0a 2a d was already .*
1e909 2a 20 74 68 65 20 6c 61 73 74 20 6f 6e 65 20 69 * the last one i
1e90a 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 29 20 n the database)
1e90b 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 then a zero is s
1e90c 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
1e90d 72 20 50 32 2e 0a 2a 2a 20 49 66 20 41 55 54 4f r P2..** If AUTO
1e90e 56 41 43 55 55 4d 20 69 73 20 64 69 73 61 62 6c VACUUM is disabl
1e90f 65 64 20 74 68 65 6e 20 61 20 7a 65 72 6f 20 69 ed then a zero i
1e910 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 s stored in regi
1e911 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 ster P2..**.** S
1e912 65 65 20 61 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a ee also: Clear.*
1e913 2f 0a 63 61 73 65 20 4f 50 5f 44 65 73 74 72 6f /.case OP_Destro
1e914 79 3a 20 7b 20 20 20 20 20 2f 2a 20 6f 75 74 32 y: { /* out2
1e915 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 -prerelease */.#
1e916 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e917 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e918 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 20 20 69 6e nto u.br */. in
1e919 74 20 69 4d 6f 76 65 64 3b 0a 20 20 69 6e 74 20 t iMoved;. int
1e91a 69 43 6e 74 3b 0a 20 20 56 64 62 65 20 2a 70 56 iCnt;. Vdbe *pV
1e91b 64 62 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a dbe;. int iDb;.
1e91c 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1e91d 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1e91e 69 6e 74 6f 20 75 2e 62 72 20 2a 2f 0a 23 69 66 into u.br */.#if
1e91f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1e920 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 _VIRTUALTABLE.
1e921 75 2e 62 72 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 u.br.iCnt = 0;.
1e922 20 66 6f 72 28 75 2e 62 72 2e 70 56 64 62 65 3d for(u.br.pVdbe=
1e923 64 62 2d 3e 70 56 64 62 65 3b 20 75 2e 62 72 2e db->pVdbe; u.br.
1e924 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 pVdbe; u.br.pVdb
1e925 65 20 3d 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e e = u.br.pVdbe->
1e926 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
1e927 75 2e 62 72 2e 70 56 64 62 65 2d 3e 6d 61 67 69 u.br.pVdbe->magi
1e928 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 c==VDBE_MAGIC_RU
1e929 4e 20 26 26 20 75 2e 62 72 2e 70 56 64 62 65 2d N && u.br.pVdbe-
1e92a 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 3c 32 20 >inVtabMethod<2
1e92b 26 26 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 && u.br.pVdbe->p
1e92c 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 2e c>=0 ){. u.
1e92d 62 72 2e 69 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d br.iCnt++;. }
1e92e 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 75 2e 62 . }.#else. u.b
1e92f 72 2e 69 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 r.iCnt = db->act
1e930 69 76 65 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64 iveVdbeCnt;.#end
1e931 69 66 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 if. pOut->flags
1e932 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 = MEM_Null;. i
1e933 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e 31 20 29 f( u.br.iCnt>1 )
1e934 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 {. rc = SQLIT
1e935 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d E_LOCKED;. p-
1e936 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f >errorAction = O
1e937 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 E_Abort;. }else
1e938 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 62 20 3d {. u.br.iDb =
1e939 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61 73 pOp->p3;. as
1e93a 73 65 72 74 28 20 75 2e 62 72 2e 69 43 6e 74 3d sert( u.br.iCnt=
1e93b 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 =1 );. assert
1e93c 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1e93d 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 62 29 29 & (1<<u.br.iDb))
1e93e 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 !=0 );. rc =
1e93f 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 sqlite3BtreeDrop
1e940 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 75 2e Table(db->aDb[u.
1e941 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 br.iDb].pBt, pOp
1e942 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 4d 6f 76 ->p1, &u.br.iMov
1e943 65 64 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 ed);. pOut->f
1e944 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a lags = MEM_Int;.
1e945 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 pOut->u.i =
1e946 75 2e 62 72 2e 69 4d 6f 76 65 64 3b 0a 23 69 66 u.br.iMoved;.#if
1e947 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
1e948 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 _AUTOVACUUM.
1e949 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f if( rc==SQLITE_O
1e94a 4b 20 26 26 20 75 2e 62 72 2e 69 4d 6f 76 65 64 K && u.br.iMoved
1e94b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c !=0 ){. sql
1e94c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 ite3RootPageMove
1e94d 64 28 26 64 62 2d 3e 61 44 62 5b 75 2e 62 72 2e d(&db->aDb[u.br.
1e94e 69 44 62 5d 2c 20 75 2e 62 72 2e 69 4d 6f 76 65 iDb], u.br.iMove
1e94f 64 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 d, pOp->p1);.
1e950 20 20 20 72 65 73 65 74 53 63 68 65 6d 61 4f 6e resetSchemaOn
1e951 46 61 75 6c 74 20 3d 20 31 3b 0a 20 20 20 20 7d Fault = 1;. }
1e952 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 .#endif. }. br
1e953 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1e954 65 3a 20 43 6c 65 61 72 20 50 31 20 50 32 20 50 e: Clear P1 P2 P
1e955 33 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 3.**.** Delete a
1e956 6c 6c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 ll contents of t
1e957 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c he database tabl
1e958 65 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 e or index whose
1e959 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e root page.** in
1e95a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 the database fi
1e95b 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 le is given by P
1e95c 31 2e 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 1. But, unlike
1e95d 44 65 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a Destroy, do not.
1e95e 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 ** remove the ta
1e95f 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f ble or index fro
1e960 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 m the database f
1e961 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 ile..**.** The t
1e962 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 able being clear
1e963 20 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 is in the main
1e964 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 database file if
1e965 20 50 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 P2==0. If.** P
1e966 32 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 2==1 then the ta
1e967 62 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 ble to be clear
1e968 69 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 is in the auxili
1e969 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c ary database fil
1e96a 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 e.** that is use
1e96b 64 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 d to store table
1e96c 73 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 s create using C
1e96d 52 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 REATE TEMPORARY
1e96e 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 TABLE..**.** If
1e96f 74 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 the P3 value is
1e970 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 non-zero, then t
1e971 68 65 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 he table referre
1e972 64 20 74 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a d to must be an.
1e973 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 ** intkey table
1e974 28 61 6e 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e (an SQL table, n
1e975 6f 74 20 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e ot an index). In
1e976 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72 this case the r
1e977 6f 77 20 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f ow change .** co
1e978 75 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 unt is increment
1e979 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 ed by the number
1e97a 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 of rows in the
1e97b 74 61 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 table being clea
1e97c 72 65 64 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 red. .** If P3 i
1e97d 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a s greater than z
1e97e 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 76 61 ero, then the va
1e97f 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 72 65 lue stored in re
1e980 67 69 73 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 gister P3 is.**
1e981 61 6c 73 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 also incremented
1e982 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
1e983 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 f rows in the ta
1e984 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 ble being cleare
1e985 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 d..**.** See als
1e986 6f 3a 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 o: Destroy.*/.ca
1e987 73 65 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 se OP_Clear: {.#
1e988 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 if 0 /* local v
1e989 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 ariables moved i
1e98a 6e 74 6f 20 75 2e 62 73 20 2a 2f 0a 20 20 69 6e nto u.bs */. in
1e98b 74 20 6e 43 68 61 6e 67 65 3b 0a 23 65 6e 64 69 t nChange;.#endi
1e98c 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 f /* local varia
1e98d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 bles moved into
1e98e 75 2e 62 73 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e u.bs */.. u.bs.
1e98f 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 61 nChange = 0;. a
1e990 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 ssert( (p->btree
1e991 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e Mask & (1<<pOp->
1e992 70 32 29 29 21 3d 30 20 29 3b 0a 20 20 72 63 20 p2))!=0 );. rc
1e993 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c = sqlite3BtreeCl
1e994 65 61 72 54 61 62 6c 65 28 0a 20 20 20 20 20 20 earTable(.
1e995 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 32 5d db->aDb[pOp->p2]
1e996 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 .pBt, pOp->p1, (
1e997 70 4f 70 2d 3e 70 33 20 3f 20 26 75 2e 62 73 2e pOp->p3 ? &u.bs.
1e998 6e 43 68 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 nChange : 0). )
1e999 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 ;. if( pOp->p3
1e99a 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 ){. p->nChang
1e99b 65 20 2b 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 e += u.bs.nChang
1e99c 65 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e e;. if( pOp->
1e99d 70 33 3e 30 20 29 7b 0a 20 20 20 20 20 20 61 4d p3>0 ){. aM
1e99e 65 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 em[pOp->p3].u.i
1e99f 2b 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b += u.bs.nChange;
1e9a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 . }. }. bre
1e9a1 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 ak;.}../* Opcode
1e9a2 3a 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 : CreateTable P1
1e9a3 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1e9a4 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 Allocate a new t
1e9a5 61 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e able in the main
1e9a6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 database file i
1e9a7 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 f P1==0 or in th
1e9a8 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 e.** auxiliary d
1e9a9 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 atabase file if
1e9aa 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 P1==1 or in an a
1e9ab 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 ttached database
1e9ac 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 if.** P1>1. Wr
1e9ad 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 ite the root pag
1e9ae 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 e number of the
1e9af 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a new table into.*
1e9b0 2a 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a * register P2.**
1e9b1 0a 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e .** The differen
1e9b2 63 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 ce between a tab
1e9b3 6c 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 le and an index
1e9b4 69 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c is this: A tabl
1e9b5 65 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 e must.** have a
1e9b6 20 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 4-byte integer
1e9b7 6b 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 key and can have
1e9b8 20 61 72 62 69 74 72 61 72 79 20 64 61 74 61 2e arbitrary data.
1e9b9 20 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 An index.** ha
1e9ba 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b s an arbitrary k
1e9bb 65 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a ey but no data..
1e9bc 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 **.** See also:
1e9bd 43 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f CreateIndex.*/./
1e9be 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 * Opcode: Create
1e9bf 49 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 Index P1 P2 * *
1e9c0 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 *.**.** Allocate
1e9c1 20 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 a new index in
1e9c2 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 the main databas
1e9c3 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 e file if P1==0
1e9c4 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 or in the.** aux
1e9c5 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 iliary database
1e9c6 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 file if P1==1 or
1e9c7 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 in an attached
1e9c8 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 database if.** P
1e9c9 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 1>1. Write the
1e9ca 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
1e9cb 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c of the new tabl
1e9cc 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 e into.** regist
1e9cd 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 er P2..**.** See
1e9ce 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f documentation o
1e9cf 6e 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 n OP_CreateTable
1e9d0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 for additional
1e9d1 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a information..*/.
1e9d2 63 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e case OP_CreateIn
1e9d3 64 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 dex:
1e9d4 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 /* out2-prerelea
1e9d5 73 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 se */.case OP_Cr
1e9d6 65 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 eateTable: {
1e9d7 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 /* out2-pr
1e9d8 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 erelease */.#if
1e9d9 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1e9da 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1e9db 20 75 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70 u.bt */. int p
1e9dc 67 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 gno;. int flags
1e9dd 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e ;. Db *pDb;.#en
1e9de 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1e9df 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1e9e0 6f 20 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 o u.bt */.. u.b
1e9e1 74 2e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 t.pgno = 0;. as
1e9e2 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1e9e3 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e && pOp->p1<db->
1e9e4 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nDb );. assert(
1e9e5 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1e9e6 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d (1<<pOp->p1))!=
1e9e7 30 20 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20 0 );. u.bt.pDb
1e9e8 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e = &db->aDb[pOp->
1e9e9 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 p1];. assert( u
1e9ea 2e 62 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 .bt.pDb->pBt!=0
1e9eb 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 );. if( pOp->op
1e9ec 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 code==OP_CreateT
1e9ed 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 able ){. /* u
1e9ee 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 .bt.flags = BTRE
1e9ef 45 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 E_INTKEY; */.
1e9f0 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 u.bt.flags = BT
1e9f1 52 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 REE_LEAFDATA|BTR
1e9f2 45 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c EE_INTKEY;. }el
1e9f3 73 65 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 se{. u.bt.fla
1e9f4 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 gs = BTREE_ZEROD
1e9f5 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 ATA;. }. rc =
1e9f6 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 sqlite3BtreeCrea
1e9f7 74 65 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62 teTable(u.bt.pDb
1e9f8 2d 3e 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e ->pBt, &u.bt.pgn
1e9f9 6f 2c 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a o, u.bt.flags);.
1e9fa 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e pOut->u.i = u.
1e9fb 62 74 2e 70 67 6e 6f 3b 0a 20 20 62 72 65 61 6b bt.pgno;. break
1e9fc 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ;.}../* Opcode:
1e9fd 50 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 50 ParseSchema P1 P
1e9fe 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 2 * P4 *.**.** R
1e9ff 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c ead and parse al
1ea00 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 l entries from t
1ea01 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 he SQLITE_MASTER
1ea02 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 table of databa
1ea03 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 se P1.** that ma
1ea04 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c tch the WHERE cl
1ea05 61 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 20 ause P4. P2 is
1ea06 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67 the "force" flag
1ea07 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a . Always do.**
1ea08 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 20 the parsing if
1ea09 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 P2 is true. If
1ea0a 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 P2 is false, the
1ea0b 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 n this routine i
1ea0c 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 s a.** no-op if
1ea0d 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f the schema is no
1ea0e 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 t currently load
1ea0f 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f ed. In other wo
1ea10 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 rds, if P2.** is
1ea11 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 false, the SQLI
1ea12 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 TE_MASTER table
1ea13 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 is only parsed i
1ea14 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 f the rest of th
1ea15 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61 e.** schema is a
1ea16 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e lready loaded in
1ea17 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 to the symbol ta
1ea18 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ble..**.** This
1ea19 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 opcode invokes t
1ea1a 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 65 he parser to cre
1ea1b 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 ate a new virtua
1ea1c 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 l machine,.** th
1ea1d 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20 en runs the new
1ea1e 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e virtual machine.
1ea1f 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20 72 It is thus a r
1ea20 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 e-entrant opcode
1ea21 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 ..*/.case OP_Par
1ea22 73 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 20 seSchema: {.#if
1ea23 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1ea24 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1ea25 20 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 69 u.bu */. int i
1ea26 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 Db;. const char
1ea27 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 61 *zMaster;. cha
1ea28 72 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 44 r *zSql;. InitD
1ea29 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 65 ata initData;.#e
1ea2a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ea2b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ea2c 74 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e to u.bu */.. u.
1ea2d 62 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 bu.iDb = pOp->p1
1ea2e 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75 ;. assert( u.bu
1ea2f 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 2e .iDb>=0 && u.bu.
1ea30 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a iDb<db->nDb );..
1ea31 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20 /* If pOp->p2
1ea32 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 20 is 0, then this
1ea33 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 20 opcode is being
1ea34 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 64 executed to read
1ea35 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72 a. ** single r
1ea36 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 ow, for example
1ea37 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 6f the row correspo
1ea38 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 69 nding to a new i
1ea39 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 65 ndex. ** create
1ea3a 64 20 62 79 20 74 68 69 73 20 56 44 42 45 2c 20 d by this VDBE,
1ea3b 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f from the sqlite_
1ea3c 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 74 master table. It
1ea3d 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20 only. ** does
1ea3e 74 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 72 this if the corr
1ea3f 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d esponding in-mem
1ea40 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 75 ory schema is cu
1ea41 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 rrently. ** loa
1ea42 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ded. Otherwise,
1ea43 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 65 the new index de
1ea44 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 20 finition can be
1ea45 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a loaded along. *
1ea46 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 * with the rest
1ea47 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 68 of the schema wh
1ea48 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 65 en it is require
1ea49 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 d.. **. ** Alt
1ea4a 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 20 hough the mutex
1ea4b 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 on the BtShared
1ea4c 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 72 object that corr
1ea4d 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 esponds to. **
1ea4e 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 44 database u.bu.iD
1ea4f 62 20 28 74 68 65 20 64 61 74 61 62 61 73 65 20 b (the database
1ea50 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 containing the s
1ea51 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 qlite_master tab
1ea52 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20 le. ** read by
1ea53 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e this instruction
1ea54 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 ) is currently h
1ea55 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 73 eld, it is neces
1ea56 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 sary to. ** obt
1ea57 61 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 ain the mutexes
1ea58 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 on all attached
1ea59 64 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 65 databases before
1ea5a 20 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a checking if. *
1ea5b 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 * the schema of
1ea5c 75 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 64 u.bu.iDb is load
1ea5d 65 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 ed. This is beca
1ea5e 75 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 72 use, at the star
1ea5f 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 t of. ** the sq
1ea60 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c lite3_exec() cal
1ea61 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20 l below, SQLite
1ea62 77 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a will invoke. **
1ea63 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 sqlite3BtreeEnt
1ea64 65 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20 erAll(). If all
1ea65 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 mutexes are not
1ea66 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 already held, th
1ea67 65 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 20 e. ** u.bu.iDb
1ea68 6d 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d mutex may be tem
1ea69 70 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65 porarily release
1ea6a 64 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c d to avoid deadl
1ea6b 6f 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 69 ock. If. ** thi
1ea6c 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 s happens, then
1ea6d 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 some other threa
1ea6e 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65 d may delete the
1ea6f 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 in-memory. **
1ea70 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61 schema of databa
1ea71 73 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 6f se u.bu.iDb befo
1ea72 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 re the SQL state
1ea73 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 73 ment runs. The s
1ea74 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 chema. ** will
1ea75 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20 not be reloaded
1ea76 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d 3e becuase the db->
1ea77 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 69 init.busy flag i
1ea78 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a 2a s set. This. **
1ea79 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61 can result in a
1ea7a 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a "no such table:
1ea7b 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 sqlite_master"
1ea7c 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 or "malformed.
1ea7d 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 ** database sche
1ea7e 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 20 ma" error being
1ea7f 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 returned to the
1ea80 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 user.. */. ass
1ea81 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 ert( sqlite3Btre
1ea82 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e eHoldsMutex(db->
1ea83 61 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 42 aDb[u.bu.iDb].pB
1ea84 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 t) );. sqlite3B
1ea85 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 treeEnterAll(db)
1ea86 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 ;. if( pOp->p2
1ea87 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 || DbHasProperty
1ea88 28 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 44 (db, u.bu.iDb, D
1ea89 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 B_SchemaLoaded)
1ea8a 29 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 73 ){. u.bu.zMas
1ea8b 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 ter = SCHEMA_TAB
1ea8c 4c 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 20 LE(u.bu.iDb);.
1ea8d 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e u.bu.initData.
1ea8e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e 62 db = db;. u.b
1ea8f 75 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d u.initData.iDb =
1ea90 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 2e pOp->p1;. u.
1ea91 62 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 bu.initData.pzEr
1ea92 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d rMsg = &p->zErrM
1ea93 73 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 71 sg;. u.bu.zSq
1ea94 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e l = sqlite3MPrin
1ea95 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 53 tf(db,. "S
1ea96 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 ELECT name, root
1ea97 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 page, sql FROM '
1ea98 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 22 %q'.%s WHERE %s"
1ea99 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 ,. db->aDb
1ea9a 5b 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65 [u.bu.iDb].zName
1ea9b 2c 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c 20 , u.bu.zMaster,
1ea9c 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 pOp->p4.z);.
1ea9d 69 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30 if( u.bu.zSql==0
1ea9e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 ){. rc = S
1ea9f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 QLITE_NOMEM;.
1eaa0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 }else{. (v
1eaa1 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 oid)sqlite3Safet
1eaa2 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 yOff(db);.
1eaa3 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 assert( db->init
1eaa4 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 .busy==0 );.
1eaa5 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 db->init.busy
1eaa6 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 2e = 1;. u.bu.
1eaa7 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51 initData.rc = SQ
1eaa8 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 LITE_OK;. a
1eaa9 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c ssert( !db->mall
1eaaa 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
1eaab 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 rc = sqlite3_e
1eaac 78 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 xec(db, u.bu.zSq
1eaad 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 l, sqlite3InitCa
1eaae 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 6e llback, &u.bu.in
1eaaf 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 itData, 0);.
1eab0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
1eab1 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 2e _OK ) rc = u.bu.
1eab2 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 initData.rc;.
1eab3 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1eab4 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 3b (db, u.bu.zSql);
1eab5 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e . db->init.
1eab6 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 busy = 0;.
1eab7 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 (void)sqlite3Saf
1eab8 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d etyOn(db);. }
1eab9 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 . }. sqlite3Bt
1eaba 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b reeLeaveAll(db);
1eabb 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1eabc 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67 E_NOMEM ){. g
1eabd 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a oto no_mem;. }.
1eabe 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 break;.}..#if
1eabf 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
1eac0 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a OMIT_ANALYZE)./*
1eac1 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 Opcode: LoadAna
1eac2 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a lysis P1 * * * *
1eac3 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 .**.** Read the
1eac4 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 sqlite_stat1 tab
1eac5 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 le for database
1eac6 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 P1 and load the
1eac7 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 content.** of th
1eac8 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 at table into th
1eac9 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 e internal index
1eaca 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 hash table. Th
1eacb 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a is will cause.**
1eacc 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f the analysis to
1eacd 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 72 be used when pr
1eace 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 eparing all subs
1eacf 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a equent queries..
1ead0 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 */.case OP_LoadA
1ead1 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 73 nalysis: {. ass
1ead2 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 ert( pOp->p1>=0
1ead3 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e && pOp->p1<db->n
1ead4 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c Db );. rc = sql
1ead5 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 ite3AnalysisLoad
1ead6 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 (db, pOp->p1);.
1ead7 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 break; .}.#end
1ead8 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 if /* !defined(S
1ead9 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 QLITE_OMIT_ANALY
1eada 5a 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 ZE) */../* Opcod
1eadb 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 e: DropTable P1
1eadc 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 * * P4 *.**.** R
1eadd 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e emove the intern
1eade 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 al (in-memory) d
1eadf 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 ata structures t
1eae0 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 hat describe.**
1eae1 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 the table named
1eae2 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 P4 in database P
1eae3 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 1. This is call
1eae4 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 ed after a table
1eae5 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 .** is dropped i
1eae6 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 n order to keep
1eae7 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 the internal rep
1eae8 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 resentation of t
1eae9 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e he.** schema con
1eaea 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 sistent with wha
1eaeb 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f t is on disk..*/
1eaec 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 .case OP_DropTab
1eaed 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 le: {. sqlite3U
1eaee 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 nlinkAndDeleteTa
1eaef 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c ble(db, pOp->p1,
1eaf0 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 pOp->p4.z);. b
1eaf1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f reak;.}../* Opco
1eaf2 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 de: DropIndex P1
1eaf3 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 * * P4 *.**.**
1eaf4 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 Remove the inter
1eaf5 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 nal (in-memory)
1eaf6 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 data structures
1eaf7 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a that describe.**
1eaf8 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 the index named
1eaf9 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 P4 in database
1eafa 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c P1. This is cal
1eafb 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 led after an ind
1eafc 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 ex.** is dropped
1eafd 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 in order to kee
1eafe 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 p the internal r
1eaff 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 epresentation of
1eb00 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 the.** schema c
1eb01 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 onsistent with w
1eb02 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a hat is on disk..
1eb03 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 */.case OP_DropI
1eb04 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 ndex: {. sqlite
1eb05 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 3UnlinkAndDelete
1eb06 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 Index(db, pOp->p
1eb07 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 1, pOp->p4.z);.
1eb08 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 break;.}../* Op
1eb09 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 code: DropTrigge
1eb0a 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a r P1 * * P4 *.**
1eb0b 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 .** Remove the i
1eb0c 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f nternal (in-memo
1eb0d 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75 ry) data structu
1eb0e 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62 res that describ
1eb0f 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 e.** the trigger
1eb10 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 named P4 in dat
1eb11 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 abase P1. This
1eb12 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
1eb13 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 a trigger.** is
1eb14 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 dropped in order
1eb15 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 to keep the int
1eb16 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 ernal representa
1eb17 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 tion of the.** s
1eb18 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 chema consistent
1eb19 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e with what is on
1eb1a 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f disk..*/.case O
1eb1b 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b P_DropTrigger: {
1eb1c 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b . sqlite3Unlink
1eb1d 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 AndDeleteTrigger
1eb1e 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f (db, pOp->p1, pO
1eb1f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 p->p4.z);. brea
1eb20 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 k;.}...#ifndef S
1eb21 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 QLITE_OMIT_INTEG
1eb22 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 RITY_CHECK./* Op
1eb23 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 code: IntegrityC
1eb24 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a k P1 P2 P3 * P5.
1eb25 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c **.** Do an anal
1eb26 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 ysis of the curr
1eb27 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 ently open datab
1eb28 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a ase. Store in.*
1eb29 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68 * register P1 th
1eb2a 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 e text of an err
1eb2b 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 or message descr
1eb2c 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 ibing any proble
1eb2d 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f ms..** If no pro
1eb2e 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c blems are found,
1eb2f 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e store a NULL in
1eb30 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a register P1..**
1eb31 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 .** The register
1eb32 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 P3 contains the
1eb33 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 maximum number
1eb34 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 of allowed error
1eb35 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 s..** At most re
1eb36 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c g(P3) errors wil
1eb37 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a l be reported..*
1eb38 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 * In other words
1eb39 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 , the analysis s
1eb3a 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 tops as soon as
1eb3b 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 reg(P1) errors a
1eb3c 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 re .** seen. Re
1eb3d 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64 g(P1) is updated
1eb3e 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 with the number
1eb3f 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 of errors remai
1eb40 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 ning..**.** The
1eb41 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 root page number
1eb42 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 s of all tables
1eb43 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 in the database
1eb44 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 are integer.** s
1eb45 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 tored in reg(P1)
1eb46 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 , reg(P1+1), reg
1eb47 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 (P1+2), .... Th
1eb48 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 ere are P2 table
1eb49 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a s.** total..**.*
1eb4a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a * If P5 is not z
1eb4b 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 ero, the check i
1eb4c 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 s done on the au
1eb4d 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 xiliary database
1eb4e 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 .** file, not th
1eb4f 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 e main database
1eb50 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 file..**.** This
1eb51 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 opcode is used
1eb52 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 to implement the
1eb53 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b integrity_check
1eb54 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 pragma..*/.case
1eb55 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a OP_IntegrityCk:
1eb56 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1eb57 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1eb58 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a ed into u.bv */.
1eb59 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 int nRoot;
1eb5a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 /* Number of t
1eb5b 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 ables to check.
1eb5c 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 (Number of root
1eb5d 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e pages.) */. in
1eb5e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a t *aRoot; /*
1eb5f 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 Array of rootpa
1eb60 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 ge numbers for t
1eb61 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 ables to be chec
1eb62 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 ked */. int j;
1eb63 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
1eb64 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
1eb65 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a t nErr; /*
1eb66 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 Number of error
1eb67 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 s reported */.
1eb68 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 char *z;
1eb69 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 /* Text of the e
1eb6a 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 rror report */.
1eb6b 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 Mem *pnErr;
1eb6c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 /* Register kee
1eb6d 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 ping track of er
1eb6e 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a rors remaining *
1eb6f 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 /.#endif /* loca
1eb70 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1eb71 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a d into u.bv */..
1eb72 20 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 70 u.bv.nRoot = p
1eb73 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 Op->p2;. assert
1eb74 28 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 29 ( u.bv.nRoot>0 )
1eb75 3b 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 3d ;. u.bv.aRoot =
1eb76 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
1eb77 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 Raw(db, sizeof(i
1eb78 6e 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b nt)*(u.bv.nRoot+
1eb79 31 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 1) );. if( u.bv
1eb7a 2e 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f .aRoot==0 ) goto
1eb7b 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 no_mem;. asser
1eb7c 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 t( pOp->p3>0 &&
1eb7d 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d pOp->p3<=p->nMem
1eb7e 20 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 );. u.bv.pnErr
1eb7f 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 = &aMem[pOp->p3
1eb80 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e ];. assert( (u.
1eb81 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 bv.pnErr->flags
1eb82 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b & MEM_Int)!=0 );
1eb83 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 76 . assert( (u.bv
1eb84 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26 20 .pnErr->flags &
1eb85 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f (MEM_Str|MEM_Blo
1eb86 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 b))==0 );. pIn1
1eb87 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 = &aMem[pOp->p1
1eb88 5d 3b 0a 20 20 66 6f 72 28 75 2e 62 76 2e 6a 3d ];. for(u.bv.j=
1eb89 30 3b 20 75 2e 62 76 2e 6a 3c 75 2e 62 76 2e 6e 0; u.bv.j<u.bv.n
1eb8a 52 6f 6f 74 3b 20 75 2e 62 76 2e 6a 2b 2b 29 7b Root; u.bv.j++){
1eb8b 0a 20 20 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b . u.bv.aRoot[
1eb8c 75 2e 62 76 2e 6a 5d 20 3d 20 28 69 6e 74 29 73 u.bv.j] = (int)s
1eb8d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c qlite3VdbeIntVal
1eb8e 75 65 28 26 70 49 6e 31 5b 75 2e 62 76 2e 6a 5d ue(&pIn1[u.bv.j]
1eb8f 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 76 2e 61 52 );. }. u.bv.aR
1eb90 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 30 3b oot[u.bv.j] = 0;
1eb91 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e . assert( pOp->
1eb92 70 35 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 p5<db->nDb );.
1eb93 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 assert( (p->btre
1eb94 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d eMask & (1<<pOp-
1eb95 3e 70 35 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e >p5))!=0 );. u.
1eb96 62 76 2e 7a 20 3d 20 73 71 6c 69 74 65 33 42 74 bv.z = sqlite3Bt
1eb97 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63 reeIntegrityChec
1eb98 6b 28 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 k(db->aDb[pOp->p
1eb99 35 5d 2e 70 42 74 2c 20 75 2e 62 76 2e 61 52 6f 5].pBt, u.bv.aRo
1eb9a 6f 74 2c 20 75 2e 62 76 2e 6e 52 6f 6f 74 2c 0a ot, u.bv.nRoot,.
1eb9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1eb9d 20 28 69 6e 74 29 75 2e 62 76 2e 70 6e 45 72 72 (int)u.bv.pnErr
1eb9e 2d 3e 75 2e 69 2c 20 26 75 2e 62 76 2e 6e 45 72 ->u.i, &u.bv.nEr
1eb9f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 r);. sqlite3DbF
1eba0 72 65 65 28 64 62 2c 20 75 2e 62 76 2e 61 52 6f ree(db, u.bv.aRo
1eba1 6f 74 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 ot);. u.bv.pnEr
1eba2 72 2d 3e 75 2e 69 20 2d 3d 20 75 2e 62 76 2e 6e r->u.i -= u.bv.n
1eba3 45 72 72 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 Err;. sqlite3Vd
1eba4 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 6e beMemSetNull(pIn
1eba5 31 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e 6e 1);. if( u.bv.n
1eba6 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 Err==0 ){. as
1eba7 73 65 72 74 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 sert( u.bv.z==0
1eba8 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 );. }else if( u
1eba9 2e 62 76 2e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 .bv.z==0 ){.
1ebaa 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d goto no_mem;. }
1ebab 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 else{. sqlite
1ebac 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 3VdbeMemSetStr(p
1ebad 49 6e 31 2c 20 75 2e 62 76 2e 7a 2c 20 2d 31 2c In1, u.bv.z, -1,
1ebae 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 73 71 SQLITE_UTF8, sq
1ebaf 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d lite3_free);. }
1ebb0 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c . UPDATE_MAX_BL
1ebb1 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 OBSIZE(pIn1);.
1ebb2 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1ebb3 65 45 6e 63 6f 64 69 6e 67 28 70 49 6e 31 2c 20 eEncoding(pIn1,
1ebb4 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 62 72 65 encoding);. bre
1ebb5 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 ak;.}.#endif /*
1ebb6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 SQLITE_OMIT_INTE
1ebb7 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a GRITY_CHECK */..
1ebb8 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 /* Opcode: RowSe
1ebb9 74 41 64 64 20 50 31 20 50 32 20 2a 20 2a 20 2a tAdd P1 P2 * * *
1ebba 0a 2a 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 74 68 .**.** Insert th
1ebbb 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 e integer value
1ebbc 68 65 6c 64 20 62 79 20 72 65 67 69 73 74 65 72 held by register
1ebbd 20 50 32 20 69 6e 74 6f 20 61 20 62 6f 6f 6c 65 P2 into a boole
1ebbe 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 65 6c 64 an index.** held
1ebbf 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e in register P1.
1ebc0 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 73 73 65 72 74 .**.** An assert
1ebc1 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 50 32 20 ion fails if P2
1ebc2 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 is not an intege
1ebc3 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f r..*/.case OP_Ro
1ebc4 77 53 65 74 41 64 64 3a 20 7b 20 20 20 20 20 20 wSetAdd: {
1ebc5 20 2f 2a 20 69 6e 31 2c 20 69 6e 32 20 2a 2f 0a /* in1, in2 */.
1ebc6 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 pIn1 = &aMem[p
1ebc7 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 20 Op->p1];. pIn2
1ebc8 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d = &aMem[pOp->p2]
1ebc9 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 6e ;. assert( (pIn
1ebca 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 2->flags & MEM_I
1ebcb 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 nt)!=0 );. if(
1ebcc 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d (pIn1->flags & M
1ebcd 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b EM_RowSet)==0 ){
1ebce 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
1ebcf 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e MemSetRowSet(pIn
1ebd0 31 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 1);. if( (pIn
1ebd1 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 1->flags & MEM_R
1ebd2 6f 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f owSet)==0 ) goto
1ebd3 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 no_mem;. }. s
1ebd4 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 qlite3RowSetInse
1ebd5 72 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 rt(pIn1->u.pRowS
1ebd6 65 74 2c 20 70 49 6e 32 2d 3e 75 2e 69 29 3b 0a et, pIn2->u.i);.
1ebd7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1ebd8 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 52 65 61 pcode: RowSetRea
1ebd9 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a d P1 P2 P3 * *.*
1ebda 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 *.** Extract the
1ebdb 20 73 6d 61 6c 6c 65 73 74 20 76 61 6c 75 65 20 smallest value
1ebdc 66 72 6f 6d 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 from boolean ind
1ebdd 65 78 20 50 31 20 61 6e 64 20 70 75 74 20 74 68 ex P1 and put th
1ebde 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 0a 2a 2a at value into.**
1ebdf 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 4f register P3. O
1ebe0 72 2c 20 69 66 20 62 6f 6f 6c 65 61 6e 20 69 6e r, if boolean in
1ebe1 64 65 78 20 50 31 20 69 73 20 69 6e 69 74 69 61 dex P1 is initia
1ebe2 6c 6c 79 20 65 6d 70 74 79 2c 20 6c 65 61 76 65 lly empty, leave
1ebe3 20 50 33 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 P3.** unchanged
1ebe4 20 61 6e 64 20 6a 75 6d 70 20 74 6f 20 69 6e 73 and jump to ins
1ebe5 74 72 75 63 74 69 6f 6e 20 50 32 2e 0a 2a 2f 0a truction P2..*/.
1ebe6 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 52 65 case OP_RowSetRe
1ebe7 61 64 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a ad: { /* j
1ebe8 75 6d 70 2c 20 69 6e 31 2c 20 6f 75 74 33 20 2a ump, in1, out3 *
1ebe9 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 /.#if 0 /* loca
1ebea 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ebeb 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 d into u.bw */.
1ebec 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 69 66 i64 val;.#endif
1ebed 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ebee 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ebef 2e 62 77 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 .bw */. CHECK_F
1ebf0 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 OR_INTERRUPT;.
1ebf1 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1ebf2 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 ->p1];. if( (pI
1ebf3 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f n1->flags & MEM_
1ebf4 52 6f 77 53 65 74 29 3d 3d 30 0a 20 20 20 7c 7c RowSet)==0. ||
1ebf5 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 sqlite3RowSetNe
1ebf6 78 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 xt(pIn1->u.pRowS
1ebf7 65 74 2c 20 26 75 2e 62 77 2e 76 61 6c 29 3d 3d et, &u.bw.val)==
1ebf8 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 0. ){. /* Th
1ebf9 65 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 e boolean index
1ebfa 69 73 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 is empty */.
1ebfb 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 sqlite3VdbeMemSe
1ebfc 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 20 tNull(pIn1);.
1ebfd 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 pc = pOp->p2 -
1ebfe 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
1ebff 2f 2a 20 41 20 76 61 6c 75 65 20 77 61 73 20 70 /* A value was p
1ec00 75 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69 ulled from the i
1ec01 6e 64 65 78 20 2a 2f 0a 20 20 20 20 73 71 6c 69 ndex */. sqli
1ec02 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 te3VdbeMemSetInt
1ec03 36 34 28 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 64(&aMem[pOp->p3
1ec04 5d 2c 20 75 2e 62 77 2e 76 61 6c 29 3b 0a 20 20 ], u.bw.val);.
1ec05 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a }. break;.}../*
1ec06 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74 54 Opcode: RowSetT
1ec07 65 73 74 20 50 31 20 50 32 20 50 33 20 50 34 0a est P1 P2 P3 P4.
1ec08 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 **.** Register P
1ec09 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 3 is assumed to
1ec0a 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 69 6e hold a 64-bit in
1ec0b 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 66 20 teger value. If
1ec0c 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 63 register P1.** c
1ec0d 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65 74 ontains a RowSet
1ec0e 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74 object and that
1ec0f 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 63 RowSet object c
1ec10 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 76 ontains.** the v
1ec11 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33 2c alue held in P3,
1ec12 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74 65 jump to registe
1ec13 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c r P2. Otherwise,
1ec14 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a 20 69 insert the.** i
1ec15 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e 74 nteger in P3 int
1ec16 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e 64 o the RowSet and
1ec17 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f 20 continue on to
1ec18 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 63 6f the.** next opco
1ec19 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f de..**.** The Ro
1ec1a 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 6f wSet object is o
1ec1b 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 68 65 ptimized for the
1ec1c 20 63 61 73 65 20 77 68 65 72 65 20 73 75 63 63 case where succ
1ec1d 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a 20 6f essive sets.** o
1ec1e 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 65 72 f integers, wher
1ec1f 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e 74 61 e each set conta
1ec20 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 74 65 ins no duplicate
1ec21 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a 20 6f s. Each set.** o
1ec22 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 65 6e f values is iden
1ec23 74 69 66 69 65 64 20 62 79 20 61 20 75 6e 69 71 tified by a uniq
1ec24 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 68 65 ue P4 value. The
1ec25 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 6d 75 first set.** mu
1ec26 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c 20 74 st have P4==0, t
1ec27 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 34 3d he final set P4=
1ec28 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 65 20 -1. P4 must be
1ec29 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a 2a 20 either -1 or.**
1ec2a 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 46 non-negative. F
1ec2b 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 or non-negative
1ec2c 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f 6e 6c values of P4 onl
1ec2d 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a 2a 2a y the lower 4.**
1ec2e 20 62 69 74 73 20 61 72 65 20 73 69 67 6e 69 66 bits are signif
1ec2f 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 icant..**.** Thi
1ec30 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d 69 7a s allows optimiz
1ec31 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 65 6e ations: (a) when
1ec32 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 73 20 P4==0 there is
1ec33 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 0a no need to test.
1ec34 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 6f 62 ** the rowset ob
1ec35 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 73 20 ject for P3, as
1ec36 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 it is guaranteed
1ec37 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e 20 not to contain
1ec38 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 6e 20 it,.** (b) when
1ec39 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 73 20 P4==-1 there is
1ec3a 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 65 72 no need to inser
1ec3b 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 73 20 t the value, as
1ec3c 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 it will.** never
1ec3d 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 2c 20 be tested for,
1ec3e 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 20 76 and (c) when a v
1ec3f 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 61 72 alue that is par
1ec40 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a 2a 2a t of set X is.**
1ec41 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72 65 inserted, there
1ec42 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 73 is no need to s
1ec43 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 66 20 earch to see if
1ec44 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 77 the same value w
1ec45 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 as.** previously
1ec46 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61 72 inserted as par
1ec47 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c 79 t of set X (only
1ec48 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76 69 if it was previ
1ec49 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 74 65 ously.** inserte
1ec4a 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f 6d d as part of som
1ec4b 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a 2a 2f e other set)..*/
1ec4c 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 54 .case OP_RowSetT
1ec4d 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 est: {
1ec4e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 /* ju
1ec4f 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a mp, in1, in3 */.
1ec50 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1ec51 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1ec52 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 20 20 69 into u.bx */. i
1ec53 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74 20 65 nt iSet;. int e
1ec54 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20 2f 2a xists;.#endif /*
1ec55 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1ec56 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 moved into u.bx
1ec57 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 */.. pIn1 = &a
1ec58 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1ec59 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn3 = &aMem[pOp
1ec5a 2d 3e 70 33 5d 3b 0a 20 20 75 2e 62 78 2e 69 53 ->p3];. u.bx.iS
1ec5b 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a et = pOp->p4.i;.
1ec5c 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e assert( pIn3->
1ec5d 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b flags&MEM_Int );
1ec5e 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 .. /* If there
1ec5f 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 is anything othe
1ec60 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 r than a rowset
1ec61 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 object in memory
1ec62 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 cell P1,. ** d
1ec63 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 elete it now and
1ec64 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 initialize P1 w
1ec65 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 ith an empty row
1ec66 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 set. */. if( (
1ec67 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 pIn1->flags & ME
1ec68 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a M_RowSet)==0 ){.
1ec69 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d sqlite3VdbeM
1ec6a 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31 emSetRowSet(pIn1
1ec6b 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 );. if( (pIn1
1ec6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f ->flags & MEM_Ro
1ec6d 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 wSet)==0 ) goto
1ec6e 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 no_mem;. }.. a
1ec6f 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 ssert( pOp->p4ty
1ec70 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a pe==P4_INT32 );.
1ec71 20 20 61 73 73 65 72 74 28 20 75 2e 62 78 2e 69 assert( u.bx.i
1ec72 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 78 2e Set==-1 || u.bx.
1ec73 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 iSet>=0 );. if(
1ec74 20 75 2e 62 78 2e 69 53 65 74 20 29 7b 0a 20 20 u.bx.iSet ){.
1ec75 20 20 75 2e 62 78 2e 65 78 69 73 74 73 20 3d 20 u.bx.exists =
1ec76 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 sqlite3RowSetTes
1ec77 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 t(pIn1->u.pRowSe
1ec78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
1ec79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec7a 20 20 28 75 38 29 28 75 2e 62 78 2e 69 53 65 74 (u8)(u.bx.iSet
1ec7b 3e 3d 30 20 3f 20 75 2e 62 78 2e 69 53 65 74 20 >=0 ? u.bx.iSet
1ec7c 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a 20 & 0xf : 0xff),.
1ec7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ec7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 pI
1ec7f 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 66 n3->u.i);. if
1ec80 28 20 75 2e 62 78 2e 65 78 69 73 74 73 20 29 7b ( u.bx.exists ){
1ec81 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d . pc = pOp-
1ec82 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 >p2 - 1;. b
1ec83 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a reak;. }. }.
1ec84 20 20 69 66 28 20 75 2e 62 78 2e 69 53 65 74 3e if( u.bx.iSet>
1ec85 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
1ec86 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 3RowSetInsert(pI
1ec87 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 n1->u.pRowSet, p
1ec88 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20 In3->u.i);. }.
1ec89 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e break;.}...#ifn
1ec8a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ec8b 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f TRIGGER../* Opco
1ec8c 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 50 de: Program P1 P
1ec8d 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 2 P3 P4 *.**.**
1ec8e 45 78 65 63 75 74 65 20 74 68 65 20 74 72 69 67 Execute the trig
1ec8f 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 73 ger program pass
1ec90 65 64 20 61 73 20 50 34 20 28 74 79 70 65 20 50 ed as P4 (type P
1ec91 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 0a 4_SUBPROGRAM). .
1ec92 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e **.** P1 contain
1ec93 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 s the address of
1ec94 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c the memory cell
1ec95 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 that contains t
1ec96 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20 he first memory
1ec97 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61 .** cell in an a
1ec98 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75 rray of values u
1ec99 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 sed as arguments
1ec9a 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67 to the sub-prog
1ec9b 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e 74 ram. P2 .** cont
1ec9c 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 ains the address
1ec9d 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74 to jump to if t
1ec9e 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74 he sub-program t
1ec9f 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20 hrows an IGNORE
1eca0 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 73 .** exception us
1eca1 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 20 ing the RAISE()
1eca2 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74 function. Regist
1eca3 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 er P3 contains t
1eca4 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 6f he address .** o
1eca5 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 f a memory cell
1eca6 69 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 72 in this (the par
1eca7 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 20 ent) VM that is
1eca8 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 used to allocate
1eca9 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 the .** memory
1ecaa 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 required by the
1ecab 73 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e 74 sub-vdbe at runt
1ecac 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 ime..**.** P4 is
1ecad 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 a pointer to th
1ecae 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 e VM containing
1ecaf 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 the trigger prog
1ecb0 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f ram..*/.case OP_
1ecb1 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 20 Program: {
1ecb2 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 /* jump */.#if
1ecb3 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 0 /* local var
1ecb4 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1ecb5 6f 20 75 2e 62 79 20 2a 2f 0a 20 20 69 6e 74 20 o u.by */. int
1ecb6 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 nMem;
1ecb7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 /* Number of
1ecb8 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 memory register
1ecb9 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 s for sub-progra
1ecba 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 m */. int nByte
1ecbb 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1ecbc 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69 * Bytes of runti
1ecbd 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 me space require
1ecbe 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 d for sub-progra
1ecbf 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b m */. Mem *pRt;
1ecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ecc1 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c * Register to al
1ecc2 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 locate runtime s
1ecc3 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 pace */. Mem *p
1ecc4 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 Mem;
1ecc5 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 /* Used to ite
1ecc6 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d rate through mem
1ecc7 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d ory cells */. M
1ecc8 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 em *pEnd;
1ecc9 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d /* Last m
1ecca 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 emory cell in ne
1eccb 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 62 w array */. Vdb
1eccc 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 eFrame *pFrame;
1eccd 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 /* New vdbe
1ecce 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 frame to execut
1eccf 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 6f e in */. SubPro
1ecd0 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 gram *pProgram;
1ecd1 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d /* Sub-program
1ecd2 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 to execute */.
1ecd3 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 void *t;
1ecd4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 /* Toke
1ecd5 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 n identifying tr
1ecd6 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 igger */.#endif
1ecd7 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ecd8 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ecd9 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 70 50 by */.. u.by.pP
1ecda 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 34 rogram = pOp->p4
1ecdb 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 62 .pProgram;. u.b
1ecdc 79 2e 70 52 74 20 3d 20 26 61 4d 65 6d 5b 70 4f y.pRt = &aMem[pO
1ecdd 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 p->p3];. assert
1ecde 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d ( u.by.pProgram-
1ecdf 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 >nOp>0 );.. /*
1ece0 49 66 20 74 68 65 20 70 35 20 66 6c 61 67 20 69 If the p5 flag i
1ece1 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 72 65 s clear, then re
1ece2 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61 74 69 cursive invocati
1ece3 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73 20 69 on of triggers i
1ece4 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 20 s. ** disabled
1ece5 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f for backwards co
1ece6 6d 70 61 74 69 62 69 6c 69 74 79 20 28 70 35 20 mpatibility (p5
1ece7 69 73 20 73 65 74 20 69 66 20 74 68 69 73 20 73 is set if this s
1ece8 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 ub-program. **
1ece9 69 73 20 72 65 61 6c 6c 79 20 61 20 74 72 69 67 is really a trig
1ecea 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f 72 65 69 ger, not a forei
1eceb 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c 20 61 gn key action, a
1ecec 6e 64 20 74 68 65 20 66 6c 61 67 20 73 65 74 0a nd the flag set.
1eced 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72 65 64 ** and cleared
1ecee 20 62 79 20 74 68 65 20 22 50 52 41 47 4d 41 20 by the "PRAGMA
1ecef 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 recursive_trigge
1ecf0 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69 73 20 63 rs" command is c
1ecf1 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20 20 2a 2a lear).. **. **
1ecf2 20 49 74 20 69 73 20 72 65 63 75 72 73 69 76 65 It is recursive
1ecf3 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 invocation of t
1ecf4 72 69 67 67 65 72 73 2c 20 61 74 20 74 68 65 20 riggers, at the
1ecf5 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 61 74 20 SQL level, that
1ecf6 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 64 is. ** disabled
1ecf7 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 . In some cases
1ecf8 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 72 a single trigger
1ecf9 20 6d 61 79 20 67 65 6e 65 72 61 74 65 20 6d 6f may generate mo
1ecfa 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 20 2a 2a re than one. **
1ecfb 20 53 75 62 50 72 6f 67 72 61 6d 20 28 69 66 20 SubProgram (if
1ecfc 74 68 65 20 74 72 69 67 67 65 72 20 6d 61 79 20 the trigger may
1ecfd 62 65 20 65 78 65 63 75 74 65 64 20 77 69 74 68 be executed with
1ecfe 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 more than one d
1ecff 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 4f 4e ifferent. ** ON
1ed00 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 CONFLICT algori
1ed01 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 72 61 6d thm). SubProgram
1ed02 20 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f structures asso
1ed03 63 69 61 74 65 64 20 77 69 74 68 20 61 0a 20 20 ciated with a.
1ed04 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 67 67 65 ** single trigge
1ed05 72 20 61 6c 6c 20 68 61 76 65 20 74 68 65 20 73 r all have the s
1ed06 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 ame value for th
1ed07 65 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b e SubProgram.tok
1ed08 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 en. ** variable
1ed09 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 70 2d . */. if( pOp-
1ed0a 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e 62 79 2e >p5 ){. u.by.
1ed0b 74 20 3d 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 t = u.by.pProgra
1ed0c 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20 66 6f m->token;. fo
1ed0d 72 28 75 2e 62 79 2e 70 46 72 61 6d 65 3d 70 2d r(u.by.pFrame=p-
1ed0e 3e 70 46 72 61 6d 65 3b 20 75 2e 62 79 2e 70 46 >pFrame; u.by.pF
1ed0f 72 61 6d 65 20 26 26 20 75 2e 62 79 2e 70 46 72 rame && u.by.pFr
1ed10 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e 62 79 ame->token!=u.by
1ed11 2e 74 3b 20 75 2e 62 79 2e 70 46 72 61 6d 65 3d .t; u.by.pFrame=
1ed12 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 50 61 u.by.pFrame->pPa
1ed13 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 75 rent);. if( u
1ed14 2e 62 79 2e 70 46 72 61 6d 65 20 29 20 62 72 65 .by.pFrame ) bre
1ed15 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 ak;. }.. if( p
1ed16 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e 61 4c ->nFrame>=db->aL
1ed17 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 imit[SQLITE_LIMI
1ed18 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 5d T_TRIGGER_DEPTH]
1ed19 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c ){. rc = SQL
1ed1a 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 ITE_ERROR;. s
1ed1b 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1ed1c 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1ed1d 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c "too many level
1ed1e 73 20 6f 66 20 74 72 69 67 67 65 72 20 72 65 63 s of trigger rec
1ed1f 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20 20 62 72 ursion");. br
1ed20 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 eak;. }.. /* R
1ed21 65 67 69 73 74 65 72 20 75 2e 62 79 2e 70 52 74 egister u.by.pRt
1ed22 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 is used to stor
1ed23 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71 e the memory req
1ed24 75 69 72 65 64 20 74 6f 20 73 61 76 65 20 74 68 uired to save th
1ed25 65 20 73 74 61 74 65 0a 20 20 2a 2a 20 6f 66 20 e state. ** of
1ed26 74 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 67 the current prog
1ed27 72 61 6d 2c 20 61 6e 64 20 74 68 65 20 6d 65 6d ram, and the mem
1ed28 6f 72 79 20 72 65 71 75 69 72 65 64 20 61 74 20 ory required at
1ed29 72 75 6e 74 69 6d 65 20 74 6f 20 65 78 65 63 75 runtime to execu
1ed2a 74 65 0a 20 20 2a 2a 20 74 68 65 20 74 72 69 67 te. ** the trig
1ed2b 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 ger program. If
1ed2c 74 68 69 73 20 74 72 69 67 67 65 72 20 68 61 73 this trigger has
1ed2d 20 62 65 65 6e 20 66 69 72 65 64 20 62 65 66 6f been fired befo
1ed2e 72 65 2c 20 74 68 65 6e 20 75 2e 62 79 2e 70 52 re, then u.by.pR
1ed2f 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 t. ** is alread
1ed30 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4f 74 68 y allocated. Oth
1ed31 65 72 77 69 73 65 2c 20 69 74 20 6d 75 73 74 20 erwise, it must
1ed32 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 be initialized.
1ed33 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e 62 79 2e */. if( (u.by.
1ed34 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 46 pRt->flags&MEM_F
1ed35 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 rame)==0 ){.
1ed36 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d 2e 6e 4d /* SubProgram.nM
1ed37 65 6d 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 em is set to the
1ed38 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 number of memor
1ed39 79 20 63 65 6c 6c 73 20 75 73 65 64 20 62 79 20 y cells used by
1ed3a 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f 67 72 the. ** progr
1ed3b 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 53 75 62 am stored in Sub
1ed3c 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20 41 73 20 Program.aOp. As
1ed3d 77 65 6c 6c 20 61 73 20 74 68 65 73 65 2c 20 6f well as these, o
1ed3e 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a ne memory. **
1ed3f 20 63 65 6c 6c 20 69 73 20 72 65 71 75 69 72 65 cell is require
1ed40 64 20 66 6f 72 20 65 61 63 68 20 63 75 72 73 6f d for each curso
1ed41 72 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72 r used by the pr
1ed42 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f 63 61 6c ogram. Set local
1ed43 0a 20 20 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 . ** variable
1ed44 20 75 2e 62 79 2e 6e 4d 65 6d 20 28 61 6e 64 20 u.by.nMem (and
1ed45 6c 61 74 65 72 2c 20 56 64 62 65 46 72 61 6d 65 later, VdbeFrame
1ed46 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74 6f 20 74 .nChildMem) to t
1ed47 68 69 73 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a his value.. *
1ed48 2f 0a 20 20 20 20 75 2e 62 79 2e 6e 4d 65 6d 20 /. u.by.nMem
1ed49 3d 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d = u.by.pProgram-
1ed4a 3e 6e 4d 65 6d 20 2b 20 75 2e 62 79 2e 70 50 72 >nMem + u.by.pPr
1ed4b 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 ogram->nCsr;.
1ed4c 20 75 2e 62 79 2e 6e 42 79 74 65 20 3d 20 52 4f u.by.nByte = RO
1ed4d 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62 65 UND8(sizeof(Vdbe
1ed4e 46 72 61 6d 65 29 29 0a 20 20 20 20 20 20 20 20 Frame)).
1ed4f 20 20 20 20 20 20 2b 20 75 2e 62 79 2e 6e 4d 65 + u.by.nMe
1ed50 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65 6d 29 0a m * sizeof(Mem).
1ed51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 +
1ed52 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.by.pProgram->n
1ed53 43 73 72 20 2a 20 73 69 7a 65 6f 66 28 56 64 62 Csr * sizeof(Vdb
1ed54 65 43 75 72 73 6f 72 20 2a 29 3b 0a 20 20 20 20 eCursor *);.
1ed55 75 2e 62 79 2e 70 46 72 61 6d 65 20 3d 20 73 71 u.by.pFrame = sq
1ed56 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
1ed57 6f 28 64 62 2c 20 75 2e 62 79 2e 6e 42 79 74 65 o(db, u.by.nByte
1ed58 29 3b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 79 );. if( !u.by
1ed59 2e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20 .pFrame ){.
1ed5a 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 goto no_mem;.
1ed5b 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
1ed5c 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 75 2e dbeMemRelease(u.
1ed5d 62 79 2e 70 52 74 29 3b 0a 20 20 20 20 75 2e 62 by.pRt);. u.b
1ed5e 79 2e 70 52 74 2d 3e 66 6c 61 67 73 20 3d 20 4d y.pRt->flags = M
1ed5f 45 4d 5f 46 72 61 6d 65 3b 0a 20 20 20 20 75 2e EM_Frame;. u.
1ed60 62 79 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 by.pRt->u.pFrame
1ed61 20 3d 20 75 2e 62 79 2e 70 46 72 61 6d 65 3b 0a = u.by.pFrame;.
1ed62 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 . u.by.pFrame
1ed63 2d 3e 76 20 3d 20 70 3b 0a 20 20 20 20 75 2e 62 ->v = p;. u.b
1ed64 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 y.pFrame->nChild
1ed65 4d 65 6d 20 3d 20 75 2e 62 79 2e 6e 4d 65 6d 3b Mem = u.by.nMem;
1ed66 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 . u.by.pFrame
1ed67 2d 3e 6e 43 68 69 6c 64 43 73 72 20 3d 20 75 2e ->nChildCsr = u.
1ed68 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 by.pProgram->nCs
1ed69 72 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 r;. u.by.pFra
1ed6a 6d 65 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 me->pc = pc;.
1ed6b 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 61 4d u.by.pFrame->aM
1ed6c 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a 20 20 em = p->aMem;.
1ed6d 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e u.by.pFrame->n
1ed6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65 6d 3b 0a 20 Mem = p->nMem;.
1ed6f 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e u.by.pFrame->
1ed70 61 70 43 73 72 20 3d 20 70 2d 3e 61 70 43 73 72 apCsr = p->apCsr
1ed71 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d ;. u.by.pFram
1ed72 65 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 70 2d 3e e->nCursor = p->
1ed73 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 75 2e 62 nCursor;. u.b
1ed74 79 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 20 3d 20 y.pFrame->aOp =
1ed75 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 75 2e 62 79 p->aOp;. u.by
1ed76 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d 20 70 .pFrame->nOp = p
1ed77 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75 2e 62 79 2e ->nOp;. u.by.
1ed78 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20 3d 20 pFrame->token =
1ed79 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 u.by.pProgram->t
1ed7a 6f 6b 65 6e 3b 0a 0a 20 20 20 20 75 2e 62 79 2e oken;.. u.by.
1ed7b 70 45 6e 64 20 3d 20 26 56 64 62 65 46 72 61 6d pEnd = &VdbeFram
1ed7c 65 4d 65 6d 28 75 2e 62 79 2e 70 46 72 61 6d 65 eMem(u.by.pFrame
1ed7d 29 5b 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e )[u.by.pFrame->n
1ed7e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 20 20 66 ChildMem];. f
1ed7f 6f 72 28 75 2e 62 79 2e 70 4d 65 6d 3d 56 64 62 or(u.by.pMem=Vdb
1ed80 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 79 2e 70 eFrameMem(u.by.p
1ed81 46 72 61 6d 65 29 3b 20 75 2e 62 79 2e 70 4d 65 Frame); u.by.pMe
1ed82 6d 21 3d 75 2e 62 79 2e 70 45 6e 64 3b 20 75 2e m!=u.by.pEnd; u.
1ed83 62 79 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 by.pMem++){.
1ed84 20 20 75 2e 62 79 2e 70 4d 65 6d 2d 3e 66 6c 61 u.by.pMem->fla
1ed85 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1ed86 20 20 20 20 20 75 2e 62 79 2e 70 4d 65 6d 2d 3e u.by.pMem->
1ed87 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 7d 0a 20 db = db;. }.
1ed88 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 62 79 }else{. u.by
1ed89 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 79 2e 70 .pFrame = u.by.p
1ed8a 52 74 2d 3e 75 2e 70 46 72 61 6d 65 3b 0a 20 20 Rt->u.pFrame;.
1ed8b 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 70 assert( u.by.p
1ed8c 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b 75 2e Program->nMem+u.
1ed8d 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 by.pProgram->nCs
1ed8e 72 3d 3d 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e r==u.by.pFrame->
1ed8f 6e 43 68 69 6c 64 4d 65 6d 20 29 3b 0a 20 20 20 nChildMem );.
1ed90 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 70 50 assert( u.by.pP
1ed91 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e rogram->nCsr==u.
1ed92 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c by.pFrame->nChil
1ed93 64 43 73 72 20 29 3b 0a 20 20 20 20 61 73 73 65 dCsr );. asse
1ed94 72 74 28 20 70 63 3d 3d 75 2e 62 79 2e 70 46 72 rt( pc==u.by.pFr
1ed95 61 6d 65 2d 3e 70 63 20 29 3b 0a 20 20 7d 0a 0a ame->pc );. }..
1ed96 20 20 70 2d 3e 6e 46 72 61 6d 65 2b 2b 3b 0a 20 p->nFrame++;.
1ed97 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 50 u.by.pFrame->pP
1ed98 61 72 65 6e 74 20 3d 20 70 2d 3e 70 46 72 61 6d arent = p->pFram
1ed99 65 3b 0a 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 e;. u.by.pFrame
1ed9a 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62 ->lastRowid = db
1ed9b 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 75 ->lastRowid;. u
1ed9c 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 61 .by.pFrame->nCha
1ed9d 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 nge = p->nChange
1ed9e 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d ;. p->nChange =
1ed9f 20 30 3b 0a 20 20 70 2d 3e 70 46 72 61 6d 65 20 0;. p->pFrame
1eda0 3d 20 75 2e 62 79 2e 70 46 72 61 6d 65 3b 0a 20 = u.by.pFrame;.
1eda1 20 70 2d 3e 61 4d 65 6d 20 3d 20 61 4d 65 6d 20 p->aMem = aMem
1eda2 3d 20 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 = &VdbeFrameMem(
1eda3 75 2e 62 79 2e 70 46 72 61 6d 65 29 5b 2d 31 5d u.by.pFrame)[-1]
1eda4 3b 0a 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e ;. p->nMem = u.
1eda5 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c by.pFrame->nChil
1eda6 64 4d 65 6d 3b 0a 20 20 70 2d 3e 6e 43 75 72 73 dMem;. p->nCurs
1eda7 6f 72 20 3d 20 28 75 31 36 29 75 2e 62 79 2e 70 or = (u16)u.by.p
1eda8 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 Frame->nChildCsr
1eda9 3b 0a 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 ;. p->apCsr = (
1edaa 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 29 26 61 VdbeCursor **)&a
1edab 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a Mem[p->nMem+1];.
1edac 20 20 70 2d 3e 61 4f 70 20 3d 20 61 4f 70 20 3d p->aOp = aOp =
1edad 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e u.by.pProgram->
1edae 61 4f 70 3b 0a 20 20 70 2d 3e 6e 4f 70 20 3d 20 aOp;. p->nOp =
1edaf 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e u.by.pProgram->n
1edb0 4f 70 3b 0a 20 20 70 63 20 3d 20 2d 31 3b 0a 0a Op;. pc = -1;..
1edb1 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f break;.}../* O
1edb2 70 63 6f 64 65 3a 20 50 61 72 61 6d 20 50 31 20 pcode: Param P1
1edb3 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 P2 * * *.**.** T
1edb4 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6f 6e his opcode is on
1edb5 6c 79 20 65 76 65 72 20 70 72 65 73 65 6e 74 20 ly ever present
1edb6 69 6e 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 in sub-programs
1edb7 63 61 6c 6c 65 64 20 76 69 61 20 74 68 65 20 0a called via the .
1edb8 2a 2a 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e ** OP_Program in
1edb9 73 74 72 75 63 74 69 6f 6e 2e 20 43 6f 70 79 20 struction. Copy
1edba 61 20 76 61 6c 75 65 20 63 75 72 72 65 6e 74 6c a value currentl
1edbb 79 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6d 65 y stored in a me
1edbc 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c 6c 20 6f 66 mory .** cell of
1edbd 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 28 70 61 the calling (pa
1edbe 72 65 6e 74 29 20 66 72 61 6d 65 20 74 6f 20 63 rent) frame to c
1edbf 65 6c 6c 20 50 32 20 69 6e 20 74 68 65 20 63 75 ell P2 in the cu
1edc0 72 72 65 6e 74 20 66 72 61 6d 65 73 20 0a 2a 2a rrent frames .**
1edc1 20 61 64 64 72 65 73 73 20 73 70 61 63 65 2e 20 address space.
1edc2 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 This is used by
1edc3 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 trigger programs
1edc4 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 6e to access the n
1edc5 65 77 2e 2a 20 0a 2a 2a 20 61 6e 64 20 6f 6c 64 ew.* .** and old
1edc6 2e 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a .* values..**.**
1edc7 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 The address of
1edc8 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 the cell in the
1edc9 70 61 72 65 6e 74 20 66 72 61 6d 65 20 69 73 20 parent frame is
1edca 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 61 64 determined by ad
1edcb 64 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75 ding.** the valu
1edcc 65 20 6f 66 20 74 68 65 20 50 31 20 61 72 67 75 e of the P1 argu
1edcd 6d 65 6e 74 20 74 6f 20 74 68 65 20 76 61 6c 75 ment to the valu
1edce 65 20 6f 66 20 74 68 65 20 50 31 20 61 72 67 75 e of the P1 argu
1edcf 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 63 ment to the.** c
1edd0 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61 alling OP_Progra
1edd1 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a m instruction..*
1edd2 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 61 6d 3a /.case OP_Param:
1edd3 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 { /*
1edd4 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 out2-prerelease
1edd5 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1edd6 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1edd7 65 64 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a ed into u.bz */.
1edd8 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 VdbeFrame *pFr
1edd9 61 6d 65 3b 0a 20 20 4d 65 6d 20 2a 70 49 6e 3b ame;. Mem *pIn;
1edda 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1eddb 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1eddc 20 69 6e 74 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 into u.bz */.
1eddd 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d 20 70 2d u.bz.pFrame = p-
1edde 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e >pFrame;. u.bz.
1eddf 70 49 6e 20 3d 20 26 75 2e 62 7a 2e 70 46 72 61 pIn = &u.bz.pFra
1ede0 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 me->aMem[pOp->p1
1ede1 20 2b 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e + u.bz.pFrame->
1ede2 61 4f 70 5b 75 2e 62 7a 2e 70 46 72 61 6d 65 2d aOp[u.bz.pFrame-
1ede3 3e 70 63 5d 2e 70 31 5d 3b 0a 20 20 73 71 6c 69 >pc].p1];. sqli
1ede4 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f te3VdbeMemShallo
1ede5 77 43 6f 70 79 28 70 4f 75 74 2c 20 75 2e 62 7a wCopy(pOut, u.bz
1ede6 2e 70 49 6e 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 .pIn, MEM_Ephem)
1ede7 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 65 ;. break;.}..#e
1ede8 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 ndif /* #ifndef
1ede9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 SQLITE_OMIT_TRIG
1edea 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 GER */..#ifndef
1edeb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
1edec 49 47 4e 5f 4b 45 59 0a 2f 2a 20 4f 70 63 6f 64 IGN_KEY./* Opcod
1eded 65 3a 20 46 6b 43 6f 75 6e 74 65 72 20 50 31 20 e: FkCounter P1
1edee 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 P2 * * *.**.** I
1edef 6e 63 72 65 6d 65 6e 74 20 61 20 22 63 6f 6e 73 ncrement a "cons
1edf0 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 20 traint counter"
1edf1 62 79 20 50 32 20 28 50 32 20 6d 61 79 20 62 65 by P2 (P2 may be
1edf2 20 6e 65 67 61 74 69 76 65 20 6f 72 20 70 6f 73 negative or pos
1edf3 69 74 69 76 65 29 2e 0a 2a 2a 20 49 66 20 50 31 itive)..** If P1
1edf4 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 is non-zero, th
1edf5 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 73 74 e database const
1edf6 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 69 73 raint counter is
1edf7 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 0a 2a 2a incremented .**
1edf8 20 28 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 (deferred forei
1edf9 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1edfa 74 73 29 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 ts). Otherwise,
1edfb 69 66 20 50 31 20 69 73 20 7a 65 72 6f 2c 20 74 if P1 is zero, t
1edfc 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 he .** statement
1edfd 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72 counter is incr
1edfe 65 6d 65 6e 74 65 64 20 28 69 6d 6d 65 64 69 61 emented (immedia
1edff 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 te foreign key c
1ee00 6f 6e 73 74 72 61 69 6e 74 73 29 2e 0a 2a 2f 0a onstraints)..*/.
1ee01 63 61 73 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 case OP_FkCounte
1ee02 72 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e r: {. if( pOp->
1ee03 70 31 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6e 44 p1 ){. db->nD
1ee04 65 66 65 72 72 65 64 43 6f 6e 73 20 2b 3d 20 70 eferredCons += p
1ee05 4f 70 2d 3e 70 32 3b 0a 20 20 7d 65 6c 73 65 7b Op->p2;. }else{
1ee06 0a 20 20 20 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 . p->nFkConst
1ee07 72 61 69 6e 74 20 2b 3d 20 70 4f 70 2d 3e 70 32 raint += pOp->p2
1ee08 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d ;. }. break;.}
1ee09 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 49 ../* Opcode: FkI
1ee0a 66 5a 65 72 6f 20 50 31 20 50 32 20 2a 20 2a 20 fZero P1 P2 * *
1ee0b 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 *.**.** This opc
1ee0c 6f 64 65 20 74 65 73 74 73 20 69 66 20 61 20 66 ode tests if a f
1ee0d 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 oreign key const
1ee0e 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 raint-counter is
1ee0f 20 63 75 72 72 65 6e 74 6c 79 20 7a 65 72 6f 2e currently zero.
1ee10 0a 2a 2a 20 49 66 20 73 6f 2c 20 6a 75 6d 70 20 .** If so, jump
1ee11 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 to instruction P
1ee12 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 2. Otherwise, fa
1ee13 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1ee14 65 20 6e 65 78 74 20 0a 2a 2a 20 69 6e 73 74 72 e next .** instr
1ee15 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 uction..**.** If
1ee16 20 50 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c P1 is non-zero,
1ee17 20 74 68 65 6e 20 74 68 65 20 6a 75 6d 70 20 69 then the jump i
1ee18 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 64 s taken if the d
1ee19 61 74 61 62 61 73 65 20 63 6f 6e 73 74 72 61 69 atabase constrai
1ee1a 6e 74 2d 63 6f 75 6e 74 65 72 0a 2a 2a 20 69 73 nt-counter.** is
1ee1b 20 7a 65 72 6f 20 28 74 68 65 20 6f 6e 65 20 74 zero (the one t
1ee1c 68 61 74 20 63 6f 75 6e 74 73 20 64 65 66 65 72 hat counts defer
1ee1d 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 red constraint v
1ee1e 69 6f 6c 61 74 69 6f 6e 73 29 2e 20 49 66 20 50 iolations). If P
1ee1f 31 20 69 73 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 1 is.** zero, th
1ee20 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 e jump is taken
1ee21 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 if the statement
1ee22 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e constraint-coun
1ee23 74 65 72 20 69 73 20 7a 65 72 6f 0a 2a 2a 20 28 ter is zero.** (
1ee24 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 immediate foreig
1ee25 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 n key constraint
1ee26 20 76 69 6f 6c 61 74 69 6f 6e 73 29 2e 0a 2a 2f violations)..*/
1ee27 0a 63 61 73 65 20 4f 50 5f 46 6b 49 66 5a 65 72 .case OP_FkIfZer
1ee28 6f 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20 o: { /*
1ee29 6a 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 70 4f jump */. if( pO
1ee2a 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 69 66 28 p->p1 ){. if(
1ee2b 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f db->nDeferredCo
1ee2c 6e 73 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 ns==0 ) pc = pOp
1ee2d 2d 3e 70 32 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b ->p2-1;. }else{
1ee2e 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 46 6b 43 . if( p->nFkC
1ee2f 6f 6e 73 74 72 61 69 6e 74 3d 3d 30 20 29 20 70 onstraint==0 ) p
1ee30 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 c = pOp->p2-1;.
1ee31 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1ee32 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 ndif /* #ifndef
1ee33 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 SQLITE_OMIT_FORE
1ee34 49 47 4e 5f 4b 45 59 20 2a 2f 0a 0a 23 69 66 6e IGN_KEY */..#ifn
1ee35 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
1ee36 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a AUTOINCREMENT./*
1ee37 20 4f 70 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 Opcode: MemMax
1ee38 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a P1 P2 * * *.**.*
1ee39 2a 20 50 31 20 69 73 20 61 20 72 65 67 69 73 74 * P1 is a regist
1ee3a 65 72 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 er in the root f
1ee3b 72 61 6d 65 20 6f 66 20 74 68 69 73 20 56 4d 20 rame of this VM
1ee3c 28 74 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 (the root frame
1ee3d 69 73 0a 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 is.** different
1ee3e 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 from the current
1ee3f 20 66 72 61 6d 65 20 69 66 20 74 68 69 73 20 69 frame if this i
1ee40 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 62 65 nstruction is be
1ee41 69 6e 67 20 65 78 65 63 75 74 65 64 0a 2a 2a 20 ing executed.**
1ee42 77 69 74 68 69 6e 20 61 20 73 75 62 2d 70 72 6f within a sub-pro
1ee43 67 72 61 6d 29 2e 20 53 65 74 20 74 68 65 20 76 gram). Set the v
1ee44 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 alue of register
1ee45 20 50 31 20 74 6f 20 74 68 65 20 6d 61 78 69 6d P1 to the maxim
1ee46 75 6d 20 6f 66 20 0a 2a 2a 20 69 74 73 20 63 75 um of .** its cu
1ee47 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20 rrent value and
1ee48 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 the value in reg
1ee49 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 ister P2..**.**
1ee4a 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e This instruction
1ee4b 20 74 68 72 6f 77 73 20 61 6e 20 65 72 72 6f 72 throws an error
1ee4c 20 69 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 if the memory c
1ee4d 65 6c 6c 20 69 73 20 6e 6f 74 20 69 6e 69 74 69 ell is not initi
1ee4e 61 6c 6c 79 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 ally.** an integ
1ee4f 65 72 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d er..*/.case OP_M
1ee50 65 6d 4d 61 78 3a 20 7b 20 20 20 20 20 20 20 20 emMax: {
1ee51 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30 20 /* in2 */.#if 0
1ee52 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 /* local variab
1ee53 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 les moved into u
1ee54 2e 63 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 .ca */. Mem *pI
1ee55 6e 31 3b 0a 20 20 56 64 62 65 46 72 61 6d 65 20 n1;. VdbeFrame
1ee56 2a 70 46 72 61 6d 65 3b 0a 23 65 6e 64 69 66 20 *pFrame;.#endif
1ee57 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1ee58 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1ee59 63 61 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 ca */. if( p->p
1ee5a 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 66 6f 72 Frame ){. for
1ee5b 28 75 2e 63 61 2e 70 46 72 61 6d 65 3d 70 2d 3e (u.ca.pFrame=p->
1ee5c 70 46 72 61 6d 65 3b 20 75 2e 63 61 2e 70 46 72 pFrame; u.ca.pFr
1ee5d 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e ame->pParent; u.
1ee5e 63 61 2e 70 46 72 61 6d 65 3d 75 2e 63 61 2e 70 ca.pFrame=u.ca.p
1ee5f 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b Frame->pParent);
1ee60 0a 20 20 20 20 75 2e 63 61 2e 70 49 6e 31 20 3d . u.ca.pIn1 =
1ee61 20 26 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 &u.ca.pFrame->a
1ee62 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 Mem[pOp->p1];.
1ee63 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 63 61 2e }else{. u.ca.
1ee64 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 pIn1 = &aMem[pOp
1ee65 2d 3e 70 31 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c ->p1];. }. sql
1ee66 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
1ee67 65 72 69 66 79 28 75 2e 63 61 2e 70 49 6e 31 29 erify(u.ca.pIn1)
1ee68 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d ;. pIn2 = &aMem
1ee69 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 73 71 6c [pOp->p2];. sql
1ee6a 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 ite3VdbeMemInteg
1ee6b 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 erify(pIn2);. i
1ee6c 66 28 20 75 2e 63 61 2e 70 49 6e 31 2d 3e 75 2e f( u.ca.pIn1->u.
1ee6d 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a 20 20 i<pIn2->u.i){.
1ee6e 20 20 75 2e 63 61 2e 70 49 6e 31 2d 3e 75 2e 69 u.ca.pIn1->u.i
1ee6f 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 = pIn2->u.i;.
1ee70 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e }. break;.}.#en
1ee71 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1ee72 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 IT_AUTOINCREMENT
1ee73 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 */../* Opcode:
1ee74 49 66 50 6f 73 20 50 31 20 50 32 20 2a 20 2a 20 IfPos P1 P2 * *
1ee75 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 *.**.** If the v
1ee76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72 alue of register
1ee77 20 50 31 20 69 73 20 31 20 6f 72 20 67 72 65 61 P1 is 1 or grea
1ee78 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e ter, jump to P2.
1ee79 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c .**.** It is ill
1ee7a 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 egal to use this
1ee7b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 instruction on
1ee7c 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 a register that
1ee7d 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 does.** not cont
1ee7e 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 ain an integer.
1ee7f 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 An assertion fa
1ee80 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 ult will result
1ee81 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 if you try..*/.c
1ee82 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a 20 7b 20 ase OP_IfPos: {
1ee83 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1ee84 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 in1 */. pIn1 =
1ee85 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1ee86 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1ee87 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b flags&MEM_Int );
1ee88 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e 69 . if( pIn1->u.i
1ee89 3e 30 20 29 7b 0a 20 20 20 20 20 70 63 20 3d 20 >0 ){. pc =
1ee8a 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d pOp->p2 - 1;. }
1ee8b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 . break;.}../*
1ee8c 4f 70 63 6f 64 65 3a 20 49 66 4e 65 67 20 50 31 Opcode: IfNeg P1
1ee8d 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 P2 * * *.**.**
1ee8e 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 If the value of
1ee8f 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 6c register P1 is l
1ee90 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 6a ess than zero, j
1ee91 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a ump to P2. .**.*
1ee92 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 * It is illegal
1ee93 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 to use this inst
1ee94 72 75 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 ruction on a reg
1ee95 69 73 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a ister that does.
1ee96 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 ** not contain a
1ee97 6e 20 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 n integer. An a
1ee98 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 ssertion fault w
1ee99 69 6c 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f ill result if yo
1ee9a 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f u try..*/.case O
1ee9b 50 5f 49 66 4e 65 67 3a 20 7b 20 20 20 20 20 20 P_IfNeg: {
1ee9c 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a /* jump, in1 *
1ee9d 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d /. pIn1 = &aMem
1ee9e 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 [pOp->p1];. ass
1ee9f 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 ert( pIn1->flags
1eea0 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 &MEM_Int );. if
1eea1 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b ( pIn1->u.i<0 ){
1eea2 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e . pc = pOp->
1eea3 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 p2 - 1;. }. br
1eea4 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 eak;.}../* Opcod
1eea5 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 20 e: IfZero P1 P2
1eea6 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 P3 * *.**.** The
1eea7 20 72 65 67 69 73 74 65 72 20 50 31 20 6d 75 73 register P1 mus
1eea8 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 t contain an int
1eea9 65 67 65 72 2e 20 20 41 64 64 20 6c 69 74 65 72 eger. Add liter
1eeaa 61 6c 20 50 33 20 74 6f 20 74 68 65 0a 2a 2a 20 al P3 to the.**
1eeab 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 value in registe
1eeac 72 20 50 31 2e 20 20 49 66 20 74 68 65 20 72 65 r P1. If the re
1eead 73 75 6c 74 20 69 73 20 65 78 61 63 74 6c 79 20 sult is exactly
1eeae 30 2c 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 0, jump to P2. .
1eeaf 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 **.** It is ille
1eeb0 67 61 6c 20 74 6f 20 75 73 65 20 74 68 69 73 20 gal to use this
1eeb1 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e 20 61 instruction on a
1eeb2 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 64 register that d
1eeb3 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 oes.** not conta
1eeb4 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 in an integer.
1eeb5 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 An assertion fau
1eeb6 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 lt will result i
1eeb7 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f 0a 63 61 f you try..*/.ca
1eeb8 73 65 20 4f 50 5f 49 66 5a 65 72 6f 3a 20 7b 20 se OP_IfZero: {
1eeb9 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 /* jump,
1eeba 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 in1 */. pIn1 =
1eebb 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a &aMem[pOp->p1];.
1eebc 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e assert( pIn1->
1eebd 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b flags&MEM_Int );
1eebe 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 . pIn1->u.i +=
1eebf 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 70 pOp->p3;. if( p
1eec0 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 7b 0a 20 In1->u.i==0 ){.
1eec1 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 pc = pOp->p2
1eec2 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 - 1;. }. brea
1eec3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a k;.}../* Opcode:
1eec4 20 41 67 67 53 74 65 70 20 2a 20 50 32 20 50 33 AggStep * P2 P3
1eec5 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 45 78 65 P4 P5.**.** Exe
1eec6 63 75 74 65 20 74 68 65 20 73 74 65 70 20 66 75 cute the step fu
1eec7 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61 67 nction for an ag
1eec8 67 72 65 67 61 74 65 2e 20 20 54 68 65 0a 2a 2a gregate. The.**
1eec9 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 50 35 function has P5
1eeca 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 20 50 34 arguments. P4
1eecb 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f is a pointer to
1eecc 20 74 68 65 20 46 75 6e 63 44 65 66 0a 2a 2a 20 the FuncDef.**
1eecd 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 73 structure that s
1eece 70 65 63 69 66 69 65 73 20 74 68 65 20 66 75 6e pecifies the fun
1eecf 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 65 67 69 ction. Use regi
1eed0 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 20 74 68 ster.** P3 as th
1eed1 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a e accumulator..*
1eed2 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 72 67 75 *.** The P5 argu
1eed3 6d 65 6e 74 73 20 61 72 65 20 74 61 6b 65 6e 20 ments are taken
1eed4 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32 from register P2
1eed5 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 75 63 63 and its.** succ
1eed6 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 73 65 20 essors..*/.case
1eed7 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b 0a 23 69 OP_AggStep: {.#i
1eed8 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1eed9 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1eeda 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 20 69 6e 74 to u.cb */. int
1eedb 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d n;. int i;. M
1eedc 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65 6d 20 em *pMem;. Mem
1eedd 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74 65 33 *pRec;. sqlite3
1eede 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 _context ctx;.
1eedf 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a sqlite3_value **
1eee0 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a apVal;.#endif /*
1eee1 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 local variables
1eee2 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 62 moved into u.cb
1eee3 20 2a 2f 0a 0a 20 20 75 2e 63 62 2e 6e 20 3d 20 */.. u.cb.n =
1eee4 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73 73 65 72 pOp->p5;. asser
1eee5 74 28 20 75 2e 63 62 2e 6e 3e 3d 30 20 29 3b 0a t( u.cb.n>=0 );.
1eee6 20 20 75 2e 63 62 2e 70 52 65 63 20 3d 20 26 61 u.cb.pRec = &a
1eee7 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 Mem[pOp->p2];.
1eee8 75 2e 63 62 2e 61 70 56 61 6c 20 3d 20 70 2d 3e u.cb.apVal = p->
1eee9 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 apArg;. assert(
1eeea 20 75 2e 63 62 2e 61 70 56 61 6c 20 7c 7c 20 75 u.cb.apVal || u
1eeeb 2e 63 62 2e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f .cb.n==0 );. fo
1eeec 72 28 75 2e 63 62 2e 69 3d 30 3b 20 75 2e 63 62 r(u.cb.i=0; u.cb
1eeed 2e 69 3c 75 2e 63 62 2e 6e 3b 20 75 2e 63 62 2e .i<u.cb.n; u.cb.
1eeee 69 2b 2b 2c 20 75 2e 63 62 2e 70 52 65 63 2b 2b i++, u.cb.pRec++
1eeef 29 7b 0a 20 20 20 20 75 2e 63 62 2e 61 70 56 61 ){. u.cb.apVa
1eef0 6c 5b 75 2e 63 62 2e 69 5d 20 3d 20 75 2e 63 62 l[u.cb.i] = u.cb
1eef1 2e 70 52 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 .pRec;. sqlit
1eef2 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 e3VdbeMemStoreTy
1eef3 70 65 28 75 2e 63 62 2e 70 52 65 63 29 3b 0a 20 pe(u.cb.pRec);.
1eef4 20 7d 0a 20 20 75 2e 63 62 2e 63 74 78 2e 70 46 }. u.cb.ctx.pF
1eef5 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 unc = pOp->p4.pF
1eef6 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 unc;. assert( p
1eef7 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d Op->p3>0 && pOp-
1eef8 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a >p3<=p->nMem );.
1eef9 20 20 75 2e 63 62 2e 63 74 78 2e 70 4d 65 6d 20 u.cb.ctx.pMem
1eefa 3d 20 75 2e 63 62 2e 70 4d 65 6d 20 3d 20 26 61 = u.cb.pMem = &a
1eefb 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 Mem[pOp->p3];.
1eefc 75 2e 63 62 2e 70 4d 65 6d 2d 3e 6e 2b 2b 3b 0a u.cb.pMem->n++;.
1eefd 20 20 75 2e 63 62 2e 63 74 78 2e 73 2e 66 6c 61 u.cb.ctx.s.fla
1eefe 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 gs = MEM_Null;.
1eeff 20 75 2e 63 62 2e 63 74 78 2e 73 2e 7a 20 3d 20 u.cb.ctx.s.z =
1ef00 30 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 73 2e 0;. u.cb.ctx.s.
1ef01 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 75 zMalloc = 0;. u
1ef02 2e 63 62 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d .cb.ctx.s.xDel =
1ef03 20 30 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 73 0;. u.cb.ctx.s
1ef04 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 63 62 .db = db;. u.cb
1ef05 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 .ctx.isError = 0
1ef06 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 70 43 6f ;. u.cb.ctx.pCo
1ef07 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e ll = 0;. if( u.
1ef08 63 62 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c cb.ctx.pFunc->fl
1ef09 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e ags & SQLITE_FUN
1ef0a 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 C_NEEDCOLL ){.
1ef0b 20 20 61 73 73 65 72 74 28 20 70 4f 70 3e 70 2d assert( pOp>p-
1ef0c 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61 73 73 65 >aOp );. asse
1ef0d 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 rt( pOp[-1].p4ty
1ef0e 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 pe==P4_COLLSEQ )
1ef0f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f ;. assert( pO
1ef10 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 p[-1].opcode==OP
1ef11 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 _CollSeq );.
1ef12 75 2e 63 62 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d u.cb.ctx.pColl =
1ef13 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70 43 6f 6c pOp[-1].p4.pCol
1ef14 6c 3b 0a 20 20 7d 0a 20 20 28 75 2e 63 62 2e 63 l;. }. (u.cb.c
1ef15 74 78 2e 70 46 75 6e 63 2d 3e 78 53 74 65 70 29 tx.pFunc->xStep)
1ef16 28 26 75 2e 63 62 2e 63 74 78 2c 20 75 2e 63 62 (&u.cb.ctx, u.cb
1ef17 2e 6e 2c 20 75 2e 63 62 2e 61 70 56 61 6c 29 3b .n, u.cb.apVal);
1ef18 0a 20 20 69 66 28 20 75 2e 63 62 2e 63 74 78 2e . if( u.cb.ctx.
1ef19 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 isError ){. s
1ef1a 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 qlite3SetString(
1ef1b 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c &p->zErrMsg, db,
1ef1c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 "%s", sqlite3_v
1ef1d 61 6c 75 65 5f 74 65 78 74 28 26 75 2e 63 62 2e alue_text(&u.cb.
1ef1e 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20 ctx.s));. rc
1ef1f 3d 20 75 2e 63 62 2e 63 74 78 2e 69 73 45 72 72 = u.cb.ctx.isErr
1ef20 6f 72 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 or;. }. sqlite
1ef21 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 3VdbeMemRelease(
1ef22 26 75 2e 63 62 2e 63 74 78 2e 73 29 3b 0a 20 20 &u.cb.ctx.s);.
1ef23 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 break;.}../* Opc
1ef24 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31 ode: AggFinal P1
1ef25 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a P2 * P4 *.**.**
1ef26 20 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e Execute the fin
1ef27 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 alizer function
1ef28 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 for an aggregate
1ef29 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20 . P1 is.** the
1ef2a 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 memory location
1ef2b 74 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75 that is the accu
1ef2c 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20 mulator for the
1ef2d 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a aggregate..**.**
1ef2e 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 P2 is the numbe
1ef2f 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 r of arguments t
1ef30 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e hat the step fun
1ef31 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a ction takes and.
1ef32 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 ** P4 is a point
1ef33 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 er to the FuncDe
1ef34 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74 f for this funct
1ef35 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20 ion. The P2.**
1ef36 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 argument is not
1ef37 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63 used by this opc
1ef38 6f 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 ode. It is only
1ef39 20 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62 there to disamb
1ef3a 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69 iguate.** functi
1ef3b 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b ons that can tak
1ef3c 65 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72 e varying number
1ef3d 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20 s of arguments.
1ef3e 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d The.** P4 argum
1ef3f 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64 ent is only need
1ef40 65 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e ed for the degen
1ef41 65 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65 erate case where
1ef42 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e .** the step fun
1ef43 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72 ction was not pr
1ef44 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e eviously called.
1ef45 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46 .*/.case OP_AggF
1ef46 69 6e 61 6c 3a 20 7b 0a 23 69 66 20 30 20 20 2f inal: {.#if 0 /
1ef47 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 * local variable
1ef48 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 s moved into u.c
1ef49 63 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d c */. Mem *pMem
1ef4a 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1ef4b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1ef4c 64 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 d into u.cc */.
1ef4d 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 assert( pOp->p1
1ef4e 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 >0 && pOp->p1<=p
1ef4f 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 63 ->nMem );. u.cc
1ef50 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f .pMem = &aMem[pO
1ef51 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1ef52 28 20 28 75 2e 63 63 2e 70 4d 65 6d 2d 3e 66 6c ( (u.cc.pMem->fl
1ef53 61 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c ags & ~(MEM_Null
1ef54 7c 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b |MEM_Agg))==0 );
1ef55 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 . rc = sqlite3V
1ef56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 75 dbeMemFinalize(u
1ef57 2e 63 63 2e 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 .cc.pMem, pOp->p
1ef58 34 2e 70 46 75 6e 63 29 3b 0a 20 20 69 66 28 20 4.pFunc);. if(
1ef59 72 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 rc ){. sqlite
1ef5a 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 3SetString(&p->z
1ef5b 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 ErrMsg, db, "%s"
1ef5c 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f , sqlite3_value_
1ef5d 74 65 78 74 28 75 2e 63 63 2e 70 4d 65 6d 29 29 text(u.cc.pMem))
1ef5e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 ;. }. sqlite3V
1ef5f 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e dbeChangeEncodin
1ef60 67 28 75 2e 63 63 2e 70 4d 65 6d 2c 20 65 6e 63 g(u.cc.pMem, enc
1ef61 6f 64 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 45 oding);. UPDATE
1ef62 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 75 2e _MAX_BLOBSIZE(u.
1ef63 63 63 2e 70 4d 65 6d 29 3b 0a 20 20 69 66 28 20 cc.pMem);. if(
1ef64 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f sqlite3VdbeMemTo
1ef65 6f 42 69 67 28 75 2e 63 63 2e 70 4d 65 6d 29 20 oBig(u.cc.pMem)
1ef66 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f ){. goto too_
1ef67 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b big;. }. break
1ef68 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e ;.}...#if !defin
1ef69 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ed(SQLITE_OMIT_V
1ef6a 41 43 55 55 4d 29 20 26 26 20 21 64 65 66 69 6e ACUUM) && !defin
1ef6b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ed(SQLITE_OMIT_A
1ef6c 54 54 41 43 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 TTACH)./* Opcode
1ef6d 3a 20 56 61 63 75 75 6d 20 2a 20 2a 20 2a 20 2a : Vacuum * * * *
1ef6e 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 *.**.** Vacuum
1ef6f 74 68 65 20 65 6e 74 69 72 65 20 64 61 74 61 62 the entire datab
1ef70 61 73 65 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 ase. This opcod
1ef71 65 20 77 69 6c 6c 20 63 61 75 73 65 20 6f 74 68 e will cause oth
1ef72 65 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 er virtual.** ma
1ef73 63 68 69 6e 65 73 20 74 6f 20 62 65 20 63 72 65 chines to be cre
1ef74 61 74 65 64 20 61 6e 64 20 72 75 6e 2e 20 20 49 ated and run. I
1ef75 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 61 6c t may not be cal
1ef76 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a led from within.
1ef77 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e ** a transaction
1ef78 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 61 63 ..*/.case OP_Vac
1ef79 75 75 6d 3a 20 7b 0a 20 20 69 66 28 20 73 71 6c uum: {. if( sql
1ef7a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 ite3SafetyOff(db
1ef7b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1ef7c 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 ue_to_misuse; .
1ef7d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 75 6e rc = sqlite3Run
1ef7e 56 61 63 75 75 6d 28 26 70 2d 3e 7a 45 72 72 4d Vacuum(&p->zErrM
1ef7f 73 67 2c 20 64 62 29 3b 0a 20 20 69 66 28 20 73 sg, db);. if( s
1ef80 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 qlite3SafetyOn(d
1ef81 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1ef82 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1ef83 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1ef84 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 ..#if !defined(S
1ef85 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 QLITE_OMIT_AUTOV
1ef86 41 43 55 55 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 ACUUM)./* Opcode
1ef87 3a 20 49 6e 63 72 56 61 63 75 75 6d 20 50 31 20 : IncrVacuum P1
1ef88 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 P2 * * *.**.** P
1ef89 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65 20 erform a single
1ef8a 73 74 65 70 20 6f 66 20 74 68 65 20 69 6e 63 72 step of the incr
1ef8b 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 70 emental vacuum p
1ef8c 72 6f 63 65 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 rocedure on.** t
1ef8d 68 65 20 50 31 20 64 61 74 61 62 61 73 65 2e 20 he P1 database.
1ef8e 49 66 20 74 68 65 20 76 61 63 75 75 6d 20 68 61 If the vacuum ha
1ef8f 73 20 66 69 6e 69 73 68 65 64 2c 20 6a 75 6d 70 s finished, jump
1ef90 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a to instruction.
1ef91 2a 2a 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 ** P2. Otherwise
1ef92 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 , fall through t
1ef93 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 o the next instr
1ef94 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 uction..*/.case
1ef95 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 3a 20 7b OP_IncrVacuum: {
1ef96 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 /* jump
1ef97 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 */.#if 0 /* loc
1ef98 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1ef99 65 64 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a ed into u.cd */.
1ef9a 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 Btree *pBt;.#e
1ef9b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1ef9c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1ef9d 74 6f 20 75 2e 63 64 20 2a 2f 0a 0a 20 20 61 73 to u.cd */.. as
1ef9e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 sert( pOp->p1>=0
1ef9f 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e && pOp->p1<db->
1efa0 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 nDb );. assert(
1efa1 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 (p->btreeMask &
1efa2 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d (1<<pOp->p1))!=
1efa3 30 20 29 3b 0a 20 20 75 2e 63 64 2e 70 42 74 20 0 );. u.cd.pBt
1efa4 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 = db->aDb[pOp->p
1efa5 31 5d 2e 70 42 74 3b 0a 20 20 72 63 20 3d 20 73 1].pBt;. rc = s
1efa6 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63 72 56 qlite3BtreeIncrV
1efa7 61 63 75 75 6d 28 75 2e 63 64 2e 70 42 74 29 3b acuum(u.cd.pBt);
1efa8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 . if( rc==SQLIT
1efa9 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70 63 E_DONE ){. pc
1efaa 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1efab 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f rc = SQLITE_
1efac 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b OK;. }. break;
1efad 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 .}.#endif../* Op
1efae 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31 20 code: Expire P1
1efaf 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 * * * *.**.** Ca
1efb0 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 64 20 use precompiled
1efb1 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 62 65 statements to be
1efb2 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 41 6e come expired. An
1efb3 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65 expired stateme
1efb4 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 nt.** fails with
1efb5 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f an error code o
1efb6 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 f SQLITE_SCHEMA
1efb7 69 66 20 69 74 20 69 73 20 65 76 65 72 20 65 78 if it is ever ex
1efb8 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 61 20 ecuted .** (via
1efb9 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 29 2e sqlite3_step()).
1efba 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 69 73 .** .** If P1 is
1efbb 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 51 4c 0, then all SQL
1efbc 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 63 6f statements beco
1efbd 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 20 50 me expired. If P
1efbe 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 1 is non-zero,.*
1efbf 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20 * then only the
1efc0 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74 currently execut
1efc1 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 69 73 ing statement is
1efc2 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 affected. .*/.c
1efc3 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a 20 7b ase OP_Expire: {
1efc4 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 31 20 . if( !pOp->p1
1efc5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
1efc6 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 pirePreparedStat
1efc7 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 65 ements(db);. }e
1efc8 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 lse{. p->expi
1efc9 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 red = 1;. }. b
1efca 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 reak;.}..#ifndef
1efcb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 SQLITE_OMIT_SHA
1efcc 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f 70 63 RED_CACHE./* Opc
1efcd 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b 20 50 ode: TableLock P
1efce 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 1 P2 P3 P4 *.**.
1efcf 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b ** Obtain a lock
1efd0 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 on a particular
1efd1 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 6e 73 table. This ins
1efd2 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 truction is only
1efd3 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 used when.** th
1efd4 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66 e shared-cache f
1efd5 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65 eature is enable
1efd6 64 2e 20 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 d. .**.** P1 is
1efd7 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 the index of the
1efd8 20 64 61 74 61 62 61 73 65 20 69 6e 20 73 71 6c database in sql
1efd9 69 74 65 33 2e 61 44 62 5b 5d 20 6f 66 20 74 68 ite3.aDb[] of th
1efda 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 6f 6e e database.** on
1efdb 20 77 68 69 63 68 20 74 68 65 20 6c 6f 63 6b 20 which the lock
1efdc 69 73 20 61 63 71 75 69 72 65 64 2e 20 20 41 20 is acquired. A
1efdd 72 65 61 64 6c 6f 63 6b 20 69 73 20 6f 62 74 61 readlock is obta
1efde 69 6e 65 64 20 69 66 20 50 33 3d 3d 30 20 6f 72 ined if P3==0 or
1efdf 0a 2a 2a 20 61 20 77 72 69 74 65 20 6c 6f 63 6b .** a write lock
1efe0 20 69 66 20 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a if P3==1..**.**
1efe1 20 50 32 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 P2 contains the
1efe2 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74 68 root-page of th
1efe3 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e e table to lock.
1efe4 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f 6e 74 61 69 .**.** P4 contai
1efe5 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 ns a pointer to
1efe6 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 the name of the
1efe7 74 61 62 6c 65 20 62 65 69 6e 67 20 6c 6f 63 6b table being lock
1efe8 65 64 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 ed. This is only
1efe9 0a 2a 2a 20 75 73 65 64 20 74 6f 20 67 65 6e 65 .** used to gene
1efea 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 rate an error me
1efeb 73 73 61 67 65 20 69 66 20 74 68 65 20 6c 6f 63 ssage if the loc
1efec 6b 20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 k cannot be obta
1efed 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 ined..*/.case OP
1efee 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 _TableLock: {.
1efef 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 3d u8 isWriteLock =
1eff0 20 28 75 38 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 (u8)pOp->p3;.
1eff1 69 66 28 20 69 73 57 72 69 74 65 4c 6f 63 6b 20 if( isWriteLock
1eff2 7c 7c 20 30 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 || 0==(db->flags
1eff3 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f &SQLITE_ReadUnco
1eff4 6d 6d 69 74 74 65 64 29 20 29 7b 0a 20 20 20 20 mmitted) ){.
1eff5 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 int p1 = pOp->p1
1eff6 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ; . assert( p
1eff7 31 3e 3d 30 20 26 26 20 70 31 3c 64 62 2d 3e 6e 1>=0 && p1<db->n
1eff8 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 Db );. assert
1eff9 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 ( (p->btreeMask
1effa 26 20 28 31 3c 3c 70 31 29 29 21 3d 30 20 29 3b & (1<<p1))!=0 );
1effb 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57 . assert( isW
1effc 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 riteLock==0 || i
1effd 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b sWriteLock==1 );
1effe 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 . rc = sqlite
1efff 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 3BtreeLockTable(
1f000 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42 74 2c db->aDb[p1].pBt,
1f001 20 70 4f 70 2d 3e 70 32 2c 20 69 73 57 72 69 74 pOp->p2, isWrit
1f002 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 eLock);. if(
1f003 28 72 63 26 30 78 46 46 29 3d 3d 53 51 4c 49 54 (rc&0xFF)==SQLIT
1f004 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 E_LOCKED ){.
1f005 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
1f006 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 = pOp->p4.z;.
1f007 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 sqlite3SetStr
1f008 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c ing(&p->zErrMsg,
1f009 20 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 74 db, "database t
1f00a 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 3a 20 able is locked:
1f00b 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 %s", z);. }.
1f00c 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1f00d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1f00e 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 MIT_SHARED_CACHE
1f00f 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1f010 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1f011 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1f012 20 56 42 65 67 69 6e 20 2a 20 2a 20 2a 20 50 34 VBegin * * * P4
1f013 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 *.**.** P4 may
1f014 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 be a pointer to
1f015 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 an sqlite3_vtab
1f016 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 73 6f structure. If so
1f017 2c 20 63 61 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 , call the .** x
1f018 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 66 6f 72 Begin method for
1f019 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a that table..**.
1f01a 2a 2a 20 41 6c 73 6f 2c 20 77 68 65 74 68 65 72 ** Also, whether
1f01b 20 6f 72 20 6e 6f 74 20 50 34 20 69 73 20 73 65 or not P4 is se
1f01c 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 t, check that th
1f01d 69 73 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 is is not being
1f01e 63 61 6c 6c 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 called from.** w
1f01f 69 74 68 69 6e 20 61 20 63 61 6c 6c 62 61 63 6b ithin a callback
1f020 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 to a virtual ta
1f021 62 6c 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68 ble xSync() meth
1f022 6f 64 2e 20 49 66 20 69 74 20 69 73 2c 20 74 68 od. If it is, th
1f023 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 e error.** code
1f024 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 53 will be set to S
1f025 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f QLITE_LOCKED..*/
1f026 0a 63 61 73 65 20 4f 50 5f 56 42 65 67 69 6e 3a .case OP_VBegin:
1f027 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1f028 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f029 65 64 20 69 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a ed into u.ce */.
1f02a 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 3b VTable *pVTab;
1f02b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c .#endif /* local
1f02c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 variables moved
1f02d 20 69 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 into u.ce */.
1f02e 75 2e 63 65 2e 70 56 54 61 62 20 3d 20 70 4f 70 u.ce.pVTab = pOp
1f02f 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 72 63 ->p4.pVtab;. rc
1f030 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 = sqlite3VtabBe
1f031 67 69 6e 28 64 62 2c 20 75 2e 63 65 2e 70 56 54 gin(db, u.ce.pVT
1f032 61 62 29 3b 0a 20 20 69 66 28 20 75 2e 63 65 2e ab);. if( u.ce.
1f033 70 56 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c pVTab ){. sql
1f034 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
1f035 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 ->zErrMsg);.
1f036 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 p->zErrMsg = u.c
1f037 65 2e 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e e.pVTab->pVtab->
1f038 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 zErrMsg;. u.c
1f039 65 2e 70 56 54 61 62 2d 3e 70 56 74 61 62 2d 3e e.pVTab->pVtab->
1f03a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d zErrMsg = 0;. }
1f03b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 . break;.}.#end
1f03c 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1f03d 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a T_VIRTUALTABLE *
1f03e 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
1f03f 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1f040 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 BLE./* Opcode: V
1f041 43 72 65 61 74 65 20 50 31 20 2a 20 2a 20 50 34 Create P1 * * P4
1f042 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 *.**.** P4 is t
1f043 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 he name of a vir
1f044 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 tual table in da
1f045 74 61 62 61 73 65 20 50 31 2e 20 43 61 6c 6c 20 tabase P1. Call
1f046 74 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 the xCreate meth
1f047 6f 64 0a 2a 2a 20 66 6f 72 20 74 68 61 74 20 74 od.** for that t
1f048 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 able..*/.case OP
1f049 5f 56 43 72 65 61 74 65 3a 20 7b 0a 20 20 72 63 _VCreate: {. rc
1f04a 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 = sqlite3VtabCa
1f04b 6c 6c 43 72 65 61 74 65 28 64 62 2c 20 70 4f 70 llCreate(db, pOp
1f04c 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c ->p1, pOp->p4.z,
1f04d 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 &p->zErrMsg);.
1f04e 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f04f 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f /* SQLITE_OMIT_
1f050 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a VIRTUALTABLE */.
1f051 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
1f052 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
1f053 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 44 65 E./* Opcode: VDe
1f054 73 74 72 6f 79 20 50 31 20 2a 20 2a 20 50 34 20 stroy P1 * * P4
1f055 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 *.**.** P4 is th
1f056 65 20 6e 61 6d 65 20 6f 66 20 61 20 76 69 72 74 e name of a virt
1f057 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 64 61 74 ual table in dat
1f058 61 62 61 73 65 20 50 31 2e 20 20 43 61 6c 6c 20 abase P1. Call
1f059 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74 the xDestroy met
1f05a 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 hod.** of that t
1f05b 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 able..*/.case OP
1f05c 5f 56 44 65 73 74 72 6f 79 3a 20 7b 0a 20 20 70 _VDestroy: {. p
1f05d 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d ->inVtabMethod =
1f05e 20 32 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 2;. rc = sqlit
1f05f 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f e3VtabCallDestro
1f060 79 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 y(db, pOp->p1, p
1f061 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e Op->p4.z);. p->
1f062 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 inVtabMethod = 0
1f063 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e ;. break;.}.#en
1f064 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
1f065 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 IT_VIRTUALTABLE
1f066 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */..#ifndef SQLI
1f067 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
1f068 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 ABLE./* Opcode:
1f069 56 4f 70 65 6e 20 50 31 20 2a 20 2a 20 50 34 20 VOpen P1 * * P4
1f06a 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 *.**.** P4 is a
1f06b 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 pointer to a vir
1f06c 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 tual table objec
1f06d 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 t, an sqlite3_vt
1f06e 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a ab structure..**
1f06f 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 P1 is a cursor
1f070 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 20 6f 70 number. This op
1f071 63 6f 64 65 20 6f 70 65 6e 73 20 61 20 63 75 72 code opens a cur
1f072 73 6f 72 20 74 6f 20 74 68 65 20 76 69 72 74 75 sor to the virtu
1f073 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 al.** table and
1f074 73 74 6f 72 65 73 20 74 68 61 74 20 63 75 72 73 stores that curs
1f075 6f 72 20 69 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 or in P1..*/.cas
1f076 65 20 4f 50 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 e OP_VOpen: {.#i
1f077 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 f 0 /* local va
1f078 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f079 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 64 62 to u.cf */. Vdb
1f07a 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 eCursor *pCur;.
1f07b 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 sqlite3_vtab_cu
1f07c 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f rsor *pVtabCurso
1f07d 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 r;. sqlite3_vta
1f07e 62 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 b *pVtab;. sqli
1f07f 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 te3_module *pMod
1f080 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c ule;.#endif /* l
1f081 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f082 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66 20 2a oved into u.cf *
1f083 2f 0a 0a 20 20 75 2e 63 66 2e 70 43 75 72 20 3d /.. u.cf.pCur =
1f084 20 30 3b 0a 20 20 75 2e 63 66 2e 70 56 74 61 62 0;. u.cf.pVtab
1f085 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 75 2e Cursor = 0;. u.
1f086 63 66 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e cf.pVtab = pOp->
1f087 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b p4.pVtab->pVtab;
1f088 0a 20 20 75 2e 63 66 2e 70 4d 6f 64 75 6c 65 20 . u.cf.pModule
1f089 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c = (sqlite3_modul
1f08a 65 20 2a 29 75 2e 63 66 2e 70 56 74 61 62 2d 3e e *)u.cf.pVtab->
1f08b 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 pModule;. asser
1f08c 74 28 75 2e 63 66 2e 70 56 74 61 62 20 26 26 20 t(u.cf.pVtab &&
1f08d 75 2e 63 66 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 u.cf.pModule);.
1f08e 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 if( sqlite3Safe
1f08f 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f tyOff(db) ) goto
1f090 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 abort_due_to_mi
1f091 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 suse;. rc = u.c
1f092 66 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e f.pModule->xOpen
1f093 28 75 2e 63 66 2e 70 56 74 61 62 2c 20 26 75 2e (u.cf.pVtab, &u.
1f094 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b cf.pVtabCursor);
1f095 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1f096 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1f097 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d ;. p->zErrMsg =
1f098 20 75 2e 63 66 2e 70 56 74 61 62 2d 3e 7a 45 72 u.cf.pVtab->zEr
1f099 72 4d 73 67 3b 0a 20 20 75 2e 63 66 2e 70 56 74 rMsg;. u.cf.pVt
1f09a 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b ab->zErrMsg = 0;
1f09b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1f09c 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 fetyOn(db) ) got
1f09d 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d o abort_due_to_m
1f09e 69 73 75 73 65 3b 0a 20 20 69 66 28 20 53 51 4c isuse;. if( SQL
1f09f 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 ITE_OK==rc ){.
1f0a0 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 /* Initialize
1f0a1 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 sqlite3_vtab_cur
1f0a2 73 6f 72 20 62 61 73 65 20 63 6c 61 73 73 20 2a sor base class *
1f0a3 2f 0a 20 20 20 20 75 2e 63 66 2e 70 56 74 61 62 /. u.cf.pVtab
1f0a4 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20 Cursor->pVtab =
1f0a5 75 2e 63 66 2e 70 56 74 61 62 3b 0a 0a 20 20 20 u.cf.pVtab;..
1f0a6 20 2f 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 76 /* Initialise v
1f0a7 64 62 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 dbe cursor objec
1f0a8 74 20 2a 2f 0a 20 20 20 20 75 2e 63 66 2e 70 43 t */. u.cf.pC
1f0a9 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 ur = allocateCur
1f0aa 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 sor(p, pOp->p1,
1f0ab 30 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20 69 0, -1, 0);. i
1f0ac 66 28 20 75 2e 63 66 2e 70 43 75 72 20 29 7b 0a f( u.cf.pCur ){.
1f0ad 20 20 20 20 20 20 75 2e 63 66 2e 70 43 75 72 2d u.cf.pCur-
1f0ae 3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 75 >pVtabCursor = u
1f0af 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 3b .cf.pVtabCursor;
1f0b0 0a 20 20 20 20 20 20 75 2e 63 66 2e 70 43 75 72 . u.cf.pCur
1f0b1 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 66 ->pModule = u.cf
1f0b2 2e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 .pVtabCursor->pV
1f0b3 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 tab->pModule;.
1f0b4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 }else{. d
1f0b5 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
1f0b6 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 63 66 2e = 1;. u.cf.
1f0b7 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 pModule->xClose(
1f0b8 75 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 u.cf.pVtabCursor
1f0b9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 );. }. }. b
1f0ba 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f reak;.}.#endif /
1f0bb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 * SQLITE_OMIT_VI
1f0bc 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 RTUALTABLE */..#
1f0bd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
1f0be 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
1f0bf 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 46 69 6c 74 /* Opcode: VFilt
1f0c0 65 72 20 50 31 20 50 32 20 50 33 20 50 34 20 2a er P1 P2 P3 P4 *
1f0c1 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 63 .**.** P1 is a c
1f0c2 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 75 73 69 ursor opened usi
1f0c3 6e 67 20 56 4f 70 65 6e 2e 20 20 50 32 20 69 73 ng VOpen. P2 is
1f0c4 20 61 6e 20 61 64 64 72 65 73 73 20 74 6f 20 6a an address to j
1f0c5 75 6d 70 20 74 6f 20 69 66 0a 2a 2a 20 74 68 65 ump to if.** the
1f0c6 20 66 69 6c 74 65 72 65 64 20 72 65 73 75 6c 74 filtered result
1f0c7 20 73 65 74 20 69 73 20 65 6d 70 74 79 2e 0a 2a set is empty..*
1f0c8 2a 0a 2a 2a 20 50 34 20 69 73 20 65 69 74 68 65 *.** P4 is eithe
1f0c9 72 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72 69 r NULL or a stri
1f0ca 6e 67 20 74 68 61 74 20 77 61 73 20 67 65 6e 65 ng that was gene
1f0cb 72 61 74 65 64 20 62 79 20 74 68 65 20 78 42 65 rated by the xBe
1f0cc 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74 68 6f stIndex.** metho
1f0cd 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e d of the module.
1f0ce 20 20 54 68 65 20 69 6e 74 65 72 70 72 65 74 61 The interpreta
1f0cf 74 69 6f 6e 20 6f 66 20 74 68 65 20 50 34 20 73 tion of the P4 s
1f0d0 74 72 69 6e 67 20 69 73 20 6c 65 66 74 0a 2a 2a tring is left.**
1f0d1 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 69 to the module i
1f0d2 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a mplementation..*
1f0d3 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 *.** This opcode
1f0d4 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 46 69 invokes the xFi
1f0d5 6c 74 65 72 20 6d 65 74 68 6f 64 20 6f 6e 20 74 lter method on t
1f0d6 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 he virtual table
1f0d7 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 specified.** by
1f0d8 20 50 31 2e 20 20 54 68 65 20 69 6e 74 65 67 65 P1. The intege
1f0d9 72 20 71 75 65 72 79 20 70 6c 61 6e 20 70 61 72 r query plan par
1f0da 61 6d 65 74 65 72 20 74 6f 20 78 46 69 6c 74 65 ameter to xFilte
1f0db 72 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 r is stored in r
1f0dc 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 2e 20 52 egister.** P3. R
1f0dd 65 67 69 73 74 65 72 20 50 33 2b 31 20 73 74 6f egister P3+1 sto
1f0de 72 65 73 20 74 68 65 20 61 72 67 63 20 70 61 72 res the argc par
1f0df 61 6d 65 74 65 72 20 74 6f 20 62 65 20 70 61 73 ameter to be pas
1f0e0 73 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 sed to the.** xF
1f0e1 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 20 52 65 ilter method. Re
1f0e2 67 69 73 74 65 72 73 20 50 33 2b 32 2e 2e 50 33 gisters P3+2..P3
1f0e3 2b 31 2b 61 72 67 63 20 61 72 65 20 74 68 65 20 +1+argc are the
1f0e4 61 72 67 63 0a 2a 2a 20 61 64 64 69 74 69 6f 6e argc.** addition
1f0e5 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 77 68 al parameters wh
1f0e6 69 63 68 20 61 72 65 20 70 61 73 73 65 64 20 74 ich are passed t
1f0e7 6f 0a 2a 2a 20 78 46 69 6c 74 65 72 20 61 73 20 o.** xFilter as
1f0e8 61 72 67 76 2e 20 52 65 67 69 73 74 65 72 20 50 argv. Register P
1f0e9 33 2b 32 20 62 65 63 6f 6d 65 73 20 61 72 67 76 3+2 becomes argv
1f0ea 5b 30 5d 20 77 68 65 6e 20 70 61 73 73 65 64 20 [0] when passed
1f0eb 74 6f 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a to xFilter..**.*
1f0ec 2a 20 41 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 * A jump is made
1f0ed 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 72 65 to P2 if the re
1f0ee 73 75 6c 74 20 73 65 74 20 61 66 74 65 72 20 66 sult set after f
1f0ef 69 6c 74 65 72 69 6e 67 20 77 6f 75 6c 64 20 62 iltering would b
1f0f0 65 20 65 6d 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 e empty..*/.case
1f0f1 20 4f 50 5f 56 46 69 6c 74 65 72 3a 20 7b 20 20 OP_VFilter: {
1f0f2 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 /* jump */.#if
1f0f3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f0f4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f0f5 20 75 2e 63 67 20 2a 2f 0a 20 20 69 6e 74 20 6e u.cg */. int n
1f0f6 41 72 67 3b 0a 20 20 69 6e 74 20 69 51 75 65 72 Arg;. int iQuer
1f0f7 79 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 y;. const sqlit
1f0f8 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 e3_module *pModu
1f0f9 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 51 75 65 72 le;. Mem *pQuer
1f0fa 79 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67 63 3b y;. Mem *pArgc;
1f0fb 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f . sqlite3_vtab_
1f0fc 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 cursor *pVtabCur
1f0fd 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 sor;. sqlite3_v
1f0fe 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 56 64 tab *pVtab;. Vd
1f0ff 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a beCursor *pCur;.
1f100 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74 int res;. int
1f101 20 69 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 i;. Mem **apAr
1f102 67 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 g;.#endif /* loc
1f103 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f104 65 64 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a ed into u.cg */.
1f105 0a 20 20 75 2e 63 67 2e 70 51 75 65 72 79 20 3d . u.cg.pQuery =
1f106 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b &aMem[pOp->p3];
1f107 0a 20 20 75 2e 63 67 2e 70 41 72 67 63 20 3d 20 . u.cg.pArgc =
1f108 26 75 2e 63 67 2e 70 51 75 65 72 79 5b 31 5d 3b &u.cg.pQuery[1];
1f109 0a 20 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 70 . u.cg.pCur = p
1f10a 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d ->apCsr[pOp->p1]
1f10b 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 ;. REGISTER_TRA
1f10c 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 67 CE(pOp->p3, u.cg
1f10d 2e 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 .pQuery);. asse
1f10e 72 74 28 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 rt( u.cg.pCur->p
1f10f 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 VtabCursor );.
1f110 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f 72 u.cg.pVtabCursor
1f111 20 3d 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 = u.cg.pCur->pV
1f112 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 tabCursor;. u.c
1f113 67 2e 70 56 74 61 62 20 3d 20 75 2e 63 67 2e 70 g.pVtab = u.cg.p
1f114 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 VtabCursor->pVta
1f115 62 3b 0a 20 20 75 2e 63 67 2e 70 4d 6f 64 75 6c b;. u.cg.pModul
1f116 65 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e e = u.cg.pVtab->
1f117 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 pModule;.. /* G
1f118 72 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 rab the index nu
1f119 6d 62 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 mber and argc pa
1f11a 72 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 rameters */. as
1f11b 73 65 72 74 28 20 28 75 2e 63 67 2e 70 51 75 65 sert( (u.cg.pQue
1f11c 72 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e ry->flags&MEM_In
1f11d 74 29 21 3d 30 20 26 26 20 75 2e 63 67 2e 70 41 t)!=0 && u.cg.pA
1f11e 72 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f rgc->flags==MEM_
1f11f 49 6e 74 20 29 3b 0a 20 20 75 2e 63 67 2e 6e 41 Int );. u.cg.nA
1f120 72 67 20 3d 20 28 69 6e 74 29 75 2e 63 67 2e 70 rg = (int)u.cg.p
1f121 41 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 Argc->u.i;. u.c
1f122 67 2e 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 g.iQuery = (int)
1f123 75 2e 63 67 2e 70 51 75 65 72 79 2d 3e 75 2e 69 u.cg.pQuery->u.i
1f124 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 ;.. /* Invoke t
1f125 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f he xFilter metho
1f126 64 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 d */. {. u.c
1f127 67 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 g.res = 0;. u
1f128 2e 63 67 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 .cg.apArg = p->a
1f129 70 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e pArg;. for(u.
1f12a 63 67 2e 69 20 3d 20 30 3b 20 75 2e 63 67 2e 69 cg.i = 0; u.cg.i
1f12b 3c 75 2e 63 67 2e 6e 41 72 67 3b 20 75 2e 63 67 <u.cg.nArg; u.cg
1f12c 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 .i++){. u.c
1f12d 67 2e 61 70 41 72 67 5b 75 2e 63 67 2e 69 5d 20 g.apArg[u.cg.i]
1f12e 3d 20 26 75 2e 63 67 2e 70 41 72 67 63 5b 75 2e = &u.cg.pArgc[u.
1f12f 63 67 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 cg.i+1];. s
1f130 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f qlite3VdbeMemSto
1f131 72 65 54 79 70 65 28 75 2e 63 67 2e 61 70 41 72 reType(u.cg.apAr
1f132 67 5b 75 2e 63 67 2e 69 5d 29 3b 0a 20 20 20 20 g[u.cg.i]);.
1f133 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 }.. if( sqlit
1f134 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 e3SafetyOff(db)
1f135 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 ) goto abort_due
1f136 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 _to_misuse;.
1f137 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 p->inVtabMethod
1f138 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e = 1;. rc = u.
1f139 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c cg.pModule->xFil
1f13a 74 65 72 28 75 2e 63 67 2e 70 56 74 61 62 43 75 ter(u.cg.pVtabCu
1f13b 72 73 6f 72 2c 20 75 2e 63 67 2e 69 51 75 65 72 rsor, u.cg.iQuer
1f13c 79 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e y, pOp->p4.z, u.
1f13d 63 67 2e 6e 41 72 67 2c 20 75 2e 63 67 2e 61 70 cg.nArg, u.cg.ap
1f13e 41 72 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 Arg);. p->inV
1f13f 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 tabMethod = 0;.
1f140 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f141 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1f142 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 ;. p->zErrMsg
1f143 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e 7a = u.cg.pVtab->z
1f144 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 67 ErrMsg;. u.cg
1f145 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 .pVtab->zErrMsg
1f146 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d = 0;. if( rc=
1f147 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1f148 20 20 20 20 75 2e 63 67 2e 72 65 73 20 3d 20 75 u.cg.res = u
1f149 2e 63 67 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f .cg.pModule->xEo
1f14a 66 28 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 f(u.cg.pVtabCurs
1f14b 6f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 or);. }. i
1f14c 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 f( sqlite3Safety
1f14d 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 On(db) ) goto ab
1f14e 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 ort_due_to_misus
1f14f 65 3b 0a 0a 20 20 20 20 69 66 28 20 75 2e 63 67 e;.. if( u.cg
1f150 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 .res ){. pc
1f151 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a = pOp->p2 - 1;.
1f152 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 63 67 }. }. u.cg
1f153 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d .pCur->nullRow =
1f154 20 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0;.. break;.}.
1f155 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1f156 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f157 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 LE */..#ifndef S
1f158 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1f159 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1f15a 65 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32 e: VColumn P1 P2
1f15b 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 P3 * *.**.** St
1f15c 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 ore the value of
1f15d 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d the P2-th colum
1f15e 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 n of.** the row
1f15f 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 of the virtual-t
1f160 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a able that the .*
1f161 2a 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 * P1 cursor is p
1f162 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 ointing to into
1f163 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a register P3..*/.
1f164 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a case OP_VColumn:
1f165 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1f166 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f167 65 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a ed into u.ch */.
1f168 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1f169 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 pVtab;. const s
1f16a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 qlite3_module *p
1f16b 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 Module;. Mem *p
1f16c 44 65 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f Dest;. sqlite3_
1f16d 63 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 context sContext
1f16e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 ;.#endif /* loca
1f16f 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 l variables move
1f170 64 20 69 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a d into u.ch */..
1f171 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 VdbeCursor *pC
1f172 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f ur = p->apCsr[pO
1f173 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1f174 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 ( pCur->pVtabCur
1f175 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 sor );. assert(
1f176 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f pOp->p3>0 && pO
1f177 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 p->p3<=p->nMem )
1f178 3b 0a 20 20 75 2e 63 68 2e 70 44 65 73 74 20 3d ;. u.ch.pDest =
1f179 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b &aMem[pOp->p3];
1f17a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e 75 6c . if( pCur->nul
1f17b 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 lRow ){. sqli
1f17c 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c te3VdbeMemSetNul
1f17d 6c 28 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 20 l(u.ch.pDest);.
1f17e 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 break;. }.
1f17f 75 2e 63 68 2e 70 56 74 61 62 20 3d 20 70 43 75 u.ch.pVtab = pCu
1f180 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e r->pVtabCursor->
1f181 70 56 74 61 62 3b 0a 20 20 75 2e 63 68 2e 70 4d pVtab;. u.ch.pM
1f182 6f 64 75 6c 65 20 3d 20 75 2e 63 68 2e 70 56 74 odule = u.ch.pVt
1f183 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 ab->pModule;. a
1f184 73 73 65 72 74 28 20 75 2e 63 68 2e 70 4d 6f 64 ssert( u.ch.pMod
1f185 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29 3b 0a ule->xColumn );.
1f186 20 20 6d 65 6d 73 65 74 28 26 75 2e 63 68 2e 73 memset(&u.ch.s
1f187 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65 Context, 0, size
1f188 6f 66 28 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 of(u.ch.sContext
1f189 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75 ));.. /* The ou
1f18a 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c tput cell may al
1f18b 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75 66 ready have a buf
1f18c 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d fer allocated. M
1f18d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 ove. ** the cur
1f18e 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74 6f rent contents to
1f18f 20 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e 73 u.ch.sContext.s
1f190 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20 so in case the
1f191 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a 20 20 user-function.
1f192 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65 20 61 ** can use the a
1f193 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 lready allocated
1f194 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 buffer instead
1f195 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 0a of allocating a.
1f196 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 ** new one..
1f197 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
1f198 4d 65 6d 4d 6f 76 65 28 26 75 2e 63 68 2e 73 43 MemMove(&u.ch.sC
1f199 6f 6e 74 65 78 74 2e 73 2c 20 75 2e 63 68 2e 70 ontext.s, u.ch.p
1f19a 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65 74 54 Dest);. MemSetT
1f19b 79 70 65 46 6c 61 67 28 26 75 2e 63 68 2e 73 43 ypeFlag(&u.ch.sC
1f19c 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 ontext.s, MEM_Nu
1f19d 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 ll);.. if( sqli
1f19e 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 te3SafetyOff(db)
1f19f 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1f1a0 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 e_to_misuse;. r
1f1a1 63 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 c = u.ch.pModule
1f1a2 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72 2d 3e ->xColumn(pCur->
1f1a3 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26 75 2e pVtabCursor, &u.
1f1a4 63 68 2e 73 43 6f 6e 74 65 78 74 2c 20 70 4f 70 ch.sContext, pOp
1f1a5 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 ->p2);. sqlite3
1f1a6 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 DbFree(db, p->zE
1f1a7 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 rrMsg);. p->zEr
1f1a8 72 4d 73 67 20 3d 20 75 2e 63 68 2e 70 56 74 61 rMsg = u.ch.pVta
1f1a9 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e b->zErrMsg;. u.
1f1aa 63 68 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 ch.pVtab->zErrMs
1f1ab 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 63 g = 0;. if( u.c
1f1ac 68 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 72 72 h.sContext.isErr
1f1ad 6f 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 75 or ){. rc = u
1f1ae 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45 .ch.sContext.isE
1f1af 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rror;. }.. /*
1f1b0 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20 Copy the result
1f1b1 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 of the function
1f1b2 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73 74 to the P3 regist
1f1b3 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f 20 74 er. We. ** do t
1f1b4 68 69 73 20 72 65 67 61 72 64 6c 65 73 73 20 6f his regardless o
1f1b5 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 f whether or not
1f1b6 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 an error occurr
1f1b7 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61 6e 79 ed to ensure any
1f1b8 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20 61 6c . ** dynamic al
1f1b9 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 75 2e 63 68 location in u.ch
1f1ba 2e 73 43 6f 6e 74 65 78 74 2e 73 20 28 61 20 4d .sContext.s (a M
1f1bb 65 6d 20 73 74 72 75 63 74 29 20 69 73 20 20 72 em struct) is r
1f1bc 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 eleased.. */.
1f1bd 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
1f1be 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 63 68 2e eEncoding(&u.ch.
1f1bf 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f sContext.s, enco
1f1c0 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53 54 45 ding);. REGISTE
1f1c1 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c R_TRACE(pOp->p3,
1f1c2 20 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 20 20 u.ch.pDest);.
1f1c3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f sqlite3VdbeMemMo
1f1c4 76 65 28 75 2e 63 68 2e 70 44 65 73 74 2c 20 26 ve(u.ch.pDest, &
1f1c5 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e 73 29 u.ch.sContext.s)
1f1c6 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 ;. UPDATE_MAX_B
1f1c7 4c 4f 42 53 49 5a 45 28 75 2e 63 68 2e 70 44 65 LOBSIZE(u.ch.pDe
1f1c8 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 st);.. if( sqli
1f1c9 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 te3SafetyOn(db)
1f1ca 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 ){. goto abor
1f1cb 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b t_due_to_misuse;
1f1cc 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 . }. if( sqlit
1f1cd 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 e3VdbeMemTooBig(
1f1ce 75 2e 63 68 2e 70 44 65 73 74 29 20 29 7b 0a 20 u.ch.pDest) ){.
1f1cf 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b goto too_big;
1f1d0 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a . }. break;.}.
1f1d1 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
1f1d2 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f1d3 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 LE */..#ifndef S
1f1d4 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
1f1d5 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 ALTABLE./* Opcod
1f1d6 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32 20 2a e: VNext P1 P2 *
1f1d7 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e * *.**.** Advan
1f1d8 63 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 ce virtual table
1f1d9 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78 74 20 P1 to the next
1f1da 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73 75 6c row in its resul
1f1db 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a 75 6d t set and.** jum
1f1dc 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e p to instruction
1f1dd 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74 68 65 P2. Or, if the
1f1de 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 virtual table h
1f1df 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20 74 68 as reached.** th
1f1e0 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72 65 73 e end of its res
1f1e1 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20 66 61 ult set, then fa
1f1e2 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 ll through to th
1f1e3 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 e next instructi
1f1e4 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 on..*/.case OP_V
1f1e5 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d Next: { /* jum
1f1e6 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c p */.#if 0 /* l
1f1e7 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f1e8 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a oved into u.ci *
1f1e9 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 /. sqlite3_vtab
1f1ea 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 *pVtab;. const
1f1eb 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 sqlite3_module
1f1ec 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 *pModule;. int
1f1ed 72 65 73 3b 0a 20 20 56 64 62 65 43 75 72 73 6f res;. VdbeCurso
1f1ee 72 20 2a 70 43 75 72 3b 0a 23 65 6e 64 69 66 20 r *pCur;.#endif
1f1ef 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f1f0 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f1f1 63 69 20 2a 2f 0a 0a 20 20 75 2e 63 69 2e 72 65 ci */.. u.ci.re
1f1f2 73 20 3d 20 30 3b 0a 20 20 75 2e 63 69 2e 70 43 s = 0;. u.ci.pC
1f1f3 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f ur = p->apCsr[pO
1f1f4 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 p->p1];. assert
1f1f5 28 20 75 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74 ( u.ci.pCur->pVt
1f1f6 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 69 66 abCursor );. if
1f1f7 28 20 75 2e 63 69 2e 70 43 75 72 2d 3e 6e 75 6c ( u.ci.pCur->nul
1f1f8 6c 52 6f 77 20 29 7b 0a 20 20 20 20 62 72 65 61 lRow ){. brea
1f1f9 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 69 2e 70 56 k;. }. u.ci.pV
1f1fa 74 61 62 20 3d 20 75 2e 63 69 2e 70 43 75 72 2d tab = u.ci.pCur-
1f1fb 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 >pVtabCursor->pV
1f1fc 74 61 62 3b 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 tab;. u.ci.pMod
1f1fd 75 6c 65 20 3d 20 75 2e 63 69 2e 70 56 74 61 62 ule = u.ci.pVtab
1f1fe 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61 73 73 ->pModule;. ass
1f1ff 65 72 74 28 20 75 2e 63 69 2e 70 4d 6f 64 75 6c ert( u.ci.pModul
1f200 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20 2f e->xNext );.. /
1f201 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 * Invoke the xNe
1f202 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 xt() method of t
1f203 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 he module. There
1f204 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 is no way for t
1f205 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 he. ** underlyi
1f206 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ng implementatio
1f207 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 n to return an e
1f208 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 75 rror if one occu
1f209 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 78 rs during. ** x
1f20a 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61 64 2c Next(). Instead,
1f20b 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 if an error occ
1f20c 75 72 73 2c 20 74 72 75 65 20 69 73 20 72 65 74 urs, true is ret
1f20d 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e urned (indicatin
1f20e 67 20 74 68 61 74 0a 20 20 2a 2a 20 64 61 74 61 g that. ** data
1f20f 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 61 is available) a
1f210 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 nd the error cod
1f211 65 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 e returned when
1f212 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 xColumn or. **
1f213 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68 6f some other metho
1f214 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b 65 d is next invoke
1f215 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76 69 d on the save vi
1f216 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 rtual table curs
1f217 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 or.. */. if( s
1f218 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 qlite3SafetyOff(
1f219 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 db) ) goto abort
1f21a 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a _due_to_misuse;.
1f21b 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f p->inVtabMetho
1f21c 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 75 2e d = 1;. rc = u.
1f21d 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 ci.pModule->xNex
1f21e 74 28 75 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74 t(u.ci.pCur->pVt
1f21f 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d 3e abCursor);. p->
1f220 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 inVtabMethod = 0
1f221 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 ;. sqlite3DbFre
1f222 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 e(db, p->zErrMsg
1f223 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 );. p->zErrMsg
1f224 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 = u.ci.pVtab->zE
1f225 72 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e 70 56 rrMsg;. u.ci.pV
1f226 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 tab->zErrMsg = 0
1f227 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 ;. if( rc==SQLI
1f228 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 2e 63 TE_OK ){. u.c
1f229 69 2e 72 65 73 20 3d 20 75 2e 63 69 2e 70 4d 6f i.res = u.ci.pMo
1f22a 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 69 2e dule->xEof(u.ci.
1f22b 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f pCur->pVtabCurso
1f22c 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 r);. }. if( sq
1f22d 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1f22e 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1f22f 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 ue_to_misuse;..
1f230 20 69 66 28 20 21 75 2e 63 69 2e 72 65 73 20 29 if( !u.ci.res )
1f231 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 {. /* If ther
1f232 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70 20 e is data, jump
1f233 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 63 20 to P2 */. pc
1f234 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 = pOp->p2 - 1;.
1f235 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 }. break;.}.#e
1f236 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
1f237 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
1f238 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c */..#ifndef SQL
1f239 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
1f23a 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a TABLE./* Opcode:
1f23b 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a 20 VRename P1 * *
1f23c 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 P4 *.**.** P4 is
1f23d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 a pointer to a
1f23e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 virtual table ob
1f23f 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 ject, an sqlite3
1f240 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e _vtab structure.
1f241 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 .** This opcode
1f242 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 invokes the corr
1f243 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61 6d esponding xRenam
1f244 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 76 61 e method. The va
1f245 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 lue.** in regist
1f246 65 72 20 50 31 20 69 73 20 70 61 73 73 65 64 20 er P1 is passed
1f247 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 72 67 as the zName arg
1f248 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 52 65 ument to the xRe
1f249 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a name method..*/.
1f24a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65 3a case OP_VRename:
1f24b 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 {.#if 0 /* loc
1f24c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 al variables mov
1f24d 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a ed into u.cj */.
1f24e 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a sqlite3_vtab *
1f24f 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a 70 4e pVtab;. Mem *pN
1f250 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c ame;.#endif /* l
1f251 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d ocal variables m
1f252 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a oved into u.cj *
1f253 2f 0a 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 20 /.. u.cj.pVtab
1f254 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d = pOp->p4.pVtab-
1f255 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 >pVtab;. u.cj.p
1f256 4e 61 6d 65 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 Name = &aMem[pOp
1f257 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 ->p1];. assert(
1f258 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f u.cj.pVtab->pMo
1f259 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b dule->xRename );
1f25a 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 . REGISTER_TRAC
1f25b 45 28 70 4f 70 2d 3e 70 31 2c 20 75 2e 63 6a 2e E(pOp->p1, u.cj.
1f25c 70 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 pName);. assert
1f25d 28 20 75 2e 63 6a 2e 70 4e 61 6d 65 2d 3e 66 6c ( u.cj.pName->fl
1f25e 61 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b ags & MEM_Str );
1f25f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 . if( sqlite3Sa
1f260 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f fetyOff(db) ) go
1f261 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f to abort_due_to_
1f262 6d 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 misuse;. rc = u
1f263 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 .cj.pVtab->pModu
1f264 6c 65 2d 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6a le->xRename(u.cj
1f265 2e 70 56 74 61 62 2c 20 75 2e 63 6a 2e 70 4e 61 .pVtab, u.cj.pNa
1f266 6d 65 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 me->z);. sqlite
1f267 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 3DbFree(db, p->z
1f268 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 ErrMsg);. p->zE
1f269 72 72 4d 73 67 20 3d 20 75 2e 63 6a 2e 70 56 74 rrMsg = u.cj.pVt
1f26a 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 ab->zErrMsg;. u
1f26b 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d .cj.pVtab->zErrM
1f26c 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 sg = 0;. if( sq
1f26d 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 lite3SafetyOn(db
1f26e 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 ) ) goto abort_d
1f26f 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 ue_to_misuse;..
1f270 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 break;.}.#endif
1f271 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f272 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 _OMIT_VIRTUALTAB
1f273 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 LE./* Opcode: VU
1f274 70 64 61 74 65 20 50 31 20 50 32 20 50 33 20 50 pdate P1 P2 P3 P
1f275 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 4 *.**.** P4 is
1f276 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 a pointer to a v
1f277 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a irtual table obj
1f278 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f ect, an sqlite3_
1f279 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a vtab structure..
1f27a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 ** This opcode i
1f27b 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 nvokes the corre
1f27c 73 70 6f 6e 64 69 6e 67 20 78 55 70 64 61 74 65 sponding xUpdate
1f27d 20 6d 65 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 method. P2 valu
1f27e 65 73 0a 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 es.** are contig
1f27f 75 6f 75 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c uous memory cell
1f280 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 50 33 s starting at P3
1f281 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68 65 20 to pass to the
1f282 78 55 70 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f xUpdate .** invo
1f283 63 61 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 cation. The valu
1f284 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 28 50 e in register (P
1f285 33 2b 50 32 2d 31 29 20 63 6f 72 72 65 73 70 6f 3+P2-1) correspo
1f286 6e 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 nds to the .** p
1f287 32 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 2th element of t
1f288 68 65 20 61 72 67 76 20 61 72 72 61 79 20 70 61 he argv array pa
1f289 73 73 65 64 20 74 6f 20 78 55 70 64 61 74 65 2e ssed to xUpdate.
1f28a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 .**.** The xUpda
1f28b 74 65 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 te method will d
1f28c 6f 20 61 20 44 45 4c 45 54 45 20 6f 72 20 61 6e o a DELETE or an
1f28d 20 49 4e 53 45 52 54 20 6f 72 20 62 6f 74 68 2e INSERT or both.
1f28e 0a 2a 2a 20 54 68 65 20 61 72 67 76 5b 30 5d 20 .** The argv[0]
1f28f 65 6c 65 6d 65 6e 74 20 28 77 68 69 63 68 20 63 element (which c
1f290 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65 orresponds to me
1f291 6d 6f 72 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a mory cell P3).**
1f292 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 is the rowid of
1f293 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 a row to delete
1f294 2e 20 20 49 66 20 61 72 67 76 5b 30 5d 20 69 73 . If argv[0] is
1f295 20 4e 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a NULL then no .*
1f296 2a 20 64 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72 * deletion occur
1f297 73 2e 20 20 54 68 65 20 61 72 67 76 5b 31 5d 20 s. The argv[1]
1f298 65 6c 65 6d 65 6e 74 20 69 73 20 74 68 65 20 72 element is the r
1f299 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 owid of the new
1f29a 0a 2a 2a 20 72 6f 77 2e 20 20 54 68 69 73 20 63 .** row. This c
1f29b 61 6e 20 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61 an be NULL to ha
1f29c 76 65 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 ve the virtual t
1f29d 61 62 6c 65 20 73 65 6c 65 63 74 20 74 68 65 20 able select the
1f29e 6e 65 77 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f new .** rowid fo
1f29f 72 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 73 r itself. The s
1f2a0 75 62 73 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e ubsequent elemen
1f2a1 74 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 ts in the array
1f2a2 61 72 65 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 are .** the valu
1f2a3 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e es of columns in
1f2a4 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a the new row..**
1f2a5 0a 2a 2a 20 49 66 20 50 32 3d 3d 31 20 74 68 65 .** If P2==1 the
1f2a6 6e 20 6e 6f 20 69 6e 73 65 72 74 20 69 73 20 70 n no insert is p
1f2a7 65 72 66 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b erformed. argv[
1f2a8 30 5d 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 0] is the rowid
1f2a9 6f 66 0a 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64 of.** a row to d
1f2aa 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 elete..**.** P1
1f2ab 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 is a boolean fla
1f2ac 67 2e 20 49 66 20 69 74 20 69 73 20 73 65 74 20 g. If it is set
1f2ad 74 6f 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 to true and the
1f2ae 78 55 70 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 xUpdate call.**
1f2af 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 is successful, t
1f2b0 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65 hen the value re
1f2b1 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 turned by sqlite
1f2b2 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 3_last_insert_ro
1f2b3 77 69 64 28 29 20 0a 2a 2a 20 69 73 20 73 65 74 wid() .** is set
1f2b4 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 to the value of
1f2b5 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 the rowid for t
1f2b6 68 65 20 72 6f 77 20 6a 75 73 74 20 69 6e 73 65 he row just inse
1f2b7 72 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 rted..*/.case OP
1f2b8 5f 56 55 70 64 61 74 65 3a 20 7b 0a 23 69 66 20 _VUpdate: {.#if
1f2b9 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 0 /* local vari
1f2ba 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f ables moved into
1f2bb 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 u.ck */. sqlit
1f2bc 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a e3_vtab *pVtab;.
1f2bd 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 sqlite3_module
1f2be 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 *pModule;. int
1f2bf 20 6e 41 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a nArg;. int i;.
1f2c0 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 sqlite_int64 r
1f2c1 6f 77 69 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 owid;. Mem **ap
1f2c2 41 72 67 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a Arg;. Mem *pX;.
1f2c3 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 #endif /* local
1f2c4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1f2c5 69 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 into u.ck */..
1f2c6 75 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 4f 70 u.ck.pVtab = pOp
1f2c7 2d 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 ->p4.pVtab->pVta
1f2c8 62 3b 0a 20 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c b;. u.ck.pModul
1f2c9 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 e = (sqlite3_mod
1f2ca 75 6c 65 20 2a 29 75 2e 63 6b 2e 70 56 74 61 62 ule *)u.ck.pVtab
1f2cb 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 ->pModule;. u.c
1f2cc 6b 2e 6e 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 k.nArg = pOp->p2
1f2cd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d ;. assert( pOp-
1f2ce 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 >p4type==P4_VTAB
1f2cf 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 );. if( ALWAYS
1f2d0 28 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 2d 3e 78 (u.ck.pModule->x
1f2d1 55 70 64 61 74 65 29 20 29 7b 0a 20 20 20 20 75 Update) ){. u
1f2d2 2e 63 6b 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 .ck.apArg = p->a
1f2d3 70 41 72 67 3b 0a 20 20 20 20 75 2e 63 6b 2e 70 pArg;. u.ck.p
1f2d4 58 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 X = &aMem[pOp->p
1f2d5 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 6b 3];. for(u.ck
1f2d6 2e 69 3d 30 3b 20 75 2e 63 6b 2e 69 3c 75 2e 63 .i=0; u.ck.i<u.c
1f2d7 6b 2e 6e 41 72 67 3b 20 75 2e 63 6b 2e 69 2b 2b k.nArg; u.ck.i++
1f2d8 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
1f2d9 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 VdbeMemStoreType
1f2da 28 75 2e 63 6b 2e 70 58 29 3b 0a 20 20 20 20 20 (u.ck.pX);.
1f2db 20 75 2e 63 6b 2e 61 70 41 72 67 5b 75 2e 63 6b u.ck.apArg[u.ck
1f2dc 2e 69 5d 20 3d 20 75 2e 63 6b 2e 70 58 3b 0a 20 .i] = u.ck.pX;.
1f2dd 20 20 20 20 20 75 2e 63 6b 2e 70 58 2b 2b 3b 0a u.ck.pX++;.
1f2de 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 }. if( sq
1f2df 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1f2e0 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f b) ) goto abort_
1f2e1 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 due_to_misuse;.
1f2e2 20 20 20 72 63 20 3d 20 75 2e 63 6b 2e 70 4d 6f rc = u.ck.pMo
1f2e3 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 28 75 2e dule->xUpdate(u.
1f2e4 63 6b 2e 70 56 74 61 62 2c 20 75 2e 63 6b 2e 6e ck.pVtab, u.ck.n
1f2e5 41 72 67 2c 20 75 2e 63 6b 2e 61 70 41 72 67 2c Arg, u.ck.apArg,
1f2e6 20 26 75 2e 63 6b 2e 72 6f 77 69 64 29 3b 0a 20 &u.ck.rowid);.
1f2e7 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f2e8 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 (db, p->zErrMsg)
1f2e9 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 ;. p->zErrMsg
1f2ea 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e 7a = u.ck.pVtab->z
1f2eb 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 6b ErrMsg;. u.ck
1f2ec 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 .pVtab->zErrMsg
1f2ed 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c = 0;. if( sql
1f2ee 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 ite3SafetyOn(db)
1f2ef 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 ) goto abort_du
1f2f0 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 e_to_misuse;.
1f2f1 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f2f2 4f 4b 20 26 26 20 70 4f 70 2d 3e 70 31 20 29 7b OK && pOp->p1 ){
1f2f3 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 . assert( u
1f2f4 2e 63 6b 2e 6e 41 72 67 3e 31 20 26 26 20 75 2e .ck.nArg>1 && u.
1f2f5 63 6b 2e 61 70 41 72 67 5b 30 5d 20 26 26 20 28 ck.apArg[0] && (
1f2f6 75 2e 63 6b 2e 61 70 41 72 67 5b 30 5d 2d 3e 66 u.ck.apArg[0]->f
1f2f7 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 29 20 29 lags&MEM_Null) )
1f2f8 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 61 73 74 ;. db->last
1f2f9 52 6f 77 69 64 20 3d 20 75 2e 63 6b 2e 72 6f 77 Rowid = u.ck.row
1f2fa 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d id;. }. p-
1f2fb 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 7d 0a >nChange++;. }.
1f2fc 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 break;.}.#endi
1f2fd 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1f2fe 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1f2ff 0a 0a 23 69 66 6e 64 65 66 20 20 53 51 4c 49 54 ..#ifndef SQLIT
1f300 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 E_OMIT_PAGER_PRA
1f301 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 GMAS./* Opcode:
1f302 50 61 67 65 63 6f 75 6e 74 20 50 31 20 50 32 20 Pagecount P1 P2
1f303 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 * * *.**.** Writ
1f304 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 e the current nu
1f305 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e mber of pages in
1f306 20 64 61 74 61 62 61 73 65 20 50 31 20 74 6f 20 database P1 to
1f307 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 32 2e 0a memory cell P2..
1f308 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 67 65 63 */.case OP_Pagec
1f309 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 ount: {
1f30a 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 /* out2-prere
1f30b 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 lease */.#if 0
1f30c 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c /* local variabl
1f30d 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e es moved into u.
1f30e 63 6c 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 0a cl */. int p1;.
1f30f 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 50 int nPage;. P
1f310 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 23 65 ager *pPager;.#e
1f311 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 ndif /* local va
1f312 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e riables moved in
1f313 74 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e to u.cl */.. u.
1f314 63 6c 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b cl.p1 = pOp->p1;
1f315 0a 20 20 75 2e 63 6c 2e 70 50 61 67 65 72 20 3d . u.cl.pPager =
1f316 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 sqlite3BtreePag
1f317 65 72 28 64 62 2d 3e 61 44 62 5b 75 2e 63 6c 2e er(db->aDb[u.cl.
1f318 70 31 5d 2e 70 42 74 29 3b 0a 20 20 72 63 20 3d p1].pBt);. rc =
1f319 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 sqlite3PagerPag
1f31a 65 63 6f 75 6e 74 28 75 2e 63 6c 2e 70 50 61 67 ecount(u.cl.pPag
1f31b 65 72 2c 20 26 75 2e 63 6c 2e 6e 50 61 67 65 29 er, &u.cl.nPage)
1f31c 3b 0a 20 20 2f 2a 20 4f 50 5f 50 61 67 65 63 6f ;. /* OP_Pageco
1f31d 75 6e 74 20 69 73 20 61 6c 77 61 79 73 20 63 61 unt is always ca
1f31e 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e lled from within
1f31f 20 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 a read transact
1f320 69 6f 6e 2e 20 20 54 68 65 0a 20 20 2a 2a 20 70 ion. The. ** p
1f321 61 67 65 20 63 6f 75 6e 74 20 68 61 73 20 61 6c age count has al
1f322 72 65 61 64 79 20 62 65 65 6e 20 73 75 63 63 65 ready been succe
1f323 73 73 66 75 6c 6c 79 20 72 65 61 64 20 61 6e 64 ssfully read and
1f324 20 63 61 63 68 65 64 2e 20 20 53 6f 20 74 68 65 cached. So the
1f325 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 . ** sqlite3Pag
1f326 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 63 61 erPagecount() ca
1f327 6c 6c 20 61 62 6f 76 65 20 63 61 6e 6e 6f 74 20 ll above cannot
1f328 66 61 69 6c 2e 20 2a 2f 0a 20 20 69 66 28 20 41 fail. */. if( A
1f329 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45 LWAYS(rc==SQLITE
1f32a 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 70 4f 75 74 _OK) ){. pOut
1f32b 2d 3e 75 2e 69 20 3d 20 75 2e 63 6c 2e 6e 50 61 ->u.i = u.cl.nPa
1f32c 67 65 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b ge;. }. break;
1f32d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 .}.#endif..#ifnd
1f32e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 ef SQLITE_OMIT_T
1f32f 52 41 43 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 RACE./* Opcode:
1f330 54 72 61 63 65 20 2a 20 2a 20 2a 20 50 34 20 2a Trace * * * P4 *
1f331 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 61 63 69 6e .**.** If tracin
1f332 67 20 69 73 20 65 6e 61 62 6c 65 64 20 28 62 79 g is enabled (by
1f333 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 72 61 the sqlite3_tra
1f334 63 65 28 29 29 20 69 6e 74 65 72 66 61 63 65 2c ce()) interface,
1f335 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 55 54 46 then.** the UTF
1f336 2d 38 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 -8 string contai
1f337 6e 65 64 20 69 6e 20 50 34 20 69 73 20 65 6d 69 ned in P4 is emi
1f338 74 74 65 64 20 6f 6e 20 74 68 65 20 74 72 61 63 tted on the trac
1f339 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 63 e callback..*/.c
1f33a 61 73 65 20 4f 50 5f 54 72 61 63 65 3a 20 7b 0a ase OP_Trace: {.
1f33b 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 #if 0 /* local
1f33c 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 variables moved
1f33d 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20 20 63 into u.cm */. c
1f33e 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 23 65 6e har *zTrace;.#en
1f33f 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 dif /* local var
1f340 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 iables moved int
1f341 6f 20 75 2e 63 6d 20 2a 2f 0a 0a 20 20 75 2e 63 o u.cm */.. u.c
1f342 6d 2e 7a 54 72 61 63 65 20 3d 20 28 70 4f 70 2d m.zTrace = (pOp-
1f343 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e 70 34 2e >p4.z ? pOp->p4.
1f344 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 z : p->zSql);.
1f345 69 66 28 20 75 2e 63 6d 2e 7a 54 72 61 63 65 20 if( u.cm.zTrace
1f346 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 ){. if( db->x
1f347 54 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 64 Trace ){. d
1f348 62 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 b->xTrace(db->pT
1f349 72 61 63 65 41 72 67 2c 20 75 2e 63 6d 2e 7a 54 raceArg, u.cm.zT
1f34a 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 race);. }.#if
1f34b 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
1f34c 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66 6c . if( (db->fl
1f34d 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71 6c ags & SQLITE_Sql
1f34e 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20 20 Trace)!=0 ){.
1f34f 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 sqlite3DebugP
1f350 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63 65 rintf("SQL-trace
1f351 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 6d 2e 7a 54 : %s\n", u.cm.zT
1f352 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e race);. }.#en
1f353 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 dif /* SQLITE_DE
1f354 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72 65 BUG */. }. bre
1f355 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f ak;.}.#endif.../
1f356 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20 2a * Opcode: Noop *
1f357 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 * * * *.**.** D
1f358 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69 73 o nothing. This
1f359 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 instruction is
1f35a 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 61 73 20 often useful as
1f35b 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69 6e a jump.** destin
1f35c 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 ation..*/./*.**
1f35d 54 68 65 20 6d 61 67 69 63 20 45 78 70 6c 61 69 The magic Explai
1f35e 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e 6c n opcode are onl
1f35f 79 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 20 y inserted when
1f360 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69 63 explain==2 (whic
1f361 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 20 77 h.** is to say w
1f362 68 65 6e 20 74 68 65 20 45 58 50 4c 41 49 4e 20 hen the EXPLAIN
1f363 51 55 45 52 59 20 50 4c 41 4e 20 73 79 6e 74 61 QUERY PLAN synta
1f364 78 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20 54 x is used.).** T
1f365 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63 6f 72 his opcode recor
1f366 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 ds information f
1f367 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 rom the optimize
1f368 72 2e 20 20 49 74 20 69 73 20 74 68 65 0a 2a 2a r. It is the.**
1f369 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20 6e the same as a n
1f36a 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 63 6f o-op. This opco
1f36b 64 65 73 6e 65 76 65 72 20 61 70 70 65 61 72 73 desnever appears
1f36c 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 70 72 in a real VM pr
1f36d 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75 6c ogram..*/.defaul
1f36e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a t: { /*
1f36f 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c 79 20 This is really
1f370 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f 45 OP_Noop and OP_E
1f371 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65 61 xplain */. brea
1f372 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a k;.}../*********
1f373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f377 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73 65 ****.** The case
1f378 73 20 6f 66 20 74 68 65 20 73 77 69 74 63 68 20 s of the switch
1f379 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 20 statement above
1f37a 74 68 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c 64 this line should
1f37b 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65 64 all be indented
1f37c 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 65 73 2e .** by 6 spaces.
1f37d 20 20 42 75 74 20 74 68 65 20 6c 65 66 74 2d 6d But the left-m
1f37e 6f 73 74 20 36 20 73 70 61 63 65 73 20 68 61 76 ost 6 spaces hav
1f37f 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20 74 e been removed t
1f380 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a 2a o improve the.**
1f381 20 72 65 61 64 61 62 69 6c 69 74 79 2e 20 20 46 readability. F
1f382 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74 20 6f rom this point o
1f383 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72 6d n down, the norm
1f384 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 72 al indentation r
1f385 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65 73 74 ules are.** rest
1f386 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ored..**********
1f387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f38a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f38b 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 66 64 ***/. }..#ifd
1f38c 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a ef VDBE_PROFILE.
1f38d 20 20 20 20 7b 0a 20 20 20 20 20 20 75 36 34 20 {. u64
1f38e 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 69 74 65 elapsed = sqlite
1f38f 33 48 77 74 69 6d 65 28 29 20 2d 20 73 74 61 72 3Hwtime() - star
1f390 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 79 t;. pOp->cy
1f391 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64 3b cles += elapsed;
1f392 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74 2b . pOp->cnt+
1f393 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20 20 +;.#if 0.
1f394 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c fprintf(stdout,
1f395 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 6c 61 70 "%10llu ", elap
1f396 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 sed);. sq
1f397 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 lite3VdbePrintOp
1f398 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50 63 2c (stdout, origPc,
1f399 20 26 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b 0a &aOp[origPc]);.
1f39a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65 6e #endif. }.#en
1f39b 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 dif.. /* The
1f39c 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 61 following code a
1f39d 64 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 dds nothing to t
1f39e 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74 69 he actual functi
1f39f 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20 6f onality. ** o
1f3a0 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 f the program.
1f3a1 49 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65 20 It is only here
1f3a2 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 for testing and
1f3a3 64 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20 2a debugging.. *
1f3a4 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 * On the other h
1f3a5 61 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75 72 and, it does bur
1f3a6 6e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76 65 n CPU cycles eve
1f3a7 72 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68 0a ry time through.
1f3a8 20 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c 75 ** the evalu
1f3a9 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20 77 ator loop. So w
1f3aa 65 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20 6f e can leave it o
1f3ab 75 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20 69 ut when NDEBUG i
1f3ac 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20 2a s defined.. *
1f3ad 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 /.#ifndef NDEBUG
1f3ae 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e . assert( pc>
1f3af 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 =-1 && pc<p->nOp
1f3b0 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 );..#ifdef SQLI
1f3b1 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 TE_DEBUG. if(
1f3b2 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 p->trace ){.
1f3b3 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 66 if( rc!=0 ) f
1f3b4 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65 2c printf(p->trace,
1f3b5 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a 20 "rc=%d\n",rc);.
1f3b6 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 if( pOp->op
1f3b7 66 6c 61 67 73 20 26 20 28 4f 50 46 4c 47 5f 4f flags & (OPFLG_O
1f3b8 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 7c 4f UT2_PRERELEASE|O
1f3b9 50 46 4c 47 5f 4f 55 54 32 29 20 29 7b 0a 20 20 PFLG_OUT2) ){.
1f3ba 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54 72 registerTr
1f3bb 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70 4f ace(p->trace, pO
1f3bc 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70 4f 70 p->p2, &aMem[pOp
1f3bd 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a ->p2]);. }.
1f3be 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f if( pOp->o
1f3bf 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f pflags & OPFLG_O
1f3c0 55 54 33 20 29 7b 0a 20 20 20 20 20 20 20 20 72 UT3 ){. r
1f3c1 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e egisterTrace(p->
1f3c2 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 trace, pOp->p3,
1f3c3 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 29 3b &aMem[pOp->p3]);
1f3c4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 . }. }.#
1f3c5 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 endif /* SQLITE
1f3c6 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69 66 _DEBUG */.#endif
1f3c7 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 /* NDEBUG */.
1f3c8 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f } /* The end o
1f3c9 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c 6f f the for(;;) lo
1f3ca 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68 72 op the loops thr
1f3cb 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f 0a ough opcodes */.
1f3cc 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 . /* If we reac
1f3cd 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 h this point, it
1f3ce 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65 63 means that exec
1f3cf 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 65 ution is finishe
1f3d0 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20 65 d with. ** an e
1f3d1 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69 6e rror of some kin
1f3d2 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72 72 d.. */.vdbe_err
1f3d3 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65 72 or_halt:. asser
1f3d4 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72 63 t( rc );. p->rc
1f3d5 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 = rc;. sqlite3
1f3d6 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 69 VdbeHalt(p);. i
1f3d7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f f( rc==SQLITE_IO
1f3d8 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e ERR_NOMEM ) db->
1f3d9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 mallocFailed = 1
1f3da 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f ;. rc = SQLITE_
1f3db 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 65 73 ERROR;. if( res
1f3dc 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20 etSchemaOnFault
1f3dd 29 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e ) sqlite3ResetIn
1f3de 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c ternalSchema(db,
1f3df 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 0);.. /* This
1f3e0 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 is the only way
1f3e1 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72 6f 63 out of this proc
1f3e2 65 64 75 72 65 2e 20 20 57 65 20 68 61 76 65 20 edure. We have
1f3e3 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 to. ** release
1f3e4 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 62 the mutexes on b
1f3e5 74 72 65 65 73 20 74 68 61 74 20 77 65 72 65 20 trees that were
1f3e6 61 63 71 75 69 72 65 64 20 61 74 20 74 68 65 0a acquired at the.
1f3e7 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 64 62 ** top. */.vdb
1f3e8 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 71 6c 69 e_return:. sqli
1f3e9 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 te3BtreeMutexArr
1f3ea 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 ayLeave(&p->aMut
1f3eb 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 ex);. return rc
1f3ec 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 ;.. /* Jump to
1f3ed 68 65 72 65 20 69 66 20 61 20 73 74 72 69 6e 67 here if a string
1f3ee 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 65 72 20 or blob larger
1f3ef 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f than SQLITE_MAX_
1f3f0 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 73 20 65 LENGTH. ** is e
1f3f1 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 20 2a 2f ncountered.. */
1f3f2 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 6c 69 .too_big:. sqli
1f3f3 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d te3SetString(&p-
1f3f4 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 73 >zErrMsg, db, "s
1f3f5 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f tring or blob to
1f3f6 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 20 3d 20 o big");. rc =
1f3f7 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 SQLITE_TOOBIG;.
1f3f8 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 goto vdbe_error
1f3f9 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d _halt;.. /* Jum
1f3fa 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 6d p to here if a m
1f3fb 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 20 alloc() fails..
1f3fc 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 */.no_mem:. db
1f3fd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d ->mallocFailed =
1f3fe 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 1;. sqlite3Set
1f3ff 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d String(&p->zErrM
1f400 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 6f 66 20 sg, db, "out of
1f401 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 63 20 3d memory");. rc =
1f402 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 SQLITE_NOMEM;.
1f403 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 goto vdbe_error
1f404 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 6d _halt;.. /* Jum
1f405 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20 61 6e p to here for an
1f406 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 SQLITE_MISUSE e
1f407 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72 74 rror.. */.abort
1f408 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3a 0a _due_to_misuse:.
1f409 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 rc = SQLITE_MI
1f40a 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 SUSE;. /* Fall
1f40b 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72 74 5f thru into abort_
1f40c 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a 2f 0a due_to_error */.
1f40d 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 . /* Jump to he
1f40e 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 re for any other
1f40f 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c 20 65 kind of fatal e
1f410 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63 22 20 rror. The "rc"
1f411 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 73 68 variable. ** sh
1f412 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 72 ould hold the er
1f413 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f ror number.. */
1f414 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 .abort_due_to_er
1f415 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 20 70 ror:. assert( p
1f416 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b 0a ->zErrMsg==0 );.
1f417 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
1f418 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20 53 51 Failed ) rc = SQ
1f419 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69 66 LITE_NOMEM;. if
1f41a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 ( rc!=SQLITE_IOE
1f41b 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 RR_NOMEM ){.
1f41c 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1f41d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1f41e 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 , "%s", sqlite3E
1f41f 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 7d 0a rrStr(rc));. }.
1f420 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f goto vdbe_erro
1f421 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75 r_halt;.. /* Ju
1f422 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 74 68 mp to here if th
1f423 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 e sqlite3_interr
1f424 75 70 74 28 29 20 41 50 49 20 73 65 74 73 20 74 upt() API sets t
1f425 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20 20 2a he interrupt. *
1f426 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62 6f * flag.. */.abo
1f427 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 rt_due_to_interr
1f428 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28 20 64 upt:. assert( d
1f429 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 b->u1.isInterrup
1f42a 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 53 51 ted );. rc = SQ
1f42b 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a LITE_INTERRUPT;.
1f42c 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 p->rc = rc;.
1f42d 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 sqlite3SetString
1f42e 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 (&p->zErrMsg, db
1f42f 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 , "%s", sqlite3E
1f430 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 67 6f rrStr(rc));. go
1f431 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 to vdbe_error_ha
1f432 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a lt;.}../********
1f433 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 ****** End of vd
1f434 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a be.c ***********
1f435 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f436 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f437 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a *****/./********
1f438 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c ****** Begin fil
1f439 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a e vdbeblob.c ***
1f43a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f43b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f43c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 *****/./*.** 200
1f43d 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 54 68 7 May 1.**.** Th
1f43e 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
1f43f 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
1f440 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
1f441 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
1f442 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
1f443 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
1f444 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
1f445 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
1f446 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
1f447 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
1f448 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
1f449 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
1f44a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
1f44b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
1f44c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
1f44d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
1f44e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
1f44f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f451 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f452 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f453 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 ******.**.** Thi
1f454 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 s file contains
1f455 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 code used to imp
1f456 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65 6e 74 lement increment
1f457 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a 2f 0a al BLOB I/O..*/.
1f458 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
1f459 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 0a _OMIT_INCRBLOB..
1f45a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c 69 /*.** Valid sqli
1f45b 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c 65 te3_blob* handle
1f45c 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72 62 s point to Incrb
1f45d 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e 0a lob structures..
1f45e 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 */.typedef struc
1f45f 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72 62 t Incrblob Incrb
1f460 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63 72 lob;.struct Incr
1f461 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c 61 blob {. int fla
1f462 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
1f463 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c 61 /* Copy of "fla
1f464 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73 71 gs" passed to sq
1f465 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
1f466 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 ) */. int nByte
1f467 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f ; /
1f468 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20 62 * Size of open b
1f469 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a 2f lob, in bytes */
1f46a 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 . int iOffset;
1f46b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 /* By
1f46c 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c 6f te offset of blo
1f46d 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74 61 b in cursor data
1f46e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a */. BtCursor *
1f46f 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f 2a pCsr; /*
1f470 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 Cursor pointing
1f471 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f 0a at blob row */.
1f472 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a sqlite3_stmt *
1f473 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74 61 pStmt; /* Sta
1f474 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20 63 tement holding c
1f475 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20 20 ursor open */.
1f476 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 sqlite3 *db;
1f477 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 /* The a
1f478 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 ssociated databa
1f479 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 se */.};../*.**
1f47a 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e 64 Open a blob hand
1f47b 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 le..*/.SQLITE_AP
1f47c 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c I int sqlite3_bl
1f47d 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 ob_open(. sqlit
1f47e 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20 20 e3* db,
1f47f 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 /* The databa
1f480 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
1f481 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1f482 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 Db, /* Th
1f483 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 e attached datab
1f484 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ase containing t
1f485 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e he blob */. con
1f486 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c st char *zTable,
1f487 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c /* The tabl
1f488 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 e containing the
1f489 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 blob */. const
1f48a 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 20 char *zColumn,
1f48b 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e /* The column
1f48c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 containing the
1f48d 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 blob */. sqlite
1f48e 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20 20 _int64 iRow,
1f48f 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f 6e /* The row con
1f490 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f 62 taining the glob
1f491 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c */. int flags,
1f492 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f493 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77 72 True -> read/wr
1f494 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c 73 ite access, fals
1f495 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20 2a e -> read-only *
1f496 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 /. sqlite3_blob
1f497 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20 48 **ppBlob /* H
1f498 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73 andle for access
1f499 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65 74 ing the blob ret
1f49a 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29 7b urned here */.){
1f49b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74 20 . int nAttempt
1f49c 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b = 0;. int iCol;
1f49d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f49e 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c 75 * Index of zColu
1f49f 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72 64 mn in row-record
1f4a0 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 56 */.. /* This V
1f4a1 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65 6b DBE program seek
1f4a2 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f 72 s a btree cursor
1f4a3 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 to the identifi
1f4a4 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62 6c ed . ** db/tabl
1f4a5 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68 65 e/row entry. The
1f4a6 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69 6e reason for usin
1f4a7 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61 6d g a vdbe program
1f4a8 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f 66 instead. ** of
1f4a9 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74 6f writing code to
1f4aa 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65 20 use the b-tree
1f4ab 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20 69 layer directly i
1f4ac 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 s that the. **
1f4ad 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69 6c vdbe program wil
1f4ae 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 l take advantage
1f4af 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 of the various
1f4b0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 2a transaction,. *
1f4b1 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65 72 * locking and er
1f4b2 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e 66 ror handling inf
1f4b3 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69 6c rastructure buil
1f4b4 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e t into the vdbe.
1f4b5 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65 72 . **. ** After
1f4b6 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75 72 seeking the cur
1f4b7 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65 78 sor, the vdbe ex
1f4b8 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65 73 ecutes an OP_Res
1f4b9 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f 64 ultRow.. ** Cod
1f4ba 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74 68 e external to th
1f4bb 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f 72 e Vdbe then "bor
1f4bc 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65 65 rows" the b-tree
1f4bd 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a 2a cursor and. **
1f4be 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70 6c uses it to impl
1f4bf 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f 72 ement the blob_r
1f4c0 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69 74 ead(), blob_writ
1f4c1 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62 6c e() and . ** bl
1f4c2 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63 74 ob_bytes() funct
1f4c3 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 ions.. **. **
1f4c4 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 The sqlite3_blob
1f4c5 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69 6f _close() functio
1f4c6 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65 20 n finalizes the
1f4c7 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20 20 vdbe program,.
1f4c8 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73 20 ** which closes
1f4c9 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73 6f the b-tree curso
1f4ca 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79 29 r and (possibly)
1f4cb 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20 20 commits the .
1f4cc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a ** transaction..
1f4cd 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f */. static co
1f4ce 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 6f nst VdbeOpList o
1f4cf 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20 20 penBlob[] = {.
1f4d0 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f {OP_Transactio
1f4d1 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 n, 0, 0, 0},
1f4d2 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20 74 /* 0: Start a t
1f4d3 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 ransaction */.
1f4d4 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b {OP_VerifyCook
1f4d5 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 ie, 0, 0, 0},
1f4d6 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68 65 /* 1: Check the
1f4d7 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a schema cookie *
1f4d8 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65 4c /. {OP_TableL
1f4d9 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 ock, 0, 0, 0},
1f4da 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75 69 /* 2: Acqui
1f4db 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 re a read or wri
1f4dc 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 te lock */..
1f4dd 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f /* One of the fo
1f4de 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 llowing two inst
1f4df 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c ructions is repl
1f4e0 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e 6f aced by an OP_No
1f4e1 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f op. */. {OP_O
1f4e2 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20 30 penRead, 0, 0, 0
1f4e3 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 }, /* 3:
1f4e4 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f Open cursor 0 fo
1f4e5 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20 20 r reading */.
1f4e6 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 {OP_OpenWrite,
1f4e7 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 0, 0, 0},
1f4e8 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73 6f /* 4: Open curso
1f4e9 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72 69 r 0 for read/wri
1f4ea 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f 56 te */.. {OP_V
1f4eb 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20 31 ariable, 1, 1, 1
1f4ec 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 }, /* 5:
1f4ed 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20 74 Push the rowid t
1f4ee 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 o the stack */.
1f4ef 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 {OP_NotExists
1f4f0 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20 20 , 0, 9, 1},
1f4f1 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68 65 /* 6: Seek the
1f4f2 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 7b cursor */. {
1f4f3 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c OP_Column, 0, 0,
1f4f4 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 1}, /*
1f4f5 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 52 7 */. {OP_R
1f4f6 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c 20 esultRow, 1, 0,
1f4f7 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20 20 0}, /* 8
1f4f8 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73 65 */. {OP_Close
1f4f9 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 , 0, 0, 0},
1f4fa 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20 /* 9 */.
1f4fb 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 {OP_Halt, 0,
1f4fc 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 0, 0},
1f4fd 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b 0a /* 10 */. };.
1f4fe 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a . Vdbe *v = 0;.
1f4ff 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
1f500 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 45 E_OK;. char *zE
1f501 72 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 rr = 0;. Table
1f502 2a 70 54 61 62 3b 0a 20 20 50 61 72 73 65 20 2a *pTab;. Parse *
1f503 70 50 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42 6c pParse;.. *ppBl
1f504 6f 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 ob = 0;. sqlite
1f505 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 3_mutex_enter(db
1f506 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 50 61 72 ->mutex);. pPar
1f507 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 se = sqlite3Stac
1f508 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 kAllocRaw(db, si
1f509 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a zeof(*pParse));.
1f50a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 if( pParse==0
1f50b 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 ){. rc = SQLI
1f50c 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f TE_NOMEM;. go
1f50d 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1f50e 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20 20 ;. }. do {.
1f50f 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c 20 memset(pParse,
1f510 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 0, sizeof(Parse)
1f511 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64 );. pParse->d
1f512 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66 28 b = db;.. if(
1f513 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e sqlite3SafetyOn
1f514 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 (db) ){. sq
1f515 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f516 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c zErr);. sql
1f517 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 ite3StackFree(db
1f518 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 , pParse);.
1f519 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c sqlite3_mutex_l
1f51a 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b eave(db->mutex);
1f51b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
1f51c 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 LITE_MISUSE;.
1f51d 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 }.. sqlite3B
1f51e 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 treeEnterAll(db)
1f51f 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c ;. pTab = sql
1f520 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 ite3LocateTable(
1f521 70 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c pParse, 0, zTabl
1f522 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 e, zDb);. if(
1f523 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74 75 pTab && IsVirtu
1f524 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 al(pTab) ){.
1f525 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 pTab = 0;.
1f526 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
1f527 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f g(pParse, "canno
1f528 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20 74 t open virtual t
1f529 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62 6c able: %s", zTabl
1f52a 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 e);. }.#ifnde
1f52b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 f SQLITE_OMIT_VI
1f52c 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 EW. if( pTab
1f52d 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 && pTab->pSelect
1f52e 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d ){. pTab =
1f52f 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0;. sqlite
1f530 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
1f531 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 , "cannot open v
1f532 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c 65 iew: %s", zTable
1f533 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
1f534 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29 7b if( !pTab ){
1f535 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 . if( pPars
1f536 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 e->zErrMsg ){.
1f537 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 sqlite3DbF
1f538 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 ree(db, zErr);.
1f539 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70 50 zErr = pP
1f53a 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 arse->zErrMsg;.
1f53b 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a pParse->z
1f53c 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 ErrMsg = 0;.
1f53d 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 53 }. rc = S
1f53e 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 QLITE_ERROR;.
1f53f 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 (void)sqlite3
1f540 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 SafetyOff(db);.
1f541 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 sqlite3Btre
1f542 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 eLeaveAll(db);.
1f543 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f goto blob_o
1f544 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a pen_out;. }..
1f545 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63 /* Now searc
1f546 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 65 h pTab for the e
1f547 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a xact column. */.
1f548 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 for(iCol=0;
1f549 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f iCol < pTab->nCo
1f54a 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20 l; iCol++) {.
1f54b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 if( sqlite3St
1f54c 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c rICmp(pTab->aCol
1f54d 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 [iCol].zName, zC
1f54e 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 olumn)==0 ){.
1f54f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
1f550 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 }. }. if
1f551 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43 ( iCol==pTab->nC
1f552 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 ol ){. sqli
1f553 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 te3DbFree(db, zE
1f554 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 rr);. zErr
1f555 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 = sqlite3MPrintf
1f556 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f (db, "no such co
1f557 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20 7a lumn: \"%s\"", z
1f558 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 72 Column);. r
1f559 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1f55a 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 ;. (void)sq
1f55b 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 lite3SafetyOff(d
1f55c 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 b);. sqlite
1f55d 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 3BtreeLeaveAll(d
1f55e 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 b);. goto b
1f55f 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 lob_open_out;.
1f560 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 }.. /* If t
1f561 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69 6e he value is bein
1f562 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 g opened for wri
1f563 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61 74 ting, check that
1f564 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 the. ** colu
1f565 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78 65 mn is not indexe
1f566 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 69 d, and that it i
1f567 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 s not part of a
1f568 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 0a 20 20 foreign key. .
1f569 20 20 2a 2a 20 49 74 20 69 73 20 61 67 61 69 6e ** It is again
1f56a 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f 20 st the rules to
1f56b 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 6f open a column to
1f56c 20 77 68 69 63 68 20 65 69 74 68 65 72 20 6f 66 which either of
1f56d 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 64 65 these. ** de
1f56e 73 63 72 69 70 74 69 6f 6e 73 20 61 70 70 6c 69 scriptions appli
1f56f 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 es for writing.
1f570 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61 67 */. if( flag
1f571 73 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 s ){. const
1f572 20 63 68 61 72 20 2a 7a 46 61 75 6c 74 20 3d 20 char *zFault =
1f573 30 3b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 0;. Index *
1f574 70 49 64 78 3b 0a 23 69 66 6e 64 65 66 20 53 51 pIdx;.#ifndef SQ
1f575 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 LITE_OMIT_FOREIG
1f576 4e 5f 4b 45 59 0a 20 20 20 20 20 20 69 66 28 20 N_KEY. if(
1f577 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 db->flags&SQLITE
1f578 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a _ForeignKeys ){.
1f579 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b /* Check
1f57a 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e that the column
1f57b 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 is not part of
1f57c 61 6e 20 46 4b 20 63 68 69 6c 64 20 6b 65 79 20 an FK child key
1f57d 64 65 66 69 6e 69 74 69 6f 6e 2e 20 49 74 0a 20 definition. It.
1f57e 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 ** is not
1f57f 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63 68 necessary to ch
1f580 65 63 6b 20 69 66 20 69 74 20 69 73 20 70 61 72 eck if it is par
1f581 74 20 6f 66 20 61 20 70 61 72 65 6e 74 20 6b 65 t of a parent ke
1f582 79 2c 20 61 73 20 70 61 72 65 6e 74 0a 20 20 20 y, as parent.
1f583 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c 75 ** key colu
1f584 6d 6e 73 20 6d 75 73 74 20 62 65 20 69 6e 64 65 mns must be inde
1f585 78 65 64 2e 20 54 68 65 20 63 68 65 63 6b 20 62 xed. The check b
1f586 65 6c 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20 75 elow will pick u
1f587 70 20 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 p this .
1f588 2a 2a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 ** case. */.
1f589 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 FKey *pFKey
1f58a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 46 ;. for(pF
1f58b 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b Key=pTab->pFKey;
1f58c 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 pFKey; pFKey=pF
1f58d 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b Key->pNextFrom){
1f58e 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a . int j
1f58f 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 ;. for(
1f590 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d 3e 6e 43 j=0; j<pFKey->nC
1f591 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 ol; j++){.
1f592 20 20 20 20 20 20 69 66 28 20 70 46 4b 65 79 2d if( pFKey-
1f593 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3d 3d >aCol[j].iFrom==
1f594 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 iCol ){.
1f595 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 zFault = "
1f596 66 6f 72 65 69 67 6e 20 6b 65 79 22 3b 0a 20 20 foreign key";.
1f597 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1f598 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1f599 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }. }.#endif
1f59a 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d . for(pIdx=
1f59b 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 pTab->pIndex; pI
1f59c 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 dx; pIdx=pIdx->p
1f59d 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 Next){. i
1f59e 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f nt j;. fo
1f59f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e r(j=0; j<pIdx->n
1f5a0 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 Column; j++){.
1f5a1 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 if( pIdx
1f5a2 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 ->aiColumn[j]==i
1f5a3 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 Col ){.
1f5a4 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e 64 zFault = "ind
1f5a5 65 78 65 64 22 3b 0a 20 20 20 20 20 20 20 20 20 exed";.
1f5a6 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
1f5a7 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a }. if( z
1f5a8 46 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 Fault ){.
1f5a9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
1f5aa 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 b, zErr);.
1f5ab 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 zErr = sqlite3
1f5ac 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e MPrintf(db, "can
1f5ad 6e 6f 74 20 6f 70 65 6e 20 25 73 20 63 6f 6c 75 not open %s colu
1f5ae 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22 2c mn for writing",
1f5af 20 7a 46 61 75 6c 74 29 3b 0a 20 20 20 20 20 20 zFault);.
1f5b0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 rc = SQLITE_ER
1f5b1 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 28 76 6f ROR;. (vo
1f5b2 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 id)sqlite3Safety
1f5b3 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 20 Off(db);.
1f5b4 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1f5b5 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 veAll(db);.
1f5b6 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 goto blob_ope
1f5b7 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 n_out;. }.
1f5b8 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73 71 }.. v = sq
1f5b9 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65 28 lite3VdbeCreate(
1f5ba 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20 29 db);. if( v )
1f5bb 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 {. int iDb
1f5bc 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
1f5bd 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d oIndex(db, pTab-
1f5be 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 >pSchema);.
1f5bf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
1f5c0 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66 28 pList(v, sizeof(
1f5c1 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f 66 openBlob)/sizeof
1f5c2 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f 70 (VdbeOpList), op
1f5c3 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 66 enBlob);. f
1f5c4 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b 20 lags = !!flags;
1f5c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5c6 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61 67 /* flags = (flag
1f5c7 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a 0a s ? 1 : 0); */..
1f5c8 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
1f5c9 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73 61 re the OP_Transa
1f5ca 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 ction */. s
1f5cb 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f5cc 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a 20 P1(v, 0, iDb);.
1f5cd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5ce 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20 66 ChangeP2(v, 0, f
1f5cf 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f 2a lags);.. /*
1f5d0 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f Configure the O
1f5d1 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a P_VerifyCookie *
1f5d2 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 /. sqlite3V
1f5d3 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 31 dbeChangeP1(v, 1
1f5d4 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 , iDb);. sq
1f5d5 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
1f5d6 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 2(v, 1, pTab->pS
1f5d7 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f chema->schema_co
1f5d8 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a okie);.. /*
1f5d9 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 74 Make sure a mut
1f5da 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 ex is held on th
1f5db 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 e table to be ac
1f5dc 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 20 cessed */.
1f5dd 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 sqlite3VdbeUsesB
1f5de 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a tree(v, iDb); ..
1f5df 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 /* Configu
1f5e0 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65 4c re the OP_TableL
1f5e1 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ock instruction
1f5e2 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
1f5e3 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 VdbeChangeP1(v,
1f5e4 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 2, iDb);. s
1f5e5 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 qlite3VdbeChange
1f5e6 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e 74 P2(v, 2, pTab->t
1f5e7 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 num);. sqli
1f5e8 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 te3VdbeChangeP3(
1f5e9 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20 20 v, 2, flags);.
1f5ea 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
1f5eb 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70 54 hangeP4(v, 2, pT
1f5ec 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54 52 ab->zName, P4_TR
1f5ed 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20 20 ANSIENT);..
1f5ee 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68 65 /* Remove eithe
1f5ef 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72 69 r the OP_OpenWri
1f5f0 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e 20 te or OpenRead.
1f5f1 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20 20 Set the P2 .
1f5f2 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 6f ** parameter o
1f5f3 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20 70 f the other to p
1f5f4 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a 20 Tab->tnum. */.
1f5f5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5f6 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 ChangeToNoop(v,
1f5f7 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a 20 4 - flags, 1);.
1f5f8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
1f5f9 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b 20 ChangeP2(v, 3 +
1f5fa 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e 75 flags, pTab->tnu
1f5fb 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 m);. sqlite
1f5fc 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 3VdbeChangeP3(v,
1f5fd 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62 29 3 + flags, iDb)
1f5fe 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 ;.. /* Conf
1f5ff 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 igure the number
1f600 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f 6e of columns. Con
1f601 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73 6f figure the curso
1f602 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 r to. ** th
1f603 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61 62 ink that the tab
1f604 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65 20 le has one more
1f605 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20 72 column than it r
1f606 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20 64 eally. ** d
1f607 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75 6d oes. An OP_Colum
1f608 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 n to retrieve th
1f609 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f 6c is imaginary col
1f60a 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a umn will. *
1f60b 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 * always return
1f60c 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68 69 an SQL NULL. Thi
1f60d 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63 61 s is useful beca
1f60e 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 use it means.
1f60f 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e 76 ** we can inv
1f610 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f oke OP_Column to
1f611 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64 62 fill in the vdb
1f612 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20 0a e cursors type .
1f613 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66 66 ** and off
1f614 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f 75 set cache withou
1f615 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49 4f t causing any IO
1f616 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 .. */.
1f617 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
1f618 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73 2c geP4(v, 3+flags,
1f619 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 SQLITE_INT_TO_P
1f61a 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 TR(pTab->nCol+1)
1f61b 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 ,P4_INT32);.
1f61c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1f61d 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61 62 ngeP2(v, 7, pTab
1f61e 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 69 ->nCol);. i
1f61f 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 f( !db->mallocFa
1f620 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 iled ){.
1f621 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 sqlite3VdbeMakeR
1f622 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31 2c eady(v, 1, 1, 1,
1f623 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 0, 0, 0);.
1f624 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 20 }. }. .
1f625 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 sqlite3BtreeLea
1f626 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72 veAll(db);. r
1f627 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 c = sqlite3Safet
1f628 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 66 yOff(db);. if
1f629 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 ( NEVER(rc!=SQLI
1f62a 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d 61 TE_OK) || db->ma
1f62b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
1f62c 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 goto blob_op
1f62d 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 en_out;. }..
1f62e 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f sqlite3_bind_
1f62f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f 73 int64((sqlite3_s
1f630 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f 77 tmt *)v, 1, iRow
1f631 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 );. rc = sqli
1f632 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74 65 te3_step((sqlite
1f633 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 3_stmt *)v);.
1f634 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
1f635 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41 74 ROW ){. nAt
1f636 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20 72 tempt++;. r
1f637 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 c = sqlite3_fina
1f638 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 lize((sqlite3_st
1f639 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20 73 mt *)v);. s
1f63a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
1f63b 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 zErr);. zE
1f63c 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 rr = sqlite3MPri
1f63d 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33 5f ntf(db, sqlite3_
1f63e 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 errmsg(db));.
1f63f 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d 0a v = 0;. }.
1f640 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74 65 } while( nAtte
1f641 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51 4c mpt<5 && rc==SQL
1f642 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a 20 ITE_SCHEMA );..
1f643 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f if( rc==SQLITE_
1f644 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 ROW ){. /* Th
1f645 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61 73 e row-record has
1f646 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75 63 been opened suc
1f647 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63 6b cessfully. Check
1f648 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a that the. **
1f649 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73 74 column in quest
1f64a 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65 78 ion contains tex
1f64b 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66 20 t or a blob. If
1f64c 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 it contains.
1f64d 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20 75 ** text, it is u
1f64e 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 p to the caller
1f64f 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f 64 to get the encod
1f650 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20 2a ing right.. *
1f651 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20 2a /. Incrblob *
1f652 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20 74 pBlob;. u32 t
1f653 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 ype = v->apCsr[0
1f654 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b 0a ]->aType[iCol];.
1f655 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31 32 . if( type<12
1f656 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
1f657 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 3DbFree(db, zErr
1f658 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d 20 );. zErr =
1f659 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
1f65a 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 b, "cannot open
1f65b 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 73 value of type %s
1f65c 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 70 ",. typ
1f65d 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 70 e==0?"null": typ
1f65e 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 6e e==7?"real": "in
1f65f 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b 0a teger". );.
1f660 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 rc = SQLIT
1f661 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 67 E_ERROR;. g
1f662 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 oto blob_open_ou
1f663 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c t;. }. pBl
1f664 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a ob = (Incrblob *
1f665 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 )sqlite3DbMalloc
1f666 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
1f667 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 20 Incrblob));.
1f668 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1f669 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 iled ){. sq
1f66a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1f66b 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 6f pBlob);. go
1f66c 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 to blob_open_out
1f66d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f ;. }. pBlo
1f66e 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73 b->flags = flags
1f66f 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 73 ;. pBlob->pCs
1f670 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 5d r = v->apCsr[0]
1f671 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 73 ->pCursor;. s
1f672 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 qlite3BtreeEnter
1f673 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 Cursor(pBlob->pC
1f674 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 sr);. sqlite3
1f675 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c BtreeCacheOverfl
1f676 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b ow(pBlob->pCsr);
1f677 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 . sqlite3Btre
1f678 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 6c eLeaveCursor(pBl
1f679 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 70 ob->pCsr);. p
1f67a 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 73 Blob->pStmt = (s
1f67b 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 3b qlite3_stmt *)v;
1f67c 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 66 . pBlob->iOff
1f67d 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30 set = v->apCsr[0
1f67e 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 5d ]->aOffset[iCol]
1f67f 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 79 ;. pBlob->nBy
1f680 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 te = sqlite3Vdbe
1f681 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 SerialTypeLen(ty
1f682 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e pe);. pBlob->
1f683 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 70 db = db;. *pp
1f684 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 5f Blob = (sqlite3_
1f685 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 blob *)pBlob;.
1f686 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b rc = SQLITE_OK
1f687 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 ;. }else if( rc
1f688 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 ==SQLITE_OK ){.
1f689 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f68a 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 (db, zErr);.
1f68b 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 zErr = sqlite3MP
1f68c 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 rintf(db, "no su
1f68d 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c ch rowid: %lld",
1f68e 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d iRow);. rc =
1f68f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
1f690 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 }..blob_open_ou
1f691 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28 72 t:. if( v && (r
1f692 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 c!=SQLITE_OK ||
1f693 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
1f694 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 ) ){. sqlite3
1f695 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b VdbeFinalize(v);
1f696 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 . }. sqlite3Er
1f697 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72 72 ror(db, rc, zErr
1f698 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 );. sqlite3DbFr
1f699 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 ee(db, zErr);.
1f69a 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 sqlite3StackFree
1f69b 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 (db, pParse);.
1f69c 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 rc = sqlite3ApiE
1f69d 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 xit(db, rc);. s
1f69e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 qlite3_mutex_lea
1f69f 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 ve(db->mutex);.
1f6a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f return rc;.}../
1f6a1 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c 6f *.** Close a blo
1f6a2 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61 b handle that wa
1f6a3 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72 65 s previously cre
1f6a4 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 ated using.** sq
1f6a5 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 lite3_blob_open(
1f6a6 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 )..*/.SQLITE_API
1f6a7 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f int sqlite3_blo
1f6a8 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f b_close(sqlite3_
1f6a9 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 blob *pBlob){.
1f6aa 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28 49 Incrblob *p = (I
1f6ab 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b ncrblob *)pBlob;
1f6ac 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c . int rc;. sql
1f6ad 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 ite3 *db;.. if(
1f6ae 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20 70 p ){. db = p
1f6af 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 ->db;. sqlite
1f6b0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 3_mutex_enter(db
1f6b1 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 ->mutex);. rc
1f6b2 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c = sqlite3_final
1f6b3 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a 20 ize(p->pStmt);.
1f6b4 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
1f6b5 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c (db, p);. sql
1f6b6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1f6b7 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d (db->mutex);. }
1f6b8 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 else{. rc = S
1f6b9 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 QLITE_OK;. }.
1f6ba 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a return rc;.}../*
1f6bb 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65 .** Perform a re
1f6bc 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65 72 ad or write oper
1f6bd 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62 0a ation on a blob.
1f6be 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c */.static int bl
1f6bf 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20 73 obReadWrite(. s
1f6c0 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c qlite3_blob *pBl
1f6c1 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c 20 ob, . void *z,
1f6c2 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e 74 . int n, . int
1f6c3 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e 74 iOffset, . int
1f6c4 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72 73 (*xCall)(BtCurs
1f6c5 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20 76 or*, u32, u32, v
1f6c6 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 oid*).){. int r
1f6c7 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 c;. Incrblob *p
1f6c8 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 = (Incrblob *)p
1f6c9 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b Blob;. Vdbe *v;
1f6ca 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a . sqlite3 *db;.
1f6cb 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 . if( p==0 ) re
1f6cc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 turn SQLITE_MISU
1f6cd 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 SE;. db = p->db
1f6ce 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 ;. sqlite3_mute
1f6cf 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 x_enter(db->mute
1f6d0 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 2a x);. v = (Vdbe*
1f6d1 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 66 )p->pStmt;.. if
1f6d2 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74 ( n<0 || iOffset
1f6d3 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e <0 || (iOffset+n
1f6d4 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20 )>p->nByte ){.
1f6d5 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 20 /* Request is
1f6d6 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 65 out of range. Re
1f6d7 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 turn a transient
1f6d8 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 72 error. */. r
1f6d9 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 c = SQLITE_ERROR
1f6da 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 ;. sqlite3Err
1f6db 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 or(db, SQLITE_ER
1f6dc 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c 73 ROR, 0);. } els
1f6dd 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 e if( v==0 ){.
1f6de 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 /* If there is
1f6df 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 61 no statement ha
1f6e0 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 62 ndle, then the b
1f6e1 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20 lob-handle has.
1f6e2 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 ** already be
1f6e3 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 en invalidated.
1f6e4 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 Return SQLITE_AB
1f6e5 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 65 ORT in this case
1f6e6 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 .. */. rc
1f6e7 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a = SQLITE_ABORT;.
1f6e8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 }else{. /*
1f6e9 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 65 Call either Btre
1f6ea 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 65 eData() or Btree
1f6eb 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 51 PutData(). If SQ
1f6ec 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 20 LITE_ABORT is.
1f6ed 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 63 ** returned, c
1f6ee 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 74 lean-up the stat
1f6ef 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20 ement handle..
1f6f0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 */. assert(
1f6f1 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a db == v->db );.
1f6f2 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 sqlite3Btree
1f6f3 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e 70 EnterCursor(p->p
1f6f4 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 78 Csr);. rc = x
1f6f5 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f Call(p->pCsr, iO
1f6f6 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 ffset+p->iOffset
1f6f7 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c , n, z);. sql
1f6f8 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 ite3BtreeLeaveCu
1f6f9 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 rsor(p->pCsr);.
1f6fa 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 if( rc==SQLIT
1f6fb 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 20 E_ABORT ){.
1f6fc 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 sqlite3VdbeFina
1f6fd 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 70 lize(v);. p
1f6fe 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 ->pStmt = 0;.
1f6ff 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 }else{. db
1f700 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a ->errCode = rc;.
1f701 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 v->rc = rc
1f702 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 ;. }. }. rc
1f703 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 = sqlite3ApiExi
1f704 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c t(db, rc);. sql
1f705 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 ite3_mutex_leave
1f706 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 (db->mutex);. r
1f707 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
1f708 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f ** Read data fro
1f709 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e m a blob handle.
1f70a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 .*/.SQLITE_API i
1f70b 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f nt sqlite3_blob_
1f70c 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f read(sqlite3_blo
1f70d 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a b *pBlob, void *
1f70e 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f z, int n, int iO
1f70f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 6e ffset){. return
1f710 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 70 blobReadWrite(p
1f711 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 Blob, z, n, iOff
1f712 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 set, sqlite3Btre
1f713 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a eData);.}../*.**
1f714 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 61 Write data to a
1f715 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f blob handle..*/
1f716 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1f717 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 sqlite3_blob_wri
1f718 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 te(sqlite3_blob
1f719 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f *pBlob, const vo
1f71a 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e id *z, int n, in
1f71b 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 t iOffset){. re
1f71c 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 turn blobReadWri
1f71d 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20 te(pBlob, (void
1f71e 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c *)z, n, iOffset,
1f71f 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 sqlite3BtreePut
1f720 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 Data);.}../*.**
1f721 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 6e Query a blob han
1f722 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a 65 dle for the size
1f723 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a of the data..**
1f724 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 .** The Incrblob
1f725 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 20 .nByte field is
1f726 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c 69 fixed for the li
1f727 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 6e fetime of the In
1f728 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20 crblob.** so no
1f729 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65 mutex is require
1f72a 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f d for access..*/
1f72b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 .SQLITE_API int
1f72c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 sqlite3_blob_byt
1f72d 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 es(sqlite3_blob
1f72e 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 *pBlob){. Incrb
1f72f 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c lob *p = (Incrbl
1f730 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65 ob *)pBlob;. re
1f731 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79 74 turn p ? p->nByt
1f732 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 e : 0;.}..#endif
1f733 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 /* #ifndef SQLI
1f734 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 TE_OMIT_INCRBLOB
1f735 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a */../**********
1f736 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 **** End of vdbe
1f737 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a blob.c *********
1f738 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f739 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f73a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1f73b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1f73c 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a journal.c ******
1f73d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f73e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f73f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 ***/./*.** 2007
1f740 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a 20 August 22.**.**
1f741 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
1f742 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
1f743 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
1f744 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
1f745 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
1f746 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
1f747 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
1f748 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
1f749 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
1f74a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
1f74b 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
1f74c 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
1f74d 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
1f74e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
1f74f 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
1f750 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
1f751 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
1f752 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f753 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f754 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f755 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f756 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
1f757 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 his file impleme
1f758 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b 69 nts a special ki
1f759 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 nd of sqlite3_fi
1f75a 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0a 2a le object used.*
1f75b 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 63 * by SQLite to c
1f75c 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 reate journal fi
1f75d 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d 69 les if the atomi
1f75e 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 c-write optimiza
1f75f 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62 6c tion.** is enabl
1f760 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 ed..**.** The di
1f761 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 61 63 stinctive charac
1f762 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 69 73 teristic of this
1f763 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 69 73 sqlite3_file is
1f764 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 63 74 that the.** act
1f765 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c 65 ual on disk file
1f766 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 7a 69 is created lazi
1f767 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 69 6c ly. When the fil
1f768 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a 2a 2a e is created,.**
1f769 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 65 63 the caller spec
1f76a 69 66 69 65 73 20 61 20 62 75 66 66 65 72 20 73 ifies a buffer s
1f76b 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 ize for an in-me
1f76c 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f 0a 2a mory buffer to.*
1f76d 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 72 * be used to ser
1f76e 76 69 63 65 20 72 65 61 64 28 29 20 61 6e 64 20 vice read() and
1f76f 77 72 69 74 65 28 29 20 72 65 71 75 65 73 74 73 write() requests
1f770 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c . The actual fil
1f771 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 e.** on disk is
1f772 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 20 70 not created or p
1f773 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c 20 65 opulated until e
1f774 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 ither:.**.** 1
1f775 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 ) The in-memory
1f776 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 67 representation g
1f777 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 20 66 rows too large f
1f778 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 or the allocated
1f779 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 65 72 .** buffer
1f77a 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68 65 , or.** 2) The
1f77b 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 sqlite3JournalC
1f77c 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e reate() function
1f77d 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 is called..*/.#
1f77e 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 ifdef SQLITE_ENA
1f77f 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 BLE_ATOMIC_WRITE
1f780 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72 6e .../*.** A Journ
1f781 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69 73 alFile object is
1f782 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 a subclass of s
1f783 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65 64 qlite3_file used
1f784 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70 65 by.** as an ope
1f785 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f n file handle fo
1f786 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e r journal files.
1f787 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72 6e .*/.struct Journ
1f788 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69 74 alFile {. sqlit
1f789 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 e3_io_methods *p
1f78a 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49 2f Method; /* I/
1f78b 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f 75 O methods on jou
1f78c 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 rnal files */.
1f78d 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20 20 int nBuf;
1f78e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f78f 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66 5b /* Size of zBuf[
1f790 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 ] in bytes */.
1f791 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20 20 char *zBuf;
1f792 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f793 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66 66 /* Space to buff
1f794 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74 65 er journal write
1f795 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a 65 s */. int iSize
1f796 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1f797 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 /* Amount
1f798 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72 65 of zBuf[] curre
1f799 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 69 ntly used */. i
1f79a 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 nt flags;
1f79b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f79c 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a 2f * xOpen flags */
1f79d 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a . sqlite3_vfs *
1f79e 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 20 pVfs;
1f79f 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c 22 /* The "real"
1f7a0 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53 20 underlying VFS
1f7a1 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c */. sqlite3_fil
1f7a2 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20 20 e *pReal;
1f7a3 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 /* The "rea
1f7a4 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 l" underlying fi
1f7a5 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f le descriptor */
1f7a6 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
1f7a7 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 Journal;
1f7a8 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 /* Name of th
1f7a9 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a e journal file *
1f7aa 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 /.};.typedef str
1f7ab 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 uct JournalFile
1f7ac 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f 2a JournalFile;../*
1f7ad 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20 6e .** If it does n
1f7ae 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 ot already exist
1f7af 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70 6f s, create and po
1f7b0 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64 69 pulate the on-di
1f7b1 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72 20 sk file .** for
1f7b2 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a 2a JournalFile p..*
1f7b3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72 65 /.static int cre
1f7b4 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c 46 ateFile(JournalF
1f7b5 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 ile *p){. int r
1f7b6 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 c = SQLITE_OK;.
1f7b7 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20 29 if( !p->pReal )
1f7b8 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 {. sqlite3_fi
1f7b9 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71 6c le *pReal = (sql
1f7ba 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b 31 ite3_file *)&p[1
1f7bb 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ];. rc = sqli
1f7bc 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56 66 te3OsOpen(p->pVf
1f7bd 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 s, p->zJournal,
1f7be 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73 2c pReal, p->flags,
1f7bf 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 0);. if( rc=
1f7c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
1f7c1 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20 70 p->pReal = p
1f7c2 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28 20 Real;. if(
1f7c3 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20 20 p->iSize>0 ){.
1f7c4 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e assert(p->
1f7c5 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29 3b iSize<=p->nBuf);
1f7c6 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 . rc = sq
1f7c7 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d 3e lite3OsWrite(p->
1f7c8 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c 20 pReal, p->zBuf,
1f7c9 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20 20 p->iSize, 0);.
1f7ca 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1f7cb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a return rc;.}..
1f7cc 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 /*.** Close the
1f7cd 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f7ce 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 int jrnlClose(sq
1f7cf 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1f7d0 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 ){. JournalFile
1f7d1 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 *p = (JournalFi
1f7d2 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 le *)pJfd;. if(
1f7d3 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 p->pReal ){.
1f7d4 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 sqlite3OsClose(
1f7d5 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 p->pReal);. }.
1f7d6 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d sqlite3_free(p-
1f7d7 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e >zBuf);. return
1f7d8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f SQLITE_OK;.}../
1f7d9 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 *.** Read data f
1f7da 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f rom the file..*/
1f7db 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c .static int jrnl
1f7dc 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33 5f Read(. sqlite3_
1f7dd 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f file *pJfd, /
1f7de 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 * The journal fi
1f7df 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f le from which to
1f7e0 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64 20 read */. void
1f7e1 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20 20 *zBuf,
1f7e2 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65 73 /* Put the res
1f7e3 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 ults here */. i
1f7e4 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20 20 nt iAmt,
1f7e5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
1f7e6 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 of bytes to read
1f7e7 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 */. sqlite_int
1f7e8 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a 20 64 iOfst /*
1f7e9 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 Begin reading at
1f7ea 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a this offset */.
1f7eb 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 ){. int rc = SQ
1f7ec 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e LITE_OK;. Journ
1f7ed 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1f7ee 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1f7ef 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 . if( p->pReal
1f7f0 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 ){. rc = sqli
1f7f1 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52 65 te3OsRead(p->pRe
1f7f2 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 al, zBuf, iAmt,
1f7f3 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65 20 iOfst);. }else
1f7f4 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 74 29 if( (iAmt+iOfst)
1f7f5 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 >p->iSize ){.
1f7f6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 rc = SQLITE_IOE
1f7f7 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 RR_SHORT_READ;.
1f7f8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 }else{. memc
1f7f9 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42 75 py(zBuf, &p->zBu
1f7fa 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29 3b f[iOfst], iAmt);
1f7fb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 . }. return rc
1f7fc 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 ;.}../*.** Write
1f7fd 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c data to the fil
1f7fe 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f7ff 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 73 71 jrnlWrite(. sq
1f800 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1f801 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 , /* The jour
1f802 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77 68 nal file into wh
1f803 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a ich to write */.
1f804 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 const void *zB
1f805 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b 65 uf, /* Take
1f806 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 data to be writ
1f807 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f ten from here */
1f808 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 . int iAmt,
1f809 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
1f80a 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 ber of bytes to
1f80b 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 write */. sqlit
1f80c 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 e_int64 iOfst
1f80d 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 74 69 /* Begin writi
1f80e 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 ng at this offse
1f80f 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 t into the file
1f810 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d */.){. int rc =
1f811 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f SQLITE_OK;. Jo
1f812 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f813 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f814 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 fd;. if( !p->pR
1f815 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69 41 eal && (iOfst+iA
1f816 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a 20 mt)>p->nBuf ){.
1f817 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46 69 rc = createFi
1f818 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 le(p);. }. if(
1f819 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
1f81a 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52 65 {. if( p->pRe
1f81b 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d al ){. rc =
1f81c 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 sqlite3OsWrite(
1f81d 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 p->pReal, zBuf,
1f81e 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 iAmt, iOfst);.
1f81f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d }else{. m
1f820 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b 69 emcpy(&p->zBuf[i
1f821 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41 6d Ofst], zBuf, iAm
1f822 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d t);. if( p-
1f823 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69 41 >iSize<(iOfst+iA
1f824 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 mt) ){. p
1f825 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 73 74 ->iSize = (iOfst
1f826 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d 0a +iAmt);. }.
1f827 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 }. }. retu
1f828 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 rn rc;.}../*.**
1f829 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69 6c Truncate the fil
1f82a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
1f82b 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73 71 jrnlTruncate(sq
1f82c 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 lite3_file *pJfd
1f82d 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 , sqlite_int64 s
1f82e 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d ize){. int rc =
1f82f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f SQLITE_OK;. Jo
1f830 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 urnalFile *p = (
1f831 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a JournalFile *)pJ
1f832 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 fd;. if( p->pRe
1f833 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 al ){. rc = s
1f834 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 qlite3OsTruncate
1f835 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65 29 (p->pReal, size)
1f836 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69 ;. }else if( si
1f837 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 ze<p->iSize ){.
1f838 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73 69 p->iSize = si
1f839 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e ze;. }. return
1f83a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 rc;.}../*.** Sy
1f83b 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a nc the file..*/.
1f83c 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 53 static int jrnlS
1f83d 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 ync(sqlite3_file
1f83e 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61 67 *pJfd, int flag
1f83f 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 s){. int rc;.
1f840 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d JournalFile *p =
1f841 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 (JournalFile *)
1f842 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 pJfd;. if( p->p
1f843 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d Real ){. rc =
1f844 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 sqlite3OsSync(p
1f845 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29 3b ->pReal, flags);
1f846 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 . }else{. rc
1f847 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
1f848 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1f849 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 ../*.** Query th
1f84a 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 e size of the fi
1f84b 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a le in bytes..*/.
1f84c 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 46 static int jrnlF
1f84d 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f ileSize(sqlite3_
1f84e 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 file *pJfd, sqli
1f84f 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 te_int64 *pSize)
1f850 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c {. int rc = SQL
1f851 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e 61 ITE_OK;. Journa
1f852 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 lFile *p = (Jour
1f853 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a nalFile *)pJfd;.
1f854 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 if( p->pReal )
1f855 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 {. rc = sqlit
1f856 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d 3e e3OsFileSize(p->
1f857 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a 20 pReal, pSize);.
1f858 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53 69 }else{. *pSi
1f859 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74 ze = (sqlite_int
1f85a 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20 20 64) p->iSize;.
1f85b 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d }. return rc;.}
1f85c 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66 ../*.** Table of
1f85d 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f 75 methods for Jou
1f85e 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 65 33 rnalFile sqlite3
1f85f 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f _file object..*/
1f860 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73 .static struct s
1f861 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 qlite3_io_method
1f862 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74 s JournalFileMet
1f863 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 hods = {. 1,
1f864 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 /* iVe
1f865 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c 43 rsion */. jrnlC
1f866 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c lose, /* xCl
1f867 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65 61 ose */. jrnlRea
1f868 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 d, /* xRead
1f869 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65 2c */. jrnlWrite,
1f86a 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a /* xWrite *
1f86b 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 /. jrnlTruncate
1f86c 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 , /* xTruncate
1f86d 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20 20 */. jrnlSync,
1f86e 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a /* xSync */.
1f86f 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 jrnlFileSize,
1f870 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f /* xFileSize */
1f871 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 . 0,
1f872 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20 /* xLock */.
1f873 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0, /
1f874 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 * xUnlock */. 0
1f875 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a , /*
1f876 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
1f877 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 ock */. 0,
1f878 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 /* xFile
1f879 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 Control */. 0,
1f87a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1f87b 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 SectorSize */.
1f87c 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0 /
1f87d 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 * xDeviceCharact
1f87e 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a eristics */.};..
1f87f 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f /* .** Open a jo
1f880 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 urnal file..*/.S
1f881 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e QLITE_PRIVATE in
1f882 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c t sqlite3Journal
1f883 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f Open(. sqlite3_
1f884 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 vfs *pVfs,
1f885 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74 6f /* The VFS to
1f886 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c 20 use for actual
1f887 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63 6f file I/O */. co
1f888 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c nst char *zName,
1f889 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 /* Name
1f88a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 of the journal
1f88b 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 file */. sqlite
1f88c 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 3_file *pJfd,
1f88d 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 /* Prealloc
1f88e 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c 65 ated, blank file
1f88f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 handle */. int
1f890 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 flags,
1f891 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 69 /* Openi
1f892 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e ng flags */. in
1f893 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20 20 t nBuf
1f894 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 /* Byte
1f895 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f 72 s buffered befor
1f896 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 e opening the fi
1f897 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72 6e le */.){. Journ
1f898 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 alFile *p = (Jou
1f899 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b rnalFile *)pJfd;
1f89a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 . memset(p, 0,
1f89b 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 sqlite3JournalSi
1f89c 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66 28 ze(pVfs));. if(
1f89d 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20 70 nBuf>0 ){. p
1f89e 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65 33 ->zBuf = sqlite3
1f89f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66 29 MallocZero(nBuf)
1f8a0 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a 42 ;. if( !p->zB
1f8a1 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 uf ){. retu
1f8a2 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
1f8a3 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a . }. }else{.
1f8a4 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 return sqlit
1f8a5 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a e3OsOpen(pVfs, z
1f8a6 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61 67 Name, pJfd, flag
1f8a7 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e s, 0);. }. p->
1f8a8 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72 6e pMethod = &Journ
1f8a9 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a 20 alFileMethods;.
1f8aa 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66 3b p->nBuf = nBuf;
1f8ab 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66 6c . p->flags = fl
1f8ac 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72 6e ags;. p->zJourn
1f8ad 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70 2d al = zName;. p-
1f8ae 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 >pVfs = pVfs;.
1f8af 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1f8b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
1f8b1 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f 69 e argument p poi
1f8b2 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61 6c nts to a Journal
1f8b3 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c 20 File structure,
1f8b4 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 and the underlyi
1f8b5 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20 6e ng.** file has n
1f8b6 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65 61 ot yet been crea
1f8b7 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20 6e ted, create it n
1f8b8 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 ow..*/.SQLITE_PR
1f8b9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 IVATE int sqlite
1f8ba 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 73 3JournalCreate(s
1f8bb 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29 7b qlite3_file *p){
1f8bc 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68 6f . if( p->pMetho
1f8bd 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 ds!=&JournalFile
1f8be 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72 Methods ){. r
1f8bf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
1f8c0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 72 . }. return cr
1f8c1 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e 61 eateFile((Journa
1f8c2 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a 2f lFile *)p);.}../
1f8c3 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 * .** Return the
1f8c4 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 number of bytes
1f8c5 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f required to sto
1f8c6 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 re a JournalFile
1f8c7 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a 2a that uses vfs.*
1f8c8 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74 65 * pVfs to create
1f8c9 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 the underlying
1f8ca 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a on-disk files..*
1f8cb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
1f8cc 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 int sqlite3Jour
1f8cd 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f nalSize(sqlite3_
1f8ce 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 72 65 vfs *pVfs){. re
1f8cf 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f 73 turn (pVfs->szOs
1f8d0 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75 72 File+sizeof(Jour
1f8d1 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65 6e nalFile));.}.#en
1f8d2 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a dif../**********
1f8d3 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75 72 **** End of jour
1f8d4 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a nal.c **********
1f8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8d7 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ***/./**********
1f8d8 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 **** Begin file
1f8d9 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a memjournal.c ***
1f8da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8dc 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 ***/./*.** 2008
1f8dd 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a 20 October 7.**.**
1f8de 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c The author discl
1f8df 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 aims copyright t
1f8e0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f o this source co
1f8e1 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 de. In place of
1f8e2 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 .** a legal noti
1f8e3 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c ce, here is a bl
1f8e4 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 essing:.**.**
1f8e5 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 May you do good
1f8e6 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a and not evil..*
1f8e7 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e * May you fin
1f8e8 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f d forgiveness fo
1f8e9 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 r yourself and f
1f8ea 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a orgive others..*
1f8eb 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 * May you sha
1f8ec 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 re freely, never
1f8ed 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 taking more tha
1f8ee 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a n you give..**.*
1f8ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1f8f3 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
1f8f4 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e his file contain
1f8f5 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d s code use to im
1f8f6 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d 65 plement an in-me
1f8f7 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f mory rollback jo
1f8f8 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69 6e urnal..** The in
1f8f9 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b -memory rollback
1f8fa 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65 64 journal is used
1f8fb 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61 6e to journal tran
1f8fc 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a 20 sactions for.**
1f8fd 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61 62 ":memory:" datab
1f8fe 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 74 68 ases and when th
1f8ff 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4d e journal_mode=M
1f900 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 73 20 EMORY pragma is
1f901 75 73 65 64 2e 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 used..*/../* For
1f902 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 ward references
1f903 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 to internal stru
1f904 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 ctures */.typede
1f905 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 f struct MemJour
1f906 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a nal MemJournal;.
1f907 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 typedef struct F
1f908 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 ilePoint FilePoi
1f909 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 nt;.typedef stru
1f90a 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c ct FileChunk Fil
1f90b 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 eChunk;../* Spac
1f90c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f e to hold the ro
1f90d 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 llback journal i
1f90e 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 s allocated in i
1f90f 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 ncrements of.**
1f910 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 2e this many bytes.
1f911 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 .**.** The size
1f912 63 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74 74 chosen is a litt
1f913 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20 70 le less than a p
1f914 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 ower of two. Th
1f915 61 74 20 77 61 79 2c 0a 2a 2a 20 74 68 65 20 46 at way,.** the F
1f916 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20 ileChunk object
1f917 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a 65 will have a size
1f918 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78 61 that almost exa
1f919 63 74 6c 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 ctly fills.** a
1f91a 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c power-of-two all
1f91b 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d ocation. This m
1f91c 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64 20 imimizes wasted
1f91d 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d 6f space in power-o
1f91e 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 f-two.** memory
1f91f 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 allocators..*/.#
1f920 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 define JOURNAL_C
1f921 48 55 4e 4b 53 49 5a 45 20 28 28 69 6e 74 29 28 HUNKSIZE ((int)(
1f922 31 30 32 34 2d 73 69 7a 65 6f 66 28 46 69 6c 65 1024-sizeof(File
1f923 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 Chunk*)))../* Ma
1f924 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 cro to find the
1f925 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e minimum of two n
1f926 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a umeric values..*
1f927 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 /.#ifndef MIN.#
1f928 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 define MIN(x,y)
1f929 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 ((x)<(y)?(x):(y)
1f92a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 ).#endif../*.**
1f92b 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 The rollback jou
1f92c 72 6e 61 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64 rnal is composed
1f92d 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 of a linked lis
1f92e 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 t of these struc
1f92f 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 tures..*/.struct
1f930 20 46 69 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 46 FileChunk {. F
1f931 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b ileChunk *pNext;
1f932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f933 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e 20 * Next chunk in
1f934 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 the journal */.
1f935 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e u8 zChunk[JOURN
1f936 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 AL_CHUNKSIZE];
1f937 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 /* Content of t
1f938 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a his chunk */.};.
1f939 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e ./*.** An instan
1f93a 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 ce of this objec
1f93b 74 20 73 65 72 76 65 73 20 61 73 20 61 20 63 75 t serves as a cu
1f93c 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 72 6f rsor into the ro
1f93d 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a llback journal..
1f93e 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 ** The cursor ca
1f93f 6e 20 62 65 20 65 69 74 68 65 72 20 66 6f 72 20 n be either for
1f940 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 reading or writi
1f941 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 ng..*/.struct Fi
1f942 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 73 71 6c 69 lePoint {. sqli
1f943 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 te3_int64 iOffse
1f944 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f t; /* O
1f945 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62 ffset from the b
1f946 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 eginning of the
1f947 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 43 68 file */. FileCh
1f948 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 unk *pChunk;
1f949 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 65 /* Spe
1f94a 63 69 66 69 63 20 63 68 75 6e 6b 20 69 6e 74 6f cific chunk into
1f94b 20 77 68 69 63 68 20 63 75 72 73 6f 72 20 70 6f which cursor po
1f94c 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a ints */.};../*.*
1f94d 2a 20 54 68 69 73 20 73 75 62 63 6c 61 73 73 20 * This subclass
1f94e 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 is a subclass of
1f94f 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20 sqlite3_file.
1f950 45 61 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 Each open memory
1f951 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 -journal.** is a
1f952 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 n instance of th
1f953 69 73 20 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 is class..*/.str
1f954 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b uct MemJournal {
1f955 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 . sqlite3_io_me
1f956 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 thods *pMethod;
1f957 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6c 61 /* Parent cla
1f958 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49 52 53 ss. MUST BE FIRS
1f959 54 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b T */. FileChunk
1f95a 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20 20 *pFirst;
1f95b 20 20 20 20 20 20 20 2f 2a 20 48 65 61 64 20 6f /* Head o
1f95c 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 6e f in-memory chun
1f95d 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 46 69 6c 65 k-list */. File
1f95e 50 6f 69 6e 74 20 65 6e 64 70 6f 69 6e 74 3b 20 Point endpoint;
1f95f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1f960 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e ointer to the en
1f961 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f d of the file */
1f962 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 72 65 61 . FilePoint rea
1f963 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 dpoint;
1f964 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f /* Pointer to
1f965 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
1f966 6c 61 73 74 20 78 52 65 61 64 28 29 20 2a 2f 0a last xRead() */.
1f967 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 };../*.** Read d
1f968 61 74 61 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d ata from the in-
1f969 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 memory journal f
1f96a 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 ile. This is th
1f96b 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e e implementation
1f96c 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 .** of the sqlit
1f96d 65 33 5f 76 66 73 2e 78 52 65 61 64 20 6d 65 74 e3_vfs.xRead met
1f96e 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 hod..*/.static i
1f96f 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 0a nt memjrnlRead(.
1f970 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a sqlite3_file *
1f971 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 pJfd, /* The
1f972 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f journal file fro
1f973 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20 m which to read
1f974 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c */. void *zBuf,
1f975 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 /* P
1f976 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20 68 ut the results h
1f977 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d ere */. int iAm
1f978 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
1f979 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1f97a 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 es to read */.
1f97b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 sqlite_int64 iOf
1f97c 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 st /* Begin
1f97d 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 reading at this
1f97e 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d offset */.){. M
1f97f 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 emJournal *p = (
1f980 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 MemJournal *)pJf
1f981 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 d;. u8 *zOut =
1f982 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 65 61 zBuf;. int nRea
1f983 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e 74 20 d = iAmt;. int
1f984 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 iChunkOffset;.
1f985 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e FileChunk *pChun
1f986 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c 69 74 65 20 k;.. /* SQLite
1f987 6e 65 76 65 72 20 74 72 69 65 73 20 74 6f 20 72 never tries to r
1f988 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e 64 ead past the end
1f989 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a of a rollback j
1f98a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 ournal file */.
1f98b 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 2b 69 assert( iOfst+i
1f98c 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 Amt<=p->endpoint
1f98d 2e 69 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 .iOffset );.. i
1f98e 66 28 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e f( p->readpoint.
1f98f 69 4f 66 66 73 65 74 21 3d 69 4f 66 73 74 20 7c iOffset!=iOfst |
1f990 7c 20 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 | iOfst==0 ){.
1f991 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 sqlite3_int64
1f992 69 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f iOff = 0;. fo
1f993 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 r(pChunk=p->pFir
1f994 73 74 3b 20 0a 20 20 20 20 20 20 20 20 41 4c 57 st; . ALW
1f995 41 59 53 28 70 43 68 75 6e 6b 29 20 26 26 20 28 AYS(pChunk) && (
1f996 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 iOff+JOURNAL_CHU
1f997 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a NKSIZE)<=iOfst;.
1f998 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 pChunk=p
1f999 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 Chunk->pNext.
1f99a 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20 2b ){. iOff +
1f99b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 = JOURNAL_CHUNKS
1f99c 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c IZE;. }. }el
1f99d 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d se{. pChunk =
1f99e 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 p->readpoint.pC
1f99f 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 hunk;. }.. iCh
1f9a0 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 unkOffset = (int
1f9a1 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f )(iOfst%JOURNAL_
1f9a2 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f CHUNKSIZE);. do
1f9a3 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 63 {. int iSpac
1f9a4 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e e = JOURNAL_CHUN
1f9a5 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 KSIZE - iChunkOf
1f9a6 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 fset;. int nC
1f9a7 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c opy = MIN(nRead,
1f9a8 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 (JOURNAL_CHUNKS
1f9a9 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 IZE - iChunkOffs
1f9aa 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 et));. memcpy
1f9ab 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e (zOut, &pChunk->
1f9ac 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 zChunk[iChunkOff
1f9ad 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 set], nCopy);.
1f9ae 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b zOut += nCopy;
1f9af 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 . nRead -= iS
1f9b0 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b pace;. iChunk
1f9b1 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 Offset = 0;. }
1f9b2 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 while( nRead>=0
1f9b3 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e && (pChunk=pChun
1f9b4 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26 20 k->pNext)!=0 &&
1f9b5 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d 3e nRead>0 );. p->
1f9b6 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 readpoint.iOffse
1f9b7 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0a t = iOfst+iAmt;.
1f9b8 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 p->readpoint.p
1f9b9 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a Chunk = pChunk;.
1f9ba 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
1f9bb 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 _OK;.}../*.** Wr
1f9bc 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 ite data to the
1f9bd 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 file..*/.static
1f9be 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 int memjrnlWrite
1f9bf 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 (. sqlite3_file
1f9c0 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 *pJfd, /* Th
1f9c1 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 e journal file i
1f9c2 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 nto which to wri
1f9c3 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f te */. const vo
1f9c4 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f id *zBuf, /
1f9c5 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20 62 * Take data to b
1f9c6 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 e written from h
1f9c7 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d ere */. int iAm
1f9c8 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 t,
1f9c9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 /* Number of byt
1f9ca 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 es to write */.
1f9cb 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f sqlite_int64 iO
1f9cc 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e fst /* Begin
1f9cd 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69 73 writing at this
1f9ce 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 offset into the
1f9cf 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 file */.){. Me
1f9d0 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d mJournal *p = (M
1f9d1 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 emJournal *)pJfd
1f9d2 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d ;. int nWrite =
1f9d3 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 72 iAmt;. u8 *zWr
1f9d4 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75 66 ite = (u8 *)zBuf
1f9d5 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 ;.. /* An in-me
1f9d6 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c mory journal fil
1f9d7 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 e should only ev
1f9d8 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 er be appended t
1f9d9 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 o. Random. ** a
1f9da 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72 65 ccess writes are
1f9db 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62 79 not required by
1f9dc 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 sqlite.. */.
1f9dd 61 73 73 65 72 74 28 20 69 4f 66 73 74 3d 3d 70 assert( iOfst==p
1f9de 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 ->endpoint.iOffs
1f9df 65 74 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 et );. UNUSED_P
1f9e0 41 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29 3b ARAMETER(iOfst);
1f9e1 0a 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69 74 .. while( nWrit
1f9e2 65 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 e>0 ){. FileC
1f9e3 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 hunk *pChunk = p
1f9e4 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e ->endpoint.pChun
1f9e5 6b 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75 6e k;. int iChun
1f9e6 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 kOffset = (int)(
1f9e7 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 p->endpoint.iOff
1f9e8 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e set%JOURNAL_CHUN
1f9e9 4b 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 KSIZE);. int
1f9ea 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 iSpace = MIN(nWr
1f9eb 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 ite, JOURNAL_CHU
1f9ec 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f NKSIZE - iChunkO
1f9ed 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 ffset);.. if(
1f9ee 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 iChunkOffset==0
1f9ef 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 ){. /* New
1f9f0 20 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69 72 chunk is requir
1f9f1 65 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68 65 ed to extend the
1f9f2 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 file. */.
1f9f3 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 FileChunk *pNew
1f9f4 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 = sqlite3_malloc
1f9f5 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e (sizeof(FileChun
1f9f6 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 k));. if( !
1f9f7 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 pNew ){.
1f9f8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f return SQLITE_IO
1f9f9 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 ERR_NOMEM;.
1f9fa 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 }. pNew->p
1f9fb 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 Next = 0;.
1f9fc 69 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 if( pChunk ){.
1f9fd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d assert( p-
1f9fe 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 >pFirst );.
1f9ff 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 pChunk->pNext
1fa00 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d = pNew;. }
1fa01 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 else{. as
1fa02 73 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73 74 sert( !p->pFirst
1fa03 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 );. p->p
1fa04 46 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 First = pNew;.
1fa05 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 }. p->e
1fa06 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d ndpoint.pChunk =
1fa07 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 pNew;. }..
1fa08 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 memcpy(&p->end
1fa09 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 point.pChunk->zC
1fa0a 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 hunk[iChunkOffse
1fa0b 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70 61 t], zWrite, iSpa
1fa0c 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 ce);. zWrite
1fa0d 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20 6e += iSpace;. n
1fa0e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65 3b Write -= iSpace;
1fa0f 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 . p->endpoint
1fa10 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70 61 .iOffset += iSpa
1fa11 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 ce;. }.. retur
1fa12 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a n SQLITE_OK;.}..
1fa13 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 /*.** Truncate t
1fa14 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 he file..*/.stat
1fa15 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 ic int memjrnlTr
1fa16 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 uncate(sqlite3_f
1fa17 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 ile *pJfd, sqlit
1fa18 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 e_int64 size){.
1fa19 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d MemJournal *p =
1fa1a 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 (MemJournal *)p
1fa1b 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b Jfd;. FileChunk
1fa1c 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 *pChunk;. asse
1fa1d 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 rt(size==0);. U
1fa1e 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 NUSED_PARAMETER(
1fa1f 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 size);. pChunk
1fa20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 = p->pFirst;. w
1fa21 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a hile( pChunk ){.
1fa22 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 FileChunk *p
1fa23 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 Tmp = pChunk;.
1fa24 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e pChunk = pChun
1fa25 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 k->pNext;. sq
1fa26 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70 29 lite3_free(pTmp)
1fa27 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d ;. }. sqlite3M
1fa28 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a emJournalOpen(pJ
1fa29 66 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 fd);. return SQ
1fa2a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a LITE_OK;.}../*.*
1fa2b 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 * Close the file
1fa2c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1fa2d 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c memjrnlClose(sql
1fa2e 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 ite3_file *pJfd)
1fa2f 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 {. memjrnlTrunc
1fa30 61 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 ate(pJfd, 0);.
1fa31 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
1fa32 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 ;.}.../*.** Sync
1fa33 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a the file..**.**
1fa34 20 53 79 6e 63 69 6e 67 20 61 6e 20 69 6e 2d 6d Syncing an in-m
1fa35 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69 73 emory journal is
1fa36 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 a no-op. And,
1fa37 69 6e 20 66 61 63 74 2c 20 74 68 69 73 20 72 6f in fact, this ro
1fa38 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 65 utine.** is neve
1fa39 72 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 77 6f r called in a wo
1fa3a 72 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 rking implementa
1fa3b 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c tion. This impl
1fa3c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 ementation.** ex
1fa3d 69 73 74 73 20 70 75 72 65 6c 79 20 61 73 20 61 ists purely as a
1fa3e 20 63 6f 6e 74 69 6e 67 65 6e 63 79 2c 20 69 6e contingency, in
1fa3f 20 63 61 73 65 20 73 6f 6d 65 20 6d 61 6c 66 75 case some malfu
1fa40 6e 63 74 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f nction in some o
1fa41 74 68 65 72 0a 2a 2a 20 70 61 72 74 20 6f 66 20 ther.** part of
1fa42 53 51 4c 69 74 65 20 63 61 75 73 65 73 20 53 79 SQLite causes Sy
1fa43 6e 63 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 nc to be called
1fa44 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 by mistake..*/.s
1fa45 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e tatic int memjrn
1fa46 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 lSync(sqlite3_fi
1fa47 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 le *NotUsed, int
1fa48 20 4e 6f 74 55 73 65 64 32 29 7b 20 20 20 2f 2a NotUsed2){ /*
1fa49 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 55 4e 55 53 NO_TEST*/. UNUS
1fa4a 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f ED_PARAMETER2(No
1fa4b 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 tUsed, NotUsed2)
1fa4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
1fa4d 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 /*NO_TE
1fa4e 53 54 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 ST*/. assert( 0
1fa4f 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 );
1fa50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa52 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a /*NO_TEST*/.
1fa53 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f return SQLITE_
1fa54 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 OK;
1fa55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fa57 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 20 *NO_TEST*/.}
1fa58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fa5b 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 /*NO_T
1fa5c 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 EST*/../*.** Que
1fa5d 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 ry the size of t
1fa5e 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 he file in bytes
1fa5f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1fa60 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 memjrnlFileSize(
1fa61 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a sqlite3_file *pJ
1fa62 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 fd, sqlite_int64
1fa63 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a *pSize){. MemJ
1fa64 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d ournal *p = (Mem
1fa65 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a Journal *)pJfd;.
1fa66 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 *pSize = (sqli
1fa67 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 te_int64) p->end
1fa68 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 point.iOffset;.
1fa69 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
1fa6a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c K;.}../*.** Tabl
1fa6b 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 e of methods for
1fa6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 MemJournal sqli
1fa6d 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e te3_file object.
1fa6e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
1fa6f 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 t sqlite3_io_met
1fa70 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d hods MemJournalM
1fa71 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 ethods = {. 1,
1fa72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1fa73 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 * iVersion */.
1fa74 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 memjrnlClose,
1fa75 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 /* xClose */.
1fa76 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 memjrnlRead,
1fa77 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 /* xRead */.
1fa78 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 memjrnlWrite,
1fa79 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a /* xWrite */.
1fa7a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 memjrnlTruncat
1fa7b 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 e, /* xTruncate
1fa7c 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e */. memjrnlSyn
1fa7d 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 c, /* xSync
1fa7e 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c */. memjrnlFil
1fa7f 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 eSize, /* xFile
1fa80 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Size */. 0,
1fa81 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1fa82 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 Lock */. 0,
1fa83 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 /* x
1fa84 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 Unlock */. 0,
1fa85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1fa86 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c xCheckReservedL
1fa87 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 ock */. 0,
1fa88 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 /* xF
1fa89 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 ileControl */.
1fa8a 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0,
1fa8b 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 /* xSectorSize
1fa8c 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20 20 */. 0
1fa8d 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 /* xDevi
1fa8e 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 ceCharacteristic
1fa8f 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 s */.};../* .**
1fa90 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 Open a journal f
1fa91 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ile..*/.SQLITE_P
1fa92 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 RIVATE void sqli
1fa93 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 te3MemJournalOpe
1fa94 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a n(sqlite3_file *
1fa95 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 pJfd){. MemJour
1fa96 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 nal *p = (MemJou
1fa97 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 61 rnal *)pJfd;. a
1fa98 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 ssert( EIGHT_BYT
1fa99 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 E_ALIGNMENT(p) )
1fa9a 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c ;. memset(p, 0,
1fa9b 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e sqlite3MemJourn
1fa9c 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e alSize());. p->
1fa9d 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f pMethod = &MemJo
1fa9e 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a urnalMethods;.}.
1fa9f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 ./*.** Return tr
1faa0 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d 68 ue if the file-h
1faa1 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73 20 andle passed as
1faa2 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 0a an argument is .
1faa3 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 ** an in-memory
1faa4 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 journal .*/.SQLI
1faa5 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1faa6 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e qlite3IsMemJourn
1faa7 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 al(sqlite3_file
1faa8 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72 6e *pJfd){. return
1faa9 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d pJfd->pMethods=
1faaa 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 =&MemJournalMeth
1faab 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 ods;.}../* .** R
1faac 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 eturn the number
1faad 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 of bytes requir
1faae 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 ed to store a Me
1faaf 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75 73 mJournal that us
1fab0 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 es vfs.** pVfs t
1fab1 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64 o create the und
1fab2 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 erlying on-disk
1fab3 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 files..*/.SQLITE
1fab4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1fab5 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 ite3MemJournalSi
1fab6 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 ze(void){. retu
1fab7 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 rn sizeof(MemJou
1fab8 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a rnal);.}../*****
1fab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 ********* End of
1faba 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a memjournal.c **
1fabb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fabd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a ********/./*****
1fabe 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 ********* Begin
1fabf 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a file walker.c **
1fac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fac2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 ********/./*.**
1fac3 32 30 30 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2008 August 16.*
1fac4 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 *.** The author
1fac5 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 disclaims copyri
1fac6 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 ght to this sour
1fac7 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 ce code. In pla
1fac8 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c ce of.** a legal
1fac9 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 notice, here is
1faca 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a a blessing:.**.
1facb 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f ** May you do
1facc 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 good and not ev
1facd 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f il..** May yo
1face 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 u find forgivene
1facf 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 ss for yourself
1fad0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 and forgive othe
1fad1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f rs..** May yo
1fad2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 u share freely,
1fad3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 never taking mor
1fad4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e e than you give.
1fad5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
1fad6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fad7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fad8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a *************.**
1fada 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 This file conta
1fadb 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 ins routines use
1fadc 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 d for walking th
1fadd 65 20 70 61 72 73 65 72 20 74 72 65 65 20 66 6f e parser tree fo
1fade 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 r.** an SQL stat
1fadf 65 6d 65 6e 74 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a ement..*/.../*.*
1fae0 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 * Walk an expres
1fae1 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76 6f sion tree. Invo
1fae2 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 ke the callback
1fae3 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e 6f once for each no
1fae4 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 de.** of the exp
1fae5 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20 64 ression, while d
1fae6 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20 6f ecending. (In o
1fae7 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 ther words, the
1fae8 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20 69 callback.** is i
1fae9 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76 69 nvoked before vi
1faea 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e 2e siting children.
1faeb 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 ).**.** The retu
1faec 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 rn value from th
1faed 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c e callback shoul
1faee 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 d be one of the
1faef 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61 6e WRC_*.** constan
1faf0 74 73 20 74 6f 20 73 70 65 63 69 66 79 20 68 6f ts to specify ho
1faf1 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69 74 w to proceed wit
1faf2 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a h the walk..**.*
1faf3 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e 75 * WRC_Continu
1faf4 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65 20 e Continue
1faf5 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e 20 descending down
1faf6 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 the tree..**.**
1faf7 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 WRC_Prune
1faf8 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73 63 Do not desc
1faf9 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20 6e end into child n
1fafa 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f 77 odes. But allow
1fafb 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
1fafc 20 20 20 20 20 20 20 20 20 74 68 65 20 77 61 6c the wal
1fafd 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 k to continue wi
1fafe 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65 73 th sibling nodes
1faff 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 41 ..**.** WRC_A
1fb00 62 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f 20 bort Do
1fb01 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63 6b no more callback
1fb02 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20 73 s. Unwind the s
1fb03 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 tack and.**
1fb04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fb05 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70 2d return the top-
1fb06 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c 2e level walk call.
1fb07 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 .**.** The retur
1fb08 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 69 n value from thi
1fb09 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52 43 s routine is WRC
1fb0a 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64 6f _Abort to abando
1fb0b 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b 0a n the tree walk.
1fb0c 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74 69 ** and WRC_Conti
1fb0d 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 2e nue to continue.
1fb0e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
1fb0f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 TE int sqlite3Wa
1fb10 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 lkExpr(Walker *p
1fb11 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 Walker, Expr *pE
1fb12 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a xpr){. int rc;.
1fb13 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 if( pExpr==0 )
1fb14 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 return WRC_Cont
1fb15 69 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73 65 inue;. testcase
1fb16 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
1fb17 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 y(pExpr, EP_Toke
1fb18 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73 74 nOnly) );. test
1fb19 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f case( ExprHasPro
1fb1a 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
1fb1b 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72 63 Reduced) );. rc
1fb1c 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 = pWalker->xExp
1fb1d 72 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 rCallback(pWalke
1fb1e 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 r, pExpr);. if(
1fb1f 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 rc==WRC_Continu
1fb20 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e.
1fb21 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 && !ExprHasAnyPr
1fb22 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f operty(pExpr,EP_
1fb23 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 TokenOnly) ){.
1fb24 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1fb25 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 kExpr(pWalker, p
1fb26 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20 72 Expr->pLeft) ) r
1fb27 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b eturn WRC_Abort;
1fb28 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
1fb29 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 WalkExpr(pWalker
1fb2a 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 , pExpr->pRight)
1fb2b 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fb2c 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78 70 ort;. if( Exp
1fb2d 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 rHasProperty(pEx
1fb2e 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 pr, EP_xIsSelect
1fb2f 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 ) ){. if( s
1fb30 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1fb31 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d (pWalker, pExpr-
1fb32 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72 65 >x.pSelect) ) re
1fb33 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1fb34 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
1fb35 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b if( sqlite3Walk
1fb36 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 ExprList(pWalker
1fb37 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 , pExpr->x.pList
1fb38 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 ) ) return WRC_A
1fb39 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a bort;. }. }.
1fb3a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57 52 return rc & WR
1fb3b 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a 2a C_Abort;.}../*.*
1fb3c 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 * Call sqlite3Wa
1fb3d 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 lkExpr() for eve
1fb3e 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e ry expression in
1fb3f 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69 6c list p or until
1fb40 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65 71 .** an abort req
1fb41 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a 2f uest is seen..*/
1fb42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
1fb43 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 int sqlite3WalkE
1fb44 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20 2a xprList(Walker *
1fb45 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69 73 pWalker, ExprLis
1fb46 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a t *p){. int i;.
1fb47 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
1fb48 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
1fb49 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66 6f if( p ){. fo
1fb4a 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 r(i=p->nExpr, pI
1fb4b 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 tem=p->a; i>0; i
1fb4c 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 --, pItem++){.
1fb4d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1fb4e 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c alkExpr(pWalker,
1fb4f 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 pItem->pExpr) )
1fb50 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1fb51 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 t;. }. }. r
1fb52 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
1fb53 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c ue;.}../*.** Wal
1fb54 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e k all expression
1fb55 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 s associated wit
1fb56 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 h SELECT stateme
1fb57 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 nt p. Do.** not
1fb58 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c 45 invoke the SELE
1fb59 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 CT callback on p
1fb5a 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f 75 , but do (of cou
1fb5b 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20 61 rse) invoke.** a
1fb5c 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63 6b ny expr callback
1fb5d 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61 6c s and SELECT cal
1fb5e 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d 65 lbacks that come
1fb5f 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65 73 from subqueries
1fb60 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f ..** Return WRC_
1fb61 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f 6e Abort or WRC_Con
1fb62 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 tinue..*/.SQLITE
1fb63 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
1fb64 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 ite3WalkSelectEx
1fb65 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b pr(Walker *pWalk
1fb66 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a er, Select *p){.
1fb67 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1fb68 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 kExprList(pWalke
1fb69 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29 20 r, p->pEList) )
1fb6a 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
1fb6b 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 ;. if( sqlite3W
1fb6c 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c alkExpr(pWalker,
1fb6d 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72 65 p->pWhere) ) re
1fb6e 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
1fb6f 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1fb70 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 kExprList(pWalke
1fb71 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 20 r, p->pGroupBy)
1fb72 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fb73 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 rt;. if( sqlite
1fb74 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 3WalkExpr(pWalke
1fb75 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20 29 r, p->pHaving) )
1fb76 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
1fb77 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 t;. if( sqlite3
1fb78 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 WalkExprList(pWa
1fb79 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72 42 lker, p->pOrderB
1fb7a 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f y) ) return WRC_
1fb7b 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c Abort;. if( sql
1fb7c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 ite3WalkExpr(pWa
1fb7d 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 lker, p->pLimit)
1fb7e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
1fb7f 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 ort;. if( sqlit
1fb80 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b e3WalkExpr(pWalk
1fb81 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 20 er, p->pOffset)
1fb82 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f ) return WRC_Abo
1fb83 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 rt;. return WRC
1fb84 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a _Continue;.}../*
1fb85 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61 72 .** Walk the par
1fb86 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 69 61 se trees associa
1fb87 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75 62 ted with all sub
1fb88 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a 2a queries in the.*
1fb89 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 * FROM clause of
1fb8a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1fb8b 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e 76 t p. Do not inv
1fb8c 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a 2a oke the select.*
1fb8d 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70 2c * callback on p,
1fb8e 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20 69 but do invoke i
1fb8f 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20 63 t on each FROM c
1fb90 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 0a 2a lause subquery.*
1fb91 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75 62 * and on any sub
1fb92 71 75 65 72 69 65 73 20 66 75 72 74 68 65 72 20 queries further
1fb93 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65 65 down in the tree
1fb94 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57 52 . Return .** WR
1fb95 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 C_Abort or WRC_C
1fb96 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c 49 ontinue;.*/.SQLI
1fb97 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1fb98 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1fb99 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57 61 From(Walker *pWa
1fb9a 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 lker, Select *p)
1fb9b 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 {. SrcList *pSr
1fb9c 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 c;. int i;. st
1fb9d 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
1fb9e 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53 72 m *pItem;.. pSr
1fb9f 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 c = p->pSrc;. i
1fba0 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 20 f( ALWAYS(pSrc)
1fba1 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 72 ){. for(i=pSr
1fba2 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d 70 c->nSrc, pItem=p
1fba3 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d Src->a; i>0; i--
1fba4 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
1fba5 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1fba6 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c kSelect(pWalker,
1fba7 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 29 pItem->pSelect)
1fba8 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 ){. retu
1fba9 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1fbaa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1fbab 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e return WRC_Con
1fbac 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a 2a tinue;.} ../*.**
1fbad 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61 6c Call sqlite3Wal
1fbae 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65 72 kExpr() for ever
1fbaf 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 y expression in
1fbb0 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 Select statement
1fbb1 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73 71 p..** Invoke sq
1fbb2 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 lite3WalkSelect(
1fbb3 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73 ) for subqueries
1fbb4 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
1fbb5 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74 68 use and.** on th
1fbb6 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 e compound selec
1fbb7 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72 69 t chain, p->pPri
1fbb8 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e or..**.** Return
1fbb9 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75 6e WRC_Continue un
1fbba 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64 69 der normal condi
1fbbb 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20 57 tions. Return W
1fbbc 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20 74 RC_Abort if.** t
1fbbd 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72 74 here is an abort
1fbbe 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 request..**.**
1fbbf 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64 6f If the Walker do
1fbc0 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 78 es not have an x
1fbc1 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 29 SelectCallback()
1fbc2 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 then this routi
1fbc3 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 ne.** is a no-op
1fbc4 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f 43 returning WRC_C
1fbc5 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 ontinue..*/.SQLI
1fbc6 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
1fbc7 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 qlite3WalkSelect
1fbc8 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 (Walker *pWalker
1fbc9 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 , Select *p){.
1fbca 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 3d int rc;. if( p=
1fbcb 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e 78 =0 || pWalker->x
1fbcc 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d 3d SelectCallback==
1fbcd 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 0 ) return WRC_C
1fbce 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d 20 ontinue;. rc =
1fbcf 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 WRC_Continue;.
1fbd0 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20 20 while( p ){.
1fbd1 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 rc = pWalker->x
1fbd2 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28 70 SelectCallback(p
1fbd3 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20 20 Walker, p);.
1fbd4 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a if( rc ) break;.
1fbd5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 if( sqlite3W
1fbd6 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70 57 alkSelectExpr(pW
1fbd7 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 alker, p) ) retu
1fbd8 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
1fbd9 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c if( sqlite3Wal
1fbda 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61 6c kSelectFrom(pWal
1fbdb 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72 6e ker, p) ) return
1fbdc 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
1fbdd 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 p = p->pPrior;.
1fbde 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 }. return rc &
1fbdf 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f WRC_Abort;.}../
1fbe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 ************** E
1fbe1 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20 2a nd of walker.c *
1fbe2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbe3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1fbe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 ************** B
1fbe6 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c 76 egin file resolv
1fbe7 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a e.c ************
1fbe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f *************/./
1fbea 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73 74 *.** 2008 August
1fbeb 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 18.**.** The au
1fbec 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 thor disclaims c
1fbed 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 opyright to this
1fbee 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 source code. I
1fbef 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 n place of.** a
1fbf0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 legal notice, he
1fbf1 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 re is a blessing
1fbf2 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 :.**.** May y
1fbf3 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e ou do good and n
1fbf4 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d ot evil..** M
1fbf5 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 ay you find forg
1fbf6 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 iveness for your
1fbf7 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 self and forgive
1fbf8 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d others..** M
1fbf9 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 ay you share fre
1fbfa 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e ely, never takin
1fbfb 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 g more than you
1fbfc 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a give..**.*******
1fbfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fbff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
1fc01 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 **.**.** This fi
1fc02 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 le contains rout
1fc03 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77 61 ines used for wa
1fc04 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65 72 lking the parser
1fc05 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65 73 tree and.** res
1fc06 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69 66 olve all identif
1fc07 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61 74 iers by associat
1fc08 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61 20 ing them with a
1fc09 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74 61 particular.** ta
1fc0a 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a ble and column..
1fc0b 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 */../*.** Turn t
1fc0c 68 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 he pExpr express
1fc0d 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 ion into an alia
1fc0e 73 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 s for the iCol-t
1fc0f 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a h column of the.
1fc10 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e ** result set in
1fc11 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 pEList..**.** I
1fc12 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 f the result set
1fc13 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d column is a sim
1fc14 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 ple column refer
1fc15 65 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 ence, then this
1fc16 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 routine.** makes
1fc17 20 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 an exact copy.
1fc18 20 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 But for any oth
1fc19 65 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 er kind of expre
1fc1a 73 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 ssion, this.** r
1fc1b 6f 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f outine make a co
1fc1c 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 py of the result
1fc1d 20 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 set column as t
1fc1e 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 he argument to t
1fc1f 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 he.** TK_AS oper
1fc20 61 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 ator. The TK_AS
1fc21 20 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 operator causes
1fc22 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
1fc23 74 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 to be.** evaluat
1fc24 65 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 ed just once and
1fc25 20 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 then reused for
1fc26 20 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a each alias..**.
1fc27 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f ** The reason fo
1fc28 72 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 r suppressing th
1fc29 65 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 e TK_AS term whe
1fc2a 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e n the expression
1fc2b 20 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 is a simple.**
1fc2c 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 column reference
1fc2d 20 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 is so that the
1fc2e 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 column reference
1fc2f 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 will be recogni
1fc30 7a 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 zed as.** usable
1fc31 20 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 by indices with
1fc32 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 in the WHERE cla
1fc33 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c use processing l
1fc34 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 ogic. .**.** Hac
1fc35 6b 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 k: The TK_AS op
1fc36 65 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 erator is inhibi
1fc37 74 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d ted if zType[0]=
1fc38 3d 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e ='G'. This mean
1fc39 73 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 s.** that in a G
1fc3a 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 ROUP BY clause,
1fc3b 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 the expression i
1fc3c 73 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 s evaluated twic
1fc3d 65 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a e. Hence:.**.**
1fc3e 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 SELECT rand
1fc3f 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 om()%5 AS x, cou
1fc40 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 nt(*) FROM tab G
1fc41 52 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 ROUP BY x.**.**
1fc42 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f Is equivalent to
1fc43 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 :.**.** SELE
1fc44 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 CT random()%5 AS
1fc45 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f x, count(*) FRO
1fc46 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 M tab GROUP BY r
1fc47 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 andom()%5.**.**
1fc48 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 The result of ra
1fc49 6e 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 ndom()%5 in the
1fc4a 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 GROUP BY clause
1fc4b 69 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 is probably diff
1fc4c 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 erent.** from th
1fc4d 65 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 e result in the
1fc4e 72 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 result-set. We
1fc4f 6d 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 might fix this s
1fc50 6f 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 omeday. Or.** t
1fc51 68 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 hen again, we mi
1fc52 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 ght not....*/.st
1fc53 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 atic void resolv
1fc54 65 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 eAlias(. Parse
1fc55 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
1fc56 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
1fc57 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ext */. ExprLis
1fc58 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 t *pEList,
1fc59 2f 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 /* A result set
1fc5a 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 */. int iCol,
1fc5b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
1fc5c 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 column in the r
1fc5d 65 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 esult set. 0..p
1fc5e 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a EList->nExpr-1 *
1fc5f 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c /. Expr *pExpr,
1fc60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1fc61 61 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 ansform this int
1fc62 6f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 o an alias to th
1fc63 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a e result set */.
1fc64 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
1fc65 79 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f ype /* "GRO
1fc66 55 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f UP" or "ORDER" o
1fc67 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 r "" */.){. Exp
1fc68 72 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 r *pOrig;
1fc69 20 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d /* The iCol-
1fc6a 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 th column of the
1fc6b 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 result set */.
1fc6c 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 Expr *pDup;
1fc6d 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 /* Copy
1fc6e 6f 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 of pOrig */. sq
1fc6f 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 lite3 *db;
1fc70 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1fc71 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1fc72 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 */.. assert( iC
1fc73 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 ol>=0 && iCol<pE
1fc74 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 List->nExpr );.
1fc75 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d pOrig = pEList-
1fc76 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a >a[iCol].pExpr;.
1fc77 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 assert( pOrig!
1fc78 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 =0 );. assert(
1fc79 70 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 pOrig->flags & E
1fc7a 50 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 P_Resolved );.
1fc7b 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
1fc7c 0a 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 . if( pOrig->op
1fc7d 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a !=TK_COLUMN && z
1fc7e 54 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a Type[0]!='G' ){.
1fc7f 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 pDup = sqlit
1fc80 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f e3ExprDup(db, pO
1fc81 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 rig, 0);. pDu
1fc82 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 p = sqlite3PExpr
1fc83 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 (pParse, TK_AS,
1fc84 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 pDup, 0, 0);.
1fc85 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 if( pDup==0 ) r
1fc86 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 eturn;. if( p
1fc87 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 EList->a[iCol].i
1fc88 41 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 Alias==0 ){.
1fc89 20 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c pEList->a[iCol
1fc8a 5d 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 ].iAlias = (u16)
1fc8b 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 (++pParse->nAlia
1fc8c 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 s);. }. pD
1fc8d 75 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c up->iTable = pEL
1fc8e 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c ist->a[iCol].iAl
1fc8f 69 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ias;. }else if(
1fc90 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
1fc91 28 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 (pOrig, EP_IntVa
1fc92 6c 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 lue) || pOrig->u
1fc93 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 .zToken==0 ){.
1fc94 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 pDup = sqlite3
1fc95 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 ExprDup(db, pOri
1fc96 67 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 g, 0);. if( p
1fc97 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Dup==0 ) return;
1fc98 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 . }else{. ch
1fc99 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 ar *zToken = pOr
1fc9a 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 ig->u.zToken;.
1fc9b 20 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e assert( zToken
1fc9c 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 !=0 );. pOrig
1fc9d 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a ->u.zToken = 0;.
1fc9e 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 pDup = sqlit
1fc9f 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f e3ExprDup(db, pO
1fca0 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 rig, 0);. pOr
1fca1 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a ig->u.zToken = z
1fca2 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 Token;. if( p
1fca3 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Dup==0 ) return;
1fca4 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44 . assert( (pD
1fca5 75 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f up->flags & (EP_
1fca6 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
1fca7 4f 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 Only))==0 );.
1fca8 20 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d pDup->flags2 |=
1fca9 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b EP2_MallocedTok
1fcaa 65 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e en;. pDup->u.
1fcab 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 zToken = sqlite3
1fcac 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f DbStrDup(db, zTo
1fcad 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 ken);. }. if(
1fcae 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 pExpr->flags & E
1fcaf 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a P_ExpCollate ){.
1fcb0 20 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 pDup->pColl
1fcb1 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a = pExpr->pColl;.
1fcb2 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 pDup->flags
1fcb3 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 |= EP_ExpCollate
1fcb4 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 ;. }. sqlite3E
1fcb5 78 70 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 xprClear(db, pEx
1fcb6 70 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 pr);. memcpy(pE
1fcb7 78 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f xpr, pDup, sizeo
1fcb8 66 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 f(*pExpr));. sq
1fcb9 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
1fcba 70 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 pDup);.}../*.**
1fcbb 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f Given the name o
1fcbc 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 f a column of th
1fcbd 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 e form X.Y.Z or
1fcbe 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c Y.Z or just Z, l
1fcbf 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e ook up.** that n
1fcc0 61 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f ame in the set o
1fcc1 66 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 f source tables
1fcc2 69 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 in pSrcList and
1fcc3 6d 61 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a make the pExpr .
1fcc4 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f ** expression no
1fcc5 64 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f de refer back to
1fcc6 20 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c that source col
1fcc7 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 umn. The follow
1fcc8 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 ing changes.** a
1fcc9 72 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 re made to pExpr
1fcca 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 :.**.** pExpr
1fccb 2d 3e 69 44 62 20 20 20 20 20 20 20 20 20 20 20 ->iDb
1fccc 53 65 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e Set the index in
1fccd 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 db->aDb[] of th
1fcce 65 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 e database X.**
1fccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcd0 20 20 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 (even if
1fcd1 20 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a X is implied)..
1fcd2 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 ** pExpr->iTa
1fcd3 62 6c 65 20 20 20 20 20 20 20 20 53 65 74 20 74 ble Set t
1fcd4 6f 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d o the cursor num
1fcd5 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c ber for the tabl
1fcd6 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 e obtained.**
1fcd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcd8 20 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c from pSrcL
1fcd9 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 ist..** pExpr
1fcda 2d 3e 70 54 61 62 20 20 20 20 20 20 20 20 20 20 ->pTab
1fcdb 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 Points to the Ta
1fcdc 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 ble structure of
1fcdd 20 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a X.Y (even if.**
1fcde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fcdf 20 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f X and/o
1fce0 72 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e r Y are implied.
1fce1 29 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 ).** pExpr->i
1fce2 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 Column Set
1fce3 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e to the column n
1fce4 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 umber within the
1fce5 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 table..** pE
1fce6 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 xpr->op
1fce7 20 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c Set to TK_COL
1fce8 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 UMN..** pExpr
1fce9 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 ->pLeft
1fcea 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 Any expression t
1fceb 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 his points to is
1fcec 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 deleted.** p
1fced 45 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 Expr->pRight
1fcee 20 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 Any expressi
1fcef 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 on this points t
1fcf0 6f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a o is deleted..**
1fcf1 0a 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 .** The zDb vari
1fcf2 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 able is the name
1fcf3 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 of the database
1fcf4 20 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 (the "X"). Thi
1fcf5 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a s value may be.*
1fcf6 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 * NULL meaning t
1fcf7 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 hat name is of t
1fcf8 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a he form Y.Z or Z
1fcf9 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 . Any available
1fcfa 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e database.** can
1fcfb 20 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a be used. The z
1fcfc 54 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 Table variable i
1fcfd 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 s the name of th
1fcfe 65 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 e table (the "Y"
1fcff 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 ). This.** valu
1fd00 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 e can be NULL if
1fd01 20 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c zDb is also NUL
1fd02 4c 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 L. If zTable is
1fd03 20 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e NULL it.** mean
1fd04 73 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 s that the form
1fd05 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a of the name is Z
1fd06 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e and that column
1fd07 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 s from any table
1fd08 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e .** can be used.
1fd09 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 .**.** If the na
1fd0a 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 me cannot be res
1fd0b 6f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 olved unambiguou
1fd0c 73 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 sly, leave an er
1fd0d 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 ror message.** i
1fd0e 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 n pParse and ret
1fd0f 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 urn WRC_Abort.
1fd10 52 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 Return WRC_Prune
1fd11 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a on success..*/.
1fd12 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 static int looku
1fd13 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a pName(. Parse *
1fd14 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a pParse, /*
1fd15 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e The parsing con
1fd16 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 text */. const
1fd17 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f char *zDb, /
1fd18 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 * Name of the da
1fd19 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e tabase containin
1fd1a 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c g table, or NULL
1fd1b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 */. const char
1fd1c 20 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 *zTab, /* Na
1fd1d 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 me of table cont
1fd1e 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f aining column, o
1fd1f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 r NULL */. cons
1fd20 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 t char *zCol,
1fd21 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 /* Name of the
1fd22 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d column. */. Nam
1fd23 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 eContext *pNC,
1fd24 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f /* The name co
1fd25 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 ntext used to re
1fd26 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a solve the name *
1fd27 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 /. Expr *pExpr
1fd28 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 /* Make
1fd29 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 this EXPR node
1fd2a 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c point to the sel
1fd2b 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a ected column */.
1fd2c 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 ){. int i, j;
1fd2d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
1fd2e 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 p counters */.
1fd2f 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 int cnt = 0;
1fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fd31 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d /* Number of m
1fd32 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e atching column n
1fd33 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e ames */. int cn
1fd34 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 tTab = 0;
1fd35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1fd36 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e umber of matchin
1fd37 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f g table names */
1fd38 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
1fd39 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 pParse->db;
1fd3a 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 /* The data
1fd3b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
1fd3c 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c */. struct SrcL
1fd3d 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
1fd3e 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f /* Use fo
1fd3f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 r looping over p
1fd40 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f SrcList items */
1fd41 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 . struct SrcLis
1fd42 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d t_item *pMatch =
1fd43 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 0; /* The matc
1fd44 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 hing pSrcList it
1fd45 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 em */. NameCont
1fd46 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e ext *pTopNC = pN
1fd47 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 C; /* Fir
1fd48 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 st namecontext i
1fd49 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 n the list */.
1fd4a 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 Schema *pSchema
1fd4b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
1fd4c 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 /* Schema of t
1fd4d 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f he expression */
1fd4e 0a 20 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 . int isTrigger
1fd4f 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 = 0;.. assert(
1fd50 20 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 pNC ); /* t
1fd51 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 he name context
1fd52 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 cannot be NULL.
1fd53 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f */. assert( zCo
1fd54 6c 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a l ); /* The Z
1fd55 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 in X.Y.Z cannot
1fd56 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 be NULL */. as
1fd57 73 65 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e sert( ~ExprHasAn
1fd58 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
1fd59 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 EP_TokenOnly|EP
1fd5a 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 _Reduced) );..
1fd5b 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 /* Initialize th
1fd5c 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 e node to no-mat
1fd5d 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 ch */. pExpr->i
1fd5e 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 Table = -1;. pE
1fd5f 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 xpr->pTab = 0;.
1fd60 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 ExprSetIrreduci
1fd61 62 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f ble(pExpr);.. /
1fd62 2a 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 * Start at the i
1fd63 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 nner-most contex
1fd64 74 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 t and move outwa
1fd65 72 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 rd until a match
1fd66 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 is found */. w
1fd67 68 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 hile( pNC && cnt
1fd68 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c ==0 ){. ExprL
1fd69 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 ist *pEList;.
1fd6a 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 SrcList *pSrcLi
1fd6b 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 st = pNC->pSrcLi
1fd6c 73 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 st;.. if( pSr
1fd6d 63 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 cList ){. f
1fd6e 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 or(i=0, pItem=pS
1fd6f 72 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 rcList->a; i<pSr
1fd70 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b cList->nSrc; i++
1fd71 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 , pItem++){.
1fd72 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b Table *pTab;
1fd73 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 . int iDb
1fd74 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e ;. Column
1fd75 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 *pCol;. .
1fd76 20 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d pTab = pItem-
1fd77 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 >pTab;. a
1fd78 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 ssert( pTab!=0 &
1fd79 26 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 & pTab->zName!=0
1fd7a 20 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 );. iDb
1fd7b 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
1fd7c 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d oIndex(db, pTab-
1fd7d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 >pSchema);.
1fd7e 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d assert( pTab-
1fd7f 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 >nCol>0 );.
1fd80 20 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 if( zTab ){.
1fd81 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 if( pIt
1fd82 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 em->zAlias ){.
1fd83 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a char *
1fd84 7a 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d zTabName = pItem
1fd85 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 ->zAlias;.
1fd86 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1fd87 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 3StrICmp(zTabNam
1fd88 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f e, zTab)!=0 ) co
1fd89 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
1fd8a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
1fd8b 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e char *zTabN
1fd8c 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d ame = pTab->zNam
1fd8d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 e;. i
1fd8e 66 28 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d f( NEVER(zTabNam
1fd8f 65 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 e==0) || sqlite3
1fd90 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 StrICmp(zTabName
1fd91 2c 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 , zTab)!=0 ){.
1fd92 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 cont
1fd93 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 inue;.
1fd94 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
1fd95 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 if( zDb!=0 && sq
1fd96 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d lite3StrICmp(db-
1fd97 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c >aDb[iDb].zName,
1fd98 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 zDb)!=0 ){.
1fd99 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e contin
1fd9a 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
1fd9b 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
1fd9c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fd9d 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b if( 0==(cntTab++
1fd9e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 ) ){. p
1fd9f 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 Expr->iTable = p
1fda0 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 Item->iCursor;.
1fda1 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1fda2 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 pTab = pTab;.
1fda3 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d pSchema =
1fda4 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a pTab->pSchema;.
1fda5 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 pMatch
1fda6 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 = pItem;.
1fda7 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 }. for(
1fda8 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e j=0, pCol=pTab->
1fda9 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 aCol; j<pTab->nC
1fdaa 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 ol; j++, pCol++)
1fdab 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 {. if(
1fdac 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
1fdad 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c Col->zName, zCol
1fdae 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1fdaf 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 IdList *pUsi
1fdb0 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ng;.
1fdb1 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 cnt++;.
1fdb2 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 pExpr->iTable
1fdb3 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f = pItem->iCurso
1fdb4 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 r;. p
1fdb5 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 Expr->pTab = pTa
1fdb6 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 b;. p
1fdb7 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 Match = pItem;.
1fdb8 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 pSche
1fdb9 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 ma = pTab->pSche
1fdba 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ma;.
1fdbb 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 /* Substitute th
1fdbc 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 e rowid (column
1fdbd 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 -1) for the INTE
1fdbe 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 GER PRIMARY KEY
1fdbf 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 */. p
1fdc0 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 Expr->iColumn =
1fdc1 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f j==pTab->iPKey ?
1fdc2 20 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 -1 : (i16)j;.
1fdc3 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3c if( i<
1fdc4 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 pSrcList->nSrc-1
1fdc5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1fdc6 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a if( pItem[1].j
1fdc7 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 ointype & JT_NAT
1fdc8 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 URAL ){.
1fdc9 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 /* If th
1fdca 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 is match occurre
1fdcb 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 d in the left ta
1fdcc 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c ble of a natural
1fdcd 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 join,.
1fdce 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 ** then s
1fdcf 6b 69 70 20 74 68 65 20 72 69 67 68 74 20 74 61 kip the right ta
1fdd0 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 ble to avoid a d
1fdd1 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a uplicate match *
1fdd2 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 /.
1fdd3 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 pItem++;.
1fdd4 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a i++;.
1fdd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 }e
1fdd6 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 lse if( (pUsing
1fdd7 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e = pItem[1].pUsin
1fdd8 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 g)!=0 ){.
1fdd9 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 /* If t
1fdda 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 his match occurs
1fddb 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 on a column tha
1fddc 74 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e t is in the USIN
1fddd 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 G clause.
1fdde 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 ** of a
1fddf 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 join, skip the
1fde0 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 search of the ri
1fde1 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 ght table of the
1fde2 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 join.
1fde3 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 ** to avoi
1fde4 64 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 d a duplicate ma
1fde5 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 tch there. */.
1fde6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e in
1fde7 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 t k;.
1fde8 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c for(k=0; k<
1fde9 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b pUsing->nId; k++
1fdea 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1fdeb 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
1fdec 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e StrICmp(pUsing->
1fded 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c a[k].zName, zCol
1fdee 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
1fdef 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 pIte
1fdf0 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 m++;.
1fdf1 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 i++;.
1fdf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1fdf3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
1fdf4 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1fdf5 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
1fdf6 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 }.
1fdf7 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
1fdf8 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
1fdf9 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fdfa 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1fdfb 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c }..#ifndef SQL
1fdfc 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 ITE_OMIT_TRIGGER
1fdfd 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 . /* If we ha
1fdfe 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 ve not already r
1fdff 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 esolved the name
1fe00 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 , then maybe .
1fe01 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 ** it is a new
1fe02 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 .* or old.* trig
1fe03 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 ger argument ref
1fe04 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 erence. */.
1fe05 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 if( zDb==0 &&
1fe06 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d zTab!=0 && cnt==
1fe07 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 0 && pParse->pTr
1fe08 69 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 iggerTab!=0 ){.
1fe09 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 int op = pP
1fe0a 61 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 arse->eTriggerOp
1fe0b 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 ;. Table *p
1fe0c 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 Tab = 0;. a
1fe0d 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 ssert( op==TK_DE
1fe0e 4c 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 LETE || op==TK_U
1fe0f 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f PDATE || op==TK_
1fe10 49 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 INSERT );.
1fe11 69 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 if( op!=TK_DELET
1fe12 45 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 E && sqlite3StrI
1fe13 43 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 Cmp("new",zTab)
1fe14 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 == 0 ){.
1fe15 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 pExpr->iTable =
1fe16 31 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 1;. pTab
1fe17 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 = pParse->pTrigg
1fe18 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c erTab;. }el
1fe19 73 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e se if( op!=TK_IN
1fe1a 53 45 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 SERT && sqlite3S
1fe1b 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 trICmp("old",zTa
1fe1c 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 b)==0 ){.
1fe1d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d pExpr->iTable =
1fe1e 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 0;. pTab
1fe1f 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 = pParse->pTrig
1fe20 67 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a gerTab;. }.
1fe21 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 . if( pTab
1fe22 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){ . int
1fe23 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 iCol;. pS
1fe24 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 chema = pTab->pS
1fe25 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 chema;. c
1fe26 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 ntTab++;.
1fe27 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f if( sqlite3IsRo
1fe28 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 wid(zCol) ){.
1fe29 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 iCol = -1
1fe2a 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
1fe2b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 . for(i
1fe2c 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 Col=0; iCol<pTab
1fe2d 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b ->nCol; iCol++){
1fe2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c . Col
1fe2f 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 umn *pCol = &pTa
1fe30 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 b->aCol[iCol];.
1fe31 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 if( s
1fe32 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 qlite3StrICmp(pC
1fe33 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 ol->zName, zCol)
1fe34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
1fe35 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 if( iCol==p
1fe36 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 Tab->iPKey ){.
1fe37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 iC
1fe38 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 ol = -1;.
1fe39 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
1fe3a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
1fe3b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
1fe3c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
1fe3d 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 }. if( iC
1fe3e 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b ol<pTab->nCol ){
1fe3f 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b . cnt++
1fe40 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
1fe41 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 iCol<0 ){.
1fe42 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 pExpr->aff
1fe43 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 inity = SQLITE_A
1fe44 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 FF_INTEGER;.
1fe45 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 }else if(
1fe46 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 pExpr->iTable==0
1fe47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
1fe48 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d testcase( iCol==
1fe49 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 31 );.
1fe4a 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c testcase( iCol
1fe4b 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 ==32 );.
1fe4c 20 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d pParse->oldm
1fe4d 61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 ask |= (iCol>=32
1fe4e 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 ? 0xffffffff :
1fe4f 28 28 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 (((u32)1)<<iCol)
1fe50 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 );. }.
1fe51 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e pExpr->
1fe52 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 iColumn = (i16)i
1fe53 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 Col;. p
1fe54 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 Expr->pTab = pTa
1fe55 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 b;. isT
1fe56 72 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 rigger = 1;.
1fe57 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
1fe58 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 }.#endif /* !d
1fe59 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d efined(SQLITE_OM
1fe5a 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a IT_TRIGGER) */..
1fe5b 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 /*. ** Pe
1fe5c 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 rhaps the name i
1fe5d 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f s a reference to
1fe5e 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a the ROWID. *
1fe5f 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 /. if( cnt==0
1fe60 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 && cntTab==1 &&
1fe61 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 sqlite3IsRowid(
1fe62 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 zCol) ){. c
1fe63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 nt = 1;. pE
1fe64 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d xpr->iColumn = -
1fe65 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 1;. pExpr->
1fe66 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 affinity = SQLIT
1fe67 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 E_AFF_INTEGER;.
1fe68 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 }.. /*.
1fe69 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 ** If the input
1fe6a 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 is of the form
1fe6b 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e Z (not Y.Z or X.
1fe6c 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 Y.Z) then the na
1fe6d 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 me Z. ** migh
1fe6e 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 t refer to an re
1fe6f 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 sult-set alias.
1fe70 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 This happens, f
1fe71 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e or example, when
1fe72 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 . ** we are r
1fe73 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 esolving names i
1fe74 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 n the WHERE clau
1fe75 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 se of the follow
1fe76 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 ing command:.
1fe77 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 **. ** S
1fe78 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 ELECT a+b AS x F
1fe79 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 ROM table WHERE
1fe7a 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 x<10;. **.
1fe7b 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b ** In cases lik
1fe7c 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 e this, replace
1fe7d 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 pExpr with a cop
1fe7e 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 y of the express
1fe7f 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 ion that. **
1fe80 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 forms the result
1fe81 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 set entry ("a+b
1fe82 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 " in the example
1fe83 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d ) and return imm
1fe84 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a ediately.. **
1fe85 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 Note that the e
1fe86 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 xpression in the
1fe87 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 result set shou
1fe88 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 ld have already
1fe89 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f been. ** reso
1fe8a 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 lved by the time
1fe8b 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1fe8c 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 e is resolved..
1fe8d 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e */. if( cn
1fe8e 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 t==0 && (pEList
1fe8f 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d = pNC->pEList)!=
1fe90 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 0 && zTab==0 ){.
1fe91 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a for(j=0; j
1fe92 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 <pEList->nExpr;
1fe93 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 j++){. ch
1fe94 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 ar *zAs = pEList
1fe95 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 ->a[j].zName;.
1fe96 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 if( zAs!=0
1fe97 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 && sqlite3StrIC
1fe98 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 mp(zAs, zCol)==0
1fe99 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 ){. Ex
1fe9a 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 pr *pOrig;.
1fe9b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
1fe9c 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 pr->pLeft==0 &&
1fe9d 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 pExpr->pRight==0
1fe9e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 );. as
1fe9f 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 sert( pExpr->x.p
1fea0 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 List==0 );.
1fea1 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
1fea2 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 pr->x.pSelect==0
1fea3 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f );. pO
1fea4 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b rig = pEList->a[
1fea5 6a 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 j].pExpr;.
1fea6 20 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c if( !pNC->al
1fea7 6c 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 lowAgg && ExprHa
1fea8 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c sProperty(pOrig,
1fea9 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 EP_Agg) ){.
1feaa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
1feab 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1feac 22 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 "misuse of alias
1fead 65 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 ed aggregate %s"
1feae 2c 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 , zAs);.
1feaf 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
1feb0 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 bort;.
1feb1 7d 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f }. reso
1feb2 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c lveAlias(pParse,
1feb3 20 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 pEList, j, pExp
1feb4 72 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 r, "");.
1feb5 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 cnt = 1;.
1feb6 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b pMatch = 0;
1feb7 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
1feb8 74 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 t( zTab==0 && zD
1feb9 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 b==0 );.
1feba 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d goto lookupnam
1febb 65 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d e_end;. }
1febc 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a . } . }.
1febd 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 . /* Advance
1febe 74 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 to the next name
1febf 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c context. The l
1fec0 6f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 oop will exit wh
1fec1 65 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a en either. **
1fec2 20 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 we have a match
1fec3 20 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e (cnt>0) or when
1fec4 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e we run out of n
1fec5 61 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 ame contexts..
1fec6 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 */. if( cnt
1fec7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 ==0 ){. pNC
1fec8 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 = pNC->pNext;.
1fec9 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 }. }.. /*.
1feca 20 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 ** If X and Y a
1fecb 72 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 re NULL (in othe
1fecc 72 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 r words if only
1fecd 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 the column name
1fece 5a 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 Z is. ** suppli
1fecf 65 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 ed) and the valu
1fed0 65 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 e of Z is enclos
1fed1 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f ed in double-quo
1fed2 74 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a tes, then. ** Z
1fed3 20 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 is a string lit
1fed4 65 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e eral if it doesn
1fed5 27 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 't match any col
1fed6 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 umn names. In t
1fed7 68 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 hat. ** case, w
1fed8 65 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e e need to return
1fed9 20 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 right away and
1feda 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 not make any cha
1fedb 6e 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 nges to. ** pEx
1fedc 70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 pr.. **. ** Be
1fedd 63 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e cause no referen
1fede 63 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f ce was made to o
1fedf 75 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 uter contexts, t
1fee0 68 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a he pNC->nRef. *
1fee1 2a 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 * fields are not
1fee2 20 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 changed in any
1fee3 63 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 context.. */.
1fee4 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 if( cnt==0 && zT
1fee5 61 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 ab==0 && ExprHas
1fee6 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 Property(pExpr,E
1fee7 50 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a P_DblQuoted) ){.
1fee8 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 pExpr->op =
1fee9 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 TK_STRING;. p
1feea 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a Expr->pTab = 0;.
1feeb 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
1feec 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a rune;. }.. /*.
1feed 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e ** cnt==0 mean
1feee 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 s there was not
1feef 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 match. cnt>1 me
1fef0 61 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74 ans there were t
1fef1 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 wo or. ** more
1fef2 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 matches. Either
1fef3 20 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e way, we have an
1fef4 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 error.. */. i
1fef5 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 f( cnt!=1 ){.
1fef6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 const char *zEr
1fef7 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e r;. zErr = cn
1fef8 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 t==0 ? "no such
1fef9 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 column" : "ambig
1fefa 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 uous column name
1fefb 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 ";. if( zDb )
1fefc 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
1fefd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
1fefe 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 "%s: %s.%s.%s",
1feff 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c zErr, zDb, zTab,
1ff00 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 zCol);. }els
1ff01 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 e if( zTab ){.
1ff02 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
1ff03 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a Msg(pParse, "%s:
1ff04 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a %s.%s", zErr, z
1ff05 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 Tab, zCol);.
1ff06 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c }else{. sql
1ff07 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1ff08 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a rse, "%s: %s", z
1ff09 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 Err, zCol);.
1ff0a 7d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 }. pTopNC->nE
1ff0b 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 rr++;. }.. /*
1ff0c 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d If a column from
1ff0d 20 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 a table in pSrc
1ff0e 4c 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 List is referenc
1ff0f 65 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a ed, then record.
1ff10 20 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 ** this fact i
1ff11 6e 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 n the pSrcList.a
1ff12 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 [].colUsed bitma
1ff13 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 sk. Column 0 ca
1ff14 75 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 uses. ** bit 0
1ff15 74 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 to be set. Colu
1ff16 6d 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e mn 1 sets bit 1.
1ff17 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 And so forth.
1ff18 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c If the. ** col
1ff19 75 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 umn number is gr
1ff1a 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e eater than the n
1ff1b 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e umber of bits in
1ff1c 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a the bitmask. *
1ff1d 2a 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 * then set the h
1ff1e 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 igh-order bit of
1ff1f 20 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 the bitmask..
1ff20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e */. if( pExpr->
1ff21 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d iColumn>=0 && pM
1ff22 61 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 atch!=0 ){. i
1ff23 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 nt n = pExpr->iC
1ff24 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 olumn;. testc
1ff25 61 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b ase( n==BMS-1 );
1ff26 0a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 . if( n>=BMS
1ff27 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 ){. n = BMS
1ff28 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 -1;. }. as
1ff29 73 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 sert( pMatch->iC
1ff2a 75 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 ursor==pExpr->iT
1ff2b 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 able );. pMat
1ff2c 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 ch->colUsed |= (
1ff2d 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a (Bitmask)1)<<n;.
1ff2e 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 }.. /* Clean
1ff2f 75 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 up and return.
1ff30 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 */. sqlite3Expr
1ff31 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 Delete(db, pExpr
1ff32 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 ->pLeft);. pExp
1ff33 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 r->pLeft = 0;.
1ff34 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
1ff35 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 e(db, pExpr->pRi
1ff36 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 ght);. pExpr->p
1ff37 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 Right = 0;. pEx
1ff38 70 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 pr->op = (isTrig
1ff39 67 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 ger ? TK_TRIGGER
1ff3a 20 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c : TK_COLUMN);.l
1ff3b 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 ookupname_end:.
1ff3c 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 if( cnt==1 ){.
1ff3d 20 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d assert( pNC!=
1ff3e 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 0 );. sqlite3
1ff3f 41 75 74 68 52 65 61 64 28 70 50 61 72 73 65 2c AuthRead(pParse,
1ff40 20 70 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c pExpr, pSchema,
1ff41 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b pNC->pSrcList);
1ff42 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e . /* Incremen
1ff43 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 t the nRef value
1ff44 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e on all name con
1ff45 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 texts from TopNC
1ff46 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 up to. ** th
1ff47 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 e point where th
1ff48 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 e name matched.
1ff49 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a */. for(;;){.
1ff4a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 assert( pT
1ff4b 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 opNC!=0 );.
1ff4c 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b pTopNC->nRef++;
1ff4d 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e . if( pTopN
1ff4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a C==pNC ) break;.
1ff4f 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 pTopNC = p
1ff50 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 TopNC->pNext;.
1ff51 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 }. return W
1ff52 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c RC_Prune;. } el
1ff53 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 se {. return
1ff54 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d WRC_Abort;. }.}
1ff55 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 ../*.** Allocate
1ff56 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f and return a po
1ff57 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 inter to an expr
1ff58 65 73 73 69 6f 6e 20 74 6f 20 6c 6f 61 64 20 74 ession to load t
1ff59 68 65 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 0a 2a he column iCol.*
1ff5a 2a 20 66 72 6f 6d 20 64 61 74 61 73 6f 75 72 63 * from datasourc
1ff5b 65 20 69 53 72 63 20 64 61 74 61 73 6f 75 72 63 e iSrc datasourc
1ff5c 65 20 69 6e 20 53 72 63 4c 69 73 74 20 70 53 72 e in SrcList pSr
1ff5d 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 c..*/.SQLITE_PRI
1ff5e 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 VATE Expr *sqlit
1ff5f 65 33 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 e3CreateColumnEx
1ff60 70 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 pr(sqlite3 *db,
1ff61 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 69 SrcList *pSrc, i
1ff62 6e 74 20 69 53 72 63 2c 20 69 6e 74 20 69 43 6f nt iSrc, int iCo
1ff63 6c 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 l){. Expr *p =
1ff64 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 sqlite3ExprAlloc
1ff65 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 (db, TK_COLUMN,
1ff66 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 29 0, 0);. if( p )
1ff67 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 {. struct Src
1ff68 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
1ff69 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 53 72 63 = &pSrc->a[iSrc
1ff6a 5d 3b 0a 20 20 20 20 70 2d 3e 70 54 61 62 20 3d ];. p->pTab =
1ff6b 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 pItem->pTab;.
1ff6c 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 p->iTable = pI
1ff6d 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 tem->iCursor;.
1ff6e 20 20 69 66 28 20 70 2d 3e 70 54 61 62 2d 3e 69 if( p->pTab->i
1ff6f 50 4b 65 79 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 PKey==iCol ){.
1ff70 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d p->iColumn =
1ff71 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a -1;. }else{.
1ff72 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e p->iColumn
1ff73 20 3d 20 28 79 6e 56 61 72 29 69 43 6f 6c 3b 0a = (ynVar)iCol;.
1ff74 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 63 6f 6c pItem->col
1ff75 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d 61 73 Used |= ((Bitmas
1ff76 6b 29 31 29 3c 3c 28 69 43 6f 6c 3e 3d 42 4d 53 k)1)<<(iCol>=BMS
1ff77 20 3f 20 42 4d 53 2d 31 20 3a 20 69 43 6f 6c 29 ? BMS-1 : iCol)
1ff78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 45 78 70 72 ;. }. Expr
1ff79 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 SetProperty(p, E
1ff7a 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 20 20 7d P_Resolved);. }
1ff7b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a . return p;.}..
1ff7c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 /*.** This routi
1ff7d 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 ne is callback f
1ff7e 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 or sqlite3WalkEx
1ff7f 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f pr()..**.** Reso
1ff80 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d lve symbolic nam
1ff81 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d es into TK_COLUM
1ff82 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72 20 N operators for
1ff83 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e the current.** n
1ff84 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72 65 ode in the expre
1ff85 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 ssion tree. Ret
1ff86 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 urn 0 to continu
1ff87 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f 77 e the search dow
1ff88 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f 72 n.** the tree or
1ff89 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 2 to abort the
1ff8a 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a tree walk..**.**
1ff8b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c This routine al
1ff8c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63 68 so does error ch
1ff8d 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 ecking and name
1ff8e 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a resolution for.*
1ff8f 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 * function names
1ff90 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 . The operator
1ff91 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75 for aggregate fu
1ff92 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 nctions is chang
1ff93 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f ed.** to TK_AGG_
1ff94 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 FUNCTION..*/.sta
1ff95 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 tic int resolveE
1ff96 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20 2a xprStep(Walker *
1ff97 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 pWalker, Expr *p
1ff98 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e Expr){. NameCon
1ff99 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 text *pNC;. Par
1ff9a 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 se *pParse;.. p
1ff9b 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e NC = pWalker->u.
1ff9c 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 pNC;. assert( p
1ff9d 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 NC!=0 );. pPars
1ff9e 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b e = pNC->pParse;
1ff9f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 . assert( pPars
1ffa0 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 e==pWalker->pPar
1ffa1 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 se );.. if( Exp
1ffa2 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 rHasAnyProperty(
1ffa3 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 pExpr, EP_Resolv
1ffa4 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 ed) ) return WRC
1ffa5 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 _Prune;. ExprSe
1ffa6 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c tProperty(pExpr,
1ffa7 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 EP_Resolved);.#
1ffa8 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 ifndef NDEBUG.
1ffa9 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 if( pNC->pSrcLis
1ffaa 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 t && pNC->pSrcLi
1ffab 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a st->nAlloc>0 ){.
1ffac 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 SrcList *pSr
1ffad 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 cList = pNC->pSr
1ffae 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 cList;. int i
1ffaf 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 ;. for(i=0; i
1ffb0 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e <pNC->pSrcList->
1ffb1 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nSrc; i++){.
1ffb2 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69 assert( pSrcLi
1ffb3 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 st->a[i].iCursor
1ffb4 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74 2d >=0 && pSrcList-
1ffb5 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 >a[i].iCursor<pP
1ffb6 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 arse->nTab);.
1ffb7 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 }. }.#endif.
1ffb8 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f switch( pExpr->o
1ffb9 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 p ){..#if define
1ffba 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f d(SQLITE_ENABLE_
1ffbb 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 UPDATE_DELETE_LI
1ffbc 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 MIT) && !defined
1ffbd 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 (SQLITE_OMIT_SUB
1ffbe 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 QUERY). /* Th
1ffbf 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 e special operat
1ffc0 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 or TK_ROW means
1ffc1 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66 6f use the rowid fo
1ffc2 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 r the first.
1ffc3 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 ** column in the
1ffc4 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 FROM clause. T
1ffc5 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 his is used by t
1ffc6 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 he LIMIT and ORD
1ffc7 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 ER BY. ** cla
1ffc8 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f use processing o
1ffc9 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45 4c n UPDATE and DEL
1ffca 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a ETE statements..
1ffcb 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 */. case
1ffcc 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 TK_ROW: {.
1ffcd 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 SrcList *pSrcLis
1ffce 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 t = pNC->pSrcLis
1ffcf 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 t;. struct
1ffd0 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 SrcList_item *pI
1ffd1 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 tem;. asser
1ffd2 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20 70 t( pSrcList && p
1ffd3 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 SrcList->nSrc==1
1ffd4 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 20 );. pItem
1ffd5 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a = pSrcList->a; .
1ffd6 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 pExpr->op
1ffd7 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 = TK_COLUMN;.
1ffd8 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d pExpr->pTab =
1ffd9 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 pItem->pTab;.
1ffda 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c pExpr->iTabl
1ffdb 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 e = pItem->iCurs
1ffdc 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d or;. pExpr-
1ffdd 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 >iColumn = -1;.
1ffde 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 pExpr->affi
1ffdf 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 nity = SQLITE_AF
1ffe0 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 F_INTEGER;.
1ffe1 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
1ffe2 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 ndif /* defined(
1ffe3 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 SQLITE_ENABLE_UP
1ffe4 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 DATE_DELETE_LIMI
1ffe5 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 T) && !defined(S
1ffe6 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 QLITE_OMIT_SUBQU
1ffe7 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 ERY) */.. /*
1ffe8 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 A lone identifie
1ffe9 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 r is the name of
1ffea 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a a column.. *
1ffeb 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44 /. case TK_ID
1ffec 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
1ffed 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 lookupName(pPar
1ffee 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72 2d se, 0, 0, pExpr-
1ffef 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c 20 >u.zToken, pNC,
1fff0 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 pExpr);. }.
1fff1 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 . /* A table
1fff2 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 name and column
1fff3 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44 0a name: ID.ID.
1fff4 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74 61 ** Or a data
1fff5 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64 20 base, table and
1fff6 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 column: ID.ID.I
1fff7 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 D. */. cas
1fff8 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 e TK_DOT: {.
1fff9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 const char *zC
1fffa 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f 6e olumn;. con
1fffb 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 3b st char *zTable;
1fffc 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 . const cha
1fffd 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45 78 r *zDb;. Ex
1fffe 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 pr *pRight;..
1ffff 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 /* if( pSrcLi
20000 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a st==0 ) break; *
20001 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20 3d /. pRight =
20002 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a pExpr->pRight;.
20003 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 if( pRight
20004 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 ->op==TK_ID ){.
20005 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b 0a zDb = 0;.
20006 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20 3d zTable =
20007 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 pExpr->pLeft->u
20008 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 .zToken;.
20009 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68 zColumn = pRigh
2000a 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 t->u.zToken;.
2000b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2000c 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68 74 assert( pRight
2000d 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b 0a ->op==TK_DOT );.
2000e 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70 45 zDb = pE
2000f 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 xpr->pLeft->u.zT
20010 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a 54 oken;. zT
20011 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e 70 able = pRight->p
20012 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Left->u.zToken;.
20013 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20 zColumn
20014 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68 74 = pRight->pRight
20015 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 ->u.zToken;.
20016 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e }. return
20017 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61 72 lookupName(pPar
20018 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65 2c se, zDb, zTable,
20019 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 zColumn, pNC, p
2001a 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 Expr);. }..
2001b 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75 6e /* Resolve fun
2001c 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20 20 ction names.
2001d 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 */. case TK_C
2001e 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 ONST_FUNC:. c
2001f 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a ase TK_FUNCTION:
20020 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 {. ExprLis
20021 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72 t *pList = pExpr
20022 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f 2a ->x.pList; /*
20023 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c 69 The argument li
20024 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 st */. int
20025 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 n = pList ? pLis
20026 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20 20 t->nExpr : 0;
20027 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 /* Number of ar
20028 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 guments */.
20029 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 6e int no_such_fun
2002a 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 c = 0; /*
2002b 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 20 True if no such
2002c 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20 function exists
2002d 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 6f */. int wro
2002e 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b ng_num_args = 0;
2002f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
20030 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20 wrong number of
20031 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 arguments */.
20032 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20 int is_agg =
20033 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 0; /
20034 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e 20 * True if is an
20035 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
20036 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 on */. int
20037 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 auth;
20038 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 6f /* Autho
20039 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 20 rization to use
2003a 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
2003b 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20 int nId;
2003c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2003d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 /* Number of c
2003e 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 6e haracters in fun
2003f 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 ction name */.
20040 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
20041 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 zId;
20042 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 /* The function
20043 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 name. */. F
20044 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20 uncDef *pDef;
20045 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
20046 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
20047 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a the function */.
20048 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 u8 enc = E
20049 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 20 NC(pParse->db);
2004a 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 /* The databas
2004b 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a 20 e encoding */..
2004c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
2004d 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4e Expr->op==TK_CON
2004e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20 20 ST_FUNC );.
2004f 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 assert( !ExprHa
20050 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c sProperty(pExpr,
20051 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 EP_xIsSelect) )
20052 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70 45 ;. zId = pE
20053 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 xpr->u.zToken;.
20054 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 nId = sqlit
20055 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b e3Strlen30(zId);
20056 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 . pDef = sq
20057 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f lite3FindFunctio
20058 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 n(pParse->db, zI
20059 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 d, nId, n, enc,
2005a 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 0);. if( pD
2005b 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ef==0 ){.
2005c 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 pDef = sqlite3F
2005d 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 indFunction(pPar
2005e 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 se->db, zId, nId
2005f 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 , -1, enc, 0);.
20060 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d if( pDef=
20061 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
20062 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 no_such_func = 1
20063 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
20064 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e 67 . wrong
20065 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 _num_args = 1;.
20066 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
20067 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 73 else{. is
20068 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 _agg = pDef->xFu
20069 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 nc==0;. }.#
2006a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
2006b 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e IT_AUTHORIZATION
2006c 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 20 . if( pDef
2006d 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68 20 ){. auth
2006e 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 = sqlite3AuthChe
2006f 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 ck(pParse, SQLIT
20070 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 E_FUNCTION, 0, p
20071 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a Def->zName, 0);.
20072 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 68 if( auth
20073 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 !=SQLITE_OK ){.
20074 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74 if( aut
20075 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 h==SQLITE_DENY )
20076 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
20077 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20078 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f arse, "not autho
20079 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75 6e rized to use fun
2007a 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 ction: %s",.
2007b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2007c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2007d 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 pDef->zName);.
2007e 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e pNC->n
2007f 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 Err++;.
20080 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 }. pEx
20081 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c pr->op = TK_NULL
20082 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
20083 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 rn WRC_Prune;.
20084 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
20085 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 #endif. if(
20086 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43 2d is_agg && !pNC-
20087 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20 >allowAgg ){.
20088 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
20089 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 rMsg(pParse, "mi
2008a 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 suse of aggregat
2008b 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 e function %.*s(
2008c 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 )", nId,zId);.
2008d 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b pNC->nErr+
2008e 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67 +;. is_ag
2008f 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c g = 0;. }el
20090 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66 se if( no_such_f
20091 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 unc ){. s
20092 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
20093 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 Parse, "no such
20094 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c function: %.*s",
20095 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 nId, zId);.
20096 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b pNC->nErr++;
20097 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
20098 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 wrong_num_args
20099 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
2009a 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
2009b 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 e,"wrong number
2009c 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 of arguments to
2009d 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 function %.*s()"
2009e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 6e ,. n
2009f 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 Id, zId);.
200a0 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 pNC->nErr++;.
200a1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
200a2 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20 20 is_agg ){.
200a3 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 pExpr->op = T
200a4 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a K_AGG_FUNCTION;.
200a5 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61 73 pNC->has
200a6 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d Agg = 1;. }
200a7 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 . if( is_ag
200a8 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 g ) pNC->allowAg
200a9 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c g = 0;. sql
200aa 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 ite3WalkExprList
200ab 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29 (pWalker, pList)
200ac 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61 ;. if( is_a
200ad 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 gg ) pNC->allowA
200ae 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a gg = 1;. /*
200af 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 FIX ME: Comput
200b0 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 e pExpr->affinit
200b1 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 65 y based on the e
200b2 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a 20 xpected return.
200b3 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 ** type of
200b4 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 the function .
200b5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 */. ret
200b6 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 urn WRC_Prune;.
200b7 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
200b8 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
200b9 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 Y. case TK_SE
200ba 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20 54 LECT:. case T
200bb 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 74 63 K_EXISTS: testc
200bc 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d ase( pExpr->op==
200bd 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 6e TK_EXISTS );.#en
200be 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f dif. case TK_
200bf 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 IN: {. test
200c0 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
200c1 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20 =TK_IN );.
200c2 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
200c3 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
200c4 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 sSelect) ){.
200c5 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70 int nRef = p
200c6 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65 NC->nRef;.#ifnde
200c7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 f SQLITE_OMIT_CH
200c8 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ECK. if(
200c9 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a pNC->isCheck ){.
200ca 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
200cb 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
200cc 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72 6f ,"subqueries pro
200cd 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b hibited in CHECK
200ce 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a constraints");.
200cf 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
200d0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
200d1 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b WalkSelect(pWalk
200d2 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 er, pExpr->x.pSe
200d3 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 61 lect);. a
200d4 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66 ssert( pNC->nRef
200d5 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20 >=nRef );.
200d6 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d if( nRef!=pNC-
200d7 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20 >nRef ){.
200d8 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 ExprSetProper
200d9 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 ty(pExpr, EP_Var
200da 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 Select);.
200db 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
200dc 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 break;. }.#i
200dd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
200de 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65 T_CHECK. case
200df 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a TK_VARIABLE: {.
200e0 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 if( pNC->i
200e1 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 sCheck ){.
200e2 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
200e3 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65 g(pParse,"parame
200e4 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64 20 ters prohibited
200e5 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 in CHECK constra
200e6 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a ints");. }.
200e7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
200e8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 }.#endif. }.
200e9 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e return (pParse->
200ea 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e nErr || pParse->
200eb 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
200ec 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 ) ? WRC_Abort :
200ed 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a WRC_Continue;.}.
200ee 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 ./*.** pEList is
200ef 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 a list of expre
200f0 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65 ssions which are
200f1 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73 75 really the resu
200f2 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a 2a lt set of the.**
200f3 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d a SELECT statem
200f4 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74 65 ent. pE is a te
200f5 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 rm in an ORDER B
200f6 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c Y or GROUP BY cl
200f7 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f ause..** This ro
200f8 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20 utine checks to
200f9 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20 73 see if pE is a s
200fa 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 imple identifier
200fb 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e which correspon
200fc 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d ds.** to the AS-
200fd 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74 name of one of t
200fe 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 he terms of the
200ff 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e expression list.
20100 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 74 If it is,.** t
20101 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 his routine retu
20102 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 rn an integer be
20103 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68 tween 1 and N wh
20104 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d ere N is the num
20105 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 6e ber of.** elemen
20106 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 6f ts in pEList, co
20107 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 rresponding to t
20108 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 he matching entr
20109 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 0a y. If there is.
2010a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 20 ** no match, or
2010b 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20 73 if pE is not a s
2010c 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 imple identifier
2010d 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 , then this rout
2010e 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e ine.** return 0.
2010f 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 61 .**.** pEList ha
20110 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e s been resolved.
20111 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a 2f pE has not..*/
20112 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
20113 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 72 lveAsName(. Par
20114 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
20115 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
20116 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 xt for error mes
20117 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c sages */. ExprL
20118 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f 2a ist *pEList, /*
20119 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 List of express
2011a 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a ions to scan */.
2011b 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 Expr *pE
2011c 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 /* Expressi
2011d 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 on we are trying
2011e 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a to match */.){.
2011f 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 int i;
20120 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
20121 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 nter */.. UNUSE
20122 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61 72 D_PARAMETER(pPar
20123 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 2d 3e se);.. if( pE->
20124 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 op==TK_ID ){.
20125 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 45 char *zCol = pE
20126 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 ->u.zToken;.
20127 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 for(i=0; i<pELis
20128 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a t->nExpr; i++){.
20129 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20 char *zAs
2012a 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a = pEList->a[i].z
2012b 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20 Name;. if(
2012c 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65 zAs!=0 && sqlite
2012d 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43 3StrICmp(zAs, zC
2012e 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 ol)==0 ){.
2012f 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 return i+1;.
20130 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
20131 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
20132 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f 69 *.** pE is a poi
20133 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 nter to an expre
20134 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20 61 ssion which is a
20135 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 single term in
20136 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 the.** ORDER BY
20137 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 of a compound SE
20138 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72 65 LECT. The expre
20139 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 ssion has not be
2013a 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c en.** name resol
2013b 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 ved..**.** At th
2013c 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75 e point this rou
2013d 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 tine is called,
2013e 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 we already know
2013f 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 45 that the.** ORDE
20140 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f 74 R BY term is not
20141 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 65 an integer inde
20142 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c x into the resul
20143 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a 20 t set. That.**
20144 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64 20 case is handled
20145 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 by the calling r
20146 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 outine..**.** At
20147 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 70 tempt to match p
20148 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 E against result
20149 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 set columns in
2014a 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a the left-most.**
2014b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
2014c 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 t. Return the i
2014d 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61 ndex i of the ma
2014e 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a 2a tching column,.*
2014f 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69 * as an indicati
20150 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 on to the caller
20151 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 that it should
20152 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74 68 sort by the i-th
20153 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 20 column..** The
20154 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e left-most column
20155 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 is 1. In other
20156 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c 75 words, the valu
20157 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 e returned is th
20158 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 65 e.** same intege
20159 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 r value that wou
2015a 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 ld be used in th
2015b 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 e SQL statement
2015c 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 to indicate.** t
2015d 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a he column..**.**
2015e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
2015f 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e match, return 0.
20160 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20 61 Return -1 if a
20161 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a n error occurs..
20162 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 */.static int re
20163 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d solveOrderByTerm
20164 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 ToExprList(. Pa
20165 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
20166 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
20167 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 ext for error me
20168 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65 ssages */. Sele
20169 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f ct *pSelect, /
2016a 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 * The SELECT sta
2016b 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20 tement with the
2016c 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
2016d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 */. Expr *pE
2016e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 /* The s
2016f 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42 59 pecific ORDER BY
20170 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e term */.){. in
20171 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
20172 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 /* Loop counter
20173 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
20174 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20 pEList; /* The
20175 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 columns of the r
20176 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e esult set */. N
20177 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20 ameContext nc;
20178 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 /* Name contex
20179 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20 t for resolving
2017a 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 pE */.. assert(
2017b 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e sqlite3ExprIsIn
2017c 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 teger(pE, &i)==0
2017d 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 );. pEList = p
2017e 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a Select->pEList;.
2017f 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c . /* Resolve al
20180 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f l names in the O
20181 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78 70 RDER BY term exp
20182 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d ression. */. m
20183 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 69 emset(&nc, 0, si
20184 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e zeof(nc));. nc.
20185 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
20186 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d . nc.pSrcList =
20187 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a pSelect->pSrc;.
20188 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 45 nc.pEList = pE
20189 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f 77 List;. nc.allow
2018a 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e 45 Agg = 1;. nc.nE
2018b 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 rr = 0;. if( sq
2018c 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
2018d 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 29 Names(&nc, pE) )
2018e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 {. sqlite3Err
2018f 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b orClear(pParse);
20190 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
20191 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 }.. /* Try to
20192 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52 20 match the ORDER
20193 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67 BY expression ag
20194 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73 ainst an express
20195 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 ion. ** in the
20196 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65 74 result set. Ret
20197 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 69 urn an 1-based i
20198 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63 ndex of the matc
20199 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c 74 hing. ** result
2019a 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f -set entry.. */
2019b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 . for(i=0; i<pE
2019c 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b List->nExpr; i++
2019d 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
2019e 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 e3ExprCompare(pE
2019f 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 List->a[i].pExpr
201a0 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 , pE) ){. r
201a1 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 7d eturn i+1;. }
201a2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f . }.. /* If no
201a3 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 match, return 0
201a4 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b . */. return 0;
201a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
201a6 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f te an ORDER BY o
201a7 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20 r GROUP BY term
201a8 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 72 out-of-range err
201a9 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f or..*/.static vo
201aa 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 id resolveOutOfR
201ab 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61 72 angeError(. Par
201ac 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
201ad 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 /* The error
201ae 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 68 context into wh
201af 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 ich to write the
201b0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 error */. cons
201b1 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 t char *zType,
201b2 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 /* "ORDER" or
201b3 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 6e "GROUP" */. in
201b4 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 t i,
201b5 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 /* The inde
201b6 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20 74 x (1-based) of t
201b7 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 he term out of r
201b8 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 ange */. int mx
201b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
201ba 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72 6d /* Largest perm
201bb 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f 66 issible value of
201bc 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 i */.){. sqlit
201bd 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
201be 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20 42 e, . "%r %s B
201bf 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 Y term out of ra
201c0 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 nge - should be
201c1 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20 31 ". "between 1
201c2 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 79 and %d", i, zTy
201c3 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a pe, mx);.}../*.*
201c4 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52 * Analyze the OR
201c5 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e DER BY clause in
201c6 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 a compound SELE
201c7 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 CT statement.
201c8 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 74 Modify.** each t
201c9 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 erm of the ORDER
201ca 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61 20 BY clause is a
201cb 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 constant integer
201cc 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 6e between 1.** an
201cd 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 d N where N is t
201ce 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c he number of col
201cf 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 umns in the comp
201d0 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a ound SELECT..**.
201d1 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d ** ORDER BY term
201d2 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65 61 s that are alrea
201d3 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 dy an integer be
201d4 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 72 tween 1 and N ar
201d5 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 2e e.** unmodified.
201d6 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 ORDER BY terms
201d7 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67 65 that are intege
201d8 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20 72 rs outside the r
201d9 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 72 ange of.** 1 thr
201da 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65 20 ough N generate
201db 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 52 an error. ORDER
201dc 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 BY terms that a
201dd 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a re expressions.*
201de 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61 67 * are matched ag
201df 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 ainst result set
201e0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 expressions of
201e1 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 0a compound SELECT.
201e2 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 ** beginning wit
201e3 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 h the left-most
201e4 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b 69 SELECT and worki
201e5 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72 69 ng toward the ri
201e6 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 66 ght..** At the f
201e7 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65 20 irst match, the
201e8 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73 73 ORDER BY express
201e9 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 6d ion is transform
201ea 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 69 ed into.** the i
201eb 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 nteger column nu
201ec 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 mber..**.** Retu
201ed 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 rn the number of
201ee 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2f errors seen..*/
201ef 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f .static int reso
201f0 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 lveCompoundOrder
201f1 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 By(. Parse *pPa
201f2 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 rse, /* P
201f3 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 arsing context.
201f4 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 Leave error mes
201f5 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 sages here */.
201f6 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 Select *pSelect
201f7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c /* The SEL
201f8 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f ECT statement co
201f9 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44 ntaining the ORD
201fa 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e ER BY */.){. in
201fb 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 t i;. ExprList
201fc 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 70 *pOrderBy;. Exp
201fd 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 rList *pEList;.
201fe 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 sqlite3 *db;.
201ff 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 int moreToDo = 1
20200 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 ;.. pOrderBy =
20201 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42 pSelect->pOrderB
20202 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 y;. if( pOrderB
20203 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b y==0 ) return 0;
20204 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e . db = pParse->
20205 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d db;.#if SQLITE_M
20206 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 AX_COLUMN. if(
20207 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e pOrderBy->nExpr>
20208 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
20209 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 E_LIMIT_COLUMN]
2020a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
2020b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
2020c 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 too many terms i
2020d 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 n ORDER BY claus
2020e 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 e");. return
2020f 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 1;. }.#endif.
20210 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 for(i=0; i<pOrde
20211 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 rBy->nExpr; i++)
20212 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e {. pOrderBy->
20213 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a 20 a[i].done = 0;.
20214 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 4e }. pSelect->pN
20215 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 ext = 0;. while
20216 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f ( pSelect->pPrio
20217 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 74 r ){. pSelect
20218 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20 ->pPrior->pNext
20219 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 = pSelect;. p
2021a 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 Select = pSelect
2021b 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 ->pPrior;. }.
2021c 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26 while( pSelect &
2021d 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20 & moreToDo ){.
2021e 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
2021f 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 t_item *pItem;.
20220 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b moreToDo = 0;
20221 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70 53 . pEList = pS
20222 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 elect->pEList;.
20223 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 assert( pELis
20224 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 t!=0 );. for(
20225 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 i=0, pItem=pOrde
20226 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 rBy->a; i<pOrder
20227 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 By->nExpr; i++,
20228 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
20229 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 int iCol = -1;.
2022a 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a Expr *pE, *
2022b 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 pDup;. if(
2022c 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f pItem->done ) co
2022d 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 ntinue;. pE
2022e 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b = pItem->pExpr;
2022f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 . if( sqlit
20230 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 e3ExprIsInteger(
20231 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 pE, &iCol) ){.
20232 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 3d if( iCol<=
20233 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74 0 || iCol>pEList
20234 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 ->nExpr ){.
20235 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f resolveOutO
20236 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 fRangeError(pPar
20237 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b 31 se, "ORDER", i+1
20238 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 , pEList->nExpr)
20239 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 ;. retu
2023a 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a rn 1;. }.
2023b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
2023c 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f iCol = reso
2023d 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65 lveAsName(pParse
2023e 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 , pEList, pE);.
2023f 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d if( iCol=
20240 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
20241 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 pDup = sqlite3Ex
20242 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 30 29 prDup(db, pE, 0)
20243 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
20244 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 !db->mallocFaile
20245 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 d ){.
20246 20 61 73 73 65 72 74 28 70 44 75 70 29 3b 0a 20 assert(pDup);.
20247 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c 20 iCol
20248 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 = resolveOrderBy
20249 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 70 TermToExprList(p
2024a 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 Parse, pSelect,
2024b 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 pDup);.
2024c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c }. sql
2024d 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 ite3ExprDelete(d
2024e 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 b, pDup);.
2024f 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 }. }.
20250 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b 0a if( iCol>0 ){.
20251 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 CollSeq
20252 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 6f *pColl = pE->pCo
20253 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ll;. int
20254 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 67 flags = pE->flag
20255 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 s & EP_ExpCollat
20256 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e;. sqlit
20257 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c e3ExprDelete(db,
20258 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70 49 pE);. pI
20259 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 20 tem->pExpr = pE
2025a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 = sqlite3Expr(db
2025b 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 29 , TK_INTEGER, 0)
2025c 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 ;. if( pE
2025d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a ==0 ) return 1;.
2025e 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f 6c pE->pCol
2025f 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 l = pColl;.
20260 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d 20 pE->flags |=
20261 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66 6c EP_IntValue | fl
20262 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45 2d ags;. pE-
20263 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 6f 6c >u.iValue = iCol
20264 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d ;. pItem-
20265 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f >iCol = (u16)iCo
20266 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d l;. pItem
20267 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 ->done = 1;.
20268 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
20269 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20 moreToDo = 1;.
2026a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
2026b 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 pSelect = pSele
2026c 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 ct->pNext;. }.
2026d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 for(i=0; i<pOrd
2026e 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b erBy->nExpr; i++
2026f 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 ){. if( pOrde
20270 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d rBy->a[i].done==
20271 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 0 ){. sqlit
20272 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
20273 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59 20 e, "%r ORDER BY
20274 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61 term does not ma
20275 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20 20 tch any ".
20276 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e "column in
20277 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 22 the result set"
20278 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72 65 , i+1);. re
20279 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 turn 1;. }.
2027a 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a }. return 0;.}.
2027b 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65 ./*.** Check eve
2027c 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f ry term in the O
2027d 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 RDER BY or GROUP
2027e 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64 65 BY clause pOrde
2027f 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45 rBy of.** the SE
20280 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 LECT statement p
20281 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79 20 Select. If any
20282 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e 63 term is referenc
20283 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74 e to a.** result
20284 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 set expression
20285 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 (as determined b
20286 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 y the ExprList.a
20287 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20 .iCol field).**
20288 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 then convert tha
20289 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f t term into a co
2028a 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 py of the corres
2028b 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20 73 ponding result s
2028c 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a et.** column..**
2028d 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 .** If any error
2028e 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20 s are detected,
2028f 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 add an error mes
20290 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20 61 sage to pParse a
20291 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e nd.** return non
20292 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a -zero. Return z
20293 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72 73 ero if no errors
20294 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 are seen..*/.SQ
20295 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 LITE_PRIVATE int
20296 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f sqlite3ResolveO
20297 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 50 rderGroupBy(. P
20298 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
20299 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
2029a 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 context. Leave
2029b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 error messages h
2029c 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ere */. Select
2029d 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f *pSelect, /
2029e 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 * The SELECT sta
2029f 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e tement containin
202a0 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a g the clause */.
202a1 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 ExprList *pOrd
202a2 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f erBy, /* The O
202a3 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 RDER BY or GROUP
202a4 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62 65 BY clause to be
202a5 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 processed */.
202a6 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 const char *zTyp
202a7 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 e /* "ORDER"
202a8 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29 or "GROUP" */.)
202a9 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c {. int i;. sql
202aa 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
202ab 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69 73 e->db;. ExprLis
202ac 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 t *pEList;. str
202ad 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 uct ExprList_ite
202ae 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 m *pItem;.. if(
202af 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 pOrderBy==0 ||
202b0 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c pParse->db->mall
202b1 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 ocFailed ) retur
202b2 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f n 0;.#if SQLITE_
202b3 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 MAX_COLUMN. if(
202b4 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 pOrderBy->nExpr
202b5 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 >db->aLimit[SQLI
202b6 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d TE_LIMIT_COLUMN]
202b7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 ){. sqlite3E
202b8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 rrorMsg(pParse,
202b9 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 "too many terms
202ba 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65 22 in %s BY clause"
202bb 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72 65 , zType);. re
202bc 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 turn 1;. }.#end
202bd 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 if. pEList = pS
202be 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 elect->pEList;.
202bf 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21 assert( pEList!
202c0 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 =0 ); /* sqlite
202c1 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75 61 3SelectNew() gua
202c2 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f 0a rantees this */.
202c3 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d for(i=0, pItem
202c4 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c =pOrderBy->a; i<
202c5 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b pOrderBy->nExpr;
202c6 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a i++, pItem++){.
202c7 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 if( pItem->i
202c8 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 Col ){. if(
202c9 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 4c pItem->iCol>pEL
202ca 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 ist->nExpr ){.
202cb 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 resolveOut
202cc 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 OfRangeError(pPa
202cd 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c rse, zType, i+1,
202ce 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b pEList->nExpr);
202cf 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
202d0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 1;. }.
202d1 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 resolveAlias(pP
202d2 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 49 arse, pEList, pI
202d3 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 74 tem->iCol-1, pIt
202d4 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70 65 em->pExpr, zType
202d5 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 );. }. }. r
202d6 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a eturn 0;.}../*.*
202d7 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e * pOrderBy is an
202d8 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
202d9 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 UP BY clause in
202da 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
202db 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 65 pSelect..** The
202dc 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 Name context of
202dd 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 the SELECT stat
202de 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20 7a ement is pNC. z
202df 54 79 70 65 20 69 73 20 65 69 74 68 65 72 0a 2a Type is either.*
202e0 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 * "ORDER" or "GR
202e1 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20 6f OUP" depending o
202e2 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f 66 20 n which type of
202e3 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79 20 clause pOrderBy
202e4 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 is..**.** This r
202e5 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73 20 outine resolves
202e6 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 each term of the
202e7 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e 20 clause into an
202e8 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 49 expression..** I
202e9 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20 74 f the order-by t
202ea 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 erm is an intege
202eb 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 61 6e r I between 1 an
202ec 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73 20 d N (where N is
202ed 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 the.** number of
202ee 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
202ef 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 result set of th
202f0 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20 74 e SELECT) then t
202f1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a he expression.**
202f2 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74 69 in the resoluti
202f3 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 on is a copy of
202f4 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74 2d the I-th result-
202f5 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 set expression.
202f6 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65 72 If.** the order
202f7 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20 69 -by term is an i
202f8 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f 72 dentify that cor
202f9 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 responds to the
202fa 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 20 AS-name of.** a
202fb 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 result-set expre
202fc 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 ssion, then the
202fd 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f term resolves to
202fe 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a a copy of the.*
202ff 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 * result-set exp
20300 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77 ression. Otherw
20301 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 73 73 ise, the express
20302 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64 20 ion is resolved
20303 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c 20 in.** the usual
20304 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c 69 way - using sqli
20305 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 te3ResolveExprNa
20306 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 mes()..**.** Thi
20307 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e s routine return
20308 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
20309 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72 6f errors. If erro
2030a 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a 2a rs occur, then.*
2030b 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 * an appropriate
2030c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 6d error message m
2030d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e 20 ight be left in
2030e 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65 72 pParse. (OOM er
2030f 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65 64 rors.** excepted
20310 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 .).*/.static int
20311 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f resolveOrderGro
20312 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 upBy(. NameCont
20313 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f 2a ext *pNC, /*
20314 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 The name contex
20315 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 t of the SELECT
20316 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 statement */. S
20317 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 elect *pSelect,
20318 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 /* The SELE
20319 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f 6c CT statement hol
2031a 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a 2f ding pOrderBy */
2031b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 . ExprList *pOr
2031c 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 4f derBy, /* An O
2031d 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 RDER BY or GROUP
2031e 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72 65 BY clause to re
2031f 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 solve */. const
20320 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20 20 char *zType
20321 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44 45 /* Either "ORDE
20322 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20 61 R" or "GROUP", a
20323 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f s appropriate */
20324 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 .){. int i;
20325 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20326 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
20327 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 43 nter */. int iC
20328 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ol;
20329 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 /* Colu
2032a 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 mn number */. s
2032b 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
2032c 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a tem *pItem; /*
2032d 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f A term of the O
2032e 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a RDER BY clause *
2032f 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 /. Parse *pPars
20330 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e;
20331 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f /* Parsing co
20332 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e ntext */. int n
20333 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20 Result;
20334 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
20335 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 ber of terms in
20336 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a the result set *
20337 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 /.. if( pOrderB
20338 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b y==0 ) return 0;
20339 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53 65 . nResult = pSe
2033a 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 lect->pEList->nE
2033b 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 xpr;. pParse =
2033c 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 66 pNC->pParse;. f
2033d 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f or(i=0, pItem=pO
2033e 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 rderBy->a; i<pOr
2033f 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b derBy->nExpr; i+
20340 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
20341 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74 65 Expr *pE = pIte
20342 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 43 m->pExpr;. iC
20343 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 ol = resolveAsNa
20344 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 me(pParse, pSele
20345 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29 3b ct->pEList, pE);
20346 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 . if( iCol>0
20347 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 ){. /* If a
20348 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68 20 n AS-name match
20349 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20 74 is found, mark t
2034a 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f 6c his ORDER BY col
2034b 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20 20 umn as being.
2034c 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 ** a copy of
2034d 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 the iCol-th resu
2034e 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20 20 lt-set column.
2034f 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 The subsequent c
20350 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 all to. **
20351 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 sqlite3ResolveOr
20352 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c derGroupBy() wil
20353 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65 78 l convert the ex
20354 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20 20 pression to a.
20355 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20 74 ** copy of t
20356 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75 6c he iCol-th resul
20357 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e t-set expression
20358 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65 6d . */. pItem
20359 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 ->iCol = (u16)iC
2035a 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e ol;. contin
2035b 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ue;. }. if
2035c 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 ( sqlite3ExprIsI
2035d 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c nteger(pE, &iCol
2035e 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 ) ){. /* Th
2035f 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 e ORDER BY term
20360 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f is an integer co
20361 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c 20 nstant. Again,
20362 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 set the column.
20363 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 73 ** number s
20364 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52 65 o that sqlite3Re
20365 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 solveOrderGroupB
20366 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 y() will convert
20367 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 72 the. ** or
20368 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20 61 der-by term to a
20369 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 copy of the res
2036a 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 ult-set expressi
2036b 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 on */. if(
2036c 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20 20 iCol<1 ){.
2036d 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 resolveOutOfRa
2036e 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c ngeError(pParse,
2036f 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52 65 zType, i+1, nRe
20370 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 72 sult);. r
20371 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d eturn 1;. }
20372 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 . pItem->iC
20373 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b 0a ol = (u16)iCol;.
20374 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a continue;.
20375 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 }.. /* Ot
20376 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 20 74 herwise, treat t
20377 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d he ORDER BY term
20378 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 as an ordinary
20379 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 expression */.
2037a 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 pItem->iCol =
2037b 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 0;. if( sqlit
2037c 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
2037d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a 20 es(pNC, pE) ){.
2037e 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
2037f 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
20380 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 n sqlite3Resolve
20381 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 OrderGroupBy(pPa
20382 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70 4f rse, pSelect, pO
20383 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 3b 0a rderBy, zType);.
20384 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 }../*.** Resolve
20385 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 53 45 names in the SE
20386 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 LECT statement p
20387 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 and all of its
20388 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a descendents..*/.
20389 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c static int resol
2038a 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 61 6c veSelectStep(Wal
2038b 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 ker *pWalker, Se
2038c 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 lect *p){. Name
2038d 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e Context *pOuterN
2038e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 74 C; /* Context t
2038f 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69 hat contains thi
20390 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 s SELECT */. Na
20391 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 meContext sNC;
20392 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f /* Name co
20393 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 53 45 ntext of this SE
20394 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69 73 LECT */. int is
20395 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 Compound;
20396 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20 69 /* True if p i
20397 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c s a compound sel
20398 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f ect */. int nCo
20399 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 mpound;
2039a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f /* Number of co
2039b 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72 6f mpound terms pro
2039c 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a 2f cessed so far */
2039d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
2039e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 ; /* Pa
2039f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
203a0 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c . ExprList *pEL
203a1 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 ist; /* Re
203a2 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 sult set express
203a3 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e ion list */. in
203a4 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
203a5 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f /* Loop co
203a6 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c unter */. ExprL
203a7 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 ist *pGroupBy;
203a8 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50 20 /* The GROUP
203a9 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 BY clause */. S
203aa 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73 74 elect *pLeftmost
203ab 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d 6d ; /* Left-m
203ac 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 6f 66 ost of SELECT of
203ad 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 a compound */.
203ae 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 sqlite3 *db;
203af 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 /* Data
203b0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 base connection
203b1 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 74 28 */. .. assert(
203b2 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 p!=0 );. if( p
203b3 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f ->selFlags & SF_
203b4 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20 20 Resolved ){.
203b5 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 return WRC_Prune
203b6 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e 43 ;. }. pOuterNC
203b7 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e = pWalker->u.pN
203b8 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 C;. pParse = pW
203b9 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 alker->pParse;.
203ba 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
203bb 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 ;.. /* Normally
203bc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 sqlite3SelectEx
203bd 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20 63 pand() will be c
203be 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64 20 alled first and
203bf 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61 will have. ** a
203c0 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 64 20 lready expanded
203c1 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 48 6f this SELECT. Ho
203c2 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 20 69 wever, if this i
203c3 73 20 61 20 73 75 62 71 75 65 72 79 20 77 69 74 s a subquery wit
203c4 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70 72 hin. ** an expr
203c5 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33 52 ession, sqlite3R
203c6 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
203c7 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 ) will be called
203c8 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a 20 without a. **
203c9 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 prior call to sq
203ca 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e lite3SelectExpan
203cb 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 74 20 d(). When that
203cc 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20 2a happens, let. *
203cd 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 * sqlite3SelectP
203ce 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66 20 rep() do all of
203cf 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 the processing f
203d0 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 2e 0a or this SELECT..
203d1 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 ** sqlite3Sele
203d2 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 69 6e ctPrep() will in
203d3 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 74 65 voke both sqlite
203d4 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 3SelectExpand()
203d5 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 72 6f and. ** this ro
203d6 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 6f 72 utine in the cor
203d7 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a 2f rect order.. */
203d8 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c . if( (p->selFl
203d9 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 ags & SF_Expande
203da 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c d)==0 ){. sql
203db 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 ite3SelectPrep(p
203dc 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 Parse, p, pOuter
203dd 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 NC);. return
203de 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c (pParse->nErr ||
203df 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
203e0 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a d) ? WRC_Abort :
203e1 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a WRC_Prune;. }.
203e2 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 . isCompound =
203e3 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20 20 p->pPrior!=0;.
203e4 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 nCompound = 0;.
203e5 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b 0a pLeftmost = p;.
203e6 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 while( p ){.
203e7 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 assert( (p->se
203e8 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 lFlags & SF_Expa
203e9 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 20 20 nded)!=0 );.
203ea 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 assert( (p->selF
203eb 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 lags & SF_Resolv
203ec 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 2d ed)==0 );. p-
203ed 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f >selFlags |= SF_
203ee 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20 2f Resolved;.. /
203ef 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 * Resolve the ex
203f0 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 pressions in the
203f1 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 LIMIT and OFFSE
203f2 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73 65 T clauses. These
203f3 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 . ** are not
203f4 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65 72 allowed to refer
203f5 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20 73 to any names, s
203f6 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79 20 o pass an empty
203f7 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20 20 NameContext..
203f8 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 */. memset(&
203f9 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 sNC, 0, sizeof(s
203fa 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 NC));. sNC.pP
203fb 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 arse = pParse;.
203fc 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 if( sqlite3Re
203fd 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 solveExprNames(&
203fe 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 20 sNC, p->pLimit)
203ff 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ||. sqlit
20400 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
20401 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 es(&sNC, p->pOff
20402 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 set) ){. re
20403 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
20404 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
20405 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61 6c Set up the local
20406 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f name-context to
20407 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65 33 pass to sqlite3
20408 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 ResolveExprNames
20409 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 73 () to. ** res
2040a 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74 2d olve the result-
2040b 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 6c set expression l
2040c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ist.. */.
2040d 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 sNC.allowAgg = 1
2040e 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 ;. sNC.pSrcLi
2040f 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 st = p->pSrc;.
20410 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4f sNC.pNext = pO
20411 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20 2f uterNC;. . /
20412 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 * Resolve names
20413 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 in the result se
20414 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73 74 t. */. pEList
20415 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 = p->pEList;.
20416 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 assert( pEList
20417 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 !=0 );. for(i
20418 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 =0; i<pEList->nE
20419 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 xpr; i++){.
2041a 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 4c 69 Expr *pX = pELi
2041b 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a st->a[i].pExpr;.
2041c 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
2041d 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 3ResolveExprName
2041e 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a 20 s(&sNC, pX) ){.
2041f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 return WR
20420 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d C_Abort;. }
20421 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a . }. . /*
20422 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65 73 Recursively res
20423 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c olve names in al
20424 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 l subqueries.
20425 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b */. for(i=0;
20426 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 i<p->pSrc->nSrc
20427 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 ; i++){. st
20428 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
20429 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 m *pItem = &p->p
2042a 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 Src->a[i];.
2042b 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c if( pItem->pSel
2042c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 63 ect ){. c
2042d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76 65 onst char *zSave
2042e 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 dContext = pPars
2042f 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b e->zAuthContext;
20430 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 . if( pIt
20431 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61 72 em->zName ) pPar
20432 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 se->zAuthContext
20433 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b = pItem->zName;
20434 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
20435 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d ResolveSelectNam
20436 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d es(pParse, pItem
20437 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74 65 ->pSelect, pOute
20438 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70 50 rNC);. pP
20439 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 arse->zAuthConte
2043a 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74 65 xt = zSavedConte
2043b 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 xt;. if(
2043c 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 pParse->nErr ||
2043d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
2043e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 ) return WRC_Ab
2043f 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 ort;. }.
20440 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 }. . /* If
20441 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67 67 there are no agg
20442 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 regate functions
20443 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 in the result-s
20444 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 et, and no GROUP
20445 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 70 72 BY . ** expr
20446 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20 61 ession, do not a
20447 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73 20 llow aggregates
20448 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 in any of the ot
20449 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 2e her expressions.
2044a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 . */. asse
2044b 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 rt( (p->selFlags
2044c 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 & SF_Aggregate)
2044d 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 6f 75 ==0 );. pGrou
2044e 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 pBy = p->pGroupB
2044f 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 y;. if( pGrou
20450 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41 67 pBy || sNC.hasAg
20451 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73 65 g ){. p->se
20452 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 lFlags |= SF_Agg
20453 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c 73 regate;. }els
20454 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c 6c e{. sNC.all
20455 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 7d owAgg = 0;. }
20456 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 . . /* If a
20457 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69 73 HAVING clause is
20458 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20 74 present, then t
20459 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 47 here must be a G
2045a 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 0a ROUP BY clause..
2045b 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
2045c 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70 47 ->pHaving && !pG
2045d 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20 20 roupBy ){.
2045e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
2045f 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55 50 pParse, "a GROUP
20460 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 72 65 BY clause is re
20461 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 48 41 quired before HA
20462 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72 65 VING");. re
20463 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a turn WRC_Abort;.
20464 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
20465 41 64 64 20 74 68 65 20 65 78 70 72 65 73 73 69 Add the expressi
20466 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20 6e on list to the n
20467 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66 6f ame-context befo
20468 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a 20 re parsing the.
20469 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70 72 ** other expr
2046a 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 essions in the S
2046b 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
2046c 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74 This is so that
2046d 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 . ** expressi
2046e 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 ons in the WHERE
2046f 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20 63 clause (etc.) c
20470 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70 72 an refer to expr
20471 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20 2a essions by. *
20472 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68 65 * aliases in the
20473 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20 20 result set..
20474 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f 72 **. ** Minor
20475 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73 20 point: If this
20476 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 is the case, the
20477 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e n the expression
20478 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 will be. **
20479 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f 72 re-evaluated for
2047a 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65 20 each reference
2047b 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 to it.. */.
2047c 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20 70 sNC.pEList = p
2047d 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 ->pEList;. if
2047e 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 ( sqlite3Resolve
2047f 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 ExprNames(&sNC,
20480 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20 20 p->pWhere) ||.
20481 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f sqlite3Reso
20482 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e lveExprNames(&sN
20483 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a 20 C, p->pHaving).
20484 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 ){. retu
20485 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
20486 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 }.. /* The
20487 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 52 4f ORDER BY and GRO
20488 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d 61 UP BY clauses ma
20489 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 74 y not refer to t
2048a 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20 6f erms in. ** o
2048b 75 74 65 72 20 71 75 65 72 69 65 73 20 0a 20 20 uter queries .
2048c 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e 65 */. sNC.pNe
2048d 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43 2e xt = 0;. sNC.
2048e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a 20 allowAgg = 1;..
2048f 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 /* Process th
20490 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
20491 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e 20 e for singleton
20492 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
20493 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f 52 s.. ** The OR
20494 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66 6f DER BY clause fo
20495 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c 45 r compounds SELE
20496 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 CT statements is
20497 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a 20 handled. **
20498 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c 6c below, after all
20499 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 of the result-s
2049a 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 ets for all of t
2049b 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a 20 he elements of.
2049c 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 ** the compou
2049d 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 nd have been res
2049e 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 olved.. */.
2049f 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f 75 6e if( !isCompoun
204a0 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 64 65 d && resolveOrde
204a1 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 70 rGroupBy(&sNC, p
204a2 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 , p->pOrderBy, "
204a3 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 20 20 ORDER") ){.
204a4 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
204a5 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 t;. }. if(
204a6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
204a7 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 d ){. retur
204a8 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 n WRC_Abort;.
204a9 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 73 }. . /* Res
204aa 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 20 42 olve the GROUP B
204ab 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 74 68 Y clause. At th
204ac 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61 6b e same time, mak
204ad 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a 20 74 e sure . ** t
204ae 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 he GROUP BY clau
204af 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 se does not cont
204b0 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 ain aggregate fu
204b1 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a nctions.. */.
204b2 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 if( pGroupBy
204b3 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 ){. struct
204b4 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
204b5 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20 20 pItem;. .
204b6 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72 64 if( resolveOrd
204b7 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20 erGroupBy(&sNC,
204b8 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47 52 p, pGroupBy, "GR
204b9 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c OUP") || db->mal
204ba 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
204bb 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f return WRC_
204bc 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 Abort;. }.
204bd 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 for(i=0, pI
204be 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b tem=pGroupBy->a;
204bf 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 i<pGroupBy->nEx
204c0 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b pr; i++, pItem++
204c1 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 ){. if( E
204c2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
204c3 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50 5f Item->pExpr, EP_
204c4 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Agg) ){.
204c5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
204c6 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 72 65 g(pParse, "aggre
204c7 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 gate functions a
204c8 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 69 re not allowed i
204c9 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 n ".
204ca 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59 20 "the GROUP BY
204cb 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20 20 clause");.
204cc 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 return WRC_A
204cd 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a bort;. }.
204ce 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 }. }..
204cf 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f /* Advance to
204d0 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20 6f the next term o
204d1 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a 20 f the compound.
204d2 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70 2d */. p = p-
204d3 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43 6f >pPrior;. nCo
204d4 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a 20 mpound++;. }..
204d5 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 /* Resolve the
204d6 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 63 6f ORDER BY on a co
204d7 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61 66 mpound SELECT af
204d8 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 ter all terms of
204d9 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f 75 . ** the compou
204da 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 nd have been res
204db 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 olved.. */. if
204dc 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 ( isCompound &&
204dd 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f resolveCompoundO
204de 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 rderBy(pParse, p
204df 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20 20 Leftmost) ){.
204e0 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
204e1 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e t;. }.. return
204e2 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a 2f WRC_Prune;.}../
204e3 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
204e4 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72 65 e walks an expre
204e5 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 72 ssion tree and r
204e6 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e 63 esolves referenc
204e7 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20 63 es to.** table c
204e8 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 75 6c olumns and resul
204e9 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20 20 t-set columns.
204ea 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 At the same time
204eb 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63 68 , do error.** ch
204ec 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74 69 ecking on functi
204ed 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65 74 on usage and set
204ee 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20 61 a flag if any a
204ef 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
204f0 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e 0a ns.** are seen..
204f1 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 **.** To resolve
204f2 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 72 table columns r
204f3 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c 6f 6f eferences we loo
204f4 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72 20 k for nodes (or
204f5 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 68 65 subtrees) of the
204f6 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 .** form X.Y.Z
204f7 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a or Y.Z or just Z
204f8 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20 20 where.**.**
204f9 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d 65 20 X: The name
204fa 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 20 20 of a database.
204fb 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20 22 Ex: "main" or "
204fc 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20 20 temp" or.**
204fd 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 6f 6c the symbol
204fe 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 ic name assigned
204ff 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d 65 64 to an ATTACH-ed
20500 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a database..**.**
20501 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 20 6e Y: The n
20502 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20 69 ame of a table i
20503 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e n a FROM clause.
20504 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 67 65 Or in a trigge
20505 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f r.** o
20506 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69 61 ne of the specia
20507 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f 72 l names "old" or
20508 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 "new"..**.**
20509 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d 65 Z: The name
2050a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 of a column in
2050b 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20 54 table Y..**.** T
2050c 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20 72 he node at the r
2050d 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 74 72 oot of the subtr
2050e 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 61 ee is modified a
2050f 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a s follows:.**.**
20510 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20 20 Expr.op
20511 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54 4b Changed to TK
20512 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45 78 _COLUMN.** Ex
20513 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f 69 pr.pTab Poi
20514 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c 65 nts to the Table
20515 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59 0a object for X.Y.
20516 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c 75 ** Expr.iColu
20517 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 mn The column
20518 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20 2d index in X.Y. -
20519 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64 2e 1 for the rowid.
2051a 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 61 62 .** Expr.iTab
2051b 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 20 63 le The VDBE c
2051c 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 ursor number for
2051d 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 6f X.Y.**.**.** To
2051e 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c 74 2d resolve result-
2051f 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 2c 20 set references,
20520 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 73 73 look for express
20521 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 ion nodes of the
20522 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74 68 .** form Z (with
20523 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 65 66 no X and Y pref
20524 69 78 29 20 77 68 65 72 65 20 74 68 65 20 5a 20 ix) where the Z
20525 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 67 68 matches the righ
20526 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20 6f t-hand.** size o
20527 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 20 69 f an AS clause i
20528 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 n the result-set
20529 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 20 54 of a SELECT. T
2052a 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f 6e 0a he Z expression.
2052b 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 ** is replaced b
2052c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 y a copy of the
2052d 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f left-hand side o
2052e 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 f the result-set
2052f 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 expression..**
20530 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20 66 Table-name and f
20531 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74 69 unction resoluti
20532 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 68 65 on occurs on the
20533 20 73 75 62 73 74 69 74 75 74 65 64 20 65 78 70 substituted exp
20534 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e ression.** tree.
20535 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 For example, i
20536 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 n:.**.** SE
20537 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 LECT a+b AS x, c
20538 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 +d AS y FROM t1
20539 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a 2a ORDER BY x;.**.*
2053a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d 20 6f * The "x" term o
2053b 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 69 f the order by i
2053c 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 22 61 s replaced by "a
2053d 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a 2a +b" to render:.*
2053e 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43 54 *.** SELECT
2053f 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20 41 a+b AS x, c+d A
20540 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44 45 S y FROM t1 ORDE
20541 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a 20 R BY a+b;.**.**
20542 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 61 Function calls a
20543 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 re checked to ma
20544 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 ke sure that the
20545 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a 2a function is .**
20546 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 68 61 defined and tha
20547 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 t the correct nu
20548 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 mber of argument
20549 73 20 61 72 65 20 73 70 65 63 69 66 69 65 64 2e s are specified.
2054a 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 .** If the funct
2054b 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 65 67 ion is an aggreg
2054c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 ate function, th
2054d 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 73 41 en the pNC->hasA
2054e 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e 64 gg is.** set and
2054f 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 63 the opcode is c
20550 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f 46 hanged from TK_F
20551 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41 47 UNCTION to TK_AG
20552 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20 49 G_FUNCTION..** I
20553 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 f an expression
20554 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 contains aggrega
20555 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 65 te functions the
20556 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a 20 n the EP_Agg.**
20557 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65 20 property on the
20558 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 expression is se
20559 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72 6f t..**.** An erro
2055a 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65 66 r message is lef
2055b 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 20 61 t in pParse if a
2055c 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73 73 nything is amiss
2055d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a . The number.**
2055e 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 72 65 if errors is re
2055f 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 turned..*/.SQLIT
20560 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
20561 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 lite3ResolveExpr
20562 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43 6f Names( . NameCo
20563 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 ntext *pNC,
20564 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20 74 /* Namespace t
20565 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 o resolve expres
20566 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20 45 sions in. */. E
20567 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 xpr *pExpr
20568 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 /* The ex
20569 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 pression to be a
2056a 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a 20 nalyzed. */.){.
2056b 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67 67 int savedHasAgg
2056c 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 ;. Walker w;..
2056d 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20 if( pExpr==0 )
2056e 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 return 0;.#if SQ
2056f 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 LITE_MAX_EXPR_DE
20570 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50 61 PTH>0. {. Pa
20571 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 4e rse *pParse = pN
20572 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 69 C->pParse;. i
20573 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 68 f( sqlite3ExprCh
20574 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73 65 eckHeight(pParse
20575 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 , pExpr->nHeight
20576 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 +pNC->pParse->nH
20577 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 eight) ){.
20578 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
20579 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 pParse->nHei
2057a 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e 48 ght += pExpr->nH
2057b 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64 69 eight;. }.#endi
2057c 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67 20 f. savedHasAgg
2057d 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a 20 = pNC->hasAgg;.
2057e 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 30 pNC->hasAgg = 0
2057f 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 ;. w.xExprCallb
20580 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 ack = resolveExp
20581 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 rStep;. w.xSele
20582 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 ctCallback = res
20583 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a olveSelectStep;.
20584 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 w.pParse = pNC
20585 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e ->pParse;. w.u.
20586 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71 6c pNC = pNC;. sql
20587 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c ite3WalkExpr(&w,
20588 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 51 4c pExpr);.#if SQL
20589 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 ITE_MAX_EXPR_DEP
2058a 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61 72 TH>0. pNC->pPar
2058b 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 70 se->nHeight -= p
2058c 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 23 Expr->nHeight;.#
2058d 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43 2d endif. if( pNC-
2058e 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 50 61 >nErr>0 || w.pPa
2058f 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a 20 rse->nErr>0 ){.
20590 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 ExprSetProper
20591 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 ty(pExpr, EP_Err
20592 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 or);. }. if( p
20593 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20 20 NC->hasAgg ){.
20594 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 ExprSetPropert
20595 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67 29 y(pExpr, EP_Agg)
20596 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 61 ;. }else if( sa
20597 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20 20 vedHasAgg ){.
20598 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 pNC->hasAgg = 1
20599 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 45 ;. }. return E
2059a 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
2059b 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29 3b Expr, EP_Error);
2059c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c .}.../*.** Resol
2059d 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 ve all names in
2059e 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 all expressions
2059f 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64 20 of a SELECT and
205a0 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e 64 in all.** decend
205a1 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c 45 ents of the SELE
205a2 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 6f CT, including co
205a3 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20 70 mpounds off of p
205a4 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75 62 ->pPrior,.** sub
205a5 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 72 65 queries in expre
205a6 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62 71 ssions, and subq
205a7 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20 46 ueries used as F
205a8 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74 65 ROM clause.** te
205a9 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 73 rms..**.** See s
205aa 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 qlite3ResolveExp
205ab 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20 64 rNames() for a d
205ac 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 escription of th
205ad 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74 72 e kinds of.** tr
205ae 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74 68 ansformations th
205af 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 at occur..**.**
205b0 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74 65 All SELECT state
205b1 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61 76 ments should hav
205b2 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64 20 e been expanded
205b3 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 using.** sqlite3
205b4 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 70 SelectExpand() p
205b5 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 rior to invoking
205b6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a this routine..*
205b7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
205b8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 void sqlite3Res
205b9 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 olveSelectNames(
205ba 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
205bb 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 , /* The
205bc 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 parser context
205bd 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 */. Select *p,
205be 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
205bf 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
205c0 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e ent being coded.
205c1 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 */. NameContex
205c2 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 t *pOuterNC /*
205c3 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 Name context for
205c4 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20 73 parent SELECT s
205c5 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 tatement */.){.
205c6 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61 73 Walker w;.. as
205c7 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
205c8 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 w.xExprCallback
205c9 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74 65 = resolveExprSte
205ca 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 p;. w.xSelectCa
205cb 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 llback = resolve
205cc 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77 2e SelectStep;. w.
205cd 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b pParse = pParse;
205ce 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f 75 . w.u.pNC = pOu
205cf 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65 33 terNC;. sqlite3
205d0 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 WalkSelect(&w, p
205d1 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a );.}../*********
205d2 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65 73 ***** End of res
205d3 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a olve.c *********
205d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205d6 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a ****/./*********
205d7 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 ***** Begin file
205d8 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a expr.c ********
205d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205db 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 ****/./*.** 2001
205dc 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a September 15.**
205dd 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 .** The author d
205de 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 isclaims copyrig
205df 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 ht to this sourc
205e0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 e code. In plac
205e1 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 e of.** a legal
205e2 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 notice, here is
205e3 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a a blessing:.**.*
205e4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 * May you do
205e5 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 good and not evi
205e6 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 l..** May you
205e7 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 find forgivenes
205e8 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 s for yourself a
205e9 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 nd forgive other
205ea 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 s..** May you
205eb 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e share freely, n
205ec 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 ever taking more
205ed 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a than you give..
205ee 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a **.*************
205ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
205f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 ************.**
205f3 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 This file contai
205f4 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 ns routines used
205f5 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20 65 for analyzing e
205f6 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a 2a xpressions and.*
205f7 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e 67 * for generating
205f8 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 VDBE code that
205f9 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 65 73 evaluates expres
205fa 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65 2e sions in SQLite.
205fb 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 .*/../*.** Retur
205fc 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79 27 n the 'affinity'
205fd 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 of the expressi
205fe 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79 2e on pExpr if any.
205ff 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 .**.** If pExpr
20600 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20 72 is a column, a r
20601 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63 6f eference to a co
20602 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53 27 lumn via an 'AS'
20603 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61 20 alias,.** or a
20604 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68 20 sub-select with
20605 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 a column as the
20606 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74 68 return value, th
20607 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69 6e en the .** affin
20608 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c 75 ity of that colu
20609 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 mn is returned.
2060a 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30 20 Otherwise, 0x00
2060b 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a 20 is returned,.**
2060c 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61 66 indicating no af
2060d 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20 65 finity for the e
2060e 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a xpression..**.**
2060f 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45 20 i.e. the WHERE
20610 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73 69 clause expresssi
20611 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f ons in the follo
20612 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 wing statements
20613 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20 61 all.** have an a
20614 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20 43 ffinity:.**.** C
20615 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 REATE TABLE t1(a
20616 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 );.** SELECT * F
20617 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b 0a ROM t1 WHERE a;.
20618 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20 62 ** SELECT a AS b
20619 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 62 FROM t1 WHERE b
2061a 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46 52 ;.** SELECT * FR
2061b 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65 6c OM t1 WHERE (sel
2061c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b 0a ect a from t1);.
2061d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
2061e 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45 78 E char sqlite3Ex
2061f 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 prAffinity(Expr
20620 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 6f *pExpr){. int o
20621 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 p = pExpr->op;.
20622 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c 45 if( op==TK_SELE
20623 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 CT ){. assert
20624 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 45 ( pExpr->flags&E
20625 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b 0a 20 P_xIsSelect );.
20626 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 return sqlite
20627 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 3ExprAffinity(pE
20628 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e xpr->x.pSelect->
20629 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 pEList->a[0].pEx
2062a 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 pr);. }.#ifndef
2062b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 SQLITE_OMIT_CAS
2062c 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 43 T. if( op==TK_C
2062d 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72 AST ){. asser
2062e 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
2062f 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e rty(pExpr, EP_In
20630 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 72 tValue) );. r
20631 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66 66 eturn sqlite3Aff
20632 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 2d inityType(pExpr-
20633 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 0a >u.zToken);. }.
20634 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f 70 #endif. if( (op
20635 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 ==TK_AGG_COLUMN
20636 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e || op==TK_COLUMN
20637 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 || op==TK_REGIS
20638 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78 70 TER) . && pExp
20639 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b 0a r->pTab!=0. ){.
2063a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52 45 /* op==TK_RE
2063b 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72 2d GISTER && pExpr-
2063c 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 >pTab!=0 happens
2063d 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 when pExpr was
2063e 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 2a originally. *
2063f 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 * a TK_COLUMN bu
20640 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
20641 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 evaluated and c
20642 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 ached in a regis
20643 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6a ter */. int j
20644 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d = pExpr->iColum
20645 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20 29 n;. if( j<0 )
20646 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 return SQLITE_A
20647 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 FF_INTEGER;.
20648 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 assert( pExpr->p
20649 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d 3e Tab && j<pExpr->
2064a 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 pTab->nCol );.
2064b 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e return pExpr->
2064c 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61 66 pTab->aCol[j].af
2064d 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72 65 finity;. }. re
2064e 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66 69 turn pExpr->affi
2064f 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 nity;.}../*.** S
20650 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 et the collating
20651 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65 78 sequence for ex
20652 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 74 pression pExpr t
20653 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 o be the collati
20654 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6e ng.** sequence n
20655 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e 20 amed by pToken.
20656 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 Return a point
20657 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73 65 er to the revise
20658 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a d expression..**
20659 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 The collating s
2065a 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b 65 equence is marke
2065b 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22 20 d as "explicit"
2065c 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78 70 using the EP_Exp
2065d 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67 2e Collate.** flag.
2065e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63 6f An explicit co
2065f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
20660 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20 69 will override i
20661 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c 61 mplicit.** colla
20662 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e 0a ting sequences..
20663 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 */.SQLITE_PRIVAT
20664 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 E Expr *sqlite3E
20665 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 xprSetColl(Parse
20666 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
20667 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70 43 pExpr, Token *pC
20668 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 ollName){. char
20669 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20 20 *zColl = 0;
2066a 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75 6f /* Dequo
2066b 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c 6c ted name of coll
2066c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 2a ation sequence *
2066d 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f /. CollSeq *pCo
2066e 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 ll;. sqlite3 *d
2066f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
20670 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 zColl = sqlite
20671 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 3NameFromToken(d
20672 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a 20 b, pCollName);.
20673 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a 43 if( pExpr && zC
20674 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c oll ){. pColl
20675 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 = sqlite3Locate
20676 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 CollSeq(pParse,
20677 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 zColl);. if(
20678 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 pColl ){. p
20679 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 Expr->pColl = pC
2067a 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 70 72 oll;. pExpr
2067b 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 ->flags |= EP_Ex
2067c 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d 0a pCollate;. }.
2067d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 }. sqlite3DbF
2067e 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b 0a ree(db, zColl);.
2067f 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b 0a return pExpr;.
20680 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 }../*.** Return
20681 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c the default coll
20682 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 ation sequence f
20683 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f or the expressio
20684 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20 74 n pExpr. If.** t
20685 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 61 75 here is no defau
20686 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 lt collation typ
20687 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a e, return 0..*/.
20688 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 SQLITE_PRIVATE C
20689 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 ollSeq *sqlite3E
2068a 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 xprCollSeq(Parse
2068b 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
2068c 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53 65 pExpr){. CollSe
2068d 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 q *pColl = 0;.
2068e 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72 3b Expr *p = pExpr;
2068f 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 . while( ALWAYS
20690 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f (p) ){. int o
20691 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 p;. pColl = p
20692 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 ->pColl;. if(
20693 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a pColl ) break;.
20694 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a op = p->op;.
20695 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62 21 if( p->pTab!
20696 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 20 20 =0 && (.
20697 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d op==TK_AGG_COLUM
20698 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 N || op==TK_COLU
20699 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 MN || op==TK_REG
2069a 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f ISTER || op==TK_
2069b 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 7b 0a TRIGGER. )){.
2069c 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f /* op==TK_
2069d 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70 REGISTER && p->p
2069e 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77 Tab!=0 happens w
2069f 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72 hen pExpr was or
206a0 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a iginally. *
206a1 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75 * a TK_COLUMN bu
206a2 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 t was previously
206a3 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63 evaluated and c
206a4 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73 ached in a regis
206a5 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e ter */. con
206a6 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a st char *zColl;.
206a7 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d int j = p-
206a8 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 >iColumn;.
206a9 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 if( j>=0 ){.
206aa 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 sqlite3 *db
206ab 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
206ac 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d zColl = p-
206ad 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a >pTab->aCol[j].z
206ae 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43 Coll;. pC
206af 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e oll = sqlite3Fin
206b0 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 dCollSeq(db, ENC
206b1 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 3b (db), zColl, 0);
206b2 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e . pExpr->
206b3 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 pColl = pColl;.
206b4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
206b5 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ak;. }. if
206b6 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26 26 ( op!=TK_CAST &&
206b7 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29 7b op!=TK_UPLUS ){
206b8 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
206b9 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 }. p = p->p
206ba 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 Left;. }. if(
206bb 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c sqlite3CheckColl
206bc 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f 6c Seq(pParse, pCol
206bd 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c 6c l) ){ . pColl
206be 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 = 0;. }. retu
206bf 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a rn pColl;.}../*.
206c0 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20 6f ** pExpr is an o
206c1 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d 70 perand of a comp
206c2 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e arison operator.
206c3 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a 2a aff2 is the.**
206c4 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 6f type affinity o
206c5 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65 72 f the other oper
206c6 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 and. This routi
206c7 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a ne returns the.*
206c8 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 * type affinity
206c9 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 that should be u
206ca 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 sed for the comp
206cb 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e arison operator.
206cc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
206cd 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 43 TE char sqlite3C
206ce 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 45 ompareAffinity(E
206cf 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 xpr *pExpr, char
206d0 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20 61 aff2){. char a
206d1 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ff1 = sqlite3Exp
206d2 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 rAffinity(pExpr)
206d3 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26 20 ;. if( aff1 &&
206d4 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 42 aff2 ){. /* B
206d5 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68 65 oth sides of the
206d6 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65 20 comparison are
206d7 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65 20 columns. If one
206d8 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20 20 has numeric.
206d9 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73 65 ** affinity, use
206da 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73 65 that. Otherwise
206db 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74 79 use no affinity
206dc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
206dd 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 sqlite3IsNumeri
206de 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29 20 cAffinity(aff1)
206df 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 || sqlite3IsNume
206e0 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 32 ricAffinity(aff2
206e1 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 ) ){. retur
206e2 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d n SQLITE_AFF_NUM
206e3 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65 7b ERIC;. }else{
206e4 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 . return SQ
206e5 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 LITE_AFF_NONE;.
206e6 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 }. }else if(
206e7 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32 20 !aff1 && !aff2
206e8 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 ){. /* Neithe
206e9 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f r side of the co
206ea 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63 6f mparison is a co
206eb 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20 74 lumn. Compare t
206ec 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 he. ** result
206ed 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20 20 s directly..
206ee 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 */. return SQ
206ef 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 LITE_AFF_NONE;.
206f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4f }else{. /* O
206f1 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f 6c ne side is a col
206f2 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20 69 umn, the other i
206f3 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20 63 s not. Use the c
206f4 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79 2e olumns affinity.
206f5 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 */. assert(
206f6 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32 3d aff1==0 || aff2=
206f7 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e =0 );. return
206f8 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b 0a (aff1 + aff2);.
206f9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 }.}../*.** pEx
206fa 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73 pr is a comparis
206fb 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52 65 on operator. Re
206fc 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61 66 turn the type af
206fd 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 finity that shou
206fe 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65 64 ld.** be applied
206ff 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 to both operand
20700 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67 s prior to doing
20701 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e the comparison.
20702 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 .*/.static char
20703 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 comparisonAffini
20704 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b ty(Expr *pExpr){
20705 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20 61 . char aff;. a
20706 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 ssert( pExpr->op
20707 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70 72 ==TK_EQ || pExpr
20708 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 70 ->op==TK_IN || p
20709 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 Expr->op==TK_LT
2070a 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 ||. pEx
2070b 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c 7c pr->op==TK_GT ||
2070c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 pExpr->op==TK_G
2070d 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d E || pExpr->op==
2070e 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20 20 TK_LE ||.
2070f 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b pExpr->op==TK
20710 5f 4e 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 _NE || pExpr->op
20711 3d 3d 54 4b 5f 49 53 20 7c 7c 20 70 45 78 70 72 ==TK_IS || pExpr
20712 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 20 29 ->op==TK_ISNOT )
20713 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 ;. assert( pExp
20714 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61 66 r->pLeft );. af
20715 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 f = sqlite3ExprA
20716 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 ffinity(pExpr->p
20717 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78 Left);. if( pEx
20718 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 pr->pRight ){.
20719 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43 aff = sqlite3C
2071a 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 ompareAffinity(p
2071b 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61 66 Expr->pRight, af
2071c 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 f);. }else if(
2071d 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
2071e 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c pExpr, EP_xIsSel
2071f 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 66 20 ect) ){. aff
20720 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 = sqlite3Compare
20721 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e Affinity(pExpr->
20722 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 x.pSelect->pELis
20723 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20 61 t->a[0].pExpr, a
20724 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 ff);. }else if(
20725 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66 66 !aff ){. aff
20726 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f = SQLITE_AFF_NO
20727 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e NE;. }. return
20728 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 aff;.}../*.** p
20729 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 Expr is a compar
2072a 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c ison expression,
2072b 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20 49 eg. '=', '<', I
2072c 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20 69 N(...) etc..** i
2072d 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20 74 dx_affinity is t
2072e 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 61 he affinity of a
2072f 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e n indexed column
20730 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a 2a . Return true.**
20731 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77 69 if the index wi
20732 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78 5f th affinity idx_
20733 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65 20 affinity may be
20734 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
20735 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72 69 t.** the compari
20736 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a 2f son in pExpr..*/
20737 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20738 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 int sqlite3Index
20739 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 AffinityOk(Expr
2073a 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 *pExpr, char idx
2073b 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63 68 _affinity){. ch
2073c 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 ar aff = compari
2073d 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 sonAffinity(pExp
2073e 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61 66 r);. switch( af
2073f 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 f ){. case SQ
20740 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 20 LITE_AFF_NONE:.
20741 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
20742 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 case SQLITE_A
20743 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 72 FF_TEXT:. r
20744 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e 69 eturn idx_affini
20745 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 ty==SQLITE_AFF_T
20746 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 EXT;. default
20747 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73 :. return s
20748 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41 qlite3IsNumericA
20749 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66 69 ffinity(idx_affi
2074a 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a nity);. }.}../*
2074b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 50 .** Return the P
2074c 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 5 value that sho
2074d 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 uld be used for
2074e 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 a binary compari
2074f 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28 4f son.** opcode (O
20750 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63 2e P_Eq, OP_Ge etc.
20751 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 ) used to compar
20752 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45 78 e pExpr1 and pEx
20753 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 pr2..*/.static u
20754 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 8 binaryCompareP
20755 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c 20 5(Expr *pExpr1,
20756 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69 6e Expr *pExpr2, in
20757 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 t jumpIfNull){.
20758 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72 29 u8 aff = (char)
20759 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
2075a 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20 61 ity(pExpr2);. a
2075b 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65 33 ff = (u8)sqlite3
2075c 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 CompareAffinity(
2075d 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20 28 pExpr1, aff) | (
2075e 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20 u8)jumpIfNull;.
2075f 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a return aff;.}..
20760 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 /*.** Return a p
20761 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f ointer to the co
20762 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 llation sequence
20763 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 that should be
20764 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e used by.** a bin
20765 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f ary comparison o
20766 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e perator comparin
20767 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 g pLeft and pRig
20768 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 ht..**.** If the
20769 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 left hand expre
2076a 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c ssion has a coll
2076b 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 ating sequence t
2076c 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a ype, then it is.
2076d 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 ** used. Otherwi
2076e 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e se the collation
2076f 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 sequence for th
20770 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 e right hand exp
20771 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 ression.** is us
20772 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 ed, or the defau
20773 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e lt (BINARY) if n
20774 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f either expressio
20775 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e n has a collatin
20776 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a g.** type..**.**
20777 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 Argument pRight
20778 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 (but not pLeft)
20779 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 may be a null p
2077a 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 ointer. In this
2077b 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e case,.** it is n
2077c 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a ot considered..*
2077d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
2077e 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 CollSeq *sqlite
2077f 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 3BinaryCompareCo
20780 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a llSeq(. Parse *
20781 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 pParse, . Expr
20782 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 *pLeft, . Expr
20783 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c *pRight.){. Col
20784 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 lSeq *pColl;. a
20785 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a ssert( pLeft );.
20786 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 if( pLeft->fla
20787 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 gs & EP_ExpColla
20788 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 te ){. assert
20789 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 ( pLeft->pColl )
2078a 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c ;. pColl = pL
2078b 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 eft->pColl;. }e
2078c 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26 lse if( pRight &
2078d 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 & pRight->flags
2078e 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 & EP_ExpCollate
2078f 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
20790 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a Right->pColl );.
20791 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 pColl = pRig
20792 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c ht->pColl;. }el
20793 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 se{. pColl =
20794 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
20795 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 eq(pParse, pLeft
20796 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c );. if( !pCol
20797 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c l ){. pColl
20798 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
20799 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 llSeq(pParse, pR
2079a 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d ight);. }. }
2079b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b . return pColl;
2079c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
2079d 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 te the operands
2079e 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e for a comparison
2079f 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66 operation. Bef
207a0 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e ore.** generatin
207a1 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65 g the code for e
207a2 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74 ach operand, set
207a3 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a the EP_AnyAff.*
207a4 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78 * flag on the ex
207a5 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 pression so that
207a6 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 it will be able
207a7 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61 to used a.** ca
207a8 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 ched column valu
207a9 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76 69 e that has previ
207aa 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20 ously undergone
207ab 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63 an.** affinity c
207ac 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 hange..*/.static
207ad 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72 void codeCompar
207ae 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72 eOperands(. Par
207af 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f se *pParse, /
207b0 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
207b1 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
207b2 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 ntext */. Expr
207b3 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 *pLeft, /*
207b4 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 The left operand
207b5 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c */. int *pRegL
207b6 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73 eft, /* Regis
207b7 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f ter where left o
207b8 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64 perand is stored
207b9 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 */. int *pFree
207ba 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20 Left, /* Free
207bb 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77 68 this register wh
207bc 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70 en done */. Exp
207bd 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f r *pRight, /
207be 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 * The right oper
207bf 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 and */. int *pR
207c0 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65 egRight, /* Re
207c1 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69 67 gister where rig
207c2 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 ht operand is st
207c3 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 ored */. int *p
207c4 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57 FreeRight /* W
207c5 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 74 rite temp regist
207c6 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65 er for right ope
207c7 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b rand there */.){
207c8 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d . while( pLeft-
207c9 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 >op==TK_UPLUS )
207ca 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 pLeft = pLeft->p
207cb 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66 Left;. pLeft->f
207cc 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66 lags |= EP_AnyAf
207cd 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d f;. *pRegLeft =
207ce 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
207cf 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 Temp(pParse, pLe
207d0 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a ft, pFreeLeft);.
207d1 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d while( pRight-
207d2 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 >op==TK_UPLUS )
207d3 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d pRight = pRight-
207d4 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74 >pLeft;. pRight
207d5 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e ->flags |= EP_An
207d6 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67 yAff;. *pRegRig
207d7 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ht = sqlite3Expr
207d8 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c CodeTemp(pParse,
207d9 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 69 pRight, pFreeRi
207da 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 ght);.}../*.** G
207db 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
207dc 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 a comparison op
207dd 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 erator..*/.stati
207de 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72 c int codeCompar
207df 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 e(. Parse *pPar
207e0 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 se, /* The pa
207e1 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20 rsing (and code
207e2 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74 generating) cont
207e3 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ext */. Expr *p
207e4 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 Left, /* Th
207e5 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a e left operand *
207e6 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 /. Expr *pRight
207e7 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 , /* The rig
207e8 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 ht operand */.
207e9 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20 int opcode,
207ea 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 /* The compari
207eb 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 son opcode */.
207ec 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32 int in1, int in2
207ed 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f , /* Register ho
207ee 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a lding operands *
207ef 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20 /. int dest,
207f0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
207f1 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a re if true. */.
207f2 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c int jumpIfNull
207f3 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 /* If true,
207f4 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f jump if either o
207f5 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a perand is NULL *
207f6 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20 /.){. int p5;.
207f7 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c int addr;. Col
207f8 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20 lSeq *p4;.. p4
207f9 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 = sqlite3BinaryC
207fa 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 ompareCollSeq(pP
207fb 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 arse, pLeft, pRi
207fc 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e ght);. p5 = bin
207fd 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65 aryCompareP5(pLe
207fe 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 ft, pRight, jump
207ff 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20 IfNull);. addr
20800 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
20801 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 Op4(pParse->pVdb
20802 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20 e, opcode, in2,
20803 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20 dest, in1,.
20804 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20805 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c (void*)p4,
20806 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 P4_COLLSEQ);.
20807 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
20808 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62 eP5(pParse->pVdb
20809 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 69 66 e, (u8)p5);. if
2080a 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 5f 41 ( (p5 & SQLITE_A
2080b 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54 45 FF_MASK)!=SQLITE
2080c 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 _AFF_NONE ){.
2080d 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
2080e 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 eAffinityChange(
2080f 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 29 3b pParse, in1, 1);
20810 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
20811 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 CacheAffinityCha
20812 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 32 2c nge(pParse, in2,
20813 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 1);. }. retur
20814 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20 53 n addr;.}..#if S
20815 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 QLITE_MAX_EXPR_D
20816 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68 65 EPTH>0./*.** Che
20817 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e 74 ck that argument
20818 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73 73 nHeight is less
20819 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 than or equal t
2081a 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a o the maximum.**
2081b 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74 expression dept
2081c 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69 74 h allowed. If it
2081d 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20 61 is not, leave a
2081e 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 n error message
2081f 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2f in.** pParse..*/
20820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
20821 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
20822 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73 65 heckHeight(Parse
20823 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e 48 *pParse, int nH
20824 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 eight){. int rc
20825 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 = SQLITE_OK;.
20826 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d 20 70 int mxHeight = p
20827 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69 Parse->db->aLimi
20828 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 t[SQLITE_LIMIT_E
20829 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69 66 XPR_DEPTH];. if
2082a 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 69 67 ( nHeight>mxHeig
2082b 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ht ){. sqlite
2082c 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
2082d 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 72 65 , . "Expre
2082e 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74 6f ssion tree is to
2082f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75 6d o large (maximum
20830 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d 78 48 depth %d)", mxH
20831 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 20 20 eight. );.
20832 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 rc = SQLITE_ERR
20833 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e OR;. }. return
20834 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 rc;.}../* The f
20835 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 66 ollowing three f
20836 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68 74 unctions, height
20837 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 68 74 OfExpr(), height
20838 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a 20 OfExprList().**
20839 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65 and heightOfSele
2083a 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20 74 ct(), are used t
2083b 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 o determine the
2083c 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0a 2a maximum height.*
2083d 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73 73 * of any express
2083e 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65 6e ion tree referen
2083f 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75 63 ced by the struc
20840 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 ture passed as t
20841 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 he.** first argu
20842 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 ment..**.** If t
20843 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 his maximum heig
20844 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 ht is greater th
20845 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 an the current v
20846 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 alue pointed.**
20847 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 2c 20 to by pnHeight,
20848 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d the second param
20849 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 20 2a eter, then set *
2084a 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 61 74 pnHeight to that
2084b 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 .** value..*/.st
2084c 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 atic void height
2084d 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 OfExpr(Expr *p,
2084e 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a int *pnHeight){.
2084f 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 if( p ){. i
20850 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a 70 f( p->nHeight>*p
20851 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20 20 nHeight ){.
20852 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 2d 3e *pnHeight = p->
20853 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 nHeight;. }.
20854 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 }.}.static void
20855 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 heightOfExprLis
20856 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 t(ExprList *p, i
20857 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 nt *pnHeight){.
20858 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 6e if( p ){. in
20859 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t i;. for(i=0
2085a 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b ; i<p->nExpr; i+
2085b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 68 74 +){. height
2085c 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e 70 OfExpr(p->a[i].p
2085d 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 29 3b Expr, pnHeight);
2085e 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 . }. }.}.sta
2085f 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f tic void heightO
20860 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20 2a fSelect(Select *
20861 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 p, int *pnHeight
20862 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 ){. if( p ){.
20863 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 heightOfExpr(p
20864 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 69 67 ->pWhere, pnHeig
20865 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f ht);. heightO
20866 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 fExpr(p->pHaving
20867 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 , pnHeight);.
20868 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d heightOfExpr(p-
20869 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67 68 >pLimit, pnHeigh
2086a 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
2086b 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 74 2c Expr(p->pOffset,
2086c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 pnHeight);.
2086d 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 heightOfExprList
2086e 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48 65 (p->pEList, pnHe
2086f 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 ight);. heigh
20870 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 tOfExprList(p->p
20871 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 67 68 GroupBy, pnHeigh
20872 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 t);. heightOf
20873 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 ExprList(p->pOrd
20874 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b erBy, pnHeight);
20875 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65 6c . heightOfSel
20876 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20 70 ect(p->pPrior, p
20877 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d 0a nHeight);. }.}.
20878 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 ./*.** Set the E
20879 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72 69 xpr.nHeight vari
2087a 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 72 75 able in the stru
2087b 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20 cture passed as
2087c 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e an .** argument.
2087d 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 An expression w
2087e 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2c ith no children,
2087f 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 20 0a Expr.pList or .
20880 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 74 20 ** Expr.pSelect
20881 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 65 69 member has a hei
20882 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f 74 ght of 1. Any ot
20883 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a her expression.*
20884 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 20 65 * has a height e
20885 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78 69 qual to the maxi
20886 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e mum height of an
20887 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66 65 y other .** refe
20888 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c 75 73 renced Expr plus
20889 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 one..*/.static
2088a 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 69 67 void exprSetHeig
2088b 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 ht(Expr *p){. i
2088c 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a nt nHeight = 0;.
2088d 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 heightOfExpr(p
2088e 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 67 68 ->pLeft, &nHeigh
2088f 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 t);. heightOfEx
20890 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 26 6e pr(p->pRight, &n
20891 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 20 45 Height);. if( E
20892 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20893 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
20894 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 ){. heightOfS
20895 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c 65 elect(p->x.pSele
20896 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 ct, &nHeight);.
20897 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 69 67 }else{. heig
20898 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e htOfExprList(p->
20899 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67 68 x.pList, &nHeigh
2089a 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48 65 t);. }. p->nHe
2089b 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 20 2b ight = nHeight +
2089c 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 1;.}../*.** Set
2089d 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67 68 the Expr.nHeigh
2089e 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 6e 67 t variable using
2089f 20 74 68 65 20 65 78 70 72 53 65 74 48 65 69 67 the exprSetHeig
208a0 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 49 ht() function. I
208a1 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 f.** the height
208a2 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 is greater than
208a3 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f the maximum allo
208a4 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 64 wed expression d
208a5 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 epth,.** leave a
208a6 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72 73 n error in pPars
208a7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
208a8 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 VATE void sqlite
208a9 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 50 3ExprSetHeight(P
208aa 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 arse *pParse, Ex
208ab 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 53 65 pr *p){. exprSe
208ac 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 73 71 tHeight(p);. sq
208ad 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65 lite3ExprCheckHe
208ae 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e ight(pParse, p->
208af 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a nHeight);.}../*.
208b0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 ** Return the ma
208b1 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 ximum height of
208b2 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 any expression t
208b3 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 0a 2a ree referenced.*
208b4 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 74 20 * by the select
208b5 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 statement passed
208b6 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e as an argument.
208b7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
208b8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 TE int sqlite3Se
208b9 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28 53 lectExprHeight(S
208ba 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 elect *p){. int
208bb 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 nHeight = 0;.
208bc 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 heightOfSelect(p
208bd 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 72 , &nHeight);. r
208be 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a 7d eturn nHeight;.}
208bf 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 .#else. #define
208c0 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 79 exprSetHeight(y
208c1 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 ).#endif /* SQLI
208c2 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 TE_MAX_EXPR_DEPT
208c3 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 H>0 */../*.** Th
208c4 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 is routine is th
208c5 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f 72 e core allocator
208c6 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73 2e for Expr nodes.
208c7 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 .**.** Construct
208c8 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f a new expressio
208c9 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75 72 n node and retur
208ca 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 n a pointer to i
208cb 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66 6f t. Memory.** fo
208cc 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e 64 20 r this node and
208cd 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e 20 61 for the pToken a
208ce 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 69 6e rgument is a sin
208cf 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a gle allocation.*
208d0 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 * obtained from
208d1 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 sqlite3DbMalloc(
208d2 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 ). The calling
208d3 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 72 function.** is r
208d4 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d esponsible for m
208d5 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 6e aking sure the n
208d6 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 67 ode eventually g
208d7 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a ets freed..**.**
208d8 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 20 74 If dequote is t
208d9 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 74 6f rue, then the to
208da 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 73 74 ken (if it exist
208db 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 2e 0a s) is dequoted..
208dc 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 ** If dequote is
208dd 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 75 6f false, no dequo
208de 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 6d 61 ting is performa
208df 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 6f 74 nce. The deQuot
208e0 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 e.** parameter i
208e1 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 54 6f s ignored if pTo
208e2 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69 ken is NULL or i
208e3 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f 65 73 f the token does
208e4 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 20 74 not.** appear t
208e5 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 49 66 o be quoted. If
208e6 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 72 65 the quotes were
208e7 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2e 2e of the form "..
208e8 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f 74 65 ." (double-quote
208e9 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 45 s).** then the E
208ea 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c 61 67 P_DblQuoted flag
208eb 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20 65 is set on the e
208ec 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e 0a xpression node..
208ed 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 63 61 **.** Special ca
208ee 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b 5f 49 se: If op==TK_I
208ef 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f 6b 65 NTEGER and pToke
208f0 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 n points to a st
208f1 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 61 6e ring that.** can
208f2 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 20 69 be translated i
208f3 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 6e 74 nto a 32-bit int
208f4 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 74 eger, then the t
208f5 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 oken is not.** s
208f6 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b 65 tored in u.zToke
208f7 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 68 65 n. Instead, the
208f8 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 integer values
208f9 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e is written.** in
208fa 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64 20 to u.iValue and
208fb 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 the EP_IntValue
208fc 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e 6f flag is set. No
208fd 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 0a 2a extra storage.*
208fe 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 74 * is allocated t
208ff 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65 67 o hold the integ
20900 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65 20 er text and the
20901 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73 20 dequote flag is
20902 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 ignored..*/.SQLI
20903 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 TE_PRIVATE Expr
20904 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f *sqlite3ExprAllo
20905 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 c(. sqlite3 *db
20906 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 , /*
20907 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 Handle for sqlit
20908 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 e3DbMallocZero()
20909 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a (may be null) *
2090a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 /. int op,
2090b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 /* E
2090c 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 xpression opcode
2090d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 */. const Toke
2090e 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 2f 2a n *pToken, /*
2090f 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e Token argument.
20910 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 Might be NULL
20911 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65 */. int dequote
20912 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
20913 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 65 20 True to dequote
20914 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e */.){. Expr *pN
20915 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 ew;. int nExtra
20916 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 61 6c = 0;. int iVal
20917 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 ue = 0;.. if( p
20918 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 Token ){. if(
20919 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 52 20 op!=TK_INTEGER
2091a 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30 0a || pToken->z==0.
2091b 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c || sql
2091c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 54 6f ite3GetInt32(pTo
2091d 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65 29 ken->z, &iValue)
2091e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78 ==0 ){. nEx
2091f 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e 2b tra = pToken->n+
20920 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 1;. }. }. p
20921 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d New = sqlite3DbM
20922 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 allocZero(db, si
20923 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74 72 zeof(Expr)+nExtr
20924 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29 a);. if( pNew )
20925 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20 3d {. pNew->op =
20926 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e 65 (u8)op;. pNe
20927 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20 20 w->iAgg = -1;.
20928 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a if( pToken ){.
20929 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 72 61 if( nExtra
2092a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ==0 ){. p
2092b 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 New->flags |= EP
2092c 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 20 _IntValue;.
2092d 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 pNew->u.iValu
2092e 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20 20 e = iValue;.
2092f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
20930 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20 int c;.
20931 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d pNew->u.zToken =
20932 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31 5d (char*)&pNew[1]
20933 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 ;. memcpy
20934 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c (pNew->u.zToken,
20935 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b pToken->z, pTok
20936 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 en->n);.
20937 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 70 pNew->u.zToken[p
20938 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 Token->n] = 0;.
20939 20 20 20 20 20 20 20 69 66 28 20 64 65 71 75 6f if( dequo
2093a 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33 20 te && nExtra>=3
2093b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 . &&
2093c 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e 7a ((c = pToken->z
2093d 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63 3d [0])=='\'' || c=
2093e 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20 7c ='"' || c=='[' |
2093f 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20 20 | c=='`') ){.
20940 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 sqlite3De
20941 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a 54 quote(pNew->u.zT
20942 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 oken);.
20943 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70 4e if( c=='"' ) pN
20944 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ew->flags |= EP_
20945 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20 20 DblQuoted;.
20946 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
20947 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 }.#if SQLITE_MA
20948 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 X_EXPR_DEPTH>0.
20949 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68 74 pNew->nHeight
2094a 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a 20 = 1;.#endif .
2094b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 }. return pNew
2094c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 ;.}../*.** Alloc
2094d 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 ate a new expres
2094e 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20 61 sion node from a
2094f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 zero-terminated
20950 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73 0a token that has.
20951 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 ** already been
20952 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51 4c dequoted..*/.SQL
20953 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 ITE_PRIVATE Expr
20954 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20 *sqlite3Expr(.
20955 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 sqlite3 *db,
20956 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 /* Hand
20957 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62 le for sqlite3Db
20958 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61 MallocZero() (ma
20959 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20 y be null) */.
2095a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 int op,
2095b 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
2095c 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a ssion opcode */.
2095d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 const char *zT
2095e 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f 6b oken /* Tok
2095f 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d 69 en argument. Mi
20960 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 ght be NULL */.)
20961 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20 78 {. Token x;. x
20962 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20 78 .z = zToken;. x
20963 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73 71 .n = zToken ? sq
20964 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 lite3Strlen30(zT
20965 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65 74 oken) : 0;. ret
20966 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72 41 urn sqlite3ExprA
20967 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78 2c lloc(db, op, &x,
20968 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 0);.}../*.** At
20969 74 61 63 68 20 73 75 62 74 72 65 65 73 20 70 4c tach subtrees pL
2096a 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20 74 eft and pRight t
2096b 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65 20 o the Expr node
2096c 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 pRoot..**.** If
2096d 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61 74 pRoot==NULL that
2096e 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 65 means that a me
2096f 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 mory allocation
20970 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 error has occurr
20971 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20 63 ed..** In that c
20972 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 ase, delete the
20973 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 20 61 subtrees pLeft a
20974 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53 51 nd pRight..*/.SQ
20975 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
20976 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 d sqlite3ExprAtt
20977 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 20 73 achSubtrees(. s
20978 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45 78 qlite3 *db,. Ex
20979 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78 70 pr *pRoot,. Exp
2097a 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70 72 r *pLeft,. Expr
2097b 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69 66 *pRight.){. if
2097c 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20 20 ( pRoot==0 ){.
2097d 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 assert( db->ma
2097e 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 llocFailed );.
2097f 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
20980 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b 0a ete(db, pLeft);.
20981 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 sqlite3ExprD
20982 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68 74 elete(db, pRight
20983 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 );. }else{.
20984 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20 20 if( pRight ){.
20985 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67 68 pRoot->pRigh
20986 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 t = pRight;.
20987 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66 6c if( pRight->fl
20988 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c ags & EP_ExpColl
20989 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 ate ){. p
2098a 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 Root->flags |= E
2098b 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 P_ExpCollate;.
2098c 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43 6f pRoot->pCo
2098d 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f ll = pRight->pCo
2098e 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ll;. }.
2098f 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20 }. if( pLeft
20990 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e ){. pRoot->
20991 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 pLeft = pLeft;.
20992 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e if( pLeft->
20993 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f flags & EP_ExpCo
20994 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 llate ){.
20995 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d pRoot->flags |=
20996 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a EP_ExpCollate;.
20997 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 pRoot->p
20998 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 Coll = pLeft->pC
20999 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 oll;. }.
2099a 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48 65 }. exprSetHe
2099b 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20 7d ight(pRoot);. }
2099c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 .}../*.** Alloca
2099d 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20 77 te a Expr node w
2099e 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d 61 hich joins as ma
2099f 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 72 65 ny as two subtre
209a0 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f 72 es..**.** One or
209a1 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75 62 both of the sub
209a2 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55 4c trees can be NUL
209a3 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 L. Return a poi
209a4 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a nter to the new.
209a5 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20 4f ** Expr node. O
209a6 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72 72 r, if an OOM err
209a7 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20 70 or occurs, set p
209a8 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
209a9 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65 65 cFailed,.** free
209aa 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61 6e the subtrees an
209ab 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a d return NULL..*
209ac 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
209ad 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45 Expr *sqlite3PE
209ae 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 xpr(. Parse *pP
209af 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f arse, /
209b0 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
209b1 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 t */. int op,
209b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
209b3 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 * Expression opc
209b4 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ode */. Expr *p
209b5 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20 20 Left,
209b6 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 /* Left operand
209b7 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 */. Expr *pRig
209b8 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a ht, /*
209b9 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a Right operand *
209ba 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 /. const Token
209bb 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20 41 *pToken /* A
209bc 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f rgument token */
209bd 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 .){. Expr *p =
209be 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 sqlite3ExprAlloc
209bf 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70 2c (pParse->db, op,
209c0 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20 73 pToken, 1);. s
209c1 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63 68 qlite3ExprAttach
209c2 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65 2d Subtrees(pParse-
209c3 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20 70 >db, p, pLeft, p
209c4 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72 6e Right);. return
209c5 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 69 p;.}../*.** Joi
209c6 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e n two expression
209c7 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20 6f s using an AND o
209c8 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69 74 perator. If eit
209c9 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 her expression i
209ca 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 s.** NULL, then
209cb 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 just return the
209cc 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e other expression
209cd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
209ce 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 ATE Expr *sqlite
209cf 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65 33 3ExprAnd(sqlite3
209d0 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65 66 *db, Expr *pLef
209d1 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74 29 t, Expr *pRight)
209d2 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d 30 {. if( pLeft==0
209d3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 ){. return p
209d4 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20 69 Right;. }else i
209d5 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b 0a f( pRight==0 ){.
209d6 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66 74 return pLeft
209d7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 45 ;. }else{. E
209d8 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 xpr *pNew = sqli
209d9 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c te3ExprAlloc(db,
209da 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 3b 0a TK_AND, 0, 0);.
209db 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 sqlite3ExprA
209dc 74 74 61 63 68 53 75 62 74 72 65 65 73 28 64 62 ttachSubtrees(db
209dd 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c 20 70 , pNew, pLeft, p
209de 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74 75 Right);. retu
209df 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a 0a rn pNew;. }.}..
209e0 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 /*.** Construct
209e1 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e a new expression
209e2 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e 63 node for a func
209e3 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69 70 tion with multip
209e4 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 2e le.** arguments.
209e5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 .*/.SQLITE_PRIVA
209e6 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 TE Expr *sqlite3
209e7 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72 ExprFunction(Par
209e8 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 se *pParse, Expr
209e9 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b List *pList, Tok
209ea 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 45 en *pToken){. E
209eb 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71 6c xpr *pNew;. sql
209ec 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
209ed 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 e->db;. assert(
209ee 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e 65 pToken );. pNe
209ef 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 w = sqlite3ExprA
209f0 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 4e 43 lloc(db, TK_FUNC
209f1 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 TION, pToken, 1)
209f2 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
209f3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 ){. sqlite3Ex
209f4 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
209f5 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69 pList); /* Avoi
209f6 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77 68 d memory leak wh
209f7 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 en malloc fails
209f8 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b */. return 0;
209f9 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 2e 70 . }. pNew->x.p
209fa 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 List = pList;.
209fb 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
209fc 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 Property(pNew, E
209fd 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a P_xIsSelect) );.
209fe 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 sqlite3ExprSet
209ff 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 Height(pParse, p
20a00 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 New);. return p
20a01 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 New;.}../*.** As
20a02 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65 20 sign a variable
20a03 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78 70 number to an exp
20a04 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e 63 ression that enc
20a05 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64 0a odes a wildcard.
20a06 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e ** in the origin
20a07 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 al SQL statement
20a08 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 . .**.** Wildca
20a09 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20 6f rds consisting o
20a0a 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20 61 f a single "?" a
20a0b 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 re assigned the
20a0c 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c 0a next sequential.
20a0d 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 ** variable numb
20a0e 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 er..**.** Wildca
20a0f 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 rds of the form
20a10 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69 67 "?nnn" are assig
20a11 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20 22 ned the number "
20a12 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a 2a nnn". We make.*
20a13 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73 20 * sure "nnn" is
20a14 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61 76 not too be to av
20a15 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 oid a denial of
20a16 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20 77 service attack w
20a17 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20 73 hen.** the SQL s
20a18 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20 66 tatement comes f
20a19 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 rom an external
20a1a 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 source..**.** Wi
20a1b 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20 66 ldcards of the f
20a1c 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61 61 orm ":aaa", "@aa
20a1d 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61 72 a", or "$aaa" ar
20a1e 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 73 e assigned the s
20a1f 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61 73 ame number.** as
20a20 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e the previous in
20a21 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 stance of the sa
20a22 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f 72 me wildcard. Or
20a23 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20 if this is the
20a24 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 first.** instanc
20a25 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61 72 e of the wildcar
20a26 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71 75 d, the next sequ
20a27 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20 6e enial variable n
20a28 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73 69 umber is.** assi
20a29 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f gned..*/.SQLITE_
20a2a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c PRIVATE void sql
20a2b 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 ite3ExprAssignVa
20a2c 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a 70 rNumber(Parse *p
20a2d 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 Parse, Expr *pEx
20a2e 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a pr){. sqlite3 *
20a2f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
20a30 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
20a31 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d ;.. if( pExpr==
20a32 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 0 ) return;. as
20a33 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e sert( !ExprHasAn
20a34 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c yProperty(pExpr,
20a35 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 50 5f EP_IntValue|EP_
20a36 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e Reduced|EP_Token
20a37 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d 20 70 Only) );. z = p
20a38 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a Expr->u.zToken;.
20a39 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20 29 assert( z!=0 )
20a3a 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d ;. assert( z[0]
20a3b 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 5b 31 !=0 );. if( z[1
20a3c 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 ]==0 ){. /* W
20a3d 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20 66 ildcard of the f
20a3e 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67 6e orm "?". Assign
20a3f 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61 62 the next variab
20a40 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 le number */.
20a41 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d 27 assert( z[0]=='
20a42 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d ?' );. pExpr-
20a43 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e 56 61 >iColumn = (ynVa
20a44 72 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 r)(++pParse->nVa
20a45 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 r);. }else if(
20a46 7a 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 z[0]=='?' ){.
20a47 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 /* Wildcard of
20a48 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e the form "?nnn".
20a49 20 20 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 Convert "nnn"
20a4a 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e to an integer an
20a4b 64 0a 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20 d. ** use it
20a4c 61 73 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 as the variable
20a4d 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e number */. in
20a4e 74 20 69 20 3d 20 61 74 6f 69 28 28 63 68 61 72 t i = atoi((char
20a4f 2a 29 26 7a 5b 31 5d 29 3b 0a 20 20 20 20 70 45 *)&z[1]);. pE
20a50 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 xpr->iColumn = (
20a51 79 6e 56 61 72 29 69 3b 0a 20 20 20 20 74 65 73 ynVar)i;. tes
20a52 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 tcase( i==0 );.
20a53 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d testcase( i==
20a54 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 1 );. testcas
20a55 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 e( i==db->aLimit
20a56 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 [SQLITE_LIMIT_VA
20a57 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 RIABLE_NUMBER]-1
20a58 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
20a59 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b ( i==db->aLimit[
20a5a 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 SQLITE_LIMIT_VAR
20a5b 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b IABLE_NUMBER] );
20a5c 0a 20 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 . if( i<1 ||
20a5d 69 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c i>db->aLimit[SQL
20a5e 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 ITE_LIMIT_VARIAB
20a5f 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 LE_NUMBER] ){.
20a60 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
20a61 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 Msg(pParse, "var
20a62 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 iable number mus
20a63 74 20 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 t be between ?1
20a64 61 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 and ?%d",.
20a65 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 db->aLimit[S
20a66 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 QLITE_LIMIT_VARI
20a67 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 ABLE_NUMBER]);.
20a68 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 }. if( i>p
20a69 50 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 Parse->nVar ){.
20a6a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 pParse->nVa
20a6b 72 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d r = i;. }. }
20a6c 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c else{. /* Wil
20a6d 64 63 61 72 64 73 20 6c 69 6b 65 20 22 3a 61 61 dcards like ":aa
20a6e 61 22 2c 20 22 24 61 61 61 22 20 6f 72 20 22 40 a", "$aaa" or "@
20a6f 61 61 61 22 2e 20 20 52 65 75 73 65 20 74 68 65 aaa". Reuse the
20a70 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 same variable.
20a71 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 ** number as
20a72 74 68 65 20 70 72 69 6f 72 20 61 70 70 65 61 72 the prior appear
20a73 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 ance of the same
20a74 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 name, or if the
20a75 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 name. ** has
20a76 20 6e 65 76 65 72 20 61 70 70 65 61 72 65 64 20 never appeared
20a77 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68 before, reuse th
20a78 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20 e same variable
20a79 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 number. */.
20a7a 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 75 33 32 int i;. u32
20a7b 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 n;. n = sqli
20a7c 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a te3Strlen30(z);.
20a7d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 for(i=0; i<p
20a7e 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b Parse->nVarExpr;
20a7f 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 i++){. Exp
20a80 72 20 2a 70 45 20 3d 20 70 50 61 72 73 65 2d 3e r *pE = pParse->
20a81 61 70 56 61 72 45 78 70 72 5b 69 5d 3b 0a 20 20 apVarExpr[i];.
20a82 20 20 20 20 61 73 73 65 72 74 28 20 70 45 21 3d assert( pE!=
20a83 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 0 );. if( m
20a84 65 6d 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 6f 6b emcmp(pE->u.zTok
20a85 65 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 26 20 en, z, n)==0 &&
20a86 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 5d 3d pE->u.zToken[n]=
20a87 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 =0 ){. pE
20a88 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 xpr->iColumn = p
20a89 45 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 E->iColumn;.
20a8a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
20a8b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 }. }. if(
20a8c 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 i>=pParse->nVar
20a8d 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 70 45 Expr ){. pE
20a8e 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 xpr->iColumn = (
20a8f 79 6e 56 61 72 29 28 2b 2b 70 50 61 72 73 65 2d ynVar)(++pParse-
20a90 3e 6e 56 61 72 29 3b 0a 20 20 20 20 20 20 69 66 >nVar);. if
20a91 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 ( pParse->nVarEx
20a92 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 pr>=pParse->nVar
20a93 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 ExprAlloc-1 ){.
20a94 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e pParse->n
20a95 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 VarExprAlloc +=
20a96 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 pParse->nVarExpr
20a97 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 Alloc + 10;.
20a98 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 pParse->apVa
20a99 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 rExpr =.
20a9a 20 20 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 sqlite3DbRea
20a9b 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20 llocOrFree(.
20a9c 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20 db,.
20a9d 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 pPar
20a9e 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 se->apVarExpr,.
20a9f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 pPa
20aa0 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c rse->nVarExprAll
20aa1 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 oc*sizeof(pParse
20aa2 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a ->apVarExpr[0]).
20aa3 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 );.
20aa4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
20aa5 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
20aa6 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ed ){. as
20aa7 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 sert( pParse->ap
20aa8 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 VarExpr!=0 );.
20aa9 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 pParse->ap
20aaa 56 61 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e VarExpr[pParse->
20aab 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45 nVarExpr++] = pE
20aac 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 xpr;. }.
20aad 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70 }. } . if( !p
20aae 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70 Parse->nErr && p
20aaf 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e Parse->nVar>db->
20ab0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 aLimit[SQLITE_LI
20ab1 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d MIT_VARIABLE_NUM
20ab2 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 BER] ){. sqli
20ab3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
20ab4 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 51 se, "too many SQ
20ab5 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b 0a 20 L variables");.
20ab6 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 }.}../*.** Clea
20ab7 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 r an expression
20ab8 73 74 72 75 63 74 75 72 65 20 77 69 74 68 6f 75 structure withou
20ab9 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 73 t deleting the s
20aba 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e tructure itself.
20abb 0a 2a 2a 20 53 75 62 73 74 72 75 63 74 75 72 65 .** Substructure
20abc 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a is deleted..*/.
20abd 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 SQLITE_PRIVATE v
20abe 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
20abf 6c 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 lear(sqlite3 *db
20ac0 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 61 73 , Expr *p){. as
20ac1 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
20ac2 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 if( !ExprHasAnyP
20ac3 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f roperty(p, EP_To
20ac4 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 kenOnly) ){.
20ac5 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
20ac6 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b e(db, p->pLeft);
20ac7 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 . sqlite3Expr
20ac8 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 Delete(db, p->pR
20ac9 69 67 68 74 29 3b 0a 20 20 20 20 69 66 28 20 21 ight);. if( !
20aca 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20acb 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 26 p, EP_Reduced) &
20acc 26 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 & (p->flags2 & E
20acd 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e P2_MallocedToken
20ace 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 )!=0 ){. sq
20acf 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
20ad0 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 p->u.zToken);.
20ad1 20 20 7d 0a 20 20 20 20 69 66 28 20 45 78 70 72 }. if( Expr
20ad2 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
20ad3 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a P_xIsSelect) ){.
20ad4 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c sqlite3Sel
20ad5 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d ectDelete(db, p-
20ad6 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 >x.pSelect);.
20ad7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
20ad8 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c lite3ExprListDel
20ad9 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 ete(db, p->x.pLi
20ada 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d st);. }. }.}
20adb 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 ../*.** Recursiv
20adc 65 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 ely delete an ex
20add 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a pression tree..*
20ade 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20adf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
20ae0 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 rDelete(sqlite3
20ae1 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 *db, Expr *p){.
20ae2 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 if( p==0 ) retu
20ae3 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 rn;. sqlite3Exp
20ae4 72 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 rClear(db, p);.
20ae5 20 69 66 28 20 21 45 78 70 72 48 61 73 50 72 6f if( !ExprHasPro
20ae6 70 65 72 74 79 28 70 2c 20 45 50 5f 53 74 61 74 perty(p, EP_Stat
20ae7 69 63 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ic) ){. sqlit
20ae8 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b e3DbFree(db, p);
20ae9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
20aea 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 turn the number
20aeb 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 of bytes allocat
20aec 65 64 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 ed for the expre
20aed 73 73 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 ssion structure
20aee 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 .** passed as th
20aef 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
20af0 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 . This is always
20af1 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c one of EXPR_FUL
20af2 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 LSIZE,.** EXPR_R
20af3 45 44 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 EDUCEDSIZE or EX
20af4 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 PR_TOKENONLYSIZE
20af5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
20af6 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 exprStructSize(E
20af7 78 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 xpr *p){. if( E
20af8 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
20af9 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 , EP_TokenOnly)
20afa 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f ) return EXPR_TO
20afb 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 KENONLYSIZE;. i
20afc 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
20afd 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 ty(p, EP_Reduced
20afe 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f ) ) return EXPR_
20aff 52 45 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 REDUCEDSIZE;. r
20b00 65 74 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 eturn EXPR_FULLS
20b01 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 IZE;.}../*.** Th
20b02 65 20 64 75 70 65 64 45 78 70 72 2a 53 69 7a 65 e dupedExpr*Size
20b03 28 29 20 72 6f 75 74 69 6e 65 73 20 65 61 63 68 () routines each
20b04 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 return the numb
20b05 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 er of bytes requ
20b06 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 ired.** to store
20b07 20 61 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78 a copy of an ex
20b08 70 72 65 73 73 69 6f 6e 20 6f 72 20 65 78 70 72 pression or expr
20b09 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 54 68 ession tree. Th
20b0a 65 79 20 64 69 66 66 65 72 20 69 6e 0a 2a 2a 20 ey differ in.**
20b0b 68 6f 77 20 6d 75 63 68 20 6f 66 20 74 68 65 20 how much of the
20b0c 74 72 65 65 20 69 73 20 6d 65 61 73 75 72 65 64 tree is measured
20b0d 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 70 65 ..**.** dupe
20b0e 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 dExprStructSize(
20b0f 29 20 20 20 20 20 53 69 7a 65 20 6f 66 20 6f 6e ) Size of on
20b10 6c 79 20 74 68 65 20 45 78 70 72 20 73 74 72 75 ly the Expr stru
20b11 63 74 75 72 65 20 0a 2a 2a 20 20 20 20 20 64 75 cture .** du
20b12 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 pedExprNodeSize(
20b13 29 20 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20 ) Size of
20b14 45 78 70 72 20 2b 20 73 70 61 63 65 20 66 6f 72 Expr + space for
20b15 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 64 75 token.** du
20b16 70 65 64 45 78 70 72 53 69 7a 65 28 29 20 20 20 pedExprSize()
20b17 20 20 20 20 20 20 20 20 45 78 70 72 20 2b 20 74 Expr + t
20b18 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63 oken + subtree c
20b19 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a omponents.**.***
20b1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
20b1e 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 ********.**.** T
20b1f 68 65 20 64 75 70 65 64 45 78 70 72 53 74 72 75 he dupedExprStru
20b20 63 74 53 69 7a 65 28 29 20 66 75 6e 63 74 69 6f ctSize() functio
20b21 6e 20 72 65 74 75 72 6e 73 20 74 77 6f 20 76 61 n returns two va
20b22 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f 67 65 74 lues OR-ed toget
20b23 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29 20 74 68 her: .** (1) th
20b24 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 e space required
20b25 20 66 6f 72 20 61 20 63 6f 70 79 20 6f 66 20 74 for a copy of t
20b26 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72 he Expr structur
20b27 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 e only and .** (
20b28 32 29 20 74 68 65 20 45 50 5f 78 78 78 20 66 6c 2) the EP_xxx fl
20b29 61 67 73 20 74 68 61 74 20 69 6e 64 69 63 61 74 ags that indicat
20b2a 65 20 77 68 61 74 20 74 68 65 20 73 74 72 75 63 e what the struc
20b2b 74 75 72 65 20 73 69 7a 65 20 73 68 6f 75 6c 64 ture size should
20b2c 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 be..** The retu
20b2d 72 6e 20 76 61 6c 75 65 73 20 69 73 20 61 6c 77 rn values is alw
20b2e 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a ays one of:.**.*
20b2f 2a 20 20 20 20 20 20 45 58 50 52 5f 46 55 4c 4c * EXPR_FULL
20b30 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20 45 58 50 SIZE.** EXP
20b31 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 R_REDUCEDSIZE
20b32 7c 20 45 50 5f 52 65 64 75 63 65 64 0a 2a 2a 20 | EP_Reduced.**
20b33 20 20 20 20 20 45 58 50 52 5f 54 4f 4b 45 4e 4f EXPR_TOKENO
20b34 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b NLYSIZE | EP_Tok
20b35 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 enOnly.**.** The
20b36 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 74 72 size of the str
20b37 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 66 6f ucture can be fo
20b38 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 und by masking t
20b39 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a he return value.
20b3a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 ** of this routi
20b3b 6e 65 20 77 69 74 68 20 30 78 66 66 66 2e 20 20 ne with 0xfff.
20b3c 54 68 65 20 66 6c 61 67 73 20 63 61 6e 20 62 65 The flags can be
20b3d 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e found by maskin
20b3e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 g the.** return
20b3f 76 61 6c 75 65 20 77 69 74 68 20 45 50 5f 52 65 value with EP_Re
20b40 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e duced|EP_TokenOn
20b41 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 ly..**.** Note t
20b42 68 61 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d hat with flags==
20b43 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 EXPRDUP_REDUCE,
20b44 74 68 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f this routines wo
20b45 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 rks on full-size
20b46 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20 .** (unreduced)
20b47 45 78 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20 Expr objects as
20b48 74 68 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c they or original
20b49 6c 79 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 ly constructed b
20b4a 79 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a y the parser..**
20b4b 20 44 75 72 69 6e 67 20 65 78 70 72 65 73 73 69 During expressi
20b4c 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20 65 78 74 on analysis, ext
20b4d 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 ra information i
20b4e 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 6d s computed and m
20b4f 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 oved into.** lat
20b50 65 72 20 70 61 72 74 73 20 6f 66 20 74 65 68 20 er parts of teh
20b51 45 78 70 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 Expr object and
20b52 74 68 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 that extra infor
20b53 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20 67 65 74 mation might get
20b54 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 chopped.** off
20b55 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f if the expressio
20b56 6e 20 69 73 20 72 65 64 75 63 65 64 2e 20 20 4e n is reduced. N
20b57 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 69 74 ote also that it
20b58 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 74 does not work t
20b59 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45 58 50 52 o.** make a EXPR
20b5a 44 55 50 5f 52 45 44 55 43 45 20 63 6f 70 79 20 DUP_REDUCE copy
20b5b 6f 66 20 61 20 72 65 64 75 63 65 64 20 65 78 70 of a reduced exp
20b5c 72 65 73 73 69 6f 6e 2e 20 20 49 74 20 69 73 20 ression. It is
20b5d 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f only legal.** to
20b5e 20 72 65 64 75 63 65 20 61 20 70 72 69 73 74 69 reduce a pristi
20b5f 6e 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 ne expression tr
20b60 65 65 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 ee from the pars
20b61 65 72 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 er. The impleme
20b62 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 ntation.** of du
20b63 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a pedExprStructSiz
20b64 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 e() contain mult
20b65 69 70 6c 65 20 61 73 73 65 72 74 28 29 20 73 74 iple assert() st
20b66 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 61 74 atements that at
20b67 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f tempt.** to enfo
20b68 72 63 65 20 74 68 69 73 20 63 6f 6e 73 74 72 61 rce this constra
20b69 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 int..*/.static i
20b6a 6e 74 20 64 75 70 65 64 45 78 70 72 53 74 72 75 nt dupedExprStru
20b6b 63 74 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 ctSize(Expr *p,
20b6c 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e int flags){. in
20b6d 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 t nSize;. asser
20b6e 74 28 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 t( flags==EXPRDU
20b6f 50 5f 52 45 44 55 43 45 20 7c 7c 20 66 6c 61 67 P_REDUCE || flag
20b70 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 s==0 ); /* Only
20b71 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75 65 20 61 one flag value a
20b72 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69 66 28 20 llowed */. if(
20b73 30 3d 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 0==(flags&EXPRDU
20b74 50 5f 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 P_REDUCE) ){.
20b75 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 nSize = EXPR_FU
20b76 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b LLSIZE;. }else{
20b77 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 . assert( !Ex
20b78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
20b79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 (p, EP_TokenOnly
20b7a 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a |EP_Reduced) );.
20b7b 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 assert( !Exp
20b7c 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 rHasProperty(p,
20b7d 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 EP_FromJoin) );
20b7e 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d . assert( (p-
20b7f 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 >flags2 & EP2_Ma
20b80 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 llocedToken)==0
20b81 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
20b82 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f p->flags2 & EP2_
20b83 49 72 72 65 64 75 63 69 62 6c 65 29 3d 3d 30 20 Irreducible)==0
20b84 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c );. if( p->pL
20b85 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 eft || p->pRight
20b86 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 || p->pColl ||
20b87 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 p->x.pList ){.
20b88 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 nSize = EXPR
20b89 5f 52 45 44 55 43 45 44 53 49 5a 45 20 7c 20 45 _REDUCEDSIZE | E
20b8a 50 5f 52 65 64 75 63 65 64 3b 0a 20 20 20 20 7d P_Reduced;. }
20b8b 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a else{. nSiz
20b8c 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e e = EXPR_TOKENON
20b8d 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 LYSIZE | EP_Toke
20b8e 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 20 7d nOnly;. }. }
20b8f 0a 20 20 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b . return nSize;
20b90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 .}../*.** This f
20b91 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 unction returns
20b92 74 68 65 20 73 70 61 63 65 20 69 6e 20 62 79 74 the space in byt
20b93 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 es required to s
20b94 74 6f 72 65 20 74 68 65 20 63 6f 70 79 20 0a 2a tore the copy .*
20b95 2a 20 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 * of the Expr st
20b96 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f ructure and a co
20b97 70 79 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75 py of the Expr.u
20b98 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 .zToken string (
20b99 69 66 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e if that.** strin
20b9a 67 20 69 73 20 64 65 66 69 6e 65 64 2e 29 0a 2a g is defined.).*
20b9b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 /.static int dup
20b9c 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 edExprNodeSize(E
20b9d 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 xpr *p, int flag
20b9e 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 s){. int nByte
20b9f 3d 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 = dupedExprStruc
20ba0 74 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 tSize(p, flags)
20ba1 26 20 30 78 66 66 66 3b 0a 20 20 69 66 28 20 21 & 0xfff;. if( !
20ba2 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 ExprHasProperty(
20ba3 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 p, EP_IntValue)
20ba4 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 && p->u.zToken )
20ba5 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 {. nByte += s
20ba6 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 qlite3Strlen30(p
20ba7 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 ->u.zToken)+1;.
20ba8 20 7d 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e }. return ROUN
20ba9 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a D8(nByte);.}../*
20baa 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e .** Return the n
20bab 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 umber of bytes r
20bac 65 71 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 equired to creat
20bad 65 20 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 e a duplicate of
20bae 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 the .** express
20baf 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 ion passed as th
20bb0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 e first argument
20bb1 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 . The second arg
20bb2 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 ument is a.** ma
20bb3 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 sk containing EX
20bb4 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e PRDUP_XXX flags.
20bb5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 .**.** The value
20bb6 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 returned includ
20bb7 65 73 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 es space to crea
20bb8 74 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 te a copy of the
20bb9 20 45 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 Expr struct.**
20bba 69 74 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 itself and the b
20bbb 75 66 66 65 72 20 72 65 66 65 72 72 65 64 20 74 uffer referred t
20bbc 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b o by Expr.u.zTok
20bbd 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a en, if any..**.*
20bbe 2a 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 * If the EXPRDUP
20bbf 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 _REDUCE flag is
20bc0 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 set, then the re
20bc1 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 turn value inclu
20bc2 64 65 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f des .** space to
20bc3 20 64 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 duplicate all E
20bc4 78 70 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 xpr nodes in the
20bc5 20 74 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 tree formed by
20bc6 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 Expr.pLeft .** a
20bc7 6e 64 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 nd Expr.pRight v
20bc8 61 72 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f ariables (but no
20bc9 74 20 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 t for any struct
20bca 75 72 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 ures pointed to
20bcb 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 or .** descended
20bcc 20 66 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 from the Expr.x
20bcd 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 .pList or Expr.x
20bce 2e 70 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c .pSelect variabl
20bcf 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 es)..*/.static i
20bd0 6e 74 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 nt dupedExprSize
20bd1 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c (Expr *p, int fl
20bd2 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 ags){. int nByt
20bd3 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 e = 0;. if( p )
20bd4 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 {. nByte = du
20bd5 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 pedExprNodeSize(
20bd6 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 p, flags);. i
20bd7 66 28 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 f( flags&EXPRDUP
20bd8 5f 52 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 _REDUCE ){.
20bd9 20 6e 42 79 74 65 20 2b 3d 20 64 75 70 65 64 45 nByte += dupedE
20bda 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 xprSize(p->pLeft
20bdb 2c 20 66 6c 61 67 73 29 20 2b 20 64 75 70 65 64 , flags) + duped
20bdc 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 67 ExprSize(p->pRig
20bdd 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 ht, flags);.
20bde 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e }. }. return n
20bdf 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 Byte;.}../*.** T
20be0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 his function is
20be1 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 similar to sqlit
20be2 65 33 45 78 70 72 44 75 70 28 29 2c 20 65 78 63 e3ExprDup(), exc
20be3 65 70 74 20 74 68 61 74 20 69 66 20 70 7a 42 75 ept that if pzBu
20be4 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 ffer .** is not
20be5 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 NULL then *pzBuf
20be6 66 65 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 fer is assumed t
20be7 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 o point to a buf
20be8 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 fer large enough
20be9 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 .** to store th
20bea 65 20 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 e copy of expres
20beb 73 69 6f 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 sion p, the copi
20bec 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 es of p->u.zToke
20bed 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 n.** (if applica
20bee 62 6c 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f ble), and the co
20bef 70 69 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 pies of the p->p
20bf0 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 Left and p->pRig
20bf1 68 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a ht expressions,.
20bf2 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 ** if any. Befor
20bf3 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a e returning, *pz
20bf4 42 75 66 66 65 72 20 69 73 20 73 65 74 20 74 6f Buffer is set to
20bf5 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20 the first byte
20bf6 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f passed the.** po
20bf7 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 rtion of the buf
20bf8 66 65 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 fer copied into
20bf9 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e by this function
20bfa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 ..*/.static Expr
20bfb 20 2a 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 *exprDup(sqlite
20bfc 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 3 *db, Expr *p,
20bfd 69 6e 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a int flags, u8 **
20bfe 70 7a 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 pzBuffer){. Exp
20bff 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 r *pNew = 0;
20c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20c01 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 /* Value to re
20c02 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 turn */. if( p
20c03 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 ){. const int
20c04 20 69 73 52 65 64 75 63 65 64 20 3d 20 28 66 6c isReduced = (fl
20c05 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 ags&EXPRDUP_REDU
20c06 43 45 29 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c CE);. u8 *zAl
20c07 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20 73 74 61 loc;. u32 sta
20c08 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 ticFlag = 0;..
20c09 20 20 61 73 73 65 72 74 28 20 70 7a 42 75 66 66 assert( pzBuff
20c0a 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63 er==0 || isReduc
20c0b 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 ed );.. /* Fi
20c0c 67 75 72 65 20 6f 75 74 20 77 68 65 72 65 20 74 gure out where t
20c0d 6f 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 o write the new
20c0e 45 78 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 Expr structure.
20c0f 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a 42 75 66 */. if( pzBuf
20c10 66 65 72 20 29 7b 0a 20 20 20 20 20 20 7a 41 6c fer ){. zAl
20c11 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66 65 72 3b loc = *pzBuffer;
20c12 0a 20 20 20 20 20 20 73 74 61 74 69 63 46 6c 61 . staticFla
20c13 67 20 3d 20 45 50 5f 53 74 61 74 69 63 3b 0a 20 g = EP_Static;.
20c14 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
20c15 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 zAlloc = sqlite3
20c16 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
20c17 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 70 2c dupedExprSize(p,
20c18 20 66 6c 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a flags));. }.
20c19 20 20 20 20 70 4e 65 77 20 3d 20 28 45 78 70 72 pNew = (Expr
20c1a 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 *)zAlloc;..
20c1b 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 if( pNew ){.
20c1c 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a /* Set nNewSiz
20c1d 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 61 6c e to the size al
20c1e 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 located for the
20c1f 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 structure pointe
20c20 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 d to. ** by
20c21 20 70 4e 65 77 2e 20 54 68 69 73 20 69 73 20 65 pNew. This is e
20c22 69 74 68 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 ither EXPR_FULLS
20c23 49 5a 45 2c 20 45 58 50 52 5f 52 45 44 55 43 45 IZE, EXPR_REDUCE
20c24 44 53 49 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a DSIZE or. *
20c25 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 * EXPR_TOKENONLY
20c26 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 SIZE. nToken is
20c27 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 set to the numbe
20c28 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 r of bytes consu
20c29 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 med. ** by
20c2a 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 the copy of the
20c2b 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 p->u.zToken stri
20c2c 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 ng (if any)..
20c2d 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 */. cons
20c2e 74 20 75 6e 73 69 67 6e 65 64 20 6e 53 74 72 75 t unsigned nStru
20c2f 63 74 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 ctSize = dupedEx
20c30 70 72 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 prStructSize(p,
20c31 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f flags);. co
20c32 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53 69 7a 65 nst int nNewSize
20c33 20 3d 20 6e 53 74 72 75 63 74 53 69 7a 65 20 26 = nStructSize &
20c34 20 30 78 66 66 66 3b 0a 20 20 20 20 20 20 69 6e 0xfff;. in
20c35 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 t nToken;.
20c36 69 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 if( !ExprHasProp
20c37 65 72 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 erty(p, EP_IntVa
20c38 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f lue) && p->u.zTo
20c39 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 6e ken ){. n
20c3a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 Token = sqlite3S
20c3b 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f trlen30(p->u.zTo
20c3c 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 ken) + 1;.
20c3d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e }else{. n
20c3e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 Token = 0;.
20c3f 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 }. if( isR
20c40 65 64 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 educed ){.
20c41 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 assert( ExprHa
20c42 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f sProperty(p, EP_
20c43 52 65 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 Reduced)==0 );.
20c44 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 memcpy(zA
20c45 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a lloc, p, nNewSiz
20c46 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b e);. }else{
20c47 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 . int nSi
20c48 7a 65 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 ze = exprStructS
20c49 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 ize(p);.
20c4a 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 memcpy(zAlloc, p
20c4b 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 , nSize);.
20c4c 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 memset(&zAlloc
20c4d 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 [nSize], 0, EXPR
20c4e 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 _FULLSIZE-nSize)
20c4f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 ;. }..
20c50 20 2f 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 /* Set the EP_R
20c51 65 64 75 63 65 64 2c 20 45 50 5f 54 6f 6b 65 6e educed, EP_Token
20c52 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 74 61 Only, and EP_Sta
20c53 74 69 63 20 66 6c 61 67 73 20 61 70 70 72 6f 70 tic flags approp
20c54 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 riately. */.
20c55 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 3d pNew->flags &=
20c56 20 7e 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 ~(EP_Reduced|EP
20c57 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 _TokenOnly|EP_St
20c58 61 74 69 63 29 3b 0a 20 20 20 20 20 20 70 4e 65 atic);. pNe
20c59 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 74 72 w->flags |= nStr
20c5a 75 63 74 53 69 7a 65 20 26 20 28 45 50 5f 52 65 uctSize & (EP_Re
20c5b 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e duced|EP_TokenOn
20c5c 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d ly);. pNew-
20c5d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61 74 69 63 >flags |= static
20c5e 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 Flag;.. /*
20c5f 43 6f 70 79 20 74 68 65 20 70 2d 3e 75 2e 7a 54 Copy the p->u.zT
20c60 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 20 oken string, if
20c61 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 any. */. if
20c62 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 ( nToken ){.
20c63 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e char *zToken
20c64 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 = pNew->u.zToke
20c65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a 41 6c 6c n = (char*)&zAll
20c66 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 oc[nNewSize];.
20c67 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 54 6f memcpy(zTo
20c68 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e ken, p->u.zToken
20c69 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 , nToken);.
20c6a 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d }.. if( 0=
20c6b 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 =((p->flags|pNew
20c6c 2d 3e 66 6c 61 67 73 29 20 26 20 45 50 5f 54 6f ->flags) & EP_To
20c6d 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 kenOnly) ){.
20c6e 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 /* Fill in t
20c6f 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 he pNew->x.pSele
20c70 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c ct or pNew->x.pL
20c71 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 ist member. */.
20c72 20 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 if( ExprH
20c73 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 asProperty(p, EP
20c74 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
20c75 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 pNew->x
20c76 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 .pSelect = sqlit
20c77 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 e3SelectDup(db,
20c78 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 p->x.pSelect, is
20c79 52 65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 Reduced);.
20c7a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
20c7b 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 pNew->x.pList
20c7c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 = sqlite3ExprLi
20c7d 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 stDup(db, p->x.p
20c7e 4c 69 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 List, isReduced)
20c7f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
20c80 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 }.. /* Fi
20c81 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 ll in pNew->pLef
20c82 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 t and pNew->pRig
20c83 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 ht. */. if(
20c84 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 ExprHasAnyPrope
20c85 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 rty(pNew, EP_Red
20c86 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c uced|EP_TokenOnl
20c87 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 y) ){. zA
20c88 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64 45 78 70 lloc += dupedExp
20c89 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 rNodeSize(p, fla
20c8a 67 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 gs);. if(
20c8b 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
20c8c 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 (pNew, EP_Reduce
20c8d 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 d) ){.
20c8e 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20 65 78 pNew->pLeft = ex
20c8f 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 prDup(db, p->pLe
20c90 66 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 ft, EXPRDUP_REDU
20c91 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 CE, &zAlloc);.
20c92 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 pNew->pR
20c93 69 67 68 74 20 3d 20 65 78 70 72 44 75 70 28 64 ight = exprDup(d
20c94 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 45 58 b, p->pRight, EX
20c95 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a PRDUP_REDUCE, &z
20c96 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 Alloc);.
20c97 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 7a }. if( pz
20c98 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 Buffer ){.
20c99 20 20 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20 *pzBuffer =
20c9a 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 zAlloc;.
20c9b 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
20c9c 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 pNew->fla
20c9d 67 73 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 gs2 = 0;.
20c9e 20 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 if( !ExprHasAny
20c9f 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 Property(p, EP_T
20ca0 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 okenOnly) ){.
20ca1 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 pNew->pLe
20ca2 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 ft = sqlite3Expr
20ca3 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 Dup(db, p->pLeft
20ca4 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 , 0);.
20ca5 70 4e 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 pNew->pRight = s
20ca6 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 qlite3ExprDup(db
20ca7 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b , p->pRight, 0);
20ca8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
20ca9 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }.. }. }.
20caa 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a return pNew;.}..
20cab 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 /*.** The follow
20cac 69 6e 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 ing group of rou
20cad 74 69 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 tines make deep
20cae 63 6f 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 copies of expres
20caf 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 sions,.** expres
20cb0 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c sion lists, ID l
20cb1 69 73 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 ists, and select
20cb2 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 statements. Th
20cb3 65 20 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 e copies can.**
20cb4 62 65 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 be deleted (by b
20cb5 65 69 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 eing passed to t
20cb6 68 65 69 72 20 72 65 73 70 65 63 74 69 76 65 20 heir respective
20cb7 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 ...Delete() rout
20cb8 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 ines).** without
20cb9 20 65 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f effecting the o
20cba 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 riginals..**.**
20cbb 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c The expression l
20cbc 69 73 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 ist, ID, and sou
20cbd 72 63 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e rce lists return
20cbe 20 62 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c by sqlite3ExprL
20cbf 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c istDup(),.** sql
20cc0 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c ite3IdListDup(),
20cc1 20 61 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c and sqlite3SrcL
20cc2 69 73 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 istDup() can not
20cc3 20 62 65 20 66 75 72 74 68 65 72 20 65 78 70 61 be further expa
20cc4 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 nded .** by subs
20cc5 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 equent calls to
20cc6 73 71 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e sqlite*ListAppen
20cc7 64 28 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a d() routines..**
20cc8 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 .** Any tables t
20cc9 68 61 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 hat the SrcList
20cca 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 might point to a
20ccb 72 65 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 re not duplicate
20ccc 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 d..**.** The fla
20ccd 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e gs parameter con
20cce 74 61 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 tains a combinat
20ccf 69 6f 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 ion of the EXPRD
20cd0 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a UP_XXX flags..**
20cd1 20 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f If the EXPRDUP_
20cd2 52 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 REDUCE flag is s
20cd3 65 74 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 et, then the str
20cd4 75 63 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 ucture returned
20cd5 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65 is a.** truncate
20cd6 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 d version of the
20cd7 20 75 73 75 61 6c 20 45 78 70 72 20 73 74 72 75 usual Expr stru
20cd8 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 cture that will
20cd9 62 65 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 be stored as.**
20cda 70 61 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d part of the in-m
20cdb 65 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 emory representa
20cdc 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 tion of the data
20cdd 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a base schema..*/.
20cde 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 SQLITE_PRIVATE E
20cdf 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 xpr *sqlite3Expr
20ce0 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c Dup(sqlite3 *db,
20ce1 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c Expr *p, int fl
20ce2 61 67 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 ags){. return e
20ce3 78 70 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c xprDup(db, p, fl
20ce4 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 ags, 0);.}.SQLIT
20ce5 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 E_PRIVATE ExprLi
20ce6 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c st *sqlite3ExprL
20ce7 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a istDup(sqlite3 *
20ce8 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c db, ExprList *p,
20ce9 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 int flags){. E
20cea 78 70 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 xprList *pNew;.
20ceb 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
20cec 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 _item *pItem, *p
20ced 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 OldItem;. int i
20cee 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 ;. if( p==0 ) r
20cef 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 eturn 0;. pNew
20cf0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
20cf1 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
20cf2 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 *pNew) );. if(
20cf3 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e pNew==0 ) return
20cf4 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 0;. pNew->iECu
20cf5 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 rsor = 0;. pNew
20cf6 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e ->nExpr = pNew->
20cf7 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 nAlloc = p->nExp
20cf8 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 r;. pNew->a = p
20cf9 49 74 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 Item = sqlite3Db
20cfa 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 MallocRaw(db, p
20cfb 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 ->nExpr*sizeof(p
20cfc 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 ->a[0]) );. if(
20cfd 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 pItem==0 ){.
20cfe 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20cff 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 b, pNew);. re
20d00 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 turn 0;. } . p
20d01 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a OldItem = p->a;.
20d02 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
20d03 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 nExpr; i++, pIte
20d04 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 m++, pOldItem++)
20d05 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 {. Expr *pOld
20d06 45 78 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d Expr = pOldItem-
20d07 3e 70 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 >pExpr;. pIte
20d08 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 m->pExpr = sqlit
20d09 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f e3ExprDup(db, pO
20d0a 6c 64 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a ldExpr, flags);.
20d0b 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pItem->zName
20d0c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 = sqlite3DbStrD
20d0d 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d up(db, pOldItem-
20d0e 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 >zName);. pIt
20d0f 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 em->zSpan = sqli
20d10 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 te3DbStrDup(db,
20d11 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 pOldItem->zSpan)
20d12 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 ;. pItem->sor
20d13 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 tOrder = pOldIte
20d14 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 m->sortOrder;.
20d15 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 pItem->done =
20d16 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 0;. pItem->iC
20d17 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 ol = pOldItem->i
20d18 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e Col;. pItem->
20d19 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 iAlias = pOldIte
20d1a 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 m->iAlias;. }.
20d1b 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a return pNew;.}.
20d1c 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 ./*.** If cursor
20d1d 73 2c 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 s, triggers, vie
20d1e 77 73 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 ws and subquerie
20d1f 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 s are all omitte
20d20 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 d from.** the bu
20d21 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f ild, then none o
20d22 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
20d23 72 6f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 routines, except
20d24 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 for .** sqlite3
20d25 53 65 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e SelectDup(), can
20d26 20 62 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 be called. sqli
20d27 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 te3SelectDup() i
20d28 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 s sometimes.** c
20d29 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c alled with a NUL
20d2a 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 L argument..*/.#
20d2b 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
20d2c 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c TE_OMIT_VIEW) ||
20d2d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
20d2e 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c _OMIT_TRIGGER) \
20d2f 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 . || !defined(SQ
20d30 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
20d31 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 RY).SQLITE_PRIVA
20d32 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 TE SrcList *sqli
20d33 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 te3SrcListDup(sq
20d34 6c 69 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 lite3 *db, SrcLi
20d35 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 st *p, int flags
20d36 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e ){. SrcList *pN
20d37 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 ew;. int i;. i
20d38 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 nt nByte;. if(
20d39 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b p==0 ) return 0;
20d3a 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f . nByte = sizeo
20d3b 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63 f(*p) + (p->nSrc
20d3c 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61 >0 ? sizeof(p->a
20d3d 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d [0]) * (p->nSrc-
20d3e 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20 1) : 0);. pNew
20d3f 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
20d40 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29 cRaw(db, nByte )
20d41 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 ;. if( pNew==0
20d42 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
20d43 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d ew->nSrc = pNew-
20d44 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 >nAlloc = p->nSr
20d45 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c c;. for(i=0; i<
20d46 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 p->nSrc; i++){.
20d47 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 struct SrcLis
20d48 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d t_item *pNewItem
20d49 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a = &pNew->a[i];.
20d4a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
20d4b 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 st_item *pOldIte
20d4c 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 m = &p->a[i];.
20d4d 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 Table *pTab;.
20d4e 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 pNewItem->zDa
20d4f 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 tabase = sqlite3
20d50 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c DbStrDup(db, pOl
20d51 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 dItem->zDatabase
20d52 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d );. pNewItem-
20d53 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 >zName = sqlite3
20d54 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c DbStrDup(db, pOl
20d55 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 dItem->zName);.
20d56 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c pNewItem->zAl
20d57 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53 ias = sqlite3DbS
20d58 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 trDup(db, pOldIt
20d59 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 em->zAlias);.
20d5a 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 pNewItem->joint
20d5b 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e ype = pOldItem->
20d5c 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 4e jointype;. pN
20d5d 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 ewItem->iCursor
20d5e 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 72 = pOldItem->iCur
20d5f 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 sor;. pNewIte
20d60 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d m->isPopulated =
20d61 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 pOldItem->isPop
20d62 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77 ulated;. pNew
20d63 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 Item->zIndex = s
20d64 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 qlite3DbStrDup(d
20d65 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e b, pOldItem->zIn
20d66 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 dex);. pNewIt
20d67 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d em->notIndexed =
20d68 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e pOldItem->notIn
20d69 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 dexed;. pNewI
20d6a 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f tem->pIndex = pO
20d6b 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a ldItem->pIndex;.
20d6c 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 pTab = pNewI
20d6d 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 tem->pTab = pOld
20d6e 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 Item->pTab;.
20d6f 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 if( pTab ){.
20d70 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a pTab->nRef++;.
20d71 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74 }. pNewIt
20d72 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 em->pSelect = sq
20d73 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 lite3SelectDup(d
20d74 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 b, pOldItem->pSe
20d75 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 lect, flags);.
20d76 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 pNewItem->pOn
20d77 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
20d78 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 (db, pOldItem->p
20d79 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 On, flags);.
20d7a 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67 pNewItem->pUsing
20d7b 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 = sqlite3IdList
20d7c 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d Dup(db, pOldItem
20d7d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70 ->pUsing);. p
20d7e 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 NewItem->colUsed
20d7f 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c = pOldItem->col
20d80 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 Used;. }. retu
20d81 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 rn pNew;.}.SQLIT
20d82 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 E_PRIVATE IdList
20d83 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 *sqlite3IdListD
20d84 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 up(sqlite3 *db,
20d85 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64 IdList *p){. Id
20d86 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e List *pNew;. in
20d87 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 t i;. if( p==0
20d88 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e ) return 0;. pN
20d89 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ew = sqlite3DbMa
20d8a 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 llocRaw(db, size
20d8b 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 of(*pNew) );. i
20d8c 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 f( pNew==0 ) ret
20d8d 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e urn 0;. pNew->n
20d8e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f Id = pNew->nAllo
20d8f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e c = p->nId;. pN
20d90 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 ew->a = sqlite3D
20d91 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 bMallocRaw(db, p
20d92 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e ->nId*sizeof(p->
20d93 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 a[0]) );. if( p
20d94 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 New->a==0 ){.
20d95 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
20d96 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 b, pNew);. re
20d97 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f turn 0;. }. fo
20d98 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b r(i=0; i<p->nId;
20d99 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 i++){. struc
20d9a 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 t IdList_item *p
20d9b 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d NewItem = &pNew-
20d9c 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 >a[i];. struc
20d9d 74 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 t IdList_item *p
20d9e 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b OldItem = &p->a[
20d9f 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d i];. pNewItem
20da0 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 ->zName = sqlite
20da1 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 3DbStrDup(db, pO
20da2 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a ldItem->zName);.
20da3 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 64 pNewItem->id
20da4 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 x = pOldItem->id
20da5 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 x;. }. return
20da6 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 pNew;.}.SQLITE_P
20da7 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 RIVATE Select *s
20da8 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 qlite3SelectDup(
20da9 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c sqlite3 *db, Sel
20daa 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 ect *p, int flag
20dab 73 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e s){. Select *pN
20dac 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 ew;. if( p==0 )
20dad 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 return 0;. pNe
20dae 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c w = sqlite3DbMal
20daf 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f locRaw(db, sizeo
20db0 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 f(*p) );. if( p
20db1 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 New==0 ) return
20db2 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 0;. pNew->pELis
20db3 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c t = sqlite3ExprL
20db4 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 istDup(db, p->pE
20db5 4c 69 73 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 List, flags);.
20db6 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c pNew->pSrc = sql
20db7 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 ite3SrcListDup(d
20db8 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 b, p->pSrc, flag
20db9 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 s);. pNew->pWhe
20dba 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 re = sqlite3Expr
20dbb 44 75 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 Dup(db, p->pWher
20dbc 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 e, flags);. pNe
20dbd 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 w->pGroupBy = sq
20dbe 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 lite3ExprListDup
20dbf 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 (db, p->pGroupBy
20dc0 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 , flags);. pNew
20dc1 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 ->pHaving = sqli
20dc2 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
20dc3 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 ->pHaving, flags
20dc4 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 );. pNew->pOrde
20dc5 72 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 rBy = sqlite3Exp
20dc6 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e rListDup(db, p->
20dc7 70 4f 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 pOrderBy, flags)
20dc8 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 ;. pNew->op = p
20dc9 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 ->op;. pNew->pP
20dca 72 69 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 rior = sqlite3Se
20dcb 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 lectDup(db, p->p
20dcc 50 72 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 Prior, flags);.
20dcd 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 pNew->pLimit =
20dce 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
20dcf 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c b, p->pLimit, fl
20dd0 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f ags);. pNew->pO
20dd1 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 ffset = sqlite3E
20dd2 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f xprDup(db, p->pO
20dd3 66 66 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 ffset, flags);.
20dd4 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 pNew->iLimit =
20dd5 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 0;. pNew->iOffs
20dd6 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e et = 0;. pNew->
20dd7 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 selFlags = p->se
20dd8 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 lFlags & ~SF_Use
20dd9 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e sEphemeral;. pN
20dda 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d ew->pRightmost =
20ddb 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 0;. pNew->addr
20ddc 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 OpenEphm[0] = -1
20ddd 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 ;. pNew->addrOp
20dde 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a enEphm[1] = -1;.
20ddf 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e pNew->addrOpen
20de0 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 Ephm[2] = -1;.
20de1 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 return pNew;.}.#
20de2 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 else.SQLITE_PRIV
20de3 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 ATE Select *sqli
20de4 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c te3SelectDup(sql
20de5 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 ite3 *db, Select
20de6 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b *p, int flags){
20de7 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 . assert( p==0
20de8 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
20de9 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
20dea 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e Add a new elemen
20deb 74 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 t to the end of
20dec 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 an expression li
20ded 73 74 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 st. If pList is
20dee 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 .** initially NU
20def 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 LL, then create
20df0 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e a new expression
20df1 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 list..**.** If
20df2 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 a memory allocat
20df3 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 ion error occurs
20df4 2c 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 , the entire lis
20df5 74 20 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a t is freed and.*
20df6 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e * NULL is return
20df7 65 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c ed. If non-NULL
20df8 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 is returned, th
20df9 65 6e 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 en it is guarant
20dfa 65 65 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 eed.** that the
20dfb 6e 65 77 20 65 6e 74 72 79 20 77 61 73 20 73 75 new entry was su
20dfc 63 63 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e ccessfully appen
20dfd 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 ded..*/.SQLITE_P
20dfe 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 RIVATE ExprList
20dff 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 *sqlite3ExprList
20e00 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 Append(. Parse
20e01 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
20e02 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
20e03 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 text */. ExprLi
20e04 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 st *pList,
20e05 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 /* List to whi
20e06 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 ch to append. Mi
20e07 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 ght be NULL */.
20e08 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 Expr *pExpr
20e09 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 /* Expr
20e0a 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 ession to be app
20e0b 65 6e 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20 ended. Might be
20e0c 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c NULL */.){. sql
20e0d 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
20e0e 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 e->db;. if( pLi
20e0f 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 st==0 ){. pLi
20e10 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 st = sqlite3DbMa
20e11 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
20e12 65 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b eof(ExprList) );
20e13 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d . if( pList==
20e14 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 0 ){. goto
20e15 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 no_mem;. }.
20e16 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
20e17 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 >nAlloc==0 );.
20e18 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e }. if( pList->n
20e19 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 Alloc<=pList->nE
20e1a 78 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 xpr ){. struc
20e1b 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 t ExprList_item
20e1c 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 *a;. int n =
20e1d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 pList->nAlloc*2
20e1e 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c + 4;. a = sql
20e1f 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 ite3DbRealloc(db
20e20 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 , pList->a, n*si
20e21 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d zeof(pList->a[0]
20e22 29 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 ));. if( a==0
20e23 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e ){. goto n
20e24 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 o_mem;. }.
20e25 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 pList->a = a;.
20e26 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 pList->nAlloc
20e27 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
20e28 6f 63 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69 ocSize(db, a)/si
20e29 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a zeof(a[0]);. }.
20e2a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d assert( pList-
20e2b 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 31 >a!=0 );. if( 1
20e2c 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 ){. struct E
20e2d 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
20e2e 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b tem = &pList->a[
20e2f 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b pList->nExpr++];
20e30 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 49 74 65 . memset(pIte
20e31 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 m, 0, sizeof(*pI
20e32 74 65 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 6d tem));. pItem
20e33 2d 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b ->pExpr = pExpr;
20e34 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c . }. return pL
20e35 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 ist;..no_mem:
20e36 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 . /* Avoid le
20e37 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 aking memory if
20e38 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 malloc has faile
20e39 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 d. */. sqlite3E
20e3a 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 xprDelete(db, pE
20e3b 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 xpr);. sqlite3E
20e3c 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 xprListDelete(db
20e3d 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 , pList);. retu
20e3e 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 rn 0;.}../*.** S
20e3f 65 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e et the ExprList.
20e40 61 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e a[].zName elemen
20e41 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 t of the most re
20e42 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 cently added ite
20e43 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 m.** on the expr
20e44 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a ession list..**.
20e45 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 ** pList might b
20e46 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 e NULL following
20e47 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 an OOM error.
20e48 42 75 74 20 70 4e 61 6d 65 20 73 68 6f 75 6c 64 But pName should
20e49 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c never be.** NUL
20e4a 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 L. If a memory
20e4b 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 allocation fails
20e4c 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 , the pParse->db
20e4d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 ->mallocFailed f
20e4e 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a lag.** is set..*
20e4f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
20e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
20e51 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 0a 20 20 rListSetName(.
20e52 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
20e53 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
20e54 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
20e55 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c ExprList *pList,
20e56 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
20e57 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 to which to add
20e58 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 the span. */. T
20e59 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20 oken *pName,
20e5a 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 74 /* Name t
20e5b 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 o be added */.
20e5c 69 6e 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 int dequote
20e5d 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
20e5e 74 6f 20 63 61 75 73 65 20 74 68 65 20 6e 61 6d to cause the nam
20e5f 65 20 74 6f 20 62 65 20 64 65 71 75 6f 74 65 64 e to be dequoted
20e60 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 */.){. assert(
20e61 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 70 50 61 pList!=0 || pPa
20e62 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rse->db->mallocF
20e63 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 ailed!=0 );. if
20e64 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 ( pList ){. s
20e65 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 truct ExprList_i
20e66 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 tem *pItem;.
20e67 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e assert( pList->n
20e68 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 70 49 Expr>0 );. pI
20e69 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b tem = &pList->a[
20e6a 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b pList->nExpr-1];
20e6b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 . assert( pIt
20e6c 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a em->zName==0 );.
20e6d 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 pItem->zName
20e6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e = sqlite3DbStrN
20e6f 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 Dup(pParse->db,
20e70 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d pName->z, pName-
20e71 3e 6e 29 3b 0a 20 20 20 20 69 66 28 20 64 65 71 >n);. if( deq
20e72 75 6f 74 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a uote && pItem->z
20e73 4e 61 6d 65 20 29 20 73 71 6c 69 74 65 33 44 65 Name ) sqlite3De
20e74 71 75 6f 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 quote(pItem->zNa
20e75 6d 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a me);. }.}../*.*
20e76 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 * Set the ExprLi
20e77 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 st.a[].zSpan ele
20e78 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 ment of the most
20e79 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 recently added
20e7a 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 item.** on the e
20e7b 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a xpression list..
20e7c 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 **.** pList migh
20e7d 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 t be NULL follow
20e7e 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 ing an OOM error
20e7f 2e 20 20 42 75 74 20 70 53 70 61 6e 20 73 68 6f . But pSpan sho
20e80 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 uld never be.**
20e81 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f NULL. If a memo
20e82 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 ry allocation fa
20e83 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d ils, the pParse-
20e84 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 >db->mallocFaile
20e85 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 d flag.** is set
20e86 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
20e87 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
20e88 45 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 ExprListSetSpan(
20e89 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
20e8a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 , /* Pa
20e8b 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
20e8c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 . ExprList *pLi
20e8d 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 st, /* Li
20e8e 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 st to which to a
20e8f 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a dd the span. */.
20e90 20 20 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 ExprSpan *pSpa
20e91 6e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 n /* The
20e92 20 73 70 61 6e 20 74 6f 20 62 65 20 61 64 64 65 span to be adde
20e93 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 d */.){. sqlite
20e94 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
20e95 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c db;. assert( pL
20e96 69 73 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 ist!=0 || db->ma
20e97 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b llocFailed!=0 );
20e98 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a . if( pList ){.
20e99 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c struct ExprL
20e9a 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 ist_item *pItem
20e9b 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 = &pList->a[pLis
20e9c 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 t->nExpr-1];.
20e9d 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e assert( pList->
20e9e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61 nExpr>0 );. a
20e9f 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f ssert( db->mallo
20ea0 63 46 61 69 6c 65 64 20 7c 7c 20 70 49 74 65 6d cFailed || pItem
20ea1 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61 6e 2d 3e ->pExpr==pSpan->
20ea2 70 45 78 70 72 20 29 3b 0a 20 20 20 20 73 71 6c pExpr );. sql
20ea3 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20ea4 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 Item->zSpan);.
20ea5 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d pItem->zSpan =
20ea6 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 sqlite3DbStrNDu
20ea7 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70 p(db, (char*)pSp
20ea8 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20 an->zStart,.
20ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20eaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20eab 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e (int)(pSpan->zEn
20eac 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 d - pSpan->zStar
20ead 74 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a t));. }.}../*.*
20eae 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
20eaf 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 ion list pEList
20eb0 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 contains more th
20eb1 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e an iLimit elemen
20eb2 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 ts,.** leave an
20eb3 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e error message in
20eb4 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 pParse..*/.SQLI
20eb5 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
20eb6 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 sqlite3ExprListC
20eb7 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 heckLength(. Pa
20eb8 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 rse *pParse,. E
20eb9 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c xprList *pEList,
20eba 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
20ebb 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 Object.){. int
20ebc 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d mx = pParse->db-
20ebd 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c >aLimit[SQLITE_L
20ebe 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 IMIT_COLUMN];.
20ebf 74 65 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 testcase( pEList
20ec0 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 && pEList->nExp
20ec1 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 r==mx );. testc
20ec2 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 ase( pEList && p
20ec3 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 EList->nExpr==mx
20ec4 2b 31 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 +1 );. if( pELi
20ec5 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 st && pEList->nE
20ec6 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 xpr>mx ){. sq
20ec7 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
20ec8 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 arse, "too many
20ec9 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 columns in %s",
20eca 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a zObject);. }.}.
20ecb 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e ./*.** Delete an
20ecc 20 65 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 entire expressi
20ecd 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 on list..*/.SQLI
20ece 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
20ecf 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 sqlite3ExprListD
20ed0 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 elete(sqlite3 *d
20ed1 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 b, ExprList *pLi
20ed2 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 st){. int i;.
20ed3 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
20ed4 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 item *pItem;. i
20ed5 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 f( pList==0 ) re
20ed6 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 turn;. assert(
20ed7 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 pList->a!=0 || (
20ed8 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 pList->nExpr==0
20ed9 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 && pList->nAlloc
20eda 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 ==0) );. assert
20edb 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d ( pList->nExpr<=
20edc 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b pList->nAlloc );
20edd 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 . for(pItem=pLi
20ede 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c st->a, i=0; i<pL
20edf 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c ist->nExpr; i++,
20ee0 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 pItem++){. s
20ee1 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 qlite3ExprDelete
20ee2 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 (db, pItem->pExp
20ee3 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 r);. sqlite3D
20ee4 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d bFree(db, pItem-
20ee5 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c >zName);. sql
20ee6 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
20ee7 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 Item->zSpan);.
20ee8 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 }. sqlite3DbFre
20ee9 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b e(db, pList->a);
20eea 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
20eeb 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a (db, pList);.}..
20eec 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 /*.** These rout
20eed 69 6e 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 ines are Walker
20eee 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b callbacks. Walk
20eef 65 72 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 er.u.pi is a poi
20ef0 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e nter.** to an in
20ef1 74 65 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f teger. These ro
20ef2 75 74 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b utines are check
20ef3 69 6e 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f ing an expressio
20ef4 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69 n to see.** if i
20ef5 74 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e t is a constant.
20ef6 20 20 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e Set *Walker.u.
20ef7 70 69 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 pi to 0 if the e
20ef8 78 70 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 xpression is.**
20ef9 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a not constant..**
20efa 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61 .** These callba
20efb 63 6b 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 ck routines are
20efc 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e used to implemen
20efd 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a t the following:
20efe 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 .**.** sqlit
20eff 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 e3ExprIsConstant
20f00 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 ().** sqlite
20f01 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 3ExprIsConstantN
20f02 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20 otJoin().**
20f03 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e sqlite3ExprIsCon
20f04 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 stantOrFunction(
20f05 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ).**.*/.static i
20f06 6e 74 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e nt exprNodeIsCon
20f07 73 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 stant(Walker *pW
20f08 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 alker, Expr *pEx
20f09 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57 pr){.. /* If pW
20f0a 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20 alker->u.i is 3
20f0b 74 68 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 then any term of
20f0c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
20f0d 74 68 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a that comes from.
20f0e 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 ** the ON or U
20f0f 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 SING clauses of
20f10 61 20 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 a join disqualif
20f11 69 65 73 20 74 68 65 20 65 78 70 72 65 73 73 69 ies the expressi
20f12 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 on. ** from bei
20f13 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f ng considered co
20f14 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 nstant. */. if(
20f15 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 pWalker->u.i==3
20f16 20 26 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 && ExprHasAnyPr
20f17 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
20f18 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 _FromJoin) ){.
20f19 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d pWalker->u.i =
20f1a 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 0;. return W
20f1b 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 RC_Abort;. }..
20f1c 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e switch( pExpr->
20f1d 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e op ){. /* Con
20f1e 73 69 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 sider functions
20f1f 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 to be constant i
20f20 66 20 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75 f all their argu
20f21 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61 ments are consta
20f22 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57 nt. ** and pW
20f23 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f alker->u.i==2 */
20f24 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e . case TK_FUN
20f25 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 CTION:. if(
20f26 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 pWalker->u.i==2
20f27 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 ) return 0;.
20f28 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 /* Fall throu
20f29 67 68 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 gh */. case T
20f2a 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 K_ID:. case T
20f2b 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 K_COLUMN:. ca
20f2c 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 se TK_AGG_FUNCTI
20f2d 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ON:. case TK_
20f2e 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 AGG_COLUMN:.
20f2f 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 testcase( pExp
20f30 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a r->op==TK_ID );.
20f31 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
20f32 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f pExpr->op==TK_CO
20f33 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 LUMN );. te
20f34 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f stcase( pExpr->o
20f35 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 p==TK_AGG_FUNCTI
20f36 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ON );. test
20f37 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d case( pExpr->op=
20f38 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 =TK_AGG_COLUMN )
20f39 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d ;. pWalker-
20f3a 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 >u.i = 0;.
20f3b 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 return WRC_Abort
20f3c 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
20f3d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
20f3e 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c Expr->op==TK_SEL
20f3f 45 43 54 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 ECT ); /* select
20f40 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 NodeIsConstant w
20f41 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a ill disallow */.
20f42 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
20f43 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 pExpr->op==TK_EX
20f44 49 53 54 53 20 29 3b 20 2f 2a 20 73 65 6c 65 63 ISTS ); /* selec
20f45 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 tNodeIsConstant
20f46 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f will disallow */
20f47 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 . return WR
20f48 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a C_Continue;. }.
20f49 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c }.static int sel
20f4a 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e ectNodeIsConstan
20f4b 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 t(Walker *pWalke
20f4c 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 r, Select *NotUs
20f4d 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 ed){. UNUSED_PA
20f4e 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 RAMETER(NotUsed)
20f4f 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 ;. pWalker->u.i
20f50 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 = 0;. return W
20f51 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 RC_Abort;.}.stat
20f52 69 63 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e ic int exprIsCon
20f53 73 74 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 st(Expr *p, int
20f54 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c initFlag){. Wal
20f55 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d ker w;. w.u.i =
20f56 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 initFlag;. w.x
20f57 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 ExprCallback = e
20f58 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e xprNodeIsConstan
20f59 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 t;. w.xSelectCa
20f5a 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e llback = selectN
20f5b 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 odeIsConstant;.
20f5c 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 sqlite3WalkExpr
20f5d 28 26 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 (&w, p);. retur
20f5e 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a n w.u.i;.}../*.*
20f5f 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 * Walk an expres
20f60 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 sion tree. Retu
20f61 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 rn 1 if the expr
20f62 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 ession is consta
20f63 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 nt.** and 0 if i
20f64 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 t involves varia
20f65 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e bles or function
20f66 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f calls..**.** Fo
20f67 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f r the purposes o
20f68 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c f this function,
20f69 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 a double-quoted
20f6a 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 string (ex: "ab
20f6b 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 c").** is consid
20f6c 65 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 ered a variable
20f6d 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f but a single-quo
20f6e 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 ted string (ex:
20f6f 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 'abc') is.** a c
20f70 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 onstant..*/.SQLI
20f71 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 TE_PRIVATE int s
20f72 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 qlite3ExprIsCons
20f73 74 61 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 tant(Expr *p){.
20f74 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f return exprIsCo
20f75 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a nst(p, 1);.}../*
20f76 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 .** Walk an expr
20f77 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 ession tree. Re
20f78 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 turn 1 if the ex
20f79 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 pression is cons
20f7a 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 tant.** that doe
20f7b 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 s no originate f
20f7c 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 rom the ON or US
20f7d 49 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 ING clauses of a
20f7e 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e join..** Return
20f7f 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 0 if it involve
20f80 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 s variables or f
20f81 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 unction calls or
20f82 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 terms from.** a
20f83 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c n ON or USING cl
20f84 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f ause..*/.SQLITE_
20f85 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
20f86 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
20f87 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 tNotJoin(Expr *p
20f88 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 ){. return expr
20f89 49 73 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d IsConst(p, 3);.}
20f8a 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 ../*.** Walk an
20f8b 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e expression tree.
20f8c 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 Return 1 if th
20f8d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 e expression is
20f8e 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 constant.** or a
20f8f 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 function call w
20f90 69 74 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 ith constant arg
20f91 75 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 uments. Return
20f92 61 6e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a and 0 if there.*
20f93 2a 20 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 * are any variab
20f94 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 les..**.** For t
20f95 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 he purposes of t
20f96 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 his function, a
20f97 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 double-quoted st
20f98 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 ring (ex: "abc")
20f99 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 .** is considere
20f9a 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 d a variable but
20f9b 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 a single-quoted
20f9c 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 string (ex: 'ab
20f9d 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 c') is.** a cons
20f9e 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f tant..*/.SQLITE_
20f9f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 PRIVATE int sqli
20fa0 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e te3ExprIsConstan
20fa1 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 tOrFunction(Expr
20fa2 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 *p){. return e
20fa3 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 xprIsConst(p, 2)
20fa4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 ;.}../*.** If th
20fa5 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 e expression p c
20fa6 6f 64 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 odes a constant
20fa7 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20 integer that is
20fa8 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 small enough.**
20fa9 74 6f 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 to fit in a 32-b
20faa 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 it integer, retu
20fab 72 6e 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 rn 1 and put the
20fac 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e value of the in
20fad 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 teger.** in *pVa
20fae 6c 75 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 lue. If the exp
20faf 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 ression is not a
20fb0 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 n integer or if
20fb1 69 74 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a it is too big.**
20fb2 20 74 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 to fit in a sig
20fb3 6e 65 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 ned 32-bit integ
20fb4 65 72 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 er, return 0 and
20fb5 20 6c 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 leave *pValue u
20fb6 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c nchanged..*/.SQL
20fb7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
20fb8 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 sqlite3ExprIsInt
20fb9 65 67 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e eger(Expr *p, in
20fba 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e t *pValue){. in
20fbb 74 20 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 t rc = 0;. if(
20fbc 70 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e p->flags & EP_In
20fbd 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 tValue ){. *p
20fbe 56 61 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 56 61 Value = p->u.iVa
20fbf 6c 75 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 lue;. return
20fc0 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 1;. }. switch(
20fc1 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 p->op ){. ca
20fc2 73 65 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b se TK_INTEGER: {
20fc3 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
20fc4 74 65 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 te3GetInt32(p->u
20fc5 2e 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 65 29 .zToken, pValue)
20fc6 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
20fc7 72 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 rc==0 );. b
20fc8 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
20fc9 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b case TK_UPLUS: {
20fca 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 . rc = sqli
20fcb 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
20fcc 28 70 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 (p->pLeft, pValu
20fcd 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b e);. break;
20fce 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
20fcf 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 TK_UMINUS: {.
20fd0 20 20 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 int v;.
20fd1 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 if( sqlite3ExprI
20fd2 73 49 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 sInteger(p->pLef
20fd3 74 2c 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 t, &v) ){.
20fd4 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a *pValue = -v;.
20fd5 20 20 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a rc = 1;.
20fd6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
20fd7 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 eak;. }. d
20fd8 65 66 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 efault: break;.
20fd9 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 }. if( rc ){.
20fda 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48 assert( ExprH
20fdb 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c asAnyProperty(p,
20fdc 20 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 EP_Reduced|EP_T
20fdd 6f 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 20 20 okenOnly).
20fde 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 2d 3e || (p->
20fdf 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c flags2 & EP2_Mal
20fe0 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 locedToken)==0 )
20fe1 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b ;. p->op = TK
20fe2 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 2d _INTEGER;. p-
20fe3 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 >flags |= EP_Int
20fe4 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d 3e 75 2e Value;. p->u.
20fe5 69 56 61 6c 75 65 20 3d 20 2a 70 56 61 6c 75 65 iValue = *pValue
20fe6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
20fe7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 c;.}../*.** Retu
20fe8 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 72 rn FALSE if ther
20fe9 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 e is no chance t
20fea 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69 hat the expressi
20feb 6f 6e 20 63 61 6e 20 62 65 20 4e 55 4c 4c 2e 0a on can be NULL..
20fec 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 **.** If the exp
20fed 72 65 73 73 69 6f 6e 20 6d 69 67 68 74 20 62 65 ression might be
20fee 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 NULL or if the
20fef 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 6f expression is to
20ff0 6f 20 63 6f 6d 70 6c 65 78 0a 2a 2a 20 74 6f 20 o complex.** to
20ff1 74 65 6c 6c 20 72 65 74 75 72 6e 20 54 52 55 45 tell return TRUE
20ff2 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 . .**.** This r
20ff3 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 61 outine is used a
20ff4 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
20ff5 6e 2c 20 74 6f 20 73 6b 69 70 20 4f 50 5f 49 73 n, to skip OP_Is
20ff6 4e 75 6c 6c 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 Null opcodes.**
20ff7 77 68 65 6e 20 77 65 20 6b 6e 6f 77 20 74 68 61 when we know tha
20ff8 74 20 61 20 76 61 6c 75 65 20 63 61 6e 6e 6f 74 t a value cannot
20ff9 20 62 65 20 4e 55 4c 4c 2e 20 20 48 65 6e 63 65 be NULL. Hence
20ffa 2c 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69 , a false positi
20ffb 76 65 0a 2a 2a 20 28 72 65 74 75 72 6e 69 6e 67 ve.** (returning
20ffc 20 54 52 55 45 20 77 68 65 6e 20 69 6e 20 66 61 TRUE when in fa
20ffd 63 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f ct the expressio
20ffe 6e 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 4e n can never be N
20fff 55 4c 4c 29 20 6d 69 67 68 74 0a 2a 2a 20 62 65 ULL) might.** be
21000 20 61 20 73 6d 61 6c 6c 20 70 65 72 66 6f 72 6d a small perform
21001 61 6e 63 65 20 68 69 74 20 62 75 74 20 69 73 20 ance hit but is
21002 6f 74 68 65 72 77 69 73 65 20 68 61 72 6d 6c 65 otherwise harmle
21003 73 73 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 65 ss. On the othe
21004 72 0a 2a 2a 20 68 61 6e 64 2c 20 61 20 66 61 6c r.** hand, a fal
21005 73 65 20 6e 65 67 61 74 69 76 65 20 28 72 65 74 se negative (ret
21006 75 72 6e 69 6e 67 20 46 41 4c 53 45 20 77 68 65 urning FALSE whe
21007 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 75 n the result cou
21008 6c 64 20 62 65 20 4e 55 4c 4c 29 0a 2a 2a 20 77 ld be NULL).** w
21009 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 73 75 6c ill likely resul
2100a 74 20 69 6e 20 61 6e 20 69 6e 63 6f 72 72 65 63 t in an incorrec
2100b 74 20 61 6e 73 77 65 72 2e 20 20 53 6f 20 77 68 t answer. So wh
2100c 65 6e 20 69 6e 20 64 6f 75 62 74 2c 20 72 65 74 en in doubt, ret
2100d 75 72 6e 0a 2a 2a 20 54 52 55 45 2e 0a 2a 2f 0a urn.** TRUE..*/.
2100e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 SQLITE_PRIVATE i
2100f 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 nt sqlite3ExprCa
21010 6e 42 65 4e 75 6c 6c 28 63 6f 6e 73 74 20 45 78 nBeNull(const Ex
21011 70 72 20 2a 70 29 7b 0a 20 20 75 38 20 6f 70 3b pr *p){. u8 op;
21012 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d . while( p->op=
21013 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 2d 3e =TK_UPLUS || p->
21014 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 7b op==TK_UMINUS ){
21015 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 20 7d p = p->pLeft; }
21016 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 . op = p->op;.
21017 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 if( op==TK_REGI
21018 53 54 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f STER ) op = p->o
21019 70 32 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 p2;. switch( op
2101a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
2101b 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 INTEGER:. cas
2101c 65 20 54 4b 5f 53 54 52 49 4e 47 3a 0a 20 20 20 e TK_STRING:.
2101d 20 63 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a case TK_FLOAT:.
2101e 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 case TK_BLOB
2101f 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 :. return 0
21020 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 ;. default:.
21021 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
21022 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 }.}../*.** Gene
21023 72 61 74 65 20 61 6e 20 4f 50 5f 49 73 4e 75 6c rate an OP_IsNul
21024 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 l instruction th
21025 61 74 20 74 65 73 74 73 20 72 65 67 69 73 74 65 at tests registe
21026 72 20 69 52 65 67 20 61 6e 64 20 6a 75 6d 70 73 r iReg and jumps
21027 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 .** to location
21028 69 44 65 73 74 20 69 66 20 74 68 65 20 76 61 6c iDest if the val
21029 75 65 20 69 6e 20 69 52 65 67 20 69 73 20 4e 55 ue in iReg is NU
2102a 4c 4c 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 LL. The value i
2102b 6e 20 69 52 65 67 20 0a 2a 2a 20 77 61 73 20 63 n iReg .** was c
2102c 6f 6d 70 75 74 65 64 20 62 79 20 70 45 78 70 72 omputed by pExpr
2102d 2e 20 20 49 66 20 77 65 20 63 61 6e 20 6c 6f 6f . If we can loo
2102e 6b 20 61 74 20 70 45 78 70 72 20 61 74 20 63 6f k at pExpr at co
2102f 6d 70 69 6c 65 2d 74 69 6d 65 20 61 6e 64 0a 2a mpile-time and.*
21030 2a 20 64 65 74 65 72 6d 69 6e 65 20 74 68 61 74 * determine that
21031 20 69 74 20 63 61 6e 20 6e 65 76 65 72 20 67 65 it can never ge
21032 6e 65 72 61 74 65 20 61 20 4e 55 4c 4c 2c 20 74 nerate a NULL, t
21033 68 65 6e 20 74 68 65 20 4f 50 5f 49 73 4e 75 6c hen the OP_IsNul
21034 6c 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 63 l operation.** c
21035 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 0a 2a an be omitted..*
21036 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 /.SQLITE_PRIVATE
21037 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 void sqlite3Exp
21038 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 rCodeIsNullJump(
21039 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 . Vdbe *v,
2103a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 /* The VD
2103b 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 BE under constru
2103c 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 ction */. const
2103d 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 2f Expr *pExpr, /
2103e 2a 20 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 20 * Only generate
2103f 4f 50 5f 49 73 4e 75 6c 6c 20 69 66 20 74 68 69 OP_IsNull if thi
21040 73 20 65 78 70 72 20 63 61 6e 20 62 65 20 4e 55 s expr can be NU
21041 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 LL */. int iReg
21042 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 , /* T
21043 65 73 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e est the value in
21044 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 66 this register f
21045 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 or NULL */. int
21046 20 69 44 65 73 74 20 20 20 20 20 20 20 20 20 20 iDest
21047 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 /* Jump here if
21048 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 the value is nu
21049 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 73 ll */.){. if( s
2104a 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e qlite3ExprCanBeN
2104b 75 6c 6c 28 70 45 78 70 72 29 20 29 7b 0a 20 20 ull(pExpr) ){.
2104c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2104d 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c Op2(v, OP_IsNull
2104e 2c 20 69 52 65 67 2c 20 69 44 65 73 74 29 3b 0a , iReg, iDest);.
2104f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 }.}../*.** Ret
21050 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 urn TRUE if the
21051 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e given expression
21052 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 77 is a constant w
21053 68 69 63 68 20 77 6f 75 6c 64 20 62 65 0a 2a 2a hich would be.**
21054 20 75 6e 63 68 61 6e 67 65 64 20 62 79 20 4f 50 unchanged by OP
21055 5f 41 66 66 69 6e 69 74 79 20 77 69 74 68 20 74 _Affinity with t
21056 68 65 20 61 66 66 69 6e 69 74 79 20 67 69 76 65 he affinity give
21057 6e 20 69 6e 20 74 68 65 20 73 65 63 6f 6e 64 0a n in the second.
21058 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a ** argument..**.
21059 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
2105a 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 is used to deter
2105b 6d 69 6e 65 20 69 66 20 74 68 65 20 4f 50 5f 41 mine if the OP_A
2105c 66 66 69 6e 69 74 79 20 6f 70 65 72 61 74 69 6f ffinity operatio
2105d 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 6d 69 74 n.** can be omit
2105e 74 65 64 2e 20 20 57 68 65 6e 20 69 6e 20 64 6f ted. When in do
2105f 75 62 74 20 72 65 74 75 72 6e 20 46 41 4c 53 45 ubt return FALSE
21060 2e 20 20 41 20 66 61 6c 73 65 20 6e 65 67 61 74 . A false negat
21061 69 76 65 0a 2a 2a 20 69 73 20 68 61 72 6d 6c 65 ive.** is harmle
21062 73 73 2e 20 20 41 20 66 61 6c 73 65 20 70 6f 73 ss. A false pos
21063 69 74 69 76 65 2c 20 68 6f 77 65 76 65 72 2c 20 itive, however,
21064 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 74 68 can result in th
21065 65 20 77 72 6f 6e 67 0a 2a 2a 20 61 6e 73 77 65 e wrong.** answe
21066 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 r..*/.SQLITE_PRI
21067 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21068 45 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 6e ExprNeedsNoAffin
21069 69 74 79 43 68 61 6e 67 65 28 63 6f 6e 73 74 20 ityChange(const
2106a 45 78 70 72 20 2a 70 2c 20 63 68 61 72 20 61 66 Expr *p, char af
2106b 66 29 7b 0a 20 20 75 38 20 6f 70 3b 0a 20 20 69 f){. u8 op;. i
2106c 66 28 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 f( aff==SQLITE_A
2106d 46 46 5f 4e 4f 4e 45 20 29 20 72 65 74 75 72 6e FF_NONE ) return
2106e 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 1;. while( p->
2106f 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 op==TK_UPLUS ||
21070 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 p->op==TK_UMINUS
21071 20 29 7b 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 ){ p = p->pLeft
21072 3b 20 7d 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 ; }. op = p->op
21073 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 ;. if( op==TK_R
21074 45 47 49 53 54 45 52 20 29 20 6f 70 20 3d 20 70 EGISTER ) op = p
21075 2d 3e 6f 70 32 3b 0a 20 20 73 77 69 74 63 68 28 ->op2;. switch(
21076 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 op ){. case
21077 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 TK_INTEGER: {.
21078 20 20 20 20 72 65 74 75 72 6e 20 61 66 66 3d 3d return aff==
21079 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
2107a 45 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 ER || aff==SQLIT
2107b 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 E_AFF_NUMERIC;.
2107c 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
2107d 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 _FLOAT: {.
2107e 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49 return aff==SQLI
2107f 54 45 5f 41 46 46 5f 52 45 41 4c 20 7c 7c 20 61 TE_AFF_REAL || a
21080 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e ff==SQLITE_AFF_N
21081 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 0a 20 20 UMERIC;. }.
21082 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 case TK_STRING
21083 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e : {. return
21084 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 aff==SQLITE_AFF
21085 5f 54 45 58 54 3b 0a 20 20 20 20 7d 0a 20 20 20 _TEXT;. }.
21086 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b case TK_BLOB: {
21087 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b . return 1;
21088 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21089 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 TK_COLUMN: {.
2108a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 54 assert( p->iT
2108b 61 62 6c 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 70 able>=0 ); /* p
2108c 20 63 61 6e 6e 6f 74 20 62 65 20 70 61 72 74 20 cannot be part
2108d 6f 66 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 of a CHECK const
2108e 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 72 raint */. r
2108f 65 74 75 72 6e 20 70 2d 3e 69 43 6f 6c 75 6d 6e eturn p->iColumn
21090 3c 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 <0. &&
21091 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 (aff==SQLITE_AFF
21092 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 3d _INTEGER || aff=
21093 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 =SQLITE_AFF_NUME
21094 52 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 RIC);. }.
21095 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
21096 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d return 0;. }
21097 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 . }.}../*.** Re
21098 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 turn TRUE if the
21099 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 given string is
2109a 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e a row-id column
2109b 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 name..*/.SQLITE
2109c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c _PRIVATE int sql
2109d 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 ite3IsRowid(cons
2109e 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 t char *z){. if
2109f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
210a0 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d (z, "_ROWID_")==
210a1 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
210a2 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
210a3 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d mp(z, "ROWID")==
210a4 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 0 ) return 1;.
210a5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 if( sqlite3StrIC
210a6 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 mp(z, "OID")==0
210a7 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 ) return 1;. re
210a8 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
210a9 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 Return true if
210aa 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20 74 we are able to t
210ab 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 6f he IN operator o
210ac 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 ptimization on a
210ad 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65 .** query of the
210ae 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 form.**.**
210af 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e x IN (SELECT .
210b0 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 ..).**.** Where
210b1 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63 6c the SELECT... cl
210b2 61 75 73 65 20 69 73 20 61 73 20 73 70 65 63 69 ause is as speci
210b3 66 69 65 64 20 62 79 20 74 68 65 20 70 61 72 61 fied by the para
210b4 6d 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a 2a meter to this.**
210b5 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 routine..**.**
210b6 54 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65 63 The Select objec
210b7 74 20 70 61 73 73 65 64 20 69 6e 20 68 61 73 20 t passed in has
210b8 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72 65 already been pre
210b9 70 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e 6f processed and no
210ba 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65 20 .** errors have
210bb 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 been found..*/.#
210bc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
210bd 49 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61 74 IT_SUBQUERY.stat
210be 69 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64 61 ic int isCandida
210bf 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65 63 teForInOpt(Selec
210c0 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 t *p){. SrcList
210c1 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c 69 *pSrc;. ExprLi
210c2 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54 61 st *pEList;. Ta
210c3 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66 28 ble *pTab;. if(
210c4 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 p==0 ) return 0
210c5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
210c6 20 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61 6e /* right-han
210c7 64 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73 20 d side of IN is
210c8 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28 20 SELECT */. if(
210c9 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74 75 p->pPrior ) retu
210ca 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 rn 0;
210cb 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 /* Not a comp
210cc 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a 20 ound SELECT */.
210cd 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 if( p->selFlags
210ce 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c & (SF_Distinct|
210cf 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29 7b SF_Aggregate) ){
210d0 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 28 . testcase( (
210d1 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 p->selFlags & (S
210d2 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 F_Distinct|SF_Ag
210d3 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44 69 gregate))==SF_Di
210d4 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74 65 stinct );. te
210d5 73 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c 46 stcase( (p->selF
210d6 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 lags & (SF_Disti
210d7 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 nct|SF_Aggregate
210d8 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65 ))==SF_Aggregate
210d9 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 );. return 0
210da 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43 54 ; /* No DISTINCT
210db 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f 20 keyword and no
210dc 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
210dd 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 ons */. }. ass
210de 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42 79 ert( p->pGroupBy
210df 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 ==0 );
210e0 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47 52 /* Has no GR
210e1 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f OUP BY clause */
210e2 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 . if( p->pLimit
210e3 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
210e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 /* Has
210e5 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 no LIMIT clause
210e6 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d */. assert( p-
210e7 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 >pOffset==0 );
210e8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
210e9 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20 6e No LIMIT means n
210ea 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69 66 o OFFSET */. if
210eb 28 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72 65 ( p->pWhere ) re
210ec 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 turn 0;
210ed 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 57 /* Has no W
210ee 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
210ef 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b pSrc = p->pSrc;
210f0 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63 21 . assert( pSrc!
210f1 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72 63 =0 );. if( pSrc
210f2 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 ->nSrc!=1 ) retu
210f3 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f rn 0; /
210f4 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e * Single term in
210f5 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a FROM clause */.
210f6 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30 5d if( pSrc->a[0]
210f7 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75 72 .pSelect ) retur
210f8 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d n 0; /* FROM
210f9 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75 65 is not a subque
210fa 72 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 ry or view */.
210fb 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b 30 pTab = pSrc->a[0
210fc 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45 ].pTab;. if( NE
210fd 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 VER(pTab==0) ) r
210fe 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 eturn 0;. asser
210ff 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 t( pTab->pSelect
21100 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 ==0 );
21101 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 /* FROM clause
21102 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 2a is not a view *
21103 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 /. if( IsVirtua
21104 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e l(pTab) ) return
21105 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 52 0; /* FR
21106 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61 20 OM clause not a
21107 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f virtual table */
21108 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 . pEList = p->p
21109 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c EList;. if( pEL
2110a 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29 20 ist->nExpr!=1 )
2110b 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 return 0;
2110c 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e /* One column in
2110d 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
2110e 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d */. if( pEList-
2110f 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 >a[0].pExpr->op!
21110 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65 74 =TK_COLUMN ) ret
21111 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c 74 urn 0; /* Result
21112 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a is a column */.
21113 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 return 1;.}.#e
21114 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f ndif /* SQLITE_O
21115 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a MIT_SUBQUERY */.
21116 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 ./*.** This func
21117 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 tion is used by
21118 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 the implementati
21119 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e 2e on of the IN (..
2111a 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 .) operator..**
2111b 49 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20 66 It's job is to f
2111c 69 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61 20 ind or create a
2111d 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 b-tree structure
2111e 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 that may be use
2111f 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20 74 d.** either to t
21120 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 est for membersh
21121 69 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29 20 ip of the (...)
21122 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61 74 set or to iterat
21123 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74 73 e through.** its
21124 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70 69 members, skippi
21125 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a 2a ng duplicates..*
21126 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f *.** The index o
21127 66 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70 65 f the cursor ope
21128 6e 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 ned on the b-tre
21129 65 20 28 64 61 74 61 62 61 73 65 20 74 61 62 6c e (database tabl
2112a 65 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 e, database inde
2112b 78 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d 61 x .** or epherma
2112c 6c 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f 72 l table) is stor
2112d 65 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c 65 ed in pX->iTable
2112e 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75 6e before this fun
2112f 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 2a ction returns..*
21130 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 * The returned v
21131 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e alue of this fun
21132 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73 20 ction indicates
21133 74 68 65 20 62 2d 74 72 65 65 20 74 79 70 65 2c the b-tree type,
21134 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a as follows:.**.
21135 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52 4f ** IN_INDEX_RO
21136 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f 72 WID - The cursor
21137 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 was opened on a
21138 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e database table.
21139 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 49 .** IN_INDEX_I
2113a 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73 6f NDEX - The curso
2113b 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 r was opened on
2113c 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 a database index
2113d 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f ..** IN_INDEX_
2113e 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72 73 EPH - The curs
2113f 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e or was opened on
21140 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72 65 a specially cre
21141 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20 20 ated and.**
21142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 p
21143 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65 6d opulated epherem
21144 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 al table..**.**
21145 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74 72 An existing b-tr
21146 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 ee may only be u
21147 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 sed if the SELEC
21148 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 T is of the simp
21149 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a le.** form:.**.*
2114a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f * SELECT <co
2114b 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c lumn> FROM <tabl
2114c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 e>.**.** If the
2114d 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d prNotFound param
2114e 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e 20 eter is 0, then
2114f 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20 the b-tree will
21150 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 be used to itera
21151 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 te.** through th
21152 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 e set members, s
21153 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70 6c kipping any dupl
21154 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73 20 icates. In this
21155 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 case an.** epher
21156 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 emal table must
21157 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20 74 be used unless t
21158 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c he selected <col
21159 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74 65 umn> is guarante
2115a 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 ed.** to be uniq
2115b 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63 61 ue - either beca
2115c 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e 54 use it is an INT
2115d 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 EGER PRIMARY KEY
2115e 20 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61 20 or it.** has a
2115f 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e UNIQUE constrain
21160 74 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64 65 t or UNIQUE inde
21161 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 x..**.** If the
21162 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61 6d prNotFound param
21163 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 eter is not 0, t
21164 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20 77 hen the b-tree w
21165 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 ill be used .**
21166 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d for fast set mem
21167 62 65 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 bership tests. I
21168 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 n this case an e
21169 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d pheremal table m
2116a 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 ust .** be used
2116b 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 unless <column>
2116c 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 is an INTEGER PR
2116d 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 IMARY KEY or an
2116e 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 index can .** be
2116f 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c found with <col
21170 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 umn> as its left
21171 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a -most column..**
21172 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d 74 .** When the b-t
21173 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73 65 ree is being use
21174 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 d for membership
21175 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c 6c tests, the call
21176 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 ing function.**
21177 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68 needs to know wh
21178 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 ether or not the
21179 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 structure conta
2117a 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 ins an SQL NULL
2117b 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72 64 .** value in ord
2117c 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79 20 er to correctly
2117d 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73 73 evaluate express
2117e 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e 20 ions like "X IN
2117f 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 (Y, Z)"..** If t
21180 68 65 72 65 20 69 73 20 61 6e 79 20 63 68 61 6e here is any chan
21181 63 65 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e ce that the (...
21182 29 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 ) might contain
21183 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a a NULL value at.
21184 2a 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e ** runtime, then
21185 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61 a register is a
21186 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65 llocated and the
21187 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 register number
21188 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a written.** to *
21189 70 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74 prNotFound. If t
2118a 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63 here is no chanc
2118b 65 20 74 68 61 74 20 74 68 65 20 28 2e 2e 2e 29 e that the (...)
2118c 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e contains a.** N
2118d 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 ULL value, then
2118e 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c *prNotFound is l
2118f 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a eft unchanged..*
21190 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73 74 *.** If a regist
21191 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 er is allocated
21192 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e and its location
21193 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f stored in *prNo
21194 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 tFound, then.**
21195 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 its initial valu
21196 65 20 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 74 e is NULL. If t
21197 68 65 20 28 2e 2e 2e 29 20 64 6f 65 73 20 6e 6f he (...) does no
21198 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e t remain constan
21199 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72 t.** for the dur
2119a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65 ation of the que
2119b 72 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45 4c ry (i.e. the SEL
2119c 45 43 54 20 77 69 74 68 69 6e 20 74 68 65 20 28 ECT within the (
2119d 2e 2e 2e 29 0a 2a 2a 20 69 73 20 61 20 63 6f 72 ...).** is a cor
2119e 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 related subquery
2119f 29 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 ) then the value
211a0 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 of the allocate
211a1 64 20 72 65 67 69 73 74 65 72 20 69 73 0a 2a 2a d register is.**
211a2 20 72 65 73 65 74 20 74 6f 20 4e 55 4c 4c 20 65 reset to NULL e
211a3 61 63 68 20 74 69 6d 65 20 74 68 65 20 73 75 62 ach time the sub
211a4 71 75 65 72 79 20 69 73 20 72 65 72 75 6e 2e 20 query is rerun.
211a5 54 68 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a This allows the.
211a6 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 ** caller to use
211a7 20 76 64 62 65 20 63 6f 64 65 20 65 71 75 69 76 vdbe code equiv
211a8 61 6c 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c alent to the fol
211a9 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 lowing:.**.**
211aa 69 66 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 if( register==NU
211ab 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 LL ){.** has
211ac 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 _null = <test if
211ad 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 data structure
211ae 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a contains null>.*
211af 2a 20 20 20 20 20 72 65 67 69 73 74 65 72 20 3d * register =
211b0 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 1.** }.**.**
211b1 69 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 in order to avoi
211b2 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 d running the <t
211b3 65 73 74 20 69 66 20 64 61 74 61 20 73 74 72 75 est if data stru
211b4 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e cture contains n
211b5 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 ull>.** test mor
211b6 65 20 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20 e often than is
211b7 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 necessary..*/.#i
211b8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
211b9 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 T_SUBQUERY.SQLIT
211ba 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
211bb 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 lite3FindInIndex
211bc 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
211bd 45 78 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 Expr *pX, int *p
211be 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 rNotFound){. Se
211bf 6c 65 63 74 20 2a 70 3b 20 20 20 20 20 20 20 20 lect *p;
211c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
211c1 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f /* SELECT to
211c2 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 49 4e the right of IN
211c3 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 operator */. i
211c4 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 20 20 20 nt eType = 0;
211c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
211c6 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 /* Type of
211c7 52 48 53 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e RHS table. IN_IN
211c8 44 45 58 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 DEX_* */. int i
211c9 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 Tab = pParse->nT
211ca 61 62 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 ab++;
211cb 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 68 /* Cursor of th
211cc 65 20 52 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 e RHS table */.
211cd 20 69 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75 int mustBeUniqu
211ce 65 20 3d 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d e = (prNotFound=
211cf 3d 30 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 =0); /* True i
211d0 66 20 52 48 53 20 6d 75 73 74 20 62 65 20 75 6e f RHS must be un
211d1 69 71 75 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 ique */.. asser
211d2 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e t( pX->op==TK_IN
211d3 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 );.. /* Check
211d4 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 78 69 to see if an exi
211d5 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72 20 69 sting table or i
211d6 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73 65 64 ndex can be used
211d7 20 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73 66 79 to. ** satisfy
211d8 20 74 68 65 20 71 75 65 72 79 2e 20 20 54 68 69 the query. Thi
211d9 73 20 69 73 20 70 72 65 66 65 72 61 62 6c 65 20 s is preferable
211da 74 6f 20 67 65 6e 65 72 61 74 69 6e 67 20 61 20 to generating a
211db 6e 65 77 20 0a 20 20 2a 2a 20 65 70 68 65 6d 65 new . ** epheme
211dc 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a ral table.. */.
211dd 20 20 70 20 3d 20 28 45 78 70 72 48 61 73 50 72 p = (ExprHasPr
211de 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 operty(pX, EP_xI
211df 73 53 65 6c 65 63 74 29 20 3f 20 70 58 2d 3e 78 sSelect) ? pX->x
211e0 2e 70 53 65 6c 65 63 74 20 3a 20 30 29 3b 0a 20 .pSelect : 0);.
211e1 20 69 66 28 20 41 4c 57 41 59 53 28 70 50 61 72 if( ALWAYS(pPar
211e2 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20 se->nErr==0) &&
211e3 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72 49 6e isCandidateForIn
211e4 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20 73 71 Opt(p) ){. sq
211e5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
211e6 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 se->db;
211e7 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 /* Database
211e8 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
211e9 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d Expr *pExpr =
211ea 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d p->pEList->a[0]
211eb 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45 78 70 .pExpr; /* Exp
211ec 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e ression <column>
211ed 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c */. int iCol
211ee 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d = pExpr->iColum
211ef 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f n; /
211f0 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d * Index of colum
211f1 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 n <column> */.
211f2 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 Vdbe *v = sqli
211f3 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 te3GetVdbe(pPars
211f4 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 e); /* Virt
211f5 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69 6e ual machine bein
211f6 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 54 g coded */. T
211f7 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 2d 3e able *pTab = p->
211f8 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b pSrc->a[0].pTab;
211f9 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 3c /* Table <
211fa 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 table>. */. i
211fb 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
211fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
211fd 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
211fe 65 20 69 64 78 20 66 6f 72 20 70 54 61 62 20 2a e idx for pTab *
211ff 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43 6f 64 /. . /* Cod
21200 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79 43 6f e an OP_VerifyCo
21201 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61 62 6c okie and OP_Tabl
21202 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62 6c 65 eLock for <table
21203 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 >. */. iDb =
21204 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 sqlite3SchemaToI
21205 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 ndex(db, pTab->p
21206 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73 71 6c Schema);. sql
21207 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 ite3CodeVerifySc
21208 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 hema(pParse, iDb
21209 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 61 );. sqlite3Ta
2120a 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 bleLock(pParse,
2120b 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c iDb, pTab->tnum,
2120c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 0, pTab->zName)
2120d 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 ;.. /* This f
2120e 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 unction is only
2120f 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77 6f 20 called from two
21210 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74 68 20 places. In both
21211 63 61 73 65 73 20 74 68 65 20 76 64 62 65 0a 20 cases the vdbe.
21212 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 ** has alread
21213 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 y been allocated
21214 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71 6c 69 . So assume sqli
21215 74 65 33 47 65 74 56 64 62 65 28 29 20 69 73 20 te3GetVdbe() is
21216 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20 73 75 always. ** su
21217 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e 0a 20 ccessful here..
21218 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 */. assert
21219 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f (v);. if( iCo
2121a 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 l<0 ){. int
2121b 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 iMem = ++pParse
2121c 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 6e ->nMem;. in
2121d 74 20 69 41 64 64 72 3b 0a 0a 20 20 20 20 20 20 t iAddr;..
2121e 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 iAddr = sqlite3V
2121f 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
21220 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 If, iMem);.
21221 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21222 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 p2(v, OP_Integer
21223 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 , 1, iMem);..
21224 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 sqlite3OpenTa
21225 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 ble(pParse, iTab
21226 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f , iDb, pTab, OP_
21227 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 OpenRead);.
21228 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 eType = IN_INDE
21229 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20 20 20 X_ROWID;..
2122a 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
2122b 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 ere(v, iAddr);.
2122c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2122d 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 Index *pIdx;
2122e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2122f 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 /* Iterator
21230 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 variable */..
21231 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 /* The colla
21232 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 tion sequence us
21233 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70 61 72 ed by the compar
21234 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e 64 65 ison. If an inde
21235 78 20 69 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a x is to. **
21236 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 be used in plac
21237 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61 62 6c e of a temp-tabl
21238 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20 6f 72 e, it must be or
21239 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e 67 0a dered according.
2123a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 69 73 ** to this
2123b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
2123c 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 43 nce. */. C
2123d 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 ollSeq *pReq = s
2123e 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 qlite3BinaryComp
2123f 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 areCollSeq(pPars
21240 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 e, pX->pLeft, pE
21241 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 xpr);.. /*
21242 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 Check that the a
21243 66 66 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c ffinity that wil
21244 6c 20 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 l be used to per
21245 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 form the .
21246 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 ** comparison is
21247 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 the same as the
21248 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 affinity of the
21249 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 column. If.
2124a 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 ** it is not,
2124b 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 it is not possib
2124c 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e le to use any in
2124d 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 dex.. */.
2124e 20 20 20 20 63 68 61 72 20 61 66 66 20 3d 20 63 char aff = c
2124f 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 omparisonAffinit
21250 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69 6e 74 y(pX);. int
21251 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 affinity_ok = (
21252 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d pTab->aCol[iCol]
21253 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66 7c 7c .affinity==aff||
21254 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f aff==SQLITE_AFF_
21255 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20 66 6f NONE);.. fo
21256 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e r(pIdx=pTab->pIn
21257 64 65 78 3b 20 70 49 64 78 20 26 26 20 65 54 79 dex; pIdx && eTy
21258 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e 69 74 pe==0 && affinit
21259 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64 78 2d y_ok; pIdx=pIdx-
2125a 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 >pNext){.
2125b 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69 43 6f if( (pIdx->aiCo
2125c 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 lumn[0]==iCol).
2125d 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 && sqlit
2125e 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 e3FindCollSeq(db
2125f 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64 78 2d , ENC(db), pIdx-
21260 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29 3d 3d >azColl[0], 0)==
21261 70 52 65 71 0a 20 20 20 20 20 20 20 20 20 26 26 pReq. &&
21262 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75 65 20 (!mustBeUnique
21263 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d || (pIdx->nColum
21264 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e 6f 6e n==1 && pIdx->on
21265 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 Error!=OE_None))
21266 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 . ){.
21267 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d int iMem =
21268 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
21269 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
2126a 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 Addr;.
2126b 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 char *pKey;. .
2126c 20 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20 pKey =
2126d 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49 (char *)sqlite3I
2126e 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 ndexKeyinfo(pPar
2126f 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 se, pIdx);.
21270 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c iAddr = sql
21271 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
21272 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a , OP_If, iMem);.
21273 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21274 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21275 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d P_Integer, 1, iM
21276 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20 em);. .
21277 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21278 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 Op4(v, OP_OpenRe
21279 61 64 2c 20 69 54 61 62 2c 20 70 49 64 78 2d 3e ad, iTab, pIdx->
2127a 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 tnum, iDb,.
2127b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2127c 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 2c 50 pKey,P
2127d 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
2127e 46 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 F);. Vd
2127f 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 beComment((v, "%
21280 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 s", pIdx->zName)
21281 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 54 79 );. eTy
21282 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e pe = IN_INDEX_IN
21283 44 45 58 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 DEX;..
21284 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
21285 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b 0a 20 ere(v, iAddr);.
21286 20 20 20 20 20 20 20 20 20 69 66 28 20 70 72 4e if( prN
21287 6f 74 46 6f 75 6e 64 20 26 26 20 21 70 54 61 62 otFound && !pTab
21288 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 ->aCol[iCol].not
21289 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 Null ){.
2128a 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 *prNotFound
2128b 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d = ++pParse->nMem
2128c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 ;. }.
2128d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
2128e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 }. }.. if(
2128f 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 eType==0 ){.
21290 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20 66 6f /* Could not fo
21291 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e 67 20 und an existing
21292 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 table or index t
21293 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48 53 o use as the RHS
21294 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a 20 b-tree.. **
21295 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f 20 We will have to
21296 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68 65 generate an ephe
21297 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20 64 meral table to d
21298 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20 2a o the job.. *
21299 2f 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61 /. int rMayHa
2129a 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 veNull = 0;.
2129b 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 eType = IN_INDEX
2129c 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72 _EPH;. if( pr
2129d 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 NotFound ){.
2129e 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 *prNotFound =
2129f 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b rMayHaveNull = +
212a0 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
212a1 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d }else if( pX-
212a2 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c >pLeft->iColumn<
212a3 30 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 0 && !ExprHasAny
212a4 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f Property(pX, EP_
212a5 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 xIsSelect) ){.
212a6 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 eType = IN_I
212a7 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20 20 NDEX_ROWID;.
212a8 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 }. sqlite3Cod
212a9 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73 eSubselect(pPars
212aa 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65 4e e, pX, rMayHaveN
212ab 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f 49 ull, eType==IN_I
212ac 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20 7d NDEX_ROWID);. }
212ad 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69 54 else{. pX->iT
212ae 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20 7d able = iTab;. }
212af 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 65 3b . return eType;
212b0 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a .}.#endif../*.**
212b1 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 Generate code f
212b2 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75 65 or scalar subque
212b3 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e 20 ries used as an
212b4 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e expression.** an
212b5 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e 20 d IN operators.
212b6 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a Examples:.**.**
212b7 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20 46 (SELECT a F
212b8 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20 20 ROM b)
212b9 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 -- subquery.**
212ba 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 EXISTS (SELEC
212bb 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d 2d T a FROM b) --
212bc 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72 79 EXISTS subquery
212bd 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34 2c .** x IN (4,
212be 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20 20 5,11)
212bf 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f -- IN operato
212c0 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20 72 r with list on r
212c1 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a 2a ight-hand side.*
212c2 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 * x IN (SELE
212c3 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 20 CT a FROM b)
212c4 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 -- IN operator
212c5 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f 6e with subquery on
212c6 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a the right.**.**
212c7 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61 6d The pExpr param
212c8 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20 74 eter describes t
212c9 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 he expression th
212ca 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 at contains the
212cb 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 6f IN.** operator o
212cc 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a r subquery..**.*
212cd 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 * If parameter i
212ce 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 sRowid is non-ze
212cf 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73 73 ro, then express
212d0 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75 61 ion pExpr is gua
212d1 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 ranteed.** to be
212d2 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 72 of the form "<r
212d3 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 owid> IN (?, ?,
212d4 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77 69 ?)", where <rowi
212d5 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 d> is a referenc
212d6 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e 74 e.** to some int
212d7 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 eger key column
212d8 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72 65 of a table B-Tre
212d9 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c e. In this case,
212da 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 use an.** intke
212db 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f 72 y B-Tree to stor
212dc 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e 28 e the set of IN(
212dd 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73 74 ...) values inst
212de 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 61 6c ead of the usual
212df 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61 72 .** (slower) var
212e0 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 iable length key
212e1 73 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 s B-Tree..**.**
212e2 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 If rMayHaveNull
212e3 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61 is non-zero, tha
212e4 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 t means that the
212e5 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e operation is an
212e6 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53 45 IN.** (not a SE
212e7 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 29 20 LECT or EXISTS)
212e8 61 6e 64 20 74 68 61 74 20 74 68 65 20 52 48 53 and that the RHS
212e9 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73 20 might contains
212ea 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68 65 NULLs..** Furthe
212eb 72 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69 73 rmore, the IN is
212ec 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61 75 in a WHERE clau
212ed 73 65 20 61 6e 64 20 74 68 61 74 20 77 65 20 72 se and that we r
212ee 65 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74 6f eally want.** to
212ef 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 74 68 iterate over th
212f0 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20 e RHS of the IN
212f1 6f 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64 65 operator in orde
212f2 72 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f 63 r to quickly loc
212f3 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 ate.** all corre
212f4 73 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c 65 sponding LHS ele
212f5 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69 73 ments. All this
212f6 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69 73 routine does is
212f7 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 initialize.** t
212f8 68 65 20 72 65 67 69 73 74 65 72 20 67 69 76 65 he register give
212f9 6e 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75 6c n by rMayHaveNul
212fa 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c l to NULL. Call
212fb 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69 6c ing routines wil
212fc 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20 6f l take.** care o
212fd 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73 20 f changing this
212fe 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20 74 register value t
212ff 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74 68 o non-NULL if th
21300 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66 72 e RHS is NULL-fr
21301 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 ee..**.** If rMa
21302 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65 72 yHaveNull is zer
21303 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 o, that means th
21304 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79 20 at the subquery
21305 69 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a 2a is being used.**
21306 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 for membership
21307 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 testing only. T
21308 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 here is no need
21309 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e to initialize an
2130a 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 74 y.** registers t
2130b 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20 70 o indicate the p
2130c 72 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65 6e resense or absen
2130d 63 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20 74 ce of NULLs on t
2130e 68 65 20 52 48 53 2e 0a 2a 2a 0a 2a 2a 20 46 6f he RHS..**.** Fo
2130f 72 20 61 20 53 45 4c 45 43 54 20 6f 72 20 45 58 r a SELECT or EX
21310 49 53 54 53 20 6f 70 65 72 61 74 6f 72 2c 20 72 ISTS operator, r
21311 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 eturn the regist
21312 65 72 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 er that holds th
21313 65 0a 2a 2a 20 72 65 73 75 6c 74 2e 20 20 46 6f e.** result. Fo
21314 72 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 6f r IN operators o
21315 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 r if an error oc
21316 63 75 72 73 2c 20 74 68 65 20 72 65 74 75 72 6e curs, the return
21317 20 76 61 6c 75 65 20 69 73 20 30 2e 0a 2a 2f 0a value is 0..*/.
21318 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
21319 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53 51 4c MIT_SUBQUERY.SQL
2131a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
2131b 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 sqlite3CodeSubse
2131c 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 lect(. Parse *p
2131d 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 Parse,
2131e 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 /* Parsing conte
2131f 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 xt */. Expr *pE
21320 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 xpr,
21321 2f 2a 20 54 68 65 20 49 4e 2c 20 53 45 4c 45 43 /* The IN, SELEC
21322 54 2c 20 6f 72 20 45 58 49 53 54 53 20 6f 70 65 T, or EXISTS ope
21323 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 rator */. int r
21324 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20 MayHaveNull,
21325 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 /* Register t
21326 68 61 74 20 72 65 63 6f 72 64 73 20 77 68 65 74 hat records whet
21327 68 65 72 20 4e 55 4c 4c 73 20 65 78 69 73 74 20 her NULLs exist
21328 69 6e 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20 in RHS */. int
21329 69 73 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 isRowid
2132a 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 /* If true,
2132b 4c 48 53 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 LHS of IN operat
2132c 6f 72 20 69 73 20 61 20 72 6f 77 69 64 20 2a 2f or is a rowid */
2132d 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64 .){. int testAd
2132e 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 dr = 0;
2132f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21330 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61 One-time test a
21331 64 64 72 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20 ddress */. int
21332 72 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 20 rReg = 0;
21333 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21334 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
21335 73 74 6f 72 69 6e 67 20 72 65 73 75 6c 74 69 6e storing resultin
21336 67 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d g */. Vdbe *v =
21337 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
21338 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 4e pParse);. if( N
21339 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 EVER(v==0) ) ret
2133a 75 72 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 urn 0;. sqlite3
2133b 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 ExprCachePush(pP
2133c 61 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 arse);.. /* Thi
2133d 73 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72 s code must be r
2133e 75 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65 un in its entire
2133f 74 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74 ty every time it
21340 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a is encountered.
21341 20 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 ** if any of t
21342 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 he following is
21343 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 true:. **. **
21344 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d * The right-
21345 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63 hand side is a c
21346 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65 orrelated subque
21347 72 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 ry. ** * Th
21348 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 e right-hand sid
21349 65 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 e is an expressi
2134a 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 on list containi
2134b 6e 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a ng variables. *
2134c 2a 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69 * * We are i
2134d 6e 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a nside a trigger.
2134e 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c **. ** If all
2134f 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72 of the above ar
21350 65 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 e false, then we
21351 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f can run this co
21352 64 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a de just once. *
21353 2a 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c * save the resul
21354 74 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68 ts, and reuse th
21355 65 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e e same result on
21356 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f subsequent invo
21357 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 cations.. */.
21358 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 if( !ExprHasAnyP
21359 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
2135a 50 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20 P_VarSelect) &&
2135b 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 !pParse->pTrigge
2135c 72 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 rTab ){. int
2135d 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e mem = ++pParse->
2135e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 nMem;. sqlite
2135f 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
21360 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 P_If, mem);.
21361 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74 testAddr = sqlit
21362 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21363 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d OP_Integer, 1, m
21364 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 em);. assert(
21365 20 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70 testAddr>0 || p
21366 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
21367 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a cFailed );. }..
21368 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d switch( pExpr-
21369 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 >op ){. case
2136a 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63 TK_IN: {. c
2136b 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 har affinity;.
2136c 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49 KeyInfo keyI
2136d 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 nfo;. int a
2136e 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 ddr; /* A
2136f 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 ddress of OP_Ope
21370 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 nEphemeral instr
21371 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 uction */.
21372 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 Expr *pLeft = pE
21373 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 xpr->pLeft;..
21374 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e if( rMayHaveN
21375 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ull ){. s
21376 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21377 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 (v, OP_Null, 0,
21378 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 rMayHaveNull);.
21379 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66 }.. af
2137a 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 finity = sqlite3
2137b 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 ExprAffinity(pLe
2137c 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 ft);.. /* W
2137d 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61 hether this is a
2137e 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e n 'x IN(SELECT..
2137f 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28 .)' or an 'x IN(
21380 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20 <exprlist>)'.
21381 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e ** expression
21382 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74 it is handled t
21383 68 65 20 73 61 6d 65 20 77 61 79 2e 20 20 41 6e he same way. An
21384 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 ephemeral table
21385 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 66 69 is . ** fi
21386 6c 6c 65 64 20 77 69 74 68 20 73 69 6e 67 6c 65 lled with single
21387 2d 66 69 65 6c 64 20 69 6e 64 65 78 20 6b 65 79 -field index key
21388 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 s representing t
21389 68 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 he results.
2138a 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 53 45 4c ** from the SEL
2138b 45 43 54 20 6f 72 20 74 68 65 20 3c 65 78 70 72 ECT or the <expr
2138c 6c 69 73 74 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a list>.. **.
2138d 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 ** If the
2138e 27 78 27 20 65 78 70 72 65 73 73 69 6f 6e 20 69 'x' expression i
2138f 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 s a column value
21390 2c 20 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e , or the SELECT.
21391 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 ... ** stat
21392 65 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20 ement returns a
21393 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68 column value, th
21394 65 6e 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 en the affinity
21395 6f 66 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a of that. **
21396 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 column is used
21397 74 6f 20 62 75 69 6c 64 20 74 68 65 20 69 6e 64 to build the ind
21398 65 78 20 6b 65 79 73 2e 20 49 66 20 62 6f 74 68 ex keys. If both
21399 20 27 78 27 20 61 6e 64 20 74 68 65 0a 20 20 20 'x' and the.
2139a 20 20 20 2a 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 ** SELECT...
2139b 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f statement are co
2139c 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 lumns, then nume
2139d 72 69 63 20 61 66 66 69 6e 69 74 79 20 69 73 20 ric affinity is
2139e 75 73 65 64 0a 20 20 20 20 20 20 2a 2a 20 69 66 used. ** if
2139f 20 65 69 74 68 65 72 20 63 6f 6c 75 6d 6e 20 68 either column h
213a0 61 73 20 4e 55 4d 45 52 49 43 20 6f 72 20 49 4e as NUMERIC or IN
213a1 54 45 47 45 52 20 61 66 66 69 6e 69 74 79 2e 20 TEGER affinity.
213a2 49 66 20 6e 65 69 74 68 65 72 0a 20 20 20 20 20 If neither.
213a3 20 2a 2a 20 27 78 27 20 6e 6f 72 20 74 68 65 20 ** 'x' nor the
213a4 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d SELECT... statem
213a5 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c ent are columns,
213a6 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 then numeric af
213a7 66 69 6e 69 74 79 0a 20 20 20 20 20 20 2a 2a 20 finity. **
213a8 69 73 20 75 73 65 64 2e 0a 20 20 20 20 20 20 2a is used.. *
213a9 2f 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 /. pExpr->i
213aa 54 61 62 6c 65 20 3d 20 70 50 61 72 73 65 2d 3e Table = pParse->
213ab 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 61 64 nTab++;. ad
213ac 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
213ad 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 AddOp2(v, OP_Ope
213ae 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 45 78 70 nEphemeral, pExp
213af 72 2d 3e 69 54 61 62 6c 65 2c 20 21 69 73 52 6f r->iTable, !isRo
213b0 77 69 64 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 wid);. mems
213b1 65 74 28 26 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 et(&keyInfo, 0,
213b2 73 69 7a 65 6f 66 28 6b 65 79 49 6e 66 6f 29 29 sizeof(keyInfo))
213b3 3b 0a 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e ;. keyInfo.
213b4 6e 46 69 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 nField = 1;..
213b5 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 if( ExprHasPr
213b6 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
213b7 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 _xIsSelect) ){.
213b8 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31 /* Case 1
213b9 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28 53 : expr IN (S
213ba 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20 20 ELECT ...).
213bb 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a **. **
213bc 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 Generate code t
213bd 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 o write the resu
213be 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 lts of the selec
213bf 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 6f t into the tempo
213c0 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 rary. **
213c1 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64 20 table allocated
213c2 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76 65 and opened above
213c3 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 .. */.
213c4 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 SelectDest
213c5 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45 78 dest;. Ex
213c6 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a prList *pEList;.
213c7 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
213c8 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20 20 !isRowid );.
213c9 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
213ca 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 ctDestInit(&dest
213cb 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70 72 , SRT_Set, pExpr
213cc 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 20 ->iTable);.
213cd 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74 79 dest.affinity
213ce 20 3d 20 28 75 38 29 61 66 66 69 6e 69 74 79 3b = (u8)affinity;
213cf 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
213d0 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 26 (pExpr->iTable&
213d1 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70 45 78 0x0000FFFF)==pEx
213d2 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 pr->iTable );.
213d3 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
213d4 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 3Select(pParse,
213d5 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 pExpr->x.pSelect
213d6 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20 , &dest) ){.
213d7 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a return 0;.
213d8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
213d9 20 20 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 pEList = pExpr
213da 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c ->x.pSelect->pEL
213db 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 ist;. if(
213dc 20 41 4c 57 41 59 53 28 70 45 4c 69 73 74 21 3d ALWAYS(pEList!=
213dd 30 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 0 && pEList->nEx
213de 70 72 3e 30 29 20 29 7b 20 0a 20 20 20 20 20 20 pr>0) ){ .
213df 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c keyInfo.aCol
213e0 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 l[0] = sqlite3Bi
213e1 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 naryCompareCollS
213e2 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 eq(pParse, pExpr
213e3 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20 20 ->pLeft,.
213e4 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e 61 pEList->a
213e5 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 [0].pExpr);.
213e6 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 }. }els
213e7 65 20 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 e if( pExpr->x.p
213e8 4c 69 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 List!=0 ){.
213e9 20 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 20 /* Case 2:
213ea 20 20 65 78 70 72 20 49 4e 20 28 65 78 70 72 6c expr IN (exprl
213eb 69 73 74 29 0a 20 20 20 20 20 20 20 20 2a 2a 0a ist). **.
213ec 20 20 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 65 ** For e
213ed 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 ach expression,
213ee 62 75 69 6c 64 20 61 6e 20 69 6e 64 65 78 20 6b build an index k
213ef 65 79 20 66 72 6f 6d 20 74 68 65 20 65 76 61 6c ey from the eval
213f0 75 61 74 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 uation and.
213f1 20 20 20 2a 2a 20 73 74 6f 72 65 20 69 74 20 69 ** store it i
213f2 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 n the temporary
213f3 74 61 62 6c 65 2e 20 49 66 20 3c 65 78 70 72 3e table. If <expr>
213f4 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 is a column, th
213f5 65 6e 20 75 73 65 0a 20 20 20 20 20 20 20 20 2a en use. *
213f6 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20 61 * that columns a
213f7 66 66 69 6e 69 74 79 20 77 68 65 6e 20 62 75 69 ffinity when bui
213f8 6c 64 69 6e 67 20 69 6e 64 65 78 20 6b 65 79 73 lding index keys
213f9 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 6e . If <expr> is n
213fa 6f 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 ot. ** a
213fb 63 6f 6c 75 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 column, use nume
213fc 72 69 63 20 61 66 66 69 6e 69 74 79 2e 0a 20 20 ric affinity..
213fd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
213fe 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 int i;.
213ff 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
21400 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 = pExpr->x.pList
21401 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 ;. struct
21402 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a ExprList_item *
21403 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20 69 pItem;. i
21404 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 0a nt r1, r2, r3;..
21405 20 20 20 20 20 20 20 20 69 66 28 20 21 61 66 66 if( !aff
21406 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20 20 20 inity ){.
21407 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 53 51 affinity = SQ
21408 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 LITE_AFF_NONE;.
21409 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2140a 20 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 keyInfo.aColl[0
2140b 5d 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 ] = sqlite3ExprC
2140c 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 ollSeq(pParse, p
2140d 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 Expr->pLeft);..
2140e 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 /* Loop t
2140f 68 72 6f 75 67 68 20 65 61 63 68 20 65 78 70 72 hrough each expr
21410 65 73 73 69 6f 6e 20 69 6e 20 3c 65 78 70 72 6c ession in <exprl
21411 69 73 74 3e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 ist>. */.
21412 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r1 = sqlite3Get
21413 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
21414 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 73 71 . r2 = sq
21415 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
21416 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 pParse);.
21417 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21418 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
21419 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 66 , r2);. f
2141a 6f 72 28 69 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 or(i=pList->nExp
2141b 72 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e r, pItem=pList->
2141c 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 a; i>0; i--, pIt
2141d 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 em++){.
2141e 20 45 78 70 72 20 2a 70 45 32 20 3d 20 70 49 74 Expr *pE2 = pIt
2141f 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 em->pExpr;.
21420 20 20 20 20 20 69 6e 74 20 69 56 61 6c 54 6f 49 int iValToI
21421 6e 73 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f ns;.. /
21422 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 * If the express
21423 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 ion is not const
21424 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c 6c ant then we will
21425 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 need to.
21426 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 ** disable th
21427 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73 20 e test that was
21428 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65 20 generated above
21429 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 0a that makes sure.
2142a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 ** thi
2142b 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65 63 s code only exec
2142c 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63 61 utes once. Beca
2142d 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f use for a non-co
2142e 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20 20 nstant.
2142f 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 77 ** expression w
21430 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e 20 e need to rerun
21431 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20 74 this code each t
21432 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a ime.. *
21433 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 /. if(
21434 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71 6c testAddr && !sql
21435 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 ite3ExprIsConsta
21436 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20 20 nt(pE2) ){.
21437 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
21438 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 beChangeToNoop(v
21439 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32 29 , testAddr-1, 2)
2143a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 ;. te
2143b 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 stAddr = 0;.
2143c 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
2143d 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 /* Evaluate t
2143e 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e he expression an
2143f 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74 6f d insert it into
21440 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20 the temp table
21441 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
21442 20 69 73 52 6f 77 69 64 20 26 26 20 73 71 6c 69 isRowid && sqli
21443 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 te3ExprIsInteger
21444 28 70 45 32 2c 20 26 69 56 61 6c 54 6f 49 6e 73 (pE2, &iValToIns
21445 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
21446 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21447 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 49 p3(v, OP_InsertI
21448 6e 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c nt, pExpr->iTabl
21449 65 2c 20 72 32 2c 20 69 56 61 6c 54 6f 49 6e 73 e, r2, iValToIns
2144a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c );. }el
2144b 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
2144c 72 33 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 r3 = sqlite3Expr
2144d 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 CodeTarget(pPars
2144e 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 e, pE2, r1);.
2144f 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52 if( isR
21450 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 owid ){.
21451 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21452 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 eAddOp2(v, OP_Mu
21453 73 74 42 65 49 6e 74 2c 20 72 33 2c 0a 20 20 20 stBeInt, r3,.
21454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21455 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
21456 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
21457 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 20 ddr(v)+2);.
21458 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
21459 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
2145a 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e _Insert, pExpr->
2145b 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33 29 3b iTable, r2, r3);
2145c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c . }el
2145d 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
2145e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2145f 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 Op4(v, OP_MakeRe
21460 63 6f 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c cord, r3, 1, r2,
21461 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a &affinity, 1);.
21462 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
21463 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 lite3ExprCacheAf
21464 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 finityChange(pPa
21465 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20 20 20 rse, r3, 1);.
21466 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 sqlit
21467 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
21468 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45 OP_IdxInsert, pE
21469 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29 xpr->iTable, r2)
2146a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a ;. }.
2146b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
2146c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 }. sq
2146d 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
2146e 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b Reg(pParse, r1);
2146f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21470 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
21471 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 Parse, r2);.
21472 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 69 }. if( !i
21473 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 sRowid ){.
21474 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
21475 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c 20 28 ngeP4(v, addr, (
21476 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66 6f 2c void *)&keyInfo,
21477 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 P4_KEYINFO);.
21478 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 }. brea
21479 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 k;. }.. ca
2147a 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 se TK_EXISTS:.
2147b 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 case TK_SELECT
2147c 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b :. default: {
2147d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 . /* If thi
2147e 73 20 68 61 73 20 74 6f 20 62 65 20 61 20 73 63 s has to be a sc
2147f 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20 47 65 alar SELECT. Ge
21480 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 70 nerate code to p
21481 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 ut the. **
21482 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 73 65 value of this se
21483 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 lect in a memory
21484 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f 72 64 cell and record
21485 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 the number.
21486 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65 6d 6f ** of the memo
21487 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 ry cell in iColu
21488 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 mn. If this is
21489 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69 74 65 an EXISTS, write
2148a 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69 6e 74 . ** an int
2148b 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78 69 73 eger 0 (not exis
2148c 74 73 29 20 6f 72 20 31 20 28 65 78 69 73 74 73 ts) or 1 (exists
2148d 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72 79 20 ) into a memory
2148e 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6e cell. ** an
2148f 64 20 72 65 63 6f 72 64 20 74 68 61 74 20 6d 65 d record that me
21490 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f mory cell in iCo
21491 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 lumn.. */.
21492 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 static cons
21493 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 t Token one = {
21494 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20 54 6f "1", 1 }; /* To
21495 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61 6c 20 ken for literal
21496 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20 20 20 value 1 */.
21497 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20 Select *pSel;
21498 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21499 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 /* SELECT
2149a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 65 6e statement to en
2149b 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 53 65 code */. Se
2149c 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 lectDest dest;
2149d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2149e 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 65 /* How to de
2149f 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74 20 72 al with SELECt r
214a0 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20 20 20 esult */..
214a1 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d testcase( pExpr-
214a2 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 >op==TK_EXISTS )
214a3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
214a4 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ( pExpr->op==TK_
214a5 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 SELECT );.
214a6 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f assert( pExpr->o
214a7 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c 7c 20 p==TK_EXISTS ||
214a8 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 pExpr->op==TK_SE
214a9 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20 20 61 LECT );.. a
214aa 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 ssert( ExprHasPr
214ab 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
214ac 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 _xIsSelect) );.
214ad 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45 78 70 pSel = pExp
214ae 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 r->x.pSelect;.
214af 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 sqlite3Selec
214b0 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 2c tDestInit(&dest,
214b1 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 0, ++pParse->nM
214b2 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 em);. if( p
214b3 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c Expr->op==TK_SEL
214b4 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20 20 64 ECT ){. d
214b5 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f est.eDest = SRT_
214b6 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c Mem;. sql
214b7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
214b8 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 , OP_Null, 0, de
214b9 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 st.iParm);.
214ba 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
214bb 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75 65 72 v, "Init subquer
214bc 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 y result"));.
214bd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
214be 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 dest.eDest = S
214bf 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20 20 20 RT_Exists;.
214c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
214c1 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
214c2 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 er, 0, dest.iPar
214c3 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 m);. Vdbe
214c4 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 Comment((v, "Ini
214c5 74 20 45 58 49 53 54 53 20 72 65 73 75 6c 74 22 t EXISTS result"
214c6 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ));. }.
214c7 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
214c8 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ete(pParse->db,
214c9 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 pSel->pLimit);.
214ca 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 pSel->pLimi
214cb 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 t = sqlite3PExpr
214cc 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54 45 (pParse, TK_INTE
214cd 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e 65 29 GER, 0, 0, &one)
214ce 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 ;. if( sqli
214cf 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 te3Select(pParse
214d0 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29 20 29 , pSel, &dest) )
214d1 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
214d2 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
214d3 20 20 72 52 65 67 20 3d 20 64 65 73 74 2e 69 50 rReg = dest.iP
214d4 61 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 arm;. ExprS
214d5 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 etIrreducible(pE
214d6 78 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 xpr);. brea
214d7 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 k;. }. }..
214d8 69 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a if( testAddr ){.
214d9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
214da 75 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 umpHere(v, testA
214db 64 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 ddr-1);. }. sq
214dc 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f lite3ExprCachePo
214dd 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 p(pParse, 1);..
214de 20 72 65 74 75 72 6e 20 72 52 65 67 3b 0a 7d 0a return rReg;.}.
214df 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
214e0 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a _OMIT_SUBQUERY *
214e1 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
214e2 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a E_OMIT_SUBQUERY.
214e3 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
214e4 6f 64 65 20 66 6f 72 20 61 6e 20 49 4e 20 65 78 ode for an IN ex
214e5 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 pression..**.**
214e6 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 x IN (SELEC
214e7 54 20 2e 2e 2e 29 0a 2a 2a 20 20 20 20 20 20 78 T ...).** x
214e8 20 49 4e 20 28 76 61 6c 75 65 2c 20 76 61 6c 75 IN (value, valu
214e9 65 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 e, ...).**.** Th
214ea 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 e left-hand side
214eb 20 28 4c 48 53 29 20 69 73 20 61 20 73 63 61 6c (LHS) is a scal
214ec 61 72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 ar expression.
214ed 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 The right-hand s
214ee 69 64 65 20 28 52 48 53 29 0a 2a 2a 20 69 73 20 ide (RHS).** is
214ef 61 6e 20 61 72 72 61 79 20 6f 66 20 7a 65 72 6f an array of zero
214f0 20 6f 72 20 6d 6f 72 65 20 76 61 6c 75 65 73 2e or more values.
214f1 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e The expression
214f2 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 is true if the
214f3 4c 48 53 20 69 73 0a 2a 2a 20 63 6f 6e 74 61 69 LHS is.** contai
214f4 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 ned within the R
214f5 48 53 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f HS. The value o
214f6 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e f the expression
214f7 20 69 73 20 75 6e 6b 6e 6f 77 6e 20 28 4e 55 4c is unknown (NUL
214f8 4c 29 0a 2a 2a 20 69 66 20 74 68 65 20 4c 48 53 L).** if the LHS
214f9 20 69 73 20 4e 55 4c 4c 20 6f 72 20 69 66 20 74 is NULL or if t
214fa 68 65 20 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f he LHS is not co
214fb 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20 74 ntained within t
214fc 68 65 20 52 48 53 20 61 6e 64 20 74 68 65 0a 2a he RHS and the.*
214fd 2a 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 6f * RHS contains o
214fe 6e 65 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 ne or more NULL
214ff 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 values..**.** Th
21500 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 is routine gener
21501 61 74 65 73 20 63 6f 64 65 20 77 69 6c 6c 20 6a ates code will j
21502 75 6d 70 20 74 6f 20 64 65 73 74 49 66 46 61 6c ump to destIfFal
21503 73 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 se if the LHS is
21504 20 6e 6f 74 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e not .** contain
21505 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 ed within the RH
21506 53 2e 20 20 49 66 20 64 75 65 20 74 6f 20 4e 55 S. If due to NU
21507 4c 4c 73 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 LLs we cannot de
21508 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 4c termine if the L
21509 48 53 0a 2a 2a 20 69 73 20 63 6f 6e 74 61 69 6e HS.** is contain
2150a 65 64 20 69 6e 20 74 68 65 20 52 48 53 20 74 68 ed in the RHS th
2150b 65 6e 20 6a 75 6d 70 20 74 6f 20 64 65 73 74 49 en jump to destI
2150c 66 4e 75 6c 6c 2e 20 20 49 66 20 74 68 65 20 4c fNull. If the L
2150d 48 53 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 0a HS is contained.
2150e 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 52 48 ** within the RH
2150f 53 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f S then fall thro
21510 75 67 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ugh..*/.static v
21511 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 oid sqlite3ExprC
21512 6f 64 65 49 4e 28 0a 20 20 50 61 72 73 65 20 2a odeIN(. Parse *
21513 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f pParse, /
21514 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f * Parsing and co
21515 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f de generating co
21516 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 ntext */. Expr
21517 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 *pExpr,
21518 20 2f 2a 20 54 68 65 20 49 4e 20 65 78 70 72 65 /* The IN expre
21519 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 ssion */. int d
2151a 65 73 74 49 66 46 61 6c 73 65 2c 20 20 20 20 20 estIfFalse,
2151b 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 /* Jump here if
2151c 20 4c 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74 LHS is not cont
2151d 61 69 6e 65 64 20 69 6e 20 74 68 65 20 52 48 53 ained in the RHS
2151e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 49 66 */. int destIf
2151f 4e 75 6c 6c 20 20 20 20 20 20 20 20 2f 2a 20 4a Null /* J
21520 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65 20 ump here if the
21521 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 6b 6e results are unkn
21522 6f 77 6e 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73 own due to NULLs
21523 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 52 68 */.){. int rRh
21524 73 48 61 73 4e 75 6c 6c 20 3d 20 30 3b 20 20 2f sHasNull = 0; /
21525 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20 * Register that
21526 69 73 20 74 72 75 65 20 69 66 20 52 48 53 20 63 is true if RHS c
21527 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c 20 76 61 6c ontains NULL val
21528 75 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 ues */. char af
21529 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20 2f finity; /
2152a 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 61 66 66 * Comparison aff
2152b 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a inity to use */.
2152c 20 20 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20 int eType;
2152d 20 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 /* Type
2152e 6f 66 20 74 68 65 20 52 48 53 20 2a 2f 0a 20 20 of the RHS */.
2152f 69 6e 74 20 72 31 3b 20 20 20 20 20 20 20 20 20 int r1;
21530 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 /* Tempora
21531 72 79 20 75 73 65 20 72 65 67 69 73 74 65 72 20 ry use register
21532 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 */. Vdbe *v;
21533 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 /* St
21534 61 74 65 6d 65 6e 74 20 75 6e 64 65 72 20 63 6f atement under co
21535 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 nstruction */..
21536 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 /* Compute the
21537 52 48 53 2e 20 20 20 41 66 74 65 72 20 74 68 69 RHS. After thi
21538 73 20 73 74 65 70 2c 20 74 68 65 20 74 61 62 6c s step, the tabl
21539 65 20 77 69 74 68 20 63 75 72 73 6f 72 0a 20 20 e with cursor.
2153a 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 ** pExpr->iTable
2153b 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 74 will contains t
2153c 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 20 6d he values that m
2153d 61 6b 65 20 75 70 20 74 68 65 20 52 48 53 2e 0a ake up the RHS..
2153e 20 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73 */. v = pPars
2153f 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 e->pVdbe;. asse
21540 72 74 28 20 76 21 3d 30 20 29 3b 20 20 20 20 20 rt( v!=0 );
21541 20 20 2f 2a 20 4f 4f 4d 20 64 65 74 65 63 74 65 /* OOM detecte
21542 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 d prior to this
21543 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 56 64 62 routine */. Vdb
21544 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c eNoopComment((v,
21545 20 22 62 65 67 69 6e 20 49 4e 20 65 78 70 72 22 "begin IN expr"
21546 29 29 3b 0a 20 20 65 54 79 70 65 20 3d 20 73 71 ));. eType = sq
21547 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 lite3FindInIndex
21548 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
21549 26 72 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 0a &rRhsHasNull);..
2154a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 /* Figure out
2154b 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 the affinity to
2154c 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 use to create a
2154d 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 key from the res
2154e 75 6c 74 73 0a 20 20 2a 2a 20 6f 66 20 74 68 65 ults. ** of the
2154f 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61 66 66 expression. aff
21550 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65 73 20 inityStr stores
21551 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 a static string
21552 73 75 69 74 61 62 6c 65 20 66 6f 72 0a 20 20 2a suitable for. *
21553 2a 20 50 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 * P4 of OP_MakeR
21554 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 61 66 ecord.. */. af
21555 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 finity = compari
21556 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 sonAffinity(pExp
21557 72 29 3b 0a 0a 20 20 2f 2a 20 43 6f 64 65 20 74 r);.. /* Code t
21558 68 65 20 4c 48 53 2c 20 74 68 65 20 3c 65 78 70 he LHS, the <exp
21559 72 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 r> from "<expr>
2155a 49 4e 20 28 2e 2e 2e 29 22 2e 0a 20 20 2a 2f 0a IN (...)".. */.
2155b 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
2155c 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a hePush(pParse);.
2155d 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 r1 = sqlite3Ge
2155e 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
2155f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 ;. sqlite3ExprC
21560 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 ode(pParse, pExp
21561 72 2d 3e 70 4c 65 66 74 2c 20 72 31 29 3b 0a 20 r->pLeft, r1);.
21562 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21563 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c p2(v, OP_IsNull,
21564 20 72 31 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 r1, destIfNull)
21565 3b 0a 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d ;... if( eType=
21566 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 =IN_INDEX_ROWID
21567 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 ){. /* In thi
21568 73 20 63 61 73 65 2c 20 74 68 65 20 52 48 53 20 s case, the RHS
21569 69 73 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20 is the ROWID of
2156a 74 61 62 6c 65 20 62 2d 74 72 65 65 0a 20 20 20 table b-tree.
2156b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
2156c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
2156d 4d 75 73 74 42 65 49 6e 74 2c 20 72 31 2c 20 64 MustBeInt, r1, d
2156e 65 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 estIfFalse);.
2156f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21570 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 p3(v, OP_NotExis
21571 74 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c ts, pExpr->iTabl
21572 65 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 e, destIfFalse,
21573 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 r1);. }else{.
21574 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73 /* In this cas
21575 65 2c 20 74 68 65 20 52 48 53 20 69 73 20 61 6e e, the RHS is an
21576 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 0a 20 index b-tree..
21577 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 */. sqlite
21578 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
21579 50 5f 41 66 66 69 6e 69 74 79 2c 20 72 31 2c 20 P_Affinity, r1,
2157a 31 2c 20 30 2c 20 26 61 66 66 69 6e 69 74 79 2c 1, 0, &affinity,
2157b 20 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 1);.. /* If
2157c 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68 the set membersh
2157d 69 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74 ip test fails, t
2157e 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f hen the result o
2157f 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 78 f the . ** "x
21580 20 49 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 IN (...)" expre
21581 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 ssion must be ei
21582 74 68 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 ther 0 or NULL.
21583 49 66 20 74 68 65 20 73 65 74 0a 20 20 20 20 2a If the set. *
21584 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 * contains no NU
21585 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20 LL values, then
21586 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30 2e the result is 0.
21587 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 20 20 If the set .
21588 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 ** contains one
21589 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 or more NULL va
2158a 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20 72 lues, then the r
2158b 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 esult of the.
2158c 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 ** expression i
2158d 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 s also NULL..
2158e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 52 68 73 */. if( rRhs
2158f 48 61 73 4e 75 6c 6c 3d 3d 30 20 7c 7c 20 64 65 HasNull==0 || de
21590 73 74 49 66 46 61 6c 73 65 3d 3d 64 65 73 74 49 stIfFalse==destI
21591 66 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 2f fNull ){. /
21592 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 * This branch ru
21593 6e 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 ns if it is know
21594 6e 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d n at compile tim
21595 65 20 74 68 61 74 20 74 68 65 20 52 48 53 0a 20 e that the RHS.
21596 20 20 20 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 63 ** cannot c
21597 6f 6e 74 61 69 6e 20 4e 55 4c 4c 20 76 61 6c 75 ontain NULL valu
21598 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 es. This happens
21599 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20 as the result.
2159a 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 22 4e 4f ** of a "NO
2159b 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69 T NULL" constrai
2159c 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 nt in the databa
2159d 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 20 se schema..
2159e 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 6c 73 **. ** Als
2159f 6f 20 72 75 6e 20 74 68 69 73 20 62 72 61 6e 63 o run this branc
215a0 68 20 69 66 20 4e 55 4c 4c 20 69 73 20 65 71 75 h if NULL is equ
215a1 69 76 61 6c 65 6e 74 20 74 6f 20 46 41 4c 53 45 ivalent to FALSE
215a2 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 . ** for th
215a3 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 49 4e is particular IN
215a4 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 20 operator..
215a5 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 */. sqlite
215a6 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 3VdbeAddOp4Int(v
215a7 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 70 , OP_NotFound, p
215a8 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 64 65 Expr->iTable, de
215a9 73 74 49 66 46 61 6c 73 65 2c 20 72 31 2c 20 31 stIfFalse, r1, 1
215aa 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 );.. }else{.
215ab 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 /* In this
215ac 62 72 61 6e 63 68 2c 20 74 68 65 20 52 48 53 20 branch, the RHS
215ad 6f 66 20 74 68 65 20 49 4e 20 6d 69 67 68 74 20 of the IN might
215ae 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20 61 contain a NULL a
215af 6e 64 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 nd. ** the
215b0 70 72 65 73 65 6e 63 65 20 6f 66 20 61 20 4e 55 presence of a NU
215b1 4c 4c 20 6f 6e 20 74 68 65 20 52 48 53 20 6d 61 LL on the RHS ma
215b2 6b 65 73 20 61 20 64 69 66 66 65 72 65 6e 63 65 kes a difference
215b3 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a in the. **
215b4 20 6f 75 74 63 6f 6d 65 2e 0a 20 20 20 20 20 20 outcome..
215b5 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 2c */. int j1,
215b6 20 6a 32 2c 20 6a 33 3b 0a 0a 20 20 20 20 20 20 j2, j3;..
215b7 2f 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 /* First check t
215b8 6f 20 73 65 65 20 69 66 20 74 68 65 20 4c 48 53 o see if the LHS
215b9 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e is contained in
215ba 20 74 68 65 20 52 48 53 2e 20 20 49 66 20 73 6f the RHS. If so
215bb 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 ,. ** then
215bc 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66 20 the presence of
215bd 4e 55 4c 4c 73 20 69 6e 20 74 68 65 20 52 48 53 NULLs in the RHS
215be 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 does not matter
215bf 2c 20 73 6f 20 6a 75 6d 70 0a 20 20 20 20 20 20 , so jump.
215c0 2a 2a 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 ** over all of t
215c1 68 65 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c he code that fol
215c2 6c 6f 77 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 lows.. */.
215c3 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 j1 = sqlite
215c4 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 3VdbeAddOp4Int(v
215c5 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 , OP_Found, pExp
215c6 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 31 r->iTable, 0, r1
215c7 2c 20 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 , 1);.. /*
215c8 48 65 72 65 20 77 65 20 62 65 67 69 6e 20 67 65 Here we begin ge
215c9 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 68 nerating code th
215ca 61 74 20 72 75 6e 73 20 69 66 20 74 68 65 20 4c at runs if the L
215cb 48 53 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 HS is not.
215cc 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 ** contained wit
215cd 68 69 6e 20 74 68 65 20 52 48 53 2e 20 20 47 65 hin the RHS. Ge
215ce 6e 65 72 61 74 65 20 61 64 64 69 74 69 6f 6e 61 nerate additiona
215cf 6c 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20 l code that.
215d0 20 20 2a 2a 20 74 65 73 74 73 20 74 68 65 20 52 ** tests the R
215d1 48 53 20 66 6f 72 20 4e 55 4c 4c 73 2e 20 20 49 HS for NULLs. I
215d2 66 20 74 68 65 20 52 48 53 20 63 6f 6e 74 61 69 f the RHS contai
215d3 6e 73 20 61 20 4e 55 4c 4c 20 74 68 65 6e 0a 20 ns a NULL then.
215d4 20 20 20 20 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 ** jump to
215d5 64 65 73 74 49 66 4e 75 6c 6c 2e 20 20 49 66 20 destIfNull. If
215d6 74 68 65 72 65 20 61 72 65 20 6e 6f 20 4e 55 4c there are no NUL
215d7 4c 73 20 69 6e 20 74 68 65 20 52 48 53 20 74 68 Ls in the RHS th
215d8 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6a 75 6d 70 en. ** jump
215d9 20 74 6f 20 64 65 73 74 49 66 46 61 6c 73 65 2e to destIfFalse.
215da 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
215db 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 j2 = sqlite3Vdbe
215dc 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 AddOp1(v, OP_Not
215dd 4e 75 6c 6c 2c 20 72 52 68 73 48 61 73 4e 75 6c Null, rRhsHasNul
215de 6c 29 3b 0a 20 20 20 20 20 20 6a 33 20 3d 20 73 l);. j3 = s
215df 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
215e0 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c Int(v, OP_Found,
215e1 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 pExpr->iTable,
215e2 30 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 0, rRhsHasNull,
215e3 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
215e4 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
215e5 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 P_Integer, -1, r
215e6 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a 20 20 20 RhsHasNull);.
215e7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
215e8 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a 20 mpHere(v, j3);.
215e9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
215ea 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 AddOp2(v, OP_Add
215eb 49 6d 6d 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c Imm, rRhsHasNull
215ec 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 1);. sqli
215ed 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
215ee 76 2c 20 6a 32 29 3b 0a 0a 20 20 20 20 20 20 2f v, j2);.. /
215ef 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 61 70 * Jump to the ap
215f0 70 72 6f 70 72 69 61 74 65 20 74 61 72 67 65 74 propriate target
215f1 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 depending on wh
215f2 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20 ether or not.
215f3 20 20 20 2a 2a 20 74 68 65 20 52 48 53 20 63 6f ** the RHS co
215f4 6e 74 61 69 6e 73 20 61 20 4e 55 4c 4c 0a 20 20 ntains a NULL.
215f5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
215f6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
215f7 2c 20 4f 50 5f 49 66 2c 20 72 52 68 73 48 61 73 , OP_If, rRhsHas
215f8 4e 75 6c 6c 2c 20 64 65 73 74 49 66 4e 75 6c 6c Null, destIfNull
215f9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
215fa 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
215fb 5f 47 6f 74 6f 2c 20 30 2c 20 64 65 73 74 49 66 _Goto, 0, destIf
215fc 46 61 6c 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f False);.. /
215fd 2a 20 54 68 65 20 4f 50 5f 46 6f 75 6e 64 20 61 * The OP_Found a
215fe 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69 t the top of thi
215ff 73 20 62 72 61 6e 63 68 20 6a 75 6d 70 73 20 68 s branch jumps h
21600 65 72 65 20 77 68 65 6e 20 74 72 75 65 2c 20 0a ere when true, .
21601 20 20 20 20 20 20 2a 2a 20 63 61 75 73 69 6e 67 ** causing
21602 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 49 4e 20 the overall IN
21603 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 expression evalu
21604 61 74 69 6f 6e 20 74 6f 20 66 61 6c 6c 20 74 68 ation to fall th
21605 72 6f 75 67 68 2e 0a 20 20 20 20 20 20 2a 2f 0a rough.. */.
21606 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21607 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 eJumpHere(v, j1)
21608 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 ;. }. }. sq
21609 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
2160a 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b Reg(pParse, r1);
2160b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 . sqlite3ExprCa
2160c 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
2160d 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 );. VdbeComment
2160e 28 28 76 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 ((v, "end IN exp
2160f 72 22 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f r"));.}.#endif /
21610 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 * SQLITE_OMIT_SU
21611 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a BQUERY */../*.**
21612 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d Duplicate an 8-
21613 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 byte value.*/.st
21614 61 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 atic char *dup8b
21615 79 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f ytes(Vdbe *v, co
21616 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 nst char *in){.
21617 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c char *out = sql
21618 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 ite3DbMallocRaw(
21619 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 sqlite3VdbeDb(v)
2161a 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 , 8);. if( out
2161b 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 ){. memcpy(ou
2161c 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 t, in, 8);. }.
2161d 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a return out;.}..
2161e 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 /*.** Generate a
2161f 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 n instruction th
21620 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 at will put the
21621 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a floating point.*
21622 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 * value describe
21623 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 d by z[0..n-1] i
21624 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 nto register iMe
21625 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d m..**.** The z[]
21626 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f string will pro
21627 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 bably not be zer
21628 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 o-terminated. B
21629 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 ut the .** z[n]
2162a 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 character is gua
2162b 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f ranteed to be so
2162c 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 mething that doe
2162d 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 s not look.** li
2162e 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 ke the continuat
2162f 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 ion of the numbe
21630 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 r..*/.static voi
21631 64 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 d codeReal(Vdbe
21632 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a *v, const char *
21633 7a 2c 20 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 z, int negateFla
21634 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 g, int iMem){.
21635 69 66 28 20 41 4c 57 41 59 53 28 7a 21 3d 30 29 if( ALWAYS(z!=0)
21636 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 ){. double v
21637 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72 20 2a alue;. char *
21638 7a 56 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 zV;. sqlite3A
21639 74 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a toF(z, &value);.
2163a 20 20 20 20 61 73 73 65 72 74 28 20 21 73 71 6c assert( !sql
2163b 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 ite3IsNaN(value)
2163c 20 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77 20 41 ); /* The new A
2163d 74 6f 46 20 6e 65 76 65 72 20 72 65 74 75 72 6e toF never return
2163e 73 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69 66 28 s NaN */. if(
2163f 20 6e 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 negateFlag ) va
21640 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 lue = -value;.
21641 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73 zV = dup8bytes
21642 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75 (v, (char*)&valu
21643 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 e);. sqlite3V
21644 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
21645 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 Real, 0, iMem, 0
21646 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a , zV, P4_REAL);.
21647 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 }.}.../*.** Ge
21648 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74 72 75 nerate an instru
21649 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 ction that will
2164a 70 75 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 put the integer
2164b 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 describe by.** t
2164c 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e ext z[0..n-1] in
2164d 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d to register iMem
2164e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 ..**.** The z[]
2164f 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 string will prob
21650 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f ably not be zero
21651 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 -terminated. Bu
21652 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 t the .** z[n] c
21653 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72 haracter is guar
21654 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d anteed to be som
21655 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 ething that does
21656 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b not look.** lik
21657 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 e the continuati
21658 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 on of the number
21659 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
2165a 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56 64 62 codeInteger(Vdb
2165b 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45 78 70 e *v, Expr *pExp
2165c 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 r, int negFlag,
2165d 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 int iMem){. if(
2165e 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 pExpr->flags &
2165f 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 EP_IntValue ){.
21660 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78 70 72 int i = pExpr
21661 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20 ->u.iValue;.
21662 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 69 20 if( negFlag ) i
21663 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 = -i;. sqlite
21664 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
21665 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d P_Integer, i, iM
21666 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 em);. }else{.
21667 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 const char *z
21668 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 = pExpr->u.zToke
21669 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a n;. assert( z
2166a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 !=0 );. if( s
2166b 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69 qlite3FitsIn64Bi
2166c 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 ts(z, negFlag) )
2166d 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 {. i64 valu
2166e 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a e;. char *z
2166f 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 V;. sqlite3
21670 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 Atoi64(z, &value
21671 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 );. if( neg
21672 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d 20 2d Flag ) value = -
21673 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 value;. zV
21674 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20 28 = dup8bytes(v, (
21675 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 char*)&value);.
21676 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21677 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 AddOp4(v, OP_Int
21678 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 64, 0, iMem, 0,
21679 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 zV, P4_INT64);.
2167a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2167b 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e codeReal(v, z, n
2167c 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 egFlag, iMem);.
2167d 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a }. }.}../*.*
2167e 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68 65 20 * Clear a cache
2167f 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 entry..*/.static
21680 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74 72 79 void cacheEntry
21681 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 Clear(Parse *pPa
21682 72 73 65 2c 20 73 74 72 75 63 74 20 79 43 6f 6c rse, struct yCol
21683 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 Cache *p){. if(
21684 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 p->tempReg ){.
21685 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e if( pParse->n
21686 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53 69 7a TempReg<ArraySiz
21687 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 e(pParse->aTempR
21688 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70 50 61 eg) ){. pPa
21689 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 rse->aTempReg[pP
2168a 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b arse->nTempReg++
2168b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 ] = p->iReg;.
2168c 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 }. p->tempRe
2168d 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f g = 0;. }.}.../
2168e 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 *.** Record in t
2168f 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 he column cache
21690 74 68 61 74 20 61 20 70 61 72 74 69 63 75 6c 61 that a particula
21691 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a r column from a.
21692 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 ** particular ta
21693 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e ble is stored in
21694 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 a particular re
21695 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 gister..*/.SQLIT
21696 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 E_PRIVATE void s
21697 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 qlite3ExprCacheS
21698 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50 61 72 tore(Parse *pPar
21699 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 69 6e se, int iTab, in
2169a 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 t iCol, int iReg
2169b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e ){. int i;. in
2169c 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 t minLru;. int
2169d 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 idxLru;. struct
2169e 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a yColCache *p;..
2169f 20 20 61 73 73 65 72 74 28 20 69 52 65 67 3e 30 assert( iReg>0
216a0 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74 65 72 ); /* Register
216a1 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 77 numbers are alw
216a2 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a ays positive */.
216a3 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d assert( iCol>=
216a4 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37 36 38 -1 && iCol<32768
216a5 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65 20 63 ); /* Finite c
216a6 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 2a 2f olumn numbers */
216a7 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 72 65 70 .. /* First rep
216a8 6c 61 63 65 20 61 6e 79 20 65 78 69 73 74 69 6e lace any existin
216a9 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66 6f 72 g entry */. for
216aa 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e (i=0, p=pParse->
216ab 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c aColCache; i<SQL
216ac 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 ITE_N_COLCACHE;
216ad 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 i++, p++){. i
216ae 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d f( p->iReg && p-
216af 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 >iTable==iTab &&
216b0 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f p->iColumn==iCo
216b1 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 l ){. cache
216b2 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 EntryClear(pPars
216b3 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e e, p);. p->
216b4 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d iLevel = pParse-
216b5 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 >iCacheLevel;.
216b6 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 p->iReg = iR
216b7 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 eg;. p->aff
216b8 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 Change = 0;.
216b9 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 p->lru = pPars
216ba 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a e->iCacheCnt++;.
216bb 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 return;.
216bc 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 }. }.. /* Fi
216bd 6e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c 6f 74 nd an empty slot
216be 20 61 6e 64 20 72 65 70 6c 61 63 65 20 69 74 20 and replace it
216bf 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d */. for(i=0, p=
216c0 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
216c1 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f e; i<SQLITE_N_CO
216c2 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b LCACHE; i++, p++
216c3 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 52 ){. if( p->iR
216c4 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 eg==0 ){. p
216c5 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 ->iLevel = pPars
216c6 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a e->iCacheLevel;.
216c7 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 p->iTable
216c8 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 70 2d = iTab;. p-
216c9 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b >iColumn = iCol;
216ca 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d . p->iReg =
216cb 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e iReg;. p->
216cc 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 affChange = 0;.
216cd 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 p->tempReg
216ce 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 = 0;. p->lr
216cf 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 u = pParse->iCac
216d0 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 heCnt++;. r
216d1 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d eturn;. }. }
216d2 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 .. /* Replace t
216d3 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74 6c 79 he last recently
216d4 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 used */. minLr
216d5 75 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a u = 0x7fffffff;.
216d6 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 idxLru = -1;.
216d7 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 for(i=0, p=pPar
216d8 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 se->aColCache; i
216d9 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 <SQLITE_N_COLCAC
216da 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 HE; i++, p++){.
216db 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 if( p->lru<mi
216dc 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20 69 64 nLru ){. id
216dd 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20 20 20 xLru = i;.
216de 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b minLru = p->lru;
216df 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 . }. }. if(
216e0 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75 3e 3d ALWAYS(idxLru>=
216e1 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 0) ){. p = &p
216e2 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 Parse->aColCache
216e3 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d [idxLru];. p-
216e4 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 >iLevel = pParse
216e5 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 ->iCacheLevel;.
216e6 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 p->iTable = i
216e7 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c Tab;. p->iCol
216e8 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 umn = iCol;.
216e9 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a p->iReg = iReg;.
216ea 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 p->affChange
216eb 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74 65 6d = 0;. p->tem
216ec 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 70 2d pReg = 0;. p-
216ed 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 >lru = pParse->i
216ee 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 CacheCnt++;.
216ef 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f return;. }.}../
216f0 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20 74 68 *.** Indicate th
216f1 61 74 20 61 20 72 65 67 69 73 74 65 72 20 69 73 at a register is
216f2 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74 74 being overwritt
216f3 65 6e 2e 20 20 50 75 72 67 65 20 74 68 65 20 72 en. Purge the r
216f4 65 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 egister.** from
216f5 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 the column cache
216f6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 ..*/.SQLITE_PRIV
216f7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 ATE void sqlite3
216f8 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 ExprCacheRemove(
216f9 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 Parse *pParse, i
216fa 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 nt iReg){. int
216fb 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c i;. struct yCol
216fc 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 Cache *p;. for(
216fd 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 i=0, p=pParse->a
216fe 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 ColCache; i<SQLI
216ff 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 TE_N_COLCACHE; i
21700 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 ++, p++){. if
21701 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 ( p->iReg==iReg
21702 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e ){. cacheEn
21703 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c tryClear(pParse,
21704 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 p);. p->iR
21705 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 eg = 0;. }.
21706 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d }.}../*.** Remem
21707 62 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 ber the current
21708 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63 6f 6e column cache con
21709 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77 20 65 text. Any new e
2170a 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a 2a 20 ntries added.**
2170b 61 64 64 65 64 20 74 6f 20 74 68 65 20 63 6f 6c added to the col
2170c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65 72 20 umn cache after
2170d 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20 72 65 this call are re
2170e 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65 0a 2a moved when the.*
2170f 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 * corresponding
21710 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 pop occurs..*/.S
21711 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f QLITE_PRIVATE vo
21712 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 id sqlite3ExprCa
21713 63 68 65 50 75 73 68 28 50 61 72 73 65 20 2a 70 chePush(Parse *p
21714 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72 73 65 Parse){. pParse
21715 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b ->iCacheLevel++;
21716 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 .}../*.** Remove
21717 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e from the column
21718 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74 72 69 cache any entri
21719 65 73 20 74 68 61 74 20 77 65 72 65 20 61 64 64 es that were add
2171a 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20 ed since the.**
2171b 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e 20 50 the previous N P
2171c 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 ush operations.
2171d 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c In other words,
2171e 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 61 63 restore the cac
2171f 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 he.** to the sta
21720 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e 20 50 te it was in N P
21721 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 ushes ago..*/.SQ
21722 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
21723 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
21724 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70 50 61 hePop(Parse *pPa
21725 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 rse, int N){. i
21726 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 nt i;. struct y
21727 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 61 ColCache *p;. a
21728 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a 20 20 ssert( N>0 );.
21729 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e assert( pParse->
2172a 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 iCacheLevel>=N )
2172b 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43 61 63 ;. pParse->iCac
2172c 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 heLevel -= N;.
2172d 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
2172e 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
2172f 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
21730 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
21731 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 if( p->iReg &&
21732 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73 p->iLevel>pPars
21733 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 20 29 e->iCacheLevel )
21734 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 {. cacheEnt
21735 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 ryClear(pParse,
21736 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 p);. p->iRe
21737 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d g = 0;. }. }
21738 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 .}../*.** When a
21739 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 69 cached column i
2173a 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65 20 73 s reused, make s
2173b 75 72 65 20 74 68 61 74 20 69 74 73 20 72 65 67 ure that its reg
2173c 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c ister is.** no l
2173d 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c 65 20 onger available
2173e 61 73 20 61 20 74 65 6d 70 20 72 65 67 69 73 74 as a temp regist
2173f 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33 38 37 er. ticket #387
21740 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 9: that same.**
21741 20 72 65 67 69 73 74 65 72 20 6d 69 67 68 74 20 register might
21742 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 be in the cache
21743 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 in multiple plac
21744 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65 20 74 es, so be sure t
21745 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20 61 6c o.** get them al
21746 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 l..*/.static voi
21747 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 d sqlite3ExprCac
21748 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 50 61 hePinRegister(Pa
21749 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 rse *pParse, int
2174a 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b iReg){. int i;
2174b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 . struct yColCa
2174c 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d che *p;. for(i=
2174d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 0, p=pParse->aCo
2174e 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 lCache; i<SQLITE
2174f 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b _N_COLCACHE; i++
21750 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 , p++){. if(
21751 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b p->iReg==iReg ){
21752 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 . p->tempRe
21753 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d g = 0;. }. }
21754 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 .}../*.** Genera
21755 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c te code that wil
21756 6c 20 65 78 74 72 61 63 74 20 74 68 65 20 69 43 l extract the iC
21757 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 olumn-th column
21758 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 from.** table pT
21759 61 62 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 ab and store the
2175a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e column value in
2175b 20 61 20 72 65 67 69 73 74 65 72 2e 20 20 41 6e a register. An
2175c 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 effort.** is ma
2175d 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 de to store the
2175e 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 column value in
2175f 72 65 67 69 73 74 65 72 20 69 52 65 67 2c 20 62 register iReg, b
21760 75 74 20 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f ut this is.** no
21761 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 20 54 t guaranteed. T
21762 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 he location of t
21763 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 he column value
21764 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a is returned..**.
21765 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 ** There must be
21766 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 an open cursor
21767 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61 62 6c to pTab in iTabl
21768 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 e when this rout
21769 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 ine.** is called
2176a 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 . If iColumn<0
2176b 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65 6e then code is gen
2176c 65 72 61 74 65 64 20 74 68 61 74 20 65 78 74 72 erated that extr
2176d 61 63 74 73 20 74 68 65 20 72 6f 77 69 64 2e 0a acts the rowid..
2176e 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
2176f 6e 65 20 6d 69 67 68 74 20 61 74 74 65 6d 70 74 ne might attempt
21770 20 74 6f 20 72 65 75 73 65 20 74 68 65 20 76 61 to reuse the va
21771 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d lue of the colum
21772 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 61 6c n that.** has al
21773 72 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 ready been loade
21774 64 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 d into a registe
21775 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 77 69 r. The value wi
21776 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 ll always.** be
21777 75 73 65 64 20 69 66 20 69 74 20 68 61 73 20 6e used if it has n
21778 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 61 6e 79 ot undergone any
21779 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 affinity change
2177a 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20 61 6e s. But if.** an
2177b 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 affinity change
2177c 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c 20 74 has occurred, t
2177d 68 65 6e 20 74 68 65 20 63 61 63 68 65 64 20 76 hen the cached v
2177e 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 alue will only b
2177f 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 61 6c 6c e.** used if all
21780 6f 77 41 66 66 43 68 6e 67 20 69 73 20 74 72 75 owAffChng is tru
21781 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 e..*/.SQLITE_PRI
21782 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 VATE int sqlite3
21783 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d ExprCodeGetColum
21784 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 n(. Parse *pPar
21785 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 se, /* Parsing
21786 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 and code genera
21787 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a ting context */.
21788 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 Table *pTab,
21789 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74 69 6f /* Descriptio
2178a 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 n of the table w
2178b 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 66 72 e are reading fr
2178c 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c om */. int iCol
2178d 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e 64 65 umn, /* Inde
2178e 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 x of the table c
2178f 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 olumn */. int i
21790 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 Table, /* T
21791 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 he cursor pointi
21792 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 ng to the table
21793 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 20 */. int iReg,
21794 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 72 /* Store r
21795 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 esults here */.
21796 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43 68 6e int allowAffChn
21797 67 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 69 g /* True if pri
21798 6f 72 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e or affinity chan
21799 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b ges are OK */.){
2179a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 . Vdbe *v = pPa
2179b 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e rse->pVdbe;. in
2179c 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 t i;. struct yC
2179d 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 olCache *p;.. f
2179e 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 or(i=0, p=pParse
2179f 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 ->aColCache; i<S
217a0 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 QLITE_N_COLCACHE
217a1 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 ; i++, p++){.
217a2 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30 20 26 if( p->iReg>0 &
217a3 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 & p->iTable==iTa
217a4 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d ble && p->iColum
217a5 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 n==iColumn.
217a6 20 20 20 20 20 20 26 26 20 28 21 70 2d 3e 61 66 && (!p->af
217a7 66 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 fChange || allow
217a8 41 66 66 43 68 6e 67 29 20 29 7b 0a 20 20 20 20 AffChng) ){.
217a9 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 p->lru = pPars
217aa 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a e->iCacheCnt++;.
217ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
217ac 72 43 61 63 68 65 50 69 6e 52 65 67 69 73 74 65 rCachePinRegiste
217ad 72 28 70 50 61 72 73 65 2c 20 70 2d 3e 69 52 65 r(pParse, p->iRe
217ae 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e g);. return
217af 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a p->iReg;. }.
217b0 20 20 7d 20 20 0a 20 20 61 73 73 65 72 74 28 20 } . assert(
217b1 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 43 v!=0 );. if( iC
217b2 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 olumn<0 ){. s
217b3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
217b4 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 (v, OP_Rowid, iT
217b5 61 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d able, iReg);. }
217b6 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 else if( ALWAYS(
217b7 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20 pTab!=0) ){.
217b8 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 72 74 75 int op = IsVirtu
217b9 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f 56 43 al(pTab) ? OP_VC
217ba 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d olumn : OP_Colum
217bb 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 n;. sqlite3Vd
217bc 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 beAddOp3(v, op,
217bd 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c iTable, iColumn,
217be 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 iReg);. sqli
217bf 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 te3ColumnDefault
217c0 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c 75 6d (v, pTab, iColum
217c1 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a 20 20 n, iReg);. }.
217c2 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
217c3 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 54 Store(pParse, iT
217c4 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 able, iColumn, i
217c5 52 65 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 Reg);. return i
217c6 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c Reg;.}../*.** Cl
217c7 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 ear all column c
217c8 61 63 68 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f ache entries..*/
217c9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
217ca 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 void sqlite3Expr
217cb 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65 CacheClear(Parse
217cc 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 *pParse){. int
217cd 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f i;. struct yCo
217ce 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f lCache *p;.. fo
217cf 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d r(i=0, p=pParse-
217d0 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 >aColCache; i<SQ
217d1 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b LITE_N_COLCACHE;
217d2 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 i++, p++){.
217d3 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 if( p->iReg ){.
217d4 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 cacheEntryC
217d5 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b lear(pParse, p);
217d6 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d . p->iReg =
217d7 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0;. }. }.}.
217d8 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 ./*.** Record th
217d9 65 20 66 61 63 74 20 74 68 61 74 20 61 6e 20 61 e fact that an a
217da 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 ffinity change h
217db 61 73 20 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 as occurred on i
217dc 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 Count.** registe
217dd 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 rs starting with
217de 20 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 iStart..*/.SQLI
217df 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
217e0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
217e1 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 50 AffinityChange(P
217e2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e arse *pParse, in
217e3 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20 69 43 t iStart, int iC
217e4 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e ount){. int iEn
217e5 64 20 3d 20 69 53 74 61 72 74 20 2b 20 69 43 6f d = iStart + iCo
217e6 75 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 unt - 1;. int i
217e7 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 ;. struct yColC
217e8 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 ache *p;. for(i
217e9 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 =0, p=pParse->aC
217ea 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 olCache; i<SQLIT
217eb 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b E_N_COLCACHE; i+
217ec 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 +, p++){. int
217ed 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 r = p->iReg;.
217ee 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72 74 20 if( r>=iStart
217ef 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 && r<=iEnd ){.
217f0 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 p->affChange
217f1 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a = 1;. }. }.
217f2 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
217f3 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 e code to move c
217f4 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 ontent from regi
217f5 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 sters iFrom...iF
217f6 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 rom+nReg-1.** ov
217f7 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e er to iTo..iTo+n
217f8 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 Reg-1. Keep the
217f9 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75 70 2d column cache up-
217fa 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 to-date..*/.SQLI
217fb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
217fc 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d sqlite3ExprCodeM
217fd 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 ove(Parse *pPars
217fe 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e e, int iFrom, in
217ff 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 t iTo, int nReg)
21800 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 {. int i;. str
21801 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 uct yColCache *p
21802 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69 46 ;. if( NEVER(iF
21803 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65 74 75 rom==iTo) ) retu
21804 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 rn;. sqlite3Vdb
21805 65 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e eAddOp3(pParse->
21806 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 pVdbe, OP_Move,
21807 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 iFrom, iTo, nReg
21808 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d );. for(i=0, p=
21809 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 pParse->aColCach
2180a 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f e; i<SQLITE_N_CO
2180b 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b LCACHE; i++, p++
2180c 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 ){. int x = p
2180d 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 ->iReg;. if(
2180e 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69 46 x>=iFrom && x<iF
2180f 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 rom+nReg ){.
21810 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69 54 6f p->iReg += iTo
21811 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 -iFrom;. }.
21812 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 }.}../*.** Gener
21813 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 ate code to copy
21814 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 content from re
21815 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e gisters iFrom...
21816 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 iFrom+nReg-1.**
21817 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f over to iTo..iTo
21818 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 +nReg-1..*/.SQLI
21819 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 TE_PRIVATE void
2181a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 sqlite3ExprCodeC
2181b 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 opy(Parse *pPars
2181c 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e e, int iFrom, in
2181d 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 t iTo, int nReg)
2181e 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 {. int i;. if(
2181f 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 NEVER(iFrom==iT
21820 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 o) ) return;. f
21821 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 or(i=0; i<nReg;
21822 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 i++){. sqlite
21823 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61 72 3VdbeAddOp2(pPar
21824 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f se->pVdbe, OP_Co
21825 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f py, iFrom+i, iTo
21826 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a +i);. }.}../*.*
21827 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 * Return true if
21828 20 61 6e 79 20 72 65 67 69 73 74 65 72 20 69 6e any register in
21829 20 74 68 65 20 72 61 6e 67 65 20 69 46 72 6f 6d the range iFrom
2182a 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 ..iTo (inclusive
2182b 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61 73 20 ).** is used as
2182c 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 part of the colu
2182d 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 mn cache..*/.sta
2182e 74 69 63 20 69 6e 74 20 75 73 65 64 41 73 43 6f tic int usedAsCo
2182f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73 65 20 lumnCache(Parse
21830 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46 72 *pParse, int iFr
21831 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 om, int iTo){.
21832 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 int i;. struct
21833 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 yColCache *p;.
21834 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 for(i=0, p=pPars
21835 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c e->aColCache; i<
21836 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 SQLITE_N_COLCACH
21837 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 E; i++, p++){.
21838 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 int r = p->iRe
21839 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 46 g;. if( r>=iF
2183a 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 rom && r<=iTo )
2183b 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 return 1;. }.
2183c 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
2183d 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20 69 ** If the last i
2183e 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64 65 64 nstruction coded
2183f 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72 61 6c is an ephemeral
21840 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a copy of any of.
21841 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 73 ** the registers
21842 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72 65 67 in the nReg reg
21843 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 isters beginning
21844 20 77 69 74 68 20 69 52 65 67 2c 20 74 68 65 6e with iReg, then
21845 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68 65 20 .** convert the
21846 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e last instruction
21847 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74 from OP_SCopy t
21848 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 o OP_Copy..*/.SQ
21849 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 LITE_PRIVATE voi
2184a 64 20 73 71 6c 69 74 65 33 45 78 70 72 48 61 72 d sqlite3ExprHar
2184b 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61 dCopy(Parse *pPa
2184c 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 rse, int iReg, i
2184d 6e 74 20 6e 52 65 67 29 7b 0a 20 20 56 64 62 65 nt nReg){. Vdbe
2184e 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 Op *pOp;. Vdbe
2184f 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 *v;.. assert( p
21850 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
21851 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 cFailed==0 );.
21852 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
21853 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d e;. assert( v!=
21854 30 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 0 );. pOp = sql
21855 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c ite3VdbeGetOp(v,
21856 20 2d 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 -1);. assert(
21857 70 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 pOp!=0 );. if(
21858 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f pOp->opcode==OP_
21859 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 SCopy && pOp->p1
2185a 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 >=iReg && pOp->p
2185b 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 1<iReg+nReg ){.
2185c 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d pOp->opcode =
2185d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a OP_Copy;. }.}.
2185e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
2185f 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68 code to store th
21860 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 e value of the i
21861 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 Alias-th alias i
21862 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 n register.** ta
21863 72 67 65 74 2e 20 20 54 68 65 20 66 69 72 73 74 rget. The first
21864 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61 time this is ca
21865 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73 20 65 lled, pExpr is e
21866 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 valuated to comp
21867 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 ute.** the value
21868 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e 20 20 of the alias.
21869 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f The value is sto
2186a 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 red in an auxili
2186b 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 ary register.**
2186c 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f and the number o
2186d 66 20 74 68 61 74 20 72 65 67 69 73 74 65 72 20 f that register
2186e 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e is returned. On
2186f 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c subsequent call
21870 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 s,.** the regist
21871 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72 65 74 er number is ret
21872 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20 67 65 urned without ge
21873 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 nerating any cod
21874 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 e..**.** Note th
21875 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 at in order for
21876 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f this to work, co
21877 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e 65 72 de must be gener
21878 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 ated in the.** s
21879 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74 20 69 ame order that i
2187a 74 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a t is executed..*
2187b 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61 72 65 *.** Aliases are
2187c 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72 74 69 numbered starti
2187d 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f 20 69 ng with 1. So i
2187e 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68 65 20 Alias is in the
2187f 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f range.** of 1 to
21880 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 pParse->nAlias
21881 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a inclusive. .**.
21882 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 ** pParse->aAlia
21883 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f s[iAlias-1] reco
21884 72 64 73 20 74 68 65 20 72 65 67 69 73 74 65 72 rds the register
21885 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68 number where th
21886 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 e value.** of th
21887 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 e iAlias-th alia
21888 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20 49 66 s is stored. If
21889 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e zero, that mean
2188a 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c s that the.** al
2188b 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65 74 20 ias has not yet
2188c 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a been computed..*
2188d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 /.static int cod
2188e 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a 70 50 eAlias(Parse *pP
2188f 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 arse, int iAlias
21890 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 , Expr *pExpr, i
21891 6e 74 20 74 61 72 67 65 74 29 7b 0a 23 69 66 20 nt target){.#if
21892 30 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 0. sqlite3 *db
21893 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 = pParse->db;.
21894 69 6e 74 20 69 52 65 67 3b 0a 20 20 69 66 28 20 int iReg;. if(
21895 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c pParse->nAliasAl
21896 6c 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41 6c 69 loc<pParse->nAli
21897 61 73 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 as ){. pParse
21898 2d 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 ->aAlias = sqlit
21899 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 e3DbReallocOrFre
2189a 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 e(db, pParse->aA
2189b 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 lias,.
2189c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2189d 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 50 sizeof(pP
2189e 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 arse->aAlias[0])
2189f 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 *pParse->nAlias
218a0 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
218a1 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
218a2 64 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 41 6c d && pParse->nAl
218a3 69 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 iasAlloc>0 );.
218a4 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
218a5 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 Failed ) return
218a6 30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 0;. memset(&p
218a7 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 70 50 Parse->aAlias[pP
218a8 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f arse->nAliasAllo
218a9 63 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 c], 0,.
218aa 20 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 (pParse->nAlia
218ab 73 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 s-pParse->nAlias
218ac 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28 70 50 Alloc)*sizeof(pP
218ad 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 arse->aAlias[0])
218ae 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e );. pParse->n
218af 41 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70 50 61 AliasAlloc = pPa
218b0 72 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d rse->nAlias;. }
218b1 0a 20 20 61 73 73 65 72 74 28 20 69 41 6c 69 61 . assert( iAlia
218b2 73 3e 30 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 s>0 && iAlias<=p
218b3 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b Parse->nAlias );
218b4 0a 20 20 69 52 65 67 20 3d 20 70 50 61 72 73 65 . iReg = pParse
218b5 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d ->aAlias[iAlias-
218b6 31 5d 3b 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 1];. if( iReg==
218b7 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 0 ){. if( pPa
218b8 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c rse->iCacheLevel
218b9 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 52 65 67 >0 ){. iReg
218ba 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
218bb 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c deTarget(pParse,
218bc 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b pExpr, target);
218bd 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
218be 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 iReg = ++pPars
218bf 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 e->nMem;. s
218c0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
218c1 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 69 52 Parse, pExpr, iR
218c2 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 eg);. pPars
218c3 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 e->aAlias[iAlias
218c4 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 -1] = iReg;.
218c5 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 }. }. return i
218c6 52 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 Reg;.#else. UNU
218c7 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 69 41 SED_PARAMETER(iA
218c8 6c 69 61 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 lias);. return
218c9 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
218ca 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
218cb 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 23 65 xpr, target);.#e
218cc 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 ndif.}../*.** Ge
218cd 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f nerate code into
218ce 20 74 68 65 20 63 75 72 72 65 6e 74 20 56 64 62 the current Vdb
218cf 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 e to evaluate th
218d0 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 e given.** expre
218d1 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 ssion. Attempt
218d2 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 to store the res
218d3 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 ults in register
218d4 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 "target"..** Re
218d5 74 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 turn the registe
218d6 72 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 r where results
218d7 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a are stored..**.*
218d8 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f 75 74 * With this rout
218d9 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f ine, there is no
218da 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 guarantee that
218db 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 results will.**
218dc 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 61 72 be stored in tar
218dd 67 65 74 2e 20 20 54 68 65 20 72 65 73 75 6c 74 get. The result
218de 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72 65 64 might be stored
218df 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a in some other.*
218e0 2a 20 72 65 67 69 73 74 65 72 20 69 66 20 69 74 * register if it
218e1 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 is convenient t
218e2 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 o do so. The ca
218e3 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a lling function.*
218e4 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68 65 * must check the
218e5 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 return code and
218e6 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75 6c 74 move the result
218e7 73 20 74 6f 20 74 68 65 20 64 65 73 69 72 65 64 s to the desired
218e8 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f .** register..*/
218e9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 .SQLITE_PRIVATE
218ea 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 int sqlite3ExprC
218eb 6f 64 65 54 61 72 67 65 74 28 50 61 72 73 65 20 odeTarget(Parse
218ec 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 *pParse, Expr *p
218ed 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 Expr, int target
218ee 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 ){. Vdbe *v = p
218ef 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f Parse->pVdbe; /
218f0 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72 20 63 * The VM under c
218f1 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 onstruction */.
218f2 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 int op;
218f3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 /* Th
218f4 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67 20 63 e opcode being c
218f5 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e oded */. int in
218f6 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20 20 20 Reg = target;
218f7 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 /* Results s
218f8 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 tored in registe
218f9 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 r inReg */. int
218fa 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b 20 20 regFree1 = 0;
218fb 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e /* If non
218fc 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73 20 -zero free this
218fd 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
218fe 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 er */. int regF
218ff 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20 20 20 ree2 = 0;
21900 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f /* If non-zero
21901 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70 6f free this tempo
21902 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a 2f rary register */
21903 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 . int r1, r2, r
21904 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 3, r4; /*
21905 56 61 72 69 6f 75 73 20 72 65 67 69 73 74 65 72 Various register
21906 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 numbers */. sq
21907 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
21908 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 se->db; /* The d
21909 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
2190a 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 on */.. assert(
2190b 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 target>0 && tar
2190c 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 get<=pParse->nMe
2190d 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 m );. if( v==0
2190e 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
2190f 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f Parse->db->mallo
21910 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 cFailed );. r
21911 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 eturn 0;. }..
21912 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a if( pExpr==0 ){.
21913 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c op = TK_NULL
21914 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f ;. }else{. o
21915 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 p = pExpr->op;.
21916 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 }. switch( op
21917 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 ){. case TK_A
21918 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 GG_COLUMN: {.
21919 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 AggInfo *pAgg
2191a 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 Info = pExpr->pA
2191b 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 ggInfo;. st
2191c 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c ruct AggInfo_col
2191d 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e *pCol = &pAggIn
2191e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e fo->aCol[pExpr->
2191f 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 iAgg];. if(
21920 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 !pAggInfo->dire
21921 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 ctMode ){.
21922 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e assert( pCol->
21923 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 iMem>0 );.
21924 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e inReg = pCol->
21925 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 iMem;. br
21926 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 eak;. }else
21927 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 if( pAggInfo->u
21928 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a seSortingIdx ){.
21929 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
2192a 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
2192b 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f Column, pAggInfo
2192c 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 ->sortingIdx,.
2192d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2192e 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c pCol
2192f 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c ->iSorterColumn,
21930 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 target);.
21931 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
21932 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 . /* Otherw
21933 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 ise, fall thru i
21934 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d nto the TK_COLUM
21935 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a N case */. }.
21936 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 case TK_COLU
21937 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 MN: {. if(
21938 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 pExpr->iTable<0
21939 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
2193a 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 is only happens
2193b 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 when coding chec
2193c 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f k constraints */
2193d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
2193e 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e pParse->ckBase>
2193f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 0 );. inR
21940 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c eg = pExpr->iCol
21941 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b umn + pParse->ck
21942 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Base;. }els
21943 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 e{. testc
21944 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 ase( (pExpr->fla
21945 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66 29 21 gs & EP_AnyAff)!
21946 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e =0 );. in
21947 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 Reg = sqlite3Exp
21948 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 rCodeGetColumn(p
21949 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 Parse, pExpr->pT
2194a 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 ab,.
2194b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2194c 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c pExpr->iCol
2194d 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 umn, pExpr->iTab
2194e 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 20 le, target,.
2194f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21950 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 pEx
21951 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 pr->flags & EP_A
21952 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a nyAff);. }.
21953 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21954 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 }. case TK_I
21955 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 NTEGER: {.
21956 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70 codeInteger(v, p
21957 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65 74 29 Expr, 0, target)
21958 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
21959 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b }. case TK
2195a 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 _FLOAT: {.
2195b 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
2195c 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 Property(pExpr,
2195d 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a EP_IntValue) );.
2195e 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76 codeReal(v
2195f 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 , pExpr->u.zToke
21960 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 n, 0, target);.
21961 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21962 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 }. case TK_ST
21963 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20 61 73 RING: {. as
21964 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
21965 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 operty(pExpr, EP
21966 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 _IntValue) );.
21967 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21968 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 ddOp4(v, OP_Stri
21969 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c 20 ng8, 0, target,
2196a 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 0, pExpr->u.zTok
2196b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 en, 0);. br
2196c 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
2196d 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 ase TK_NULL: {.
2196e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
2196f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c AddOp2(v, OP_Nul
21970 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 l, 0, target);.
21971 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21972 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
21973 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 _OMIT_BLOB_LITER
21974 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 AL. case TK_B
21975 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 LOB: {. int
21976 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 n;. const
21977 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 char *z;. c
21978 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 har *zBlob;.
21979 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
2197a 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
2197b 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
2197c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
2197d 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b pExpr->u.zToken[
2197e 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 0]=='x' || pExpr
2197f 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 ->u.zToken[0]=='
21980 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 X' );. asse
21981 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f rt( pExpr->u.zTo
21982 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a ken[1]=='\'' );.
21983 20 20 20 20 20 20 7a 20 3d 20 26 70 45 78 70 72 z = &pExpr
21984 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 ->u.zToken[2];.
21985 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 n = sqlite3
21986 53 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3b Strlen30(z) - 1;
21987 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a . assert( z
21988 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 [n]=='\'' );.
21989 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 zBlob = sqlit
2198a 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 e3HexToBlob(sqli
2198b 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 7a 2c te3VdbeDb(v), z,
2198c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 n);. sqlit
2198d 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
2198e 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 OP_Blob, n/2, ta
2198f 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 rget, 0, zBlob,
21990 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 P4_DYNAMIC);.
21991 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
21992 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 #endif. case
21993 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 TK_VARIABLE: {.
21994 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 VdbeOp *pOp
21995 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21996 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
21997 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 (pExpr, EP_IntVa
21998 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61 73 lue) );. as
21999 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a sert( pExpr->u.z
2199a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 Token!=0 );.
2199b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d assert( pExpr-
2199c 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 >u.zToken[0]!=0
2199d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 );. if( pEx
2199e 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d pr->u.zToken[1]=
2199f 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 =0. && (
219a0 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 pOp = sqlite3Vdb
219a1 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 29 2d 3e eGetOp(v, -1))->
219a2 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 opcode==OP_Varia
219a3 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26 26 20 ble. &&
219a4 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d pOp->p1+pOp->p3=
219a5 3d 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a =pExpr->iColumn.
219a6 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d && pOp-
219a7 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 >p2+pOp->p3==tar
219a8 67 65 74 0a 20 20 20 20 20 20 20 20 20 26 26 20 get. &&
219a9 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 pOp->p4.z==0.
219aa 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a ){. /*
219ab 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 If the previous
219ac 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 instruction was
219ad 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 a copy of the p
219ae 72 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a revious unnamed.
219af 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d ** param
219b0 65 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70 72 eter into the pr
219b1 65 76 69 6f 75 73 20 72 65 67 69 73 74 65 72 2c evious register,
219b2 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 then simply inc
219b3 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20 20 rement the.
219b4 20 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 ** repeat cou
219b5 6e 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 nt on the prior
219b6 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74 68 instruction rath
219b7 65 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 er than making a
219b8 20 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 new. **
219b9 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 instruction..
219ba 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
219bb 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 pOp->p3++;.
219bc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
219bd 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
219be 33 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 3(v, OP_Variable
219bf 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e , pExpr->iColumn
219c0 2c 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 , target, 1);.
219c1 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
219c2 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 >u.zToken[1]!=0
219c3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c ){. sql
219c4 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 ite3VdbeChangeP4
219c5 28 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 (v, -1, pExpr->u
219c6 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 .zToken, 0);.
219c7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
219c8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
219c9 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45 }. case TK_RE
219ca 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 GISTER: {.
219cb 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 inReg = pExpr->i
219cc 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 Table;. bre
219cd 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
219ce 73 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 se TK_AS: {.
219cf 20 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c inReg = codeAl
219d0 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 ias(pParse, pExp
219d1 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72 r->iTable, pExpr
219d2 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 ->pLeft, target)
219d3 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 ;. break;.
219d4 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
219d5 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 ITE_OMIT_CAST.
219d6 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 case TK_CAST:
219d7 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 {. /* Expre
219d8 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f ssions of the fo
219d9 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74 rm: CAST(pLeft
219da 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 AS token) */.
219db 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f int aff, to_
219dc 6f 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 op;. inReg
219dd 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
219de 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 eTarget(pParse,
219df 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 pExpr->pLeft, ta
219e0 72 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73 rget);. ass
219e1 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
219e2 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
219e3 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
219e4 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 aff = sqlite3
219e5 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 AffinityType(pEx
219e6 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 pr->u.zToken);.
219e7 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 to_op = aff
219e8 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 - SQLITE_AFF_TE
219e9 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a XT + OP_ToText;.
219ea 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
219eb 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 _op==OP_ToText
219ec 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
219ed 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a _AFF_TEXT );.
219ee 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
219ef 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 _op==OP_ToBlob
219f0 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
219f1 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a _AFF_NONE );.
219f2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
219f3 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 _op==OP_ToNumeri
219f4 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 c || aff!=SQLITE
219f5 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a _AFF_NUMERIC );.
219f6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
219f7 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 _op==OP_ToInt
219f8 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
219f9 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a _AFF_INTEGER );.
219fa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f assert( to
219fb 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 _op==OP_ToReal
219fc 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 || aff!=SQLITE
219fd 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a _AFF_REAL );.
219fe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
219ff 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 to_op==OP_ToText
21a00 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21a01 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f se( to_op==OP_To
21a02 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65 Blob );. te
21a03 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f stcase( to_op==O
21a04 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 P_ToNumeric );.
21a05 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 testcase( t
21a06 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 o_op==OP_ToInt )
21a07 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21a08 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 ( to_op==OP_ToRe
21a09 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 al );. if(
21a0a 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b inReg!=target ){
21a0b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21a0c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21a0d 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 _SCopy, inReg, t
21a0e 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
21a0f 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a inReg = target;.
21a10 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 }. sq
21a11 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
21a12 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 v, to_op, inReg)
21a13 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21a14 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 ( usedAsColumnCa
21a15 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 che(pParse, inRe
21a16 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 g, inReg) );.
21a17 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21a18 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
21a19 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c e(pParse, inReg,
21a1a 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 1);. break
21a1b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
21a1c 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 * SQLITE_OMIT_CA
21a1d 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 ST */. case T
21a1e 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 K_LT:. case T
21a1f 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_LE:. case T
21a20 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54 K_GT:. case T
21a21 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_GE:. case T
21a22 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 K_NE:. case T
21a23 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 K_EQ: {. as
21a24 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f sert( TK_LT==OP_
21a25 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 Lt );. asse
21a26 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 rt( TK_LE==OP_Le
21a27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21a28 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 ( TK_GT==OP_Gt )
21a29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21a2a 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a TK_GE==OP_Ge );.
21a2b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
21a2c 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 _EQ==OP_Eq );.
21a2d 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e assert( TK_N
21a2e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 E==OP_Ne );.
21a2f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21a30 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 TK_LT );. t
21a31 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f estcase( op==TK_
21a32 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 LE );. test
21a33 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 case( op==TK_GT
21a34 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21a35 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a e( op==TK_GE );.
21a36 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21a37 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 op==TK_EQ );.
21a38 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
21a39 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 =TK_NE );.
21a3a 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 codeCompareOpera
21a3b 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 nds(pParse, pExp
21a3c 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 r->pLeft, &r1, &
21a3d 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 regFree1,.
21a3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21a3f 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 pExp
21a40 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 r->pRight, &r2,
21a41 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 ®Free2);.
21a42 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 codeCompare(pP
21a43 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 arse, pExpr->pLe
21a44 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 ft, pExpr->pRigh
21a45 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 t, op,.
21a46 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c r1, r2,
21a47 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 inReg, SQLITE_S
21a48 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74 TOREP2);. t
21a49 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
21a4a 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
21a4b 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
21a4c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
21a4d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
21a4e 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 se TK_IS:. ca
21a4f 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 se TK_ISNOT: {.
21a50 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f testcase( o
21a51 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 p==TK_IS );.
21a52 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21a53 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 TK_ISNOT );.
21a54 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 codeCompareOpe
21a55 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 rands(pParse, pE
21a56 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c xpr->pLeft, &r1,
21a57 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
21a58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21a59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 pE
21a5a 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 xpr->pRight, &r2
21a5b 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 , ®Free2);.
21a5c 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b op = (op==TK
21a5d 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 _IS) ? TK_EQ : T
21a5e 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 K_NE;. code
21a5f 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 Compare(pParse,
21a60 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 pExpr->pLeft, pE
21a61 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c xpr->pRight, op,
21a62 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21a63 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 r1, r2, inReg
21a64 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 , SQLITE_STOREP2
21a65 20 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 | SQLITE_NULLEQ
21a66 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21a67 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 e( regFree1==0 )
21a68 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21a69 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b ( regFree2==0 );
21a6a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21a6b 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21a6c 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b AND:. case TK
21a6d 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b _OR:. case TK
21a6e 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20 _PLUS:. case
21a6f 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73 TK_STAR:. cas
21a70 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 e TK_MINUS:.
21a71 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 case TK_REM:.
21a72 20 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a case TK_BITAND:
21a73 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 . case TK_BIT
21a74 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f OR:. case TK_
21a75 53 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20 SLASH:. case
21a76 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 TK_LSHIFT:. c
21a77 61 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a ase TK_RSHIFT: .
21a78 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 case TK_CONC
21a79 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 AT: {. asse
21a7a 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 rt( TK_AND==OP_A
21a7b 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 nd );. asse
21a7c 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 rt( TK_OR==OP_Or
21a7d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21a7e 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 ( TK_PLUS==OP_Ad
21a7f 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 d );. asser
21a80 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f t( TK_MINUS==OP_
21a81 53 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20 Subtract );.
21a82 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d assert( TK_REM
21a83 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 ==OP_Remainder )
21a84 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21a85 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 TK_BITAND==OP_Bi
21a86 74 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 tAnd );. as
21a87 73 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d sert( TK_BITOR==
21a88 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 OP_BitOr );.
21a89 20 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 assert( TK_SLA
21a8a 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b SH==OP_Divide );
21a8b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 . assert( T
21a8c 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 K_LSHIFT==OP_Shi
21a8d 66 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 ftLeft );.
21a8e 61 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46 assert( TK_RSHIF
21a8f 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74 T==OP_ShiftRight
21a90 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21a91 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f ( TK_CONCAT==OP_
21a92 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 Concat );.
21a93 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21a94 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 _AND );. te
21a95 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f stcase( op==TK_O
21a96 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 R );. testc
21a97 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 ase( op==TK_PLUS
21a98 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21a99 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 se( op==TK_MINUS
21a9a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21a9b 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 se( op==TK_REM )
21a9c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
21a9d 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 ( op==TK_BITAND
21a9e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21a9f 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 e( op==TK_BITOR
21aa0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21aa1 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 e( op==TK_SLASH
21aa2 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
21aa3 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 e( op==TK_LSHIFT
21aa4 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21aa5 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 se( op==TK_RSHIF
21aa6 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
21aa7 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 ase( op==TK_CONC
21aa8 41 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d AT );. r1 =
21aa9 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21aaa 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
21aab 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
21aac 72 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20 ree1);. r2
21aad 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 = sqlite3ExprCod
21aae 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 eTemp(pParse, pE
21aaf 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 xpr->pRight, &re
21ab0 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73 gFree2);. s
21ab1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
21ab2 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 (v, op, r2, r1,
21ab3 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 target);. t
21ab4 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
21ab5 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 1==0 );. te
21ab6 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
21ab7 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 ==0 );. bre
21ab8 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 ak;. }. ca
21ab9 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a se TK_UMINUS: {.
21aba 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 Expr *pLef
21abb 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 t = pExpr->pLeft
21abc 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21abd 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69 pLeft );. i
21abe 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b f( pLeft->op==TK
21abf 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 _FLOAT ){.
21ac0 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 assert( !ExprH
21ac1 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
21ac2 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 , EP_IntValue) )
21ac3 3b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65 ;. codeRe
21ac4 61 6c 28 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a al(v, pLeft->u.z
21ac5 54 6f 6b 65 6e 2c 20 31 2c 20 74 61 72 67 65 74 Token, 1, target
21ac6 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 );. }else i
21ac7 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b f( pLeft->op==TK
21ac8 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20 _INTEGER ){.
21ac9 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 codeInteger(
21aca 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 v, pLeft, 1, tar
21acb 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 get);. }els
21acc 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 72 e{. regFr
21acd 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 ee1 = r1 = sqlit
21ace 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
21acf 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 rse);. sq
21ad0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
21ad1 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
21ad2 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 72 , r1);. r
21ad3 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 2 = sqlite3ExprC
21ad4 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 odeTemp(pParse,
21ad5 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 pExpr->pLeft, &r
21ad6 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 egFree2);.
21ad7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21ad8 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 Op3(v, OP_Subtra
21ad9 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67 ct, r2, r1, targ
21ada 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 et);. tes
21adb 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d tcase( regFree2=
21adc 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 =0 );. }.
21add 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67 inReg = targ
21ade 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b et;. break;
21adf 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21ae0 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 TK_BITNOT:. c
21ae1 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 ase TK_NOT: {.
21ae2 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42 assert( TK_B
21ae3 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 ITNOT==OP_BitNot
21ae4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21ae5 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 ( TK_NOT==OP_Not
21ae6 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
21ae7 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f se( op==TK_BITNO
21ae8 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 T );. testc
21ae9 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 ase( op==TK_NOT
21aea 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
21aeb 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
21aec 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d p(pParse, pExpr-
21aed 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 >pLeft, ®Free
21aee 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 1);. testca
21aef 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 se( regFree1==0
21af0 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d );. inReg =
21af1 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 73 target;. s
21af2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21af3 28 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 (v, op, r1, inRe
21af4 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b g);. break;
21af5 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 . }. case
21af6 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 TK_ISNULL:. c
21af7 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 ase TK_NOTNULL:
21af8 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 {. int addr
21af9 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
21afa 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 TK_ISNULL==OP_Is
21afb 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 Null );. as
21afc 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c sert( TK_NOTNULL
21afd 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a ==OP_NotNull );.
21afe 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
21aff 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b op==TK_ISNULL );
21b00 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21b01 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 op==TK_NOTNULL
21b02 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21b03 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21b04 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72 _Integer, 1, tar
21b05 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d get);. r1 =
21b06 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21b07 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 Temp(pParse, pEx
21b08 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 pr->pLeft, ®F
21b09 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73 ree1);. tes
21b0a 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21b0b 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72 =0 );. addr
21b0c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
21b0d 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b dOp1(v, op, r1);
21b0e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
21b0f 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 beAddOp2(v, OP_A
21b10 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d ddImm, target, -
21b11 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
21b12 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
21b13 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72 addr);. br
21b14 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
21b15 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 ase TK_AGG_FUNCT
21b16 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 ION: {. Agg
21b17 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 Info *pInfo = pE
21b18 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 xpr->pAggInfo;.
21b19 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d if( pInfo==
21b1a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 0 ){. ass
21b1b 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
21b1c 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f perty(pExpr, EP_
21b1d 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 IntValue) );.
21b1e 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
21b1f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 rMsg(pParse, "mi
21b20 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74 suse of aggregat
21b21 65 3a 20 25 73 28 29 22 2c 20 70 45 78 70 72 2d e: %s()", pExpr-
21b22 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 >u.zToken);.
21b23 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21b24 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e inReg = pInfo->
21b25 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 aFunc[pExpr->iAg
21b26 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d g].iMem;. }
21b27 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
21b28 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f }. case TK_
21b29 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 CONST_FUNC:.
21b2a 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e case TK_FUNCTION
21b2b 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 : {. ExprLi
21b2c 73 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 20 st *pFarg;
21b2d 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e 63 /* List of func
21b2e 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a tion arguments *
21b2f 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 72 /. int nFar
21b30 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f g; /
21b31 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e 63 * Number of func
21b32 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a tion arguments *
21b33 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 /. FuncDef
21b34 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 2f *pDef; /
21b35 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 * The function d
21b36 65 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 efinition object
21b37 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 */. int nI
21b38 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d;
21b39 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 /* Length of th
21b3a 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 e function name
21b3b 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 in bytes */.
21b3c 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 const char *zI
21b3d 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 d; /* The
21b3e 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f function name */
21b3f 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 . int const
21b40 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a Mask = 0; /*
21b41 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f Mask of functio
21b42 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 n arguments that
21b43 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f are constant */
21b44 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 . int i;
21b45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
21b46 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f Loop counter */
21b47 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20 . u8 enc =
21b48 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f 2a ENC(db); /*
21b49 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 The text encodi
21b4a 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 ng used by this
21b4b 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 database */.
21b4c 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c CollSeq *pColl
21b4d 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f = 0; /* A co
21b4e 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 llating sequence
21b4f 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 */.. asser
21b50 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 t( !ExprHasPrope
21b51 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
21b52 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 sSelect) );.
21b53 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d testcase( op==
21b54 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b TK_CONST_FUNC );
21b55 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
21b56 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e op==TK_FUNCTION
21b57 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78 );. if( Ex
21b58 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 prHasAnyProperty
21b59 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e (pExpr, EP_Token
21b5a 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 Only) ){.
21b5b 20 70 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 pFarg = 0;.
21b5c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
21b5d 20 70 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e pFarg = pExpr->
21b5e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d x.pList;. }
21b5f 0a 20 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70 . nFarg = p
21b60 46 61 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 Farg ? pFarg->nE
21b61 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 61 xpr : 0;. a
21b62 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
21b63 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21b64 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
21b65 20 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72 zId = pExpr
21b66 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 ->u.zToken;.
21b67 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53 nId = sqlite3S
21b68 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 trlen30(zId);.
21b69 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 pDef = sqlit
21b6a 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 e3FindFunction(d
21b6b 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61 b, zId, nId, nFa
21b6c 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 rg, enc, 0);.
21b6d 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 if( pDef==0 )
21b6e 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21b6f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
21b70 2c 20 22 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 , "unknown funct
21b71 69 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49 ion: %.*s()", nI
21b72 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 d, zId);.
21b73 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
21b74 0a 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70 . /* Attemp
21b75 74 20 61 20 64 69 72 65 63 74 20 69 6d 70 6c 65 t a direct imple
21b76 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 mentation of the
21b77 20 62 75 69 6c 74 2d 69 6e 20 43 4f 41 4c 45 53 built-in COALES
21b78 43 45 28 29 20 61 6e 64 0a 20 20 20 20 20 20 2a CE() and. *
21b79 2a 20 49 46 4e 55 4c 4c 28 29 20 66 75 6e 63 74 * IFNULL() funct
21b7a 69 6f 6e 73 2e 20 20 54 68 69 73 20 61 76 6f 69 ions. This avoi
21b7b 64 73 20 75 6e 6e 65 63 65 73 73 61 72 79 20 65 ds unnecessary e
21b7c 76 61 6c 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20 valation of.
21b7d 20 20 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 ** arguments p
21b7e 61 73 74 20 74 68 65 20 66 69 72 73 74 20 6e 6f ast the first no
21b7f 6e 2d 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e n-NULL argument.
21b80 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
21b81 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 if( pDef->flags
21b82 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f & SQLITE_FUNC_CO
21b83 41 4c 45 53 43 45 20 29 7b 0a 20 20 20 20 20 20 ALESCE ){.
21b84 20 20 69 6e 74 20 65 6e 64 43 6f 61 6c 65 73 63 int endCoalesc
21b85 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d e = sqlite3VdbeM
21b86 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 akeLabel(v);.
21b87 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 46 61 assert( nFa
21b88 72 67 3e 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 rg>=2 );.
21b89 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21b8a 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e (pParse, pFarg->
21b8b 61 5b 30 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 a[0].pExpr, targ
21b8c 65 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 et);. for
21b8d 28 69 3d 31 3b 20 69 3c 6e 46 61 72 67 3b 20 69 (i=1; i<nFarg; i
21b8e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 ++){. s
21b8f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
21b90 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 (v, OP_NotNull,
21b91 74 61 72 67 65 74 2c 20 65 6e 64 43 6f 61 6c 65 target, endCoale
21b92 73 63 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 sce);.
21b93 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
21b94 52 65 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 74 Remove(pParse, t
21b95 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 arget);.
21b96 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
21b97 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a hePush(pParse);.
21b98 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
21b99 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
21b9a 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 , pFarg->a[i].pE
21b9b 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 xpr, target);.
21b9c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21b9d 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 xprCachePop(pPar
21b9e 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 se, 1);.
21b9f 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
21ba0 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
21ba1 6c 28 76 2c 20 65 6e 64 43 6f 61 6c 65 73 63 65 l(v, endCoalesce
21ba2 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b );. break
21ba3 3b 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20 20 ;. }...
21ba4 20 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a 20 if( pFarg ){.
21ba5 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 r1 = sqli
21ba6 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 te3GetTempRange(
21ba7 70 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b 0a pParse, nFarg);.
21ba8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21ba9 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 xprCachePush(pPa
21baa 72 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69 63 rse); /* Tic
21bab 6b 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 ket 2ea2425d34be
21bac 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 */. sqli
21bad 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c te3ExprCodeExprL
21bae 69 73 74 28 70 50 61 72 73 65 2c 20 70 46 61 72 ist(pParse, pFar
21baf 67 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 g, r1, 1);.
21bb0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
21bb1 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
21bb2 29 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20 32 ); /* Ticket 2
21bb3 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a 20 ea2425d34be */.
21bb4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
21bb5 20 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20 20 r1 = 0;.
21bb6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 }.#ifndef SQLI
21bb7 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
21bb8 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50 6f ABLE. /* Po
21bb9 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20 ssibly overload
21bba 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 the function if
21bbb 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 the first argume
21bbc 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 nt is. ** a
21bbd 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 virtual table c
21bbe 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a olumn.. **.
21bbf 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e 66 ** For inf
21bc0 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c 49 ix functions (LI
21bc1 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58 50 KE, GLOB, REGEXP
21bc2 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73 65 , and MATCH) use
21bc3 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 the. ** se
21bc4 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 6e cond argument, n
21bc5 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 61 73 ot the first, as
21bc6 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f the argument to
21bc7 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20 2a test to. *
21bc8 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20 61 * see if it is a
21bc9 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 column in a vir
21bca 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69 tual table. Thi
21bcb 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75 73 s is done becaus
21bcc 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c e. ** the l
21bcd 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 eft operand of i
21bce 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 nfix functions (
21bcf 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20 77 the operand we w
21bd0 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 ant to. **
21bd1 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 control overload
21bd2 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73 20 ing) ends up as
21bd3 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d the second argum
21bd4 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20 20 ent to the.
21bd5 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 ** function. T
21bd6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 41 he expression "A
21bd7 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75 69 glob B" is equi
21bd8 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20 valent to .
21bd9 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e 20 ** "glob(B,A).
21bda 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65 20 We want to use
21bdb 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f 62 the A in "A glob
21bdc 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20 20 B" to test.
21bdd 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69 6f ** for functio
21bde 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20 20 n overloading.
21bdf 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20 42 But we use the B
21be0 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28 42 term in "glob(B
21be1 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 ,A)".. */.
21be2 20 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e 3d if( nFarg>=
21be3 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c 61 2 && (pExpr->fla
21be4 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75 6e gs & EP_InfixFun
21be5 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 c) ){. pD
21be6 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 ef = sqlite3Vtab
21be7 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e OverloadFunction
21be8 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 (db, pDef, nFarg
21be9 2c 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 , pFarg->a[1].pE
21bea 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 xpr);. }els
21beb 65 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29 7b e if( nFarg>0 ){
21bec 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 . pDef =
21bed 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72 6c sqlite3VtabOverl
21bee 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 oadFunction(db,
21bef 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46 61 pDef, nFarg, pFa
21bf0 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b rg->a[0].pExpr);
21bf1 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
21bf2 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
21bf3 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 <nFarg; i++){.
21bf4 20 20 20 20 20 20 69 66 28 20 69 3c 33 32 20 26 if( i<32 &
21bf5 26 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 & sqlite3ExprIsC
21bf6 6f 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e 61 onstant(pFarg->a
21bf7 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20 20 [i].pExpr) ){.
21bf8 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61 73 constMas
21bf9 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20 20 k |= (1<<i);.
21bfa 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
21bfb 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 f( (pDef->flags
21bfc 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 & SQLITE_FUNC_NE
21bfd 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21 70 EDCOLL)!=0 && !p
21bfe 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 Coll ){.
21bff 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 pColl = sqlite
21c00 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 3ExprCollSeq(pPa
21c01 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69 5d rse, pFarg->a[i]
21c02 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 .pExpr);.
21c03 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }. }.
21c04 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67 73 if( pDef->flags
21c05 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e & SQLITE_FUNC_N
21c06 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 EEDCOLL ){.
21c07 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 if( !pColl )
21c08 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c pColl = db->pDfl
21c09 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20 20 tColl; .
21c0a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21c0b 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 4(v, OP_CollSeq,
21c0c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 0, 0, 0, (char
21c0d 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c *)pColl, P4_COLL
21c0e 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 SEQ);. }.
21c0f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21c10 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e 63 ddOp4(v, OP_Func
21c11 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c tion, constMask,
21c12 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20 20 r1, target,.
21c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c14 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 65 66 (char*)pDef
21c15 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20 , P4_FUNCDEF);.
21c16 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21c17 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 ChangeP5(v, (u8)
21c18 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69 66 nFarg);. if
21c19 28 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20 20 ( nFarg ){.
21c1a 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
21c1b 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 eTempRange(pPars
21c1c 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 e, r1, nFarg);.
21c1d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
21c1e 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 ite3ExprCacheAff
21c1f 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 inityChange(pPar
21c20 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a se, r1, nFarg);.
21c21 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
21c22 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
21c23 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a E_OMIT_SUBQUERY.
21c24 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 case TK_EXIS
21c25 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f TS:. case TK_
21c26 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 SELECT: {.
21c27 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b testcase( op==TK
21c28 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 _EXISTS );.
21c29 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 testcase( op==T
21c2a 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 K_SELECT );.
21c2b 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 inReg = sqlite
21c2c 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 70 3CodeSubselect(p
21c2d 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30 2c Parse, pExpr, 0,
21c2e 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0);. break
21c2f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 ;. }. case
21c30 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 TK_IN: {.
21c31 69 6e 74 20 64 65 73 74 49 66 46 61 6c 73 65 20 int destIfFalse
21c32 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
21c33 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
21c34 20 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c 20 int destIfNull
21c35 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
21c36 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
21c37 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
21c38 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
21c39 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 , target);.
21c3a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
21c3b 49 4e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 IN(pParse, pExpr
21c3c 2c 20 64 65 73 74 49 66 46 61 6c 73 65 2c 20 64 , destIfFalse, d
21c3d 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 estIfNull);.
21c3e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
21c3f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
21c40 72 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 r, 1, target);.
21c41 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
21c42 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 ResolveLabel(v,
21c43 64 65 73 74 49 66 46 61 6c 73 65 29 3b 0a 20 20 destIfFalse);.
21c44 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21c45 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 ddOp2(v, OP_AddI
21c46 6d 6d 2c 20 74 61 72 67 65 74 2c 20 30 29 3b 0a mm, target, 0);.
21c47 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
21c48 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
21c49 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 destIfNull);.
21c4a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
21c4b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
21c4c 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 E_OMIT_SUBQUERY
21c4d 2a 2f 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 */... /*.
21c4e 2a 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 ** x BETWEEN
21c4f 79 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 y AND z. **.
21c50 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 ** This is eq
21c51 75 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 uivalent to.
21c52 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d **. ** x>=
21c53 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a y AND x<=z. *
21c54 2a 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 *. ** X is st
21c55 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 ored in pExpr->p
21c56 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 Left.. ** Y i
21c57 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 s stored in pExp
21c58 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 r->pList->a[0].p
21c59 45 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 Expr.. ** Z i
21c5a 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 s stored in pExp
21c5b 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 r->pList->a[1].p
21c5c 45 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 Expr.. */.
21c5d 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e case TK_BETWEEN
21c5e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a : {. Expr *
21c5f 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 pLeft = pExpr->p
21c60 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 Left;. stru
21c61 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d ct ExprList_item
21c62 20 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 *pLItem = pExpr
21c63 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 ->x.pList->a;.
21c64 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 Expr *pRight
21c65 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 = pLItem->pExpr
21c66 3b 0a 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d ;.. codeCom
21c67 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 pareOperands(pPa
21c68 72 73 65 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c rse, pLeft, &r1,
21c69 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 ®Free1,.
21c6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 pR
21c6c 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 ight, &r2, ®F
21c6d 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 ree2);. tes
21c6e 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d tcase( regFree1=
21c6f 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 =0 );. test
21c70 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d case( regFree2==
21c71 30 20 29 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 0 );. r3 =
21c72 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 sqlite3GetTempRe
21c73 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 g(pParse);.
21c74 20 72 34 20 3d 20 73 71 6c 69 74 65 33 47 65 74 r4 = sqlite3Get
21c75 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b TempReg(pParse);
21c76 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 . codeCompa
21c77 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 re(pParse, pLeft
21c78 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c , pRight, OP_Ge,
21c79 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
21c7a 20 20 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 r1, r2, r3, S
21c7b 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a QLITE_STOREP2);.
21c7c 20 20 20 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a pLItem++;.
21c7d 20 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 pRight = p
21c7e 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 LItem->pExpr;.
21c7f 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 sqlite3Relea
21c80 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
21c81 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 , regFree2);.
21c82 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 r2 = sqlite3E
21c83 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
21c84 73 65 2c 20 70 52 69 67 68 74 2c 20 26 72 65 67 se, pRight, ®
21c85 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 74 65 Free2);. te
21c86 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 stcase( regFree2
21c87 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 ==0 );. cod
21c88 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c eCompare(pParse,
21c89 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 pLeft, pRight,
21c8a 4f 50 5f 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 OP_Le, r1, r2, r
21c8b 34 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 4, SQLITE_STOREP
21c8c 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2);. sqlite
21c8d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
21c8e 50 5f 41 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 P_And, r3, r4, t
21c8f 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 arget);. sq
21c90 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
21c91 52 65 67 28 70 50 61 72 73 65 2c 20 72 33 29 3b Reg(pParse, r3);
21c92 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 . sqlite3Re
21c93 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
21c94 72 73 65 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 rse, r4);.
21c95 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 break;. }.
21c96 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 case TK_UPLUS:
21c97 7b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 {. inReg =
21c98 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 sqlite3ExprCodeT
21c99 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 arget(pParse, pE
21c9a 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 xpr->pLeft, targ
21c9b 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b et);. break
21c9c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 ;. }.. cas
21c9d 65 20 54 4b 5f 54 52 49 47 47 45 52 3a 20 7b 0a e TK_TRIGGER: {.
21c9e 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 /* If the
21c9f 6f 70 63 6f 64 65 20 69 73 20 54 4b 5f 54 52 49 opcode is TK_TRI
21ca0 47 47 45 52 2c 20 74 68 65 6e 20 74 68 65 20 65 GGER, then the e
21ca1 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 72 xpression is a r
21ca2 65 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20 2a eference. *
21ca3 2a 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69 6e * to a column in
21ca4 20 74 68 65 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c the new.* or ol
21ca5 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 d.* pseudo-table
21ca6 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 0a 20 s available to.
21ca7 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 ** trigger
21ca8 70 72 6f 67 72 61 6d 73 2e 20 49 6e 20 74 68 69 programs. In thi
21ca9 73 20 63 61 73 65 20 45 78 70 72 2e 69 54 61 62 s case Expr.iTab
21caa 6c 65 20 69 73 20 73 65 74 20 74 6f 20 31 20 66 le is set to 1 f
21cab 6f 72 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 or the. **
21cac 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 new.* pseudo-tab
21cad 6c 65 2c 20 6f 72 20 30 20 66 6f 72 20 74 68 65 le, or 0 for the
21cae 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 old.* pseudo-ta
21caf 62 6c 65 2e 20 45 78 70 72 2e 69 43 6f 6c 75 6d ble. Expr.iColum
21cb0 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73 65 n. ** is se
21cb1 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 t to the column
21cb2 6f 66 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 of the pseudo-ta
21cb3 62 6c 65 20 74 6f 20 72 65 61 64 2c 20 6f 72 20 ble to read, or
21cb4 74 6f 20 2d 31 20 74 6f 0a 20 20 20 20 20 20 2a to -1 to. *
21cb5 2a 20 72 65 61 64 20 74 68 65 20 72 6f 77 69 64 * read the rowid
21cb6 20 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2a field.. **
21cb7 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 65 78 . ** The ex
21cb8 70 72 65 73 73 69 6f 6e 20 69 73 20 69 6d 70 6c pression is impl
21cb9 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 61 6e emented using an
21cba 20 4f 50 5f 50 61 72 61 6d 20 6f 70 63 6f 64 65 OP_Param opcode
21cbb 2e 20 54 68 65 20 70 31 0a 20 20 20 20 20 20 2a . The p1. *
21cbc 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 73 * parameter is s
21cbd 65 74 20 74 6f 20 30 20 66 6f 72 20 61 6e 20 6f et to 0 for an o
21cbe 6c 64 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e ld.rowid referen
21cbf 63 65 2c 20 6f 72 20 74 6f 20 28 69 2b 31 29 0a ce, or to (i+1).
21cc0 20 20 20 20 20 20 2a 2a 20 74 6f 20 72 65 66 65 ** to refe
21cc1 72 65 6e 63 65 20 61 6e 6f 74 68 65 72 20 63 6f rence another co
21cc2 6c 75 6d 6e 20 6f 66 20 74 68 65 20 6f 6c 64 2e lumn of the old.
21cc3 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 * pseudo-table,
21cc4 77 68 65 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 where . **
21cc5 69 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f i is the index o
21cc6 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f f the column. Fo
21cc7 72 20 61 20 6e 65 77 2e 72 6f 77 69 64 20 72 65 r a new.rowid re
21cc8 66 65 72 65 6e 63 65 2c 20 70 31 20 69 73 0a 20 ference, p1 is.
21cc9 20 20 20 20 20 2a 2a 20 73 65 74 20 74 6f 20 28 ** set to (
21cca 6e 2b 31 29 2c 20 77 68 65 72 65 20 6e 20 69 73 n+1), where n is
21ccb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 the number of c
21ccc 6f 6c 75 6d 6e 73 20 69 6e 20 65 61 63 68 20 70 olumns in each p
21ccd 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 20 20 20 seudo-table..
21cce 20 20 20 2a 2a 20 46 6f 72 20 61 20 72 65 66 65 ** For a refe
21ccf 72 65 6e 63 65 20 74 6f 20 61 6e 79 20 6f 74 68 rence to any oth
21cd0 65 72 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 er column in the
21cd1 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f 2d 74 61 new.* pseudo-ta
21cd2 62 6c 65 2c 20 70 31 0a 20 20 20 20 20 20 2a 2a ble, p1. **
21cd3 20 69 73 20 73 65 74 20 74 6f 20 28 6e 2b 32 2b is set to (n+2+
21cd4 69 29 2c 20 77 68 65 72 65 20 6e 20 61 6e 64 20 i), where n and
21cd5 69 20 61 72 65 20 61 73 20 64 65 66 69 6e 65 64 i are as defined
21cd6 20 70 72 65 76 69 6f 75 73 6c 79 2e 20 46 6f 72 previously. For
21cd7 0a 20 20 20 20 20 20 2a 2a 20 65 78 61 6d 70 6c . ** exampl
21cd8 65 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 e, if the table
21cd9 6f 6e 20 77 68 69 63 68 20 74 72 69 67 67 65 72 on which trigger
21cda 73 20 61 72 65 20 62 65 69 6e 67 20 66 69 72 65 s are being fire
21cdb 64 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 d is. ** de
21cdc 63 6c 61 72 65 64 20 61 73 3a 0a 20 20 20 20 20 clared as:.
21cdd 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 43 **. ** C
21cde 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 REATE TABLE t1(a
21cdf 2c 20 62 29 3b 0a 20 20 20 20 20 20 2a 2a 0a 20 , b);. **.
21ce0 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 70 31 20 ** Then p1
21ce1 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 is interpreted a
21ce2 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 s follows:.
21ce3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 **. ** p
21ce4 31 3d 3d 30 20 20 20 2d 3e 20 20 20 20 6f 6c 64 1==0 -> old
21ce5 2e 72 6f 77 69 64 20 20 20 20 20 70 31 3d 3d 33 .rowid p1==3
21ce6 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e 72 6f 77 -> new.row
21ce7 69 64 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 id. ** p1
21ce8 3d 3d 31 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e ==1 -> old.
21ce9 61 20 20 20 20 20 20 20 20 20 70 31 3d 3d 34 20 a p1==4
21cea 20 20 2d 3e 20 20 20 20 6e 65 77 2e 61 0a 20 20 -> new.a.
21ceb 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 32 20 20 ** p1==2
21cec 20 2d 3e 20 20 20 20 6f 6c 64 2e 62 20 20 20 20 -> old.b
21ced 20 20 20 20 20 70 31 3d 3d 35 20 20 20 2d 3e 20 p1==5 ->
21cee 20 20 20 6e 65 77 2e 62 20 20 20 20 20 20 20 0a new.b .
21cef 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 */. T
21cf0 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 45 78 able *pTab = pEx
21cf1 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 pr->pTab;.
21cf2 69 6e 74 20 70 31 20 3d 20 70 45 78 70 72 2d 3e int p1 = pExpr->
21cf3 69 54 61 62 6c 65 20 2a 20 28 70 54 61 62 2d 3e iTable * (pTab->
21cf4 6e 43 6f 6c 2b 31 29 20 2b 20 31 20 2b 20 70 45 nCol+1) + 1 + pE
21cf5 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 xpr->iColumn;..
21cf6 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 assert( pEx
21cf7 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 7c 7c pr->iTable==0 ||
21cf8 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d pExpr->iTable==
21cf9 31 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 1 );. asser
21cfa 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d t( pExpr->iColum
21cfb 6e 3e 3d 2d 31 20 26 26 20 70 45 78 70 72 2d 3e n>=-1 && pExpr->
21cfc 69 43 6f 6c 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 iColumn<pTab->nC
21cfd 6f 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 ol );. asse
21cfe 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c rt( pTab->iPKey<
21cff 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 0 || pExpr->iCol
21d00 75 6d 6e 21 3d 70 54 61 62 2d 3e 69 50 4b 65 79 umn!=pTab->iPKey
21d01 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 );. assert
21d02 28 20 70 31 3e 3d 30 20 26 26 20 70 31 3c 28 70 ( p1>=0 && p1<(p
21d03 54 61 62 2d 3e 6e 43 6f 6c 2a 32 2b 32 29 20 29 Tab->nCol*2+2) )
21d04 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ;.. sqlite3
21d05 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
21d06 5f 50 61 72 61 6d 2c 20 70 31 2c 20 74 61 72 67 _Param, p1, targ
21d07 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 et);. VdbeC
21d08 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 omment((v, "%s.%
21d09 73 20 2d 3e 20 24 25 64 22 2c 0a 20 20 20 20 20 s -> $%d",.
21d0a 20 20 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c (pExpr->iTabl
21d0b 65 20 3f 20 22 6e 65 77 22 20 3a 20 22 6f 6c 64 e ? "new" : "old
21d0c 22 29 2c 0a 20 20 20 20 20 20 20 20 28 70 45 78 "),. (pEx
21d0d 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 3f 20 pr->iColumn<0 ?
21d0e 22 72 6f 77 69 64 22 20 3a 20 70 45 78 70 72 2d "rowid" : pExpr-
21d0f 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 >pTab->aCol[pExp
21d10 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d r->iColumn].zNam
21d11 65 29 2c 0a 20 20 20 20 20 20 20 20 74 61 72 67 e),. targ
21d12 65 74 0a 20 20 20 20 20 20 29 29 3b 0a 0a 20 20 et. ));..
21d13 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f /* If the co
21d14 6c 75 6d 6e 20 68 61 73 20 52 45 41 4c 20 61 66 lumn has REAL af
21d15 66 69 6e 69 74 79 2c 20 69 74 20 6d 61 79 20 63 finity, it may c
21d16 75 72 72 65 6e 74 6c 79 20 62 65 20 73 74 6f 72 urrently be stor
21d17 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20 2a ed as an. *
21d18 2a 20 69 6e 74 65 67 65 72 2e 20 55 73 65 20 4f * integer. Use O
21d19 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 74 P_RealAffinity t
21d1a 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 o make sure it i
21d1b 73 20 72 65 61 6c 6c 79 20 72 65 61 6c 2e 20 20 s really real.
21d1c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 */. if( pEx
21d1d 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 0a pr->iColumn>=0 .
21d1e 20 20 20 20 20 20 20 26 26 20 70 54 61 62 2d 3e && pTab->
21d1f 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c aCol[pExpr->iCol
21d20 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 umn].affinity==S
21d21 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 20 QLITE_AFF_REAL.
21d22 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
21d23 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21d24 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 1(v, OP_RealAffi
21d25 6e 69 74 79 2c 20 74 61 72 67 65 74 29 3b 0a 20 nity, target);.
21d26 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 }. bre
21d27 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 ak;. }...
21d28 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 41 /*. ** Form A
21d29 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 :. ** CASE
21d2a 78 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 x WHEN e1 THEN r
21d2b 31 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 1 WHEN e2 THEN r
21d2c 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 2 ... WHEN eN TH
21d2d 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 EN rN ELSE y END
21d2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 . **. ** F
21d2f 6f 72 6d 20 42 3a 0a 20 20 20 20 2a 2a 20 20 20 orm B:. **
21d30 43 41 53 45 20 57 48 45 4e 20 65 31 20 54 48 45 CASE WHEN e1 THE
21d31 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48 45 N r1 WHEN e2 THE
21d32 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65 4e N r2 ... WHEN eN
21d33 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 THEN rN ELSE y
21d34 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a END. **. *
21d35 2a 20 46 6f 72 6d 20 41 20 69 73 20 63 61 6e 20 * Form A is can
21d36 62 65 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 69 be transformed i
21d37 6e 74 6f 20 74 68 65 20 65 71 75 69 76 61 6c 65 nto the equivale
21d38 6e 74 20 66 6f 72 6d 20 42 20 61 73 20 66 6f 6c nt form B as fol
21d39 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 20 20 20 43 lows:. ** C
21d3a 41 53 45 20 57 48 45 4e 20 78 3d 65 31 20 54 48 ASE WHEN x=e1 TH
21d3b 45 4e 20 72 31 20 57 48 45 4e 20 78 3d 65 32 20 EN r1 WHEN x=e2
21d3c 54 48 45 4e 20 72 32 20 2e 2e 2e 0a 20 20 20 20 THEN r2 ....
21d3d 2a 2a 20 20 20 20 20 20 20 20 57 48 45 4e 20 78 ** WHEN x
21d3e 3d 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 =eN THEN rN ELSE
21d3f 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 y END. **.
21d40 20 20 2a 2a 20 58 20 28 69 66 20 69 74 20 65 78 ** X (if it ex
21d41 69 73 74 73 29 20 69 73 20 69 6e 20 70 45 78 70 ists) is in pExp
21d42 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a r->pLeft.. **
21d43 20 59 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e Y is in pExpr->
21d44 70 52 69 67 68 74 2e 20 20 54 68 65 20 59 20 69 pRight. The Y i
21d45 73 20 61 6c 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e s also optional.
21d46 20 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f If there is no
21d47 0a 20 20 20 20 2a 2a 20 45 4c 53 45 20 63 6c 61 . ** ELSE cla
21d48 75 73 65 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 use and no other
21d49 20 74 65 72 6d 20 6d 61 74 63 68 65 73 2c 20 74 term matches, t
21d4a 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f hen the result o
21d4b 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 70 f the. ** exp
21d4c 72 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a rssion is NULL..
21d4d 20 20 20 20 2a 2a 20 45 69 20 69 73 20 69 6e 20 ** Ei is in
21d4e 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b pExpr->pList->a[
21d4f 69 2a 32 5d 20 61 6e 64 20 52 69 20 69 73 20 70 i*2] and Ri is p
21d50 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 Expr->pList->a[i
21d51 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 *2+1].. **.
21d52 20 20 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 ** The result
21d53 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f of the expressio
21d54 6e 20 69 73 20 74 68 65 20 52 69 20 66 6f 72 20 n is the Ri for
21d55 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 the first matchi
21d56 6e 67 20 45 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 ng Ei,. ** or
21d57 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 if there is no
21d58 6d 61 74 63 68 69 6e 67 20 45 69 2c 20 74 68 65 matching Ei, the
21d59 20 45 4c 53 45 20 74 65 72 6d 20 59 2c 20 6f 72 ELSE term Y, or
21d5a 20 69 66 20 74 68 65 72 65 20 69 73 0a 20 20 20 if there is.
21d5b 20 2a 2a 20 6e 6f 20 45 4c 53 45 20 74 65 72 6d ** no ELSE term
21d5c 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 , NULL.. */.
21d5d 20 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65 default: asse
21d5e 72 74 28 20 6f 70 3d 3d 54 4b 5f 43 41 53 45 20 rt( op==TK_CASE
21d5f 29 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 ); {. int e
21d60 6e 64 4c 61 62 65 6c 3b 20 20 20 20 20 20 20 20 ndLabel;
21d61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21d62 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 65 GOTO label for e
21d63 6e 64 20 6f 66 20 43 41 53 45 20 73 74 6d 74 20 nd of CASE stmt
21d64 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78 */. int nex
21d65 74 43 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 tCase;
21d66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f /* GO
21d67 54 4f 20 6c 61 62 65 6c 20 66 6f 72 20 6e 65 78 TO label for nex
21d68 74 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f t WHEN clause */
21d69 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72 . int nExpr
21d6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
21d6b 20 20 20 20 20 20 20 20 20 2f 2a 20 32 78 20 6e /* 2x n
21d6c 75 6d 62 65 72 20 6f 66 20 57 48 45 4e 20 74 65 umber of WHEN te
21d6d 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 rms */. int
21d6e 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
21d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
21d70 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a * Loop counter *
21d71 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 /. ExprList
21d72 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 20 *pEList;
21d73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 /* Lis
21d74 74 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 t of WHEN terms
21d75 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 */. struct
21d76 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 ExprList_item *a
21d77 4c 69 73 74 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 Listelem; /* Ar
21d78 72 61 79 20 6f 66 20 57 48 45 4e 20 74 65 72 6d ray of WHEN term
21d79 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 s */. Expr
21d7a 6f 70 43 6f 6d 70 61 72 65 3b 20 20 20 20 20 20 opCompare;
21d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
21d7c 54 68 65 20 58 3d 3d 45 69 20 65 78 70 72 65 73 The X==Ei expres
21d7d 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 sion */. Ex
21d7e 70 72 20 63 61 63 68 65 58 3b 20 20 20 20 20 20 pr cacheX;
21d7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21d80 2f 2a 20 43 61 63 68 65 64 20 65 78 70 72 65 73 /* Cached expres
21d81 73 69 6f 6e 20 58 20 2a 2f 0a 20 20 20 20 20 20 sion X */.
21d82 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 Expr *pX;
21d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21d84 20 20 2f 2a 20 54 68 65 20 58 20 65 78 70 72 65 /* The X expre
21d85 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 ssion */. E
21d86 78 70 72 20 2a 70 54 65 73 74 20 3d 20 30 3b 20 xpr *pTest = 0;
21d87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21d88 20 2f 2a 20 58 3d 3d 45 69 20 28 66 6f 72 6d 20 /* X==Ei (form
21d89 41 29 20 6f 72 20 6a 75 73 74 20 45 69 20 28 66 A) or just Ei (f
21d8a 6f 72 6d 20 42 29 20 2a 2f 0a 20 20 20 20 20 20 orm B) */.
21d8b 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20 69 43 VVA_ONLY( int iC
21d8c 61 63 68 65 4c 65 76 65 6c 20 3d 20 70 50 61 72 acheLevel = pPar
21d8d 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b se->iCacheLevel;
21d8e 20 29 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 ).. assert
21d8f 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
21d90 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
21d91 53 65 6c 65 63 74 29 20 26 26 20 70 45 78 70 72 Select) && pExpr
21d92 2d 3e 78 2e 70 4c 69 73 74 20 29 3b 0a 20 20 20 ->x.pList );.
21d93 20 20 20 61 73 73 65 72 74 28 28 70 45 78 70 72 assert((pExpr
21d94 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 ->x.pList->nExpr
21d95 20 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20 % 2) == 0);.
21d96 20 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d assert(pExpr-
21d97 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 >x.pList->nExpr
21d98 3e 20 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 > 0);. pELi
21d99 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c st = pExpr->x.pL
21d9a 69 73 74 3b 0a 20 20 20 20 20 20 61 4c 69 73 74 ist;. aList
21d9b 65 6c 65 6d 20 3d 20 70 45 4c 69 73 74 2d 3e 61 elem = pEList->a
21d9c 3b 0a 20 20 20 20 20 20 6e 45 78 70 72 20 3d 20 ;. nExpr =
21d9d 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 pEList->nExpr;.
21d9e 20 20 20 20 20 65 6e 64 4c 61 62 65 6c 20 3d 20 endLabel =
21d9f 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
21da0 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 abel(v);. i
21da1 66 28 20 28 70 58 20 3d 20 70 45 78 70 72 2d 3e f( (pX = pExpr->
21da2 70 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 20 20 pLeft)!=0 ){.
21da3 20 20 20 20 20 63 61 63 68 65 58 20 3d 20 2a 70 cacheX = *p
21da4 58 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 X;. testc
21da5 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f ase( pX->op==TK_
21da6 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 COLUMN );.
21da7 20 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e testcase( pX->
21da8 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 op==TK_REGISTER
21da9 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 );. cache
21daa 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 X.iTable = sqlit
21dab 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 e3ExprCodeTemp(p
21dac 50 61 72 73 65 2c 20 70 58 2c 20 26 72 65 67 46 Parse, pX, ®F
21dad 72 65 65 31 29 3b 0a 20 20 20 20 20 20 20 20 74 ree1);. t
21dae 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 estcase( regFree
21daf 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 1==0 );.
21db0 63 61 63 68 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 cacheX.op = TK_R
21db1 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 20 EGISTER;.
21db2 20 6f 70 43 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 opCompare.op =
21db3 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20 20 20 6f TK_EQ;. o
21db4 70 43 6f 6d 70 61 72 65 2e 70 4c 65 66 74 20 3d pCompare.pLeft =
21db5 20 26 63 61 63 68 65 58 3b 0a 20 20 20 20 20 20 &cacheX;.
21db6 20 20 70 54 65 73 74 20 3d 20 26 6f 70 43 6f 6d pTest = &opCom
21db7 70 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 pare;. }.
21db8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e for(i=0; i<n
21db9 45 78 70 72 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 Expr; i=i+2){.
21dba 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
21dbb 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 rCachePush(pPars
21dbc 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
21dbd 70 58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 pX ){.
21dbe 61 73 73 65 72 74 28 20 70 54 65 73 74 21 3d 30 assert( pTest!=0
21dbf 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 70 );. op
21dc0 43 6f 6d 70 61 72 65 2e 70 52 69 67 68 74 20 3d Compare.pRight =
21dc1 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 aListelem[i].pE
21dc2 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c xpr;. }el
21dc3 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 se{. pT
21dc4 65 73 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b est = aListelem[
21dc5 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 i].pExpr;.
21dc6 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 65 78 74 }. next
21dc7 43 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56 64 Case = sqlite3Vd
21dc8 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a beMakeLabel(v);.
21dc9 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
21dca 28 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f ( pTest->op==TK_
21dcb 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 COLUMN );.
21dcc 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
21dcd 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 alse(pParse, pTe
21dce 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53 51 st, nextCase, SQ
21dcf 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 LITE_JUMPIFNULL)
21dd0 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 ;. testca
21dd1 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b se( aListelem[i+
21dd2 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 1].pExpr->op==TK
21dd3 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 _COLUMN );.
21dd4 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c 69 testcase( aLi
21dd5 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 stelem[i+1].pExp
21dd6 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 r->op==TK_REGIST
21dd7 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 ER );. sq
21dd8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 lite3ExprCode(pP
21dd9 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d 5b arse, aListelem[
21dda 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72 67 i+1].pExpr, targ
21ddb 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c et);. sql
21ddc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21ddd 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e , OP_Goto, 0, en
21dde 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20 20 dLabel);.
21ddf 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
21de0 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b ePop(pParse, 1);
21de1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
21de2 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
21de3 28 76 2c 20 6e 65 78 74 43 61 73 65 29 3b 0a 20 (v, nextCase);.
21de4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 }. if(
21de5 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 pExpr->pRight )
21de6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21de7 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 3ExprCachePush(p
21de8 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
21de9 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
21dea 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 pParse, pExpr->p
21deb 52 69 67 68 74 2c 20 74 61 72 67 65 74 29 3b 0a Right, target);.
21dec 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
21ded 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 xprCachePop(pPar
21dee 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 65 se, 1);. }e
21def 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
21df0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21df1 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 , OP_Null, 0, ta
21df2 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 rget);. }.
21df3 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d assert( db-
21df4 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c >mallocFailed ||
21df5 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 pParse->nErr>0
21df6 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 . || p
21df7 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 Parse->iCacheLev
21df8 65 6c 3d 3d 69 43 61 63 68 65 4c 65 76 65 6c 20 el==iCacheLevel
21df9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
21dfa 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
21dfb 28 76 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 (v, endLabel);.
21dfc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
21dfd 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 }.#ifndef SQLITE
21dfe 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 _OMIT_TRIGGER.
21dff 20 20 63 61 73 65 20 54 4b 5f 52 41 49 53 45 3a case TK_RAISE:
21e00 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 {. assert(
21e01 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 pExpr->affinity
21e02 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 0a 20 ==OE_Rollback .
21e03 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 || pEx
21e04 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 pr->affinity==OE
21e05 5f 41 62 6f 72 74 0a 20 20 20 20 20 20 20 20 20 _Abort.
21e06 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66 66 69 || pExpr->affi
21e07 6e 69 74 79 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 nity==OE_Fail.
21e08 20 20 20 20 20 20 20 20 20 7c 7c 20 70 45 78 70 || pExp
21e09 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f r->affinity==OE_
21e0a 49 67 6e 6f 72 65 0a 20 20 20 20 20 20 29 3b 0a Ignore. );.
21e0b 20 20 20 20 20 20 69 66 28 20 21 70 50 61 72 73 if( !pPars
21e0c 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 e->pTriggerTab )
21e0d 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
21e0e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 3ErrorMsg(pParse
21e0f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
21e10 20 20 20 20 20 20 20 20 20 22 52 41 49 53 45 28 "RAISE(
21e11 29 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 ) may only be us
21e12 65 64 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 ed within a trig
21e13 67 65 72 2d 70 72 6f 67 72 61 6d 22 29 3b 0a 20 ger-program");.
21e14 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b return 0;
21e15 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
21e16 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 f( pExpr->affini
21e17 74 79 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a ty==OE_Abort ){.
21e18 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d sqlite3M
21e19 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b ayAbort(pParse);
21e1a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
21e1b 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
21e1c 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
21e1d 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 P_IntValue) );.
21e1e 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e if( pExpr->
21e1f 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e affinity==OE_Ign
21e20 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 ore ){. s
21e21 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
21e22 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 2c (. v,
21e23 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 OP_Halt, SQLITE
21e24 5f 4f 4b 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 _OK, OE_Ignore,
21e25 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 0, pExpr->u.zTok
21e26 65 6e 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c en,0);. }el
21e27 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 se{. sqli
21e28 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e te3HaltConstrain
21e29 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d t(pParse, pExpr-
21e2a 3e 61 66 66 69 6e 69 74 79 2c 20 70 45 78 70 72 >affinity, pExpr
21e2b 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a ->u.zToken, 0);.
21e2c 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 62 }.. b
21e2d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 reak;. }.#end
21e2e 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 if. }. sqlite3
21e2f 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
21e30 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 Parse, regFree1)
21e31 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 ;. sqlite3Relea
21e32 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 seTempReg(pParse
21e33 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 , regFree2);. r
21e34 65 74 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a eturn inReg;.}..
21e35 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 /*.** Generate c
21e36 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 ode to evaluate
21e37 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e an expression an
21e38 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 d store the resu
21e39 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 lts.** into a re
21e3a 67 69 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 gister. Return
21e3b 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d the register num
21e3c 62 65 72 20 77 68 65 72 65 20 74 68 65 20 72 65 ber where the re
21e3d 73 75 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f sults.** are sto
21e3e 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 red..**.** If th
21e3f 65 20 72 65 67 69 73 74 65 72 20 69 73 20 61 20 e register is a
21e40 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 temporary regist
21e41 65 72 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 er that can be d
21e42 65 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 eallocated,.** t
21e43 68 65 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 hen write its nu
21e44 6d 62 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e mber into *pReg.
21e45 20 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 If the result
21e46 72 65 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a register is not.
21e47 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 ** a temporary,
21e48 74 68 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 then set *pReg t
21e49 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 o zero..*/.SQLIT
21e4a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 E_PRIVATE int sq
21e4b 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d lite3ExprCodeTem
21e4c 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c p(Parse *pParse,
21e4d 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e Expr *pExpr, in
21e4e 74 20 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 t *pReg){. int
21e4f 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 r1 = sqlite3GetT
21e50 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a empReg(pParse);.
21e51 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 int r2 = sqlit
21e52 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 e3ExprCodeTarget
21e53 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 (pParse, pExpr,
21e54 72 31 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 r1);. if( r2==r
21e55 31 20 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 1 ){. *pReg =
21e56 20 72 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 r1;. }else{.
21e57 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
21e58 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
21e59 72 31 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d r1);. *pReg =
21e5a 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 0;. }. return
21e5b 20 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 r2;.}../*.** Ge
21e5c 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 nerate code that
21e5d 20 77 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 will evaluate e
21e5e 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 xpression pExpr
21e5f 61 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a and store the.**
21e60 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 results in regi
21e61 73 74 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 ster target. Th
21e62 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 e results are gu
21e63 61 72 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 aranteed to appe
21e64 61 72 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 ar.** in registe
21e65 72 20 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c r target..*/.SQL
21e66 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 ITE_PRIVATE int
21e67 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 sqlite3ExprCode(
21e68 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 Parse *pParse, E
21e69 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 xpr *pExpr, int
21e6a 74 61 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 target){. int i
21e6b 6e 52 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 nReg;.. assert(
21e6c 20 74 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 target>0 && tar
21e6d 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 get<=pParse->nMe
21e6e 6d 20 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 m );. inReg = s
21e6f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 qlite3ExprCodeTa
21e70 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 rget(pParse, pEx
21e71 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 pr, target);. a
21e72 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 ssert( pParse->p
21e73 56 64 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e Vdbe || pParse->
21e74 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
21e75 20 29 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 );. if( inReg!
21e76 3d 74 61 72 67 65 74 20 26 26 20 70 50 61 72 73 =target && pPars
21e77 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 e->pVdbe ){.
21e78 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
21e79 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 2(pParse->pVdbe,
21e7a 20 4f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 OP_SCopy, inReg
21e7b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 , target);. }.
21e7c 20 72 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a return target;.
21e7d 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
21e7e 65 20 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c e code that eval
21e7f 75 74 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 utes the given e
21e80 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 xpression and pu
21e81 74 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a ts the result.**
21e82 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 in register tar
21e83 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 get..**.** Also
21e84 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 make a copy of t
21e85 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 he expression re
21e86 73 75 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 sults into anoth
21e87 65 72 20 22 63 61 63 68 65 22 20 72 65 67 69 73 er "cache"